Last Updated Feb 23, 2016 — DevOps Expert
DevOps

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 TasksUnderstanding control tasks in XL Deploy || XebiaLabs documentationCreate a custom control task || XebiaLabs documentationControl 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 documentationThe 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 fileUsage :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

 
    
      
        
      
    
  

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 hierarchyUsage : 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

 
    
    
        
      
    
  
 
  
    
    
        
      
    
  

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 typeUsage : 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

 
    
        
            
            
        
    

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!

Are you ready to scale your enterprise?

Explore

What's New In The World of Digital.ai

August 4, 2023

Why Financial Services Need DevSecOps More Than Ever

With Digital.ai DevSecOps solutions, financial organizations can improve their security posture, meet regulatory requirements and focus on delivering innovative financial products.

Learn More
June 23, 2023

Governance and Compliance for DevOps at Scale

Implement a Software Chain of Custody in DevOps for compliance, traceability, and cost reduction. Gain visibility and automate processes with Digital.ai Release & Deploy.

Learn More
April 10, 2023

Continuous Delivery Implementation Done Right: Learn from National Broadband Ireland Story

Through NBI’s approach to be the best open-access operator in telecom, they have been supported by key technology partners, with automation capabilities provided by Digital.ai Release and Deploy underpinning integral aspects.

Learn More