-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
366 lines (337 loc) · 7.19 KB
/
main.go
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"bufio"
"errors"
"io"
"log"
"os"
"path/filepath"
"slices"
"strings"
"text/template"
)
const defaultTemplate = `#!/bin/sh
# /etc/init.d/{{.Description}}
#
# {{.Description}}
#
### BEGIN INIT INFO
# Provides: {{.Name}}
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: {{.Description}}
### END INIT INFO
_DONE=".spinstop"
_CLR=1
cln() {
tput rc
tput cnorm
return 1
}
trap cln INT QUIT TERM
spinner() {
if [ -t 1 ]; then
_SYM=". o O o"
_ppid=$(ps -p "$$" -o ppid=)
while :; do
tput civis
for c in $_SYM; do
tput sc
env printf "$c"
tput rc
if [ -f "$_DONE" ]; then
if [ $_CLR -eq 1 ]; then
tput el
fi
rm "$_DONE"
break 2
fi
env sleep .2
if [ -n "$_ppid" ]; then
_pup=$(ps --no-headers "$_ppid")
if [ -z "$_pup" ]; then
break 2
fi
fi
done
done
tput cnorm
return 0
fi
}
PIDFILE="/var/run/{{.Name}}.pid"
LOGFILE="/var/log/{{.Name}}.log"
SCRIPT="{{.ExecStart}}"
RUNAS="{{.User}}"
start() {
if [ -f "$PIDFILE" ] && [ -s "$PIDFILE" ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running, PID: $(cat PIDFILE)' >&2
return 1
fi
spinner &
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
sleep 2
PID=$(cat $PIDFILE)
if pgrep -u $RUNAS -f $NAME > /dev/null
then
touch $_DONE
echo "$NAME is now running, the PID is $PID"
else
touch $_DONE
echo ''
echo "Error! Could not start $NAME, check $LOGFILE for more information."
fi
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
spinner &
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
touch $_DONE
echo 'Service stopped' >&2
}
status() {
printf "%-50s" "Checking {{.Name}}..."
spinner &
if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
touch $_DONE
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
touch $_DONE
echo "Running, the PID is $PID"
fi
else
touch $_DONE
printf "%s\n" "Service not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
{{if .HasReload}}
reload)
echo "Reloading {{.Name}}"
spinner &
{{.ExecReload}}
touch $_DONE
;;
{{end}}
status)
status()
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|reload|status|restart}"
exit 1
;;
esac
exit 0
`
type BrokenService struct {
Name string
Description string
Documentation string
ExecStart string
ExecStop string
ExecReload string
PartOf string
Service string
Restart string
RuntimeDirectory string
RemainAfterExit bool
HasReload bool
Requires string
Wants string
After string
Before string
User string
Group string
}
func (bs *BrokenService) parseLine(line string) {
var index = map[string]*string{
"Description": &bs.Description, "Documentation": &bs.Documentation,
"ExecStart": &bs.ExecStart, "ExecStop": &bs.ExecStop,
"ExecReload": &bs.ExecReload, "Restart": &bs.Restart,
"Requires": &bs.Requires, "Wants": &bs.Wants,
"After": &bs.After, "Before": &bs.Before,
"RuntimeDirectory": &bs.RuntimeDirectory, "#RuntimeDirectory": &bs.RuntimeDirectory,
}
for key, value := range index {
if strings.HasPrefix(strings.TrimSpace(line), key+"=") {
*value = strings.TrimPrefix(line, key+"=")
return
}
}
}
type ServiceFixer struct {
Name string
Template *template.Template
Corpse BrokenService
buf []byte
}
func (s *ServiceFixer) Write(p []byte) (int, error) {
reader, writer := io.Pipe()
type writeRes struct {
n int
err error
}
resChan := make(chan writeRes)
go func() {
defer func() {
_ = writer.Close()
}()
n, err := writer.Write(p)
resChan <- writeRes{n, err}
}()
sad, sadErr := ReadBrokenService(reader)
if sadErr != nil {
return 0, sadErr
}
s.Corpse = sad
s.Template = template.Must(template.New("sysvinit").Parse(defaultTemplate))
s.buf = make([]byte, 0)
res := <-resChan
return res.n, res.err
}
func (s *ServiceFixer) Fix() error {
var output strings.Builder
s.Corpse.Name = s.Name
if s.Template == nil {
s.Template = template.Must(template.New("sysvinit").Parse(defaultTemplate))
}
if s.Corpse.ExecStart == "" {
return errors.New("ExecStart is required")
}
if s.Corpse.Name == "" {
return errors.New("name is required, try using the -n flag")
}
err := s.Template.Execute(&output, s.Corpse)
if err != nil {
return err
}
s.buf = []byte(output.String())
return nil
}
func (s *ServiceFixer) Read(p []byte) (int, error) {
if len(s.buf) == 0 {
return 0, io.EOF
}
if err := s.Fix(); err != nil {
return 0, err
}
n := copy(p, s.buf)
s.buf = s.buf[n:]
return n, nil
}
func ReadBrokenServiceFile(filepath string) (BrokenService, error) {
file, err := os.Open(filepath)
if err != nil {
return BrokenService{}, err
}
defer func() {
_ = file.Close()
}()
return ReadBrokenService(file)
}
func ReadBrokenService(r io.Reader) (BrokenService, error) {
verySad := &BrokenService{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if scanner.Err() != nil && !errors.Is(scanner.Err(), io.EOF) {
return *verySad, scanner.Err()
}
line := strings.TrimSpace(scanner.Text())
verySad.parseLine(line)
}
if (verySad.Name == "" || strings.Contains(verySad.Name, " ")) && verySad.RuntimeDirectory != "" {
verySad.Name = verySad.RuntimeDirectory
}
if verySad.ExecReload != "" {
verySad.HasReload = true
}
return *verySad, nil
}
func main() {
var (
err error
src io.Reader
dst io.Writer
name string
)
fubar := "name flag present but no name provided"
for i, arg := range os.Args {
switch arg {
case "-n", "--name":
if len(os.Args) < i+1 {
log.Fatal(fubar)
}
name = os.Args[i+1]
os.Args = slices.Delete(os.Args, i, i+1)
if strings.Contains(arg, "=") {
name = strings.Split(arg, "=")[1]
if name == "" {
log.Fatal(fubar)
}
}
default:
continue
}
}
if len(os.Args) > 1 {
if src, err = os.Open(os.Args[1]); err != nil {
log.Fatal(err)
}
name = strings.TrimSuffix(filepath.Base(os.Args[1]), filepath.Ext(os.Args[1]))
}
if len(os.Args) > 2 {
if dst, err = os.Create(os.Args[2]); err != nil {
log.Fatal(err)
}
}
if src == nil {
src = os.Stdin
}
if dst == nil {
dst = os.Stdout
}
sad, sadErr := ReadBrokenService(src)
if sadErr != nil {
log.Fatal(sadErr)
}
if name == "" {
name = sad.Description
if strings.Contains(name, " ") && sad.RuntimeDirectory != "" {
name = filepath.Base(sad.RuntimeDirectory)
}
}
sad.Name = name
s := &ServiceFixer{Name: name, Corpse: sad}
_, err = io.Copy(s, src)
if err != nil {
log.Fatal(err)
}
err = s.Fix()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(dst, s)
if err != nil {
log.Fatal(err)
}
}