diff --git a/bin/ssu-raqia b/bin/ssu-raqia new file mode 100644 index 00000000..ee2c3e5c --- /dev/null +++ b/bin/ssu-raqia @@ -0,0 +1,69 @@ +#!/usr/bin/env node + +'use strict'; + +var url = require('url'); +var querystring = require('querystring'); +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 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 \n'); + console.log('Please supply the code sent to you by Lamassu support.'); + process.exit(1); +} + +var rec; +try { + rec = JSON.parse(new Buffer(code, 'base64').toString()); +} catch(ex) { + console.log('There was a problem with the code. Please contact Lamassu support.'); + process.exit(2); +} + +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 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', + search: querystring.stringify({account_id: rec.accountId, code: rec.code, count: machines.length}) + }); + + Wreck.post(uri, {json: true}, 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); + + console.log(payload); //DEBUG + + var users = {}; + _.forEach(machines, function(fingerprint, i) { + users[fingerprint] = payload.users[i]; + }); + + fs.writeFileSync(raqiaPath, JSON.stringify(users)); + }); + }); +}); diff --git a/package.json b/package.json index 0177e5a5..05f05a58 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "lamassu-identitymind": "^1.0.1", "lodash": "^2.4.1", "minimist": "0.0.8", - "pg": "~2.11.1" + "pg": "~2.11.1", + "wreck": "^5.1.0" }, "repository": { "type": "git",