37d3e566dc0da906a4756cc397cdfa861f89aa3f
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 Bell Canada.
3  *  Modifications Copyright © 2018-2019 IBM.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.ansible.executor
19
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
39 import java.net.URI
40 import java.net.URLEncoder
41 import java.util.NoSuchElementException
42
43 /**
44  * ComponentRemoteAnsibleExecutor
45  *
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.
48  *
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
51  *
52  * @author Serge Simard
53  */
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
59 ) :
60     AbstractComponentFunction() {
61
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")
67
68     var checkDelay: Long = 15_000
69
70     companion object {
71         private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutor::class.java)
72
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"
84
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"
90     }
91
92     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
93
94         try {
95             val restClientService = getAWXRestClient()
96
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()
101             if (isWorkflowJT) {
102                 jobTemplateName = getOperationInput(INPUT_WORKFLOW_JOB_TEMPLATE_NAME).asText()
103                 workflowURIPrefix = "workflow_"
104             }
105             var isAnsibleFireFailure = false
106             if (getOptionalOperationInput(ANSIBLE_FIRE_FAILURE) != null) {
107                 isAnsibleFireFailure = getOperationInput(ANSIBLE_FIRE_FAILURE).asBoolean()
108             }
109
110             val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName, workflowURIPrefix)
111             if (jtId.isNotEmpty()) {
112                 runJobTemplateOnAWX(restClientService, jobTemplateName, jtId, workflowURIPrefix, isAnsibleFireFailure)
113             } else {
114                 val message = "Workflow/Job template $jobTemplateName does not exists"
115                 log.error(message)
116                 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
117             }
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})")
121         }
122     }
123
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)
128     }
129
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"
136      *
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"
142      *     }
143      */
144     private fun getAWXRestClient(): BlueprintWebClientService {
145
146         val endpointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
147
148         try {
149             return blueprintRestLibPropertyService.blueprintWebClientService(endpointSelector)
150         } catch (e: NoSuchElementException) {
151             throw IllegalArgumentException("No value provided for input selector $endpointSelector", e)
152         }
153     }
154
155     /**
156      * Finds the job template ID based on the job template name provided in the request
157      */
158     private fun lookupJobTemplateIDByName(
159         awxClient: BlueprintWebClientService,
160         job_template_name: String?,
161         workflowPrefix: String
162     ): String {
163         val encodedJTName = URI(
164             null, null,
165             "/api/v2/${workflowPrefix}job_templates/$job_template_name/",
166             null, null
167         ).rawPath
168
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()
173     }
174
175     /**
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.
180      */
181     private fun runJobTemplateOnAWX(
182         awxClient: BlueprintWebClientService,
183         job_template_name: String?,
184         jtId: String,
185         workflowPrefix: String,
186         isAnsibleFireFailure: Boolean
187     ) {
188         setNodeOutputProperties("preparing".asJsonPrimitive(), "".asJsonPrimitive(), "".asJsonPrimitive())
189
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())
195
196         log.info("Running job with $payload, for requestId $processId.")
197
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.")
206             }
207         }
208
209         if (response.status in HTTP_SUCCESS) {
210             val jobId: String = jtLaunched.at("/id").asText()
211
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)
221             }
222
223             log.info("Execution of job template $job_template_name in job #$jobId finished with status ($jobStatus) for requestId $processId")
224
225             if (isAnsibleFireFailure && jobStatus == ANSIBLE_FAILED_STATUS) {
226                 val message = "Execution of job template $job_template_name failed for requestId $processId." + " (Response: ${response.body}) "
227                 log.error(message)
228                 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
229             } else {
230                 populateJobRunResponse(awxClient, jobId, workflowPrefix, jobStatus)
231             }
232         } else {
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}) "
238             log.error(message)
239             setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
240         }
241     }
242
243     /**
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
246      */
247     private fun populateJobRunResponse(
248         awxClient: BlueprintWebClientService,
249         jobId: String,
250         workflowPrefix: String,
251         jobStatus: String
252     ) {
253
254         val collectedResponses = StringBuilder(4096)
255         val artifacts: MutableMap<String, JsonNode> = mutableMapOf()
256
257         collectJobIdsRelatedToJobRun(awxClient, jobId, workflowPrefix).forEach { aJobId ->
258
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
263                 collectedResponses
264                     .append("Output for Job $aJobId :" + System.lineSeparator())
265                     .append(jobOutput)
266                     .append(System.lineSeparator())
267                 log.info("Response for job $aJobId: \n $jobOutput \n")
268             } else {
269                 log.warn("Could not gather response for job $aJobId. Status=${response.status}")
270             }
271
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())
278                 }
279             }
280         }
281
282         log.info("Artifacts for job $jobId: \n $artifacts \n")
283
284         setNodeOutputProperties(jobStatus.asJsonPrimitive(), collectedResponses.toString().asJsonPrimitive(), artifacts.asJsonNode())
285     }
286
287     /**
288      * List all the job Ids for a give workflow, i.e. sub jobs, or the jobId if not a workflow instance
289      */
290     private fun collectJobIdsRelatedToJobRun(awxClient: BlueprintWebClientService, jobId: String, workflowPrefix: String): Array<String> {
291
292         var jobIds: Array<String>
293
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")
297
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())
303                 }
304             }
305         } else {
306             jobIds = arrayOf(jobId)
307         }
308         return jobIds
309     }
310
311     /**
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
315      */
316     private fun prepareLaunchPayload(
317         awxClient: BlueprintWebClientService,
318         jtLaunchReqs: JsonNode,
319         isWorkflow: Boolean
320     ): String {
321         val payload = JacksonUtils.objectMapper.createObjectNode()
322
323         // Parameter defaults
324         val inventoryProp = getOptionalOperationInput(INPUT_INVENTORY)
325         val extraArgs = getOperationInput(INPUT_EXTRA_VARS)
326
327         if (!isWorkflow) {
328             val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST)
329             val tagsProp = getOptionalOperationInput(INPUT_TAGS)
330             val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS)
331
332             val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean()
333             if (askLimitOnLaunch && !limitProp.isNullOrMissing()) {
334                 payload.set<JsonNode>(INPUT_LIMIT_TO_HOST, limitProp)
335             }
336             val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean()
337             if (askTagsOnLaunch && !tagsProp.isNullOrMissing()) {
338                 payload.set<JsonNode>(INPUT_TAGS, tagsProp)
339             }
340             if (askTagsOnLaunch && !skipTagsProp.isNullOrMissing()) {
341                 payload.set<JsonNode>("skip_tags", skipTagsProp)
342             }
343         }
344
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()
349             } else {
350                 inventoryProp
351             }
352             payload.set<JsonNode>(INPUT_INVENTORY, inventoryKeyId)
353         }
354
355         payload.set<JsonNode>("extra_vars", extraArgs)
356
357         return payload.asJsonString(false)
358     }
359
360     private fun resolveInventoryIdByName(awxClient: BlueprintWebClientService, inventoryProp: String): Int? {
361         var invId: Int? = null
362
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")
373             }
374         }
375
376         if (invId == null) {
377             val message = "Could not resolve inventory $inventoryProp by name..."
378             log.error(message)
379             throw IllegalArgumentException(message)
380         }
381
382         return invId
383     }
384
385     /**
386      * Utility function to set the output properties of the executor node
387      */
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")
395     }
396
397     /**
398      * Utility function to set the output properties and errors of the executor node, in cas of errors
399      */
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)
404
405         addError(status, ATTRIBUTE_EXEC_CMD_LOG, message)
406     }
407 }