正式对外发布的python版本_python 之自动发布版本

#!/usr/local/bin/python3#coding:utf-8

#====================================================#Author: chang - EMail:[email protected]#Last modified: 2017-5-17#Filename: updateversion.py#Description: file upload ,base paramiko, sys, time#blog:http://www.cnblogs.com/changbo#====================================================

"""1,close tomcat

2,bakup website

3,delete old website

4,start tomcat"""

importparamikoimporttimeimportsys

websitelist= [‘api‘]

iplist= [‘10.0.0.2‘, ‘10.0.0.149‘, ‘10.0.0.122‘]

accpwd= {iplist[0]: ‘abc-123‘, iplist[1]: ‘abc-123‘, iplist[2]: ‘abc-123‘}

basefile= ‘/usr/local/tomcat‘localpath= ‘123.pdf‘targetpath= ‘%s/webapps/%s‘ %(basefile, websitelist[0])classSSHSession:def __init__(self, host, pwd, username=‘root‘, port=22):

self.host=host

self.port=port

self.username=username

self.pwd=pwddefconnect(self):

transport=paramiko.Transport((self.host, self.port))

transport.connect(username=self.username, password=self.pwd)

self.__transport =transportdefclose(self):

self.__transport.close()defshutdown(self):

ssh=paramiko.SSHClient()

ssh._transport= self.__transport

print("[*] tomcat will be shutdown ....")

stdin, stdout, stderr= ssh.exec_command(basefile + "/bin/shutdown.sh")

time.sleep(2)

stdin, stdout, stderr= ssh.exec_command("ps -axu|grep tomcat |grep -v grep|awk ‘{print $2}‘")

tomcatpid=stdout.read().decode()if tomcatpid !=0:print(‘>>> Tomcat already shutdown!‘)else:print(‘>>> Tomcat not shutdown complete! will kill the tomcat pid‘)

ssh.exec_command("kill -9" +tomcatpid)print(‘>>> The tomcat pid already killed!‘)defstartapp(self):

ssh=paramiko.SSHClient()

ssh._transport= self.__transport

print("[*] Tomcat will be start, please check tomcat log....")

stdin, stdout, stderr= ssh.exec_command(basefile + "/bin/start.sh")print(stdout.read().decode())defbackup(self):

ssh=paramiko.SSHClient()

ssh._transport= self.__transport

print(‘[*] Bakup old site now ,It will take a few minutes .....‘)

ssh.exec_command(‘"Time=`date +%m%d%H%M`‘

‘&& cd /usr/local/tomcat/webapps‘

‘&& tar czf /home/websitebakup/webapps$Time.tar.gz‘ +websitelist[0])print(‘>>> The web site backup complete!‘)def progress_bar(self, transferred, toBeTransferred, suffix=‘‘):

bar_len= 60filled_len= int(round(bar_len * transferred /float(toBeTransferred)))

percents= round(100.0 * transferred / float(toBeTransferred), 1)

bar= ‘=‘ * filled_len + ‘-‘ * (bar_len -filled_len)

sys.stdout.write(‘upload [%s] %s%s ...%s\n‘ % (bar, percents, ‘%‘, suffix))

sys.stdout.flush()defuploadfile(self, local_path, target_path):

ssh=paramiko.SSHClient()

ssh._transport= self.__transport

print(‘[*] Delete old site now ,It will take a few minutes .....‘)

ssh.exec_command(‘mv -f /usr/local/tomcat/webapps/‘ + websitelist[0] + ‘/tmp‘)print(‘>>> The web site delete complete!\n‘)print(‘[*] Clean tomcat web cache now ....‘)

ssh.exec_command("mv -f" + basefile + "/temp/* /tmp && mv -f" + basefile + "work/Catalina/localhost/* /tmp")print(‘>>> Tomcat web cache already cleaned!‘)print("[*] Upload website file to remote server...")

sftp= paramiko.SFTPClient.from_transport(self.__transport)

sftp.put(local_path, target_path, callback=self.progress_bar)if __name__ == ‘__main__‘:

i=iplist[0]try:

ssh=SSHSession(i, accpwd[i])

ssh.connect()

ssh.uploadfile(localpath, targetpath)

ssh.close()exceptException as e:print(e)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>