Extending XL Deploy with Jython Scripts
It is very easy to extend XL Deploy with Jython scripts. There many ways you can use Jython scripts to extend XL Deploy. Today I will demonstrate how you can use Jython to create a control task to check the filesystem of a host for the largest files. As an administrator it is frequently necessary to look for the largest files on a system and possibility remove unneeded ones.To create our control task we need to define it in the synthetic.xml file as follows:
<type-modification type="overthere.SshHost"> <method name="hog" description="Look for biggest files on file system" task-description="Look for biggest files on file system" delegate="jython" script="custom/host/hog.py"> <parameters> <parameter name="path" kind="string" description="Path to check"/> <parameter name="slash" kind="string" description="Path seperator" default="/" /> <parameter name="maxfiles" kind="integer" description="Top Number of File to return" default="2" /> </parameters> </method> </type-modification>Lets examine what is happening in this snippet of synthetic.xml. The first thing I do is indicate I will be adding something to overthere.SshHosts. When this is done you will see a new option when you right click on a overthere.SshHost. The name of this new command is going to be "hog". There are some other parameters to further define this new task and the location of the script. We can pass parameters to this task and these are defined with the parameter tag. XL Deploy supports a variety of parameter kinds. In this example I am using string and integer kinds. Next lets look at the Jython script.
1 import os 2 3 path = parameters["path"] 4 5 # Windows and linux slashes go in opposite directions. 6 # Uncomment the slash appropriate for your system. 7 systemslash= parameters["slash"] 8 #systemslash='/' 9 10 maxfiles = parameters["maxfiles"] 11 def get_list_of_files(inDirectory, container=): 12 x=0 13 try: 14 for entry in os.listdir(inDirectory): 15 entry = inDirectory+systemslash+entry 16 if os.path.isdir(entry): 17 get_list_of_files(entry,container) 18 else: 19 filesize = os.path.getsize(entry) 20 fileandsize = (filesize, entry) 21 container.append(fileandsize) 22 return container 23 # End for 24 except: 25 x=x+1 26 # End try 27 # End def 28 29 Final_List_of_Files = get_list_of_files(path) 30 Final_List_of_Files.sort(reverse=True) 31 32 fileCount=0 33 for obj in Final_List_of_Files: 34 context.logOutput("%12d - %s" % (obj, obj)) 35 fileCount = fileCount + 1 36 if fileCount >= maxfiles: 37 break 38 # End forThis little script gets the parameters collected from the user in the GUI and passes them to the script. Notice lines 3, 7 & 10. This script gets called with a variable called "parameters" defined with all of the parameters we identified in our synthetic.xml file. With these two files defined in the XL Deploy ext folder we can now restart our XL Deploy server and see the new control task execute. Bellow is are two screen shots of the new control task as it appears in XL Deploy. Once the parameters are entered and the task is executed the output can be seen in the UI as follows: We could package our control task as a plugin as well. In order to package our control task we need to move all of our changes out of the ext folder. Then we need to replicate the files we created in another directory and add a plugin-version.properties file. For this plugin my file is as follows:
1 plugin=custom-hog-plugin 2 version=1.0.0We can then zip the files as a jar file. The files in our jar file is as follows:
Archive: /tmp/custom-hog-plugin-1.0.0.jar testing: custom/ OK testing: custom/host/ OK testing: custom/host/hog.py OK testing: plugin-version.properties OK testing: synthetic.xml OKThen we can copy the new plugin file to the XL Deploy plugin folder and restart the server. The hog control task is running as a plugin. Extending XL Deploy with Jython is very easy and flexible, your imagination is the the only limit on the ways XL Deploy and be extended with Jython. The little control task that I developed for this blog post is simple, but the techniques can be use for much more complicated tasks. The source code for this example is available on GitHub custom-hog-plugin