Last active
July 2, 2019 08:55
-
-
Save bmoussaud/4959291 to your computer and use it in GitHub Desktop.
XLDeploy: Import package and deploy it to an environment (include retry & rollback)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import traceback | |
def importDAR(darFile): | |
print "import dar file: ", darfile | |
return deployit.importPackage(darfile).id | |
def validate(d): | |
validations = 0 | |
print "...validations" | |
d=deployment.validate(d) | |
for deployed in d.deployeds: | |
for v in deployed.validations: | |
print "...Validation Error '%s' on property '%s' : %s " % (v.ciId,v.propertyName, v.message) | |
validations = validations + 1 | |
if validations > 0: | |
raise Exception("Validation (%s) errors found" % (validations)) | |
return d | |
def generateDeployementTask(versionId, envId): | |
if not repository.exists(envId): | |
raise Exception("Environment '%s' does not exist" % envId) | |
if not repository.exists(versionId): | |
raise Exception("Version '%s' does not exist" % versionId) | |
applicationId = repository.read(versionId).application | |
applicationName = repository.read(applicationId).name | |
deployedApplicationId='%s/%s' % (envId,applicationName) | |
if repository.exists(deployedApplicationId): | |
print "...update %s -> %s" % (versionId, deployedApplicationId) | |
d=deployment.prepareUpgrade(versionId, deployedApplicationId) | |
else: | |
print "...initial %s -> %s" % (versionId, envId) | |
d=deployment.prepareInitial(versionId, envId) | |
d=deployment.prepareAutoDeployeds(d) | |
d=validate(d) | |
return deployment.createDeployTask(d) | |
def printSteps(taskId, verbose=False): | |
ti = tasks.steps(taskId) | |
print 'State: ',ti.state, ti.currentStepNr,'/',ti.nrSteps | |
for i,stepinfo in enumerate(ti.steps): | |
print ti.id,'#',(i+1),'\t',stepinfo.state,'\t',stepinfo.description | |
if verbose: | |
print stepinfo.log | |
return ti | |
def deploy(versionId, envId): | |
print 'version is %s' % versionId | |
print 'environment is %s' % envId | |
taskInfo = generateDeployementTask(versionId,envId) | |
printSteps(taskInfo.id, False) | |
print "Starting deployment '%s' (ID: '%s')" % (taskInfo.label, taskInfo.id) | |
deployit.startTaskAndWait(taskInfo.id) | |
taskInfo = printSteps(taskInfo.id, True) | |
if taskInfo.state.final: | |
print "Done" | |
else: | |
raise Exception("WARN: Deployment '%s' was not completed successfully. " % (taskInfo.id)) | |
def executeDeploymentTask(taskInfo, nbRetryOnError): | |
printSteps(taskInfo.id, False) | |
print "Execute Deployment '%s' (ID: '%s')" % (taskInfo.description, taskInfo.id) | |
nbtry = 0 | |
while 1: | |
deployit.startTaskAndWait(taskInfo.id) | |
taskInfo = printSteps(taskInfo.id, True) | |
if taskInfo.state.final: | |
print "Done" | |
# startTaskAndWait archive the task. | |
# print "Archive the task %s " % (taskInfo.id) | |
# tasks.archive(taskInfo.id) | |
break | |
nbtry = nbtry + 1 | |
if nbtry < nbRetryOnError: | |
print "Retry Deployment '%s' (ID: '%s')" % (taskInfo.description, taskInfo.id) | |
else: | |
print "Too Many Retries....abort" | |
raise | |
def deploy(versionId, envId, nbRetryOnError, rollbackOnError): | |
print ' version is %s' % versionId | |
print ' environment is %s' % envId | |
print ' # retry is %s' % nbRetryOnError | |
print ' rollback is %s' % rollbackOnError | |
taskInfo = generateDeployementTask(versionId,envId) | |
try: | |
executeDeploymentTask(taskInfo, nbRetryOnError) | |
except : | |
if rollbackOnError: | |
print "Rollback task %s" % (taskInfo.id) | |
rollbackTask = deployment.createRollbackTask(taskInfo.id) | |
try: | |
executeDeploymentTask(rollbackTask, nbRetryOnError) | |
sys.exit(50) | |
except: | |
print "SEVERE: Error in rollback task !" | |
sys.exit(100) | |
else: | |
print "Don't Rollback task %s" % (taskInfo.id) | |
sys.exit(10) | |
#deploy('Applications/Java/PetPortal/2.0-57','Environments/Dev/Tomcat-Dev', 3, True) |
@bmoussaud: when I am running deploy command. I am getting below error.
javax.ws.rs.ProcessingException: com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : java.lang.NullPointerException
cause-message : Name is null
class : com.xebialabs.deployit.engine.api.execution.SerializableTask
required-type : com.xebialabs.deployit.engine.api.execution.SerializableTask
converter-type : com.xebialabs.deployit.booter.remote.xml.TaskConverterSelector
path : /task
line number : 1
version : not available
@vrathore18 this script uses an old API to interact with the tasks. the script should be updated to use task2. object.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create the deploy function.
This script can be put on $DEPLOYIT_CLI_HOME/ext