forked from bahamas10/bash-vsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsv
executable file
·554 lines (475 loc) · 12.7 KB
/
vsv
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env bash
#
# Void Linux service manager (runit)
#
# Author: Dave Eddy <[email protected]>
# Date: August 29, 2018
# License: MIT
VSV_VERSION='v1.3.4'
export SVDIR=${SVDIR:-/var/service}
charup='✔'
chardown='X'
charunknown='?'
progname=${0##*/}
num_re='^-?[0-9]+$'
svc_re='^[a-zA-Z0-9_\.-]+$'
shopt -s nullglob
# Get time in a human format, like 1 hour ago, 7 minutes ago, etc.
human() {
local seconds=$1
if ((seconds < 0)); then
((seconds *= -1))
fi
local times=(
$((seconds / 60 / 60 / 24 / 365)) # years
$((seconds / 60 / 60 / 24 / 30)) # months
$((seconds / 60 / 60 / 24 / 7)) # weeks
$((seconds / 60 / 60 / 24)) # days
$((seconds / 60 / 60)) # hours
$((seconds / 60)) # minutes
$((seconds)) # seconds
)
local names=(year month week day hour minute second)
local i
for ((i = 0; i < ${#names[@]}; i++)); do
if ((${times[$i]} > 1)); then
echo "${times[$i]} ${names[$i]}s"
return
elif ((${times[$i]} == 1)); then
echo "${times[$i]} ${names[$i]}"
return
fi
done
echo '0 seconds'
}
# enable or disable colors based on the argument given, i.e.:
# setcolors on # colors on
# setcolors off # colors off
# setcolors auto # colors on or off depending on environment
setcolors() {
local opt=$1
local colors
if [[ $opt == auto ]]; then
# no colors if stdout is not a TTY
if [[ ! -t 1 ]]; then
opt='off'
else
# stdout is a tty, check tput capability for colors
colors=$(tput colors 2>/dev/null || echo -1)
if ! [[ $colors =~ $num_re ]]; then
fatal "failed to parse output of \`tput colors\` ($colors)"
fi
if ((colors >= 8)); then
opt='on'
else
opt='off'
fi
fi
fi
case "$opt" in
on|yes|true)
colorred=$(tput setaf 1)
colorgreen=$(tput setaf 2)
coloryellow=$(tput setaf 3)
colorblue=$(tput setaf 4)
colormagenta=$(tput setaf 5)
colorcyan=$(tput setaf 6)
#colorwhite=$(tput setaf 7)
colorgray=$(tput setaf 8)
colorbold=$(tput bold)
colorreset=$(tput sgr0)
;;
off|no|false)
colorred=
colorgreen=
coloryellow=
colormagenta=
colorcyan=
#colorwhite=
colorgray=
colorbold=
colorreset=
;;
*)
echo "unknown color option: '$opt'" >&2
exit 1
;;
esac
}
usage() {
local cr=$colorreset
local logo
logo=$(getlogo)
cat <<EOF
$colormagenta __ _______ __
$colormagenta \\ \\ / / __\\ \\ / / $colorgreen Void Service Manager ($VSV_VERSION)
$colormagenta \\ V /\\__ \\\\ V / $colorgreen Source: https://github.com/bahamas10/vsv
$colormagenta \\_/ |___/ \\_/ $colorgreen MIT License
$logo $colorblue Manage and view runit services
$logo $colorblue Made specifically for Void Linux but should work anywhere
$logo $colorblue Author: Dave Eddy <[email protected]> (bahamas10)
${coloryellow}USAGE:${colorgreen}
$progname [OPTIONS] [SUBCOMMAND] [<ARGS>]
$progname [-u] [-d <dir>] [-h] [-t] [SUBCOMMAND] [...]
${coloryellow}OPTIONS:${colorgreen}
${colorgreen}-c <yes|no|auto> $cr Enable/disable color output, defaults to auto
${colorgreen}-d <dir> $cr Directory to look into, defaults to env SVDIR or /var/service if unset
${colorgreen}-h $cr Print this message and exit
${colorgreen}-l $cr Show log processes, this is a shortcut for 'status -l'
${colorgreen}-t $cr Tree view, this is a shortcut for 'status -t'
${colorgreen}-u $cr User mode, this is a shortcut for '-d ~/runit/service'
${colorgreen}-v $cr Increase verbosity
${colorgreen}-V $cr Print the version number and exit
${coloryellow}ENV:${colorgreen}
SVDIR $cr The directory to use, passed to the 'sv' command, can
be overridden with '-d <dir>'
${coloryellow}SUBCOMMANDS:${colorgreen}
status [-lt] [filter] $cr Default subcommand, show process status
$cr '-t' enables tree mode (process tree)
$cr '-l' enables log mode (show log processes)
$cr 'filter' is an optional string to match service names against
${colorgreen}
enable <svc> [...] $cr Enable the service(s) (remove the "down" file, does not start service)
${colorgreen}
disable <svc> [...] $cr Disable the service(s) (create the "down" file, does not stop service)
Any other subcommand gets passed directly to the 'sv' command, see sv(1) for the
full list of subcommands and information about what each does specifically.
Common subcommands:
${colorgreen}start <service> $cr Start the service
${colorgreen}stop <service> $cr Stop the service
${colorgreen}restart <service> $cr Restart the service
${colorgreen}reload <service> $cr Reload the service (send SIGHUP)
${coloryellow}EXAMPLES:${colorgreen}
${colorgreen}$progname $cr Show service status in /var/service
${colorgreen}$progname status $cr Same as above
${colorgreen}$progname -t $cr Show service status + pstree output
${colorgreen}$progname status -t $cr Same as above
${colorgreen}$progname status tty $cr Show service status for any service that matches tty*
${colorgreen}$progname check uuidd $cr Check the uuidd svc, wrapper for 'sv check uuidd'
${colorgreen}$progname restart sshd $cr Restart sshd, wrapper for 'sv restart sshd'
${colorgreen}$progname -u $cr Show service status in ~/runit/service
${colorgreen}$progname -u restart ssh-agent $cr Restart ssh-agent in ~/runit/service/ssh-agent
EOF
}
verbose() {
if ((verbosity > 0)); then
echo '>' "$colorgray" "$@" "$colorreset" >&2
fi
}
# print the logo with brackets colorized
getlogo() {
printf '%s[%s%s%s]%s' \
"$colorcyan" \
"$colormagenta" "$progname" \
"$colorcyan" \
"$colorreset"
}
# prints a message
msg() {
local logo
logo=$(getlogo)
echo "$logo" "$colorblue$*$colorreset"
}
# prints a fatal message and exists
fatal() {
echo "${colorred}FATAL:" "$@" "$colorreset"
exit 2
}
# rmsg - same as msg but colorized based on return status passed via $1
rmsg() {
local code=$1
local logo
local statuscolor
shift
logo=$(getlogo)
if ((code == 0)); then
statuscolor=$colorgreen
else
statuscolor=$colorred
fi
echo "$logo" "$statuscolor$*$colorreset"
}
disable_message() {
local svc=$1
echo "service $svc disabled by user $USER on $(date)"
}
# process the status of a single service
process_service() {
local svc=$1
local now=$2
local pid when up msg char err enabled msgcolor enabledcolor \
statecolor code islog prog down
islog=false
if [[ $svc == */log ]]; then
islog=true
fi
# check permissions effectively
err=$(sv status "$svc" 2>&1)
code=$?
# get service state, or set error messages
if ((code == 0)); then
err=
state=$(< "$svc/supervise/stat")
pid=$(< "$svc/supervise/pid")
when=$(stat --printf %Y "$svc/supervise/pid")
up=$((now - when))
msg=$(human "$up")
# if the service is enabled (wants up or wants down)
if down=$(cat "$svc/down" 2>&1); then
verbose "service disabled: $down"
enabled='false'
else
enabled='true'
fi
# color time if the service hasn't been up long
if ((up < 5)); then
msgcolor=$colorred
elif ((up < 30)); then
msgcolor=$coloryellow
else
msgcolor=$colorgray
fi
else
pid=
enabled=
state='n/a'
msg=${err##*: }
msgcolor=$colorred
fi
# sanity check pid
if [[ -n $pid ]] && ! [[ $pid =~ $num_re ]]; then
fatal "invalid pid: '$pid'"
fi
# figure out character and color to use for status
if [[ -n $err ]]; then
char=$charunknown
statecolor=$coloryellow
elif [[ $state == 'run' ]]; then
char=$charup
statecolor=$colorgreen
else
char=$chardown
statecolor=$colorred
fi
# figure out enabled color
if [[ $enabled == 'true' ]]; then
enabledcolor=$colorgreen
elif [[ $enabled == 'false' ]]; then
enabledcolor=$colorred
else
enabledcolor=$coloryellow
fi
# msg color
if [[ -z $msgcolor ]]; then
msgcolor=$colorgray
fi
# format service name if it is a log service
if $log && $islog && ! $tree; then
svc='- log'
fi
# figure out program name
if [[ -n $pid ]]; then
IFS= read -d $'\0' -r prog _ < /proc/$pid/cmdline
prog=${prog##*/}
prog=${prog:0:17}
fi
# print service line
printf ' %s%s%s %s%-20s%s %s%-7s%s %s%-9s%s %s%-8s%s %s%-17s%s %s%s%s\n' \
"$statecolor" "$char" "$colorreset" \
"$colorreset" "$svc" "$colorreset" \
"$statecolor" "$state" "$colorreset" \
"$enabledcolor" "${enabled:----}" "$colorreset" \
"$colormagenta" "${pid:----}" "$colorreset" \
"$colorgreen" "${prog:----}" "$colorreset" \
"$msgcolor" "$msg" "$colorreset"
# optionally print the pstree
if $tree; then
if [[ -n $pid ]] && ((pid > 0)); then
echo "$colorgray"
pstree -ac "$pid"
fi
echo "$colorreset"
fi
}
# handle the status subcommand
do_status() {
local OPTIND option
local d
local logo
local now
local svcs=()
logo=$(getlogo)
while getopts 'hlt' option; do
case "$option" in
h) usage; exit 0;;
l) log=true;;
t) tree=true;;
*) usage; exit 1;;
esac
done
shift "$((OPTIND - 1))"
local filter=$1
# loop service directories
for d in ./*/; do
d=${d%/}
svc=${d##*/}
# this is us being a little over-protective here
if ! [[ $svc =~ $svc_re ]]; then
rmsg -1 "unexpected characters in name: '$svc'" >&2
continue
fi
if [[ ! -d $svc/supervise ]]; then
verbose "skipping $svc - '$svc/supervise' not found"
continue
fi
if [[ -n $filter && $svc != *"$filter"* ]]; then
verbose "filtering out '$svc' because match '$filter'"
continue
fi
svcs+=("$svc")
$log && svcs+=("$svc/log")
done
# print title if verbose
if ((verbosity > 0)); then
# service count
local services
local count=${#svcs[@]}
if ((count == 1)); then
services='service'
else
services='services'
fi
printf '%s> %s %s-%s %s%s %s(%s%s%s)%s %s-%s %s%d %s%s\n' \
"$colorgray" "$logo" \
"$colorgray" "$colorreset" \
"$colorblue" "$HOSTNAME" \
"$colorcyan" "$colorgreen" "${PWD/#$HOME/\~}" \
"$colorcyan" "$colorreset" \
"$colorgray" "$colorreset" \
"$colorblue" "$count" "$services" "$colorreset"
fi
echo
# table header
printf '%s %-20s %-7s %-9s %-8s %-17s %s%s\n' \
"$colorbold" \
'SERVICE' 'STATE' 'ENABLED' 'PID' 'COMMAND' 'TIME' \
"$colorreset"
# loop services
printf -v now '%(%s)T' -1
for svc in "${svcs[@]}"; do
process_service "$svc" "$now"
done
echo
}
# handle the enable and disable subcommands
do_enable_disable() {
local cmd=$1
local svc
local ret=0
local file
shift
if (($# < 1)); then
rmsg -1 "Argument expected for '$cmd'"
return 1
fi
# Loop all arguments as services
msg "Running $progname $cmd $*"
for svc in "$@"; do
# Validate service name
if ! [[ $svc =~ $svc_re ]]; then
rmsg -1 "unexpected characters in name: '$svc'" >&2
continue
fi
# ensure service exists
if ! [[ -d $svc ]]; then
rmsg -1 "service directory '$svc' does not exist" >&2
continue
fi
file=$svc/down
case "$cmd" in
enable)
verbose "enable '$svc': rm -f '$file'"
if ! rm -f "$file"; then
rmsg 1 "failed to enable '$svc'"
ret=1
fi
;;
disable)
verbose "disabling '$svc': creating '$file'"
if ! disable_message "$svc" > "$file"; then
rmsg 1 "failed to disable '$svc'"
ret=1
fi
;;
*)
rmsg 2 "unknown command: $cmd" >&2
return 2
;;
esac
done
rmsg "$ret" "[$progname $cmd $*], exit code: $ret"
return "$ret"
}
# handle any other subcommand
do_sv_cmd() {
if (($# < 2)); then
rmsg -1 "Argument expected for 'sv $1'"
return 1
fi
msg "Running sv command (SVDIR=$SVDIR sv $*):"
sv "$@"
local ret=$?
rmsg "$ret" "[sv $*], exit code: $ret"
return "$ret"
}
colors_set=false
tree=false
log=false
do_usage=-1
verbosity=0
while getopts 'c:d:hltuvV' option; do
case "$option" in
c) setcolors "$OPTARG"; colors_set=true;;
d) SVDIR=$OPTARG;;
h) do_usage=0;;
l) log=true; cmd='status';;
t) tree=true; cmd='status';;
u) SVDIR=~/runit/service;;
v) ((verbosity++));;
V) echo "$VSV_VERSION"; exit 0;;
*) do_usage=1;;
esac
done
shift "$((OPTIND - 1))"
$colors_set || setcolors auto
# we wait until the colors are optionally set to output the usage message
if ((do_usage > -1)); then
usage
exit "$do_usage"
fi
# move to the service directory
cd "$SVDIR" || fatal "failed to enter dir: $SVDIR"
# figure out 'cmd' command
if [[ -z $cmd ]]; then
cmd=${1:-status}
shift
fi
case "$cmd" in
status)
do_status "$@"
;;
enable|disable)
do_enable_disable "$cmd" "$@"
;;
*)
if $tree; then
rmsg -1 "-t can only be specified with 'status'"
exit 1
fi
if $log; then
rmsg -1 "-l can only be specified with 'status'"
exit 1
fi
do_sv_cmd "$cmd" "$@"
;;
esac