This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
436 lines (401 loc) · 14.1 KB
/
agent.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* Deployment daemon for luajit.me
*
* - Receives notification from GitHub whenever a new deployment is requested;
*
* - deploys to staging or production using Terraform configuration in the
* branch being deployed;
*
* - updates deployment status on GitHub;
*
* - destroys staging as soon as it becomes obsolete;
*
* - captures staging's IP address and works as a proxy
* for HTTP requests hitting staging.luajit.me.
*
* The daemon responds to HTTP requests on localhost:8000.
* It is supposed to be deployed behind nginx.
*/
const assert = require('assert');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const childProcess = require('child_process');
const express = require('express');
const fs = require('fs');
const https = require('https');
const { spawn } = require('child_process');
const dataDir = process.env.DATA_DIR || '.';
const logDir = process.env.LOG_DIR || '.';
const logURLPrefix = process.env.LOG_URL_PREFIX || 'https://deploy.luajit.me/logs';
// authenticate this service users
const agentSecret = process.env.AGENT_SECRET;
delete process.env.AGENT_SECRET;
// used to modify deployment statuses
const githubToken = process.env.GITHUB_TOKEN;
delete process.env.GITHUB_TOKEN;
// full access
const digitalOceanToken = process.env.DIGITALOCEAN_TOKEN;
delete process.env.DIGITALOCEAN_TOKEN;
//
const sshKeyFingerprint = process.env.SSH_KEY_FINGERPRINT;
delete process.env.SSH_KEY_FINGERPRINT;
//
const sshPrivateKeyFile = process.env.SSH_PRIVATE_KEY_FILE;
delete process.env.SSH_PRIVATE_KEY_FILE;
const state = (function() {
try {
return JSON.parse(fs.readFileSync(dataDir + '/state.json'));
} catch (e) {
if (e.code === 'ENOENT') return {};
console.log('Error loading state:', e.message);
process.exit(-1);
}
}) ();
const [persistState, persistStateNow] = (() => {
let persistPending = false;
function persistStateNow() {
persistPending = false;
try {
fs.writeFileSync(dataDir + '/.state.json', JSON.stringify(state, null, 2));
fs.renameSync(dataDir + '/.state.json', dataDir + '/state.json');
} catch (e) {
console.log('Error saving state:', e.message);
process.exit(-1);
}
}
function persistState() {
if (!persistPending) {
persistPending = true;
process.nextTick(() => persistPending && persistStateNow());
}
}
return [persistState, persistStateNow];
}) ();
// addDeployment(id) -> bool; true iff this is a new deployment
const addDeployment = (function() {
const deployments = new Set(state.deployments || []);
state.deployments = { toJSON: () => Array.from(deployments) };
return function(id) {
if (deployments.has(id)) return false;
deployments.add(id);
persistState();
return true;
}
}) ();
// postDeploymentStatus(id, ghStatus)
const postDeploymentStatus = (function() {
const inflight = new Map(), pending = new Map();
function deliver(id, ghStatus, cb) {
const req = https.request({
hostname: 'api.github.com',
path: `/repos/rapidlua/luajit.me/deployments/${id}/statuses`,
method: 'POST',
headers: {
'User-Agent': 'deploy-daemon',
'Authorization': 'token ' + githubToken,
'Content-Type': 'application/json',
'Accept': 'application/vnd.github.flash-preview+json, application/vnd.github.ant-man-preview+json'
}
}, (res) => cb(res.statusCode === 201 ? null : new Error(res.statusMessage)));
req.on('error', cb);
req.end(JSON.stringify(ghStatus));
}
function postStatus(id, ghStatus) {
if (inflight.has(id)) return void pending.set(id, ghStatus);
inflight.set(id, ghStatus);
deliver(id, ghStatus, (err) => {
if (err) console.log(`Failed to update status: #${id} -> ${ghStatus.state}:`, err.message);
inflight.delete(id);
if (!(ghStatus = pending.get(id))) return void persistState();
pending.delete(id);
postStatus(id, ghStatus);
});
}
for (const [key, value] of Object.entries(state.inDelivery || {})) {
postStatus(+key, value);
}
function fromEntries(map) {
const res = {};
for (let [k, v] of map.entries()) res[k] = v;
return res;
}
state.inDelivery = { toJSON: () => Object.assign(fromEntries(inflight), fromEntries(pending)) };
return (id, ghStatus) => (postStatus(id, ghStatus), void persistState());
}) ();
// createDeploymentEnv(...) -> { deploy: ..., deactivate: ..., rescale: ..., sha: ... }
function createDeploymentEnv(name, handler) {
const PENDING = 'pending', INPROGRESS = 'in-progress', FAILED = 'failed', DONE = undefined;
let id, sha, payload, s = DONE, next = null, rescalePending = false;
function deploy(options) {
persistState();
if (next && next.id !== undefined)
postDeploymentStatus(next.id, { state: 'inactive' });
next = { id: options.id, sha: options.sha, payload: options.payload };
if (options.id !== undefined)
postDeploymentStatus(options.id, { state: 'queued' });
doDeploy();
}
function onComplete(status) {
persistState();
assert(s === INPROGRESS);
s = status === undefined ? DONE : FAILED;
doDeploy();
}
function doDeploy() {
if (s === INPROGRESS || !(next || rescalePending)) return;
persistState();
rescalePending = false;
if (next) {
if (id !== undefined && s !== FAILED)
postDeploymentStatus(id, { state: 'inactive' });
({id, sha, payload} = next);
next = null;
process.nextTick(handler, name, id ? 'deploy' : 'deactivate', {id, sha, payload}, onComplete);
} else {
process.nextTick(handler, name, 'rescale', {id, sha, payload}, onComplete);
}
s = INPROGRESS;
}
if (state[name]) {
if ([DONE, FAILED].indexOf(state[name].s) === -1) deploy(state[name]);
else ({id, sha, payload, s} = state[name]);
}
state[name] = { toJSON: () => Object.assign({ s: PENDING }, next || {id, sha, payload, s}) };
// persisting a single deployment state, hence current state is lost
// when next is present; to compensate, persistent state will include an
// extra inDelivery message; if delivered, it switches the lost deployment's to inactive
const gsInDelivery = state.inDelivery;
state.inDelivery = { toJSON: function() {
const inDelivery = gsInDelivery.toJSON();
if (id !== undefined && next && s !== FAILED)
inDelivery[id] = { state: 'inactive' };
return inDelivery;
}};
const o = {
deploy: function(options) {
assert(options.id !== undefined);
addDeployment(options.id) && deploy(options);
},
rescale: function() {
rescalePending = id !== undefined && s !== FAILED;
doDeploy();
},
deactivate: function() { id !== undefined && deploy({}); }
};
return Object.defineProperty(o, 'sha', { get: () => next ? next.sha : sha });
}
async function deploy(env, action, options, onComplete) {
const srcDir = `${dataDir}/${env}/src`;
const terraformStateDir = `${dataDir}/${env}/tfstate`;
const terraformSrcDir = `${dataDir}/${env}/src/deployments/${env}`;
let logFD;
function logWrite(data) {
if (logFD !== undefined) fs.writeFileSync(logFD, data + '');
else console.log(data);
}
function gitFetch() {
return new Promise((resolve, reject) => {
logWrite('$ git fetch\n');
const git = spawn('git', ['fetch'], {
cwd: srcDir, stdio: ['ignore', logFD, logFD]
});
git.on('error', reject);
git.on('close', (code) => {
code === 0 ? resolve() : reject(new Error('git fetch'));
});
});
}
function gitCheckout() {
return new Promise((resolve, reject) => {
logWrite(`$ git checkout ${options.sha}\n`);
const git = spawn('git', ['-c', 'advice.detachedHead=false', 'checkout', '-f', options.sha], {
cwd: srcDir, stdio: ['ignore', logFD, logFD]
});
git.on('error', reject);
git.on('close', (code) => {
code === 0 ? resolve() : reject(new Error('git checkout'));
});
});
}
function isSHA1AncestorOfSHA2(sha1, sha2) {
return new Promise((resolve, reject) => {
if (!sha2 || !sha2) resolve(false);
const git = spawn('git', ['merge-base', '--is-ancestor', sha1, sha2], {
cwd: srcDir, stdio: 'ignore'
});
git.on('error', reject);
git.on('close', (code) => resolve(code === 0));
});
}
function getTerraformEnv() {
return {
TF_VAR_digitalocean_token: digitalOceanToken,
TF_VAR_ssh_key_fingerprint: sshKeyFingerprint,
TF_VAR_ssh_private_key_file: sshPrivateKeyFile,
TF_VAR_app_version: options.payload ? options.payload.version : ""
};
}
function terraformInit() {
const terraformInitArgs = [ 'init', '-no-color', terraformSrcDir ];
return new Promise((resolve, reject) => {
logWrite('$ terraform init\n');
const terraform = spawn('terraform', terraformInitArgs, {
cwd: terraformStateDir, stdio: ['ignore', logFD, logFD]
});
terraform.on('error', reject);
terraform.on('close', (code) => {
code === 0 ? resolve() : reject(new Error('terraform init'));
});
});
}
function terraformApply() {
const terraformApplyArgs = [
'apply', '-input=false', '-no-color', '-auto-approve',
'-parallelism=100', terraformSrcDir
];
return new Promise((resolve, reject) => {
logWrite('$ terraform apply\n');
const terraform = spawn('terraform', terraformApplyArgs, {
cwd: terraformStateDir, stdio: ['ignore', logFD, logFD],
env: Object.assign({}, process.env, getTerraformEnv())
});
terraform.on('error', reject);
terraform.on('close', (code) => {
code === 0 ? resolve() : reject(new Error('terraform apply'));
});
});
}
function terraformOutput() {
return new Promise((resolve, reject) => {
const terraform = spawn('terraform', ['output', '-json'], {
cwd: terraformStateDir, stdio: ['ignore', 'pipe', logFD]
});
const out = [];
terraform.stdout.on('data', buf => out.push(buf));
terraform.stdout.on('close', function() {
try {
resolve(JSON.parse(Buffer.concat(out).toString()));
} catch (e) { reject(e); }
});
});
}
function terraformDestroy() {
const terraformDestroyArgs = [
'destroy', '-input=false', '-no-color', '-auto-approve', terraformSrcDir
];
return new Promise((resolve, reject) => {
logWrite('$ terraform destroy\n');
const terraform = spawn('terraform', terraformDestroyArgs, {
cwd: terraformStateDir, stdio: ['ignore', logFD, logFD],
env: Object.assign({}, process.env, getTerraformEnv())
});
terraform.on('error', reject);
terraform.on('close', (code) => {
code === 0 ? resolve() : reject(new Error('terraform destroy'));
});
});
}
function reconfigure(terraformOutput) {
const to = terraformOutput;
switch (env) {
case 'staging':
state.stagingHost = to.staging_ip ? to.staging_ip.value[0] : undefined;
break;
}
persistState();
}
let logURL;
try {
// create log file
while (true) {
const name = Math.random().toString(36).substr(2, 8);
if (name.length !== 8) continue;
try {
logFD = fs.openSync(
`${logDir}/${name}.log`,
fs.constants.O_RDWR | fs.constants.O_CREAT |
fs.constants.O_EXCL | fs.constants.O_APPEND
);
logURL = `${logURLPrefix}/${name}.log`;
break;
} catch (e) {
if (e.code !== 'EEXIST') throw e;
}
}
if (options.id)
postDeploymentStatus(options.id, { state: 'in_progress', log_url: logURL });
switch (action) {
case 'deploy':
await gitFetch();
await gitCheckout();
if (env === 'staging' && await isSHA1AncestorOfSHA2(staging.sha, production.sha)) {
staging.deactivate();
logWrite('[deployment superceeded by a newer production deployment]\n');
break;
}
await terraformInit();
await terraformApply();
reconfigure(await terraformOutput());
if (env === 'production' && await isSHA1AncestorOfSHA2(staging.sha, options.sha))
staging.deactivate();
break;
case 'deactivate':
await terraformDestroy();
reconfigure({});
break;
case 'rescale':
await terraformApply();
reconfigure(await terraformOutput());
break;
}
if (options.id)
postDeploymentStatus(options.id, { state: 'success', log_url: logURL });
logWrite('[deployment succeeded]\n');
onComplete();
} catch (e) {
logWrite(`[deployment failed: ${e.message}]\n`);
if (options.id)
postDeploymentStatus(options.id, { state: 'failure', log_url: logURL });
onComplete('failed');
}
if (logFD !== undefined) fs.closeSync(logFD);
}
const production = createDeploymentEnv('production', deploy);
const staging = createDeploymentEnv('staging', deploy);
const app = express();
const rawParser = bodyParser.raw({ type: '*/*' });
const proxy = require('express-http-proxy');
app.post('/deploy/new', rawParser, function (req, res) {
const signature = req.header('X-Hub-Signature');
if (signature && req.body instanceof Buffer
&& secureEqual(signature, computeSignature(req.body))
) {
try {
const d = JSON.parse(req.body.toString()).deployment;
if (d) {
(d.environment === 'production' ? production : staging).deploy(d);
persistStateNow();
}
res.status(200).send();
} catch (e) {
console.error(e);
res.status(500).send(e.message);
}
} else {
res.status(500).send('Signature mismatch');
}
});
function computeSignature(data) {
return 'sha1=' + crypto.createHmac('sha1', agentSecret).update(data).digest('hex');
}
function secureEqual(a, b) {
a = new Buffer(a);
b = new Buffer(b);
return a.length === b.length && crypto.timingSafeEqual(a, b);
}
app.use('/deploy', (req, res) => res.redirect('https://github.com/rapidlua/luajit.me/deployments'));
app.use('/staging', proxy(() => 'http://' + state.stagingHost, {
memoizeHost: false,
filter: () => state.stagingHost !== undefined
}));
app.use('/staging', express.static(__dirname + '/staging-placeholder'));
app.listen(8000, 'localhost', function () { console.log('agent ready'); });