65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
|
|
import paramiko, time
|
||
|
|
|
||
|
|
NAS_HOST = "100.85.152.112"
|
||
|
|
NAS_PORT = 22
|
||
|
|
NAS_USER = "simon"
|
||
|
|
NAS_PASS = "Nas82829330"
|
||
|
|
|
||
|
|
def exec_and_read(c, cmd, wait=3):
|
||
|
|
stdin, stdout, stderr = c.exec_command(cmd)
|
||
|
|
time.sleep(wait)
|
||
|
|
while not stdout.channel.exit_status_ready():
|
||
|
|
time.sleep(0.2)
|
||
|
|
out = stdout.read().decode("utf-8", errors="replace")
|
||
|
|
return out
|
||
|
|
|
||
|
|
def main():
|
||
|
|
c = paramiko.SSHClient()
|
||
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
|
|
c.connect(NAS_HOST, NAS_PORT, NAS_USER, NAS_PASS, timeout=15)
|
||
|
|
|
||
|
|
path = "/volume1/docker/gaokao-portal/backend"
|
||
|
|
|
||
|
|
# 1. 检查 seed_data.py 文件类型
|
||
|
|
out = exec_and_read(c, f"file {path}/seed_data.py", wait=3)
|
||
|
|
print(f" 文件类型: {out}")
|
||
|
|
|
||
|
|
# 2. 删除错误的 gzip 文件,重新上传正确的 seed_data.py
|
||
|
|
print(" 重新上传正确的 seed_data.py...")
|
||
|
|
|
||
|
|
# 用之前修改好的本地文件上传
|
||
|
|
local_file = r"C:\Users\simon\WorkBuddy\2026-05-20-15-22-53\deploy\backend\seed_data.py"
|
||
|
|
|
||
|
|
# 读取本地文件内容并上传
|
||
|
|
with open(local_file, "r", encoding="utf-8") as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
# 通过 echo 写入(处理特殊字符)
|
||
|
|
import base64
|
||
|
|
b64 = base64.b64encode(content.encode("utf-8")).decode("ascii")
|
||
|
|
|
||
|
|
# 上传
|
||
|
|
cmd = f"echo '{b64}' | base64 -d > {path}/seed_data.py"
|
||
|
|
exec_and_read(c, cmd, wait=3)
|
||
|
|
print(" 上传完成")
|
||
|
|
|
||
|
|
# 验证文件
|
||
|
|
out = exec_and_read(c, f"head -5 {path}/seed_data.py", wait=3)
|
||
|
|
print(f" 验证: {out[:200]}")
|
||
|
|
|
||
|
|
# 运行 seed
|
||
|
|
docker = "echo 'Nas82829330' | sudo -S /usr/local/bin/docker"
|
||
|
|
exec_and_read(c, f"cd {path} && python3 seed_data.py", wait=5)
|
||
|
|
|
||
|
|
# 重启后端
|
||
|
|
exec_and_read(c, f"{docker} restart gaokao-portal-backend-1", wait=3)
|
||
|
|
time.sleep(8)
|
||
|
|
|
||
|
|
# 检查问卷
|
||
|
|
out = exec_and_read(c, "curl -s http://localhost:8080/api/questionnaires", wait=3)
|
||
|
|
print(f"\n问卷结果: {out[:400]}")
|
||
|
|
|
||
|
|
c.close()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|