|
|
@@ -732,9 +732,24 @@ def tar_paths_from_source(paths: set[str], extraction_root: Path) -> None:
|
|
|
"\n" + tar_err.decode("utf-8", "replace")[-4000:])
|
|
|
|
|
|
|
|
|
+def remote_ssh_command(host: str, user: str, password: str, script: str) -> str:
|
|
|
+ askpass = RUN_ROOT / f".askpass-{host.replace('.', '-')}.sh"
|
|
|
+ askpass_file(askpass, password)
|
|
|
+ cmd = ["setsid", "-w", "ssh", "-T", "-o", "StrictHostKeyChecking=no",
|
|
|
+ "-o", "ConnectTimeout=15", f"{user}@{host}", "bash -s"]
|
|
|
+ proc = subprocess.run(cmd, input=script.encode(), env=ssh_env(askpass),
|
|
|
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
+ if proc.returncode != 0:
|
|
|
+ raise JobError(f"{host} 远程命令失败:" + proc.stderr.decode("utf-8", "replace")[-4000:])
|
|
|
+ return proc.stdout.decode("utf-8", "replace")
|
|
|
+
|
|
|
+
|
|
|
def mirror_to_web2() -> str:
|
|
|
+ password = require_env("HXCS_WEB2_SSH_PASS")
|
|
|
+ remote_ssh_command("10.0.42.5", "hxxy", password,
|
|
|
+ f"mkdir -p {shlex.quote(str(RUN_ROOT / 'webapp'))} && chmod 700 {shlex.quote(str(RUN_ROOT / 'webapp'))}\n")
|
|
|
askpass = RUN_ROOT / ".askpass-web2.sh"
|
|
|
- askpass_file(askpass, require_env("HXCS_WEB2_SSH_PASS"))
|
|
|
+ askpass_file(askpass, password)
|
|
|
destination = f"hxxy@10.0.42.5:{RUN_ROOT}/webapp/"
|
|
|
command = ["setsid", "-w", "rsync", "-a", "-e",
|
|
|
"ssh -o StrictHostKeyChecking=no -o ConnectTimeout=15",
|
|
|
@@ -744,7 +759,7 @@ def mirror_to_web2() -> str:
|
|
|
proc = subprocess.run(command, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
if proc.returncode != 0:
|
|
|
raise JobError("暂存资源同步到 10.0.42.5 失败:" + proc.stderr.decode("utf-8", "replace")[-4000:])
|
|
|
- return remote_tree_hash("10.0.42.5", "hxxy", require_env("HXCS_WEB2_SSH_PASS"))
|
|
|
+ return remote_tree_hash("10.0.42.5", "hxxy", password)
|
|
|
|
|
|
|
|
|
def remote_tree_hash(host: str, user: str, password: str) -> str:
|
|
|
@@ -773,17 +788,26 @@ def stage_assets() -> None:
|
|
|
tar_paths_from_source(remaining, STAGE_WEBAPP)
|
|
|
missing = sorted(p for p in derived if not staged_path_exists(p))
|
|
|
ASSET_MISSING.write_text("\n".join(missing) + "\n", encoding="utf-8")
|
|
|
+ finalize_assets(len(direct), len(derived), len(missing))
|
|
|
+
|
|
|
+
|
|
|
+def finalize_assets(direct_count: int | None = None, derived_count: int | None = None,
|
|
|
+ missing_count: int | None = None) -> None:
|
|
|
+ if missing_count is None:
|
|
|
+ missing_count = len(ASSET_MISSING.read_text(encoding="utf-8").splitlines()) if ASSET_MISSING.is_file() else 0
|
|
|
+ file_count = sum(1 for p in STAGE_WEBAPP.rglob("*") if p.is_file())
|
|
|
local_hash = subprocess.run(
|
|
|
f"cd {shlex.quote(str(STAGE_WEBAPP))} && find . -type f -print0 | sort -z | xargs -0 -r sha256sum | sha256sum",
|
|
|
shell=True, check=True, stdout=subprocess.PIPE, text=True).stdout.split()[0]
|
|
|
+ log(f"开始同步 {file_count} 个暂存资源到 10.0.42.5")
|
|
|
remote_hash = mirror_to_web2()
|
|
|
if local_hash != remote_hash:
|
|
|
raise JobError(f"双节点资源哈希不一致:{local_hash} / {remote_hash}")
|
|
|
atomic_json(REPORT_DIR / "asset-report.json", {
|
|
|
- "direct_paths": len(direct), "derived_paths": len(derived),
|
|
|
- "missing": len(missing), "sha256": local_hash,
|
|
|
+ "direct_paths": direct_count, "derived_paths": derived_count,
|
|
|
+ "missing": missing_count, "files": file_count, "sha256": local_hash,
|
|
|
})
|
|
|
- log(f"资源暂存完成:清单 {len(direct) + len(derived)},缺失 {len(missing)},双节点哈希一致")
|
|
|
+ log(f"资源暂存完成:暂存 {file_count} 个文件,缺失 {missing_count},双节点哈希一致")
|
|
|
|
|
|
|
|
|
def verify_database() -> dict:
|
|
|
@@ -1031,11 +1055,17 @@ def run_mode(mode: str) -> None:
|
|
|
update_status(stage="verify")
|
|
|
verify()
|
|
|
update_status(stage="complete", state="complete", finished_at=now())
|
|
|
+ elif mode == "continue-mirror":
|
|
|
+ update_status(stage="stage-assets", continued_at=now())
|
|
|
+ finalize_assets()
|
|
|
+ update_status(stage="verify")
|
|
|
+ verify()
|
|
|
+ update_status(stage="complete", state="complete", finished_at=now())
|
|
|
elif mode == "refresh-delta":
|
|
|
raise JobError("refresh-delta 需要在正式切换前的停写窗口另行授权后实现/执行")
|
|
|
else:
|
|
|
raise JobError(f"未知模式:{mode}")
|
|
|
- if mode not in {"all", "resume", "continue-merge", "continue-assets"}:
|
|
|
+ if mode not in {"all", "resume", "continue-merge", "continue-assets", "continue-mirror"}:
|
|
|
update_status(state="complete", stage=mode, finished_at=now())
|
|
|
except Exception as exc:
|
|
|
log("任务失败:" + str(exc))
|