active_postgres 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ REPMGR_CONF="/etc/repmgr.conf"
5
+ DNS_USER="<%= dns_user %>"
6
+ DNS_SERVERS=(<%= dns_servers.map(&:to_s).join(' ') %>)
7
+ DNS_SSH_KEY="<%= dns_ssh_key_path %>"
8
+ DNSMASQ_FILE="/etc/dnsmasq.d/active_postgres.conf"
9
+ PRIMARY_RECORD="<%= primary_record %>"
10
+ REPLICA_RECORD="<%= replica_record %>"
11
+ SSH_STRICT_HOST_KEY="<%= ssh_strict_host_key %>"
12
+ SSH_KNOWN_HOSTS="/var/lib/postgresql/.ssh/known_hosts"
13
+ LOG_TAG="active_postgres_dns"
14
+
15
+ cluster_csv=$(repmgr -f "$REPMGR_CONF" cluster show --csv 2>/dev/null || true)
16
+ if [[ -z "$cluster_csv" ]]; then
17
+ exit 0
18
+ fi
19
+
20
+ primary_host=$(printf "%s\n" "$cluster_csv" | awk -F',' 'NR>1 && tolower($3) ~ /primary/ {print $NF; exit}' | sed -n 's/.*host=\\([^ ]*\\).*/\\1/p')
21
+ standby_hosts=$(printf "%s\n" "$cluster_csv" | awk -F',' 'NR>1 && tolower($3) ~ /standby/ {print $NF}' | sed -n 's/.*host=\\([^ ]*\\).*/\\1/p' | sort -u)
22
+
23
+ if [[ -z "$primary_host" ]]; then
24
+ exit 0
25
+ fi
26
+
27
+ content=$'# Managed by active_postgres\n'
28
+ printf -v content '%saddress=/%s/%s\n' "$content" "$PRIMARY_RECORD" "$primary_host"
29
+
30
+ if [[ -n "$standby_hosts" ]]; then
31
+ for host in $standby_hosts; do
32
+ printf -v content '%saddress=/%s/%s\n' "$content" "$REPLICA_RECORD" "$host"
33
+ done
34
+ fi
35
+
36
+ ssh_opts=(-i "$DNS_SSH_KEY" -o BatchMode=yes -o StrictHostKeyChecking="$SSH_STRICT_HOST_KEY" -o UserKnownHostsFile="$SSH_KNOWN_HOSTS")
37
+
38
+ for server in "${DNS_SERVERS[@]}"; do
39
+ if [[ -z "$server" ]]; then
40
+ continue
41
+ fi
42
+
43
+ if ! /usr/bin/ssh "${ssh_opts[@]}" "${DNS_USER}@${server}" \
44
+ "sudo bash -c 'cat > ${DNSMASQ_FILE} << \"EOF\"\\n${content}EOF\\n' && (sudo systemctl reload dnsmasq || sudo systemctl restart dnsmasq)"; then
45
+ /usr/bin/logger -t "$LOG_TAG" "Failed updating dnsmasq on ${server}"
46
+ fi
47
+ done
48
+
49
+ /usr/bin/logger -t "$LOG_TAG" "Updated DNS records for ${PRIMARY_RECORD} and ${REPLICA_RECORD} (primary=${primary_host})"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BoringCache
@@ -195,9 +195,13 @@ files:
195
195
  - lib/tasks/rotate_credentials.rake
196
196
  - templates/pg_hba.conf.erb
197
197
  - templates/pgbackrest.conf.erb
198
+ - templates/pgbouncer-follow-primary.service.erb
199
+ - templates/pgbouncer-follow-primary.timer.erb
198
200
  - templates/pgbouncer.ini.erb
201
+ - templates/pgbouncer_follow_primary.sh.erb
199
202
  - templates/postgresql.conf.erb
200
203
  - templates/repmgr.conf.erb
204
+ - templates/repmgr_dns_failover.sh.erb
201
205
  homepage: https://github.com/boringcache/active_postgres
202
206
  licenses:
203
207
  - MIT