vmsync

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 25 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 vmsync keeps a manifest's VM hosts in step with the hypervisors that run them: `authama ssh vm sync`, the VM half of `authama doctor --fix` and the web UI's VM page.

For every VM host it asks the hypervisor what became of the VM, and plans:

  • a running VM: pin its host key under the alias when nothing is pinned (replace a changed pin only when asked to), re-install the host's public key in the guest when the guest refuses it, and create or refresh the Docker context ssh://<alias>;
  • a VM the hypervisor no longer has: remove the host entry (never its key files) and the Docker context authama made for it;
  • a suspended or stopped VM: report it, and leave it alone - sync never starts, stops or deletes a VM.

It also adds the devboxes authama.json declares that are not hosts yet (adopting a declared loose key by copying it) and removes Docker contexts authama made whose host is gone from the manifest.

A plan is computed without writing anything, so a dry run shows exactly what a run does; a run applied twice changes nothing the second time. Nothing here ever reads a private key: the guest gets the .pub.

Index

Constants

View Source
const (
	ProbeOK          = "ok"          // the guest accepted the key
	ProbeKeyRefused  = "key-refused" // the guest answered and refused every key
	ProbeUnreachable = "unreachable" // anything else: timeout, host key, proxy
)

Probe outcomes.

View Source
const (
	ActPin          = "pin"           // pin the guest's host key under the alias
	ActRepin        = "repin"         // replace a changed pin (opt-in)
	ActInstallKey   = "install-key"   // append the host's .pub to the guest's authorized_keys
	ActDockerCreate = "docker-create" // docker context create <alias>
	ActDockerUpdate = "docker-update" // docker context update <alias>
	ActDockerRemove = "docker-remove" // docker context rm <alias> (backed up first)
	ActRemoveHost   = "remove-host"   // drop a gone VM's host entry (keys stay)
	ActAddDevbox    = "add-devbox"    // add a devbox authama.json declares
	ActAdoptKey     = "adopt-key"     // copy a declared loose key into the profile
	ActUpdateDevbox = "update-devbox" // bring a host in line with its declaration
)

Action kinds.

Variables

This section is empty.

Functions

func AddVMHost

func AddVMHost(p paths.Paths, profile, alias, hv, vmName, user, keyName string) error

AddVMHost adds a VM host to profile (created per_service when missing), using keyName when set. It does not re-render the config.

func ReleaseReservation added in v0.1.6

func ReleaseReservation(P paths.Paths, src string) (bool, error)

ReleaseReservation drops the reservation of a loose key that has just been adopted from src (a path directly in ~/.ssh): the key now has a host, so the note that it is kept for later no longer applies. Reports whether one was dropped.

Types

type Action

type Action struct {
	Kind    string
	Profile string
	Alias   string
	Detail  string // the line a plan prints
	// contains filtered or unexported fields
}

Action is one step a sync takes.

func (Action) Outside

func (a Action) Outside() bool

Outside reports whether the action acts outside the manifest and config home (the guest, known_hosts, Docker).

type AdoptResult

type AdoptResult struct {
	Dest     string
	Recorded bool
	Moved    bool
}

AdoptResult is what AdoptKey did.

func AdoptKey

func AdoptKey(P paths.Paths, src, profile, keyName string, move bool) (AdoptResult, error)

AdoptKey copies the loose keypair src (and src.pub) into profile as keyName, records it in the inventory, and - only when move is set and the copy is verified byte-identical - removes the originals. It refuses to overwrite a key already in the profile.

type Checker added in v0.1.6

type Checker func(name, user string) dockerctx.Diagnosis

Checker asks the engine behind a context; dockerctx.Check is the real one.

type ContextStatus added in v0.1.6

type ContextStatus struct {
	Name     string `json:"name"`
	Endpoint string `json:"endpoint"`
	// Managed: authama made it (its description), or it points exactly
	// where an authama context for its name would.
	Managed bool `json:"managed"`
	Current bool `json:"current"`
	// Host is profile/alias of the VM host by that name, "" when none.
	Host string `json:"host,omitempty"`
	// VMState is the hypervisor's word for the host's VM (running, stopped,
	// gone, ...), "" when there is no host.
	VMState string `json:"vm_state,omitempty"`
	// Docker is the engine check; nil when it was not made (not asked for,
	// or the VM is not running: a check never starts a VM).
	Docker *dockerctx.Diagnosis `json:"docker,omitempty"`
	// Problem says why the context needs attention, "" when it does not;
	// Fix says what to do.
	Problem string `json:"problem,omitempty"`
	Fix     string `json:"fix,omitempty"`
}

ContextStatus is one Docker context that belongs to a VM host: whether it still points where it should, the VM's state, and (when asked) whether the engine behind it answers. doctor and the web UI's VMs page show these.

func ContextStatuses added in v0.1.6

func ContextStatuses(plan Plan, contexts []dockerctx.Context, check Checker) []ContextStatus

ContextStatuses lists the Docker contexts that belong to VM hosts: every one authama manages, and any other named after a VM host. Other contexts (default, orbstack, desktop-linux, ...) are not authama's and are left out. With check, the engine behind each context of a running VM is asked; without it nothing connects to a guest.

type Deps

type Deps struct {
	Driver  func(name string) (hypervisor.Driver, error)
	Enabled func(name string) bool
	Scan    func(sshDir, host string, port int) ([]knownhosts.ScannedKey, error)
	Probe   func(sshDir, alias string, port int, scanned []knownhosts.ScannedKey) ProbeResult
	Docker  DockerOps
	// CloseShared ends a host's shared connection (vm.multiplex) before its
	// pin is replaced or its entry removed, so no master keeps talking to the
	// old guest. nil does nothing.
	CloseShared func(sshDir, alias, user string, port int)
}

Deps are the outside world, swappable in tests.

func RealDeps

func RealDeps() Deps

RealDeps is the real hypervisors, ssh and docker.

type DockerOps

type DockerOps interface {
	List() ([]dockerctx.Context, error)
	Apply(alias string) (string, error)
	Remove(c dockerctx.Context, backupDir string) (string, error)
}

DockerOps is the docker CLI.

type Host

type Host struct {
	Profile    string `json:"profile"`
	Alias      string `json:"alias"`
	Hypervisor string `json:"hypervisor"`
	VM         string `json:"vm"`
	// State: running | stopped | suspended | ... (the hypervisor's word) |
	// gone | unknown | disabled | not-installed.
	State string `json:"state"`
	Note  string `json:"note,omitempty"`
	// Declared is set when authama.json's devboxes section declares the host.
	Declared bool `json:"declared,omitempty"`
	// Running is set when the hypervisor says the VM runs (State is its own
	// word for it, which differs between hypervisors).
	Running bool `json:"running,omitempty"`
	// User is the guest user the host connects as.
	User string `json:"user,omitempty"`
	// Docker is what the probe found about the guest's Docker engine; nil
	// when the guest was not probed (not running, or a plan without probes).
	Docker *dockerctx.Diagnosis `json:"docker,omitempty"`
}

Host is one VM host as the hypervisor sees it now.

type Options

type Options struct {
	// Probe connects to running guests (ssh-keyscan and one BatchMode ssh)
	// to check the host key and the key. Off, the plan is the hypervisor's
	// view only - what doctor shows without --fix.
	Probe bool
	// Repin replaces a VM host's pin when the guest's host key changed.
	Repin bool
	// PruneGone removes the host of a VM the hypervisor no longer has.
	PruneGone bool
	// AutoPin pins an unpinned running VM host (ssh.auto_pin).
	AutoPin bool
	// Docker manages Docker contexts (off when the docker CLI is missing).
	Docker bool
}

Options steer a sync.

func DefaultOptions

func DefaultOptions(getenv func(string) string) Options

DefaultOptions reads the settings: vm.prune_gone, vm.repin_changed, ssh.auto_pin, and whether docker is installed.

type Plan

type Plan struct {
	Hosts   []Host   `json:"hosts"`
	Actions []Action `json:"-"`
	Notes   []string `json:"notes,omitempty"`
}

Plan is everything a sync would do, and why.

func (Plan) Empty

func (p Plan) Empty() bool

Empty reports whether the plan changes nothing.

func (Plan) Lines

func (p Plan) Lines() []string

Lines are the plan's actions as printable lines.

func (Plan) ManifestChanges

func (p Plan) ManifestChanges() bool

ManifestChanges reports whether the plan edits the manifest.

type ProbeResult

type ProbeResult struct {
	Status string
	Docker bool   // the guest has a docker command
	Detail string // why, when not ok
	// Daemon is whether the guest user reaches the Docker engine: a docker
	// command alone is not enough (the user may not be in the docker group,
	// or the engine may be stopped). Empty State when the probe did not run.
	Daemon dockerctx.Diagnosis
}

ProbeResult is what one BatchMode ssh to the guest found.

type Syncer

type Syncer struct {
	P    paths.Paths
	Cfg  settings.VMConfig
	Opt  Options
	Deps Deps
}

Syncer plans and applies a sync.

func New

func New(p paths.Paths, cfg settings.VMConfig, opt Options) *Syncer

New builds a Syncer over the real world.

func (*Syncer) ApplyManifest

func (s *Syncer) ApplyManifest(plan Plan, out io.Writer) (int, error)

ApplyManifest makes the plan's manifest steps: removing gone hosts, adding and updating declared devboxes, adopting declared keys. The caller holds the lock, has snapshotted ~/.ssh and backed the manifest up, and re-renders the config afterwards.

func (*Syncer) ApplyOutside

func (s *Syncer) ApplyOutside(plan Plan, out io.Writer) (int, error)

ApplyOutside makes the plan's steps outside the manifest: pins, key installs, Docker contexts. Each is independent; a failure is reported and the rest still run.

func (*Syncer) BackupDir

func (s *Syncer) BackupDir() string

BackupDir is where sync keeps what it backs up before removing: the manifest and inventory, and exported Docker contexts.

func (*Syncer) BackupManifest

func (s *Syncer) BackupManifest() (string, error)

BackupManifest copies manifest.json and inventory.json into BackupDir and returns the manifest copy's path.

func (*Syncer) Plan

func (s *Syncer) Plan() (Plan, error)

Plan works out what a sync would do. It writes nothing.

Jump to

Keyboard shortcuts

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