Add auto update component

This commit is contained in:
Alice Gaudon 2020-05-05 15:19:23 +02:00
parent 3f66f728e5
commit 924104f700
4 changed files with 57 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
node_modules
dist
dist
yarn-error.log

View File

@ -4,6 +4,7 @@ export default {
public_url: "http://localhost:4899",
public_websocket_url: "ws://localhost:4899",
port: 4899,
gitlab_webhook_token: 'secret',
mysql: {
connectionLimit: 10,
host: "localhost",

View File

@ -1,6 +1,6 @@
{
"name": "wms-core",
"version": "0.4.24",
"version": "0.4.25",
"description": "Node web framework",
"repository": "git@gitlab.com:ArisuOngaku/wms-core.git",
"author": "Alice Gaudon <alice@gaudon.pro>",

View File

@ -0,0 +1,53 @@
import {Express, Router} from "express";
import config from "config";
import * as child_process from "child_process";
import ApplicationComponent from "../ApplicationComponent";
import {ForbiddenHttpError} from "../HttpError";
import Logger from "../Logger";
const ROUTE = '/update/push.json';
export default class AutoUpdateComponent extends ApplicationComponent<void> {
public async start(app: Express, router: Router): Promise<void> {
router.post(ROUTE, (req, res, next) => {
const token = req.header('X-Gitlab-Token');
if (!token || token !== config.get<string>('gitlab_webhook_token')) throw new ForbiddenHttpError('Invalid token', req.url);
this.update(req.body.checkout_sha)
.catch(Logger.error);
res.json({
'status': 'ok',
});
});
}
public async stop(): Promise<void> {
}
private async update(checkout_sha: string) {
await this.app!.stop();
try {
Logger.info('Starting auto update...');
// Checkout new source
await this.runCommand(`git checkout ${checkout_sha}`);
// Install new dependencies
await this.runCommand(`yarn install --production=false`);
// Process assets
await this.runCommand(`yarn dist`);
Logger.info('Success!');
} catch (e) {
Logger.error(e, 'An error occurred while running the auto update.');
}
}
private async runCommand(command: string) {
Logger.info(`> ${command}`);
Logger.info(child_process.execSync(command).toString());
}
}