Skip to content

Instantly share code, notes, and snippets.

@codemoran
Last active December 31, 2015 02:29
Show Gist options
  • Save codemoran/7921146 to your computer and use it in GitHub Desktop.
Save codemoran/7921146 to your computer and use it in GitHub Desktop.
Restart a Service on your local machine
#!/bin/env node
var exec = require('child_process').exec;
var http = require('http');
var querystring = require('querystring');
var COMMAND_TO_RUN = 'supervisorctl restart graphdat';
var PATH = '/restart-server';
var PORT = 8000;
var USERNAME = 'restart';
var PASSWORD = 'sup3rs3cr3t';
var server = http.createServer(function (req, res) {
function invalidContent() {
console.log('Invalid Content');
res.writeHead(400, {'Content-Type': 'text/plain'});
res.end('The body of the request is not valid JSON');
}
function invalidMethod() {
console.log('Invalid Method, tried to ' + req.method);
res.writeHead(405, {'Content-Type': 'text/plain'});
res.end('The resource ' + req.url + ' accepts POST only');
}
function noAccess(){
console.log('Not Authorized to make the call');
res.writeHead(401, {'Content-Type': 'text/plain'});
res.end('You do not have access to the resource ' + req.url);
}
function notFound() {
console.log('URL was not found: ' + req.url);
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('The URL ' + req.url + ' was not found');
}
function ignored() {
console.log('Ignored the URL');
res.writeHead(202, {'Content-Type': 'text/plain'});
res.end('The server ignored the request');
}
function decodeBase64(encoded) {
return (new Buffer(encoded, 'base64').toString('utf8'));
}
// verify the path is correct
if (req.url !== PATH)
return notFound();
// verify the authorization
var auth = req.headers.authorization;
if (auth === undefined)
return noAccess();
// we only support basic auth
var authSplit = auth.split(" ");
if (authSplit[0] !== "Basic")
return noAccess();
// check the username and password match
authSplit = decodeBase64(authSplit[1]).split(':');
var username = authSplit[0];
var password = authSplit[1];
if (username !== USERNAME || password !== PASSWORD)
return noAccess();
// get the POST params
if (req.method !== 'POST')
return invalidMethod();
// parse the parameters
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
try {
body = JSON.parse(body);
} catch(e) {
return invalidContent();
}
// we only execute the command when the alarm triggers, we ignore the
// resolved state because we are up and running already
if (!body || !body.status || body.status !== 'TRIGGERED')
return ignored();
exec(COMMAND_TO_RUN, function(err, stdout, stderr) {
var output = '';
if (stdout)
output += stdout;
if (stderr)
output += stderr;
if (err)
output += JSON.stringify(err);
console.log(output);
res.writeHead(200, {"Content-Type": "text/plain"});
res.end(output);
});
});
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(PORT);
// Put a friendly message on the terminal
console.log('Server running at http://127.0.0.1:%s/', PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment