Files
chef/scripts/postgresql/switch_primary.sh
T
raucao 470dcb82ee Migrate the Chef repo to Cinc Workstation
Cinc Workstation replaces Chef Workstation for driving knife-zero and
Test Kitchen; nodes run (or are moving to) Cinc Client.

- Pin .ruby-version to cinc-workstation
- Move knife config from .chef/ to .cinc/ (Cinc's USER_CONF_DIR) and
  update .gitignore, README, AGENTS and docs accordingly. On-node config
  now lives in /etc/cinc
- Drop the Gemfile/Gemfile.lock; install knife-zero into Cinc
  Workstation's user gem dir with `cinc gem install knife-zero`
- README: rewrite setup/usage for Cinc Workstation
- scripts/postgresql/switch_primary.sh: drop `bundle exec`
2026-11-01 18:28:44 +04:00

25 lines
1.1 KiB
Bash
Executable File

#!/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=`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)"
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 "Stopping postgresql on $old_primary_hostname"
knife ssh name:$old_primary_hostname -a knife_zero.host "sudo systemctl stop postgresql@12-main.service"
echo "Replacing pg.kosmos.local host entries with $new_primary_ip"
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"
# TODO
# 1. Change roles in node configs
# 2. Converge new primary
echo "You need to update the role in the '$new_primary_hostname' node config to 'postgres_primary' and converge it now."