ticket-py/yes24/dispatcher.py
2024-04-30 17:47:01 +08:00

105 lines
3.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import paramiko
import _thread
import xlrd
import time
import json
dispatcher_conf = open("dispatcher.json", encoding='utf-8')
dispatcher = json.load(dispatcher_conf)
# 编译后的可执行文件,进程名
localfile_bin = dispatcher["localfile_bin"]
# 需要分发的配套配置文件
localpath_conf = dispatcher["localpath_conf"]
position_conf = dispatcher["position_conf"]
# 机器ip信息最好是内网地址
ipFile = dispatcher["ipFile"]
# 每台机子配置账号数量账号起始位置为0
usernum_for_perip = dispatcher["usernum_for_perip"]
hostsuc = set()
hosterr = set()
def sendFile(hostname, username, password):
scp = paramiko.Transport((hostname))
scp.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(scp)
print(hostname + "登陆成功,正在分发程序...")
sftp.put(localfile_bin, localfile_bin)
sftp.put(localpath_conf, localpath_conf)
files = sftp.listdir()
for f in files:
if f == localfile_bin:
print(hostname + "部署成功...")
break
scp.close()
def start(hostname, username, password, offset):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=username,
password=password, look_for_keys=False)
print(hostname + "登陆成功,正启动...")
exec_command(ssh, "echo \""+str(list(range(offset, offset +
usernum_for_perip)))+"\" > " + position_conf)
exec_command(ssh, "chmod +x " + localfile_bin)
ssh.exec_command("./" + localfile_bin + " & >/dev/null")
time.sleep(1)
psinfo = exec_command(ssh, "ps -e|grep " + localfile_bin)
if (psinfo.decode('utf-8').find(localfile_bin) != -1):
hostsuc.add(hostname)
print(hostname + ":已启动")
else:
hosterr.add(hostname)
print(hostname + ":启动失败")
ssh.close()
def stopandclean(hostname, username, password):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=username,
password=password, look_for_keys=False)
print(hostname + "登陆成功,正在执行清理...")
exec_command(ssh, "ps -e|grep "+localfile_bin +
" |awk '{print $1}' |sort -r|xargs kill -9")
exec_command(ssh, "rm -rf logs;mkdir logs;rm -rf captchas;mkdir captchas")
exec_command(ssh, "rm -f "+localfile_bin)
print(hostname + "登陆成功,正在清理完成...")
ssh.close()
def exec_command(ssh, command):
stdin, stdout, stderr = ssh.exec_command(command)
return stdout.read()
def dispacth(hostname, username, password, offset):
try:
stopandclean(hostname, username, password)
sendFile(hostname, username, password)
start(hostname, username, password, offset)
except (TimeoutError, IOError) as err:
hosterr.add(hostname)
print(hostname + "部署异常...", err)
wb = xlrd.open_workbook(filename=ipFile)
sheet = wb.sheet_by_index(0)
for i in range(0, sheet.nrows):
hostname = sheet.cell_value(i, 0)
username = sheet.cell_value(i, 1)
password = sheet.cell_value(i, 2)
_thread.start_new_thread(
dispacth, (hostname, username, password, i * usernum_for_perip + 1, ))
while((len(hostsuc) + len(hosterr)) != sheet.nrows):
time.sleep(1)
if len(hosterr) > 0:
print("部署完成,但"+str(hosterr)+"运行失败")
else:
print("部署完成,全部正常运行")