Fabric 更新SQL, JAVA, PHP
import os
from fabric.api import env, roles, run, execute, cd
import pymysql
basepath = r'172.16.78.122:/data/share/public/teamshare/04build/1.1.2.2/'
# server2 ={
# 'chameleon-callback': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'chameleon-callback-0.0.1-SNAPSHOT.jar',
# 'remotepath':os.path.join(basepath, 'java/chameleon-callback/'),
# 'destpath':'/opt/chameleon/resource/async_callback/'
# },
# 'chameleon-compute':{
# 'sqlname':'chameleon_compute.sql',
# 'dbname':'chameleon_compute',
# 'packagename':'chameleon-compute-0.0.1-SNAPSHOT.jar',
# 'remotepath':os.path.join(basepath, 'java/chameleon-compute/'),
# 'destpath':''
# },
# 'chameleon-gateway': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-network': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-product': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-stats':{
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-user': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-volume': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'fc-gateway': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# }
#
# server3 = {
# 'chameleon-message':{
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-cvg':{
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-monitor':{
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'vm-gateway':{
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'chameleon-cloudedit': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'approval': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'cvg-approval': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'platform':{
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'xs-gateway': {
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# },
# 'web':{
# 'sqlname':'',
# 'dbname':'',
# 'packagename':'',
# 'remotepath':'',
# 'destpath':''
# }
# }
sql = {
'server2':{
#'chameleon-callback': '',
'chameleon-compute':'chameleon_compute.sql',
#'chameleon-gateway': '',
'chameleon-network': 'chameleon_network.sql',
'chameleon-product': 'chameleon_product.sql',
'chameleon-stats': 'chameleon_stats.sql',
'chameleon-user': 'chameleon_user.sql',
'chameleon-volume': 'chameleon_volume.sql',
#'fc-gateway': 'fusion-sphere-gateway-0.1.0.jar',
},
'server3':{
'chameleon-message': 'chameleon_message.sql',
#'chameleon-cvg': '',
'chameleon-monitor': 'chameleon_monitor.sql',
#'vm-gateway': '',
'chameleon-cloudedit': 'chameleon_cloudedit.sql',
'approval': 'chameleon_approval.sql',
#'cvg-approval': '',
'platform': 'chameleon_platform.sql',
#'xs-gateway': 'chameleon_xengateway.sql',
#'web':''
}
}
dbname = {
'server2':{
#'chameleon-callback': '',
'chameleon-compute':'chameleon_compute',
#'chameleon-gateway': '',
'chameleon-network': 'chameleon_network',
'chameleon-product': 'chameleon_product',
'chameleon-stats': 'chameleon_stats',
'chameleon-user': 'chameleon_user',
'chameleon-volume': 'chameleon_volume',
#'fc-gateway': 'fusion-sphere-gateway-0.1.0.jar',
},
'server3':{
'chameleon-message': 'chameleon_message',
#'chameleon-cvg': '',
'chameleon-monitor': 'chameleon_monitor',
#'vm-gateway': '',
'chameleon-cloudedit': 'chameleon_cloudedit',
'approval': 'chameleon_approval',
#'cvg-approval': '',
'platform': 'chameleon_platform',
#'xs-gateway': 'xengateway',
#'web':''
}
}
filenames = {
'server2':{
'chameleon-callback': 'chameleon-callback-0.0.1-SNAPSHOT.jar',
'chameleon-compute':'chameleon-compute-0.0.1-SNAPSHOT.jar',
'chameleon-gateway': 'chameleon-gateway-0.0.1-SNAPSHOT.jar',
'chameleon-network': 'chameleon-network-0.0.1-SNAPSHOT.jar',
'chameleon-product': 'chameleon-product-0.0.1-SNAPSHOT.jar',
'chameleon-stats': 'chameleon-stats-0.0.1-SNAPSHOT.jar',
'chameleon-user': 'chameleon-user-0.0.1-SNAPSHOT.jar',
'chameleon-volume': 'chameleon-volume-0.0.1-SNAPSHOT.jar',
'fc-gateway': 'fusion-sphere-gateway-0.1.0.jar'},
'server3':{
'chameleon-message': 'chameleon-message-1.3.9.tar.gz',
'chameleon-cvg': 'chameleon-cvg-0.0.1-SNAPSHOT.jar',
'chameleon-monitor': 'chameleon-monitor-0.0.1-SNAPSHOT.jar',
'vm-gateway': 'vmware-server-gateway-0.1.0.jar',
'chameleon-cloudedit': 'chameleon-cloudedit-0.0.1-SNAPSHOT.tar.gz',
# 'approval': 'approval.tar.gz',
# 'cvg-approval': 'cvg-approval.tar.gz',
# 'platform': 'platform.tar.gz',
# 'xs-gateway': 'xenGateway.zip',
#'web':''
}
}
remotepath = {
'server2':{
'chameleon-callback': '172.16.78.122:/root/chameleon/chameleon-async-callback/',
'chameleon-compute': '172.16.78.122:/root/chameleon/chameleon-compute/',
'chameleon-gateway': '172.16.78.122:/root/chameleon/chameleon-gateway/',
'chameleon-network': '172.16.78.122:/root/chameleon/chameleon-network/',
'chameleon-product': '172.16.78.122:/root/chameleon/chameleon-product/',
'chameleon-stats': '172.16.78.122:/root/chameleon/chameleon-stats/',
'chameleon-user': '172.16.78.122:/root/chameleon/chameleon-user/',
'chameleon-volume': '172.16.78.122:/root/chameleon/chameleon-volume/',
'fc-gateway': os.path.join(basepath, 'java/fc-gateway/')},
'server3':{
'chameleon-message': '172.16.78.122:/root/chameleon/chameleon-message/',
'chameleon-cvg': '172.16.78.122:/root/chameleon/chameleon-cvg/',
'chameleon-monitor': '172.16.78.122:/root/chameleon/chameleon-monitor/',
'vm-gateway': os.path.join(basepath, 'java/vm-gateway/'),
'chameleon-cloudedit':'172.16.78.122:/root/chameleon/chameleon-cloudedit/',
# 'approval': os.path.join(basepath, 'php/approval/'),
# 'cvg-approval': os.path.join(basepath, 'php/cvg-approval/'),
# 'platform': os.path.join(basepath, 'php/platform/'),
# 'xs-gateway': os.path.join(basepath, 'php/xs-gateway/'),
#'web':'web/',
}
}
destpath = {
'server2':{
'chameleon-callback': '/opt/chameleon/resource/async_callback/',
'chameleon-compute': '/opt/chameleon/resource/compute/',
'chameleon-gateway': '/opt/chameleon/resource/gateway/',
'chameleon-network': '/opt/chameleon/resource/network/',
'chameleon-product': '/opt/chameleon/product/',
'chameleon-stats': '/opt/chameleon/stats/',
'chameleon-user': '/opt/chameleon/user/',
'chameleon-volume': '/opt/chameleon/resource/volume/',
'fc-gateway': '/opt/chameleon/resource/fusionsphere/'},
'server3':{
'chameleon-message': '/opt/chameleon/message/',
'chameleon-cvg': '/opt/chameleon/cvg/',
'chameleon-monitor': '/opt/chameleon/monitor/',
'vm-gateway': '/opt/chameleon/vmware/',
'chameleon-cloudedit':'/opt/chameleon/cloudedit/',
# 'approval': '/var/www/html/',
# 'cvg-approval': '/var/www/html/',
# 'platform': '/var/www/html/',
# 'xs-gateway': '/var/www/html/',
'web':'/var/www/html/'
}
}
env.roledefs = {
'role1':['root@172.16.78.71:22'],
'role2':['root@172.16.78.72:22'],
'role3':['root@172.16.78.73:22']
}
env.passwords={
'root@172.16.78.71:22':'1qazxsw2',
'root@172.16.78.72:22':'1qazxsw2',
'root@172.16.78.73:22':'1qazxsw2'
}
def kill_java(jpsfile):
jps_list = run('jps -q')
print(jps_list,file = open('%s.txt' %(jpsfile),'w+'))
for i in open('%s.txt' %(jpsfile),'r').readlines():
if i.strip() != '':
try:
run('kill %s'%(i) )
except:
print('%s is not killed'%(i.strip()) )
def del_old_files(server):
for i in destpath[server].keys():
try:
if i != 'web':
run('rm %s' %(os.path.join(destpath[server][i],filenames[server][i])))
print('%s is deleted' %(filenames[server][i]))
except:
#print(err)
print('%s is not deleted or alrady deleted before' % (filenames[server][i]))
def copy_java_start(server):
for i in remotepath[server].keys():
try:
run('sshpass -p test2017 scp root@%s %s' %(os.path.join(remotepath[server][i],filenames[server][i]),destpath[server][i]))
print('%s is copied'%(filenames[server][i]),file=open('copy.txt','a+'))
with cd('%s' %(destpath[server][i])):
run('sh start.sh')
except:
print('%s is not copyied and started' %(filenames[server][i]))
@roles('role1')
def update_database():
#drop database
for i in dict(dbname['server2'],**dbname['server3']).values():
print(i)
try:
run('mysql -h172.16.78.71 -P3306 -uroot -p1qazxsw2 --default-character-set=utf8 -e "drop database %s"' %i)
#drop_database(dict(dbname['server2'],**dbname['server3'])[i])
except:
print('%s is not removed' %i)
try:
run('sshpass -p test2017 scp -r root@%s /root/'%os.path.join(basepath,'java/sql'))
run('sshpass -p test2017 scp -r root@%s /root/'%os.path.join(basepath, 'php/sql'))
except:
print('sql is not copied')
for i in dict(sql['server2'],**sql['server3']).values():
try:
run('mysql -h172.16.78.71 -P3306 -uroot -p1qazxsw2 --default-character-set=utf8 -e "source %s"' %(os.path.join('/root/sql/',i)))
except:
print('%s is not updated'%i)
@roles('role2')
def update_java():
#kill all java process
kill_java('jps_62')
#delete all old java files
del_old_files('server2')
#copy all java files and start it by sh
copy_java_start('server2')
@roles('role3')
def update_java_php():
#kill all java process
kill_java('jps_63')
# delete all old java files
del_old_files('server3')
#copy all java,php files and start it by sh
for i in remotepath['server3'].keys():
try:
run('sshpass -p test2017 scp root@%s %s' %(os.path.join(remotepath['server3'][i],filenames['server3'][i]),destpath['server3'][i]))
print('%s is copied'%(filenames['server3'][i]),file=open('copy.txt','a+'))
except:
print('%s is not copied' % (filenames['server3'][i]))
if i in[ 'chameleon-cvg', 'chameleon-monitor', 'vm-gateway']:
try:
with cd('%s' % (destpath['server3'][i])):
run('sh start.sh')
print('%s is stared'%(filenames['server3'][i]),file=open('start.txt','a+'))
except:
print('%s is not started' %(filenames['server3'][i]))
elif i in ['chameleon-message', 'chameleon-cloudedit']:
try:
with cd('%s' %destpath['server3'][i]):
run('rm -rf config/ lib/ logs/ ms_message.pid reload.sh sql/ LOG_PATH_IS_UNDEFINED/ ms_cloudedit.pid')
run('tar -xzvf %s -C %s' %(filenames['server3'][i], destpath['server3'][i]))
run('sh start.sh')
print('%s is stared' % (filenames['server3'][i]), file=open('start.txt', 'a+'))
except :
#print(err)
print('%s is not started' % (filenames['server3'][i]))
# elif i in ['approval', 'cvg-approval', 'platform']:
# try:
# with cd('%s'%destpath['server3'][i]):
# run('tar -xzvf %s -C %s' %(filenames['server3'][i],destpath['server3'][i]))
# print('%s is copied and start automatically' %(filenames['server3'][i]), file=open('start.txt', 'a+'))
# except:
# pass
# elif i in ['xs-gateway']:
# try:
# with cd('%s'%destpath['server3'][i]):
# run('unzip -f %s -d %s' %(filenames['server3'][i],destpath['server3'][i]))
# print('%s is copied and started automatically' %(filenames['server3'][i]), file=open('start.txt', 'a+'))
# except:
# pass
try:
run('sshpass -p test2017 scp -r root@%s %s' %(os.path.join(basepath,'web'),destpath['server3'][i]))
print('web is copied', file=open('start.txt', 'a+'))
except:
print('web is not copied', file=open('start.txt', 'a+'))
def task():
#execute(update_database)
#execute(update_java)
execute(update_java_php)
if __name__ == '__main__':
os.system('fab task')