FEATURE: change login directory to discourse home when enter a container
[discourse_docker.git] / templates / postgres.template.yml
index 0d53741074c852beea53ceb53481f5d0dda18f68..e48de016b97eae5a51ec0830f726b0ee596a1c5e 100644 (file)
@@ -1,23 +1,18 @@
-hooks:
-  after_cron:
-    - file:
-       path: /var/lib/postgresql/take-database-backup
-       chown: postgres:postgres
-       chmod: "+x"
-       contents: |
-          #!/bin/bash
-          ID=db-$(date +%F_%T)
-          FILENAME=/shared/postgres_backup/$ID.tar.gz
-          pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
-          echo $FILENAME
-
-    - file:
-       path: /var/spool/cron/crontabs/postgres
-       contents: |
-          # m h  dom mon dow   command
-          #MAILTO=?
-          0 */4 * * * /var/lib/postgresql/take-database-backup
+params:
+  db_synchronous_commit: "off"
+  db_shared_buffers: "256MB"
+  db_work_mem: "10MB"
+  db_default_text_search_config: "pg_catalog.english"
+  db_name: discourse
+  db_user: discourse
+  db_wal_level: minimal
+  db_max_wal_senders: 0
+  db_checkpoint_segments: 6
+  db_logging_collector: off
+  db_log_min_duration_statement: 100
+
 
+hooks:
   before_code:
     - replace:
        filename: /etc/service/unicorn/run
@@ -25,8 +20,6 @@ hooks:
        to: sv start postgres || exit 1
 
 run:
-  # temporary
-  - exec: apt-get install -y socat
   - exec: mkdir -p /shared/postgres_run
   - exec: chown postgres:postgres /shared/postgres_run
   - exec: chmod 775 /shared/postgres_run
@@ -35,13 +28,24 @@ run:
   - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
   - exec: rm -fr /shared/postgres_run/.s*
   - exec: rm -fr /shared/postgres_run/*.pid
+  - exec: mkdir -p /shared/postgres_run/9.3-main.pg_stat_tmp
+  - exec: chown postgres:postgres /shared/postgres_run/9.3-main.pg_stat_tmp
   - file:
      path: /etc/service/postgres/run
      chmod: "+x"
      contents: |
         #!/bin/sh
         exec 2>&1
-        exec sudo -u postgres /usr/lib/postgresql/9.3/bin/postmaster -D /etc/postgresql/9.3/main
+        echo -1000 >/proc/self/oom_score_adj
+        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
+
+  - file:
+     path: /etc/runit/3.d/99-postgres
+     chmod: "+x"
+     contents: |
+       #!/bin/bash
+       sv stop postgres
+
   - file:
      path: /root/upgrade_postgres
      chmod: "+x"
@@ -78,7 +82,7 @@ run:
            echo
            echo Then destroy your container "./launcher destroy standalone"
            echo Undo the base_image and postgres.9.2 template from your container config
-           echo Run: "sudo mv /var/docker/shared/postgres_data /var/shared/postgres_data_old"
+           echo Run: "sudo mv /var/discourse/shared/postgres_data /var/shared/postgres_data_old"
            echo Run: "./launcher bootstrap"
            echo
            echo "Login to the blank site, and import your backup (remember to set the allow_restore site setting)"
@@ -107,6 +111,8 @@ run:
         - chown -R postgres:postgres /var/run/postgresql
 
   - exec: /root/upgrade_postgres
+  - exec: rm /root/upgrade_postgres
+
   - replace:
       filename: "/etc/postgresql/9.3/main/postgresql.conf"
       from: "data_directory = '/var/lib/postgresql/9.3/main'"
@@ -118,6 +124,30 @@ run:
       from: /#?listen_addresses *=.*/
       to: "listen_addresses = '*'"
 
+  # sync commit off is faster and less spiky, also marginally less safe
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?synchronous_commit *=.*/
+      to: "synchronous_commit = $db_synchronous_commit"
+
+  # default is 128MB which is way too small
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?shared_buffers *=.*/
+      to: "shared_buffers = $db_shared_buffers"
+
+  # default is 1MB which is too small
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?work_mem *=.*/
+      to: "work_mem = $db_work_mem"
+
+  # allow for other
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?default_text_search_config *=.*/
+      to: "default_text_search_config = '$db_default_text_search_config'"
+
   # Necessary to enable backups
   - exec:
       cmd:
@@ -126,12 +156,27 @@ run:
   - replace:
       filename: "/etc/postgresql/9.3/main/postgresql.conf"
       from: /#?max_wal_senders *=.*/
-      to: "max_wal_senders = 4"
+      to: "max_wal_senders = $db_max_wal_senders"
 
   - replace:
       filename: "/etc/postgresql/9.3/main/postgresql.conf"
       from: /#?wal_level *=.*/
-      to: "wal_level = hot_standby"
+      to: "wal_level = $db_wal_level"
+
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?checkpoint_segments *=.*/
+      to: "checkpoint_segments = $db_checkpoint_segments"
+
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?logging_collector *=.*/
+      to: "logging_collector = $db_logging_collector"
+
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?log_min_duration_statement *=.*/
+      to: "log_min_duration_statement = $db_log_min_duration_statement"
 
   - replace:
       filename: "/etc/postgresql/9.3/main/pg_hba.conf"
@@ -146,28 +191,44 @@ run:
 
   - exec:
       background: true
-      cmd: "sudo -u postgres /usr/lib/postgresql/9.3/bin/postmaster -D /etc/postgresql/9.3/main"
+      # use fast shutdown for pg
+      stop_signal: INT
+      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
 
   # give db a few secs to start up
   - exec: "sleep 5"
 
-  - exec: sudo -u postgres createdb discourse || exit 0
+  - exec: su postgres -c 'createdb $db_name' || true
+  - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
+  - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
+  - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
+  - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
+  - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
+  - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
+  - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
   - exec:
       stdin: |
-        create user discourse;
-      cmd: sudo -u postgres psql discourse
+        update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
+      cmd: sudo -u postgres psql $db_name
       raise_on_fail: false
 
-  - exec:
-      stdin: |
-        grant all privileges on database discourse to discourse;
-      cmd: sudo -u postgres psql discourse
-      raise_on_fail: false
+  - file:
+     path: /var/lib/postgresql/take-database-backup
+     chown: postgres:postgres
+     chmod: "+x"
+     contents: |
+        #!/bin/bash
+        ID=db-$(date +%F_%T)
+        FILENAME=/shared/postgres_backup/$ID.tar.gz
+        pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
+        echo $FILENAME
 
-  - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "alter schema public owner to discourse;"'
-  - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists hstore;"'
-  - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists pg_trgm;"'
-  - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists fuzzystrmatch;"'
+  - file:
+     path: /var/spool/cron/crontabs/postgres
+     contents: |
+        # m h  dom mon dow   command
+        #MAILTO=?
+        #0 */4 * * * /var/lib/postgresql/take-database-backup
 
   - exec:
       hook: postgres