Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
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 
 -->
<echo file="${LogFile}" message="
${logtime} : ${Message}.
" append="true" />
</target>
</project>
Below is the Deploying Python file.<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 
 -->
<echo file="${LogFile}" message="
${logtime} : ${Message}.
" append="true" />
</target>
</project>
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.'''
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)
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)
'''
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)

Difference between Jython and python
- Python actually means CPython, as it is written in C. where as Jython is for the python scripts to be run on JVM.
- The main advantage of Jython over Cpython is that Jython can call any of the JAVA objects and vice versa. Any jython code compiled into pure java code.
- Due to the high ease of integration and also with huge library base available, jython is a favorite for java programmers.
- How ever jython is lesly being used when compared with Cpython usage due to CPython strength in terms of using OS specific features.
- Jython is one of the main scripting language being used in IBM Websphere and hence it has become such a famous.
- Portability wise Cpython is better than as Jython runs on JVM which brings down its portability.
- Lot more differences were well explained in the following references. Go through this for better understanding.

I Started recently working on jython/python, and come across the following problems and here are the solutions for those
2010-02-25 03:14:40 CDSUpdateAPP.py main ERROR ('Update of application failed:', <class exceptions.NameError at 664938402>, <exceptions.NameError instance at 97256908>)
The above exception can be arise while running jythin scripts which has usage of undeclared variables. see example below.
print super
The above is one of the code line in our jython script, in this case super is treated as variable and if it is not declared the above exceptions will results as super varibale is not initiated.
we can do some thing like
print "super" which will treat super as string and prints it on console.
P.S : Line spaces and alignment are very much important in python scripts

- How to compare strings in the jython/python script
norm = "6.0.2.35"In the above code snippet cmp(wsadminVers, norm) will compare two string values in wsadminVers and norm. If wsadminVers less than norm it return -1. If both are equal it returns 0. If wsadminVers is greater than norm it return 1. So cmp method is very easy string comparison method in jython.
if cmp(wsadminVers, norm) == -1:
print "quote_required"
else:
print "quote_not_required"
quote = 0
print quote
- How to resolve this exceptions or errors
2010-02-25 03:14:40 CDSUpdateAPP.py main ERROR ('Update of application failed:', <class exceptions.NameError at 664938402>, <exceptions.NameError instance at 97256908>)
The above exception can be arise while running jythin scripts which has usage of undeclared variables. see example below.
print super
The above is one of the code line in our jython script, in this case super is treated as variable and if it is not declared the above exceptions will results as super varibale is not initiated.
we can do some thing like
print "super" which will treat super as string and prints it on console.
P.S : Line spaces and alignment are very much important in python scripts

How to find WebsphereVersion(Exact build number) in python/jython scripts ?
The following lines of code will give the exact WAS version along with build number when used in a jython or python script.
wsadminNode = AdminControl.getNode()
print wsadminNode
wsadminSvr = AdminControl.queryNames("node="+wsadminNode+",type=Server,*")
print wsadminSvr
#wsadminVers = AdminControl.getAttribute(wsadminSvr, "platformVersion")
# The above line also gives us the WAS version but it will not give the exact build number details.
wsadminSvrON = AdminControl.makeObjectName(wsadminSvr)
wsadminVers = wsadminSvrON.getKeyProperty('version')
print wsadminSvr
Hope this helps in finding the WAS versions in a jython/python script.
The above when run with wsadmin.sh script some times it throws the following error.
AppSrv01\bin>wsadmin.bat -conntype NONE -javaoption -Xmx384m -f C:/kb/1321CDSUpdateAPP.py -lang jython -appServerName CDSServer -logDir C:/kb -logFile test.log -appFileName "C:\\kb\\test.txt" -appName CDS -controlFile c:/kb/test.txt
2010-02-25 02:41:40 CDSUpdateAPP.py main ERROR ('Update of application failed:',, com.ibm.ws.scripting.ScriptingException: AdminControl service not available)
The above error can be rectified by passing the hostname explicitly or else by changing the connection type to RMI and RMI port. When we run the above lines of code using connType none as below
./wsadmin.sh -conntype NONE -javaoption -Xmx384m -f /opt/builds/1321CDSUpdateAPP.py -lang jython -appServerName CDSServer -logDir /opt/builds -logFile test.log -appFileName /opt/builds/test.txt -appName CDS -controlFile /opt/IBM/WebSphere/AppServer/bin/testctrl.txt
It may through the error above we have seen
Solution for this will be
-host with complete hostname or ipaddr is the alternate solution, since its a failure with Java
probably we can try InetAddress.getAllByName(host) in a standalone Java program and try to resolve it the same solution will apply to wsadmin as well.
Using Connection type RMI
./wsadmin.sh -conntype RMI -port 2809 -lang jython -javaoption -Xmx384m -f /opt/builds/1321CDSUpdateAPP.py -appServerName CDSServer -logDir /opt/builds -logFile test.log -appFileName /opt/builds/test.txt -appName CDS -controlFile /opt/IBM/WebSphere/AppServer/bin/testctrl.txt
Using Host name and removing connection type NONE
./wsadmin.sh -host nc145002.tivlab.austin.ibm.com -lang jython -javaoption -Xmx384m -f /opt/builds/1321CDSUpdateAPP.py -appServerName CDSServer -logDir /opt/builds -logFile test.log -appFileName /opt/builds/test.txt -appName CDS -controlFile /opt/IBM/WebSphere/AppServer/bin/testctrl.txt
P.S: To find the different connection types and ports used by the Websphere installation, try open the Administration console using the url http://
Popular Posts
-
The best solution to know about these init levels is to understand the " man init " command output on Unix. There are basically 8...
-
How to Unlock BSNL 3G data card to use it with Airtel and Vodafone Model no : LW272 ? How to unlock BSNL 3G data card( Model no : LW272 )us...
-
How to transfer bike registration from one State to other (Karnataka to Andhra)?? Most of us having two wheelers purchased and registered in...
-
Latest Post on ETV D3 Old Post on ETV D1 D2 link ETV Dhee 3 - D3 - Enjoy maadi ETV Dhee feedback link - Direct ETV site use this...
-
ఓం శ్రీ స్వామియే శరణం ఆయ్యప్ప!! Related posts : Trip to Sabarimala - Part 1 Trip to Sabarimala - Part 2 Ayappa Deeksha required things...
Popular posts
- Airtel and vodafone GPRS settings for pocket PC phones
- Andhra 2 America
- Ayyappa Deeksha required things
- Blogs I watch !
- Captions for your bike
- DB2 FAQs
- Deepavali Vs The Goddes of sleep
- ETV - Dhee D2 D3
- Evolution of smoking in India Women
- How to make credit card payments?
- init 0, init 1, init 2 ..
- Java-J2EE interview preparation
- mCheck Application jar or jad download
- My SQL FAQs
- My Travelogues
- Old is blod - New is italic
- Online pay methids for credit cards
- Oracle FAQs
- Pilgrimages
- Smoking in Indian Women
- Technology Vs Humans
- Twitter feeds for all Telugu stars on single page.
- Unix best practices
- Unix FAQs
