diff --git a/config.json b/config.json deleted file mode 100644 index 2fe8b6c..0000000 --- a/config.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "edexGuiOnly": true, - "iguanaGuiOnly": false, - "manualIguanaStart": false, - "skipBasiliskNetworkCheck": false -} \ No newline at end of file diff --git a/main.js b/main.js index ac7c897..8aa06f2 100644 --- a/main.js +++ b/main.js @@ -10,8 +10,10 @@ const path = require('path'), url = require('url'), os = require('os'), spawn = require('child_process').spawn, - exec = require('child_process').exec; + exec = require('child_process').exec, + fixPath = require('fix-path'); var fs = require('fs'), + fsnode = require('fs'), fs = require('fs-extra'), mkdirp = require('mkdirp'), pm2 = require('pm2'); @@ -20,18 +22,41 @@ var iguanaAppPort = 17777; Promise = require('bluebird'); -var appConfig = JSON.parse(fs.readFileSync('config.json', 'utf8')); - app.setName('Iguana'); if (os.platform() === 'linux') { process.env.ELECTRON_RUN_AS_NODE = true; console.log(process.env); } +if (os.platform() === 'darwin') { + fixPath(); + var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana'), + iguanaDir = process.env.HOME + '/Library/Application Support/iguana'; +} + +if (os.platform() === 'linux') { + var iguanaBin = path.join(__dirname, '../assets/bin/linux64/iguana'), + iguanaDir = process.env.HOME + '/.iguana'; +} + +if (os.platform() === 'win32') { + var iguanaDir = process.env.APPDATA + '/iguana'; + iguanaDir = path.normalize(iguanaDir); +} + +var appSettings = { + "edexGuiOnly": true, + "iguanaGuiOnly": false, + "manualIguanaStart": false, + "skipBasiliskNetworkCheck": false +}; + +var appConfig = appSettings; // preload.js const _setImmediate = setImmediate, _clearImmediate = clearImmediate; + process.once('loaded', () => { global.setImmediate = _setImmediate; global.clearImmediate = _clearImmediate; @@ -48,12 +73,14 @@ process.once('loaded', () => { var shepherd = require('./routes/shepherd'), guiapp = express(); +shepherd.saveLocalAppConf(appSettings); + guiapp.use(bodyParser.json()); // support json encoded bodies guiapp.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies guiapp.get('/', function (req, res) { res.send('Iguana app server'); -}) +}); var guipath = path.join(__dirname, '/gui'); guiapp.use('/gui', express.static(guipath)); diff --git a/routes/shepherd.js b/routes/shepherd.js index 82cffc8..9c411a1 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -65,8 +65,7 @@ shepherd.get('/', function(req, res, next) { }); shepherd.get('/appconf', function(req, res, next) { - saveLocalAppConf(); - var obj = JSON.parse(fs.readFileSync('config.json', 'utf8')); + var obj = JSON.parse(fs.readFileSync(iguanaDir + '/config.json', 'utf8')); res.send(obj); }); @@ -93,13 +92,13 @@ shepherd.post('/herdlist', function(req, res) { if (err) throw err; // TODO: proper error handling pm2.describe(req.body.herdname, function(err, list) { pm2.disconnect(); // disconnect after getting proc info list - + if (err) throw err; // TODO: proper error handling - + console.log(list[0].pm2_env.status) // print status of IGUANA proc console.log(list[0].pid) // print pid of IGUANA proc - + res.end('{ "herdname": ' + req.body.herdname + ', "status": ' + list[0].pm2_env.status + ', "pid": ' + list[0].pid + '}'); }); }); @@ -151,7 +150,7 @@ function herder(flock, data) { console.log('selected data: ' + data); //Make sure iguana isn't running before starting new process, kill it dammit! - // A simple pid lookup + // A simple pid lookup /*ps.lookup({ command: 'iguana', //arguments: '--debug', @@ -163,7 +162,7 @@ function herder(flock, data) { if( process ){ console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments ); console.log(process.pid); - // A simple pid lookup + // A simple pid lookup ps.kill( process.pid, function( err ) { if (err) { throw new Error( err ); @@ -192,7 +191,7 @@ function herder(flock, data) { fs.copy(iguanaConfsDirSrc, iguanaConfsDir, function (err) { if (err) return console.error(err); - + console.log('confs files copied successfully at: ' + iguanaConfsDir); }); @@ -274,27 +273,61 @@ function slayer(flock) { }); } -function saveLocalAppConf() { +shepherd.saveLocalAppConf = function(appSettings) { var appConfFileName = iguanaDir + '/conf.json'; console.log(iguanaDir); - var fsWrite = function() { + var CheckFileExists = function() { + return new Promise(function(resolve, reject) { + var result = 'Check app conf.json file exists is done' + + fs.ensureFile(appConfFileName, function(err) { + console.log(err); // => null + }); + + setTimeout(function() { + console.log(result); + resolve(result); + }, 2000); + }); + } + + var FixFilePermissions = function() { + return new Promise(function(resolve, reject) { + var result = 'conf.json file permissions updated to Read/Write'; + + fsnode.chmodSync(appConfFileName, '0666'); + + setTimeout(function() { + console.log(result); + resolve(result); + }, 1000); + }); + } + + var FsWrite = function() { return new Promise(function(resolve, reject) { var result = 'write file is done' - fs.writeFile(appConfFileName, 'test', 'utf8', function(err) { - if (err) - return console.log(err); - }); + fs.writeFile(appConfFileName, JSON.stringify(appSettings), 'utf8', function(err) { + if (err) + return console.log(err); + }); fsnode.chmodSync(appConfFileName, '0666'); setTimeout(function() { console.log(result); + console.log('app conf.json file is copied successfully at: ' + iguanaConfsDir); resolve(result); }, 2000); }); } + CheckFileExists() + .then(function(result) { + return FixFilePermissions(); + }) + .then(FsWrite); } function setConf(flock) { @@ -361,7 +394,7 @@ function setConf(flock) { } var rmlines = data.replace(/(?:(?:\r\n|\r|\n)\s*){2}/gm, '\n'); - + fs.writeFile(DaemonConfPath, rmlines, 'utf8', function(err) { if (err) return console.log(err); @@ -456,7 +489,7 @@ function setConf(flock) { console.log('addnode: OK'); } else { console.log('addnode: NOT FOUND') - fs.appendFile(DaemonConfPath, + fs.appendFile(DaemonConfPath, '\naddnode=78.47.196.146' + '\naddnode=5.9.102.210' + '\naddnode=178.63.69.164' +