Skip to content

Commit df26df2

Browse files
committed
fix postgres cluster
1 parent 1bb05d0 commit df26df2

8 files changed

Lines changed: 124 additions & 79 deletions

File tree

infra/aggregation_mode/ansible/playbooks/ini/config-hoodi.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[DEFAULT]
1+
[global]
22
# ============================================
33
# Hoodi Environment Configuration
44
# ============================================

infra/aggregation_mode/ansible/playbooks/ini/config-mainnet.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[DEFAULT]
1+
[global]
22
# ============================================
33
# Mainnet Environment Configuration
44
# ============================================

infra/aggregation_mode/ansible/playbooks/pg_autofailover_common.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- postgresql-common
1010
- ca-certificates
1111
- curl
12+
- acl
1213
state: latest
1314
update_cache: true
1415
vars:

infra/aggregation_mode/ansible/playbooks/pg_monitor.yaml

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
1+
- name: Import pg_autofailover_common playbook
2+
ansible.builtin.import_playbook: pg_autofailover_common.yaml
3+
vars:
4+
host: "{{ host }}"
5+
16
- name: PostgreSQL Monitor Setup
27
hosts: "{{ host }}"
8+
vars:
9+
ansible_common_remote_group: postgres
310

411
tasks:
5-
- name: Import pg_autofailover_common playbook
6-
ansible.builtin.import_playbook: pg_autofailover_common.yaml
7-
vars:
8-
host: "{{ host }}"
12+
- name: Set config file path
13+
set_fact:
14+
config_file: "ini/config-{{ env }}.ini"
915

10-
- hosts: "{{ host }}"
11-
vars:
12-
db_password: "{{ lookup('ini', 'db_password', file='ini/config-{{ env }}.ini') }}"
13-
monitor_pgdata: "{{ lookup('ini', 'monitor_pgdata', file='ini/config-{{ env }}.ini', default='/var/lib/postgresql/monitor') }}"
14-
monitor_port: "{{ lookup('ini', 'monitor_port', file='ini/config-{{ env }}.ini', default='5432') }}"
16+
- name: Debug config file
17+
debug:
18+
msg: "Using config file: {{ config_file }}"
19+
20+
- name: Set config vars from INI file
21+
set_fact:
22+
db_password: "{{ lookup('ini', 'db_password', file=config_file) }}"
23+
monitor_pgdata: "{{ lookup('ini', 'monitor_pgdata', file=config_file, default='/var/lib/postgresql/monitor') }}"
24+
monitor_port: "{{ lookup('ini', 'monitor_port', file=config_file, default='5432') }}"
25+
no_log: true
26+
27+
- name: Debug vars
28+
debug:
29+
msg: "env={{ env }}, monitor_pgdata={{ monitor_pgdata }}"
1530

16-
tasks:
1731
- name: Install postgresql-16-auto-failover
1832
become: true
1933
apt:
@@ -50,19 +64,6 @@
5064
args:
5165
creates: "{{ monitor_pgdata }}/PG_VERSION"
5266

53-
- name: Wait for monitor to be ready
54-
wait_for:
55-
port: "{{ monitor_port }}"
56-
delay: 5
57-
timeout: 60
58-
59-
- name: Set password for autoctl_node user
60-
become: true
61-
become_user: postgres
62-
shell: |
63-
psql -d pg_auto_failover -c "ALTER USER autoctl_node PASSWORD '{{ db_password }}';"
64-
no_log: true
65-
6667
- name: Update pg_hba.conf for Tailscale network
6768
become: true
6869
become_user: postgres
@@ -99,3 +100,16 @@
99100
daemon_reload: true
100101
vars:
101102
ansible_ssh_user: "{{ admin_user }}"
103+
104+
- name: Wait for monitor to be ready
105+
wait_for:
106+
port: "{{ monitor_port }}"
107+
delay: 5
108+
timeout: 60
109+
110+
- name: Set password for autoctl_node user
111+
become: true
112+
become_user: postgres
113+
shell: |
114+
psql -d pg_auto_failover -c "ALTER USER autoctl_node PASSWORD '{{ db_password }}';"
115+
no_log: true

infra/aggregation_mode/ansible/playbooks/pg_node.yaml

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
- name: Import pg_autofailover_common playbook
2+
ansible.builtin.import_playbook: pg_autofailover_common.yaml
3+
vars:
4+
host: "{{ host }}"
5+
16
- name: PostgreSQL Node Setup
27
hosts: "{{ host }}"
8+
vars:
9+
ansible_common_remote_group: postgres
310

411
tasks:
5-
- name: Import pg_autofailover_common playbook
6-
ansible.builtin.import_playbook: pg_autofailover_common.yaml
7-
vars:
8-
host: "{{ host }}"
12+
- name: Set config file path
13+
set_fact:
14+
config_file: "ini/config-{{ env }}.ini"
915

10-
- hosts: "{{ host }}"
11-
vars:
12-
db_name: "{{ lookup('ini', 'db_name', file='ini/config-{{ env }}.ini') }}"
13-
db_user: "{{ lookup('ini', 'db_user', file='ini/config-{{ env }}.ini') }}"
14-
db_password: "{{ lookup('ini', 'db_password', file='ini/config-{{ env }}.ini') }}"
15-
postgres_monitor_hostname: "{{ lookup('ini', 'postgres_monitor_hostname', file='ini/config-{{ env }}.ini') }}"
16-
node_pgdata: "{{ lookup('ini', 'node_pgdata', file='ini/config-{{ env }}.ini', default='/var/lib/postgresql/node') }}"
17-
node_port: "{{ lookup('ini', 'node_port', file='ini/config-{{ env }}.ini', default='5432') }}"
18-
backup_dir: "{{ lookup('ini', 'backup_dir', file='ini/config-{{ env }}.ini', default='/var/lib/backup') }}"
16+
- name: Set config vars from INI file
17+
set_fact:
18+
db_name: "{{ lookup('ini', 'db_name', file=config_file) }}"
19+
db_user: "{{ lookup('ini', 'db_user', file=config_file) }}"
20+
db_password: "{{ lookup('ini', 'db_password', file=config_file) }}"
21+
postgres_monitor_hostname: "{{ lookup('ini', 'postgres_monitor_hostname', file=config_file) }}"
22+
node_pgdata: "{{ lookup('ini', 'node_pgdata', file=config_file, default='/var/lib/postgresql/node') }}"
23+
node_port: "{{ lookup('ini', 'node_port', file=config_file, default='5432') }}"
24+
backup_dir: "{{ lookup('ini', 'backup_dir', file=config_file, default='/var/lib/backup') }}"
25+
no_log: true
1926

20-
tasks:
2127
- name: Create backup directory
2228
become: true
2329
file:
@@ -36,6 +42,10 @@
3642
become: true
3743
become_user: postgres
3844

45+
- name: Debug node_initialized
46+
debug:
47+
msg: "Node initialized: {{ node_initialized.stat.exists }}, path: {{ node_pgdata }}/PG_VERSION"
48+
3949
- name: Get Tailscale IP
4050
shell: tailscale ip --4
4151
register: tailscale_ip
@@ -56,7 +66,6 @@
5666
--hostname {{ tailscale_ip.stdout }} \
5767
--monitor 'postgres://{{ db_user }}:{{ db_password }}@{{ postgres_monitor_hostname }}:5432/pg_auto_failover?sslmode=require'
5868
when: not node_initialized.stat.exists
59-
no_log: true
6069
args:
6170
creates: "{{ node_pgdata }}/PG_VERSION"
6271

@@ -108,3 +117,19 @@
108117
port: "{{ node_port }}"
109118
delay: 5
110119
timeout: 60
120+
121+
- name: Check if node is writable (primary)
122+
become: true
123+
become_user: postgres
124+
shell: |
125+
psql -d {{ db_name }} -tAc "SELECT NOT pg_is_in_recovery();"
126+
register: is_writable
127+
changed_when: false
128+
129+
- name: Set password for pgautofailover_replicator user
130+
become: true
131+
become_user: postgres
132+
shell: |
133+
psql -d {{ db_name }} -c "ALTER USER pgautofailover_replicator PASSWORD '{{ db_password }}';"
134+
when: is_writable.stdout == 't'
135+
no_log: true
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
- name: Deploy PostgreSQL Auto-Failover Cluster
2-
hosts: localhost
3-
gather_facts: no
1+
- name: Deploy PostgreSQL Monitor
2+
ansible.builtin.import_playbook: pg_monitor.yaml
3+
vars:
4+
host: postgres_monitor
5+
env: "{{ env }}"
46

5-
tasks:
6-
- name: Deploy PostgreSQL Monitor
7-
ansible.builtin.import_playbook: pg_monitor.yaml
8-
vars:
9-
host: postgres_monitor
7+
- name: Deploy PostgreSQL Primary Node
8+
ansible.builtin.import_playbook: pg_node.yaml
9+
vars:
10+
host: postgres_primary
11+
env: "{{ env }}"
1012

11-
- name: Deploy PostgreSQL Primary Node
12-
ansible.builtin.import_playbook: pg_node.yaml
13-
vars:
14-
host: postgres_primary
13+
- name: Deploy PostgreSQL Secondary Node
14+
ansible.builtin.import_playbook: pg_node.yaml
15+
vars:
16+
host: postgres_secondary
17+
env: "{{ env }}"
1518

16-
- name: Deploy PostgreSQL Secondary Node
17-
ansible.builtin.import_playbook: pg_node.yaml
18-
vars:
19-
host: postgres_secondary
20-
21-
- name: Run Database Migrations
22-
ansible.builtin.import_playbook: postgres_migrations.yaml
23-
vars:
24-
host: postgres_primary
19+
- name: Run Database Migrations
20+
ansible.builtin.import_playbook: postgres_migrations.yaml
21+
vars:
22+
host: postgres_primary
23+
env: "{{ env }}"

infra/aggregation_mode/ansible/playbooks/postgres_migrations.yaml

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1+
- name: Import setup playbook
2+
ansible.builtin.import_playbook: setup.yaml
3+
vars:
4+
host: "{{ host }}"
5+
env: "{{ env }}"
6+
7+
- name: Import rust playbook
8+
ansible.builtin.import_playbook: rust.yaml
9+
vars:
10+
host: "{{ host }}"
11+
env: "{{ env }}"
12+
113
- name: PostgreSQL Migrations Setup
214
hosts: "{{ host }}"
315

416
tasks:
5-
- name: Import setup playbook
6-
ansible.builtin.import_playbook: setup.yaml
7-
vars:
8-
host: "{{ host }}"
17+
- name: Set config file path
18+
set_fact:
19+
config_file: "ini/config-{{ env }}.ini"
920

10-
- name: Import rust playbook
11-
ansible.builtin.import_playbook: rust.yaml
12-
vars:
13-
host: "{{ host }}"
14-
15-
- hosts: "{{ host }}"
16-
vars:
17-
db_name: "{{ lookup('ini', 'db_name', file='ini/config-{{ env }}.ini') }}"
18-
db_user: "{{ lookup('ini', 'db_user', file='ini/config-{{ env }}.ini') }}"
19-
db_password: "{{ lookup('ini', 'db_password', file='ini/config-{{ env }}.ini') }}"
20-
postgres_primary_hostname: "{{ lookup('ini', 'postgres_primary_hostname', file='ini/config-{{ env }}.ini') }}"
21-
git_branch: "{{ lookup('ini', 'git_branch', file='ini/config-{{ env }}.ini') }}"
21+
- name: Set config vars from INI file
22+
set_fact:
23+
db_name: "{{ lookup('ini', 'db_name', file=config_file) }}"
24+
db_user: "{{ lookup('ini', 'db_user', file=config_file) }}"
25+
db_password: "{{ lookup('ini', 'db_password', file=config_file) }}"
26+
postgres_primary_hostname: "{{ lookup('ini', 'postgres_primary_hostname', file=config_file) }}"
27+
git_branch: "{{ lookup('ini', 'git_branch', file=config_file) }}"
28+
no_log: true
2229

23-
tasks:
2430
- name: Clone aligned_layer repository
2531
git:
2632
repo: https://github.com/yetanotherco/aligned_layer.git

infra/aggregation_mode/ansible/playbooks/rust.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
- name: Rust Setup
22
hosts: "{{ host }}"
33
vars:
4-
rust_version: 1.83.0
4+
rust_version: 1.92.0
55

66
tasks:
77
- name: Check if cargo is installed

0 commit comments

Comments
 (0)