From 2b981f8dff70e22d94a1c58dd82c613f88f7a7d8 Mon Sep 17 00:00:00 2001 From: vvvomit Date: Thu, 29 Oct 2026 04:54:44 +0330 Subject: [PATCH] =?UTF-8?q?Updates=20=E2=80=BA=20d:=20dry-run=20the=20upda?= =?UTF-8?q?te=20in=20check=20mode,=20in=20a=20copy=20of=20the=20playbook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Shows what an update would change on the server before it does. The new playbook goes in a copy beside yours, borrowing its git objects, so a dry run never leaves your checkout half-updated. Co-Authored-By: Claude Opus 5.5 --- README.md | 7 +++- internal/deploy/deploy.go | 17 +++++++++- internal/deploy/dryrun.go | 58 ++++++++++++++++++++++++++++++++++ internal/deploy/dryrun_test.go | 38 ++++++++++++++++++++++ internal/ui/screens_deploy.go | 24 ++++++++++++++ internal/ui/screens_tools.go | 6 +++- 6 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 internal/deploy/dryrun.go create mode 100644 internal/deploy/dryrun_test.go diff --git a/README.md b/README.md index 2e90d85..0889c2a 100644 --- a/README.md +++ b/README.md @@ -218,7 +218,12 @@ or the [Telegram group](https://t.me/+6UwIu9F4gZQxNjM0). - **Update preview:** the playbook's new `CHANGELOG.md` entries, the new commits and the component versions they change, before installing anything. Entries about your homeserver or a component you've turned - on come first, marked, with their first paragraph. + on come first, marked, with their first paragraph. `d` there is a + dry run: the newest playbook is checked out beside yours (yours isn't + touched), its roles fetched, and the update run in Ansible's check + mode, which lists the tasks that would change something on the server + without changing it. The playbook doesn't fully support check mode, + so a run may stop early; it then says where. - **Rollback:** every successful install records its playbook commit and answers (the last 10). Rolling back backs up the database, checks that commit out and reinstalls; the playbook stays held there until diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 9802294..3450946 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -50,6 +50,8 @@ const ( StepUninstall StepDisk StepAlerts + StepDryPlaybook + StepDryRun ) var stepNames = map[Step]string{ @@ -78,6 +80,8 @@ var stepNames = map[Step]string{ StepUninstall: "Remove everything", StepDisk: "Measure the disk", StepAlerts: "Set up alerts", + StepDryPlaybook: "Get the newest playbook, beside yours", + StepDryRun: "Run it without changing anything", } func (s Step) String() string { return stepNames[s] } @@ -105,6 +109,7 @@ const ( ActionUninstall // the playbook's remove-all (docs/uninstalling.md) ActionDisk // what takes the server's disk space ActionAlerts // the server's own checks and alerts (Config.Alerts) + ActionDryRun // an update in check mode, in a copy of the playbook ) // Plan lists the steps an action runs, in order. @@ -144,6 +149,8 @@ func Plan(a Action) []Step { return []Step{StepAnsible, StepInventory, StepDisk} case ActionAlerts: return []Step{StepAnsible, StepInventory, StepAlerts} + case ActionDryRun: + return []Step{StepAnsible, StepDryPlaybook, StepInventory, StepRoles, StepDryRun} case ActionMigrate: return []Step{StepAnsible, StepPlaybook, StepInventory, StepRoles, StepStopOld, StepCopyData, StepMirrors, StepInstall, StepSelfCheck} } @@ -176,6 +183,9 @@ type Result struct { Images []string // image check: images the registries have MissingImages []string // image check: "ref: why" Version string // self-update: the version installed + + WouldChange []string // dry run: the tasks that would change something + StoppedAt string // dry run: the task check mode couldn't get past } type NewUser struct { @@ -264,10 +274,13 @@ type Runner struct { func Start(ctx context.Context, cfg *config.Config, opts Options) <-chan Event { // A migration and blackout mirror picks change the run's config; the // caller applies those changes through events. - if opts.Action == ActionMigrate || cfg.Sources.Blackout { + if opts.Action == ActionMigrate || opts.Action == ActionDryRun || cfg.Sources.Blackout { cp := *cfg cfg = &cp } + if opts.Action == ActionDryRun { + cfg.PlaybookDir = DryRunDir(cfg) + } r := &Runner{cfg: cfg, opts: opts, events: make(chan Event, 256)} go func() { defer close(r.events) @@ -317,6 +330,8 @@ func (r *Runner) run(ctx context.Context) error { StepUninstall: r.uninstall, StepDisk: r.disk, StepAlerts: r.alerts, + StepDryPlaybook: r.dryPlaybook, + StepDryRun: r.dryRun, } plan := Plan(r.opts.Action) r.emit(Event{Plan: plan}) diff --git a/internal/deploy/dryrun.go b/internal/deploy/dryrun.go new file mode 100644 index 0000000..c2c99d9 --- /dev/null +++ b/internal/deploy/dryrun.go @@ -0,0 +1,58 @@ +package deploy + +import ( + "context" + "os" + "path/filepath" + "slices" + "strings" + + "interzone/internal/config" +) + +// A dry run is an update in Ansible's check mode: the newest playbook is +// checked out beside yours (so yours stays as it is), its roles fetched, +// and the playbook run with --check, which reports what it would change +// on the server without changing it. + +// DryRunDir is the playbook copy dry runs use. +func DryRunDir(c *config.Config) string { return strings.TrimRight(c.PlaybookDir, "/") + "-dry-run" } + +func (r *Runner) dryPlaybook(ctx context.Context) error { + r.begin(StepDryPlaybook) + dir := r.cfg.PlaybookDir + if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil { + _ = os.RemoveAll(dir) + // --shared borrows the objects of your copy, so this takes no time or space. + src := strings.TrimSuffix(dir, "-dry-run") + if err := r.exec(ctx, StepDryPlaybook, "", "git", "clone", "--quiet", "--shared", src, dir); err != nil { + return err + } + } + if err := r.exec(ctx, StepDryPlaybook, dir, "git", "fetch", "--quiet", r.cfg.Sources.PlaybookGit, defaultBranch(dir)); err != nil { + return err + } + return r.exec(ctx, StepDryPlaybook, dir, "git", "checkout", "--quiet", "--force", "--detach", "FETCH_HEAD") +} + +func (r *Runner) dryRun(ctx context.Context) error { + task := "" + r.onLine = func(ev Event) { + switch { + case taskLine.MatchString(ev.Line): + task = taskLine.FindStringSubmatch(ev.Line)[1] + case strings.HasPrefix(ev.Line, "changed:") && task != "" && !slices.Contains(r.result.WouldChange, task): + r.result.WouldChange = append(r.result.WouldChange, task) + } + } + err := r.playbookRun(ctx, StepDryRun, "--tags=install-all,ensure-matrix-users-created,start", "--check") + r.onLine = nil + if err != nil && ctx.Err() == nil && task != "" { + // Check mode can't get past tasks that need an earlier one's real + // result; what was checked up to there still stands. + r.result.StoppedAt = task + r.note(StepDryRun, "! check mode stopped at "+task+"; the tasks after it weren't checked") + return nil + } + return err +} diff --git a/internal/deploy/dryrun_test.go b/internal/deploy/dryrun_test.go new file mode 100644 index 0000000..5de7f32 --- /dev/null +++ b/internal/deploy/dryrun_test.go @@ -0,0 +1,38 @@ +package deploy + +import ( + "context" + "os" + "path/filepath" + "slices" + "testing" + + "interzone/internal/config" +) + +// A dry run lists the tasks reporting changes, and a stop in check mode +// is a note, not a failure. +func TestDryRun(t *testing.T) { + bin := t.TempDir() + stub := "#!/bin/sh\ncat <<'OUT'\nTASK [matrix-synapse : Ensure Synapse config]\nok: [x]\nTASK [matrix-synapse : Ensure Synapse image]\nchanged: [x]\nTASK [traefik : Ensure Traefik is started]\nchanged: [x] => (item=a)\nchanged: [x] => (item=b)\nTASK [postgres : Run a command]\nfatal: [x]: FAILED!\nOUT\nexit 2\n" + if err := os.WriteFile(filepath.Join(bin, "ansible-playbook"), []byte(stub), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", bin+":"+os.Getenv("PATH")) + c := config.Default() + c.PlaybookDir = t.TempDir() + r := &Runner{cfg: c, events: make(chan Event, 256)} + r.env = Env(c) + go func() { + for range r.events { + } + }() + if err := r.dryRun(context.Background()); err != nil { + t.Fatal(err) + } + close(r.events) + want := []string{"matrix-synapse : Ensure Synapse image", "traefik : Ensure Traefik is started"} + if !slices.Equal(r.result.WouldChange, want) || r.result.StoppedAt != "postgres : Run a command" { + t.Errorf("got %q, stopped at %q", r.result.WouldChange, r.result.StoppedAt) + } +} diff --git a/internal/ui/screens_deploy.go b/internal/ui/screens_deploy.go index 337d4f1..942abd4 100644 --- a/internal/ui/screens_deploy.go +++ b/internal/ui/screens_deploy.go @@ -375,6 +375,27 @@ func (s *reviewScreen) checksView(w int) []string { return lines } +// dryRunLines list what an update would change on the server. +func dryRunLines(res *deploy.Result) []string { + var lines []string + if len(res.WouldChange) == 0 { + lines = append(lines, sGood.Render("✓ Nothing on the server would change.")) + } else { + lines = append(lines, sTitle.Render(fmt.Sprintf("%d task(s) would change something:", len(res.WouldChange)))) + for i, t := range res.WouldChange { + if i == 25 { + lines = append(lines, sFaint.Render(fmt.Sprintf(" …and %d more (see the output above)", len(res.WouldChange)-i))) + break + } + lines = append(lines, sMuted.Render(" • "+t)) + } + } + if res.StoppedAt != "" { + lines = append(lines, "", sWarn.Render("! Check mode couldn't get past "+res.StoppedAt+", so the tasks after it weren't checked.")) + } + return append(lines, "", sMuted.Render("Nothing was changed, and your playbook copy wasn't updated. Updates › enter updates for real.")) +} + // portsView lists the ports a firewall in front of the server must let in // for the components turned on. func portsView(c *config.Config, w int) []string { @@ -524,6 +545,7 @@ var actionTitles = map[deploy.Action][3]string{ // running, done, failed deploy.ActionUninstall: {"Removing everything…", "Everything was removed", "The removal stopped"}, deploy.ActionDisk: {"Measuring the disk…", "Disk usage", "Couldn't measure the disk"}, deploy.ActionAlerts: {"Setting up alerts…", "Alerts set", "Couldn't set up alerts"}, + deploy.ActionDryRun: {"Trying the update without changing anything…", "What the update would change", "The dry run stopped"}, } func (s *deployScreen) name() string { return "Deploy" } @@ -921,6 +943,8 @@ func (s *deployScreen) resultView(w int) string { return panel(cMint, sGood.Render("✓ The server backs up its database ")+sTitle.Render(c.Backups.When)+ sGood.Render(fmt.Sprintf(", keeping %d.", c.Backups.Keep)), sMuted.Render("Dumps go to "+deploy.ServerBackupDir+"; download them from Manage › Restore with d.")) + case deploy.ActionDryRun: + return panel(cMint, dryRunLines(res)...) case deploy.ActionAlerts: if !c.Alerts.Enabled { return panel(cMint, sGood.Render("✓ Alerts are off; the server no longer checks itself.")) diff --git a/internal/ui/screens_tools.go b/internal/ui/screens_tools.go index e3db7b3..c9547db 100644 --- a/internal/ui/screens_tools.go +++ b/internal/ui/screens_tools.go @@ -115,7 +115,7 @@ func (s *updatesScreen) keys() [][2]string { if s.loading || s.err != nil { return [][2]string{{"r", "check again"}} } - return [][2]string{{"enter", "update now"}, {"↑↓", "scroll"}, {"r", "check again"}} + return [][2]string{{"enter", "update now"}, {"d", "dry run"}, {"↑↓", "scroll"}, {"r", "check again"}} } func (s *updatesScreen) leave(*config.Config) {} @@ -150,6 +150,10 @@ func (s *updatesScreen) update(msg tea.Msg) tea.Cmd { if !s.loading { return s.check() } + case "d": + if !s.loading { + return run(deploy.Options{Action: deploy.ActionDryRun}) + } case "enter": if !s.loading { s.cfg.PinnedCommit = ""