Added auto-deployment for lighthouse

This commit is contained in:
Mark Beamer Jr 2018-10-27 13:47:13 -04:00
parent df9b318da6
commit 36db99ddca
No known key found for this signature in database
GPG key ID: 1C314FB89AD76973
7 changed files with 467 additions and 10281 deletions
server/controllers

View file

@ -6,6 +6,8 @@ import rp from 'request-promise';
import pretty from 'prettysize';
import {claimSync} from '../utils/chainquery';
import {getStats} from '../utils/importer';
import crypto from 'crypto';
import got from 'got';
const loggerStream = winstonStream(winston, 'info');
@ -346,6 +348,11 @@ function getEscapedQuery (query) {
return escapedQuery;
}
async function update () {
const shell = require('shelljs');
shell.exec('cd ~ && ./update.sh');
}
class LighthouseControllers {
/* eslint-disable no-param-reassign */
// Start syncing blocks...
@ -420,6 +427,39 @@ class LighthouseControllers {
ctx.body = await getStatus();
}
/**
* AutoUpdate updates the application from the master branch.
* @param {ctx} Koa Context
*/
async autoUpdate (ctx) {
let travisSignature = Buffer.from(ctx.request.headers.signature, 'base64');
let payload = ctx.request.body.payload;
let travisResponse = await got('https://api.travis-ci.com/config', {timeout: 10000});
let travisPublicKey = JSON.parse(travisResponse.body).config.notifications.webhook.public_key;
let verifier = crypto.createVerify('sha1');
verifier.update(payload);
let status = verifier.verify(travisPublicKey, travisSignature);
if (status) {
let notification = JSON.parse(payload);
if (notification.branch === 'auto_deploy_api') {
if (!notification.isPullRequest) {
console.log('Auto Updating Lighthouse - ', notification.message);
update();
ctx.body = 'OK';
} else {
ctx.status = 400;
ctx.body = 'skip auto update: pull request';
}
} else {
ctx.status = 400;
ctx.body = 'only deploys on master branch';
}
} else {
ctx.status = 500;
ctx.body = 'could not verify webhook';
}
}
/* eslint-enable no-param-reassign */
}