fixed mass deploy
This commit is contained in:
+31
-22
@@ -453,33 +453,36 @@ class DeployBackend:
|
||||
return s, if_index
|
||||
|
||||
def accept_host_key(self, mac, ip):
|
||||
"""Возвращает fingerprint для ЭТОГО mac; не пишет в общий cfg (параллельный деплой)."""
|
||||
if self.cfg["auth"] != "password":
|
||||
return
|
||||
return None
|
||||
cached = self.hostkeys.get(mac)
|
||||
if cached:
|
||||
self.cfg["_hostkey"] = cached.strip()
|
||||
return
|
||||
return cached.strip()
|
||||
if self.cfg.get("host_key"):
|
||||
self.cfg["_hostkey"] = self.cfg["host_key"].strip()
|
||||
self.hostkeys.set(mac, self.cfg["_hostkey"])
|
||||
return
|
||||
cmd = [self.cfg["_plink"], "-batch", "-pw", self.cfg["device_password"], f"{self.cfg['device_user']}@{ip}", "exit"]
|
||||
key = self.cfg["host_key"].strip()
|
||||
self.hostkeys.set(mac, key)
|
||||
return key
|
||||
cmd = [
|
||||
self.cfg["_plink"], "-batch", "-pw", self.cfg["device_password"],
|
||||
f"{self.cfg['device_user']}@{ip}", "exit",
|
||||
]
|
||||
try:
|
||||
_, out = run(cmd, timeout=30)
|
||||
except Exception:
|
||||
return
|
||||
return None
|
||||
for line in out.splitlines():
|
||||
line = line.strip()
|
||||
if line.startswith("ssh-") and "SHA256:" in line:
|
||||
self.cfg["_hostkey"] = line
|
||||
self.hostkeys.set(mac, line)
|
||||
return
|
||||
return line
|
||||
return None
|
||||
|
||||
def build_copy_cmd(self, ip, local_path, remote_path):
|
||||
def build_copy_cmd(self, ip, local_path, remote_path, hostkey=None):
|
||||
if self.cfg["auth"] == "password":
|
||||
cmd = [self.cfg["_pscp"], "-batch", "-pw", self.cfg["device_password"]]
|
||||
if self.cfg.get("_hostkey"):
|
||||
cmd += ["-hostkey", self.cfg["_hostkey"]]
|
||||
if hostkey:
|
||||
cmd += ["-hostkey", hostkey]
|
||||
cmd += [local_path, f"{self.cfg['device_user']}@{ip}:{remote_path}"]
|
||||
return cmd
|
||||
return [
|
||||
@@ -487,11 +490,11 @@ class DeployBackend:
|
||||
"-i", self.cfg["device_key_path"], local_path, f"{self.cfg['device_user']}@{ip}:{remote_path}",
|
||||
]
|
||||
|
||||
def build_ssh_cmd(self, ip, remote_cmd):
|
||||
def build_ssh_cmd(self, ip, remote_cmd, hostkey=None):
|
||||
if self.cfg["auth"] == "password":
|
||||
cmd = [self.cfg["_plink"], "-batch", "-pw", self.cfg["device_password"]]
|
||||
if self.cfg.get("_hostkey"):
|
||||
cmd += ["-hostkey", self.cfg["_hostkey"]]
|
||||
if hostkey:
|
||||
cmd += ["-hostkey", hostkey]
|
||||
cmd += [f"{self.cfg['device_user']}@{ip}", remote_cmd]
|
||||
return cmd
|
||||
return [
|
||||
@@ -506,12 +509,15 @@ class DeployBackend:
|
||||
self.update_status(mac, state="ОШИБКА", detail="SSH не поднялся за отведённое время")
|
||||
return
|
||||
self.update_status(mac, state="hostkey", detail="получаю SSH fingerprint...")
|
||||
self.accept_host_key(mac, ip)
|
||||
hostkey = self.accept_host_key(mac, ip)
|
||||
if self.cfg["auth"] == "password" and not hostkey:
|
||||
self.update_status(mac, state="ОШИБКА", detail="не удалось получить SSH host key")
|
||||
return
|
||||
|
||||
bundle = self.cfg["_bundle_path"]
|
||||
remote_tgz = "/tmp/btdeploy.tgz"
|
||||
self.update_status(mac, state="заливаю", detail=f"копирую {os.path.basename(bundle)}...")
|
||||
rc, out = run(self.build_copy_cmd(ip, bundle, remote_tgz), timeout=300)
|
||||
rc, out = run(self.build_copy_cmd(ip, bundle, remote_tgz, hostkey=hostkey), timeout=300)
|
||||
if rc != 0:
|
||||
write_log(mac, out)
|
||||
self.update_status(mac, state="ОШИБКА", detail=f"копирование не удалось: {out[-200:]}")
|
||||
@@ -525,7 +531,10 @@ class DeployBackend:
|
||||
f"mv /tmp/btdeploy/config/network.json /tmp/btdeploy/config/network.json.sshdeploy; fi && "
|
||||
f"{sp}bash /tmp/btdeploy/device-install.sh"
|
||||
)
|
||||
rc, out = run(self.build_ssh_cmd(ip, remote_cmd), timeout=self.cfg.get("install_timeout_seconds", 600))
|
||||
rc, out = run(
|
||||
self.build_ssh_cmd(ip, remote_cmd, hostkey=hostkey),
|
||||
timeout=self.cfg.get("install_timeout_seconds", 600),
|
||||
)
|
||||
if "INSTALL-OK" not in out:
|
||||
log_path = write_log(mac, out)
|
||||
tail = (out.strip().splitlines() or ["нет вывода"])[-1][:160]
|
||||
@@ -540,14 +549,14 @@ class DeployBackend:
|
||||
f"{sp}cp /tmp/network.json /opt/buttontask/config/network.json && "
|
||||
f"{sp}chown buttontask:buttontask /opt/buttontask/config/network.json || true && rm -f /tmp/network.json"
|
||||
)
|
||||
rc, out = run(self.build_ssh_cmd(ip, write_net), timeout=60)
|
||||
rc, out = run(self.build_ssh_cmd(ip, write_net, hostkey=hostkey), timeout=60)
|
||||
if rc != 0:
|
||||
write_log(mac, out)
|
||||
self.update_status(mac, state="ОШИБКА", detail=f"network.json: {out[-200:]}")
|
||||
return
|
||||
|
||||
try:
|
||||
run(self.build_ssh_cmd(ip, f"{sp}rm -rf /tmp/btdeploy /tmp/btdeploy.tgz"), timeout=30)
|
||||
run(self.build_ssh_cmd(ip, f"{sp}rm -rf /tmp/btdeploy /tmp/btdeploy.tgz", hostkey=hostkey), timeout=30)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -559,7 +568,7 @@ class DeployBackend:
|
||||
self.update_status(mac, state="halt", detail="выключаю устройство (halt)...")
|
||||
power_cmd = f"{sp}halt -p"
|
||||
try:
|
||||
run(self.build_ssh_cmd(ip, power_cmd), timeout=30)
|
||||
run(self.build_ssh_cmd(ip, power_cmd, hostkey=hostkey), timeout=30)
|
||||
except Exception:
|
||||
pass
|
||||
if after == "halt":
|
||||
|
||||
Reference in New Issue
Block a user