Support gns3_server.ini configuration, Fixes: #324

This commit is contained in:
ziajka 2019-03-05 09:06:15 +01:00
parent 7ccc9db33b
commit 24e9e23f7e
3 changed files with 45 additions and 3 deletions

View File

@ -2,6 +2,7 @@ const { spawn } = require('child_process');
const kill = require('tree-kill'); const kill = require('tree-kill');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const ini = require('ini');
const { ipcMain } = require('electron') const { ipcMain } = require('electron')
const { app } = require('electron') const { app } = require('electron')
@ -72,7 +73,7 @@ async function findLocalServerPath(baseDirectory) {
return; return;
} }
function getServerArguments(server, overrides) { function getServerArguments(server, overrides, configPath) {
let serverArguments = []; let serverArguments = [];
if(server.host) { if(server.host) {
serverArguments.push('--host'); serverArguments.push('--host');
@ -82,6 +83,14 @@ function getServerArguments(server, overrides) {
serverArguments.push('--port'); serverArguments.push('--port');
serverArguments.push(server.port); serverArguments.push(server.port);
} }
serverArguments.push('--local');
if(configPath) {
serverArguments.push('--config');
serverArguments.push(configPath);
}
return serverArguments; return serverArguments;
} }
@ -153,6 +162,31 @@ async function stop(serverName) {
return stopped; return stopped;
} }
async function getIniFile(server) {
return path.join(app.getPath('userData'), `gns3_server_${server.id}.ini`);
}
async function configure(configPath, server) {
if(!fs.existsSync(configPath)) {
fs.closeSync(fs.openSync(configPath, 'w'));
console.log(`Configuration file '${configPath}' has been created.`);
}
var config = ini.parse(fs.readFileSync(configPath, 'utf-8'));
if(server.path) {
config.path = server.path;
}
if(server.host) {
config.host = server.host;
}
if(server.port) {
config.port = server.port;
}
fs.writeFileSync(configPath, ini.stringify(config, { section: 'Server' }));
}
async function run(server, options) { async function run(server, options) {
if(!options) { if(!options) {
options = {}; options = {};
@ -161,9 +195,14 @@ async function run(server, options) {
const logStdout = options.logStdout || false; const logStdout = options.logStdout || false;
const logSterr = options.logSterr || false; const logSterr = options.logSterr || false;
console.log(`Configuring`)
const configPath = await getIniFile(server);
await configure(configPath, server);
console.log(`Running '${server.path}'`); console.log(`Running '${server.path}'`);
let serverProcess = spawn(server.path, getServerArguments(server)); let serverProcess = spawn(server.path, getServerArguments(server, {}, configPath));
notifyStatus({ notifyStatus({
serverName: server.name, serverName: server.name,

View File

@ -58,6 +58,7 @@
"css-tree": "^1.0.0-alpha.29", "css-tree": "^1.0.0-alpha.29",
"d3-ng2-service": "^2.1.0", "d3-ng2-service": "^2.1.0",
"hammerjs": "^2.0.8", "hammerjs": "^2.0.8",
"ini": "^1.3.5",
"material-design-icons": "^3.0.1", "material-design-icons": "^3.0.1",
"ng2-file-upload": "^1.3.0", "ng2-file-upload": "^1.3.0",
"ngx-electron": "^2.1.1", "ngx-electron": "^2.1.1",
@ -66,9 +67,9 @@
"raven-js": "^3.27.0", "raven-js": "^3.27.0",
"rxjs": "^6.4.0", "rxjs": "^6.4.0",
"rxjs-compat": "^6.4.0", "rxjs-compat": "^6.4.0",
"tree-kill": "^1.2.1",
"typeface-roboto": "^0.0.54", "typeface-roboto": "^0.0.54",
"yargs": "^13.2.1", "yargs": "^13.2.1",
"tree-kill": "^1.2.1",
"zone.js": "^0.8.29" "zone.js": "^0.8.29"
}, },
"devDependencies": { "devDependencies": {

View File

@ -38,9 +38,11 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy {
// attach to notification stream when any of running local servers experienced issues // attach to notification stream when any of running local servers experienced issues
this.serverStatusSubscription = this.serverManagement.serverStatusChanged.subscribe((serverStatus) => { this.serverStatusSubscription = this.serverManagement.serverStatusChanged.subscribe((serverStatus) => {
if(serverStatus.status === 'errored') { if(serverStatus.status === 'errored') {
console.error(serverStatus.message);
this.toasterService.error(serverStatus.message); this.toasterService.error(serverStatus.message);
} }
if(serverStatus.status === 'stderr') { if(serverStatus.status === 'stderr') {
console.error(serverStatus.message);
this.toasterService.error(serverStatus.message); this.toasterService.error(serverStatus.message);
} }
}); });