daemon

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 21 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 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

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

View Source
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

func Addresses(sys System) []string

Addresses are the addresses of the host that can say where it is: not loopback, not link-local, not multicast.

func DefaultStatePath

func DefaultStatePath() (string, error)

DefaultStatePath is where the state lives: /var/lib/dym for root (the service), the user's config otherwise.

func ParseCPUModel

func ParseCPUModel(text string) string

ParseCPUModel reads the model of the CPU from /proc/cpuinfo (x86 says "model name", Arm says "Model" or "Processor").

func ParseMemTotal

func ParseMemTotal(text string) int64

ParseMemTotal reads MemTotal from /proc/meminfo, which is in KiB, and gives bytes.

func ParseOSRelease

func ParseOSRelease(text string) (name, version string)

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.

func SaveState

func SaveState(path string, s State) error

SaveState writes the state so that nobody else can read it, and so that a crash never leaves half of it: it is written aside and renamed.

func SocketURL

func SocketURL(server string) (string, error)

SocketURL turns the address of Dymmer (https://dymmer.com) into the websocket endpoint of its daemon socket. Plain http is accepted for a Dymmer that runs on this machine or inside a private network.

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

func Dial(ctx context.Context, url string, header http.Header) (*Conn, error)

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) Close

func (c *Conn) Close()

Close ends the connection.

func (*Conn) Done

func (c *Conn) Done() <-chan struct{}

Done is closed when the connection has ended.

func (*Conn) Err

func (c *Conn) Err() error

Err says why the connection ended, once Done is closed.

func (*Conn) Events

func (c *Conn) Events() <-chan Event

Events delivers what the server pushes on the topics that were joined. It is closed when the connection ends.

func (*Conn) Join

func (c *Conn) Join(ctx context.Context, topic string, payload map[string]any) (map[string]any, error)

Join asks the server to join a topic and returns what it answered.

func (*Conn) Push

func (c *Conn) Push(ctx context.Context, topic, event string, payload map[string]any) (map[string]any, error)

Push sends a message on a joined topic and waits for the answer of the server.

type DialError

type DialError struct {
	Status int
	Err    error
}

DialError is a handshake that the server rejected (a wrong secret, a deleted host, too many attempts).

func (*DialError) Error

func (e *DialError) Error() string

func (*DialError) Unwrap

func (e *DialError) Unwrap() error

type Event

type Event struct {
	Topic   string
	Name    string
	Payload map[string]any
}

Event is a message that the server pushed on a topic that was joined.

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.

func Collect

func Collect(sys System) Inventory

Collect reads the inventory. A piece that cannot be read is left out: the daemon is useful with what it has.

func (Inventory) Payload

func (i Inventory) Payload() map[string]any

Payload is the inventory as the map that goes in a channel message.

type JoinError

type JoinError struct {
	Topic  string
	Reason string
}

JoinError is the answer of a server that refused to let the client join a topic.

func (*JoinError) Error

func (e *JoinError) Error() string

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

type State struct {
	HostID string `json:"host_id"`
	Secret string `json:"secret"`
}

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 LoadState

func LoadState(path string) (State, error)

LoadState reads the state, or ErrNotPaired when there is none.

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.

type System

type System struct {
	GOOS, GOARCH string
	NumCPU       int
	Hostname     func() (string, error)
	Addrs        func() ([]net.Addr, error)
	ReadFile     func(path string) ([]byte, error)
	Sysctl       func(name string) (string, error)
}

System is what Collect reads from the machine; tests give their own.

func HostSystem

func HostSystem() System

HostSystem is the machine the daemon runs on.

Jump to

Keyboard shortcuts

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