Skip to content

Commit

Permalink
Merge branch 'master' of github.com:TheGlobalMail/YourFederalBudget2012
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Cobby committed Oct 10, 2012
2 parents 5841de6 + 4156b9d commit 7ae1357
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
74 changes: 50 additions & 24 deletions deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,40 @@ var CloudfilesMirror = require("cloudfiles-mirror");
var exec = require('child_process').exec;
var async = require("async");
var colors = require('colors');
var apiKey, env;
var request = require('request');
var argv = require('optimist')
.usage('Usage: $0 -e [environment] -p [apikey]')
.demand(['e','k'])
.alias('e', 'env')
.describe('e', 'Environment to deploy to')
.alias('k', 'key')
.describe('k', 'Rackspace API key')
.alias('a', 'all')
.describe('a', 'Sync all asssets (not just build files)')
.argv;

function parseArgs(){
if (process.argv.length !== 4){
console.error('Usage: ./deploy.js staging RACKSPACE_API_KEY');
process.exit(1);
}
env = process.argv[2];
apiKey = process.argv[3];
}
var auth = 'ac2aa55ec8adecc56501fc32cc22ec38';

function getContainer(){
if (env === 'staging'){
if (argv.env === 'staging'){
return 'budget2012-staging';
}else if (env === 'production'){
}else if (argv.env === 'production'){
return 'budget2012';
}else{
console.error('unknown environment: ' + env + '. Should be staging or production');
console.error('unknown environment: ' + argv.env + '. Should be staging or production');
process.exit(1);
}
}

function checkout(cb){
run('git checkout ' + env, cb);
function getServers(){
if (argv.env === 'staging'){
return ['50.56.185.86'];
}else if (argv.env === 'production'){
return ['50.56.172.114', '198.101.231.69'];
}else{
console.error('unknown environment: ' + argv.env + '. Should be staging or production');
process.exit(1);
}
}

function build(cb){
Expand All @@ -35,13 +45,14 @@ function build(cb){

function cdnSync(cb){
var container = getContainer();
async.forEach(['build', 'img', 'vendor'], function(dir, next){
console.error('Syncing ' + dir + ' to ' + container + '...');
var syncDirs = argv.all ? ['build', 'img', 'vendor'] : ['build'];
async.forEach(syncDirs, function(dir, next){
console.error('Syncing ' + dir + ' to ' + container);
mirror = CloudfilesMirror({
localPath: './web/' + dir,
remoteBase: dir,
container: container,
auth : { username: 'theglobalmail', apiKey: apiKey},
auth : { username: 'theglobalmail', apiKey: argv.key},
cdnEnabled: true,
pushOnBoot: true
});
Expand All @@ -53,12 +64,8 @@ function cdnSync(cb){
}, cb);
}

function push(cb){
run('git push origin HEAD', cb);
}

function run(cmd, cb){
console.log('Running command: ' + cmd);
console.error('Running command: ' + cmd);
exec(cmd, function(err, stdout, stderr){
if (err){
console.error(stdout.blue);
Expand All @@ -71,8 +78,27 @@ function run(cmd, cb){
});
}

parseArgs();
async.series([checkout, build, cdnSync, push], function(err){
function deploy(cb){
var servers = getServers();
async.forEach(servers, function(server, done){
console.error('Triggering deploy on ' + server);
request({method: 'POST', url: 'http://' + server + '/deploy', form: {auth: auth}}, function(err, res, body){
if (err){
console.error(('ERROR: ' + err).red);
return done('Triggering deploy on ' + server + ' ERROR');
}
if (res.statusCode != 200){
console.error(('ERROR: status code was ' + res.statusCode).red);
console.error(('ERROR: ' + body).red);
return done('Triggering deploy on ' + server + ' ERROR');
}
console.error(('Triggering deploy on ' + server + ' OK').green);
done();
});
}, cb);
}

async.series([build, cdnSync, deploy], function(err){
if (err){
console.error(('ERROR: ' + err).red);
process.exit(1);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"should": "~1.1.0",
"async": "~0.1.22",
"cloudfiles-mirror": "*",
"colors": "~0.6.0-1"
"colors": "~0.6.0-1",
"optimist": "~0.3.4"
},
"devDependencies": {
"wd": "0.0.21"
Expand Down
30 changes: 23 additions & 7 deletions src/DGM/Provider/BaseControllerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,34 @@ public function connect(Application $app)
$committedBranch = (isset($data['ref'])) ? $data['ref'] : $request->get('ref');
$request->request->replace(is_array($data) ? $data : array());

# Only update this deployment if the commit was on the current branch
$branch = trim($app['config']['branch']);
$app['monolog']->addInfo("Build app on $branch");
if ($branch !== "master"){
$app['monolog']->addInfo("Rejected github hook as not on master branch");
$exec = "Commit was not on master and will be ignored";
$response = 200;
} else{
$app['monolog']->addInfo("Build app on $branch");
$dir = realpath(__DIR__ . '/../../../');
$exec = shell_exec("cd $dir && git pull && git submodule update --init && composer install 2>&1 >> logs/build_log.txt");
$response = $exec == null ? 500 : 200;
}

if ($committedBranch === "refs/heads/$branch") {
return new Response($exec, $response);
});

$controllers->post('/deploy', function(Request $request) use ($app) {
$auth = $request->get('auth');

$branch = trim($app['config']['branch']);
if ($auth !== 'ac2aa55ec8adecc56501fc32cc22ec38'){
$app['monolog']->addInfo("Rejected deploy with bad auth: {$auth}");
$exec = "Auth error";
$response = 500;
}else{
$app['monolog']->addInfo("Build app on $branch");
$dir = realpath(__DIR__ . '/../../../');
// @TODO refactor epic one-liner?
$exec = shell_exec("cd $dir && git pull && git submodule update --init && composer install 2>&1 >> logs/build_log.txt");
$response = $exec == null ? 500 : 200;
} else {
$exec = "Commit was not on $branch";
$response = 200;
}

return new Response($exec, $response);
Expand Down

0 comments on commit 7ae1357

Please sign in to comment.