diff --git a/.gitignore b/.gitignore index 76fb2e5..5b5cd12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea node_modules -dist \ No newline at end of file +dist +yarn-error.log diff --git a/config/default.ts b/config/default.ts index 3bf3500..083afae 100644 --- a/config/default.ts +++ b/config/default.ts @@ -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", diff --git a/package.json b/package.json index 6669c6c..ee7545c 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/components/AutoUpdateComponent.ts b/src/components/AutoUpdateComponent.ts new file mode 100644 index 0000000..f65f171 --- /dev/null +++ b/src/components/AutoUpdateComponent.ts @@ -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 { + public async start(app: Express, router: Router): Promise { + router.post(ROUTE, (req, res, next) => { + const token = req.header('X-Gitlab-Token'); + if (!token || token !== config.get('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 { + } + + 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()); + } +} \ No newline at end of file