diff --git a/bin/ssu b/bin/ssu index b035b82c..bf8e4fe5 100755 --- a/bin/ssu +++ b/bin/ssu @@ -4,80 +4,149 @@ var wreck = require('wreck') var argv = process.argv.slice(2) +var Promise = require('es6-promise') +var pgp = require('pg-promise')({ + promiseLib: Promise +}) + +var fs = require('fs') var cmd = argv[0] -var fingerprint = argv[1] -if (!cmd || !fingerprint) { +if (!cmd) bail() + +function bail () { console.log('Command line utility for lamassu-server') - console.log('\nUsage: ssu reboot ') + console.log('\nssu reboot ') console.log('This will remotely reboot your lamassu-machine.') + console.log('\nssu crypto ') + console.log('This will configure a new cryptocurrency.') process.exit(1) } -var opts = {json: true} -wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err, res, payload) { - if (err) { - console.log('Please make sure that lamassu-server is running on this box.') - process.exit(2) +switch (cmd) { + case 'reboot': + reboot() + break + case 'crypto': + crypto() + break + default: + +} + +function reboot () { + var fingerprint = argv[1] + + if (!fingerprint) { + console.log('Fingerprint required') + process.exit(1) } - if (!payload || !payload.pid) { - console.log('The requested lamassu-machine appears to be running an old version.') - process.exit(3) - } - - var pid = payload.pid - - if (Date.now() - payload.ts > 10000) { - console.log('lamassu-machine is not connected to server.') - process.exit(6) - } - - var opts2 = { - headers: {'Content-Type': 'application/json'}, - payload: JSON.stringify({pid: pid, fingerprint: fingerprint}) - } - - wreck.post('http://localhost:7070/reboot', opts2, function (err2, res) { - if (err2) { + var opts = {json: true} + wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err, res, payload) { + if (err) { console.log('Please make sure that lamassu-server is running on this box.') process.exit(2) } - if (res.statusCode !== 200) { - console.log('Communication error') - return + if (!payload || !payload.pid) { + console.log('The requested lamassu-machine appears to be running an old version.') + process.exit(3) } - console.log('Rebooting...') + var pid = payload.pid - var ts = null + if (Date.now() - payload.ts > 10000) { + console.log('lamassu-machine is not connected to server.') + process.exit(6) + } - setTimeout(function () { - if (Date.now() - ts < 10000) { - console.log('lamassu-machine did not reboot but is still contacting server.') - process.exit(4) + var opts2 = { + headers: {'Content-Type': 'application/json'}, + payload: JSON.stringify({pid: pid, fingerprint: fingerprint}) + } + + wreck.post('http://localhost:7070/reboot', opts2, function (err2, res) { + if (err2) { + console.log('Please make sure that lamassu-server is running on this box.') + process.exit(2) } - console.log('lamassu-machine rebooted but is not coming back up.') - process.exit(5) - }, 30000) + if (res.statusCode !== 200) { + console.log('Communication error') + return + } - setInterval(function () { - wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err3, res, payload) { - if (err3) { - console.log('lamassu-server appears to be down.') - process.exit(2) + console.log('Rebooting...') + + var ts = null + + setTimeout(function () { + if (Date.now() - ts < 10000) { + console.log('lamassu-machine did not reboot but is still contacting server.') + process.exit(4) } - ts = payload.ts + console.log('lamassu-machine rebooted but is not coming back up.') + process.exit(5) + }, 30000) - if (payload.pid !== pid) { - console.log('lamassu-machine is back up!') - process.exit(0) - } - }) - }, 5000) + setInterval(function () { + wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err3, res, payload) { + if (err3) { + console.log('lamassu-server appears to be down.') + process.exit(2) + } + + ts = payload.ts + + if (payload.pid !== pid) { + console.log('lamassu-machine is back up!') + process.exit(0) + } + }) + }, 5000) + }) }) -}) +} + +function crypto () { + var code = argv[1] + var tickerPlugin = argv[2] + var walletPlugin = argv[3] + + if (!code || !tickerPlugin || !walletPlugin) { + console.log('\nssu crypto ') + console.log('This will configure a new cryptocurrency.') + process.exit(1) + } + + code = code.toUpperCase() + + var psqlUrl + try { + psqlUrl = process.env.DATABASE_URL || JSON.parse(fs.readFileSync('/etc/lamassu.json')).postgresql + } catch (ex) { + psqlUrl = 'psql://lamassu:lamassu@localhost/lamassu' + } + var db = pgp(psqlUrl) + + return db.one('select data from user_config where type=$1', 'exchanges') + .then(function (data) { + var config = data.data + config.exchanges.plugins.current[code] = { + ticker: tickerPlugin, + transfer: walletPlugin + } + return db.none('update user_config set data=$1 where type=$2', [config, 'exchanges']) + }) + .then(function () { + console.log('success') + pgp.end() + }) + .catch(function (err) { + console.log(err.stack) + pgp.end() + }) +} diff --git a/lib/plugins.js b/lib/plugins.js index 128abb5a..92050280 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -2,7 +2,10 @@ var _ = require('lodash') var async = require('async') + var BigNumber = require('bignumber.js') +BigNumber.config({DECIMAL_PLACES: 40}) + var logger = require('./logger') var argv = require('minimist')(process.argv.slice(2)) diff --git a/lib/plugins/kraken.js b/lib/plugins/kraken.js index 29b61d8f..29376ca4 100644 --- a/lib/plugins/kraken.js +++ b/lib/plugins/kraken.js @@ -1,8 +1,9 @@ require('es6-promise').polyfill() var axios = require('axios') var _ = require('lodash') + var BigNumber = require('bignumber.js') -BigNumber.config({DECIMAL_PLACES: 30}) +BigNumber.config({DECIMAL_PLACES: 40}) exports.NAME = 'Kraken' exports.SUPPORTED_MODULES = ['ticker'] diff --git a/package.json b/package.json index b25ad8c9..79659a18 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,9 @@ "lodash": "^2.4.1", "minimist": "0.0.8", "node-uuid": "^1.4.2", - "pg": "~2.11.1", + "pg": "^4.5.1", + "pg-promise": "^3.4.3", + "web3": "^0.15.3", "wreck": "5.1.0" }, "repository": {