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/gui/index.html b/gui/index.html index 4d1d9ab..9630b28 100644 --- a/gui/index.html +++ b/gui/index.html @@ -10,10 +10,6 @@ - >
diff --git a/gui/init.js b/gui/init.js index 5c56b0a..a607491 100644 --- a/gui/init.js +++ b/gui/init.js @@ -1,4 +1,7 @@ $(document).ready(function() { + const remote = require('electron').remote; + var window = remote.getCurrentWindow(); + $('#pulse').jRoll({ radius: 100, animation: 'pulse' diff --git a/gui/loading.js b/gui/loading.js index fc80b82..d61547d 100644 --- a/gui/loading.js +++ b/gui/loading.js @@ -92,7 +92,7 @@ function EDEX_DEXnotarychains() { 'agent': 'dpow', 'method': 'notarychains' }, - AjaxOutputData = IguanaAJAX('http://127.0.0.1:7778',ajax_data).done(function(data) { + AjaxOutputData = IguanaAJAX('http://127.0.0.1:7778', ajax_data).done(function(data) { //console.log(AjaxOutputData.responseText); AjaxOutputData = JSON.parse(AjaxOutputData.responseText); //console.log(AjaxOutputData); @@ -131,17 +131,17 @@ function EDEX_DEXgetinfoAll() { 'symbol': coin_value }, basiliskMinNotariesConnected = 10; - + console.log(ajax_data); if (coin_value !== 'MESH' || coin_value !== 'CEAL') { var getinfo_each_chain = IguanaAJAX('http://127.0.0.1:7778', ajax_data).done(function(data) { getinfo_each_chain = JSON.parse(getinfo_each_chain.responseText); console.log(getinfo_each_chain); - + tmp_index = parseInt(coin_index) + 1; $('#loading_sub_status_text').text('Connection status... ' + tmp_index + '/' + get_dex_notarychains.length + ': ' + coin_value); - + if (getinfo_each_chain.error === 'less than required responses') { $('#loading_sub_status_output_text').text('Output: ' + getinfo_each_chain.error); } else { @@ -151,7 +151,7 @@ function EDEX_DEXgetinfoAll() { if ( tmp_index == 10 ) { console.log('min notaries connected'); const remote = require('electron').remote; - var window = remote.getCurrentWindow(); + var window = remote.getCurrentWindow(); window.hide(); } }).fail(function(xhr, textStatus, error) { diff --git a/main.js b/main.js index ac7c897..d38a689 100644 --- a/main.js +++ b/main.js @@ -3,25 +3,23 @@ const electron = require('electron'), app = electron.app, - BrowserWindow = electron.BrowserWindow; -var express = require('express'), - bodyParser = require('body-parser'); -const path = require('path'), + BrowserWindow = electron.BrowserWindow, + path = require('path'), url = require('url'), os = require('os'), spawn = require('child_process').spawn, - exec = require('child_process').exec; -var fs = require('fs'), + exec = require('child_process').exec, + fixPath = require('fix-path'); +var express = require('express'), + bodyParser = require('body-parser'), + fs = require('fs'), + fsnode = require('fs'), fs = require('fs-extra'), mkdirp = require('mkdirp'), - pm2 = require('pm2'); - -var iguanaAppPort = 17777; + pm2 = require('pm2'); Promise = require('bluebird'); -var appConfig = JSON.parse(fs.readFileSync('config.json', 'utf8')); - app.setName('Iguana'); if (os.platform() === 'linux') { @@ -29,45 +27,47 @@ if (os.platform() === 'linux') { console.log(process.env); } +// GUI APP settings and starting gui on address http://120.0.0.1:17777 +var shepherd = require('./routes/shepherd'), + guiapp = express(); + +var appConfig = shepherd.loadLocalConfig(); // load app config + // preload.js const _setImmediate = setImmediate, _clearImmediate = clearImmediate; + process.once('loaded', () => { global.setImmediate = _setImmediate; global.clearImmediate = _clearImmediate; if (os.platform() === 'darwin') { - process.setFdLimit(90000); + process.setFdLimit(appConfig.maxDescriptors.darwin); } if (os.platform() === 'linux') { - process.setFdLimit(1000000); + process.setFdLimit(appConfig.maxDescriptors.linux); } }); -// GUI APP settings and starting gui on address http://120.0.0.1:17777 -var shepherd = require('./routes/shepherd'), - guiapp = express(); - 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)); guiapp.use('/shepherd', shepherd); -var rungui = guiapp.listen(iguanaAppPort, function () { - console.log('guiapp listening on port ' + iguanaAppPort + '!'); +var rungui = guiapp.listen(appConfig.iguanaAppPort, function () { + console.log('guiapp listening on port ' + appConfig.iguanaAppPort + '!'); }) module.exports = guiapp; // END GUI App Settings - //require('./assets/js/iguana.js'); //below code shall be separated into asset js for public version /* // SELECTING IGUANA BUILD TO RUN AS PER OS DETECTED BY DESKTOP APP @@ -138,7 +138,7 @@ function createLoadingWindow() { }); // load our index.html (i.e. easyDEX GUI) - loadingWindow.loadURL('http://127.0.0.1:17777/gui/'); + loadingWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/'); // DEVTOOLS - only for dev purposes - ca333 //loadingWindow.webContents.openDevTools() @@ -171,7 +171,7 @@ function createLoadingWindow() { //if (os.platform() !== 'win32') { ig.stderr.on( 'error: ', data => { console.log( `stderr: ${data}` ); }); } } -app.on('ready', createLoadingWindow) +app.on('ready', createLoadingWindow); function createWindow (status) { if ( status === 'open') { @@ -187,9 +187,9 @@ function createWindow (status) { // load our index.html (i.e. easyDEX GUI) if (appConfig.edexGuiOnly) { - mainWindow.loadURL('http://127.0.0.1:' + iguanaAppPort + '/gui/EasyDEX-GUI/'); + mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/EasyDEX-GUI/'); } else { - mainWindow.loadURL('http://127.0.0.1:' + iguanaAppPort + '/gui/main.html'); + mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/main.html'); } // DEVTOOLS - only for dev purposes - ca333 @@ -201,7 +201,7 @@ function createWindow (status) { return new Promise(function(resolve, reject) { console.log('Closing Main Window...'); - pm2.connect(true,function(err) { + pm2.connect(true, function(err) { console.log('connecting to pm2...'); if (err) { @@ -223,6 +223,7 @@ function createWindow (status) { pm2.killDaemon(function(err) { pm2.disconnect(); console.log('killed to pm2...'); + if (err) throw err; }); diff --git a/package.json b/package.json index dc3a701..72f95dd 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "md5": "^2.2.1", "mkdirp": "^0.5.1", "pm2": "^2.3.0", - "ps-node": "^0.1.4" + "ps-node": "^0.1.4", + "read-last-lines": "^1.1.0" } } diff --git a/pull-gui.sh b/pull-gui.sh index 60c6fbe..2d9bdbd 100755 --- a/pull-gui.sh +++ b/pull-gui.sh @@ -14,25 +14,23 @@ else git clone https://github.com/SuperNETorg/EasyDEX-GUI.git fi -if [ -d "Iguana-application" ]; then - cd Iguana-application - git checkout 0.3.1 - git branch 0.3.1 --track origin/0.3.1 - git pull -else - git clone https://github.com/SuperNETorg/Iguana-application.git -b 0.3.1 - cd Iguana-application - git branch 0.3.1 --track origin/0.3.1 -fi +#if [ -d "Iguana-application" ]; then +# cd Iguana-application +# git checkout 0.3.1 +# git pull +#else +# git clone https://github.com/SuperNETorg/Iguana-application.git -b 0.3.1 +# cd Iguana-application +#fi -bower install -npm install -gulp electron +#bower install +#npm install +#gulp electron cd ../../ rm "gui/EasyDEX-GUI" -rf -rm "gui/Iguana-GUI" -rf +#rm "gui/Iguana-GUI" -rf -cp -rf "tmp/Iguana-application/compiled/prod" "gui/Iguana-GUI" -cp -rf "tmp/EasyDEX-GUI" "gui/EasyDEX-GUI" +#cp -rf "tmp/Iguana-application/compiled/prod" "gui/Iguana-GUI" +cp -rf "tmp/EasyDEX-GUI" "gui/EasyDEX-GUI" \ No newline at end of file diff --git a/routes/shepherd.js b/routes/shepherd.js index 87474e0..a85b91a 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -10,7 +10,8 @@ const electron = require('electron'), express = require('express'), exec = require('child_process').exec, md5 = require('md5'), - pm2 = require('pm2'); + pm2 = require('pm2'), + readLastLines = require('read-last-lines'); Promise = require('bluebird'); @@ -56,18 +57,33 @@ if (os.platform() === 'win32') { iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc); } -console.log(iguanaDir); -console.log(iguanaBin); +shepherd.appConfig = { + "edexGuiOnly": true, + "iguanaGuiOnly": false, + "manualIguanaStart": false, + "skipBasiliskNetworkCheck": false, + "host": "127.0.0.1", + "iguanaAppPort": 17777, + "iguanaCorePort": 7778, + "maxDescriptors": { + "darwin": 90000, + "linux": 1000000 + } +}; + +console.log('iguana dir: ' + iguanaDir); +console.log('iguana bin: ' + iguanaBin); // END IGUANA FILES AND CONFIG SETTINGS shepherd.get('/', function(req, res, next) { - res.send('Iguana app server') -}) + res.send('Iguana app server'); +}); shepherd.get('/appconf', function(req, res, next) { - var obj = JSON.parse(fs.readFileSync('config.json', 'utf8')); + shepherd.readDebugLog(); + var obj = shepherd.loadLocalConfig(); res.send(obj); -}) +}); shepherd.post('/herd', function(req, res) { console.log('======= req.body ======='); @@ -92,12 +108,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 - + + 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 + '}'); }); }); @@ -130,11 +147,61 @@ shepherd.post('/getconf', function(req, res) { //console.log(req.body.chain); var confpath = getConf(req.body.chain); - console.log('got conf path is:') + console.log('got conf path is:'); console.log(confpath); res.end('{ "msg": "success", "result": "' + confpath + '" }'); }); +shepherd.loadLocalConfig = function() { + if (fs.existsSync(iguanaDir + '/config.json')) { + var localAppConfig = fs.readFileSync(iguanaDir + '/config.json', 'utf8'); + console.log('app config set from local file'); + + // find diff between local and hardcoded configs + // append diff to local config + var compareJSON = function(obj1, obj2) { + var result = {}; + + for (var i in obj1) { + if (!obj2.hasOwnProperty(i)) { + result[i] = obj1[i]; + } + } + + return result; + }; + + var compareConfigs = compareJSON(shepherd.appConfig, JSON.parse(localAppConfig)); + if (Object.keys(compareConfigs).length) { + var newConfig = Object.assign(JSON.parse(localAppConfig), compareConfigs); + + console.log('config diff is found, updating local config'); + console.log('config diff:'); + console.log(compareConfigs); + + shepherd.saveLocalAppConf(newConfig); + return newConfig; + } else { + return JSON.parse(localAppConfig); + } + + } else { + console.log('local config file is not found!'); + shepherd.saveLocalAppConf(shepherd.appConfig); + + return shepherd.appConfig; + } +}; + +shepherd.readDebugLog = function() { + console.log('reading debug.log'); + console.log(komodoDir + '/debug.log'); + + readLastLines + .read(komodoDir + '/debug.log', 50) + .then((lines) => console.log(lines)); +}; + function herder(flock, data) { //console.log(flock); //console.log(data); @@ -149,7 +216,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', @@ -161,7 +228,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 ); @@ -190,7 +257,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); }); @@ -208,7 +275,7 @@ function herder(flock, data) { }, function(err, apps) { pm2.disconnect(); // Disconnect from PM2 if (err) - throw err + throw err; }); }); } @@ -255,7 +322,8 @@ function herder(flock, data) { cwd: iguanaDir, }, function(err, apps) { pm2.disconnect(); // Disconnect from PM2 - if (err) throw err + if (err) + throw err; }); }); } @@ -271,6 +339,49 @@ function slayer(flock) { }); } +shepherd.saveLocalAppConf = function(appSettings) { + var appConfFileName = iguanaDir + '/config.json'; + + var FixFilePermissions = function() { + return new Promise(function(resolve, reject) { + var result = 'config.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 = 'config.json write file is done' + + fs.writeFile(appConfFileName, + JSON.stringify(appSettings) + .replace(/,/g, ',\n') // format json in human readable form + .replace(/:/g, ': ') + .replace(/{/g, '{\n') + .replace(/}/g, '\n}'), '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 created successfully at: ' + iguanaConfsDir); + resolve(result); + }, 2000); + }); + } + + FsWrite() + .then(FixFilePermissions()); // not really required now +} + function setConf(flock) { console.log(flock); @@ -303,7 +414,7 @@ function setConf(flock) { fs.ensureFile(DaemonConfPath, function(err) { console.log(err); // => null - }) + }); setTimeout(function() { console.log(result); @@ -335,7 +446,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); @@ -430,7 +541,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' +