use winston instead of bunyan
This commit is contained in:
parent
24124b7cc5
commit
e559548b04
6 changed files with 1588 additions and 474 deletions
309
bin/ssu
309
bin/ssu
|
|
@ -1,309 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
'use strict'
|
||||
|
||||
var chalk = require('chalk')
|
||||
var wreck = require('wreck')
|
||||
var argv = process.argv.slice(2)
|
||||
var pgp = require('pg-promise')()
|
||||
var inquirer = require('inquirer')
|
||||
var R = require('ramda')
|
||||
var psqlUrl = require('../lib/options').postgresql
|
||||
|
||||
var cmd = argv[0]
|
||||
|
||||
if (!cmd) bail()
|
||||
|
||||
function bail (error) {
|
||||
error = error || 'Command line utility for lamassu-server'
|
||||
console.log(chalk.bold(error))
|
||||
console.log('\nssu reboot <machine-id>')
|
||||
console.log('This will remotely reboot your lamassu-machine.')
|
||||
console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin> [<trader-plugin>]')
|
||||
console.log('This will configure a new cryptocurrency.')
|
||||
console.log('\nssu crypto <code> [on|off]')
|
||||
console.log('This will activate or deactivate a cryptocurrency.')
|
||||
console.log('\nssu config <plugin> <key>')
|
||||
console.log('Configure a plugin setting.')
|
||||
console.log('\nssu notify [email] [sms]')
|
||||
console.log('Set notification plugin types.')
|
||||
console.log('\nssu set <plugin type> <plugin>')
|
||||
console.log('Set current plugin for plugin type.')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
switch (cmd) {
|
||||
case 'reboot':
|
||||
reboot()
|
||||
break
|
||||
case 'crypto':
|
||||
crypto()
|
||||
break
|
||||
case 'config':
|
||||
configure()
|
||||
break
|
||||
case 'notify':
|
||||
notify()
|
||||
break
|
||||
case 'set':
|
||||
setPlugin()
|
||||
break
|
||||
default:
|
||||
bail('No such command: ' + cmd)
|
||||
break
|
||||
}
|
||||
|
||||
function reboot () {
|
||||
var deviceId = argv[1]
|
||||
|
||||
if (!deviceId) {
|
||||
console.log('Machine ID required')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
var opts = {json: true}
|
||||
wreck.get('http://localhost:7070/pid?device_id=' + deviceId, opts, function (err, res, payload) {
|
||||
if (err) {
|
||||
console.log('Please make sure that lamassu-server is running on this box.')
|
||||
process.exit(2)
|
||||
}
|
||||
|
||||
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, deviceId: deviceId})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if (res.statusCode !== 200) {
|
||||
console.log('Communication error')
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
console.log('lamassu-machine rebooted but is not coming back up.')
|
||||
process.exit(5)
|
||||
}, 30000)
|
||||
|
||||
setInterval(function () {
|
||||
wreck.get('http://localhost:7070/pid?device_id=' + deviceId, 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 cryptoActivate (code, on) {
|
||||
var db = pgp(psqlUrl)
|
||||
|
||||
return db.one('select data from user_config where type=$1', 'exchanges')
|
||||
.then(function (data) {
|
||||
var config = data.data
|
||||
if (on) {
|
||||
if (R.contains(code, config.exchanges.settings.coins)) {
|
||||
console.log('success [no changes]')
|
||||
process.exit(0)
|
||||
}
|
||||
config.exchanges.settings.coins = R.append(code, config.exchanges.settings.coins)
|
||||
} else {
|
||||
config.exchanges.settings.coins = R.without([code], config.exchanges.settings.coins)
|
||||
}
|
||||
db.none('update user_config set data=$1 where type=$2', [config, 'exchanges'])
|
||||
db.none('notify "config_update"')
|
||||
})
|
||||
.then(function () {
|
||||
console.log('success')
|
||||
pgp.end()
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log(err.stack)
|
||||
pgp.end()
|
||||
})
|
||||
}
|
||||
|
||||
function crypto () {
|
||||
var code = argv[1]
|
||||
var tickerPlugin = argv[2]
|
||||
var walletPlugin = argv[3]
|
||||
var traderPlugin = argv[4]
|
||||
|
||||
if (code && tickerPlugin === 'on') return cryptoActivate(code, true)
|
||||
if (code && tickerPlugin === 'off') return cryptoActivate(code, false)
|
||||
|
||||
if (!code || !tickerPlugin || !walletPlugin) {
|
||||
console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin> [<trader-plugin>]')
|
||||
console.log('This will configure a new cryptocurrency.')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
code = code.toUpperCase()
|
||||
|
||||
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,
|
||||
trader: traderPlugin
|
||||
}
|
||||
config.exchanges.settings.coins = R.union(config.exchanges.settings.coins, [code])
|
||||
db.none('update user_config set data=$1 where type=$2', [config, 'exchanges'])
|
||||
db.none('notify "config_update"')
|
||||
})
|
||||
.then(function () {
|
||||
console.log('success')
|
||||
pgp.end()
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log(err.stack)
|
||||
pgp.end()
|
||||
})
|
||||
}
|
||||
|
||||
function connect () {
|
||||
return pgp(psqlUrl)
|
||||
}
|
||||
|
||||
function loadConfig (db) {
|
||||
return db.one('select data from user_config where type=$1', 'exchanges')
|
||||
.then(function (data) {
|
||||
return data.data
|
||||
})
|
||||
}
|
||||
|
||||
function updateConfig (db, config) {
|
||||
db.none('update user_config set data=$1 where type=$2', [config, 'exchanges'])
|
||||
db.none('notify "config_update"')
|
||||
}
|
||||
|
||||
function modifyConfig (config, plugin, keys, result) {
|
||||
keys.forEach(function (key) {
|
||||
var value = result[key]
|
||||
config.exchanges.plugins.settings[plugin] = config.exchanges.plugins.settings[plugin] || {}
|
||||
config.exchanges.plugins.settings[plugin][key] = value
|
||||
})
|
||||
return config
|
||||
}
|
||||
|
||||
function configure () {
|
||||
var plugin = argv[1]
|
||||
var keys = argv.slice(2)
|
||||
|
||||
if (!plugin || keys.length === 0) {
|
||||
console.log('\nssu config <plugin> <key> [<key> ...]')
|
||||
console.log('Configure a plugin setting.')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
var questions = keys.map(function (key) {
|
||||
return {
|
||||
type: 'input',
|
||||
name: key,
|
||||
message: 'Enter value for ' + key + ':',
|
||||
validate: function (val) {
|
||||
return !val || val.length === 0
|
||||
? 'Please enter a value for ' + key
|
||||
: true
|
||||
}
|
||||
}
|
||||
})
|
||||
inquirer.prompt(questions).then(function (result) {
|
||||
var db = connect()
|
||||
return loadConfig(db)
|
||||
.then(function (config) {
|
||||
var _config = modifyConfig(config, plugin, keys, result)
|
||||
return updateConfig(db, _config)
|
||||
})
|
||||
.then(function () {
|
||||
console.log('success')
|
||||
pgp.end()
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log(err.stack)
|
||||
pgp.end()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function notify () {
|
||||
var plugins = argv.slice(1)
|
||||
var db = connect()
|
||||
return loadConfig(db)
|
||||
.then(function (config) {
|
||||
config.exchanges.plugins.current.notify = plugins
|
||||
return updateConfig(db, config)
|
||||
})
|
||||
.then(function () {
|
||||
console.log('success')
|
||||
pgp.end()
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log(err.stack)
|
||||
pgp.end()
|
||||
})
|
||||
}
|
||||
|
||||
function setPlugin () {
|
||||
var pluginType = argv[1]
|
||||
var plugin = argv[2]
|
||||
|
||||
var db = connect()
|
||||
return loadConfig(db)
|
||||
.then(function (config) {
|
||||
if (!plugin) {
|
||||
var _plugin = config.exchanges.plugins.current[pluginType]
|
||||
_plugin
|
||||
? console.log('Current plugin for %s: %s', pluginType, _plugin)
|
||||
: console.log('No plugin set for %s', pluginType)
|
||||
process.exit(0)
|
||||
}
|
||||
config.exchanges.plugins.current[pluginType] = plugin
|
||||
return updateConfig(db, config)
|
||||
})
|
||||
.then(function () {
|
||||
console.log('success')
|
||||
pgp.end()
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.log(err.stack)
|
||||
pgp.end()
|
||||
})
|
||||
}
|
||||
126
bin/ssu-raqia
126
bin/ssu-raqia
|
|
@ -1,126 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
var url = require('url');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var argv = process.argv.slice(2);
|
||||
var Wreck = require('wreck');
|
||||
var _ = require('lodash');
|
||||
var pg = require('pg');
|
||||
var uuid = require('node-uuid');
|
||||
var async = require('async');
|
||||
var psqlUrl = require('../lib/options').postgresql
|
||||
|
||||
var raqiaPath = path.join(__dirname, '..', 'raqia.json');
|
||||
|
||||
var code = argv[0];
|
||||
if (!code) {
|
||||
console.log('Registers your machines with the Lamassu raqia.is API.');
|
||||
console.log('Usage: ssu-raqia <code>\n');
|
||||
console.log('Please supply the code sent to you by Lamassu support.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
var apiKey, apiSecret;
|
||||
|
||||
try {
|
||||
var buf = new Buffer(code, 'hex');
|
||||
apiKey = uuid.unparse(buf);
|
||||
apiSecret = uuid.unparse(buf, 16);
|
||||
} catch(ex) {
|
||||
console.log('There was a problem with the code. Please contact Lamassu support.');
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
var client = new pg.Client(psqlUrl);
|
||||
client.connect(function(err) {
|
||||
if (err) return console.log(err);
|
||||
var sql = 'SELECT * FROM devices ORDER BY id';
|
||||
client.query(sql, function(err, res) {
|
||||
if (err) return console.log(err);
|
||||
client.end();
|
||||
var machines = _.pluck(res.rows, 'fingerprint');
|
||||
|
||||
var uri = url.format({
|
||||
protocol: 'https',
|
||||
host: 'api.raqia.is',
|
||||
pathname: '/auth/users'
|
||||
});
|
||||
|
||||
var opts = {
|
||||
headers: headers(apiKey, apiSecret),
|
||||
json:true
|
||||
};
|
||||
|
||||
Wreck.get(uri, opts, function(err, res, payload) {
|
||||
if (err) return console.log(err.message);
|
||||
if (res.statusCode !== 200) return console.log('Could not connect to raqia.is: %d', res.statusCode);
|
||||
|
||||
var configuredMachines = _(payload).pluck('fingerprint').compact().value();
|
||||
var remainingMachines = _.difference(machines, configuredMachines);
|
||||
|
||||
var users = {};
|
||||
var zcUsers = _.filter(payload, zeroConfScopeOnly);
|
||||
|
||||
if (zcUsers.length < remainingMachines.length)
|
||||
bail('You need more raqia users for your account. Please contact Lamassu support.');
|
||||
|
||||
_.forEach(configuredMachines, function(fingerprint) {
|
||||
var user = _.find(payload, {fingerprint: fingerprint});
|
||||
users[fingerprint] = user;
|
||||
});
|
||||
|
||||
async.each(remainingMachines,
|
||||
function(fingerprint, cb) {
|
||||
var zcUser = zcUsers.pop();
|
||||
users[fingerprint] = zcUser;
|
||||
updateUser(zcUser, fingerprint, cb);
|
||||
},
|
||||
function(err) {
|
||||
if (err) bail(err.message);
|
||||
fs.writeFileSync(raqiaPath, JSON.stringify(users));
|
||||
console.log('Success.');
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function updateUser(user, fingerprint, cb) {
|
||||
var uri = 'https://api.raqia.is/auth/users/' + user.userId;
|
||||
var opts = {
|
||||
json: true,
|
||||
payload: JSON.stringify({fingerprint: fingerprint}),
|
||||
headers: headers(apiKey, apiSecret)
|
||||
};
|
||||
|
||||
Wreck.post(uri, opts, function(err, res) {
|
||||
if (err) return cb(err);
|
||||
if (res.statusCode !== 200) return cb(new Error('Could not connect to raqia.is: ' + res.statusCode));
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
function zeroConfScopeOnly(user) {
|
||||
return user.apiKeys[0].scope[0] === 'zero-conf' &&
|
||||
user.apiKeys[0].scope.length === 1 &&
|
||||
!user.fingerprint;
|
||||
}
|
||||
|
||||
function headers(apiKey, apiSecret) {
|
||||
return {
|
||||
'request-id': uuid.v4(),
|
||||
Authorization: 'Basic ' + buildAuth(apiKey, apiSecret)
|
||||
};
|
||||
}
|
||||
|
||||
function buildAuth(apiKey, apiSecret) {
|
||||
return new Buffer([apiKey, apiSecret].join(':')).toString('base64');
|
||||
}
|
||||
|
||||
function bail(msg) {
|
||||
console.log(msg);
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
@ -1,38 +1,19 @@
|
|||
'use strict'
|
||||
|
||||
var Bunyan = require('bunyan')
|
||||
var async = require('async')
|
||||
const winston = require('winston')
|
||||
|
||||
var logLevel = typeof process.env.LAMASSU_ENV === 'string'
|
||||
? process.env.LAMASSU_ENV
|
||||
: 'info'
|
||||
|
||||
console.log('DEBUG1: %s, %s', process.env.LAMASSU_ENV, logLevel)
|
||||
var bunyan = Bunyan.createLogger({name: 'lamassu-server', level: logLevel})
|
||||
const logger = new winston.Logger({
|
||||
level: logLevel,
|
||||
transports: [
|
||||
new (winston.transports.Console)({colorize: true})
|
||||
]
|
||||
})
|
||||
|
||||
// log version
|
||||
var version = require('../package.json').version
|
||||
bunyan.info('Version: %s', version)
|
||||
logger.info('Version: %s', version)
|
||||
|
||||
// log git stuff (optional)
|
||||
// `git-rev` omits `err` param in callback, without this wrapper
|
||||
// `async` interprets returned values as errors.
|
||||
function wrapper (fn, cb) {
|
||||
fn(function (value) {
|
||||
cb(null, value)
|
||||
})
|
||||
}
|
||||
try {
|
||||
var git = require('git-rev')
|
||||
|
||||
async.parallel([
|
||||
async.apply(wrapper, git.branch),
|
||||
async.apply(wrapper, git.short)
|
||||
],
|
||||
function (err, values) {
|
||||
if (err) return bunyan.error(err)
|
||||
bunyan.info('Git: #%s @%s', values[0], values[1])
|
||||
})
|
||||
} catch (_) {}
|
||||
|
||||
module.exports = bunyan
|
||||
module.exports = logger
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
const BigNumber = require('bignumber.js')
|
||||
const pgp = require('pg-promise')()
|
||||
var psqlUrl = require('../lib/options').postgresql
|
||||
const backoff = require('u-promised').backoff
|
||||
|
||||
const logger = require('./logger')
|
||||
|
||||
|
|
@ -476,7 +475,8 @@ exports.nextCashOutSerialHD = function nextCashOutSerialHD (txId, cryptoCode) {
|
|||
.then(() => serialNumber)
|
||||
})
|
||||
|
||||
return backoff(100, 0, 5, attempt)
|
||||
// TODO: retry on failure
|
||||
return attempt()
|
||||
}
|
||||
|
||||
exports.fetchLiveHD = function fetchLiveHD () {
|
||||
|
|
|
|||
11
package.json
11
package.json
|
|
@ -2,22 +2,17 @@
|
|||
"name": "lamassu-server",
|
||||
"description": "bitcoin atm client server protocol module",
|
||||
"keywords": [],
|
||||
"version": "4.0.0",
|
||||
"version": "4.0.1",
|
||||
"license": "Unlicense",
|
||||
"author": "Lamassu (https://lamassu.is)",
|
||||
"dependencies": {
|
||||
"async": "~0.2.9",
|
||||
"bignumber.js": "^2.3.0",
|
||||
"body-parser": "^1.15.1",
|
||||
"bunyan": "^1.8.1",
|
||||
"chalk": "^1.1.3",
|
||||
"express": "^4.13.4",
|
||||
"express-limiter": "^1.6.0",
|
||||
"helmet": "^2.3.0",
|
||||
"inquirer": "^1.0.0",
|
||||
"lamassu-bitcoinaverage": "~1.0.0",
|
||||
"lamassu-bitcoind": "lamassu/lamassu-bitcoind",
|
||||
"lamassu-bitgo": "^0.2.2",
|
||||
"lamassu-bitpay": "~1.0.0",
|
||||
"lamassu-bitstamp": "^1.0.4",
|
||||
"lamassu-blockcypher": "~0.1.0",
|
||||
|
|
@ -33,14 +28,12 @@
|
|||
"node-hkdf-sync": "^1.0.0",
|
||||
"node-uuid": "^1.4.2",
|
||||
"numeral": "^1.5.3",
|
||||
"on-finished": "^2.3.0",
|
||||
"pg": "^4.5.5",
|
||||
"pg-promise": "^4.3.3",
|
||||
"pify": "^2.3.0",
|
||||
"pretty-ms": "^2.1.0",
|
||||
"ramda": "^0.21.0",
|
||||
"u-promised": "^0.2.4",
|
||||
"wreck": "5.1.0"
|
||||
"winston": "^2.3.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue