Skip to content

Instantly share code, notes, and snippets.

@tblobaum
Created July 16, 2012 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tblobaum/3125540 to your computer and use it in GitHub Desktop.
Save tblobaum/3125540 to your computer and use it in GitHub Desktop.
global
log 0.0.0.0 local2 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
daemon
defaults
option http-server-close
option http-pretend-keepalive
timeout connect 4s
timeout client 60s
timeout server 30s
listen stats :50080
mode http
stats uri /
stats auth admin:admin
frontend public :80,:443
{{ for (var s in services) { }}
{{ if (services[s].length && typeof services[s][0].frontend !== 'undefined') { }}
{{ services[s][0].frontend.forEach(function (line) { }}
{{- line }}
{{ }) }}
{{ } }}
{{ } }}
{{ for (var s in services) { }}
# {{- s }}
backend {{- s }}
stats uri /{{- s }}?stats
stats scope .
stats hide-version
stats show-desc {{- s }}
stats realm {{- s }}
{{ if (services[s][0]) { }}
stats show-desc {{- s }}@{{- services[s][0].version }}
{{ if (typeof services[s][0].backend !== 'undefined') { }}
{{ services[s][0].backend.forEach(function (line) { }}
{{- line }}
{{ }) }}
{{ } }}
{{ } }}
{{ services[s].forEach(function (srvc) { }}
server {{- s }}-{{- srvc.port }} localhost:{{- srvc.port }} check
{{ }) }}
{{ } }}
#!/usr/bin/env node
var seaport = require('seaport')
, seaproxy = seaport.createServer({ host : 'localhost', port : 1026 })
, fs = require('fs')
, ejs = require('ejs')
, configFile = __dirname + '/haproxy.conf'
, exec = require('child_process').exec
, spawn = require('child_process').spawn
, changes = true
, haproxy
ejs.open = '{{'
ejs.close = '}}'
seaproxy.on('allocate', reload)
seaproxy.on('assume', reload)
seaproxy.on('free', reload)
function reload () {
changes = true
}
setInterval(function () {
if (changes) {
reloadHAProxy(function () {
changes = false
})
}
}, 5000) // respond to changes every 5 seconds
seaproxy.listen(1026)
reloadHAProxy()
function reloadHAProxy (callback) {
ejs.renderFile('./conf.ejs', { services : seaproxy.roles }, run)
function run (error, haproxyConf) {
fs.writeFile(configFile, haproxyConf, 'utf8', function () {
var args = [ '-f', configFile, '-p', '/var/run/haproxy.pid' ]
exec('cat /var/run/haproxy.pid', function (e, pid) {
pid = pid.match(/[0-9]+/)
if (pid && pid[0]) {
console.info('haproxy already exists', pid[0])
args.push('-sf')
args.push(pid[0])
}
haproxy = spawn('/usr/sbin/haproxy', args)
haproxy.stdout.pipe(process.stdout)
haproxy.stderr.pipe(process.stderr)
haproxy.on('exit', function (code, signal) {
console.log('haproxy exit', code, signal)
})
return callback && callback()
})
})
}
}
process.on('exit', function () { haproxy.kill() })
{
"web" : {
"title": "web",
"role": "web@0.1.0",
"frontend": [
"acl web_acl path_dir -i web",
"acl web_acl hdr_beg(host) -i web",
"use_backend web if web_acl"
],
"backend": [
"reqrep ^([^\\ :]*)\\ /web/(.*) \\1\\ /\\2"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment