ops: add production observability and recovery

This commit is contained in:
hailin
2026-07-27 19:06:34 +08:00
parent e69368a2f3
commit b02d18d3b9
33 changed files with 8832 additions and 1 deletions

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,91 @@
import json
from pathlib import Path
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from apps.system.blueprint_readiness import build_blueprint_readiness_report
class Command(BaseCommand):
help = "生成覆盖阶段 2~6 退出条件的统一蓝图就绪报告"
def add_arguments(self, parser):
parser.add_argument("--evidence", help="外部验收证据 JSON;不得包含任何凭据")
parser.add_argument(
"--recovery-evidence",
help="隔离恢复演练 JSON;默认读取仓库内最新已登记记录",
)
parser.add_argument(
"--include-synthetic",
action="store_true",
help="包含合成案例用于诊断;合成案例不会使阶段 2/3 通过",
)
parser.add_argument(
"--format",
choices=["text", "json"],
default="text",
dest="output_format",
)
parser.add_argument("--output", help="可选 JSON 报告输出路径")
parser.add_argument(
"--fail-on-not-ready",
action="store_true",
help="任一蓝图退出条件未满足时返回非零退出码",
)
def handle(self, *args, **options):
workspace_root = Path(settings.BASE_DIR)
recovery_path = Path(
options.get("recovery_evidence")
or workspace_root / "docs/operations/recovery-drill-2026-07-27.json"
)
external = _load_json(options.get("evidence"), label="外部验收证据")
recovery = _load_json(recovery_path, label="隔离恢复证据")
report = build_blueprint_readiness_report(
include_synthetic=options["include_synthetic"],
external_evidence=external,
recovery_evidence=recovery,
)
report_json = json.dumps(report, ensure_ascii=False, indent=2)
output_path = options.get("output")
if output_path:
path = Path(output_path)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(f"{report_json}\n", encoding="utf-8")
if options["output_format"] == "json":
self.stdout.write(report_json)
else:
summary = report["summary"]
self.stdout.write(
f"蓝图验收状态:{report['status']};"
f"已通过 {summary['passed_gate_count']}/{summary['total_gate_count']};"
f"剩余 {summary['remaining_gate_count']}"
)
for gate in report["gates"]:
marker = "通过" if gate["status"] == "passed" else "待验收"
self.stdout.write(f"[{marker}] {gate['title']}")
for reason in gate["reasons"]:
self.stdout.write(self.style.WARNING(f" - {reason}"))
if output_path:
self.stdout.write(f"JSON 报告:{Path(output_path).resolve()}")
if options["fail_on_not_ready"] and not report["exit_ready"]:
raise CommandError("产品蓝图尚未满足全部退出条件")
def _load_json(path_value, *, label: str):
if not path_value:
return None
path = Path(path_value)
if not path.exists():
return None
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as error:
raise CommandError(f"{label}无法读取:{error}") from error
if not isinstance(payload, dict):
raise CommandError(f"{label}必须是 JSON 对象")
return payload