Annotation of /trunk/include/mtools.minc
Parent Directory | Revision Log
Revision 34 -
(hide annotations)
(download)
Fri Jan 13 02:29:33 2012 UTC (12 years, 10 months ago) by niro
Original Path: branches/magellan-next/include/mtools.minc
File size: 8037 byte(s)
Fri Jan 13 02:29:33 2012 UTC (12 years, 10 months ago) by niro
Original Path: branches/magellan-next/include/mtools.minc
File size: 8037 byte(s)
-support mage-feature autosvc and fixed handling of service_exec files
1 | niro | 18 | # $Id$ |
2 | niro | 1 | |
3 | # adds services to runlevels and starts them | ||
4 | # mstartservice service {/path/to/service_exec} | ||
5 | mstartservice() | ||
6 | { | ||
7 | local service="$1" | ||
8 | local service_exec="$2" | ||
9 | |||
10 | niro | 18 | # only run if sysV init was found |
11 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
12 | then | ||
13 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
14 | return | ||
15 | fi | ||
16 | |||
17 | niro | 34 | # don't run if feature !autosvc was set |
18 | if mqueryfeature "!autosvc" | ||
19 | then | ||
20 | echo "!autosvc detected; auto management of services disabled." | ||
21 | return | ||
22 | fi | ||
23 | |||
24 | niro | 20 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
25 | niro | 1 | |
26 | # add service to default runlevels | ||
27 | niro | 8 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding ${service} to default runlevels ..." |
28 | niro | 1 | [[ -x ${MROOT}/etc/rc.d/init.d/${service} ]] && rc-config add ${service} &> /dev/null |
29 | |||
30 | # do not start services on bootstrap or MROOT!=/ | ||
31 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
32 | then | ||
33 | # start service | ||
34 | if [[ -n $(pidof ${service_exec}) ]] | ||
35 | then | ||
36 | # restart service | ||
37 | /etc/init.d/${service} restart | ||
38 | else | ||
39 | # start service | ||
40 | /etc/init.d/${service} start | ||
41 | fi | ||
42 | fi | ||
43 | } | ||
44 | |||
45 | # removes services from runlevels and stops them | ||
46 | # mstopservice service {/path/to/service_exec} | ||
47 | mstopservice() | ||
48 | { | ||
49 | local service="$1" | ||
50 | local service_exec="$2" | ||
51 | |||
52 | niro | 18 | # only run if sysV init was found |
53 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
54 | then | ||
55 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
56 | return | ||
57 | fi | ||
58 | |||
59 | niro | 34 | # don't run if feature !autosvc was set |
60 | if mqueryfeature "!autosvc" | ||
61 | then | ||
62 | echo "!autosvc detected; auto management of services disabled." | ||
63 | return | ||
64 | fi | ||
65 | niro | 1 | |
66 | niro | 34 | if [[ -z ${service_exec} ]] |
67 | then | ||
68 | service_exec="$(which ${service})" | ||
69 | elif [[ x$(basename ${service_exec}) = x${service_exec} ]] | ||
70 | then | ||
71 | # expand full path | ||
72 | service_exec="$(which ${service_exec})" | ||
73 | fi | ||
74 | |||
75 | niro | 1 | # only stop the service if ${service_exec} does not exist |
76 | niro | 34 | [[ -x ${MROOT}/${service_exec} ]] && return |
77 | niro | 1 | |
78 | # del services from runlevel regardless if they exist or not | ||
79 | niro | 8 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..." |
80 | niro | 1 | rc-config del ${service} &> /dev/null |
81 | |||
82 | # do not stop services on bootstrap or MROOT!=/ | ||
83 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
84 | then | ||
85 | # stop service if running | ||
86 | if [[ -n $(pidof ${service_exec}) ]] | ||
87 | then | ||
88 | killall -15 ${service_exec} &> /dev/null | ||
89 | sleep 1 | ||
90 | killall -9 ${service_exec} &> /dev/null | ||
91 | fi | ||
92 | fi | ||
93 | niro | 4 | } |
94 | |||
95 | # reloads a service if already running | ||
96 | # mreloadservice service {/path/to/service_exec} | ||
97 | mreloadservice() | ||
98 | { | ||
99 | local service="$1" | ||
100 | local service_exec="$2" | ||
101 | |||
102 | niro | 18 | # only run if sysV init was found |
103 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
104 | then | ||
105 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
106 | return | ||
107 | fi | ||
108 | |||
109 | niro | 34 | # don't run if feature !autosvc was set |
110 | if mqueryfeature "!autosvc" | ||
111 | then | ||
112 | echo "!autosvc detected; auto management of services disabled." | ||
113 | return | ||
114 | fi | ||
115 | |||
116 | niro | 20 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
117 | niro | 4 | |
118 | # do not stop services on bootstrap or MROOT!=/ | ||
119 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
120 | then | ||
121 | # only reload the service if running | ||
122 | if [[ -n $(pidof ${service_exec}) ]] | ||
123 | then | ||
124 | /etc/init.d/${service} reload | ||
125 | fi | ||
126 | fi | ||
127 | } | ||
128 | niro | 16 | |
129 | # adds systemd units to runlevels and starts them | ||
130 | # mstartunit service {/path/to/service_exec} | ||
131 | mstartunit() | ||
132 | { | ||
133 | local service="$1" | ||
134 | local service_exec="$2" | ||
135 | niro | 32 | local opts |
136 | niro | 16 | |
137 | # only run if systemd was found | ||
138 | if [[ ! -x /bin/systemctl ]] | ||
139 | then | ||
140 | echo "systemd not found, not adding unit ${service} to runlevels!" | ||
141 | return | ||
142 | fi | ||
143 | |||
144 | niro | 34 | # don't run if feature !autosvc was set |
145 | if mqueryfeature "!autosvc" | ||
146 | then | ||
147 | echo "!autosvc detected; auto management of services disabled." | ||
148 | return | ||
149 | fi | ||
150 | |||
151 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
152 | niro | 16 | then |
153 | niro | 32 | opts="--root ${MROOT}" |
154 | niro | 16 | fi |
155 | |||
156 | niro | 20 | if [[ -z ${service_exec} ]] |
157 | then | ||
158 | case ${service} in | ||
159 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
160 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
161 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
162 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
163 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
164 | esac | ||
165 | fi | ||
166 | niro | 16 | |
167 | # add service to default runlevels | ||
168 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..." | ||
169 | # reload daemon to honor changed unit files | ||
170 | niro | 32 | systemctl ${opts} daemon-reload |
171 | systemctl ${opts} enable ${service} | ||
172 | niro | 16 | |
173 | # do not start services on bootstrap or MROOT!=/ | ||
174 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
175 | then | ||
176 | niro | 21 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} starting unit ${service} ..." |
177 | niro | 16 | # start or restart the service |
178 | niro | 22 | # dont use try-restart, works only with active services! |
179 | systemctl restart ${service} | ||
180 | niro | 16 | fi |
181 | } | ||
182 | |||
183 | # removes systemd units from runlevels and stops them | ||
184 | # mstopunit service {/path/to/service_exec} | ||
185 | mstopunit() | ||
186 | { | ||
187 | local service="$1" | ||
188 | local service_exec="$2" | ||
189 | niro | 32 | local opts |
190 | niro | 16 | |
191 | # only run if systemd was found | ||
192 | if [[ ! -x /bin/systemctl ]] | ||
193 | then | ||
194 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
195 | return | ||
196 | fi | ||
197 | |||
198 | niro | 34 | # don't run if feature !autosvc was set |
199 | if mqueryfeature "!autosvc" | ||
200 | then | ||
201 | echo "!autosvc detected; auto management of services disabled." | ||
202 | return | ||
203 | fi | ||
204 | |||
205 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
206 | niro | 16 | then |
207 | niro | 32 | opts="--root ${MROOT}" |
208 | niro | 16 | fi |
209 | |||
210 | niro | 20 | if [[ -z ${service_exec} ]] |
211 | then | ||
212 | case ${service} in | ||
213 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
214 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
215 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
216 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
217 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
218 | esac | ||
219 | niro | 34 | elif [[ x$(basename ${service_exec}) = x${service_exec} ]] |
220 | then | ||
221 | # expand full path | ||
222 | service_exec="$(which ${service_exec})" | ||
223 | niro | 20 | fi |
224 | niro | 16 | |
225 | # only stop the service if ${service_exec} does not exist | ||
226 | niro | 34 | [[ -x ${MROOT}/${service_exec} ]] && return |
227 | niro | 16 | |
228 | # del services from runlevel regardless if they exist or not | ||
229 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..." | ||
230 | # reload daemon to honor changed unit files | ||
231 | niro | 32 | systemctl ${opts} daemon-reload |
232 | systemctl ${opts} disable ${service} | ||
233 | niro | 16 | |
234 | # do not stop services on bootstrap or MROOT!=/ | ||
235 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
236 | then | ||
237 | niro | 21 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} stopping unit ${service} ..." |
238 | niro | 16 | systemctl stop ${service} |
239 | # try harder | ||
240 | if [[ -n $(pidof ${service_exec}) ]] | ||
241 | then | ||
242 | killall -15 ${service_exec} &> /dev/null | ||
243 | sleep 1 | ||
244 | killall -9 ${service_exec} &> /dev/null | ||
245 | fi | ||
246 | fi | ||
247 | } | ||
248 | |||
249 | # reloads a systemd unit if already running | ||
250 | # mreloadunit service {/path/to/service_exec} | ||
251 | mreloadunit() | ||
252 | { | ||
253 | local service="$1" | ||
254 | local service_exec="$2" | ||
255 | niro | 32 | local opts |
256 | niro | 16 | |
257 | # only run if systemd was found | ||
258 | if [[ ! -x /bin/systemctl ]] | ||
259 | then | ||
260 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
261 | return | ||
262 | fi | ||
263 | |||
264 | niro | 34 | # don't run if feature !autosvc was set |
265 | if mqueryfeature "!autosvc" | ||
266 | then | ||
267 | echo "!autosvc detected; auto management of services disabled." | ||
268 | return | ||
269 | fi | ||
270 | |||
271 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
272 | niro | 16 | then |
273 | niro | 32 | opts="--root ${MROOT}" |
274 | niro | 16 | fi |
275 | |||
276 | niro | 20 | if [[ -z ${service_exec} ]] |
277 | then | ||
278 | case ${service} in | ||
279 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
280 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
281 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
282 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
283 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
284 | esac | ||
285 | fi | ||
286 | niro | 16 | |
287 | niro | 28 | # reload daemon to honor changed unit files |
288 | niro | 32 | systemctl ${opts} daemon-reload |
289 | niro | 28 | |
290 | niro | 16 | # do not stop services on bootstrap or MROOT!=/ |
291 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
292 | then | ||
293 | # only reload the service if running | ||
294 | systemctl reload-or-try-restart ${service} | ||
295 | fi | ||
296 | } |