lamassu-migrate-config to update ls basepath (#216)

* lamassu-migrate-config to update ls basepath

* documentation added

* constrain to a particular set of options
This commit is contained in:
Fabio Cigliano 2018-11-23 05:04:41 +13:00 committed by Josh Harvey
parent 0ec647f2b2
commit dc1ed52357

View file

@ -1,9 +1,18 @@
const _ = require('lodash/fp')
const fs = require('fs')
const makeDir = require('make-dir')
const path = require('path')
const load = require('./options-loader')
// current path of lamassu-server project
const currentBasePath = path.dirname(__dirname)
// get path as array of path components
const paths = _.wrap(_.split, path.sep)
// find the index of the lamassu-server directory
// /usr/lib/node_modules/lamassu-server/certs/Lamassu_OP.pem => 3
const indexOfLs = _.flow(paths, _.wrap(_.indexOf, 'lamassu-server'))
module.exports = {run, mapKeyValuesDeep}
function mapKeyValuesDeep (cb, obj, key) {
@ -26,6 +35,36 @@ async function run () {
// check if there are new options to add
let result = _.mergeAll([defaultOpts, currentOpts])
// get all the options
// that ends with "Path" suffix
console.log(`Detected lamassu-server basepath: ${currentBasePath}`)
_.map(optionName => {
const currentPath = _.get(optionName, result)
// process only keys that contains
// lamassu-server dir in its path
const i = indexOfLs(currentPath)
if (i === -1) {
return
}
// workout the relative path
// /usr/lib/node_modules/lamassu-server/certs/Lamassu_OP.pem => certs/Lamassu_OP.pem
const rPath = _.drop(i + 1, paths(currentPath))
// prepend the current lamassu-server path
// certs/Lamassu_OP.pem => /usr/local/lib/node_modules/lamassu-server/certs/Lamassu_OP.pem
const newPath = _.join(path.sep, _.concat([currentBasePath], rPath))
// update this option
// if the value has changed
if (!_.isEqual(currentPath, newPath)) {
console.log(`Migrating option ${optionName} to new path ${newPath}`)
result[optionName] = newPath
}
}, ['seedPath', 'caPath', 'certPath', 'keyPath', 'lamassuCaPath'])
const shouldMigrate = !_.isEqual(result, currentOpts) || _.has('lamassuServerPath', result)
// write the resulting lamassu.json
@ -33,8 +72,18 @@ async function run () {
// remove old lamassuServerPath config
result = _.omit('lamassuServerPath', result)
const newOpts = _.pick(_.difference(_.keys(result), _.keys(currentOpts)), result)
console.log('Adding options', newOpts)
// find keys for which values
// have been changed
const differentValue = _.wrap(_.filter, key => !_.isEqual(result[key], currentOpts[key]))
// output affected options
const newOpts = _.pick(_.union(
// find change keys
differentValue(_.keys(result)),
// find new opts
_.difference(_.keys(result), _.keys(currentOpts))
), result)
console.log('Updating options', newOpts)
// store new lamassu.json file
fs.writeFileSync(options.path, JSON.stringify(result, null, ' '))