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