Documentation
¶
Overview ¶
Package actions provides remote action execution and hook management for plexd mesh nodes.
Index ¶
- Constants
- func DiscoverHooks(hooksDir string, logger *slog.Logger) ([]api.HookInfo, error)
- func HandleActionRequest(executor *Executor, nodeID string, logger *slog.Logger) api.EventHandler
- type ActionReporter
- type ArtifactFetcher
- type BuiltinFunc
- func ConfigDump(provider ConfigProvider) BuiltinFunc
- func DiagnosticsCollect() BuiltinFunc
- func DiagnosticsTraceroutePeer(info NodeInfoProvider) BuiltinFunc
- func GatherInfo(info NodeInfoProvider) BuiltinFunc
- func HealthCheck(health HealthProvider) BuiltinFunc
- func LogsSnapshot(provider LogProvider) BuiltinFunc
- func MeshReconnect(reconnector MeshReconnector) BuiltinFunc
- func PingPeer(info NodeInfoProvider) BuiltinFunc
- func ServiceReloadConfig() BuiltinFunc
- func ServiceRestart() BuiltinFunc
- func ServiceUpgrade(fetcher ArtifactFetcher) BuiltinFunc
- type Config
- type ConfigProvider
- type Executor
- func (e *Executor) ActiveCount() int
- func (e *Executor) Capabilities() ([]api.ActionInfo, []api.HookInfo)
- func (e *Executor) Execute(ctx context.Context, nodeID string, req api.ActionRequest)
- func (e *Executor) RegisterBuiltin(name, description string, params []api.ActionParam, fn BuiltinFunc)
- func (e *Executor) RunLocal(ctx context.Context, action string, params map[string]string) (string, string, int, error)
- func (e *Executor) SetHooks(hooks []api.HookInfo)
- func (e *Executor) Shutdown(_ context.Context)
- type HealthProvider
- type HookChangeCallback
- type HookEvent
- type HookVerifier
- type HookWatcher
- type IntegrityAlertCallback
- type LogProvider
- type MeshReconnector
- type NodeInfoProvider
Constants ¶
const DefaultHooksDir = "/etc/plexd/hooks"
DefaultHooksDir is the default directory for hook scripts.
const DefaultMaxActionTimeout = 10 * time.Minute
DefaultMaxActionTimeout is the default maximum duration for a single action.
const DefaultMaxConcurrent = 5
DefaultMaxConcurrent is the default maximum number of concurrent actions.
const DefaultMaxOutputBytes = 1 << 20
DefaultMaxOutputBytes is the default maximum output size per action (1 MiB).
const MaxSnapshotLines = 10000
MaxSnapshotLines is the maximum number of lines that logs.snapshot will return.
Variables ¶
This section is empty.
Functions ¶
func DiscoverHooks ¶
DiscoverHooks scans hooksDir for executable files and returns their metadata. Returns an empty slice (not nil) and no error if the directory does not exist. Individual file errors (hash failures, unreadable sidecars) are logged at warn level but do not prevent discovery of other hooks.
func HandleActionRequest ¶
HandleActionRequest returns an api.EventHandler for action_request events. It parses the SSE payload into an ActionRequest and delegates to the Executor. When the executor's config is disabled, all requests are rejected with reason=actions_disabled.
Types ¶
type ActionReporter ¶
type ActionReporter interface {
AckExecution(ctx context.Context, nodeID, executionID string, ack api.ExecutionAck) error
ReportResult(ctx context.Context, nodeID, executionID string, result api.ExecutionResult) error
}
ActionReporter abstracts control plane communication for testability.
type ArtifactFetcher ¶
type ArtifactFetcher interface {
FetchArtifact(ctx context.Context, version, goos, arch string) (io.ReadCloser, error)
}
ArtifactFetcher downloads a plexd binary artifact.
type BuiltinFunc ¶
type BuiltinFunc func(ctx context.Context, params map[string]string) (stdout string, stderr string, exitCode int, err error)
BuiltinFunc is the signature for built-in action implementations. It receives a context (with timeout deadline) and parameters, and returns stdout, stderr, an exit code, and an optional error.
func ConfigDump ¶
func ConfigDump(provider ConfigProvider) BuiltinFunc
ConfigDump returns a BuiltinFunc that outputs the sanitized configuration.
func DiagnosticsCollect ¶
func DiagnosticsCollect() BuiltinFunc
DiagnosticsCollect returns a BuiltinFunc that collects system diagnostics and returns them as JSON. Optional parameters: "include_network" (default "true"), "include_processes" (default "true").
func DiagnosticsTraceroutePeer ¶
func DiagnosticsTraceroutePeer(info NodeInfoProvider) BuiltinFunc
DiagnosticsTraceroutePeer returns a BuiltinFunc that runs traceroute to a mesh peer. Requires a "peer_id" parameter (mesh IP). Optional "max_hops" parameter (default 15).
func GatherInfo ¶
func GatherInfo(info NodeInfoProvider) BuiltinFunc
GatherInfo returns a BuiltinFunc that collects system information and returns it as JSON. The output includes: hostname, os, arch, go_version, mesh_ip, peer_count, node_id.
func HealthCheck ¶
func HealthCheck(health HealthProvider) BuiltinFunc
HealthCheck returns a BuiltinFunc that reports the node's health status. Optional parameter: "include_peers" (default "true") — include per-peer status. Status is "healthy" if tunnel_count > 0, otherwise "degraded".
func LogsSnapshot ¶
func LogsSnapshot(provider LogProvider) BuiltinFunc
LogsSnapshot returns a BuiltinFunc that retrieves recent log lines. Accepts optional parameters:
- "lines": number of lines to return (default 100, max 10000)
- "since": duration string (e.g. "5m", "1h") to filter lines by age
func MeshReconnect ¶
func MeshReconnect(reconnector MeshReconnector) BuiltinFunc
MeshReconnect returns a BuiltinFunc that triggers mesh reconnection. On failure, returns exit code 1 with error details but no system error.
func PingPeer ¶
func PingPeer(info NodeInfoProvider) BuiltinFunc
PingPeer returns a BuiltinFunc that pings a mesh peer and reports latency. Requires a "peer_id" parameter (mesh IP). Optional "count" parameter (default 1).
func ServiceReloadConfig ¶
func ServiceReloadConfig() BuiltinFunc
ServiceReloadConfig returns a BuiltinFunc that sends SIGHUP to the current process to trigger a configuration reload.
func ServiceRestart ¶
func ServiceRestart() BuiltinFunc
ServiceRestart returns a BuiltinFunc that restarts the plexd service via systemctl.
func ServiceUpgrade ¶
func ServiceUpgrade(fetcher ArtifactFetcher) BuiltinFunc
ServiceUpgrade returns a BuiltinFunc that performs an in-place binary upgrade. It downloads the new binary from the control plane, verifies its SHA-256 checksum, atomically replaces the current binary, and triggers a systemd restart.
Required parameters:
- version: target version string (e.g. "1.5.0")
- checksum: expected SHA-256 checksum of the new binary (hex-encoded, with or without "sha256:" prefix)
type Config ¶
type Config struct {
// Enabled controls whether action execution is active.
// Default: true (set by ApplyDefaults).
Enabled bool `yaml:"enabled"`
// HooksDir is the directory containing hook scripts.
// Default: /etc/plexd/hooks
HooksDir string `yaml:"hooks_dir"`
// MaxConcurrent is the maximum number of actions that can run concurrently.
// Must be at least 1 when enabled. Default: 5.
MaxConcurrent int `yaml:"max_concurrent"`
// MaxActionTimeout is the maximum duration for a single action.
// Must be at least 10s when enabled. Default: 10m.
MaxActionTimeout time.Duration `yaml:"max_action_timeout"`
// MaxOutputBytes is the maximum output size per action in bytes.
// Must be at least 1024 when enabled. Default: 1 MiB.
MaxOutputBytes int64 `yaml:"max_output_bytes"`
}
Config holds the configuration for remote action execution.
func (*Config) ApplyDefaults ¶
func (c *Config) ApplyDefaults()
ApplyDefaults sets default values for zero-valued fields. On a zero-valued Config, Enabled defaults to true. To disable action execution, set Enabled=false before or after calling ApplyDefaults.
type ConfigProvider ¶
type ConfigProvider interface {
DumpConfig() string
}
ConfigProvider supplies sanitized configuration for dumping.
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor orchestrates action execution, concurrency control, and result reporting.
func NewExecutor ¶
func NewExecutor(cfg Config, reporter ActionReporter, verifier HookVerifier, logger *slog.Logger) *Executor
NewExecutor creates an Executor with the given configuration, reporter, verifier, and logger.
func (*Executor) ActiveCount ¶
ActiveCount returns the number of currently running actions.
func (*Executor) Capabilities ¶
func (e *Executor) Capabilities() ([]api.ActionInfo, []api.HookInfo)
Capabilities returns builtin action metadata and hooks for capability reporting.
func (*Executor) RegisterBuiltin ¶
func (e *Executor) RegisterBuiltin(name, description string, params []api.ActionParam, fn BuiltinFunc)
RegisterBuiltin stores a builtin action for execution.
func (*Executor) RunLocal ¶
func (e *Executor) RunLocal(ctx context.Context, action string, params map[string]string) (string, string, int, error)
RunLocal executes a built-in action synchronously and returns the output. This is used by the local node API for CLI-triggered action execution. Only built-in actions are supported; hook execution requires control plane checksum.
type HealthProvider ¶
type HealthProvider interface {
TunnelCount() int
ConnectedPeers() int
Uptime() time.Duration
LastHeartbeat() time.Time
LastReconcile() time.Time
}
HealthProvider supplies health status information to built-in actions.
type HookChangeCallback ¶
HookChangeCallback is called when hooks change. Receives the full current hooks list.
type HookVerifier ¶
type HookVerifier interface {
VerifyHook(ctx context.Context, nodeID, hookPath, expectedChecksum string) (bool, error)
}
HookVerifier abstracts hook integrity verification for testability.
type HookWatcher ¶
type HookWatcher struct {
// contains filtered or unexported fields
}
HookWatcher monitors a hooks directory for changes using fsnotify.
func NewHookWatcher ¶
func NewHookWatcher(hooksDir string, onChange HookChangeCallback, onIntegrity IntegrityAlertCallback, logger *slog.Logger) *HookWatcher
NewHookWatcher creates a new HookWatcher.
func (*HookWatcher) Hooks ¶
func (w *HookWatcher) Hooks() []api.HookInfo
Hooks returns a sorted snapshot of the current hooks.
type IntegrityAlertCallback ¶
type IntegrityAlertCallback func(hookName, oldChecksum, newChecksum string)
IntegrityAlertCallback is called when a hook file's checksum changes unexpectedly.
type LogProvider ¶
LogProvider supplies recent log lines.
type MeshReconnector ¶
MeshReconnector triggers mesh reconnection.
type NodeInfoProvider ¶
NodeInfoProvider supplies mesh node information to built-in actions.