systemd

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnabledAndRunningProperties = UnitProperties{
	"LoadState":     "loaded",
	"ActiveState":   "active",
	"SubState":      "running",
	"UnitFileState": "enabled",
}

EnabledAndRunningProperties are properties that a systemd service unit should have to be considered active and running.

View Source
var KnownServices = []string{"boundary", "consul", "vault"}

KnownServices are list of known HashiCorp services.

Functions

This section is empty.

Types

type Client

type Client interface {
	// CreateUnitFile creates a systemd unit file for the provided request
	CreateUnitFile(ctx context.Context, req *CreateUnitFileRequest) error
	// EnableService enables a systemd service with the provided unit name
	EnableService(ctx context.Context, unit string) error
	// GetUnitJournal gets the journal for a systemd unit
	GetUnitJournal(ctx context.Context, req *GetUnitJournalRequest) (remoteflight.GetLogsResponse, error)
	// ListServices gets the list of systemd services installed
	ListServices(ctx context.Context) ([]ServiceInfo, error)
	// RestartService restarts a systemd service with the provided unit name
	RestartService(ctx context.Context, unit string) error
	// RunSystemctlCommand runs a systemctl command for the provided request
	RunSystemctlCommand(ctx context.Context, req *SystemctlCommandReq) (*SystemctlCommandRes, error)
	// ShowProperties gets the properties of a matching unit or job
	ShowProperties(ctx context.Context, unit string) (UnitProperties, error)
	// StartService starts a systemd service with the provided unit name
	StartService(ctx context.Context, unit string) error
	// StopService stops a systemd service with the provided unit name
	StopService(ctx context.Context, unit string) error
	// ServiceStatus gets the service status for a systemd service with the provided unit name
	ServiceStatus(ctx context.Context, unit string) SystemctlStatusCode
}

Client an interface for a sysetmd client.

func NewClient

func NewClient(transport it.Transport, logger log.Logger) Client

NewClient takes a transport and logger and returns a new systemd Client.

type CreateUnitFileOpt

type CreateUnitFileOpt func(*CreateUnitFileRequest) *CreateUnitFileRequest

CreateUnitFileOpt is a functional option for an systemd unit request.

func WithUnitChmod

func WithUnitChmod(chmod string) CreateUnitFileOpt

WithUnitChmod sets systemd unit permissions.

func WithUnitChown

func WithUnitChown(chown string) CreateUnitFileOpt

WithUnitChown sets systemd unit ownership.

func WithUnitFile

func WithUnitFile(unit Iniable) CreateUnitFileOpt

WithUnitFile sets systemd unit to use.

func WithUnitUnitPath

func WithUnitUnitPath(path string) CreateUnitFileOpt

WithUnitUnitPath sets the unit name.

type CreateUnitFileRequest

type CreateUnitFileRequest struct {
	Unit     Iniable
	UnitPath string
	Chmod    string
	Chown    string
}

CreateUnitFileRequest is a systemd unit file creator.

func NewCreateUnitFileRequest

func NewCreateUnitFileRequest(opts ...CreateUnitFileOpt) *CreateUnitFileRequest

NewCreateUnitFileRequest takes functional options and returns a new systemd unit request.

type GetUnitJournalRequest

type GetUnitJournalRequest struct {
	Unit string
	Host string
}

type GetUnitJournalResponse

type GetUnitJournalResponse struct {
	Unit string
	Host string
	Logs []byte
}

func (*GetUnitJournalResponse) GetAppName

func (s *GetUnitJournalResponse) GetAppName() string

GetAppName implements remoteflight.GetUnitJournalResponse.

func (*GetUnitJournalResponse) GetLogFileName

func (s *GetUnitJournalResponse) GetLogFileName() string

func (*GetUnitJournalResponse) GetLogs

func (s *GetUnitJournalResponse) GetLogs() []byte

type Iniable

type Iniable interface {
	ToIni() (string, error)
}

Iniable is an interface for a type that can be converted into a systemd unit.

type RunSystemctlCommandOpt

type RunSystemctlCommandOpt func(*SystemctlCommandReq) *SystemctlCommandReq

RunSystemctlCommandOpt is a functional option for an systemd unit request.

func WithSystemctlCommandOptions

func WithSystemctlCommandOptions(options string) RunSystemctlCommandOpt

WithSystemctlCommandOptions sets any optional options to pass in.

func WithSystemctlCommandPattern

func WithSystemctlCommandPattern(pattern string) RunSystemctlCommandOpt

WithSystemctlCommandPattern sets any optional pattern to pass in.

func WithSystemctlCommandSubCommand

func WithSystemctlCommandSubCommand(cmd SystemctlSubCommand) RunSystemctlCommandOpt

WithSystemctlCommandSubCommand sets the systemctl sub-command.

func WithSystemctlCommandUnitName

func WithSystemctlCommandUnitName(unit string) RunSystemctlCommandOpt

WithSystemctlCommandUnitName sets the command unit name.

func WithSystemctlCommandUnitType

func WithSystemctlCommandUnitType(typ UnitType) RunSystemctlCommandOpt

WithSystemctlCommandUnitType sets the command unit type.

func WithSystemctlCommandUser

func WithSystemctlCommandUser() RunSystemctlCommandOpt

WithSystemctlCommandUser sets command to --user mode.

type ServiceInfo

type ServiceInfo struct {
	Unit        string
	Load        string
	Active      string
	Sub         string
	Description string
}

ServiceInfo is a list units of type service from systemctl command reference https://man7.org/linux/man-pages/man1/systemctl.1.html#COMMANDS

type SystemctlCommandReq

type SystemctlCommandReq struct {
	User       bool                // enable user mode
	Name       string              // unit name
	Type       UnitType            // unit type
	SubCommand SystemctlSubCommand // systemctl command
	Pattern    string              // optional command pattern to pass as args
	Options    string              // optional command options to pass as args
	Env        map[string]string   // any environment variables that need to be set
}

SystemctlCommandReq is a sysmtemctl command request.

func NewRunSystemctlCommand

func NewRunSystemctlCommand(opts ...RunSystemctlCommandOpt) *SystemctlCommandReq

NewRunSystemctlCommand takes functional options and returns a new systemd command.

func (*SystemctlCommandReq) String

func (c *SystemctlCommandReq) String() (string, error)

String returns the command request as a systemctl string.

type SystemctlCommandRes

type SystemctlCommandRes struct {
	Stdout string
	Stderr string
	Status SystemctlStatusCode
}

SystemctlCommandRes is the command response.

type SystemctlStatusCode

type SystemctlStatusCode int

SystemctlStatusCode is a systemctl exit code. Systemctl attempts to use LSB exit codes, however, there are lots of corner cases which make certain codes unreliable and not all sub-commands adhere to these codes. As such, we should strive to interpret a units state by looking at its properties whenever possible

Further reading:

const (
	StatusOK         SystemctlStatusCode = 0
	StatusNotFailed  SystemctlStatusCode = 1
	StatusNotActive  SystemctlStatusCode = 3
	StatusNoSuchUnit SystemctlStatusCode = 4
	StatusUnknown    SystemctlStatusCode = 9
)

func (SystemctlStatusCode) String

func (s SystemctlStatusCode) String() string

type SystemctlSubCommand

type SystemctlSubCommand int

SystemctlSubCommand is the systemctl sub command to use.

const (
	SystemctlSubCommandNotSet SystemctlSubCommand = iota
	SystemctlSubCommandDaemonReload
	SystemctlSubCommandEnable
	SystemctlSubCommandIsActive
	SystemctlSubCommandKill
	SystemctlSubCommandListUnits
	SystemctlSubCommandReload
	SystemctlSubCommandRestart
	SystemctlSubCommandShow
	SystemctlSubCommandStart
	SystemctlSubCommandStatus
	SystemctlSubCommandStop
)

SystemctlSubCommands are the systemctl sub commands.

type Unit

type Unit map[string]map[string]string

Unit is a map structure representing any systemd unit. The first keys represent stanzas and the values of each stanza is a map of filed names and values.

func (Unit) ToIni

func (s Unit) ToIni() (string, error)

ToIni converts a Unit to the textual representation. Due to go maps not being ordered, the Unit may render differently each time that the function is called. In testing this hasn't shown any negative effects but might be confusing.

type UnitProperties

type UnitProperties map[string]string

UnitProperties are a key value map of unit properties.

func NewUnitProperties

func NewUnitProperties() UnitProperties

NewUnitProperties returns a new NewUnitProperties.

func (UnitProperties) Find

func (s UnitProperties) Find(names ...string) (UnitProperties, error)

Find takes one-or-more property names and returns a set of unit properties that match the names. Unless all properties are found an error will be returned.

func (UnitProperties) FindProperties

func (s UnitProperties) FindProperties(in UnitProperties) (UnitProperties, error)

FindProperties is like Find() but instead of a list of values it derives the key name from those defined in the pass in UnitProperties. The values of properties in the given set are ignored.

func (UnitProperties) HasProperties

func (s UnitProperties) HasProperties(in UnitProperties) bool

HasProperties determines whether the current unit properties contains all properties of another property set. This does not enfore exact comparison, only that the first set shares all from the second set.

func (UnitProperties) String

func (s UnitProperties) String() string

type UnitType

type UnitType int

UnitType is the systemd unit type to operate on.

const (
	UnitTypeNotSet UnitType = iota
	UnitTypeService
	UnitTypeSocket
	UnitTypeDevice
	UnitTypeMount
	UnitTypeAutomount
	UnitTypeSwap
	UnitTypeTarget
	UnitTypePath
	UnitTypeTimer
	UnitTypeSlice
	UnitTypeScope
)

SystemdUnitTypes are the system unit types.

func (UnitType) String

func (u UnitType) String() string

Jump to

Keyboard shortcuts

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