Python脚本上xxx.WAR包

import paramiko
import sys
import time
import threading


class automaticThread(threading.Thread):
    def __init__(self, host, port, username, password, localFilePath, tempFilePath):
        threading.Thread.__init__(self)
        self.tempFilePath = tempFilePath
        self.localFilePath = localFilePath
        self.host = host
        self.port = port
        self.username = username
        self.password = password

    def run(self):
        transport = paramiko.Transport((self.host, int(self.port)))
        transport.connect(username=self.username, password=self.password)
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=self.host, port=self.port, username=self.username, password=self.password)
        print('【', self.host, '】', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "服务器开始上传xxx.WAR文件")
        sftpFileStartTime = time.time()
        sftp = ssh.open_sftp()
        sftp.put(self.localFilePath, self.tempFilePath, callback=progress_bar)
        sftp.close()
        sftpFileEndTime = time.time()
        print('【', self.host, '】', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "结束上传xxx.WAR文件,耗时:",
              (sftpFileEndTime - sftpFileStartTime), "秒")
        # 执行命令 判断automatic文件夹是否存在
        backupsFile = '/home/xxx/automatic/' + time.strftime("%Y%m%d", time.localtime())
        stdin, stdout, stderr = ssh.exec_command('[ -f ' + backupsFile + '/xxx.war ] && echo OK')
        # 结果放到stdout中,如果有错误将放到stderr中
        backupsCheckFile = stdout.read().decode()
        if "OK" != backupsCheckFile:
            # 不管有没有先删除一下今天的文件夹
            ssh.exec_command('rm -rf ' + backupsFile)
            # 创建文件夹 并且备份生成包
            ssh.exec_command('mkdir ' + backupsFile)
            ssh.exec_command('cp /home/xxx/apache-tomcat-9.0.46/webapps/xxx.war ' + backupsCheckFile + 'xxx.war')
        print('【', self.host, '】', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "xxx.WAR备份完成")
        # 查找TOMCAT进程ID
        stdin, stdout, stderr = ssh.exec_command("ps -ef | grep tomcat | grep -v grep | awk '{print $2}'")
        tomcatSID = stdout.read().decode()
        print('【', self.host, '】', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "查找TOMCAT进程ID" + tomcatSID)
        # 杀掉TOMCAT进程
        ssh.exec_command("kill -9 " + tomcatSID)
        print('【', self.host, '】', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "杀掉TOMCAT进程")
        # 执行命令 移动上线包至TOMCAT
        ssh.exec_command('mv xxx.war /home/xxx/apache-tomcat-9.0.46/webapps/xxx.war')
        ssh.exec_command('cd /home/xxx/apache-tomcat-9.0.46/bin;./startup.sh')
        print('【', self.host, '】', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), 'TOMCAT服务已启动,判读是否可以正常访问')

        checkStart = 1
        while checkStart == 1:
            stdin, stdout, stderr = ssh.exec_command(
                'curl -I -m 10 -o /dev/null -s -w %{http_code} ' + self.host + ':8080/xxx/index.html')
            checkStartCheck = stdout.read().decode().strip()
            if not checkStartCheck == '200' or checkStart == '302':
                print('【', self.host, '】', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                      "暂未启动成功,距离下一次判断间隔为:20S")
                time.sleep(10)
            else:
                checkStart = 2
        else:
            print('【', self.host, '】', "启动成功")
        # 关闭连接
        ssh.close()


def progress_bar(transferred, toBeTransferred, suffix=''):
    bar_len = 100
    filled_len = int(round(bar_len * transferred / float(toBeTransferred)))
    percents = round(100.0 * transferred / float(toBeTransferred), 1)
    bar = '\033[32;1m%s\033[0m' % '=' * filled_len + '-' * (bar_len - filled_len)
    sys.stdout.write('[%s] %s%s %s\r' % (bar, '\033[32;1m%s\033[0m' % percents, '%', suffix))
    sys.stdout.flush()


if __name__ == '__main__':
    host1 = 'xxx.xxx.xxx.xxx'
    port1 = 22
    username1 = 'xxx'
    password1 = 'xxx'
    localFilePath1 = "D:/xxx.war"
    serverFilePath1 = "xxx.war"
    thread1 = automaticThread(host1, port1, username1, password1, localFilePath1, serverFilePath1)
    thread1.start()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值