Lamassu update script changes (#126)
* lamassu-migrate-config script added + lamassu-default.json * lamassu update script changes * lamassu-update unit test added
This commit is contained in:
parent
63e0782e32
commit
c3535e6ed3
8 changed files with 213 additions and 36 deletions
51
lib/migrate-options.js
Normal file
51
lib/migrate-options.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
const _ = require('lodash/fp')
|
||||
const fs = require('fs')
|
||||
const makeDir = require('make-dir')
|
||||
|
||||
const load = require('./options-loader')
|
||||
|
||||
module.exports = {run, mapKeyValuesDeep}
|
||||
|
||||
function mapKeyValuesDeep (cb, obj, key) {
|
||||
if (_.isArray(obj)) {
|
||||
return _.mapValues((val, key) => mapKeyValuesDeep(cb, val, key), obj)
|
||||
} else if (_.isObject(obj)) {
|
||||
return _.pickBy((val, key) => mapKeyValuesDeep(cb, val, key), obj)
|
||||
} else {
|
||||
return cb(obj, key)
|
||||
}
|
||||
}
|
||||
|
||||
async function run () {
|
||||
// load defaults
|
||||
const defaultOpts = require('../lamassu-default')
|
||||
|
||||
// load current opts
|
||||
const options = load()
|
||||
const currentOpts = options.opts
|
||||
|
||||
// check if there are new options to add
|
||||
const result = _.mergeAll([defaultOpts, currentOpts])
|
||||
const shouldMigrate = !_.isEqual(result, currentOpts)
|
||||
|
||||
// write the resulting lamassu.json
|
||||
if (shouldMigrate) {
|
||||
const newOpts = _.pick(_.difference(_.keys(result), _.keys(currentOpts)), result)
|
||||
console.log('Adding options', newOpts)
|
||||
|
||||
// store new lamassu.json file
|
||||
fs.writeFileSync(options.path, JSON.stringify(result, null, ' '))
|
||||
}
|
||||
|
||||
// get all the new options
|
||||
// that ends with "Dir" suffix
|
||||
mapKeyValuesDeep((v, k) => {
|
||||
if (_.endsWith('Dir', k)) {
|
||||
const path = _.attempt(() => makeDir.sync(v))
|
||||
|
||||
if (_.isError(path)) {
|
||||
console.error(`Error while creating folder ${v}`, path)
|
||||
}
|
||||
}
|
||||
}, result)
|
||||
}
|
||||
46
lib/options-loader.js
Normal file
46
lib/options-loader.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
const argv = require('minimist')(process.argv.slice(2))
|
||||
|
||||
/**
|
||||
* @return {{path: string, opts: any}}
|
||||
*/
|
||||
function load () {
|
||||
if (process.env.LAMASSU_CONFIG) {
|
||||
const configPath = process.env.LAMASSU_CONFIG
|
||||
return {
|
||||
path: configPath,
|
||||
opts: JSON.parse(fs.readFileSync(configPath))
|
||||
}
|
||||
}
|
||||
|
||||
if (argv.f) {
|
||||
const configPath = argv.f
|
||||
return {
|
||||
path: configPath,
|
||||
opts: JSON.parse(fs.readFileSync(configPath))
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const globalConfigPath = path.resolve('/etc', 'lamassu', 'lamassu.json')
|
||||
return {
|
||||
path: globalConfigPath,
|
||||
opts: JSON.parse(fs.readFileSync(globalConfigPath))
|
||||
}
|
||||
} catch (_) {
|
||||
try {
|
||||
const homeConfigPath = path.resolve(os.homedir(), '.lamassu', 'lamassu.json')
|
||||
return {
|
||||
path: homeConfigPath,
|
||||
opts: JSON.parse(fs.readFileSync(homeConfigPath))
|
||||
}
|
||||
} catch (_) {
|
||||
console.error("Couldn't open lamassu.json config file.")
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = load
|
||||
|
|
@ -1,35 +1,8 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
const _ = require('lodash/fp')
|
||||
const argv = require('minimist')(process.argv.slice(2))
|
||||
const load = require('./options-loader')
|
||||
|
||||
function load () {
|
||||
if (process.env.LAMASSU_CONFIG) {
|
||||
const configPath = process.env.LAMASSU_CONFIG
|
||||
return JSON.parse(fs.readFileSync(configPath))
|
||||
}
|
||||
|
||||
if (argv.f) {
|
||||
const configPath = argv.f
|
||||
return JSON.parse(fs.readFileSync(configPath))
|
||||
}
|
||||
|
||||
try {
|
||||
const globalConfigPath = path.resolve('/etc', 'lamassu', 'lamassu.json')
|
||||
return JSON.parse(fs.readFileSync(globalConfigPath))
|
||||
} catch (_) {
|
||||
try {
|
||||
const homeConfigPath = path.resolve(os.homedir(), '.lamassu', 'lamassu.json')
|
||||
return JSON.parse(fs.readFileSync(homeConfigPath))
|
||||
} catch (_) {
|
||||
console.error("Couldn't open lamassu.json config file.")
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const serverConfig = load()
|
||||
const serverConfig = load().opts
|
||||
const defaults = {logLevel: 'info'}
|
||||
const commandLine = {logLevel: argv.logLevel}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue