/update could report success at the correct target version while the archie-agent container running it never actually changed: the update mechanism trusted the configured installer's exit code and reported whatever version number the release claimed, with nothing verifying the two agreed. The first piece of the fix: a new internal/installtype package reports how a binary was built for distribution (Type(), defaulting to "unknown"), stamped once per release artifact via "-ldflags -X .../installtype.buildType=<value>" rather than guessed at runtime by probing the environment -- restic and Tailscale both treat "which install shape is this" as a build-time decision for the same reason: a probed signal is guessable, a compiled-in one is not. releaseupdate.Service.Install now refuses to run the configured Installer at all when InstallType is unset or installtype.Unknown, so an unstamped or misconfigured build can no longer silently attempt an update it has no way to know is even the right kind for how this instance was deployed. Wired through every real archied build path so the gate doesn't just exist in principle: Taskfile.yml's build task (binary), Dockerfile.archied (container), and install.sh (binary) all now stamp it; cmd/archied/telegram_setup.go wires Service.InstallType from installtype.Type(). Configuration-page display of install type/version and post-update verification against the actually-running reference (not just the installer's reported version) remain open -- this lands the correctness gate, not the full observability surface. Refs #522
218 lines
6.5 KiB
Go
218 lines
6.5 KiB
Go
// Package releaseupdate provides deployment-neutral release discovery,
|
|
// per-user deferrals, and installation orchestration.
|
|
package releaseupdate
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"sync"
|
|
|
|
"github.com/samcharles93/archie-core/internal/installtype"
|
|
)
|
|
|
|
// Catalog discovers the currently installed and newest available releases.
|
|
// Deployment-specific adapters (a package manager, image registry, or custom
|
|
// release feed) implement it outside the chat gateway.
|
|
type Catalog interface {
|
|
Check(context.Context) (Snapshot, error)
|
|
}
|
|
|
|
// Installer applies an approved update. It reports only work it has actually
|
|
// started; callers remain responsible for telling users the final result.
|
|
type Installer interface {
|
|
Install(context.Context, func(string)) error
|
|
}
|
|
|
|
type Component struct {
|
|
ID string
|
|
Label string
|
|
Installed string
|
|
Available string
|
|
Changelog string
|
|
}
|
|
|
|
type Snapshot struct {
|
|
Components []Component
|
|
Deferred bool
|
|
}
|
|
|
|
func (s Snapshot) Available() []Component {
|
|
available := make([]Component, 0, len(s.Components))
|
|
for _, component := range s.Components {
|
|
if component.Available != "" && component.Available != component.Installed {
|
|
available = append(available, component)
|
|
}
|
|
}
|
|
return available
|
|
}
|
|
|
|
// SameAvailable reports whether two snapshots offer the same component
|
|
// versions. Metadata such as changelogs may change without changing the
|
|
// release the operator is approving, so only installable versions matter.
|
|
func SameAvailable(left, right Snapshot) bool {
|
|
leftAvailable, rightAvailable := left.Available(), right.Available()
|
|
if len(leftAvailable) != len(rightAvailable) {
|
|
return false
|
|
}
|
|
versions := make(map[string]string, len(leftAvailable))
|
|
for _, component := range leftAvailable {
|
|
versions[component.ID] = component.Available
|
|
}
|
|
for _, component := range rightAvailable {
|
|
if versions[component.ID] != component.Available {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Service persists a user's decision to defer an exact available version. A
|
|
// later version is deliberately shown again rather than being silently hidden.
|
|
type Service struct {
|
|
Catalog Catalog
|
|
Installer Installer
|
|
StatePath string
|
|
|
|
// InstallType gates Install: it must be a value the release pipeline
|
|
// actually stamps (see package installtype), never "" or
|
|
// installtype.Unknown, or Install refuses to run the configured
|
|
// Installer at all. The composition root wires this from
|
|
// installtype.Type() -- Service itself never reads that package
|
|
// directly, so a test can exercise every InstallType without touching
|
|
// process-wide state.
|
|
InstallType string
|
|
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (s *Service) Check(ctx context.Context, recipient int64) (Snapshot, error) {
|
|
if s == nil || s.Catalog == nil {
|
|
return Snapshot{}, errors.New("update discovery is not configured")
|
|
}
|
|
snapshot, err := s.Catalog.Check(ctx)
|
|
if err != nil {
|
|
return Snapshot{}, fmt.Errorf("check for updates: %w", err)
|
|
}
|
|
if s.StatePath == "" {
|
|
return snapshot, nil
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
state, err := loadDeferrals(s.StatePath)
|
|
if err != nil {
|
|
return Snapshot{}, err
|
|
}
|
|
deferred := state[strconv.FormatInt(recipient, 10)]
|
|
for index := range snapshot.Components {
|
|
component := &snapshot.Components[index]
|
|
if deferred[component.ID] == component.Available {
|
|
component.Available = ""
|
|
snapshot.Deferred = true
|
|
}
|
|
}
|
|
return snapshot, nil
|
|
}
|
|
|
|
// Defer records only the exact versions the caller displayed. It refuses a
|
|
// changed catalog rather than silently suppressing a release the user never saw.
|
|
func (s *Service) Defer(ctx context.Context, recipient int64, expected Snapshot) error {
|
|
if s == nil || s.Catalog == nil {
|
|
return errors.New("update discovery is not configured")
|
|
}
|
|
if s.StatePath == "" {
|
|
return errors.New("update deferral storage is not configured")
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
snapshot, err := s.Catalog.Check(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("check updates before deferring: %w", err)
|
|
}
|
|
state, err := loadDeferrals(s.StatePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
key := strconv.FormatInt(recipient, 10)
|
|
if state[key] == nil {
|
|
state[key] = make(map[string]string)
|
|
}
|
|
if !SameAvailable(snapshot, expected) {
|
|
return errors.New("available releases changed; check again")
|
|
}
|
|
for _, component := range expected.Available() {
|
|
state[key][component.ID] = component.Available
|
|
}
|
|
return saveDeferrals(s.StatePath, state)
|
|
}
|
|
|
|
// ErrUnknownInstallType is returned by Install when InstallType is unset or
|
|
// installtype.Unknown -- there is no way to know, without it, whether the
|
|
// configured Installer is even the right kind of update for how this
|
|
// instance was actually deployed.
|
|
var ErrUnknownInstallType = errors.New("refusing to install: install type is unknown")
|
|
|
|
func (s *Service) Install(ctx context.Context, progress func(string)) error {
|
|
if s == nil || s.Installer == nil {
|
|
return errors.New("update installation is not configured")
|
|
}
|
|
if s.InstallType == "" || s.InstallType == installtype.Unknown {
|
|
return ErrUnknownInstallType
|
|
}
|
|
return s.Installer.Install(ctx, progress)
|
|
}
|
|
|
|
func (s *Service) CanInstall() bool { return s != nil && s.Installer != nil }
|
|
|
|
type deferrals map[string]map[string]string
|
|
|
|
func loadDeferrals(path string) (deferrals, error) {
|
|
data, err := os.ReadFile(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return make(deferrals), nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read update deferrals: %w", err)
|
|
}
|
|
var state deferrals
|
|
if err := json.Unmarshal(data, &state); err != nil {
|
|
return nil, fmt.Errorf("decode update deferrals: %w", err)
|
|
}
|
|
return state, nil
|
|
}
|
|
|
|
func saveDeferrals(path string, state deferrals) error {
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
return fmt.Errorf("create update state directory: %w", err)
|
|
}
|
|
file, err := os.CreateTemp(filepath.Dir(path), ".update-deferrals-*")
|
|
if err != nil {
|
|
return fmt.Errorf("create update state: %w", err)
|
|
}
|
|
tempPath := file.Name()
|
|
defer func() { _ = os.Remove(tempPath) }()
|
|
if err := file.Chmod(0o600); err != nil {
|
|
_ = file.Close()
|
|
return fmt.Errorf("protect update state: %w", err)
|
|
}
|
|
if err := json.NewEncoder(file).Encode(state); err != nil {
|
|
_ = file.Close()
|
|
return fmt.Errorf("encode update deferrals: %w", err)
|
|
}
|
|
if err := file.Sync(); err != nil {
|
|
_ = file.Close()
|
|
return fmt.Errorf("sync update deferrals: %w", err)
|
|
}
|
|
if err := file.Close(); err != nil {
|
|
return fmt.Errorf("close update state: %w", err)
|
|
}
|
|
if err := os.Rename(tempPath, path); err != nil {
|
|
return fmt.Errorf("replace update state: %w", err)
|
|
}
|
|
return nil
|
|
}
|