From 7cdc15f44b27be71fd905fd72aab7207388b36ca Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Mon, 4 Mar 2019 19:13:49 +0100
Subject: [PATCH] Allow to launch a workflow job

New resource argument can be passed to launch_job_template.
It is optional and is set to "job" by default.
Shall be set to "workflow_job" to run a workflow template.

Note that passing the workflow name didn't work during my test.
The id had to be used instead.

JIRA INFRA-867 #action In Progress
---
 app/settings.py |  3 +++
 app/tasks.py    | 13 ++++++++++---
 app/utils.py    |  7 ++++++-
 3 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/app/settings.py b/app/settings.py
index 0587eb3..4e567ed 100644
--- a/app/settings.py
+++ b/app/settings.py
@@ -91,6 +91,9 @@ CSENTRY_ENVIRONMENT = "staging"
 AWX_URL = "https://torn.tn.esss.lu.se"
 # AWX job templates
 AWX_CORE_SERVICES_UPDATE = "ics-ans-core @ DHCP test"
+# Shall be set to job or workflow_job
+# Assumed to be job if the variable is not defined
+AWX_CORE_SERVICES_UPDATE_RESOURCE = "job"
 AWX_CREATE_VM = "deploy-vm-in-proxmox"
 AWX_CREATE_VIOC = "ics-ans-deploy-vioc"
 AWX_ZTP_CONFIGURATION = "ics-ans-ztp"
diff --git a/app/tasks.py b/app/tasks.py
index d260ac3..c861cf4 100644
--- a/app/tasks.py
+++ b/app/tasks.py
@@ -160,7 +160,14 @@ def trigger_ztp_configuration(host):
     return task
 
 
-def launch_job_template(job_template, **kwargs):
+def launch_job_template(job_template, resource="job", **kwargs):
+    """Launch an AWX job or workflow job
+
+    :param job_template: name or id of the job template
+    :param resource: job|workflow_job
+    :param **kwargs: keyword arguments passed to launch the job
+    :returns: A dictionary with information from resource.monitor
+    """
     rq_job = get_current_job()
     if job_template in (
         current_app.config["AWX_CREATE_VIOC"],
@@ -172,8 +179,8 @@ def launch_job_template(job_template, **kwargs):
         current_app.logger.info("AWX job is disabled. Not sending any request.")
         return "AWX job not triggered"
     # Launch the AWX job
-    resource = tower_cli.get_resource("job")
-    result = resource.launch(job_template=job_template, **kwargs)
+    resource = tower_cli.get_resource(resource)
+    result = resource.launch(job_template, **kwargs)
     # Save the AWX job id in the task
     task = models.Task.query.get(rq_job.id)
     task.awx_job_id = result["id"]
diff --git a/app/utils.py b/app/utils.py
index b05c883..17b2d5e 100644
--- a/app/utils.py
+++ b/app/utils.py
@@ -240,12 +240,17 @@ def trigger_core_services_update():
     Make sure that we don't have more than one in queue.
     """
     job_template = current_app.config["AWX_CORE_SERVICES_UPDATE"]
+    resource = current_app.config.get("AWX_CORE_SERVICES_UPDATE_RESOURCE", "job")
     if current_user.is_task_waiting("trigger_core_services_update"):
         current_app.logger.info(
             'Already one "trigger_core_services_update" task waiting. No need to trigger a new one.'
         )
         return None
-    kwargs = {"func": "launch_job_template", "job_template": job_template}
+    kwargs = {
+        "func": "launch_job_template",
+        "job_template": job_template,
+        "resource": resource,
+    }
     started = current_user.get_task_started("trigger_core_services_update")
     if started:
         # There is already one running task. Trigger a new one when it's done.
-- 
GitLab