-
-
Save matschaffer/5039503 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint multistr:true */ | |
var fs = require('fs'), | |
exec = require('child_process').exec, | |
path = require('path'), | |
os = require('os'); | |
var Sentinel = require("node-sentinel"), | |
_ = require("underscore"), | |
async = require("async"), | |
nodemailer = require("nodemailer"); | |
function Agent(config){ | |
if(!_.isObject(config)){ | |
return console.error("Bad config"); | |
} | |
this.nutcracker_config_file = config.nutcracker_config_file; | |
this.redis_sentinel_ip = config.redis_sentinel_ip; | |
this.redis_sentinel_port = config.redis_sentinel_port; | |
this.log = ""; | |
} | |
Agent.prototype.restart_nutcracker = function(callback){ | |
var self = this; | |
var child = exec( | |
"/etc/init.d/nutcracker restart", | |
function(error, stdout, stderr) { | |
self.log += "<h2>Nutcracker restarted with outuput:</h2>"; | |
self.log += "<div><pre>"+stdout+"</pre></div>"; | |
if (error !== null) { | |
self.log += "<h2>Nutcracker failed restarting with error:</h2>"; | |
self.log += "<div><pre>"+error+"</pre></div>"; | |
} | |
return callback(); | |
} | |
); | |
}; | |
Agent.prototype.update_nutcracker_config = function(data, callback){ | |
var old_content = fs.readFileSync(this.nutcracker_config_file, 'utf8'); | |
var new_content = old_content.replace( | |
data.details["old-ip"]+":"+data.details["old-port"], | |
data.details["new-ip"]+":"+data.details["new-port"] | |
); | |
fs.writeFileSync(this.nutcracker_config_file, new_content, 'utf8'); | |
this.log += "<h2>Nutcracker config updated with new content:</h2>"; | |
this.log += "<div><pre>"+new_content+"</pre></div>"; | |
return callback(); | |
}; | |
Agent.prototype.send_mail = function() { | |
var self = this; | |
var transport = nodemailer.createTransport("Sendmail"); | |
var mailOptions = { | |
from: "[email protected]", | |
to: "[email protected]", | |
subject: "Warning: Nutcracker restarted forced on "+os.hostname(), | |
generateTextFromHTML: true, | |
html: "<h1>Nutcracker was restarted due to master switch in redis configuration.</h1>" + this.log | |
}; | |
transport.sendMail(mailOptions, function(error, responseStatus){ | |
self.log = ""; | |
if(error) { | |
return console.error(error); | |
} | |
return console.info( | |
responseStatus.message+"\n"+responseStatus.messageId | |
); | |
}); | |
}; | |
Agent.prototype.switch_master_handler = function(){ | |
var self = this; | |
return function(data) { | |
async.series([ | |
function(callback) { | |
self.update_nutcracker_config(data, callback); | |
}, | |
function(callback) { | |
self.restart_nutcracker(callback); | |
} | |
], | |
function(){ | |
return self.send_mail(); | |
}); | |
}; | |
}; | |
Agent.prototype.start_sentinel = function(){ | |
this.sentinel = new Sentinel( | |
this.redis_sentinel_ip, this.redis_sentinel_port | |
); | |
this.sentinel.on("switch-master", this.switch_master_handler()); | |
}; | |
Agent.prototype.check_nutcracker_config = function(cb){ | |
fs.appendFile(this.nutcracker_config_file, "", cb); | |
}; | |
Agent.prototype.bootstrap = function(){ | |
var self = this; | |
this.check_nutcracker_config( | |
function(error){ | |
if(error) { | |
return console.error("Nutcracker config file: "+error); | |
} | |
return self.start_sentinel(); | |
} | |
); | |
}; | |
module.exports = Agent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
"nutcracker_config_file": "/etc/nutcracker/nutcracker.yml", | |
"redis_sentinel_ip": "127.0.0.1", | |
"redis_sentinel_port": "26379" | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require("path"), | |
Agent = require(path.join(__dirname, "./lib/agent.js")); | |
var config = require( | |
path.join(__dirname, "./config_"+process.env.NODE_ENV) | |
); | |
module.exports = new Agent(config).bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "redis_nutcracker_agent", | |
"version": "0.0.1", | |
"description": "Redis agent for nutcracker update", | |
"dependencies": { | |
"node-sentinel": "0.0.3", | |
"underscore": "~1.4.4", | |
"async": "~0.2.5", | |
"nodemailer": "~0.3.42" | |
}, | |
"engine": "node >= 0.8.16" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment