packaging

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 12 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 packaging installs plexd as a host service: a systemd unit on Linux, a launchd daemon on macOS, a Windows service on Windows.

Index

Constants

View Source
const (
	SessionHelperSocketUnitName  = "plexd-session-helper.socket"
	SessionHelperServiceUnitName = "plexd-session-helper@.service"
)

The units of the session helper, which starts the processes of mediated ssh sessions outside plexd's sandbox. Their files live beside plexd.service.

View Source
const DaemonLogFile = "plexd.log"

DaemonLogFile is the name of the file the service manager writes the daemon's output to, under LogDir, where a manager keeps such a file (launchd's StandardOutPath and StandardErrorPath on macOS).

View Source
const DefaultServiceName = "plexd"

DefaultServiceName is the default service name.

Variables

View Source
var DefaultBinaryPath = defaultBinaryPath()

DefaultBinaryPath is the default path to install the plexd binary, resolved per platform by defaultBinaryPath.

View Source
var DefaultConfigDir = paths.ConfigDir()

DefaultConfigDir is the default configuration directory.

View Source
var DefaultDataDir = paths.DataDir()

DefaultDataDir is the default data directory.

View Source
var DefaultLogDir = defaultLogDir()

DefaultLogDir is the default directory the service manager writes plexd's output to, resolved per platform by defaultLogDir. It is empty where the manager keeps the logs itself.

View Source
var DefaultRunDir = paths.RunDir()

DefaultRunDir is the default runtime directory.

View Source
var DefaultUnitFilePath = defaultUnitFilePath()

DefaultUnitFilePath is the default path of the service definition file, resolved per platform by defaultUnitFilePath. It is empty where the manager keeps no file.

View Source
var ErrNotRegistered = errors.New("packaging: service is not registered")

ErrNotRegistered is returned by Status when the service definition is missing.

Functions

func GenerateDefaultConfig

func GenerateDefaultConfig(apiBaseURL string) string

GenerateDefaultConfig produces a minimal default config.yaml for plexd. If apiBaseURL is empty, a placeholder comment is written instead.

data_dir and registration.token_file are written as the platform defaults, so the file names the same directories the daemon would fall back to without it. Both are plain YAML scalars, where a backslash is a literal character and needs no escaping.

func GenerateLaunchdPlist added in v0.8.0

func GenerateLaunchdPlist(cfg InstallConfig) string

GenerateLaunchdPlist produces the LaunchDaemon property list for the plexd service. It calls cfg.ApplyDefaults() to fill in zero-valued fields before generating the output.

KeepAlive with ThrottleInterval is the launchd form of the unit file's Restart=always and RestartSec=5s. launchd has no StartLimitBurst counterpart, so a daemon that exits on a configuration error restarts every five seconds until an operator boots it out.

func GenerateNewsyslogConf added in v0.8.0

func GenerateNewsyslogConf(cfg InstallConfig) string

GenerateNewsyslogConf produces the newsyslog rule for the daemon's log file: rotate at 10 MiB, keep five generations, bzip2 the rotated files. launchd appends to StandardOutPath forever, so without this rule the file grows without bound.

func GenerateSessionHelperServiceUnit added in v0.8.0

func GenerateSessionHelperServiceUnit(cfg InstallConfig) string

GenerateSessionHelperServiceUnit produces plexd-session-helper@.service, the template each connection to the socket instantiates. It calls cfg.ApplyDefaults() to fill in zero-valued fields.

The unit deliberately carries no sandbox directives: it exists to run outside plexd's sandbox, where a shell can become the login user. Its guard is the helper's own check of the session token against a key plexd cannot write. KillMode=control-group takes down whatever a session left behind.

func GenerateSessionHelperSocketUnit added in v0.8.0

func GenerateSessionHelperSocketUnit() string

GenerateSessionHelperSocketUnit produces plexd-session-helper.socket. It is root-only (0600), and Accept=yes starts one helper instance per connection.

systemd's defaults for Accept=yes would let one session holder break ssh sessions for the whole node: MaxConnections=64 is below the 400 processes plexd's defaults allow (10 sessions of 4 connections of 10 channels), and going past the trigger limit of 200 per 2 s fails the socket until an operator restarts it. MaxConnections is therefore raised above plexd's own bound, which is what limits the helpers, and the trigger limit is turned off.

func GenerateUnitFile

func GenerateUnitFile(cfg InstallConfig) string

GenerateUnitFile produces a complete systemd unit file for the plexd service. It calls cfg.ApplyDefaults() to fill in zero-valued fields before generating the output.

The unit wants the session helper socket and is ordered after it, and enabling plexd enables the socket too (Also=), so `systemctl enable --now plexd` brings both up.

ProtectSystem=full keeps /etc read-only, and the session helper relies on that: plexd must not write config.yaml or the signing key the helper pins beside it. The bootstrap token file is therefore the only writable path under ConfigDir, so the registrar can empty the spent token it cannot unlink. Every ReadWritePaths entry is optional ("-"), because a missing one fails the unit with 226/NAMESPACE: RunDir is on tmpfs and gone after a reboot until plexd recreates it for the node API socket, DataDir is gone after deregister --purge until plexd saves a new identity, and an absent token file leaves nothing to empty.

Types

type InstallConfig

type InstallConfig struct {
	// BinaryPath is the path to install the plexd binary.
	// Default: DefaultBinaryPath
	BinaryPath string

	// ConfigDir is the configuration directory.
	// Default: paths.ConfigDir()
	ConfigDir string

	// DataDir is the data directory.
	// Default: paths.DataDir()
	DataDir string

	// RunDir is the runtime directory.
	// Default: paths.RunDir()
	RunDir string

	// LogDir is the directory the service manager writes plexd's output to.
	// Empty where the manager keeps the logs itself (journald on Linux, the
	// Event Log on Windows).
	// Default: DefaultLogDir
	LogDir string

	// UnitFilePath is the path of the service definition file. Empty where the
	// manager keeps no file (the Service Control Manager on Windows).
	// Default: DefaultUnitFilePath
	UnitFilePath string

	// ServiceName is the name the host's service manager knows plexd by.
	// Default: plexd
	ServiceName string

	// APIBaseURL is the control plane API URL (optional).
	APIBaseURL string

	// TokenValue is the bootstrap token value (optional).
	TokenValue string

	// TokenFile is the path to the token file to copy from (optional).
	TokenFile string
}

InstallConfig holds the configuration for packaging and installing plexd as a host service. InstallConfig is passed as a constructor argument — no file I/O in this package.

func (*InstallConfig) ApplyDefaults

func (c *InstallConfig) ApplyDefaults()

ApplyDefaults sets default values for zero-valued fields.

func (*InstallConfig) Validate

func (c *InstallConfig) Validate() error

Validate checks that required fields are set.

UnitFilePath and LogDir are not required: the Service Control Manager keeps neither a definition file nor a log directory, and the two managers that do need one check it themselves.

type Installer

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

Installer handles installing and uninstalling plexd as a host service. It owns the files that are the same everywhere — the binary, the config, the token — and leaves the service definition to the host's ServiceManager.

func NewInstaller

func NewInstaller(cfg InstallConfig, mgr ServiceManager, root RootChecker, logger *slog.Logger) *Installer

NewInstaller creates a new Installer with defaults applied.

func (*Installer) Install

func (ins *Installer) Install() error

Install installs plexd as a host service.

func (*Installer) Uninstall

func (ins *Installer) Uninstall(purge bool) error

Uninstall removes the plexd host service. If purge is true, data and config dirs are also removed.

type RootChecker

type RootChecker interface {
	// IsRoot returns true if the current process has root privileges.
	IsRoot() bool
}

RootChecker abstracts privilege checking for testability.

func NewRootChecker

func NewRootChecker() RootChecker

NewRootChecker returns a RootChecker that checks the real process UID.

type Service added in v0.8.0

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

Service binds a ServiceManager to one InstallConfig. The service.restart and service.upgrade actions restart the installed daemon through it.

func NewService added in v0.8.0

func NewService(mgr ServiceManager, cfg InstallConfig) *Service

NewService binds mgr to cfg with the platform defaults applied, so a caller that only ever drives the default installation passes an empty InstallConfig.

func (*Service) Available added in v0.8.0

func (s *Service) Available() bool

Available reports whether the host's service manager can be driven from this process.

func (*Service) Restart added in v0.8.0

func (s *Service) Restart(ctx context.Context) error

Restart asks the manager to restart the service. It returns once the request is accepted; the restart itself completes afterwards, and may end this process before it does.

type ServiceManager added in v0.8.0

type ServiceManager interface {
	// Name identifies the manager in messages: "systemd", "launchd" or
	// "service control manager".
	Name() string

	// Available reports whether the manager can be driven from this process.
	Available() bool

	// Registered reports whether the service definition exists.
	Registered(cfg InstallConfig) (bool, error)

	// Register writes the service definition so the service exists and is
	// known to the manager. It never starts the service. Repeating it
	// rewrites the same definition and returns nil.
	Register(cfg InstallConfig) error

	// Unregister stops the service if it runs and removes its definition.
	// A definition that is already gone is not an error.
	Unregister(cfg InstallConfig) error

	// Start starts the service now.
	Start(cfg InstallConfig) error

	// Stop stops the service and returns once it has stopped. A service
	// that is not running is not an error.
	Stop(cfg InstallConfig) error

	// Restart asks the manager to restart the service and returns once the
	// request is accepted. The caller may be the process being restarted,
	// so the restart itself completes after Restart returns.
	Restart(ctx context.Context, cfg InstallConfig) error

	// Status reports whether the service runs. It returns ErrNotRegistered
	// when the definition is missing.
	Status(cfg InstallConfig) (ServiceStatus, error)
}

ServiceManager registers plexd with the host's service manager and drives the registered service. NewServiceManager returns the host's own: systemd on Linux and other Unix, launchd on macOS, the Service Control Manager on Windows. Every method takes the InstallConfig it acts on, so the service name and the definition path have one source.

func NewLaunchdManager added in v0.8.0

func NewLaunchdManager(run launchctlRunner, logger *slog.Logger) ServiceManager

NewLaunchdManager returns the launchd ServiceManager driving launchctl through run.

func NewServiceManager added in v0.8.0

func NewServiceManager(logger *slog.Logger) ServiceManager

NewServiceManager returns the host's own service manager.

func NewSystemdManager added in v0.8.0

func NewSystemdManager(ctl SystemdController, logger *slog.Logger) ServiceManager

NewSystemdManager returns the systemd ServiceManager over ctl.

type ServiceStatus added in v0.8.0

type ServiceStatus string

ServiceStatus is what Status reports about a registered service.

const (
	// StatusRunning means the service is running.
	StatusRunning ServiceStatus = "running"

	// StatusStopped means the service is registered but not running.
	StatusStopped ServiceStatus = "stopped"
)

type SystemdController

type SystemdController interface {
	// IsAvailable returns true if systemd (systemctl) is available on the system.
	IsAvailable() bool

	// DaemonReload executes systemctl daemon-reload to reload unit file changes.
	DaemonReload() error

	// Enable enables the named service to start on boot.
	Enable(service string) error

	// Disable disables the named service from starting on boot.
	Disable(service string) error

	// Start starts the named service.
	Start(service string) error

	// Stop stops the named service. Returns nil if the service is not running.
	Stop(service string) error

	// Restart restarts the named service. The caller may be the process being
	// restarted, so the command is run under ctx and returns once systemd has
	// accepted the request.
	Restart(ctx context.Context, service string) error

	// IsActive returns true if the named service is currently running.
	IsActive(service string) bool
}

SystemdController abstracts systemd service management for testability. All methods that modify state must be idempotent: repeating an operation that is already applied returns nil.

func NewSystemdController

func NewSystemdController() SystemdController

NewSystemdController returns a SystemdController that calls the real systemctl binary.

Jump to

Keyboard shortcuts

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