feat(backend): admin 手动运维接真实 lifecycle/provision

管理后台节点页的「拉黑/恢复(draining/up)」与「一键换机(replace)」此前
注入的是 StubLifecycle/StubProvision(Ready()=false,UI 置灰、操作返回
服务不可用)。本次接真实实现:

- RealLifecycle:复用 scheduler 的乐观锁状态流转(UPDATE nodes WHERE
  status=from + node_events + directory_version bump),手动运维与调度
  自动循环共用同一条 canonical 路径;读当前状态作 from 守卫,幂等安全。
- RealProvision:委托 provision.Service.Replace 的 make-before-break 换机
  (先拉起新节点再 drain 旧节点,容量不掉)。
- BuildServices:lifecycle 恒为真实;provision 在服务初始化成功时为真实,
  否则回退 not-ready stub(无厂商凭证时点击换机给出明确错误)。

补 services_real_test.go:换机委托(空 UUID 起新编排)、错误透传、
非法目标状态拒绝、Ready 标志。server 全量 23 包测试通过。
e2e(真实换机)待机群 + 厂商凭证。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-17 13:24:15 +08:00
parent 5f3f4189e5
commit 9a028ab907
3 changed files with 161 additions and 5 deletions
+18 -5
View File
@@ -10,6 +10,8 @@ import (
"github.com/go-chi/chi/v5/middleware"
"github.com/redis/go-redis/v9"
"github.com/wangjia/pangolin/server/internal/codes"
"github.com/wangjia/pangolin/server/internal/provision"
"github.com/wangjia/pangolin/server/internal/provision/providers"
)
// NewRouter wires the full admin HTTP handler chain:
@@ -46,17 +48,28 @@ func NewRouter(h *Handlers, sessions *SessionStore, cfg *Config, sec *SecurityLo
return r
}
// BuildServices assembles the downstream services. The codes service (#3) is
// real; lifecycle (#5) and provisioning (#14) are stubs until those modules
// land — the UI greys out their controls (Ready()==false).
// BuildServices assembles the downstream services. Codes (#3) and lifecycle (#5)
// are always real (DB-backed). Provisioning (#14) is real when the provision
// service initializes; otherwise it falls back to the not-ready stub and the UI
// greys out the replace control (Ready()==false).
func BuildServices(db *sql.DB, rdb *redis.Client, failMax int, lockDur time.Duration) Services {
codeStore := codes.NewStore(db)
codeSvc := codes.NewService(codeStore, rdb, failMax, lockDur)
return Services{
svc := Services{
Codes: NewCodesAdapter(codeSvc, codeStore),
Lifecycle: NewStubLifecycle(),
Lifecycle: NewRealLifecycle(db),
Provision: NewStubProvision(),
}
provSvc, err := provision.NewService(provision.Config{
Store: provision.NewMySQLStore(db),
Adapters: providers.NewRegistry(),
})
if err != nil {
log.Printf("admin: provision service init failed (%v) — replace control disabled", err)
} else {
svc.Provision = NewRealProvision(provSvc)
}
return svc
}
// NewHandler builds the complete admin http.Handler from its dependencies.