Skip to content

Commit

Permalink
NOP 'Delete' operation during 'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS' p…
Browse files Browse the repository at this point in the history
…hase.
  • Loading branch information
mweagle committed Nov 19, 2015
1 parent c25d06e commit 684b48e
Showing 1 changed file with 65 additions and 17 deletions.
82 changes: 65 additions & 17 deletions resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var SPARTA_BINARY_NAME = 'Sparta.lambda.amd64';
var SPARTA_BINARY_PATH = path.join('/tmp', SPARTA_BINARY_NAME);
var MAXIMUM_RESPAWN_COUNT = 5;

var PROXIED_MODULES = ['s3', 'sns', 'apigateway']
var PROXIED_MODULES = ['s3', 'sns', 'apigateway'];

var golangProcess = null;
var failCount = 0;
Expand Down Expand Up @@ -151,31 +151,79 @@ var createForwarder = function(path) {
return forwardToGolangProcess;
};

var sendResponse = function(event, context, e, results)
{
try {
var response = require('cfn-response');
var data = {
ERROR: e ? e.toString() : undefined,
RESULTS: results || undefined
};
response.send(event, context, e ? response.FAILED : response.SUCCESS, data);
}
catch (eResponse) {
// NOP
console.log('ERROR sending response: ' + eResponse.toString());
}
};

// CustomResource Configuration exports
PROXIED_MODULES.forEach(function (eachConfig) {
var exportName = util.format('%sConfiguration', eachConfig);
exports[exportName] = function(event, context)
{
try {
console.log('Delegating to configurator: ' + eachConfig);
var svc = require(util.format('./%s', eachConfig))
svc.handler(event, context);
} catch (e) {
console.error('Failed to load configurator:' + e.toString());
var AWS = require('aws-sdk');
var awsConfig = new AWS.Config({});

// If the stack is in update mode, don't delegate
var proxyTasks = [];
proxyTasks.push(function (taskCB) {
var params = {
StackName: event.StackId
};
var awsConfig = new AWS.Config({});
awsConfig.logger = console;
var cloudFormation = new AWS.CloudFormation(awsConfig);
cloudFormation.describeStacks(params, taskCB);
});

try {
var response = require('cfn-response');
var data = {
Error: e.toString()
// Only delegate to the stack if the update is in progress.
var onStackDescription = function(e, stackDescriptionResponse) {
if (e)
{
sendResponse(event, context, e, null);
}
else {
var stackDescription = stackDescriptionResponse.Stacks ? stackDescriptionResponse.Stacks[0] : {};
var stackStatus = stackDescription.StackStatus || "";
if (stackStatus !== "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS")
{
try {
console.log('Delegating to configurator: ' + eachConfig);
var svc = require(util.format('./%s', eachConfig));
svc.handler(event, context);
} catch (e) {
sendResponse(event, context, e, null);
}
} else {
console.log('Bypassing configurator execution due to status: ' + stackStatus);
sendResponse(event, context, e, "NOP");
}
}
response.send(event, context, response.FAILED, data);
}
catch (loader) {
// NOP
}
};
// Get the current stack status
var params = {
StackName: event.StackId
};
var cloudFormation = new AWS.CloudFormation(awsConfig);
cloudFormation.describeStacks(params, onStackDescription);
} catch (e) {
console.error('Failed to load configurator:' + e.toString());
sendResponse(event, context, e, null);
}
}
})
};
});

exports.main = createForwarder('/');
// Additional golang handlers to be dynamically appended below

0 comments on commit 684b48e

Please sign in to comment.