patroni/debian/pg_createconfig_patroni

97 lines
2.3 KiB
Plaintext
Raw Normal View History

#!/bin/sh
for i in "$@"
do
case $i in
--scope=*)
SCOPE="${i#*=}"
shift # past argument=value
;;
--hostip=*)
HOSTIP="${i#*=}"
shift # past argument=value
;;
--port=*)
PORT="${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
if [ -z "$PORT" ]; then
# try to guess next free port
PORT=$(($(pg_lsclusters | awk '{print $3}' | grep -v Port | sort -n | tail -1) + 1))
if [ "$PORT" -eq 1 ]; then
# No cluster exists yet, use default port
PORT=5432
fi
else
# validate specified port
pg_lsclusters | awk '{print $3}' | grep -q $PORT && echo "Port $PORT already in use" && 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
DCS_CONFIG="$(cat /etc/patroni/dcs.yml | sed -e ':a;N;$!ba;s/\n/\\n/g' -e 's/\$/\\$/g')"
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 -e s/.*src.// -e s/\ //g)
fi
# add remaining patroni configuration from template
cat /etc/patroni/config.yml.in | \
sed -e "s/@VERSION@/${VERSION}/g" \
-e "s/@CLUSTER@/${CLUSTER}/g" \
-e "s/@HOSTNAME@/${HOSTNAME}/g" \
-e "s/@HOSTIP@/${HOSTIP}/g" \
-e "s/@PORT@/${PORT}/g" \
-e "s/@DCS_CONFIG@/${DCS_CONFIG}/g" \
>> $CONFIG_FILE
# Set permissions
chown postgres:postgres $CONFIG_FILE
chmod 660 $CONFIG_FILE