e35f5239250fefe39cb2639973be780e5022ecab
[discourse_docker.git] / templates / postgres.template.yml
1 hooks:
2 after_cron:
3 - file:
4 path: /var/lib/postgresql/take-database-backup
5 chown: postgres:postgres
6 chmod: "+x"
7 contents: |
8 #!/bin/bash
9 ID=db-$(date +%F_%T)
10 FILENAME=/shared/postgres_backup/$ID.tar.gz
11 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
12 echo $FILENAME
13
14 - file:
15 path: /var/spool/cron/crontabs/postgres
16 contents: |
17 # m h dom mon dow command
18 #MAILTO=?
19 0 */4 * * * /var/lib/postgresql/take-database-backup
20
21 before_code:
22 - replace:
23 filename: /etc/service/sidekiq/run
24 from: "# postgres"
25 to: sv start postgres || exit 1
26 - replace:
27 filename: /etc/service/unicorn/run
28 from: "# postgres"
29 to: sv start postgres || exit 1
30
31 run:
32 # temporary
33 - exec: apt-get install -y socat
34 - exec: mkdir -p /shared/postgres_run
35 - exec: chown postgres:postgres /shared/postgres_run
36 - exec: chmod 775 /shared/postgres_run
37 - exec: rm -fr /var/run/postgresql
38 - exec: ln -s /shared/postgres_run /var/run/postgresql
39 - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
40 - file:
41 path: /etc/service/postgres/run
42 chmod: "+x"
43 contents: |
44 #!/bin/sh
45 exec 2>&1
46 exec sudo -u postgres /usr/lib/postgresql/9.3/bin/postmaster -D /etc/postgresql/9.3/main
47 - file:
48 path: /root/upgrade_postgres
49 chmod: "+x"
50 contents: |
51 #!/bin/bash
52 if [ "9.2" = `cat /shared/postgres_data/PG_VERSION` ]; then
53 echo Upgrading PostgreSQL to from version 9.2 to 9.3
54 rm -fr /shared/postgres_data_new
55 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
56 apt-get install -y postgresql-9.2
57 cd ~postgres
58 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 || exit 1
59
60 mv /shared/postgres_data /shared/postgres_data_old
61 mv /shared/postgres_data_new /shared/postgres_data
62
63 echo -------------------------------------------------------------------------------------
64 echo UPGRADE OF POSTGRES COMPLETE
65 echo
66 echo Old 9.2 database is stored at /shared/postgres_data_old
67 echo
68 echo Please run "./launcher bootstrap" again
69 echo -------------------------------------------------------------------------------------
70 exit 1
71 fi
72 - exec: /root/upgrade_postgres
73 - exec:
74 cmd:
75 - chown -R root /var/lib/postgresql/9.3/main
76 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -u postgres /usr/lib/postgresql/9.3/bin/initdb -D /shared/postgres_data || exit 0"
77 - chown -R postgres:postgres /shared/postgres_data
78 - chown -R postgres:postgres /var/run/postgresql
79
80 - replace:
81 filename: "/etc/postgresql/9.3/main/postgresql.conf"
82 from: "data_directory = '/var/lib/postgresql/9.3/main'"
83 to: "data_directory = '/shared/postgres_data'"
84
85 # listen on all interfaces
86 - replace:
87 filename: "/etc/postgresql/9.3/main/postgresql.conf"
88 from: /#?listen_addresses *=.*/
89 to: "listen_addresses = '*'"
90
91 # Necessary to enable backups
92 - exec:
93 cmd:
94 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
95
96 - replace:
97 filename: "/etc/postgresql/9.3/main/postgresql.conf"
98 from: /#?max_wal_senders *=.*/
99 to: "max_wal_senders = 4"
100
101 - replace:
102 filename: "/etc/postgresql/9.3/main/postgresql.conf"
103 from: /#?wal_level *=.*/
104 to: "wal_level = hot_standby"
105
106 - replace:
107 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
108 from: /^#local +replication +postgres +peer$/
109 to: "local replication postgres peer"
110
111 # allow all to connect in with md5 auth
112 - replace:
113 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
114 from: /^host.*all.*all.*127.*$/
115 to: "host all all 0.0.0.0/0 md5"
116
117 - exec:
118 background: true
119 cmd: "sudo -u postgres /usr/lib/postgresql/9.3/bin/postmaster -D /etc/postgresql/9.3/main"
120
121 # give db a few secs to start up
122 - exec: "sleep 5"
123
124 - exec: sudo -u postgres createdb discourse || exit 0
125 - exec:
126 stdin: |
127 create user discourse;
128 cmd: sudo -u postgres psql discourse
129 raise_on_fail: false
130
131 - exec:
132 stdin: |
133 grant all privileges on database discourse to discourse;
134 cmd: sudo -u postgres psql discourse
135 raise_on_fail: false
136
137 - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "alter schema public owner to discourse;"'
138 - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists hstore;"'
139 - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists pg_trgm;"'
140
141 - exec:
142 hook: postgres
143 cmd: "echo postgres installed!"