diff options
Diffstat (limited to 'lib/cloudify.js')
-rw-r--r-- | lib/cloudify.js | 137 |
1 files changed, 76 insertions, 61 deletions
diff --git a/lib/cloudify.js b/lib/cloudify.js index 23e779a..2db460a 100644 --- a/lib/cloudify.js +++ b/lib/cloudify.js @@ -1,5 +1,5 @@ /* -Copyright(c) 2017 AT&T Intellectual Property. All rights reserved. +Copyright(c) 2017-2018 AT&T Intellectual Property. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ const CLOUDIFY = "cloudify-manager"; const FINISHED = [ "terminated", "cancelled", "failed" ]; const RETRY_INTERVAL = 5000; // Every 5 seconds const MAX_TRIES = 720; // Up to 1 hour - +const DEFAULT_TENANT = "default_tenant"; const doRequest = require('./promise_request').doRequest; const repeat = require('./repeat'); const admzip = require('adm-zip'); @@ -64,7 +64,8 @@ ExeQueue.prototype.nextExecution = function(deployment_id) { } return depl.exe_queue[0]; }; -var exeQueue = new ExeQueue(); +const exeQueue = new ExeQueue(); +exports.exeQueue = exeQueue; // Delay function--returns a promise that's resolved after 'dtime' // milliseconds.` @@ -75,32 +76,32 @@ var delay = function(dtime) { }; // Get current status of a workflow execution -const getExecutionStatus = function(execution_id, mainReq) { - /* Defense: Some callers do not supply mainReq */ - mainReq = mainReq || {}; +const getExecutionStatus = function(req, execution_id) { var reqOptions = { method : "GET", uri : cfyAPI + "/executions/" + execution_id }; - addAuthToOptions(reqOptions); - return doRequest(reqOptions, null, CLOUDIFY, mainReq); + + addAuthToOptions(reqOptions, req); + + return doRequest(req, reqOptions, null, CLOUDIFY); }; // Poll for the result of a workflow execution until it's done -var getWorkflowResult = function(execution_id, mainReq) { +const getWorkflowResult = function(mainReq, execution_id) { /* Defense: Some callers do not supply mainReq */ mainReq = mainReq || {}; - logger.debug(mainReq.dcaeReqId, "Getting workflow result for execution id: " + execution_id); + logger.info(mainReq.dcaeReqId, "Getting workflow result for execution id: " + execution_id); // Function for testing if workflow is finished // Expects the result of getExecStatus var checkStatus = function(res) { - logger.debug(mainReq.dcaeReqId, "Checking result: " + JSON.stringify(res) + " ==> " + (res.json && res.json.status && FINISHED.indexOf(res.json.status) < 0)); + logger.info(mainReq.dcaeReqId, "Checking result: " + JSON.stringify(res) + " ==> " + (res.json && res.json.status && FINISHED.indexOf(res.json.status) < 0)); return res.json && res.json.status && FINISHED.indexOf(res.json.status) < 0; }; // Create execution status checker function - var getExecStatus = function() {return getExecutionStatus(execution_id, mainReq);}; + var getExecStatus = function() {return getExecutionStatus(mainReq, execution_id);}; return repeat.repeatWhile(getExecStatus, checkStatus, MAX_TRIES, RETRY_INTERVAL) .then( @@ -108,7 +109,7 @@ var getWorkflowResult = function(execution_id, mainReq) { /* Handle fulfilled promise from repeatWhile */ function(res) { - logger.debug(mainReq.dcaeReqId, 'workflow result: ' + JSON.stringify(res)); + logger.info(mainReq.dcaeReqId, 'workflow result: ' + JSON.stringify(res)); /* Successful completion */ if (res.json && res.json.status && res.json.status === 'terminated') { @@ -173,7 +174,9 @@ const startWorkflowExecution = function(mainReq, deployment_id, workflow_id, par "Accept" : "*/*" } }; - addAuthToOptions(reqOptions); + + addAuthToOptions(reqOptions, mainReq); + var body = { "deployment_id" : deployment_id, "workflow_id" : workflow_id @@ -181,18 +184,18 @@ const startWorkflowExecution = function(mainReq, deployment_id, workflow_id, par if (parameters) {body.parameters = parameters;} // Make the POST request - return doRequest(reqOptions, JSON.stringify(body), CLOUDIFY, mainReq); + return doRequest(mainReq, reqOptions, JSON.stringify(body), CLOUDIFY); }; //Initiate a workflow execution against a deployment -const initiateWorkflowExecution = function(deployment_id, workflow_id, parameters) { - return startWorkflowExecution(null, deployment_id, workflow_id, parameters) +const initiateWorkflowExecution = function(req, deployment_id, workflow_id, parameters) { + return startWorkflowExecution(req, deployment_id, workflow_id, parameters) .then(function(result) { - logger.debug(null, "Result from POSTing workflow execution start: " + JSON.stringify(result)); + logger.info(req.dcaeReqId, "Result from POSTing workflow execution start: " + JSON.stringify(result)); if (result.json && result.json.id) { return {deploymentId: deployment_id, workflowType: workflow_id, executionId: result.json.id}; } - logger.debug(null,"Did not get expected JSON body from POST to start workflow"); + logger.info(req.dcaeReqId,"Did not get expected JSON body from POST to start workflow"); var err = new Error("POST to start workflow got success response but no body"); err.status = err.code = 502; throw err; @@ -200,31 +203,32 @@ const initiateWorkflowExecution = function(deployment_id, workflow_id, parameter }; // Uploads a blueprint via the Cloudify API -exports.uploadBlueprint = function(bpid, blueprint) { +exports.uploadBlueprint = function(req, bpid, blueprint) { + logger.info(req.dcaeReqId, "uploadBlueprint " + bpid); // Cloudify API wants a gzipped tar of a directory, not the blueprint text - var zip = new admzip(); + const zip = new admzip(); zip.addFile('work/', new Buffer(0)); zip.addFile('work/blueprint.yaml', new Buffer(blueprint, 'utf8')); - var src = (zip.toBuffer()); + const zip_buffer = zip.toBuffer(); // Set up the HTTP PUT request - var reqOptions = { - method : "PUT", - uri : cfyAPI + "/blueprints/" + bpid, - headers : { - "Content-Type" : "application/octet-stream", - "Accept" : "*/*" - } + const reqOptions = { + method : "PUT", + uri : cfyAPI + "/blueprints/" + bpid, + headers : { + "Content-Type" : "application/octet-stream", + "Accept" : "*/*" + } }; - addAuthToOptions(reqOptions); + addAuthToOptions(reqOptions, req); // Initiate PUT request and return the promise for a result - return doRequest(reqOptions, src, CLOUDIFY); + return doRequest(req, reqOptions, zip_buffer, CLOUDIFY); }; // Creates a deployment from a blueprint -exports.createDeployment = function(dpid, bpid, inputs) { +exports.createDeployment = function(req, dpid, bpid, inputs) { // Set up the HTTP PUT request var reqOptions = { @@ -235,7 +239,7 @@ exports.createDeployment = function(dpid, bpid, inputs) { "Accept" : "*/*" } }; - addAuthToOptions(reqOptions); + addAuthToOptions(reqOptions, req); var body = { blueprint_id : bpid @@ -245,7 +249,7 @@ exports.createDeployment = function(dpid, bpid, inputs) { } // Make the PUT request to create the deployment - return doRequest(reqOptions, JSON.stringify(body), CLOUDIFY); + return doRequest(req, reqOptions, JSON.stringify(body), CLOUDIFY); }; // Initiate a workflow execution against a deployment @@ -258,19 +262,19 @@ exports.getWorkflowExecutionStatus = getExecutionStatus; exports.getWorkflowResult = getWorkflowResult; // Executes a workflow against a deployment and returns a promise for final result -exports.executeWorkflow = function(deployment_id, workflow_id, parameters) { - return initiateWorkflowExecution(deployment_id, workflow_id, parameters) +exports.executeWorkflow = function(req, deployment_id, workflow_id, parameters) { + return initiateWorkflowExecution(req, deployment_id, workflow_id, parameters) // Wait for the result .then (function(result) { - logger.debug(null, "Result from initiating workflow: " + JSON.stringify(result)); - return getWorkflowResult(result.executionId); + logger.info(req.dcaeReqId, "Result from initiating workflow: " + JSON.stringify(result)); + return getWorkflowResult(req, result.executionId); }); }; // Retrieves outputs for a deployment -exports.getOutputs = function(dpid) { +exports.getOutputs = function(req, dpid) { var reqOptions = { method : "GET", uri : cfyAPI + "/deployments/" + dpid + "/outputs", @@ -278,13 +282,14 @@ exports.getOutputs = function(dpid) { "Accept" : "*/*" } }; - addAuthToOptions(reqOptions); - return doRequest(reqOptions, null, CLOUDIFY); + addAuthToOptions(reqOptions, req); + + return doRequest(req, reqOptions, null, CLOUDIFY); }; // Get the output descriptions for a deployment -exports.getOutputDescriptions = function(dpid) { +exports.getOutputDescriptions = function(req, dpid) { var reqOptions = { method : "GET", uri : cfyAPI + "/deployments/" + dpid + "?include=outputs", @@ -292,31 +297,34 @@ exports.getOutputDescriptions = function(dpid) { "Accept" : "*/*" } }; - addAuthToOptions(reqOptions); - return doRequest(reqOptions, null, CLOUDIFY); + addAuthToOptions(reqOptions, req); + + return doRequest(req, reqOptions, null, CLOUDIFY); }; // Deletes a deployment -exports.deleteDeployment = function(dpid) { +exports.deleteDeployment = function(req, dpid) { var reqOptions = { method : "DELETE", uri : cfyAPI + "/deployments/" + dpid }; - addAuthToOptions(reqOptions); - return doRequest(reqOptions, null, CLOUDIFY); + addAuthToOptions(reqOptions, req); + + return doRequest(req, reqOptions, null, CLOUDIFY); }; // Deletes a blueprint -exports.deleteBlueprint = function(bpid) { +exports.deleteBlueprint = function(req, bpid) { var reqOptions = { method : "DELETE", uri : cfyAPI + "/blueprints/" + bpid }; - addAuthToOptions(reqOptions); - return doRequest(reqOptions, null, CLOUDIFY); + addAuthToOptions(reqOptions, req); + + return doRequest(req, reqOptions, null, CLOUDIFY); }; // Allow client to set the Cloudify API root address @@ -329,10 +337,16 @@ exports.setCredentials = function(user, password) { cfyAuth = cfyAuth || (user + ':' + password); }; -function addAuthToOptions(reqOptions) { +function addAuthToOptions(reqOptions, req) { + if (!!cfyAuth && cfyAuth !== "undefined:undefined") { reqOptions.auth = cfyAuth; } + reqOptions.headers = reqOptions.headers || {}; + reqOptions.headers.Tenant = req.query.cfy_tenant_name || DEFAULT_TENANT; + + logger.debug(req.dcaeReqId, "Calling " + reqOptions.uri + " with Tenant: " + reqOptions.headers.Tenant ); + } // Set a logger @@ -346,12 +360,13 @@ exports.getNodeInstances = function (mainReq, on_next_node_instances, offset) { method : "GET", uri : cfyAPI + "/node-instances?_include=id,deployment_id,runtime_properties&_offset=" + offset }; - addAuthToOptions(reqOptions); - logger.debug(mainReq.dcaeReqId, "getNodeInstances: " + JSON.stringify(reqOptions)); - return doRequest(reqOptions, null, CLOUDIFY, mainReq) + addAuthToOptions(reqOptions, mainReq); + + logger.info(mainReq.dcaeReqId, "getNodeInstances: " + JSON.stringify(reqOptions)); + return doRequest(mainReq, reqOptions, null, CLOUDIFY) .then(function(cloudify_response) { - logger.debug(mainReq.dcaeReqId, "getNodeInstances response: " + JSON.stringify(cloudify_response)); + logger.info(mainReq.dcaeReqId, "getNodeInstances response: " + JSON.stringify(cloudify_response)); var response = {}; cloudify_response = cloudify_response && cloudify_response.json; if (!cloudify_response || !Array.isArray(cloudify_response.items)) { @@ -364,7 +379,7 @@ exports.getNodeInstances = function (mainReq, on_next_node_instances, offset) { response.message = 'got no more node_instances'; return response; } - logger.debug(mainReq.dcaeReqId, 'getNodeInstances got node_instances ' + cloudify_response.items.length); + logger.info(mainReq.dcaeReqId, 'getNodeInstances got node_instances ' + cloudify_response.items.length); if (typeof on_next_node_instances === 'function') { on_next_node_instances(cloudify_response.items); } @@ -396,20 +411,20 @@ const runQueuedExecution = function(mainReq, deployment_id, workflow_id, paramet + " with params(" + JSON.stringify(parameters || {}) + ")"; startWorkflowExecution(mainReq, deployment_id, workflow_id, parameters) .then(function(result) { - logger.debug(mainReq.dcaeReqId, "result of start the execution for" + exe_deployment_str + ": " + JSON.stringify(result)); + logger.info(mainReq.dcaeReqId, "result of start the execution for" + exe_deployment_str + ": " + JSON.stringify(result)); execution_id = result.json && result.json.id; if (!execution_id) { throw createError("failed to start execution - no execution_id for" + exe_deployment_str, 553, "api", 553, CLOUDIFY); } exeQueue.setExecutionId(deployment_id, execution_id); - return getWorkflowResult(execution_id, mainReq); + return getWorkflowResult(mainReq, execution_id); }) .then(function(result) { - logger.debug(mainReq.dcaeReqId, 'successfully finished execution: ' + execution_id + " for" + exe_deployment_str); + logger.info(mainReq.dcaeReqId, 'successfully finished execution: ' + execution_id + " for" + exe_deployment_str); var nextExecution = exeQueue.nextExecution(deployment_id); if (nextExecution) { - logger.debug(nextExecution.mainReq.dcaeReqId, "next execution for deployment_id " + deployment_id + logger.info(nextExecution.mainReq.dcaeReqId, "next execution for deployment_id " + deployment_id + " to " + nextExecution.workflow_id + " with params(" + JSON.stringify(nextExecution.parameters || {}) + ")"); runQueuedExecution(nextExecution.mainReq, deployment_id, nextExecution.workflow_id, nextExecution.parameters); @@ -455,7 +470,7 @@ exports.executeOperation = function (mainReq, deployment_id, operation, operatio if (exeQueue.isDeploymentBusy(deployment_id)) { exeQueue.queueUpExecution(mainReq, deployment_id, workflow_id, parameters); - logger.debug(mainReq.dcaeReqId, "deployment busy - queue up execution for deployment_id " + deployment_id + logger.info(mainReq.dcaeReqId, "deployment busy - queue up execution for deployment_id " + deployment_id + " to " + workflow_id + " with params(" + JSON.stringify(parameters || {}) + ")"); return; } |