Reckoning DevOps’ role in the enterprise value stream
DevOps
If you’re a software or digital solutions company, you may use DevOps ...
Read MoreThis post is from the XebiaLabs blog and has not been updated since the original publish date.
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.
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 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<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:
<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:
<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.