File: mk-runscript

package info (click to toggle)
runit 2.2.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,568 kB
  • sloc: ansic: 6,071; sh: 3,419; makefile: 399
file content (109 lines) | stat: -rwxr-xr-x 2,815 bytes parent folder | download | duplicates (5)
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
#!/bin/sh
# runscript --- manage runscripts that are not part of debian package

# Copyright (C) 2016 Dmitry Bogatov <KAction@gnu.org>

# Author: Dmitry Bogatov <KAction@gnu.org>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -eu
COLOR_RED=$(tput setaf 1)
COLOR_YELLOW=$(tput setaf 3)
COLOR_BLUE=$(tput setaf 4)
COLOR_BOLD=$(tput bold)
COLOR_RESET=$(tput sgr0)

check_svdir () {
	if [ -z "${SVDIR:-}" ] ; then
		if [ $(id -u) -eq 0 ] ; then
			SVDIR=/etc/service
		else
			SVDIR=$HOME/.service
		fi
		info "SVDIR is not set. Using default value $SVDIR".
	fi
	[ -d "$SVDIR" ] || error "SVDIR=$SVDIR: no such directory"
	[ -w "$SVDIR" ] || error "SVDIR=$SVDIR: permission denied"
}

info () { echo "${COLOR_BOLD}${COLOR_BLUE}info:${COLOR_RESET}" "$@"; }
error() { echo "${COLOR_BOLD}${COLOR_RED}error:${COLOR_RESET}" "$@"; exit 1; }

help () {
	cat <<'EOF'
Usage: runscript [--add|--remove] TYPE [ARGS]

Add or remove runscript of specified type into SVDIR. Currently
supported types are:

 * unprivileged: create runscript, that creates runit instance,
   that supervise services in non-root user's private ~/.service
   directory. The only argument is username.
EOF
}

case "${1:--invalid}" in
	--add)
		action=add
		;;
	--remove)
		action=remove
		;;
	--help)
		help
		exit 0
		;;
	*)
		help
		exit 1
		;;
esac
shift

check_svdir

[ -n "${1:-}" ] || error "runscript type is not specified. Try --help."
case "$1" in
	unprivileged)
		DOTS=$(seq 40|paste -s|sed 's/././g')
		CFG_USER=${2:-}
		[ -n "$CFG_USER" ] || error "missing argument USERNAME"
		if ! getent passwd "$CFG_USER" >/dev/null ; then
			error "unknown user \`$CFG_USER'"
		fi
		CFG_HOME=$(getent passwd "$CFG_USER"|cut -d: -f6)
		SV_NAME="unprivileged.$CFG_USER"
		case "$action" in
			add)
				mkdir -p "$SVDIR/$SV_NAME/control"
				cat <<EOF > "$SVDIR/$SV_NAME/run"
#!/bin/sh
exec 2>&1
export USER='$CFG_USER'
export HOME='$CFG_HOME'
exec chpst -u '$CFG_USER' runsvdir -P '$CFG_HOME/.service' '$DOTS'
EOF
                                cat << EOF > "$SVDIR/$SV_NAME/control/t"
#!/bin/sh
exec sv -v hup .
EOF
				chmod +x "$SVDIR/$SV_NAME/run"
				chmod +x "$SVDIR/$SV_NAME/control/t"
				;;
			remove)
				rm -r "${SVDIR:?}/$SV_NAME"
				;;
		esac
		;;
esac