114 lines
4.3 KiB
Python
114 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""新 git clone 后:修正属主、创建 .venv、安装 requirements(需与 deploy_live_paramiko 相同环境变量)。"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
|
||
try:
|
||
import paramiko
|
||
except ImportError:
|
||
print("请先安装: pip install paramiko", file=sys.stderr)
|
||
sys.exit(1)
|
||
|
||
HOST = os.environ.get("LIVE_DEPLOY_HOST", "192.168.31.56")
|
||
USER = os.environ.get("LIVE_DEPLOY_USER", "orangepi")
|
||
PASS = os.environ.get("LIVE_DEPLOY_PASS", "orangepi")
|
||
SSH_PORT = int(os.environ.get("LIVE_DEPLOY_PORT", os.environ.get("LIVE_SSH_PORT", "22")))
|
||
JUMP_HOST = os.environ.get("LIVE_DEPLOY_JUMP_HOST", "")
|
||
JUMP_PORT = int(os.environ.get("LIVE_DEPLOY_JUMP_PORT", "22"))
|
||
JUMP_USER = os.environ.get("LIVE_DEPLOY_JUMP_USER", "")
|
||
JUMP_PASS = os.environ.get("LIVE_DEPLOY_JUMP_PASS", "")
|
||
REMOTE_BASE = os.environ.get("LIVE_DEPLOY_PATH", "/home/orangepi/d2ypp2-src").rstrip("/")
|
||
|
||
|
||
def main() -> int:
|
||
sock = None
|
||
jump = None
|
||
if JUMP_HOST:
|
||
jump = paramiko.SSHClient()
|
||
jump.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
jump.connect(
|
||
JUMP_HOST,
|
||
port=JUMP_PORT,
|
||
username=JUMP_USER or USER,
|
||
password=JUMP_PASS or PASS,
|
||
timeout=30,
|
||
banner_timeout=40,
|
||
auth_timeout=30,
|
||
)
|
||
sock = jump.get_transport().open_channel("direct-tcpip", (HOST, SSH_PORT), ("127.0.0.1", 0), timeout=30)
|
||
c = paramiko.SSHClient()
|
||
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
c.connect(HOST, port=SSH_PORT, username=USER, password=PASS, sock=sock, timeout=30, banner_timeout=40, auth_timeout=30)
|
||
try:
|
||
def run(title: str, cmd: str, timeout: int = 600, stdin_text: str | None = None) -> int:
|
||
print(f"\n=== {title} ===")
|
||
wrapped = (
|
||
"export LC_ALL=C.UTF-8 LANG=C.UTF-8; "
|
||
f"cd {REMOTE_BASE} 2>/dev/null || exit 9; "
|
||
+ cmd
|
||
)
|
||
stdin, stdout, stderr = c.exec_command(wrapped, timeout=timeout, get_pty=False)
|
||
if stdin_text is not None:
|
||
stdin.write(stdin_text)
|
||
stdin.flush()
|
||
try:
|
||
stdin.channel.shutdown_write()
|
||
except Exception:
|
||
pass
|
||
out = (stdout.read() or b"").decode("utf-8", "replace")
|
||
err = (stderr.read() or b"").decode("utf-8", "replace")
|
||
code = stdout.channel.recv_exit_status()
|
||
if out.strip():
|
||
print(out.rstrip())
|
||
if err.strip():
|
||
print(err.rstrip(), file=sys.stderr)
|
||
print(f"exit={code}")
|
||
return code
|
||
|
||
code = run(
|
||
"chown (root 克隆后必做)",
|
||
f"sudo -S -p '' chown -R {USER}:{USER} {REMOTE_BASE} && echo chown_ok",
|
||
timeout=60,
|
||
stdin_text=f"{PASS}\n",
|
||
)
|
||
if code != 0:
|
||
return 1
|
||
|
||
run("chmod +x 常用脚本", "chmod +x start.sh web.sh dev.sh 2>/dev/null; chmod +x scripts/launch.py 2>/dev/null; echo chmod_ok", timeout=20)
|
||
|
||
venv_cmd = (
|
||
f"if [ -x .venv/bin/pip ]; then echo venv_exists; "
|
||
f"else python3 -m venv .venv && echo venv_created; fi && "
|
||
f".venv/bin/pip install -q --upgrade pip && "
|
||
f".venv/bin/pip install -r requirements.txt"
|
||
)
|
||
code = run("venv + pip install -r requirements.txt", venv_cmd, timeout=900)
|
||
if code != 0:
|
||
return 2
|
||
run(
|
||
"restore runtime dirs to live user",
|
||
(
|
||
"if [ -f scripts/linux/lib/common.sh ]; then "
|
||
". scripts/linux/lib/common.sh && load_env && ensure_runtime_dirs_owned_by_live; "
|
||
"else "
|
||
"mkdir -p config logs downloads backup backup_config .pm2 .process_runner services/neko services/neko/data && "
|
||
"chown -R live:live config logs downloads backup backup_config .pm2 .process_runner services/neko 2>/dev/null || true; "
|
||
"fi"
|
||
),
|
||
timeout=180,
|
||
)
|
||
|
||
print("\n完成。接着在本机执行: python tools/deploy_live_paramiko.py")
|
||
return 0
|
||
finally:
|
||
c.close()
|
||
if jump:
|
||
jump.close()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|