Skip to content

Commit

Permalink
Move to exposing as module. Fix #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Sep 23, 2015
1 parent 6d4254a commit 86ff8d8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
66 changes: 14 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,21 @@ var downloadDump = require('./src/downloadDump');
var unzipAndExtract = require('./src/unzipAndExtract');
var logger = require('./src/logger');

var shuttingDown, id;
var config = require('./config');
config.ec2 = ec2;
config.timeout = 60000;
module.exports = function(callback) {
logger('Initializing');
async.waterfall([
startInstance.bind(null, config),
findPublicIp,
connectToInstance,
makeDownloadDirectory,
downloadDump,
unzipAndExtract
], function(err, id, ip) {
logger('done!');
callback(err, id, ip);
});
}

async.waterfall([
startInstance.bind(null, config),
findPublicIp,
connectToInstance,
makeDownloadDirectory,
downloadDump,
unzipAndExtract
], function(err, ider, ip) {
id = ider;
console.log(err, id, ip);
logger('done!');
});

process.on('SIGINT', function() {
logger('Will try to shut down servers.');
if (!shuttingDown) {
ec2.terminateInstances({
InstanceIds: [
id
]
}, function(err) {
if (err) {
logger('Problem shutting down instance', 'error');
throw err;
}
else {
logger('Instance shut down');
}
});
}
else {
logger('Multiple SIGINT detected. Will hard shutdown in 10 seconds.', 'error');
setTimeout(function() {
throw new Error('Force quitting after waiting to quit.');
}, 10000);
}
shuttingDown = true;

});

http.createServer(function(req, res) {
// @todo. Do something sensible here.
res.end(JSON.stringify({
id: id
}));
}).listen(9999, function(err) {
if (err) {
throw err;
}
logger('Started webserver on port 9999');
});
49 changes: 49 additions & 0 deletions starter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
var http = require('http');
var AWS = require('aws-sdk');
// @todo. Expose as config.
AWS.config.update({region: 'us-east-1'});
var ec2 = new AWS.EC2({apiVersion: '2015-04-15'});
var shuttingDown, id;
var logger = require('./src/logger');
require('./index')(function(err, ider, ip) {
id = ider;
http.createServer(function(req, res) {
// @todo. Do something sensible here.
res.end(JSON.stringify({
id: id
}));
}).listen(9999, function(err) {
if (err) {
throw err;
}
logger('Started webserver on port 9999');
});
});

process.on('SIGINT', function() {
logger('Will try to shut down servers.');
if (!shuttingDown) {
ec2.terminateInstances({
InstanceIds: [
id
]
}, function(err) {
if (err) {
logger('Problem shutting down instance', 'error');
throw err;
}
else {
logger('Instance shut down');
}
});
}
else {
logger('Multiple SIGINT detected. Will hard shutdown in 10 seconds.', 'error');
setTimeout(function() {
throw new Error('Force quitting after waiting to quit.');
}, 10000);
}
shuttingDown = true;

});

0 comments on commit 86ff8d8

Please sign in to comment.