分类: 系统运维
2017-05-27 19:54:02
软件环境
#创建目录
mkdir /opt/svncode/gitpro/
#获取源码,需要先上传服务器publicKey到gitlab账户中
git clone ssh://git地址
创建自动化脚本
该脚本可以放到任意目录
!/usr/bin/python
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
from fabric.contrib.files import *
import time,os
i='machtalk@10.112.80.2:22'
env.hosts=[i]
env.roledefs = {
'all':[i]
}
env.passwords = {
i:'GVk2qWUTW7kP2hwQ'
}
env.profile='qa'
env.project_basepath='/opt/svncode/gitpro/'
env.project_parent_dir = env.project_basepath+'nextparent/'
env.postfix='-1.0-SNAPSHOT.jar'
env.deploy_basepath='/opt/machtalk/pro/'
env.exec_dir_name = 'exec'
def project_update(dir_path):
with lcd(dir_path):
local("git fetch origin")
local("git checkout gome")
local("git pull origin gome")
def project_package(dir_path, project_name):
with lcd(dir_path):
local("mvn clean package -Dmaven.test.skip=true -P "+env.profile+","+project_name)
def project_init_dir(deploy_path):
if not exists(env.deploy_basepath):
run("mkdir -p "+env.deploy_basepath+"logs")
if not exists("/data/logs"):
run("sudo mkdir -p /data")
run("sudo ln -s "+(env.deploy_basepath+"logs")+" /data/logs")
if not exists(deploy_path):
run("mkdir -p "+deploy_path+env.exec_dir_name)
run("mkdir -p "+deploy_path+"bak")
def project_put(source_dir, target_dir):
result = put(source_dir, target_dir)
if result.failed and not confirm("put file failed, Continue[Y/NO]?"):
abort("aborting!")
def project_port_stop(port):
with settings(warn_only=True):
result = run("sudo netstat -lnp|grep "+port+"|awk '{print $7}'|awk -F '/' '{print $1}'")
if result:
run("kill "+result)
def project_clean(exec_dir):
run('rm -rf '+exec_dir+'/*')
def project_start(exec_dir, jar_name):
with settings(warn_only=True):
with cd(exec_dir):
run("nohup jar -jar "+jar_name+" >dev/null 2>&1 &")
def project_deploy(project_name, port):
deploy_dir = env.deploy_basepath + project_name+'/'
exec_dir = deploy_dir + env.exec_dir_name
jar_name = project_name + env.postfix
project_init_dir(deploy_dir)
project_update(env.project_parent_dir)
project_package(env.project_parent_dir, project_name)
project_port_stop(port)
project_clean(exec_dir)
project_put(env.project_parent_dir+project_name+'/target/'+jar_name, exec_dir+'')
project_start(exec_dir, jar_name)
@task
@roles('all')
def commonservice():
project_deploy('xcloud-commonservice', '2003')
@task
@roles('all')
def personal_openapi():
project_deploy('xcloud-personal-openapi', '2009')
@task
@roles('all')
def apigateway():
project_deploy('xcloud-apigateway', '2002')
@task
@roles('all')
def appservice():
project_deploy('xcloud-appservice', '2007')
@task
@roles('all')
def authcenter():
project_deploy('xcloud-authcenter', '2008')
@task
@roles('all')
def deviceservice():
project_deploy('xcloud-deviceservice', '2005')
@task
@roles('all')
def discoveryserver():
project_deploy('xcloud-discoveryserver', '2000')
@task
@roles('all')
def servicemonitor():
project_deploy('xcloud-servicemonitor', '2001')
@task
@roles('all')
def userservice():
project_deploy('xcloud-userservice', '2004')
fab list
#输出结果
Available commands:
apigateway
appservice
authcenter
commonservice
deviceservice
discoveryserver
personal_openapi
servicemonitor
userservice
jenkins & fabric脚本管理
以部署 apigateway微服务为例
cd fabfile.py的目录 && fab apigateway