Skip to content

Commit

Permalink
refactor: centralize SECRET/PUBLIC/MACHINE_NAME + change some wordings
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed May 10, 2018
1 parent c261413 commit d0a2a30
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
7 changes: 6 additions & 1 deletion constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ var csts = {

LOW_MEMORY_ENVIRONMENT : process.env.PM2_OPTIMIZE_MEMORY || false,

KEYMETRICS_ROOT_URL : process.env.KEYMETRICS_NODE || 'root.keymetrics.io',
MACHINE_NAME : process.env.INSTANCE_NAME || process.env.MACHINE_NAME,
SECRET_KEY : process.env.KEYMETRICS_SECRET || process.env.PM2_SECRET_KEY || process.env.SECRET_KEY,
PUBLIC_KEY : process.env.KEYMETRICS_PUBLIC || process.env.PM2_PUBLIC_KEY || process.env.PUBLIC_KEY,
KEYMETRICS_ROOT_URL : process.env.KEYMETRICS_NODE || process.env.ROOT_URL || process.env.INFO_NODE || 'root.keymetrics.io',


KEYMETRICS_BANNER : '../lib/motd',
KEYMETRICS_UPDATE : '../lib/motd.update',
DEFAULT_MODULE_JSON : 'package.json',
Expand Down
14 changes: 7 additions & 7 deletions lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ var IMMUTABLE_MSG = chalk.bold.blue('Use --update-env to update environment vari
* @param {String} [opts.pm2_home=[<paths.js>]] pm2 directory for log, pids, socket files
* @param {Boolean} [opts.independent=false] unique PM2 instance (random pm2_home)
* @param {Boolean} [opts.daemon_mode=true] should be called in the same process or not
* @param {String} [opts.public_key=null] keymetrics bucket public key
* @param {String} [opts.secret_key=null] keymetrics bucket secret key
* @param {String} [opts.machine_name=null] keymetrics instance name
* @param {String} [opts.public_key=null] pm2 plus bucket public key
* @param {String} [opts.secret_key=null] pm2 plus bucket secret key
* @param {String} [opts.machine_name=null] pm2 plus instance name
*/
class API {

Expand All @@ -54,9 +54,9 @@ class API {

this.daemon_mode = typeof(opts.daemon_mode) == 'undefined' ? true : opts.daemon_mode;
this.pm2_home = conf.PM2_ROOT_PATH;
this.public_key = process.env.KEYMETRICS_SECRET || opts.public_key || null;
this.secret_key = process.env.KEYMETRICS_PUBLIC || opts.secret_key || null;
this.machine_name = process.env.INSTANCE_NAME || opts.machine_name || null
this.public_key = conf.PUBLIC_KEY || opts.public_key || null;
this.secret_key = conf.SECRET_KEY || opts.secret_key || null;
this.machine_name = conf.MACHINE_NAME || opts.machine_name || null

/**
* CWD resolution
Expand Down Expand Up @@ -372,7 +372,7 @@ class API {
that.Client.executeRemote('notifyKillPM2', {}, function() {});

that.getVersion(function(err, new_version) {
// If not linked to keymetrics, and update pm2 to latest, display motd.update
// If not linked to PM2 plus, and update PM2 to latest, display motd.update
if (!that.gl_is_km_linked && !err && (pkg.version != new_version)) {
var dt = fs.readFileSync(path.join(__dirname, that._conf.KEYMETRICS_UPDATE));
console.log(dt.toString());
Expand Down
12 changes: 6 additions & 6 deletions lib/API/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ module.exports = function(CLI) {
var that = this;

if (cmd == 'stop' || cmd == 'kill') {
console.log(chalk.cyan('[Keymetrics.io]') + ' Stopping agent...');
console.log(chalk.cyan('[PM2 agent]') + ' Stopping agent...');
that.killInteract(function() {
console.log(chalk.cyan('[Keymetrics.io]') + ' Stopped');
console.log(chalk.cyan('[PM2 agent]') + ' Stopped');
return process.exit(cst.SUCCESS_EXIT);
});
return false;
}

if (cmd == 'info') {
console.log(chalk.cyan('[Keymetrics.io]') + ' Getting agent information...');
console.log(chalk.cyan('[PM2 agent]') + ' Getting agent information...');
that.interactInfos(function(err, infos) {
if (err) {
console.error(err.message);
Expand All @@ -106,10 +106,10 @@ module.exports = function(CLI) {
try {
fs.unlinkSync(cst.INTERACTION_CONF);
} catch(e) {
console.log(chalk.cyan('[Keymetrics.io]') + ' No interaction config file found');
console.log(chalk.cyan('[PM2 agent]') + ' No interaction config file found');
return process.exit(cst.SUCCESS_EXIT);
}
console.log(chalk.cyan('[Keymetrics.io]') + ' Agent interaction ended');
console.log(chalk.cyan('[PM2 agent]') + ' Agent interaction ended');
return process.exit(cst.SUCCESS_EXIT);
});
return false;
Expand All @@ -132,7 +132,7 @@ module.exports = function(CLI) {
}

if (cmd && !public_key) {
console.error(chalk.cyan('[Keymetrics.io]') + ' Command [%s] unknown or missing public key', cmd);
console.error(chalk.cyan('[PM2 agent]') + ' Command [%s] unknown or missing public key', cmd);
return process.exit(cst.ERROR_EXIT);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/binaries/Runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ commander.version(pkg.version)
.option('--auto-manage', 'keep application online after command exit')
.option('--fast-boot', 'boot app faster by keeping pm2 runtime online in background (effective at second exit/start)')
.option('--web [port]', 'launch process web api on [port] default to 9615')
.option('--secret [key]', 'keymetrics secret key')
.option('--public [key]', 'keymetrics public key')
.option('--machine-name [name]', 'keymetrics machine name')
.option('--secret [key]', 'PM2 plus secret key')
.option('--public [key]', 'PM2 plus public key')
.option('--machine-name [name]', 'PM2 plus machine name')
.option('--env [name]', 'select env_[name] env variables in process config file')
.option('--watch', 'Watch and Restart')
.option('-i --instances <number>', 'launch [number] instances with load-balancer')
Expand All @@ -32,9 +32,9 @@ commander.command('*')
.action(function(cmd){
pm2 = new PM2.custom({
pm2_home : path.join(process.env.HOME, '.pm3'),
secret_key : process.env.KEYMETRICS_SECRET || commander.secret,
public_key : process.env.KEYMETRICS_PUBLIC || commander.public,
machine_name : process.env.INSTANCE_NAME || commander.machineName
secret_key : cst.SECRET_KEY || commander.secret,
public_key : cst.PUBLIC_KEY || commander.public,
machine_name : cst.MACHINE_NAME || commander.machineName
});

pm2.connect(function() {
Expand Down
12 changes: 6 additions & 6 deletions lib/binaries/Runtime4Docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ process.env.PM2_DISCRETE_MODE = true;
commander.version(pkg.version)
.description('pm2-runtime is a drop-in replacement Node.js binary for containers')
.option('-i --instances <number>', 'launch [number] of processes automatically load-balanced. Increase overall performances and performance stability.')
.option('--secret [key]', '[MONITORING] keymetrics secret key')
.option('--secret [key]', '[MONITORING] PM2 plus secret key')
.option('--no-autorestart', 'start an app without automatic restart')
.option('--node-args <node_args>', 'space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"')
.option('-n --name <name>', 'set a <name> for script')
.option('--max-memory-restart <memory>', 'specify max memory amount used to autorestart (in octet or use syntax like 100M)')
.option('-c --cron <cron_pattern>', 'restart a running process based on a cron pattern')
.option('--interpreter <interpreter>', 'the interpreter pm2 should use for executing app (bash, python...)')
.option('--public [key]', '[MONITORING] keymetrics public key')
.option('--machine-name [name]', '[MONITORING] keymetrics machine name')
.option('--public [key]', '[MONITORING] PM2 plus public key')
.option('--machine-name [name]', '[MONITORING] PM2 plus machine name')
.option('--trace', 'enable transaction tracing with km')
.option('--v8', 'enable v8 data collecting')
.option('--format', 'output logs formated like key=val')
Expand Down Expand Up @@ -65,9 +65,9 @@ var Runtime = {
instanciate : function(cmd) {
this.pm2 = new PM2.custom({
pm2_home : process.env.PM2_HOME || path.join(process.env.HOME, '.pm2'),
secret_key : process.env.KEYMETRICS_SECRET || commander.secret,
public_key : process.env.KEYMETRICS_PUBLIC || commander.public,
machine_name : process.env.INSTANCE_NAME || commander.machineName,
secret_key : cst.SECRET_KEY || commander.secret,
public_key : cst.PUBLIC_KEY || commander.public,
machine_name : cst.MACHINE_NAME || commander.machineName,
daemon_mode : process.env.PM2_RUNTIME_DEBUG || false
});

Expand Down

0 comments on commit d0a2a30

Please sign in to comment.