chef/scripts/postgresql/switch_primary.sh

20 lines
981 B
Bash
Raw Normal View History

#!/bin/bash
set -e
if [[ $# -ne 2 ]] ; then
echo "Usage: scripts/postgresql/switch_primary.sh <old_primary_hostname> <new_primary_hostname>"
exit 1;
fi
old_primary_hostname=$1
new_primary_hostname=$2
new_primary_ip=`bundle exec knife node show $new_primary_hostname -a knife_zero.host | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}" -o`
echo "Switching primary to $new_primary_hostname ($new_primary_ip)"
bundle exec knife ssh name:$new_primary_hostname -a knife_zero.host "sudo su - postgres -c '/usr/lib/postgresql/12/bin/pg_ctl promote -D /var/lib/postgresql/12/main'"
echo "Replacing pg.kosmos.local host entries with $new_primary_ip"
bundle exec knife ssh roles:postgresql_client -a knife_zero.host "sudo sed -r \"s/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s(pg.kosmos.local)/$new_primary_ip\t\1/\" -i /etc/hosts"
echo "Stopping postgresql on $old_primary_hostname"
bundle exec knife ssh name:$old_primary_hostname -a knife_zero.host "sudo systemctl stop postgresql@12-main.service"