Documentation
¶
Overview ¶
Package daemon is the part of dym that runs on a host and reaches Dymmer by itself, instead of Dymmer reaching the host over SSH: it pairs with a one-time PIN, keeps one connection open, and tells Dymmer that the host is alive and what it is. It accepts no command from Dymmer.
Index ¶
- Variables
- func Addresses(sys System) []string
- func DefaultStatePath() (string, error)
- func ParseCPUModel(text string) string
- func ParseMemTotal(text string) int64
- func ParseOSRelease(text string) (name, version string)
- func Run(ctx context.Context, o RunOptions) error
- func SaveState(path string, s State) error
- func SocketURL(server string) (string, error)
- type Conn
- func (c *Conn) Close()
- func (c *Conn) Done() <-chan struct{}
- func (c *Conn) Err() error
- func (c *Conn) Events() <-chan Event
- func (c *Conn) Join(ctx context.Context, topic string, payload map[string]any) (map[string]any, error)
- func (c *Conn) Push(ctx context.Context, topic, event string, payload map[string]any) (map[string]any, error)
- type DialError
- type Event
- type Inventory
- type JoinError
- type PairOptions
- type RunOptions
- type State
- type System
Constants ¶
This section is empty.
Variables ¶
var ErrNotPaired = errors.New("this host is not paired yet: run `dym auth daemon`")
ErrNotPaired is returned when there is no state yet: `dym auth daemon` has not been run on this host.
var ErrPairingExpired = errors.New("the PIN expired before it was entered in Dymmer: run `dym auth daemon` again")
ErrPairingExpired is the end of a PIN that nobody entered in Dymmer in time.
var HeartbeatEvery = 30 * time.Second
HeartbeatEvery is how often the heartbeat is sent. The server of Dymmer closes a connection that is silent for a minute, so this must stay well under that.
Functions ¶
func Addresses ¶
Addresses are the addresses of the host that can say where it is: not loopback, not link-local, not multicast.
func DefaultStatePath ¶
DefaultStatePath is where the state lives: /var/lib/dym for root (the service), the user's config otherwise.
func ParseCPUModel ¶
ParseCPUModel reads the model of the CPU from /proc/cpuinfo (x86 says "model name", Arm says "Model" or "Processor").
func ParseMemTotal ¶
ParseMemTotal reads MemTotal from /proc/meminfo, which is in KiB, and gives bytes.
func ParseOSRelease ¶
ParseOSRelease reads /etc/os-release and gives the ID (debian, ubuntu...) and the VERSION_ID.
func Run ¶
func Run(ctx context.Context, o RunOptions) error
Run keeps the host connected to Dymmer until ctx ends. While the host is not paired it only waits for the state that `dym auth daemon` leaves. A connection that fails or ends is retried with a wait that grows, so that a Dymmer that is down is not hammered by every host at once, and the wait starts again after a connection that held.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a connection to a Phoenix socket (serializer 2.0.0: a frame is [join_ref, ref, topic, event, payload]). It sends the heartbeat the server expects, answers the pushes of the topics joined with Events, and ends when the connection does: Done is closed and Err says why.
func Dial ¶
Dial opens the connection. `url` is the websocket endpoint without the query (the version is added here) and `header` carries the credentials, which must not go in the URL because proxies write it in their logs.
func (*Conn) Done ¶
func (c *Conn) Done() <-chan struct{}
Done is closed when the connection has ended.
func (*Conn) Events ¶
Events delivers what the server pushes on the topics that were joined. It is closed when the connection ends.
type DialError ¶
DialError is a handshake that the server rejected (a wrong secret, a deleted host, too many attempts).
type Inventory ¶
type Inventory struct {
Hostname string `json:"hostname,omitempty"`
IPAddresses []string `json:"ip_addresses,omitempty"`
OSName string `json:"os_name,omitempty"`
OSVersion string `json:"os_version,omitempty"`
CPUName string `json:"cpu_name,omitempty"`
CPUCores int `json:"cpu_cores,omitempty"`
Memory int64 `json:"memory,omitempty"` // bytes
}
Inventory is what the daemon tells Dymmer about its host. The names are the ones Dymmer stores.
type PairOptions ¶
type PairOptions struct {
// URL is the websocket endpoint of Dymmer's daemon socket.
URL string
// StatePath is where the host and its permanent secret are kept once Dymmer gives them.
StatePath string
// Inventory is what to tell Dymmer about this host, so that the host is created already filled in.
Inventory Inventory
// OnPIN is called as soon as Dymmer hands out the PIN, to show it to whoever is at the console.
OnPIN func(pin string, validFor time.Duration)
}
PairOptions says how to pair this host.
type RunOptions ¶
type RunOptions struct {
// URL is the websocket endpoint of Dymmer's daemon socket.
URL string
// StatePath is where the pairing is kept.
StatePath string
// Collect reads the inventory of the host; the default reads the machine.
Collect func() Inventory
// Logger receives what happens; the default discards it.
Logger *slog.Logger
// MinBackoff and MaxBackoff bound the wait between attempts to connect (1s and 60s by default).
MinBackoff, MaxBackoff time.Duration
// StableAfter is how long a connection has to last for the wait to start again from the minimum (60s).
StableAfter time.Duration
// InventoryEvery is how often the inventory is read again and sent if it changed (5 minutes).
InventoryEvery time.Duration
// PollPairing is how often it looks for the state while the host is not paired (5s).
PollPairing time.Duration
}
RunOptions says how the daemon runs.
type State ¶
State is what the daemon keeps after pairing: which host it is and the permanent secret that proves it. The secret lets anyone connect as this host, so the file is readable only by its owner.
func Pair ¶
func Pair(ctx context.Context, o PairOptions) (State, error)
Pair asks Dymmer for a PIN and waits, on the same connection, until somebody enters it in Dymmer. Dymmer then creates the host and sends its permanent secret down the open connection; that is saved before Pair returns, so the daemon can reconnect by itself from now on.