initsystem

package
v2.0.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 13 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 (
	// 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

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

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.

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.

type Provider

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

func NewProvider

func NewProvider() *Provider

NewProvider returns a new Provider.

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.

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

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.

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(ctx context.Context, h cmd.ContextRunner, s string) (string, error)

ServiceEnvironmentPath returns a path to an environment override file path.

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.

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.

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.

func (WinSCM) EnableService

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

EnableService enables a service.

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 the logs for a service.

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.

Jump to

Keyboard shortcuts

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