0.0.1 • Published 9 years ago

haconfig v0.0.1

Weekly downloads
3
License
-
Repository
-
Last release
9 years ago

HAconfig

Small script to build a haproxy.cfg file from multiple files. Useful if you want to drop some service specific configs into a directory then re-render haproxy.cfg and reload.

Warning: I hacked this together in a short space of time with minimal thought/testing - I wouldn't recommend using it anywhere that matters.

Usage

haconfig /path/to/template/directory > /etc/haproxy/haproxy.cfg

Templates files

The HAProxy config is built using multiple files in a single directory - they can be called whatever you like and will be loaded in alphabetical order.

templates/
    bar.cfg
    base.cfg
    foo.cfg

/etc/haproxy/templates/base.cfg

The files can look like a regular haproxy.cfg.

global
    log 127.0.0.1	local0
    log 127.0.0.1	local1 notice
    maxconn 4096
    user haproxy
    group haproxy

defaults
    log	global
    mode	http
    option	httplog
    option	dontlognull
    retries	3
    option redispatch
    maxconn	2000
    timeout connect 5000
    timeout client 50000
    timeout server 50000

frontend http_in
    bind *:80
    acl is_foo hdr_beg(host) -i foo
    use_backend foo if is_foo

backend foo
    server foo1 127.0.0.1:8080 check
    server foo2 127.0.0.1:8081 check

/etc/haproxy/templates/global2.cfg

If you define the same section in multiple files you'll get an error:

# THIS DOESN'T WORK - clashes with base.cfg!

global
    foo bar
    testing 123

/etc/haproxy/templates/bar.cfg

However, you can extend sections using the extend keyword:

extend frontend http_in
    acl is_bar hdr_beg(host) -i bar
    use_backend bar if is_bar

backend bar
    server bar1 127.0.0.1:8082
    server bar2 127.0.0.1:8083

/etc/haproxy/haproxy.cfg

Combining the above templates/base.cfg and templates/bar.cfg files will produce the following output:

global
    log 127.0.0.1	local0
    log 127.0.0.1	local1 notice
    maxconn 4096
    user haproxy
    group haproxy

defaults
    log	global
    mode	http
    option	httplog
    option	dontlognull
    retries	3
    option redispatch
    maxconn	2000
    timeout connect 5000
    timeout client 50000
    timeout server 50000

frontend http_in
    bind *:80
    acl is_bar hdr_beg(host) -i bar
    use_backend bar if is_bar
    acl is_foo hdr_beg(host) -i foo
    use_backend foo if is_foo

backend bar
    server bar1 127.0.0.1:8082
    server bar2 127.0.0.1:8083

backend foo
    server foo1 127.0.0.1:8080 check
    server foo2 127.0.0.1:8081 check