forked from GluuFederation/oxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxd-server.init
executable file
·528 lines (440 loc) · 12.1 KB
/
oxd-server.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/env bash
# LSB Tags
### BEGIN INIT INFO
# Provides: oxd-server
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: oxd-server start script.
# Description: Start oxd server.
### END INIT INFO
# Startup script for oxd-server under *nix systems (it works under NT/cygwin too).
##################################################
# Set the name which is used by other variables.
# Defaults to the file name without extension.
##################################################
NAME=$(echo $(basename $0) | sed -e 's/^[SK][0-9]*//' -e 's/\.sh$//')
# To get the service to restart correctly on reboot, uncomment below (3 lines):
# ========================
# chkconfig: 3 99 99
# description: oxd server
# processname: oxd-server
# ========================
# Configuration files
#
# /etc/default/$NAME
# If it exists, this is read at the start of script. It may perform any
# sequence of shell commands, like setting relevant environment variables.
#
# $HOME/.$NAMErc (e.g. $HOME/.oxd-serverrc)
# If it exists, this is read at the start of script. It may perform any
# sequence of shell commands, like setting relevant environment variables.
#
# The files will be checked for existence before being passed to oxd-server.
#
# Configuration variables
#
# JAVA
# Command to invoke Java. If not set, java (from the PATH) will be used.
#
# JAVA_OPTIONS
# Extra options to pass to the JVM
#
# OXD_HOME
# Where oxd is installed. If not set, the script will try go
# guess it by looking at the invocation path for the script
#
# OXD_BASE
# Where your oxd server base directory is. If not set, the value from
# $OXD_HOME will be used.
#
# OXD_RUN
# Where the $NAME.pid file should be stored. It defaults to the
# first available of /var/run, /usr/var/run OXD_BASE and /tmp
# if not set.
#
# OXD_PID
# The oxd-server PID file, defaults to $OXD_RUN/$NAME.pid
#
# OXD_ARGS
# The default arguments to pass to oxd-server.
#
# OXD_USER
# if set, then used as a username to run the server as
#
# OXD_SHELL
# If set, then used as the shell by su when starting the server. Will have
# no effect if start-stop-daemon exists. Useful when OXD_USER does not
# have shell access, e.g. /bin/false
#
usage()
{
echo "Usage: ${0##*/} [-d] {start|stop|run|restart|check|supervise} [ CONFIGS ... ] "
exit 1
}
[ $# -gt 0 ] || usage
##################################################
# Some utility functions
##################################################
findDirectory()
{
local L OP=$1
shift
for L in "$@"; do
[ "$OP" "$L" ] || continue
printf %s "$L"
break
done
}
running()
{
if [ -f "$1" ]
then
local PID=$(cat "$1" 2>/dev/null) || return 1
kill -0 "$PID" 2>/dev/null
return
fi
rm -f "$1"
return 1
}
started()
{
# wait for 60s to see "STARTED" in PID file
for T in 1 2 3 4 5 6 7 9 10 11 12 13 14 15
do
sleep 4
[ -z "$(grep STARTED $1 2>/dev/null)" ] || return 0
[ -z "$(grep STOPPED $1 2>/dev/null)" ] || return 1
[ -z "$(grep FAILED $1 2>/dev/null)" ] || return 1
local PID=$(cat "$2" 2>/dev/null) || return 1
kill -0 "$PID" 2>/dev/null || return 1
echo -n ". "
done
return 1;
}
readConfig()
{
(( DEBUG )) && echo "Reading $1.."
source "$1"
}
##################################################
# Get the action & configs
##################################################
CONFIGS=()
NO_START=0
DEBUG=0
while [[ $1 = -* ]]; do
case $1 in
-d) DEBUG=1 ;;
esac
shift
done
ACTION=$1
shift
##################################################
# Read any configuration files
##################################################
ETC=/etc
if [ $UID != 0 ]
then
ETC=$HOME/etc
fi
for CONFIG in {/etc,~/etc}/default/${NAME}{,9} $HOME/.${NAME}rc; do
if [ -f "$CONFIG" ] ; then
readConfig "$CONFIG"
fi
done
##################################################
# Set tmp if not already set.
##################################################
TMPDIR=${TMPDIR:-/tmp}
##################################################
# oxd-server's hallmark
##################################################
OXD_INSTALL_TRACE_FILE="oxd-server-jar-with-dependencies.jar"
##################################################
# Try to determine OXD_HOME if not set
##################################################
if [ -z "$OXD_HOME" ]
then
OXD_SH=$0
case "$OXD_SH" in
/*) OXD_HOME=${OXD_SH%/*/*} ;;
./*/*) OXD_HOME=${OXD_SH%/*/*} ;;
./*) OXD_HOME=.. ;;
*/*/*) OXD_HOME=./${OXD_SH%/*/*} ;;
*/*) OXD_HOME=. ;;
*) OXD_HOME=.. ;;
esac
if [ ! -f "$OXD_HOME/lib/$OXD_INSTALL_TRACE_FILE" ]
then
OXD_HOME=
fi
fi
##################################################
# No OXD_HOME yet? We're out of luck!
##################################################
if [ -z "$OXD_HOME" ]; then
echo "** ERROR: OXD_HOME not set, you need to set it or install in a standard location"
exit 1
fi
cd "$OXD_HOME"
OXD_HOME=$PWD
##################################################
# Set OXD_BASE
##################################################
if [ -z "$OXD_BASE" ]; then
OXD_BASE=$OXD_HOME
fi
cd "$OXD_BASE"
OXD_BASE=$PWD
#####################################################
# Check that oxd server is where we think it is
#####################################################
if [ ! -r "$OXD_HOME/lib/$OXD_INSTALL_TRACE_FILE" ]
then
echo "** ERROR: Oops! oxd server doesn't appear to be installed in $OXD_HOME"
echo "** ERROR: $OXD_HOME/lib/$OXD_INSTALL_TRACE_FILE is not readable!"
exit 1
fi
#####################################################
# Find a location for the pid file
#####################################################
if [ -z "$OXD_RUN" ]
then
OXD_RUN=$(findDirectory -w /var/run /usr/var/run $OXD_BASE /tmp)
fi
#####################################################
# Find a pid and state file
#####################################################
if [ -z "$OXD_PID" ]
then
OXD_PID="$OXD_RUN/${NAME}.pid"
fi
if [ -z "$OXD_STATE" ]
then
OXD_STATE=$OXD_BASE/${NAME}.state
fi
case "`uname`" in
CYGWIN*) OXD_STATE="`cygpath -w $OXD_STATE`";;
esac
OXD_ARGS=(${OXD_ARGS[*]} "oxd-server.state=$OXD_STATE")
##################################################
# Setup JAVA if unset
##################################################
if [ -z "$JAVA" ]
then
JAVA=$(which java)
fi
if [ -z "$JAVA" ]
then
echo "Cannot find a Java JDK. Please set either set JAVA or put java (>=1.5) in your PATH." >&2
exit 1
fi
#####################################################
# See if OXD_LOGS is defined
#####################################################
if [ -z "$OXD_LOGS" ] && [ -d $OXD_BASE/logs ]
then
OXD_LOGS=$OXD_BASE/logs
fi
if [ -z "$OXD_LOGS" ] && [ -d $OXD_HOME/logs ]
then
OXD_LOGS=$OXD_HOME/logs
fi
if [ "$OXD_LOGS" ]
then
case "`uname`" in
CYGWIN*) OXD_LOGS="`cygpath -w $OXD_LOGS`";;
esac
JAVA_OPTIONS=(${JAVA_OPTIONS[*]} "-Doxd.logging.dir=$OXD_LOGS")
fi
#####################################################
# Are we running on Windows? Could be, with Cygwin/NT.
#####################################################
case "`uname`" in
CYGWIN*) PATH_SEPARATOR=";";;
*) PATH_SEPARATOR=":";;
esac
#####################################################
# Add oxd server properties to Java VM options.
#####################################################
case "`uname`" in
CYGWIN*)
OXD_HOME="`cygpath -w $OXD_HOME`"
OXD_BASE="`cygpath -w $OXD_BASE`"
TMPDIR="`cygpath -w $TMPDIR`"
;;
esac
JAVA_OPTIONS=(${JAVA_OPTIONS[*]} "-Doxd.home=$OXD_HOME" "-Doxd.base=$OXD_BASE" "-Djava.io.tmpdir=$TMPDIR")
#####################################################
# This is how the oxd server will be started
#####################################################
OXD_START="org.xdi.oxd.server.ServerLauncher"
case "`uname`" in
CYGWIN*) OXD_START="`cygpath -w $OXD_START`";;
esac
RUN_ARGS=(${JAVA_OPTIONS[@]} "$OXD_START" ${OXD_ARGS[*]})
RUN_CMD=("$JAVA" ${RUN_ARGS[@]})
#####################################################
# Comment these out after you're happy with what
# the script is doing.
#####################################################
if (( DEBUG ))
then
echo "OXD_HOME = $OXD_HOME"
echo "OXD_BASE = $OXD_BASE"
echo "OXD_CONF = $OXD_CONF"
echo "OXD_PID = $OXD_PID"
echo "OXD_START = $OXD_START"
echo "OXD_ARGS = ${OXD_ARGS[*]}"
echo "JAVA_OPTIONS = ${JAVA_OPTIONS[*]}"
echo "JAVA = $JAVA"
echo "RUN_CMD = ${RUN_CMD[*]}"
fi
##################################################
# Do the action
##################################################
case "$ACTION" in
start)
echo -n "Starting oxd server: "
if (( NO_START )); then
echo "Not starting ${NAME} - NO_START=1";
exit
fi
if [ $UID -eq 0 ] && type start-stop-daemon > /dev/null 2>&1
then
unset CH_USER
if [ -n "$OXD_USER" ]
then
CH_USER="-c$OXD_USER"
fi
start-stop-daemon -S -p"$OXD_PID" $CH_USER -d"$OXD_BASE" -b -m -a "$JAVA" -- "${RUN_ARGS[@]}" start-log-file="$OXD_LOGS/start.log" >> "$OXD_LOGS/start.log" 2>&1
else
if running $OXD_PID
then
echo "Already Running $(cat $OXD_PID)!"
exit 1
fi
if [ -n "$OXD_USER" ] && [ `whoami` != "$OXD_USER" ]
then
unset SU_SHELL
if [ "$OXD_SHELL" ]
then
SU_SHELL="-s $OXD_SHELL"
fi
touch "$OXD_PID"
chown "$OXD_USER" "$OXD_PID"
# FIXME: Broken solution: wordsplitting, pathname expansion, arbitrary command execution, etc.
su - "$OXD_USER" $SU_SHELL -c "
exec ${RUN_CMD[*]} start-log-file="$OXD_LOGS/start.log" >> "$OXD_LOGS/start.log" 2>&1 &
disown \$!
echo \$! > '$OXD_PID'"
else
"${RUN_CMD[@]}" > /dev/null &
disown $!
echo $! > "$OXD_PID"
fi
fi
if expr "${OXD_ARGS[*]}" : '.*oxd-server-started.xml.*' >/dev/null
then
if started "$OXD_STATE" "$OXD_PID"
then
echo "OK `date`"
else
echo "FAILED `date`"
exit 1
fi
else
echo "ok `date`"
fi
;;
stop)
echo -n "Stopping oxd server: "
if [ $UID -eq 0 ] && type start-stop-daemon > /dev/null 2>&1; then
start-stop-daemon -K -p"$OXD_PID" -d"$OXD_HOME" -a "$JAVA" -s HUP
TIMEOUT=30
while running "$OXD_PID"; do
if (( TIMEOUT-- == 0 )); then
start-stop-daemon -K -p"$OXD_PID" -d"$OXD_HOME" -a "$JAVA" -s KILL
fi
sleep 1
done
else
if [ ! -f "$OXD_PID" ] ; then
echo "ERROR: no pid found at $OXD_PID"
exit 1
fi
PID=$(cat "$OXD_PID" 2>/dev/null)
if [ -z "$PID" ] ; then
echo "ERROR: no pid id found in $OXD_PID"
exit 1
fi
kill "$PID" 2>/dev/null
TIMEOUT=30
while running $OXD_PID; do
if (( TIMEOUT-- == 0 )); then
kill -KILL "$PID" 2>/dev/null
fi
sleep 1
done
fi
rm -f "$OXD_PID"
rm -f "$OXD_STATE"
echo OK
;;
restart)
OXD_SH=$0
> "$OXD_STATE"
"$OXD_SH" stop "$@"
"$OXD_SH" start "$@"
;;
supervise)
#
# Under control of daemontools supervise monitor which
# handles restarts and shutdowns via the svc program.
#
exec "${RUN_CMD[@]}"
;;
run|demo)
echo "Running oxd server: "
if running "$OXD_PID"
then
echo Already Running $(cat "$OXD_PID")!
exit 1
fi
exec "${RUN_CMD[@]}"
;;
check|status)
if running "$OXD_PID"
then
echo "oxd server running pid=$(< "$OXD_PID")"
else
echo "oxd server NOT running"
fi
echo
echo "OXD_HOME = $OXD_HOME"
echo "OXD_BASE = $OXD_BASE"
echo "OXD_CONF = $OXD_CONF"
echo "OXD_PID = $OXD_PID"
echo "OXD_START = $OXD_START"
echo "OXD_LOGS = $OXD_LOGS"
echo "OXD_STATE = $OXD_STATE"
echo "CLASSPATH = $CLASSPATH"
echo "JAVA = $JAVA"
echo "JAVA_OPTIONS = ${JAVA_OPTIONS[*]}"
echo "OXD_ARGS = ${OXD_ARGS[*]}"
echo "RUN_CMD = ${RUN_CMD[*]}"
echo
if running "$OXD_PID"
then
exit 0
fi
exit 1
;;
*)
usage
;;
esac
exit 0