Two changes that arrived together: the cutover phase (ARCHITECTURE.md 4.5) is implemented, and the rollback phase is deleted. Recovery from a failed migration is now explicitly the operator's own snapshot or backup, and out of scope for this tool. internal/cutover implements 4.5 as seven checkpointed steps: verify the staged binary's version, install it, preserve and rewrite the service definition, reload, start, wait for a healthy JMAP session, recalculate quotas. The unit is rewritten in place rather than generated from a template. An operator's unit carries hardening options, limits and dependencies this tool has no business having an opinion about, and regenerating it would silently drop them. It repoints ExecStart (preserving systemd's -@:+! prefix characters and every argument after the executable), updates --config, and strips recovery-mode Environment lines - leaving STALWART_RECOVERY_MODE=1 set would recovery-boot the service on every restart, forever. It refuses on a unit with no ExecStart, and on an Environment line mixing a recovery variable with others: a line it only partly understands is one it must not edit. Quota recalculation is the one step allowed to fail without failing the phase. Its wire format is grounded in Stalwart's x:Task schema reference - Task/set creating one AccountMaintenance per account with maintenanceType recalculateQuota - but the upgrade guide only documents the WebUI path, so two details remain inferred and are called out in stalwartapi/task.go: whether the schema's "read-only" annotation on accountId/maintenanceType means "immutable after creation", and whether a finished task simply leaves the queue (TaskStatus documents Pending/Retry/Failed with no success state). Warning rather than failing is the honest response to that uncertainty, and stale counters are an accounting problem next to calling for a restore of a machine that is otherwise migrated and serving mail. Docker deployments are refused outright: cutting a container over means pulling an image and recreating it, not swapping a binary. On removing rollback. The implementation worked and was tested, and it was removed because restoring bytes correctly is not the hard part. It copied file contents and permissions and verified every restored file against a manifest - and did not preserve ownership. Run as root, as this tool requires, it would have produced a byte-perfect, checksum-verified, root-owned data directory that Stalwart, running as its own user, could not open, and it would have reported success. The PostgreSQL path was worse: pg_dump without --clean emits CREATE TABLE + COPY, which fails replaying into a database whose tables still exist, and the ON_ERROR_STOP=1 added so a half-applied restore couldn't be reported as success turned that into a hard failure. None of it had ever run against a real server. A filesystem snapshot has none of these failure modes, because it never lost the metadata to begin with. So cutover's gate is no longer rollback.CanRollBack but an explicit RecoveryPointConfirmed acknowledgement. That is an assertion, not a check - this tool cannot verify someone else's snapshot - and its only value is that nobody migrates a production mail server having never been asked the question. Two consequences are accepted deliberately: restoring any pre-migration recovery point discards mail delivered since, and a failed migration now stops and reports rather than undoing itself. What the tool still does to make a manual restore easier: the old binary is preserved and never deleted, the original service definition is preserved before the rewrite, the settings and principals dumps stay on disk, and every artifact path and checksum stays in the checkpoint where `status <run-id>` can print it. Also removed: the `confirm` command stub and RollbackWindowClosed, whose only purpose was closing a rollback window that no longer exists, and checkpoint.PhaseRollback. Old state.json files still load - JSON ignores the now-unknown field. Still open, and recorded in 8: cutover ignores systemd drop-ins, so an ExecStart or Environment override in stalwart.service.d/*.conf is invisible to the rewrite - including the recovery variable it exists to strip; nothing prevents concurrent runs on the same run-id; and nothing in this repo has ever run against a real Stalwart, real systemd, or a real store.
122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
package plan
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
// semver is a minimal major.minor.patch version - package-local like the
|
|
// equivalent in internal/preflight, since this comparison is the only thing
|
|
// plan needs from it and duplicating ~20 lines keeps phase packages
|
|
// independent per ARCHITECTURE.md §7.
|
|
type semver struct{ Major, Minor, Patch int }
|
|
|
|
var versionPattern = regexp.MustCompile(`v?(\d+)\.(\d+)\.(\d+)`)
|
|
|
|
func parseSemver(s string) (semver, error) {
|
|
m := versionPattern.FindStringSubmatch(s)
|
|
if m == nil {
|
|
return semver{}, fmt.Errorf("plan: no version number found in %q", s)
|
|
}
|
|
major, _ := strconv.Atoi(m[1])
|
|
minor, _ := strconv.Atoi(m[2])
|
|
patch, _ := strconv.Atoi(m[3])
|
|
return semver{major, minor, patch}, nil
|
|
}
|
|
|
|
func (v semver) String() string { return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch) }
|
|
|
|
func (v semver) Compare(o semver) int {
|
|
if v.Major != o.Major {
|
|
return cmp(v.Major, o.Major)
|
|
}
|
|
if v.Minor != o.Minor {
|
|
return cmp(v.Minor, o.Minor)
|
|
}
|
|
return cmp(v.Patch, o.Patch)
|
|
}
|
|
|
|
func cmp(a, b int) int {
|
|
switch {
|
|
case a < b:
|
|
return -1
|
|
case a > b:
|
|
return 1
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// PhaseName identifies one phase in an ordered migration plan - the phase
|
|
// packages this names are internal/preflight, internal/backup,
|
|
// internal/recovery, internal/cutover and internal/validate.
|
|
type PhaseName string
|
|
|
|
const (
|
|
PhasePreflight PhaseName = "preflight"
|
|
PhaseBackup PhaseName = "backup"
|
|
PhaseRecovery PhaseName = "recovery" // only present when crossing the 0.15/0.16 boundary
|
|
PhaseCutover PhaseName = "cutover"
|
|
PhaseValidate PhaseName = "validate"
|
|
)
|
|
|
|
// Plan is the ordered list of phases one migration run needs, decided once
|
|
// from the source and target versions. See ARCHITECTURE.md §4.6: crossing
|
|
// the 0.15/0.16 boundary needs the full recovery-mode migration (§4.4); a
|
|
// same-boundary patch bump (every 0.16.1-0.16.14 release so far, per
|
|
// Stalwart's own changelog) is the fast path with no recovery phase at all.
|
|
type Plan struct {
|
|
Phases []PhaseName
|
|
CrossesMajorBoundary bool
|
|
SourceVersion string
|
|
TargetVersion string
|
|
Reason string
|
|
}
|
|
|
|
// HasPhase reports whether name appears in the plan.
|
|
func (p *Plan) HasPhase(name PhaseName) bool {
|
|
for _, ph := range p.Phases {
|
|
if ph == name {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Decide returns the plan for migrating from sourceVersion to
|
|
// targetVersion. It refuses (rather than guesses) if the source is already
|
|
// at or beyond the target, since that's not a migration this tool should
|
|
// attempt to run.
|
|
func Decide(sourceVersion, targetVersion string) (*Plan, error) {
|
|
src, err := parseSemver(sourceVersion)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("plan: parse source version %q: %w", sourceVersion, err)
|
|
}
|
|
tgt, err := parseSemver(targetVersion)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("plan: parse target version %q: %w", targetVersion, err)
|
|
}
|
|
if src.Compare(tgt) >= 0 {
|
|
return nil, fmt.Errorf("plan: source %s is already at or beyond target %s - nothing to migrate", src, tgt)
|
|
}
|
|
|
|
crosses := src.Major == 0 && src.Minor < 16 && (tgt.Major > 0 || tgt.Minor >= 16)
|
|
if crosses {
|
|
return &Plan{
|
|
Phases: []PhaseName{PhasePreflight, PhaseBackup, PhaseRecovery, PhaseCutover, PhaseValidate},
|
|
CrossesMajorBoundary: true,
|
|
SourceVersion: src.String(),
|
|
TargetVersion: tgt.String(),
|
|
Reason: fmt.Sprintf("%s -> %s crosses the 0.15/0.16 major boundary: full recovery-mode migration required", src, tgt),
|
|
}, nil
|
|
}
|
|
return &Plan{
|
|
Phases: []PhaseName{PhasePreflight, PhaseBackup, PhaseCutover, PhaseValidate},
|
|
CrossesMajorBoundary: false,
|
|
SourceVersion: src.String(),
|
|
TargetVersion: tgt.String(),
|
|
Reason: fmt.Sprintf("%s -> %s is a same-boundary patch upgrade: fast path applies (no recovery-mode phase)", src, tgt),
|
|
}, nil
|
|
}
|