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 path = require('path');
const fs = require('fs');
const ini = require('ini');
const { ipcMain } = require('electron')
const { app } = require('electron')
@ -72,7 +73,7 @@ async function findLocalServerPath(baseDirectory) {
return;
}
function getServerArguments(server, overrides) {
function getServerArguments(server, overrides, configPath) {
let serverArguments = [];
if(server.host) {
serverArguments.push('--host');
@ -82,6 +83,14 @@ function getServerArguments(server, overrides) {
serverArguments.push('--port');
serverArguments.push(server.port);
}
serverArguments.push('--local');
if(configPath) {
serverArguments.push('--config');
serverArguments.push(configPath);
}
return serverArguments;
}
@ -153,6 +162,31 @@ async function stop(serverName) {
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) {
if(!options) {
options = {};
@ -161,9 +195,14 @@ async function run(server, options) {
const logStdout = options.logStdout || false;
const logSterr = options.logSterr || false;
console.log(`Configuring`)
const configPath = await getIniFile(server);
await configure(configPath, server);
console.log(`Running '${server.path}'`);
let serverProcess = spawn(server.path, getServerArguments(server));
let serverProcess = spawn(server.path, getServerArguments(server, {}, configPath));
notifyStatus({
serverName: server.name,