Merge pull request #105 from SAMdroid-apps/docker-options
[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
7 hooks:
8 before_code:
9 - replace:
10 filename: /etc/service/unicorn/run
11 from: "# postgres"
12 to: sv start postgres || exit 1
13
14 run:
15 - exec: mkdir -p /shared/postgres_run
16 - exec: chown postgres:postgres /shared/postgres_run
17 - exec: chmod 775 /shared/postgres_run
18 - exec: rm -fr /var/run/postgresql
19 - exec: ln -s /shared/postgres_run /var/run/postgresql
20 - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
21 - exec: rm -fr /shared/postgres_run/.s*
22 - exec: rm -fr /shared/postgres_run/*.pid
23 - file:
24 path: /etc/service/postgres/run
25 chmod: "+x"
26 contents: |
27 #!/bin/sh
28 exec 2>&1
29 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
30
31 - file:
32 path: /etc/runit/3.d/99-postgres
33 chmod: "+x"
34 contents: |
35 #!/bin/bash
36 sv stop postgres
37
38 - file:
39 path: /root/upgrade_postgres
40 chmod: "+x"
41 contents: |
42 #!/bin/bash
43 if [ "9.2" = `cat /shared/postgres_data/PG_VERSION` ]; then
44 echo Upgrading PostgreSQL to from version 9.2 to 9.3
45 rm -fr /shared/postgres_data_new
46 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
47 apt-get update
48 apt-get install -y postgresql-9.2
49 /etc/init.d/postgresql stop
50 rm -fr /shared/postgres_data/postmaster.pid
51 cd ~postgres
52 cp /etc/postgresql/9.2/main/*.conf /shared/postgres_data
53 echo >> /shared/postgres_data/postgresql.conf
54 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
55 SUCCESS=true
56 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
57
58 if [[ "$SUCCESS" == 'false' ]]; then
59 echo -------------------------------------------------------------------------------------
60 echo UPGRADE OF POSTGRES FAILED
61 echo
62 echo You are going to need to export your data and import into a clean instance:
63 echo
64 echo Add the line: 'base_image: "samsaffron/discourse:0.1.1"'
65 echo Change: "templates/postgres.template.yml TO templates/postgres.9.2.template.yml"
66 echo
67 echo Run "./launcher bootstrap" again
68 echo
69 echo When your instance is running:
70 echo - Go to the admin UI, enable readonly and backup your site
71 echo
72 echo Then destroy your container "./launcher destroy standalone"
73 echo Undo the base_image and postgres.9.2 template from your container config
74 echo Run: "sudo mv /var/discourse/shared/postgres_data /var/shared/postgres_data_old"
75 echo Run: "./launcher bootstrap"
76 echo
77 echo "Login to the blank site, and import your backup (remember to set the allow_restore site setting)"
78 echo Restart your container: "./launcher restart"
79 echo -------------------------------------------------------------------------------------
80 exit 1
81 fi
82
83 mv /shared/postgres_data /shared/postgres_data_old
84 mv /shared/postgres_data_new /shared/postgres_data
85
86 echo -------------------------------------------------------------------------------------
87 echo UPGRADE OF POSTGRES COMPLETE
88 echo
89 echo Old 9.2 database is stored at /shared/postgres_data_old
90 echo
91 echo Please run "./launcher bootstrap" again
92 echo -------------------------------------------------------------------------------------
93 exit 1
94 fi
95 - exec:
96 cmd:
97 - chown -R root /var/lib/postgresql/9.3/main
98 - "[ ! -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"
99 - chown -R postgres:postgres /shared/postgres_data
100 - chown -R postgres:postgres /var/run/postgresql
101
102 - exec: /root/upgrade_postgres
103 - exec: rm /root/upgrade_postgres
104
105 - replace:
106 filename: "/etc/postgresql/9.3/main/postgresql.conf"
107 from: "data_directory = '/var/lib/postgresql/9.3/main'"
108 to: "data_directory = '/shared/postgres_data'"
109
110 # listen on all interfaces
111 - replace:
112 filename: "/etc/postgresql/9.3/main/postgresql.conf"
113 from: /#?listen_addresses *=.*/
114 to: "listen_addresses = '*'"
115
116 # sync commit off is faster and less spiky, also marginally less safe
117 - replace:
118 filename: "/etc/postgresql/9.3/main/postgresql.conf"
119 from: /#?synchronous_commit *=.*/
120 to: "synchronous_commit = $db_synchronous_commit"
121
122 # default is 128MB which is way too small
123 - replace:
124 filename: "/etc/postgresql/9.3/main/postgresql.conf"
125 from: /#?shared_buffers *=.*/
126 to: "shared_buffers = $db_shared_buffers"
127
128 # default is 1MB which is too small
129 - replace:
130 filename: "/etc/postgresql/9.3/main/postgresql.conf"
131 from: /#?work_mem *=.*/
132 to: "work_mem = $db_work_mem"
133
134 # allow for other
135 - replace:
136 filename: "/etc/postgresql/9.3/main/postgresql.conf"
137 from: /#?default_text_search_config *=.*/
138 to: "default_text_search_config = '$db_default_text_search_config'"
139
140 # Necessary to enable backups
141 - exec:
142 cmd:
143 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
144
145 - replace:
146 filename: "/etc/postgresql/9.3/main/postgresql.conf"
147 from: /#?max_wal_senders *=.*/
148 to: "max_wal_senders = 4"
149
150 - replace:
151 filename: "/etc/postgresql/9.3/main/postgresql.conf"
152 from: /#?wal_level *=.*/
153 to: "wal_level = hot_standby"
154
155 - replace:
156 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
157 from: /^#local +replication +postgres +peer$/
158 to: "local replication postgres peer"
159
160 # allow all to connect in with md5 auth
161 - replace:
162 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
163 from: /^host.*all.*all.*127.*$/
164 to: "host all all 0.0.0.0/0 md5"
165
166 - exec:
167 background: true
168 # use fast shutdown for pg
169 stop_signal: INT
170 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
171
172 # give db a few secs to start up
173 - exec: "sleep 5"
174
175 - exec: su postgres -c 'createdb discourse' || true
176 - exec: su postgres -c 'psql discourse -c "create user discourse;"' || true
177 - exec: su postgres -c 'psql discourse -c "grant all privileges on database discourse to discourse;"' || true
178 - exec: su postgres -c 'psql discourse -c "alter schema public owner to discourse;"'
179 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
180 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
181 - exec: su postgres -c 'psql discourse -c "create extension if not exists hstore;"'
182 - exec: su postgres -c 'psql discourse -c "create extension if not exists pg_trgm;"'
183
184 - file:
185 path: /var/lib/postgresql/take-database-backup
186 chown: postgres:postgres
187 chmod: "+x"
188 contents: |
189 #!/bin/bash
190 ID=db-$(date +%F_%T)
191 FILENAME=/shared/postgres_backup/$ID.tar.gz
192 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
193 echo $FILENAME
194
195 - file:
196 path: /var/spool/cron/crontabs/postgres
197 contents: |
198 # m h dom mon dow command
199 #MAILTO=?
200 #0 */4 * * * /var/lib/postgresql/take-database-backup
201
202 - exec:
203 hook: postgres
204 cmd: "echo postgres installed!"