Skip to main content
DevOps Image

This post is from the XebiaLabs blog and has not been updated since the original publish date.

Last Updated Dec 09, 2014 — DevOps Expert

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"
            <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" />
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
  3 path = parameters["path"]
  5 # Windows and linux slashes go in opposite directions.
  6 # Uncomment the slash appropriate for your system.
  7 systemslash= parameters["slash"]
  8 #systemslash='/'
 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
 29 Final_List_of_Files = get_list_of_files(path)
 30 Final_List_of_Files.sort(reverse=True)
 32 fileCount=0
 33 for obj in Final_List_of_Files:
 34    context.logOutput("%12d - %s" % (obj[0], obj[1]))
 35    fileCount = fileCount + 1
 36    if fileCount >= maxfiles:
 37       break
 38 # End for
This 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 file.  For this plugin my file is as follows:
  1 plugin=custom-hog-plugin
  2 version=1.0.0
We 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/       OK
    testing:   OK
    testing: synthetic.xml            OK
Then 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

More from the Blog

View more
May 17, 2021

Why Companies in Competitive Industries Adjusted Better During COVID-19

As we continue to assess the dramatic effects of the global COVID-19 p ...
Read More
May 06, 2021

Use Value Stream Management to release apps with confidence

Many companies worldwide use a blend of DevOps and agile methods to he ...
Read More
Agile or DevOps on Its own Is not enough
Apr 23, 2021

Agile or DevOps on Its own Is not enough

As every company becomes a software company, it becomes increasingly i ...
Read More
Mar 16, 2021

Does successful change management require DevOps?

Around the world, digital product providers are looking to reduce dysf ...
Read More
Contact Us