This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.js
executable file
·266 lines (253 loc) · 8.73 KB
/
controller.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var util = require('util');
var proc = require('procstreams');
var async = require('async');
var nconf = require('nconf');
var cliOptions = {
branch: {
alias: 'b',
demand: true,
describe: 'The branch to check for changes.'
},
cwd: {
demand: true,
describe: 'The working directory of the git repo.'
},
environment: {
alias: 'e',
demand: false,
describe: 'The environment to deploy, such as production or staging. Defaults to $NODE_ENV'
},
deployer: {
alias: 'd',
demand: true,
describe: 'The script in cwd that will be used to perform the deployment if changes are detected.'
},
'show-output': {
alias: 'o',
describe: 'Causes output of child processes to be piped to stdout'
},
force: {
alias: 'f',
describe: 'Force deployment, even if there are no changes to the local repo.'
}
};
nconf.argv(cliOptions);
nconf.env();
nconf.defaults({environment: nconf.get('NODE_ENV')});
var config = nconf.load();
process.chdir(config.cwd);
console.log(process.cwd());
var wrapData = function(callback) {
return function(stdout, stderr) {
callback(null, {stdout: stdout, stderr: stderr});
};
};
var wrapError = function(callback) {
return function(err, stdout, stderr) {
callback({err: err, stdout: stdout, stderr: stderr});
};
};
parsers = {
refs: function(stdout, callback) {
var refs = [];
stdout.split('\n').forEach(function(ref) {
if(ref.trim()) {
refs.push(JSON.parse(ref));
}
});
callback(null, refs);
}
};
var gitProcOptions = {
out: config['show-output']
};
async.auto({
locals: function(callback) {
proc('git', [
'for-each-ref',
'--format={"objectname": "%(objectname:short)", "refname": "%(refname:short)", "upstream":"%(upstream:short)"}',
'refs/heads'
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
},
parseLocals: ['locals', function(callback, results) { parsers.refs(results.locals.stdout, callback); }],
remotes: function(callback) {
proc('git', [
'for-each-ref',
'--format={"objectname": "%(objectname:short)", "refname": "%(refname:short)", "upstream":"%(upstream:short)"}',
'refs/remotes'
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
},
parseRemotes: ['remotes', function(callback, results) { parsers.refs(results.remotes.stdout, callback); }],
findHead: function(callback) {
proc('git', [
'symbolic-ref',
'-q',
'HEAD'
], gitProcOptions).data(function(stdout, stderr) {
if(stdout) {
var shortname = stdout.replace(new RegExp('(refs\/heads\/)(.*)'), '$2').trim();
callback(null, shortname);
}
else {
callback('Failed to find HEAD');
}
});
},
checkBranch: ['parseLocals', 'parseRemotes', function(callback, results) {
ensureTrackingBranch(config.branch, results.parseLocals, results.parseRemotes, callback);
}],
remoteName: ['checkBranch', function(callback, results) {
callback(null, results.checkBranch.upstream.replace(new RegExp('(^[^/]*)(\/.*)?$'), '$1'));
}],
remoteUrl: ['remoteName', function(callback, results) {
proc('git', [
'config',
'--get',
util.format('remote.%s.url', results.remoteName)
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
}],
fetchRemote: ['remoteName', function(callback, results) {
proc('git', [
'fetch',
results.remoteName
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
}],
logLocalRemote: ['fetchRemote', function(callback, results) {
proc('git', [
'log',
util.format('%s..%s', results.checkBranch.refname, results.checkBranch.upstream)
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
}],
checkoutLocal: ['findHead', 'logLocalRemote', 'fetchRemote', function(callback, results) {
proc('git', [
'checkout',
results.checkBranch.refname
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
}],
mergeRemote: ['logLocalRemote', 'checkoutLocal', function(callback, results) {
proc('git', [
'merge',
results.checkBranch.upstream
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
}],
deployment: ['remoteUrl', 'mergeRemote', function(callback, results) {
if(!results.logLocalRemote.stdout.trim() && !config.force) {
return callback(null, 'Nothing to deploy.');
}
console.log('Proceding with deployment');
var outerResults = results;
try {
var plugin = require(path.join(path.resolve(config.cwd), config.deployer));
var deployer = new plugin(config.environment || config.NODE_ENV);
async.auto({
outer: function(callback) { callback(null, outerResults); },
deploy: ['outer', function(callback, results) { deployer.deploy(callback, results); }],
verify: ['deploy', function(callback, results) { deployer.verify(callback, results); }],
commit: ['verify', function(callback, results) { deployer.commit(callback, results); }]
}, function(err, results) {
if(err) {
results.error = err;
try {
deployer.rollback(function() { callback(err, results); }, results);
}
catch(rollbackError) {
console.error('Error executing rollback:');
console.error(rollbackError);
callback(err, results);
}
}
else {
callback(null, results);
}
});
}
catch(error) {
console.error('Error executing deployment:');
console.error(error);
callback(error);
}
}]
}, function(err, results) {
if(err) {
console.error(err);
if(results.checkBranch) {
console.log('Resetting %s to previous head (%s)', results.checkBranch.refname, results.checkBranch.objectname);
async.auto({
reset: function(callback) {
proc('git', [
'reset',
'--hard',
results.checkBranch.objectname
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
},
clean: ['reset', function(callback, results) {
proc('git', [
'clean',
'-df'
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
}]
}, function(err, results) {
process.exit();
});
}
else {
process.exit(1);
}
}
else {
console.log('Deployment completed. These are the tasks that were preformed and their results:');
for(var i in results) {
console.log('%s:', i);
console.log(results[i]);
}
}
});
var ensureTrackingBranch = function(trackingBranch, locals, remotes, callback) {
var localFiltered = locals.filter(function(ref) {
return ref.refname == trackingBranch || ref.upstream == trackingBranch;
});
// is the branch we're tracking a local ref?
var local = localFiltered.length === 0 ? null : localFiltered[0];
if(local) {
return local.upstream ? callback(null, local)
: callback(util.format('Local branch %s is not tracking an upstream. Try running something like git branch --set-upstream %s origin/%s', local.refname, local.refname));
}
var remoteFiltered = remotes.filter(function(ref) { return ref.refname == trackingBranch; });
// is the branch we're tracking a remote ref?
var remote = remoteFiltered.length === 0 ? null : remoteFiltered[0];
if(!remote) {
return callback(util.format('%s was not found in the local or remote branches', trackingBranch));
}
console.log('Found remote %s, but it is not tracking locally. Creating local branch...', remote.refname);
async.auto({
checkout: function(callback) {
proc('git', [
'checkout',
'--track',
remote.refname
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
},
locals: ['checkout', function(callback, results) {
proc('git', [
'for-each-ref',
'--format={"objectname": "%(objectname:short)", "refname": "%(refname:short)", "upstream":"%(upstream:short)"}',
'refs/heads'
], gitProcOptions).data(wrapData(callback)).error(wrapError(callback));
}],
parseLocals: ['locals', function(callback, results) {
parsers.refs(results.locals.stdout, callback);
}],
findLocal: ['parseLocals', function(callback, results) {
callback(null, results.parseLocals.filter(function(ref) { return ref.upstream == trackingBranch; })[0]);
}]
}, function(err, results) {
if(err) {
return callback(err);
}
console.log('Created %s with upstream %s', results.findLocal.refname, results.findLocal.upstream);
callback(null, results.findLocal);
});
};