2 * Copyright © 2019 Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.ansible.executor
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import com.fasterxml.jackson.databind.node.ObjectNode
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
38 * ComponentRemoteAnsibleExecutor
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.
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
46 * @author Serge Simard
48 @Component("component-remote-ansible-executor")
49 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
50 open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
51 : AbstractComponentFunction() {
53 private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutor::class.java)!!
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
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"
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"
75 const val CHECKDELAY: Long = 10000
78 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
81 val restClientService = getAWXRestClient()
83 val jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).asText()
84 val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName)
85 if (jtId.isNotEmpty()) {
86 runJobTemplateOnAWX(restClientService, jobTemplateName, jtId)
88 val message = "Job template ${jobTemplateName} does not exists"
90 setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
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})")
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)
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"
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"
119 private fun getAWXRestClient(): BlueprintWebClientService {
121 val endpointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
124 return blueprintRestLibPropertyService.blueprintWebClientService(endpointSelector)
125 } catch (e : NoSuchElementException) {
126 throw IllegalArgumentException("No value provided for input selector $endpointSelector", e)
131 * Finds the job template ID based on the job template name provided in the request
133 private fun lookupJobTemplateIDByName(awxClient : BlueprintWebClientService, job_template_name: String?): String {
134 val mapper = ObjectMapper()
136 val encodedJTName = URI(null,null,
137 "/api/v2/job_templates/${job_template_name}/",
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()
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.
152 private fun runJobTemplateOnAWX(awxClient : BlueprintWebClientService, job_template_name: String?, jtId: String) {
153 val mapper = ObjectMapper()
155 setNodeOutputProperties( "preparing".asJsonPrimitive(), "".asJsonPrimitive())
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.")
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.")
174 if (response.status in HTTP_SUCCESS) {
175 val jobId: String = jtLaunched.at("/id").asText()
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)
188 log.info("Execution of job template $job_template_name in job #$jobId finished with status ($jobStatus) for requestId $processId")
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)
195 setNodeOutputProperties( jobStatus.asJsonPrimitive(), response.body.asJsonPrimitive())
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}) "
203 setNodeOutputErrors( ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message)
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
212 private fun prepareLaunchPayload(awxClient : BlueprintWebClientService, jtLaunchReqs: JsonNode): String {
213 val payload = JacksonUtils.jsonNode("{}") as ObjectNode
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)
222 val askLimitOnLaunch = jtLaunchReqs.at( "/ask_limit_on_launch").asBoolean()
223 if (askLimitOnLaunch && limitProp!!.isNotEmpty()) {
224 payload.put(INPUT_LIMIT_TO_HOST, limitProp)
226 val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean()
227 if (askTagsOnLaunch && tagsProp!!.isNotEmpty()) {
228 payload.put(INPUT_TAGS, tagsProp)
230 if (askTagsOnLaunch && skipTagsProp!!.isNotEmpty()) {
231 payload.put("skip_tags", skipTagsProp)
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)
239 payload.put(INPUT_INVENTORY, inventoryKeyId)
241 val askVariablesOnLaunch = jtLaunchReqs.at("/ask_variables_on_launch").asBoolean()
242 if (askVariablesOnLaunch && extraArgs != null) {
243 payload.put("extra_vars", extraArgs)
246 val strPayload = "$payload"
251 private fun resolveInventoryIdByName(awxClient : BlueprintWebClientService, inventoryProp: String): Int? {
252 var invId : Int? = null
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()
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")
270 val message = "Could not resolve inventory $inventoryProp by name..."
272 throw IllegalArgumentException(message)
279 * Utility function to set the output properties of the executor node
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")
289 * Utility function to set the output properties and errors of the executor node, in cas of errors
291 private fun setNodeOutputErrors(status: String, message: String) {
292 setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status.asJsonPrimitive())
293 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message.asJsonPrimitive())
295 addError(status, ATTRIBUTE_EXEC_CMD_LOG, message)