commit
3c7923384c
9 changed files with 616 additions and 10290 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -8,3 +8,4 @@ npm-debug.log
|
||||||
.vscode
|
.vscode
|
||||||
claimTrieCache.json
|
claimTrieCache.json
|
||||||
syncState.json
|
syncState.json
|
||||||
|
yarn-error.log
|
||||||
|
|
6
.travis.yml
Normal file
6
.travis.yml
Normal 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
10244
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -41,10 +41,12 @@
|
||||||
"bitcoin-promise": "filipnyquist/node-bitcoin-promise#1fbf1cb8913ca3542b66060d48ebea185661e0a7",
|
"bitcoin-promise": "filipnyquist/node-bitcoin-promise#1fbf1cb8913ca3542b66060d48ebea185661e0a7",
|
||||||
"bluebird": "^3.5.0",
|
"bluebird": "^3.5.0",
|
||||||
"chalk": "^2.0.1",
|
"chalk": "^2.0.1",
|
||||||
|
"crypto": "^1.0.1",
|
||||||
"elastic-queue": "^0.3.0",
|
"elastic-queue": "^0.3.0",
|
||||||
"elasticsearch": "^13.2.0",
|
"elasticsearch": "^13.2.0",
|
||||||
"file-exists": "^4.0.0",
|
"file-exists": "^4.0.0",
|
||||||
"glob": "^7.1.1",
|
"glob": "^7.1.1",
|
||||||
|
"got": "^9.2.2",
|
||||||
"jsonfile": "^3.0.1",
|
"jsonfile": "^3.0.1",
|
||||||
"jsonwebtoken": "^7.2.1",
|
"jsonwebtoken": "^7.2.1",
|
||||||
"koa": "^2.0.0-alpha.7",
|
"koa": "^2.0.0-alpha.7",
|
||||||
|
@ -54,6 +56,7 @@
|
||||||
"koa-logger": "^2.0.0",
|
"koa-logger": "^2.0.0",
|
||||||
"koa-router": "^7.0.0",
|
"koa-router": "^7.0.0",
|
||||||
"limited-request-queue": "^3.0.4",
|
"limited-request-queue": "^3.0.4",
|
||||||
|
"node-slack": "^0.0.7",
|
||||||
"oas": "^0.8.8",
|
"oas": "^0.8.8",
|
||||||
"ora": "^1.3.0",
|
"ora": "^1.3.0",
|
||||||
"prettysize": "^1.1.0",
|
"prettysize": "^1.1.0",
|
||||||
|
|
|
@ -6,6 +6,9 @@ import rp from 'request-promise';
|
||||||
import pretty from 'prettysize';
|
import pretty from 'prettysize';
|
||||||
import {claimSync} from '../utils/chainquery';
|
import {claimSync} from '../utils/chainquery';
|
||||||
import {getStats} from '../utils/importer';
|
import {getStats} from '../utils/importer';
|
||||||
|
import crypto from 'crypto';
|
||||||
|
import got from 'got';
|
||||||
|
import {logToSlack} from '../index';
|
||||||
|
|
||||||
const loggerStream = winstonStream(winston, 'info');
|
const loggerStream = winstonStream(winston, 'info');
|
||||||
|
|
||||||
|
@ -346,6 +349,11 @@ function getEscapedQuery (query) {
|
||||||
return escapedQuery;
|
return escapedQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function update () {
|
||||||
|
const shell = require('shelljs');
|
||||||
|
shell.exec('cd ~ && ./update.sh');
|
||||||
|
}
|
||||||
|
|
||||||
class LighthouseControllers {
|
class LighthouseControllers {
|
||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
// Start syncing blocks...
|
// Start syncing blocks...
|
||||||
|
@ -420,6 +428,39 @@ class LighthouseControllers {
|
||||||
ctx.body = await getStatus();
|
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 */
|
/* eslint-enable no-param-reassign */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,12 +6,14 @@ import helmet from 'koa-helmet';
|
||||||
import routing from './routes/';
|
import routing from './routes/';
|
||||||
import { port } from './config';
|
import { port } from './config';
|
||||||
import winston from 'winston';
|
import winston from 'winston';
|
||||||
|
import slack from 'node-slack';
|
||||||
require('winston-daily-rotate-file');
|
require('winston-daily-rotate-file');
|
||||||
|
|
||||||
// Setup logging
|
// Setup logging
|
||||||
winston.remove(winston.transports.Console);
|
winston.remove(winston.transports.Console);
|
||||||
winston.add(winston.transports.Console, { colorize: true, timestamp: true, prettyPrint: true });
|
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
|
// Create Koa Application
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
|
|
||||||
|
@ -24,6 +26,26 @@ app
|
||||||
routing(app);
|
routing(app);
|
||||||
|
|
||||||
// Start the application
|
// 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 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',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -103,4 +103,17 @@ router.get('/autocomplete', LighthouseControllers.autoComplete);
|
||||||
*/
|
*/
|
||||||
router.get('/status', LighthouseControllers.status);
|
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;
|
export default router;
|
||||||
|
|
|
@ -12,6 +12,7 @@ import appRoot from 'app-root-path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import fileExists from 'file-exists';
|
import fileExists from 'file-exists';
|
||||||
import * as util from '../../utils/importer/util';
|
import * as util from '../../utils/importer/util';
|
||||||
|
import {logErrorToSlack} from '../../index';
|
||||||
|
|
||||||
const elasticsearchloglevel = 'info';
|
const elasticsearchloglevel = 'info';
|
||||||
const loggerStream = winstonStream(winston, elasticsearchloglevel);
|
const loggerStream = winstonStream(winston, elasticsearchloglevel);
|
||||||
|
@ -68,7 +69,7 @@ export async function claimSync () {
|
||||||
await sleep(600000);
|
await sleep(600000);
|
||||||
claimSync();
|
claimSync();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
winston.log(err);
|
logErrorToSlack(err);
|
||||||
status.err = err;
|
status.err = err;
|
||||||
await sleep(600000);
|
await sleep(600000);
|
||||||
claimSync();
|
claimSync();
|
||||||
|
@ -116,6 +117,7 @@ function getJSON (path) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
jsonfile.readFile(path, function (err, jsoncontent) {
|
jsonfile.readFile(path, function (err, jsoncontent) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
logErrorToSlack(err);
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
resolve(jsoncontent);
|
resolve(jsoncontent);
|
||||||
|
@ -127,6 +129,7 @@ function saveJSON (path, obj) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
jsonfile.writeFile(path, obj, function (err, jsoncontent) {
|
jsonfile.writeFile(path, obj, function (err, jsoncontent) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
logErrorToSlack(err);
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
resolve();
|
resolve();
|
||||||
|
@ -146,7 +149,7 @@ function getBlockedOutpoints () {
|
||||||
resolve(htmlString);
|
resolve(htmlString);
|
||||||
})
|
})
|
||||||
.catch(function (err) {
|
.catch(function (err) {
|
||||||
winston.log('error', '[Importer] Error getting blocked outpoints. ' + err);
|
logErrorToSlack('[Importer] Error getting blocked outpoints. ' + err);
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -174,7 +177,7 @@ function getClaimsSince (time) {
|
||||||
resolve(htmlString);
|
resolve(htmlString);
|
||||||
})
|
})
|
||||||
.catch(function (err) {
|
.catch(function (err) {
|
||||||
winston.log('error', '[Importer] Error getting updated claims. ' + err);
|
logErrorToSlack('[Importer] Error getting updated claims. ' + err);
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue