Files
pangolin/server/internal/provision/store.go
T
wangjia 3d5bac66b4 feat(provision): 弹性节点基建 Terraform + 一键更换 (tsk_6u0FxmbC7Yeq)
IaC 面 (infra/) 与控制面 (server/internal/provision) 双产出,落地 doc/04 §4
「节点是牲口」弹性拓扑与 make-before-break 一键更换。

server/internal/provision:
- CloudAdapter 适配层 + Registry(首发 vultr 消耗品池 / hetzner 精品池各一);
  厂商凭证仅从 PROVISION_<VENDOR>_* env 注入,不入库不入 git。
- ProvisionService:CreateNode(幂等键重放不重复开机)、DestroyNode(幂等)、
  RotateIP(换 IP 不换机 + version bump)、ListProviders。
- Replace 一键更换:先建后拆,新机 up 先于旧机 draining(容量不下降),
  replacement_uuid 幂等键 + replacements 表分步记录,崩溃可续跑不重复。
- RotatePool:池内滚动轮换,并发度 1–2。
- cmd/nodectl CLI:create/destroy/rotate-ip/replace/rotate-pool/providers。
- 单测(mock 厂商 API + 内存 Store):幂等重放、make-before-break 时序断言、
  开机失败/探活超时→destroyed+失败计数+告警钩子、崩溃续跑、RotatePool。

infra/:
- terraform/:探针机 + 控制面基线模块化(probe / control-plane)+ README,
  低频基线进 state,节点不进 Terraform。
- cloud-init/node.yaml.tmpl:节点引导模板(注入一次性 bootstrap token,task #5)。
- identity-isolation.md:身份隔离登记表(doc/06 §2 红线,无任何凭证)。

migrations/000008:nodes 增 provider_instance_id/elastic_ip_id、node_events
增 ip_rotated、provision_idempotency / replacements 表(附加式,不动现网)。

红线:仅面向新厂商池,绝不纳管现网生产 EC2(deploy/ marzban)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-13 14:23:39 +08:00

82 lines
3.2 KiB
Go

package provision
import (
"context"
"time"
)
// ReplaceStep enumerates the resumable steps of a Replace orchestration.
// Persisted in replacements.step so a crashed process can continue without
// repeating a boot or a destroy.
type ReplaceStep string
const (
StepOpenNew ReplaceStep = "open_new" // create the replacement node
StepProbing ReplaceStep = "probing" // wait for self-register + probe
StepNewUp ReplaceStep = "new_up" // promote new node, bump version
StepDraining ReplaceStep = "draining" // drain the old node
StepDestroyOld ReplaceStep = "destroy_old" // destroy old node + release IP
StepDone ReplaceStep = "done"
)
// ReplaceStatus is the terminal/running state of a replacement record.
type ReplaceStatus string
const (
ReplaceRunning ReplaceStatus = "running"
ReplaceDone ReplaceStatus = "done"
ReplaceFailed ReplaceStatus = "failed"
)
// Replacement mirrors a replacements row (crash-recoverable orchestration).
type Replacement struct {
UUID string
OldNodeID int64
NewNodeID int64 // 0 until the new node is created
Pool Pool
Status ReplaceStatus
Step ReplaceStep
CreatedAt time.Time
UpdatedAt time.Time
}
// Store is the persistence boundary for the provision package. Both the MySQL
// implementation (mysqlStore) and the in-memory test fake satisfy it, keeping
// the service / orchestration logic database-agnostic and unit-testable.
type Store interface {
// --- nodes ---
InsertNode(ctx context.Context, n *Node) (int64, error)
GetNode(ctx context.Context, id int64) (*Node, error)
GetNodeByUUID(ctx context.Context, uuid string) (*Node, error)
UpdateNodeStatus(ctx context.Context, id int64, status Status) error
UpdateNodeEndpoint(ctx context.Context, id int64, endpoint string) error
// SetNodeInstance records the vendor instance ID and IP-derived endpoint.
SetNodeInstance(ctx context.Context, id int64, instanceID, endpoint string) error
SetNodeWeight(ctx context.Context, id int64, weight int) error
// ListNodesByPool returns nodes whose provider belongs to pool, optionally
// filtered to a single status (empty = all statuses).
ListNodesByPool(ctx context.Context, pool Pool, status Status) ([]*Node, error)
// --- providers ---
ListProviders(ctx context.Context, pool Pool) ([]*Provider, error)
GetProvider(ctx context.Context, id int64) (*Provider, error)
// --- events / audit / directory ---
WriteNodeEvent(ctx context.Context, nodeID int64, event Event, detailJSON string) error
WriteAuditLog(ctx context.Context, actor, action, target, metaJSON string) error
BumpDirectoryVersion(ctx context.Context) (int64, error)
// --- idempotency ---
// LookupIdempotency returns the node UUID previously bound to key, or
// (\"\", false, nil) if unseen.
LookupIdempotency(ctx context.Context, key string) (string, bool, error)
// SaveIdempotency binds key→nodeUUID. It is a no-op if the key already
// exists (first write wins).
SaveIdempotency(ctx context.Context, key, nodeUUID string) error
// --- replacements (crash recovery) ---
CreateReplacement(ctx context.Context, r *Replacement) error
GetReplacement(ctx context.Context, uuid string) (*Replacement, error)
UpdateReplacement(ctx context.Context, r *Replacement) error
}