Эх сурвалжийг харах

补齐旧站资源双节点同步目录与续跑入口

在同步到 10.0.42.5 前先创建远端 staging 父目录,修复 rsync 无法创建目标 webapp 目录的问题。\n\n新增 continue-mirror 后台模式,可在不重复复制 102GB 源资源的情况下重新计算双节点哈希并进入最终验收。\n\nValidation:\n- python3 -m py_compile local-run/bin/legacy-sites-job.py\n- bash -n local-run/bin/legacy-sites-to-hxcs.sh\n- 资源暂存完成:102GB、202601 个文件、827 条缺失引用\n- 当前进入双节点镜像与校验阶段
非繁 3 долоо хоног өмнө
parent
commit
879bd3e945

+ 36 - 6
local-run/bin/legacy-sites-job.py

@@ -732,9 +732,24 @@ def tar_paths_from_source(paths: set[str], extraction_root: Path) -> None:
                        "\n" + tar_err.decode("utf-8", "replace")[-4000:])
                        "\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:
 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 = 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/"
     destination = f"hxxy@10.0.42.5:{RUN_ROOT}/webapp/"
     command = ["setsid", "-w", "rsync", "-a", "-e",
     command = ["setsid", "-w", "rsync", "-a", "-e",
                "ssh -o StrictHostKeyChecking=no -o ConnectTimeout=15",
                "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)
     proc = subprocess.run(command, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     if proc.returncode != 0:
     if proc.returncode != 0:
         raise JobError("暂存资源同步到 10.0.42.5 失败:" + proc.stderr.decode("utf-8", "replace")[-4000:])
         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:
 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)
         tar_paths_from_source(remaining, STAGE_WEBAPP)
     missing = sorted(p for p in derived if not staged_path_exists(p))
     missing = sorted(p for p in derived if not staged_path_exists(p))
     ASSET_MISSING.write_text("\n".join(missing) + "\n", encoding="utf-8")
     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(
     local_hash = subprocess.run(
         f"cd {shlex.quote(str(STAGE_WEBAPP))} && find . -type f -print0 | sort -z | xargs -0 -r sha256sum | sha256sum",
         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]
         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()
     remote_hash = mirror_to_web2()
     if local_hash != remote_hash:
     if local_hash != remote_hash:
         raise JobError(f"双节点资源哈希不一致:{local_hash} / {remote_hash}")
         raise JobError(f"双节点资源哈希不一致:{local_hash} / {remote_hash}")
     atomic_json(REPORT_DIR / "asset-report.json", {
     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:
 def verify_database() -> dict:
@@ -1031,11 +1055,17 @@ def run_mode(mode: str) -> None:
             update_status(stage="verify")
             update_status(stage="verify")
             verify()
             verify()
             update_status(stage="complete", state="complete", finished_at=now())
             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":
         elif mode == "refresh-delta":
             raise JobError("refresh-delta 需要在正式切换前的停写窗口另行授权后实现/执行")
             raise JobError("refresh-delta 需要在正式切换前的停写窗口另行授权后实现/执行")
         else:
         else:
             raise JobError(f"未知模式:{mode}")
             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())
             update_status(state="complete", stage=mode, finished_at=now())
     except Exception as exc:
     except Exception as exc:
         log("任务失败:" + str(exc))
         log("任务失败:" + str(exc))

+ 6 - 1
local-run/bin/legacy-sites-to-hxcs.sh

@@ -22,11 +22,12 @@ MODE="${1:-status}"
 die() { printf '失败:%s\n' "$*" >&2; exit 1; }
 die() { printf '失败:%s\n' "$*" >&2; exit 1; }
 usage() {
 usage() {
 	cat <<'EOF'
 	cat <<'EOF'
-用法:legacy-sites-to-hxcs.sh <preflight|snapshot|build-shadow|stage-assets|verify|run-detached|resume-detached|continue-detached|continue-assets-detached|status|wait|refresh-delta>
+用法:legacy-sites-to-hxcs.sh <preflight|snapshot|build-shadow|stage-assets|verify|run-detached|resume-detached|continue-detached|continue-assets-detached|continue-mirror-detached|status|wait|refresh-delta>
   run-detached  在 10.0.42.6 脱离 SSH 会话执行完整复制流程
   run-detached  在 10.0.42.6 脱离 SSH 会话执行完整复制流程
   resume-detached  保留旧生产快照,重新克隆影子库后断点续跑
   resume-detached  保留旧生产快照,重新克隆影子库后断点续跑
   continue-detached  复用现有影子库,仅重新执行合并、资源和验收
   continue-detached  复用现有影子库,仅重新执行合并、资源和验收
   continue-assets-detached  复用影子库和已复制资源,重新执行资源整理和验收
   continue-assets-detached  复用影子库和已复制资源,重新执行资源整理和验收
+  continue-mirror-detached  复用已整理资源,只执行双节点同步和验收
   status        查看后台任务状态和最后日志
   status        查看后台任务状态和最后日志
   wait         等待后台任务结束(本机 VPN 中断不影响任务)
   wait         等待后台任务结束(本机 VPN 中断不影响任务)
 EOF
 EOF
@@ -180,6 +181,10 @@ case "${MODE}" in
 		deploy_tools
 		deploy_tools
 		start_detached continue-assets
 		start_detached continue-assets
 		;;
 		;;
+	continue-mirror-detached)
+		deploy_tools
+		start_detached continue-mirror
+		;;
 	status)
 	status)
 		show_status
 		show_status
 		;;
 		;;