about summary refs log tree commit diff
path: root/bin/sv
blob: 4a7e976324d4c436bdeb5429033a990171364ce3 (plain) (blame)
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
#!/bin/sh
set -eu
[ -n "${DEBUG:-}" ] && set -x

SYS_RT=/run
USER_RT=${XDG_RUNTIME_DIR:-/nouser} # fallback when not set

main() {
	# NOTE: this assumes that:
	#   system services have their live dir at /run/s6-rc
	#     user services have their live dir at $XDG_RUNTIME_DIR/s6-rc
	local rt=$SYS_RT
	[ "${1:-}" = "-u" ] && rt=$USER_RT && shift
	[ "${UID:-$(id -u)}" -ne 0 ] && rt=$USER_RT
	local l=$rt/s6-rc

	# NOTE: this assumes that:
	#   $l/compiled is a symlink to a directory inside the confdir
	#   which contains a directory src, defining the s6-rc source
	local confdir
	confdir=$(readlink -e "$l/compiled")
	confdir=$(dirname "$confdir")
	test -d "$confdir/src"

	local cmd=${1:-}
	shift

	case "$cmd" in
	##
	## db
	##
	reload | recompile)
		export LIVE_DIR="$l"
		export STATE_DIR="$confdir"
		export SOURCE_DIR="$confdir/src"
		exec /opt/svtree/bin/svtree-compile
		;;

	##
	## service
	##
	up | start)
		exec s6-rc -v2 -l "$l" -b start $@
		;;

	dn | down | stop)
		exec s6-rc -v2 -l "$l" -b stop $@
		;;

	re | restart)
		$0 dn $@ && $0 up $@
		;;

	st | status | info)
		local sv=${1:?}
		s6-rc-db -l "$l" type $sv >/dev/null 2>&1 || exit 1

		printf 'Service (%s):\n' $(s6-rc-db -l "$l" type $sv)
		for ln in $(s6-rc-db -l "$l" atomics $sv); do
			printf '%s: ' $ln
			s6-svstat "$l/servicedirs/$ln"
		done | sort -k 2 | column -s ':' -t

		printf '\nDependencies:\n'
		s6-rc-db -l "$l" dependencies $sv

		# NOTE: this assumes logs are stored in $rt/log
		logfile="$rt/log/$sv/current"
		if test -f "$logfile"; then
			printf '\nLogs (%s):\n' "$logfile"
			tail -n 10 "$logfile"
		fi
		;;

	logtail | logs)
		local sv=${1:?}
		# NOTE: this assumes logs are stored in $rt/log
		logfile="$rt/log/$sv/current"
		if test -f "$logfile"; then
			tail -f "$logfile"
		fi
		;;

	##
	## bundles
	##
	bundle-ls)
		find "$confdir/src" -mindepth 1 -maxdepth 1 -exec sh -c 'test "$(cat {}/type)" = "bundle" && basename {}' \;
		;;

	bundle-add)
		# TODO: add confirmation?
		local sv=${1:?}
		local bundle=${2:?}
		touch "$confdir/src/$bundle/contents.d/$sv"
		;;
	bundle-del)
		# TODO: add confirmation?
		local sv=${1:?}
		local bundle=${2:?}
		rm "$confdir/src/$bundle/contents.d/$sv"
		;;

	##
	## listall
	##
	ls | list | ps | *)
		[ "${1:-}" = "-a" ] && exec s6-rc-db -l "$l" list services
		exec s6-rc -v2 -l "$l" -a list
		;;

	esac
}

main $@