Documentation
¶
Overview ¶
Package initsystem provides a common interface for interacting with init systems like systemd, openrc, sysvinit, etc.
Index ¶
- Variables
- func RegisterLaunchd(repo *Provider)
- func RegisterOpenRC(repo *Provider)
- func RegisterRunit(repo *Provider)
- func RegisterSysVinit(repo *Provider)
- func RegisterSystemd(repo *Provider)
- func RegisterUpstart(repo *Provider)
- func RegisterWinSCM(repo *Provider)
- type Factory
- type InitSystemProvider
- 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
- 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
- type Provider
- 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
- type Service
- type ServiceEnvironmentManager
- type ServiceManager
- type ServiceManagerLogReader
- 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
- 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(ctx context.Context, h 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
- 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
- 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
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultProvider is the default repository for init systems. DefaultProvider = sync.OnceValue(func() *Provider { provider := NewProvider() 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 *Provider)
RegisterLaunchd registers the launchd init system to a init system repository.
func RegisterOpenRC ¶
func RegisterOpenRC(repo *Provider)
RegisterOpenRC registers OpenRC to a repository.
func RegisterRunit ¶
func RegisterRunit(repo *Provider)
RegisterRunit register runit in a repository.
func RegisterSysVinit ¶
func RegisterSysVinit(repo *Provider)
RegisterSysVinit registers SysVinit in a repository.
func RegisterSystemd ¶
func RegisterSystemd(repo *Provider)
RegisterSystemd registers systemd into a repository.
func RegisterUpstart ¶
func RegisterUpstart(repo *Provider)
RegisterUpstart registers Upstart in a repository.
func RegisterWinSCM ¶
func RegisterWinSCM(repo *Provider)
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 InitSystemProvider ¶
type InitSystemProvider interface {
Get(conn cmd.ContextRunner) (ServiceManager, error)
}
InitSystemProvider is a function that returns a ServiceManager given a runner.
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 = plumbing.Provider[cmd.ContextRunner, ServiceManager]
Provider 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.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 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 managament operations. It supports operations like starting and stopping services.
func NewInitSystemService ¶
func NewInitSystemService(provider InitSystemProvider, runner cmd.ContextRunner) *Service
NewInitSystemService creates a new instance of PackageManagerService with the provided PackageManagerProvider.
func (*Service) GetServiceManager ¶
func (p *Service) GetServiceManager() (ServiceManager, error)
GetServiceManager returns a ServiceManager or an error if a service manager could not be initialized.
func (*Service) ServiceManager ¶
func (p *Service) ServiceManager() ServiceManager
ServiceManager provides easy access to the underlying init system service manager instance. It initializes the service manager if it has not been initialized yet. If the initialization fails, a NullServiceManager instance is returned which will return the initialization error on every operation that is attempted on it.
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 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(ctx context.Context, h cmd.ContextRunner, s string) (string, error)
ServiceEnvironmentPath returns a path to an environment override file path.
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.
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.
func (WinSCM) EnableService ¶
EnableService enables a service.
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 the logs for a service.
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.