Merge branch 'master' of git://github.com/shane-tomlinson/awsbox

This commit is contained in:
Lloyd Hilaiel 2012-10-31 12:54:17 -06:00
Родитель 5f0e37b7b5 dcecd02548
Коммит 302637dc24
5 изменённых файлов: 169 добавлений и 28 удалений

Просмотреть файл

@ -3,8 +3,8 @@
process.title = 'awsbox';
const
aws = require('./lib/aws.js');
path = require('path');
aws = require('./lib/aws.js'),
path = require('path'),
vm = require('./lib/vm.js'),
key = require('./lib/key.js'),
ssh = require('./lib/ssh.js'),
@ -12,6 +12,8 @@ dns = require('./lib/dns.js'),
git = require('./lib/git.js'),
optimist = require('optimist'),
urlparse = require('urlparse'),
hooks = require('./lib/hooks'),
config = require('./lib/config'),
fs = require('fs'),
relativeDate = require('relative-date');
@ -42,6 +44,18 @@ function validateName(name) {
}
}
function copySSLCertIfAvailable(opts, deets, cb) {
if (opts.p && opts.s) {
console.log(" ... copying up SSL cert");
ssh.copySSL(deets.ipAddress, opts.p, opts.s, function(err) {
checkErr(err);
cb && cb(null, null);
});
} else {
cb && cb(null, null);
}
}
verbs['destroy'] = function(args) {
if (!args || args.length != 1) {
throw 'missing required argument: name of instance';
@ -179,11 +193,7 @@ verbs['create'] = function(args) {
console.log("reading .awsbox.json");
try {
var awsboxJson = JSON.parse(fs.readFileSync("./.awsbox.json"));
} catch(e) {
checkErr("Can't read awsbox.json: " + e);
}
var awsboxJson = config.get();
console.log("attempting to set up VM \"" + name + "\"");
@ -257,22 +267,15 @@ verbs['create'] = function(args) {
}
ssh.installPackages(deets.ipAddress, awsboxJson.packages, function(err, r) {
checkErr(err);
var postcreate = (awsboxJson.hooks && awsboxJson.hooks.postcreate) || null;
if (postcreate) {
console.log(" ... running post_create hook");
}
ssh.runScript(deets.ipAddress, postcreate, function(err, r) {
hooks.runRemoteHook('postcreate', deets, function(err, r) {
checkErr(err);
if (opts.p && opts.s) {
console.log(" ... copying up SSL cert");
ssh.copySSL(deets.ipAddress, opts.p, opts.s, function(err) {
checkErr(err);
copySSLCertIfAvailable(opts, deets, function(err, status) {
checkErr(err);
hooks.runLocalHook('postcreate', deets, function(err) {
printInstructions(name, dnsHost, opts.u, deets);
});
} else {
printInstructions(name, dnsHost, opts.u, deets);
}
});
});
});
});
@ -361,6 +364,37 @@ verbs['list'] = function(args) {
});
};
verbs['update'] = function(args) {
if (!args || args.length != 1) {
throw 'missing required argument: name of instance';
}
var name = args[0];
validateName(name);
vm.find(name, function(err, deets) {
checkErr(err);
if (deets && deets.ipAddress) {
console.log("pushing to git repo", deets.ipAddress);
git.push(deets.ipAddress, function(line) {
console.log(line);
}, function(status) {
if (!status) {
hooks.runLocalHook('poststart', deets);
}
else {
checkErr("Could not push git instance");
}
});
}
else {
console.log(name, "is not an awsbox instance");
}
});
};
var error = (process.argv.length <= 2);
if (!error) {

14
lib/config.js Normal file
Просмотреть файл

@ -0,0 +1,14 @@
const fs = require('fs');
exports.get = function() {
try {
var awsboxJson = JSON.parse(fs.readFileSync("./.awsbox.json"));
} catch(e) {
checkErr("Can't read awsbox.json: " + e);
}
return awsboxJson;
}

Просмотреть файл

@ -1,8 +1,28 @@
const
child_process = require('child_process');
child_process = require('child_process'),
spawn = child_process.spawn,
path = require('path');
// getEnv is used to pass all the rest of the environment variables to git.
// This prevents the user from being required to enter their password on a git
// push
function getEnv(extraEnv) {
var env = {};
// copy over the original environment
for(var key in process.env) {
env[key] = process.env[key];
}
// add each item in extraEnv
for(var key in extraEnv) {
env[key] = extraEnv[key];
}
return env;
}
exports.addRemote = function(name, host, cb) {
var cmd = 'git remote add "' + name + '" app@'+ host + ':git';
child_process.exec(cmd, cb);
@ -41,7 +61,7 @@ exports.currentSHA = function(dir, cb) {
}
var p = spawn('git', [ 'log', '--pretty=%h', '-1' ], {
env: { GIT_DIR: path.join(dir, ".git") }
env: getEnv({ GIT_DIR: path.join(dir, ".git") })
});
var buf = "";
p.stdout.on('data', function(d) {
@ -71,14 +91,14 @@ exports.push = function(dir, host, pr, cb) {
cb = pr;
pr = host;
host = dir;
dir = path.join(__dirname, '..', '..');
dir = path.join(__dirname, '..', '..', '..');
}
var p = spawn('git', [ 'push', 'app@' + host + ":git", 'HEAD:master' ], {
env: {
env: getEnv({
GIT_DIR: path.join(dir, ".git"),
GIT_WORK_TREE: dir
}
})
});
p.stdout.on('data', function(c) { splitAndEmit(c, pr); });
p.stderr.on('data', function(c) { splitAndEmit(c, pr); });
@ -89,11 +109,11 @@ exports.push = function(dir, host, pr, cb) {
exports.pull = function(dir, remote, branch, pr, cb) {
var p = spawn('git', [ 'pull', "-f", remote, branch + ":" + branch ], {
env: {
env: getEnv({
GIT_DIR: path.join(dir, ".git"),
GIT_WORK_TREE: dir,
PWD: dir
},
}),
cwd: dir
});
@ -107,10 +127,10 @@ exports.pull = function(dir, remote, branch, pr, cb) {
exports.init = function(dir, cb) {
var p = spawn('git', [ 'init' ], {
env: {
env: getEnv({
GIT_DIR: path.join(dir, ".git"),
GIT_WORK_TREE: dir
}
})
});
p.on('exit', function(code, signal) {
return cb(code = 0);

39
lib/hooks.js Normal file
Просмотреть файл

@ -0,0 +1,39 @@
const
path = require('path'),
fs = require('fs'),
child_process = require('./process'),
ssh = require('./ssh.js'),
config = require('./config.js');
function getRemoteHook(which) {
var awsboxJson = config.get();
var remoteHooks = awsboxJson.remote_hooks || awsboxJson.hooks;
return (remoteHooks && remoteHooks[which]) || null;
}
function getLocalHook(which) {
var awsboxJson = config.get();
var localHooks = awsboxJson.local_hooks;
return (localHooks && localHooks[which]) || null;
}
exports.runRemoteHook = function(which, deets, cb) {
var cmd = getRemoteHook(which);
if (cmd) {
console.log(" ... running remote", which, "hook");
}
ssh.runScript(deets.ipAddress, cmd, cb);
}
exports.runLocalHook = function(which, deets, cb) {
var cmd = getLocalHook(which);
if (cmd) {
console.log(" ... running local ", which, "hook");
}
// let each local hook now what the remote AWS host is.
process.env['AWS_IP_ADDRESS'] = deets.ipAddress;
var childProcess = child_process.exec(cmd, cb);
}

34
lib/process.js Normal file
Просмотреть файл

@ -0,0 +1,34 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const
child_process = require('child_process'),
util = require('util');
exports.exec = function(cmd, options, done) {
// options is optional, so if done does not exist, create a default options
// object, update the done reference.
if (!done) {
done = options;
options = {
env: process.env
};
}
if (!cmd) {
done && done(null);
return;
}
var childProcess = child_process.exec(cmd, options, done);
childProcess.stdout.on('data', function(data) {
util.print(data.toString());
});
childProcess.stderr.on('data', function(data) {
util.error(data.toString());
});
};