Browse Source

basic pm2 implemented to start iguana

ca333-dev
Satinder Grewal 8 years ago
parent
commit
7634af174e
  1. 57
      gui/index.html
  2. 85
      gui/loading.js
  3. 43
      main.js
  4. 4
      package.json
  5. 148
      routes/shepherd.js

57
gui/index.html

@ -5,65 +5,10 @@
<link rel="stylesheet" href="EasyDEX-GUI/assets/global/css/bootstrap.min.css">
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<script type="text/javascript" src="EasyDEX-GUI/assets/global/vendor/jquery/jquery.min.js"></script>
<script type="text/javascript" src="loading.js"></script>
<script>if (window.module) module = window.module;</script>
</head>
<body>
<script type"text/javascript">
function Iguana_activehandle() {
var result = [];
//comment
var ajax_data = {"agent":"SuperNET","method":"activehandle"};
//console.log(ajax_data);
$.ajax({
async: false,
type: 'POST',
data: JSON.stringify(ajax_data),
url: 'http://127.0.0.1:7778',
//dataType: 'text',
success: function(data, textStatus, jqXHR) {
var AjaxOutputData = JSON.parse(data);
//console.log('== ActiveHandle Data OutPut ==');
//console.log(AjaxOutputData);
result.push(AjaxOutputData);
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText);
if ( xhr.readyState == 0 ) {
//Iguana_ServiceUnavailable();
result.push('error')
}
console.log(textStatus);
console.log(error);
}
});
//return 'Executed Iguana_activehandle. Check Iguana_activehandle_output var value.';
return result;
}
$(document).ready( function() {
//var check = Iguana_activehandle();
//console.log(check[0]) // here I invoke the checking function
const remote = require('electron').remote;
var window = remote.getCurrentWindow();
var portcheck;
function sartcheck() {
portcheck = setTimeout(function(){
var check = Iguana_activehandle();
console.log(check[0])
if (check[0] !== 'error') {
stopcheck();
window.close();
}
},2000);
}
function stopcheck() {
clearTimeout(portcheck);
}
sartcheck();
//setTimeout(function(){ window.close(); }, 15000);
});
</script>
<div style="text-align: center;">
<div style="background-image: url('loading.gif'); background-repeat: no-repeat; background-position: center; height: 200px; width: 400px; position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto;">Starting Wallet. Please wait...</div>
</div>

85
gui/loading.js

@ -0,0 +1,85 @@
function Iguana_activehandle() {
var result = [];
//comment
var ajax_data = {"agent":"SuperNET","method":"activehandle"};
//console.log(ajax_data);
$.ajax({
async: false,
type: 'POST',
data: JSON.stringify(ajax_data),
url: 'http://127.0.0.1:7778',
//dataType: 'text',
success: function(data, textStatus, jqXHR) {
var AjaxOutputData = JSON.parse(data);
//console.log('== ActiveHandle Data OutPut ==');
//console.log(AjaxOutputData);
result.push(AjaxOutputData);
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText);
if ( xhr.readyState == 0 ) {
//Iguana_ServiceUnavailable();
result.push('error')
}
console.log(textStatus);
console.log(error);
}
});
//return 'Executed Iguana_activehandle. Check Iguana_activehandle_output var value.';
return result;
}
function StartIguana() {
var ajax_data = {"herd":"iguana"};
console.log(ajax_data);
$.ajax({
async: false,
type: 'POST',
data: JSON.stringify(ajax_data),
url: 'http://127.0.0.1:17777/shepherd/herd',
dataType: "xml/html/script/json", // expected format for response
contentType: "application/json", // send as JSON
success: function(data, textStatus, jqXHR) {
var AjaxOutputData = JSON.parse(data);
console.log('== ActiveHandle Data OutPut ==');
console.log(AjaxOutputData);
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText);
if ( xhr.readyState == 0 ) {
}
console.log(textStatus);
console.log(error);
}
});
}
$(document).ready( function() {
//var check = Iguana_activehandle();
//console.log(check[0]) // here I invoke the checking function
const remote = require('electron').remote;
var window = remote.getCurrentWindow();
StartIguana();
var portcheck;
function sartcheck() {
portcheck = setInterval(function(){
var check = Iguana_activehandle();
console.log(check[0])
if (check[0] !== 'error') {
stopcheck();
window.close();
}
},2000);
}
function stopcheck() {
clearInterval(portcheck);
}
sartcheck();
//setTimeout(function(){ window.close(); }, 15000);
});

43
main.js

@ -4,6 +4,8 @@
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
var express = require('express')
var bodyParser = require('body-parser')
const path = require('path')
const url = require('url')
const os = require('os')
@ -24,24 +26,31 @@ process.once('loaded', () => {
})
// GUI APP settings and starting gui on address http://120.0.0.1:17777
var express = require('express')
var guiapp = express()
var shepherd = require('./routes/shepherd');
var guipath = path.join(__dirname, '/gui')
guiapp.use('/gui', express.static(guipath))
var 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('Hello World!')
})
var guipath = path.join(__dirname, '/gui')
guiapp.use('/gui', express.static(guipath))
guiapp.use('/shepherd', shepherd);
var rungui = guiapp.listen(17777, function () {
console.log('guiapp listening on port 17777!')
})
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
var iguanaOSX = path.join(__dirname, '/assets/bin/osx/iguana');
var iguanaLinux = path.join(__dirname, '/assets/bin/linux64/iguana');
@ -65,9 +74,13 @@ if (os.platform() === 'win32') {
var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico')
iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc);
}
*/
//console.log(iguanaDir);
if (os.platform() === 'linux') { var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png') }
if (os.platform() === 'win32') { var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico') }
//console.log(iguanaDir);
/*
// MAKE SURE IGUANA DIR IS THERE FOR USER
mkdirp(iguanaDir, function (err) {
if (err)
@ -85,7 +98,7 @@ fs.copy(iguanaConfsDirSrc, iguanaConfsDir, function (err) {
if (err) return console.error(err)
console.log('confs files copied successfully at: '+ iguanaConfsDir )
})
*/
let mainWindow
let loadingWindow
@ -112,7 +125,7 @@ function createLoadingWindow() {
})
//ca333 todo - add os detector to use correct binary - so we can use the same bundle on ALL OS platforms
if (os.platform() === 'win32') {
/*if (os.platform() === 'win32') {
process.chdir(iguanaDir);
//exec(iguanaWin, {cwd: iguanaDir}); //specify binary in startup
ig = spawn(iguanaWin);
@ -123,12 +136,12 @@ function createLoadingWindow() {
//corsproxy_process = spawn('corsproxy');
}
if (os.platform() === 'darwin') {
process.chdir(iguanaDir);
ig = spawn(iguanaOSX);
//process.chdir(iguanaDir);
//ig = spawn(iguanaOSX);
//corsproxy_process = spawn('corsproxy');
}
}*/
if (os.platform() !== 'win32') { ig.stderr.on( 'error: ', data => { console.log( `stderr: ${data}` ); }); }
//if (os.platform() !== 'win32') { ig.stderr.on( 'error: ', data => { console.log( `stderr: ${data}` ); }); }
}
app.on('ready', createLoadingWindow)
@ -146,11 +159,11 @@ function createWindow (status) {
// if window closed we kill iguana proc
mainWindow.on('closed', function () {
if (os.platform() !== 'win32') { ig.kill(); /*corsproxy_process.kill();*/ }
if (os.platform() === 'win32') {
//if (os.platform() !== 'win32') { ig.kill(); /*corsproxy_process.kill();*/ }
/*if (os.platform() === 'win32') {
//exec('TASKKILL /F /IM iguana.exe /T', {cwd: iguanaDir});
ig.kill();
}
}*/
// our app does not have multiwindow - so we dereference the window object instead of
// putting them into an window_arr
mainWindow = null

4
package.json

@ -22,9 +22,11 @@
"electron": "^1.4.1"
},
"dependencies": {
"body-parser": "^1.15.2",
"corsproxy": "^1.5.0",
"express": "^4.14.0",
"fs-extra": "^1.0.0",
"mkdirp": "^0.5.1",
"corsproxy": "^1.5.0"
"pm2": "^2.2.3"
}
}

148
routes/shepherd.js

@ -0,0 +1,148 @@
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
const os = require('os')
var fs = require('fs');
var fs = require('fs-extra')
var mkdirp = require('mkdirp');
var pm2 = require('pm2');
// IGUANA FILES AND CONFIG SETTINGS
var iguanaConfsDirSrc = path.join(__dirname, '../assets/deps/confs');
// SETTING OS DIR TO RUN IGUANA FROM
// SETTING APP ICON FOR LINUX AND WINDOWS
if (os.platform() === 'darwin') {
var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana');
var iguanaDir = process.env.HOME + '/Library/Application Support/iguana';
var iguanaConfsDir = iguanaDir + '/confs';
}
if (os.platform() === 'linux') {
var iguanaBin = path.join(__dirname, '../assets/bin/linux64/iguana');
var iguanaDir = process.env.HOME + '/.iguana'
var iguanaConfsDir = iguanaDir + '/confs';
var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png')
}
if (os.platform() === 'win32') {
var iguanaBin = path.join(__dirname, '../assets/bin/win64/iguana.exe'); iguanaBin = path.normalize(iguanaBin);
var iguanaDir = process.env.APPDATA + '/iguana'; iguanaDir = path.normalize(iguanaDir)
var iguanaConfsDir = process.env.APPDATA + '/iguana/confs'; iguanaConfsDir = path.normalize(iguanaConfsDir)
var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico')
iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc);
}
console.log(iguanaDir);
console.log(iguanaBin);
// END IGUANA FILES AND CONFIG SETTINGS
var express = require('express');
var shepherd = express.Router();
shepherd.get('/', function(req, res, next) {
res.send('Hello World!')
})
shepherd.post('/herd', function(req, res) {
console.log('======= req.body =======');
//console.log(req);
console.log(req.body);
//console.log(req.body.herd);
//console.log(req.body.options);
herder(req.body.herd, req.body.options);
res.end('{"msg": "success","result": "result"}');
});
shepherd.post('/slay', function(req, res) {
console.log('======= req.body =======');
//console.log(req);
console.log(req.body);
//console.log(req.body.slay);
slayer(req.body.slay);
res.end('{"msg": "success","result": "result"}');
});
function herder(flock, options) {
//console.log(flock);
//console.log(options);
if (options == undefined) { options = 'none'; console.log('it is undefined'); }
if (flock === 'iguana') {
console.log('iguana flock selected...');
console.log('selected options: '+options);
// MAKE SURE IGUANA DIR IS THERE FOR USER
mkdirp(iguanaDir, function (err) {
if (err)
console.error(err)
else
fs.readdir(iguanaDir, (err, files) => {
files.forEach(file => {
//console.log(file);
});
})
});
// COPY CONFS DIR WITH PEERS FILE TO IGUANA DIR, AND KEEP IT IN SYNC
fs.copy(iguanaConfsDirSrc, iguanaConfsDir, function (err) {
if (err) return console.error(err)
console.log('confs files copied successfully at: '+ iguanaConfsDir )
})
pm2.connect(function(err) { //start up pm2 god
if (err) {
console.error(err);
process.exit(2);
}
pm2.start({
script : iguanaBin, // path to binary
name: 'IGUANA',
exec_mode : 'fork',
cwd: iguanaDir, //set correct iguana directory
}, function(err, apps) {
pm2.disconnect(); // Disconnect from PM2
if (err) throw err
});
});
}
if (flock === 'komodod') {
console.log('komodod flock selected...');
console.log('selected options: '+options);
}
}
function slayer(flock) {
console.log(flock);
pm2.delete('IGUANA', function(err, ret) {
should(err).be.null()
pm2.list(function(err, ret) {
should(err).be.null()
ret.length.should.eql(8);
done();
});
});
}
module.exports = shepherd;
Loading…
Cancel
Save