2 * Copyright © 2019 Bell Canada.
3 * Modifications Copyright © 2018-2019 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.ansible.executor
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.ObjectMapper
22 import com.fasterxml.jackson.databind.node.TextNode
23 import kotlinx.coroutines.delay
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintRestLibPropertyService
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
29 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
30 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
31 import org.onap.ccsdk.cds.controllerblueprints.core.isNullOrMissing
32 import org.onap.ccsdk.cds.controllerblueprints.core.returnNullIfMissing
33 import org.onap.ccsdk.cds.controllerblueprints.core.rootFieldsToMap
34 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
35 import org.slf4j.LoggerFactory
36 import org.springframework.beans.factory.config.ConfigurableBeanFactory
37 import org.springframework.context.annotation.Scope
38 import org.springframework.http.HttpMethod
39 import org.springframework.stereotype.Component
41 import java.net.URLEncoder
42 import java.util.NoSuchElementException
45 * ComponentRemoteAnsibleExecutor
47 * Component that launches a run of a job template (INPUT_JOB_TEMPLATE_NAME) representing an Ansible playbook,
48 * and its parameters, via the AWX server identified by the INPUT_ENDPOINT_SELECTOR parameter.
50 * It supports extra_vars, limit, tags, skip-tags, inventory (by name or Id) Ansible parameters.
51 * It reports the results of the execution via properties, named execute-command-status and execute-command-logs
53 * @author Serge Simard
55 @Component("component-remote-ansible-executor")
56 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
57 open class ComponentRemoteAnsibleExecutor(
58 private val blueprintRestLibPropertyService: BlueprintRestLibPropertyService,
59 private val mapper: ObjectMapper
61 AbstractComponentFunction() {
63 // HTTP related constants
64 private val HTTP_SUCCESS = 200..202
65 private val GET = HttpMethod.GET.name
66 private val POST = HttpMethod.POST.name
67 private val plainTextHeaders = mapOf("Accept" to "text/plain")
69 var checkDelay: Long = 15_000
73 private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutor::class.java)
75 // input fields names accepted by this executor
76 const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector"
77 const val INPUT_JOB_TEMPLATE_NAME = "job-template-name"
78 const val ANSIBLE_FIRE_FAILURE = "ansible-fire-failure"
79 const val ANSIBLE_FAILED_STATUS = "failed"
80 const val INPUT_WORKFLOW_JOB_TEMPLATE_NAME = "workflow-job-template-id"
81 const val INPUT_LIMIT_TO_HOST = "limit"
82 const val INPUT_INVENTORY = "inventory"
83 const val INPUT_EXTRA_VARS = "extra-vars"
84 const val INPUT_TAGS = "tags"
85 const val INPUT_SKIP_TAGS = "skip-tags"
87 // output fields names (and values) populated by this executor; aligned with job details status field values.
88 const val ATTRIBUTE_EXEC_CMD_ARTIFACTS = "ansible-artifacts"
89 const val ATTRIBUTE_EXEC_CMD_STATUS = "ansible-command-status"
90 const val ATTRIBUTE_EXEC_CMD_LOG = "ansible-command-logs"
91 const val ATTRIBUTE_EXEC_CMD_STATUS_ERROR = "error"
94 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
97 val restClientService = getAWXRestClient()
99 // Get either a job template name or a workflow template name property
100 var workflowURIPrefix = ""
101 var jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).returnNullIfMissing()?.textValue() ?: ""
102 val isWorkflowJT = jobTemplateName.isBlank()
104 jobTemplateName = getOperationInput(INPUT_WORKFLOW_JOB_TEMPLATE_NAME).asText()
105 workflowURIPrefix = "workflow_"
107 var isAnsibleFireFailure = false
108 if (getOptionalOperationInput(ANSIBLE_FIRE_FAILURE) != null) {
109 isAnsibleFireFailure = getOperationInput(ANSIBLE_FIRE_FAILURE).asBoolean()
112 val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName, workflowURIPrefix)
113 if (jtId.isNotEmpty()) {
114 runJobTemplateOnAWX(restClientService, jobTemplateName, jtId, workflowURIPrefix, isAnsibleFireFailure)
116 val message = "Workflow/Job template $jobTemplateName does not exists"
118 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
120 } catch (e: Exception) {
121 log.error("Failed to process on remote executor (${e.message})", e)
122 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, "Failed to process on remote executor (${e.message})")
126 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
127 val message = "Error in ComponentRemoteAnsibleExecutor : ${runtimeException.message}"
128 log.error(message, runtimeException)
129 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
132 /** Creates a TokenAuthRestClientService, since this executor expect type property to be "token-auth" and the
133 * token to be an OAuth token (access_token response field) generated via the AWX /api/o/token rest endpoint
134 * The token field is of the form "Bearer access_token_from_response", for example :
135 * "blueprintsprocessor.restclient.awx.type=token-auth"
136 * "blueprintsprocessor.restclient.awx.url=http://awx-endpoint"
137 * "blueprintsprocessor.restclient.awx.token=Bearer J9gEtMDzxcqw25574fioY9VAhLDIs1"
139 * Also supports json endpoint definition via DSL entry, e.g.:
140 * "ansible-remote-endpoint": {
141 * "type": "token-auth",
142 * "url": "http://awx-endpoint",
143 * "token": "Bearer J9gEtMDzxcqw25574fioY9VAhLDIs1"
146 private fun getAWXRestClient(): BlueprintWebClientService {
148 val endpointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
151 return blueprintRestLibPropertyService.blueprintWebClientService(endpointSelector)
152 } catch (e: NoSuchElementException) {
153 throw IllegalArgumentException("No value provided for input selector $endpointSelector", e)
158 * Finds the job template ID based on the job template name provided in the request
160 private fun lookupJobTemplateIDByName(
161 awxClient: BlueprintWebClientService,
162 job_template_name: String?,
163 workflowPrefix: String
165 val encodedJTName = URI(
167 "/api/v2/${workflowPrefix}job_templates/$job_template_name/",
171 // Get Job Template details by name
172 var response = awxClient.exchangeResource(GET, encodedJTName, "")
173 val jtDetails: JsonNode = mapper.readTree(response.body)
174 return jtDetails.at("/id").asText()
178 * Performs the job template execution on AWX, ie. prepare arguments as per job template
179 * requirements (ask fields) and provided overriding values. Then it launches the run, and monitors
180 * its execution. Finally, it retrieves the job results via the stdout api.
181 * The status and output attributes are populated in the process.
183 private suspend fun runJobTemplateOnAWX(
184 awxClient: BlueprintWebClientService,
185 job_template_name: String?,
187 workflowPrefix: String,
188 isAnsibleFireFailure: Boolean
190 setNodeOutputProperties("preparing".asJsonPrimitive(), "".asJsonPrimitive(), "".asJsonPrimitive())
192 // Get Job Template requirements
193 var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}job_templates/$jtId/launch/", "")
194 // FIXME: handle non-successful SC
195 val jtLaunchReqs: JsonNode = mapper.readTree(response.body)
196 val payload = prepareLaunchPayload(awxClient, jtLaunchReqs, workflowPrefix.isNotBlank())
198 log.info("Running job with $payload, for requestId $processId.")
200 // Launch the job for the targeted template
201 var jtLaunched: JsonNode = JacksonUtils.objectMapper.createObjectNode()
202 response = awxClient.exchangeResource(POST, "/api/v2/${workflowPrefix}job_templates/$jtId/launch/", payload)
203 if (response.status in HTTP_SUCCESS) {
204 jtLaunched = mapper.readTree(response.body)
205 val fieldsIgnored: JsonNode = jtLaunched.at("/ignored_fields")
206 if (fieldsIgnored.rootFieldsToMap().isNotEmpty()) {
207 log.warn("Ignored fields : $fieldsIgnored, for requestId $processId.")
211 if (response.status in HTTP_SUCCESS) {
212 val jobId: String = jtLaunched.at("/id").asText()
214 // Poll current job status while job is not executed
215 var jobStatus = "unknown"
216 var jobEndTime = "null"
217 while (jobEndTime == "null") {
218 response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/$jobId/", "")
219 val jobLaunched: JsonNode = mapper.readTree(response.body)
220 jobStatus = jobLaunched.at("/status").asText()
221 jobEndTime = jobLaunched.at("/finished").asText()
225 log.info("Execution of job template $job_template_name in job #$jobId finished with status ($jobStatus) for requestId $processId")
227 if (isAnsibleFireFailure && jobStatus == ANSIBLE_FAILED_STATUS) {
228 val message = "Execution of job template $job_template_name failed for requestId $processId." + " (Response: ${response.body}) "
230 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
233 populateJobRunResponse(awxClient, jobId, workflowPrefix, jobStatus)
235 // The job template requirements were not fulfilled with the values passed in. The message below will
236 // provide more information via the response, like the ignored_fields, or variables_needed_to_start,
237 // or resources_needed_to_start, in order to help user pinpoint the problems with the request.
238 val message = "Execution of job template $job_template_name could not be started for requestId $processId." +
239 " (Response: ${response.body}) "
241 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
246 * Extracts the output from either a job stdout call OR collects the workflow run output, as well as the artifacts
247 * and populate the component corresponding output properties
249 private fun populateJobRunResponse(
250 awxClient: BlueprintWebClientService,
252 workflowPrefix: String,
256 val collectedResponses = StringBuilder(4096)
257 val artifacts: MutableMap<String, JsonNode> = mutableMapOf()
259 collectJobIdsRelatedToJobRun(awxClient, jobId, workflowPrefix).forEach { aJobId ->
261 // Collect the response text from the corresponding jobIds
262 var response = awxClient.exchangeResource(GET, "/api/v2/jobs/$aJobId/stdout/?format=txt", "", plainTextHeaders)
263 if (response.status in HTTP_SUCCESS) {
264 val jobOutput = response.body
266 .append("Output for Job $aJobId :" + System.lineSeparator())
268 .append(System.lineSeparator())
269 log.info("Response for job $aJobId: \n $jobOutput \n")
271 log.warn("Could not gather response for job $aJobId. Status=${response.status}")
274 // Collect artifacts variables from each job and gather them up in one json node
275 response = awxClient.exchangeResource(GET, "/api/v2/jobs/$aJobId/", "")
276 if (response.status in HTTP_SUCCESS) {
277 val jobArtifacts = mapper.readTree(response.body).at("/artifacts")
278 if (jobArtifacts != null) {
279 artifacts.putAll(jobArtifacts.rootFieldsToMap())
284 log.info("Artifacts for job $jobId: \n $artifacts \n")
286 setNodeOutputProperties(jobStatus.asJsonPrimitive(), collectedResponses.toString().asJsonPrimitive(), artifacts.asJsonNode())
290 * List all the job Ids for a give workflow, i.e. sub jobs, or the jobId if not a workflow instance
292 private fun collectJobIdsRelatedToJobRun(awxClient: BlueprintWebClientService, jobId: String, workflowPrefix: String): Array<String> {
294 var jobIds: Array<String>
296 if (workflowPrefix.isNotEmpty()) {
297 var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/$jobId/workflow_nodes/", "")
298 val jobDetails = mapper.readTree(response.body).at("/results")
300 // gather up job Id of all actual job nodes that ran during the workflow
301 jobIds = emptyArray()
302 for (jobDetail in jobDetails.elements()) {
303 if (jobDetail.at("/do_not_run").asText() == "false") {
304 jobIds = jobIds.plus(jobDetail.at("/summary_fields/job/id").asText())
308 jobIds = arrayOf(jobId)
314 * Prepares the JSON payload expected by the job template api,
315 * by applying the overrides that were provided
316 * and allowed by the template definition flags in jtLaunchReqs
318 private fun prepareLaunchPayload(
319 awxClient: BlueprintWebClientService,
320 jtLaunchReqs: JsonNode,
323 val payload = JacksonUtils.objectMapper.createObjectNode()
325 // Parameter defaults
326 val inventoryProp = getOptionalOperationInput(INPUT_INVENTORY)
327 val extraArgs = getOperationInput(INPUT_EXTRA_VARS)
330 val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST)
331 val tagsProp = getOptionalOperationInput(INPUT_TAGS)
332 val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS)
334 val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean()
335 if (askLimitOnLaunch && !limitProp.isNullOrMissing()) {
336 payload.set<JsonNode>(INPUT_LIMIT_TO_HOST, limitProp)
338 val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean()
339 if (askTagsOnLaunch && !tagsProp.isNullOrMissing()) {
340 payload.set<JsonNode>(INPUT_TAGS, tagsProp)
342 if (askTagsOnLaunch && !skipTagsProp.isNullOrMissing()) {
343 payload.set<JsonNode>("skip_tags", skipTagsProp)
347 val askInventoryOnLaunch = jtLaunchReqs.at("/ask_inventory_on_launch").asBoolean()
348 if (askInventoryOnLaunch && !inventoryProp.isNullOrMissing()) {
349 var inventoryKeyId = if (inventoryProp is TextNode) {
350 resolveInventoryIdByName(awxClient, inventoryProp.textValue())?.asJsonPrimitive()
354 payload.set<JsonNode>(INPUT_INVENTORY, inventoryKeyId)
357 payload.set<JsonNode>("extra_vars", extraArgs)
359 return payload.asJsonString(false)
362 private fun resolveInventoryIdByName(awxClient: BlueprintWebClientService, inventoryProp: String): Int? {
363 var invId: Int? = null
365 // Get Inventory by name
366 val encoded = URLEncoder.encode(inventoryProp)
367 val response = awxClient.exchangeResource(GET, "/api/v2/inventories/?name=$encoded", "")
368 if (response.status in HTTP_SUCCESS) {
369 // Extract the inventory ID from response
370 val invDetails = mapper.readTree(response.body)
371 val nbInvFound = invDetails.at("/count").asInt()
372 if (nbInvFound == 1) {
373 invId = invDetails["results"][0]["id"].asInt()
374 log.info("Resolved inventory $inventoryProp to ID #: $invId")
379 val message = "Could not resolve inventory $inventoryProp by name..."
381 throw IllegalArgumentException(message)
388 * Utility function to set the output properties of the executor node
390 private fun setNodeOutputProperties(status: JsonNode, message: JsonNode, artifacts: JsonNode) {
391 setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status)
392 log.info("Executor status : $status")
393 setAttribute(ATTRIBUTE_EXEC_CMD_ARTIFACTS, artifacts)
394 log.info("Executor artifacts: $artifacts")
395 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message)
396 log.info("Executor message : $message")
400 * Utility function to set the output properties and errors of the executor node, in cas of errors
402 private fun setNodeOutputErrors(status: String, message: String, artifacts: JsonNode = "".asJsonPrimitive()) {
403 setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status.asJsonPrimitive())
404 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message.asJsonPrimitive())
405 setAttribute(ATTRIBUTE_EXEC_CMD_ARTIFACTS, artifacts)
407 addError(status, ATTRIBUTE_EXEC_CMD_LOG, message)