// Command mesh-control is the control plane: everything that needs to know about more than one // node (novox/hq ADR 0006). // // It runs as one process holding several contexts, each owning its own store. Today it holds one, // `inventory`, and does one thing with it — brings its schema up to date, which is step 3 of the // bootstrap in novox/hq 07-the-substrate and the step the first node cannot get past without. package main import ( "context" "fmt" "os" "os/signal" "syscall" "time" "github.com/novox/mesh-control/internal/inventory" "github.com/novox/mesh-control/internal/store" ) // version is stamped at link time. Unset in a development build, and it says so rather than // claiming a number. var version = "development build" // held is a context this process was granted, and the schema it carries. // // novox/hq ADR 0006 names seven. One is built. The list is short because the others do not exist // yet, not because they are optional. var held = []struct { name string migrations func() ([]store.Migration, error) }{ {inventory.Name, inventory.Migrations}, } func main() { if err := run(); err != nil { fmt.Fprintf(os.Stderr, "mesh-control: %v\n", err) os.Exit(1) } } func run() error { args := os.Args[1:] if len(args) == 0 { usage() return fmt.Errorf("no command given") } ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() switch args[0] { case "migrate": return migrate(ctx) case "version": fmt.Println(version) return nil case "help", "-h", "--help": usage() return nil default: usage() return fmt.Errorf("%q is not a command", args[0]) } } func usage() { fmt.Fprint(os.Stderr, `mesh-control — the control plane migrate bring each context's schema up to date version what this binary is Each context reaches its own store through its own credential (novox/hq ADR 0008), named `+store.Variable("")+`. This process holds: `) for _, c := range held { fmt.Fprintf(os.Stderr, " %-12s database %-12s from %s\n", c.name, store.Database(c.name), store.Variable(c.name)) } fmt.Fprintln(os.Stderr) } // migrate brings every held context's schema up to date. // // Reported per context and per migration, because this runs during a bootstrap on a machine with // nothing else on it — the output is the only account of what happened, and "migrated" is not one. func migrate(ctx context.Context) error { for _, c := range held { migrations, err := c.migrations() if err != nil { return err } s, err := store.Open(ctx, c.name) if err != nil { return err } defer s.Close() // The bootstrap raises PostgreSQL moments before this runs, and a container that is // running is not a database that will answer — a distinction this project has already // paid for once, when a crash-looping database reported itself as up between restarts. if err := s.Ready(ctx, 60*time.Second); err != nil { return err } done, err := s.Migrate(ctx, migrations) for _, m := range done { fmt.Printf("%s: applied %04d-%s\n", c.name, m.Number, m.Name) } if err != nil { return err } if len(done) == 0 { applied, err := s.AppliedMigrations(ctx) if err != nil { return err } fmt.Printf("%s: already up to date — %d migration(s)\n", c.name, len(applied)) } } return nil }