refactor(staterpc,releaseupdate,agentexec,storecontract,worktree): cut comments down to what the code does
This commit is contained in:
1 parent
ee5f59e189
commit
2fc11b60e1
22 files changed
+175
-822
No files matched your search
@@ -36,21 +36,15 @@ func (c CommandCatalog) Check(ctx context.Context) (Snapshot, error) {
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
// CommandInstaller runs the deployment's own install command. HealthURL is
|
||||
// the base URL its restart tooling must poll for /healthz -- only this
|
||||
// process knows which address it actually serves, so a script left to guess
|
||||
// one rolls back releases that came up healthy. Empty
|
||||
// leaves ARCHIE_HEALTH_URL as the environment already has it.
|
||||
// CommandInstaller runs the deployment's install command. HealthURL is
|
||||
// passed as ARCHIE_HEALTH_URL when set.
|
||||
type CommandInstaller struct {
|
||||
Command []string
|
||||
HealthURL string
|
||||
}
|
||||
|
||||
// Install runs the configured command and streams its stdout to progress
|
||||
// line by line as the command emits it -- the earlier CombinedOutput-based
|
||||
// version buffered everything until exit, so callers saw nothing until the
|
||||
// whole update either finished or failed. One stdout line may be a
|
||||
// structured Result instead of prose; see updateResultSentinel.
|
||||
// Install runs the command, streaming stdout lines to progress. One line may
|
||||
// be a structured Result.
|
||||
func (i CommandInstaller) Install(ctx context.Context, snapshot Snapshot, meta InstallMeta, progress func(string)) (Result, error) {
|
||||
if len(i.Command) == 0 {
|
||||
return Result{}, fmt.Errorf("update install command is empty")
|
||||
|
||||
@@ -7,13 +7,8 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Report is the phase-2 outcome of an update: whether the daemon came back
|
||||
// up healthy after restarting, written by whichever process determined that
|
||||
// (the deployment's restart/health-check tooling) and read, relayed to
|
||||
// Channel/ChatID/ThreadID, and cleared by whichever archied process boots
|
||||
// next -- which may be the newly installed version (success) or the
|
||||
// previous one, if a crash-loop forced a rollback before this code could
|
||||
// ever run on the new version.
|
||||
// Report is an update's post-restart outcome, written by the restart tooling
|
||||
// and relayed by the next archied boot.
|
||||
type Report struct {
|
||||
Channel string `json:"channel"`
|
||||
ChatID int64 `json:"chat_id"`
|
||||
|
||||
@@ -8,12 +8,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
// ReportEvent turns a phase-2 Report, plus its verification against
|
||||
// the versions actually running, into the activity-stream event the
|
||||
// dashboard renders. Unlike Telegram's formatPendingReport, this ships the
|
||||
// structured Verification rather than pre-rendered prose, so the frontend
|
||||
// controls presentation -- the two channels' operators read the same facts
|
||||
// through different UIs, not necessarily the same sentence.
|
||||
// ReportEvent turns a Report and its verification into a dashboard event.
|
||||
func ReportEvent(report Report, running map[string]string) events.Event {
|
||||
verification := report.Verify(running)
|
||||
drift := make([]map[string]any, 0, len(verification.Drift))
|
||||
@@ -40,13 +35,8 @@ func ReportEvent(report Report, running map[string]string) events.Event {
|
||||
}
|
||||
}
|
||||
|
||||
// summarizeReport is the one-line summary shown in the dashboard's
|
||||
// activity feed, which renders event.Detail generically for every event
|
||||
// kind (see ui/src/dashboard/dashboard.js activityRow). It condenses the
|
||||
// same facts internal/channels/telegram/update.go's formatPendingReport
|
||||
// spells out at length -- not a byte-identical wording, since the two
|
||||
// surfaces render differently, but the same four outcomes: confirmed,
|
||||
// drifted, unverified, or rolled back.
|
||||
// summarizeReport returns a one-line summary: confirmed, drifted, unverified
|
||||
// or rolled back.
|
||||
func summarizeReport(report Report, verification Verification) string {
|
||||
if report.RolledBack {
|
||||
return "Update failed its health check and was rolled back to the previous version."
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
package releaseupdate
|
||||
|
||||
// InstallMeta identifies who to notify once an installed update finishes
|
||||
// restarting. The process that calls Install returns before the daemon
|
||||
// actually restarts (the reference installer queues the restart through a
|
||||
// detached unit so restarting archied.service doesn't kill the installer
|
||||
// mid-run), so this identity has to survive the restart on disk -- see
|
||||
// Report and WritePendingReport.
|
||||
// InstallMeta identifies who to notify after the update restarts.
|
||||
type InstallMeta struct {
|
||||
Channel string
|
||||
ChatID int64
|
||||
@@ -24,11 +19,8 @@ type ComponentResult struct {
|
||||
Status string `json:"status"` // "updated", "unchanged", or "failed"
|
||||
}
|
||||
|
||||
// Result is the structured outcome of the synchronous phase of an install:
|
||||
// fetch/build/install, before any restart happens. Previous and Installed
|
||||
// map a component ID to its version. Field names are snake_case in JSON to
|
||||
// match the install script's ARCHIE_UPDATE_RESULT sentinel line -- see
|
||||
// command.go and scripts/archie-update-install.
|
||||
// Result is the outcome of an install before restart. Previous and Installed
|
||||
// map component IDs to versions.
|
||||
type Result struct {
|
||||
Previous map[string]string `json:"previous"`
|
||||
Installed map[string]string `json:"installed"`
|
||||
|
||||
@@ -23,11 +23,8 @@ 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.
|
||||
// The returned Result covers only the synchronous phase (fetch/build/
|
||||
// install) -- whether a subsequent restart actually came up healthy is
|
||||
// reported later, out of band, via a Report (see pending_report.go).
|
||||
// Installer applies an approved update. The restart outcome is reported
|
||||
// later as a Report.
|
||||
type Installer interface {
|
||||
Install(context.Context, Snapshot, InstallMeta, func(string)) (Result, error)
|
||||
}
|
||||
@@ -38,15 +35,8 @@ type Component struct {
|
||||
Installed string
|
||||
Available string
|
||||
Changelog string
|
||||
// InstallType and Reference describe how this component is actually
|
||||
// deployed -- "binary"/"container"/"embedded"/"external", and a
|
||||
// deployment-specific pointer such as an image digest or a NATS URL.
|
||||
// The external check command that populates Installed/Available cannot
|
||||
// know either of these: it reports what version exists, not how this
|
||||
// particular instance was installed. See Service.Enrich, which fills
|
||||
// them in from sources that actually know (this process's own build
|
||||
// stamp, an observed archie-agent version, deployment configuration for
|
||||
// a third-party dependency this repo doesn't build).
|
||||
// InstallType and Reference describe how this component is deployed, filled
|
||||
// by Service.Enrich.
|
||||
InstallType string
|
||||
Reference string
|
||||
}
|
||||
@@ -93,25 +83,11 @@ type Service struct {
|
||||
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 must be a stamped install type, or Install refuses to run.
|
||||
InstallType string
|
||||
|
||||
// Enrich supplies InstallType/Reference for a known component ID (see
|
||||
// ComponentDaemon, ComponentAgent, ComponentNATS), using sources the
|
||||
// external check command has no way to know -- this process's own
|
||||
// compiled-in installtype.Type(), an archie-agent version actually
|
||||
// observed at runtime (daemon.AgentStatus), or deployment configuration
|
||||
// for a third-party dependency archie doesn't build. Optional: nil (or
|
||||
// a component ID Enrich doesn't recognise, or an empty installType
|
||||
// return) leaves whatever the check command itself reported untouched,
|
||||
// so a check command that DOES report its own value is never silently
|
||||
// overwritten.
|
||||
// Enrich returns a component's install type and reference. Nil or an empty
|
||||
// result leaves the check command's values.
|
||||
Enrich func(componentID string) (installType, reference string)
|
||||
|
||||
mu sync.Mutex
|
||||
@@ -193,12 +169,7 @@ func (s *Service) Defer(ctx context.Context, recipient int64, expected Snapshot)
|
||||
// instance was actually deployed.
|
||||
var ErrUnknownInstallType = errors.New("refusing to install: install type is unknown")
|
||||
|
||||
// ErrInstallInProgress is returned by Install when another install is
|
||||
// already running. Telegram and the web dashboard each keep their own
|
||||
// local "already in progress" flag for a quick UI response, but both are
|
||||
// wired to the same Service in production -- this is the one guard that
|
||||
// actually sees both, since a caller-local bool would let one adapter start
|
||||
// a second install while the other's is still copying binaries.
|
||||
// ErrInstallInProgress is returned when another install is running.
|
||||
var ErrInstallInProgress = errors.New("an update is already in progress")
|
||||
|
||||
// installTimeout bounds an install after it has been deliberately detached
|
||||
@@ -206,17 +177,8 @@ var ErrInstallInProgress = errors.New("an update is already in progress")
|
||||
// script would hold the in-progress lock forever.
|
||||
const installTimeout = 30 * time.Minute
|
||||
|
||||
// Install runs the configured Installer. The context it hands the
|
||||
// Installer is intentionally NOT ctx: ctx belongs to whatever triggered the
|
||||
// update (a chat message handler cancelled by /restart, an HTTP request
|
||||
// cancelled by a client disconnect), and exec.CommandContext SIGKILLs its
|
||||
// child the instant that context is cancelled. The install script backs up
|
||||
// and overwrites the live binaries with plain, non-atomic copies -- a kill
|
||||
// mid-copy can leave the daemon unable to start, with the one thing that
|
||||
// could roll that back (the watchdog) never launched because the script
|
||||
// never reached that line. Detaching the install from ctx, bounded by
|
||||
// installTimeout instead, is what actually makes the caller's own
|
||||
// lifecycle safe to interrupt without corrupting an in-flight update.
|
||||
// Install runs the Installer under installTimeout, detached from ctx so a
|
||||
// cancelled caller cannot kill an install mid-copy.
|
||||
func (s *Service) Install(ctx context.Context, snapshot Snapshot, meta InstallMeta, progress func(string)) (Result, error) {
|
||||
if s == nil || s.Installer == nil {
|
||||
return Result{}, errors.New("update installation is not configured")
|
||||
@@ -269,11 +231,7 @@ func saveDeferrals(path string, state deferrals) error {
|
||||
return writeFileAtomic(path, data)
|
||||
}
|
||||
|
||||
// writeFileAtomic writes data to path via a temp file in the same
|
||||
// directory, synced and renamed into place, so a reader never observes a
|
||||
// partially written file. Shared by saveDeferrals and WritePendingReport --
|
||||
// both are small JSON state files under the same operational directory
|
||||
// (cfg.WorkDir) with the same durability requirement.
|
||||
// writeFileAtomic writes data to path through a synced temp file and rename.
|
||||
func writeFileAtomic(path string, data []byte) error {
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
|
||||
@@ -5,69 +5,35 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Component IDs used by the reference install script and watchdog in the
|
||||
// Result and Report JSON documents (scripts/archie-update-install,
|
||||
// scripts/archie-update-watchdog). They are named here because the
|
||||
// composition root has to key its running-version map by exactly these
|
||||
// strings for verification to match anything -- a typo would otherwise
|
||||
// silently downgrade every component to "unverifiable" and restore the
|
||||
// false-success behaviour this file exists to prevent.
|
||||
// Component IDs used by the install script and watchdog.
|
||||
const (
|
||||
ComponentDaemon = "daemon"
|
||||
ComponentAgent = "agent"
|
||||
// ComponentNATS identifies the NATS server in a Snapshot's Components,
|
||||
// by the same convention -- a third-party dependency archie doesn't
|
||||
// build and Report never claims to have installed, but Service.Enrich
|
||||
// can still describe from deployment configuration (embedded vs
|
||||
// external, and the URL an external server was configured with).
|
||||
// ComponentNATS identifies the NATS server.
|
||||
ComponentNATS = "nats"
|
||||
)
|
||||
|
||||
// Version placeholders that carry no release information. Both mean "nobody
|
||||
// recorded a real version here", and neither may be compared as though it
|
||||
// were one: doing so turns a healthy update whose bookkeeping went missing
|
||||
// into a reported failure.
|
||||
//
|
||||
// - VersionUnknown is what the reference watchdog substitutes when it
|
||||
// cannot tell what was installed ("${ARCHIE_UPDATE_INSTALLED_GATEWAY:-unknown}").
|
||||
// - VersionDev is what an unstamped build reports for itself: it is the
|
||||
// default of gatewayVersion/runtimeVersion in cmd/archied, of
|
||||
// GATEWAY_VERSION in Dockerfile.archied, and of the version Taskfile.yml
|
||||
// falls back to when git describes no tag. A plain `go build` or an
|
||||
// untagged image is therefore "dev", not a release.
|
||||
// Placeholder versions that are never compared: VersionUnknown from the
|
||||
// watchdog, VersionDev from unstamped builds.
|
||||
const (
|
||||
VersionUnknown = "unknown"
|
||||
VersionDev = "dev"
|
||||
)
|
||||
|
||||
// VersionDrift is one component whose reported install the running system
|
||||
// contradicts: the installer said it put Claimed into service, but the
|
||||
// component itself reports it is running Running. Both are the versions as
|
||||
// recorded rather than normalized, so an operator sees the strings the two
|
||||
// sides actually wrote; renderers should trim them for display.
|
||||
// VersionDrift is a component running a different version than the one
|
||||
// claimed installed.
|
||||
type VersionDrift struct {
|
||||
ID string
|
||||
Claimed string
|
||||
Running string
|
||||
|
||||
// RunningIsPrevious reports whether the version actually running is the
|
||||
// one this update set out to replace. True is the ordinary case -- the
|
||||
// install silently did nothing and the pre-update process is still there
|
||||
// answering the health probe. False means what is running is neither the
|
||||
// installed version nor the replaced one, and a caller must NOT describe
|
||||
// that as "still on the old release": it does not know what happened,
|
||||
// and asserting a cause it cannot support is the same defect as the
|
||||
// false success this type exists to catch.
|
||||
// RunningIsPrevious reports whether the running version is the one the
|
||||
// update replaced.
|
||||
RunningIsPrevious bool
|
||||
}
|
||||
|
||||
// Verification is the outcome of checking a Report's claims against the
|
||||
// versions actually running. Every component the Report claims to have
|
||||
// installed lands in exactly one of the three sets.
|
||||
//
|
||||
// Unverified is not a lesser Drift: a caller that cannot tell the two apart
|
||||
// will either cry wolf over components that simply cannot self-report, or
|
||||
// keep asserting success it never checked. Both have shipped here.
|
||||
// Verification sorts each claimed component into confirmed, drifted or
|
||||
// unverified.
|
||||
type Verification struct {
|
||||
// Confirmed lists component IDs whose claimed version matches what the
|
||||
// component reports for itself, in ID order.
|
||||
@@ -81,23 +47,9 @@ type Verification struct {
|
||||
Unverified []string
|
||||
}
|
||||
|
||||
// Verify compares what r claims was installed against the versions actually
|
||||
// running now.
|
||||
//
|
||||
// This exists because a Report is entirely self-reported. The reference
|
||||
// watchdog decides "passed" from an HTTP health probe, which proves only
|
||||
// that *a* daemon is answering -- if the new binary never actually made it
|
||||
// into service, the previous one answers the probe just as happily, and the
|
||||
// watchdog then copies the version the installer *intended* into Installed.
|
||||
// The result reads as a successful upgrade to a version that is not running.
|
||||
// That is not hypothetical: it shipped, and a task run immediately after a
|
||||
// reported-successful update hit the exact bug the update was supposed to
|
||||
// fix.
|
||||
//
|
||||
// running maps a component ID (see ComponentDaemon and ComponentAgent) to
|
||||
// the version that component reports for itself -- archied's own compiled-in
|
||||
// build version, for instance, which no installer can talk it out of. A
|
||||
// component absent from running is Unverified, not drifting.
|
||||
// Verify compares the versions r claims were installed with the versions
|
||||
// components report running. Components absent from running are
|
||||
// unverified.
|
||||
func (r Report) Verify(running map[string]string) Verification {
|
||||
var result Verification
|
||||
for id, claimed := range r.Installed {
|
||||
@@ -133,18 +85,8 @@ func IsRecordedVersion(version string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// comparableVersion reduces a recorded version to the form both sides of a
|
||||
// comparison can be expected to agree on, and reports whether it carries any
|
||||
// usable information at all.
|
||||
//
|
||||
// Normalization is deliberately minimal: surrounding whitespace, and a
|
||||
// single leading "v" or "V" so an installer that records the git tag
|
||||
// "v1.9.11" matches a binary stamped "1.9.11" (the reference installer
|
||||
// strips that prefix itself, but nothing forces another deployment shape
|
||||
// to). Everything else is compared exactly -- an installer is expected to
|
||||
// record the version it stamped, not a differently formatted description of
|
||||
// it, and guessing at looser equivalences here would start hiding the very
|
||||
// mismatches this check is for.
|
||||
// comparableVersion trims whitespace and a leading "v", and reports whether
|
||||
// the version is real.
|
||||
func comparableVersion(version string) (string, bool) {
|
||||
normalized := strings.TrimSpace(version)
|
||||
if len(normalized) > 0 && (normalized[0] == 'v' || normalized[0] == 'V') {
|
||||
|
||||
Reference in new issue
Block a user