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