|
|
@@ -13,10 +13,6 @@ settings = get_settings()
|
|
|
def _get_ssh_prefix() -> list[str]:
|
|
|
"""构建 ssh/scp 命令前缀,支持密钥或密码登录。"""
|
|
|
prefix = ["-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10"]
|
|
|
- if settings.compute_node_ssh_key:
|
|
|
- prefix.extend(["-i", settings.compute_node_ssh_key])
|
|
|
- elif settings.compute_node_ssh_password:
|
|
|
- prefix = ["sshpass", "-p", settings.compute_node_ssh_password] + prefix
|
|
|
return prefix
|
|
|
|
|
|
|
|
|
@@ -26,17 +22,23 @@ def ssh_exec(cmd: str, timeout: int | None = None) -> tuple[int, str, str]:
|
|
|
raise RuntimeError("未配置算力节点(compute_node_host 为空)")
|
|
|
|
|
|
target = f"{settings.compute_node_ssh_user}@{settings.compute_node_host}"
|
|
|
- ssh_cmd = [
|
|
|
+ ssh_args = [
|
|
|
"ssh", *_get_ssh_prefix(),
|
|
|
"-p", str(settings.compute_node_ssh_port),
|
|
|
target,
|
|
|
cmd,
|
|
|
]
|
|
|
|
|
|
+ # sshpass 需要包裹 ssh 命令,而不是作为 ssh 的参数
|
|
|
+ if settings.compute_node_ssh_key:
|
|
|
+ ssh_args = ["ssh", "-i", settings.compute_node_ssh_key] + ssh_args[1:]
|
|
|
+ elif settings.compute_node_ssh_password:
|
|
|
+ ssh_args = ["sshpass", "-p", settings.compute_node_ssh_password] + ssh_args
|
|
|
+
|
|
|
timeout = timeout or settings.compute_node_ssh_timeout
|
|
|
try:
|
|
|
proc = subprocess.run(
|
|
|
- ssh_cmd,
|
|
|
+ ssh_args,
|
|
|
capture_output=True,
|
|
|
text=True,
|
|
|
timeout=timeout,
|