Chapter 8. Administration with scripting 343
Figure 8-5 Jython editor running the command output automatically
8.8 Example: Using scripts with the job manager
This section provides an example of how to use scripting to automate a WebSphere
installation that uses a flexible management environment.
Most companies have routine tasks that occur in different phases of the software
development lifecycle. In an environment with multiple WebSphere Application Server
installations, automation of these tasks can save a lot of time. The combination of wsadmin
scripts, script libraries, and the automated management provided in a flexible management
environment provides an automation solution.
8.8.1 Introduction
The scenario that we describe here uses a simple WebSphere environment to illustrate how
to automate tasks. You can use these techniques in more complex environments. This
scenario contains the following steps:
1. Write a customized script to automate the tasks.
2. Configure the job manager.
3. Verify the results.
344 WebSphere Application Server V8.5 Administration and Configuration Guide for the Full Profile
Figure 8-6 shows the scenario environment. A single application server is configured on
Node B. The deployment manager for the cell is registered to a job manager on Node A.
Figure 8-6 The environment details
Applications in this environment are installed frequently, and the administrator needs a quick
way to install these applications, which can be accomplished by completing the following
steps:
1. A wsadmin script is prepared to install an application. The script makes use of the script
libraries.
2. A job is scheduled to run the script at regular intervals.
3. The script checks a text file that lists the new applications to be installed. The new
application information stored in a text file includes the application name, application
location, node name, and server name (see Figure 8-7).
Figure 8-7 Scripting details
Node B
Applications
(hellon.ear)
Configurations
Deployment
Manager
Application
Server
Node A
Configurations
Job Manager
Install
wsadmin script
(python)
LIST
AppName(EAR)
AppDirectory
NodeName
ServerName
appinstall.py
1. Check the list
2. Install the application
(using AdminTask script library)
Hello1.ear Hello2.ear
Install
Chapter 8. Administration with scripting 345
4. If the job runs and finds that the application is not installed, the application is then
installed. If the application is already installed, it is uninstalled and then re-installed.
5. The text file is renamed filename.txt.old when the job is executed.
6. If the job executes and no text file exists, no actions are taken. It is only when you
distribute a new text file and application that the job performs the install.
To test, we used the following applications:
hello1.ear
hello2.ear
8.8.2 Creating the customized script
The AdminApplication script in the script libraries includes procedures that accomplish the
installation and update of applications. In this scenario, we used the following procedures
from the script libraries:
AdminApplication.uninstallApplication
This procedure is used to uninstall an application. The arguments specify the application
name.
AdminApplication.installAppWithNodeAndServerOptions
This procedure is used to install an application. This procedure is selected because there
is only a single server in this environment. In an environment with clustered application
servers, use the installAppWithClusterOption procedure instead.
The script file is written in Python and is called appInstall.py (see Example 8-28).
Example 8-28 The appInstall.py script
#Check the list and install/update the applications.
#
import sys
import java
import os
import re
import time
from java.lang import *
dir = "C:/WebSphereV8.5/AppServer/profiles/dmgr40/downloadedContent/inputfile"
#
sep = System.getProperty("line.separator")
for fname in os.listdir(dir):
print fname
path = os.path.join(dir, fname)
if (os.path.isfile(path)) and (not re.match(".*old$",path) and (not
re.match("appInstall.py",fname))):
print "procesing %s" % (path)
inp = open(path, 'r')
for line in inp.readlines():
itemList = line[:-1].split(',')
appName = itemList[0]
earFile = itemList[1]
nodeName= itemList[2]
346 WebSphere Application Server V8.5 Administration and Configuration Guide for the Full Profile
serverName = itemList[3]
# application existence check
print "application existence check"
existAppList = AdminApp.list().split(sep)
isExist = 0
for existApp in existAppList:
if(appName == existApp):
isExist = 1
break
# acquire application manager
print "acquire application manager"
appMgrID =
AdminControl.queryNames("type=ApplicationManager,node="+nodeName+",process="+serverName+",*" )
# if exists, uninstall application
print "app exists - uninstall"
if( isExist == 1 ):
appId = ""
try:
_excp_ = 0
appID =
AdminControl.completeObjectName("type=Application,node="+nodeName+",Server="+serverName+",name="
+appName+",*" )
except:
_type_, _value_, _tbck_ = sys.exc_info()
_excp_ = 1
#endTry
# if running, stop application
print "appID is %s" % (appID)
if(len(appID) > 0):
print "stopping %s" % (appName)
stopped = AdminControl.invoke(appMgrID, "stopApplication", appName)
sleep(1)
# uninstall application
print "Uninstalling %s" % (appName)
AdminApplication.uninstallApplication(appName)
# install application
print "Installing %s" % (appName)
AdminApplication.installAppWithNodeAndServerOptions(appName, earFile, nodeName,
serverName)
print "Starting %s" % (appName)
started = AdminControl.invoke(appMgrID, "startApplication", appName)
sleep(1)
inp.close()
os.rename(fname, fname + ".old")
#endIf
#endFor

Get WebSphere Application Server V8.5 Administration and Configuration Guide for the Full Profile now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.