说明
部署时需要执行linux命令,可以使用shell脚本。但我更希望全程部署使用python实现,因此有了此文,作为记录。
参考文章:
python3模拟实现xshell远程执行linux命令的方法
代码
# -*- coding: UTF-8 -*-
import os
import time
import paramiko
class MyLinux(object):
# 通过IP, 用户名,密码,超时时间初始化一个远程Linux主机
def __init__(self, ip, username, password, timeout=30):
self.ip = ip
self.username = username
self.password = password
self.timeout = timeout
# transport和chanel
self.t = ''
self.chan = ''
# 链接失败的重试次数
self.try_times = 3
# 调用该方法连接远程主机
def connect(self):
while True:
# 连接过程中可能会抛出异常,比如网络不通、链接超时
try:
self.t = paramiko.Transport(sock=(self.ip, 22))
self.t.connect(username=self.username, password=self.password)
self.chan = self.t.open_session()
self.chan.settimeout(self.timeout)
self.chan.get_pty()
self.chan.invoke_shell()
# 如果没有抛出异常说明连接成功,直接返回
print('连接%s成功' % self.ip)
# 接收到的网络数据解码为str
return
except Exception as e1:
if self.try_times != 0:
print('连接%s失败,进行重试' % self.ip)
self.try_times -= 1
else:
print('重试3次失败,结束程序')
exit(1)
# 断开连接
def close(self):
# 如果要运行服务,不能关闭连接
self.chan.close()
self.t.close()
# 发送要执行的命令
def send(self, cmd):
cmd += '\r'
# 发送要执行的命令
self.chan.send(cmd)
# 回显很长的命令可能执行较久,通过循环分批次取回回显,执行成功返回true,失败返回false
result = ''
while True:
time.sleep(0.5)
ret = self.chan.recv(65535)
result += ret.decode('utf-8')
if len(ret) < 65535:
break
print(result.replace(result.split("\n")[-1], "").strip("\n"))
def upload_file(self, upload_files, upload_path):
"""
上传文件
:param upload_files: 上传文件路径 例如:/tmp/test.py
:param upload_path: 上传到目标路径 例如:/tmp/test_new.py
:return:
"""
try:
tran = paramiko.Transport(sock=(self.ip, self.port))
tran.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(tran)
result = sftp.put(upload_files, upload_path)
return True if result else False
except Exception as ex:
print(ex)
tran.close()
finally:
tran.close()
def replace_file_cmd(self, from_file, to_file):
"""替换文件"""
cmd = f"cp {from_file} {to_file}"
self.send(cmd)
def replace_file(self, deploy_dir, project_from_file, project_to_file):
"""部署路径下的文件替换"""
from_file = os.path.join(deploy_dir, project_from_file)
to_file = os.path.join(deploy_dir, project_to_file)
self.replace_file_cmd(from_file, to_file)
def replace_text_file(self, from_text, to_text, file):
"""替换文件中的文本"""
cmd = f"sed -e 's/{from_text}/{to_text}/g' {file} "
self.send(cmd)
def my_deploy():
host = MyLinux('172.27.11.200', 'rst', 'rst.2020') # 传入Ip,用户名,密码
host.connect()
# 根据8001端口号杀死服务进程
host.send(" ps -ef | grep 8001 | grep -v grep | cut -c 9-16 | xargs kill -s 9")
host.send("conda activate django ")
host.send("cd /forest_monitor ")
host.send("nohup python manage.py runserver 0:8001 &")
# 如果要运行服务,不能关闭连接
# host.close()
if __name__ == '__main__':
my_deploy()