Last Updated Feb 23, 2016 — DevOps Expert
XL Deploy Control Task for Managing A Repository
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:- 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.
- Execute the control task on a CI. Select the control task you want to trigger. This will invoke the selected action on the CI.
- Provide parameters. If the control task has parameters, you must provide them before you start the control task.
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, thejythonScript
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- Go to the repository view in UI
- Create an empty dictionary anywhere under the Environment hierarchy and save it.
- Then right click on the dictionary and select "Add Raw Properties"
- In the new tab on right side, copy/paste the content of a properties files and the execute
- 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.
<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 hierarchyUsage : To use the following task, do the following:
- Go to the repository view in UI
- Right click on any parent (Infrastructure/Environment/etc) or any existing Directory
- Select the option "Create Folders under"
- Then, in the new tab, provide a coma separated list of folder names and execute
- Once you have refreshed the view, you'll see new folders created under the hierarchy
<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 + "/" + folderSample 3 use case : Perform a control task on multiple items of the same typeUsage : To use the following task, do the following:
- Go to the repository view in UI
- 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)
- Select the option "Perform Bulk Control Task"
- The in the new tab, first select a list of hosts from the available hosts
- Second, type a control task name like checkConnection and then execute
- In the log section below, you would see that the control task would iterate over each Host and execute a checkConnection
- You can see them later if you go to Reports > Controls Task tabs and search for the current date/time range
<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!