patroni/debian/pg_createconfig_patroni
Michael Banck 8cb3cbda6e * debian/pg_createconfig_patroni: New script.
* debian/dcs.yml, debian/config.yml.in: New configuration templates.
  * debian/patroni.install: Install new script to /usr/bin and configuration
    templates to /etc/patroni.
2018-11-05 08:49:40 +01:00

82 lines
1.8 KiB
Bash
Executable file

#!/bin/sh
for i in "$@"
do
case $i in
--scope=*)
SCOPE="${i#*=}"
shift # past argument=value
;;
--hostip=*)
HOSTIP="${i#*=}"
shift # past argument=value
;;
--force)
FORCE="y"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
if [ -z "$SCOPE" ]; then
echo "Required option --scope missing"
exit 1
fi
# support both '-' and '/' as separator
if [ $(echo $SCOPE | grep -- -) ]
then
VERSION=$(echo $SCOPE | sed -e 's/-.*//')
CLUSTER=$(echo $SCOPE | sed -e 's/.*-//')
else
VERSION=$(echo $SCOPE | sed -e 's/\/.*//')
CLUSTER=$(echo $SCOPE | sed -e 's/.*\///')
fi
# check DCS configuration
if [ ! -f /etc/patroni/dcs.yml ]; then
echo "DCS not configured yet, edit /etc/patroni/dcs.yml"
exit 1
fi
grep -v "^#" /etc/patroni/dcs.yml | grep -v "^$" > /dev/null 2>&1
if [ $? != 0 ]; then
echo "DCS not configured yet, edit /etc/patroni/dcs.yml"
exit 1
fi
CONFIG_FILE=/etc/patroni/${VERSION}-${CLUSTER}.yml
if [ -f $CONFIG_FILE -a -z "$FORCE" ]; then
echo "Patroni configuration file already exists"
exit 1
else
rm -f $CONFIG_FILE
touch $CONFIG_FILE
fi
HOSTNAME=$(hostname)
# set default ipv4 address in case it was not provided
if [ -z "$HOSTIP" ]; then
HOSTIP=$(ip -4 route get 8.8.8.8 | grep ^8.8.8.8 | sed s/.*src.//)
fi
echo "scope: \"$VERSION-$CLUSTER\"" >> $CONFIG_FILE
echo "namespace: \"/postgresql-common/\"" >> $CONFIG_FILE
echo "name: \"$HOSTNAME\"" >> $CONFIG_FILE
echo "" >> $CONFIG_FILE
# add DCS configuration
cat /etc/patroni/dcs.yml >> $CONFIG_FILE
echo "" >> $CONFIG_FILE
# add remaining patroni configuration from template
cat /etc/patroni/config.yml.in | \
sed -e s/@VERSION@/${VERSION}/ \
-e s/@CLUSTER@/${CLUSTER}/ \
-e s/@HOSTIP@/${HOSTIP}/ \
>> $CONFIG_FILE