1 |
commit: 975400d5765f7405f611ddf66a3ad717cb21a203 |
2 |
Author: Michał Kępień <github <AT> kempniu <DOT> pl> |
3 |
AuthorDate: Wed Oct 5 09:08:55 2016 +0000 |
4 |
Commit: Justin Bronder <jsbronder <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Oct 28 12:57:14 2016 +0000 |
6 |
URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=975400d5 |
7 |
|
8 |
net-nntp/sabnzbd: Rework init script |
9 |
|
10 |
net-nntp/sabnzbd/files/sabnzbd.initd | 130 ++++++++++++++--------------------- |
11 |
1 file changed, 51 insertions(+), 79 deletions(-) |
12 |
|
13 |
diff --git a/net-nntp/sabnzbd/files/sabnzbd.initd b/net-nntp/sabnzbd/files/sabnzbd.initd |
14 |
old mode 100644 |
15 |
new mode 100755 |
16 |
index 9b6a941..ad11573 |
17 |
--- a/net-nntp/sabnzbd/files/sabnzbd.initd |
18 |
+++ b/net-nntp/sabnzbd/files/sabnzbd.initd |
19 |
@@ -1,99 +1,71 @@ |
20 |
#!/sbin/openrc-run |
21 |
-# Copyright 1999-2014 Gentoo Foundation |
22 |
+# Copyright 1999-2016 Gentoo Foundation |
23 |
# Distributed under the terms of the GNU General Public License v2 |
24 |
|
25 |
-RUNDIR=/var/run/sabnzbd |
26 |
+PIDFILE="/run/sabnzbd/sabnzbd.pid" |
27 |
|
28 |
depend() { |
29 |
- need net |
30 |
+ need net |
31 |
} |
32 |
|
33 |
get_var() { |
34 |
- echo $(sed -n \ |
35 |
- '/^\[misc]/,/^'$1'/ s/^'$1' = \([[:alnum:].]\+\)[\r|\n|\r\n]*$/\1/p' \ |
36 |
- "${SABNZBD_CONFIGFILE}") |
37 |
+ grep -P -o -m 1 "(?<=^${1} = ).*" "${SABNZBD_CONFIGFILE}" || echo 0 |
38 |
} |
39 |
|
40 |
-get_port() { |
41 |
- if [ "$(get_var 'enable_https')" -eq 1 ]; then |
42 |
- echo $(get_var 'https_port') |
43 |
- else |
44 |
- echo $(get_var 'port') |
45 |
- fi |
46 |
+start() { |
47 |
+ ebegin "Starting SABnzbd" |
48 |
+ |
49 |
+ checkpath -q -d -o ${SABNZBD_USER}:${SABNZBD_GROUP} -m 0770 "$(dirname "${PIDFILE}")" |
50 |
+ |
51 |
+ start-stop-daemon \ |
52 |
+ --quiet \ |
53 |
+ --start \ |
54 |
+ --user ${SABNZBD_USER} \ |
55 |
+ --group ${SABNZBD_GROUP} \ |
56 |
+ --pidfile "${PIDFILE}" \ |
57 |
+ --wait 1000 \ |
58 |
+ --exec /usr/bin/sabnzbd \ |
59 |
+ -- \ |
60 |
+ --config-file "${SABNZBD_CONFIGFILE}" \ |
61 |
+ --logging "${SABNZBD_LOGGING}" \ |
62 |
+ --daemon \ |
63 |
+ --pidfile "${PIDFILE}" |
64 |
+ |
65 |
+ eend $? |
66 |
} |
67 |
|
68 |
-get_addr() { |
69 |
- local host=$(get_var 'host') |
70 |
- local protocol='http' |
71 |
+stop() { |
72 |
+ local protocol="http" |
73 |
+ local host="$(get_var "host")" |
74 |
+ local port="$(get_var "port")" |
75 |
|
76 |
- [ "${host}" == "0.0.0.0" ] && host=localhost |
77 |
- [ "$(get_var 'enable_https')" -eq 1 ] && protocol='https' |
78 |
+ if [ $(get_var "enable_https") -eq 1 ]; then |
79 |
+ protocol="https" |
80 |
+ port="$(get_var "https_port")" |
81 |
+ fi |
82 |
|
83 |
- echo "${protocol}://${host}:$(get_port)" |
84 |
-} |
85 |
+ case "${host}" in |
86 |
+ *:*) host="[${host}]" ;; |
87 |
+ esac |
88 |
|
89 |
-get_pidfile() { |
90 |
- echo "${RUNDIR}/sabnzbd-$(get_port).pid" |
91 |
-} |
92 |
+ local url="${protocol}://${host}:${port}/sabnzbd/api?mode=shutdown" |
93 |
|
94 |
-start() { |
95 |
- ebegin "Starting SABnzbd" |
96 |
- |
97 |
- checkpath -q -d -o ${SABNZBD_USER}:${SABNZBD_GROUP} -m 0770 "${RUNDIR}" |
98 |
- |
99 |
- start-stop-daemon \ |
100 |
- --quiet \ |
101 |
- --start \ |
102 |
- --user ${SABNZBD_USER} \ |
103 |
- --group ${SABNZBD_GROUP} \ |
104 |
- --name sabnzbd \ |
105 |
- --background \ |
106 |
- --pidfile "$(get_pidfile)" \ |
107 |
- --exec /usr/bin/sabnzbd \ |
108 |
- -- \ |
109 |
- --config-file "${SABNZBD_CONFIGFILE}" \ |
110 |
- --logging "${SABNZBD_LOGGING}" \ |
111 |
- --daemon \ |
112 |
- --pid "${RUNDIR}" |
113 |
- |
114 |
- eend $? |
115 |
-} |
116 |
+ if [ $(get_var "disable_api_key") -eq 0 ]; then |
117 |
+ url="${url}&apikey=$(get_var "api_key")" |
118 |
+ fi |
119 |
|
120 |
-start_pre() { |
121 |
- if [ "$RC_CMD" == "restart" ]; then |
122 |
- local pidfile=$(get_pidfile) |
123 |
- while [ -e ${pidfile} ]; do |
124 |
- sleep 1 |
125 |
- done |
126 |
- fi |
127 |
+ local signals="TERM/1/KILL/1" |
128 |
|
129 |
- return 0 |
130 |
-} |
131 |
+ ebegin "Stopping SABnzbd" |
132 |
|
133 |
-stop() { |
134 |
- local api_key=$(get_var 'api_key') |
135 |
- local addr=$(get_addr) |
136 |
- local rc=1 |
137 |
- |
138 |
- ebegin "Stopping SABnzbd @ ${addr}" |
139 |
- # This can only work if we have enabled the API |
140 |
- if [ -n "${api_key}" -a "$(get_var 'disable_api_key')" -ne 1 ]; then |
141 |
- local ret |
142 |
- einfo "Attempting web-based shutdown @ ${addr}" |
143 |
- |
144 |
- # SABnzbd will return "ok" if shutdown is successful |
145 |
- ret=$(/usr/bin/curl -k -s "${addr}/sabnzbd/api?mode=shutdown&apikey=${api_key}") |
146 |
- [ "${ret}" == "ok" ] && rc=0 |
147 |
- fi |
148 |
- |
149 |
- if [ "${rc}" -ne 0 ]; then |
150 |
- einfo "Falling back to SIGTERM, this may not work if you restarted via the web interface" |
151 |
- start-stop-daemon \ |
152 |
- --stop \ |
153 |
- --pidfile $(get_pidfile) \ |
154 |
- --retry SIGTERM/1/SIGKILL/5 |
155 |
- rc=$? |
156 |
- fi |
157 |
- |
158 |
- eend ${rc} |
159 |
+ if [ "$(wget -q -t 1 -O - -T 10 "${url}")" = "ok" ]; then |
160 |
+ signals="CONT/5/${signals}" |
161 |
+ fi |
162 |
+ |
163 |
+ start-stop-daemon \ |
164 |
+ --stop \ |
165 |
+ --pidfile "${PIDFILE}" \ |
166 |
+ --retry "${signals}" |
167 |
+ |
168 |
+ eend $? |
169 |
} |