{"modulePath":"github.com/flashcatcloud/flashduty-runner","version":"v0.0.27","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package selfupdate\n\nPackage selfupdate implements in-process binary self-update for the runner.\n\n## Functions\n\n```go\nfunc BinaryVersion(ctx context.Context, path string) string\n```\nBinaryVersion runs \\`\\\u003cpath\u003e version\\` and parses the first line, which every released runner prints as \"flashduty-runner \\\u003cversion\u003e\". Returns \"\" (not an error) when the file doesn't exist, so callers can treat absent-canonical and unreadable-version identically: don't redirect.\n\n```go\nfunc NewerVersion(candidate, own string) bool\n```\nNewerVersion reports whether candidate is a parseable version strictly newer than own. Either side unparseable → false: a dev build never trampolines, and a canonical binary whose version can't be read is never exec'd into.\n\n```go\nfunc RestartInto(path string) error\n```\nRestartInto hands the process over to the binary at path (exec on unix, spawn-and-exit on windows). Exposed for the boot trampoline in cmd.\n\n```go\nfunc SeedCanonical(exe, canonical string) error\n```\nSeedCanonical copies the running (old) binary to the canonical path if nothing is there yet. Run before the first relocated Apply so the swap's .bak snapshot — and therefore probation rollback — has a real predecessor: rolling back from a bad new version lands on this copy of the version that was running, not on a missing file. Copies via a temp file + rename so a crash mid-copy can never leave a truncated binary at the canonical path.\n\n```go\nfunc ShouldSkipTarget(exePath, target string) bool\n```\nShouldSkipTarget reports whether an advertised target should be ignored because this host already rolled back from exactly that version.\n\n## Types\n\n```go\ntype Marker struct {\n\tTargetVersion string `json:\"target_version\"`\n\tAttempts      int    `json:\"attempts\"`\n\tRolledBack    bool   `json:\"rolled_back\"`\n}\n```\nMarker persists across re-exec/restart to drive the probation/rollback state machine. A live marker means \"an upgrade to TargetVersion is on probation\"; it is deleted on commit (handshake) and carries RolledBack=true across the rollback re-exec so the restored old binary can report the outcome once.\n\n```go\ntype Outcome struct {\n\tInProbation bool // main should Commit() after first successful handshake\n}\n```\nOutcome tells main what to do after CheckOnBoot.\n\n```go\ntype ProbationMgr struct {\n\tExePath        string\n\tCurrentVersion string\n\tMaxAttempts    int\n\t// contains filtered or unexported fields\n}\n```\nProbationMgr runs the boot-time probation/rollback state machine driven by the persisted upgrade marker. reExec is an injectable seam so tests never actually restart; nil means the real platform restartSelf.\n\n```go\nfunc NewProbationMgr(exePath, currentVersion string, maxAttempts int) *ProbationMgr\n```\nNewProbationMgr returns a ProbationMgr for the given canonical executable.\n\n```go\nfunc (p *ProbationMgr) CheckOnBoot() (Outcome, error)\n```\nCheckOnBoot inspects the marker at startup and advances the probation state machine: no marker -\u003e normal; committed/stale -\u003e cleanup; rolled\\_back -\u003e report; matching target not yet committed -\u003e probation (attempts++ then rollback past MaxAttempts).\n\n```go\nfunc (p *ProbationMgr) Commit()\n```\nCommit is called by main after the first successful WS handshake during probation. It clears the marker, the .bak rollback copy, and any prior failed-version memory (we've successfully moved to a new version, so older failures are moot).\n\n```go\nfunc (p *ProbationMgr) RollbackNow()\n```\nRollbackNow restores the previous binary and re-execs into it. main calls this when the probation deadline elapses without a successful handshake (failure mode ②: the new binary boots but cannot complete the WS handshake — auth/url/ protocol regression). It is independent of the boot-attempt counter, which can never fire under the unlimited-reconnect (maxAttempts=0) config because the process never exits.\n\n```go\ntype StatusFunc func(phase, errMsg string)\n```\nStatusFunc reports an upgrade phase (and optional error message) to the caller.\n\n```go\ntype Target struct {\n\tExe       string // running executable, symlinks resolved\n\tPath      string // upgrade target; \"\" if no writable location exists at all\n\tRelocated bool   // Path is the canonical state-home fallback, not Exe\n}\n```\nTarget is where self-update operates for this process.\n\nPath is the binary path upgrades download to, swap at, and re-exec from — and therefore also where the probation marker, .bak rollback copy, and failed-version memory live (layoutFor anchors on it). When the running executable's own directory is writable, Path == Exe and upgrades swap in place (systemd state-dir installs, root installs, dev builds). When it is not — a root-owned /usr/local/bin binary run as a regular user — Path falls back to the canonical runner-owned location \\\u003cstate home\u003e/bin, which the boot trampoline in cmd/main.go redirects into on every later start.\n\n```go\nfunc ResolveTarget(exePath, stateHome string) Target\n```\nResolveTarget decides where self-update should operate, given the running executable path and the runner state home (environment.RunnerStateHome()).\n\nexePath is symlink-resolved first: on Linux os.Executable() already returns the /proc/self/exe target, but on darwin it returns the invocation path, so an installer layout that symlinks /usr/local/bin -\u003e \\\u003cstate dir\u003e/bin would otherwise be misjudged by the writability probe.\n\nThe canonical fallback directory is created on demand; this is the same \\\u003cstate home\u003e/bin that holds the bundled fduty CLI, guaranteed writable by the runner's own user. Note it deliberately ignores FLASHDUTY\\_RUNNER\\_BIN\\_DIR: that override exists to point bash's PATH at an image-baked fduty (e.g. /usr/local/bin on cloud images) and may well be read-only — the runner binary's home must stay in the user-writable state tree.\n\n```go\ntype Updater struct {\n\tExePath string\n\t// contains filtered or unexported fields\n}\n```\nUpdater performs an in-process self-update: download, verify, extract, atomic swap, marker write, and restart. reExec is an injectable seam so tests never actually restart; nil means the real platform restartSelf (exec on unix, spawn-and-exit on windows).\n\n```go\nfunc NewUpdater(exePath string) *Updater\n```\nNewUpdater returns an Updater bound to the binary path upgrades operate on — Target.Path from ResolveTarget: the running executable for in-place updates, or the canonical state-home location for relocated ones.\n\n```go\nfunc (u *Updater) Apply(ctx context.Context, p protocol.UpgradePayload, status StatusFunc) error\n```\nApply downloads the target artifact, verifies it, atomically swaps it into place, writes the probation marker, and re-execs. On any failure before the swap it leaves the running binary untouched; on a post-swap re-exec failure it rolls back inline.\n\n","path":"github.com/flashcatcloud/flashduty-runner/selfupdate","name":"selfupdate","synopsis":"Package selfupdate implements in-process binary self-update for the runner.","isRedistributable":true}
