52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
||||
|
|
"""部署编排 + 回滚冒烟脚本(Issue #60)。
|
|||
|
|
|
|||
|
|
直接运行 ``python _sanity_check.py`` 验证:默认 Helm 计划全成功部署、
|
|||
|
|
快照压栈、模拟部署失败触发回滚到上一稳定版本。零第三方依赖。
|
|||
|
|
"""
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
|
|||
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|||
|
|
sys.path.insert(0, HERE)
|
|||
|
|
|
|||
|
|
from deploy_plan import ( # noqa: E402
|
|||
|
|
DeployOrchestrator, DeployPlan, DeployStepKind)
|
|||
|
|
from rollback import RollbackManager # noqa: E402
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main() -> int:
|
|||
|
|
# 1) 部署前快照(当前稳定版本 v1.0.0)
|
|||
|
|
mgr = RollbackManager()
|
|||
|
|
stable = DeployPlan.default_helm_release(version="v1.0.0")
|
|||
|
|
mgr.snapshot(stable, reason="部署 v1.1.0 前的稳定版本")
|
|||
|
|
print(f"[OK] 快照压栈:稳定版本 {mgr.latest().version}")
|
|||
|
|
|
|||
|
|
# 2) 部署 v1.1.0 全成功
|
|||
|
|
new_plan = DeployPlan.default_helm_release(version="v1.1.0")
|
|||
|
|
report = DeployOrchestrator(new_plan).execute()
|
|||
|
|
assert report.succeeded, report.outcome.reason
|
|||
|
|
mgr.snapshot(new_plan, reason="v1.1.0 部署成功")
|
|||
|
|
print(f"[OK] 部署 v1.1.0 成功:{len(report.results)} 步,无需回滚")
|
|||
|
|
|
|||
|
|
# 3) 模拟 v1.2.0 部署失败(health_check 失败)→ 触发回滚
|
|||
|
|
def _bad_health(ctx):
|
|||
|
|
return False, "/health 503(服务未就绪)"
|
|||
|
|
bad_plan = DeployPlan.default_helm_release(
|
|||
|
|
version="v1.2.0",
|
|||
|
|
step_actions={DeployStepKind.HEALTH_CHECK: _bad_health})
|
|||
|
|
bad_report = DeployOrchestrator(bad_plan).execute()
|
|||
|
|
assert not bad_report.succeeded
|
|||
|
|
assert bad_report.outcome.needs_rollback
|
|||
|
|
print(f"[OK] v1.2.0 部署失败:{bad_report.outcome.reason}")
|
|||
|
|
|
|||
|
|
result = mgr.rollback_to_latest(reason="v1.2.0 健康检查失败")
|
|||
|
|
assert result.succeeded, result.detail
|
|||
|
|
print(f"[OK] 回滚到 {result.target.version}:{result.detail}")
|
|||
|
|
print("部署编排 + 回滚冒烟通过 ✅")
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
raise SystemExit(main())
|