From d95f09db7f972349e453bfe52fff778ea37a9298 Mon Sep 17 00:00:00 2001
From: Nicolas KAROLAK <nicolas@karolak.fr>
Date: Fri, 24 Apr 2020 07:52:39 +0000
Subject: [PATCH] fix pgconf path

---
 roles/postgres-ha/handlers/main.yml            |  9 +++++++++
 roles/postgres-ha/tasks/main.yml               | 18 ++++++++++++------
 .../rephacheck.py.j2}                          |  2 +-
 .../repmgr-event.py.j2}                        |  2 +-
 roles/postgres-ha/templates/repmgr.conf.j2     |  6 +++---
 5 files changed, 26 insertions(+), 11 deletions(-)
 rename roles/postgres-ha/{files/rephacheck.py => templates/rephacheck.py.j2} (93%)
 rename roles/postgres-ha/{files/repmgr-event.py => templates/repmgr-event.py.j2} (94%)

diff --git a/roles/postgres-ha/handlers/main.yml b/roles/postgres-ha/handlers/main.yml
index ba20097f..6b60369e 100644
--- a/roles/postgres-ha/handlers/main.yml
+++ b/roles/postgres-ha/handlers/main.yml
@@ -1,5 +1,9 @@
 ---
 
+- name: reload systemd
+  systemd:
+    daemon_reload: true
+
 - name: restart postgresql
   systemd:
     name: postgresql@{{ repmgr_pg_version }}-{{ repmgr_pg_cluster }}
@@ -10,4 +14,9 @@
     name: repmgrd
     state: restarted
 
+- name: restart rephacheck
+  systemd:
+    name: rephacheck.socket
+    state: restarted
+
 ...
diff --git a/roles/postgres-ha/tasks/main.yml b/roles/postgres-ha/tasks/main.yml
index 7ca4b71e..8092f18e 100644
--- a/roles/postgres-ha/tasks/main.yml
+++ b/roles/postgres-ha/tasks/main.yml
@@ -84,9 +84,9 @@
     replace: '{{ item.key }}={{ item.value }}'
 
 - name: copy events notification script
-  copy:
-    src: repmgr-event.py
-    dest: /usr/local/bin/repmgr-event
+  template:
+    src: repmgr-event.py.j2
+    dest: /usr/bin/repmgr-event
     mode: 0755
 
 - name: configure sudo
@@ -268,8 +268,8 @@
 # REPHACHECK
 
 - name: install rephacheck
-  copy:
-    src: rephacheck.py
+  template:
+    src: rephacheck.py.j2
     dest: /usr/bin/rephacheck
     mode: 0755
 
@@ -281,12 +281,15 @@
 - name: configure rephacheck
   template:
     src: rephacheck.conf.j2
-    dest: /etc/postgresql/11/main/rephacheck.conf
+    dest: /etc/postgresql/{{ repmgr_pg_version }}/{{ repmgr_pg_cluster }}/rephacheck.conf
     owner: postgres
     group: postgres
     mode: 0644
 
 - name: configure rephacheck socket
+  notify:
+    - reload systemd
+    - restart rephacheck
   copy:
     dest: /etc/systemd/system/rephacheck.socket
     content: |
@@ -301,6 +304,9 @@
       WantedBy=sockets.target
 
 - name: configure rephacheck service
+  notify:
+    - reload systemd
+    - restart rephacheck
   copy:
     dest: /etc/systemd/system/rephacheck@.service
     content: |
diff --git a/roles/postgres-ha/files/rephacheck.py b/roles/postgres-ha/templates/rephacheck.py.j2
similarity index 93%
rename from roles/postgres-ha/files/rephacheck.py
rename to roles/postgres-ha/templates/rephacheck.py.j2
index 34f7c7eb..7ec6cfea 100644
--- a/roles/postgres-ha/files/rephacheck.py
+++ b/roles/postgres-ha/templates/rephacheck.py.j2
@@ -50,7 +50,7 @@ def get_quorum_state(node_id):
 
 
 if __name__ == '__main__':
-    with open('/etc/postgresql/11/main/rephacheck.conf') as rephaconf:
+    with open('/etc/postgresql/{{ repmgr_pg_version }}/{{ repmgr_pg_cluster }}/rephacheck.conf') as rephaconf:
         conf = toml.load(rephaconf)
 
     NODES = conf.get('nodes')
diff --git a/roles/postgres-ha/files/repmgr-event.py b/roles/postgres-ha/templates/repmgr-event.py.j2
similarity index 94%
rename from roles/postgres-ha/files/repmgr-event.py
rename to roles/postgres-ha/templates/repmgr-event.py.j2
index e95a1bef..de85cebc 100755
--- a/roles/postgres-ha/files/repmgr-event.py
+++ b/roles/postgres-ha/templates/repmgr-event.py.j2
@@ -36,7 +36,7 @@ def main():
 
     # load config if exists
     try:
-        with open("/etc/postgresql/11/main/repmgr-event.json") as data_file:
+        with open("/etc/postgresql/{{ repmgr_pg_version }}/{{ repmgr_pg_cluster }}/repmgr-event.json") as data_file:
             data = json.load(data_file)
     except EnvironmentError:
         data = {}
diff --git a/roles/postgres-ha/templates/repmgr.conf.j2 b/roles/postgres-ha/templates/repmgr.conf.j2
index 2cd1dc39..7934ba59 100644
--- a/roles/postgres-ha/templates/repmgr.conf.j2
+++ b/roles/postgres-ha/templates/repmgr.conf.j2
@@ -3,7 +3,7 @@ node_name={{ repmgr_node_name }}
 conninfo='{{ repmgr_conninfo }}'
 data_directory=/var/lib/postgresql/{{ repmgr_pg_version }}/{{ repmgr_pg_cluster }}
 use_replication_slots=1
-event_notification_command='/usr/local/bin/repmgr-event %n %e %s "%t" "%d"'
+event_notification_command='/usr/bin/repmgr-event %n %e %s "%t" "%d"'
 pg_bindir=/usr/lib/postgresql/{{ repmgr_pg_version }}/bin/
 pg_ctl_options='-s'
 pg_basebackup_options='--label=repmgr_backup'
@@ -11,8 +11,8 @@ primary_follow_timeout=30
 failover=automatic
 reconnect_attempts=6
 reconnect_interval=5
-promote_command='repmgr standby promote'
-follow_command='repmgr standby follow -W'
+promote_command='repmgr -f {{ repmgr_config }} standby promote'
+follow_command='repmgr -f {{ repmgr_config }} standby follow -W'
 service_start_command='sudo systemctl start postgresql@{{ repmgr_pg_version }}-{{ repmgr_pg_cluster }}'
 service_stop_command='sudo systemctl stop postgresql@{{ repmgr_pg_version }}-{{ repmgr_pg_cluster }}'
 service_restart_command='sudo systemctl restart postgresql@{{ repmgr_pg_version }}-{{ repmgr_pg_cluster }}'
-- 
GitLab