Skip to content

Commit 55ae020

Browse files
authored
refactor(ansible): bring our ansible up to modern ansible-lint standards (#1881)
* refactor(ansible): bring our ansible up to modern ansible-lint standards * fix(stage2-setup-postgres): typo fixup * fix(stage2-setup-postgres): lazy eval means we trip over variable naming rules * fix(stage2-setup-postgres): more playing w/ the variable evaluation * fix(stage2-setup-postgres): fine, you win. we won't loop, and we'll do it the long, slow ass way * fix(stage2-setup-postgres): when moving stuff around it hekps to move loop_var with it. sigh * fix(stage2-setup-postgres): not sure how i munged that path so badly. i blame the drugs * fix(stage2-setup-postgres): force overwriting * fix(stage2-setup-postgres): stop making things harder and just symlink parent dirs * fix(stage2-setup-postgres): nuke the dir so we can recreate it as a symlink * test(test-image): cat the pg conf file to stdout so i can see wtf is going on * test(test-image): don't let nvim autocomplete conf to config, damnit * fix(stage2-setup-postgres): the replacement isn't a regex so don't treat it as one * test(test-image): remove my debug output s things are sorted out now * refactor(ansible): bring our ansible up to modern ansible-lint standards * fix(test-image): remove errant double quote * fix(test-image): typo fix * fix(test-image): le sigh. mandatory args are mandatory * fix(test-image): thinko. we should be looking for the regex to be removed, not present * fix(test-image): a double ** is a no-no, and removing it makes the 2 lines identical, so nuke the one line * fix(test-image): community.postgresql needs psycopg2 * fix(test-image): restart PG and then try resetting stats * fix(test-image): put the reset task where it was and just skip pg_stat_statements when resetting
1 parent 9181e4d commit 55ae020

File tree

1 file changed

+94
-77
lines changed

1 file changed

+94
-77
lines changed

ansible/tasks/test-image.yml

Lines changed: 94 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,104 @@
1-
# - name: Temporarily disable PG Sodium references in config
2-
# become: yes
3-
# become_user: postgres
4-
# shell:
5-
# cmd: sed -i.bak -e "s/pg_net,\ pgsodium,\ timescaledb/pg_net,\ timescaledb/g" -e "s/pgsodium.getkey_script=/#pgsodium.getkey_script=/g" /etc/postgresql/postgresql.conf
6-
# when: debpkg_mode or stage2_nix
1+
- name: Execute tasks when (debpkg_mode or stage2_nix)
2+
when:
3+
- (debpkg_mode or stage2_nix)
4+
block:
5+
- name: Make a backup of the /etc/postgresql/postgresql.conf file
6+
ansible.builtin.copy:
7+
dest: '/etc/postgresql/postgresql.conf.bak'
8+
src: '/etc/postgresql/postgresql.conf'
9+
become: true
710

8-
- name: Temporarily disable PG Sodium and Supabase Vault references in config
9-
become: yes
10-
become_user: postgres
11-
shell:
12-
cmd: >
13-
sed -i.bak
14-
-e 's/\(shared_preload_libraries = '\''.*\)pgsodium,\(.*'\''\)/\1\2/'
15-
-e 's/\(shared_preload_libraries = '\''.*\)supabase_vault,\(.*'\''\)/\1\2/'
16-
-e 's/\(shared_preload_libraries = '\''.*\), *supabase_vault'\''/\1'\''/'
17-
-e 's/pgsodium.getkey_script=/#pgsodium.getkey_script=/'
18-
/etc/postgresql/postgresql.conf
19-
when: debpkg_mode or stage2_nix
11+
- name: Temporarily disable PG Sodium and Supabase Vault references in /etc/postgresql/postgresql.conf
12+
ansible.builtin.replace:
13+
path: '/etc/postgresql/postgresql.conf'
14+
regexp: "{{ regx['in'] }}"
15+
replace: "{{ regx['out'] }}"
16+
become: true
17+
become_user: 'postgres'
18+
loop:
19+
- { in: "^(shared_preload_libraries = '.*)pgsodium(.*')", out: '\1\2' }
20+
- { in: "^(shared_preload_libraries = '.*)supabase_vault(.*')", out: '\1\2' }
21+
- { in: "^(shared_preload_libraries = '.*)*supabase_vault(.*')", out: '\1\2' }
22+
- { in: '^(pgsodium\.getkey_script=)', out: '#\1' }
23+
loop_control:
24+
loop_var: 'regx'
2025

21-
- name: Verify pgsodium and vault removal from config
22-
become: yes
23-
become_user: postgres
24-
shell:
25-
cmd: |
26-
FOUND=$(grep -E "shared_preload_libraries.*pgsodium|shared_preload_libraries.*supabase_vault|^pgsodium\.getkey_script" /etc/postgresql/postgresql.conf)
27-
if [ ! -z "$FOUND" ]; then
28-
echo "Found unremoved references:"
29-
echo "$FOUND"
30-
exit 1
31-
fi
32-
register: verify_result
33-
failed_when: verify_result.rc != 0
34-
when: debpkg_mode or stage2_nix
26+
- name: Make sure we disabled all the things
27+
ansible.builtin.lineinfile:
28+
path: '/etc/postgresql/postgresql.conf'
29+
regexp: "{{ regx }}"
30+
state: 'absent'
31+
check_mode: true
32+
failed_when:
33+
- (pgconf is changed) or (pgconf is failed)
34+
loop:
35+
- "^shared_preload_libraries = '.*pgsodium.*'"
36+
- "^shared_preload_libraries = '.*supabase_vault.*'"
37+
- '^pgsodium\.getkey_script='
38+
loop_control:
39+
loop_var: 'regx'
40+
register: 'pgconf'
3541

3642
- name: Start Postgres Database to load all extensions.
37-
become: yes
38-
become_user: postgres
39-
shell:
43+
ansible.builtin.command:
4044
cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data start "-o -c config_file=/etc/postgresql/postgresql.conf"
41-
when: debpkg_mode
45+
become: true
46+
become_user: 'postgres'
47+
when:
48+
- debpkg_mode
4249

43-
- name: Stop Postgres Database in stage 2
44-
become: yes
45-
become_user: postgres
46-
shell: source /var/lib/postgresql/.bashrc && /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data stop
47-
args:
48-
executable: /bin/bash
49-
environment:
50-
LANG: en_US.UTF-8
51-
LANGUAGE: en_US.UTF-8
52-
LC_ALL: en_US.UTF-8
53-
LC_CTYPE: en_US.UTF-8
54-
LOCALE_ARCHIVE: /usr/lib/locale/locale-archive
55-
when: stage2_nix
50+
- name: Execute tasks when stage2_nix
51+
when:
52+
- stage2_nix
53+
block:
54+
- name: Restart Postgres Database in stage 2 to load all extensions
55+
ansible.builtin.command:
56+
cmd: "/usr/lib/postgresql/bin/pg_ctl --pgdata /var/lib/postgresql/data --mode fast --options '-c config_file=/etc/postgresql/postgresql.conf' {{ ctlcmd }}"
57+
become: true
58+
become_user: 'postgres'
59+
environment:
60+
LANG: 'en_US.UTF-8'
61+
LANGUAGE: 'en_US.UTF-8'
62+
LC_ALL: 'en_US.UTF-8'
63+
LC_CTYPE: 'en_US.UTF-8'
64+
LOCALE_ARCHIVE: '/usr/lib/locale/locale-archive'
65+
loop:
66+
- stop
67+
- start
68+
loop_control:
69+
loop_var: 'ctlcmd'
5670

57-
- name: Start Postgres Database to load all extensions.
58-
become: yes
59-
become_user: postgres
60-
shell: source /var/lib/postgresql/.bashrc && /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data start "-o -c config_file=/etc/postgresql/postgresql.conf"
61-
args:
62-
executable: /bin/bash
63-
environment:
64-
LANG: en_US.UTF-8
65-
LANGUAGE: en_US.UTF-8
66-
LC_ALL: en_US.UTF-8
67-
LC_CTYPE: en_US.UTF-8
68-
LOCALE_ARCHIVE: /usr/lib/locale/locale-archive
69-
when: stage2_nix
71+
- name: Execute tasks when (debpkg_mode or stage2_nix)
72+
when:
73+
- (debpkg_mode or stage2_nix)
74+
block:
75+
- name: Re-enable PG Sodium references in /etc/postgresql/postgresql.conf
76+
ansible.builtin.command:
77+
cmd: mv /etc/postgresql/postgresql.conf.bak /etc/postgresql/postgresql.conf
78+
become: true
79+
become_user: 'postgres'
7080

71-
- name: Re-enable PG Sodium references in config
72-
become: yes
73-
become_user: postgres
74-
shell:
75-
cmd: mv /etc/postgresql/postgresql.conf.bak /etc/postgresql/postgresql.conf
76-
when: debpkg_mode or stage2_nix
81+
- name: Install psycopg2
82+
ansible.builtin.apt:
83+
name: 'python3-psycopg2'
84+
state: 'present'
85+
update_cache: true
86+
become: true
7787

78-
- name: Reset db stats
79-
shell: /usr/lib/postgresql/bin/psql --no-password --no-psqlrc -d postgres -h localhost -U supabase_admin -c 'SELECT pg_stat_statements_reset(); SELECT pg_stat_reset();'
80-
when: debpkg_mode or stage2_nix
88+
- name: Reset db stats
89+
community.postgresql.postgresql_query:
90+
login_db: 'postgres'
91+
login_host: 'localhost'
92+
login_user: 'supabase_admin'
93+
query: "{{ stat_item }}"
94+
loop:
95+
# - 'SELECT pg_stat_statements_reset()'
96+
- 'SELECT pg_stat_reset()'
97+
loop_control:
98+
loop_var: 'stat_item'
8199

82-
- name: Stop Postgres Database
83-
become: yes
84-
become_user: postgres
85-
shell:
86-
cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data stop
87-
when: debpkg_mode or stage2_nix
100+
- name: Restart Postgres Database in stage 2 to load all extensions
101+
ansible.builtin.command:
102+
cmd: /usr/lib/postgresql/bin/pg_ctl --pgdata /var/lib/postgresql/data --mode fast --options '-c config_file=/etc/postgresql/postgresql.conf' stop
103+
become: true
104+
become_user: 'postgres'

0 commit comments

Comments
 (0)