f145d967799d60bacd14afda06bd554ff0a85eae
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 Bell Canada.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.ansible.executor
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import com.fasterxml.jackson.databind.node.ObjectNode
22 import java.net.URI
23 import java.net.URLEncoder
24 import java.util.NoSuchElementException
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
28 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
29 import org.onap.ccsdk.cds.controllerblueprints.core.*
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.slf4j.LoggerFactory
32 import org.springframework.beans.factory.config.ConfigurableBeanFactory
33 import org.springframework.context.annotation.Scope
34 import org.springframework.http.HttpMethod
35 import org.springframework.stereotype.Component
36
37 /**
38  * ComponentRemoteAnsibleExecutor
39  *
40  * Component that launches a run of a job template (INPUT_JOB_TEMPLATE_NAME) representing an Ansible playbook,
41  * and its parameters, via the AWX server identified by the INPUT_ENDPOINT_SELECTOR parameter.
42  *
43  * It supports extra_vars, limit, tags, skip-tags, inventory (by name or Id) Ansible parameters.
44  * It reports the results of the execution via properties, named execute-command-status and execute-command-logs
45  *
46  * @author Serge Simard
47  */
48 @Component("component-remote-ansible-executor")
49 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
50 open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
51     : AbstractComponentFunction() {
52
53     private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutor::class.java)!!
54
55     // HTTP related constants
56     private val HTTP_SUCCESS = 200..202
57     private val GET = HttpMethod.GET.name
58     private val POST = HttpMethod.POST.name
59
60     companion object {
61         // input fields names accepted by this executor
62         const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector"
63         const val INPUT_JOB_TEMPLATE_NAME = "job-template-name"
64         const val INPUT_LIMIT_TO_HOST = "limit"
65         const val INPUT_INVENTORY = "inventory"
66         const val INPUT_EXTRA_VARS = "extra-vars"
67         const val INPUT_TAGS = "tags"
68         const val INPUT_SKIP_TAGS = "skip-tags"
69
70         // output fields names (and values) populated by this executor; aligned with job details status field values.
71         const val ATTRIBUTE_EXEC_CMD_STATUS = "ansible-command-status"
72         const val ATTRIBUTE_EXEC_CMD_LOG = "ansible-command-logs"
73         const val ATTRIBUTE_EXEC_CMD_STATUS_ERROR = "error"
74
75         const val CHECKDELAY: Long = 10000
76     }
77
78     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
79
80         try {
81             val restClientService = getAWXRestClient()
82
83             val jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).asText()
84             val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName)
85             if (jtId.isNotEmpty()) {
86                 runJobTemplateOnAWX(restClientService, jobTemplateName, jtId)
87             } else {
88                 val message = "Job template ${jobTemplateName} does not exists"
89                 log.error(message)
90                 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
91             }
92         } catch (e: Exception) {
93             log.error("Failed to process on remote executor (${e.message})", e)
94             setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, "Failed to process on remote executor (${e.message})")
95         }
96     }
97
98
99     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
100         val message = "Error in ComponentRemoteAnsibleExecutor : ${runtimeException.message}"
101         log.error(message,runtimeException)
102         setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
103     }
104
105     /** Creates a TokenAuthRestClientService, since this executor expect type property to be "token-auth" and the
106      * token to be an OAuth token (access_token response field) generated via the AWX /api/o/token rest endpoint
107      * The token field is of the form "Bearer access_token_from_response", for example :
108      *  "blueprintsprocessor.restclient.awx.type=token-auth"
109      *  "blueprintsprocessor.restclient.awx.url=http://awx-endpoint"
110      *  "blueprintsprocessor.restclient.awx.token=Bearer J9gEtMDzxcqw25574fioY9VAhLDIs1"
111      *
112      * Also supports json endpoint definition via DSL entry, e.g.:
113      *     "ansible-remote-endpoint": {
114      *        "type": "token-auth",
115      *        "url": "http://awx-endpoint",
116      *        "token": "Bearer J9gEtMDzxcqw25574fioY9VAhLDIs1"
117      *     }
118      */
119     private fun getAWXRestClient(): BlueprintWebClientService {
120
121         val endpointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
122
123         try {
124             return blueprintRestLibPropertyService.blueprintWebClientService(endpointSelector)
125         } catch (e : NoSuchElementException) {
126             throw IllegalArgumentException("No value provided for input selector $endpointSelector", e)
127         }
128     }
129
130     /**
131      * Finds the job template ID based on the job template name provided in the request
132      */
133     private fun lookupJobTemplateIDByName(awxClient : BlueprintWebClientService, job_template_name: String?): String {
134         val mapper = ObjectMapper()
135
136         val encodedJTName = URI(null,null,
137                 "/api/v2/job_templates/${job_template_name}/",
138                 null,null).rawPath
139
140         // Get Job Template details by name
141         var response = awxClient.exchangeResource(GET, encodedJTName,"")
142         val jtDetails: JsonNode = mapper.readTree(response.body)
143         return jtDetails.at("/id").asText()
144     }
145
146     /**
147      * Performs the job template execution on AWX, ie. prepare arguments as per job template
148      * requirements (ask fields) and provided overriding values. Then it launches the run, and monitors
149      * its execution. Finally, it retrieves the job results via the stdout api.
150      * The status and output attributes are populated in the process.
151      */
152     private fun runJobTemplateOnAWX(awxClient : BlueprintWebClientService, job_template_name: String?, jtId: String) {
153         val mapper = ObjectMapper()
154
155         setNodeOutputProperties( "preparing".asJsonPrimitive(), "".asJsonPrimitive())
156
157         // Get Job Template requirements
158         var response = awxClient.exchangeResource(GET, "/api/v2/job_templates/${jtId}/launch/","")
159         val jtLaunchReqs: JsonNode = mapper.readTree(response.body)
160         var payload = prepareLaunchPayload(awxClient, jtLaunchReqs)
161         log.info("Running job with $payload, for requestId $processId.")
162
163         // Launch the job for the targeted template
164         var jtLaunched : JsonNode = JacksonUtils.jsonNode("{}") as ObjectNode
165         response = awxClient.exchangeResource(POST, "/api/v2/job_templates/${jtId}/launch/", payload)
166         if (response.status in HTTP_SUCCESS) {
167             jtLaunched = mapper.readTree(response.body)
168             val fieldsIgnored: JsonNode = jtLaunched.at("/ignored_fields")
169             if (fieldsIgnored.rootFieldsToMap().isNotEmpty()) {
170                 log.warn("Ignored fields : $fieldsIgnored, for requestId $processId.")
171             }
172         }
173
174         if (response.status in HTTP_SUCCESS) {
175             val jobId: String = jtLaunched.at("/id").asText()
176
177             // Poll current job status while job is not executed
178             var jobStatus = "unknown"
179             var jobEndTime = "null"
180             while (jobEndTime == "null") {
181                 response = awxClient.exchangeResource(GET, "/api/v2/jobs/${jobId}/", "")
182                 val jobLaunched: JsonNode = mapper.readTree(response.body)
183                 jobStatus = jobLaunched.at("/status").asText()
184                 jobEndTime = jobLaunched.at("/finished").asText()
185                 Thread.sleep(CHECKDELAY)
186             }
187
188             log.info("Execution of job template $job_template_name in job #$jobId finished with status ($jobStatus) for requestId $processId")
189
190             // Get job execution results (stdout)
191             val plainTextHeaders = mutableMapOf<String, String>()
192             plainTextHeaders["Content-Type"] = "text/plain ;utf-8"
193             response = awxClient.exchangeResource(GET, "/api/v2/jobs/${jobId}/stdout/?format=txt","", plainTextHeaders)
194
195             setNodeOutputProperties( jobStatus.asJsonPrimitive(), response.body.asJsonPrimitive())
196         } else {
197             // The job template requirements were not fulfilled with the values passed in. The message below will
198             // provide more information via the response, like the ignored_fields, or variables_needed_to_start,
199             // or resources_needed_to_start, in order to help user pinpoint the problems with the request.
200             val message = "Execution of job template $job_template_name could not be started for requestId $processId." +
201                     " (Response: ${response.body}) "
202             log.error(message)
203             setNodeOutputErrors( ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
204         }
205     }
206
207     /**
208      * Prepares the JSON payload expected by the job template api,
209      * by applying the overrides that were provided
210      * and allowed by the template definition flags in jtLaunchReqs
211      */
212     private fun prepareLaunchPayload(awxClient : BlueprintWebClientService, jtLaunchReqs: JsonNode): String {
213         val payload = JacksonUtils.jsonNode("{}") as ObjectNode
214
215         // Parameter defaults
216         val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST)?.asText()
217         val tagsProp = getOptionalOperationInput(INPUT_TAGS)?.asText()
218         val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS)?.asText()
219         val inventoryProp : String? = getOptionalOperationInput(INPUT_INVENTORY)?.asText()
220         val extraArgs : JsonNode = getOperationInput(INPUT_EXTRA_VARS)
221
222         val askLimitOnLaunch = jtLaunchReqs.at( "/ask_limit_on_launch").asBoolean()
223         if (askLimitOnLaunch && limitProp!!.isNotEmpty()) {
224             payload.put(INPUT_LIMIT_TO_HOST, limitProp)
225         }
226         val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean()
227         if (askTagsOnLaunch && tagsProp!!.isNotEmpty()) {
228             payload.put(INPUT_TAGS, tagsProp)
229         }
230         if (askTagsOnLaunch && skipTagsProp!!.isNotEmpty()) {
231             payload.put("skip_tags", skipTagsProp)
232         }
233         val askInventoryOnLaunch = jtLaunchReqs.at("/ask_inventory_on_launch").asBoolean()
234         if (askInventoryOnLaunch && inventoryProp != null) {
235             var inventoryKeyId = inventoryProp.toIntOrNull()
236             if (inventoryKeyId == null) {
237                 inventoryKeyId = resolveInventoryIdByName(awxClient, inventoryProp)
238             }
239             payload.put(INPUT_INVENTORY, inventoryKeyId)
240         }
241         val askVariablesOnLaunch = jtLaunchReqs.at("/ask_variables_on_launch").asBoolean()
242         if (askVariablesOnLaunch && extraArgs != null) {
243             payload.put("extra_vars", extraArgs)
244         }
245
246         val strPayload = "$payload"
247
248         return strPayload
249     }
250
251     private fun resolveInventoryIdByName(awxClient : BlueprintWebClientService, inventoryProp: String): Int? {
252         var invId : Int? = null
253
254         // Get Inventory by name
255         val encoded = URLEncoder.encode(inventoryProp)
256         val response = awxClient.exchangeResource(GET,"/api/v2/inventories/?name=$encoded","")
257         if (response.status in HTTP_SUCCESS) {
258             val mapper = ObjectMapper()
259
260             // Extract the inventory ID from response
261             val invDetails = mapper.readTree(response.body)
262             val nbInvFound = invDetails.at("/count").asInt()
263             if (nbInvFound == 1) {
264                 invId = invDetails["results"][0]["id"].asInt()
265                 log.info("Resolved inventory $inventoryProp to ID #: $invId")
266             }
267         }
268
269         if (invId == null) {
270             val message = "Could not resolve inventory $inventoryProp by name..."
271             log.error(message)
272             throw IllegalArgumentException(message)
273         }
274
275         return invId
276     }
277
278     /**
279      * Utility function to set the output properties of the executor node
280      */
281     private fun setNodeOutputProperties(status: JsonNode, message: JsonNode) {
282         setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status)
283         log.info("Executor status: $status")
284         setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message)
285         log.info("Executor message: $message")
286     }
287
288     /**
289      * Utility function to set the output properties and errors of the executor node, in cas of errors
290      */
291     private fun setNodeOutputErrors(status: String, message: String) {
292         setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status.asJsonPrimitive())
293         setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message.asJsonPrimitive())
294
295         addError(status, ATTRIBUTE_EXEC_CMD_LOG, message)
296     }
297 }