Skip to content

Commit

Permalink
refactor(redsmin): fixes #18, fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
FGRibreau committed Mar 29, 2015
1 parent 0c460b4 commit a6f2fb3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 116 deletions.
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
var fs = require('fs');
var net = require('net');
var tls = require('tls');
var path = require('path');

var log = require('./lib/log')('debug');
var config = require('./lib/config')(log);
var Config = require('./lib/config')(log);
var config = new Config(Config.config.config_file);

var jsonPackage = JSON.parse(fs.readFileSync('./package.json'));
var jsonPackage = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json')));

var Endpoint = require('./lib/Endpoint')(log, jsonPackage, tls, process);

Expand Down
104 changes: 1 addition & 103 deletions bin/redsmin
Original file line number Diff line number Diff line change
@@ -1,105 +1,3 @@
#!/usr/bin/env node

var log = require('../lib/log')('info');
var Config = require('../lib/config')(log);
var config = new Config(Config.config.config_file);
var p = require('path');
var fs = require('fs');
var daemon = require("daemonize2").setup({
main: p.resolve(__dirname, "../app.js"),
name: "redsmin",
pidfile: p.resolve(__dirname, "../etc/redsmin.pid"),
silent: true
});

log.cli();

daemon.on("starting", function () {
log.info("Starting redsmin daemon...");
log.debug("Connecting on Redis at " + config.redis);
}).on("started", function (pid) {
log.info("Redsmin daemon started. PID: " + pid);
}).on("stopping", function () {
log.info("Stopping redsmin daemon...");
}).on("stopped", function (pid) {
log.info("Redsmin daemon stopped.");
}).on("running", function (pid) {
log.info("Redsmin daemon already running. PID: " + pid);
}).on("notrunning", function () {
log.info("Redsmin daemon is not running");
}).on("error", function (err) {
log.error("Redsmin daemon failed to start: " + err.message);
// log.error("-- Please create a report with the following text --");
// log.info();
// try{
// fs.readFileSync(p.resolve(__dirname, '../etc/log.log'), );
// }
});

function usage() {
log.info("Usage: [start|stop|kill|set_key|path|restart|reload|status]");
}

if (!process.argv[2]) {
return usage();
}

switch (process.argv[2]) {

case "start":
// This will trigger an error if there is something with the configuration
daemon.start().once("started", function () {
process.exit();
});
break;

case "stop":
daemon.stop();
break;

case "set_key":
if (!process.env.RKEY) {
return log.info('Usage: RKEY=REDSMIN_CONNECTION_KEY [RURL=redis://127.0.0.1:6379] [RAUTH=password] redsmin set_key');
}

config.write(process.env.RKEY, process.env.RURL, process.env.RAUTH, function (err) {
if (err) {
log.error("Could not write the configuration file", err);
return process.exit(1);
}

log.info("Configuration file written");
});
break;

case "path":
console.log(p.resolve(__dirname, '../'));
break;

case "kill":
daemon.kill();
break;

case "restart":
if (daemon.status()) {
daemon.stop().once("stopped", function () {
daemon.start().once("started", function () {
process.exit();
});
});
} else {
daemon.start().once("started", function () {
process.exit();
});
}
break;

case "status":
var pid = daemon.status();
if (pid) log.info("Redmsin daemon running. PID: " + pid);
else log.error("Redsmin daemon is not running.");
break;

default:
usage();
}
require('../app');
31 changes: 22 additions & 9 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ var path = require('path');
var assert = require('assert');

var KEY_REGEX = /^[a-f0-9]{24}$/i;
module.exports = function (log) {
module.exports = function (log, DISPLAY_LOG) {
assert(_.isObject(log));

var config = require('common-env')(log).getOrElseAll({
var commonEnv = require('common-env');
commonEnv.DISPLAY_LOG = DISPLAY_LOG !== false;

var envConfig = commonEnv(log).getOrElseAll({
debug: false,
config_file: path.resolve(__dirname, '../etc/redsmin.json'),

Expand All @@ -29,8 +32,8 @@ module.exports = function (log) {
}
});

process.env.NODE_TLS_REJECT_UNAUTHORIZED = config.redsmin.reject_unauthorized;
assert(_.isString(config.config_file));
process.env.NODE_TLS_REJECT_UNAUTHORIZED = envConfig.redsmin.reject_unauthorized;
assert(_.isString(envConfig.config_file));
/**
* Strip all Javascript type comments from the string.
* (extracted from http://lorenwest.github.com/node-config/ )
Expand Down Expand Up @@ -94,7 +97,8 @@ module.exports = function (log) {

function Config(jsonFilename) {
assert(_.isString(jsonFilename));
_.extend(this, config);
this.debug = envConfig.debug;


this.jsonFilename = fs.realpathSync(jsonFilename);
var fileContent = fs.readFileSync(this.jsonFilename, 'UTF-8');
Expand All @@ -108,10 +112,19 @@ module.exports = function (log) {
return false;
}


this.redsmin = {
port: envConfig.redsmin.port,
hostname: envConfig.redsmin.hostname,
key: envConfig.redsmin.key || json.key
};

this.redis = {
uri: json.redis || envConfig.redis.uri,
auth: json.auth || envConfig.redis.auth
};

// valid the connection key
this.redis.uri = json.redis || this.redis.uri;
this.redis.auth = json.auth || this.redis.auth;
this.redsmin.key = json.key || this.redsmin.key;
this.checkKey(this.redsmin.key, function (err) {
if (err) {
log.error(err);
Expand Down Expand Up @@ -160,6 +173,6 @@ module.exports = function (log) {
};

Config.INVALID_KEY = 'Invalid connection key "{key}", please browse http://bit.ly/YAIeAM';
Config.config = config; // expose env. vars. config
Config.config = envConfig; // expose env. vars. config
return Config;
};
2 changes: 0 additions & 2 deletions lib/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('config', function () {
t.strictEqual(config.redsmin.key, '5331e06500617e0b0a0000aa');
t.strictEqual(config.redsmin.port, 993);
t.strictEqual(config.redsmin.hostname, 'ssl.redsmin.com');
t.strictEqual(config.redsmin.reject_unauthorized, 1);
});

describe('write', function () {
Expand Down Expand Up @@ -54,7 +53,6 @@ describe('config', function () {
t.strictEqual(config.redsmin.key, KEY);
t.strictEqual(config.redsmin.port, 993);
t.strictEqual(config.redsmin.hostname, 'ssl.redsmin.com');
t.strictEqual(config.redsmin.reject_unauthorized, 1);

t.strictEqual(fs.readFileSync(FILE, 'utf-8'), JSON.stringify({
key: KEY,
Expand Down

0 comments on commit a6f2fb3

Please sign in to comment.