-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_manager
More file actions
executable file
·803 lines (685 loc) · 23.4 KB
/
Copy pathinstance_manager
File metadata and controls
executable file
·803 lines (685 loc) · 23.4 KB
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
#!/bin/bash
THIS=`basename $0`
: ${TOMCAT_INSTANCES_DIR:=/usr/local/tomcat_instances}
# force any relative path to canonical path
TOMCAT_INSTANCES_DIR="$(readlink -e "$TOMCAT_INSTANCES_DIR")"
#///////////////////////////////////////////////////////////////#
# #
# Management script for Tomcat instances. #
# mheiges@uga.edu 2007 #
# #
#///////////////////////////////////////////////////////////////#
# clear invoker's env. Only use what is specified inline or by
# source-ing instance.env
unset $(/usr/bin/env | \
egrep -v 'TOMCAT_INSTANCES_DIR|USER' | \
egrep '^(\w+)=(.*)$' | \
/usr/bin/cut -d= -f1);
export TERM=vt100 # for help piped through less
export PATH=/bin:/usr/bin:/usr/local/bin
pushd $TOMCAT_INSTANCES_DIR &> /dev/null
core_usage() {
echo
echo "Manage Tomcat instances and webapps."
echo
echo "Usage: sudo $THIS <start|stop|restart> <instance|all> [force]"
echo " sudo $THIS <enable|disable> <instance>"
echo " $THIS manage <instance> list"
echo " $THIS manage <instance> <start|stop|reload|undeploy|redeploy> <webapp>"
echo " $THIS manage <instance> deploy </path/to/context.xml>"
echo " $THIS status [all]"
echo
}
usage() {
core_usage
echo "Use '$THIS help' for expanded information."
echo
exit 1
}
expanded_usage() {
local known_instances=`get_known_instances`
(
core_usage
echo "The <instance> option refers to a Tomcat server having its own configuration"
echo "and JVM process. Known Tomcat instances on this server are:"
echo "$known_instances"
echo
echo "Each instance can be stopped, started and restarted independently of the"
echo "others. In uncommon cases where the instance will not stop properly, you"
echo "can 'stop' with the 'force' option to kill the JVM. The 'start', 'stop'"
echo "and 'restart' actions of the Tomcat instance affects all contained webapps,"
echo "therefore these actions require sudo permissions. Example:"
echo " sudo $THIS restart ApiDB"
echo "Using 'all' in place of an instance name can be used to start or stop all"
echo "the known instances (those whose name does not have a '_' prefix)."
echo
echo "The 'manage' verb allows you to act within a given instance. The manage actions"
echo "do not require sudo permissions. The 'list' action will print the webapps"
echo "deployed in the instance and and their running state. Example:"
echo " $THIS manage ApiDB list"
echo
echo "A given <webapp> can be started, stopped, reloaded, undeployed or redeployed"
echo "from the instance being managed. Example:"
echo " $THIS manage ApiDB reload apidb.dev"
echo
echo "A 'redeploy' action first undeploys the existing context xml file from the"
echo "instance and then deploys that same xml file back in. The primary use case"
echo "for redeploy is to clear the webapp's cache of compiled JSP."
echo "If you want to change the context configuration then you must first undeploy"
echo "the webapp and then deploy a new, edited copy of the context xml file."
echo
echo "A webapp can be added to the instance via the 'deploy' command. A webapp"
echo "may not be running after deployment, so monitor the Tomcat logs during"
echo "deployment or use the $THIS 'list' command to check the webapp's"
echo "state after deploying. Example:"
echo " $THIS manage ApiDB deploy \$PROJECT_HOME/../etc/apidb.dev.xml"
echo
echo "The 'status' verb prints the currently running Tomcat instances along with"
echo "their process id, process owner, ports and instance uptime. Append 'all'"
echo "to include instances that are not running."
echo
echo "The 'disable' action stops the instance and temporarily excludes it from "
echo "the $THIS script and the system init protocols. 'enable' will"
echo "make the instance useable again and start it. These actions require"
echo "sudo permissions."
echo
echo "See http://s.eupathdb.org/1gjo853 for full documentation."
echo
echo "-------"
echo "$THIS is a product of EuPathDB Bioinformatics Resource Center <help@eupathdb.org>"
) | less
exit $PIPESTATUS
}
get_known_instances() {
# an instance is a directory under /usr/local/tomcat_instances with a conf/instance.env file
# and whose name does not have a '_' prefix
possible_instances=`find $TOMCAT_INSTANCES_DIR -mindepth 1 -maxdepth 1 -type d -not -name Instance_Template -not -name shared -not -name common -not -name '.*' -not -name '_*' -printf '%f '`
declare -a known_instances
for dir in $possible_instances; do
[[ -f "$TOMCAT_INSTANCES_DIR/$dir/conf/instance.env" ]] &&
known_instances[${#known_instances[@]}]="$dir"
done
if [[ ${#known_instances[@]} -eq 0 ]];then
: #echo "<none found>"
else
echo "${known_instances[@]}"
fi
}
# Extract a manager username and password from a given instance's
# conf/tomcat-users.xml. Expects at least one username with role
# of 'manager' only (not e.g. roles='manager,admin')
set_manager_credentials() {
local instance=$1
userxml="${TOMCAT_INSTANCES_DIR}/${instance}/conf/tomcat-users.xml"
test -e $userxml || {
echo
echo "'$userxml' not found."
echo "Can not get tomcat login credentials."
echo
exit 1
}
cred=($(
perl -MXML::Simple -e \
"\$x=XMLin(
'$userxml',
NormalizeSpace=>2,
KeyAttr=>['roles','username'],
ForceArray=>1);
\$c = \$x->{'user'}->{'manager'} || \$x->{'user'}->{'manager-script'};
print \"\$c->{'username'} \$c->{'password'}\";"
))
TC_USER=${cred[0]}
TC_PASS=${cred[1]}
}
require_sudo() {
if [[ "$TOMCAT_USER" != "$USER" && `id -u` != 0 ]]; then
echo 'sudo/root permissions required for this action.'; exit 4;
fi
}
# determine the version of Tomcat.
# The version.sh script is included with Tomcat >= 5.0, don't know about earlier.
# Basically it runs "java -classpath $CATALINA_HOME/lib/catalina.jar org.apache.catalina.util.ServerInfo"
tomcat_version() {
$CATALINA_HOME/bin/version.sh | grep '^Server version:' | cut -d'/' -f2
}
# The url path to the Tomcat Manager varies with Tomcat version
# /manager/<verb> for TC 6, /manager/text/<verb> for TC 7 .
manager_path_for_version() {
local tomcat_version="$1"
case "$tomcat_version" in
6.*)
echo 'manager'
;;
7.*)
echo 'manager/text'
;;
8.*)
echo 'manager/text'
;;
9.*)
echo 'manager/text'
;;
*)
echo 1>&2 "unable to determine the manager's URL path for Tomcat version '${tomcat_version}'"
;;
esac
}
# initialize for management tasks (start, stop, enable, disable, list)
init_for_managment() {
[ "$INSTANCE" == "" ] && usage;
if [ -d "${INSTANCE}" ]; then
local state=enabled
elif [ -d "_${INSTANCE}" ]; then
echo "${INSTANCE} is disabled."
if [ $1 == "start" ]; then
echo "Enable and start with '${THIS} enable ${INSTANCE}'."
fi
exit 0
else
echo "Instance '$INSTANCE' not found." && exit 1;
fi
for arg in "$@"; do
if [[ "$arg" == "verbose" ]]; then
VERBOSE=1
fi
done
# an associative array of extra options that can be defined in
# global.env or instance.env
declare -A CATALINA_OPTS
global_cfg=$TOMCAT_INSTANCES_DIR/shared/conf/global.env
if [ ! -e $global_cfg ]; then
echo "$global_cfg not found";
exit 1;
fi
[[ $VERBOSE ]] && echo "source global configuration: '$global_cfg'"
source $global_cfg
export ORACLE_HOME LD_LIBRARY_PATH LANG
# Instance specific configuration
cfg=${INSTANCE}/conf/instance.env
if [ ! -e $cfg ]; then
echo "$cfg not found";
exit 1;
fi
[[ $VERBOSE ]] && echo "source instance configuration: '$cfg'"
source $cfg
PATH="$JAVA_HOME/bin:$PATH"
# Backward compatibility:
# convert any old SHUTDOWN_PORT configurations to JMX_PORT.
# We launch via jsvc which does not use a shutdown port so
# this is OK.
JMX_PORT=${JMX_PORT:-$SHUTDOWN_PORT}
# check that we have required values
required_cfg=( HTTP_PORT )
for opt in ${required_cfg[@]}; do
[ ! "${!opt}" ] && undefined="$undefined $opt"
done
if [ "$undefined" ]; then
echo
echo "FATAL: Undefined properties in '$cfg':"
echo $undefined
exit 1
fi
: ${JSVC_EXE:=$CATALINA_HOME/bin/jsvc}
: ${CATALINA_BASE:=$TOMCAT_INSTANCES_DIR/$INSTANCE}
: ${TMP_DIR:=$CATALINA_BASE/tmp}
: ${PID_DIR:=/var/run/var/run}
: ${PID_FILE:=$PID_DIR/jsvc_$INSTANCE.pid}
TOMCAT_VERSION=`tomcat_version`
MANAGER_PATH=`manager_path_for_version $TOMCAT_VERSION`
[[ -n "$MANAGER_PATH" ]] || exit 1
# properties required for proper function tomcat_instance framework and its support scripts.
: ${FRAMEWORK_PROPERTIES:=" \
-Dhttp.port=$HTTP_PORT \
-Dajp13.port=$AJP13_PORT \
-Dshutdown.port=-1 \
-Dinstance.name=$INSTANCE \
-Dtcif.root=$TOMCAT_INSTANCES_DIR \
"}
# 'eval echo' expands any embedded variables
for val in "${CATALINA_OPTS[*]}"; do
CATALINA_OPTS_E="${CATALINA_OPTS_E} $(eval echo $val)"
done
DEFAULT_CLASSPATHS=(
"$JAVA_HOME/lib/tools.jar"
"$CATALINA_HOME"
"$CATALINA_HOME/bin/bootstrap.jar"
"$CATALINA_HOME/bin/commons-logging-api.jar"
"$CATALINA_HOME/bin/tomcat-juli.jar"
)
if [[ -z "$CLASSPATH" ]]; then
for p in ${DEFAULT_CLASSPATHS[*]}; do
CLASSPATH="$CLASSPATH:$p"
done
fi
# Tomcat 9 fails with
# SEVERE: Unable to process file [/proc/self/task/15224...
# when there empty slots in classpath (e.g. :a:b::c),
# so ensure a valid path.
CLASSPATH=$(sed 's/^://' <<< "$CLASSPATH")
CLASSPATH=$(sed 's/::*/:/g' <<< "$CLASSPATH")
CLASSPATH=$(sed 's/:$//' <<< "$CLASSPATH")
} # init_for_managment()
# output parent and child processes
get_pids() {
# processes owned by $TOMCAT_USER
# jsvc starts parent (session leader) and child processes,
# For system installs, the session leader is owned by root and
# the child is owned by TOMCAT_USER, but when running in
# standalone mode both processes are owned by TOMCAT_USER
# so we can't rely solely on 'ps -u' and need to filter out
# the session leader (STAT == 'Ss').
pids=$(ps -u $TOMCAT_USER --format stat,pid,ppid,cmd | \
egrep -v '^Ss ' | \
egrep '[j]svc.exe' | \
egrep "\b$INSTANCE\b" | \
awk '{print $2 " " $3}'
)
echo $pids
}
start() {
require_sudo
pids=$(get_pids)
if [[ -n "$pids" ]]; then
# trim off start time for parent
start_time=$(ps -o bsdstart= -p $pids | head -n 1)
echo "$INSTANCE is already started since $start_time"
exit
fi
if [[ -f $PID_FILE ]]; then
# the pid file may have multiple entries. not sure why.
# looks like the first is always what we want
pid=$(head -n1 $PID_FILE)
ps $pid 2>&1>/dev/null
exit_code=$?
if [[ $exit_code -gt 0 ]]; then
echo "Removing stale $PID_FILE ..."
rm -f $PID_FILE
fi
fi
echo starting $INSTANCE...
STARTUP_EXEC="$JSVC_EXE \
-user $TOMCAT_USER \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-Dcatalina.base=$CATALINA_BASE \
-Djava.io.tmpdir=$TMP_DIR \
-wait 10 \
-pidfile $PID_FILE \
-outfile $CATALINA_BASE/logs/catalina.out \
-errfile '&1' \
$FRAMEWORK_PROPERTIES \
$CATALINA_OPTS_E \
-DautoDeploy=$AUTO_DEPLOY \
-cp $CLASSPATH \
org.apache.catalina.startup.Bootstrap"
[[ $VERBOSE ]] && echo -e "CATALINA_OPTS\n${CATALINA_OPTS_E}\n"
[[ $VERBOSE ]] && echo -e "CLASSPATH\n${CLASSPATH}\n"
[[ $VERBOSE ]] && echo -e "complete startup command\n${STARTUP_EXEC}\n"
eval $STARTUP_EXEC
#
# To get a verbose JVM
#-verbose \
# To get a debug of jsvc.
#-debug
exit_status=$?
# jsvc returns 1 even when successfully starting. So we resort
# to other methods of checking and returning a proper status.
if [[ -f $PID_FILE ]]; then
# the pid file may have multiple entries. not sure why.
# looks like the first is always what we want
pid=$(head -n1 $PID_FILE)
ps $pid 2>&1>/dev/null
return $?
fi
return $exit_status
}
stop() {
require_sudo
pids=$(get_pids)
if [[ -z "$pids" ]]; then
echo "$INSTANCE is already stopped"
return
fi
for arg in "$@"; do
if [[ "$arg" == "force" ]]; then
echo killing PIDs $pids...
kill -9 $pids
return
fi
done
echo -n stopping $INSTANCE...
STOP_EXEC="$JSVC_EXE \
-stop \
-pidfile $PID_FILE \
org.apache.catalina.startup.Bootstrap"
eval $STOP_EXEC
# print a dot on the same line every 5 second that instance is still running
while [[ -n "$(get_pids)" ]]; do
echo -n "."
sleep 5
done
# add newline after last dot
echo ""
}
disable() {
require_sudo
cd "${TOMCAT_INSTANCES_DIR}"
if [[ -d "_${INSTANCE}" ]]; then
echo "${INSTANCE} is already disabled."
exit 0
fi
init_for_managment "$@"
stop force
echo "Renaming ${INSTANCE} directory to _${INSTANCE}"
cd "${TOMCAT_INSTANCES_DIR}"
mv "${INSTANCE}" "_${INSTANCE}"
}
enable() {
require_sudo
cd "${TOMCAT_INSTANCES_DIR}"
if [[ -d "${INSTANCE}" ]]; then
echo "${INSTANCE} is already enabled."
else
echo "Renaming _${INSTANCE} directory to ${INSTANCE}"
mv "_${INSTANCE}" "${INSTANCE}"
fi
init_for_managment "$@"
start "${@:3}"
}
deployFile() {
cd ~-0 # cd back to user's cwd so a relative path to context.xml file is sensical
if [[ -z $2 ]]; then
echo
echo "no context xml file given"
echo
usage
exit 1
fi
local HTTP_PORT=$1
local context_xml_file=$(readlink -f $2)
if [[ ! -f $context_xml_file ]]; then
echo
echo "context xml file '$2' not found"
echo
usage
exit 1
fi
local context=$(perl -MXML::Simple -e "print XMLin('$context_xml_file', NormalizeSpace=>2)->{path};")
set_manager_credentials $INSTANCE
local deploy_url="http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/deploy?config=file:$context_xml_file&war&path=$context"
[[ $VERBOSE ]] && echo "fetching '$deploy_url'"
curl -s "$deploy_url"
cd - > /dev/null
}
# pass in context xml to create and deploy from temporary context.xml file
deployVirtual() {
local HTTP_PORT=$1
local context_xml_data="$2"
local tmp_ctx_file=$(mktemp -tu instance_manager.XXXXXXXXXX) || {
echo "could not create temp context file '$tmp_ctx_file'"; exit 1;
}
echo "$context_xml_data" > $tmp_ctx_file
local context=$(perl -MXML::Simple -e "print XMLin('$tmp_ctx_file', NormalizeSpace=>2)->{path};")
set_manager_credentials $INSTANCE
local deploy_url="http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/deploy?config=file:$tmp_ctx_file&war&path=$context"
[[ $VERBOSE ]] && echo "fetching '$deploy_url'"
curl -s "$deploy_url"
rm -f $tmp_ctx_file || { echo "could not delete temp file '$tmp_ctx_file'. Please manually remove it."; }
}
undeploy() {
local webapp=$1
local tomcat_ctx=$([ "$webapp" == "ROOT" ] && echo "/" || echo "/$webapp")
echo -n "Whoa, Nelly, do you really want me to remove $webapp from $instance?[y/n] "
read ans
[ "$ans" != "y" ] && exit 1
echo
local undeploy_url="http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/undeploy?path=$tomcat_ctx"
[[ $VERBOSE ]] && echo "fetching '$undeploy_url'"
curl -s "$undeploy_url"
}
function join { local IFS="$1"; shift; echo "$*"; }
tstatus() {
local showall=0
report_on=()
for arg in "$@"; do
if [[ "$arg" == "all" ]]; then
showall=1
unset report_on
break
else
report_on+=("\b${arg}\b")
fi
done
# list of all instances found on filesystem. We will remove running instances from the list.
offline_instances=`get_known_instances`
local OLDIFS="$IFS"
local IFS="
"
# filter on tcif.root whose TOMCAT_INSTANCES_DIR matches value set by this script,
# to isolate instances managed by other TcIF installations.
tcif_filter="grep 'tcif.root=$TOMCAT_INSTANCES_DIR'"
# filter on optional list of specific instances
if [[ ${#report_on[@] > 0} ]]; then
gstr=`join '|' ${report_on[@]}`
instance_filter="egrep '$gstr'"
else
# match everything
instance_filter='grep .'
fi
running_instances=($(ps -fd | egrep '[j]svc.exe' | eval $tcif_filter | eval $instance_filter ))
format="%-10.10s %6.6s %10.10s %6.6s %6.6s %6.6s %10s\n"
printf $format INSTANCE PID USER HTTP AJP13 JMX UPTIME
for inst in ${running_instances[@]}; do
pid=`echo $inst | awk '{print $2}'`
instance_name=`expr "$inst" : '.*Dinstance.name=\([^ ]*\)'`
http=`expr "$inst" : '.*Dhttp.port=\([^ ]*\)'`
ajp13=`expr "$inst" : '.*Dajp13.port=\([^ ]*\)'`
jmx=`expr "$inst" : '.*Dcom.sun.management.jmxremote.port=\([^ ]*\)'`
user=`expr "$inst" : '.*-user \([^ ]*\) '`
uptime=`ps -o etime $pid|grep -v ELAPSED | sed 's/[\s\n\r]*//g' | sed "s/\(.*\)-\(.*\):\(.*\):\(.*\)/\1d \2h/; s/\(.*\):\(.*\):\(.*\)/\1h \2m/; s/\(.*\):\(.*\)/\1m \2s/"`
# skip any jsvc processes that don't have a corresponding directory that we manage,
# assuming that someone must have started it manually.
[[ ! -d "${TOMCAT_INSTANCES_DIR}/${instance_name}" ]] && continue
printf "$format" "$instance_name" "$pid" "$user" "$http" "$ajp13" "$jmx" "$uptime"
# remove this running instance from the list
offline_instances=`echo $offline_instances | sed "s/\s*${instance_name}\s*//g"`
done
local IFS="$OLDIFS"
if [[ $showall -eq 1 ]]; then
if [[ ${#offline_instances} -gt 0 ]]; then
echo "Not running:"
for offline in $offline_instances; do echo $offline; done
fi
fi
# Return non-zero exit status if no instances are running. This is
# especially necessary when testing a specific instance
# ('instance_manager status FooDB') by provisioning tools such as
# Ansible or Puppet so it can tell the instance is not running.
if [[ ${#running_instances[@]} -eq 0 ]]; then
exit 1
else
exit 0
fi
}
wstatus() {
local webapp=$1
local tomcat_ctx=$([ "$webapp" == "ROOT" ] && echo "/" || echo "/$webapp")
set_manager_credentials $INSTANCE
curl -s "http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/list" | egrep "^$tomcat_ctx:" | awk -F: "{print \$2}"
}
start_app() {
local webapp=$1
if [ -z $webapp ]; then
echo
echo "no webapp given"
echo
usage
fi
local tomcat_ctx=$([ "$webapp" == "ROOT" ] && echo "/" || echo "/$webapp")
if [ "`wstatus $webapp`" == "running" ]; then
echo
echo "$webapp is already running."
echo "I'm going to attempt to reload it instead."
echo
manage $INSTANCE reload $webapp
else
set_manager_credentials $INSTANCE
local start_url="http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/start?path=$tomcat_ctx"
[[ $VERBOSE ]] && echo "fetching '$start_url'"
curl -s "$start_url"
fi
}
manage() {
[ "$1" == "" ] && usage;
[ ! -d "$1" ] && echo "Instance '$1' not found." && exit 1;
local instance=$1
local cmd=$2
local context=$3
local webapp=$(echo "$3" | sed -e "s/^\///")
local tomcat_ctx=$([ "$webapp" == "ROOT" ] && echo "/" || echo "/$webapp")
local cfg=${instance}/conf/instance.env
if [ ! -e $cfg ]; then
echo "$cfg not found";
exit 1;
fi
[[ $VERBOSE ]] && echo "source instance configuration: '$cfg'"
source $cfg
set_manager_credentials $instance
case "$cmd" in
list)
local list_url="http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/list"
[[ $VERBOSE ]] && echo "fetching '$list_url'"
curl -s "$list_url"
;;
stop)
if [ -z $webapp ]; then
echo
echo "no webapp given"
echo
usage
fi
local stop_url="http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/stop?path=$tomcat_ctx"
[[ $VERBOSE ]] && echo "fetching '$stop_url'"
curl -s "$stop_url"
;;
start)
if [ -z $webapp ]; then
echo
echo "no webapp given"
echo
usage
fi
start_app $webapp
;;
reload)
if [ -z $webapp ]; then
echo
echo "no webapp given"
echo
usage
fi
if [ "`wstatus $webapp`" == "stopped" ]; then
echo
echo "$webapp is stopped. I will attempt to start it. Wish me luck."
echo
start_app $webapp
else
local reload_url="http://$TC_USER:$TC_PASS@127.0.0.1:$HTTP_PORT/${MANAGER_PATH}/reload?path=$tomcat_ctx"
[[ $VERBOSE ]] && echo "fetching '$reload_url'"
curl -s "$reload_url"
fi
;;
undeploy)
if [ -z $webapp ]; then
echo
echo "no webapp given"
echo
usage
fi
undeploy $webapp
;;
redeploy)
ctx_xml_file="$TOMCAT_INSTANCES_DIR/$instance/conf/Catalina/localhost/$webapp.xml"
contextXmlData="$(cat $ctx_xml_file)" || {
echo "'$ctx_xml_file' not found or is not readable. Is $webapp deployed in $instance? Quitting."; exit 1;
}
undeploy $webapp
deployVirtual $HTTP_PORT "$contextXmlData"
;;
deploy)
deployFile $HTTP_PORT $context
;;
*)
cat <<EOF
Usage: $THIS manage $instance COMMAND
Command Options:
list
stop <webapp>
start <webapp>
reload <webapp>
deploy </path/to/context.xml>
undeploy <webapp>
redeploy <webapp>
EOF
;;
esac
}
INSTANCE=$2
# Special case when starting/stoping instance 'all'. Iteratively
# invoke this script for each active instance found.
if [[
"$INSTANCE" == "all" &&
("$1" == "stop" || "$1" == "start" || "$1" == "restart")
]]; then
require_sudo
for instance in `$0 list`; do
$0 $1 $instance $3
done
exit 0
fi
case "$1" in
start)
init_for_managment "$@"
start "${@:3}"
exit $?
;;
stop)
init_for_managment "$@"
stop "${@:3}"
exit $?
;;
enable)
enable "$@"
exit $?
;;
disable)
disable "$@"
exit $?
;;
restart)
init_for_managment "$@"
stop "${@:3}"
start "${@:3}"
exit $?
;;
stat*)
shift
tstatus "$@"
;;
man*)
shift
init_for_managment "$@"
manage "$@"
exit $?
;;
help)
expanded_usage
exit $?
;;
list)
get_known_instances
;;
*)
usage
;;
esac