hosts

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package hosts defines the host search record model and pluggable cloud backends.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheKeySHA256

func CacheKeySHA256(providerID string, payload []byte) string

CacheKeySHA256 builds a stable cache key from provider id and payload.

func DefaultCacheDir

func DefaultCacheDir() (string, error)

DefaultCacheDir returns XDG-style cache directory for honey.

func DefaultCacheKey

func DefaultCacheKey(p Backend, q Query) ([]byte, error)

DefaultCacheKey serializes query fields for caching per provider instance.

func MetaSSHIdentityFile added in v0.2.9

func MetaSSHIdentityFile(r *Record) (path string, ok bool)

MetaSSHIdentityFile returns the private key path from r.Meta["ssh_identity_file"] when set (recipe-level ssh_private_key applied by cue-exec; not inventory search meta).

func MetaSSHPort added in v0.2.9

func MetaSSHPort(r *Record) (port int, ok bool)

MetaSSHPort returns the TCP port from r.Meta["ssh_port"] when it is a valid decimal integer in 1..65535.

func ParseBackendNames

func ParseBackendNames(s string) []string

ParseBackendNames splits a comma-separated list of backend names (trimmed, lowercased). Empty input returns nil.

func ParseProviders

func ParseProviders(s string) []string

ParseProviders splits comma list; empty means all.

Types

type Backend

type Backend interface {
	ID() string
	// BackendName is an optional config label (YAML backends.*.name) used with --backends.
	// Empty for unnamed backends (e.g. default flag-only quartet).
	BackendName() string
	// CacheIdentity distinguishes multiple instances with the same ID() (e.g. two GCP projects).
	// May be empty when a single implicit backend is used.
	CacheIdentity() string
	Search(ctx context.Context, q Query) ([]Record, error)
}

Backend is implemented by each cloud integration.

func FilterBackendsByNames

func FilterBackendsByNames(provs []Backend, want []string) []Backend

FilterBackendsByNames keeps backends matching any token in want (case-insensitive). Tokens without ":" match BackendName() across all kinds (legacy --backends / URL behavior). Tokens with "kind:name" match a single YAML backend kind and name (e.g. truenas:prod, kubernetes:prod). Unnamed backends (BackendName() empty) are excluded when want is non-empty. When want is empty, provs is returned unchanged.

type CacheEntry

type CacheEntry struct {
	StoredAt time.Time `json:"stored_at"`
	Records  []Record  `json:"records"`
}

CacheEntry is a single cached provider result.

type FileCache

type FileCache struct {
	// contains filtered or unexported fields
}

FileCache is a simple JSON file cache with TTL.

func NewFileCache

func NewFileCache(path string, ttl time.Duration) *FileCache

NewFileCache creates a JSON-backed cache at path with ttl.

func (*FileCache) Get

func (c *FileCache) Get(cacheKey string) ([]Record, bool, error)

Get returns records and true if fresh.

func (*FileCache) Set

func (c *FileCache) Set(cacheKey string, records []Record) error

Set stores records for key.

type InventoryValue added in v0.3.6

type InventoryValue struct {
	// contains filtered or unexported fields
}

InventoryValue is a scalar inventory variable value.

func MustInventoryValue added in v0.3.6

func MustInventoryValue(v any) InventoryValue

MustInventoryValue is for tests and static literals.

func NewInventoryValue added in v0.3.6

func NewInventoryValue(v any) (InventoryValue, error)

NewInventoryValue returns a scalar inventory value or an error for maps, lists, nil, or unsupported types.

func (InventoryValue) Any added in v0.3.6

func (v InventoryValue) Any() any

Any returns the underlying scalar value for CEL and JSON contexts.

func (InventoryValue) Bool added in v0.3.6

func (v InventoryValue) Bool() bool

Bool returns the underlying bool or false for non-bool values.

func (InventoryValue) IsSet added in v0.3.6

func (v InventoryValue) IsSet() bool

IsSet reports whether the value was decoded from a supported scalar.

func (InventoryValue) MarshalJSON added in v0.3.6

func (v InventoryValue) MarshalJSON() ([]byte, error)

MarshalJSON writes the scalar value for host JSON output.

func (InventoryValue) String added in v0.3.6

func (v InventoryValue) String() string

String returns a stable string form for env export.

func (*InventoryValue) UnmarshalYAML added in v0.3.6

func (v *InventoryValue) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML rejects non-scalar YAML inventory variable values.

type Query

type Query struct {
	NameSubstring string
	NameRegex     string
	Providers     []string // e.g. gcp, aws, k8s, consul — empty means all
	Backends      []string // e.g. aws:dev, gcp:prod — empty means all
}

Query carries global search filters for a search run.

func (Query) MatchesName added in v0.3.6

func (q Query) MatchesName(name string) (bool, error)

MatchesName applies NameSubstring, NameRegex, or accepts all if both empty.

type Record

type Record struct {
	Provider     string                    `json:"provider"`
	Name         string                    `json:"name"`
	PrimaryIP    string                    `json:"primary_ip"`
	ExtraIPs     []string                  `json:"extra_ips,omitempty"`
	Zone         string                    `json:"zone,omitempty"`
	Region       string                    `json:"region,omitempty"`
	Meta         map[string]string         `json:"meta,omitempty"`
	Vars         map[string]InventoryValue `json:"vars,omitempty"`
	Groups       []string                  `json:"groups,omitempty"`
	Capabilities []string                  `json:"capabilities,omitempty"`
}

Record is a normalized host across cloud providers.

func CloneWithMetaSSHIdentityFile added in v0.2.9

func CloneWithMetaSSHIdentityFile(r Record, path string) Record

CloneWithMetaSSHIdentityFile returns a shallow copy of r with meta["ssh_identity_file"] set.

func CloneWithMetaSSHPort added in v0.2.9

func CloneWithMetaSSHPort(r Record, port int) Record

CloneWithMetaSSHPort returns a shallow copy of r with meta["ssh_port"] set to the decimal string for port (used by cue-exec to apply recipe-level ports).

func RunParallel

func RunParallel(
	ctx context.Context,
	q Query,
	provs []Backend,
	fc *FileCache,
	noCache bool,
	refresh bool,
	cacheKeyFn func(p Backend, q Query) ([]byte, error),
) ([]Record, error)

RunParallel executes enabled providers in parallel with optional disk cache.

func (Record) DedupeKey added in v0.3.6

func (r Record) DedupeKey() string

DedupeKey returns a stable key for deduplication.

func (Record) DeriveCapabilities added in v0.3.6

func (r Record) DeriveCapabilities() []string

DeriveCapabilities returns connection protocols available for this host, derived from provider and meta. Order is stable: ssh, docker_exec, truenas_api.

func (Record) ExternalIP added in v0.3.6

func (r Record) ExternalIP() string

ExternalIP returns the VM's public/out-of-VPC address when present. GCP and AWS store it in ExtraIPs while PrimaryIP is the private address used for SSH.

func (Record) IsConnectable added in v0.3.6

func (r Record) IsConnectable() bool

IsConnectable reports whether honey can exec, upload, or open a terminal on r.

func (Record) IsDocker added in v0.3.6

func (r Record) IsDocker() bool

IsDocker reports whether r is a connectable Docker container or swarm task row.

func (Record) IsTrueNASAPIShell added in v0.3.6

func (r Record) IsTrueNASAPIShell() bool

IsTrueNASAPIShell reports whether r is a TrueNAS row that can use /websocket/shell (shape only; backend config is checked at runtime).

func (Record) NodeDisplayIP added in v0.3.6

func (r Record) NodeDisplayIP() string

NodeDisplayIP prefers ExternalIP for UI tables; falls back to PrimaryIP.

func (Record) PrimaryIPTrimmed added in v0.3.6

func (r Record) PrimaryIPTrimmed() string

PrimaryIPTrimmed returns r.PrimaryIP with surrounding whitespace removed.

type RecordSet added in v0.3.6

type RecordSet []Record

RecordSet is a collection of records.

func (RecordSet) MergeDedupe added in v0.3.6

func (rs RecordSet) MergeDedupe(others ...RecordSet) RecordSet

MergeDedupe merges multiple slices into this RecordSet and removes exact duplicates by DedupeKey.

Jump to

Keyboard shortcuts

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