From 34271e1853d711bb269835472bbc88be14403868 Mon Sep 17 00:00:00 2001 From: Ernestas Poskus Date: Fri, 6 May 2016 21:24:19 +0300 Subject: [PATCH] Udpate haproxy marathon script --- defaults/main.yml | 2 +- files/haproxy_dns_cfg | 236 ++++++++++++++++++++++++++++++------------ 2 files changed, 168 insertions(+), 70 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 582f124..8e8a19e 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,5 @@ --- -marathon_playbook_version: "0.3.3" +marathon_playbook_version: "0.3.4" marathon_version: "0.8.1" # Debian: Mesosphere apt repository URL diff --git a/files/haproxy_dns_cfg b/files/haproxy_dns_cfg index fea3721..17cb2ac 100755 --- a/files/haproxy_dns_cfg +++ b/files/haproxy_dns_cfg @@ -1,109 +1,207 @@ #!/bin/bash set -o errexit -o nounset -o pipefail + +########################################################## +# DEPRECATION WARNING: This script has been deprecated. # +# # +# Please use marathon-lb instead: # +# https://github.com/mesosphere/marathon-lb # +########################################################## + function -h { -cat <<\USAGE - USAGE: haproxy_cfg +cat <+ + $name install_haproxy_system + + + Generates a new configuration file for HAProxy from the specified Marathon + servers, replaces the file in /etc/haproxy and restarts the service. + + In the second form, installs the script itself, HAProxy and a cronjob that + once a minute pings one of the Marathon servers specified and refreshes + HAProxy if anything has changed. The list of Marathons to ping is stored, + one per line, in: + + $cronjob_conf_file + + The script is installed as: -haproxy_cfg generates a config file to run HAProxy on localhost and proxy to a number of backend hosts. + $script_path -To gracefully reload haproxy: + The cronjob is installed as: -:; haproxy -f /path/to/config -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) + $cronjob + + and run as root. USAGE }; function --help { -h ;} export LC_ALL=en_US.UTF-8 -function header { - cat < /tmp/"$conf_file" + if ! diff -q /tmp/"$conf_file" /etc/haproxy/"$conf_file" >&2 + then + msg "Found changes. Sending reload request to HAProxy..." + cat /tmp/"$conf_file" > /etc/haproxy/"$conf_file" + if [[ -f /etc/init/haproxy.conf ]] + then reload haproxy ## Upstart + elif [[ -f /usr/lib/systemd/system/haproxy.service ]] + then systemctl reload haproxy ## systemd + else /etc/init.d/haproxy reload + fi + fi } -function marathon_apps { - z="`curl -H "Accept: text/plain" -s "$1/v2/tasks"`" +function install_haproxy_system { + + sudo="" + if [[ $EUID -ne 0 ]] + then sudo="sudo" + fi + + current_path=$(dirname $0) + current_path=$(cd $current_path && pwd) ## absolute path + if [ $current_path/$name = $script_path ] + then + # executing the script from its install location leads to an empty file + echo "running $name in install mode from the install path $script_path is not supported." + exit 1 ## fail + fi + + if hash lsb_release 2>/dev/null + then + os=$(lsb_release -si) + elif [ -e "/etc/system-release" ] && (grep -q "Amazon Linux AMI" "/etc/system-release") + then + os="AmazonAMI" + elif [ -e "/etc/SuSE-release" ] + then + os="SuSE" + fi + + if [[ $os == "CentOS" ]] || [[ $os == "RHEL" ]] || [[ $os == "AmazonAMI" ]] || [[ $os == "OracleServer" ]] + then + $sudo yum install -y haproxy + $sudo chkconfig haproxy on + elif [[ $os == "Ubuntu" ]] || [[ $os == "Debian" ]] + then + $sudo env DEBIAN_FRONTEND=noninteractive aptitude install -y haproxy + $sudo sed -i 's/^ENABLED=0/ENABLED=1/' /etc/default/haproxy + elif [[ $os == "SuSE" ]] + then + $sudo zypper -n --no-gpg-checks --non-interactive install --auto-agree-with-licenses haproxy + $sudo chkconfig haproxy on + else + echo "$os is not a supported OS for this feature." + exit 1 + fi + install_cronjob "$@" +} - frontend "$z" - backend "$z" +function install_cronjob { + $sudo mkdir -p "$(dirname "$cronjob_conf_file")" + [[ -f $cronjob_conf_file ]] || $sudo touch "$cronjob_conf_file" + if [[ $# -gt 0 ]] + then printf '%s\n' "$@" | $sudo dd of="$cronjob_conf_file" + fi + cat "$0" | $sudo dd of="$script_path" + $sudo chmod ug+rx "$script_path" + cronjob | $sudo dd of="$cronjob" + header | $sudo dd of=/etc/haproxy/"$conf_file" } -function frontend { - cat < >(logger -p user.info -t "$name[$$]") + exec 2> >(logger -p user.notice -t "$name[$$]") + "$@" } function msg { out "$*" >&2 ;} function err { local x=$? ; msg "$*" ; return $(( $x == 0 ? 1 : $x )) ;} function out { printf '%s\n' "$*" ;} +# If less than 1 argument is provided, print usage and exit. At least one +# argument is required as described in the `USAGE` message. +[ $# -lt 1 ] && { -h; exit 1; } + if [[ ${1:-} ]] && declare -F | cut -d' ' -f3 | fgrep -qx -- "${1:-}" then "$@" else main "$@"