aboutsummaryrefslogtreecommitdiff
path: root/lib/util/svc.bash
blob: fd65e4cc018e0849f2beb8c952095c703e06c0a2 (plain)
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
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2022 Patrick Spek <p.spek@tyil.nl>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

# OS independent service management.
svc() {
	local system="bashtard/svc"

	local service
	local action

	action=$1 ; shift
	service="$(config "svc.$1")" ; shift

	if [[ -z $service ]]
	then
		crit "$system" "No service name for $service"
		return 1
	fi

	if [[ "$(type -t "svc_$action")" != "function" ]]
	then
		crit "$system" "Invalid service manager action $action"
		return 1
	fi

	"svc_$action" "$service"
}

svc_disable() {
	local system="bashtard/svc/disable"

	local service=$1

	case "${BASHTARD_PLATFORM[key]}" in
		linux-gentoo) set -- /sbin/rc-update del "$service" ;;
		linux-*)      set -- systemctl disable "$service" ;;
		*)
			crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}"
			return 1
	esac

	notice "$system" "$*"
	# shellcheck disable=SC2068
	$@
}

svc_enable() {
	local system="bashtard/svc/enable"

	local service=$1
	local rc

	case "${BASHTARD_PLATFORM[key]}" in
		freebsd)
			rc="$(printf '%s_enable="YES"' "$service")"

			if [[ ! -f "/etc/rc.conf.d/$service" ]] || ! grep -Fqx "$rc" "/etc/rc.conf.d/$service"
			then
				printf "%s\n" "$rc" >> "/etc/rc.conf.d/$service"
			fi

			return 0

			;;
		linux-gentoo) set -- /sbin/rc-update add "$service" ;;
		linux-*)      set -- systemctl enable "$service" ;;
		*)
			crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}"
			return 1
	esac

	notice "$system" "$*"
	# shellcheck disable=SC2068
	$@
}

svc_reload() {
	local system="bashtard/svc/reload"

	local service=$1

	case "${BASHTARD_PLATFORM[key]}" in
		freebsd)      set -- service "$service" reload ;;
		linux-gentoo) set -- /sbin/rc-service "$service" reload ;;
		linux-*)      set -- systemctl reload "$service" ;;
		*)
			crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}"
			return 1
	esac

	notice "$system" "$*"
	# shellcheck disable=SC2068
	$@
}

svc_restart() {
	local system="bashtard/svc/restart"

	local service=$1

	case "${BASHTARD_PLATFORM[key]}" in
		freebsd)      set -- service "$service" restart ;;
		linux-gentoo) set -- /sbin/rc-service "$service" restart ;;
		linux-*)      set -- systemctl restart "$service" ;;
		*)
			crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}"
			return 1
	esac

	notice "$system" "$*"
	# shellcheck disable=SC2068
	$@
}

svc_start() {
	local system="bashtard/svc/start"

	local service=$1

	case "${BASHTARD_PLATFORM[key]}" in
		freebsd)      set -- service "$service" start ;;
		linux-gentoo) set -- /sbin/rc-service "$service" start ;;
		linux-*)      set -- systemctl start "$service" ;;
		*)
			crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}"
			return 1
	esac

	notice "$system" "$*"
	# shellcheck disable=SC2068
	$@
}

svc_stop() {
	local system="bashtard/svc/stop"

	local service=$1

	case "${BASHTARD_PLATFORM[key]}" in
		freebsd)      set -- service "$service" stop ;;
		linux-gentoo) set -- /sbin/rc-service "$service" stop ;;
		linux-*)      set -- systemctl stop "$service" ;;
		*)
			crit "$system" "No service manager configured for ${BASHTARD_PLATFORM[key]}"
			return 1
	esac

	notice "$system" "$*"
	# shellcheck disable=SC2068
	$@
}