Merge pull request #127 from lbryio/auto_deploy_api

Auto deploy api
This commit is contained in:
Mark 2018-11-04 01:16:45 -04:00 committed by GitHub
commit 3c7923384c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 616 additions and 10290 deletions

1
.gitignore vendored
View file

@ -8,3 +8,4 @@ npm-debug.log
.vscode
claimTrieCache.json
syncState.json
yarn-error.log

6
.travis.yml Normal file
View file

@ -0,0 +1,6 @@
language: node_js
node_js:
- "node"
notifications:
email: false
webhooks: https://lighthouse.lbry.io/autoupdate

10244
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -41,10 +41,12 @@
"bitcoin-promise": "filipnyquist/node-bitcoin-promise#1fbf1cb8913ca3542b66060d48ebea185661e0a7",
"bluebird": "^3.5.0",
"chalk": "^2.0.1",
"crypto": "^1.0.1",
"elastic-queue": "^0.3.0",
"elasticsearch": "^13.2.0",
"file-exists": "^4.0.0",
"glob": "^7.1.1",
"got": "^9.2.2",
"jsonfile": "^3.0.1",
"jsonwebtoken": "^7.2.1",
"koa": "^2.0.0-alpha.7",
@ -54,6 +56,7 @@
"koa-logger": "^2.0.0",
"koa-router": "^7.0.0",
"limited-request-queue": "^3.0.4",
"node-slack": "^0.0.7",
"oas": "^0.8.8",
"ora": "^1.3.0",
"prettysize": "^1.1.0",

View file

@ -6,6 +6,9 @@ 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';
import {logToSlack} from '../index';
const loggerStream = winstonStream(winston, 'info');
@ -346,6 +349,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 +428,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 === 'master') {
if (!notification.isPullRequest) {
logToSlack('Auto Updating Lighthouse - ' + notification.message);
update();
ctx.body = 'OK';
} else {
ctx.status = 400;
ctx.body = 'skip auto update: pull request'; logToSlack(ctx.body);
}
} else {
ctx.status = 400;
ctx.body = 'skip auto update: only deploys on master branch'; logToSlack(ctx.body);
}
} else {
ctx.status = 500;
ctx.body = 'skip auto update: could not verify webhook'; logToSlack(ctx.body);
}
}
/* eslint-enable no-param-reassign */
}

View file

@ -6,12 +6,14 @@ import helmet from 'koa-helmet';
import routing from './routes/';
import { port } from './config';
import winston from 'winston';
import slack from 'node-slack';
require('winston-daily-rotate-file');
// Setup logging
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, { colorize: true, timestamp: true, prettyPrint: true });
var slackAPIKey = process.env.SLACK_HOOK_URL;
var mySlack = new slack(slackAPIKey, {});
// Create Koa Application
const app = new Koa();
@ -24,6 +26,26 @@ app
routing(app);
// Start the application
app.listen(port, () => winston.log('info', `Lighthouse API server is running at http://localhost:${port}/`));
app.listen(port, () => logToSlack(`Lighthouse API server is running at http://localhost:${port}/`));
export default app;
export function logToSlack (message) {
winston.log('info', 'SentToSlack: ' + message);
mySlack.send({
text : message,
channel : '#lighthouse-status',
username : 'Lighthouse',
icon_emoji: 'lighthouse',
});
}
export function logErrorToSlack (message) {
winston.log('error', 'SentToSlack: ' + message);
mySlack.send({
text : message,
channel : '#lighthouse-status',
username : 'Lighthouse',
icon_emoji: 'lighthouse',
});
}

View file

@ -103,4 +103,17 @@ router.get('/autocomplete', LighthouseControllers.autoComplete);
*/
router.get('/status', LighthouseControllers.status);
/**
* @oas [get] /autoupdate
* tags:
* - Auto Update API
* description: "Checks signature of travis webhook and calls deploy script to get the latest master branch to deploy."
* responses:
* 200:
* description: Successful if script called.
*
*/
router.post('/autoupdate', LighthouseControllers.autoUpdate);
export default router;

View file

@ -12,6 +12,7 @@ import appRoot from 'app-root-path';
import fs from 'fs';
import fileExists from 'file-exists';
import * as util from '../../utils/importer/util';
import {logErrorToSlack} from '../../index';
const elasticsearchloglevel = 'info';
const loggerStream = winstonStream(winston, elasticsearchloglevel);
@ -68,7 +69,7 @@ export async function claimSync () {
await sleep(600000);
claimSync();
} catch (err) {
winston.log(err);
logErrorToSlack(err);
status.err = err;
await sleep(600000);
claimSync();
@ -116,6 +117,7 @@ function getJSON (path) {
return new Promise((resolve, reject) => {
jsonfile.readFile(path, function (err, jsoncontent) {
if (err) {
logErrorToSlack(err);
reject(err);
} else {
resolve(jsoncontent);
@ -127,6 +129,7 @@ function saveJSON (path, obj) {
return new Promise((resolve, reject) => {
jsonfile.writeFile(path, obj, function (err, jsoncontent) {
if (err) {
logErrorToSlack(err);
reject(err);
} else {
resolve();
@ -146,7 +149,7 @@ function getBlockedOutpoints () {
resolve(htmlString);
})
.catch(function (err) {
winston.log('error', '[Importer] Error getting blocked outpoints. ' + err);
logErrorToSlack('[Importer] Error getting blocked outpoints. ' + err);
reject(err);
});
});
@ -174,7 +177,7 @@ function getClaimsSince (time) {
resolve(htmlString);
})
.catch(function (err) {
winston.log('error', '[Importer] Error getting updated claims. ' + err);
logErrorToSlack('[Importer] Error getting updated claims. ' + err);
reject(err);
});
});

563
yarn.lock

File diff suppressed because it is too large Load diff