From b8b223c87c61cf1c93ceeba88fcd6c461c43e72c Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 19 Oct 2026 10:12:48 +1100 Subject: [PATCH] feat(configuration): name the instance with ARCHIE_INSTANCE and scope default paths by it --- internal/app/archied/drain.go | 7 +-- internal/app/archied/forge_extensions.go | 6 +-- internal/app/archied/update.go | 7 +-- .../app/archiemessaging/extension_channels.go | 6 +-- internal/config/config.go | 3 ++ .../infrastructure/configuration/defaults.go | 12 ++--- .../infrastructure/configuration/instance.go | 51 +++++++++++++++++++ .../infrastructure/configuration/loader.go | 4 ++ .../infrastructure/secretengine/runner.go | 5 +- 9 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 internal/infrastructure/configuration/instance.go diff --git a/internal/app/archied/drain.go b/internal/app/archied/drain.go index e3d4c92a7..a0957d695 100644 --- a/internal/app/archied/drain.go +++ b/internal/app/archied/drain.go @@ -18,12 +18,7 @@ import ( const defaultDrainPollInterval = 5 * time.Second // DefaultDrainRequestPath returns the marker file path the daemon watches. It -// lives under the config home (${XDG_CONFIG_HOME:-~/.config}/archie) -- the -// established convention for archied's per-user paths -- rather than the -// epic's literal ~/.archie, which is not a path this daemon ever uses. The -// discrepancy is intentional: every other archied path reads from the XDG -// config home, so the drain marker must too, or an operator pointing at the -// documented location would miss it. +// lives in the instance's config directory. func DefaultDrainRequestPath() string { return filepath.Join(configuration.DefaultConfigDir(), drainio.DefaultMarkerFilename) } diff --git a/internal/app/archied/forge_extensions.go b/internal/app/archied/forge_extensions.go index f23391139..e3d8124e4 100644 --- a/internal/app/archied/forge_extensions.go +++ b/internal/app/archied/forge_extensions.go @@ -3,7 +3,6 @@ package archied import ( "context" "log/slog" - "os" "path/filepath" "slices" @@ -11,6 +10,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/storepkg" "github.com/samcharles93/archie-core/internal/forge" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/extension" "github.com/samcharles93/archie-core/internal/infrastructure/forgeext" ) @@ -27,7 +27,7 @@ type forgeExtensions struct { } func newForgeExtensions(source extension.Source, log *slog.Logger) *forgeExtensions { - cache, err := os.UserCacheDir() + cache, err := configuration.DefaultCacheDir() if err != nil { log.Warn("forge extensions unavailable: no cache directory", "err", err) return nil @@ -35,7 +35,7 @@ func newForgeExtensions(source extension.Source, log *slog.Logger) *forgeExtensi return &forgeExtensions{ host: extension.NewHost(hclog.New(&hclog.LoggerOptions{Name: "extension", Level: hclog.Warn})), source: source, - cacheDir: filepath.Join(cache, "archie", "extensions", "forge"), + cacheDir: filepath.Join(cache, "extensions", "forge"), log: log, } } diff --git a/internal/app/archied/update.go b/internal/app/archied/update.go index 1a66120b8..a9d017b7b 100644 --- a/internal/app/archied/update.go +++ b/internal/app/archied/update.go @@ -254,12 +254,7 @@ func updateStateDir() string { if dir := os.Getenv("ARCHIE_UPDATE_STATE_DIR"); dir != "" { return dir } - dir := os.Getenv("XDG_STATE_HOME") - if dir == "" { - home, _ := os.UserHomeDir() - dir = filepath.Join(home, ".local", "state") - } - return filepath.Join(dir, "archie", "update") + return configuration.DefaultUpdateStateDir() } // resumeUpdate hands an interrupted transaction to the frozen watchdog and diff --git a/internal/app/archiemessaging/extension_channels.go b/internal/app/archiemessaging/extension_channels.go index 15e0a91ae..66c88fef9 100644 --- a/internal/app/archiemessaging/extension_channels.go +++ b/internal/app/archiemessaging/extension_channels.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "os" "path/filepath" "slices" "strings" @@ -14,6 +13,7 @@ import ( "github.com/samcharles93/archie-core/internal/channels" "github.com/samcharles93/archie-core/internal/domain/storepkg" "github.com/samcharles93/archie-core/internal/infrastructure/channelext" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/extension" "github.com/samcharles93/archie-core/internal/secret" ) @@ -26,7 +26,7 @@ const refSuffix = "_ref" // the set takes a restart, as every channel binding does. Running extensions // stop with ctx. func openExtensionChannels(ctx context.Context, source extension.Source, secrets *secret.Registry, log *slog.Logger) []*channelInstance { - cache, err := os.UserCacheDir() + cache, err := configuration.DefaultCacheDir() if err != nil { log.Warn("channel extensions unavailable: no cache directory", "err", err) return nil @@ -52,7 +52,7 @@ func openExtensionChannels(ctx context.Context, source extension.Source, secrets if index < 0 { continue } - ch, err := buildExtensionChannel(ctx, host, source, filepath.Join(cache, "archie", "extensions", "channels"), pkg, pkg.Descriptor.Contributes.Extensions[index].Path, setting.Settings, secrets) + ch, err := buildExtensionChannel(ctx, host, source, filepath.Join(cache, "extensions", "channels"), pkg, pkg.Descriptor.Contributes.Extensions[index].Path, setting.Settings, secrets) if err != nil { log.Warn("channel extension not started", "channel", pkg.Name, "err", err) continue diff --git a/internal/config/config.go b/internal/config/config.go index f48bfbc4d..b3590f99f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -352,6 +352,9 @@ type ToolsConfig struct { // Config is the daemon configuration. type Config struct { + // Instance names this deployment among others on the same host. It comes + // from ARCHIE_INSTANCE, never the file, because it selects the file. + Instance string `toml:"-" yaml:"-"` Services Services `toml:"services" yaml:"services"` // TLS is the certificate every gRPC hop uses. Restart-required. TLS TLS `toml:"tls" yaml:"tls"` diff --git a/internal/infrastructure/configuration/defaults.go b/internal/infrastructure/configuration/defaults.go index 9d42d6da4..26f13cff3 100644 --- a/internal/infrastructure/configuration/defaults.go +++ b/internal/infrastructure/configuration/defaults.go @@ -169,21 +169,21 @@ func applyWebFetchDefaults(cfg *config.Config) { // DefaultWorkDir returns the default work directory. func DefaultWorkDir() string { - return filepath.Join(xdgDataHome(), "archie", "work") + return filepath.Join(xdgDataHome(), InstanceName(), "work") } // DefaultPostgresSocketDir is where the bundled PostgreSQL's Unix socket is // mounted on the host. func DefaultPostgresSocketDir() string { - return filepath.Join(xdgDataHome(), "archie", "postgres") + return filepath.Join(xdgDataHome(), InstanceName(), "postgres") } // DefaultConfigDir returns the directory archie's configuration lives in: -// $XDG_CONFIG_HOME/archie, or ~/.config/archie. It is the base every +// $XDG_CONFIG_HOME/, or ~/.config/. It is the base every // config-home-derived path is built on. It is not the -config source // directory, which is whichever file or directory the operator names. func DefaultConfigDir() string { - return filepath.Join(xdgConfigHome(), "archie") + return filepath.Join(xdgConfigHome(), InstanceName()) } // DefaultConfigPath returns the config file archie binaries read when @@ -201,10 +201,10 @@ func (l *Loader) applyGeneralDefaults(cfg *config.Config) { cfg.Chat.Workspace = expandHomePath(cfg.Chat.Workspace) // DatabaseURL has no default. if cfg.WorkDir == "" { - cfg.WorkDir = filepath.Join(l.dataHome(), "archie", "work") + cfg.WorkDir = filepath.Join(l.dataHome(), cfg.Instance, "work") } if cfg.StateDir == "" { - cfg.StateDir = filepath.Join(l.dataHome(), "archie") + cfg.StateDir = filepath.Join(l.dataHome(), cfg.Instance) } if cfg.PollInterval == 0 { cfg.PollInterval = config.Duration(defaultPollInterval) diff --git a/internal/infrastructure/configuration/instance.go b/internal/infrastructure/configuration/instance.go new file mode 100644 index 000000000..5c92aac33 --- /dev/null +++ b/internal/infrastructure/configuration/instance.go @@ -0,0 +1,51 @@ +package configuration + +import ( + "fmt" + "os" + "path/filepath" + "regexp" +) + +// DefaultInstance is the instance name when ARCHIE_INSTANCE is unset. Every +// host-global name derived from it is unchanged from a single-instance host. +const DefaultInstance = "archie" + +var instancePattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,30}$`) + +// InstanceName returns the instance this process belongs to, from +// ARCHIE_INSTANCE. It names the config, data, state and cache directories, so +// it cannot live in the config file it selects. Loading rejects an invalid +// name, so callers past startup can use it as a path segment. +func InstanceName() string { + if name := os.Getenv("ARCHIE_INSTANCE"); name != "" { + return name + } + return DefaultInstance +} + +func validateInstanceName(name string) error { + if !instancePattern.MatchString(name) { + return fmt.Errorf("ARCHIE_INSTANCE %q: must match %s", name, instancePattern) + } + return nil +} + +// DefaultCacheDir returns the instance's user cache directory. +func DefaultCacheDir() (string, error) { + cache, err := os.UserCacheDir() + if err != nil { + return "", err + } + return filepath.Join(cache, InstanceName()), nil +} + +// DefaultUpdateStateDir returns $XDG_STATE_HOME//update. +func DefaultUpdateStateDir() string { + dir := os.Getenv("XDG_STATE_HOME") + if dir == "" { + home, _ := os.UserHomeDir() + dir = filepath.Join(home, ".local", "state") + } + return filepath.Join(dir, InstanceName(), "update") +} diff --git a/internal/infrastructure/configuration/loader.go b/internal/infrastructure/configuration/loader.go index 64afabd54..4f7f0ff0b 100644 --- a/internal/infrastructure/configuration/loader.go +++ b/internal/infrastructure/configuration/loader.go @@ -359,6 +359,10 @@ func (l *Loader) decodeMain(doc *Document, path string, isYAMLFile bool, layer L // finalize applies defaults, then validate. func (l *Loader) finalize(doc *Document, validate func(*config.Config) error) (*Document, error) { doc.UnknownKeys = sortedUnique(append(doc.UnknownKeys, unregisteredServiceKeys(doc.Config.Services)...)) + doc.Config.Instance = InstanceName() + if err := validateInstanceName(doc.Config.Instance); err != nil { + return nil, err + } l.applyDefaults(&doc.Config) if err := validate(&doc.Config); err != nil { return nil, err diff --git a/internal/infrastructure/secretengine/runner.go b/internal/infrastructure/secretengine/runner.go index 2a2b7ba16..4544ba836 100644 --- a/internal/infrastructure/secretengine/runner.go +++ b/internal/infrastructure/secretengine/runner.go @@ -17,6 +17,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/applystatus" "github.com/samcharles93/archie-core/internal/domain/storepkg" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" "github.com/samcharles93/archie-core/internal/infrastructure/extension" "github.com/samcharles93/archie-core/internal/secret" @@ -195,13 +196,13 @@ func (r *Runner) Close() { r.host.Close() } // is retried each tick; it never stops the caller. The returned Runner's Close // stops the engines. func Supervise(ctx context.Context, registry *secret.Registry, source Source, process string, report func(ctx context.Context, version int64, err error), log *slog.Logger) *Runner { - cache, err := os.UserCacheDir() + cache, err := configuration.DefaultCacheDir() if err != nil { log.Warn("secret engines unavailable: no cache directory", "err", err) return &Runner{host: extension.NewHost(hclog.NewNullLogger())} } host := extension.NewHost(hclog.New(&hclog.LoggerOptions{Name: "extension", Level: hclog.Warn})) - runner := NewRunner(host, registry, source, filepath.Join(cache, "archie", "extensions", process), log) + runner := NewRunner(host, registry, source, filepath.Join(cache, "extensions", process), log) runner.ReportTo(report) if err := runner.Sync(ctx); err != nil { log.Warn("secret engines", "err", err)