Files
archie/internal/releaseupdate/installer.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

87 lines
2.6 KiB
Go

package releaseupdate
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// ResultSentinel prefixes the one line `archied update install` prints to
// report its Result. Every other line is progress.
const ResultSentinel = "ARCHIE_UPDATE_RESULT "
// ErrNotConfigured is returned when no archied sits beside this binary to
// install with.
var ErrNotConfigured = errors.New("updates are not configured")
// ArchiedInstaller runs `archied update install` from beside this binary, so
// every service installs through the daemon's one transaction.
type ArchiedInstaller struct{}
func (ArchiedInstaller) path() string {
path := filepath.Join(executableDir(), WatchdogBinary)
if info, err := os.Stat(path); err != nil || info.Mode()&0o111 == 0 {
return ""
}
return path
}
// Installable reports whether archied is present to install with.
func (i ArchiedInstaller) Installable(context.Context) bool { return i.path() != "" }
// Install installs the snapshot's release, streaming progress lines.
func (i ArchiedInstaller) Install(ctx context.Context, snapshot Snapshot, meta InstallMeta, progress func(string)) (Result, error) {
path := i.path()
if path == "" {
return Result{}, ErrNotConfigured
}
available := snapshot.Available()
if len(available) == 0 {
return Result{}, errors.New("nothing to install")
}
cmd := exec.CommandContext(ctx, path, "update", "install",
"-version", available[0].Available,
"-report", meta.ReportPath,
"-channel", meta.Channel,
"-chat", strconv.FormatInt(meta.ChatID, 10),
"-thread", strconv.Itoa(meta.ThreadID))
stdout, err := cmd.StdoutPipe()
if err != nil {
return Result{}, fmt.Errorf("open update installer stdout: %w", err)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
return Result{}, fmt.Errorf("start update installer: %w", err)
}
var result Result
var resultErr error
scanner := bufio.NewScanner(stdout)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for scanner.Scan() {
line := scanner.Text()
if rest, ok := strings.CutPrefix(line, ResultSentinel); ok {
if err := json.Unmarshal([]byte(rest), &result); err != nil {
resultErr = fmt.Errorf("decode update installer result: %w", err)
}
continue
}
progress(line)
}
if err := scanner.Err(); err != nil {
resultErr = fmt.Errorf("read update installer output: %w", err)
}
if err := cmd.Wait(); err != nil {
return result, fmt.Errorf("run update installer: %w: %s", err, strings.TrimSpace(stderr.String()))
}
return result, resultErr
}