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 org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
26 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
27 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
29 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
30 import org.onap.ccsdk.cds.controllerblueprints.core.isNullOrMissing
31 import org.onap.ccsdk.cds.controllerblueprints.core.returnNullIfMissing
32 import org.onap.ccsdk.cds.controllerblueprints.core.rootFieldsToMap
33 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
34 import org.slf4j.LoggerFactory
35 import org.springframework.beans.factory.config.ConfigurableBeanFactory
36 import org.springframework.context.annotation.Scope
37 import org.springframework.http.HttpMethod
38 import org.springframework.stereotype.Component
40 import java.net.URLEncoder
41 import java.util.NoSuchElementException
44 * ComponentRemoteAnsibleExecutor
46 * Component that launches a run of a job template (INPUT_JOB_TEMPLATE_NAME) representing an Ansible playbook,
47 * and its parameters, via the AWX server identified by the INPUT_ENDPOINT_SELECTOR parameter.
49 * It supports extra_vars, limit, tags, skip-tags, inventory (by name or Id) Ansible parameters.
50 * It reports the results of the execution via properties, named execute-command-status and execute-command-logs
52 * @author Serge Simard
54 @Component("component-remote-ansible-executor")
55 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
56 open class ComponentRemoteAnsibleExecutor(
57 private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService,
58 private val mapper: ObjectMapper
60 AbstractComponentFunction() {
62 // HTTP related constants
63 private val HTTP_SUCCESS = 200..202
64 private val GET = HttpMethod.GET.name
65 private val POST = HttpMethod.POST.name
66 private val plainTextHeaders = mapOf("Accept" to "text/plain")
68 var checkDelay: Long = 15_000
71 private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutor::class.java)
73 // input fields names accepted by this executor
74 const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector"
75 const val INPUT_JOB_TEMPLATE_NAME = "job-template-name"
76 const val ANSIBLE_FIRE_FAILURE = "ansible-fire-failure"
77 const val ANSIBLE_FAILED_STATUS = "failed"
78 const val INPUT_WORKFLOW_JOB_TEMPLATE_NAME = "workflow-job-template-id"
79 const val INPUT_LIMIT_TO_HOST = "limit"
80 const val INPUT_INVENTORY = "inventory"
81 const val INPUT_EXTRA_VARS = "extra-vars"
82 const val INPUT_TAGS = "tags"
83 const val INPUT_SKIP_TAGS = "skip-tags"
85 // output fields names (and values) populated by this executor; aligned with job details status field values.
86 const val ATTRIBUTE_EXEC_CMD_ARTIFACTS = "ansible-artifacts"
87 const val ATTRIBUTE_EXEC_CMD_STATUS = "ansible-command-status"
88 const val ATTRIBUTE_EXEC_CMD_LOG = "ansible-command-logs"
89 const val ATTRIBUTE_EXEC_CMD_STATUS_ERROR = "error"
92 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
95 val restClientService = getAWXRestClient()
97 // Get either a job template name or a workflow template name property
98 var workflowURIPrefix = ""
99 var jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).returnNullIfMissing()?.textValue() ?: ""
100 val isWorkflowJT = jobTemplateName.isBlank()
102 jobTemplateName = getOperationInput(INPUT_WORKFLOW_JOB_TEMPLATE_NAME).asText()
103 workflowURIPrefix = "workflow_"
105 var isAnsibleFireFailure = false
106 if (getOptionalOperationInput(ANSIBLE_FIRE_FAILURE) != null) {
107 isAnsibleFireFailure = getOperationInput(ANSIBLE_FIRE_FAILURE).asBoolean()
110 val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName, workflowURIPrefix)
111 if (jtId.isNotEmpty()) {
112 runJobTemplateOnAWX(restClientService, jobTemplateName, jtId, workflowURIPrefix, isAnsibleFireFailure)
114 val message = "Workflow/Job template $jobTemplateName does not exists"
116 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
118 } catch (e: Exception) {
119 log.error("Failed to process on remote executor (${e.message})", e)
120 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, "Failed to process on remote executor (${e.message})")
124 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
125 val message = "Error in ComponentRemoteAnsibleExecutor : ${runtimeException.message}"
126 log.error(message, runtimeException)
127 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
130 /** Creates a TokenAuthRestClientService, since this executor expect type property to be "token-auth" and the
131 * token to be an OAuth token (access_token response field) generated via the AWX /api/o/token rest endpoint
132 * The token field is of the form "Bearer access_token_from_response", for example :
133 * "blueprintsprocessor.restclient.awx.type=token-auth"
134 * "blueprintsprocessor.restclient.awx.url=http://awx-endpoint"
135 * "blueprintsprocessor.restclient.awx.token=Bearer J9gEtMDzxcqw25574fioY9VAhLDIs1"
137 * Also supports json endpoint definition via DSL entry, e.g.:
138 * "ansible-remote-endpoint": {
139 * "type": "token-auth",
140 * "url": "http://awx-endpoint",
141 * "token": "Bearer J9gEtMDzxcqw25574fioY9VAhLDIs1"
144 private fun getAWXRestClient(): BlueprintWebClientService {
146 val endpointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
149 return blueprintRestLibPropertyService.blueprintWebClientService(endpointSelector)
150 } catch (e: NoSuchElementException) {
151 throw IllegalArgumentException("No value provided for input selector $endpointSelector", e)
156 * Finds the job template ID based on the job template name provided in the request
158 private fun lookupJobTemplateIDByName(
159 awxClient: BlueprintWebClientService,
160 job_template_name: String?,
161 workflowPrefix: String
163 val encodedJTName = URI(
165 "/api/v2/${workflowPrefix}job_templates/$job_template_name/",
169 // Get Job Template details by name
170 var response = awxClient.exchangeResource(GET, encodedJTName, "")
171 val jtDetails: JsonNode = mapper.readTree(response.body)
172 return jtDetails.at("/id").asText()
176 * Performs the job template execution on AWX, ie. prepare arguments as per job template
177 * requirements (ask fields) and provided overriding values. Then it launches the run, and monitors
178 * its execution. Finally, it retrieves the job results via the stdout api.
179 * The status and output attributes are populated in the process.
181 private fun runJobTemplateOnAWX(
182 awxClient: BlueprintWebClientService,
183 job_template_name: String?,
185 workflowPrefix: String,
186 isAnsibleFireFailure: Boolean
188 setNodeOutputProperties("preparing".asJsonPrimitive(), "".asJsonPrimitive(), "".asJsonPrimitive())
190 // Get Job Template requirements
191 var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}job_templates/$jtId/launch/", "")
192 // FIXME: handle non-successful SC
193 val jtLaunchReqs: JsonNode = mapper.readTree(response.body)
194 val payload = prepareLaunchPayload(awxClient, jtLaunchReqs, workflowPrefix.isNotBlank())
196 log.info("Running job with $payload, for requestId $processId.")
198 // Launch the job for the targeted template
199 var jtLaunched: JsonNode = JacksonUtils.objectMapper.createObjectNode()
200 response = awxClient.exchangeResource(POST, "/api/v2/${workflowPrefix}job_templates/$jtId/launch/", payload)
201 if (response.status in HTTP_SUCCESS) {
202 jtLaunched = mapper.readTree(response.body)
203 val fieldsIgnored: JsonNode = jtLaunched.at("/ignored_fields")
204 if (fieldsIgnored.rootFieldsToMap().isNotEmpty()) {
205 log.warn("Ignored fields : $fieldsIgnored, for requestId $processId.")
209 if (response.status in HTTP_SUCCESS) {
210 val jobId: String = jtLaunched.at("/id").asText()
212 // Poll current job status while job is not executed
213 var jobStatus = "unknown"
214 var jobEndTime = "null"
215 while (jobEndTime == "null") {
216 response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/$jobId/", "")
217 val jobLaunched: JsonNode = mapper.readTree(response.body)
218 jobStatus = jobLaunched.at("/status").asText()
219 jobEndTime = jobLaunched.at("/finished").asText()
220 Thread.sleep(checkDelay)
223 log.info("Execution of job template $job_template_name in job #$jobId finished with status ($jobStatus) for requestId $processId")
225 if (isAnsibleFireFailure && jobStatus == ANSIBLE_FAILED_STATUS) {
226 val message = "Execution of job template $job_template_name failed for requestId $processId." + " (Response: ${response.body}) "
228 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
230 populateJobRunResponse(awxClient, jobId, workflowPrefix, jobStatus)
233 // The job template requirements were not fulfilled with the values passed in. The message below will
234 // provide more information via the response, like the ignored_fields, or variables_needed_to_start,
235 // or resources_needed_to_start, in order to help user pinpoint the problems with the request.
236 val message = "Execution of job template $job_template_name could not be started for requestId $processId." +
237 " (Response: ${response.body}) "
239 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
244 * Extracts the output from either a job stdout call OR collects the workflow run output, as well as the artifacts
245 * and populate the component corresponding output properties
247 private fun populateJobRunResponse(
248 awxClient: BlueprintWebClientService,
250 workflowPrefix: String,
254 val collectedResponses = StringBuilder(4096)
255 val artifacts: MutableMap<String, JsonNode> = mutableMapOf()
257 collectJobIdsRelatedToJobRun(awxClient, jobId, workflowPrefix).forEach { aJobId ->
259 // Collect the response text from the corresponding jobIds
260 var response = awxClient.exchangeResource(GET, "/api/v2/jobs/$aJobId/stdout/?format=txt", "", plainTextHeaders)
261 if (response.status in HTTP_SUCCESS) {
262 val jobOutput = response.body
264 .append("Output for Job $aJobId :" + System.lineSeparator())
266 .append(System.lineSeparator())
267 log.info("Response for job $aJobId: \n $jobOutput \n")
269 log.warn("Could not gather response for job $aJobId. Status=${response.status}")
272 // Collect artifacts variables from each job and gather them up in one json node
273 response = awxClient.exchangeResource(GET, "/api/v2/jobs/$aJobId/", "")
274 if (response.status in HTTP_SUCCESS) {
275 val jobArtifacts = mapper.readTree(response.body).at("/artifacts")
276 if (jobArtifacts != null) {
277 artifacts.putAll(jobArtifacts.rootFieldsToMap())
282 log.info("Artifacts for job $jobId: \n $artifacts \n")
284 setNodeOutputProperties(jobStatus.asJsonPrimitive(), collectedResponses.toString().asJsonPrimitive(), artifacts.asJsonNode())
288 * List all the job Ids for a give workflow, i.e. sub jobs, or the jobId if not a workflow instance
290 private fun collectJobIdsRelatedToJobRun(awxClient: BlueprintWebClientService, jobId: String, workflowPrefix: String): Array<String> {
292 var jobIds: Array<String>
294 if (workflowPrefix.isNotEmpty()) {
295 var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/$jobId/workflow_nodes/", "")
296 val jobDetails = mapper.readTree(response.body).at("/results")
298 // gather up job Id of all actual job nodes that ran during the workflow
299 jobIds = emptyArray()
300 for (jobDetail in jobDetails.elements()) {
301 if (jobDetail.at("/do_not_run").asText() == "false") {
302 jobIds = jobIds.plus(jobDetail.at("/summary_fields/job/id").asText())
306 jobIds = arrayOf(jobId)
312 * Prepares the JSON payload expected by the job template api,
313 * by applying the overrides that were provided
314 * and allowed by the template definition flags in jtLaunchReqs
316 private fun prepareLaunchPayload(
317 awxClient: BlueprintWebClientService,
318 jtLaunchReqs: JsonNode,
321 val payload = JacksonUtils.objectMapper.createObjectNode()
323 // Parameter defaults
324 val inventoryProp = getOptionalOperationInput(INPUT_INVENTORY)
325 val extraArgs = getOperationInput(INPUT_EXTRA_VARS)
328 val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST)
329 val tagsProp = getOptionalOperationInput(INPUT_TAGS)
330 val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS)
332 val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean()
333 if (askLimitOnLaunch && !limitProp.isNullOrMissing()) {
334 payload.set<JsonNode>(INPUT_LIMIT_TO_HOST, limitProp)
336 val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean()
337 if (askTagsOnLaunch && !tagsProp.isNullOrMissing()) {
338 payload.set<JsonNode>(INPUT_TAGS, tagsProp)
340 if (askTagsOnLaunch && !skipTagsProp.isNullOrMissing()) {
341 payload.set<JsonNode>("skip_tags", skipTagsProp)
345 val askInventoryOnLaunch = jtLaunchReqs.at("/ask_inventory_on_launch").asBoolean()
346 if (askInventoryOnLaunch && !inventoryProp.isNullOrMissing()) {
347 var inventoryKeyId = if (inventoryProp is TextNode) {
348 resolveInventoryIdByName(awxClient, inventoryProp.textValue())?.asJsonPrimitive()
352 payload.set<JsonNode>(INPUT_INVENTORY, inventoryKeyId)
355 payload.set<JsonNode>("extra_vars", extraArgs)
357 return payload.asJsonString(false)
360 private fun resolveInventoryIdByName(awxClient: BlueprintWebClientService, inventoryProp: String): Int? {
361 var invId: Int? = null
363 // Get Inventory by name
364 val encoded = URLEncoder.encode(inventoryProp)
365 val response = awxClient.exchangeResource(GET, "/api/v2/inventories/?name=$encoded", "")
366 if (response.status in HTTP_SUCCESS) {
367 // Extract the inventory ID from response
368 val invDetails = mapper.readTree(response.body)
369 val nbInvFound = invDetails.at("/count").asInt()
370 if (nbInvFound == 1) {
371 invId = invDetails["results"][0]["id"].asInt()
372 log.info("Resolved inventory $inventoryProp to ID #: $invId")
377 val message = "Could not resolve inventory $inventoryProp by name..."
379 throw IllegalArgumentException(message)
386 * Utility function to set the output properties of the executor node
388 private fun setNodeOutputProperties(status: JsonNode, message: JsonNode, artifacts: JsonNode) {
389 setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status)
390 log.info("Executor status : $status")
391 setAttribute(ATTRIBUTE_EXEC_CMD_ARTIFACTS, artifacts)
392 log.info("Executor artifacts: $artifacts")
393 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message)
394 log.info("Executor message : $message")
398 * Utility function to set the output properties and errors of the executor node, in cas of errors
400 private fun setNodeOutputErrors(status: String, message: String, artifacts: JsonNode = "".asJsonPrimitive()) {
401 setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status.asJsonPrimitive())
402 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message.asJsonPrimitive())
403 setAttribute(ATTRIBUTE_EXEC_CMD_ARTIFACTS, artifacts)
405 addError(status, ATTRIBUTE_EXEC_CMD_LOG, message)