1
2
3
4
19
20 package initsystem
21
22 import (
23 "fmt"
24 "os/exec"
25 "strings"
26
27 "github.com/pkg/errors"
28 )
29
30
31 type OpenRCInitSystem struct{}
32
33
34 func (openrc OpenRCInitSystem) ServiceStart(service string) error {
35 args := []string{service, "start"}
36 return exec.Command("rc-service", args...).Run()
37 }
38
39
40 func (openrc OpenRCInitSystem) ServiceStop(service string) error {
41 args := []string{service, "stop"}
42 return exec.Command("rc-service", args...).Run()
43 }
44
45
46 func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
47 args := []string{service, "restart"}
48 return exec.Command("rc-service", args...).Run()
49 }
50
51
52
53
54
55 func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
56 args := []string{service, "status"}
57 outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
58 return !strings.Contains(string(outBytes), "does not exist")
59 }
60
61
62 func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
63 args := []string{"show", "default"}
64 outBytes, _ := exec.Command("rc-update", args...).Output()
65 return strings.Contains(string(outBytes), service)
66 }
67
68
69 func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
70 args := []string{service, "status"}
71 outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
72 outStr := string(outBytes)
73 return !strings.Contains(outStr, "stopped") && !strings.Contains(outStr, "does not exist")
74 }
75
76
77 func (openrc OpenRCInitSystem) EnableCommand(service string) string {
78 return fmt.Sprintf("rc-update add %s default", service)
79 }
80
81
82 type SystemdInitSystem struct{}
83
84
85 func (sysd SystemdInitSystem) EnableCommand(service string) string {
86 return fmt.Sprintf("systemctl enable %s.service", service)
87 }
88
89
90 func (sysd SystemdInitSystem) reloadSystemd() error {
91 if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
92 return errors.Wrap(err, "failed to reload systemd")
93 }
94 return nil
95 }
96
97
98 func (sysd SystemdInitSystem) ServiceStart(service string) error {
99
100 if err := sysd.reloadSystemd(); err != nil {
101 return err
102 }
103 args := []string{"start", service}
104 return exec.Command("systemctl", args...).Run()
105 }
106
107
108 func (sysd SystemdInitSystem) ServiceRestart(service string) error {
109
110 if err := sysd.reloadSystemd(); err != nil {
111 return err
112 }
113 args := []string{"restart", service}
114 return exec.Command("systemctl", args...).Run()
115 }
116
117
118 func (sysd SystemdInitSystem) ServiceStop(service string) error {
119 args := []string{"stop", service}
120 return exec.Command("systemctl", args...).Run()
121 }
122
123
124 func (sysd SystemdInitSystem) ServiceExists(service string) bool {
125 args := []string{"status", service}
126 outBytes, _ := exec.Command("systemctl", args...).Output()
127 output := string(outBytes)
128 return !strings.Contains(output, "Loaded: not-found") && !strings.Contains(output, "could not be found")
129 }
130
131
132 func (sysd SystemdInitSystem) ServiceIsEnabled(service string) bool {
133 args := []string{"is-enabled", service}
134 err := exec.Command("systemctl", args...).Run()
135 return err == nil
136 }
137
138
139
140
141 func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
142 args := []string{"is-active", service}
143
144 outBytes, _ := exec.Command("systemctl", args...).Output()
145 output := strings.TrimSpace(string(outBytes))
146 if output == "active" || output == "activating" {
147 return true
148 }
149 return false
150 }
151
152
153
154
155 func GetInitSystem() (InitSystem, error) {
156
157 _, err := exec.LookPath("systemctl")
158 if err == nil {
159 return &SystemdInitSystem{}, nil
160 }
161 _, err = exec.LookPath("openrc")
162 if err == nil {
163 return &OpenRCInitSystem{}, nil
164 }
165
166 return nil, errors.New("no supported init system detected, skipping checking for services")
167 }
168
View as plain text