More adjustments to the launcher resource checks
[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
15 hooks:
16 before_code:
17 - replace:
18 filename: /etc/service/unicorn/run
19 from: "# postgres"
20 to: sv start postgres || exit 1
21
22 run:
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/9.3-main.pg_stat_tmp
32 - exec: chown postgres:postgres /shared/postgres_run/9.3-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/9.3/bin/postmaster -D /etc/postgresql/9.3/main
40
41 - file:
42 path: /etc/runit/3.d/99-postgres
43 chmod: "+x"
44 contents: |
45 #!/bin/bash
46 sv stop postgres
47
48 - file:
49 path: /root/upgrade_postgres
50 chmod: "+x"
51 contents: |
52 #!/bin/bash
53 if [ "9.2" = `cat /shared/postgres_data/PG_VERSION` ]; then
54 echo Upgrading PostgreSQL to from version 9.2 to 9.3
55 rm -fr /shared/postgres_data_new
56 install -d -m 0755 -o postgres -g postgres /shared/postgres_data_new && sudo -u postgres /usr/lib/postgresql/9.3/bin/initdb -D /shared/postgres_data_new || exit 0
57 apt-get update
58 apt-get install -y postgresql-9.2
59 /etc/init.d/postgresql stop
60 rm -fr /shared/postgres_data/postmaster.pid
61 cd ~postgres
62 cp /etc/postgresql/9.2/main/*.conf /shared/postgres_data
63 echo >> /shared/postgres_data/postgresql.conf
64 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
65 SUCCESS=true
66 sudo -u postgres /usr/lib/postgresql/9.3/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/9.2/bin -B /usr/lib/postgresql/9.3/bin || SUCCESS=false
67
68 if [[ "$SUCCESS" == 'false' ]]; then
69 echo -------------------------------------------------------------------------------------
70 echo UPGRADE OF POSTGRES FAILED
71 echo
72 echo You are going to need to export your data and import into a clean instance:
73 echo
74 echo Add the line: 'base_image: "samsaffron/discourse:0.1.1"'
75 echo Change: "templates/postgres.template.yml TO templates/postgres.9.2.template.yml"
76 echo
77 echo Run "./launcher bootstrap" again
78 echo
79 echo When your instance is running:
80 echo - Go to the admin UI, enable readonly and backup your site
81 echo
82 echo Then destroy your container "./launcher destroy standalone"
83 echo Undo the base_image and postgres.9.2 template from your container config
84 echo Run: "sudo mv /var/discourse/shared/postgres_data /var/shared/postgres_data_old"
85 echo Run: "./launcher bootstrap"
86 echo
87 echo "Login to the blank site, and import your backup (remember to set the allow_restore site setting)"
88 echo Restart your container: "./launcher restart"
89 echo -------------------------------------------------------------------------------------
90 exit 1
91 fi
92
93 mv /shared/postgres_data /shared/postgres_data_old
94 mv /shared/postgres_data_new /shared/postgres_data
95
96 echo -------------------------------------------------------------------------------------
97 echo UPGRADE OF POSTGRES COMPLETE
98 echo
99 echo Old 9.2 database is stored at /shared/postgres_data_old
100 echo
101 echo Please run "./launcher bootstrap" again
102 echo -------------------------------------------------------------------------------------
103 exit 1
104 fi
105 - exec:
106 cmd:
107 - chown -R root /var/lib/postgresql/9.3/main
108 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/9.3/bin/initdb -D /shared/postgres_data || exit 0"
109 - chown -R postgres:postgres /shared/postgres_data
110 - chown -R postgres:postgres /var/run/postgresql
111
112 - exec: /root/upgrade_postgres
113 - exec: rm /root/upgrade_postgres
114
115 - replace:
116 filename: "/etc/postgresql/9.3/main/postgresql.conf"
117 from: "data_directory = '/var/lib/postgresql/9.3/main'"
118 to: "data_directory = '/shared/postgres_data'"
119
120 # listen on all interfaces
121 - replace:
122 filename: "/etc/postgresql/9.3/main/postgresql.conf"
123 from: /#?listen_addresses *=.*/
124 to: "listen_addresses = '*'"
125
126 # sync commit off is faster and less spiky, also marginally less safe
127 - replace:
128 filename: "/etc/postgresql/9.3/main/postgresql.conf"
129 from: /#?synchronous_commit *=.*/
130 to: "synchronous_commit = $db_synchronous_commit"
131
132 # default is 128MB which is way too small
133 - replace:
134 filename: "/etc/postgresql/9.3/main/postgresql.conf"
135 from: /#?shared_buffers *=.*/
136 to: "shared_buffers = $db_shared_buffers"
137
138 # default is 1MB which is too small
139 - replace:
140 filename: "/etc/postgresql/9.3/main/postgresql.conf"
141 from: /#?work_mem *=.*/
142 to: "work_mem = $db_work_mem"
143
144 # allow for other
145 - replace:
146 filename: "/etc/postgresql/9.3/main/postgresql.conf"
147 from: /#?default_text_search_config *=.*/
148 to: "default_text_search_config = '$db_default_text_search_config'"
149
150 # Necessary to enable backups
151 - exec:
152 cmd:
153 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
154
155 - replace:
156 filename: "/etc/postgresql/9.3/main/postgresql.conf"
157 from: /#?max_wal_senders *=.*/
158 to: "max_wal_senders = $db_max_wal_senders"
159
160 - replace:
161 filename: "/etc/postgresql/9.3/main/postgresql.conf"
162 from: /#?wal_level *=.*/
163 to: "wal_level = $db_wal_level"
164
165 - replace:
166 filename: "/etc/postgresql/9.3/main/postgresql.conf"
167 from: /#?checkpoint_segments *=.*/
168 to: "checkpoint_segments = $db_checkpoint_segments"
169
170 - replace:
171 filename: "/etc/postgresql/9.3/main/postgresql.conf"
172 from: /#?logging_collector *=.*/
173 to: "logging_collector = $db_logging_collector"
174
175 - replace:
176 filename: "/etc/postgresql/9.3/main/postgresql.conf"
177 from: /#?log_min_duration_statement *=.*/
178 to: "log_min_duration_statement = $db_log_min_duration_statement"
179
180 - replace:
181 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
182 from: /^#local +replication +postgres +peer$/
183 to: "local replication postgres peer"
184
185 # allow all to connect in with md5 auth
186 - replace:
187 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
188 from: /^host.*all.*all.*127.*$/
189 to: "host all all 0.0.0.0/0 md5"
190
191 - exec:
192 background: true
193 # use fast shutdown for pg
194 stop_signal: INT
195 cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/9.3/bin/postmaster -D /etc/postgresql/9.3/main
196
197 # give db a few secs to start up
198 - exec: "sleep 5"
199
200 - exec: su postgres -c 'createdb $db_name' || true
201 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
202 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
203 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
204 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
205 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
206 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
207 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
208 - exec:
209 stdin: |
210 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
211 cmd: sudo -u postgres psql $db_name
212 raise_on_fail: false
213
214 - file:
215 path: /var/lib/postgresql/take-database-backup
216 chown: postgres:postgres
217 chmod: "+x"
218 contents: |
219 #!/bin/bash
220 ID=db-$(date +%F_%T)
221 FILENAME=/shared/postgres_backup/$ID.tar.gz
222 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
223 echo $FILENAME
224
225 - file:
226 path: /var/spool/cron/crontabs/postgres
227 contents: |
228 # m h dom mon dow command
229 #MAILTO=?
230 #0 */4 * * * /var/lib/postgresql/take-database-backup
231
232 - exec:
233 hook: postgres
234 cmd: "echo postgres installed!"