XL Deploy Control Task for Managing A Repository

| February 24, 2016 | 1 Comment

Today we’ll talk about how you can write a Control Task in XL Deploy using Jython for managing the contents of the XL Deploy Repository itself.


Control Tasks

Control tasks are actions that can be performed on middleware or middleware resources; for example, checking the connection to a host, start/stop a middleware server is a control task. When a control task is invoked, XL Deploy starts a task that executes the steps associated with the control task.

To trigger a control task on a CI in the repository, do the following:

  1. List the control tasks for a CI. In the Repository, locate the CI for which you want to trigger a control task. Right-click the item to see the control tasks.
  2. Execute the control task on a CI. Select the control task you want to trigger. This will invoke the selected action on the CI.
  3. Provide parameters. If the control task has parameters, you must provide them before you start the control task.

Control tasks comes as both simple tasks which you can trigger, and more complex ones where you can pass some parameters for a desired behavior.

The links provided below will help you build your knowledge of Control Tasks

Understanding control tasks in XL Deploy || XebiaLabs documentation

Create a custom control task || XebiaLabs documentation

Control task delegates in the XL Deploy Generic plugin || XebiaLabs …

Execute a control task from the XL Deploy CLI || XebiaLabs …

Add a control task to an existing type || XebiaLabs documentation

The third link provides knowledge of the most common types of control tasks available:

The shellScript delegate has the capability of executing a single script on a target host.

The localShellScript delegate has the capability of executing a single script on a the XL Deploy host.

The shellScripts delegate has the capability of executing multiple scripts on a target host.

The localShellScripts delegate has the capability of executing multiple scripts on the XL Deploy host.

 

jythonScript Delegate

If you want to create or update your XL Deploy Repository, the jythonScript Delegate can come to the rescue.

I’ve provided some samples of how to use jythonScript Delegates. You can copy/paste the content in your synthetic.xml under XLDeploy_HOME/ext folder. and create a utils folder XLDeploy_HOME/ext. Create the mentioned scripts and copy content in them under the utils folder.

Sample 1 use case : Fill a dictionary with values from properties file

Usage :To use the following task, do the following

  1. Go to the repository view in UI
  2. Create an empty dictionary anywhere under the Environment hierarchy and save it.
  3. Then right click on the dictionary and select “Add Raw Properties”
  4. In the new tab on right side, copy/paste the content of a properties files and the execute
  5. The task will fill the dictionary with the key/value pairs from the properties file and give back an output that consists of key={{key}} for all keys. You can paste it back into your properties file and voila! You have a ready dictionary along with properties file updated with placeholders.

Synthetic.xml

 <type-modification type="udm.Dictionary">
    <method name="getProperties" label="Add Raw Properties.." description="Pulls dictionary values from raw properties" delegate="jythonScript"
      script="utils/addProperties.py">
      <parameters>
        <parameter name="text" size="large" />
      </parameters>
    </method>
  </type-modification>

utils/addProperties.py

import sys
print "=================================================================="
print "Copy new file contents with replaced placeholders from below here"
print "=================================================================="
properties = [value.strip() for value in params.text.split("\n")]
rep = thisCi._delegate
for prop in properties:
    if  prop.find("#") !=0 and len(prop) != 0:
        keyVal = prop.partition("=")
        rep.entries[keyVal[0]] = keyVal[2]
        print keyVal[0] + "={{" + keyVal[0] + "}}"
    else:
        print prop
repositoryService.update(thisCi.id,thisCi)

Sample 2 use case : Create multiple folders under a hierarchy

Usage : To use the following task, do the following:

  1. Go to the repository view in UI
  2. Right click on any parent  (Infrastructure/Environment/etc) or any existing Directory
  3. Select the option “Create Folders under”
  4. Then, in the new tab, provide a coma separated list of folder names and execute
  5. Once you have refreshed the view, you’ll see new folders created under the hierarchy

Synthetic.xml

 <type-modification type="core.Directory">
    <method name="createFolders" label="Create Folders Under" description="Create multiple folders under this directory" delegate="jythonScript"
      script="utils/createFolders.py">
    <parameters>
        <parameter name="folderNames" description="Provide comma separated folder names"  />
      </parameters>
    </method>
  </type-modification>
  
  <type-modification type="internal.Root">
    <method name="createFolders" label="Create Folders Under" description="Create multiple folders under this directory" delegate="jythonScript"
      script="utils/createFolders.py">
    <parameters>
        <parameter name="folderNames" description="Provide comma separated folder names"  />
      </parameters>
    </method>
  </type-modification>

utils/createFolders.py

import sys
from com.xebialabs.deployit.plugin.api.reflect import Type
def createFolder(foldername):
    if (repositoryService.exists(foldername)):
        return False
    typeObj = Type.valueOf("core.Directory")
    myfolder = metadataService.findDescriptor(typeObj).newInstance(foldername)
    repositoryService.create(myfolder.id, myfolder)
    return True
for folder in params.folderNames.split(","):
    if createFolder(thisCi.id + "/" + folder):
        print "Created Folder : " + thisCi.id + "/" + folder

Sample 3 use case : Perform a control task on multiple items of the same type

Usage : To use the following task, do the following:

  1. Go to the repository view in UI
  2. Go to infrastructure and right click on that or some directory. (NOTE: Now this code only showcases the capability of running multiple control task at once, but references to only overthere.Host type and executes control tasks that don’t take any parameters. If you want to change this behavior, you would have to further modify the script and method signature)
  3. Select the option “Perform Bulk Control Task”
  4. The in the new tab, first select a list of hosts from the available hosts
  5. Second, type a control task name like checkConnection and then execute
  6. In the log section below, you would see that the control task would iterate over each Host and execute a checkConnection
  7. You can see them later if you go to Reports > Controls Task tabs and search for the current date/time range

Synthetic.xml

 <type-modification type="internal.Root">
    <method name="performBulkControlTask"
            description="Performs a control task on multiple CIs of same type"
            task-description="Performs a control task on multiple CIs of same type"
            delegate="jythonScript"
            script="utils/performBulkControlTask.py">
        <parameters>
            <parameter name="hostList"  kind="list_of_ci" referenced-type="overthere.Host" description="Host Selection" />
            <parameter name="controlTaskName"  kind="string" description="Control Task Name" />
        </parameters>
    </method>
</type-modification>

utils/performBulkControlTask.py

import time
for item in params.hostList:
    ctrlobj = controlService.prepare(params.controlTaskName,str(item))
    taskid = controlService.createTask(ctrlobj)
    taskBlockService.start(taskid)
    taskobj = taskBlockService.getTask(taskid)._delegate
    print "Executing " + taskobj.description
    time.sleep(5)
    for item in taskobj.steps:
        print item.description + " : " + str(item.state) + "\n" + item.log
    taskBlockService.archive(taskid)

 

You can change the logic of any of these scripts for a different desired behavior. Hopefully I have been able to showcase the capability of what you can achieve using jythonScript delegate.

Happy Learning.

 


Looking for more XL Deploy Tips and Tricks? We’ve got plenty on our Resources Page!


About the Author ()

Amit is the Lead Pre-Sales Solution Architect managing US Pre-Sales Team at XebiaLabs.