2019-03-02 15:19:08 +00:00
|
|
|
import Chokidar from 'chokidar';
|
|
|
|
import fs from 'fs';
|
|
|
|
import ChildProcess from 'child_process';
|
|
|
|
import kill from 'tree-kill';
|
|
|
|
import AutheliaServerInterface from './AutheliaServerInterface';
|
2019-03-03 22:51:52 +00:00
|
|
|
import sleep from '../utils/sleep';
|
2019-03-02 15:19:08 +00:00
|
|
|
|
|
|
|
class AutheliaServerWithHotReload implements AutheliaServerInterface {
|
|
|
|
private watcher: Chokidar.FSWatcher;
|
|
|
|
private configPath: string;
|
|
|
|
private AUTHELIA_INTERRUPT_FILENAME = '.authelia-interrupt';
|
|
|
|
private serverProcess: ChildProcess.ChildProcess | undefined;
|
|
|
|
private clientProcess: ChildProcess.ChildProcess | undefined;
|
2019-03-03 22:51:52 +00:00
|
|
|
private filesChangedBuffer: string[] = [];
|
|
|
|
private changeInProgress: boolean = false;
|
2019-04-24 21:52:08 +00:00
|
|
|
private isInterrupted: boolean = false
|
2019-03-02 15:19:08 +00:00
|
|
|
|
2019-03-23 14:44:46 +00:00
|
|
|
constructor(configPath: string, watchedPaths: string[]) {
|
2019-03-02 15:19:08 +00:00
|
|
|
this.configPath = configPath;
|
2019-04-24 21:52:08 +00:00
|
|
|
const pathsToReload = ['**/*.go',
|
2019-03-24 14:15:49 +00:00
|
|
|
this.AUTHELIA_INTERRUPT_FILENAME, configPath].concat(watchedPaths);
|
|
|
|
console.log("Authelia will reload on changes of files or directories in " + pathsToReload.join(', '));
|
|
|
|
this.watcher = Chokidar.watch(pathsToReload, {
|
2019-03-02 15:19:08 +00:00
|
|
|
persistent: true,
|
|
|
|
ignoreInitial: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async startServer() {
|
2019-03-03 22:51:52 +00:00
|
|
|
if (this.serverProcess) return;
|
2019-04-24 21:52:08 +00:00
|
|
|
this.serverProcess = ChildProcess.spawn('go',
|
|
|
|
['run', 'main.go', '-config', this.configPath], {
|
2019-03-24 14:15:49 +00:00
|
|
|
env: {
|
|
|
|
...process.env,
|
2019-04-24 21:52:08 +00:00
|
|
|
NODE_TLS_REJECT_UNAUTHORIZED: "0",
|
2019-03-24 14:15:49 +00:00
|
|
|
},
|
2019-03-02 15:19:08 +00:00
|
|
|
});
|
2019-04-24 21:52:08 +00:00
|
|
|
if (!this.serverProcess || !this.serverProcess.stdout || !this.serverProcess.stderr) return;
|
2019-03-02 15:19:08 +00:00
|
|
|
this.serverProcess.stdout.pipe(process.stdout);
|
|
|
|
this.serverProcess.stderr.pipe(process.stderr);
|
|
|
|
this.serverProcess.on('exit', () => {
|
|
|
|
if (!this.serverProcess) return;
|
2019-03-03 22:51:52 +00:00
|
|
|
console.log('Authelia server with pid=%s exited.', this.serverProcess.pid);
|
2019-03-02 15:19:08 +00:00
|
|
|
this.serverProcess.removeAllListeners();
|
|
|
|
this.serverProcess = undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private killServer() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (this.serverProcess) {
|
2019-03-03 22:51:52 +00:00
|
|
|
const pid = this.serverProcess.pid;
|
2019-03-02 15:19:08 +00:00
|
|
|
try {
|
|
|
|
const timeout = setTimeout(
|
2019-03-03 22:51:52 +00:00
|
|
|
() => reject(new Error(`Server with pid=${pid} not killed after timeout.`)), 10000);
|
2019-03-02 15:19:08 +00:00
|
|
|
this.serverProcess.on('exit', () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
kill(this.serverProcess.pid, 'SIGKILL');
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async startClient() {
|
2019-03-03 22:51:52 +00:00
|
|
|
if (this.clientProcess) return;
|
|
|
|
|
2019-03-02 15:19:08 +00:00
|
|
|
this.clientProcess = ChildProcess.spawn('npm', ['run', 'start'], {
|
|
|
|
cwd: './client',
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
'BROWSER': 'none'
|
|
|
|
}
|
|
|
|
});
|
2019-04-24 21:52:08 +00:00
|
|
|
if (!this.clientProcess || !this.clientProcess.stdout || !this.clientProcess.stderr) return;
|
2019-03-02 15:19:08 +00:00
|
|
|
this.clientProcess.stdout.pipe(process.stdout);
|
|
|
|
this.clientProcess.stderr.pipe(process.stderr);
|
|
|
|
this.clientProcess.on('exit', () => {
|
|
|
|
if (!this.clientProcess) return;
|
2019-03-03 22:51:52 +00:00
|
|
|
console.log('Authelia client exited with pid=%s.', this.clientProcess.pid);
|
2019-03-02 15:19:08 +00:00
|
|
|
this.clientProcess.removeAllListeners();
|
|
|
|
this.clientProcess = undefined;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private killClient() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (this.clientProcess) {
|
2019-03-03 22:51:52 +00:00
|
|
|
const pid = this.clientProcess.pid;
|
2019-03-02 15:19:08 +00:00
|
|
|
try {
|
|
|
|
const timeout = setTimeout(
|
2019-03-03 22:51:52 +00:00
|
|
|
() => reject(new Error(`Server with pid=${pid} not killed after timeout.`)), 10000);
|
2019-03-02 15:19:08 +00:00
|
|
|
this.clientProcess.on('exit', () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
kill(this.clientProcess.pid, 'SIGKILL');
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle file changes.
|
|
|
|
* @param path The path of the file that has been changed.
|
|
|
|
*/
|
2019-04-24 21:52:08 +00:00
|
|
|
private onFilesChanged = async (paths: string[]) => {
|
|
|
|
const interruptFileExist = fs.existsSync(this.AUTHELIA_INTERRUPT_FILENAME);
|
|
|
|
const interruptFileModified = paths.filter(
|
2019-03-03 22:51:52 +00:00
|
|
|
(p) => p === this.AUTHELIA_INTERRUPT_FILENAME).length > 0;
|
2019-04-24 21:52:08 +00:00
|
|
|
if (interruptFileExist) {
|
|
|
|
if (interruptFileModified) {
|
|
|
|
console.log('Authelia is being interrupted.');
|
|
|
|
this.isInterrupted = true;
|
|
|
|
await this.killServer();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (this.isInterrupted && interruptFileModified && !interruptFileExist){
|
2019-03-02 15:19:08 +00:00
|
|
|
console.log('Authelia is restarting.');
|
|
|
|
await this.startServer();
|
2019-04-24 21:52:08 +00:00
|
|
|
this.isInterrupted = false;
|
|
|
|
return;
|
2019-03-02 15:19:08 +00:00
|
|
|
}
|
2019-03-03 22:51:52 +00:00
|
|
|
|
2019-03-02 15:19:08 +00:00
|
|
|
await this.killServer();
|
|
|
|
await this.startServer();
|
2019-03-03 22:51:52 +00:00
|
|
|
|
|
|
|
if (this.filesChangedBuffer.length > 0) {
|
|
|
|
await this.consumeFileChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async consumeFileChanged() {
|
|
|
|
this.changeInProgress = true;
|
|
|
|
const paths = this.filesChangedBuffer;
|
|
|
|
this.filesChangedBuffer = [];
|
|
|
|
try {
|
|
|
|
await this.onFilesChanged(paths);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
this.changeInProgress = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private enqueueFileChanged(path: string) {
|
|
|
|
console.log(`File ${path} has been changed, reloading...`);
|
|
|
|
this.filesChangedBuffer.push(path);
|
|
|
|
if (this.changeInProgress) return;
|
|
|
|
this.consumeFileChanged();
|
2019-03-02 15:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
if (fs.existsSync(this.AUTHELIA_INTERRUPT_FILENAME)) {
|
|
|
|
console.log('Authelia is interrupted. Consider removing ' + this.AUTHELIA_INTERRUPT_FILENAME + ' if it\'s not expected.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Start watching file changes...');
|
2019-03-03 22:51:52 +00:00
|
|
|
this.watcher.on('add', (p) => this.enqueueFileChanged(p));
|
|
|
|
this.watcher.on('unlink', (p) => this.enqueueFileChanged(p));
|
|
|
|
this.watcher.on('change', (p) => this.enqueueFileChanged(p));
|
2019-03-02 15:19:08 +00:00
|
|
|
|
|
|
|
this.startClient();
|
|
|
|
this.startServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {
|
|
|
|
await this.killClient();
|
|
|
|
await this.killServer();
|
2019-03-03 22:51:52 +00:00
|
|
|
await sleep(2000);
|
2019-03-02 15:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AutheliaServerWithHotReload;
|