Documentation
¶
Overview ¶
Package selfupdate implements ScopeWeaver's one-click update: download a GitHub release executable, verify and stage it, then atomically replace it on startup.
Responsibilities (see start.sh / start.bat):
Startup scripts: supervise the process and decide whether to restart from its exit code. This package: download, SHA256 verification, smoke testing, replacement, and rollback.
Replacement lives in Go because shell and batch would need separate checksum and smoke-test implementations (sha256sum/shasum/certutil) at the most critical safety boundary. An unexecutable update would otherwise require manual recovery.
A complete upgrade involves three process starts:
1. Old server receives /api/update/apply, downloads/verifies, stages scopeweaver-cli.new, exits 75. 2. Supervisor restarts old server; Bootstrap verifies/smoke-tests/replaces the staged file, exits 75. 3. Supervisor starts the new version; Bootstrap records an attempt and clears the marker after stability.
Failure preserves/restores the old version: step 2 discards invalid staging; three consecutive unstable starts in step 3 automatically restore scopeweaver-cli.old.
Index ¶
- Constants
- func AssetName(tag, goos, goarch string) string
- func Bootstrap() (Action, State)
- func CompareVersions(a, b string) (int, bool)
- func HasBackup() bool
- func InDocker() bool
- func NewClient(proxy string) *http.Client
- func Rollback() error
- func Settle()
- func Stage(ctx context.Context, c *http.Client, rel *Release, currentVersion string, ...) error
- func StageMessages(ctx context.Context, c *http.Client, rel *Release, currentVersion string, ...) error
- type Action
- type Asset
- type MessageProgress
- type Paths
- type Phase
- type Progress
- type Release
- type State
Constants ¶
const ExitRestart = 75
ExitRestart asks the supervisor to relaunch immediately (EX_TEMPFAIL), without crash backoff. Exit 0 is a normal stop; other exit codes indicate crashes.
const Repo = "cskwork/scopeweaver-cli"
Repo is the fixed release source. Making it configurable would grant anyone who can edit configuration a remote-code-execution path, which is unacceptable here.
const SettleDelay = 30 * time.Second
SettleDelay is the stable runtime required to confirm that the new version survived.
Variables ¶
This section is empty.
Functions ¶
func AssetName ¶
AssetName returns the platform package name, matching build.sh package_binary: scopeweaver-cli-<version>-<os>-<arch>.zip, with the leading v removed from the version.
func Bootstrap ¶
Bootstrap runs first in main, before opening a database or listening on ports.
Three possible states:
1. scopeweaver-cli.new exists: verify and smoke-test, then replace/restart; discard on failure. 2. Only a marker remains: increment the post-replacement attempt count; roll back after repeated failures. 3. Neither exists: start normally.
func CompareVersions ¶
CompareVersions returns -1/0/1 for a<b, a==b, or a>b. ok=false means at least one version is not comparable, such as dev or git-describe output like 0.3.7-2-gabc1234-dirty. Disable one-click updates then, rather than overwriting a development build and uncommitted changes with a release.
func HasBackup ¶
func HasBackup() bool
HasBackup reports whether a previous version exists, controlling the rollback button.
func InDocker ¶
func InDocker() bool
InDocker reports container execution. Replacement modifies the writable layer; recreating the container with docker compose restores the image's version. This is expected when pulling a new image, but the UI must explain it accurately.
func NewClient ¶
NewClient accepts only GitHub hosts. An empty proxy uses a direct connection.
Do not reuse the default transport: updates must require TLS and certificate verification regardless of InsecureSkipVerify settings elsewhere.
func Rollback ¶
func Rollback() error
Rollback implements /api/update/rollback, explicitly restoring the previous version. It only swaps files; the caller exits with ExitRestart and the supervisor restarts it.
func Settle ¶
func Settle()
Settle confirms stable operation of the new version and clears the upgrade marker.
main calls it after a delay following HTTP startup. Crashing before that point leaves the marker so subsequent attempts accumulate until rollback is triggered.
func Stage ¶
func Stage(ctx context.Context, c *http.Client, rel *Release, currentVersion string, prog Progress) error
Stage downloads and verifies the platform package, then stages scopeweaver-cli.new.
Use the full ZIP rather than a bare executable because SHA256SUMS covers ZIPs. This matches the release pipeline and its existing archives. The package also includes skills for potential future synchronization, costing only a few hundred KB.
Returning successfully means staging is complete; the caller then shuts down with ExitRestart.
func StageMessages ¶
func StageMessages(ctx context.Context, c *http.Client, rel *Release, currentVersion string, prog MessageProgress) error
StageMessages retains built-in progress templates for per-subscriber rendering. Stage remains compatible with existing callers that consume plain text.
Types ¶
type Asset ¶
type Asset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
Size int64 `json:"size"`
}
Asset describes one attached release file.
type MessageProgress ¶
MessageProgress carries a localizable progress event without modifying raw arguments.
type Paths ¶
type Paths struct {
Dir string // Executable directory.
Current string // Current executable: scopeweaver-cli / scopeweaver-cli.exe.
New string // Staged version: scopeweaver-cli.new / scopeweaver-cli.new.exe.
Sum string // Staged SHA256 (hex): scopeweaver-cli.new.sha256 / scopeweaver-cli.new.exe.sha256.
Old string // Previous executable: scopeweaver-cli.old / scopeweaver-cli.old.exe.
Marker string // Upgrade state marker: scopeweaver-cli.upgrade.json.
}
Paths contains all upgrade files, rooted in the executable's directory. Do not use CWD: a service may run from / or any other directory, which would put staging files elsewhere and break replacement.
func ResolvePaths ¶
ResolvePaths derives all upgrade paths from the current executable.
Windows .new/.old files must retain .exe for smoke testing and execution. Strip the extension before adding the suffix to keep platform naming symmetric.
type Progress ¶
Progress reports to the caller/UI. pct is meaningful only during download (0-100); other phases use -1.
type Release ¶
type Release struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Body string `json:"body"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
PublishedAt time.Time `json:"published_at"`
HTMLURL string `json:"html_url"`
Assets []Asset `json:"assets"`
}
Release contains the GitHub release fields needed by the updater.
func FetchLatest ¶
FetchLatest fetches the latest stable release.
type State ¶
type State struct {
Pending bool // Replacement has not yet been confirmed stable.
RolledBack bool // An automatic rollback occurred during this startup.
FailedStage bool // Staged update failed verification/smoke testing and was discarded.
Detail string // One-sentence user-facing explanation.
}
State describes this startup's upgrade state so /api/update/check can report accurately whether the previous upgrade succeeded or was rolled back.