63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
||
"""SSH:在板子上执行 service_ctl.sh neko start(拉镜像可能较久)。"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
|
||
import paramiko
|
||
|
||
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("/")
|
||
|
||
CMD = (
|
||
f"export LC_ALL=C.UTF-8 LANG=C.UTF-8; "
|
||
f"cd {REMOTE_BASE} && bash scripts/linux/service_ctl.sh neko start"
|
||
)
|
||
|
||
|
||
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=25,
|
||
)
|
||
sock = jump.get_transport().open_channel("direct-tcpip", (HOST, SSH_PORT), ("127.0.0.1", 0), timeout=25)
|
||
c = paramiko.SSHClient()
|
||
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
try:
|
||
c.connect(HOST, port=SSH_PORT, username=USER, password=PASS, sock=sock, timeout=25)
|
||
except Exception as e:
|
||
print(f"SSH failed: {e}", file=sys.stderr)
|
||
return 2
|
||
_, stdout, stderr = c.exec_command(CMD, timeout=600)
|
||
out = stdout.read().decode("utf-8", errors="replace")
|
||
err = stderr.read().decode("utf-8", errors="replace")
|
||
code = stdout.channel.recv_exit_status()
|
||
c.close()
|
||
if jump:
|
||
jump.close()
|
||
print(out, end="")
|
||
if err.strip():
|
||
print("--- stderr ---", file=sys.stderr)
|
||
print(err, file=sys.stderr, end="")
|
||
return code
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|