Documentation
¶
Overview ¶
Package config loads the hlab configuration file and turns it into a resolved inventory.
Index ¶
- Constants
- func AddHost(path string, host inventory.Host) error
- func DefaultPath() string
- func EnsureExists(path string) (bool, error)
- func FactsPath() string
- func FindStale(hosts []inventory.Host, before time.Time) []inventory.Host
- func Home() string
- func InterfaceKindFor(h inventory.Host, address string) string
- func ManagementKeyPath() string
- func MoveHost(path, name, address string) (absorbed string, err error)
- func RememberIgnored(path, endpoint string) error
- func RemoveHosts(path string, names []string) error
- func RenameHost(path, oldName, newName string) error
- func SearchPaths() []string
- func SetHostAddress(path, name, address string) error
- func UnmanageHost(path, name string) ([]string, error)
- func UpdateHostAttributes(path string, updates []HostAttributes, now time.Time) (int, error)
- func ValidateHostName(name string) error
- func WiredAddressOf(h inventory.Host) string
- type AssetChange
- type ChangeKind
- type Config
- type Defaults
- type DuplicateGroup
- type Duration
- type File
- type HostAttributes
- type LegacyDiscoveredHost
- type Observation
- type ScanDiff
Constants ¶
const EnvConfigPath = "HLAB_CONFIG"
EnvConfigPath overrides the config file location.
const EnvHome = "HLAB_HOME"
EnvHome overrides the directory hlab stores everything in.
Variables ¶
This section is empty.
Functions ¶
func AddHost ¶
AddHost records a host in the config, creating the file if it does not exist.
It is an upsert keyed on address and port: a machine a scan already recorded as unmanaged is promoted in place rather than duplicated, which is exactly what enrolment needs. Observed fields and the original FirstSeen survive the promotion.
The existing document is edited through a yaml.Node tree rather than re-marshalled from structs, so comments and unrelated settings survive. The previous contents are copied to <path>.bak first, and the write is atomic.
func DefaultPath ¶
func DefaultPath() string
DefaultPath is where `hlab config init` writes a new config.
func EnsureExists ¶
EnsureExists creates the config file if it is missing, so no command has to be preceded by `hlab config init`. It reports whether it created one.
An existing file is never touched.
func FactsPath ¶
func FactsPath() string
FactsPath is where collected host state is stored, alongside the config.
func FindStale ¶
FindStale returns unmanaged records not seen since the given time.
Managed machines are never stale: hlab holds credentials for them, and a machine being switched off is not a reason to forget how to reach it.
func Home ¶
func Home() string
Home returns the one directory hlab owns: everything it stores — the config, its management key, and any future state — lives here.
It deliberately sits under the user's home directory rather than the working directory, so running hlab inside a repository never leaves an inventory or a private key somewhere that could be committed.
Override it with $HLAB_HOME.
func InterfaceKindFor ¶
InterfaceKindFor reports the kind of interface a given address sits on, or "" if the machine has no interface holding it.
func ManagementKeyPath ¶
func ManagementKeyPath() string
ManagementKeyPath is where hlab keeps the private key it installs on managed machines.
This is deliberately independent of where the config file lives: an explicit --config pointing into a repository must not drag the key in after it.
func MoveHost ¶
MoveHost repoints a record at the address its machine now answers on.
A scan that ran after the move will have recorded the new address as an unmanaged machine of its own. The caller has proved the machine there is this one, so that record is a duplicate and is absorbed; its name is returned so the caller can say so. A *managed* record at the new address is a genuine conflict — two credentials claiming one endpoint — and is refused rather than resolved by guessing.
func RememberIgnored ¶
RememberIgnored records an SSH endpoint that should be skipped during onboarding.
func RemoveHosts ¶
RemoveHosts deletes records by name, along with any group membership.
func RenameHost ¶
RenameHost changes a machine's recorded name, leaving everything else about the record — and the rest of the file — untouched.
Group membership is updated alongside it, so a rename cannot leave a group pointing at a machine that no longer exists.
func SearchPaths ¶
func SearchPaths() []string
SearchPaths lists, in priority order, where hlab looks for its config when none is given explicitly.
The working directory is not searched. A per-project inventory is still perfectly possible — point at it with --config or $HLAB_CONFIG — but it is never picked up implicitly, so hlab cannot silently adopt (or write beside) a file that happens to be in a checkout.
func SetHostAddress ¶
SetHostAddress changes the address a machine is reached at, leaving the rest of the record alone.
func UnmanageHost ¶
UnmanageHost turns a managed record back into an observed one: the machine stays in the inventory as a known device, but hlab no longer holds a way in.
The user and identity file go with it. They described access that has just been revoked, and leaving them would make the record claim a credential that no longer works — and make re-enrolling look like it had nothing to do. Everything observed about the machine is kept.
Group memberships are dropped, returning the groups it was removed from: a group lists machines to act on, and an unmanaged member could only ever be reported as skipped.
func UpdateHostAttributes ¶
UpdateHostAttributes writes collected attributes onto the matching host records, leaving names, credentials, tags and everything else untouched.
Hosts with no matching update keep whatever they already had.
func ValidateHostName ¶
ValidateHostName rejects names that a selector could not address.
func WiredAddressOf ¶
WiredAddressOf returns an IPv4 address on a working ethernet interface, for suggesting where a record should point instead of a wireless address.
Types ¶
type AssetChange ¶
type AssetChange struct {
Name string `json:"name" yaml:"name"`
Address string `json:"address" yaml:"address"`
Kind ChangeKind `json:"kind" yaml:"kind"`
// Changes describes each differing field, e.g. "ssh: OpenSSH_9.6p1 -> OpenSSH_10.0p2".
Changes []string `json:"changes,omitempty" yaml:"changes,omitempty"`
}
AssetChange describes what one scan observation did to the stored record.
type ChangeKind ¶
type ChangeKind string
ChangeKind classifies what a scan did to a record.
const ( ChangeAdded ChangeKind = "new" ChangeUpdated ChangeKind = "changed" ChangeUnchanged ChangeKind = "unchanged" )
type Config ¶
type Config struct {
Ignored []string
// Path is where the config was read from, or "" if defaults were used.
Path string
Defaults Defaults
Inventory *inventory.Inventory
Endpoints map[string][]health.Endpoint
}
Config is a loaded, validated configuration.
type Defaults ¶
type Defaults struct {
User string `yaml:"user"`
Port int `yaml:"port"`
IdentityFile string `yaml:"identityFile"`
KnownHostsFile string `yaml:"knownHostsFile"`
ConnectTimeout Duration `yaml:"connectTimeout"`
Concurrency int `yaml:"concurrency"`
}
Defaults are applied to every host that does not override them.
type DuplicateGroup ¶
type DuplicateGroup struct {
// Keep is the record worth keeping; Remove are the others.
Keep inventory.Host
Remove []inventory.Host
// Reason says what proved they are the same machine.
Reason string
}
DuplicateGroup is a set of records that turned out to be one machine.
func FindDuplicates ¶
func FindDuplicates(hosts []inventory.Host) []DuplicateGroup
FindDuplicates groups records that describe the same physical machine.
A machine with both ethernet and wireless online has two addresses, so a network scan sees it twice and records it twice. The MAC addresses on each record's interfaces settle it: the same hardware cannot appear under two identities, whatever addresses it currently holds.
Reverse DNS and hostnames are deliberately not used on their own — two machines can share a badly configured name, and that is how the wrong records would get merged.
type File ¶
type File struct {
Ignored []string `yaml:"ignored,omitempty"`
Endpoints map[string][]health.Endpoint `yaml:"endpoints,omitempty"`
Defaults Defaults `yaml:"defaults"`
Hosts []hostEntry `yaml:"hosts"`
Groups map[string][]string `yaml:"groups"`
// Printerlab is the printer inventory read by `hlab printer`. It is held
// opaquely: the printer loader owns its schema and its strictness, and a
// mistake in it must not stop `hlab run` from reaching a server.
Printerlab yaml.Node `yaml:"printerlab"`
// Discovered is a section written by earlier builds, when scan results
// lived separately from the inventory. It is still read so those configs
// keep working, and is folded into Hosts on the next write. Nothing
// produces it any more.
Discovered []LegacyDiscoveredHost `yaml:"discovered"`
}
File is the on-disk shape of the config file.
type HostAttributes ¶
type HostAttributes struct {
Name string
// Hostname is what the machine calls itself. It supersedes anything a scan
// inferred from reverse DNS, which can be wrong or shared: a DNS server
// answering PTR queries for its whole subnet will hand the same name to
// every address on it.
Hostname string
Kernel string
Arch string
CPUs int
MemoryMB int64
Network *inventory.NetworkInfo
OS string
// Containers is applied wholesale, including when empty, so a container
// that has been removed disappears from the record rather than lingering.
Containers []inventory.Container
}
HostAttributes are the durable properties of a machine, learned by collection. They live on the host record because they describe the asset itself rather than what it happens to be doing.
type LegacyDiscoveredHost ¶
type LegacyDiscoveredHost struct {
Address string `yaml:"address"`
Port int `yaml:"port,omitempty"`
Hostname string `yaml:"hostname,omitempty"`
OS string `yaml:"os,omitempty"`
Banner string `yaml:"banner,omitempty"`
FirstSeen time.Time `yaml:"firstSeen,omitempty"`
LastSeen time.Time `yaml:"lastSeen,omitempty"`
}
LegacyDiscoveredHost is an entry from the retired `discovered:` section.
type Observation ¶
type Observation struct {
Address string
Port int
Hostname string
OS string
Banner string
// MatchName, when set, names the existing record this observation belongs
// to. The caller supplies it when a host is recorded under a DNS name that
// resolves to this address, which address matching alone would miss.
MatchName string
}
Observation is what a scan saw at one address.
type ScanDiff ¶
type ScanDiff struct {
Added int
Updated int
Unchanged int
// ByAddress indexes the per-machine outcome so results can be annotated.
ByAddress map[string]AssetChange
}
ScanDiff summarises what a scan changed.
func MergeObservations ¶
MergeObservations folds scan results into the config's asset list.
Machines already recorded are enriched in place: observed fields are refreshed and LastSeen advanced, while everything that makes a machine manageable — its name, credentials and tags — is left alone. A scan can never turn a managed machine back into an unmanaged one.
Machines not seen before are appended as unmanaged records.