Websphere WSadmin.sh Scripting basics
WSADMIN Scripting basiscs:
Why to Use Scripting?
- Making configuration changes using the Websphere administrative console is error prone and dangerous in complex production environments
- Routine tasks can be automated, making repetitive tasks more easily done and done consistently
- Automated tasks faster to execute.
- Common Operational and Configurationally tasks can be performed from scripts and command line
What can be done using WSADMIN?
Few Examples:
- Start and stop deployment manager, nodes, application servers, enterprise applications and clusters
- Configure virtual hosts, JDBC providers, JMS resources
- Create application servers
- Create clusters and add members to a cluster
- Deploying Applications
- Almost all tasks available through administrative Console
Running WSADMIN
WSADMIN tool can be run in 3 different modes
- Interactive mode
This is the default mode. In this you’ll enter the command, wait for output and then enter the second command. This is useful for quick administrative tasks
$wsadmin.sh
>wsadmin> your Command
Output
>wsadmin
- Inline mode
You can specify this mode by using –C after wsadmin. Here you will run one command at a time. The command will be specified in line with wsadmin.
$wsadmin.sh –C your_command
- Using a File Input
A script file can be given as input using –f option along with wsadmin.
$ wsadmin.sh –f your_script_file
How to use WSADMIN
Wsadmin.sh host port
- Host name is the name of the machine which has websphere running and port number is either SOAP or RMI port number of the websphere JVM to which you want to connect.
- If the Global security is enabled…you will be asked for user name and password or you can specify it inline using –user and –password.
- You can specify the language of the script file, the command, or an interactive shell using –Lang. The possible languages include: Jacl and Jython
- You can run the profile scripts before executing the actual wsadmin commands by using –profile option.
- To specify the profile from which the wsadmin tool will run, you can use –profileName option.
- Note: the wsadmin command will be available in bin directory of your DMGR as well as the bin your application server profiles.
- You can use either SOAP or RMI to connect to the wsadmin and this can be specifeid using –conntype option.
WSADMIN Syntax and all options
wsadmin [-c ] [-p ] [-profile ] [-profileName ] [-f ] [-javaoption java_option] [-lang language] [-wsadmin_classpath classpath] [-conntype SOAP [-host host_name] [-port port_number] [-user userid] [-password password] | RMI [-host host_name] [-portport_number] [-user userid] [-password password] | NONE]
Example: wsadmin -lang jython -f test1.py -profile setup.py -conntype SOAP -port mymachinesoapportnumber -hostmymachine -userid userid -password password
WSADMIN has 5 objects
- Help – The Help object provides general help and dynamic online information about the currently running MBeans. You can use the Help object as an aid in writing and running scripts with the AdminControl object.
- AdminConfig – Use the AdminConfig object to invoke configuration commands and to create or change elements of the WebSphere Application Server configuration
- AdminControl – Use the AdminControl object to invoke operational commands that deal with running objects in the WebSphere Application Server.
- AdminApp – Use the AdminApp object to install, modify, and administer applications.
- AdminTask – Use the AdminTask object to run an administrative command.
Using WSADMIN:
Deploying Applications
You can install the application in batch mode, using the install command, or you can install the application in interactive mode using the install interactive command. Interactive mode prompts you through a series of tasks to provide information. All the options available to use are listed here in the IBM documentation.
Note: words in bold are command or options and in italic are your inputs
To find out the options available to install the EAR file, run:
AdminApp.options()
Installing EAR application:
Using Batch Mode:
If you are deploying on single servers….make sure that server is running before you execute the commands.
AdminApp.install(‘/home/joseph/applications/SampleApp.ear’, ‘[-server test_server1]‘)
If you are deploying it in network deployment/ Clusters, you must connect to WSADMIN of deployment manager.
AdminApp.install(‘/home/joseph/applications/SampleApp.ear’, ‘[-cluster sample_cluster]‘)
Using Interactive Mode:
AdminApp.installInteractive(‘/home/joseph/applications/SampleApp.ear’)
now it will take you through a series of questions before finishing the deployment.
After the deployment is done, you need to perform two more steps
step1. Save the changes by issuing
AdminConfig.save()
step2. Synchronize the changes across all nodes by using
Sync1 = AdminControl.completeObjectName(‘type=NodeSync,node=myNodeName,*’)
and then
AdminControl.invoke(Sync1, ‘sync’)
step2 is required only for network deployment models.
Starting Applications
This involves two steps.
step1: getting the handler of the application, means .. locating where the application is running. To do this we use a variable and use query names option on admincontrol object
appHandle= AdminControl.queryNames‘cell=SampleCell, node=virt_node1,type=ApplicationManager,process=test_server1,*’)
print appHandle
then you will get an output like the one below
WebSphere:cell=SampleCell,name=ApplicationManager,mbeanIdentifier=ApplicationManager,type=ApplicationManager,node=virt_node1,process=test_server1
step2: Now we execute the command to start the application
AdminControl.invoke(appHandle, ‘startApplication’, ‘Dynamic Cache Monitor’)
You can do both steps at once by using the below format
AdminControl.invoke(‘WebSphere:name=ApplicationManager,process=test_server1,node=virt_node1,type=ApplicationManager,mbeanIdentifier=ApplicationManager,cell=SampleCell, ‘startApplication‘, ‘[“Dynamic Cache Monitor“]‘, ‘[java.lang.String]‘)
Stopping Application
The steps are same as starting the application except ‘startApplication’ should be renamed to ‘stopApplication’
But, if you want to stop all applications running on a server… after executing step1 of above
Step2: you need to first get the list of applications
# get line separator
import java.lang.System as sys
lineSeparator = sys.getProperty(‘line.separator’)
apps_list =AdminControl.queryNames(‘cell=SampleCell,node=virt_node1,type=Application,process=test_server1,*’).split(lineSeparator)
print apps_list
Step3: now we need to create a loop to stop the applications one by one
for app in apps_list:
appName = AdminControl.getAttribute(app, ‘name’)
AdminControl.invoke(appHandle, ‘stopApplication’, appName)
Updating Application
We have different options to update applications, like, full update, module update, single file update etc…Similar to the install, we do have both batch mode and interactive mode for updating [update and updateinteractive]. All the options available to use are listed here in the IBM documentation.
- updating entire application
AdminApp.update(‘SampleApp’, ‘app’, ‘[-operation update -contents /home/joseph/applications/SampleAppv2.ear ]‘)
- Adding a new module to EJB
AdminApp.update(‘SampleApp’, ‘modulefile’, ‘[-operation addupdate -contents /home/joseph/applications/Increment.jar -contenturi Increment.jar -nodeployejb
-BindJndiForEJBNonMessageBinding [[“Increment Enterprise Java Bean” Increment Increment.jar,META-INF/ejb-jar.xml Inc]]]’)
- Updating Single file
AdminApp.update(‘SampleApp’, ‘file’, ‘[-operation update -contents /home/joseph/applications/my.xml -contenturi SampleApp/my.xml]‘)
TIP: Use the taskInfo command of the AdminApp object to obtain information about the data that is needed for your application.
for example, you want to know what options you need to supply for mapping modules to servers…use the following taskinfo command
AdminApp taskInfo MapModulesToServers
- Adding an application server
There are 3 steps involved in creating a new application server
first we need to get the node on which we want to create the application server. Obtain the configuration ID of the object and assign it to the node variable
node = AdminConfig.getid(‘/Node:mynode/’)
Then, create the application server using admintask
AdminTask.createApplicationServer(node, [‘-name’, ‘test1’, ‘-templateName’, ‘default’])
where test1 is the name of the server you want to create and mynode is the node on which this server will be created.
Then, save the changes using admintask.save ()
- create a new cluster
This involves 3 steps
1A. using existing server
1Aa. Choose the server you want to convert into server and assign it to a variable
server = AdminConfig.getid(‘/Cell:mycell/Node:mynode/Server:server1/’)
1Ab. Convert that existing server into cluster
print AdminConfig.convertToCluster(server, ‘myCluster1?)
1B. creating new cluster without members
1Ba. Identify the cell configuration ID and assign it to a variable
C1 = AdminConfig.getid(‘/Cell:mycell/’)
1Bb. Create a new cluster without a cluster member
print AdminConfig.create(‘ServerCluster’, C1, ‘[[name ClusterName]]’)
- Adding new members:
2a. get the ncluster configuration ID and assign it to a variable
cluster = AdminConfig.getid(‘/ServerCluster:myCluster1/’)
print cluster
2b. get the ncluster configuration ID and assign it to a variable
node = AdminConfig.getid(‘/Node:mynode/’)
print node
2c. get the ncluster configuration ID and assign it to a variable
serverTemplate = AdminConfig.listTemplates(‘Server’)
print serverTemplate
2d. Create the cluster member
AdminConfig.createClusterMember(cluster, node, [[‘memberName’, ‘clusterMember1’]])
- save the changes and synchronize them.
- start/stop application servers
Starting
print AdminControl.startServer(‘server1?, ‘mynode’)
Output:
WASX7319I: The serverStartupSyncEnabled attribute is set to false. A start will be attempted for server “server1? but the configuration information for node “mynode” may not be current.
WASX7262I: Start completed for server “server1? on node “mynode”
Stopping
print AdminControl.stopServer(‘serverName’, ‘mynode’)
this command will stop the server named servername on node named mynode. Since we used print, we can see the following output on the console
Output
WASX7337I: Invoked stop for server “serverName” Waiting for stop completion.
WASX7264I: Stop completed for server “serverName” on node “mynode”
Exit codes for stop/starting of the servers
Server state | Return code |
Server initialization failed | -1 |
Server initialization timed out | -2 |
Server start in progress | 1 |
Server is initializing the applications that are present | 2 |
Server initialization is complete (successful) | 0 |
Server stop failed | -10 |
Server stop timed out | -11 |
Server stop operation started | 1000 |
Server successfully stopped | 0 |
- Checking the status of the servers
first get the server MBean and assign it to a variable
server = AdminControl.completeObjectName(‘cell=mycell,node=mynode,name=server1,type=Server,*’)
Print server
you can see output similar to the below:
WebSphere:cell=mycell,name=server1,mbeanIdentifier=server.xml#Server_1,type=Server,node=mynode,process=server1,processType=ManagedProcess
The execute the following command
print AdminControl.getAttribute(server, ‘state’)
This will return the state of the server on the command line, something like STARTED/STOPPED etc.
Recent Comments