Documentation
¶
Overview ¶
Package selfupdate 实现面板内零停机自更新。分两个半身在同一二进制里:
托管侧(Service):面板端点调 Start/Rollback,负责下载校验 release 二进制到状态目录、发现托管 unit、并把编排进程拉起到 unit 控制域之外 (systemd-run 瞬态 service 同时逃出 KillMode=control-group 的 cgroup 连坐与 ProtectSystem=strict 的挂载命名空间;macOS 无 sandbox,setsid 脱离进程组即可)。随后编排侧接管,本进程在 restart 中被排空替换。
编排侧(Drive,见 driver.go):新二进制作为完整实例带 REUSEPORT 入组 接住排空窗口的连接,做原子换名(当前→.backup、自身→当前)、按平台 重启托管 unit、轮询 update:status 直到新托管实例落 done,再自我 SIGTERM 走常规排空退出;超时则记 failed 并保留服役(同 deploy 交接 降级语义——它跑的就是新版本,留着比端口无人接强)。
进度事实源是 runtime_state 的 update:status 键:写入者沿交接链轮换 (托管实例记下载阶段 → 编排进程记换名/重启 → 新托管实例 boot 收尾记 done/failed),面板轮询在重启前后都能读到同一条记录。
Index ¶
- Variables
- func BootFinalize(ctx context.Context, st StatusStore, version string)
- type DriverDeps
- type Service
- func (s *Service) Check(ctx context.Context, force bool) map[string]any
- func (s *Service) Rollback(ctx context.Context) (*Status, error)
- func (s *Service) Start(ctx context.Context, tag string) (*Status, error)
- func (s *Service) StatusView(ctx context.Context) map[string]any
- func (s *Service) Supported() bool
- type Status
- type StatusStore
Constants ¶
This section is empty.
Variables ¶
var ( ErrBusy = errors.New("an update is already in progress") ErrUnsupported = errors.New("self-update requires a managed instance (systemd --user or launchd) on Linux/macOS") ErrNoBackup = errors.New("no .backup binary to roll back to") ErrBadTag = errors.New("invalid release tag") )
errBusy/errUnsupported 由面板端点映射为 409/501;errStale 不外泄。
Functions ¶
func BootFinalize ¶
func BootFinalize(ctx context.Context, st StatusStore, version string)
BootFinalize 是托管实例的启动收尾:读 update:status,在途记录按 自身版本结算——本进程版本 == to 说明换名+重启已生效,记 done(编排 进程轮询到它即退场);== from 说明还没换就被重启打断,记 failed; 两者皆否同样记 failed。编排进程/交接进程不跑(role.duty 门禁在 调用侧)——编排进程版本就是 to,它跑这里会在换名前就误报 done。
Types ¶
type DriverDeps ¶
type DriverDeps struct {
Store StatusStore
To string
Install string
Unit string
OldPID int
Rollback bool
SelfExe string
}
DriverDeps 是编排侧跑一轮所需的全部参数;EnvDeps 从环境装配。
func EnvDeps ¶
func EnvDeps(st StatusStore) (*DriverDeps, bool)
EnvDeps 判定本进程是不是自更新编排进程并装配参数。标记缺席返回 ok=false(普通实例与 deploy 交接进程都走这里但立即判否)。
func (*DriverDeps) Drive ¶
func (d *DriverDeps) Drive()
Drive 是编排进程的主推进体:换名→重启托管 unit→等新实例报到→ 自我 SIGTERM。由 main 在 store 就绪后以 goroutine 拉起;本函数 返回时要么已发信号(正常退场走 run() 排空),要么进入超时保活。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 是托管侧自更新服务:装配在面板后面,生命周期同托管进程。
func New ¶
func New(st StatusStore, version, configPath, stateDir string) (*Service, error)
New 装配托管侧服务。install 是托管二进制路径:普通实例取 os.Executable,编排进程优先取 DEVIN2API_UPDATE_INSTALL(自身 exe 是 stateDir 的 .new,拿它算 .backup 会失真)。裸跑实例 discoverUnit 判 unsupported,端点 501。
func (*Service) Check ¶
Check 返回面板「检查更新」视图:当前版本 + 最新 tag + 是否需要更新。 结果按 checkCacheTTL 缓存;force 跳过缓存。并发 Check(含 force) 经 checkInflight 收敛成一趟 latestTag——面板连点/轮询扇出不该各发 一次 GitHub 调用。
func (*Service) Rollback ¶
Rollback 拉起 .backup 二进制做对称换回:不下载,spawn 目标换成 install.backup,编排侧按 ROLLBACK 分支做三段换名。返回记录供 202。
func (*Service) Start ¶
Start 发起一次前向更新:tag 空取最新 release。202 语义——校验 准入后立即返回,下载/校验/拉起编排进程在后台 goroutine 推进, 进度经 update:status 供轮询。
func (*Service) StatusView ¶
StatusView 组装 GET /admin/update/status 的响应体。
type Status ¶
type Status struct {
Phase string `json:"phase"`
From string `json:"from"`
To string `json:"to"`
Error string `json:"error,omitempty"`
Since int64 `json:"since"` // unix 毫秒,当前阶段开始时刻
PID int `json:"pid"`
Rollback bool `json:"rollback,omitempty"`
}
Status 是一次更新尝试的可持久化进度记录。PID 记录当前阶段推进者: 下载期是旧托管实例 pid,交接期是编排进程 pid——staleness 判定与 残留回收都以它为准。