MCPcopy
hub / github.com/Unitech/pm2 / getOrSetConf

Method getOrSetConf

modules/pm2-io-agent/src/InteractorClient.js:354–411  ·  view source on GitHub ↗

* Retrieve Interactor configuration from env, params and filesystem. * @param {Object} cst global constants * @param {Object} infos data used to start the interactor [optional] * @param {String} infos.secret_key the secret key used to cipher data [optional] * @param {String} infos.public

(cst, infos, cb)

Source from the content-addressed store, hash-verified

352 * @param {Function} cb invoked with <err, configuration>
353 */
354 static getOrSetConf (cst, infos, cb) {
355 infos = infos || {}
356 let configuration = {
357 version_management: {
358 active: true
359 }
360 }
361 let confFS = {}
362
363 // Try loading configuration file on FS
364 try {
365 let fileContent = fs.readFileSync(cst.INTERACTION_CONF).toString()
366 // Handle old configuration with json5
367 fileContent = fileContent.replace(/\s(\w+):/g, '"$1":')
368 // parse
369 confFS = JSON.parse(fileContent)
370
371 if (confFS.version_management) {
372 configuration.version_management.active = confFS.version_management.active
373 }
374 } catch (e) {
375 log('Interaction file does not exists')
376 }
377
378 // load the configration (first have priority)
379 // -> from env variable
380 // -> from params (eg. CLI)
381 // -> from configuration on FS
382 configuration.public_key = process.env.PM2_PUBLIC_KEY || process.env.KEYMETRICS_PUBLIC || infos.public_key || confFS.public_key
383 configuration.secret_key = process.env.PM2_SECRET_KEY || process.env.KEYMETRICS_SECRET || infos.secret_key || confFS.secret_key
384 configuration.machine_name = process.env.PM2_MACHINE_NAME || process.env.INSTANCE_NAME || infos.machine_name || confFS.machine_name || `${os.hostname()}-${require('crypto').randomBytes(2).toString('hex')}`
385 configuration.pm2_version = process.env.PM2_VERSION || infos.pm2_version || confFS.pm2_version
386 configuration.reverse_interact = confFS.reverse_interact || true
387 // is setup empty ? use the one provided in env OR root OTHERWISE get the one on FS conf OR fallback on root
388 configuration.info_node = process.env.KEYMETRICS_NODE || infos.info_node || confFS.info_node || cst.KEYMETRICS_ROOT_URL
389
390
391 if (!configuration.secret_key) {
392 log('Secret key is not defined in configuration', configuration)
393 return cb(new Error('secret key is not defined'))
394 }
395 if (!configuration.public_key) {
396 log('Public key is not defined in configuration', configuration)
397 return cb(new Error('public key is not defined'))
398 }
399
400 // write configuration on FS
401 try {
402 fs.writeFileSync(cst.INTERACTION_CONF, JSON.stringify(configuration, null, 4))
403 } catch (e) {
404 console.error('Error when writting configuration file %s', cst.INTERACTION_CONF)
405 return cb(e)
406 }
407 if (configuration.info_node.indexOf('http') === -1) { // handle old file
408 configuration.info_node = `https://${configuration.info_node}`
409 }
410 return cb(null, configuration)
411 }

Callers 3

launchAndInteractMethod · 0.95

Calls 4

cbFunction · 0.85
toStringMethod · 0.80
indexOfMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected