Files
CephfsMig/cmd/cephmig/main.go
T
Sebastian UnterschützandClaude Opus 5.5 907d400765 feat: remove cephmig snapshots during sync and add cleanup command
Users of a subvolume should not see traces of the migration. Each sync
round now deletes the older cephmig-* round snapshots at source and
target and keeps only the newest one as base for the next round. The new
command `cephmig cleanup` deletes the remaining one after the cutover.
Application snapshots are never touched.

The journal marks a snapshot as removed before deleting it, so an
interrupted run finishes the deletion instead of reporting a vanished
snapshot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 19:52:13 +01:00

185 lines
4.5 KiB
Go

// Command cephmig migrates a CephFS subvolume, including its snapshots,
// between two Ceph clusters while the application keeps running.
//
// Usage:
//
// cephmig doctor
// cephmig inspect --config FILE
// cephmig sync --config FILE --state FILE
// cephmig finalize --config FILE --state FILE --writers-stopped
// cephmig cleanup --config FILE --state FILE
// cephmig status --state FILE
//
// inspect checks both clusters without changing them. sync copies the
// current state and may be repeated. finalize performs the last round after
// all writers are stopped and marks the job ready for cutover. cleanup
// deletes the snapshots cephmig created, at source and target, once the
// cutover is done. status prints the migration journal. All results are
// written to standard output as JSON.
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"git.zb-server.de/Sebi/CephfsMig/internal/migration"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := run(ctx, os.Args[1:], os.Stdout, os.Stderr); err != nil {
fmt.Fprintln(os.Stderr, "cephmig:", err)
os.Exit(1)
}
}
const usage = "usage: cephmig doctor | inspect --config FILE | sync --config FILE --state FILE | finalize --config FILE --state FILE --writers-stopped | cleanup --config FILE --state FILE | status --state FILE"
func run(ctx context.Context, args []string, out, log io.Writer) error {
if len(args) == 0 {
return errors.New(usage)
}
if args[0] == "help" || args[0] == "--help" || args[0] == "-h" {
fmt.Fprintln(out, usage)
return nil
}
if args[0] == "doctor" {
if len(args) != 1 {
return errors.New(usage)
}
return doctor(ctx, out, log)
}
switch args[0] {
case "inspect", "sync", "finalize", "cleanup", "status":
default:
return errors.New(usage)
}
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
flags.SetOutput(log)
configPath := flags.String("config", "", "JSON migration configuration")
statePath := flags.String("state", "", "persistent local state file (outside CephFS mounts)")
stopped := flags.Bool("writers-stopped", false, "confirm ALL source writers are stopped; keep them stopped until cutover completes")
if err := flags.Parse(args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if flags.NArg() != 0 {
return errors.New("unexpected positional arguments")
}
if args[0] == "status" {
if *statePath == "" {
return errors.New("--state is required")
}
s, err := migration.LoadState(*statePath)
if err != nil {
return err
}
return json.NewEncoder(out).Encode(s)
}
if *configPath == "" {
return errors.New("--config is required")
}
c, err := migration.ReadConfig(*configPath)
if err != nil {
return err
}
process := migration.Process{Stderr: log}
if args[0] != "inspect" && *statePath == "" {
return errors.New("--state is required")
}
if args[0] == "finalize" && !*stopped {
return errors.New("finalize requires --writers-stopped; stop and flush all application writers first")
}
if args[0] != "finalize" && *stopped {
return errors.New("--writers-stopped is only valid with finalize")
}
if c.Backend != "native" && args[0] != "inspect" && args[0] != "cleanup" {
if _, err = exec.LookPath("rsync"); err != nil {
return fmt.Errorf("rsync is required: %w", err)
}
}
engine, closeEngine, err := migration.NewEngine(ctx, c, process, log)
if err != nil {
return err
}
defer closeEngine.Close()
if args[0] == "inspect" {
info, err := engine.Inspect(ctx, c)
if err != nil {
return err
}
return json.NewEncoder(out).Encode(info)
}
if args[0] == "cleanup" {
err = engine.Cleanup(ctx, c, *statePath)
} else {
err = engine.Sync(ctx, c, *statePath, args[0] == "finalize")
}
if err != nil {
return err
}
s, err := migration.LoadState(*statePath)
if err != nil {
return err
}
return json.NewEncoder(out).Encode(s)
}
func doctor(ctx context.Context, out, log io.Writer) error {
checks := []struct {
name string
args []string
}{
{"go", []string{"version"}},
{"docker", []string{"compose", "version"}},
{"docker", []string{"info", "--format", "Docker server: {{.ServerVersion}}"}},
}
for _, check := range checks {
checkCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
cmd := exec.CommandContext(checkCtx, check.name, check.args...)
cmd.Stdout, cmd.Stderr = out, log
err := cmd.Run()
cancel()
if err != nil {
return fmt.Errorf("check %s: %w", check.name, err)
}
}
return nil
}