feat(configuration): name the instance with ARCHIE_INSTANCE and scope default paths by it
This commit is contained in:
9 files changed
+75
-26
No files matched your search
@@ -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