2020-07-11 11:46:16 +02:00
|
|
|
import {Router} from "express";
|
2020-05-05 15:19:23 +02:00
|
|
|
import config from "config";
|
|
|
|
import * as child_process from "child_process";
|
|
|
|
import ApplicationComponent from "../ApplicationComponent";
|
|
|
|
import {ForbiddenHttpError} from "../HttpError";
|
|
|
|
import Logger from "../Logger";
|
|
|
|
|
2020-09-25 22:03:22 +02:00
|
|
|
export default class AutoUpdateComponent extends ApplicationComponent {
|
2020-09-25 23:42:15 +02:00
|
|
|
private static async runCommand(command: string): Promise<void> {
|
|
|
|
Logger.info(`> ${command}`);
|
|
|
|
Logger.info(child_process.execSync(command).toString());
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:06:13 +02:00
|
|
|
public async checkSecuritySettings(): Promise<void> {
|
|
|
|
this.checkSecurityConfigField('gitlab_webhook_token');
|
|
|
|
}
|
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
public async init(router: Router): Promise<void> {
|
|
|
|
router.post('/update/push.json', (req, res) => {
|
2020-05-05 15:19:23 +02:00
|
|
|
const token = req.header('X-Gitlab-Token');
|
2020-09-25 23:42:15 +02:00
|
|
|
if (!token || token !== config.get<string>('gitlab_webhook_token'))
|
|
|
|
throw new ForbiddenHttpError('Invalid token', req.url);
|
2020-05-05 15:19:23 +02:00
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
this.update(req.body).catch(Logger.error);
|
2020-05-05 15:19:23 +02:00
|
|
|
|
|
|
|
res.json({
|
|
|
|
'status': 'ok',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
private async update(params: { [p: string]: unknown }) {
|
2020-07-11 11:46:16 +02:00
|
|
|
Logger.info('Update params:', params);
|
2020-05-05 15:19:23 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
Logger.info('Starting auto update...');
|
|
|
|
|
2020-06-15 12:58:27 +02:00
|
|
|
// Fetch
|
2020-09-25 23:42:15 +02:00
|
|
|
await AutoUpdateComponent.runCommand(`git pull`);
|
2020-05-05 15:19:23 +02:00
|
|
|
|
|
|
|
// Install new dependencies
|
2020-09-25 23:42:15 +02:00
|
|
|
await AutoUpdateComponent.runCommand(`yarn install --production=false`);
|
2020-05-05 15:19:23 +02:00
|
|
|
|
|
|
|
// Process assets
|
2020-09-25 23:42:15 +02:00
|
|
|
await AutoUpdateComponent.runCommand(`yarn dist`);
|
2020-05-05 15:19:23 +02:00
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
// Stop app
|
2020-09-25 22:03:22 +02:00
|
|
|
await this.getApp().stop();
|
2020-07-11 11:46:16 +02:00
|
|
|
|
2020-05-05 15:19:23 +02:00
|
|
|
Logger.info('Success!');
|
|
|
|
} catch (e) {
|
|
|
|
Logger.error(e, 'An error occurred while running the auto update.');
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|