372 lines
12 KiB
Go
372 lines
12 KiB
Go
package releaseupdate
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/buildinfo"
|
|
)
|
|
|
|
// ReleaseURL is where published release assets are downloaded from.
|
|
const ReleaseURL = "https://github.com/" + ReleaseSlug + "/releases/download"
|
|
|
|
var (
|
|
releaseVersion = regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+([+-][0-9A-Za-z.-]+)?$`)
|
|
releaseUnit = regexp.MustCompile(`^archie[a-z0-9-]*$`)
|
|
pinnedImage = regexp.MustCompile(`^[a-zA-Z0-9./:_-]+@sha256:[a-f0-9]{64}$`)
|
|
)
|
|
|
|
// PrepareRequest names the release to stage and who hears the outcome.
|
|
type PrepareRequest struct {
|
|
Version string
|
|
// ReleaseURL is the base the release's zip and SHA256SUMS are under.
|
|
ReleaseURL string
|
|
Meta InstallMeta
|
|
// CheckTopology refuses a host missing a unit or config section the
|
|
// release requires. managed reports whether units are observable.
|
|
CheckTopology func(ctx context.Context, required Topology, managed bool) error
|
|
}
|
|
|
|
// Manifest is the release.json a release zip carries.
|
|
type Manifest struct {
|
|
Required Topology `json:"required_topology"`
|
|
AgentImage string `json:"agent_image"`
|
|
}
|
|
|
|
// Prepare verifies and stages a release without changing anything that runs:
|
|
// the zip is checked against SHA256SUMS, every binary must report the release,
|
|
// the agent image is pulled by digest and the database is backed up before a
|
|
// journal is published. The watchdog (Run) takes it from there.
|
|
func (t *Transaction) Prepare(ctx context.Context, req PrepareRequest) (Result, error) {
|
|
if !releaseVersion.MatchString(req.Version) {
|
|
return Result{}, fmt.Errorf("refusing non-release version %q", req.Version)
|
|
}
|
|
unlock, held, err := lockState(ctx, t.StateDir, false)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if !held {
|
|
return Result{}, ErrInstallInProgress
|
|
}
|
|
defer unlock()
|
|
if _, err := os.Stat(JournalPath(t.StateDir)); err == nil {
|
|
return Result{}, fmt.Errorf("an unfinished update is pending recovery at %s", JournalPath(t.StateDir))
|
|
}
|
|
work, err := os.MkdirTemp(t.StateDir, ".release-*")
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
defer func() { _ = os.RemoveAll(work) }()
|
|
|
|
release, manifest, err := t.fetch(ctx, req, work)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
managed := t.Units.Managed(ctx)
|
|
if req.CheckTopology != nil {
|
|
if err := req.CheckTopology(ctx, manifest.Required, managed); err != nil {
|
|
return Result{}, err
|
|
}
|
|
}
|
|
j, err := t.snapshotHost(ctx, req, manifest, managed)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := t.stage(req, release, j); err != nil {
|
|
for _, app := range append(slices.Clone(j.Units), j.Binaries...) {
|
|
_ = os.Remove(t.staged(app))
|
|
}
|
|
return Result{}, err
|
|
}
|
|
installed := map[string]string{ComponentDaemon: req.Version, ComponentAgent: req.Version}
|
|
return Result{
|
|
Previous: j.Previous,
|
|
Installed: installed,
|
|
Components: []ComponentResult{
|
|
{ID: ComponentDaemon, Label: "Gateway", Status: "updated"},
|
|
{ID: ComponentAgent, Label: "Runtime", Status: "updated"},
|
|
},
|
|
RestartRequested: managed,
|
|
}, nil
|
|
}
|
|
|
|
// fetch downloads, verifies and unpacks the release, and checks every binary
|
|
// reports the version being installed.
|
|
func (t *Transaction) fetch(ctx context.Context, req PrepareRequest, work string) (string, Manifest, error) {
|
|
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
|
|
return "", Manifest{}, fmt.Errorf("no published release for %s/%s", runtime.GOOS, runtime.GOARCH)
|
|
}
|
|
name := fmt.Sprintf("archie-core-archied-v%s-linux-amd64", req.Version)
|
|
base := strings.TrimSuffix(req.ReleaseURL, "/") + "/v" + req.Version + "/"
|
|
t.say("downloading %s", name+".zip")
|
|
archive := filepath.Join(work, name+".zip")
|
|
if err := downloadVerified(ctx, base, name+".zip", archive); err != nil {
|
|
return "", Manifest{}, err
|
|
}
|
|
if err := unpack(archive, name, work); err != nil {
|
|
return "", Manifest{}, err
|
|
}
|
|
release := filepath.Join(work, name)
|
|
manifest, err := readManifest(filepath.Join(release, "release.json"))
|
|
if err != nil {
|
|
return "", Manifest{}, err
|
|
}
|
|
for _, binary := range manifest.Required.apps() {
|
|
reported, err := reportedVersion(ctx, filepath.Join(release, binary), binary)
|
|
if err != nil {
|
|
return "", Manifest{}, err
|
|
}
|
|
if !sameVersion(reported, req.Version) {
|
|
return "", Manifest{}, fmt.Errorf("candidate %s reports %q, not %s", binary, reported, req.Version)
|
|
}
|
|
}
|
|
return release, manifest, nil
|
|
}
|
|
|
|
// snapshotHost pulls the image and records what rollback needs: the current
|
|
// versions, the current image policy, the schema version and a backup.
|
|
func (t *Transaction) snapshotHost(ctx context.Context, req PrepareRequest, manifest Manifest, managed bool) (journal, error) {
|
|
t.say("pulling %s", manifest.AgentImage)
|
|
if err := t.Images.Pull(ctx, manifest.AgentImage); err != nil {
|
|
return journal{}, fmt.Errorf("pull agent image: %w", err)
|
|
}
|
|
image, err := t.Policy.Image(ctx)
|
|
if err != nil {
|
|
return journal{}, fmt.Errorf("read image policy: %w", err)
|
|
}
|
|
schema, err := t.Database.SchemaVersion(ctx)
|
|
if err != nil {
|
|
return journal{}, fmt.Errorf("read schema version: %w", err)
|
|
}
|
|
t.say("backing up the database to %s", t.snapshot())
|
|
if err := t.Database.Backup(ctx, t.snapshot()); err != nil {
|
|
return journal{}, fmt.Errorf("back up the database: %w", err)
|
|
}
|
|
return journal{
|
|
Phase: phasePrepared,
|
|
Version: req.Version,
|
|
Previous: map[string]string{
|
|
ComponentDaemon: ReleaseVersion(buildinfo.Version),
|
|
ComponentAgent: installedAgent(t.BinDir),
|
|
},
|
|
Units: manifest.Required.Units,
|
|
Binaries: manifest.Required.Binaries,
|
|
Managed: managed,
|
|
Image: imageChange{Previous: image, Candidate: manifest.AgentImage},
|
|
SchemaBefore: schema,
|
|
Meta: req.Meta,
|
|
}, nil
|
|
}
|
|
|
|
// stage freezes the watchdog and every file rollback restores, places the
|
|
// candidates beside the live binaries, and publishes the journal last.
|
|
func (t *Transaction) stage(req PrepareRequest, release string, j journal) error {
|
|
preparing := filepath.Join(t.StateDir, "preparing")
|
|
if err := os.RemoveAll(preparing); err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(preparing, "previous"), 0o700); err != nil {
|
|
return err
|
|
}
|
|
// The release's own archied runs the watchdog, so it speaks the protocol
|
|
// of the services it switches to.
|
|
if err := installFile(filepath.Join(release, WatchdogBinary), filepath.Join(preparing, WatchdogBinary), 0o700); err != nil {
|
|
return fmt.Errorf("freeze the watchdog: %w", err)
|
|
}
|
|
for _, unit := range append(slices.Clone(j.Units), j.Binaries...) {
|
|
live := filepath.Join(t.BinDir, unit)
|
|
if _, err := os.Stat(live); err == nil {
|
|
if err := installFile(live, filepath.Join(preparing, "previous", unit), 0o755); err != nil {
|
|
return fmt.Errorf("save %s: %w", unit, err)
|
|
}
|
|
}
|
|
if err := installFile(filepath.Join(release, unit), t.staged(unit), 0o755); err != nil {
|
|
return fmt.Errorf("stage %s: %w", unit, err)
|
|
}
|
|
}
|
|
version := filepath.Join(t.BinDir, agentVersionFile)
|
|
if _, err := os.Stat(version); err == nil {
|
|
if err := installFile(version, filepath.Join(preparing, agentVersionFile), 0o644); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := syncDir(t.BinDir); err != nil {
|
|
return err
|
|
}
|
|
if err := writeJournal(preparing, j); err != nil {
|
|
return err
|
|
}
|
|
if err := syncDir(filepath.Join(preparing, "previous")); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Rename(preparing, JournalPath(t.StateDir)); err != nil {
|
|
return err
|
|
}
|
|
return syncDir(t.StateDir)
|
|
}
|
|
|
|
func downloadVerified(ctx context.Context, base, name, dst string) error {
|
|
var sums bytes.Buffer
|
|
if err := download(ctx, base+"SHA256SUMS", &sums, 1<<20); err != nil {
|
|
return err
|
|
}
|
|
expected := ""
|
|
scanner := bufio.NewScanner(&sums)
|
|
for scanner.Scan() {
|
|
if fields := strings.Fields(scanner.Text()); len(fields) == 2 && fields[1] == name {
|
|
if expected != "" {
|
|
return fmt.Errorf("SHA256SUMS lists %s twice", name)
|
|
}
|
|
expected = strings.ToLower(fields[0])
|
|
}
|
|
}
|
|
if len(expected) != sha256.Size*2 {
|
|
return fmt.Errorf("SHA256SUMS has no checksum for %s", name)
|
|
}
|
|
file, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
hash := sha256.New()
|
|
err = download(ctx, base+name, io.MultiWriter(file, hash), 1<<30)
|
|
if closeErr := file.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hex.EncodeToString(hash.Sum(nil)) != expected {
|
|
return fmt.Errorf("release checksum mismatch for %s", name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func download(ctx context.Context, url string, out io.Writer, limit int64) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("download %s: %w", url, err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("download %s: %s", url, resp.Status)
|
|
}
|
|
_, err = io.Copy(out, io.LimitReader(resp.Body, limit))
|
|
return err
|
|
}
|
|
|
|
// unpack extracts the zip's single distribution directory, refusing any
|
|
// entry outside it and anything but regular files and directories.
|
|
func unpack(archive, name, dst string) error {
|
|
reader, err := zip.OpenReader(archive)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = reader.Close() }()
|
|
for _, file := range reader.File {
|
|
clean := path.Clean(file.Name)
|
|
if !strings.HasPrefix(file.Name, name+"/") || strings.Contains(file.Name, `\`) ||
|
|
clean != strings.TrimSuffix(file.Name, "/") || !strings.HasPrefix(clean, name) {
|
|
return fmt.Errorf("unsafe archive path %q", file.Name)
|
|
}
|
|
mode := file.Mode()
|
|
target := filepath.Join(dst, filepath.FromSlash(clean))
|
|
switch {
|
|
case mode.IsDir():
|
|
if err := os.MkdirAll(target, 0o700); err != nil {
|
|
return err
|
|
}
|
|
case mode.IsRegular():
|
|
if err := extract(file, target); err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
return fmt.Errorf("archive entry %q is not a regular file", file.Name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func extract(file *zip.File, target string) error {
|
|
if err := os.MkdirAll(filepath.Dir(target), 0o700); err != nil {
|
|
return err
|
|
}
|
|
src, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = src.Close() }()
|
|
dst, err := os.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o700)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := io.Copy(dst, io.LimitReader(src, 1<<30)); err != nil {
|
|
_ = dst.Close()
|
|
return err
|
|
}
|
|
return dst.Close()
|
|
}
|
|
|
|
func readManifest(path string) (Manifest, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return Manifest{}, err
|
|
}
|
|
var manifest Manifest
|
|
if err := json.Unmarshal(data, &manifest); err != nil {
|
|
return Manifest{}, fmt.Errorf("decode release.json: %w", err)
|
|
}
|
|
if len(manifest.Required.Units) == 0 {
|
|
return Manifest{}, errors.New("release has no required_topology")
|
|
}
|
|
for _, unit := range manifest.Required.apps() {
|
|
if !releaseUnit.MatchString(unit) {
|
|
return Manifest{}, fmt.Errorf("invalid release unit %q", unit)
|
|
}
|
|
}
|
|
if !pinnedImage.MatchString(manifest.AgentImage) {
|
|
return Manifest{}, errors.New("release has no digest-pinned agent image")
|
|
}
|
|
return manifest, nil
|
|
}
|
|
|
|
// reportedVersion runs binary -version and returns the version on the line
|
|
// naming unit.
|
|
func reportedVersion(ctx context.Context, binary, unit string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
output, err := exec.CommandContext(ctx, binary, "-version").Output()
|
|
if err != nil {
|
|
return "", fmt.Errorf("%s -version: %w", unit, err)
|
|
}
|
|
for line := range strings.SplitSeq(string(output), "\n") {
|
|
if fields := strings.Fields(line); len(fields) == 2 && fields[0] == unit {
|
|
return fields[1], nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("%s -version names no version", unit)
|
|
}
|