309 lines
7.8 KiB
JavaScript
Executable file
309 lines
7.8 KiB
JavaScript
Executable file
#!/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-fingerprint>')
|
|
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 fingerprint = argv[1]
|
|
|
|
if (!fingerprint) {
|
|
console.log('Fingerprint required')
|
|
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)
|
|
}
|
|
|
|
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) {
|
|
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?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 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 = JSON.stringify(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()
|
|
})
|
|
}
|