Commit | Line | Data |
---|---|---|
6f99f3c2 S |
1 | params: |
2 | db_synchronous_commit: "off" | |
3 | db_shared_buffers: "256MB" | |
4 | db_work_mem: "10MB" | |
8019560c | 5 | db_default_text_search_config: "pg_catalog.english" |
cfd0322a GXT |
6 | db_name: discourse |
7 | db_user: discourse | |
83d3ef11 S |
8 | db_wal_level: minimal |
9 | db_max_wal_senders: 0 | |
10 | db_checkpoint_segments: 6 | |
11 | db_logging_collector: off | |
12 | db_log_min_duration_statement: 100 | |
13 | ||
9fb5f2d3 | 14 | hooks: |
9fb5f2d3 | 15 | before_code: |
9fb5f2d3 SS |
16 | - replace: |
17 | filename: /etc/service/unicorn/run | |
18 | from: "# postgres" | |
19 | to: sv start postgres || exit 1 | |
20 | ||
9fb5f2d3 | 21 | run: |
d7ed7814 SS |
22 | - exec: mkdir -p /shared/postgres_run |
23 | - exec: chown postgres:postgres /shared/postgres_run | |
24 | - exec: chmod 775 /shared/postgres_run | |
25 | - exec: rm -fr /var/run/postgresql | |
26 | - exec: ln -s /shared/postgres_run /var/run/postgresql | |
27 | - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1 | |
61975bbd SS |
28 | - exec: rm -fr /shared/postgres_run/.s* |
29 | - exec: rm -fr /shared/postgres_run/*.pid | |
8194e684 GXT |
30 | - exec: mkdir -p /shared/postgres_run/10-main.pg_stat_tmp |
31 | - exec: chown postgres:postgres /shared/postgres_run/10-main.pg_stat_tmp | |
9fb5f2d3 SS |
32 | - file: |
33 | path: /etc/service/postgres/run | |
34 | chmod: "+x" | |
35 | contents: | | |
36 | #!/bin/sh | |
37 | exec 2>&1 | |
8194e684 GXT |
38 | HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main |
39 | ||
3f9f3c3b S |
40 | - file: |
41 | path: /etc/service/postgres/log/run | |
42 | chmod: "+x" | |
43 | contents: | | |
44 | #!/bin/sh | |
45 | mkdir -p /var/log/postgres | |
46 | exec svlogd /var/log/postgres | |
074f2b6a S |
47 | |
48 | - file: | |
49 | path: /etc/runit/3.d/99-postgres | |
50 | chmod: "+x" | |
51 | contents: | | |
52 | #!/bin/bash | |
53 | sv stop postgres | |
54 | ||
d69fe0fb S |
55 | - file: |
56 | path: /root/upgrade_postgres | |
57 | chmod: "+x" | |
58 | contents: | | |
59 | #!/bin/bash | |
49b852c5 S |
60 | PG_MAJOR_OLD=`cat /shared/postgres_data/PG_VERSION` |
61 | ||
8194e684 | 62 | if [ ! "10" = "$PG_MAJOR_OLD" ]; then |
448a70b9 GXT |
63 | echo Upgrading PostgreSQL from version ${PG_MAJOR_OLD} to 10 |
64 | free_disk=$(df /var | tail -n 1 | awk '{print $4}') | |
65 | required=$(($(du -s /shared/postgres_data | awk '{print $1}') * 2)) | |
66 | ||
67 | if [ "$free_disk" -lt "$required" ]; then | |
969cc9b9 | 68 | echo "WARNING: Upgrading PostgresSQL would require an addtional $(numfmt --to=si $(($required - $free_disk))) of disk space" |
448a70b9 GXT |
69 | echo "Please free up some space, or expand your disk, before continuing." |
70 | exit 1 | |
71 | fi | |
72 | ||
d69fe0fb | 73 | rm -fr /shared/postgres_data_new |
8194e684 | 74 | install -d -m 0755 -o postgres -g postgres /shared/postgres_data_new && sudo -u postgres /usr/lib/postgresql/10/bin/initdb -D /shared/postgres_data_new || exit 0 |
a0958e4f | 75 | apt-get update |
49b852c5 | 76 | apt-get install -y postgresql-${PG_MAJOR_OLD} |
8194e684 | 77 | chown -R postgres:postgres /var/lib/postgresql/10 |
61975bbd SS |
78 | /etc/init.d/postgresql stop |
79 | rm -fr /shared/postgres_data/postmaster.pid | |
d69fe0fb | 80 | cd ~postgres |
8194e684 | 81 | cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data |
61975bbd SS |
82 | echo >> /shared/postgres_data/postgresql.conf |
83 | echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf | |
9b9cdd3b | 84 | SUCCESS=true |
8194e684 | 85 | sudo -u postgres /usr/lib/postgresql/10/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/${PG_MAJOR_OLD}/bin -B /usr/lib/postgresql/10/bin || SUCCESS=false |
9b9cdd3b S |
86 | |
87 | if [[ "$SUCCESS" == 'false' ]]; then | |
88 | echo ------------------------------------------------------------------------------------- | |
89 | echo UPGRADE OF POSTGRES FAILED | |
90 | echo | |
91 | echo You are going to need to export your data and import into a clean instance: | |
92 | echo | |
49b852c5 | 93 | |
8194e684 GXT |
94 | if [ "$PG_MAJOR_OLD" = "9.5" ]; then |
95 | echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml"' | |
49b852c5 S |
96 | echo |
97 | fi | |
98 | ||
99 | echo Run "./launcher rebuild app" again | |
9b9cdd3b S |
100 | echo |
101 | echo When your instance is running: | |
49b852c5 | 102 | echo Run "./launcher enter app" |
44f13244 | 103 | echo Run apt-get remove postgresql-client-9.5 && apt-get instatll postgresql-client-10 |
49b852c5 | 104 | echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db" |
9b9cdd3b | 105 | echo |
8194e684 GXT |
106 | |
107 | if [ "$PG_MAJOR_OLD" = "9.5" ]; then | |
4ea0bfa8 LB |
108 | echo Undo the base_image in your container config |
109 | fi | |
8194e684 | 110 | |
49b852c5 | 111 | echo Run: "./launcher stop app" |
51d2e647 | 112 | echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old" |
49b852c5 | 113 | echo Run: "./launcher rebuild app" |
9b9cdd3b | 114 | echo |
49b852c5 S |
115 | echo Run: "./launcher enter app" |
116 | echo Run: "cd /shared/postgres_backup" | |
117 | echo Run: "sv stop unicorn" | |
118 | echo Run: "sudo -iu postgres dropdb discourse" | |
119 | echo Run: "sudo -iu postgres createdb discourse" | |
120 | echo Run: "sudo -iu postgres psql discourse < backup.db" | |
121 | echo Run: "exit" | |
122 | echo Run: "./launcher rebuild app" | |
9b9cdd3b S |
123 | exit 1 |
124 | fi | |
125 | ||
d69fe0fb S |
126 | mv /shared/postgres_data /shared/postgres_data_old |
127 | mv /shared/postgres_data_new /shared/postgres_data | |
128 | ||
129 | echo ------------------------------------------------------------------------------------- | |
130 | echo UPGRADE OF POSTGRES COMPLETE | |
131 | echo | |
49b852c5 | 132 | echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old |
d69fe0fb | 133 | echo |
72b89d4d AH |
134 | echo To complete the upgrade, rebuild again using: |
135 | echo | |
136 | echo ./launcher rebuild app | |
d69fe0fb | 137 | echo ------------------------------------------------------------------------------------- |
b4ab14c2 S |
138 | # Magic exit status to denote no failure |
139 | exit 77 | |
d69fe0fb | 140 | fi |
8194e684 | 141 | |
9fb5f2d3 SS |
142 | - exec: |
143 | cmd: | |
8194e684 GXT |
144 | - chown -R root /var/lib/postgresql/10/main |
145 | - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/10/bin/initdb -D /shared/postgres_data || exit 0" | |
9fb5f2d3 | 146 | - chown -R postgres:postgres /shared/postgres_data |
d69fe0fb | 147 | - chown -R postgres:postgres /var/run/postgresql |
9fb5f2d3 | 148 | |
61975bbd | 149 | - exec: /root/upgrade_postgres |
6f99f3c2 S |
150 | - exec: rm /root/upgrade_postgres |
151 | ||
9fb5f2d3 | 152 | - replace: |
8194e684 GXT |
153 | filename: "/etc/postgresql/10/main/postgresql.conf" |
154 | from: "data_directory = '/var/lib/postgresql/10/main'" | |
9fb5f2d3 SS |
155 | to: "data_directory = '/shared/postgres_data'" |
156 | ||
80c11be3 SS |
157 | # listen on all interfaces |
158 | - replace: | |
8194e684 | 159 | filename: "/etc/postgresql/10/main/postgresql.conf" |
80c11be3 SS |
160 | from: /#?listen_addresses *=.*/ |
161 | to: "listen_addresses = '*'" | |
162 | ||
6f99f3c2 S |
163 | # sync commit off is faster and less spiky, also marginally less safe |
164 | - replace: | |
8194e684 | 165 | filename: "/etc/postgresql/10/main/postgresql.conf" |
6f99f3c2 S |
166 | from: /#?synchronous_commit *=.*/ |
167 | to: "synchronous_commit = $db_synchronous_commit" | |
168 | ||
169 | # default is 128MB which is way too small | |
170 | - replace: | |
8194e684 | 171 | filename: "/etc/postgresql/10/main/postgresql.conf" |
6f99f3c2 S |
172 | from: /#?shared_buffers *=.*/ |
173 | to: "shared_buffers = $db_shared_buffers" | |
174 | ||
175 | # default is 1MB which is too small | |
176 | - replace: | |
8194e684 | 177 | filename: "/etc/postgresql/10/main/postgresql.conf" |
6f99f3c2 S |
178 | from: /#?work_mem *=.*/ |
179 | to: "work_mem = $db_work_mem" | |
180 | ||
181 | # allow for other | |
182 | - replace: | |
8194e684 | 183 | filename: "/etc/postgresql/10/main/postgresql.conf" |
6f99f3c2 S |
184 | from: /#?default_text_search_config *=.*/ |
185 | to: "default_text_search_config = '$db_default_text_search_config'" | |
186 | ||
9fb5f2d3 SS |
187 | # Necessary to enable backups |
188 | - exec: | |
189 | cmd: | |
190 | - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup | |
191 | ||
192 | - replace: | |
8194e684 | 193 | filename: "/etc/postgresql/10/main/postgresql.conf" |
9fb5f2d3 | 194 | from: /#?max_wal_senders *=.*/ |
83d3ef11 | 195 | to: "max_wal_senders = $db_max_wal_senders" |
9fb5f2d3 SS |
196 | |
197 | - replace: | |
8194e684 | 198 | filename: "/etc/postgresql/10/main/postgresql.conf" |
9fb5f2d3 | 199 | from: /#?wal_level *=.*/ |
83d3ef11 S |
200 | to: "wal_level = $db_wal_level" |
201 | ||
202 | - replace: | |
8194e684 | 203 | filename: "/etc/postgresql/10/main/postgresql.conf" |
83d3ef11 S |
204 | from: /#?checkpoint_segments *=.*/ |
205 | to: "checkpoint_segments = $db_checkpoint_segments" | |
206 | ||
207 | - replace: | |
8194e684 | 208 | filename: "/etc/postgresql/10/main/postgresql.conf" |
83d3ef11 S |
209 | from: /#?logging_collector *=.*/ |
210 | to: "logging_collector = $db_logging_collector" | |
211 | ||
212 | - replace: | |
8194e684 | 213 | filename: "/etc/postgresql/10/main/postgresql.conf" |
83d3ef11 S |
214 | from: /#?log_min_duration_statement *=.*/ |
215 | to: "log_min_duration_statement = $db_log_min_duration_statement" | |
9fb5f2d3 SS |
216 | |
217 | - replace: | |
8194e684 | 218 | filename: "/etc/postgresql/10/main/pg_hba.conf" |
9fb5f2d3 SS |
219 | from: /^#local +replication +postgres +peer$/ |
220 | to: "local replication postgres peer" | |
80c11be3 SS |
221 | |
222 | # allow all to connect in with md5 auth | |
223 | - replace: | |
8194e684 | 224 | filename: "/etc/postgresql/10/main/pg_hba.conf" |
80c11be3 SS |
225 | from: /^host.*all.*all.*127.*$/ |
226 | to: "host all all 0.0.0.0/0 md5" | |
227 | ||
228 | - exec: | |
229 | background: true | |
c244475c S |
230 | # use fast shutdown for pg |
231 | stop_signal: INT | |
8194e684 | 232 | cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main |
80c11be3 SS |
233 | |
234 | # give db a few secs to start up | |
235 | - exec: "sleep 5" | |
236 | ||
cfd0322a GXT |
237 | - exec: su postgres -c 'createdb $db_name' || true |
238 | - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true | |
239 | - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true | |
240 | - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"' | |
7f4eef4c | 241 | - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"' |
242 | - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"' | |
cfd0322a GXT |
243 | - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"' |
244 | - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"' | |
2b09a847 AT |
245 | - exec: |
246 | stdin: | | |
e4eb2bc0 | 247 | update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII'); |
cfd0322a | 248 | cmd: sudo -u postgres psql $db_name |
2b09a847 | 249 | raise_on_fail: false |
80c11be3 | 250 | |
be82e068 S |
251 | - file: |
252 | path: /var/lib/postgresql/take-database-backup | |
253 | chown: postgres:postgres | |
254 | chmod: "+x" | |
255 | contents: | | |
256 | #!/bin/bash | |
257 | ID=db-$(date +%F_%T) | |
258 | FILENAME=/shared/postgres_backup/$ID.tar.gz | |
259 | pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME | |
260 | echo $FILENAME | |
261 | ||
262 | - file: | |
263 | path: /var/spool/cron/crontabs/postgres | |
264 | contents: | | |
265 | # m h dom mon dow command | |
266 | #MAILTO=? | |
e56a65f6 | 267 | #0 */4 * * * /var/lib/postgresql/take-database-backup |
be82e068 | 268 | |
80c11be3 SS |
269 | - exec: |
270 | hook: postgres | |
271 | cmd: "echo postgres installed!" |