50
50
// systemctl. It's exported so it can be overridden by testing.
51
51
var SystemctlCmd = run
53
// SystemCtl exposes a minimal interface to manage systemd via the systemctl command.
54
type SystemCtl interface {
53
// Systemd exposes a minimal interface to manage systemd via the systemctl command.
54
type Systemd interface {
55
55
DaemonReload() error
56
56
Enable(service string) error
57
57
Disable(service string) error
59
59
Stop(service string) error
62
// New returns a SystemCtl that uses the given rootDir
63
func New(rootDir string) SystemCtl {
64
return &systemCtl{rootDir: rootDir}
62
// New returns a Systemd that uses the given rootDir
63
func New(rootDir string) Systemd {
64
return &systemd{rootDir: rootDir}
67
type systemCtl struct {
71
71
// DaemonReload reloads systemd's configuration.
72
func (*systemCtl) DaemonReload() error {
72
func (*systemd) DaemonReload() error {
73
73
_, err := SystemctlCmd("daemon-reload")
77
77
// Enable the given service
78
func (s *systemCtl) Enable(serviceName string) error {
78
func (s *systemd) Enable(serviceName string) error {
79
79
_, err := SystemctlCmd("--root", s.rootDir, "enable", serviceName)
83
83
// Disable the given service
84
func (s *systemCtl) Disable(serviceName string) error {
84
func (s *systemd) Disable(serviceName string) error {
85
85
_, err := SystemctlCmd("--root", s.rootDir, "disable", serviceName)
89
89
// Start the given service
90
func (*systemCtl) Start(serviceName string) error {
90
func (*systemd) Start(serviceName string) error {
91
91
_, err := SystemctlCmd("start", serviceName)
95
95
// Stop the given service, and wait until it has stopped.
96
func (*systemCtl) Stop(serviceName string) error {
96
func (*systemd) Stop(serviceName string) error {
97
97
if _, err := SystemctlCmd("stop", serviceName); err != nil {
129
129
return fmt.Sprintf("%v failed with exit status %d", e.cmd, e.exitCode)
132
// Timeout is returned if the systemctl action failed to reach the
132
// Timeout is returned if the systemd action failed to reach the
133
133
// expected state in a reasonable amount of time
134
134
type Timeout struct {