Thursday, January 10, 2013

Nodejs in admin land

Today I needed to install a node app on a remote server which uses monit as monitoring facility.

Sending the app over to the remote host was done using the 'git clone' command.
But now that the app is in place I need to monitor it.

Oh and I forgot to tell you that this app (a very lightweight and efficient app obviously ;) is ment to be deployed many times on the same server in order to serve the clients of some of my customers in an independent manner. Hence, I have to create multiple monit configuration files for each of the node apps to be monitored but they will all look the same.

So what I needed was a monitrc template file that I could render with different settings. First I remembered that I already did this kind of stuff when I was a Unix admin ... in perl ... it was a long time ago now. But wouldn't it be wonderful to use node for this also?

Yes, let's! :D

#!/usr/bin/env node

  Output the monitrc of this app

   OPTIONS='{"port":<port-number>, ...}' ./ 

var options = JSON.parse(process.env.OPTIONS || "null") || {};
options.port = options.port || 3000;
options.dir = options.dir || __dirname;


  'check process node with pidfile "<%- dir %>/../tmp/"',
  '  start program = "/usr/bin/env PORT=<%- port %> <%- dir %>/ start"',
  '  stop program = "<%- dir %>/ stop"',
  '  if failed port <%- port %> protocol HTTP',
  '    request /',
  '    with timeout 10 seconds',
  '    then restart'


Notice that dir is the dirname of the script because the script is installed in ./bin of the app directory. As you can see I use 'EJS' to manage the template part because EJS is a global module used in the apps. The script is a standard start/stop/status rc script which starts node.js.

How wonderful! I now not only use javascript on the client side, on the server side but also on the admin side...
One javascript to rule them all :)


No comments:

Post a Comment