feat(configuration): name the instance with ARCHIE_INSTANCE and scope default paths by it
This commit is contained in:
1 parent
9d3ea208ef
commit
b8b223c87c
9 files changed
+75
-26
No files matched your search
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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/<instance>, or ~/.config/<instance>. 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)
|
||||
|
||||
@@ -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/<instance>/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")
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user