Skip to content

Instantly share code, notes, and snippets.

@elisium-oat
Last active April 3, 2020 14:07
Show Gist options
  • Select an option

  • Save elisium-oat/5e4563c35fcaf536f9dc3d9f928c6d2d to your computer and use it in GitHub Desktop.

Select an option

Save elisium-oat/5e4563c35fcaf536f9dc3d9f928c6d2d to your computer and use it in GitHub Desktop.

Script for adding a service in a HAProxy config file.

Here is the original haproxy.cfg (modified from here):

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

defaults
       log     global
       mode    http
       option  httplog
       option  dontlognull
       option forwardfor
       option http-server-close
       stats enable
       stats auth someuser:somepassword
       stats uri /haproxyStats

frontend http-in
       bind *:80

       # Define hosts
       acl host_bacon hdr(host) -i ilovebacon.com
       acl host_milkshakes hdr(host) -i bobsmilkshakes.com

       ## figure out which one to use
       use_backend bacon_cluster if host_bacon
       use_backend milshake_cluster if host_milkshakes

# --- frontend http-in ---

backend baconcluster
       balance leastconn
       option httpclose
       option forwardfor
       cookie JSESSIONID prefix
       server node1 10.0.0.1:8080 cookie A check
       server node1 10.0.0.2:8080 cookie A check
       server node1 10.0.0.3:8080 cookie A check

backend milshake_cluster
       balance leastconn
       option httpclose
       option forwardfor
       cookie JSESSIONID prefix
       server node1 10.0.0.4:8080 cookie A check
       server node1 10.0.0.5:8080 cookie A check
       server node1 10.0.0.6:8080 cookie A check

Notice the # --- frontend http-in ---

Here is ./add-service.sh:

#!/bin/bash

CONFIG_FILE=$1

# Check if the file exists. If not, exit with error code 1
test ! -f "$CONFIG_FILE" && echo "Can't find the config file '$CONFIG_FILE'. Aborting..." && exit 1

read -p "Service name: " service_name
read -p "IP Address:port (Ex: 0.0.0.0:8080): " service_ip
read -p "Service domain (Ex: svc.domain.local): " service_domain

service_slug="$(echo -n "$service_name" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)";

frontend="\
        # $service_name\n\
        acl host_$service_slug hdr(host) -i $service_domain\n\
        use_backend $service_slug if host_$service_slug\n"

backend="\n\
# $service_name\n\
backend $service_slug\n\
        balance leastconn\n\
        option httpclose\n\
        option forwardfor\n\
        server serv_${service_slug}_01 $service_ip\n"

# All the action happens here
sed -i "/# --- frontend http-in/i \\$frontend" "$CONFIG_FILE"
echo -e "$backend" >> "$CONFIG_FILE"

echo "Service added '$service_name' to '$CONFIG_FILE'"

The execution should look like this:

$ ./add-service.sh haproxy.cfg
Service name: Testing
IP Address:port (Ex: 0.0.0.0:8080): 127.0.0.1:8080
Service domain (Ex: svc.domain.local): service.example.com
Service added 'Testing' to 'haproxy.cfg'

The updated config file should look like this:

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

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option forwardfor
        option http-server-close
        stats enable
        stats auth someuser:somepassword
        stats uri /haproxyStats

frontend http-in
        bind *:80

        # Define hosts
        acl host_bacon hdr(host) -i ilovebacon.com
        acl host_milkshakes hdr(host) -i bobsmilkshakes.com

        ## figure out which one to use
        use_backend bacon_cluster if host_bacon
        use_backend milshake_cluster if host_milkshakes

        # Testing
        acl host_testing hdr(host) -i service.example.com
        use_backend testing if host_testing

# --- frontend http-in ---

backend baconcluster
        balance leastconn
        option httpclose
        option forwardfor
        cookie JSESSIONID prefix
        server node1 10.0.0.1:8080 cookie A check
        server node1 10.0.0.2:8080 cookie A check
        server node1 10.0.0.3:8080 cookie A check

backend milshake_cluster
        balance leastconn
        option httpclose
        option forwardfor
        cookie JSESSIONID prefix
        server node1 10.0.0.4:8080 cookie A check
        server node1 10.0.0.5:8080 cookie A check
        server node1 10.0.0.6:8080 cookie A check

# Testing
backend testing
        balance leastconn
        option httpclose
        option forwardfor
        server serv_testing_01 127.0.0.1:8080

It works well for my use case. Please feel free to adapt to your needs.

@herr-felix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment