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.
This blog post caters to users who have a good understanding of Tasks in XL Releases and also know how to code in python. It talks about how you can make the best use of enhanced Jython API that has been available since XL Release 4.6.0 to dynamically generate tasks in a phase inside a release during runtime. Jython API can be used in two different ways in XL Release.
import sys, string, time import com.xhaus.jyson.JysonCodec as json from com.xebialabs.xlrelease.domain import Task from com.xebialabs.deployit.plugin.api.reflect import Type from java.text import SimpleDateFormat def createSimpleTask(phaseId, taskTypeValue, title, propertyMap): parenttaskType = Type.valueOf(taskTypeValue) parentTask = parenttaskType.descriptor.newInstance("nonamerequired") parentTask.setTitle(title) sdf = SimpleDateFormat("yyyy-MM-dd hh:mm:ss") for item in propertyMap: if item.lower().find("date") > -1: if propertyMap[item] is not None and len(propertyMap[item]) != 0: parentTask.setProperty(item,sdf.parse(propertyMap[item])) else: parentTask.setProperty(item,propertyMap[item]) taskApi.addTask(phaseId,parentTask) createSimpleTask(phase.id,"xlrelease.Task", "this is cool", {'description':'coolio'}) createSimpleTask(phase.id,"xlrelease.NotificationTask", "this is the title", {'description':'this is the description'}) createSimpleTask(phase.id,"xlrelease.ScriptTask", "this is the title", {'description':'this is the description'}) createSimpleTask(phase.id,"xlrelease.DeployitTask", "this is the title", {'description':'this is the description','server':'localhost'})Custom Script Tasks with Jython
import sys, string, time import com.xhaus.jyson.JysonCodec as json from com.xebialabs.xlrelease.domain import Task from com.xebialabs.deployit.plugin.api.reflect import Type from java.text import SimpleDateFormat def createScriptBasedTask(phaseId,taskTypeValue,title,precondition, propertyMap): parenttaskType = Type.valueOf("xlrelease.CustomScriptTask") parentTask = parenttaskType.descriptor.newInstance("nonamerequired") parentTask.setTitle(title) childTaskType = Type.valueOf(taskTypeValue) childTask = childTaskType.descriptor.newInstance("nonamerequired") for item in propertyMap: childTask.setProperty(item,propertyMap[item]) parentTask.setPythonScript(childTask) parentTask.setPrecondition(precondition) taskApi.addTask(phaseId,parentTask) createScriptBasedTask(phase.id,"webhook.JsonWebhook", "this is the title",None,{"URL":"http://myurl", "method":"PUT", "body":"{key1:val1,key2:'value is 2'}", "username": "user", "password":"pass"}) createScriptBasedTask(phase.id,"webhook.XmlWebhook", "this is the title",None,{}) createScriptBasedTask(phase.id,"jenkins.Build", "this is the title",None,{})Now the next cool thing would be to encapsulate these into Custom Task wrapper so that we can only expose the input and outputs steps:
<type type="mytype.GenerateDeployments" extends="xlrelease.PythonScript" > <property name="server" label="XL Deploy Server Reference" category="input" /> <property name="targetPhase" label="Target Phase to add Deployments" category="input" /> <property name="deploymentMap" label="Deployment Map" category="input" size="large" /> </type>
import sys, string, time import com.xhaus.jyson.JysonCodec as json from com.xebialabs.xlrelease.domain import Task from com.xebialabs.deployit.plugin.api.reflect import Type from java.text import SimpleDateFormat def createSimpleTask(phaseId, taskTypeValue, title, propertyMap): parenttaskType = Type.valueOf(taskTypeValue) parentTask = parenttaskType.descriptor.newInstance("nonamerequired") parentTask.setTitle(title) sdf = SimpleDateFormat("yyyy-MM-dd hh:mm:ss") for item in propertyMap: if item.lower().find("date") > -1: if propertyMap[item] is not None and len(propertyMap[item]) != 0: parentTask.setProperty(item,sdf.parse(propertyMap[item])) else: parentTask.setProperty(item,propertyMap[item]) taskApi.addTask(phaseId,parentTask) serverId = "Configuration/Deployit/" + str(server) deploymentList = deploymentMap.split(",") phaseList = phaseApi.searchPhasesByTitle(targetPhase,release.id) if len(phaseList) == 1: for item in deploymentList: itemSplit = item.split(":") deploymentPackage = itemSplit[0] environment = itemSplit[1] createSimpleTask(phaseList[0].id,"xlrelease.DeployitTask", "Deployment of %s to %s"%(deploymentPackage,environment), {'description':"Deployment of %s to %s"%(deploymentPackage,environment),'server':serverId,'deploymentPackage':deploymentPackage,'environment':environment})
{ "releaseTitle":"Release123", "releaseVariables":{"${deploymentMap}":"App/1.0:DEV,App/2.0:QA"} }