Rollback was the one phase gating everything else: `run` without --dry-run refused because this tool could not undo a cutover it had committed to. That reason is now gone, and the refusal has narrowed to the fact that there is no real cutover to undo yet. internal/rollback implements ARCHITECTURE.md 4.8 as eight checkpointed steps under PhaseRollback: verify-backup, stop-service, preserve-failed-state, restore-data, restore-binary, restore-service-config, start-service, verify-rollback. Three things depart from what 4.8 specified, each for a reason: - The backup is re-verified against its manifest *before* the service is stopped, which the design didn't call out. Finding a corrupt backup is survivable while the failed instance is still up, and unsurvivable once its data directory has been moved aside. - BuildPlan is separate from Run, so every reason to refuse (closed rollback window, FoundationDB, no recorded backup, unknown deployment kind, missing database credentials) is found before anything is touched. The CLI prints that resolved plan and acts only with --yes. - The restore is re-verified against the same manifest after writing. A restore that put back truncated bytes and reported success would be worse than one that failed outright. Nothing from the failed attempt is deleted: the half-migrated data directory and the displaced binary are moved to .failed-<run-id> names, so a retry after the underlying issue is fixed still has both the evidence and the artifacts. Afterwards a reduced validation suite runs against the *restored* instance (version, reachability, directory counts) rather than assuming the restore worked. internal/service is a new package holding the systemd/Docker control this needs. It's separate rather than living inside internal/rollback because cutover will need the identical operations, and because the commands that can take mail delivery down belong in one auditable place - the same reasoning that makes stalwartapi the only thing speaking JMAP. preflight.DeploymentKind is now a type alias for service.Kind so detection and control can't drift apart. Its Active() reads `systemctl is-active`'s output rather than its exit status: systemctl exits non-zero for every non-active state, so exit-status logic would make "inactive" - the answer a rollback most needs - look like a failure to read the state at all. Also fixes a pre-existing bug in `status`: Go's flag package stops parsing at the first positional argument, so `status <run-id> --state-dir X` looked the run up in the default directory and reported it missing. `rollback` would have inherited the same footgun on a command whose flags decide what gets overwritten. Still open: `confirm` cannot set RollbackWindowClosed. Rollback honours the flag and refuses when it's set, but closing the window is the point of no return for the backups this restores from, so it should land with the retention policy 6 describes rather than before it. Verified end to end against a fake systemd deployment: half-migrated data restored to its original contents, failed state preserved, old binary reinstalled and reporting 0.15.5, unit restarted, and a re-run of the completed rollback inert.
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
|
|
"github.com/LINUXexpert-org/stalwart-migrator/internal/checkpoint"
|
|
)
|
|
|
|
func runStatus(args []string) error {
|
|
fs := flag.NewFlagSet("status", flag.ExitOnError)
|
|
stateDir := fs.String("state-dir", checkpoint.DefaultBaseDir, "directory runs are checkpointed in")
|
|
|
|
// Same treatment as `rollback`: without this, `status <run-id>
|
|
// --state-dir X` would look up the run in the default directory and
|
|
// report it missing, since flag parsing stops at the run-id.
|
|
runID, rest := splitRunID(fs, args)
|
|
if err := fs.Parse(rest); err != nil {
|
|
return err
|
|
}
|
|
if fs.NArg() != 0 {
|
|
return fmt.Errorf("usage: stalwart-migrate status [run-id] [flags]")
|
|
}
|
|
|
|
store := checkpoint.NewStore(*stateDir)
|
|
|
|
if runID == "" {
|
|
ids, err := store.List()
|
|
if err != nil {
|
|
return fmt.Errorf("list runs: %w", err)
|
|
}
|
|
if len(ids) == 0 {
|
|
fmt.Println("no runs found in", *stateDir)
|
|
return nil
|
|
}
|
|
fmt.Println("runs (newest first):")
|
|
for _, id := range ids {
|
|
fmt.Println(" ", id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
rs, err := store.Load(runID)
|
|
if err != nil {
|
|
return fmt.Errorf("load run %s: %w", runID, err)
|
|
}
|
|
|
|
fmt.Printf("run: %s\n", rs.RunID)
|
|
fmt.Printf("source: %s\n", rs.SourceVersion)
|
|
fmt.Printf("target: %s\n", rs.TargetVersion)
|
|
fmt.Printf("topology: deployment=%s store=%s\n", rs.Topology.DeploymentKind, rs.Topology.StoreBackend)
|
|
fmt.Printf("rollback window closed: %v\n", rs.RollbackWindowClosed)
|
|
fmt.Println("steps:")
|
|
for _, step := range rs.Steps {
|
|
tag := string(step.Status)
|
|
if step.Verdict != "" {
|
|
tag = step.Verdict
|
|
}
|
|
line := fmt.Sprintf(" [%-4s] %s/%s", tag, step.Phase, step.Name)
|
|
if step.Detail != "" {
|
|
line += " - " + step.Detail
|
|
}
|
|
if step.Error != "" {
|
|
line += " (error: " + step.Error + ")"
|
|
}
|
|
fmt.Println(line)
|
|
}
|
|
return nil
|
|
}
|