Anybody who writes python scripts for WebSphere Applican Server quickly discovers the need for libraries of scripts. Finding documentation that will help you create and use libraries of your own python scripts is not easy. It is also not easy to find documentation that tells you how to import the WAS script objects into your script libraries. This article will walk you through that process.
In order to create, install and use a python library of your own creation, you must:
- Make the WAS script objects available to your scripts
- Load your scripts as profiles when wsadmin starts
- Run any initialization code that your scripts might require
In order to get wsadmin to load your python script library, you will have to write something that IBM calls a profile script. This is a script that wsadmin runs before its environment is fully initialized. Let us write some python code that displays the environment of wsadmin at this not-so-fully-initialized time. Download junkProfile.py and save it on your file system. Next, call wsadmin using junkProfile.py as a profile. You will have to type the fully qualified name of the profile. On my system, that name is /home/localuser/junkProfile.py Then, we will redirect the output of wsadmin to a file on my system. The command line to do this will look something like this. Adjust the path names to match the realities of your WAS installation.
./wsadmin.sh -profile /home/localuser/junkProfile.py -conntype none -c "print 'Hello World!' " > ~localuser/junkOutput.txt
Open junkOutput.txt and do a text search for the following words. You won't find them. You will find lots of other stuff, but not these words.
It is very hard to write administrative scripts that do not use one or more of these script objects. We are going to modify the initialization environment of wsadmin so that the five script objects become available. We will use a profile script called WASscriptingImports.py in order to make the WAS script objects availible to our library scripts and in order get wsadmin to load our library scripts.
Line 7 through line 11 make the five WAS script objects available to our various library scripts. This is how those lines work. Python has a sys module. By default, it is one of the modules that wsadmin imports automatically. One of the data structures of the Python sys module is a dictionary called "modules". We make the five WAS scripting objects available for our library scripts by inserting them into the sys.modules dictionary. Once we do that, we can load library modules that import any or all of the administrative script objects.
The sample script library files are in a zip file that you can download. Unpack the zip file in a location of your choice. You will find a WASscriptingImports.py file and two subdirectories: server and misc. In order to import each of our script library files, we must first add the fully qualified path to server and misc to wsadmin's classpath. Then we can import the individual files in those folders.
Lines 23 and 29 add our folders to wsadmin's classpath. Notice that those two lines depend on a jython variable called "scriptDirectory". That variable gets set on line 17 of WASscriptingImports.py You have to modify this line to reflect the location where you unpacked our sample library zip file.
The last thing you have to do is modify your wsadmin.properties file. If you look at line 86 of the sample wsadmin.properties file, you will find a property called com.ibm.ws.scripting.profiles. In our sample wsadmin.properties file, that property has three file names. In the wsadmin.properties file in the config directory of your deployment manager profile, that same property probably has only two file names. Look at the last file name in the com.ibm.ws.scripting.profiles property in our sample wsadmin.properties file. Line 86 looks like this:
In order to make it fit on this page, we split the line above at each semicolon. In real life, the above sample would all be on one line and theere would be no spaces anywhere. Notice that the last file name on that line is the fully qualified file name of our profile file. Make that path match the realities of your system and you have just loaded our sample script library into wsadmin.
To prove that you successfully loaded our sample library files, start wsadmin in interactive mode. Make sure that jython is your default language. When you get a wsadmin prompt, you can type:
dir( serverFunctions )
The first line will show you all the public names in the serverFunctions module. The second will show you the documentation for the serverFunctions module as a whole. The third will show you the documentation for one method in the serverFunctions module.