initsystem

package
v2.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package initsystem provides a common interface for interacting with init systems like systemd, openrc, sysvinit, etc.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultRegistry is the default repository for init systems.
	DefaultRegistry = sync.OnceValue(func() *Registry {
		provider := NewRegistry()
		RegisterSystemd(provider)
		RegisterOpenRC(provider)
		RegisterUpstart(provider)
		RegisterSysVinit(provider)
		RegisterWinSCM(provider)
		RegisterRunit(provider)
		RegisterLaunchd(provider)
		return provider
	})

	// ErrNoInitSystem is returned when no supported init system is found.
	ErrNoInitSystem = errors.New("no supported init system found")
)

Functions

func RegisterLaunchd

func RegisterLaunchd(repo *Registry)

RegisterLaunchd registers the launchd init system to a init system repository.

func RegisterOpenRC

func RegisterOpenRC(repo *Registry)

RegisterOpenRC registers OpenRC to a repository.

func RegisterRunit

func RegisterRunit(repo *Registry)

RegisterRunit register runit in a repository.

func RegisterSysVinit

func RegisterSysVinit(repo *Registry)

RegisterSysVinit registers SysVinit in a repository.

func RegisterSystemd

func RegisterSystemd(repo *Registry)

RegisterSystemd registers systemd into a repository.

func RegisterUpstart

func RegisterUpstart(repo *Registry)

RegisterUpstart registers Upstart in a repository.

func RegisterWinSCM

func RegisterWinSCM(repo *Registry)

RegisterWinSCM registers the WinSCM in a repository.

Types

type Factory

Factory is a type alias for the plumbing.Factory type specialized for initsystem ServiceManagers.

type Launchd

type Launchd struct{}

Launchd is the init system for macOS (and darwin), the implementation is very basic and doesn't handle services in user space.

func (Launchd) DisableService

func (i Launchd) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error

DisableService disables a launchd service by renaming the plist file (not very elegant).

func (Launchd) EnableService

func (i Launchd) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error

EnableService enables a launchd service (not very elegant).

func (Launchd) ServiceIsRunning

func (i Launchd) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool

ServiceIsRunning checks if a launchd service is running.

func (Launchd) ServiceLogs

func (i Launchd) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)

ServiceLogs returns the logs for a launchd service.

func (Launchd) ServiceScriptPath

func (i Launchd) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)

ServiceScriptPath returns the path to a launchd service plist file.

func (Launchd) StartService

func (i Launchd) StartService(ctx context.Context, h cmd.ContextRunner, s string) error

StartService starts a launchd service.

func (Launchd) StopService

func (i Launchd) StopService(ctx context.Context, h cmd.ContextRunner, s string) error

StopService stops a launchd service.

func (Launchd) StreamServiceLogs

func (i Launchd) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error

StreamServiceLogs streams service logs to w using the macOS log stream command, until ctx is cancelled.

func (Launchd) String

func (Launchd) String() string

String returns the name of the init system.

type NullServiceManager

type NullServiceManager struct {
	Err error
}

NullServiceManager is a service manager that always returns an error on every operation.

func (*NullServiceManager) DisableService

func (n *NullServiceManager) DisableService(ctx context.Context, _ cmd.ContextRunner, s string) error

DisableService always returns an error.

func (*NullServiceManager) EnableService

func (n *NullServiceManager) EnableService(ctx context.Context, _ cmd.ContextRunner, s string) error

EnableService always returns an error.

func (*NullServiceManager) ServiceIsRunning

func (n *NullServiceManager) ServiceIsRunning(_ context.Context, _ cmd.ContextRunner, _ string) bool

ServiceIsRunning always returns false.

func (*NullServiceManager) ServiceScriptPath

func (n *NullServiceManager) ServiceScriptPath(ctx context.Context, _ cmd.ContextRunner, s string) (string, error)

ServiceScriptPath always returns an error.

func (*NullServiceManager) StartService

func (n *NullServiceManager) StartService(ctx context.Context, _ cmd.ContextRunner, s string) error

StartService always returns an error.

func (*NullServiceManager) StopService

func (n *NullServiceManager) StopService(ctx context.Context, _ cmd.ContextRunner, s string) error

StopService always returns an error.

type OpenRC

type OpenRC struct{}

OpenRC is found on some linux systems, often installed on Alpine for example.

func (OpenRC) DisableService

func (i OpenRC) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error

DisableService disables a service.

func (OpenRC) EnableService

func (i OpenRC) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error

EnableService enables a service.

func (OpenRC) RestartService

func (i OpenRC) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error

RestartService restarts a service.

func (OpenRC) ServiceEnvironmentContent

func (i OpenRC) ServiceEnvironmentContent(env map[string]string) string

ServiceEnvironmentContent returns a formatted string for a service environment override file.

func (OpenRC) ServiceEnvironmentPath

func (i OpenRC) ServiceEnvironmentPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)

ServiceEnvironmentPath returns a path to an environment override file path.

func (OpenRC) ServiceIsRunning

func (i OpenRC) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool

ServiceIsRunning returns true if a service is running.

func (OpenRC) ServiceScriptPath

func (i OpenRC) ServiceScriptPath(ctx context.Context, h cmd.ContextRunner, s string) (string, error)

ServiceScriptPath returns the path to a service configuration file.

func (OpenRC) StartService

func (i OpenRC) StartService(ctx context.Context, h cmd.ContextRunner, s string) error

StartService starts a service.

func (OpenRC) StopService

func (i OpenRC) StopService(ctx context.Context, h cmd.ContextRunner, s string) error

StopService stops a service.

func (OpenRC) String

func (OpenRC) String() string

String returns the name of the init system.

type Provider

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

Provider provides a unified interface to interact with different init systems. It ensures that a suitable service manager is lazily initialized and made available for service management operations. It supports operations like starting and stopping services.

func NewInitSystemProvider

func NewInitSystemProvider(get ServiceManagerProvider, runner cmd.ContextRunner) *Provider

NewInitSystemProvider creates a new instance of Provider with the provided ServiceManagerProvider function.

func (*Provider) ServiceManager

func (p *Provider) ServiceManager() (ServiceManager, error)

ServiceManager returns a ServiceManager or an error if a service manager could not be initialized.

type Registry

Registry is a type alias for the plumbing.Provider type specialized for initsystem ServiceManagers.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a new Registry.

type Runit

type Runit struct{}

Runit is an init system implementation for runit.

func (Runit) DisableService

func (i Runit) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error

DisableService removes the symlink from the runit service directory to disable a service.

func (Runit) EnableService

func (i Runit) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error

EnableService creates a symlink in the runit service directory to enable a service.

func (Runit) RestartService

func (i Runit) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error

RestartService restarts a runit service.

func (Runit) ServiceIsRunning

func (i Runit) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool

ServiceIsRunning returns true if a runit service is running.

func (Runit) ServiceLogs

func (i Runit) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)

ServiceLogs returns the logs for a runit service from /var/log/<service>/current. It's not guaranteed that the log file exists or that the service logs to this file.

func (Runit) ServiceScriptPath

func (i Runit) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)

ServiceScriptPath returns the path to a runit service script.

func (Runit) StartService

func (i Runit) StartService(ctx context.Context, h cmd.ContextRunner, s string) error

StartService starts a runit service.

func (Runit) StopService

func (i Runit) StopService(ctx context.Context, h cmd.ContextRunner, s string) error

StopService stops a runit service.

func (Runit) StreamServiceLogs

func (i Runit) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error

StreamServiceLogs streams service logs to w by following /var/log/<service>/current, until ctx is cancelled. It's not guaranteed that the log file exists or that the service logs to this file.

func (Runit) String

func (Runit) String() string

String returns the name of the init system.

type ServiceEnvironmentManager

type ServiceEnvironmentManager interface {
	ServiceEnvironmentPath(ctx context.Context, h cmd.ContextRunner, s string) (string, error)
	ServiceEnvironmentContent(env map[string]string) string
}

ServiceEnvironmentManager is a servicemanager that supports environment files (like systemd .env files).

type ServiceManager

type ServiceManager interface {
	StartService(ctx context.Context, h cmd.ContextRunner, s string) error
	StopService(ctx context.Context, h cmd.ContextRunner, s string) error
	ServiceScriptPath(ctx context.Context, h cmd.ContextRunner, s string) (string, error)
	EnableService(ctx context.Context, h cmd.ContextRunner, s string) error
	DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
	ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
}

ServiceManager defines the methods for interacting with an init system like OpenRC.

type ServiceManagerLogReader

type ServiceManagerLogReader interface {
	ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)
}

ServiceManagerLogReader is a servicemanager that supports reading service logs.

type ServiceManagerLogStreamer

type ServiceManagerLogStreamer interface {
	StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error
}

ServiceManagerLogStreamer is an optional interface for init systems that can follow service logs in real time. The method streams log output to w until ctx is cancelled or an error occurs. Context cancellation is treated as a clean stop, not an error.

type ServiceManagerProvider

type ServiceManagerProvider func(cmd.ContextRunner) (ServiceManager, error)

ServiceManagerProvider is a function that returns a ServiceManager given a runner.

type ServiceManagerReloader

type ServiceManagerReloader interface {
	DaemonReload(ctx context.Context, h cmd.ContextRunner) error
}

ServiceManagerReloader is a servicemanager that needs reloading (like systemd daemon-reload).

type ServiceManagerRestarter

type ServiceManagerRestarter interface {
	RestartService(ctx context.Context, h cmd.ContextRunner, s string) error
}

ServiceManagerRestarter is a servicemanager that supports direct restarts (instead of stop+start).

type SysVinit

type SysVinit struct{}

SysVinit is the service manager for SysVinit.

func (SysVinit) DisableService

func (i SysVinit) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error

DisableService for SysVinit tries to remove symlinks in the appropriate runlevel directories.

func (SysVinit) EnableService

func (i SysVinit) EnableService(ctx context.Context, h cmd.ContextRunner, service string) error

EnableService for SysVinit tries to create symlinks in the appropriate runlevel directories.

func (SysVinit) RestartService

func (i SysVinit) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error

RestartService restarts a service.

func (SysVinit) ServiceIsRunning

func (i SysVinit) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool

ServiceIsRunning checks if a service is running.

func (SysVinit) ServiceScriptPath

func (i SysVinit) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)

ServiceScriptPath returns the path to a SysVinit service script.

func (SysVinit) StartService

func (i SysVinit) StartService(ctx context.Context, h cmd.ContextRunner, s string) error

StartService starts a service.

func (SysVinit) StopService

func (i SysVinit) StopService(ctx context.Context, h cmd.ContextRunner, s string) error

StopService stops a service.

func (SysVinit) String

func (SysVinit) String() string

String returns the name of the init system.

type Systemd

type Systemd struct{}

Systemd is found by default on most linux distributions today.

func (Systemd) DaemonReload

func (i Systemd) DaemonReload(ctx context.Context, h cmd.ContextRunner) error

DaemonReload reloads init system configuration.

func (Systemd) DisableService

func (i Systemd) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error

DisableService disables a service.

func (Systemd) EnableService

func (i Systemd) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error

EnableService enables a service.

func (Systemd) RestartService

func (i Systemd) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error

RestartService restarts a service.

func (Systemd) ServiceEnvironmentContent

func (i Systemd) ServiceEnvironmentContent(env map[string]string) string

ServiceEnvironmentContent returns a formatted string for a service environment override file.

func (Systemd) ServiceEnvironmentPath

func (i Systemd) ServiceEnvironmentPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)

ServiceEnvironmentPath returns the drop-in environment override path for the service. Drop-ins always go under /etc/systemd/system/ regardless of where the unit file is installed, so that overrides survive package updates and are always writable.

func (Systemd) ServiceIsRunning

func (i Systemd) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool

ServiceIsRunning returns true if a service is running.

func (Systemd) ServiceLogs

func (i Systemd) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)

ServiceLogs returns the last n lines of a service log.

func (Systemd) ServiceScriptPath

func (i Systemd) ServiceScriptPath(ctx context.Context, h cmd.ContextRunner, s string) (string, error)

ServiceScriptPath returns the path to a service configuration file.

func (Systemd) StartService

func (i Systemd) StartService(ctx context.Context, h cmd.ContextRunner, s string) error

StartService starts a service.

func (Systemd) StopService

func (i Systemd) StopService(ctx context.Context, h cmd.ContextRunner, s string) error

StopService stops a service.

func (Systemd) StreamServiceLogs

func (i Systemd) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error

StreamServiceLogs streams service logs to w using journalctl -f, until ctx is cancelled.

func (Systemd) String

func (Systemd) String() string

String returns the name of the init system.

type Upstart

type Upstart struct{}

Upstart is the init system used by Ubuntu 14.04 and older.

func (Upstart) DisableService

func (i Upstart) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error

DisableService for Upstart.

func (Upstart) EnableService

func (i Upstart) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error

EnableService for Upstart (might involve creating a symlink or managing an override file).

func (Upstart) RestartService

func (i Upstart) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error

RestartService restarts a service.

func (Upstart) ServiceIsRunning

func (i Upstart) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool

ServiceIsRunning checks if a service is running.

func (Upstart) ServiceLogs

func (i Upstart) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)

ServiceLogs returns the logs for a service from /var/log/upstart/<service>.log. It's not guaranteed that the log file exists or that the service logs to this file.

func (Upstart) ServiceScriptPath

func (i Upstart) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)

ServiceScriptPath returns the path to an Upstart service configuration file.

func (Upstart) StartService

func (i Upstart) StartService(ctx context.Context, h cmd.ContextRunner, s string) error

StartService starts a service.

func (Upstart) StopService

func (i Upstart) StopService(ctx context.Context, h cmd.ContextRunner, s string) error

StopService stops a service.

func (Upstart) StreamServiceLogs

func (i Upstart) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error

StreamServiceLogs streams service logs to w by following /var/log/upstart/<service>.log, until ctx is cancelled. It's not guaranteed that the log file exists or that the service logs to this file.

func (Upstart) String

func (Upstart) String() string

String returns the name of the init system.

type WinSCM

type WinSCM struct{}

WinSCM is a struct that implements the InitSystem interface for Windows Service Control Manager.

func (WinSCM) DisableService

func (c WinSCM) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error

DisableService disables a service by setting its startup type to Disabled.

func (WinSCM) EnableService

func (c WinSCM) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error

EnableService enables a service by setting its startup type to Automatic.

func (WinSCM) RestartService

func (c WinSCM) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error

RestartService restarts a service.

func (WinSCM) ServiceIsRunning

func (c WinSCM) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool

ServiceIsRunning returns true if a service is running.

func (WinSCM) ServiceLogs

func (c WinSCM) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)

ServiceLogs returns Service Control Manager lifecycle events for the service from the System event log (start, stop, crash). This reflects SCM control events only, not the service's own application output. Services that write to a file or a separate event log source are not covered.

func (WinSCM) ServiceScriptPath

func (c WinSCM) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, _ string) (string, error)

ServiceScriptPath returns the path to a service configuration file.

func (WinSCM) StartService

func (c WinSCM) StartService(ctx context.Context, h cmd.ContextRunner, s string) error

StartService starts a service.

func (WinSCM) StopService

func (c WinSCM) StopService(ctx context.Context, h cmd.ContextRunner, s string) error

StopService stops a service.

func (WinSCM) String

func (WinSCM) String() string

String returns the name of the init system.

Jump to

Keyboard shortcuts

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