This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 551
/
ofbiz-framework.ofbiz.init
412 lines (391 loc) · 8.91 KB
/
ofbiz-framework.ofbiz.init
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/sh
### BEGIN INIT INFO
# Provides: ofbiz
# Required-Start: $local_fs $remote_fs $network $time
# Required-Stop: $local_fs $remote_fs $network $time
# Should-Start: postgresql mysql exim4
# Should-Stop: postgresql mysql exim4
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: OfBiz container
### END INIT INFO
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements. See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership. The ASF licenses this file
#to you under the Apache License, Version 2.0 (the
#"License"); you may not use this file except in compliance
#with the License. You may obtain a copy of the License at
#
#http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied. See the License for the
#specific language governing permissions and limitations
#under the License.
set -e
if ! [ -e "/usr/share/ofbiz/ofbiz.jar" ]; then
exit 0
fi
PATH=/bin:/usr/bin:/sbin:/usr/sbin
# set defaults
DEFAULT=/etc/default/ofbiz
JAVA_HOME=""
JAVA_HOMES="/usr/lib/jvm/java-6-sun"
MEMIF=""
UMASK=002
. /lib/lsb/init-functions
if [ -e /etc/default/rcS ]; then
. /etc/default/rcS
fi
if [ -e /etc/default/locale ]; then
. /etc/default/locale
export LANG
fi
# allow local admin to change defaults
if [ -e "$DEFAULT" ]; then
. "$DEFAULT"
fi
umask $UMASK
PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=ofbiz
DESC="OfBiz Container"
OFBIZ_HOME=/usr/share/ofbiz
USER=ofbiz
PIDFILE=/var/run/ofbiz.pid
CLASS=org.ofbiz.base.start.Start
PROPS=""
if [ -f "/etc/ofbiz/debconf.cfg" ]; then
PROPS="$PROPS -Dofbiz.system.props=/etc/ofbiz/debconf.cfg"
fi
if [ "x$JAVA_HOME" = x ]; then
if [ "x$JAVA_HOMES" != x ]; then
for possible in $JAVA_HOMES; do
if [ -e "$possible/bin/java" ]; then
JAVA_HOME="$possible"
break
fi
done
fi
if [ "x$JAVA_HOME" = x ]; then
log_failure_msg "Couldn't find a java."
if [ "x$JAVA_HOMES" != x ]; then
log_failure_msg "Tried $JAVA_HOMES"
fi
exit 1
fi
fi
JAVA="$JAVA_HOME/bin/java"
CLASSPATH="$OFBIZ_HOME/ofbiz.jar:/usr/share/java/commons-daemon.jar"
run_ofbiz_cmd() {
(
cd "$OFBIZ_HOME"
"$JAVA" $PROPS -jar ofbiz.jar "$1"
)
}
if [ -z "$(which jsvc)" ]; then
jsvc() {
jsvc_jvm=""
jsvc_wait=""
jsvc_verbose_class=""
jsvc_verbose_gc=""
jsvc_verbose_jni=""
jsvc_pidfile=""
jsvc_detach=1
jsvc_user=""
jsvc_outfile="/dev/null"
jsvc_errfile="/dev/null"
jsvc_home=""
jsvc_cp=""
jsvc_extra=""
jsvc_stop=""
while [ $# -gt 0 ]; do
case "$1" in
(-version) ;;
(-help) ;;
(-?) ;;
(-check) ;;
(-jvm) jsvc_jvm="$2"; shift;;
(-wait) jsvc_wait="$2"; shift;;
(-stop) jsvc_stop=1;;
(-verbose:class) jsvc_verbose_class=1; shift;;
(-verbose:gc) jsvc_verbose_gc=1; shift;;
(-verbose:jni) jsvc_verbose_jni=1; shift;;
(-pidfile) jsvc_pidfile="$2"; shift;;
(-nodetach) jsvc_detach="";;
(-user) jsvc_user="$2"; shift;;
(-outfile) jsvc_outfile="$2"; shift;;
(-errfile) jsvc_errfile="$2"; shift;;
(-home) jsvc_home="$2"; shift;;
(-classpath) jsvc_cp="$2"; shift;;
(-cp) jsvc_cp="$2"; shift;;
(-D*) jsvc_extra="$jsvc_extra \"$1\"";;
(-X*) jsvc_extra="$jsvc_extra \"$1\"";;
(*) break;;
esac
shift
done
if ! [ "$jsvc_home" ]; then log_failure_msg "-home not set"; exit 1; fi
if ! [ "$jsvc_cp" ]; then log_failure_msg "-cp not set"; exit 1; fi
if [ "$jsvc_jvm" ]; then log_failure_msg "-jvm not supported"; exit 1; fi
if [ "$jsvc_wait" ]; then log_failure_msg "-wait not supported"; exit 1; fi
if [ "$jsvc_stop" ]; then log_failure_msg "-stop not supported"; exit 1; fi
jsvc_args=""
if [ "$jsvc_verbose_class" ]; then :; fi
if [ "$jsvc_verbose_gc" ]; then :; fi
if [ "$jsvc_verbose_jni" ]; then :; fi
jsvc_command_file=$(mktemp)
echo "#!/bin/sh" > "$jsvc_command_file"
chmod +x "$jsvc_command_file"
echo "rm -f \"$jsvc_command_file\"" >> "$jsvc_command_file"
if [ "$jsvc_pidfile" ]; then
touch "$jsvc_pidfile"
if [ "$jsvc_user" ]; then
chown "$jsvc_user" "$jsvc_pidfile"
fi
echo "echo \$\$ > \"$jsvc_pidfile\"" >> "$jsvc_command_file"
fi
echo "exec \"$JAVA\" -classpath \"$jsvc_cp\" $jsvc_extra $@" >> "$jsvc_command_file"
if [ "$jsvc_user" ]; then
chown "$jsvc_user" "$jsvc_command_file"
jsvc_command="su -c \"$jsvc_command_file\" -s \"/bin/sh\" \"$jsvc_user\""
else
jsvc_command="$jsvc_command_file"
fi
jsvc_command="$jsvc_command 1>\"$jsvc_outfile\" 2>\"$jsvc_errfile\""
if [ "$jsvc_detach" ]; then
eval $jsvc_command &
else
trap "rm -f \"$jsvc_command_file\"" EXIT
eval $jsvc_command
fi
return $?
}
fi
get_status() {
status="$(run_ofbiz_cmd -status 2>/dev/null | sed -n 's/^Current Status : //p')"
case "$status" in
(Running|Starting|Stopping|Not\ Running)
echo $status
;;
(*)
echo Not Running
;;
esac
}
wait_for_status() {
count="$1"
while :; do
if [ "$(get_status)" = "$2" ]; then
break;
fi
count=$(($count - 1))
if [ "$count" -eq 0 ]; then
log_failure_msg "Timeout waiting for $3"
exit 1
fi
log_begin_msg "."
sleep 1
done
}
ofbiz_shutdown() {
run_ofbiz_cmd -shutdown >/dev/null
}
wait_for_stop() {
wait_for_status 30 "Not Running" "stop"
count="10"
while :; do
if ! start-stop-daemon --pidfile "$PIDFILE" --exec /usr/bin/jsvc --user $USER --signal 0 --quiet --stop; then
break
fi
count=$(($count - 1))
if [ "$count" -eq 0 ]; then
log_failure_msg "Timeout waiting for stop"
exit 1
fi
log_begin_msg "."
sleep 1
done
}
start() {
case "$(get_status)" in
(Stopping)
log_progress_msg "(waiting for stop"
wait_for_stop
log_progress_msg ")"
;;
(Not\ Running)
;;
(Starting|Running)
log_progress_msg "(already running)"
return
;;
(*)
log_progress_msg "(forcing stop"
ofbiz_shutdown
wait_for_stop
log_progress_msg ")"
;;
esac
(
cd "$OFBIZ_HOME"
touch /var/log/ofbiz/ofbiz.log
chown ofbiz.ofbiz /var/log/ofbiz/ofbiz.log
jsvc -pidfile "$PIDFILE" \
-user $USER \
-outfile /var/log/ofbiz/console.log -errfile '&1' \
-home "$JAVA_HOME" -cp "$CLASSPATH" \
$PROPS \
$MEMIF \
$CLASS
)
wait_for_status 120 "Running" "start"
}
stop() {
case "$(get_status)" in
(Not\ Running)
;;
(*)
ofbiz_shutdown
;;
esac
wait_for_stop
if [ -e "$PIDFILE" ]; then
rm -f "$PIDFILE"
fi
}
install() {
case "$(get_status)" in
(Stopping)
log_progress_msg "(waiting for stop"
wait_for_stop
log_progress_msg ")"
;;
(Not\ Running)
;;
(Starting|Running)
log_failure_msg "(running, can't install)"
exit 1
;;
esac
(
cd "$OFBIZ_HOME"
touch /var/log/ofbiz/ofbiz.log
chown ofbiz.ofbiz /var/log/ofbiz/ofbiz.log
jsvc -pidfile "$PIDFILE" \
-nodetach \
-user $USER \
-outfile /dev/fd/3 -errfile '&1' \
-home "$JAVA_HOME" -cp "$CLASSPATH" \
$PROPS \
$MEMIF \
$CLASS \
install "$@" 3>&1 | \
tee -a /var/log/ofbiz/install.log
)
}
module_hook() {
files=""
result="0"
while [ $# -gt 0 ]; do
case "$1" in
(-file=*)
file="$(echo "$1" | sed 's/^-file=//')"
if ! [ -e "$file" ]; then
log_failure_msg "File $file does not exist"
result=1
else
files="$files,$file"
fi
;;
(-file)
file="$2"
if ! shift; then
log_failure_msg "No file specified"
result=1
elif ! [ -e "$file" ]; then
log_failure_msg "File $file does not exist"
result=1
else
files="$files,$file"
fi
;;
(*)
log_failure_msg "Unknown argument($1)"
exit 1
;;
esac
shift
done
if ! [ "$files" ]; then
return 0
elif [ "$result" eq 1 ]; then
return 1
fi
files="$(echo "$files" | cut -f 2- -d ,)"
was_running=""
case "$(get_status)" in
(Not\ Running)
;;
(Stopping)
wait_for_stop
;;
(*)
log_daemon_msg "Stopping $DESC" "$NAME"
ofbiz_shutdown
wait_for_stop
log_end_msg 0
was_running=1
;;
esac
cd "$OFBIZ_HOME"
if ! jsvc \
-user $USER -outfile /dev/tty -errfile '&1' -nodetach \
-home "$JAVA_HOME" -cp "$CLASSPATH" \
$CLASS install -file="$files"; then
log_failure_msg "Error installing files"
result=1
fi
if [ "$was_running" ]; then
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg 0
fi
return "$result"
}
cmd="$1"
shift || true
case "$cmd" in
(start)
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg 0
;;
(stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
log_end_msg 0
;;
(install)
log_daemon_msg "Installing $DESC" "$NAME"
install "$@"
log_end_msg 0
;;
(reload|restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
stop
start
log_end_msg 0
;;
(module-hook)
if ! module_hook "$@"; then
exit 1
fi
;;
(*)
log_success_msg "Usage: $0 {start|stop|install|reload|force-reload|restart"
exit 1
;;
esac