Invoking p(j)ython script through ant script to deploy and un-deploy WAR files in Websphere

How to invoke p(j)ython script through ANT scripts do deploy WAR files on to Webshere. ?  
The below is a simple ant script invoking python files which will deploy and un-deploy the given J2EE Apps in running Websphere server profile.
1. The ant script will invoke python files.
2. Ant script's target pathconvert will convert the property values containg back slashes in a file path to forward slashes.
3. Ant script target timestamp will print the time stamp along with message in the given log file. Takes care of new line character using 
 at the end of message ...
Ant Script to call jython scripts which will deploy and undeploy provided J2E WAR file.
Below is the Ant script.
hi
<project name="DeployFSAdmin" default="install" basedir=".">
    <description>
        Silent Deployment of WAR files
    </description>
    <property name="FSAdminLog" value="c:/FSAdmin_install.log" />
    <target id="install" name="install" depends="pathconvert">
        <antcall target="timestamp">
            <param name="LogFile" value="${FSAdminLog}" />
            <param name="Message" value="Starting Server now" />
        </antcall>
        <exec executable="C:\\Program Files\\IBM\\WebSphere\\AppServer\\profiles\\AppSrv01\\bin\\startServer.bat" output="${FSAdminLog}" append="true">
            <arg value="server1" />
        </exec>
        <antcall target="timestamp">
            <param name="LogFile" value="${FSAdminLog}" />
            <param name="Message" value="Starting FS Admin App Deployment now" />
        </antcall>
        <exec executable="C:\\Program Files\\IBM\\WebSphere\\AppServer\\profiles\\AppSrv01\\bin\\wsadmin.bat" output="${FSAdminLog}" append="true">
            <arg value="-lang" />
            <arg value="jython" />
            <arg value="-username" />
            <arg value="krishna" />
            <arg value="-password" />
            <arg value="krishna" />
            <arg value="-f" />
            <arg value="${deployFile}" />
            <arg value="${updatedWARFile}" />
            <arg value="FSAdminApp" />
            <arg value="server1" />
        </exec>
        <antcall target="timestamp">
            <param name="LogFile" value="${FSAdminLog}" />
            <param name="Message" value="FS Admin App Deployment completed" />
        </antcall>
    </target>
    <target id="unistall" name="uninstall">
        <antcall target="timestamp">
            <param name="LogFile" value="${FSAdminLog}" />
            <param name="Message" value="Starting Server now." />
        </antcall>
        <exec executable="C:\\Program Files\\IBM\\WebSphere\\AppServer\\profiles\\AppSrv01\\bin\\startServer.bat" output="${FSAdminLog}" append="true">
            <arg value="server1" />
        </exec>
        <antcall target="timestamp">
            <param name="LogFile" value="${FSAdminLog}" />
            <param name="Message" value="Un-Deployment of FS Admin in progress..." />
        </antcall>
        <exec executable="C:\\Program Files\\IBM\\WebSphere\\AppServer\\profiles\\AppSrv01\\bin\\wsadmin.bat" output="${FSAdminLog}" append="true">
            <arg value="-lang" />
            <arg value="jython" />
            <arg value="-username" />
            <arg value="krishna" />
            <arg value="-password" />
            <arg value="krishna" />
            <arg value="-f" />
            <arg value="${undeployFile}" />
            <arg value="FSAdminApp" />
            <arg value="server1" />
        </exec>
        <antcall target="timestamp">
            <param name="LogFile" value="${FSAdminLog}" />
            <param name="Message" value="Undeployment of FASApp completed" />
        </antcall>
    </target>
    <!-- The target "pathconvert" is to convert all the windows back slahes into forward slashes-->
    <target id="pathconvert" name="pathconvert">
        <tempfile property="temp.file" />
        <echo message="${warfile}" file="${temp.file}" />
        <loadfile srcfile="${temp.file}" property="updatedWARFile">
            <filterchain>
                <replaceregex pattern="\\" replace="/" flags="g" />
            </filterchain>
        </loadfile>
        <delete file="${temp.file}" />
    </target>
    <!-- To print the time stamp along with message in a log file -->
    <target name="timestamp">
        <tstamp>
            <format property="logtime" pattern="yyyy.MM.dd ':' HH:mm:ss z" />
        </tstamp>
        <!-- New line charecter uasage &#xa; -->
        <echo file="${LogFile}" message="&#xa;${logtime} : ${Message}.&#xa;" append="true" />
    </target>
</project>


Below is the Deploying Python file.
hi
'''
Created on Jan 18, 2012

@author: krishna
'''
import sys
#--------------------------------------------------------------
# Install an application onto this server
#--------------------------------------------------------------
def install(serverName, nodeName, app, appName=None):
    print "Installing the application"
    appOptions = "[-server " + serverName + " -node " + nodeName
    if len(appName) != 0:
         appOptions = appOptions + " -appname " + appName +" -contextroot " + appName
         appOptions = appOptions + " -defaultbinding.virtual.host default_host -usedefaultbindings]"
    print "Server Options: " + appOptions
    AdminApp.install(app, appOptions)
#--------------------------------------------------------------
# Save all changes and stop the server
#--------------------------------------------------------------   
    print "Now saving all the configurations"
    AdminConfig.save()
    print "Now stoping the server"
    AdminControl.stopServer(serverName, nodeName)



#-----------------------------------------------------------------
# Main
#-----------------------------------------------------------------

if (len(sys.argv) != 3):
   print "This script requires 3 parameters:, , "
   print "e.g.:  argsDeployWARfile.py C:/Krish/SimpleJ2E_1.0.0.war SimpleJ2E server1"
else:
   application = sys.argv[0]
   appName = sys.argv[1]
   print "application: " + application
   print "app name: " + appName
   nodeName = AdminControl.getNode()
   print nodeName
   serverName = sys.argv[2]
   print serverName
   install(serverName, nodeName, application, appName)

Below is the un-deploying Python file.
hi
'''
Created on Jan 18, 2012

@author: krishna
'''
#-----------------------------------------------------------------
# Main
#-----------------------------------------------------------------

if (len(sys.argv) != 2):
   print "This script requires 1 parameters: , "
   print "e.g.:  undeployWAR.py SimpleJ2E server1"
else:
   appName = sys.argv[0]
   print "application name: " + appName
   serverName = sys.argv[1]
   print "Server Name: " + serverName
   nodeName = AdminControl.getNode()
   AdminApp.uninstall (appName)
   print "Now saving all the configurations"
   AdminConfig.save()
   print "Now stopping the server"
   AdminControl.stopServer(serverName, nodeName)



0 comments to "Invoking p(j)ython script through ant script to deploy and un-deploy WAR files in Websphere"

Post a Comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree