vmsync

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT 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 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.

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 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
}

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"`
}

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
}

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