Contents of /mage/trunk/include/mtools.minc
Parent Directory
|
Revision Log
Revision 4047 -
(show annotations)
(download)
Tue Nov 6 12:57:02 2012 UTC (11 years, 6 months ago) by niro
File size: 8136 byte(s)
Tue Nov 6 12:57:02 2012 UTC (11 years, 6 months ago) by niro
File size: 8136 byte(s)
-sync with upstream
1 | # $Id$ |
2 | |
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 | # 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 | # 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 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
25 | |
26 | # add service to default runlevels |
27 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding ${service} to default runlevels ..." |
28 | [[ -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 | # 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 | # 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 | |
66 | 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 | # only stop the service if ${service_exec} does not exist |
76 | [[ -x ${MROOT}/${service_exec} ]] && return |
77 | |
78 | # del services from runlevel regardless if they exist or not |
79 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..." |
80 | 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 | } |
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 | # 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 | # 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 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
117 | |
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 | |
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 | local opts |
136 | |
137 | # only run if systemd was found |
138 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
139 | then |
140 | echo "systemd not found, not adding unit ${service} to runlevels!" |
141 | return |
142 | fi |
143 | |
144 | # 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 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
152 | then |
153 | opts="--root ${MROOT}" |
154 | fi |
155 | |
156 | 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 | |
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 | systemctl ${opts} daemon-reload |
171 | systemctl ${opts} enable ${service} |
172 | |
173 | # do not start services on bootstrap or MROOT!=/ |
174 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
175 | then |
176 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} starting unit ${service} ..." |
177 | # start or restart the service |
178 | # dont use try-restart, works only with active services! |
179 | systemctl restart ${service} |
180 | 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 | local opts |
190 | |
191 | # only run if systemd was found |
192 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
193 | then |
194 | echo "systemd not found, not removing unit ${service} from runlevels!" |
195 | return |
196 | fi |
197 | |
198 | # 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 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
206 | then |
207 | opts="--root ${MROOT}" |
208 | fi |
209 | |
210 | 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 | elif [[ x$(basename ${service_exec}) = x${service_exec} ]] |
220 | then |
221 | # expand full path |
222 | service_exec="$(which ${service_exec})" |
223 | fi |
224 | |
225 | # only stop the service if ${service_exec} does not exist |
226 | [[ -x ${MROOT}/${service_exec} ]] && return |
227 | |
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 | systemctl ${opts} daemon-reload |
232 | systemctl ${opts} disable ${service} |
233 | |
234 | # do not stop services on bootstrap or MROOT!=/ |
235 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
236 | then |
237 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} stopping unit ${service} ..." |
238 | 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 | local opts |
256 | |
257 | # only run if systemd was found |
258 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
259 | then |
260 | echo "systemd not found, not removing unit ${service} from runlevels!" |
261 | return |
262 | fi |
263 | |
264 | # 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 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
272 | then |
273 | opts="--root ${MROOT}" |
274 | fi |
275 | |
276 | 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 | |
287 | # reload daemon to honor changed unit files |
288 | systemctl ${opts} daemon-reload |
289 | |
290 | # 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 | } |