-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.js
executable file
·109 lines (101 loc) · 3.18 KB
/
deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /usr/bin/env node
var CloudfilesMirror = require("cloudfiles-mirror");
var exec = require('child_process').exec;
var async = require("async");
var colors = require('colors');
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;
var auth = 'ac2aa55ec8adecc56501fc32cc22ec38';
function getContainer(){
if (argv.env === 'staging'){
return 'budget2012-staging';
}else if (argv.env === 'production'){
return 'budget2012';
}else{
console.error('unknown environment: ' + argv.env + '. Should be staging or production');
process.exit(1);
}
}
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){
run('./build.php', cb);
}
function cdnSync(cb){
var container = getContainer();
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: argv.key},
cdnEnabled: true,
pushOnBoot: true,
headers: { '\\.(eot|svg|ttf|woff)$': ['Access-Control-Allow-Origin: *'] }
});
mirror.mime.define({'application/octet-stream': ['woff']});
mirror.on('error', next);
mirror.on('end', function(err){
console.error(('Syncing ' + dir + ' to ' + container + ' OK').green)
next();
});
}, cb);
}
function run(cmd, cb){
console.error('Running command: ' + cmd);
exec(cmd, function(err, stdout, stderr){
if (err){
console.error(stdout.blue);
console.error(stderr.pink);
console.error(('Running command: ' + cmd + ' ERROR').red);
}else{
console.error(('Running command: ' + cmd + ' OK').green);
}
cb(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);
}
console.error('ok'.green);
});