A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(rexctl): show what doctor --fix changes with --dry-run (#3861)
Hampusbc5fbd1e9c23
diff --git a/fluxer_docs/src/content/docs/operator/rexctl.mdx b/fluxer_docs/src/content/docs/operator/rexctl.mdxindex d596b2cef..4dacf6908 100644--- a/fluxer_docs/src/content/docs/operator/rexctl.mdx+++ b/fluxer_docs/src/content/docs/operator/rexctl.mdx@@ -262,6 +262,10 @@ Check your instance and this machine for problems.rexctl doctor [flags]```+- `--dry-run`++ Show what --fix would change without changing anything.+- `--fix`Fix the problems rexctl can fix safely.diff --git a/fluxer_docs/src/content/docs/operator/running.mdx b/fluxer_docs/src/content/docs/operator/running.mdxindex 10b5e50cb..9c9bfb564 100644--- a/fluxer_docs/src/content/docs/operator/running.mdx+++ b/fluxer_docs/src/content/docs/operator/running.mdx@@ -215,7 +215,7 @@ To add your own Caddy directives, such as extra routes or headers, put them in arexctl doctor```-This checks your instance and the machine, and tells you how to fix each problem it finds. `rexctl doctor --fix` fixes the problems rexctl can fix safely.+This checks your instance and the machine, and tells you how to fix each problem it finds. `rexctl doctor --fix` fixes the problems rexctl can fix safely. To see what it changes first, run `rexctl doctor --fix --dry-run`.If `rexctl doctor` says memory overcommit is off, the machine has little free memory compared with Fluxer's cache. The cache saves its data in the background by briefly copying itself, and with `vm.overcommit_memory` set to 0 Linux can refuse that copy when memory runs low. `rexctl doctor --fix` sets it to 1, which is what Valkey recommends. With plenty of free memory and swap, rexctl doesn't warn about it.diff --git a/fluxer_rexctl/cmd/doctor.go b/fluxer_rexctl/cmd/doctor.goindex 2c33b28d7..d20babdfc 100644--- a/fluxer_rexctl/cmd/doctor.go+++ b/fluxer_rexctl/cmd/doctor.go@@ -5,6 +5,7 @@ package cmdimport ("context""errors"+ "slices""strings""github.com/spf13/cobra"@@ -12,6 +13,7 @@ import ("github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/checks""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui")@@ -22,21 +24,22 @@ type doctorJSON struct {}func newDoctorCmd(app *App) *cobra.Command {- var fix bool+ var fix, dryRun boolc := &cobra.Command{Use: "doctor",Short: "Check your instance and this machine for problems",Args: noArgs,RunE: func(c *cobra.Command, _ []string) error {- return runDoctor(c.Context(), app, fix)+ return runDoctor(c.Context(), app, fix || dryRun, dryRun)},}c.Flags().BoolVar(&fix, "fix", false, "fix the problems rexctl can fix safely")+ c.Flags().BoolVar(&dryRun, "dry-run", false, "show what --fix would change without changing anything")app.jsonFlag(c)return c}-func runDoctor(ctx context.Context, app *App, fix bool) error {+func runDoctor(ctx context.Context, app *App, fix, dryRun bool) error {l, err := app.Locate()if err != nil {return err@@ -80,7 +83,7 @@ func runDoctor(ctx context.Context, app *App, fix bool) error {}}run := func() error {- results := diagnose(ctx, app, env, phase, fix)+ results := diagnose(ctx, app, env, phase, fix, dryRun)if env.Instance != nil {warnUnusedRootFiles(ctx, app, env.Instance)}@@ -91,15 +94,15 @@ func runDoctor(ctx context.Context, app *App, fix bool) error {if errors.As(env.Instance.Managed(), &ej) && !app.Flags.JSON {defer app.UI.Info("%s %s", ej.Error(), ej.Fix())}- return reportDoctor(app, results)+ return reportDoctor(app, results, fix, dryRun)}- if fix && env.Instance != nil {+ if fix && !dryRun && env.Instance != nil {return app.Locked(ctx, env.Instance, run)}return run()}-func diagnose(ctx context.Context, app *App, env *checks.Env, phase checks.Phase, fix bool) []checks.Result {+func diagnose(ctx context.Context, app *App, env *checks.Env, phase checks.Phase, fix, dryRun bool) []checks.Result {check := func() []checks.Result {var results []checks.Result_ = app.UI.Step(ctx, "Checking your instance", func(ctx context.Context, _ *ui.Step) error {@@ -112,6 +115,16 @@ func diagnose(ctx context.Context, app *App, env *checks.Env, phase checks.Phaseif !fix {return results}+ if dryRun {+ for _, i := range fixOrder(results) {+ if !results[i].Fixable {+ continue+ }+ p := planFix(ctx, env, results[i].ID)+ results[i].Plan = &p+ }+ return results+ }fixed := map[string]bool{}skipped := map[string]string{}failed := map[string]bool{}@@ -130,6 +143,7 @@ func diagnose(ctx context.Context, app *App, env *checks.Env, phase checks.Phase}if !app.UI.Yes() {app.UI.Warn(r.Message, r.Fix)+ printPlan(app.UI, planFix(ctx, env, r.ID), true)}ok, err := app.UI.Confirm("Fix this?", true)if err != nil || !ok {@@ -212,7 +226,56 @@ func fixOrder(results []checks.Result) []int {return append(first, last...)}-func reportDoctor(app *App, results []checks.Result) error {+func planFix(ctx context.Context, env *checks.Env, id string) checks.Plan {+ p, err := checks.PlanFix(ctx, env, id)+ if err != nil {+ p.Steps, p.Error = []host.Step{}, err.Error()+ }+ return p+}++func printPlan(u *ui.UI, p checks.Plan, prompt bool) {+ if p.Error != "" {+ u.Info(" rexctl can't show what this fix does. Run rexctl doctor --fix --dry-run --verbose to see why.")+ u.Detail(" %s", p.Error)+ return+ }+ if len(p.Steps) == 0 {+ return+ }+ u.Info(" rexctl doctor --fix does this:")+ for _, s := range p.Steps {+ u.Info(" %s", s.Do)+ if s.Command != "" {+ u.Info(" %s", s.Command)+ }+ }+ if p.Settings {+ u.Info(" rexctl applies these settings changes together with the other fixes and restarts the services that use them.")+ }+ switch {+ case prompt:+ case p.Asks:+ u.Info(" rexctl asks before it does this, unless you pass --yes.")+ default:+ u.Info(" rexctl does this without asking when you run rexctl doctor --fix.")+ }+ var undo []string+ for i := len(p.Steps) - 1; i >= 0; i-- {+ if p.Steps[i].Undo != "" {+ undo = append(undo, p.Steps[i].Undo)+ }+ }+ if len(undo) == 0 {+ return+ }+ u.Info(" To undo it, run:")+ for _, c := range undo {+ u.Info(" %s", c)+ }+}++func reportDoctor(app *App, results []checks.Result, fix, dryRun bool) error {for _, r := range results {if app.Flags.JSON {break@@ -236,6 +299,17 @@ func reportDoctor(app *App, results []checks.Result) error {for _, d := range r.Details {app.UI.Detail(" %s", d)}+ if r.Plan != nil {+ printPlan(app.UI, *r.Plan, false)+ }+ }+ if !app.Flags.JSON {+ switch {+ case dryRun:+ app.UI.Info("Dry run. Nothing changed.")+ case !fix && slices.ContainsFunc(results, func(r checks.Result) bool { return r.Fixable }):+ app.UI.Info("To see what rexctl doctor --fix changes before you run it, run rexctl doctor --fix --dry-run.")+ }}if app.Flags.JSON {if err := app.UI.JSON(doctorJSON{OK: !checks.Failed(results), Checks: results}); err != nil {diff --git a/fluxer_rexctl/cmd/eject.go b/fluxer_rexctl/cmd/eject.goindex c1e78284d..2df64e722 100644--- a/fluxer_rexctl/cmd/eject.go+++ b/fluxer_rexctl/cmd/eject.go@@ -140,7 +140,11 @@ func removeBootUnit(ctx context.Context, inst *instance.Instance, e *engine.Engiif e != nil {u.Podman, u.Rootless = e.Kind == engine.Podman, e.Rootless}- if err := host.RemoveBootUnit(ctx, u); err != nil {+ steps, err := u.RemoveSteps(ctx)+ if err == nil {+ err = host.RunSteps(ctx, steps)+ }+ if err != nil {return fmt.Errorf("removing the boot unit: %w", err)}return nildiff --git a/fluxer_rexctl/cmd/install.go b/fluxer_rexctl/cmd/install.goindex 9a0ae98f8..b1d6af7e3 100644--- a/fluxer_rexctl/cmd/install.go+++ b/fluxer_rexctl/cmd/install.go@@ -541,7 +541,7 @@ func (in *installer) startPodman(ctx context.Context) error {in.plan = append(in.plan, "Start Podman's API socket.")return nil}- if err := host.FixPodman(ctx, s); err != nil {+ if err := host.RunSteps(ctx, s.Steps()); err != nil {return ui.Wrap(err, "Couldn't start Podman's API socket.", "Run rexctl install --verbose to see why.")}e, err := engine.Connect(ctx, engine.ConnectOptions{Prefer: engine.Podman, AllowOld: true})@@ -597,7 +597,7 @@ func (in *installer) setupPodman(ctx context.Context) error {in.plan = append(in.plan, "Set up Podman for Fluxer.")return nil}- if err := host.FixPodman(ctx, s); err != nil {+ if err := host.RunSteps(ctx, s.Steps()); err != nil {return ui.Wrap(err, "Couldn't set up Podman for Fluxer.", "Run rexctl install --verbose to see why.")}in.app.UI.OK("Set up Podman for Fluxer.")@@ -622,7 +622,7 @@ func (in *installer) allowLowPorts(ctx context.Context) error {in.plan = append(in.plan, "Let programs that don't run as root use ports 80 and up.")return nil}- if err := host.AllowLowPorts(ctx); err != nil {+ if err := host.RunSteps(ctx, host.LowPortsSysctl.Steps()); err != nil {return ui.Wrap(err, "Couldn't open ports 80 and up to programs that don't run as root.", "Run rexctl install --verbose to see why.")}in.app.UI.OK("Programs that don't run as root can use ports 80 and up.")@@ -1342,7 +1342,11 @@ func installBootUnit(ctx context.Context, inst *instance.Instance, e *engine.Engif err != nil {return err}- return host.InstallBootUnit(ctx, u)+ steps, err := u.InstallSteps(ctx)+ if err != nil {+ return err+ }+ return host.RunSteps(ctx, steps)}func refreshBootUnit(ctx context.Context, inst *instance.Instance, e *engine.Engine, p *engine.Project) error {diff --git a/fluxer_rexctl/cmd/proxy.go b/fluxer_rexctl/cmd/proxy.goindex feb8ba921..2dc0a7704 100644--- a/fluxer_rexctl/cmd/proxy.go+++ b/fluxer_rexctl/cmd/proxy.go@@ -118,7 +118,7 @@ func allowLowPorts(ctx context.Context, app *App, inst *instance.Instance) errorif !ok {return ui.Refused(fmt.Sprintf("Fluxer can't serve HTTPS until %s can use ports 80 and 443.", name), "Run rexctl proxy off again and let rexctl allow them.")}- if err := host.AllowLowPorts(ctx); err != nil {+ if err := host.RunSteps(ctx, host.LowPortsSysctl.Steps()); err != nil {return ui.Wrap(err, fmt.Sprintf("Couldn't let %s use ports 80 and 443.", name), "Run rexctl proxy off --verbose to see why.")}app.UI.OK("Let %s use ports 80 and 443.", name)diff --git a/fluxer_rexctl/cmd/restore.go b/fluxer_rexctl/cmd/restore.goindex 4d7fe9dde..62a76f1ba 100644--- a/fluxer_rexctl/cmd/restore.go+++ b/fluxer_rexctl/cmd/restore.go@@ -317,12 +317,20 @@ func restoreBootUnit(ctx context.Context, app *App, inst *instance.Instance, e *prev.Podman = old.Kind == string(engine.Podman)prev.Rootless = strings.Contains(old.Host, "/run/user/")if prev.Rootless != u.Rootless {- if err := host.RemoveBootUnit(ctx, prev); err != nil {+ steps, err := prev.RemoveSteps(ctx)+ if err == nil {+ err = host.RunSteps(ctx, steps)+ }+ if err != nil {app.UI.Detail("%v", err)}}}- if err := host.InstallBootUnit(ctx, u); err != nil {+ steps, err := u.InstallSteps(ctx)+ if err == nil {+ err = host.RunSteps(ctx, steps)+ }+ if err != nil {app.UI.Warn("Fluxer won't start again after this machine restarts.", "Run rexctl doctor --fix to start it with the machine.")app.UI.Detail("%v", err)}diff --git a/fluxer_rexctl/internal/checks/app_proxy.go b/fluxer_rexctl/internal/checks/app_proxy.goindex f8fe1af62..7c8e5a5b8 100644--- a/fluxer_rexctl/internal/checks/app_proxy.go+++ b/fluxer_rexctl/internal/checks/app_proxy.go@@ -5,6 +5,8 @@ package checksimport ("context""strings"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host")func init() {@@ -31,5 +33,8 @@ func fixAppProxy(ctx context.Context, e *Env) error {if e.Project == nil {return errNoProject}- return e.Project.Restart(ctx, "app-proxy")+ return e.run(ctx, host.Step{+ Do: "Restarts app-proxy.",+ Run: func(ctx context.Context) error { return e.Project.Restart(ctx, "app-proxy") },+ })}diff --git a/fluxer_rexctl/internal/checks/boot_unit.go b/fluxer_rexctl/internal/checks/boot_unit.goindex db2197faa..1486c9313 100644--- a/fluxer_rexctl/internal/checks/boot_unit.go+++ b/fluxer_rexctl/internal/checks/boot_unit.go@@ -95,5 +95,9 @@ func fixBootUnit(ctx context.Context, e *Env) error {if !ok || !s.Needed() {return nil}- return host.InstallBootUnit(ctx, u)+ steps, err := u.InstallSteps(ctx)+ if err != nil {+ return err+ }+ return e.run(ctx, steps...)}diff --git a/fluxer_rexctl/internal/checks/checks.go b/fluxer_rexctl/internal/checks/checks.goindex 32fc46c79..d0b69a3f3 100644--- a/fluxer_rexctl/internal/checks/checks.go+++ b/fluxer_rexctl/internal/checks/checks.go@@ -4,6 +4,8 @@ package checksimport ("context"+ "maps"+ "slices""sort""strings"@@ -37,6 +39,7 @@ type Result struct {Fix string `json:"fix,omitempty"`Fixable bool `json:"fixable"`Fixed bool `json:"fixed"`+ Plan *Plan `json:"plan,omitempty"`Manual string `json:"-"`Details []string `json:"-"`}@@ -64,6 +67,9 @@ type Env struct {importEnv boolstagedBy []stringkeptEdits [][2]string+ dryRun bool+ steps []host.Step+ stageSteps map[string]intpublicIP stringpublicErr errorpublicDone bool@@ -221,6 +227,36 @@ func ApplyFix(ctx context.Context, env *Env, id string) (Result, error) {return r, nil}+type Plan struct {+ Asks bool `json:"asks"`+ Settings bool `json:"settings"`+ Steps []host.Step `json:"steps"`+ Error string `json:"error,omitempty"`+}++func PlanFix(ctx context.Context, env *Env, id string) (Plan, error) {+ c, ok := registry[id]+ if !ok || c.Fix == nil {+ return Plan{}, ErrNoFix+ }+ staged, secrets, admin := maps.Clone(env.staged), maps.Clone(env.stagedSecrets), maps.Clone(env.stagedAdmin)+ importEnv, kept := env.importEnv, slices.Clone(env.keptEdits)+ defer func() {+ env.staged, env.stagedSecrets, env.stagedAdmin = staged, secrets, admin+ env.importEnv, env.keptEdits = importEnv, kept+ env.dryRun = false+ env.env = nil+ env.steps, env.stageSteps = nil, nil+ }()+ env.dryRun = true+ err := c.Fix(ctx, env)+ steps := env.steps+ if steps == nil {+ steps = []host.Step{}+ }+ return Plan{Asks: !c.SafeFix, Settings: len(env.stageSteps) > 0, Steps: steps}, err+}+func Failed(results []Result) bool {for _, r := range results {if r.Status == Fail {diff --git a/fluxer_rexctl/internal/checks/engine.go b/fluxer_rexctl/internal/checks/engine.goindex f9cc96033..c17060286 100644--- a/fluxer_rexctl/internal/checks/engine.go+++ b/fluxer_rexctl/internal/checks/engine.go@@ -85,9 +85,17 @@ func fixEngine(ctx context.Context, e *Env) error {if !e.podmanSocketOff() {return nil}- if err := host.FixPodman(ctx, host.CheckPodman(ctx, os.Geteuid() != 0, true)); err != nil {- return err+ steps := host.CheckPodman(ctx, os.Geteuid() != 0, true).Steps()+ if len(steps) == 0 {+ return nil+ }+ last := steps[len(steps)-1].Run+ steps[len(steps)-1].Run = func(ctx context.Context) error {+ if err := last(ctx); err != nil {+ return err+ }+ e.reconnect(ctx)+ return nil}- e.reconnect(ctx)- return nil+ return e.run(ctx, steps...)}diff --git a/fluxer_rexctl/internal/checks/env.go b/fluxer_rexctl/internal/checks/env.goindex 744e1a827..59c3e9ab3 100644--- a/fluxer_rexctl/internal/checks/env.go+++ b/fluxer_rexctl/internal/checks/env.go@@ -190,17 +190,46 @@ func (e *Env) lookup(key string) (string, bool) {}func (e *Env) stage(key string, value *string) {+ e.stageSetting(key, value, false)+}++func (e *Env) stageSetting(key string, value *string, secret bool) {if e.staged == nil {e.staged = map[string]*string{}}+ if e.dryRun {+ e.stageStep(key, e.settingStep(key, value, secret))+ }e.staged[key] = valuee.env = nil}+func (e *Env) settingStep(key string, value *string, secret bool) host.Step {+ set := func(v string) string {+ if secret {+ return "rexctl config set " + key + " -"+ }+ return "rexctl config set " + key + " " + instance.ShellQuote(v)+ }+ undo := "rexctl config unset " + key+ if old := e.setting(key); old != "" {+ undo = set(old)+ }+ if value == nil {+ return host.Step{Do: "Removes " + key + " from your settings.", Command: "rexctl config unset " + key, Undo: undo}+ }+ do := "Sets " + key + " to " + *value + "."+ if secret {+ do = "Sets " + key + " to the value in .env."+ }+ return host.Step{Do: do, Command: set(*value), Undo: undo}+}+func (e *Env) stageSecret(key, value string) {if e.stagedSecrets == nil {e.stagedSecrets = map[string]string{}}+ e.stageStep("secret "+key, host.Step{Do: "Saves " + key + " in the secrets rexctl keeps."})e.stagedSecrets[key] = valuee.env = nil}@@ -209,10 +238,46 @@ func (e *Env) stageAdmin(key, value string) {if e.stagedAdmin == nil {e.stagedAdmin = map[string]string{}}+ e.stageStep("admin "+key, host.Step{Do: "Sends " + key + " from .env to your instance's admin settings."})e.stagedAdmin[key] = valuee.env = nil}+func (e *Env) stageEnvImport() {+ e.stageStep(".env", host.Step{Do: "Deletes .env from " + e.Instance.Dir + " once the settings are applied."})+ e.importEnv = true+}++func (e *Env) stageStep(key string, step host.Step) {+ if !e.dryRun {+ return+ }+ if e.stageSteps == nil {+ e.stageSteps = map[string]int{}+ }+ if i, ok := e.stageSteps[key]; ok {+ step.Undo = e.steps[i].Undo+ e.steps[i] = step+ return+ }+ e.stageSteps[key] = len(e.steps)+ e.steps = append(e.steps, step)+}++func (e *Env) run(ctx context.Context, steps ...host.Step) error {+ if e.dryRun {+ e.steps = append(e.steps, steps...)+ return nil+ }+ return host.RunSteps(ctx, steps)+}++func (e *Env) reload(ctx context.Context) {+ e.Project, e.loadErr = nil, nil+ e.prepared = false+ e.prepare(ctx)+}+func (e *Env) stagedCount() int {n := len(e.staged) + len(e.stagedSecrets) + len(e.stagedAdmin)if e.importEnv {@@ -280,9 +345,7 @@ func Commit(ctx context.Context, e *Env, o deploy.ApplyOptions) error {return err}done = true- e.Project = nil- e.prepared = false- e.prepare(ctx)+ e.reload(ctx)e.reset()return nil}@@ -472,9 +535,14 @@ func (e *Env) up(ctx context.Context) error {if e.Instance == nil || e.Engine == nil {return errNoProject}- if e.loadErr != nil {+ if e.loadErr != nil && !e.dryRun {return e.loadErr}- _, err := deploy.Apply(ctx, e.Instance, e.Engine, deploy.ApplyOptions{})- return err+ return e.run(ctx, host.Step{+ Do: "Writes Fluxer's files from your settings and starts any service that changed or isn't running.",+ Run: func(ctx context.Context) error {+ _, err := deploy.Apply(ctx, e.Instance, e.Engine, deploy.ApplyOptions{})+ return err+ },+ })}diff --git a/fluxer_rexctl/internal/checks/env_file.go b/fluxer_rexctl/internal/checks/env_file.goindex 215f1d8a8..ad138bf56 100644--- a/fluxer_rexctl/internal/checks/env_file.go+++ b/fluxer_rexctl/internal/checks/env_file.go@@ -9,6 +9,7 @@ import ("os""path/filepath"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/release""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"@@ -59,18 +60,22 @@ func fixEnvFile(ctx context.Context, e *Env) error {return err}if r.Env.Same {- return instance.RemoveEnv(e.Instance)+ return e.run(ctx, host.Step{+ Do: "Deletes .env from " + e.Instance.Dir + ". rexctl already keeps every setting in it.",+ Run: func(context.Context) error { return instance.RemoveEnv(e.Instance) },+ })}- for k, v := range r.Env.Settings() {- e.stage(k, v)- }- for k, v := range r.Env.Values(instance.ChangeSecret) {- e.stageSecret(k, v)- }- for k, v := range r.Env.Values(instance.ChangeAdmin) {- e.stageAdmin(k, v)+ for _, c := range r.Env.Changes {+ switch c.Kind {+ case instance.ChangeSetting:+ e.stageSetting(c.Key, &c.Value, c.Secret)+ case instance.ChangeSecret:+ e.stageSecret(c.Key, c.Value)+ case instance.ChangeAdmin:+ e.stageAdmin(c.Key, c.Value)+ }}- e.importEnv = true+ e.stageEnvImport()return nil}@@ -102,25 +107,43 @@ func runStackEdits(ctx context.Context, e *Env) Result {}func fixStackEdits(ctx context.Context, e *Env) error {- edits, err := instance.KeepEdits(e.Instance)+ r, err := e.drift()if err != nil {return err}- for _, ed := range edits {- e.keptEdits = append(e.keptEdits, [2]string{ed.Name, ed.Copy})- if err := os.Remove(e.Instance.Generated(ed.Name)); err != nil && !errors.Is(err, os.ErrNotExist) {- return err- }- }- if _, err := release.RestoreMissing(e.Instance); err != nil {- return err+ if len(r.Changed) == 0 {+ return e.up(ctx)}- if err := e.Instance.Save(); err != nil {+ version := "version"+ if len(r.Changed) > 1 {+ version = "versions"+ }+ err = e.run(ctx, host.Step{+ Do: fmt.Sprintf("Copies your %s of %s to %s and puts rexctl's %s back.", version, ui.JoinAnd(r.Changed), e.Instance.Path(instance.StateDirName, instance.EditedDir), version),+ Run: func(ctx context.Context) error {+ edits, err := instance.KeepEdits(e.Instance)+ if err != nil {+ return err+ }+ for _, ed := range edits {+ e.keptEdits = append(e.keptEdits, [2]string{ed.Name, ed.Copy})+ if err := os.Remove(e.Instance.Generated(ed.Name)); err != nil && !errors.Is(err, os.ErrNotExist) {+ return err+ }+ }+ if _, err := release.RestoreMissing(e.Instance); err != nil {+ return err+ }+ if err := e.Instance.Save(); err != nil {+ return err+ }+ e.reload(ctx)+ return nil+ },+ })+ if err != nil {return err}- e.Project = nil- e.prepared = false- e.prepare(ctx)return e.up(ctx)}diff --git a/fluxer_rexctl/internal/checks/low_ports.go b/fluxer_rexctl/internal/checks/low_ports.goindex 606cce1d6..044b27c55 100644--- a/fluxer_rexctl/internal/checks/low_ports.go+++ b/fluxer_rexctl/internal/checks/low_ports.go@@ -23,12 +23,12 @@ func runLowPorts(_ context.Context, e *Env) Result {proxy = "run rexctl proxy on"}r := fixable(fail(fmt.Sprintf("Rootless %s can't use ports 80 and 443, because only root can use ports below 1024 on this machine.", e.engineName()), "Run rexctl doctor --fix to allow them, or "+proxy+" to use your own proxy."))- r.Manual = "Ask an administrator to add " + host.LowPortsSetting + " to " + host.LowPortsFile + " and run sudo sysctl --system, or " + proxy + " to use your own proxy."+ r.Manual = "Ask an administrator to run " + host.LowPortsSysctl.HostCommand() + ", or " + proxy + " to use your own proxy."return r}return pass(fmt.Sprintf("Rootless %s can use ports 80 and 443.", e.engineName()))}-func fixLowPorts(ctx context.Context, _ *Env) error {- return host.AllowLowPorts(ctx)+func fixLowPorts(ctx context.Context, e *Env) error {+ return e.run(ctx, host.LowPortsSysctl.Steps()...)}diff --git a/fluxer_rexctl/internal/checks/media_proxy_spool.go b/fluxer_rexctl/internal/checks/media_proxy_spool.goindex 148caa4af..8ef1f910b 100644--- a/fluxer_rexctl/internal/checks/media_proxy_spool.go+++ b/fluxer_rexctl/internal/checks/media_proxy_spool.go@@ -7,6 +7,7 @@ import ("io""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host")const (@@ -36,5 +37,10 @@ func fixMediaProxySpool(ctx context.Context, e *Env) error {if e.Project == nil {return errNoProject}- return e.Project.Exec(ctx, "media-proxy", engine.ExecOptions{User: "0", Stdout: io.Discard, Stderr: io.Discard}, "sh", "-c", mediaProxySpoolRepair)+ return e.run(ctx, host.Step{+ Do: "Makes the media proxy's temporary folder and lets every account in its container write to it.",+ Run: func(ctx context.Context) error {+ return e.Project.Exec(ctx, "media-proxy", engine.ExecOptions{User: "0", Stdout: io.Discard, Stderr: io.Discard}, "sh", "-c", mediaProxySpoolRepair)+ },+ })}diff --git a/fluxer_rexctl/internal/checks/overcommit.go b/fluxer_rexctl/internal/checks/overcommit.goindex 9402339ef..b2dec70ac 100644--- a/fluxer_rexctl/internal/checks/overcommit.go+++ b/fluxer_rexctl/internal/checks/overcommit.go@@ -10,9 +10,8 @@ import ()var (- overcommit = host.Overcommit- enableOvercommit = host.EnableOvercommit- freeMemory = host.FreeMemory+ overcommit = host.Overcommit+ freeMemory = host.FreeMemory)const (@@ -35,17 +34,17 @@ func runOvercommit(_ context.Context, e *Env) Result {case host.OvercommitUnknown:return skip("")case host.OvercommitLocked:- return warn(overcommitOff, "This machine is a container and can't change it. Run this on the container host: "+host.OvercommitCommand)+ return warn(overcommitOff, "This machine is a container and can't change it. Run this on the container host: "+host.OvercommitSysctl.HostCommand())case host.OvercommitOff:r := fixable(warn(overcommitOff, "Run rexctl doctor --fix to turn it on."))- r.Manual = "Run this as an administrator, or on the container host if this machine is a container: " + host.OvercommitCommand+ r.Manual = "Run this as an administrator, or on the container host if this machine is a container: " + host.OvercommitSysctl.HostCommand()return r}return pass("Memory overcommit is on.")}-func fixOvercommit(ctx context.Context, _ *Env) error {- return enableOvercommit(ctx)+func fixOvercommit(ctx context.Context, e *Env) error {+ return e.run(ctx, host.OvercommitSysctl.Steps()...)}func enoughForValkey(e *Env) bool {diff --git a/fluxer_rexctl/internal/checks/plan_test.go b/fluxer_rexctl/internal/checks/plan_test.gonew file mode 100644index 000000000..6f5dd3de4--- /dev/null+++ b/fluxer_rexctl/internal/checks/plan_test.go@@ -0,0 +1,57 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package checks++import (+ "context"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"+)++func TestPlanFixStagesNothing(t *testing.T) {+ env := &Env{+ Instance: &instance.Instance{Dir: t.TempDir(), State: &instance.State{Settings: map[string]string{settings.VoicePublicIP: "198.51.100.4"}}},+ publicIP: "203.0.113.7",+ publicDone: true,+ }+ p, err := PlanFix(context.Background(), env, "livekit-ip")+ if err != nil {+ t.Fatal(err)+ }+ if len(p.Steps) != 1 {+ t.Fatalf("got %d steps, want 1", len(p.Steps))+ }+ if got, want := p.Steps[0].Command, "rexctl config set voice.public_ip 203.0.113.7"; got != want {+ t.Fatalf("command %q, want %q", got, want)+ }+ if got, want := p.Steps[0].Undo, "rexctl config set voice.public_ip 198.51.100.4"; got != want {+ t.Fatalf("undo %q, want %q", got, want)+ }+ if !p.Asks {+ t.Fatal("the plan says rexctl doesn't ask first")+ }+ if env.Staged() {+ t.Fatal("planning the fix left a staged change behind")+ }+}++func TestPlanFixRunsNothing(t *testing.T) {+ const id = "plan-test"+ Register(Check{ID: id, Fix: func(ctx context.Context, e *Env) error {+ return e.run(ctx, host.Step{Do: "Changes this machine.", Run: func(context.Context) error {+ t.Error("a dry run ran a step")+ return nil+ }})+ }})+ t.Cleanup(func() { delete(registry, id) })+ p, err := PlanFix(context.Background(), &Env{}, id)+ if err != nil {+ t.Fatal(err)+ }+ if len(p.Steps) != 1 || p.Steps[0].Do != "Changes this machine." {+ t.Fatalf("got steps %+v", p.Steps)+ }+}diff --git a/fluxer_rexctl/internal/checks/podman.go b/fluxer_rexctl/internal/checks/podman.goindex 2f324daca..e8cfded41 100644--- a/fluxer_rexctl/internal/checks/podman.go+++ b/fluxer_rexctl/internal/checks/podman.go@@ -65,5 +65,5 @@ func fixPodmanSetup(ctx context.Context, e *Env) error {if !ok {return nil}- return host.FixPodman(ctx, s)+ return e.run(ctx, s.Steps()...)}diff --git a/fluxer_rexctl/internal/checks/postgres.go b/fluxer_rexctl/internal/checks/postgres.goindex 3c6a6aa29..74547e33d 100644--- a/fluxer_rexctl/internal/checks/postgres.go+++ b/fluxer_rexctl/internal/checks/postgres.go@@ -7,6 +7,8 @@ import ("errors""strconv""strings"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host")const maxSCRAMIterations = 100000@@ -116,6 +118,11 @@ func fixPostgresRole(ctx context.Context, e *Env) error {if strings.ContainsAny(password, "\n\r") {return errors.New("checks: the database password has a line break")}- _, err := e.exec(ctx, "postgres", alterRoleScript(password), "sh", "-c", psqlLocal, "psql", "-f", "-")- return err+ return e.run(ctx, host.Step{+ Do: "Sets the password of Fluxer's database account to POSTGRES_PASSWORD from your settings and stores it with SCRAM-SHA-256.",+ Run: func(ctx context.Context) error {+ _, err := e.exec(ctx, "postgres", alterRoleScript(password), "sh", "-c", psqlLocal, "psql", "-f", "-")+ return err+ },+ })}diff --git a/fluxer_rexctl/internal/checks/services.go b/fluxer_rexctl/internal/checks/services.goindex 66396bffb..cff101ad6 100644--- a/fluxer_rexctl/internal/checks/services.go+++ b/fluxer_rexctl/internal/checks/services.go@@ -9,8 +9,10 @@ import ("time""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui")func init() {@@ -172,13 +174,22 @@ func fixServices(ctx context.Context, e *Env) error {}}if len(restart) > 0 {- if err := e.Project.Restart(ctx, restart...); err != nil {+ err := e.run(ctx, host.Step{+ Do: "Restarts " + ui.JoinAnd(restart) + ".",+ Run: func(ctx context.Context) error { return e.Project.Restart(ctx, restart...) },+ })+ if err != nil {return err}}if err := e.up(ctx); err != nil {return err}- _ = e.Project.WaitReady(ctx, 5*time.Minute, nil)- return ctx.Err()+ return e.run(ctx, host.Step{+ Do: "Waits up to 5 minutes for Fluxer to be ready.",+ Run: func(ctx context.Context) error {+ _ = e.Project.WaitReady(ctx, 5*time.Minute, nil)+ return ctx.Err()+ },+ })}diff --git a/fluxer_rexctl/internal/checks/stack_files.go b/fluxer_rexctl/internal/checks/stack_files.goindex a5a64e9d6..e3b398589 100644--- a/fluxer_rexctl/internal/checks/stack_files.go+++ b/fluxer_rexctl/internal/checks/stack_files.go@@ -4,8 +4,11 @@ package checksimport ("context"+ "fmt"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/release"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui")func init() {@@ -29,24 +32,38 @@ func runStackFiles(ctx context.Context, e *Env) Result {}func fixStackFiles(ctx context.Context, e *Env) error {- restored, err := release.RestoreMissing(e.Instance)- if err != nil {+ missing := release.MissingFiles(e.Instance)+ var restored []string+ err := e.run(ctx, host.Step{+ Do: fmt.Sprintf("Writes %s again from Fluxer %s.", ui.JoinAnd(missing), e.Instance.State.Release),+ Run: func(ctx context.Context) error {+ var err error+ if restored, err = release.RestoreMissing(e.Instance); err != nil {+ return err+ }+ if err := e.Instance.Save(); err != nil {+ return err+ }+ e.reload(ctx)+ return nil+ },+ })+ if err != nil || e.Project == nil {return err}- if err := e.Instance.Save(); err != nil {- return err- }- e.Project = nil- e.prepared = false- e.prepare(ctx)- if e.Project == nil {- return nil- }if err := e.up(ctx); err != nil {return err}- if restart := release.RestartFor(restored); len(restart) > 0 {- return e.Project.Restart(ctx, restart...)+ if restart := release.RestartFor(missing); len(restart) > 0 {+ return e.run(ctx, host.Step{+ Do: fmt.Sprintf("Restarts %s, which read these files.", ui.JoinAnd(restart)),+ Run: func(ctx context.Context) error {+ if restart := release.RestartFor(restored); len(restart) > 0 {+ return e.Project.Restart(ctx, restart...)+ }+ return nil+ },+ })}return nil}diff --git a/fluxer_rexctl/internal/checks/stale.go b/fluxer_rexctl/internal/checks/stale.goindex 87c0a139f..32640184e 100644--- a/fluxer_rexctl/internal/checks/stale.go+++ b/fluxer_rexctl/internal/checks/stale.go@@ -7,6 +7,8 @@ import ("strings""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui")func init() {@@ -72,5 +74,10 @@ func fixStale(ctx context.Context, e *Env) error {if e.loadErr != nil {return e.loadErr}- return e.Project.Up(ctx, engine.UpOptions{Services: stale, NoDeps: true, Recreate: engine.RecreateAlways})+ return e.run(ctx, host.Step{+ Do: "Recreates " + ui.JoinAnd(stale) + " from the Fluxer version on this machine, so it stops running the older one.",+ Run: func(ctx context.Context) error {+ return e.Project.Up(ctx, engine.UpOptions{Services: stale, NoDeps: true, Recreate: engine.RecreateAlways})+ },+ })}diff --git a/fluxer_rexctl/internal/checks/unfinished.go b/fluxer_rexctl/internal/checks/unfinished.goindex 479c90a5d..00a519c54 100644--- a/fluxer_rexctl/internal/checks/unfinished.go+++ b/fluxer_rexctl/internal/checks/unfinished.go@@ -7,6 +7,7 @@ import ("fmt""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/release""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui")@@ -34,11 +35,32 @@ func fixPendingChange(ctx context.Context, e *Env) error {if e.state() == nil || e.Engine == nil {return errNoProject}- if err := deploy.Resolve(ctx, e.Instance, e.Engine); err != nil {- return err+ st := e.state()+ if st.Pending == nil {+ return nil+ }+ what := "the last change"+ if len(st.Pending.Keys) > 0 {+ what = "the change to " + ui.JoinAnd(st.Pending.Keys)+ }+ do := fmt.Sprintf("Puts back the settings from before %s, writes Fluxer's files from them and starts Fluxer.", what)+ if st.Stopped {+ do = fmt.Sprintf("Puts back the settings from before %s and writes Fluxer's files from them.", what)+ }+ var steps []host.Step+ if deploy.PendingIPv6(st) && !st.Stopped {+ steps = append(steps, host.Step{Do: "Stops Fluxer and its voice servers first, because the IPv6 setting changes."})}- e.env = nil- return nil+ return e.run(ctx, append(steps, host.Step{+ Do: do,+ Run: func(ctx context.Context) error {+ if err := deploy.Resolve(ctx, e.Instance, e.Engine); err != nil {+ return err+ }+ e.env = nil+ return nil+ },+ })...)}func runUpgradeUnfinished(ctx context.Context, e *Env) Result {diff --git a/fluxer_rexctl/internal/checks/voice.go b/fluxer_rexctl/internal/checks/voice.goindex 5881ff325..a723d415c 100644--- a/fluxer_rexctl/internal/checks/voice.go+++ b/fluxer_rexctl/internal/checks/voice.go@@ -122,8 +122,19 @@ func fixVoiceEndpoints(ctx context.Context, e *Env) error {if e.Admin == nil {return ErrNoFix}- _, err := deploy.MoveVoiceEndpoints(ctx, e.Admin, e.Instance)- return err+ origin := voice.WSOrigin(e.Instance.Origin())+ var steps []host.Step+ for _, id := range deploy.StaleVoiceEndpoints(e.Instance) {+ steps = append(steps, host.Step{Do: fmt.Sprintf("Tells your instance that %s now uses %s.", id, origin+voice.Path(id))})+ }+ if len(steps) == 0 {+ return nil+ }+ steps[len(steps)-1].Run = func(ctx context.Context) error {+ _, err := deploy.MoveVoiceEndpoints(ctx, e.Admin, e.Instance)+ return err+ }+ return e.run(ctx, steps...)}func runVoicePorts(ctx context.Context, e *Env) Result {diff --git a/fluxer_rexctl/internal/deploy/resolve.go b/fluxer_rexctl/internal/deploy/resolve.goindex 831eb07f6..091d0ac00 100644--- a/fluxer_rexctl/internal/deploy/resolve.go+++ b/fluxer_rexctl/internal/deploy/resolve.go@@ -27,6 +27,10 @@ func Interrupted(inst *instance.Instance) string {strings.Join(p.Keys[:len(p.Keys)-1], ", "), p.Keys[len(p.Keys)-1])}+func PendingIPv6(st *instance.State) bool {+ return st.Pending != nil && parseBool(st.Pending.Settings[settings.IPv6]) != parseBool(st.Settings[settings.IPv6])+}+func Resolve(ctx context.Context, inst *instance.Instance, e *engine.Engine) error {p := inst.State.Pendingif p == nil {@@ -36,7 +40,7 @@ func Resolve(ctx context.Context, inst *instance.Instance, e *engine.Engine) errif err != nil {return err}- ipv6 := parseBool(p.Settings[settings.IPv6]) != parseBool(inst.State.Settings[settings.IPv6])+ ipv6 := PendingIPv6(inst.State)if _, err := render(inst, rs, inst.State.Settings); err != nil {return err}diff --git a/fluxer_rexctl/internal/host/autoupgrade.go b/fluxer_rexctl/internal/host/autoupgrade.goindex c3ab7a13d..105c5e1fe 100644--- a/fluxer_rexctl/internal/host/autoupgrade.go+++ b/fluxer_rexctl/internal/host/autoupgrade.go@@ -515,7 +515,9 @@ func InstallAutoUpgrade(ctx context.Context, a AutoUpgrade) error {}}if a.Scheduler == Systemd && a.Rootless && !lingering(ctx) {- return enableLinger(ctx)+ if step, ok := lingerStep(); ok {+ return step.Run(ctx)+ }}return nil}diff --git a/fluxer_rexctl/internal/host/bootunit.go b/fluxer_rexctl/internal/host/bootunit.goindex 0f598edbb..d2a4836f4 100644--- a/fluxer_rexctl/internal/host/bootunit.go+++ b/fluxer_rexctl/internal/host/bootunit.go@@ -232,25 +232,42 @@ func CheckBootUnit(ctx context.Context, u BootUnit) (BootUnitState, error) {return s, nil}-func InstallBootUnit(ctx context.Context, u BootUnit) error {+func (u BootUnit) systemctl(args string) string {+ if u.Rootless {+ return "systemctl --user " + args+ }+ return rootCmd("systemctl " + args)+}++func (u BootUnit) InstallSteps(ctx context.Context) ([]Step, error) {if !BootUnitSupported() {- return nil+ return nil, nil}- if err := writeBootUnit(ctx, u); err != nil {- return err+ steps, path, err := u.writeSteps(ctx)+ if err != nil {+ return nil, err}- if err := systemctlRun(ctx, u.Rootless, "enable", u.Name()); err != nil {- return err+ if !unitIs(ctx, u.Rootless, u.Name(), "enabled") {+ steps = append(steps, Step{+ Do: "Starts " + u.Name() + " with the machine from now on.",+ Command: u.systemctl("enable " + u.Name()),+ Undo: u.removeSteps(path, true)[0].Command,+ Run: func(ctx context.Context) error { return systemctlRun(ctx, u.Rootless, "enable", u.Name()) },+ })}if u.Rootless && !lingering(ctx) {- if err := enableLinger(ctx); err != nil {- return err+ if step, ok := lingerStep(); ok {+ steps = append(steps, step)}}if !unitIs(ctx, u.Rootless, u.Name(), "active") {- return systemctlRun(ctx, u.Rootless, "start", "--no-block", u.Name())+ steps = append(steps, Step{+ Do: "Starts " + u.Name() + " now.",+ Command: u.systemctl("start --no-block " + u.Name()),+ Run: func(ctx context.Context) error { return systemctlRun(ctx, u.Rootless, "start", "--no-block", u.Name()) },+ })}- return nil+ return steps, nil}func RefreshBootUnit(ctx context.Context, u BootUnit) error {@@ -264,29 +281,53 @@ func RefreshBootUnit(ctx context.Context, u BootUnit) error {if _, err := os.Stat(path); os.IsNotExist(err) {return nil}- return writeBootUnit(ctx, u)+ steps, _, err := u.writeSteps(ctx)+ if err != nil {+ return err+ }+ return RunSteps(ctx, steps)}-func RemoveBootUnit(ctx context.Context, u BootUnit) error {+func (u BootUnit) RemoveSteps(ctx context.Context) ([]Step, error) {if !BootUnitSupported() {- return nil+ return nil, nil}path, err := u.Path()if err != nil {- return err+ return nil, err}if _, err := os.Stat(path); os.IsNotExist(err) {- return nil+ return nil, nil}- if unitIs(ctx, u.Rootless, u.Name(), "enabled") {- if err := systemctlRun(ctx, u.Rootless, "disable", u.Name()); err != nil {- return err- }- }- if err := removeHostFiles(ctx, u.Rootless, path); err != nil {- return err- }- return systemctlRun(ctx, u.Rootless, "daemon-reload")+ return u.removeSteps(path, unitIs(ctx, u.Rootless, u.Name(), "enabled")), nil+}++func (u BootUnit) removeSteps(path string, enabled bool) []Step {+ rm := "rm -f " + path+ if !u.Rootless {+ rm = rootCmd(rm)+ }+ steps := []Step{+ {+ Do: "Stops starting " + u.Name() + " with the machine.",+ Command: u.systemctl("disable " + u.Name()),+ Run: func(ctx context.Context) error { return systemctlRun(ctx, u.Rootless, "disable", u.Name()) },+ },+ {+ Do: "Deletes " + path + ".",+ Command: rm,+ Run: func(ctx context.Context) error { return removeHostFiles(ctx, u.Rootless, path) },+ },+ {+ Do: "Tells systemd that " + u.Name() + " is gone.",+ Command: u.systemctl("daemon-reload"),+ Run: func(ctx context.Context) error { return systemctlRun(ctx, u.Rootless, "daemon-reload") },+ },+ }+ if !enabled {+ return steps[1:]+ }+ return steps}func removeHostFiles(ctx context.Context, user bool, paths ...string) error {@@ -302,24 +343,41 @@ func removeHostFiles(ctx context.Context, user bool, paths ...string) error {return nil}-func writeBootUnit(ctx context.Context, u BootUnit) error {+func (u BootUnit) writeSteps(ctx context.Context) ([]Step, string, error) {if !filepath.IsAbs(u.Binary) || !filepath.IsAbs(u.Dir) {- return fmt.Errorf("host: boot unit needs absolute paths, got %q and %q", u.Binary, u.Dir)+ return nil, "", fmt.Errorf("host: boot unit needs absolute paths, got %q and %q", u.Binary, u.Dir)}path, err := u.Path()if err != nil {- return err- }- if !u.Rootless {- if err := installBinary(ctx, true, u.Binary, LibexecBinary); err != nil {- return err+ return nil, "", err+ }+ var steps []Step+ if !u.Rootless && !sameFile(u.Binary, LibexecBinary) {+ steps = append(steps, Step{+ Do: "Copies this rexctl to " + LibexecBinary + " for the service to use.",+ Command: rootCmd("install " + strings.Join(installArgs(u.Binary, LibexecBinary), " ")),+ Run: func(ctx context.Context) error { return installBinary(ctx, true, u.Binary, LibexecBinary) },+ })+ }+ content := []byte(u.content(ctx))+ if old, err := os.ReadFile(path); err != nil || !bytes.Equal(old, content) {+ undo := ""+ if os.IsNotExist(err) {+ undo = joinCommands(u.removeSteps(path, false))}- }- changed, err := writeHostFile(ctx, u.Rootless, path, []byte(u.content(ctx)))- if err != nil || !changed {- return err- }- return systemctlRun(ctx, u.Rootless, "daemon-reload")+ steps = append(steps, Step{+ Do: "Writes " + path + ", which starts Fluxer with the machine.",+ Undo: undo,+ Run: func(ctx context.Context) error {+ changed, err := writeHostFile(ctx, u.Rootless, path, content)+ if err != nil || !changed {+ return err+ }+ return systemctlRun(ctx, u.Rootless, "daemon-reload")+ },+ })+ }+ return steps, path, nil}func installBinary(ctx context.Context, system bool, src, dst string) error {diff --git a/fluxer_rexctl/internal/host/low_ports.go b/fluxer_rexctl/internal/host/low_ports.goindex c0f7cb699..ef4244bc0 100644--- a/fluxer_rexctl/internal/host/low_ports.go+++ b/fluxer_rexctl/internal/host/low_ports.go@@ -3,18 +3,12 @@package hostimport (- "context""os""runtime""strconv""strings")-const (- LowPortsSetting = "net.ipv4.ip_unprivileged_port_start = 80"- LowPortsFile = "/etc/sysctl.d/60-fluxer-ports.conf"-)-func LowPortsBlocked(rootless bool) bool {if !rootless || runtime.GOOS != "linux" {return false@@ -26,11 +20,3 @@ func LowPortsBlocked(rootless bool) bool {n, err := strconv.Atoi(strings.TrimSpace(string(data)))return err == nil && n > 80}--func AllowLowPorts(ctx context.Context) error {- if _, err := output(ctx, false, "sh", "-c", "printf '"+LowPortsSetting+"\\n' > "+LowPortsFile); err != nil {- return err- }- _, err := output(ctx, false, "sysctl", "-p", LowPortsFile)- return err-}diff --git a/fluxer_rexctl/internal/host/overcommit.go b/fluxer_rexctl/internal/host/overcommit.goindex 9a55958f7..ce78a8579 100644--- a/fluxer_rexctl/internal/host/overcommit.go+++ b/fluxer_rexctl/internal/host/overcommit.go@@ -3,9 +3,7 @@package hostimport (- "context""errors"- "fmt""io/fs""os""runtime"@@ -14,12 +12,6 @@ import ("syscall")-const (- OvercommitSetting = "vm.overcommit_memory = 1"- OvercommitFile = "/etc/sysctl.d/61-fluxer-overcommit.conf"- OvercommitCommand = "echo '" + OvercommitSetting + "' | sudo tee " + OvercommitFile + " && sudo sysctl --system"-)-type OvercommitState intconst (@@ -81,15 +73,9 @@ func FreeMemory() uint64 {return free}-func EnableOvercommit(ctx context.Context) error {- if _, err := output(ctx, false, "sh", "-c", "printf '"+OvercommitSetting+"\\n' > "+OvercommitFile); err != nil {- return err- }- if _, err := output(ctx, false, "sysctl", "-p", OvercommitFile); err != nil {- return err- }+func overcommitChanged() error {if s := Overcommit(); s == OvercommitOff || s == OvercommitLocked {- return fmt.Errorf("%w after sysctl -p %s", ErrOvercommitUnchanged, OvercommitFile)+ return ErrOvercommitUnchanged}return nil}diff --git a/fluxer_rexctl/internal/host/podman.go b/fluxer_rexctl/internal/host/podman.goindex 9233d16c6..3a95a05fe 100644--- a/fluxer_rexctl/internal/host/podman.go+++ b/fluxer_rexctl/internal/host/podman.go@@ -85,15 +85,22 @@ func lingering(ctx context.Context) bool {return strings.TrimSpace(string(out)) == "yes"}-func enableLinger(ctx context.Context) error {+func lingerStep() (Step, bool) {u, err := user.Current()if err != nil {- return nil- }- if out, err := exec.CommandContext(ctx, "loginctl", "enable-linger", u.Username).CombinedOutput(); err != nil {- return &CommandError{Command: "loginctl enable-linger", Output: string(out), Err: err}+ return Step{}, false}- return nil+ return Step{+ Do: "Lets your account's services keep running after you sign out, so Fluxer starts with the machine.",+ Command: "loginctl enable-linger " + u.Username,+ Undo: "loginctl disable-linger " + u.Username,+ Run: func(ctx context.Context) error {+ if out, err := exec.CommandContext(ctx, "loginctl", "enable-linger", u.Username).CombinedOutput(); err != nil {+ return &CommandError{Command: "loginctl enable-linger", Output: string(out), Err: err}+ }+ return nil+ },+ }, true}func HasSubIDs(ctx context.Context) bool {@@ -161,28 +168,54 @@ func healthcheckEventsSet(rootless bool) bool {return false}-func FixPodman(ctx context.Context, s PodmanSetup) error {+func (s PodmanSetup) Steps() []Step {+ var steps []Stepif (s.SocketMissing || s.NoSocketStart) && s.Systemd {- if out, err := systemctl(ctx, s.Rootless, "enable", "--now", "podman.socket").CombinedOutput(); err != nil {- return &CommandError{Command: "systemctl enable --now podman.socket", Output: string(out), Err: err}+ sys := rootCmd("systemctl")+ if s.Rootless {+ sys = "systemctl --user"+ }+ undo := sys + " disable --now podman.socket"+ if !s.SocketMissing {+ undo = sys + " disable podman.socket"}- waitPodmanSocket(ctx, s.Rootless)+ steps = append(steps, Step{+ Do: "Starts Podman's API socket, and starts it with the machine from now on.",+ Command: sys + " enable --now podman.socket",+ Undo: undo,+ Run: func(ctx context.Context) error {+ if out, err := systemctl(ctx, s.Rootless, "enable", "--now", "podman.socket").CombinedOutput(); err != nil {+ return &CommandError{Command: "systemctl enable --now podman.socket", Output: string(out), Err: err}+ }+ waitPodmanSocket(ctx, s.Rootless)+ return nil+ },+ })}if s.NoLinger {- if err := enableLinger(ctx); err != nil {- return err+ if step, ok := lingerStep(); ok {+ steps = append(steps, step)}}if s.EventsOn {dir := filepath.Join(containersConfDirs(s.Rootless)[0], "containers.conf.d")- if err := os.MkdirAll(dir, 0o755); err != nil {- return err- }- if err := envfile.WriteFileAtomic(filepath.Join(dir, healthcheckEventsFile), []byte("[engine]\nhealthcheck_events = false\n"), false); err != nil {- return err+ path := filepath.Join(dir, healthcheckEventsFile)+ rm := "rm " + path+ if !s.Rootless {+ rm = rootCmd(rm)}+ steps = append(steps, Step{+ Do: "Writes healthcheck_events = false to " + path + ", so Podman stops writing every health check to the system log.",+ Undo: rm,+ Run: func(context.Context) error {+ if err := os.MkdirAll(dir, 0o755); err != nil {+ return err+ }+ return envfile.WriteFileAtomic(path, []byte("[engine]\nhealthcheck_events = false\n"), false)+ },+ })}- return nil+ return steps}func podmanSocketPath(rootless bool) string {diff --git a/fluxer_rexctl/internal/host/step.go b/fluxer_rexctl/internal/host/step.gonew file mode 100644index 000000000..ee78f2637--- /dev/null+++ b/fluxer_rexctl/internal/host/step.go@@ -0,0 +1,44 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package host++import (+ "context"+ "strings"+)++type Step struct {+ Do string `json:"do"`+ Command string `json:"command,omitempty"`+ Undo string `json:"undo,omitempty"`+ Run func(context.Context) error `json:"-"`+}++func RunSteps(ctx context.Context, steps []Step) error {+ for _, s := range steps {+ if s.Run == nil {+ continue+ }+ if err := s.Run(ctx); err != nil {+ return err+ }+ }+ return nil+}++func rootCmd(s string) string {+ if isRoot() {+ return s+ }+ return "sudo " + s+}++func joinCommands(steps []Step) string {+ var cmds []string+ for _, s := range steps {+ if s.Command != "" {+ cmds = append(cmds, s.Command)+ }+ }+ return strings.Join(cmds, " && ")+}diff --git a/fluxer_rexctl/internal/host/sysctl.go b/fluxer_rexctl/internal/host/sysctl.gonew file mode 100644index 000000000..7670fa75b--- /dev/null+++ b/fluxer_rexctl/internal/host/sysctl.go@@ -0,0 +1,91 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package host++import (+ "context"+ "fmt"+ "os"+ "strings"+)++type Sysctl struct {+ Key string+ Value string+ File string+ Why string+ Now string+ check func() error+}++var (+ OvercommitSysctl = Sysctl{+ Key: "vm.overcommit_memory",+ Value: "1",+ File: "/etc/sysctl.d/61-fluxer-overcommit.conf",+ Why: "so it stays on after a restart",+ Now: "Turns it on now.",+ check: overcommitChanged,+ }+ LowPortsSysctl = Sysctl{+ Key: "net.ipv4.ip_unprivileged_port_start",+ Value: "80",+ File: "/etc/sysctl.d/60-fluxer-ports.conf",+ Why: "so programs that don't run as root can use ports 80 and 443 after a restart",+ Now: "Applies it now.",+ }+)++func (s Sysctl) line() string { return s.Key + " = " + s.Value }++func (s Sysctl) current() string {+ data, err := os.ReadFile("/proc/sys/" + strings.ReplaceAll(s.Key, ".", "/"))+ if err != nil {+ return ""+ }+ return strings.TrimSpace(string(data))+}++func (s Sysctl) commands(sudo string) (save, apply string) {+ return "echo '" + s.line() + "' | " + sudo + "tee " + s.File, sudo + "sysctl -p " + s.File+}++func (s Sysctl) HostCommand() string {+ save, apply := s.commands("sudo ")+ return save + " && " + apply+}++func (s Sysctl) Steps() []Step {+ undo := ""+ if cur := s.current(); cur != "" {+ undo = rootCmd("sysctl -w " + s.Key + "=" + cur)+ }+ save, apply := s.commands(rootCmd(""))+ return []Step{+ {+ Do: fmt.Sprintf("Saves %s in %s, %s.", s.line(), s.File, s.Why),+ Command: save,+ Undo: rootCmd("rm " + s.File),+ Run: func(ctx context.Context) error {+ _, err := output(ctx, false, "sh", "-c", "printf '"+s.line()+"\\n' > "+s.File)+ return err+ },+ },+ {+ Do: s.Now,+ Command: apply,+ Undo: undo,+ Run: func(ctx context.Context) error {+ if _, err := output(ctx, false, "sysctl", "-p", s.File); err != nil {+ return err+ }+ if s.check != nil {+ if err := s.check(); err != nil {+ return fmt.Errorf("%w after sysctl -p %s", err, s.File)+ }+ }+ return nil+ },+ },+ }+}diff --git a/fluxer_rexctl/internal/instance/drift.go b/fluxer_rexctl/internal/instance/drift.goindex 3ccf5de3b..117c7594e 100644--- a/fluxer_rexctl/internal/instance/drift.go+++ b/fluxer_rexctl/internal/instance/drift.go@@ -118,27 +118,6 @@ func (r *EnvDrift) Fix() string {return unused + " Change the others with these commands, then delete " + r.Path + ":"}-func (r *EnvDrift) Settings() map[string]*string {- out := map[string]*string{}- for _, c := range r.Changes {- if c.Kind == ChangeSetting {- v := c.Value- out[c.Key] = &v- }- }- return out-}--func (r *EnvDrift) Values(kind ChangeKind) map[string]string {- out := map[string]string{}- for _, c := range r.Changes {- if c.Kind == kind {- out[c.Key] = c.Value- }- }- return out-}-func KeepEdited(inst *Instance, name string) (string, error) {data, err := os.ReadFile(inst.Path(name))if err != nil {diff --git a/fluxer_rexctl/internal/instance/envdiff.go b/fluxer_rexctl/internal/instance/envdiff.goindex 0fa802fc5..6e320b67f 100644--- a/fluxer_rexctl/internal/instance/envdiff.go+++ b/fluxer_rexctl/internal/instance/envdiff.go@@ -32,10 +32,10 @@ func (c EnvChange) Command() string {if c.Secret {return "rexctl config set " + c.Key + " -"}- return "rexctl config set " + c.Key + " " + shellQuote(c.Value)+ return "rexctl config set " + c.Key + " " + ShellQuote(c.Value)}-func shellQuote(v string) string {+func ShellQuote(v string) string {if v == "" {return "''"}