-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice_linux-systemd.go
More file actions
269 lines (215 loc) · 6.37 KB
/
service_linux-systemd.go
File metadata and controls
269 lines (215 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// +build linux
package service
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
logging "github.com/codemodify/systemkit-logging"
encoders "github.com/codemodify/systemkit-service-encoders-systemd"
spec "github.com/codemodify/systemkit-service-spec"
"github.com/codemodify/systemkit-service/helpers"
)
var logTagSystemD = "SystemD-SERVICE"
type systemdService struct {
serviceSpec spec.SERVICE
useConfigAsFileContent bool
fileContentTemplate string
}
func newServiceFromSERVICE_SystemD(serviceSpec spec.SERVICE) Service {
logging.Debugf("%s: spec.SERVICE object: %s", logTagSystemD, helpers.AsJSONString(serviceSpec))
return &systemdService{
serviceSpec: serviceSpec,
useConfigAsFileContent: true,
}
}
func newServiceFromName_SystemD(name string) (Service, error) {
fileContent := []byte{}
var err error
if helpers.IsRoot() {
serviceFile := filepath.Join("/etc/systemd/system", name+".service")
fileContent, err = ioutil.ReadFile(serviceFile)
if err != nil {
serviceFile = filepath.Join("/usr/lib/systemd/system", name+".service")
fileContent, err = ioutil.ReadFile(serviceFile)
if err != nil {
return nil, ErrServiceDoesNotExist
}
}
} else {
serviceFile := filepath.Join(helpers.HomeDir(""), ".config/systemd/user", name+".service")
fileContent, err = ioutil.ReadFile(serviceFile)
if err != nil {
return nil, ErrServiceDoesNotExist
}
}
return newServiceFromPlatformTemplate_SystemD(name, string(fileContent))
}
func newServiceFromPlatformTemplate_SystemD(name string, template string) (Service, error) {
logging.Debugf("%s: template: %s", logTagSystemD, template)
serviceSpec := encoders.SystemDToSERVICE(template)
return &systemdService{
serviceSpec: serviceSpec,
useConfigAsFileContent: false,
fileContentTemplate: template,
}, nil
}
func (thisRef systemdService) Install() error {
dir := filepath.Dir(thisRef.filePath())
// 1.
logging.Debugf("making sure folder exists: %s", dir)
os.MkdirAll(dir, os.ModePerm)
// 2.
logging.Debugf("generating unit file")
fileContent := encoders.SERVICEToSystemD(thisRef.serviceSpec)
if !thisRef.useConfigAsFileContent {
fileContent = thisRef.fileContentTemplate
}
logging.Debugf("writing unit to: %s", thisRef.filePath())
err := ioutil.WriteFile(thisRef.filePath(), []byte(fileContent), 0644)
if err != nil {
return err
}
logging.Debugf("wrote unit: %s", fileContent)
return nil
}
func (thisRef systemdService) Uninstall() error {
// 1.
logging.Debugf("%s: attempting to uninstall: %s", logTagSystemD, thisRef.serviceSpec.Name)
// 2.
err := thisRef.Stop()
if err != nil && !helpers.Is(err, ErrServiceDoesNotExist) {
return err
}
// 3.
logging.Debugf("remove unit file")
err = os.Remove(thisRef.filePath())
if e, ok := err.(*os.PathError); ok {
if os.IsNotExist(e.Err) {
return nil
}
}
return err
}
func (thisRef systemdService) Start() error {
// 1.
logging.Debugf("reloading daemon")
output, err := runSystemCtlCommand("daemon-reload")
if err != nil {
return err
}
// 2.
logging.Debugf("enabling unit file with systemd")
output, err = runSystemCtlCommand("enable", thisRef.serviceSpec.Name)
if err != nil {
if strings.Contains(output, "Failed to enable unit") && strings.Contains(output, "does not exist") {
return ErrServiceDoesNotExist
}
return err
}
// 3.
logging.Debugf("loading unit file with systemd")
output, err = runSystemCtlCommand("start", thisRef.serviceSpec.Name)
if err != nil {
if strings.Contains(output, "Failed to start") && strings.Contains(output, "not found") {
return ErrServiceDoesNotExist
}
return err
}
return nil
}
func (thisRef systemdService) Stop() error {
// 1.
logging.Debugf("reloading daemon")
_, err := runSystemCtlCommand("daemon-reload")
if err != nil {
return err
}
// 2.
logging.Debugf("stopping unit file with systemd")
output, err := runSystemCtlCommand("stop", thisRef.serviceSpec.Name)
if err != nil {
if strings.Contains(output, "Failed to stop") && strings.Contains(output, "not loaded") {
return ErrServiceDoesNotExist
}
return err
}
// 3.
logging.Debugf("disabling unit file with systemd")
output, err = runSystemCtlCommand("disable", thisRef.serviceSpec.Name)
if err != nil {
logging.Warningf("stopping unit file with systemd")
if strings.Contains(output, "Failed to disable") && strings.Contains(output, "does not exist") {
return ErrServiceDoesNotExist
} else if strings.Contains(output, "Removed") {
return nil
}
return err
}
// 4.
logging.Debugf("reloading daemon")
_, err = runSystemCtlCommand("daemon-reload")
if err != nil {
return err
}
// 5.
logging.Debugf("running reset-failed")
_, err = runSystemCtlCommand("reset-failed")
if err != nil {
return err
}
return nil
}
func (thisRef systemdService) Info() Info {
fileContent, _ := ioutil.ReadFile(thisRef.filePath())
result := Info{
Error: nil,
Service: thisRef.serviceSpec,
IsRunning: false,
PID: -1,
FilePath: thisRef.filePath(),
FileContent: string(fileContent),
}
output, err := runSystemCtlCommand("status", thisRef.serviceSpec.Name)
if err != nil {
result.Error = err
return result
}
if strings.Contains(output, "could not be found") {
result.Error = ErrServiceDoesNotExist
return result
}
for _, line := range strings.Split(output, "\n") {
if strings.Contains(line, "Main PID") {
lineParts := strings.Split(strings.TrimSpace(line), " ")
if len(lineParts) >= 2 {
result.PID, _ = strconv.Atoi(lineParts[2])
}
} else if strings.Contains(line, "Active") {
if strings.Contains(line, "active (running)") {
result.IsRunning = true
}
}
}
return result
}
func (thisRef systemdService) filePath() string {
if helpers.IsRoot() {
return filepath.Join("/etc/systemd/system", thisRef.serviceSpec.Name+".service")
}
return filepath.Join(helpers.HomeDir(""), ".config/systemd/user", thisRef.serviceSpec.Name+".service")
}
func runSystemCtlCommand(args ...string) (string, error) {
if !helpers.IsRoot() {
args = append([]string{"--user"}, args...)
}
logging.Debugf("%s: RUN-SYSTEMCTL: systemctl %s", logTagSystemD, strings.Join(args, " "))
output, err := helpers.ExecWithArgs("systemctl", args...)
errAsString := ""
if err != nil {
errAsString = err.Error()
}
logging.Debugf("%s: RUN-SYSTEMCTL-OUT: output: %s, error: %s", logTagSystemD, output, errAsString)
return output, err
}