upgrade

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package upgrade replaces the controller and agent binaries in place with automatic rollback (§5.0.1). The process that is replaced cannot watch its own replacement, so a detached guard (a copy of the old binary) stops it, swaps the binary, starts the new one, checks it and restores the old binary (and, for the controller, the database snapshot) when it is not healthy.

Index

Constants

View Source
const (
	PhasePreparing   = "preparing"
	PhaseStopping    = "stopping"
	PhaseStarting    = "starting"
	PhaseVerifying   = "verifying"
	PhaseDone        = "done"
	PhaseRollingBack = "rolling-back"
	PhaseRolledBack  = "rolled-back"
	PhaseFailed      = "failed" // rollback did not bring the old controller back
)

Phases of a controller upgrade.

View Source
const DefaultReleaseURL = version.Repository

DefaultReleaseURL is where releases are published: the project's GitHub releases.

View Source
const RunInfoFile = "controller-run.json"

RunInfoFile is where the controller records how it was started.

Variables

View Source
var AgentReconnectTimeout = 2 * time.Minute

AgentReconnectTimeout is how long a new agent has to reconnect before the guard restores the old one.

Functions

func AgentGuard

func AgentGuard(ctx context.Context, dataDir string) error

AgentGuard waits for the new agent to reconnect (it removes the pending file) and otherwise restores and restarts the old one.

func AgentStagingFile

func AgentStagingFile(dataDir string) string

AgentStagingFile is where an incoming agent binary is written.

func FileSHA256

func FileSHA256(path string) (string, error)

FileSHA256 is the hex SHA-256 of a file.

func Guard

func Guard(ctx context.Context, dataDir string) error

Guard runs the upgrade prepared by Begin: stop, swap, start, verify, and roll back when the new controller is not healthy.

func Install

func Install(src, dst string) error

Install replaces dst with src atomically (rename over a running binary is safe on Linux: the old inode lives until the process exits).

func Reexec

func Reexec(ri RunInfo) error

Reexec replaces this process with the (new) binary, keeping its PID, arguments and environment. Containers keep running meanwhile.

func Spawn

func Spawn(name, logPath string, underSystemd bool, argv ...string) error

Spawn starts a detached helper (the guard) that survives the caller: as a transient systemd unit when the caller runs under systemd (so stopping the caller's unit does not kill it), else in its own session.

func Start

func Start(ri RunInfo, fallbackLog string) (int, error)

Start starts the process ri describes again (detached) and returns its PID.

func Stop

func Stop(ri RunInfo, timeout time.Duration) error

Stop stops the process ri describes and waits for it to exit.

func Terminal

func Terminal(p string) bool

Terminal reports whether no guard works on a state in phase p.

func ValidVersion

func ValidVersion(v string) bool

ValidVersion reports whether v is usable as a path segment.

func WriteRunInfo

func WriteRunInfo(path, healthURL string) error

WriteRunInfo records this process in path.

func WriteSums

func WriteSums(dir string) error

WriteSums writes SHA256SUMS for every agent and synctl binary in dir.

Types

type AgentPending

type AgentPending struct {
	Version  string    `json:"version"`
	From     string    `json:"from"`
	Exe      string    `json:"exe"`
	Prev     string    `json:"prev"`
	Deadline time.Time `json:"deadline"`
	Run      RunInfo   `json:"run"`
}

AgentPending is written before an agent replaces itself and removed by the new agent once the controller accepts it.

func ReadAgentPending

func ReadAgentPending(dataDir string) (AgentPending, bool)

ReadAgentPending returns the pending upgrade, if any.

type AgentResult

type AgentResult struct {
	Version string `json:"version"`
	Error   string `json:"error"`
}

AgentResult is left for the restored agent to report.

func AgentConnected

func AgentConnected(dataDir, running string) *AgentResult

AgentConnected is called by an agent once the controller accepted it: a pending upgrade to this version succeeded. It returns a result to report (a rolled-back upgrade), if any.

type Files

type Files map[string]string

Files of a release fetched by Fetch, by base name.

type Health

type Health struct {
	Version  string   `json:"version"`
	Ready    bool     `json:"ready"`
	Problems []string `json:"problems"`
}

Health is what /api/v1/system/health returns.

func CheckHealth

func CheckHealth(ctx context.Context, url string) (Health, error)

CheckHealth asks a controller whether it is ready.

type Options

type Options struct {
	DataDir   string
	DBPath    string
	Version   string // target
	Current   string // running version
	Source    Source
	HealthURL string
	Downloads string // agents and synctl served to workers ("" = none)
	// Snapshot writes a consistent copy of the database to path.
	Snapshot func(ctx context.Context, path string) error
	// Run is how the running controller was started.
	Run     RunInfo
	Settle  time.Duration // healthy for this long before it counts (default 2m)
	Timeout time.Duration // to become healthy (default 5m)
}

Options of a controller upgrade.

type RunInfo

type RunInfo struct {
	PID  int      `json:"pid"`
	Exe  string   `json:"exe"`
	Argv []string `json:"argv"`
	Env  []string `json:"env"`
	Dir  string   `json:"dir"`
	// Stdout is the file standard output and error went to ("" if not a file).
	Stdout string `json:"stdout,omitempty"`
	// Unit is the systemd unit running the process ("" when not under systemd).
	Unit string `json:"unit,omitempty"`
	// HealthURL is the controller's /api/v1/system/health (controllers only).
	HealthURL string `json:"healthUrl,omitempty"`
}

RunInfo is how a running controller or agent was started, written at start so a guard can stop and start it again the same way.

func BeginAgent

func BeginAgent(dataDir, staged, version, from string) (RunInfo, error)

BeginAgent replaces the running agent's binary with staged (already verified) after starting a guard; the caller then re-executes itself.

func CurrentRunInfo

func CurrentRunInfo() (RunInfo, error)

CurrentRunInfo describes this process.

func ReadRunInfo

func ReadRunInfo(path string) (RunInfo, error)

ReadRunInfo reads a RunInfo written by WriteRunInfo.

type Service

type Service struct {
	DataDir   string
	DBPath    string
	Downloads string
	Source    Source
	Channel   string
	Current   string
	Settle    time.Duration
	Snapshot  func(ctx context.Context, path string) error
	// contains filtered or unexported fields
}

Service starts controller upgrades for the API and reports their state.

func (*Service) Begin

func (s *Service) Begin(ctx context.Context, version string) (State, error)

Begin upgrades to version ("" = the channel's latest).

func (*Service) Latest

func (s *Service) Latest(ctx context.Context, refresh bool) (string, error)

Latest is the newest version on the channel (cached for 10 minutes).

func (*Service) RunInfoPath

func (s *Service) RunInfoPath() string

RunInfoPath is where the running controller recorded how it was started.

func (*Service) Start

func (s *Service) Start(ctx context.Context, version string) error

Start checks what can be checked at once and runs Begin in the background (downloads can take a while); progress is in State.

func (*Service) State

func (s *Service) State() (State, bool)

State is the last upgrade (ok false when none ran).

type Source

type Source struct {
	Base string
	HTTP *http.Client
	// API is GitHub's REST API (default https://api.github.com).
	API string
}

Release layout (the same as install.sh downloads):

<base>/channels/<channel>       a version, e.g. "1.4.2"
<base>/<version>/SHA256SUMS
<base>/<version>/syncloud-controller-linux-<arch>
<base>/<version>/syncloud-agent-linux-<arch>
<base>/<version>/synctl-linux-<arch>

base is https://…, http://… or file:///dir (mirrors, air-gapped installs and tests). A GitHub repository base (https://github.com/<owner>/<repo>) uses GitHub's release layout instead: files are release assets of the tag v<version>, "stable" is the latest release and "beta" the newest one including prereleases.

func (Source) Fetch

func (s Source) Fetch(ctx context.Context, version, dir string) (Files, error)

Fetch downloads a release into dir and checks every file against SHA256SUMS. The controller for this machine's architecture is required; agents and synctl for other architectures are taken when present.

func (Source) FetchFile

func (s Source) FetchFile(ctx context.Context, version, name, dst string) error

FetchFile downloads one file of a release to dst, checked against the release's SHA256SUMS (for large files such as image archives).

func (Source) Latest

func (s Source) Latest(ctx context.Context, channel string) (string, error)

Latest is the version a channel points at.

type State

type State struct {
	ID         string     `json:"id"`
	From       string     `json:"from"`
	To         string     `json:"to"`
	Phase      string     `json:"phase"`
	Message    string     `json:"message,omitempty"`
	Steps      []Step     `json:"steps"`
	StartedAt  time.Time  `json:"startedAt"`
	UpdatedAt  time.Time  `json:"updatedAt"`
	FinishedAt *time.Time `json:"finishedAt,omitempty"`

	Dir       string  `json:"dir"`
	Exe       string  `json:"exe"`
	Prev      string  `json:"prev"`
	New       string  `json:"new"`
	Snapshot  string  `json:"snapshot"`
	DBPath    string  `json:"dbPath"`
	HealthURL string  `json:"healthUrl"`
	Downloads string  `json:"downloads,omitempty"`
	Files     Files   `json:"files"`
	Run       RunInfo `json:"run"`
	Settle    int     `json:"settleSeconds"`
	Timeout   int     `json:"timeoutSeconds"`
}

State of the current (or last) controller upgrade, in <data>/upgrade/state.json.

func Begin

func Begin(ctx context.Context, o Options) (State, error)

Begin prepares an upgrade (download, verify, snapshot, keep the current binary) and hands it to a detached guard, which restarts the controller.

func ReadState

func ReadState(dataDir string) (State, error)

ReadState returns the last upgrade's state (os.ErrNotExist when none ran).

type Step

type Step struct {
	At   time.Time `json:"at"`
	Text string    `json:"text"`
}

Step is one line of an upgrade's progress.

Directories

Path Synopsis
Package rollout upgrades agents node by node (§5.0.1 step 6, §6.2): each node gets the agent binary from the controller's downloads over its agent stream, restarts into it, and must reconnect with the new version before the next node starts.
Package rollout upgrades agents node by node (§5.0.1 step 6, §6.2): each node gets the agent binary from the controller's downloads over its agent stream, restarts into it, and must reconnect with the new version before the next node starts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL