Document every exported type, function and method in the migration, storage and cephfsnative packages and add package comments for migration and the cephmig command, following the commentary rules of Effective Go. Comments on unexported helpers now start with the name they describe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
4.2 KiB
Go
153 lines
4.2 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 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. 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 | 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", "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" {
|
|
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 err = engine.Sync(ctx, c, *statePath, args[0] == "finalize"); 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
|
|
}
|