mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-11-17 01:44:23 +08:00
fix(panel): make self-upgrades durable and observable
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
# Panel upgrade safety and recovery
|
||||
|
||||
The helper is a subcommand in the **currently running image**, dispatched before
|
||||
application/database initialization. This fix must first be installed through the
|
||||
normal administrator deployment procedure on older releases lacking the helper.
|
||||
It cannot retroactively repair an already-running legacy upgrade helper.
|
||||
|
||||
Supported deployments have one `docker-compose.yml`, the original Compose project
|
||||
labels, and a writable host bind mount containing that file and `.env`. The
|
||||
helper mounts that directory at its **host absolute path** and passes explicit
|
||||
project, project-directory, env-file and compose-file options. Multiple Compose
|
||||
files, mismatched mounts/resources, or changed database settings fail closed.
|
||||
|
||||
Jobs are under `<deployment>/.panel-upgrades/<jobId>/` (directory 0700, files
|
||||
0600). `*.original` contains immutable per-job configuration snapshots, not a live
|
||||
database backup. `events.log` contains safe phase messages only. Legacy
|
||||
`.upgrade.bak` files and `upgrade.log` are never changed. Helper containers remain
|
||||
for diagnosis; they are intentionally not automatically removed. Operators may
|
||||
remove completed helpers after retaining the job's files.
|
||||
|
||||
The `latest` pointer plus filesystem lock prevents simultaneous jobs across API
|
||||
processes and backend replacement. Failed validation/pull leaves live files
|
||||
untouched. A failed configuration application attempts to restore both files
|
||||
before any recreate. Once `restarting` is recorded, failures require recovery:
|
||||
no automatic database rollback, image rollback, or container rollback is safe.
|
||||
The helper has a 30 minute overall deadline, including a maximum five minute
|
||||
post-recreate verification window. Docker client timeout during recreation can
|
||||
leave daemon work in flight; it is always treated as uncertain, not retried.
|
||||
|
||||
## Operator recovery after an interrupted/uncertain job
|
||||
|
||||
There is intentionally no remotely callable “clear lock” endpoint. Do not delete
|
||||
job directories or rewrite status JSON to force another attempt.
|
||||
|
||||
1. Work on the Docker host and identify the job from `latest`, `status.json` and
|
||||
`plan.json`. Preserve the entire job directory, live Compose/env files, and
|
||||
container inspection information in a private location. These files can
|
||||
contain credentials. Never paste `.env` or raw Compose config into public logs.
|
||||
2. Restore Docker daemon access if necessary. Inspect the exact helper container
|
||||
named by the job. If it is running, let it finish or explicitly stop it as an
|
||||
administrator. Verify it has stopped. If launch was uncertain, establish that
|
||||
no matching helper exists and no Docker create/up operation remains in flight.
|
||||
Do not clear the job while any of these facts are uncertain.
|
||||
3. Inspect the original project's backend and frontend container IDs, images,
|
||||
mounts and health. Check the actual database and application logs privately.
|
||||
After any attempted recreation, assume migrations may already have occurred.
|
||||
Preserve a current database snapshot before further manual deployment work.
|
||||
Use the normal documented deployment procedure to bring both services to a
|
||||
consistent, healthy state. Do not copy an old database over the current one or
|
||||
blindly redeploy an older image. Configuration snapshots alone cannot undo a
|
||||
schema migration.
|
||||
4. Only after verifying no helper/daemon work remains and both services and their
|
||||
database are consistent, archive the `latest` pointer under a unique filename
|
||||
in `.panel-upgrades/` (for example `resolved-<jobId>.pointer`). Do this while
|
||||
the API is stopped, or hold an exclusive OS flock on `.panel-upgrades/lock`,
|
||||
so no other start can race this manual recovery. Keep the job directory and
|
||||
archived pointer. This permits a new job without changing recovery evidence.
|
||||
5. Restart the API if stopped and reload the browser settings page so it discovers
|
||||
the current `latest` task instead of following the resolved task ID. No current
|
||||
job is expected. A fresh start creates a new ID and new backups, never
|
||||
overwriting old evidence.
|
||||
|
||||
A lost helper, unavailable daemon, or stale status is displayed as
|
||||
`recovery_required` while retaining the active lock. This is intentionally a
|
||||
conservative derived status, so a helper that is still completing work can report
|
||||
its actual durable result without racing a status request.
|
||||
@@ -0,0 +1,311 @@
|
||||
package panelupgrade
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const helperTimeout = 30 * time.Minute
|
||||
|
||||
var verificationTimeout = 5 * time.Minute
|
||||
|
||||
type composeConfig struct {
|
||||
FullServices map[string]map[string]any `json:"-"`
|
||||
Services map[string]struct {
|
||||
Image string `json:"image"`
|
||||
ContainerName string `json:"container_name"`
|
||||
Volumes []map[string]any `json:"volumes"`
|
||||
Environment map[string]string `json:"environment"`
|
||||
} `json:"services"`
|
||||
Volumes map[string]any `json:"volumes"`
|
||||
Networks map[string]any `json:"networks"`
|
||||
}
|
||||
|
||||
func composeArgs(p Plan, dir string, args ...string) []string {
|
||||
return append([]string{"compose", "--project-name", p.Project, "--project-directory", p.HostDir, "--env-file", filepath.Join(dir, ".env"), "-f", filepath.Join(dir, "docker-compose.yml")}, args...)
|
||||
}
|
||||
func config(ctx context.Context, p Plan, dir string) (composeConfig, error) {
|
||||
var c composeConfig
|
||||
b, e := docker(ctx, composeArgs(p, dir, "config", "--format", "json")...)
|
||||
if e != nil {
|
||||
return c, e
|
||||
}
|
||||
e = json.Unmarshal(b, &c)
|
||||
if e == nil {
|
||||
var raw struct {
|
||||
Services map[string]map[string]any `json:"services"`
|
||||
}
|
||||
e = json.Unmarshal(b, &raw)
|
||||
c.FullServices = raw.Services
|
||||
}
|
||||
return c, e
|
||||
}
|
||||
func validateConfig(old, next composeConfig, p Plan) error {
|
||||
// Compare complete normalized services, not a projection that can silently drop
|
||||
// host bindings, commands, security options, networks or dependency changes.
|
||||
if old.FullServices == nil || next.FullServices == nil {
|
||||
return errors.New("missing full Compose configuration")
|
||||
}
|
||||
raw, _ := json.Marshal(old.FullServices)
|
||||
var expected map[string]map[string]any
|
||||
if json.Unmarshal(raw, &expected) != nil {
|
||||
return errors.New("invalid configuration")
|
||||
}
|
||||
for _, service := range []string{"backend", "frontend"} {
|
||||
if expected[service] == nil || next.FullServices[service] == nil {
|
||||
return errors.New("missing service")
|
||||
}
|
||||
expected[service]["image"] = next.FullServices[service]["image"]
|
||||
}
|
||||
if env, ok := expected["backend"]["environment"].(map[string]any); ok {
|
||||
env["FLUX_VERSION"] = p.Job.TargetVersion
|
||||
} else {
|
||||
return errors.New("missing backend environment")
|
||||
}
|
||||
if !reflect.DeepEqual(expected, next.FullServices) {
|
||||
return errors.New("unsupported service configuration change; manual upgrade required")
|
||||
}
|
||||
|
||||
for service, image := range map[string]string{"backend": "ghcr.io/sagit-chu/flux-panel-backend:", "frontend": "ghcr.io/sagit-chu/vite-frontend:"} {
|
||||
a, ok := old.Services[service]
|
||||
b, present := next.Services[service]
|
||||
if !ok || !present || b.Image != image+p.Job.TargetVersion || a.ContainerName != b.ContainerName || !reflect.DeepEqual(a.Volumes, b.Volumes) {
|
||||
return errors.New("service image, container identity or persistent mounts do not match the supported deployment")
|
||||
}
|
||||
}
|
||||
if next.Services["backend"].ContainerName != p.Backend || next.Services["backend"].Environment["FLUX_VERSION"] != p.Job.TargetVersion {
|
||||
return errors.New("backend identity/version mismatch")
|
||||
}
|
||||
|
||||
for service := range next.Services {
|
||||
if service != "backend" && service != "frontend" && service != "postgres" {
|
||||
return errors.New("unsupported service")
|
||||
}
|
||||
}
|
||||
for key, value := range old.Services["backend"].Environment {
|
||||
if key != "FLUX_VERSION" && next.Services["backend"].Environment[key] != value {
|
||||
return errors.New("backend environment changed; manual upgrade required")
|
||||
}
|
||||
}
|
||||
for key, value := range next.Services["backend"].Environment {
|
||||
if key != "FLUX_VERSION" && old.Services["backend"].Environment[key] != value {
|
||||
return errors.New("backend environment changed; manual upgrade required")
|
||||
}
|
||||
}
|
||||
// Database configuration and shared resource identities are not upgrade targets.
|
||||
if !reflect.DeepEqual(old.Services["postgres"], next.Services["postgres"]) || !reflect.DeepEqual(old.Volumes, next.Volumes) || !reflect.DeepEqual(old.Networks, next.Networks) {
|
||||
return errors.New("database or shared resource configuration changed; manual upgrade required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateLive proves the normalized source configuration still describes the running
|
||||
// backend. In particular, Compose must not attach a fresh database volume by accident.
|
||||
func validateLive(ctx context.Context, p Plan, c composeConfig) error {
|
||||
live, err := inspect(ctx, p.Backend)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if live.Image != p.Image || !live.State.Running || live.Config.Labels["com.docker.compose.project"] != p.Project || live.Config.Labels["com.docker.compose.service"] != "backend" {
|
||||
return errors.New("running backend changed")
|
||||
}
|
||||
if len(live.Mounts) != len(c.Services["backend"].Volumes) {
|
||||
return errors.New("unexpected live mounts; manual upgrade required")
|
||||
}
|
||||
for _, v := range c.Services["backend"].Volumes {
|
||||
typ, _ := v["type"].(string)
|
||||
src, _ := v["source"].(string)
|
||||
dst, _ := v["target"].(string)
|
||||
if typ == "volume" {
|
||||
spec, ok := c.Volumes[src].(map[string]any)
|
||||
if !ok {
|
||||
return errors.New("unknown volume")
|
||||
}
|
||||
src, _ = spec["name"].(string)
|
||||
}
|
||||
if src == "" || dst == "" || (typ != "volume" && typ != "bind") {
|
||||
return errors.New("unsupported mount")
|
||||
}
|
||||
found := false
|
||||
for _, mount := range live.Mounts {
|
||||
actual := mount.Source
|
||||
if typ == "volume" {
|
||||
actual = mount.Name
|
||||
}
|
||||
if mount.Type == typ && mount.Destination == dst && actual == src && mount.RW != (v["read_only"] == true) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return errors.New("live persistent mount does not match Compose")
|
||||
}
|
||||
}
|
||||
env := map[string]string{}
|
||||
for _, v := range live.Config.Env {
|
||||
parts := strings.SplitN(v, "=", 2)
|
||||
if len(parts) == 2 {
|
||||
env[parts[0]] = parts[1]
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"DB_TYPE", "DB_PATH", "DATABASE_URL", "PANEL_DEPLOY_DIR", "PANEL_BACKEND_CONTAINER"} {
|
||||
if env[key] != c.Services["backend"].Environment[key] {
|
||||
return errors.New("live database/deployment environment does not match Compose")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunHelper runs before app/database initialization and does not depend on the old backend process.
|
||||
func RunHelper(planPath string) error {
|
||||
b, e := os.ReadFile(planPath)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
var p Plan
|
||||
if e = json.Unmarshal(b, &p); e != nil {
|
||||
return e
|
||||
}
|
||||
if !idPattern.MatchString(p.Job.ID) || ValidateVersion(p.Job.TargetVersion) != nil || !projectPattern.MatchString(p.Project) {
|
||||
return errors.New("invalid plan")
|
||||
}
|
||||
m := Manager{Dir: p.HostDir, Backend: p.Backend}
|
||||
dir := m.jobDir(p.Job.ID)
|
||||
if filepath.Clean(planPath) != filepath.Join(dir, "plan.json") {
|
||||
return errors.New("invalid plan location")
|
||||
}
|
||||
// Reject duplicate helper invocations without affecting the live job.
|
||||
f, e := os.OpenFile(filepath.Join(dir, "helper.started"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
f.Close()
|
||||
j, e := m.read(p.Job.ID)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if !j.Active {
|
||||
return errors.New("job is not active")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), helperTimeout)
|
||||
defer cancel()
|
||||
fail := func(msg string) error {
|
||||
if e := m.write(j, "failed", msg); e != nil {
|
||||
return e
|
||||
}
|
||||
return errors.New(msg)
|
||||
}
|
||||
recovery := func(msg string) error {
|
||||
m.write(j, "recovery_required", msg+";请管理员核对容器、数据库和任务备份后人工恢复,禁止自动回滚或重复升级")
|
||||
return errors.New(msg)
|
||||
}
|
||||
if e = m.write(j, "preparing", "校验部署配置和持久化资源"); e != nil {
|
||||
return e
|
||||
}
|
||||
old, e := config(ctx, p, p.HostDir)
|
||||
if e != nil {
|
||||
return fail("现有 Compose 配置校验失败,线上配置未变更")
|
||||
}
|
||||
next, e := config(ctx, p, dir)
|
||||
if e != nil {
|
||||
return fail("目标 Compose 配置校验失败,线上配置未变更")
|
||||
}
|
||||
if validateConfig(old, next, p) != nil {
|
||||
return fail("目标镜像、服务或持久化资源不匹配,需手动升级")
|
||||
}
|
||||
if validateLive(ctx, p, old) != nil {
|
||||
return fail("运行中后端与部署配置不一致,需手动升级")
|
||||
}
|
||||
if e = m.write(j, "pulling", "正在拉取并检查目标镜像,线上配置尚未变更"); e != nil {
|
||||
return e
|
||||
}
|
||||
if _, e = docker(ctx, composeArgs(p, dir, "pull", "backend", "frontend")...); e != nil {
|
||||
return fail("镜像拉取失败或超时,线上配置未变更")
|
||||
}
|
||||
targets := map[string]string{}
|
||||
for _, service := range []string{"backend", "frontend"} {
|
||||
out, err := docker(ctx, "image", "inspect", "--format", "{{.Id}}", next.Services[service].Image)
|
||||
if err != nil || !strings.HasPrefix(strings.TrimSpace(string(out)), "sha256:") {
|
||||
return fail("目标镜像校验失败,线上配置未变更")
|
||||
}
|
||||
targets[service] = strings.TrimSpace(string(out))
|
||||
}
|
||||
if validateLive(ctx, p, old) != nil {
|
||||
return fail("拉取期间运行中后端发生变化,已安全取消")
|
||||
}
|
||||
// Detect edits since snapshots before replacing either live file.
|
||||
for _, name := range []string{"docker-compose.yml", ".env"} {
|
||||
live, err := os.ReadFile(filepath.Join(p.HostDir, name))
|
||||
if err != nil {
|
||||
return fail("读取线上配置失败")
|
||||
}
|
||||
backup, err := os.ReadFile(filepath.Join(dir, name+".original"))
|
||||
if err != nil || string(live) != string(backup) {
|
||||
return fail("线上配置已被修改,本任务已安全取消")
|
||||
}
|
||||
}
|
||||
if e = m.write(j, "applying", "正在应用已校验配置"); e != nil {
|
||||
return e
|
||||
}
|
||||
for _, name := range []string{"docker-compose.yml", ".env"} {
|
||||
data, err := os.ReadFile(filepath.Join(dir, name))
|
||||
if err == nil {
|
||||
err = atomic(filepath.Join(p.HostDir, name), data)
|
||||
}
|
||||
if err != nil {
|
||||
for _, restore := range []string{"docker-compose.yml", ".env"} {
|
||||
original, re := os.ReadFile(filepath.Join(dir, restore+".original"))
|
||||
if re != nil || atomic(filepath.Join(p.HostDir, restore), original) != nil {
|
||||
return recovery("配置恢复失败,尚未执行容器重建")
|
||||
}
|
||||
}
|
||||
return fail("应用配置失败,已恢复原配置,未执行容器重建")
|
||||
}
|
||||
}
|
||||
// Persist the point of no automatic rollback BEFORE Docker can recreate any service.
|
||||
if e = m.write(j, "restarting", "正在重建面板服务"); e != nil {
|
||||
return recovery("无法记录重建状态,已停止执行")
|
||||
}
|
||||
if _, e = docker(ctx, composeArgs(p, p.HostDir, "up", "-d", "--no-deps", "--force-recreate", "--pull", "never", "backend", "frontend")...); e != nil {
|
||||
return recovery("容器重建失败或超时,可能已部分生效")
|
||||
}
|
||||
if e = m.write(j, "verifying", "正在验证服务版本及健康状态"); e != nil {
|
||||
return e
|
||||
}
|
||||
deadline := time.Now().Add(verificationTimeout)
|
||||
for time.Now().Before(deadline) && ctx.Err() == nil {
|
||||
ready := true
|
||||
for _, service := range []string{"backend", "frontend"} {
|
||||
c, err := inspect(ctx, next.Services[service].ContainerName)
|
||||
if err != nil || !c.State.Running || c.Image != targets[service] || c.Config.Labels["com.docker.compose.project"] != p.Project || c.Config.Labels["com.docker.compose.service"] != service {
|
||||
ready = false
|
||||
continue
|
||||
}
|
||||
if c.State.Health != nil && c.State.Health.Status != "healthy" {
|
||||
ready = false
|
||||
}
|
||||
if service == "backend" && c.State.Health == nil {
|
||||
ready = false
|
||||
}
|
||||
}
|
||||
if ready { // Frontend has no Docker healthcheck in the stock template.
|
||||
probeCtx, probeCancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
_, probeErr := docker(probeCtx, "exec", next.Services["frontend"].ContainerName, "wget", "-q", "-T", "5", "-O", "/dev/null", "http://127.0.0.1:80/")
|
||||
probeCancel()
|
||||
ready = probeErr == nil
|
||||
}
|
||||
if ready {
|
||||
return m.write(j, "succeeded", "升级完成,目标镜像和服务健康状态已验证")
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(3 * time.Second):
|
||||
}
|
||||
}
|
||||
return recovery("升级后服务健康检查失败或超时")
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
// Package panelupgrade implements durable, conservative Docker Compose upgrades.
|
||||
package panelupgrade
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var versionPattern = regexp.MustCompile(`^v?[0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z0-9][A-Za-z0-9.-]*)?$`)
|
||||
var idPattern = regexp.MustCompile(`^[a-f0-9]{32}$`)
|
||||
var projectPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]*$`)
|
||||
|
||||
func ValidateVersion(v string) error {
|
||||
if !versionPattern.MatchString(v) || len(v) > 128 {
|
||||
return errors.New("invalid release version")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
ID string `json:"id"`
|
||||
TargetVersion string `json:"targetVersion"`
|
||||
Stage string `json:"stage"`
|
||||
Active bool `json:"active"`
|
||||
RequiresRecovery bool `json:"requiresRecovery"`
|
||||
Message string `json:"message"`
|
||||
StartedAt int64 `json:"startedAt"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
HelperContainer string `json:"helperContainer,omitempty"`
|
||||
BackupDir string `json:"backupDir,omitempty"`
|
||||
}
|
||||
type Plan struct {
|
||||
Job Job
|
||||
DeployDir, HostDir, Project, Backend, Image string
|
||||
}
|
||||
type Manager struct{ Dir, Backend string }
|
||||
|
||||
func (m Manager) root() string { return filepath.Join(m.Dir, ".panel-upgrades") }
|
||||
func (m Manager) jobDir(id string) string { return filepath.Join(m.root(), id) }
|
||||
func atomic(path string, b []byte) error {
|
||||
f, e := os.CreateTemp(filepath.Dir(path), ".write-")
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
n := f.Name()
|
||||
defer os.Remove(n)
|
||||
if e = f.Chmod(0600); e == nil {
|
||||
_, e = f.Write(b)
|
||||
}
|
||||
if e == nil {
|
||||
e = f.Sync()
|
||||
}
|
||||
ce := f.Close()
|
||||
if e == nil {
|
||||
e = ce
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if e = os.Rename(n, path); e != nil {
|
||||
return e
|
||||
}
|
||||
d, e := os.Open(filepath.Dir(path))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
defer d.Close()
|
||||
return d.Sync()
|
||||
}
|
||||
func save(path string, v any) error {
|
||||
b, e := json.Marshal(v)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return atomic(path, b)
|
||||
}
|
||||
func (m Manager) lock() (func(), error) {
|
||||
if e := os.MkdirAll(m.root(), 0700); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
f, e := os.OpenFile(filepath.Join(m.root(), "lock"), os.O_CREATE|os.O_RDWR, 0600)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if e = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); e != nil {
|
||||
f.Close()
|
||||
return nil, errors.New("another upgrade operation is in progress")
|
||||
}
|
||||
return func() { syscall.Flock(int(f.Fd()), syscall.LOCK_UN); f.Close() }, nil
|
||||
}
|
||||
func docker(ctx context.Context, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, "docker", args...)
|
||||
cmd.WaitDelay = 2 * time.Second
|
||||
b, e := cmd.Output()
|
||||
if e != nil {
|
||||
return nil, errors.New("Docker operation failed (details suppressed to protect credentials)")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
type container struct {
|
||||
Image string
|
||||
Config struct {
|
||||
Labels map[string]string
|
||||
Env []string
|
||||
}
|
||||
State struct {
|
||||
Running bool
|
||||
Health *struct{ Status string }
|
||||
}
|
||||
Mounts []struct {
|
||||
Type, Source, Destination, Name string
|
||||
RW bool
|
||||
}
|
||||
}
|
||||
|
||||
func inspect(ctx context.Context, name string) (container, error) {
|
||||
var c []container
|
||||
b, e := docker(ctx, "inspect", name)
|
||||
if e != nil {
|
||||
return container{}, e
|
||||
}
|
||||
if json.Unmarshal(b, &c) != nil || len(c) != 1 {
|
||||
return container{}, errors.New("invalid Docker inspection")
|
||||
}
|
||||
return c[0], nil
|
||||
}
|
||||
func (m Manager) Identity(ctx context.Context) (Plan, error) {
|
||||
p := Plan{DeployDir: m.Dir, Backend: m.Backend}
|
||||
if !filepath.IsAbs(m.Dir) || filepath.Clean(m.Dir) != m.Dir {
|
||||
return p, errors.New("deployment directory must be an absolute clean path")
|
||||
}
|
||||
c, e := inspect(ctx, m.Backend)
|
||||
if e != nil {
|
||||
return p, e
|
||||
}
|
||||
p.Image = c.Image
|
||||
p.Project = c.Config.Labels["com.docker.compose.project"]
|
||||
if !c.State.Running || !projectPattern.MatchString(p.Project) || c.Config.Labels["com.docker.compose.service"] != "backend" || !strings.HasPrefix(p.Image, "sha256:") {
|
||||
return p, errors.New("cannot establish original Compose identity")
|
||||
}
|
||||
for _, v := range c.Mounts {
|
||||
if v.Destination == m.Dir && v.Type == "bind" && v.RW {
|
||||
p.HostDir = v.Source
|
||||
}
|
||||
}
|
||||
if p.HostDir == "/" || !filepath.IsAbs(p.HostDir) || strings.ContainsAny(p.HostDir, ",\n\r") || filepath.Clean(p.HostDir) != p.HostDir {
|
||||
return p, errors.New("deployment directory must be a writable host bind mount")
|
||||
}
|
||||
|
||||
for _, protected := range []string{"/app/paneld", "/usr/local/bin/docker", "/usr/local/libexec/docker/cli-plugins/docker-compose", "/var/run/docker.sock", "/bin", "/lib", "/etc", "/proc", "/sys", "/dev"} {
|
||||
if p.HostDir == protected || strings.HasPrefix(protected, p.HostDir+"/") || strings.HasPrefix(p.HostDir, protected+"/") {
|
||||
return p, errors.New("host deployment mount would shadow helper runtime paths")
|
||||
}
|
||||
}
|
||||
if c.Config.Labels["com.docker.compose.project.working_dir"] != p.HostDir || c.Config.Labels["com.docker.compose.project.config_files"] != filepath.Join(p.HostDir, "docker-compose.yml") {
|
||||
return p, errors.New("custom or multiple Compose paths require manual upgrade")
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
func (m Manager) read(id string) (*Job, error) {
|
||||
if !idPattern.MatchString(id) {
|
||||
return nil, errors.New("invalid job ID")
|
||||
}
|
||||
b, e := os.ReadFile(filepath.Join(m.jobDir(id), "status.json"))
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
var j Job
|
||||
e = json.Unmarshal(b, &j)
|
||||
return &j, e
|
||||
}
|
||||
func (m Manager) Status(ctx context.Context, id string) (*Job, error) {
|
||||
if id == "" {
|
||||
b, e := os.ReadFile(filepath.Join(m.root(), "latest"))
|
||||
if os.IsNotExist(e) {
|
||||
return nil, nil
|
||||
}
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
id = string(b)
|
||||
}
|
||||
j, e := m.read(id)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if !j.Active || time.Now().UnixMilli()-j.UpdatedAt < 90000 {
|
||||
return j, nil
|
||||
}
|
||||
// An unavailable daemon, missing helper, or uncertain launch must never release the task lock.
|
||||
c, e := inspect(ctx, j.HelperContainer)
|
||||
if e == nil && c.State.Running {
|
||||
return j, nil
|
||||
}
|
||||
j.RequiresRecovery = true
|
||||
j.Message = "升级 helper 已停止或无法确认状态。请管理员检查任务备份和容器状态;在确认安全前禁止再次升级。"
|
||||
// This is a derived view: do not race the helper's durable state writes.
|
||||
j.Stage = "recovery_required"
|
||||
return j, nil
|
||||
}
|
||||
func (m Manager) write(j *Job, stage, msg string) error {
|
||||
j.Stage = stage
|
||||
j.Message = msg
|
||||
j.UpdatedAt = time.Now().UnixMilli()
|
||||
j.Active = stage != "succeeded" && stage != "failed"
|
||||
j.RequiresRecovery = stage == "recovery_required"
|
||||
if e := save(filepath.Join(m.jobDir(j.ID), "status.json"), j); e != nil {
|
||||
return e
|
||||
}
|
||||
f, e := os.OpenFile(filepath.Join(m.jobDir(j.ID), "events.log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
defer f.Close()
|
||||
_, e = fmt.Fprintf(f, "%d %s %s\n", j.UpdatedAt, stage, msg)
|
||||
return e
|
||||
}
|
||||
func (m Manager) Start(ctx context.Context, version string, compose []byte) (*Job, string, error) {
|
||||
if e := ValidateVersion(version); e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
unlock, e := m.lock()
|
||||
if e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
defer unlock()
|
||||
old, e := m.Status(ctx, "")
|
||||
if e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
if old != nil && (old.Active || old.RequiresRecovery) {
|
||||
return old, "", errors.New("已有面板升级任务执行中或需要人工恢复")
|
||||
}
|
||||
p, e := m.Identity(ctx)
|
||||
if e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
b := make([]byte, 16)
|
||||
if _, e = rand.Read(b); e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
id := hex.EncodeToString(b)
|
||||
dir := m.jobDir(id)
|
||||
if e = os.Mkdir(dir, 0700); e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
j := Job{ID: id, TargetVersion: version, StartedAt: time.Now().UnixMilli(), HelperContainer: "flvx-upgrade-" + id, BackupDir: filepath.Join(p.HostDir, ".panel-upgrades", id)}
|
||||
p.Job = j
|
||||
if e = m.write(&j, "preparing", "正在准备独立升级任务"); e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
if e = atomic(filepath.Join(m.root(), "latest"), []byte(id)); e != nil {
|
||||
return nil, "", e
|
||||
}
|
||||
fail := func(err error) (*Job, string, error) {
|
||||
m.write(&j, "failed", "准备失败,线上配置未变更")
|
||||
return &j, p.Image, err
|
||||
}
|
||||
for _, name := range []string{"docker-compose.yml", ".env"} {
|
||||
data, err := os.ReadFile(filepath.Join(m.Dir, name))
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
if err = atomic(filepath.Join(dir, name+".original"), data); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
}
|
||||
env, e := os.ReadFile(filepath.Join(dir, ".env.original"))
|
||||
if e != nil {
|
||||
return fail(e)
|
||||
}
|
||||
lines := strings.Split(string(env), "\n")
|
||||
for i, line := range lines {
|
||||
if strings.HasPrefix(strings.TrimSpace(line), "FLUX_VERSION=") || strings.HasPrefix(strings.TrimSpace(line), "export FLUX_VERSION=") {
|
||||
lines[i] = ""
|
||||
}
|
||||
}
|
||||
env = []byte(strings.Join(lines, "\n") + "\nFLUX_VERSION=" + version + "\n")
|
||||
if e = atomic(filepath.Join(dir, ".env"), env); e != nil {
|
||||
return fail(e)
|
||||
}
|
||||
if e = atomic(filepath.Join(dir, "docker-compose.yml"), compose); e != nil {
|
||||
return fail(e)
|
||||
}
|
||||
p.Job = j
|
||||
if e = save(filepath.Join(dir, "plan.json"), p); e != nil {
|
||||
return fail(e)
|
||||
}
|
||||
// No --rm: an exited helper is evidence needed after a daemon/client timeout.
|
||||
args := []string{"run", "-d", "--name", j.HelperContainer, "--label", "io.flvx.upgrade-job=" + id, "--mount", "type=bind,src=" + p.HostDir + ",dst=" + p.HostDir, "--mount", "type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock", "--entrypoint", "/app/paneld", p.Image, "panel-upgrade-helper", filepath.Join(p.HostDir, ".panel-upgrades", id, "plan.json")}
|
||||
if _, e = docker(ctx, args...); e != nil {
|
||||
c, ie := inspect(ctx, j.HelperContainer)
|
||||
if ie != nil || !c.State.Running {
|
||||
return &j, p.Image, errors.New("helper 启动结果不确定;任务已保留,请查询升级状态,勿重复启动")
|
||||
}
|
||||
}
|
||||
return &j, p.Image, nil
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
package panelupgrade
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func fakeDocker(t *testing.T, body string) {
|
||||
t.Helper()
|
||||
d := t.TempDir()
|
||||
if e := os.WriteFile(filepath.Join(d, "docker"), []byte("#!/bin/sh\n"+body), 0700); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
t.Setenv("PATH", d+":"+os.Getenv("PATH"))
|
||||
}
|
||||
func fixture(t *testing.T) (Manager, Plan) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
m := Manager{Dir: dir, Backend: "flux-panel-backend"}
|
||||
id := strings.Repeat("a", 32)
|
||||
os.MkdirAll(m.jobDir(id), 0700)
|
||||
j := Job{ID: id, TargetVersion: "2.5.0", HelperContainer: "helper", StartedAt: time.Now().UnixMilli()}
|
||||
m.write(&j, "preparing", "ready")
|
||||
atomic(filepath.Join(m.root(), "latest"), []byte(id))
|
||||
p := Plan{Job: j, HostDir: dir, DeployDir: "/opt/flvx-panel", Project: "original-project", Backend: m.Backend, Image: "sha256:old"}
|
||||
save(filepath.Join(m.jobDir(id), "plan.json"), p)
|
||||
for _, n := range []string{".env", "docker-compose.yml"} {
|
||||
atomic(filepath.Join(dir, n), []byte("original"))
|
||||
atomic(filepath.Join(m.jobDir(id), n+".original"), []byte("original"))
|
||||
atomic(filepath.Join(m.jobDir(id), n), []byte("target"))
|
||||
}
|
||||
return m, p
|
||||
}
|
||||
func TestVersionRejectsComposeAndPathInjection(t *testing.T) {
|
||||
for _, v := range []string{"latest", "../v1", "1.2.3\nSECRET=x", "1.2.3${TOKEN}", "1.2.3 x", "1.2.3;id"} {
|
||||
if ValidateVersion(v) == nil {
|
||||
t.Fatal(v)
|
||||
}
|
||||
}
|
||||
for _, v := range []string{"2.5.0-rc10", "v2.5.0", "2.1.9-beta6"} {
|
||||
if ValidateVersion(v) != nil {
|
||||
t.Fatal(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestPersistentLockAcrossManagers(t *testing.T) {
|
||||
m, p := fixture(t)
|
||||
fakeDocker(t, "exit 1\n")
|
||||
_, _, e := m.Start(context.Background(), "2.5.0", nil)
|
||||
if e == nil {
|
||||
t.Fatal("accepted concurrent job")
|
||||
}
|
||||
b, _ := os.ReadFile(filepath.Join(m.root(), "latest"))
|
||||
if string(b) != p.Job.ID {
|
||||
t.Fatal("changed active job")
|
||||
}
|
||||
}
|
||||
func TestUncertainHelperNeverUnlocks(t *testing.T) {
|
||||
m, p := fixture(t)
|
||||
j, _ := m.read(p.Job.ID)
|
||||
j.UpdatedAt = 1
|
||||
save(filepath.Join(m.jobDir(j.ID), "status.json"), j)
|
||||
fakeDocker(t, "exit 1\n")
|
||||
j, e := m.Status(context.Background(), "")
|
||||
if e != nil || !j.Active || !j.RequiresRecovery || j.Stage != "recovery_required" {
|
||||
t.Fatalf("%+v %v", j, e)
|
||||
}
|
||||
if _, _, e = m.Start(context.Background(), "2.5.0", nil); e == nil {
|
||||
t.Fatal("uncertain launch unlocked")
|
||||
}
|
||||
}
|
||||
func configs(p Plan) (string, string) {
|
||||
old := composeConfig{}
|
||||
json.Unmarshal([]byte(`{"services":{"backend":{"image":"old","container_name":"flux-panel-backend","environment":{"FLUX_VERSION":"old"}},"frontend":{"image":"old","container_name":"vite-frontend"}}}`), &old)
|
||||
a, _ := json.Marshal(old)
|
||||
next := old
|
||||
next.Services = make(map[string]struct {
|
||||
Image string `json:"image"`
|
||||
ContainerName string `json:"container_name"`
|
||||
Volumes []map[string]any `json:"volumes"`
|
||||
Environment map[string]string `json:"environment"`
|
||||
})
|
||||
for k, v := range old.Services {
|
||||
next.Services[k] = v
|
||||
}
|
||||
v := next.Services["backend"]
|
||||
v.Image = "ghcr.io/sagit-chu/flux-panel-backend:" + p.Job.TargetVersion
|
||||
v.Environment = map[string]string{"FLUX_VERSION": p.Job.TargetVersion}
|
||||
next.Services["backend"] = v
|
||||
v = next.Services["frontend"]
|
||||
v.Image = "ghcr.io/sagit-chu/vite-frontend:" + p.Job.TargetVersion
|
||||
next.Services["frontend"] = v
|
||||
b, _ := json.Marshal(next)
|
||||
return string(a), string(b)
|
||||
}
|
||||
func TestHelperFailureBoundaries(t *testing.T) {
|
||||
for _, stage := range []string{"config", "pull", "apply", "up", "health", "success"} {
|
||||
t.Run(stage, func(t *testing.T) {
|
||||
m, p := fixture(t)
|
||||
old, next := configs(p)
|
||||
dir := m.jobDir(p.Job.ID)
|
||||
if stage == "apply" {
|
||||
os.Remove(filepath.Join(dir, ".env"))
|
||||
os.Mkdir(filepath.Join(dir, ".env"), 0700)
|
||||
}
|
||||
if stage == "health" {
|
||||
before := verificationTimeout
|
||||
verificationTimeout = 0
|
||||
t.Cleanup(func() { verificationTimeout = before })
|
||||
}
|
||||
script := `case "$*" in
|
||||
*"config --format json"*)
|
||||
case "$*" in *"` + p.Job.ID + `"*) echo '` + next + `';; *) echo '` + old + `';; esac
|
||||
;;
|
||||
*"pull backend frontend"*) `
|
||||
if stage == "pull" {
|
||||
script += "echo SECRET >&2; exit 1"
|
||||
} else {
|
||||
script += "exit 0"
|
||||
}
|
||||
script += `;;
|
||||
"image inspect"*) echo sha256:target;;
|
||||
*"up -d"*) `
|
||||
if stage == "up" {
|
||||
script += "exit 1"
|
||||
} else {
|
||||
script += "touch \"" + filepath.Join(dir, "recreated") + "\"; exit 0"
|
||||
}
|
||||
script += `;;
|
||||
"inspect flux-panel-backend") if [ -f "` + filepath.Join(dir, "recreated") + `" ]; then IMG=sha256:target; else IMG=sha256:old; fi; echo '[{"Image":"'"$IMG"'","State":{"Running":true,"Health":{"Status":"healthy"}},"Config":{"Labels":{"com.docker.compose.project":"original-project","com.docker.compose.service":"backend"}}}]';;
|
||||
"inspect vite-frontend") echo '[{"Image":"sha256:target","State":{"Running":true},"Config":{"Labels":{"com.docker.compose.project":"original-project","com.docker.compose.service":"frontend"}}}]';;
|
||||
"exec vite-frontend wget"*) exit 0;;
|
||||
*) exit 1;; esac
|
||||
`
|
||||
if stage == "config" {
|
||||
script = "echo SECRET >&2;exit 1\n"
|
||||
}
|
||||
fakeDocker(t, script)
|
||||
e := RunHelper(filepath.Join(dir, "plan.json"))
|
||||
j, _ := m.read(p.Job.ID)
|
||||
want := "failed"
|
||||
if stage == "up" || stage == "health" {
|
||||
want = "recovery_required"
|
||||
}
|
||||
if stage == "success" {
|
||||
want = "succeeded"
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if j.Stage != want {
|
||||
t.Fatalf("stage=%s error=%v", j.Stage, e)
|
||||
}
|
||||
for _, n := range []string{".env", "docker-compose.yml"} {
|
||||
b, _ := os.ReadFile(filepath.Join(m.Dir, n))
|
||||
expected := "original"
|
||||
if stage == "up" || stage == "success" || stage == "health" {
|
||||
expected = "target"
|
||||
}
|
||||
if string(b) != expected {
|
||||
t.Fatalf("%s mutated unexpectedly", n)
|
||||
}
|
||||
b, _ = os.ReadFile(filepath.Join(dir, n+".original"))
|
||||
if string(b) != "original" {
|
||||
t.Fatal("backup overwritten")
|
||||
}
|
||||
}
|
||||
b, _ := os.ReadFile(filepath.Join(dir, "events.log"))
|
||||
if strings.Contains(string(b), "SECRET") {
|
||||
t.Fatal("secret leaked")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestComposeUsesOriginalHostPathAndProject(t *testing.T) {
|
||||
p := Plan{Project: "my-project", HostDir: "/srv/panel"}
|
||||
s := strings.Join(composeArgs(p, "/srv/panel/.panel-upgrades/job", "pull", "backend", "frontend"), " ")
|
||||
if !strings.Contains(s, "--project-name my-project --project-directory /srv/panel") || strings.Contains(s, "remove-orphans") {
|
||||
t.Fatal(s)
|
||||
}
|
||||
}
|
||||
func TestStatusWithoutJobNeedsNoDocker(t *testing.T) {
|
||||
fakeDocker(t, "exit 99\n")
|
||||
j, e := (Manager{Dir: t.TempDir()}).Status(context.Background(), "")
|
||||
if e != nil || j != nil {
|
||||
t.Fatalf("%v %v", j, e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFullConfigRejectsUnintendedChanges(t *testing.T) {
|
||||
for _, field := range []string{"ports", "network_mode", "command", "entrypoint", "privileged", "networks"} {
|
||||
t.Run(field, func(t *testing.T) {
|
||||
_, p := fixture(t)
|
||||
oldJSON, nextJSON := configs(p)
|
||||
var old, next composeConfig
|
||||
json.Unmarshal([]byte(oldJSON), &old)
|
||||
json.Unmarshal([]byte(nextJSON), &next)
|
||||
var a, b struct {
|
||||
Services map[string]map[string]any `json:"services"`
|
||||
}
|
||||
json.Unmarshal([]byte(oldJSON), &a)
|
||||
json.Unmarshal([]byte(nextJSON), &b)
|
||||
old.FullServices = a.Services
|
||||
next.FullServices = b.Services
|
||||
old.FullServices["frontend"][field] = "private-custom-setting"
|
||||
if validateConfig(old, next, p) == nil {
|
||||
t.Fatal("accepted service setting removal: " + field)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestLiveRejectsShadowMount(t *testing.T) {
|
||||
_, p := fixture(t)
|
||||
fakeDocker(t, `echo '[{"Image":"sha256:old","State":{"Running":true},"Config":{"Labels":{"com.docker.compose.project":"original-project","com.docker.compose.service":"backend"}},"Mounts":[{"Type":"bind","Source":"/srv/db/gost.db","Destination":"/app/data/gost.db","RW":true}]}]'`)
|
||||
oldJSON, _ := configs(p)
|
||||
var old composeConfig
|
||||
json.Unmarshal([]byte(oldJSON), &old)
|
||||
if validateLive(context.Background(), p, old) == nil {
|
||||
t.Fatal("accepted undeclared shadow mount")
|
||||
}
|
||||
}
|
||||
func identityJSON(dir, host, project, files string) string {
|
||||
c := map[string]any{"Image": "sha256:old", "State": map[string]any{"Running": true}, "Config": map[string]any{"Labels": map[string]string{"com.docker.compose.project": project, "com.docker.compose.service": "backend", "com.docker.compose.project.working_dir": host, "com.docker.compose.project.config_files": files}}, "Mounts": []map[string]any{{"Type": "bind", "Source": host, "Destination": dir, "RW": true}}}
|
||||
b, _ := json.Marshal([]any{c})
|
||||
return string(b)
|
||||
}
|
||||
func TestStartUsesHostMountAndKeepsUniqueBackups(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
m := Manager{Dir: dir, Backend: "backend"}
|
||||
for _, n := range []string{".env", "docker-compose.yml", ".env.upgrade.bak"} {
|
||||
os.WriteFile(filepath.Join(dir, n), []byte("original-secret"), 0600)
|
||||
}
|
||||
args := filepath.Join(t.TempDir(), "args")
|
||||
fakeDocker(t, `case "$1" in inspect) echo '`+identityJSON(dir, "/srv/original-panel", "original-project", "/srv/original-panel/docker-compose.yml")+`';; run) printf '%s\n' "$@" > '`+args+`'; echo container-id;; *) exit 1;; esac`)
|
||||
j, _, e := m.Start(context.Background(), "2.5.0-rc10", []byte("target"))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
b, _ := os.ReadFile(args)
|
||||
s := string(b)
|
||||
for _, expected := range []string{"type=bind,src=/srv/original-panel,dst=/srv/original-panel", "/app/paneld", "sha256:old", "panel-upgrade-helper"} {
|
||||
if !strings.Contains(s, expected) {
|
||||
t.Fatal(s)
|
||||
}
|
||||
}
|
||||
if strings.Contains(s, "--rm") || strings.Contains(s, "--volumes-from") {
|
||||
t.Fatal(s)
|
||||
}
|
||||
first := j.ID
|
||||
m.write(j, "failed", "safe failure")
|
||||
j, _, e = m.Start(context.Background(), "2.5.1", []byte("target2"))
|
||||
if e != nil || j.ID == first {
|
||||
t.Fatalf("%v %v", j, e)
|
||||
}
|
||||
for _, n := range []string{".env", "docker-compose.yml", ".env.upgrade.bak"} {
|
||||
b, _ := os.ReadFile(filepath.Join(dir, n))
|
||||
if string(b) != "original-secret" {
|
||||
t.Fatal("live or legacy overwritten")
|
||||
}
|
||||
}
|
||||
b, _ = os.ReadFile(filepath.Join(m.jobDir(first), ".env.original"))
|
||||
if string(b) != "original-secret" {
|
||||
t.Fatal("old job backup changed")
|
||||
}
|
||||
}
|
||||
func TestIdentityFailsClosed(t *testing.T) {
|
||||
for _, host := range []string{"/", "/app", "/usr", "/var/run", "/proc", "relative", "/srv/bad,host"} {
|
||||
t.Run(host, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
fakeDocker(t, "echo '"+identityJSON(dir, host, "project", filepath.Join(host, "docker-compose.yml"))+"'\n")
|
||||
if _, e := (Manager{Dir: dir, Backend: "backend"}).Identity(context.Background()); e == nil {
|
||||
t.Fatal(host)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestUncertainRunIsNotRetriedOrUnlocked(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
m := Manager{Dir: dir, Backend: "backend"}
|
||||
for _, n := range []string{".env", "docker-compose.yml"} {
|
||||
os.WriteFile(filepath.Join(dir, n), []byte("original"), 0600)
|
||||
}
|
||||
calls := filepath.Join(t.TempDir(), "runs")
|
||||
fakeDocker(t, `case "$*" in "inspect backend") echo '`+identityJSON(dir, "/srv/panel", "project", "/srv/panel/docker-compose.yml")+`';; run*) echo run >> '`+calls+`'; exit 1;; *) exit 1;; esac`)
|
||||
j, _, e := m.Start(context.Background(), "2.5.0", []byte("target"))
|
||||
if e == nil || j == nil || !j.Active {
|
||||
t.Fatalf("%v %v", j, e)
|
||||
}
|
||||
_, _, e = m.Start(context.Background(), "2.5.0", nil)
|
||||
if e == nil {
|
||||
t.Fatal("retry accepted")
|
||||
}
|
||||
b, _ := os.ReadFile(calls)
|
||||
if string(b) != "run\n" {
|
||||
t.Fatal("retried uncertain docker run")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user