mirror of
https://github.com/GNS3/gns3-web-ui.git
synced 2025-06-16 22:08:08 +00:00
Starting and stopping gns3server in local-server.js manager
This commit is contained in:
@ -1,39 +1,71 @@
|
||||
const { spawn } = require('child_process');
|
||||
const kill = require('tree-kill');
|
||||
|
||||
servers = [
|
||||
{
|
||||
name: 'my-local',
|
||||
path: 'c:\\Program Files\\GNS3\\gns3server.EXE'
|
||||
}
|
||||
]
|
||||
let runningServers = {};
|
||||
|
||||
function getServerArguments(server, overrides) {
|
||||
let serverArguments = [];
|
||||
return serverArguments;
|
||||
}
|
||||
|
||||
async function stopAll() {
|
||||
Object.keys(runningServers).forEach(async (serverName) => {
|
||||
await stop(serverName);
|
||||
});
|
||||
}
|
||||
|
||||
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}'`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function run(server, options) {
|
||||
const logStdout = options.logStdout || false;
|
||||
|
||||
async function run(server) {
|
||||
console.log(`Running '${server.path}'`);
|
||||
const process = spawn(server.path);
|
||||
|
||||
let serverProcess = spawn(server.path, getServerArguments(server));
|
||||
|
||||
process.on('exit', () => {
|
||||
console.log(`Process has exited`);
|
||||
runningServers[server.name] = {
|
||||
process: serverProcess
|
||||
};
|
||||
|
||||
serverProcess.stdout.on('data', function(data) {
|
||||
if(logStdout) {
|
||||
console.log(data.toString());
|
||||
}
|
||||
});
|
||||
|
||||
process.on('close', (code) => {
|
||||
console.log(`child process exited with code ${code}`);
|
||||
});
|
||||
|
||||
process.stdout.on('data', (data) => {
|
||||
console.log(`stdout: ${data}`);
|
||||
});
|
||||
|
||||
process.stderr.on('data', (data) => {
|
||||
console.log(`stderr: ${data}`);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await run(servers[0]);
|
||||
await run({
|
||||
name: 'my-local',
|
||||
path: 'c:\\Program Files\\GNS3\\gns3server.EXE',
|
||||
port: 3080
|
||||
}, {
|
||||
logStdout: true
|
||||
});
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
process.on('SIGINT', function() {
|
||||
console.log("Caught interrupt signal");
|
||||
stopAll();
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.log(`UnhandledRejection occured 'reason'`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
main();
|
||||
}
|
Reference in New Issue
Block a user