Skip to content

Commit

Permalink
Added undeploy task
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewinne committed Jan 24, 2016
1 parent 10d364a commit 7b2949f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 30 deletions.
15 changes: 14 additions & 1 deletion src/main/resources/synthetic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@
<property name="failOnPause" category="input" kind="boolean" default="true" />

</type>


<type type="xldeploy.UndeployTask" extends="xldeploy.TaskRunningTask" description="Executes an undeployment in XL Deploy">
<property name="scriptLocation" default="xldeploy/undeployTask.py" hidden="true" />

<property name="deployedApplication" category="input" label="Deployed Application" required="true" description="Name of the deployed application you want to undeploy (Only the name, without Environments, etc...)" />
<property name="environment" category="input" label="Environment" default="" required="true" description="Environment you want to undeploy from (fully qualified starting with Environments/...)" />

<property name="orchestrators" category="input" label="Orchestrators" default="" required="false" />
<property name="deployedApplicationProperties" category="input" default="" required="false" description="A dictionary with key value pairs" />
<property name="rollbackOnError" category="input" kind="boolean" />
<property name="failOnPause" category="input" kind="boolean" default="true" />

</type>

<type type="xldeploy.ControlTask" extends="xldeploy.TaskRunningTask" description="Invokes a control task on an XL Deploy Configuration Item">
<property name="scriptLocation" default="xldeploy/controlTask.py" hidden="true" />

Expand Down
60 changes: 38 additions & 22 deletions src/main/resources/xldeploy/XLDeployClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,25 @@ def invoke_task_and_wait_for_result(self, task_id, polling_interval = 10, number
break
time.sleep(polling_interval)
return status

def deploymentExists(self, deploymentPackage, environment):
deploymentExistsUrl = "/deployit/deployment/exists?application=%s&environment=%s" % (deploymentPackage.rsplit('/',1)[0],environment)
# print 'DEBUG: checking deployment exists with url %s \n' % deploymentExistsUrl
deploymentExists_response = self.http_request.get(deploymentExistsUrl, contentType='application/xml')
response = deploymentExists_response.getResponse()

def get_deployment_package(self, deployed_application_id):
ci = self.get_ci(deployed_application_id,'json')
data = json.loads(ci)
return data['version']


def deployment_exists(self, deployment_package, environment):
deployment_exists_url = "/deployit/deployment/exists?application=%s&environment=%s" % (deployment_package.rsplit('/',1)[0],environment)
# print 'DEBUG: checking deployment exists with url %s \n' % deployment_exists_url
deployment_exists_response = self.http_request.get(deployment_exists_url, contentType='application/xml')
response = deployment_exists_response.getResponse()
return 'true' in response


def deployment_prepare_undeploy(self, deployed_application_id):
deployment_prepare_undeploy_url = "/deployit/deployment/prepare/undeploy?deployedApplication=%s" % (deployed_application_id)
deployment_prepare_undeploy_url_response = self.http_request.get(deployment_prepare_undeploy_url, contentType='application/xml')
return deployment_prepare_undeploy_url_response.getResponse()

def deploymentPrepareUpdate(self, deploymentPackage, environment):
deploymentPrepareUpdateUrl = "/deployit/deployment/prepare/update?version=%s&deployedApplication=%s" % (deploymentPackage, "%s/%s" % (environment, deploymentPackage.rsplit('/',2)[1]))
deploymentPrepareUpdate_response = self.http_request.get(deploymentPrepareUpdateUrl, contentType='application/xml')
Expand All @@ -121,16 +132,19 @@ def deploymentPrepareInitial(self, deploymentPackage, environment):
deploymentPrepareInitial_response = self.http_request.get(deploymentPrepareInitialUrl, contentType='application/xml')
return deploymentPrepareInitial_response.getResponse()

def add_orchestrators(self, root, orchestrators):
def add_orchestrators(self, deployment_xml, orchestrators):
root = ET.fromstring(deployment_xml)
if orchestrators:
params = root.find(".//orchestrator")
params.clear()
orchs = orchestrators.split(",")
for orch in orchs:
orchestrator = ET.SubElement(params, 'value')
orchestrator.text = orch.strip()
return ET.tostring(root)

def set_deployed_application_properties(self, root, deployed_application_properties):
def set_deployed_application_properties(self, deployment_xml, deployed_application_properties):
root = ET.fromstring(deployment_xml)
if deployed_application_properties:
deployeds_application_properties_dict = dict(ast.literal_eval(deployed_application_properties))
# print 'DEBUG: deployed application properties dict is %s \n' % deployeds_application_properties_dict
Expand All @@ -145,9 +159,11 @@ def set_deployed_application_properties(self, root, deployed_application_propert
# print "DEBUG: Searching for deployed application: %s" % child
pkey_xml = ET.SubElement(child, key)
pkey_xml.text = deployeds_application_properties_dict[key]
return ET.tostring(root)


def set_deployed_properties(self, root, deployed_properties):
def set_deployed_properties(self, deployment_xml, deployed_properties):
root = ET.fromstring(deployment_xml)
if deployed_properties:
deployeds_properties_dict = dict(ast.literal_eval(deployed_properties))
for key in deployeds_properties_dict:
Expand All @@ -163,6 +179,7 @@ def set_deployed_properties(self, root, deployed_properties):
if not pkey_xml:
pkey_xml = ET.SubElement(xlr_tag_deployed, pkey)
pkey_xml.text = deployed_properties_dict[pkey]
return ET.tostring(root)


def deployment_prepare_deployeds(self, deployment, orchestrators = None, deployed_application_properties = None, deployed_properties = None):
Expand All @@ -172,12 +189,11 @@ def deployment_prepare_deployeds(self, deployment, orchestrators = None, deploye
# print 'DEBUG: Deployment object including mapping is now %s \n' % deployment
deployment_xml = deployment_prepare_deployeds_response.getResponse()
# print 'DEBUG: deployment_xml is ' + deployment_xml
root = ET.fromstring(deployment_xml)
self.add_orchestrators(root, orchestrators)
self.set_deployed_application_properties(root, deployed_application_properties)
deployment_xml = self.add_orchestrators(deployment_xml, orchestrators)
deployment_xml = self.set_deployed_application_properties(deployment_xml, deployed_application_properties)
# print 'DEBUG: Deployment object after updating orchestrators: %s \n' % ET.tostring(root)
self.set_deployed_properties(root, deployed_properties)
return ET.tostring(root)
deployment_xml = self.set_deployed_properties(deployment_xml, deployed_properties)
return deployment_xml

def get_deployment_task_id(self, deployment):
getDeploymentTaskId = "/deployit/deployment"
Expand All @@ -186,22 +202,22 @@ def get_deployment_task_id(self, deployment):
# print 'DEBUG: getDeploymentTaskId response is %s \n' % (deploymentTaskId_response.getResponse())
return deploymentTaskId_response.getResponse()

def deploymentRollback(self, taskId):
def deployment_rollback(self, taskId):
deploymentRollback = "/deployit/deployment/rollback/%s" % taskId
# print 'DEBUG: calling rollback for taskId %s \n' % taskId
deploymentRollback_response = self.http_request.post(deploymentRollback,'',contentType='application/xml')
# print 'DEBUG: received rollback taskId %s \n' % deploymentRollback_response.getResponse()
return deploymentRollback_response.getResponse()

def archiveTask(self, taskId):
archiveTask = "/deployit/task/%s/archive" % taskId
self.http_request.post(archiveTask,'',contentType='application/xml')
def archive_task(self, task_id):
archive_task = "/deployit/task/%s/archive" % task_id
self.http_request.post(archive_task,'',contentType='application/xml')

def cancelTask(self, taskId):
def cancel_task(self, taskId):
cancelTask = "/deployit/task/%s" % taskId
self.http_request.delete(cancelTask, contentType='application/xml')

def stopTask(self, taskId):
def stop_task(self, taskId):
stopTask = "/deployit/task/%s/stop" % taskId
self.http_request.post(stopTask,'',contentType='application/xml')

Expand All @@ -214,7 +230,7 @@ def fetch_package(self, fetchURL):
fetchTask = "/deployit/package/fetch"
self.http_request.post(fetchTask, fetchURL, contentType='application/xml')

def get_latest_package_version(self, application_id, old_implementation = False):
def get_latest_package_version(self, application_id):
query_task = "/deployit/repository/query?parent=%s&resultsPerPage=-1" % application_id
query_task_response = self.http_request.get(query_task, contentType='application/xml')
root = ET.fromstring(query_task_response.getResponse())
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/xldeploy/controlTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
print 'DEBUG: About to invoke task and wait for response', task_id, '\n'
task_state = xldClient.invoke_task_and_wait_for_result(task_id, pollingInterval, numberOfPollingTrials, continueIfStepFails, numberOfContinueRetrials)
print 'DEBUG: Task state for', task_id, ':', task_state, '\n'
xldClient.archiveTask(task_id)
xldClient.archive_task(task_id)
if task_state in ('DONE','EXECUTED'):
sys.exit(0)
sys.exit(1)
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/xldeploy/deployTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
xldClient = XLDeployClientUtil.createXLDeployClient(xldeployServer, username, password)

deployment = None
if xldClient.deploymentExists(deploymentPackage, environment):
if xldClient.deployment_exists(deploymentPackage, environment):
print "Upgrading deployment \n"
deployment = xldClient.deploymentPrepareUpdate(deploymentPackage,environment)
else:
Expand All @@ -33,18 +33,18 @@

if taskState in ('DONE','EXECUTED'):
print "Deployment ended in %s \n" % taskState
xldClient.archiveTask(taskId)
xldClient.archive_task(taskId)
sys.exit(0)

# rollbackOnError
if rollbackOnError and taskState in ('FAILED', 'STOPPED'):
print "Going to rollback \n"
xldClient.stopTask(taskId)
rollBackTaskId = xldClient.deploymentRollback(taskId)
xldClient.stop_task(taskId)
rollBackTaskId = xldClient.deployment_rollback(taskId)
taskState = xldClient.invoke_task_and_wait_for_result(rollBackTaskId, pollingInterval, numberOfPollingTrials, continueIfStepFails, numberOfContinueRetrials)
xldClient.archiveTask(rollBackTaskId)
xldClient.archive_task(rollBackTaskId)
sys.exit(1)
elif taskState in ('FAILED', 'STOPPED'):
print "Task failed, rollback not enabled. \n"
xldClient.cancelTask(taskId)
xldClient.cancel_task(taskId)
sys.exit(1)
52 changes: 52 additions & 0 deletions src/main/resources/xldeploy/undeployTask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS.
#

import sys
from xldeploy.XLDeployClientUtil import XLDeployClientUtil

xld_client = XLDeployClientUtil.createXLDeployClient(xldeployServer, username, password)

deployment = None
deployment_package = xld_client.get_deployment_package("%s/%s" % (environment, deployedApplication))
if xld_client.deployment_exists(deployment_package, environment):
print "Undeploying [%s] from environment [%s] \n" % (deployment_package, environment)
deployment = xld_client.deployment_prepare_undeploy("%s/%s" % (environment, deployedApplication))
else:
print "No deployed application found [%s] for environment [%s] \n" % (deployedApplication, environment)
sys.exit(1)

# Mapping deployables to the target environment
print "Set orchestrators \n"
deployment = xld_client.add_orchestrators(deployment, orchestrators)

print "Set Deployed Application Properties \n"
deployment = xld_client.set_deployed_application_properties(deployment, deployedApplicationProperties)

print "Creating a deployment task \n"
task_id = xld_client.get_deployment_task_id(deployment)

print "Execute task with id: %s" % task_id
task_state = xld_client.invoke_task_and_wait_for_result(task_id, pollingInterval, numberOfPollingTrials, continueIfStepFails, numberOfContinueRetrials, failOnPause)

xld_client.display_step_logs(task_id)

if task_state in ('DONE','EXECUTED'):
print "Deployment ended in %s \n" % task_state
xld_client.archive_task(task_id)
sys.exit(0)

# rollbackOnError
if rollbackOnError and task_state in ('FAILED', 'STOPPED'):
print "Going to rollback \n"
xld_client.stop_task(task_id)
rollback_task_id = xld_client.deployment_rollback(task_id)
task_state = xld_client.invoke_task_and_wait_for_result(rollback_task_id, pollingInterval, numberOfPollingTrials, continueIfStepFails, numberOfContinueRetrials)
xld_client.archive_task(rollback_task_id)
sys.exit(1)
elif task_state in ('FAILED', 'STOPPED'):
print "Task failed, rollback not enabled. \n"
xld_client.cancel_task(task_id)
sys.exit(1)

0 comments on commit 7b2949f

Please sign in to comment.