The Web Listener

Listening for Github Webhooks

We'll use NodeJS to create a simple web listener. It will listen for web hooks and kick off a build of the Fabric script.

NodeJS Web Listener

We'll install some dependencies so make listening for webhooks easier. Log into the build server as user deployer and run the following:

cd ~/build-server
npm init
# Install Express web framework
npm install --save express

Edit index.js to do some web hook listening:

var express = require('express');
var app = express();

var exec = require('child_process').exec;

/**
 * A custom webhook for ourselves to call externally
 *   Perhaps from Slack or another chatroom
 *   Or from a CI server
 */
app.post('/deploy', function (req, res) {
    deploy_app()
    res.send('Deploying');
});

/**
 * Make ExpressJS application listen
 */
var server = app.listen(8080, '0.0.0.0',function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Deployer listening at http://%s:%s', host, port);
});

/**
 * Do the actual application deploment
 * @return null
 */
function deploy_app()
{
    var options = {
        cwd: '/home/deployer/build-server',
        shell: '/bin/bash',
        maxBuffer: 1048576 // One MB
        // uid: //deployer
        // gid: // deployer
    };

    var childProcess = exec(
        'source .venv/bin/activate && python deploy.py',
        options,
        function(error, stdout, stderr)
        {
            // Possibly log stdout/stderr
            // Do error checking
            console.log(error, stdout, stderr)
        });

    return childProcess;
}

Note that we're sourcing our Python virtualenv and then running the deploy.py script.

Then we can try running this.

# I use tmux and split the screen via ctrl+b, shift+6
tmux

# On the right panel:
node index.js

# On the left panel:
curl -X POST localhost:8080/deploy

We'll see the deployment outout on the right, since the output of the python command (Fabfile) is output via console.log in our NodeJS code.

Resources