update ssu, email and sms
This commit is contained in:
parent
e42bde7b63
commit
7e15308499
5 changed files with 117 additions and 53 deletions
74
bin/ssu
74
bin/ssu
|
|
@ -199,43 +199,65 @@ function crypto () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function configure () {
|
function connect () {
|
||||||
var plugin = argv[1]
|
|
||||||
var key = argv[2]
|
|
||||||
|
|
||||||
if (!plugin || !key) {
|
|
||||||
console.log('\nssu config <plugin> <key>')
|
|
||||||
console.log('Configure a plugin setting.')
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
inquirer.prompt([{
|
|
||||||
type: 'password',
|
|
||||||
name: 'value',
|
|
||||||
message: 'Enter value for ' + key + ': ',
|
|
||||||
validate: function (val) {
|
|
||||||
return !val || val.length === 0
|
|
||||||
? 'Please enter a value for ' + key
|
|
||||||
: true
|
|
||||||
}
|
|
||||||
}]).then(function (result) {
|
|
||||||
var value = result.value
|
|
||||||
|
|
||||||
var psqlUrl
|
var psqlUrl
|
||||||
try {
|
try {
|
||||||
psqlUrl = process.env.DATABASE_URL || JSON.parse(fs.readFileSync('/etc/lamassu.json')).postgresql
|
psqlUrl = process.env.DATABASE_URL || JSON.parse(fs.readFileSync('/etc/lamassu.json')).postgresql
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
psqlUrl = 'psql://lamassu:lamassu@localhost/lamassu'
|
psqlUrl = 'psql://lamassu:lamassu@localhost/lamassu'
|
||||||
}
|
}
|
||||||
var db = pgp(psqlUrl)
|
return pgp(psqlUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadConfig (db) {
|
||||||
return db.one('select data from user_config where type=$1', 'exchanges')
|
return db.one('select data from user_config where type=$1', 'exchanges')
|
||||||
.then(function (data) {
|
.then(function (data) {
|
||||||
var config = data.data
|
return data.data
|
||||||
config.exchanges.plugins.settings[plugin] = config.exchanges.plugins.settings[plugin] || {}
|
})
|
||||||
config.exchanges.plugins.settings[plugin][key] = value
|
}
|
||||||
|
|
||||||
|
function updateConfig (db, config) {
|
||||||
db.none('update user_config set data=$1 where type=$2', [config, 'exchanges'])
|
db.none('update user_config set data=$1 where type=$2', [config, 'exchanges'])
|
||||||
db.none('notify "config_update"')
|
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 () {
|
.then(function () {
|
||||||
console.log('success')
|
console.log('success')
|
||||||
|
|
|
||||||
24
lib/config.js
Normal file
24
lib/config.js
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
require('es6-promise').polyfill()
|
||||||
|
var fs = require('fs')
|
||||||
|
var pgp = require('pg-promise')()
|
||||||
|
|
||||||
|
function connect () {
|
||||||
|
var psqlUrl
|
||||||
|
try {
|
||||||
|
psqlUrl = process.env.DATABASE_URL || JSON.parse(fs.readFileSync('/etc/lamassu.json')).postgresql
|
||||||
|
} catch (ex) {
|
||||||
|
psqlUrl = 'psql://lamassu:lamassu@localhost/lamassu'
|
||||||
|
}
|
||||||
|
return pgp(psqlUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadConfig () {
|
||||||
|
var db = connect()
|
||||||
|
return db.one('select data from user_config where type=$1', 'exchanges')
|
||||||
|
.then(function (data) {
|
||||||
|
pgp.end()
|
||||||
|
return data.data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.loadConfig = loadConfig
|
||||||
12
lib/email.js
12
lib/email.js
|
|
@ -1,4 +1,5 @@
|
||||||
var SMTPConnection = require('smtp-connection')
|
var SMTPConnection = require('smtp-connection')
|
||||||
|
var Config = require('./config')
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
|
@ -17,9 +18,15 @@ var options = {
|
||||||
}
|
}
|
||||||
|
|
||||||
function send (from, to, subject, body, cb) {
|
function send (from, to, subject, body, cb) {
|
||||||
|
Config.loadConfig()
|
||||||
|
.then(function (config) {
|
||||||
|
var _config = config.exchanges.plugins.settings.email
|
||||||
|
var user = _config.user
|
||||||
|
var pass = _config.pass
|
||||||
|
|
||||||
var connection = new SMTPConnection(options)
|
var connection = new SMTPConnection(options)
|
||||||
connection.connect(function () {
|
connection.connect(function () {
|
||||||
connection.login({user: 'josh@lamassu.is', pass: 'HPtXGp}9baafiqns%6YFH'}, function (err) {
|
connection.login({user: user, pass: pass}, function (err) {
|
||||||
if (err) return console.error(err)
|
if (err) return console.error(err)
|
||||||
var envelope = {
|
var envelope = {
|
||||||
from: from,
|
from: from,
|
||||||
|
|
@ -32,9 +39,10 @@ function send (from, to, subject, body, cb) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
send('josh@lamassu.is', 'joshmh@gmail.com', 'Another test', 'Screen is stale.\n\nTest3', function (err, info) {
|
send('josh@lamassu.is', 'joshmh@gmail.com', 'Another test', 'Screen is stale.\n\nTest4', function (err, info) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
console.log(info)
|
console.log(info)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
20
lib/sms.js
20
lib/sms.js
|
|
@ -1,12 +1,22 @@
|
||||||
var accountSid = 'AC5b08587439d5e0adb5132d133941ab76'
|
var Client = require('twilio')
|
||||||
var authToken = 'b4acf1212c0271d852706d17711e9670'
|
var Config = require('./config')
|
||||||
var client = require('twilio')(accountSid, authToken)
|
|
||||||
|
|
||||||
|
var toNumber = process.argv[2]
|
||||||
|
|
||||||
|
Config.loadConfig()
|
||||||
|
.then(function (config) {
|
||||||
|
var _config = config.exchanges.plugins.settings.sms
|
||||||
|
var accountSid = _config.accountSid
|
||||||
|
var authToken = _config.authToken
|
||||||
|
var fromNumber = _config.fromNumber
|
||||||
|
|
||||||
|
var client = Client(accountSid, authToken)
|
||||||
client.messages.create({
|
client.messages.create({
|
||||||
body: '[Lamassu] ALERT Stale screen: acceptingFirstBill',
|
body: '[Lamassu] ALERT Stale screen: acceptingFirstBill',
|
||||||
to: '+359899948650',
|
to: toNumber,
|
||||||
from: '+16035383222'
|
from: fromNumber
|
||||||
}, function (err, message) {
|
}, function (err, message) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
console.log(message)
|
console.log(message)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@
|
||||||
"prompt": "^1.0.0",
|
"prompt": "^1.0.0",
|
||||||
"promptly": "^1.1.0",
|
"promptly": "^1.1.0",
|
||||||
"ramda": "^0.19.1",
|
"ramda": "^0.19.1",
|
||||||
"smtp-connection": "^2.2.1",
|
"smtp-connection": "^2.3.2",
|
||||||
"twilio": "^3.3.0-edge",
|
"twilio": "^3.3.0-edge",
|
||||||
"web3": "^0.15.3",
|
"web3": "^0.15.3",
|
||||||
"wreck": "5.1.0"
|
"wreck": "5.1.0"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue