2019-02-06 08:12:14 +00:00
|
|
|
const { spawn } = require('child_process');
|
2019-02-07 12:35:07 +00:00
|
|
|
const kill = require('tree-kill');
|
2019-02-06 08:12:14 +00:00
|
|
|
|
2019-02-07 12:35:07 +00:00
|
|
|
let runningServers = {};
|
2019-02-06 08:12:14 +00:00
|
|
|
|
2019-02-07 12:35:07 +00:00
|
|
|
function getServerArguments(server, overrides) {
|
|
|
|
let serverArguments = [];
|
|
|
|
return serverArguments;
|
|
|
|
}
|
2019-02-06 08:12:14 +00:00
|
|
|
|
2019-02-07 12:35:07 +00:00
|
|
|
async function stopAll() {
|
|
|
|
Object.keys(runningServers).forEach(async (serverName) => {
|
|
|
|
await stop(serverName);
|
2019-02-06 08:12:14 +00:00
|
|
|
});
|
2019-02-07 12:35:07 +00:00
|
|
|
}
|
2019-02-06 08:12:14 +00:00
|
|
|
|
2019-02-07 12:35:07 +00:00
|
|
|
async function stop(serverName) {
|
|
|
|
const runningServer = runningServers[serverName];
|
|
|
|
const pid = runningServer.process.pid;
|
|
|
|
console.log(`Stopping '${serverName}' with PID='${pid}'`);
|
|
|
|
kill(pid, (error) => {
|
|
|
|
if(error) {
|
|
|
|
console.error(`Error occured during stopping '${serverName}' with PID='${pid}'`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(`Stopped '${serverName}' with PID='${pid}'`);
|
|
|
|
}
|
2019-02-06 08:12:14 +00:00
|
|
|
});
|
2019-02-07 12:35:07 +00:00
|
|
|
}
|
2019-02-06 08:12:14 +00:00
|
|
|
|
2019-02-07 12:35:07 +00:00
|
|
|
async function run(server, options) {
|
|
|
|
const logStdout = options.logStdout || false;
|
2019-02-06 08:12:14 +00:00
|
|
|
|
2019-02-07 12:35:07 +00:00
|
|
|
console.log(`Running '${server.path}'`);
|
|
|
|
|
|
|
|
let serverProcess = spawn(server.path, getServerArguments(server));
|
2019-02-06 08:12:14 +00:00
|
|
|
|
2019-02-07 12:35:07 +00:00
|
|
|
runningServers[server.name] = {
|
|
|
|
process: serverProcess
|
|
|
|
};
|
|
|
|
|
|
|
|
serverProcess.stdout.on('data', function(data) {
|
|
|
|
if(logStdout) {
|
|
|
|
console.log(data.toString());
|
|
|
|
}
|
|
|
|
});
|
2019-02-06 08:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2019-02-07 12:35:07 +00:00
|
|
|
await run({
|
|
|
|
name: 'my-local',
|
|
|
|
path: 'c:\\Program Files\\GNS3\\gns3server.EXE',
|
|
|
|
port: 3080
|
|
|
|
}, {
|
|
|
|
logStdout: true
|
|
|
|
});
|
2019-02-06 08:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (require.main === module) {
|
2019-02-07 12:35:07 +00:00
|
|
|
process.on('SIGINT', function() {
|
|
|
|
console.log("Caught interrupt signal");
|
|
|
|
stopAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, promise) => {
|
|
|
|
console.log(`UnhandledRejection occured 'reason'`);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2019-02-06 08:12:14 +00:00
|
|
|
main();
|
|
|
|
}
|