Skip to content

Commit

Permalink
Merge pull request #21 from davinerd/master
Browse files Browse the repository at this point in the history
  • Loading branch information
hassy authored Mar 26, 2018
2 parents f981e5e + 294ceec commit 24d23ef
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Chaosfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
{
"key": "chaos_monkey",
"value": "true"
}
],
"slackWebhook": [
{
"channel": "#random",
"webhookId": "XXXXXXXXX"
}
]
}
87 changes: 84 additions & 3 deletions lambda/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// provided under the terms of either of those licenses.

var AWS = require('aws-sdk');
var https = require('https');
var util = require('util');
var llamaConfig = require('./config.json');

AWS.config.region = llamaConfig.region || 'eu-west-1';
Expand Down Expand Up @@ -29,6 +31,7 @@ ec2.describeInstances(function(err, data) {
}

var candidates = [];
var asgNames = [];
data.Reservations.forEach(function(res) {
res.Instances.forEach(function(inst) {
inst.Tags.forEach(function(tag) {
Expand All @@ -38,11 +41,13 @@ ec2.describeInstances(function(err, data) {
// this takes precedence - if defined we don't even look at disableForASGs
if (llamaConfig.enableForASGs.indexOf(tag.Value) !== -1) {
candidates.push(inst);
asgNames.push(tag.Value);
}
} else {
if (llamaConfig.disableForASGs) {
if (llamaConfig.disableForASGs.indexOf(tag.Value) === -1) {
candidates.push(inst);
asgNames.push(tag.Value);
}
}
}
Expand All @@ -69,9 +74,63 @@ ec2.describeInstances(function(err, data) {

var random = Math.floor(Math.random() * numInstances);
var target = candidates[random];
var asgName = asgNames[random];

console.log('Going to terminate instance with id = %s', target.InstanceId);

if (llamaConfig.slackWebhook) {
llamaConfig.slackWebhook.forEach(function(slack) {
if ( (slack.channel === null || typeof slack.channel === 'undefined') || (slack.webhookId === null || typeof slack.webhookId === 'undefined')) {
console.log('No channel or webhook specified. Slack message not sent.');
return context.done(null);
}

if (slack.username === null || typeof slack.username === 'undefined') {
slack.username = "Chaos Lambda";
}

// code taken from https://gist.github.com/stack72/ad97da2df376754e413a
var slackMessage = [
"*Event*: CHAOS_TERMINATION - ShutdownInstance",
"*Description*: Going to terminate instance with id " + target.InstanceId + " (ASG *" + asgName + "*)",
"*Cause*: At " + getTimestamp() + " Chaos Lambda struk again!",
].join("\n");

var postData = {
channel: slack.channel,
username: slack.username,
text: "*Chaos Lambda Termination Notification*",
attachments: [{ text: slackMessage, mrkdwn_in: ["text"] }]
};

var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: '/services/' + slack.webhookId
};

var req = https.request(options, function(res) {
res.setEncoding('utf8');
if (res.statusCode !== "200") {
console.log('HTTP status code: %s', res.statusCode);
console.log('HTTP headers: %s', res.headers);
}
res.on('data', function (chunk) {
context.done(null);
});
});

req.on('error', function(e) {
context.fail(e);
console.log('request error: ' + e.message);
});

req.write(util.format("%j", postData));
req.end();
});
}

ec2.terminateInstances({InstanceIds:[target.InstanceId]}, function(err, data) {
if (err) {
return context.done(err, null);
Expand All @@ -83,7 +142,29 @@ ec2.describeInstances(function(err, data) {
});
};

function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
function randomIntFromInterval(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}

function getTimestamp() {
var date = new Date();

var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;

var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;

var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;

var year = date.getFullYear();

var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;

var day = date.getDate();
day = (day < 10 ? "0" : "") + day;

return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
}

0 comments on commit 24d23ef

Please sign in to comment.