Updates › d: dry-run the update in check mode, in a copy of the playbook

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 <noreply@anthropic.com>
This commit is contained in:
vvvomitandClaude Opus 5.5 committed 2026-10-29 04:54:44 +03:30
1 parent 0181775300
commit 2b981f8dff
6 files changed
+147 -3

No files matched your search

+6 -1
View File
@@ -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
+16 -1
View File
@@ -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})
+58
View File
@@ -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
}
+38
View File
@@ -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)
}
}
+24
View File
@@ -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."))
+5 -1
View File
@@ -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 = ""