From 153556a7890baa1232ad1acfc6ac47d116eb792c Mon Sep 17 00:00:00 2001 From: Satinder Grewal Date: Thu, 5 Jan 2017 03:46:13 +1300 Subject: [PATCH] Shepherd API update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Starts komodod, zcashd, or komodo assechains via ajax call and JSON options - module added to read config file of selected daemon or assetchain and return it’s values in JSON format. will add further conditions in next update to add any missing settings if required. --- private/setconf.js | 131 +++++++++++++++++++++++++++++++++++++++++++++ routes/shepherd.js | 51 ++++++++++++++++-- 2 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 private/setconf.js diff --git a/private/setconf.js b/private/setconf.js new file mode 100644 index 0000000..02041b1 --- /dev/null +++ b/private/setconf.js @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2015 Satinderjit Singh + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +const child_process = require('child_process'); +const fs = require('fs') +const path = require('path') +const os = require('os') + +var coind_conf = module.exports = { + exec: child_process.exec, + status: status +}; + +/** + * Parses the status for a single network interface. + * + * @private + * @static + * @category coind_conf + * @returns {object} The parsed network interface status. + * + */ +function parse_status_block(block) { + var match; + + var parsed = { + rpcuser: block.match(/rpcuser=([^\s]+)/)[1] + }; + + if ((match = block.match(/rpcpassword=\s*(.*)/))) { + parsed.rpcpass = match[1]; + } + + if ((match = block.match(/rpcport=\s*(.*)/))) { + parsed.rpcport = match[1]; + } + + if ((match = block.match(/rpcbind=\s*(.*)/))) { + parsed.rpcbind = match[1]; + } + + if ((match = block.match(/server=\s*(.*)/))) { + parsed.server = match[1]; + } + + if ((match = block.match(/txindex=\s*(.*)/))) { + parsed.txindex = match[1]; + } + + if ((match = block.match(/addnode=\s*(.*)/))) { + parsed.addnode = match[1]; + } + + return parsed; +} + +/** + * Parses the status for all network interfaces. + * + * @private + * @static + * @category coind_conf + * @param {function} callback The callback function. + * + */ +function parse_status(callback) { + return function(error, stdout, stderr) { + if (error) callback(error); + else callback(error, + stdout.trim().split('\n\n').map(parse_status_block)); + //stdout.trim().split(/\s\*-usb:/g).map(parse_status_block)); + //console.log(stdout.trim().split(/\s\*-usb:/g)) + }; +} + +/** + * The **coind_conf status** command is used to query the status of all + * configured interfaces. + * + * @static + * @category coind_conf + * @param {function} callback The callback function. + * @example + * + * var coind_conf = require('./coind_conf'); + * + * coind_conf.status(function(err, status) { + * console.log(status); + * }); + * + * // => + * [ + * { + * "rpcuser": "pi", + * "rpcpass": "c892f76fc45365d50cb744ec1d5cdde659c98e0b", + * "rpcport": "8336", + * "server": "1" + * } + * ] + * + */ +function status(confPath, callback) { + //console.log(confPath); + if (os.platform() === 'darwin' || os.platform() === 'linux') { + this.exec('cat "' + confPath + '"', parse_status(callback)); + } + if (os.platform() === 'win32') { + this.exec('type "' + confPath + '"', parse_status(callback)); + } + +} diff --git a/routes/shepherd.js b/routes/shepherd.js index d5de33d..4b12d71 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -11,6 +11,8 @@ var express = require('express'); var pm2 = require('pm2'); Promise = require('bluebird'); +var setconf = require("../private/setconf.js"); + var shepherd = express.Router(); @@ -85,6 +87,19 @@ shepherd.post('/slay', function(req, res) { }); +shepherd.post('/setconf', function(req, res) { + console.log('======= req.body ======='); + //console.log(req); + console.log(req.body); + //console.log(req.body.chain); + + setConf(req.body.chain); + + res.end('{"msg": "success","result": "result"}'); + +}); + + function herder(flock, data) { //console.log(flock); //console.log(data); @@ -143,11 +158,12 @@ function herder(flock, data) { } pm2.start({ - script : komododBin, // path to binary - name: 'PAXUSD', + script: komododBin, // path to binary + name: data.ac_name, //REVS, USD, EUR etc. exec_mode : 'fork', cwd: komodoDir, - args: ["-server", "-ac_name=USD", "-addnode=78.47.196.146"], //separate the params with commas + args: data.ac_options, + //args: ["-server", "-ac_name=USD", "-addnode=78.47.196.146"], //separate the params with commas }, function(err, apps) { pm2.disconnect(); // Disconnect from PM2 if (err) throw err @@ -162,8 +178,37 @@ function slayer(flock) { pm2.delete(flock, function(err, ret) { //console.log(err); + pm2.disconnect(); console.log(ret); }); } +function setConf(flock) { + console.log(flock); + + if (os.platform() === 'darwin') { + var komodoDir = process.env.HOME + '/Library/Application Support/Komodo'; + var ZcashDir = process.env.HOME + '/Library/Application Support/Zcash'; + } + if (os.platform() === 'linux') { + var komodoDir = process.env.HOME + '/.komodo' + var ZcashDir = process.env.HOME + '/.zcash' + } + + + switch (flock) { + case 'komodod': var DaemonConfPath = komodoDir + '/komodo.conf'; + break; + case 'zcashd': var DaemonConfPath = ZcashDir + '/zcash.conf'; + break; + default: var DaemonConfPath = komodoDir + '/' + flock + '/' + flock + '.conf'; + } + + console.log(DaemonConfPath); + + setconf.status(DaemonConfPath, function(err, status) { + console.log(status); + }); +} + module.exports = shepherd; \ No newline at end of file