Skip to content

Instantly share code, notes, and snippets.

@ezhuk
ezhuk / confirm_action
Created December 27, 2013 18:25
Ask the user to confirm an action.
#!/bin/bash
#
# This function asks the user to confirm an action with No used as a default
# answer.
confirm()
{
while true; do
read -p "$1" RES
case "$RES" in
@ezhuk
ezhuk / haproxy_multiple_domains
Created December 26, 2013 20:39
Multiple domains routing in HAProxy.
frontend main *:80
acl is_foo hdr_dom(host) -i foo
acl is_bar hdr_dom(host) -i bar
use_backend app_foo if is_foo
use_backend app_bar if is_bar
backend app_foo
# app_foo settings go here
@ezhuk
ezhuk / create_temp_file
Last active January 1, 2016 11:29
Create a temporary file the right way.
#!/bin/bash
#
# This creates a temporary file and sets up a trap handler so that it gets
# deleted when the script exits.
BASENAME=$(which basename)
MKTEMP=$(which mktemp)
RM=$(which rm)
TMP_PREFIX=$($BASENAME "$0")
@ezhuk
ezhuk / haproxy_force_https
Last active March 28, 2017 20:37
Force HTTPS in HAProxy behind Amazon ELB.
frontend main *:80
acl is_http hdr(X-Forwarded-Proto) http
redirect prefix https://example.com code 301 if is_http
@ezhuk
ezhuk / haproxy_reload
Last active January 6, 2024 04:19
Reload HAProxy configuration with no downtime.
#!/bin/bash
#
# The trick here is to dump pids of all haproxy processes into a file
# (-p option) and then tell them to finish their work and exit (-sf).
HAPROXY=/usr/sbin/haproxy
HAPROXY_PID=/var/run/haproxy.pid
HAPROXY_CONFIG=/etc/haproxy/haproxy.cfg
$HAPROXY -f $HAPROXY_CONFIG -p $HAPROXY_PID -sf $(cat $HAPROXY_PID)