diff --git a/cmd/archie-state-store/main.go b/cmd/archie-state-store/main.go index 0048c25fd..323a4a8ea 100644 --- a/cmd/archie-state-store/main.go +++ b/cmd/archie-state-store/main.go @@ -11,7 +11,7 @@ import ( "strings" "syscall" - "github.com/samcharles93/archie-core/internal/app/archied" + "github.com/samcharles93/archie-core/internal/app/statestore" "github.com/samcharles93/archie-core/internal/buildinfo" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" ) @@ -23,10 +23,13 @@ func run() int { // including no arguments at all, serves the State Store contract. An // unrecognised word is refused rather than ignored, so a mistyped recovery // command never leaves an operator watching a server start instead. + if args := os.Args[1:]; statestore.IsAccessResetArgs(args) { + return statestore.RunAccessReset(context.Background(), args[2:], os.Stderr) + } if args := os.Args[1:]; len(args) > 0 && !strings.HasPrefix(args[0], "-") { return runRecovery(args, os.Stdout, os.Stderr) } - var options archied.StateStoreOptions + var options statestore.Options showVersion := buildinfo.RegisterVersionFlag("archie-state-store") flag.StringVar(&options.Config, "config", configuration.DefaultConfigPath(), "configuration file or directory") flag.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory") @@ -34,14 +37,14 @@ func run() int { flag.StringVar(&options.Token, "token", "", "bearer token required for a non-loopback listener (defaults to [services.state].target_token)") flag.StringVar(&options.ReadyAddr, "ready-addr", "", "optional readiness HTTP listen address (e.g. 127.0.0.1:9091)") flag.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\noffline recovery: archie-state-store -h\n\n") + fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\noffline recovery: archie-state-store -h | access reset -h\n\n") flag.PrintDefaults() } flag.Parse() showVersion() ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() - if err := archied.RunStateStore(ctx, options); err != nil { + if err := statestore.Run(ctx, options); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } diff --git a/cmd/archie-state-store/recovery.go b/cmd/archie-state-store/recovery.go index 42ff19115..976ba0f41 100644 --- a/cmd/archie-state-store/recovery.go +++ b/cmd/archie-state-store/recovery.go @@ -7,7 +7,7 @@ import ( "fmt" "io" - "github.com/samcharles93/archie-core/internal/app/archied" + "github.com/samcharles93/archie-core/internal/app/statestore" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" ) @@ -51,9 +51,9 @@ func runRecovery(args []string, stdout, stderr io.Writer) int { return 2 } command := args[0] - options := archied.StateStoreRecoveryOptions{Operation: command} + options := statestore.RecoveryOptions{Operation: command} switch command { - case archied.RecoveryBackup, archied.RecoveryRestore, archied.RecoveryValidate, archied.RecoveryRollback: + case statestore.RecoveryBackup, statestore.RecoveryRestore, statestore.RecoveryValidate, statestore.RecoveryRollback: default: fmt.Fprintf(stderr, "archie-state-store: unknown command %q\n\n", command) fmt.Fprint(stderr, recoveryUsage) @@ -84,7 +84,7 @@ func runRecovery(args []string, stdout, stderr io.Writer) int { return 2 } - summary, err := archied.RunStateStoreRecovery(context.Background(), options) + summary, err := statestore.RunRecovery(context.Background(), options) if err != nil { fmt.Fprintf(stderr, "archie-state-store %s: %v\n", command, err) return 1 diff --git a/cmd/archied/main.go b/cmd/archied/main.go index 5955eb316..a12379d30 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -2,7 +2,6 @@ package main import ( - "context" "os" "github.com/samcharles93/archie-core/internal/app/archied" @@ -16,10 +15,5 @@ func main() { if archied.IsSetupArgs(args) { os.Exit(archied.RunSetup(args[1:], os.Stdin, os.Stdout, os.Stderr)) } - // The access recovery command runs before the daemon's flags are parsed: - // it is a State Store host operation, not a daemon run. - if archied.IsAccessResetArgs(args) { - os.Exit(archied.RunAccessReset(context.Background(), args[2:], os.Stderr)) - } os.Exit(archied.Run()) } diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 0e3de9f21..d528ab1ec 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -22,6 +22,8 @@ import ( "syscall" "time" + "github.com/samcharles93/archie-core/internal/app/servicekit" + "github.com/jackc/pgx/v5/pgxpool" natsio "github.com/nats-io/nats.go" "github.com/samcharles93/ai-sdk/runtime" @@ -113,21 +115,11 @@ type boot struct { playbooks *playbook.Store - st storecontract.TaskStore - eda eventCaptureStore - // pg is the State Store process's process-scoped PostgreSQL pool, opened - // and migrated by openStateStorePool. It is the one connection the - // standalone archie-state-store binary holds: do not open a second pool - // per subsystem. Nil for every other process: the daemon and Gateway reach - // the State Store over gRPC and hold only the conversation store's pool. - pg *pgxpool.Pool // stateStore is the State Store contract adapter every daemon and gateway // store consumer depends on. It is ALWAYS the remote *staterpc.Client // dialed to the standalone archie-state-store gRPC service at // [services.state].target. It is set by openStateStoreAdapter, which - // requires [services.state].target to be set. The b.st field remains solely - // for the standalone archie-state-store binary, which serves the task store - // from Postgres. + // requires [services.state].target to be set. stateStore storecontract.TaskStore // accessChain is the daemon's policy engine, built once from the stored // policies (openAccessChain); accessProblems is what the readiness @@ -354,55 +346,29 @@ func (b *boot) cleanup() { } } -// loadConfig resolves the file config. A Resolve failure is reported on -// stderr because the file log destination is itself configuration that has -// not been read yet. func (b *boot) loadConfig(_ context.Context, cfgPath, overlayPath string) error { - loader := configuration.New(b.log) - b.loader = loader - doc, err := loader.Resolve(cfgPath, overlayPath) + loader, doc, err := servicekit.Resolve(b.log, cfgPath, overlayPath) if err != nil { - fmt.Fprintln(os.Stderr, err) return err } - b.doc = doc - // A stray/misspelled key parses and validates cleanly (unknown TOML keys - // are otherwise silently discarded), so it must be visible somewhere rather - // than just quietly doing nothing. - if len(doc.UnknownKeys) > 0 { - b.log.Warn("config file has unrecognised keys; check for typos", "keys", doc.UnknownKeys) - } + b.loader, b.doc = loader, doc b.cfg = b.doc.Config b.cfgHolder = config.NewHolder(b.cfg) b.currentProvenance.Store(&b.doc.Provenance) - return b.setupLogging() + b.setupLogging() + return nil } -func (b *boot) setupLogging() error { - cfg := b.cfg +// setupLogging stays on stderr for an offline command (stderrLog): creating, +// appending to or rotating the deployment's log is a side effect a diagnosis +// must not have. +func (b *boot) setupLogging() { if b.stderrLog { - return nil + return } - logFeed := logging.NewFeed(1000) - b.logFeed = logFeed - taskLogs := logging.NewTaskRegistry(filepath.Join(cfg.StateDir, "logs", "tasks"), logFeed, logging.TaskSinkOptions{}) - b.taskLogs = taskLogs - fileLog, logCloser, logErr := logging.New(logging.Options{ - File: cfg.Log.File, - MaxSizeMB: cfg.Log.MaxSizeMB, - Keep: cfg.Log.Keep, - Level: cfg.Log.Level, - Stderr: !cfg.Log.Quiet, - Feed: logFeed, - }) - b.log = fileLog.With("component", "daemon") - b.addCleanup(func() { _ = logCloser.Close() }) - if logErr != nil { - b.log.Error("file logging disabled", "err", logErr) - } else if cfg.Log.File != "" { - b.log.Info("logging to file", "path", cfg.Log.File) - } - return nil + logs := servicekit.Logging(b.cfg, "daemon") + b.log, b.logFeed, b.taskLogs = logs.Log, logs.Feed, logs.TaskLogs + b.addCleanup(func() { _ = logs.Closer.Close() }) } // openStores wires the secret registry and the conversation store. The @@ -474,7 +440,7 @@ func (b *boot) openAccessChain(ctx context.Context) error { } func (b *boot) openChatSessions(ctx context.Context) error { - pool, err := openServicePool(ctx, b.cfg.DatabaseURL, "the conversation store") + pool, err := servicekit.OpenPool(ctx, b.cfg.DatabaseURL, "the conversation store") if err != nil { return fmt.Errorf("open conversation store: %w", err) } @@ -1256,7 +1222,7 @@ func (b *boot) buildDaemon() { ContainerPool: b.containerPool, KitLauncher: b.kitLauncher, Identities: b.identityRunners, - RootIdentityID: identity.StableID(configuredIdentityNames(b.cfg)[0]), + RootIdentityID: identity.StableID(servicekit.IdentityNames(b.cfg)[0]), TaskLogs: b.taskLogs, AgentStatus: b.agentStatus, KindWorkflows: b.kindWorkflows, diff --git a/internal/app/archied/gateway.go b/internal/app/archied/gateway.go index 75d070eaa..5aafde9cb 100644 --- a/internal/app/archied/gateway.go +++ b/internal/app/archied/gateway.go @@ -7,6 +7,8 @@ import ( "os" "time" + "github.com/samcharles93/archie-core/internal/app/servicekit" + natsio "github.com/nats-io/nats.go" "google.golang.org/grpc" @@ -38,7 +40,7 @@ type GatewayOptions struct { // inventing a value -- resolveServiceListen explains why an empty address is an // error rather than a default. func gatewayListenAndToken(b *boot, options GatewayOptions) (listen, token string, err error) { - listen, err = resolveServiceListen("gateway", options.Listen, b.cfg.Services.Get(config.ServiceNameGateway).Listen) + listen, err = servicekit.ResolveListen("gateway", options.Listen, b.cfg.Services.Get(config.ServiceNameGateway).Listen) if err != nil { return "", "", err } diff --git a/internal/app/archied/health.go b/internal/app/archied/health.go index 82cd846c2..b78658cba 100644 --- a/internal/app/archied/health.go +++ b/internal/app/archied/health.go @@ -3,11 +3,10 @@ package archied import ( "context" "encoding/json" - "fmt" - "net" "net/http" "sync/atomic" - "time" + + "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/domain/health" ) @@ -89,26 +88,11 @@ func (b *boot) startHealth(ctx context.Context) error { return nil } -// serveHealth runs mux on addr until shutdown, registering its own cleanup. -// Shared by the daemon and the standalone State Store, which serve the same -// three routes over different probes. label names the surface in the log and -// error text, which operators and the State Store's own process test read. func (b *boot) serveHealth(ctx context.Context, addr string, mux http.Handler, label string) error { - listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", addr) + stop, err := servicekit.ServeHealth(ctx, addr, mux, label, b.log) if err != nil { - return fmt.Errorf("listen for %s: %w", label, err) + return err } - srv := &http.Server{Handler: mux, ReadHeaderTimeout: 5 * time.Second} - go func() { - if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed { - b.log.Error(label+" server stopped", "err", err) - } - }() - b.addCleanup(func() { - shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second) - defer cancel() - _ = srv.Shutdown(shutdownCtx) - }) - b.log.Info(label+" listening", "addr", "http://"+listener.Addr().String()) + b.addCleanup(stop) return nil } diff --git a/internal/app/servicekit/config.go b/internal/app/servicekit/config.go new file mode 100644 index 000000000..12e4d9bab --- /dev/null +++ b/internal/app/servicekit/config.go @@ -0,0 +1,60 @@ +package servicekit + +import ( + "fmt" + "io" + "log/slog" + "os" + "path/filepath" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" + "github.com/samcharles93/archie-core/internal/logging" +) + +// Resolve reads the file config. A failure is printed to stderr because the +// file log destination is itself configuration that has not been read yet. +func Resolve(log *slog.Logger, cfgPath, overlayPath string) (*configuration.Loader, *configuration.Document, error) { + loader := configuration.New(log) + doc, err := loader.Resolve(cfgPath, overlayPath) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return nil, nil, err + } + // Unknown TOML keys are otherwise discarded silently, so a typo would + // quietly do nothing. + if len(doc.UnknownKeys) > 0 { + log.Warn("config file has unrecognised keys; check for typos", "keys", doc.UnknownKeys) + } + return loader, doc, nil +} + +// Logs is a service's configured log and the task log registry beside it. +type Logs struct { + Log *slog.Logger + Feed *logging.Feed + TaskLogs *logging.TaskRegistry + Closer io.Closer +} + +// Logging opens the configured log for component. A file log that cannot be +// opened is reported and logging continues on stderr. +func Logging(cfg config.Config, component string) Logs { + feed := logging.NewFeed(1000) + taskLogs := logging.NewTaskRegistry(filepath.Join(cfg.StateDir, "logs", "tasks"), feed, logging.TaskSinkOptions{}) + fileLog, closer, err := logging.New(logging.Options{ + File: cfg.Log.File, + MaxSizeMB: cfg.Log.MaxSizeMB, + Keep: cfg.Log.Keep, + Level: cfg.Log.Level, + Stderr: !cfg.Log.Quiet, + Feed: feed, + }) + log := fileLog.With("component", component) + if err != nil { + log.Error("file logging disabled", "err", err) + } else if cfg.Log.File != "" { + log.Info("logging to file", "path", cfg.Log.File) + } + return Logs{Log: log, Feed: feed, TaskLogs: taskLogs, Closer: closer} +} diff --git a/internal/app/servicekit/health.go b/internal/app/servicekit/health.go new file mode 100644 index 000000000..6551ba84d --- /dev/null +++ b/internal/app/servicekit/health.go @@ -0,0 +1,32 @@ +package servicekit + +import ( + "context" + "errors" + "fmt" + "log/slog" + "net" + "net/http" + "time" +) + +// ServeHealth serves mux on addr in the background and returns the function +// that shuts it down. label names the surface in log and error text. +func ServeHealth(ctx context.Context, addr string, mux http.Handler, label string, log *slog.Logger) (func(), error) { + listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", addr) + if err != nil { + return nil, fmt.Errorf("listen for %s: %w", label, err) + } + srv := &http.Server{Handler: mux, ReadHeaderTimeout: 5 * time.Second} + go func() { + if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Error(label+" server stopped", "err", err) + } + }() + log.Info(label+" listening", "addr", "http://"+listener.Addr().String()) + return func() { + shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second) + defer cancel() + _ = srv.Shutdown(shutdownCtx) + }, nil +} diff --git a/internal/app/servicekit/identities.go b/internal/app/servicekit/identities.go new file mode 100644 index 000000000..1084b7335 --- /dev/null +++ b/internal/app/servicekit/identities.go @@ -0,0 +1,20 @@ +package servicekit + +import "github.com/samcharles93/archie-core/internal/config" + +// IdentityNames is the agent identities the config names, or the bot user +// alone when it names none. +func IdentityNames(cfg config.Config) []string { + if len(cfg.Identities) == 0 { + name := cfg.BotUser + if name == "" { + name = "archie" + } + return []string{name} + } + names := make([]string, 0, len(cfg.Identities)) + for _, value := range cfg.Identities { + names = append(names, value.Name) + } + return names +} diff --git a/internal/app/archied/listen.go b/internal/app/servicekit/listen.go similarity index 87% rename from internal/app/archied/listen.go rename to internal/app/servicekit/listen.go index 7fad92e00..d4565aff4 100644 --- a/internal/app/archied/listen.go +++ b/internal/app/servicekit/listen.go @@ -1,11 +1,11 @@ -package archied +package servicekit import ( "fmt" "strings" ) -// resolveServiceListen picks the address a service listener binds: the -listen +// ResolveListen picks the address a service listener binds: the -listen // flag when it was given, else the address the configuration names. // // Both services this repository runs used to take their address from a flag @@ -21,7 +21,7 @@ import ( // An empty result is an error, never a fallback: net.Listen treats "" as "any // free port", so defaulting here would bind the service somewhere its clients // do not look and report success while doing it. -func resolveServiceListen(service, flagValue, configured string) (string, error) { +func ResolveListen(service, flagValue, configured string) (string, error) { if v := strings.TrimSpace(flagValue); v != "" { return v, nil } diff --git a/internal/app/servicekit/pool.go b/internal/app/servicekit/pool.go new file mode 100644 index 000000000..e64d61628 --- /dev/null +++ b/internal/app/servicekit/pool.go @@ -0,0 +1,27 @@ +package servicekit + +import ( + "context" + "fmt" + + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" +) + +// OpenPool opens and migrates the PostgreSQL pool a service owns its tables in. +// service names the owner in the error an empty database_url produces. +func OpenPool(ctx context.Context, url, service string) (*pgxpool.Pool, error) { + if url == "" { + return nil, fmt.Errorf("database_url is required: %s is Postgres-only", service) + } + pool, err := postgres.Open(ctx, url) + if err != nil { + return nil, err + } + if err := postgres.Migrate(ctx, pool, postgres.Migrations()); err != nil { + pool.Close() + return nil, err + } + return pool, nil +} diff --git a/internal/app/archied/access_reset.go b/internal/app/statestore/access_reset.go similarity index 96% rename from internal/app/archied/access_reset.go rename to internal/app/statestore/access_reset.go index 6c92aca2b..1bdf08954 100644 --- a/internal/app/archied/access_reset.go +++ b/internal/app/statestore/access_reset.go @@ -1,4 +1,4 @@ -// The `archied access reset` command: the locked-out-org and broken-instance +// The `archie-state-store access reset` command: the locked-out-org and broken-instance // recovery surface. It restores the shipped role policies for one org and // removes its other org-level policies, or removes the stored instance // policies; the cross-org forbid is engine-enforced and survives any reset. @@ -7,7 +7,7 @@ // recorded as an audit event. It claims the same store ownership the State // Store holds, so a running store refuses it: stop the store, reset, start // the store. -package archied +package statestore import ( "context" @@ -54,7 +54,7 @@ func RunAccessReset(ctx context.Context, args []string, stderr io.Writer) int { return 2 } - b := newBootstrap() + b := newServer() defer b.cleanup() if err := b.loadConfig(ctx, *cfgPath, *overlayPath); err != nil { return 1 @@ -65,6 +65,7 @@ func RunAccessReset(ctx context.Context, args []string, stderr io.Writer) int { fmt.Fprintln(stderr, "reset: open the State Store database (stop archie-state-store first)") return 1 } + b.openTaskStore() resetter, ok := b.st.(access.Resetter) if !ok { fmt.Fprintln(stderr, "reset: the State Store does not support the access reset surface") diff --git a/internal/app/archied/binding_cipher.go b/internal/app/statestore/binding_cipher.go similarity index 98% rename from internal/app/archied/binding_cipher.go rename to internal/app/statestore/binding_cipher.go index 34d3cf293..29cc32a95 100644 --- a/internal/app/archied/binding_cipher.go +++ b/internal/app/statestore/binding_cipher.go @@ -1,4 +1,4 @@ -package archied +package statestore import ( "fmt" diff --git a/internal/app/statestore/boot.go b/internal/app/statestore/boot.go new file mode 100644 index 000000000..7231c4f41 --- /dev/null +++ b/internal/app/statestore/boot.go @@ -0,0 +1,74 @@ +package statestore + +import ( + "context" + "log/slog" + "net/http" + "os" + "slices" + + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/samcharles93/archie-core/internal/app/servicekit" + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/logging" + "github.com/samcharles93/archie-core/internal/sdnotify" + "github.com/samcharles93/archie-core/internal/secret" +) + +// server is the State Store process: the one owner of the task and +// control-plane tables. +type server struct { + cfg config.Config + log *slog.Logger + // stderrLog keeps an offline command off cfg.Log.File: a diagnosis must + // not create, append to or rotate the deployment's log. + stderrLog bool + + secrets *secret.Registry + taskLogs *logging.TaskRegistry + pg *pgxpool.Pool + st storecontract.TaskStore + eda eventCaptureStore + + cleanups []func() +} + +func newServer() *server { + return &server{log: slog.New(slog.NewJSONHandler(os.Stderr, nil))} +} + +func (b *server) addCleanup(fn func()) { b.cleanups = append(b.cleanups, fn) } + +func (b *server) cleanup() { + for _, fn := range slices.Backward(b.cleanups) { + fn() + } +} + +func (b *server) loadConfig(_ context.Context, cfgPath, overlayPath string) error { + _, doc, err := servicekit.Resolve(b.log, cfgPath, overlayPath) + if err != nil { + return err + } + b.cfg = doc.Config + if b.stderrLog { + return nil + } + logs := servicekit.Logging(b.cfg, "state-store") + b.log, b.taskLogs = logs.Log, logs.TaskLogs + b.addCleanup(func() { _ = logs.Closer.Close() }) + return nil +} + +func (b *server) serveHealth(ctx context.Context, addr string, mux http.Handler, label string) error { + stop, err := servicekit.ServeHealth(ctx, addr, mux, label, b.log) + if err != nil { + return err + } + b.addCleanup(stop) + return nil +} + +func (b *server) announceReady() { sdnotify.Ready(b.log) } diff --git a/internal/app/archied/state_store_eda.go b/internal/app/statestore/eda.go similarity index 91% rename from internal/app/archied/state_store_eda.go rename to internal/app/statestore/eda.go index 4ae6d6240..9b752b23e 100644 --- a/internal/app/archied/state_store_eda.go +++ b/internal/app/statestore/eda.go @@ -1,4 +1,4 @@ -package archied +package statestore import ( "github.com/samcharles93/archie-core/internal/domain/storecontract" @@ -23,6 +23,6 @@ type eventCaptureStore interface { //nolint:interfacebloat // composite of the s // openEDAStore serves the event-capture store from the State Store's Postgres // pool, sealing binding secrets with cipher. -func (b *boot) openEDAStore(cipher bindingcipher.BindingCipher) { +func (b *server) openEDAStore(cipher bindingcipher.BindingCipher) { b.eda = postgres.NewEDA(b.pg, cipher) } diff --git a/internal/app/archied/state_store_pool.go b/internal/app/statestore/pool.go similarity index 59% rename from internal/app/archied/state_store_pool.go rename to internal/app/statestore/pool.go index c27381c0c..b79ca26ee 100644 --- a/internal/app/archied/state_store_pool.go +++ b/internal/app/statestore/pool.go @@ -1,11 +1,9 @@ -package archied +package statestore import ( "context" - "fmt" - - "github.com/jackc/pgx/v5/pgxpool" + "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/infrastructure/postgres" ) @@ -17,8 +15,8 @@ import ( // // The pool is stored on boot.pg and closed at shutdown; nothing opens a second // pool per subsystem. -func (b *boot) openStateStorePool(ctx context.Context) error { - pool, err := openServicePool(ctx, b.cfg.DatabaseURL, "the State Store") +func (b *server) openStateStorePool(ctx context.Context) error { + pool, err := servicekit.OpenPool(ctx, b.cfg.DatabaseURL, "the State Store") if err != nil { b.log.Error("open state store postgres pool", "err", err) return err @@ -37,20 +35,3 @@ func (b *boot) openStateStorePool(ctx context.Context) error { b.addCleanup(func() { _ = ownership.Release(releaseCtx) }) return nil } - -// openServicePool opens and migrates a serving process's pool. service names -// the process in the error an operator reads. -func openServicePool(ctx context.Context, url, service string) (*pgxpool.Pool, error) { - if url == "" { - return nil, fmt.Errorf("database_url is required: %s is Postgres-only", service) - } - pool, err := postgres.Open(ctx, url) - if err != nil { - return nil, err - } - if err := postgres.Migrate(ctx, pool, postgres.Migrations()); err != nil { - pool.Close() - return nil, err - } - return pool, nil -} diff --git a/internal/app/archied/state_store_recovery.go b/internal/app/statestore/recovery.go similarity index 92% rename from internal/app/archied/state_store_recovery.go rename to internal/app/statestore/recovery.go index dbd6f2ea4..c68a4faf6 100644 --- a/internal/app/archied/state_store_recovery.go +++ b/internal/app/statestore/recovery.go @@ -16,7 +16,7 @@ // the offline checks resolve the same workflow step vocabulary as the process // whose verdict they predict, and a constructor error is a refusal here rather // than a server that would validate against a different vocabulary. -package archied +package statestore import ( "context" @@ -49,9 +49,9 @@ const ( RecoveryRollback = "rollback" ) -// StateStoreRecoveryOptions are the process inputs for one offline recovery +// RecoveryOptions are the process inputs for one offline recovery // operation. -type StateStoreRecoveryOptions struct { +type RecoveryOptions struct { // Operation is one of backup, restore, validate, rollback. Operation string // Out is the snapshot backup writes. @@ -71,16 +71,16 @@ type StateStoreRecoveryOptions struct { Revision int64 } -// RunStateStoreRecovery performs one offline operation on the task database and +// RunRecovery performs one offline operation on the task database and // returns the line the command reports on stdout. -func RunStateStoreRecovery(ctx context.Context, options StateStoreRecoveryOptions) (string, error) { +func RunRecovery(ctx context.Context, options RecoveryOptions) (string, error) { return runPostgresRecovery(ctx, options) } // checkBootGate runs the writer's per-resource validation over what is stored, // then boot's gate over the file config with the stored resources layered on, // and returns how many stored resources validated. -func checkBootGate(ctx context.Context, server *controlplane.Server, stored bool, options StateStoreRecoveryOptions) (int, error) { +func checkBootGate(ctx context.Context, server *controlplane.Server, stored bool, options RecoveryOptions) (int, error) { checked := 0 if stored { var err error @@ -115,8 +115,8 @@ func checkBootGate(ctx context.Context, server *controlplane.Server, stored bool // It reads that config with the daemon's stderr logger: resolving it is a // diagnosis, and a diagnosis that opened, appended to or rotated cfg.Log.File // would edit the deployment it is inspecting. -func bootConfig(ctx context.Context, options StateStoreRecoveryOptions) (config.Config, error) { - b := newBootstrap() +func bootConfig(ctx context.Context, options RecoveryOptions) (config.Config, error) { + b := newServer() b.stderrLog = true defer b.cleanup() if err := b.loadConfig(ctx, options.Config, options.Overlay); err != nil { @@ -127,7 +127,7 @@ func bootConfig(ctx context.Context, options StateStoreRecoveryOptions) (config. // replayRevision puts back the revision options names through the control // plane's ordinary replace. The caller holds the store's ownership claim. -func replayRevision(ctx context.Context, resources controlplane.ResourceStore, options StateStoreRecoveryOptions) (string, error) { +func replayRevision(ctx context.Context, resources controlplane.ResourceStore, options RecoveryOptions) (string, error) { server, err := openStateStoreControlPlane(resources) if err != nil { return "", err diff --git a/internal/app/archied/state_store_recovery_postgres.go b/internal/app/statestore/recovery_postgres.go similarity index 91% rename from internal/app/archied/state_store_recovery_postgres.go rename to internal/app/statestore/recovery_postgres.go index 6d97a330c..75e3b9522 100644 --- a/internal/app/archied/state_store_recovery_postgres.go +++ b/internal/app/statestore/recovery_postgres.go @@ -1,4 +1,4 @@ -package archied +package statestore import ( "context" @@ -14,7 +14,7 @@ import ( // database the configuration names. There is no automatic schema rollback: // goose Down drops tables, so the way back from a migration is restoring the // snapshot taken before it, which loses every write made after that snapshot. -func runPostgresRecovery(ctx context.Context, options StateStoreRecoveryOptions) (string, error) { +func runPostgresRecovery(ctx context.Context, options RecoveryOptions) (string, error) { cfg, err := bootConfig(ctx, options) if err != nil { return "", err @@ -74,7 +74,7 @@ func openRecoveryPool(ctx context.Context, url string) (*pgxpool.Pool, int64, er // the stored resources pass the writer's validation and boot's gate. An // unmigrated database holds nothing to check; the State Store migrates and // seeds it on its next start, so only boot's config gate applies. -func validatePostgres(ctx context.Context, url string, options StateStoreRecoveryOptions) (string, error) { +func validatePostgres(ctx context.Context, url string, options RecoveryOptions) (string, error) { pool, version, err := openRecoveryPool(ctx, url) if err != nil { return "", err @@ -94,7 +94,7 @@ func validatePostgres(ctx context.Context, url string, options StateStoreRecover // rollbackPostgres replays an earlier revision holding the State Store's // ownership claim, so it refuses while a State Store serves and none can // start until it finishes. -func rollbackPostgres(ctx context.Context, url string, options StateStoreRecoveryOptions) (string, error) { +func rollbackPostgres(ctx context.Context, url string, options RecoveryOptions) (string, error) { pool, _, err := openRecoveryPool(ctx, url) if err != nil { return "", err diff --git a/internal/app/archied/state_store.go b/internal/app/statestore/server.go similarity index 92% rename from internal/app/archied/state_store.go rename to internal/app/statestore/server.go index f7b5a93df..12d647602 100644 --- a/internal/app/archied/state_store.go +++ b/internal/app/statestore/server.go @@ -1,9 +1,9 @@ // state_store.go composes the standalone archie-state-store process. It mirrors // gateway.go / cmd/archie-gateway: the binary is a thin main that passes -// process inputs into RunStateStore, which serves the task and event-capture +// process inputs into Run, which serves the task and event-capture // stores from Postgres, registers every StateStore gRPC handler (the same service .4.2 serves // in-process on the daemon), and shuts down cleanly on ctx cancellation. -package archied +package statestore import ( "context" @@ -17,6 +17,7 @@ import ( "google.golang.org/grpc" "github.com/samcharles93/archie-core/internal/app/controlplane" + "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/health" @@ -29,26 +30,12 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/readiness" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" registry "github.com/samcharles93/archie-core/internal/infrastructure/storepkg" + "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" "github.com/samcharles93/archie-core/internal/secret" ) -func configuredIdentityNames(cfg config.Config) []string { - if len(cfg.Identities) == 0 { - name := cfg.BotUser - if name == "" { - name = "archie" - } - return []string{name} - } - names := make([]string, 0, len(cfg.Identities)) - for _, value := range cfg.Identities { - names = append(names, value.Name) - } - return names -} - -// StateStoreOptions contains process inputs for the standalone State Store. -type StateStoreOptions struct { +// Options contains process inputs for the standalone State Store. +type Options struct { Config string Overlay string // Listen is the gRPC listen address. Loopback binds (127.0.0.1, ::1, @@ -64,18 +51,18 @@ type StateStoreOptions struct { ReadyAddr string } -// RunStateStore serves the task and event-capture stores from Postgres, +// Run serves the task and event-capture stores from Postgres, // registers every StateStore gRPC handler, and serves until ctx is cancelled. // The conversation store belongs to the separate archie-gateway process. The // store service owns its own DB lifecycle, so // b.cleanup() is the sole owner closing b.st here (in-process owner). -func RunStateStore(ctx context.Context, options StateStoreOptions) error { //nolint:cyclop // the composition root's setup sequence is deliberately flat and sequential - b := newBootstrap() +func Run(ctx context.Context, options Options) error { //nolint:cyclop // the composition root's setup sequence is deliberately flat and sequential + b := newServer() defer b.cleanup() if err := b.loadConfig(ctx, options.Config, options.Overlay); err != nil { return err } - listen, err := resolveServiceListen("state", options.Listen, b.cfg.Services.Get(config.ServiceNameState).Listen) + listen, err := servicekit.ResolveListen("state", options.Listen, b.cfg.Services.Get(config.ServiceNameState).Listen) if err != nil { return err } @@ -89,7 +76,7 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error { //nol if !ok { return fmt.Errorf("state store does not support identity bootstrap") } - legacyNames := configuredIdentityNames(b.cfg) + legacyNames := servicekit.IdentityNames(b.cfg) if err := identityStore.BootstrapIdentities(ctx, legacyNames); err != nil { return fmt.Errorf("bootstrap identities: %w", err) } @@ -164,7 +151,7 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error { //nol // engines the consumers build; an invalid instance policy fails this boot -- // the store stops serving until it is fixed. A store without the access // surfaces degrades: there is no chain to seed. -func (b *boot) seedAndValidatePolicies(ctx context.Context) error { +func (b *server) seedAndValidatePolicies(ctx context.Context) error { policies, ok := b.st.(access.PolicyStore) if !ok { return nil @@ -214,10 +201,10 @@ func (b *boot) seedAndValidatePolicies(ctx context.Context) error { // with one it cannot run. The seed is retried on this process's next start, so // a corrected config.toml takes effect when archie-state-store restarts. // -// It is a method rather than a loop in RunStateStore because the boot sequence +// It is a method rather than a loop in Run because the boot sequence // is at its complexity budget: the branch belongs to the report's own policy, // not to the sequence that triggers it. -func (b *boot) reportUnseededResources(skipped []controlplane.SeedSkip) { +func (b *server) reportUnseededResources(skipped []controlplane.SeedSkip) { for _, skip := range skipped { b.log.Error("control-plane resource not seeded; the kind stays absent and the file's value is the one in effect", "kind", skip.Kind, "err", skip.Err) @@ -232,7 +219,7 @@ func (b *boot) reportUnseededResources(skipped []controlplane.SeedSkip) { // server admits is compilable there; a skewed deploy is only fixed by a // matching deploy. func openStateStoreControlPlane(resources controlplane.ResourceStore) (*controlplane.Server, error) { - steps, err := stepVocabulary() + steps, err := workflowsteps.NewManager() if err != nil { return nil, fmt.Errorf("register workflow step vocabulary: %w", err) } @@ -249,7 +236,7 @@ func openStateStoreControlPlane(resources controlplane.ResourceStore) (*controlp // from it. // It does NOT open gateway chat sessions -- those live in the separate // archie-gateway process on their own store and are out of state-store scope. -func (b *boot) openStateStore(ctx context.Context) error { +func (b *server) openStateStore(ctx context.Context) error { cfg, log := b.cfg, b.log // The State Store serves the installed packages extension engines come from, // so it resolves its own references through the env engine only. @@ -275,7 +262,7 @@ func (b *boot) openStateStore(ctx context.Context) error { // the capture/mapping/binding surfaces, so each is asserted here (the same // pattern the daemon's wireWebStoreSurfaces uses) and a store that lacks one // degrades that group rather than aborting boot. -func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps { +func (b *server) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps { deps := staterpc.Deps{Tasks: b.st, Log: b.log, Grants: grants} if b.pg != nil { packages := storepkg.Service{ @@ -363,7 +350,7 @@ func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps { // executionDeps wires the execution-tree surfaces (workflow calls, step // recording/reading, cancellation) from the same store, degrading each one // independently the store lacks it rather than failing the boot. -func (b *boot) executionDeps(deps *staterpc.Deps) { +func (b *server) executionDeps(deps *staterpc.Deps) { if wc, ok := b.st.(storecontract.WorkflowCaller); ok { deps.WorkflowCalls = wc } @@ -382,7 +369,7 @@ func (b *boot) executionDeps(deps *staterpc.Deps) { // store. A store without them -- one that owns // no tenant boundary yet -- degrades the access RPCs rather than failing the // boot, the same pattern the other optional surfaces use. -func (b *boot) accessDeps(deps *staterpc.Deps) { +func (b *server) accessDeps(deps *staterpc.Deps) { if ps, ok := b.st.(access.PrincipalSource); ok { deps.Principals = ps } @@ -418,7 +405,7 @@ func serveStateStore(ctx context.Context, listener net.Listener, deps staterpc.D // process is up), while GET /health/detailed runs the state_db readiness probe // and answers 503 when the store is degraded. It is the standalone process's // counterpart to the daemon's /healthz + /health/detailed (internal/webui). -func (b *boot) startStateStoreReadiness(ctx context.Context, readyAddr string) error { +func (b *server) startStateStoreReadiness(ctx context.Context, readyAddr string) error { registry := health.NewRegistry(readiness.NewStoreProbe(b.st)) mux := http.NewServeMux() mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) { @@ -443,11 +430,11 @@ func (b *boot) startStateStoreReadiness(ctx context.Context, readyAddr string) e // startOptionalSurfaces starts the HTTP surfaces this process serves beside // the gRPC contract, each enabled only by its own flag. // -// They are gathered here rather than branched inline because RunStateStore is +// They are gathered here rather than branched inline because Run is // at its complexity budget: a surface that is off by default belongs to its // own decision, not to the boot sequence that triggers it -- the same reason // reportUnseededResources is a method. -func (b *boot) startOptionalSurfaces(ctx context.Context, options StateStoreOptions) error { +func (b *server) startOptionalSurfaces(ctx context.Context, options Options) error { if options.ReadyAddr != "" { if err := b.startStateStoreReadiness(ctx, options.ReadyAddr); err != nil { return err @@ -501,7 +488,7 @@ func stateStoreServerOpts(listen, token string, grants *staterpc.TaskGrants) (op // dashboard edits with, so a definition the projector admits is compilable // where it dispatches (openStateStoreControlPlane for the pairing). func newPackageProjections(pool *pgxpool.Pool) (map[string]storepkg.FamilyProjector, error) { - steps, err := stepVocabulary() + steps, err := workflowsteps.NewManager() if err != nil { return nil, fmt.Errorf("register workflow step vocabulary: %w", err) } diff --git a/internal/app/archied/state_store_task.go b/internal/app/statestore/task.go similarity index 88% rename from internal/app/archied/state_store_task.go rename to internal/app/statestore/task.go index abc406920..21547c179 100644 --- a/internal/app/archied/state_store_task.go +++ b/internal/app/statestore/task.go @@ -1,4 +1,4 @@ -package archied +package statestore import ( "github.com/samcharles93/archie-core/internal/infrastructure/postgres" @@ -8,6 +8,6 @@ import ( // Serve ownership is the pool's claim (openStateStorePool), so a second State // Store on the same database already failed before this runs, and the pool's // cleanup closes what this store reads through. -func (b *boot) openTaskStore() { +func (b *server) openTaskStore() { b.st = postgres.New(b.pg) } diff --git a/internal/app/archied/state_store_toolcalls.go b/internal/app/statestore/toolcalls.go similarity index 99% rename from internal/app/archied/state_store_toolcalls.go rename to internal/app/statestore/toolcalls.go index 96d23049d..0ed34d520 100644 --- a/internal/app/archied/state_store_toolcalls.go +++ b/internal/app/statestore/toolcalls.go @@ -1,4 +1,4 @@ -package archied +package statestore import ( "context" diff --git a/internal/domain/access/roles.go b/internal/domain/access/roles.go index 654f80059..0fe658601 100644 --- a/internal/domain/access/roles.go +++ b/internal/domain/access/roles.go @@ -54,7 +54,7 @@ const orgOwnerPolicy = `permit(principal, action, resource) when { };` // ShippedOrgPolicies returns the four shipped role policies for one org, as -// the store seeds them and `archied access reset --org` restores them. +// the store seeds them and `archie-state-store access reset --org` restores them. func ShippedOrgPolicies(orgID org.OrgID) []Policy { return []Policy{ {ID: PolicyOrgRead, Level: LevelOrg, OrgID: orgID, Text: orgReadPolicy}, @@ -66,7 +66,7 @@ func ShippedOrgPolicies(orgID org.OrgID) []Policy { // CrossOrgForbidID is the ID the shipped cross-org forbid is known by in // denial records. The forbid is engine-enforced, never stored, so it cannot -// be edited away or removed by `archied access reset --instance`. +// be edited away or removed by `archie-state-store access reset --instance`. const CrossOrgForbidID = PolicyCrossOrgID // The cross-org forbid's semantics, carried as text for the policy surface diff --git a/internal/infrastructure/postgres/access.go b/internal/infrastructure/postgres/access.go index c6869ac18..62ac6aa86 100644 --- a/internal/infrastructure/postgres/access.go +++ b/internal/infrastructure/postgres/access.go @@ -169,7 +169,7 @@ func (s *Store) EnsureShippedOrgPolicies(ctx context.Context, orgID org.OrgID) e // ResetOrgPolicies restores the shipped role policies for one org and // removes its other org-level policies. It is the store-side half of -// `archied access reset --org`, which runs on this host and records the +// `archie-state-store access reset --org`, which runs on this host and records the // reset as an audit event. func (s *Store) ResetOrgPolicies(ctx context.Context, orgID org.OrgID) error { if orgID == "" { diff --git a/internal/infrastructure/staterpc/access_client.go b/internal/infrastructure/staterpc/access_client.go index 3293bc7dd..e55788997 100644 --- a/internal/infrastructure/staterpc/access_client.go +++ b/internal/infrastructure/staterpc/access_client.go @@ -1,6 +1,6 @@ // Access: the client half of the policy-chain and denial-record RPCs. // The reset surface is deliberately absent: -// `archied access reset` runs on the State Store host, never over the wire. +// `archie-state-store access reset` runs on the State Store host, never over the wire. package staterpc import ( diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index 121bfdb2b..4f723a8be 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -118,7 +118,7 @@ type Deps struct { Principals access.PrincipalSource // Policies is the stored policy chain. // Optional: nil answers the access RPCs with codes.Unavailable. The reset - // surface is deliberately not an RPC: `archied access reset` runs on the + // surface is deliberately not an RPC: `archie-state-store access reset` runs on the // State Store host, never over the network. Policies access.PolicyStore // Denials records and lists denial records. Optional: nil disables the