state

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: GPL-3.0, LGPL-3.0 Imports: 14 Imported by: 1 module, 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 state locates sind's per-user state on the host and holds the realm lock, which serializes the operations that change a realm.

sind keeps its state in $XDG_STATE_HOME/sind, or ~/.local/state/sind: the MCP stream's generated token (mcp-token) and, per realm, the lock file and the exported SSH configuration. The realm itself (its mesh, DNS records, known_hosts and clusters) lives in the Docker daemon, and so does the second part of the realm lock, which serializes the daemon's clients (LockRealm).

Index

Constants

View Source
const ActivityLogName = "activity.jsonl"

ActivityLogName is the name of the activity log, the event log that the commands of sind that report progress append to when no flag or variable names another, in sind's state directory.

Variables

This section is empty.

Functions

func ActivityLogPath added in v0.13.0

func ActivityLogPath() (string, error)

ActivityLogPath returns the path of the activity log, Dir()/activity.jsonl.

func Dir

func Dir() (string, error)

Dir returns sind's state directory, $XDG_STATE_HOME/sind, falling back to ~/.local/state/sind. A relative XDG_STATE_HOME is ignored, as the XDG Base Directory specification requires; the state would otherwise move with the working directory. Adapted from GSI-HPC/clusterctl internal/config/paths.go.

func LockNetworkName

func LockNetworkName(realm string) docker.NetworkName

LockNetworkName returns the name of realm's lock on the Docker daemon, <realm>-lock: a configuration-only network that exists while a sind command changes the realm.

func LockRealm

func LockRealm(ctx context.Context, realm string, opts *LockOptions) (unlock func(), err error)

LockRealm takes the exclusive lock of realm and returns the function that releases it, which the caller must call when its operation ends, failed or not.

cluster.Create, cluster.Delete, cluster.DeleteAll, cluster.WorkerAdd, cluster.WorkerRemove, cluster.Connect, cluster.Disconnect, cluster.PowerOn, cluster.PowerReboot and cluster.PowerCycle change state that all clusters of a realm share: the mesh, its DNS records and known_hosts, the networks that its external containers join, and a cluster's Slurm node list. They take no lock themselves. A caller holds the realm lock from before mesh.Manager.EnsureMesh until the call returns, as every sind command that changes a realm does; concurrent calls in one realm otherwise lose each other's DNS records, host keys and node definitions, or remove each other's resources, and a cluster.Create can leave a container that cluster.Connect attaches meanwhile off its new cluster network, or put one that cluster.Disconnect detaches on it.

The lock has two parts. The first is a flock(2) on the file "lock" in the realm's state directory. It serializes the callers that share that directory, the goroutines of one process as much as sind commands, without a call to the daemon. With opts.Client, LockRealm then takes the realm's lock on that Docker daemon, the configuration-only network LockNetworkName(realm), which serializes all clients of the daemon: other users, CI jobs that share the host's Docker socket, processes with another XDG_STATE_HOME. Without opts.Client, such clients are not serialized against each other.

LockRealm tries each part without waiting first. When another process holds the file lock, it calls opts.OnWait and blocks until the lock is free. When another client holds the daemon lock, it calls opts.OnWait with the holder and tries again, at intervals that grow to two seconds; clients take a freed lock in no particular order. A holder that runs on this host, in this PID namespace and since this boot, but no longer exists, was killed before it could release the lock: LockRealm removes its lock and says so through opts.OnWarning. Whether a holder elsewhere still runs it cannot tell, so it leaves that lock alone, and after a minute names the command that removes it, through opts.OnWarning. The waits have no timeout; when ctx ends, LockRealm returns ctx.Err().

The returned function removes the daemon lock, without ctx, so that it works after Ctrl+C as well, and then releases the file lock.

func PrepareActivityDir added in v0.13.0

func PrepareActivityDir() (string, error)

PrepareActivityDir creates the state directory, readable by its owner alone (mode 0700), and returns the path of the activity log in it, which it leaves as it is: go-clikit's cliprogress moves it aside and checks it as it opens it (Options.DefaultLog). It fails when the directory cannot be made, as in a read-only home.

func PrepareActivityLog added in v0.13.0

func PrepareActivityLog() (string, error)

PrepareActivityLog makes the activity log ready for a command to append to, and returns its path: it creates the state directory, readable by its owner alone (mode 0700), moves a log larger than 8 MiB aside to activity.jsonl.1, replacing the copy before, and creates the log, in the mode 0600, if it is not there. It fails, and the caller goes on without a log, when the directory or the file cannot be used: a read-only home, or a file that is a link, is another user's or can be read or written by others than its owner. The log is a convenience, so no caller makes a command fail for the error.

func RealmDir

func RealmDir(realm string) (string, error)

RealmDir returns the state directory of realm, Dir()/<realm>, which holds the realm's lock file and SSH configuration export.

Types

type LockHolder

type LockHolder struct {
	// Command is the holder's command line, e.g. "sind create cluster dev".
	Command string
	// Host is the host name of the machine, or container, it runs on.
	Host string
	// PID is its process ID there, 0 when unknown.
	PID int
	// Since is when it took the lock, by the daemon's clock.
	Since time.Time
	// contains filtered or unexported fields
}

LockHolder is the process that holds a realm's lock on the Docker daemon, as the lock's labels describe it.

func Holder added in v0.13.0

func Holder(ctx context.Context, client *docker.Client, realm string) (*LockHolder, error)

Holder returns the process that holds realm's lock on the Docker daemon of client, as the lock's labels describe it, or nil, with no error, when nobody holds it: it is how a command that does not take the lock, such as a display of what a realm is busy with, tells that a sind command is changing the realm, and which. It reads the daemon part of LockRealm's lock alone, the one every client of the daemon shares; a process that waits for the file lock of a state directory holds nothing yet. The holder of a network named <realm>-lock that no sind command made has no command and no host, which LockHolder.String words as unknown.

func (*LockHolder) String

func (h *LockHolder) String() string

String describes the holder for a message: its command, process and host, and since when it holds the lock, in local time.

type LockOptions

type LockOptions struct {
	// Dir is the directory that holds the lock file; RealmDir(realm) when
	// empty.
	Dir string
	// Client, when set, is the Docker daemon on which LockRealm also takes
	// the realm's lock, after the file lock. Every caller that changes a
	// realm sets it: the realm lives on the daemon, which clients with
	// other state directories share.
	Client *docker.Client
	// Command is the command line that the daemon lock records for the
	// wait notices of other clients; the process's arguments when empty.
	Command string
	// OnWait, when set, is called once before LockRealm waits for each of
	// the two locks: with a nil holder for the file lock, whose holder it
	// does not know, and with the holder of the daemon lock. LockRealm logs
	// at info level instead when it is nil.
	OnWait func(holder *LockHolder)
	// OnWarning, when set, receives the warnings meant for the user: a
	// daemon lock whose holder no longer runs, which LockRealm removes; a
	// holder that LockRealm cannot check and that keeps the lock for long,
	// with the command that removes the lock; and a lock that the unlock
	// function could not remove. LockRealm reports them through the
	// context (sindlog.Warn) instead when it is nil.
	OnWarning func(msg string)
	// contains filtered or unexported fields
}

LockOptions configures LockRealm.

Jump to

Keyboard shortcuts

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