forked from mvanholsteijn/weblogic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weblogic
executable file
·546 lines (499 loc) · 12.8 KB
/
weblogic
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
#!/bin/bash
#
# Copyright 2012 Xebia Nederland B.V.
#
# Licensed 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.
#
# NAME
# weblogic - weblogic service control script
#
# SYNOPSIS
# weblogic [start|stop|status]
#
# DESCRIPTION
# This script will start all weblogic servers from installed domains
# on this machine. It uses the default startWebLogic.sh and
# startManagedWebLogic.sh scripts to avoid the dependencies on the
# admin server and the node manager.
#
# CAVEATS
# It determines which servers and nodemanagers to boot, by testing the
# listen-address of the server and nodemanager configurations in the
# config.xml. If it is absent, the script will start an instance on
# all machines the domain is copied too. So set the listen-address!
#
# On the very first boot, it is adviseable to start the admin server
# machine first. On subsequent boots the managed servers will
# manage to start without.
#
# COPYRIGHT
# Copyright 2012 Xebia Nederland B.V.
#
COMMAND=${1:status}
MW_HOME=${MW_HOME:-/opt/weblogic}
WL_HOME=${WL_HOME:-${MW_HOME}/wlserver}
NODEMANAGER_HOME=$WL_HOME/common/nodemanager
MAXWAIT=12
function allDomains() {
grep -v '^#' $WL_HOME/common/nodemanager/nodemanager.domains | \
sed -e 's/^[^=]*=//'
return 0
}
#
# Set ths domain configuration file for the domain $DOMAIN_HOME.
#
function setDomainConfig() {
DOMAIN=$(basename $DOMAIN_HOME)
DOMAIN_CONFIG=$DOMAIN_HOME/config/config.xml
if [ ! -f $DOMAIN_CONFIG ] ; then
DOMAIN_CONFIG=$DOMAIN_HOME/config/config_bootstrap.xml
fi
if [ ! -f $DOMAIN_CONFIG ] ; then
echo ERROR: Domain configuration file is missing for domain $DOMAIN >&2
return 1
fi
return 0
}
#
# Gets the ip address of host $1.
#
function getAddress() {
echo $(ping -c 1 $1 2>/dev/null | head -1 | awk '{print $3}' | sed -e 's/[()]//g')
}
#
# is address $1 served by this host?
#
function onThisHost() {
ADDRESS=$(getAddress $1)
if [ -z "$ADDRESS" ] ; then
echo "WARN: no address specified for $1 " >&2
return 0
fi
if expr "$ADDRESS" : '127\.' > /dev/null ; then
#echo "INFO: $1 is loopback address " >&2
return 0
fi
PRESENT=$(/sbin/ifconfig | grep -e "addr:$ADDRESS")
if [ -z "$PRESENT" ] ; then
#echo "INFO: $1 is NOT on this host." >&2
return 1
else
#echo "INFO: $1 is on this host. " >&2
return 0
fi
}
#
# get the process id of WebLogic Server name $1
#
function getServerPid() {
PID=$(ps -ef | grep -e "-Dweblogic.Name=$1" | grep -v grep | awk '{print $2}')
echo $PID
test -n "$PID"
}
#
# get the process id of the node manager machine $1?
#
function getNodeManagerPid() {
ADDRESS=$(getAddress $2)
PORT=${3:-5556}
PID=$(netstat -n -l -p 2>/dev/null | grep -e $ADDRESS:$PORT -e :::$PORT | sed -e 's/.*LISTEN[^0-9]*\([0-9][0-9]*\).*/\1/')
echo $PID
test -n "$PID"
}
#
# start WebLogic Server $1 by calling the appropriate script in the domain.
#
function callStartWebLogic() {
JAVA_OPTIONS=$(xmlstarlet sel -N d=http://xmlns.oracle.com/weblogic/domain \
-t -m "/d:domain/d:server[d:name='$1']" \
-v 'd:server-start/d:arguments' \
$DOMAIN_CONFIG 2>/dev/null)
export JAVA_OPTIONS
ADMINSERVERINFO=$(getAdminServer)
ADMIN_NAME=$(echo $ADMINSERVERINFO | awk '{print $1}')
ADMIN_HOST=$(echo $ADMINSERVERINFO | awk '{print $2}')
ADMIN_PORT=$(echo $ADMINSERVERINFO | awk '{print $3}')
if [ "$ADMIN_NAME" = "$1" ] ; then
nohup $DOMAIN_HOME/startWebLogic.sh >/dev/null 2>&1 &
else
nohup $DOMAIN_HOME/bin/startManagedWebLogic.sh $1 $ADMIN_HOST:$ADMIN_PORT >/dev/null 2>&1 &
fi
}
#
# lists all non admin servers in the domain configuration file.
#
function listAllServers() {
xmlstarlet sel -N d=http://xmlns.oracle.com/weblogic/domain \
-t -m "/d:domain/d:server[d:name!=/d:domain/d:admin-server-name]" \
-v "concat(d:name, ' ', d:listen-address, ' ', d:listen-port)" \
-n \
$DOMAIN_CONFIG 2>/dev/null | \
sed -e '/^[ ]*$/d' | \
awk -vhost=$(hostname) -F" " 'BEGIN { port = 7001; }
{ if ($2 == "") $2 = host;
if ($3 == "") $3 = 7001;
print $1, $2, $3;
}'
}
#
# lists all node managers in the domain configuration file.
#
function listNodeManagers() {
xmlstarlet sel -N d=http://xmlns.oracle.com/weblogic/domain \
-t -m "/d:domain/d:machine" \
-v "concat(d:name, ' ', d:node-manager/d:listen-address, ' ', d:node-manager/d:listen-port)" \
-n \
$DOMAIN_CONFIG 2>/dev/null | \
sed -e '/^[ ]*$/d' | \
awk -vhost=$(hostname) -F" " \
'{ if ($2 == "") $2 = host;
if ($4 == "") $3 = 5556;
print $1, $2, $3;
}'
}
#
# Check that all servers have an explicit listen-address specification in the domain configuration file.
#
function checkExplicitListenAddress() {
xmlstarlet sel -N d=http://xmlns.oracle.com/weblogic/domain \
-t -m "/d:domain/d:server" \
-v "concat(d:name, ' ', d:listen-address)" \
-n \
$DOMAIN_CONFIG 2>/dev/null | \
awk -F" " \
'
{ if ($1 != "" && $2 == "") {
print "WARN: Server " $1 " does not have an explicit listen address and will start on any server." > "/dev/stderr"
}
}'
}
#
# gets the admin server from the domain configuration file.
#
function getAdminServer() {
xmlstarlet sel -N d=http://xmlns.oracle.com/weblogic/domain \
-t -m "/d:domain/d:server[d:name=/d:domain/d:admin-server-name]" \
-v "concat(d:name, ' ', d:listen-address, ' ', d:listen-port)" \
-n \
$DOMAIN_CONFIG 2>/dev/null | \
sed -e '/^$/d' | \
awk -vhost=$(hostname) -F" " \
'
{ if ($2 == "") $2 = host;
if ($3 == "") $3 = 7001;
print $1, $2, $3;
}'
}
#
# Start the server if it is not already running.
#
function startServer() {
if onThisHost $2 ; then
if listenerOnPort $2 ${3:-7001} ; then
echo "INFO: Server $1 of domain $DOMAIN is already running."
else
echo -n "INFO: starting $1 of domain $DOMAIN"
callStartWebLogic $@
COUNT=0
ISRUNNING=1
while [ $ISRUNNING -ne 0 -a $COUNT -lt $MAXWAIT ] ; do
sleep 10; echo -n .
listenerOnPort $2 ${3:-7001}
ISRUNNING=$?
COUNT=$(($COUNT +1))
done
echo
if [ $ISRUNNING -eq 1 ] ; then
PID=$(getServerPid $1)
if [ -z "$PID" ] ; then
echo "ERROR: failed to start server $2 of domain $DOMAIN" 2>&1
return 1
else
echo "WARN: server $2 of domain $DOMAIN started but not yet listening on ${3:7001}." 2>&1
return 2
fi
fi
fi
fi
}
#
# Stops the server if it is running.
#
function stopServer() {
if onThisHost $2 ; then
PID=$(getServerPid $1)
if test -n "$PID" && listenerOnPort $2 ${3:-7001} ; then
echo -n "INFO: stopping $1 of domain $DOMAIN"
kill -15 $PID
COUNT=0
ISRUNNING=0
while [ $ISRUNNING -eq 0 -a $COUNT -lt $MAXWAIT ] ; do
sleep 10; echo -n .
listenerOnPort $2 ${3:-7001}
ISRUNNING=$?
COUNT=$(($COUNT + 1))
done
echo
if [ $ISRUNNING -eq 0 ] ; then
echo "ERROR: failed to stop server $2 of domain $DOMAIN. " 2>&1
return 1
else
echo "INFO: Server $1 of domain $DOMAIN is stopped."
fi
else
echo "INFO: Server $1 of domain $DOMAIN is already stopped."
fi
fi
return 0
}
#
# tests if there is a listener on port $2 of network address $1
#
function listenerOnPort() {
ADDRESS=$(getAddress $1)
LISTENING=$(netstat -a -n | \
egrep -e "$ADDRESS:$2[ ].*LISTEN" -e ":::$2[ ].*LISTEN" )
test -n "$LISTENING"
return $?
}
#
# stop the node manager if it is running.
function stopNodeManager() {
if onThisHost $2 ; then
PID=$(getNodeManagerPid $@)
if test -n "$PID" && listenerOnPort $2 ${3:-5556} ; then
echo -n "INFO: stopping nodemanager $1"
kill -15 $PID
COUNT=0
ISRUNNING=0
while [ -n "$PID" -a $ISRUNNING -eq 0 -a $COUNT -lt $MAXWAIT ] ; do
sleep 10; echo -n .
listenerOnPort $2 ${3:-5556}
ISRUNNING=$?
COUNT=$(($COUNT + 1))
done
echo
if [ $ISRUNNING -eq 0 ] ; then
echo "ERROR: failed to stop nodemanager $1. " 2>&1
return 1
else
echo "INFO: Nodemanager $1 is stopped."
fi
else
echo "INFO: NodeManager $1 is already stopped."
fi
fi
return 0
}
#
# output the status of the server
#
function statusServer() {
if onThisHost $2 ; then
if listenerOnPort $2 ${3:-7001}; then
PID=$(getServerPid $1)
if [ -n "$PID" ] ; then
echo "INFO: Server $1 of domain $DOMAIN is running on port ${3:-7001} process id $PID."
else
echo "INFO: Server $1 of domain $DOMAIN port ${3:-7001} is in use, but there is no process."
fi
else
echo "INFO: Server $1 of domain $DOMAIN is not running."
fi
fi
}
#
# Output the status of the node manager.
#
function statusNodeManager() {
if onThisHost $2 ; then
if listenerOnPort $2 ${3:-5556}; then
PID=$(getNodeManagerPid $@)
if [ -n "$PID" ] ; then
echo "INFO: NodeManager $1 is running on port ${3:-5556} process id $PID."
else
echo "WARN: NodeManager $1 port ${3:-5556} is in use, but there is no process."
fi
else
echo "INFO: NodeManager $1 is not running."
fi
fi
}
#
# Start the node manager if on this host and it is not running.
#
function startNodeManager() {
if onThisHost $2 ; then
if listenerOnPort $2 ${3:-5556}; then
echo "INFO: NodeManager $1 is already running."
else
echo -n "INFO: starting $1"
nohup $WL_HOME/server/bin/startNodeManager.sh >/dev/null 2>&1 &
COUNT=0
ISRUNNING=1
while [ $ISRUNNING -ne 0 -a $COUNT -lt $MAXWAIT ] ; do
sleep 3; echo -n .
listenerOnPort $2 ${3:-5556}
ISRUNNING=$?
COUNT=$(($COUNT + 1))
done
echo
if [ $ISRUNNING -eq 1 ] ; then
echo "ERROR: failed to start node manager $2" 2>&1
return 1
fi
fi
fi
}
#
# stop all managed servers.
#
function stopAllServers() {
result=0
listAllServers | \
while read LINE ; do
stopServer $LINE
[ $? -ne 0 ] && result=1
done
return $result
}
#
# report the status of all servers.
#
function statusAllServers() {
result=0
listAllServers | \
while read LINE ; do
statusServer $LINE
[ $? -ne 0 ] && result=1
done
return $result
}
#
# copy the boot.properties of the admin server to the managed servers if it is missing.
#
function copyBootProperties() {
result=0
ADMIN=$(getAdminServer | awk '{print $1}')
SOURCE=$DOMAIN_HOME/servers/$ADMIN/security/boot.properties
if [ -f $SOURCE ] ; then
listAllServers | \
while read LINE ; do
set $LINE
SERVER=$1
if onThisHost $2 ; then
TARGETDIR=$DOMAIN_HOME/servers/$SERVER/security
if [ ! -f $TARGETDIR/boot.properties ] ; then
mkdir -p $TARGETDIR
cp -f $SOURCE $TARGETDIR
[ $? -ne 0 ] && result=1
echo "INFO: copied boot.properties to $SERVER" >&2
fi
fi
done
else
echo "ERROR: No boot.properties found for the admin server to copy. "
result=1
fi
return $result
}
#
# start all managed servers.
#
function startAllServers() {
result=0
listAllServers | \
while read LINE ; do
startServer $LINE
[ $? -ne 0 ] && result=1
done
return $result
}
#
# report the status of all node managers
#
function statusAllNodeManagers() {
result=0
listNodeManagers | \
while read LINE ; do
statusNodeManager $LINE
[ $? -ne 0 ] && result=1
done
return $result
}
#
# start all node managers.
#
function startAllNodeManagers() {
result=0
listNodeManagers | \
while read LINE ; do
startNodeManager $LINE
[ $? -ne 0 ] && result=1
done
return $result
}
#
# stop all the node managers.
#
function stopAllNodeManagers() {
result=0
listNodeManagers | \
while read LINE ; do
stopNodeManager $LINE
[ $? -ne 0 ] && result=1
done
return $result
}
#
# Test the pre-conditions
#
xmlStarletXists=$(which xmlstarlet 2> /dev/null)
if [ -z "$xmlStarletXists" ]; then
echo "ERROR: I am useless without an installation of xmlstarlet! Please install" >&2
exit 1;
fi
if [ ! -d $WL_HOME -a ! -d $NODEMANAGER_HOME ] ; then
echo "ERROR: It does not appear as if WL_HOME is pointing to a WebLogic Server installation directory." >&2
exit 1;
fi
if [ $COMMAND = "stop" ] ; then
for DOMAIN_HOME in $(allDomains) ; do
if setDomainConfig $DOMAIN_HOME ; then
stopAllServers
stopServer $(getAdminServer)
stopAllNodeManagers
fi
done
fi
if [ $COMMAND = "status" ] ; then
for DOMAIN_HOME in $(allDomains) ; do
if setDomainConfig $DOMAIN_HOME ; then
checkExplicitListenAddress
statusAllNodeManagers
statusServer $(getAdminServer)
statusAllServers
fi
done
fi
if [ $COMMAND = "start" ]; then
for DOMAIN_HOME in $(allDomains) ; do
if setDomainConfig $DOMAIN_HOME ; then
checkExplicitListenAddress
startAllNodeManagers
startServer $(getAdminServer)
copyBootProperties
startAllServers
fi
done
fi