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.
102 lines
3.3 KiB
Go
102 lines
3.3 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/statestore"
|
|
"github.com/samcharles93/archie-core/internal/domain/health"
|
|
)
|
|
|
|
// systemdUnits drives the archie user units through systemctl --user.
|
|
type systemdUnits struct{}
|
|
|
|
// Managed reports whether a systemd user manager answers.
|
|
func (systemdUnits) Managed(ctx context.Context) bool {
|
|
return systemctl(ctx, "show-environment") == nil
|
|
}
|
|
|
|
func (systemdUnits) Start(ctx context.Context, unit string) error {
|
|
return systemctl(ctx, "restart", unit+".service")
|
|
}
|
|
|
|
func (systemdUnits) Stop(ctx context.Context, unit string) error {
|
|
return systemctl(ctx, "stop", unit+".service")
|
|
}
|
|
|
|
func systemctl(ctx context.Context, args ...string) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
|
defer cancel()
|
|
output, err := exec.CommandContext(ctx, "systemctl", append([]string{"--user"}, args...)...).CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("systemctl --user %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(string(output)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// dockerImages pulls through the Docker CLI.
|
|
type dockerImages struct{}
|
|
|
|
func (dockerImages) Pull(ctx context.Context, ref string) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
|
|
defer cancel()
|
|
if output, err := exec.CommandContext(ctx, "docker", "pull", ref).CombinedOutput(); err != nil {
|
|
return fmt.Errorf("docker pull %s: %w: %s", ref, err, strings.TrimSpace(string(output)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// stateDatabase is the task database, backed up and restored in-process by
|
|
// the State Store's own recovery code.
|
|
type stateDatabase struct{ configPath string }
|
|
|
|
func (d stateDatabase) Backup(ctx context.Context, out string) error {
|
|
_, err := statestore.RunRecovery(ctx, statestore.RecoveryOptions{Operation: statestore.RecoveryBackup, Out: out, Config: d.configPath})
|
|
return err
|
|
}
|
|
|
|
func (d stateDatabase) Restore(ctx context.Context, from string) error {
|
|
_, err := statestore.RunRecovery(ctx, statestore.RecoveryOptions{Operation: statestore.RecoveryRestore, From: from, Config: d.configPath})
|
|
return err
|
|
}
|
|
|
|
func (d stateDatabase) SchemaVersion(ctx context.Context) (int64, error) {
|
|
return statestore.SchemaVersion(ctx, statestore.RecoveryOptions{Config: d.configPath})
|
|
}
|
|
|
|
// daemonHealth asks archied's /health/detailed, which answers 200 only once
|
|
// the daemon serves and every readiness probe passes.
|
|
type daemonHealth struct{ url string }
|
|
|
|
func (h daemonHealth) Healthy(ctx context.Context) (string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, h.url+"/health/detailed", nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var report struct {
|
|
Status health.Status `json:"status"`
|
|
Version string `json:"version"`
|
|
}
|
|
if resp.StatusCode != http.StatusOK || json.Unmarshal(body, &report) != nil || report.Status != health.StatusOK {
|
|
return report.Version, fmt.Errorf("%s: %s", resp.Status, strings.TrimSpace(string(body)))
|
|
}
|
|
return report.Version, nil
|
|
}
|