fix(install): 守护配置对齐宿主机 cron 与验收检查
Nexus CI/CD / test (push) Waiting to run
Nexus CI/CD / deploy (push) Blocked by required conditions
Nexus Pre-commit Checks / quick-check (push) Waiting to run

Docker 向导提示 nx cron/宿主机路径;裸机 cron 含 db_backup;验收脚本检查 cron 与 MySQL 备份能力。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nexus Agent
2026-06-06 23:11:02 +08:00
parent 89865ec690
commit c9e08799f9
7 changed files with 169 additions and 28 deletions
+61 -22
View File
@@ -656,9 +656,61 @@ def _detect_bt_panel() -> bool:
return Path("/www/server/panel/class/common.py").exists()
def _host_deploy_root(install_dir: str) -> str:
"""宿主机仓库路径(容器内 /app 与宿主机 /opt/nexus 不同)。"""
host = os.environ.get("NEXUS_HOST_ROOT", "").strip()
if host:
return host
if install_dir != "/app" and Path(install_dir).is_dir():
return install_dir
return "/opt/nexus"
def _append_ops_cron_results(results: list[str], install_dir: str) -> None:
"""Crontab 条目与 deploy/install_ops_cron.sh 保持一致。"""
health_marker = "nexus-health-monitor"
backup_marker = "nexus-mysql-backup"
health_line = (
f"* * * * * NEXUS_DEPLOY_DIR={install_dir} "
f"{install_dir}/deploy/health_monitor.sh "
f">> /var/log/nexus_health.log 2>&1 # {health_marker}"
)
backup_line = (
f"0 3 * * * NEXUS_ROOT={install_dir} "
f"{install_dir}/deploy/db_backup.sh "
f">> /var/log/nexus_backup.log 2>&1 # {backup_marker}"
)
try:
current = subprocess.run(
["crontab", "-l"], capture_output=True, text=True, timeout=5
).stdout or ""
added: list[str] = []
if health_marker not in current and "health_monitor.sh" not in current:
added.append(health_line)
if backup_marker not in current and "db_backup.sh" not in current:
added.append(backup_line)
if added:
new_cron = current.rstrip() + "\n" + "\n".join(added) + "\n"
proc = subprocess.run(
["crontab", "-"], input=new_cron, capture_output=True, text=True, timeout=5
)
if proc.returncode == 0:
results.append("✓ Crontab 已配置(health_monitor + db_backup")
else:
results.append("✗ Crontab 配置失败 — 请手动: bash deploy/install_ops_cron.sh")
else:
results.append("✓ Crontab 已存在 health_monitor / db_backup 条目")
except Exception:
results.append(
f"⚠ 宿主机 cron 需在安装完成后执行: "
f"sudo bash {install_dir}/deploy/install_ops_cron.sh"
)
def _configure_docker_guardian(install_dir: str, api_port: str) -> list[str]:
"""1Panel + Docker: Compose restart/healthcheck + in-app self_monitor (no host Supervisor)."""
results: list[str] = []
host_root = _host_deploy_root(install_dir)
if Path("/.dockerenv").is_file():
results.append("✓ Layer 1: Docker 容器运行中(Compose restart: unless-stopped")
@@ -703,7 +755,11 @@ def _configure_docker_guardian(install_dir: str, api_port: str) -> list[str]:
f"ℹ Layer 3(宿主机): 配置 OpenResty 反代 → 127.0.0.1:{publish_port}(见 deploy/1panel/openresty-nexus.conf.example"
)
results.append(f" 日常升级: 宿主机 cd {install_dir} && nx update")
results.append(
f" Layer 3(宿主机 cron: sudo bash {host_root}/deploy/install_ops_cron.sh"
)
results.append(" 或: sudo nx cronhealth_monitor 每分钟 + MySQL 备份每日 03:00")
results.append(f" 日常升级: 宿主机 cd {host_root} && nx update")
return results
@@ -759,7 +815,7 @@ def _configure_guardian(install_dir: str, api_port: str) -> list[str]:
except OSError:
results.append("✗ Supervisor 配置写入失败(需 root 权限)")
# 3. Update health_monitor.sh INSTALL_DIR
# 3. health_monitor 默认路径(cron 行内 NEXUS_DEPLOY_DIR 优先,此处仅兼容旧部署)
health_sh = Path(install_dir) / "deploy" / "health_monitor.sh"
if health_sh.exists():
try:
@@ -772,31 +828,14 @@ def _configure_guardian(install_dir: str, api_port: str) -> list[str]:
)
write_utf8_lf(health_sh, content)
health_sh.chmod(0o755)
results.append("✓ health_monitor.sh INSTALL_DIR 已更新")
results.append("✓ health_monitor.sh DEPLOY_DIR 默认值已更新")
except OSError:
results.append("✗ health_monitor.sh 更新失败")
else:
results.append("⚠ health_monitor.sh 未找到,跳过")
# 4. Crontab
cron_entry = f"* * * * * {install_dir}/deploy/health_monitor.sh"
try:
current = subprocess.run(
["crontab", "-l"], capture_output=True, text=True, timeout=5
).stdout or ""
if "health_monitor.sh" not in current:
new_cron = current.rstrip() + "\n" + cron_entry + "\n"
proc = subprocess.run(
["crontab", "-"], input=new_cron, capture_output=True, text=True, timeout=5
)
if proc.returncode == 0:
results.append("✓ Crontab 已配置(每分钟健康检查)")
else:
results.append("✗ Crontab 配置失败 — 请手动添加")
else:
results.append("✓ Crontab 已存在 health_monitor 条目")
except Exception:
results.append("⚠ Crontab 配置跳过(权限不足或 cron 不可用)")
# 4. Crontab(与 install_ops_cron.sh 同格式)
_append_ops_cron_results(results, install_dir)
# 5. Reload Supervisor
try: