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()
Python脚本上xxx.WAR包
该博客内容涉及使用Python的paramiko库实现自动化SSH连接到远程服务器,进行文件上传、文件夹检查、文件备份、TOMCAT进程管理以及服务状态监测。主要流程包括:上传WAR文件、检查文件夹、备份旧版本、停止TOMCAT服务、启动新版本服务以及健康检查,实现了高效且可靠的自动化部署流程。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.9
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

被折叠的 条评论
为什么被折叠?



