Documentation
¶
Overview ¶
Package initsystem provides a common interface for interacting with init systems like systemd, openrc, sysvinit, etc.
Index ¶
- Variables
- func RegisterLaunchd(repo *Registry)
- func RegisterOpenRC(repo *Registry)
- func RegisterRunit(repo *Registry)
- func RegisterSysVinit(repo *Registry)
- func RegisterSystemd(repo *Registry)
- func RegisterUpstart(repo *Registry)
- func RegisterWinSCM(repo *Registry)
- type Factory
- type Launchd
- func (i Launchd) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Launchd) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Launchd) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
- func (i Launchd) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)
- func (i Launchd) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)
- func (i Launchd) StartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Launchd) StopService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Launchd) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error
- func (Launchd) String() string
- type NullServiceManager
- func (n *NullServiceManager) DisableService(ctx context.Context, _ cmd.ContextRunner, s string) error
- func (n *NullServiceManager) EnableService(ctx context.Context, _ cmd.ContextRunner, s string) error
- func (n *NullServiceManager) ServiceIsRunning(_ context.Context, _ cmd.ContextRunner, _ string) bool
- func (n *NullServiceManager) ServiceScriptPath(ctx context.Context, _ cmd.ContextRunner, s string) (string, error)
- func (n *NullServiceManager) StartService(ctx context.Context, _ cmd.ContextRunner, s string) error
- func (n *NullServiceManager) StopService(ctx context.Context, _ cmd.ContextRunner, s string) error
- type OpenRC
- func (i OpenRC) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i OpenRC) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i OpenRC) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i OpenRC) ServiceEnvironmentContent(env map[string]string) string
- func (i OpenRC) ServiceEnvironmentPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)
- func (i OpenRC) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
- func (i OpenRC) ServiceScriptPath(ctx context.Context, h cmd.ContextRunner, s string) (string, error)
- func (i OpenRC) StartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i OpenRC) StopService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (OpenRC) String() string
- type Provider
- type Registry
- type Runit
- func (i Runit) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Runit) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Runit) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Runit) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
- func (i Runit) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)
- func (i Runit) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)
- func (i Runit) StartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Runit) StopService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Runit) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error
- func (Runit) String() string
- type ServiceEnvironmentManager
- type ServiceManager
- type ServiceManagerLogReader
- type ServiceManagerLogStreamer
- type ServiceManagerProvider
- type ServiceManagerReloader
- type ServiceManagerRestarter
- type SysVinit
- func (i SysVinit) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i SysVinit) EnableService(ctx context.Context, h cmd.ContextRunner, service string) error
- func (i SysVinit) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i SysVinit) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
- func (i SysVinit) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)
- func (i SysVinit) StartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i SysVinit) StopService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (SysVinit) String() string
- type Systemd
- func (i Systemd) DaemonReload(ctx context.Context, h cmd.ContextRunner) error
- func (i Systemd) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Systemd) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Systemd) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Systemd) ServiceEnvironmentContent(env map[string]string) string
- func (i Systemd) ServiceEnvironmentPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)
- func (i Systemd) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
- func (i Systemd) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)
- func (i Systemd) ServiceScriptPath(ctx context.Context, h cmd.ContextRunner, s string) (string, error)
- func (i Systemd) StartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Systemd) StopService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Systemd) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error
- func (Systemd) String() string
- type Upstart
- func (i Upstart) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Upstart) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Upstart) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Upstart) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
- func (i Upstart) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)
- func (i Upstart) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, s string) (string, error)
- func (i Upstart) StartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Upstart) StopService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (i Upstart) StreamServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, w io.Writer) error
- func (Upstart) String() string
- type WinSCM
- func (c WinSCM) DisableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (c WinSCM) EnableService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (c WinSCM) RestartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (c WinSCM) ServiceIsRunning(ctx context.Context, h cmd.ContextRunner, s string) bool
- func (c WinSCM) ServiceLogs(ctx context.Context, h cmd.ContextRunner, s string, lines int) ([]string, error)
- func (c WinSCM) ServiceScriptPath(_ context.Context, _ cmd.ContextRunner, _ string) (string, error)
- func (c WinSCM) StartService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (c WinSCM) StopService(ctx context.Context, h cmd.ContextRunner, s string) error
- func (WinSCM) String() string
Constants ¶
This section is empty.
Variables ¶
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 ¶
type Factory = plumbing.Factory[cmd.ContextRunner, ServiceManager]
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 ¶
DisableService disables a launchd service by renaming the plist file (not very elegant).
func (Launchd) EnableService ¶
EnableService enables a launchd service (not very elegant).
func (Launchd) ServiceIsRunning ¶
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 ¶
StartService starts a launchd service.
func (Launchd) StopService ¶
StopService stops a launchd service.
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 ¶
DisableService disables a service.
func (OpenRC) EnableService ¶
EnableService enables a service.
func (OpenRC) RestartService ¶
RestartService restarts a service.
func (OpenRC) ServiceEnvironmentContent ¶
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 ¶
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 ¶
StartService starts a service.
func (OpenRC) StopService ¶
StopService stops a service.
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 ¶
type Registry = plumbing.Provider[cmd.ContextRunner, ServiceManager]
Registry is a type alias for the plumbing.Provider type specialized for initsystem ServiceManagers.
type Runit ¶
type Runit struct{}
Runit is an init system implementation for runit.
func (Runit) DisableService ¶
DisableService removes the symlink from the runit service directory to disable a service.
func (Runit) EnableService ¶
EnableService creates a symlink in the runit service directory to enable a service.
func (Runit) RestartService ¶
RestartService restarts a runit service.
func (Runit) ServiceIsRunning ¶
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 ¶
ServiceScriptPath returns the path to a runit service script.
func (Runit) StartService ¶
StartService starts a runit service.
func (Runit) StopService ¶
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.
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 ¶
DisableService for SysVinit tries to remove symlinks in the appropriate runlevel directories.
func (SysVinit) EnableService ¶
EnableService for SysVinit tries to create symlinks in the appropriate runlevel directories.
func (SysVinit) RestartService ¶
RestartService restarts a service.
func (SysVinit) ServiceIsRunning ¶
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 ¶
StartService starts a service.
func (SysVinit) StopService ¶
StopService stops a service.
type Systemd ¶
type Systemd struct{}
Systemd is found by default on most linux distributions today.
func (Systemd) DaemonReload ¶
DaemonReload reloads init system configuration.
func (Systemd) DisableService ¶
DisableService disables a service.
func (Systemd) EnableService ¶
EnableService enables a service.
func (Systemd) RestartService ¶
RestartService restarts a service.
func (Systemd) ServiceEnvironmentContent ¶
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 ¶
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 ¶
StartService starts a service.
func (Systemd) StopService ¶
StopService stops a service.
type Upstart ¶
type Upstart struct{}
Upstart is the init system used by Ubuntu 14.04 and older.
func (Upstart) DisableService ¶
DisableService for Upstart.
func (Upstart) EnableService ¶
EnableService for Upstart (might involve creating a symlink or managing an override file).
func (Upstart) RestartService ¶
RestartService restarts a service.
func (Upstart) ServiceIsRunning ¶
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 ¶
StartService starts a service.
func (Upstart) StopService ¶
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.
type WinSCM ¶
type WinSCM struct{}
WinSCM is a struct that implements the InitSystem interface for Windows Service Control Manager.
func (WinSCM) DisableService ¶
DisableService disables a service by setting its startup type to Disabled.
func (WinSCM) EnableService ¶
EnableService enables a service by setting its startup type to Automatic.
func (WinSCM) RestartService ¶
RestartService restarts a service.
func (WinSCM) ServiceIsRunning ¶
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 ¶
ServiceScriptPath returns the path to a service configuration file.
func (WinSCM) StartService ¶
StartService starts a service.
func (WinSCM) StopService ¶
StopService stops a service.