From 2dfc33b9136b1bec8047382a6daecf3b38f05211 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Fri, 15 Nov 2019 19:12:14 +0100 Subject: [PATCH] * debian/pg_createconfig_patroni: Add --vip option and write out a vip-manager configuration if present. * debian/config.yml.in: Add @LISTEN_VIP@ tag. --- debian/changelog | 5 ++- debian/config.yml.in | 2 +- debian/pg_createconfig_patroni | 56 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/debian/changelog b/debian/changelog index 4d6768d..b1aafd8 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,9 @@ patroni (1.6.0-5) UNRELEASED; urgency=medium - * + [ Michael Banck ] + * debian/pg_createconfig_patroni: Add --vip option and write out a + vip-manager configuration if present. + * debian/config.yml.in: Add @LISTEN_VIP@ tag. -- Debian PostgreSQL Maintainers Tue, 08 Oct 2019 12:58:40 +0200 diff --git a/debian/config.yml.in b/debian/config.yml.in index 16d33aa..76c690c 100644 --- a/debian/config.yml.in +++ b/debian/config.yml.in @@ -75,7 +75,7 @@ postgresql: pg_clonecluster: command: /usr/share/patroni/pg_clonecluster_patroni - listen: "@HOSTIP@,127.0.0.1:@PORT@" + listen: "@HOSTIP@@LISTEN_VIP@,127.0.0.1:@PORT@" connect_address: @HOSTIP@:@PORT@ use_unix_socket: true data_dir: /var/lib/postgresql/@VERSION@/@CLUSTER@ diff --git a/debian/pg_createconfig_patroni b/debian/pg_createconfig_patroni index ea32788..0ef1851 100755 --- a/debian/pg_createconfig_patroni +++ b/debian/pg_createconfig_patroni @@ -19,6 +19,10 @@ case $i in FORCE="y" shift # past argument=value ;; + --vip=*) + VIP_IP="${i#*=}" + shift # past argument=value + ;; *) # unknown option ;; @@ -78,6 +82,44 @@ if [ $? != 0 ]; then fi DCS_CONFIG="$(egrep -v '^[[:space:]]*$|^ *#' /etc/patroni/dcs.yml | sed -e ':a;N;$!ba;s/\n/\\n/g' -e 's/\$/\\$/g')" +# check vip configuration +if [ -n "$VIP_IP" ]; then + VIP_FILE=/etc/patroni/${VERSION}-${CLUSTER}.vip + if [ ! -e /etc/patroni/vip.in ]; then + echo "VIP template /etc/patroni/vip.in does not exist, cannot write VIP file" + exit 1 + fi + if [ $(grep -q LISTEN_VIP /etc/patroni/config.yml.in) ]; then + echo "Patroni configuration template does not have @LISTEN_VIP@ tag" + echo "Postgres will not be able to bind to the VIP $VIP_IP." + exit 1 + fi + VIP_IFACE="$(ip -4 route get 8.8.8.8 | grep ^8.8.8.8 | sed -e s/.*dev.// -e s/\ .*//)" + if [ -z "$VIP_IFACE" ]; then + echo "Network interface could not be determined, cannot write VIP file" + exit 1 + fi + VIP_MASK="$(ip -o -f inet addr show $VIP_IFACE | awk '{print $4}' | sed -e 's/.*\///' | uniq)" + if [ -z "$VIP_MASK" ]; then + echo "Netmask could not be determined, cannot write VIP file" + exit 1 + fi + VIP_KEY="/postgresql-common/${VERSION}-${CLUSTER}/leader" + DCS_TYPE="$(egrep -v '^[[:space:]]*$|^ *#' /etc/patroni/dcs.yml | egrep '(etcd|consul|zookeeper)' | sed s/:.*//)" + if [ -z "$DCS_TYPE" ]; then + echo "DCS type could not be determined from /etc/patroni/dcs.yml, cannot write VIP file" + exit 1 + fi + DCS_ENDPOINT="$(egrep -v '^[[:space:]]*$|^ *#' /etc/patroni/dcs.yml | grep 'host' | sed -r -e s/.*host:// -e s/-// -e 's/ //g' -e 's/^([0-9])/http:\/\/\1/')" + if [ -z "$DCS_ENDPOINT" ]; then + echo "DCS endpoint URL could not be determined from /etc/patroni/dcs.yml, cannot write VIP file" + exit 1 + fi + LISTEN_VIP=",$VIP_IP" +else + LISTEN_VIP= +fi + CONFIG_FILE=/etc/patroni/${VERSION}-${CLUSTER}.yml if [ -f $CONFIG_FILE -a -z "$FORCE" ]; then @@ -117,12 +159,26 @@ cat /etc/patroni/config.yml.in | \ -e "s/@CLUSTER@/${CLUSTER}/g" \ -e "s/@HOSTNAME@/${HOSTNAME}/g" \ -e "s/@HOSTIP@/${HOSTIP}/g" \ + -e "s/@LISTEN_VIP@/${LISTEN_VIP}/g" \ -e "s#@NETWORK@#${NETWORK}#g" \ -e "s/@API_PORT@/${API_PORT}/g" \ -e "s/@PORT@/${PORT}/g" \ -e "s/@DCS_CONFIG@/${DCS_CONFIG}/g" \ >> $CONFIG_FILE +# write vip configuration, if requested +if [ -n "$VIP_IP" ]; then + cat /etc/patroni/vip.in | \ + sed -e "s/@VIP_IP@/${VIP_IP}/g" \ + -e "s/@VIP_MASK@/${VIP_MASK}/g" \ + -e "s/@VIP_IFACE@/${VIP_IFACE}/g" \ + -e "s#@VIP_KEY@#${VIP_KEY}#g" \ + -e "s/@VIP_HOST@/${HOSTNAME}/g" \ + -e "s/@VIP_TYPE@/${DCS_TYPE}/g" \ + -e "s#@VIP_ENDPOINT@#${DCS_ENDPOINT}#g" \ + >> $VIP_FILE +fi + # Set permissions chown postgres:postgres $CONFIG_FILE chmod 660 $CONFIG_FILE