Files
archie/internal/releaseupdate/installer.go
T
sam 9d8f364af1
Quality / check (push) Successful in 5m53s
deploy / gate (push) Canceled after 0s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
feat(releaseupdate): signed OCI distribution sources for releases and the catalogue (#1715)
2026-10-31 18:18:43 +11:00

98 lines
2.9 KiB
Go

package releaseupdate
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/samcharles93/archie-core/internal/domain/distribution"
)
// 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. Source is the
// distribution source the release was found in, so it installs from the same.
type ArchiedInstaller struct {
Source func(context.Context) (distribution.Source, error)
}
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")
}
source, err := i.Source(ctx)
if err != nil {
return Result{}, err
}
cmd := exec.CommandContext(ctx, path, "update", "install",
"-version", available[0].Available,
"-source", source.Reference,
"-key", distribution.EncodeKey(source.Key),
"-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
}