Last active
August 29, 2015 14:10
-
-
Save lukaswelte/6240556f4d1aae4f2a7b to your computer and use it in GitHub Desktop.
Marathon Bridge Cluster by Task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o errexit -o nounset -o pipefail | |
function -h { | |
cat <<\USAGE | |
USAGE: haproxy_cfg <marathon host:port> | |
haproxy_cfg generates a config file to run HAProxy on localhost and proxy to a number of backend hosts. | |
To gracefully reload haproxy: | |
:; haproxy -f /path/to/config -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) | |
USAGE | |
}; function --help { -h ;} | |
export LC_ALL=en_US.UTF-8 | |
function header { | |
cat <<EOF | |
global | |
daemon | |
log 127.0.0.1 local0 notice | |
maxconn 4096 | |
ssl-server-verify none | |
tune.ssl.default-dh-param 2048 | |
user haproxy | |
group haproxy | |
defaults | |
log global | |
retries 3 | |
maxconn 60000 | |
timeout connect 5000 | |
timeout client 240000 | |
timeout server 240000 | |
} | |
function marathon_apps { | |
z="`curl -H "Accept: text/plain" -s "$1/v2/tasks"`" | |
frontend "$z" | |
backend "$z" | |
} | |
function frontend { | |
cat <<EOF | |
frontend http-in | |
bind *:80 | |
mode http | |
option httplog | |
option dontlognull | |
option forwardfor | |
option contstats | |
option http-server-close | |
EOF | |
while IFS= read | |
do | |
set -- $REPLY | |
local name="$1" | |
cat <<EOF | |
acl host_${name} hdr(host) -i ${name} | |
use_backend ${name}_cluster if host_${name} | |
EOF | |
done <<< "$1" | |
} | |
function backend { | |
cat <<EOF | |
EOF | |
while IFS= read | |
do | |
set -- $REPLY | |
local name="$1" | |
local port="$2" | |
shift 2 | |
cat <<EOF | |
backend ${name}_cluster | |
option httpclose | |
option forwardfor | |
mode http | |
balance leastconn | |
option httpchk GET / | |
http-check expect ! rstatus ^5 | |
EOF | |
while [[ $# -gt 0 ]] | |
do | |
out " server ${name}-$# $1 check" | |
shift | |
done | |
done <<< "$1" | |
} | |
function config { | |
header | |
marathon_apps "$@" | |
} | |
function main { | |
config "$@" | |
} | |
function msg { out "$*" >&2 ;} | |
function err { local x=$? ; msg "$*" ; return $(( $x == 0 ? 1 : $x )) ;} | |
function out { printf '%s\n' "$*" ;} | |
if [[ ${1:-} ]] && declare -F | cut -d' ' -f3 | fgrep -qx -- "${1:-}" | |
then "$@" | |
else main "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment