2018-08-10 03:24:02 +07:00
|
|
|
const { exec } = require('child_process');
|
|
|
|
import Bluebird = require("bluebird");
|
|
|
|
|
|
|
|
function docker_compose(includes: string[]) {
|
|
|
|
const compose_args = includes.map((dc: string) => `-f ${dc}`).join(' ');
|
|
|
|
return `docker-compose ${compose_args}`;
|
|
|
|
}
|
|
|
|
|
2018-08-19 21:51:36 +07:00
|
|
|
export class Environment {
|
|
|
|
private includes: string[];
|
|
|
|
constructor(includes: string[]) {
|
|
|
|
this.includes = includes;
|
|
|
|
}
|
2018-08-10 03:24:02 +07:00
|
|
|
|
2018-08-19 21:51:36 +07:00
|
|
|
private runCommand(command: string, timeout?: number): Bluebird<void> {
|
2018-10-22 14:35:06 +07:00
|
|
|
return new Bluebird<void>((resolve, reject) => {
|
2018-08-19 21:51:36 +07:00
|
|
|
console.log('[ENVIRONMENT] Running: %s', command);
|
2018-10-22 14:35:06 +07:00
|
|
|
exec(command, (err, stdout, stderr) => {
|
2018-08-19 21:51:36 +07:00
|
|
|
if(err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!timeout) resolve();
|
|
|
|
else setTimeout(resolve, timeout);
|
|
|
|
});
|
2018-08-10 03:24:02 +07:00
|
|
|
});
|
2018-08-19 21:51:36 +07:00
|
|
|
}
|
|
|
|
|
2018-08-10 03:24:02 +07:00
|
|
|
|
2018-08-19 21:51:36 +07:00
|
|
|
setup(timeout?: number): Bluebird<void> {
|
|
|
|
const command = docker_compose(this.includes) + ' up -d'
|
|
|
|
console.log('[ENVIRONMENT] Starting up...');
|
|
|
|
return this.runCommand(command, timeout);
|
|
|
|
}
|
2018-08-10 03:24:02 +07:00
|
|
|
|
2018-08-19 21:51:36 +07:00
|
|
|
cleanup(): Bluebird<void> {
|
2018-10-22 14:35:06 +07:00
|
|
|
if(process.env.KEEP_ENV != "true") {
|
|
|
|
const command = docker_compose(this.includes) + ' down'
|
|
|
|
console.log('[ENVIRONMENT] Cleaning up...');
|
|
|
|
return this.runCommand(command);
|
|
|
|
}
|
|
|
|
return Bluebird.resolve();
|
2018-08-19 21:51:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_service(serviceName: string): Bluebird<void> {
|
|
|
|
const command = docker_compose(this.includes) + ' stop ' + serviceName;
|
|
|
|
console.log('[ENVIRONMENT] Stopping service %s...', serviceName);
|
|
|
|
return this.runCommand(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
start_service(serviceName: string): Bluebird<void> {
|
|
|
|
const command = docker_compose(this.includes) + ' start ' + serviceName;
|
|
|
|
console.log('[ENVIRONMENT] Starting service %s...', serviceName);
|
|
|
|
return this.runCommand(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
restart_service(serviceName: string, timeout?: number): Bluebird<void> {
|
|
|
|
const command = docker_compose(this.includes) + ' restart ' + serviceName;
|
|
|
|
console.log('[ENVIRONMENT] Restarting service %s...', serviceName);
|
|
|
|
return this.runCommand(command, timeout);
|
|
|
|
}
|
2018-08-10 03:24:02 +07:00
|
|
|
}
|