-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemd.go
More file actions
302 lines (260 loc) · 6.76 KB
/
Copy pathsystemd.go
File metadata and controls
302 lines (260 loc) · 6.76 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
const servicePrefix = "krun-"
const serviceTemplateSrc = `[Unit]
Description=krun: {{.Name}}
After=default.target
[Service]
Type=simple
{{- if .LogDir}}
ExecStart=/bin/bash -c 'mkdir -p {{.LogDir}} && exec {{.ExecCmd}} >>{{.LogDir}}/{{.Name}}-$(date +%%Y-%%m-%%d).log 2>>{{.LogDir}}/{{.Name}}-$(date +%%Y-%%m-%%d)-error.log'
{{- else}}
ExecStart=/bin/bash -c 'exec {{.ExecCmd}}'
{{- end}}
WorkingDirectory={{.Cwd}}
{{- if .AutoRestart}}
Restart=always
RestartSec=3
{{- else}}
Restart=no
{{- end}}
{{- range .EnvLines}}
Environment="{{.}}"
{{- end}}
{{- if .MaxMemory}}
MemoryMax={{.MaxMemory}}
{{- end}}
{{- if and .LogFile (not .LogDir)}}
StandardOutput=append:{{.LogFile}}
{{- end}}
{{- if and .ErrorFile (not .LogDir)}}
StandardError=append:{{.ErrorFile}}
{{- end}}
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
[Install]
WantedBy=default.target
`
const timerTemplateSrc = `[Unit]
Description=krun restart timer: {{.Name}}
[Timer]
OnCalendar={{.Schedule}}
Persistent=true
[Install]
WantedBy=timers.target
`
const restartServiceTemplateSrc = `[Unit]
Description=krun restart trigger: {{.Name}}
[Service]
Type=oneshot
ExecStart=/bin/systemctl --user restart {{.ServiceUnit}}
`
type serviceData struct {
Name string
ExecCmd string
Cwd string
AutoRestart bool
EnvLines []string
MaxMemory string
LogFile string
ErrorFile string
LogDir string
}
func userServiceDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config", "systemd", "user")
}
func unitName(name string) string {
return servicePrefix + name + ".service"
}
func unitFilePath(name string) string {
return filepath.Join(userServiceDir(), unitName(name))
}
func timerName(name string) string {
return servicePrefix + name + "-restart.timer"
}
func timerFilePath(name string) string {
return filepath.Join(userServiceDir(), timerName(name))
}
func restartUnitName(name string) string {
return servicePrefix + name + "-restart.service"
}
func restartUnitFilePath(name string) string {
return filepath.Join(userServiceDir(), restartUnitName(name))
}
func systemctl(args ...string) error {
cmd := exec.Command("systemctl", append([]string{"--user"}, args...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func systemctlQuiet(args ...string) error {
cmd := exec.Command("systemctl", append([]string{"--user"}, args...)...)
return cmd.Run()
}
func systemctlOutput(args ...string) (string, error) {
out, err := exec.Command("systemctl", append([]string{"--user"}, args...)...).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
func getUnitProperty(unit, prop string) string {
val, _ := systemctlOutput("show", "-p", prop, "--value", unit)
return val
}
func ensureLinger() {
user := os.Getenv("USER")
if user == "" {
return
}
lingerPath := filepath.Join("/var/lib/systemd/linger", user)
if _, err := os.Stat(lingerPath); err != nil {
exec.Command("loginctl", "enable-linger", user).Run()
}
}
func disableLinger() {
user := os.Getenv("USER")
if user != "" {
exec.Command("loginctl", "disable-linger", user).Run()
}
}
func daemonReload() error {
return systemctlQuiet("daemon-reload")
}
func generateServiceFile(app AppConfig) error {
dir := userServiceDir()
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("cannot create service dir: %w", err)
}
execCmd := app.Cmd
if app.Interpreter != "" {
execCmd = app.Interpreter + " " + app.Cmd
}
// Escape single quotes in the command for bash -c 'exec ...'
execCmd = strings.ReplaceAll(execCmd, "'", "'\"'\"'")
var envLines []string
for k, v := range app.Env {
envLines = append(envLines, k+"="+v)
}
data := serviceData{
Name: app.Name,
ExecCmd: execCmd,
Cwd: app.Cwd,
AutoRestart: app.ShouldAutoRestart(),
EnvLines: envLines,
MaxMemory: app.MaxMemory,
LogFile: app.LogFile,
ErrorFile: app.ErrorFile,
LogDir: app.LogDir,
}
tmpl, err := template.New("service").Parse(serviceTemplateSrc)
if err != nil {
return err
}
f, err := os.Create(unitFilePath(app.Name))
if err != nil {
return err
}
defer f.Close()
return tmpl.Execute(f, data)
}
func generateTimerFiles(app AppConfig) error {
if app.CronRestart == "" {
return nil
}
dir := userServiceDir()
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
// Generate restart service
restartTmpl, _ := template.New("restart").Parse(restartServiceTemplateSrc)
rf, err := os.Create(restartUnitFilePath(app.Name))
if err != nil {
return err
}
restartTmpl.Execute(rf, struct {
Name string
ServiceUnit string
}{app.Name, unitName(app.Name)})
rf.Close()
// Generate timer
timerTmpl, _ := template.New("timer").Parse(timerTemplateSrc)
tf, err := os.Create(timerFilePath(app.Name))
if err != nil {
return err
}
timerTmpl.Execute(tf, struct {
Name string
Schedule string
}{app.Name, app.CronRestart})
tf.Close()
return nil
}
func removeTimerFiles(name string) {
systemctlQuiet("stop", timerName(name))
systemctlQuiet("disable", timerName(name))
os.Remove(timerFilePath(name))
os.Remove(restartUnitFilePath(name))
}
func listKrunUnits() []string {
dir := userServiceDir()
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}
var units []string
for _, e := range entries {
n := e.Name()
if strings.HasPrefix(n, servicePrefix) && strings.HasSuffix(n, ".service") && !strings.Contains(n, "-restart.service") {
units = append(units, n)
}
}
return units
}
func unitToAppName(unit string) string {
name := strings.TrimPrefix(unit, servicePrefix)
name = strings.TrimSuffix(name, ".service")
return name
}
func parseServiceFileFields(path string) (cmd, cwd string, env map[string]string) {
data, err := os.ReadFile(path)
if err != nil {
return "?", "?", nil
}
env = make(map[string]string)
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
switch {
case strings.HasPrefix(line, "ExecStart=/bin/bash -c 'exec "):
cmd = strings.TrimPrefix(line, "ExecStart=/bin/bash -c 'exec ")
cmd = strings.TrimSuffix(cmd, "'")
cmd = strings.ReplaceAll(cmd, "'\"'\"'", "'")
case strings.HasPrefix(line, "ExecStart="):
if cmd == "" {
cmd = strings.TrimPrefix(line, "ExecStart=")
}
case strings.HasPrefix(line, "WorkingDirectory="):
cwd = strings.TrimPrefix(line, "WorkingDirectory=")
case strings.HasPrefix(line, "Environment=\"") && !strings.Contains(line, "PATH="):
val := strings.TrimPrefix(line, "Environment=\"")
val = strings.TrimSuffix(val, "\"")
if idx := strings.Index(val, "="); idx > 0 {
env[val[:idx]] = val[idx+1:]
}
}
}
if cmd == "" {
cmd = "?"
}
if cwd == "" {
cwd = "?"
}
return
}