Skip to content

Commit 050674c

Browse files
committed
feat(pgsql): add pgsql util playbooks
- start and stop postgres cluster - start and stop patroni - patroni pause and resume - patroni restart (gracefully)
1 parent b670279 commit 050674c

File tree

11 files changed

+259
-0
lines changed

11 files changed

+259
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
4+
- name: check postgres ready
5+
tags: check_pg_ready
6+
vars:
7+
dbsu: "{{ pg_dbsu|default('postgres') }}"
8+
block:
9+
- name: wait for postgres
10+
# when: pg_role == 'primary'
11+
wait_for: host={{ inventory_hostname }} port={{ pg_port }} state=started timeout=60
12+
ignore_errors: true
13+
14+
- name: check postgres ready
15+
become_user: "{{ dbsu }}"
16+
shell: |
17+
{{ pg_bin_dir }}/pg_isready -t 5 -p {{ pg_port }}
18+
register: result
19+
retries: 6
20+
until: result.rc == 0
21+
delay: 5
22+
23+
- name: Set fact pg_ready_result
24+
set_fact:
25+
pg_ready_result: "{{ result }}"
26+
27+
...
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
#--------------------------------------------------------------#
4+
# patroni restart pg_cls gracefully [grace_pt_restart]
5+
# avoiding switching pg primary/standby
6+
# steps:
7+
# 1. patroni pause
8+
# 2. patroni restart pg_cluster
9+
# 3. patroni resume
10+
#--------------------------------------------------------------#
11+
- name: patroni restart pg_cls gracefully
12+
tags: grace_pt_restart
13+
become_user: "{{ dbsu }}"
14+
vars:
15+
dbsu: "{{ pg_dbsu|default('postgres') }}"
16+
block:
17+
- import_tasks: patroni_pause.yml
18+
19+
- import_tasks: patroni_restart.yml
20+
when: patroni_mode != 'remove' and pg_role == 'primary'
21+
22+
- import_tasks: check_pg_ready.yml
23+
when: pg_role == 'primary'
24+
25+
- import_tasks: patroni_resume.yml
26+
when: patroni_mode != 'remove' and pg_role == 'primary'
27+
28+
# finally, check if all postgres is ready
29+
- import_tasks: check_pg_ready.yml
30+
31+
rescue:
32+
- name: check postgres ready failed for {{ pg_cluster }}
33+
debug:
34+
msg: |
35+
rc: {{ pg_ready_result.rc }}
36+
STDOUT: {{ pg_ready_result.stdout }}
37+
STDERR: {{ pg_ready_result.stderr }}
38+
when: pg_ready_result is defined and pg_ready_result.rc != 0
39+
40+
- name: Exit Playbook due to error
41+
meta: end_play
42+
43+
...
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
- name: run patroni list
4+
become_user: "{{ pg_dbsu|default('postgres') }}"
5+
when: patroni_mode != 'remove'
6+
args: { executable: /bin/bash }
7+
shell: |
8+
/usr/bin/patronictl -c /pg/bin/patroni.yml list 2>/dev/null | tail -n 3
9+
register: patroni_status_cmd
10+
ignore_errors: yes
11+
changed_when: false
12+
13+
- name: read patroni maintenance mode
14+
when: patroni_mode != 'remove'
15+
set_fact:
16+
patroni_paused: "{{ patroni_status_cmd.stdout | regex_search('Maintenance mode: on') }}"
17+
changed_when: false
18+
19+
...
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
4+
- name: patroni pause
5+
tags: patroni_pause
6+
vars:
7+
dbsu: "{{ pg_dbsu|default('postgres') }}"
8+
any_errors_fatal: true
9+
when: patroni_mode != 'remove'
10+
block:
11+
- import_tasks: is_patroni_paused.yml
12+
when: pg_role == 'primary'
13+
14+
- name: patroni pause primary {{ pg_cluster }}
15+
become_user: "{{ dbsu }}"
16+
when: (patroni_paused is not defined or patroni_paused == '') and pg_role == 'primary'
17+
command: /usr/bin/patronictl -c /pg/bin/patroni.yml pause
18+
19+
- name: sleep for patroni paused
20+
command: sleep 3
21+
when: (patroni_paused is not defined or patroni_paused == '')
22+
changed_when: false
23+
24+
- import_tasks: is_patroni_paused.yml
25+
when: pg_role != 'primary'
26+
27+
- name: patroni pause replica {{ pg_cluster }} (may fail)
28+
become_user: "{{ dbsu }}"
29+
when: (patroni_paused is not defined or patroni_paused == '') and pg_role != 'primary'
30+
command: /usr/bin/patronictl -c /pg/bin/patroni.yml pause
31+
ignore_errors: yes
32+
33+
...
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
4+
- name: patroni restart {{ pg_cluster }}
5+
become_user: "{{ dbsu }}"
6+
vars:
7+
dbsu: "{{ pg_dbsu|default('postgres') }}"
8+
command: /usr/bin/patronictl -c /pg/bin/patroni.yml restart --force {{ pg_cluster }}
9+
10+
...
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
4+
- name: patroni resume primary
5+
tags: patroni_resume
6+
become_user: "{{ dbsu }}"
7+
vars:
8+
dbsu: "{{ pg_dbsu|default('postgres') }}"
9+
command: /usr/bin/patronictl -c /pg/bin/patroni.yml resume
10+
11+
...
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
4+
- name: pg_backup
5+
when: pgbackrest_enabled|bool
6+
tags: pg_backup
7+
vars:
8+
dbsu: "{{ pg_dbsu|default('postgres') }}"
9+
block:
10+
- name: full backup cls {{ pg_cluster }}
11+
become_user: "{{ dbsu }}"
12+
when: pg_role == 'primary'
13+
command: /pg/bin/pg-backup full
14+
register: back_ret_cmd
15+
ignore_errors: false
16+
17+
- name: show backup result for {{ pg_cluster }}
18+
when: pg_role == 'primary'
19+
debug:
20+
msg: |
21+
STDOUT {{ back_ret_cmd.stdout_lines }}
22+
STDERR {{ back_ret_cmd.stderr_lines }}
23+
changed_when: false
24+
25+
rescue:
26+
- name: pg-backup failed for {{ pg_cluster }}
27+
debug:
28+
msg: |
29+
STDOUT: {{ back_ret_cmd.stdout }}
30+
STDERR: {{ back_ret_cmd.stderr }}
31+
when: back_ret_cmd is defined
32+
33+
- name: Exit Playbook due to backup failure
34+
meta: end_play
35+
...
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
- name: start patroni
4+
tags: start_patroni
5+
vars:
6+
dbsu: "{{ pg_dbsu|default('postgres') }}"
7+
block:
8+
- name: start patroni
9+
when: patroni_mode == 'default'
10+
command: systemctl start patroni
11+
12+
- name: sleep waiting for starting patroni
13+
when: patroni_mode == 'default'
14+
args: { executable: /bin/bash }
15+
shell: sleep 5
16+
changed_when: false
17+
18+
...
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
4+
- name: start postgres cluster
5+
tags: start_pg
6+
vars:
7+
dbsu: "{{ pg_dbsu|default('postgres') }}"
8+
block:
9+
- name: check if postmaster.pid exists and is not empty
10+
stat:
11+
path: "{{ pg_data }}/postmaster.pid"
12+
register: postmaster_pid_stat
13+
changed_when: false
14+
15+
- name: start postgres
16+
become_user: "{{ dbsu }}"
17+
when: (postmaster_pid_stat.stat.exists == false or postmaster_pid_stat.stat.size == 0)
18+
args: { executable: /bin/bash }
19+
shell: |
20+
{{ pg_bin_dir }}/pg_ctl -D {{ pg_data }} start
21+
22+
- import_tasks: check_pg_ready.yml
23+
24+
rescue:
25+
- name: check postgres ready failed for {{ pg_cluster }}
26+
debug:
27+
msg: |
28+
rc: {{ pg_ready_result.rc }}
29+
STDOUT: {{ pg_ready_result.stdout }}
30+
STDERR: {{ pg_ready_result.stderr }}
31+
when: pg_ready_result is defined and pg_ready_result.rc != 0
32+
33+
- name: Exit Playbook due to error
34+
meta: end_play
35+
36+
37+
...
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/ansible-playbook
2+
---
3+
4+
- name: stop patroni
5+
tags: stop_patroni
6+
any_errors_fatal: true
7+
when: patroni_mode == 'default'
8+
command: systemctl stop patroni
9+
10+
...

0 commit comments

Comments
 (0)