From a4739134052680e5ea7abf1863b3910afba063f5 Mon Sep 17 00:00:00 2001 From: Nathan Rijksen Date: Tue, 22 Apr 2014 16:33:19 -0400 Subject: [PATCH] first commit --- .gitignore | 1 + daemon | 31 ++++++ deployment.log | 265 +++++++++++++++++++++++++++++++++++++++++++++++++ deployments.js | 10 ++ package.json | 12 +++ readme.md | 18 ++++ server.js | 197 ++++++++++++++++++++++++++++++++++++ 7 files changed, 534 insertions(+) create mode 100644 .gitignore create mode 100755 daemon create mode 100644 deployment.log create mode 100644 deployments.js create mode 100644 package.json create mode 100644 readme.md create mode 100644 server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ccbe46 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules/ diff --git a/daemon b/daemon new file mode 100755 index 0000000..a7c5d5a --- /dev/null +++ b/daemon @@ -0,0 +1,31 @@ +#!/bin/bash +# Deployment daemon +# chkconfig: 345 20 80 +# description: Manages the komodo-web-deploy instance, basically just wraps "forever" +# processname: ko-deploy + +DAEMON_PATH="/home/deployment/komodo-web-deploy" +DAEMON="node server.js" +NAME="ko-deploy" +USER="nathanr" + +case "$1" in +start) + su $USER -c "$DAEMON_PATH/node_modules/forever/bin/forever stop $DAEMON_PATH/server.js" +;; +status) + forever list +;; +stop) + su $USER -c "$DAEMON_PATH/node_modules/forever/bin/forever stop $DAEMON_PATH/server.js" +;; + +restart) + $0 stop + $0 start +;; + +*) + echo "Usage: $0 {status|start|stop|restart}" + exit 1 +esac diff --git a/deployment.log b/deployment.log new file mode 100644 index 0000000..a628377 --- /dev/null +++ b/deployment.log @@ -0,0 +1,265 @@ +{"level":"info","message":"Listening on port 8282","timestamp":"2014-04-22T16:48:38.592Z"} +{"level":"info","message":"Listening on port 8282","timestamp":"2014-04-22T16:49:55.480Z"} +{"level":"info","message":"Listening on port 8282","timestamp":"2014-04-22T16:50:01.855Z"} +{"level":"info","message":"Listening on port 8282","timestamp":"2014-04-22T16:50:13.547Z"} +{"level":"info","message":"Listening on port 8282","timestamp":"2014-04-22T16:50:24.850Z"} +{"level":"info","message":"Listening on port 8282","timestamp":"2014-04-22T16:52:51.787Z"} +{"level":"info","message":"======================================================================","timestamp":"2014-04-22T16:52:52.320Z"} +{"level":"info","message":"Running job /Users/nathanrijksen/Projects/komodo-website-master","timestamp":"2014-04-22T16:52:52.320Z"} +{"level":"info","message":"======================================================================","timestamp":"2014-04-22T16:52:52.320Z"} +{"level":"info","message":"\nExecuting npm prune","timestamp":"2014-04-22T16:52:52.321Z"} +{"level":"info","message":"\nExecuting npm install","timestamp":"2014-04-22T16:52:52.847Z"} +{"level":"info","message":"\nExecuting bower prune","timestamp":"2014-04-22T16:52:54.395Z"} +{"level":"info","message":"\nExecuting bower install","timestamp":"2014-04-22T16:52:56.522Z"} +{"level":"info","message":"Done deploying komodo-website-master","timestamp":"2014-04-22T16:52:58.607Z"} +2014-04-22T16:54:29.674Z - info: Listening on port 8282 +2014-04-22T16:54:30.243Z - info: ====================================================================== +2014-04-22T16:54:30.243Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T16:54:30.243Z - info: ====================================================================== +2014-04-22T16:54:30.244Z - info: Executing npm prune +2014-04-22T16:54:30.759Z - info: Executing npm install +2014-04-22T16:54:32.217Z - info: Executing bower prune +2014-04-22T16:54:34.259Z - info: Executing bower install +2014-04-22T16:54:36.170Z - info: Done deploying komodo-website-master +2014-04-22T17:08:46.463Z - info: Listening on port 8282 +2014-04-22T17:08:47.033Z - info: ====================================================================== +2014-04-22T17:08:47.034Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:08:47.034Z - info: ====================================================================== +2014-04-22T17:08:47.035Z - info: Executing npm prune +2014-04-22T17:08:47.656Z - info: Executing npm install +2014-04-22T17:08:49.294Z - info: Executing bower prune +2014-04-22T17:08:51.932Z - info: Executing bower install +2014-04-22T17:08:54.040Z - info: Executing docpad clean +2014-04-22T17:08:54.301Z - info: Executing docpad generate +2014-04-22T17:08:54.544Z - info: Done deploying komodo-website-master +2014-04-22T17:10:11.722Z - info: Listening on port 8282 +2014-04-22T17:10:12.251Z - info: ====================================================================== +2014-04-22T17:10:12.251Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:10:12.252Z - info: ====================================================================== +2014-04-22T17:10:12.253Z - info: Executing npm prune +2014-04-22T17:10:12.867Z - info: Executing npm install +2014-04-22T17:10:14.544Z - info: Executing bower prune +2014-04-22T17:10:16.715Z - info: Executing bower install +2014-04-22T17:10:18.754Z - info: Executing docpad clean +2014-04-22T17:10:18.968Z - info: Executing docpad generate +2014-04-22T17:10:19.179Z - info: Done deploying komodo-website-master +2014-04-22T17:16:30.135Z - info: Listening on port 8282 +2014-04-22T17:16:31.072Z - info: ====================================================================== +2014-04-22T17:16:31.072Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:16:31.072Z - info: ====================================================================== +2014-04-22T17:16:31.073Z - info: Executing npm prune +2014-04-22T17:16:32.133Z - info: Executing npm install +2014-04-22T17:16:34.426Z - info: Executing bower prune +2014-04-22T17:16:36.831Z - info: Executing bower install +2014-04-22T17:16:38.570Z - info: Executing docpad clean +2014-04-22T17:16:41.804Z - info: Executing docpad generate +2014-04-22T17:20:49.313Z - info: Listening on port 8282 +2014-04-22T17:20:49.937Z - info: ====================================================================== +2014-04-22T17:20:49.938Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:20:49.938Z - info: ====================================================================== +2014-04-22T17:20:49.939Z - info: Executing npm prune +2014-04-22T17:20:51.316Z - info: Executing npm install +2014-04-22T17:20:53.625Z - info: Executing bower prune +2014-04-22T17:20:56.318Z - info: Executing bower install +2014-04-22T17:20:58.434Z - info: Executing docpad clean +2014-04-22T17:21:00.997Z - info: Executing docpad generate --debug +2014-04-22T17:21:07.503Z - error: Error while deploying komodo-website-master: Error: stdout maxBuffer exceeded. +2014-04-22T17:21:07.504Z - info: Done deploying komodo-website-master +2014-04-22T17:22:22.040Z - info: Listening on port 8282 +2014-04-22T17:22:22.077Z - info: Scheduling deployment for /Users/nathanrijksen/Projects/komodo-website-master : master +2014-04-22T17:22:22.082Z - info: Deploying komodo-website-master +2014-04-22T17:22:22.599Z - info: ====================================================================== +2014-04-22T17:22:22.599Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:22:22.599Z - info: ====================================================================== +2014-04-22T17:22:22.600Z - info: Executing npm prune +2014-04-22T17:22:23.930Z - info: Executing npm install +2014-04-22T17:23:36.130Z - error: Error while deploying komodo-website-master: Error: Command failed: npm http GET https://github.com/Komodo/docpad-plugin-scheduling/tarball/compiled +npm http GET https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm http GET https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm http 503 https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm ERR! fetch failed https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm http 502 https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm ERR! fetch failed https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm http 200 https://github.com/Komodo/docpad-plugin-scheduling/tarball/compiled +npm http GET https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm http GET https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm http 503 https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm http 503 https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm ERR! fetch failed https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm ERR! fetch failed https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm http GET https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm http GET https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm http 503 https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm ERR! fetch failed https://github.com/ervwalter/docpad-plugin-paged/tarball/compiled +npm ERR! Error: 503 Service Unavailable +npm ERR! at WriteStream. (/usr/local/Cellar/node/0.10.20/lib/node_modules/npm/lib/utils/fetch.js:57:12) +npm ERR! at WriteStream.EventEmitter.emit (events.js:117:20) +npm ERR! at fs.js:1596:14 +npm ERR! at /usr/local/Cellar/node/0.10.20/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:103:5 +npm ERR! at Object.oncomplete (fs.js:107:15) +npm ERR! If you need help, you may report this log at: +npm ERR! +npm ERR! or email it to: +npm ERR! + +npm ERR! System Darwin 13.1.0 +npm ERR! command "/usr/local/Cellar/node/0.10.20/bin/node" "/usr/local/bin/npm" "install" +npm ERR! cwd /Users/nathanrijksen/Projects/komodo-website-master +npm ERR! node -v v0.10.20 +npm ERR! npm -v 1.3.11 +npm http 503 https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm ERR! fetch failed https://github.com/Komodo/docpad-plugin-partials/tarball/compiled +npm ERR! +npm ERR! Additional logging details can be found in: +npm ERR! /Users/nathanrijksen/Projects/komodo-website-master/npm-debug.log +npm ERR! not ok code 0 + +2014-04-22T17:23:36.130Z - info: Done deploying komodo-website-master +2014-04-22T17:24:04.420Z - info: Listening on port 8282 +2014-04-22T17:24:05.357Z - info: ====================================================================== +2014-04-22T17:24:05.358Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:24:05.358Z - info: ====================================================================== +2014-04-22T17:24:05.359Z - info: Executing npm prune +2014-04-22T17:24:06.443Z - info: Executing npm install +2014-04-22T17:24:25.950Z - info: Listening on port 8282 +2014-04-22T17:24:25.977Z - info: Scheduling deployment for /Users/nathanrijksen/Projects/komodo-website-master : master +2014-04-22T17:24:25.982Z - info: Deploying komodo-website-master +2014-04-22T17:24:26.508Z - info: ====================================================================== +2014-04-22T17:24:26.508Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:24:26.508Z - info: ====================================================================== +2014-04-22T17:24:26.509Z - info: Executing npm prune +2014-04-22T17:24:27.763Z - info: Executing bower prune +2014-04-22T17:24:30.351Z - info: Executing bower install +2014-04-22T17:24:32.460Z - info: Executing docpad clean +2014-04-22T17:24:35.556Z - info: Executing docpad generate --debug 6 +2014-04-22T17:33:22.048Z - info: Listening on port 8282 +2014-04-22T17:33:22.706Z - info: ====================================================================== +2014-04-22T17:33:22.707Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T17:33:22.707Z - info: ====================================================================== +2014-04-22T17:33:22.708Z - info: Executing npm prune +2014-04-22T17:33:24.018Z - info: Executing bower prune +2014-04-22T17:33:26.980Z - info: Executing bower install +2014-04-22T17:33:29.248Z - info: Executing docpad clean +2014-04-22T17:33:32.981Z - info: Executing docpad generate --silent +2014-04-22T17:34:17.340Z - info: Done deploying komodo-website-master +2014-04-22T19:34:40.195Z - info: Listening on port 8282 +2014-04-22T19:34:40.759Z - info: ====================================================================== +2014-04-22T19:34:40.759Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:34:40.759Z - info: ====================================================================== +2014-04-22T19:34:40.760Z - info: Executing npm prune +2014-04-22T19:34:41.999Z - info: Executing npm install +2014-04-22T19:34:44.028Z - info: Executing bower prune +2014-04-22T19:34:46.649Z - info: Executing bower install +2014-04-22T19:34:48.685Z - info: Executing docpad clean +2014-04-22T19:36:59.727Z - info: Listening on port 8282 +2014-04-22T19:37:00.338Z - info: ====================================================================== +2014-04-22T19:37:00.338Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:37:00.339Z - info: ====================================================================== +2014-04-22T19:37:00.339Z - info: Executing npm prune +2014-04-22T19:37:01.914Z - info: Executing npm install +2014-04-22T19:37:09.559Z - info: Executing bower prune +2014-04-22T19:37:12.265Z - info: Executing bower install +2014-04-22T19:37:14.462Z - info: Executing docpad clean +2014-04-22T19:37:17.921Z - info: Executing docpad generate --silent +2014-04-22T19:38:31.818Z - info: Listening on port 8282 +2014-04-22T19:38:32.396Z - info: ====================================================================== +2014-04-22T19:38:32.397Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:38:32.397Z - info: ====================================================================== +2014-04-22T19:38:32.397Z - info: Executing npm prune +2014-04-22T19:38:33.506Z - info: Executing npm install +2014-04-22T19:40:18.565Z - info: Listening on port 8282 +2014-04-22T19:40:20.098Z - info: ====================================================================== +2014-04-22T19:40:20.099Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:40:20.099Z - info: ====================================================================== +2014-04-22T19:40:20.100Z - info: Executing npm prune +2014-04-22T19:40:21.247Z - info: Executing npm install +2014-04-22T19:40:23.441Z - info: Executing bower prune +2014-04-22T19:40:25.286Z - info: Executing bower install +2014-04-22T19:40:27.091Z - info: Executing docpad clean +2014-04-22T19:40:30.646Z - info: Executing docpad generate --silent +2014-04-22T19:41:21.303Z - info: Listening on port 8282 +2014-04-22T19:41:22.281Z - info: ====================================================================== +2014-04-22T19:41:22.281Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:41:22.281Z - info: ====================================================================== +2014-04-22T19:41:22.282Z - info: Executing npm prune +2014-04-22T19:41:23.943Z - info: Executing npm install +2014-04-22T19:41:26.662Z - info: Executing bower prune +2014-04-22T19:41:28.858Z - info: Executing bower install +2014-04-22T19:41:30.916Z - info: Executing docpad clean +2014-04-22T19:45:21.712Z - info: Listening on port 8282 +2014-04-22T19:45:21.724Z - info: Scheduling deployment for /Users/nathanrijksen/Projects/komodo-website-master : master +2014-04-22T19:45:21.743Z - info: Deploying komodo-website-master +2014-04-22T19:45:22.272Z - info: ====================================================================== +2014-04-22T19:45:22.272Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:45:22.272Z - info: ====================================================================== +2014-04-22T19:45:22.273Z - info: Executing npm prune +2014-04-22T19:45:23.418Z - info: Executing npm install +2014-04-22T19:45:25.406Z - info: Executing bower prune +2014-04-22T19:45:28.525Z - info: Executing bower install +2014-04-22T19:45:30.813Z - info: Executing docpad clean +2014-04-22T19:45:34.069Z - info: Executing docpad generate --silent +2014-04-22T19:46:07.596Z - info: Listening on port 8282 +2014-04-22T19:46:08.525Z - info: ====================================================================== +2014-04-22T19:46:08.526Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:46:08.526Z - info: ====================================================================== +2014-04-22T19:46:08.527Z - info: Executing npm prune +2014-04-22T19:46:09.654Z - info: Executing npm install +2014-04-22T19:46:11.963Z - info: Executing bower prune +2014-04-22T19:46:13.949Z - info: Executing bower install +2014-04-22T19:46:15.777Z - info: Executing docpad clean +2014-04-22T19:46:17.830Z - info: Executing docpad generate --silent +2014-04-22T19:46:36.973Z - info: Listening on port 8282 +2014-04-22T19:46:37.939Z - info: ====================================================================== +2014-04-22T19:46:37.940Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:46:37.940Z - info: ====================================================================== +2014-04-22T19:46:37.940Z - info: Executing npm prune +2014-04-22T19:46:39.138Z - info: Executing npm install +2014-04-22T19:46:41.840Z - info: Executing bower prune +2014-04-22T19:46:44.044Z - info: Executing bower install +2014-04-22T19:46:46.174Z - info: Executing docpad clean +2014-04-22T19:48:07.265Z - info: Listening on port 8282 +2014-04-22T19:48:07.825Z - info: ====================================================================== +2014-04-22T19:48:07.825Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T19:48:07.825Z - info: ====================================================================== +2014-04-22T19:48:07.826Z - info: Executing npm prune +2014-04-22T19:48:08.961Z - info: Executing npm install +2014-04-22T19:48:10.946Z - info: Executing bower prune +2014-04-22T19:48:12.781Z - info: Executing bower install +2014-04-22T19:48:14.780Z - info: Executing docpad clean +2014-04-22T19:48:17.191Z - info: Executing docpad generate --silent +2014-04-22T20:00:04.388Z - info: Listening on port 8282 +2014-04-22T20:00:08.035Z - info: ====================================================================== +2014-04-22T20:00:08.035Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T20:00:08.035Z - info: ====================================================================== +2014-04-22T20:00:08.036Z - info: Executing npm prune +2014-04-22T20:00:09.465Z - info: Executing npm install +2014-04-22T20:00:11.794Z - info: Executing bower prune +2014-04-22T20:00:14.560Z - info: Executing bower install +2014-04-22T20:00:16.716Z - info: Executing docpad clean +2014-04-22T20:00:19.633Z - info: Executing docpad generate --silent +2014-04-22T20:01:06.681Z - info: Done deploying komodo-website-master +2014-04-22T20:10:50.168Z - info: Listening on port 8282 +2014-04-22T20:10:51.128Z - info: ====================================================================== +2014-04-22T20:10:51.128Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T20:10:51.129Z - info: ====================================================================== +2014-04-22T20:10:51.129Z - info: Executing npm prune +2014-04-22T20:10:52.586Z - info: Executing npm install +2014-04-22T20:10:54.872Z - info: Executing bower prune +2014-04-22T20:10:57.378Z - info: Executing bower install +2014-04-22T20:10:59.225Z - info: Executing docpad clean +2014-04-22T20:11:02.240Z - info: Executing docpad generate --silent +2014-04-22T20:11:35.692Z - info: Done deploying komodo-website-master +2014-04-22T20:13:37.237Z - info: Received push event +2014-04-22T20:13:59.240Z - info: Received push event +2014-04-22T20:14:11.323Z - info: Received push event +2014-04-22T20:14:11.324Z - info: Deploying komodo-website-master +2014-04-22T20:14:11.855Z - info: ====================================================================== +2014-04-22T20:14:11.855Z - info: Running job /Users/nathanrijksen/Projects/komodo-website-master +2014-04-22T20:14:11.855Z - info: ====================================================================== +2014-04-22T20:14:11.856Z - info: Executing npm prune +2014-04-22T20:14:13.237Z - info: Executing npm install +2014-04-22T20:14:15.237Z - info: Executing bower prune +2014-04-22T20:14:17.776Z - info: Executing bower install +2014-04-22T20:14:19.693Z - info: Executing docpad clean +2014-04-22T20:14:22.889Z - info: Executing docpad generate --silent +2014-04-22T20:15:01.629Z - info: Done deploying komodo-website-master diff --git a/deployments.js b/deployments.js new file mode 100644 index 0000000..2ec67be --- /dev/null +++ b/deployments.js @@ -0,0 +1,10 @@ +module.exports = +{ + "komodo-website": // repository + { + "master": // branch + { + path: __dirname + "/../komodo-website-master" + } + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..c78995c --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "komodo-web-deploy", + "description": "Minimal deployment system for Komodo web apps", + "version": "0.0.1", + "private": true, + "dependencies": { + "express": "3.x", + "cron": "~1.0.4", + "winston": "~0.7.3", + "forever": "~0.11.0" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d4ae45a --- /dev/null +++ b/readme.md @@ -0,0 +1,18 @@ +This is a small and very simple deployment script that allows us to automatically +deploy eg. komodoide.com whenever we push to our deployment branches. + +It is designed specifically with static sites in mind, and will only work if +both deployer and deployment configurations are on the same server, in the same +parent directory. + +For a site to be valid for deployment it must have a deploy.js library in its +root which exposes the following methods: + + * init(logger) + * run(params, done) + * schedule(branch, cron, deploy) - optional, if you want to schedule deployments + via cron + +To learn more about how this works please look at [deploy.js] on the komodo-website repository + + [deploy.js]: https://github.com/Komodo/komodo-website/blob/master/deploy.js diff --git a/server.js b/server.js new file mode 100644 index 0000000..db11e34 --- /dev/null +++ b/server.js @@ -0,0 +1,197 @@ +var server = new function() +{ + var express = require('express'); + var sys = require('sys'); + var fs = require('fs'); + var exec = require('child_process').exec; + var CronJob = require('cron').CronJob; + var winston = require('winston'); + + var deploying = {}; + var queued = {}; + var app; + + // Run the logger, which dumps everything to stdout and deployment.log + var logger = new winston.Logger({ + transports: [ + new (winston.transports.Console)({ level: 'debug', colorize: true }), + new (winston.transports.File)({ filename: 'deployment.log', level: 'verbose', json: false }) + ] + }); + + var init = function () + { + // Initialize App and define how we parse the request body + app = express(); + + app.configure(function () { + app.use(function (err, req, res, next) + { + logger.error(err.stack); + next(err); + }); + + app.use(express.json()); + app.use(express.urlencoded()); + app.use(express.multipart()); + }); + + // Bind our routes + bindRoutes(); + + // Bind schedulers + bindSchedulers(); + + // Launch the server + var server = app.listen(8282, function() + { + logger.info('Listening on port %d', server.address().port); + }); + + // Handle server related errors + server.on("error", function (err) + { + logger.error('there was an error:', err.message); + }); + }; + + var bindRoutes = function() + { + app.post('/hooks/push', routeHookPush); + }; + + /** + * Bind schedulers from sibling directories + */ + var bindSchedulers = function() + { + // Scan the parent dir + var files = fs.readdirSync(__dirname + "/.."); + for (k in files) + { + var file = files[k]; + if (file[0] == '.') continue; + + // Validate whether the current file/folder contains deploy.js + var path = __dirname + "/../" + file + if ( ! fs.existsSync(path + "/deploy.js")) continue; + + // Validate whether this deployerRunner has a schedule method + var deployerRunner = require(path + "/deploy.js"); + if ( ! ("schedule" in deployerRunner)) continue; + + deployerRunner.init(logger); + + // Retrieve the branch name of the current repo + (function(path, deployerRunner) + { + exec('cd "'+path+'" && git rev-parse --abbrev-ref HEAD', function(err, stdo, stde) + { + if (err) return; + + var branch = stdo.replace(/\s/g,''); + + // Run the scheduler + logger.debug("Running scheduler for " + path); + deployerRunner.schedule(branch, CronJob, deploy); + }); + })(path, deployerRunner); + } + }; + + /** + * Github event hook for push events + */ + var routeHookPush = function(req, res) + { + logger.info("Received push event"); + + // Parse relevant info + var payload = req.body; + var repo = payload.repository.name; + var branch = payload.ref.split("/").slice(-1).join("-"); + var deployerName = [repo,branch].join("-"); + + // Prepare object to be passed to deployerRunner + var deployer = { + name: deployerName, + path: __dirname + "/../" + deployerName, + repository: payload.repository.name, + branch: payload.ref.split("/").slice(-1)[0] + }; + + logger.debug("Deployer data", deployer); + + // Validate whether we have a deployment for the current repo and branch + fs.exists(deployer.path + "/deploy.js", function(exists) + { + logger.debug(deployer.path + " exists: " + exists); + if ( ! exists) return; + deploy(deployer); + }) + + res.send(''); + }; + + /** + * Perform a deployment + */ + var deploy = function(deployer) + { + // Add to queue if a deployment is already in progress for this repo+branch + if (deployer.name in deploying) + { + logger.info("Queueing " + deployer.name); + queued[deployer.name] = true; + return; + } + + logger.info("Deploying " + deployer.name); + deploying[deployer.name] = true; + + // Perform a git pull on the targeted deployment so we can execute + // the latest version of deploy.js + exec('cd "'+deployer.path+'" && git pull', function(err, stdo, stde) + { + logger.debug(stdo); + + if (err !== null) + { + logger.error("Git pull error ("+deployer.name+"): " + err); + return; + } + + // Invoke the actual deployment script + var deployerRunner = require(deployer.path + "/deploy.js"); + deployerRunner.init(logger); + deployerRunner.run(deployer, function(err) + { + if (err) + { + logger.error("Error while deploying "+deployer.name+": " + err); + } + + // All done, unblock this deployer + delete deploying[deployer.name]; + logger.info("Done deploying " + deployer.name); + + // Run the job again if another deploy has + // been scheduled for this deployer + if (deployer.name in queued) + { + delete queued[deployer.name]; + deploy(deployer) + } + }); + }); + }; + + // Catch uncaught exceptions and restart the server + process.on('uncaughtException', function (err) { + logger.error('uncaughtException: ' + err.message, err.stack); + process.exit(1); // Forever should restart our process (if we're running it through ./daemon) + }); + + init(); + +}