Files
archie/internal/app/archiemessaging/config.go
T
sam 417d379ede feat(releaseupdate): transactional self-update through archied (#1633)
archied update install verifies the release zip, checks every binary
reports the release, pulls the agent image by digest, backs up the
database in-process and stages the binaries before handing off to
archied update watchdog in its own process. The watchdog restarts units
in release order, requires archied healthy for 120s and otherwise rolls
back binaries and image policy, restoring the database only when a
migration ran. Boot recovery resumes the frozen watchdog.

Deletes scripts/archie-update-install, scripts/archie-update-watchdog,
the updater's --from-source and --auto, CommandInstaller,
ScriptInstaller and the update-image and update-topology subcommands.
2026-10-12 03:21:02 +11:00

147 lines
4.0 KiB
Go

package archiemessaging
import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/secret"
)
// ResolvedConfig contains the resolved inputs needed by the Messaging Service.
type ResolvedConfig struct {
Options Options
TelegramToken string
Telegram config.TelegramConfig
// WorkDir and BotUser are not channel transport settings. They locate
// this identity's release-announcement and update-report state files.
WorkDir string
BotUser string
ShowToolCalls bool
}
type projection struct {
gateway ServiceTarget
stateStore ServiceTarget
messaging config.ServiceConnection
telegram config.TelegramConfig
workDir string
botUser string
showToolCalls bool
}
// Resolve loads the configuration, extracts the messaging projection and
// applies defaults. Credentials are not resolved here: they may name an
// extension engine that starts only after the State Store is dialed, so
// resolveTokens runs afterwards.
func Resolve(o Options, log *slog.Logger) (ResolvedConfig, error) {
var proj projection
if o.Config != "" {
p, found, err := readProjection(o.Config, o.Overlay, log)
if err != nil {
return ResolvedConfig{}, err
}
if found {
proj = p
o = merge(o, proj)
}
}
o = withDefaults(o)
o = withEnvTokens(o)
if err := o.validate(); err != nil {
return ResolvedConfig{}, err
}
return ResolvedConfig{
Options: o,
Telegram: proj.telegram,
WorkDir: proj.workDir,
BotUser: proj.botUser,
ShowToolCalls: proj.showToolCalls,
}, nil
}
func readProjection(path, overlay string, log *slog.Logger) (projection, bool, error) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) && overlay == "" {
return projection{}, false, nil
}
doc, err := configuration.New(log).Resolve(path, overlay)
if err != nil {
return projection{}, false, fmt.Errorf("read messaging configuration: %w", err)
}
return project(doc.Config), true, nil
}
func project(cfg config.Config) projection {
return projection{
gateway: ServiceTarget{
Target: cfg.Services.Get(config.ServiceNameGateway).Target,
Token: cfg.Services.Get(config.ServiceNameGateway).TargetToken,
},
stateStore: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameState).Target, Token: cfg.Services.Get(config.ServiceNameState).TargetToken},
messaging: cfg.Services.Get(config.ServiceNameMessaging),
telegram: cfg.Chat.Telegram,
workDir: cfg.WorkDir,
botUser: cfg.BotUser,
showToolCalls: cfg.Chat.ShowToolCalls,
}
}
func withEnvTokens(o Options) Options {
if o.Gateway.Token == "" {
o.Gateway.Token = os.Getenv("GATEWAY_TOKEN")
}
if o.StateStore.Token == "" {
o.StateStore.Token = os.Getenv("STATE_STORE_TOKEN")
}
if o.Token == "" {
o.Token = os.Getenv("MESSAGING_TOKEN")
}
return o
}
func merge(o Options, p projection) Options {
if o.Gateway.Target == "" {
o.Gateway.Target = p.gateway.Target
}
if o.Gateway.Token == "" {
o.Gateway.Token = p.gateway.Token
}
if o.StateStore.Target == "" {
o.StateStore.Target = p.stateStore.Target
}
if o.StateStore.Token == "" {
o.StateStore.Token = p.stateStore.Token
}
if o.Listen == "" {
o.Listen = p.messaging.Listen
}
if o.Token == "" {
o.Token = p.messaging.TargetToken
}
return o
}
// resolveTelegramToken resolves the configured bot token; an unset
// reference means the channel is off.
func resolveTelegramToken(cfg config.TelegramConfig, secrets *secret.Registry) (string, error) {
if cfg.Token == (secret.SecretRef{}) {
return "", nil
}
return secrets.Resolve(cfg.Token)
}
// resolveTokens fills the channel credentials from the file configuration.
func (c *ResolvedConfig) resolveTokens(secrets *secret.Registry) error {
token, err := resolveTelegramToken(c.Telegram, secrets)
if err != nil {
return fmt.Errorf("resolve telegram token: %w", err)
}
c.TelegramToken = token
return nil
}