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_checkpoint_segments: 6
  db_logging_collector: off
  db_log_min_duration_statement: 100

hooks:
  before_code:
    - replace:
       filename: /etc/service/unicorn/run
       from: "# postgres"
       to: |
         if [ -f /root/install_postgres ]; then
           /root/install_postgres
           rm /root/install_postgres
         fi
         sv start postgres || exit 1

run:
  - exec: DEBIAN_FRONTEND=noninteractive apt-get purge -y postgresql-15 postgresql-client-15 postgresql-contrib-15 postgresql-15-pgvector
  - exec: apt-get update && apt-get install -y postgresql-13 postgresql-client-13 postgresql-contrib-13 postgresql-13-pgvector
  - file:
     path: /etc/service/postgres/run
     chmod: "+x"
     contents: |
        #!/bin/sh
        exec 2>&1
        HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/13/bin/postmaster -D /etc/postgresql/13/main

  - file:
     path: /etc/service/postgres/log/run
     chmod: "+x"
     contents: |
       #!/bin/sh
       mkdir -p /var/log/postgres
       exec svlogd /var/log/postgres

  - file:
     path: /etc/runit/3.d/99-postgres
     chmod: "+x"
     contents: |
       #!/bin/bash
       sv stop postgres

  - file:
      path: /root/install_postgres
      chmod: "+x"
      contents: |
        #!/bin/bash -x
        sed -i "s/^# $LANG/$LANG/" /etc/locale.gen
        locale-gen && update-locale
        mkdir -p /shared/postgres_run
        chown postgres:postgres /shared/postgres_run
        chmod 775 /shared/postgres_run
        rm -fr /var/run/postgresql
        ln -s /shared/postgres_run /var/run/postgresql
        if [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then
          socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
        fi
        rm -fr /shared/postgres_run/.s*
        rm -fr /shared/postgres_run/*.pid
        mkdir -p /shared/postgres_run/13-main.pg_stat_tmp
        chown postgres:postgres /shared/postgres_run/13-main.pg_stat_tmp
        chown -R root /var/lib/postgresql/13/main
        [ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/13/bin/initdb -D /shared/postgres_data || exit 0
        chown -R postgres:postgres /shared/postgres_data
        chown -R postgres:postgres /var/run/postgresql
        # Necessary to enable backups
        install -d -m 0755 -o postgres -g postgres /shared/postgres_backup

  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: "data_directory = '/var/lib/postgresql/13/main'"
      to: "data_directory = '/shared/postgres_data'"

  # listen on all interfaces
  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?listen_addresses *=.*/
      to: "listen_addresses = '*'"

  # sync commit off is faster and less spiky, also marginally less safe
  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?synchronous_commit *=.*/
      to: "synchronous_commit = $db_synchronous_commit"

  # default is 128MB which is way too small
  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?shared_buffers *=.*/
      to: "shared_buffers = $db_shared_buffers"

  # default is 1MB which is too small
  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?work_mem *=.*/
      to: "work_mem = $db_work_mem"

  # allow for other
  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?default_text_search_config *=.*/
      to: "default_text_search_config = '$db_default_text_search_config'"

  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?checkpoint_segments *=.*/
      to: "checkpoint_segments = $db_checkpoint_segments"

  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?logging_collector *=.*/
      to: "logging_collector = $db_logging_collector"

  - replace:
      filename: "/etc/postgresql/13/main/postgresql.conf"
      from: /#?log_min_duration_statement *=.*/
      to: "log_min_duration_statement = $db_log_min_duration_statement"

  - replace:
      filename: "/etc/postgresql/13/main/pg_hba.conf"
      from: /^#local +replication +postgres +peer$/
      to: "local replication postgres  peer"

  # allow all to connect in with md5 auth
  - replace:
      filename: "/etc/postgresql/13/main/pg_hba.conf"
      from: /^host.*all.*all.*127.*$/
      to: "host all all 0.0.0.0/0 md5"

  # allow all to connect in with md5 auth (IPv6)
  - replace:
      filename: "/etc/postgresql/13/main/pg_hba.conf"
      from: /^host.*all.*all.*::1\/128.*$/
      to: "host all all ::/0 md5"

  - exec:
      tag: db
      cmd: |
        if [ -f /root/install_postgres ]; then
          /root/install_postgres && rm -f /root/install_postgres
        elif [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then
          socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
        fi

  - exec:
      tag: db
      background: true
      # 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/13/bin/postmaster -D /etc/postgresql/13/main

  - file:
      path: /usr/local/bin/create_db
      chmod: +x
      contents: |
        #!/bin/bash
        su postgres -c 'createdb $db_name' || true
        su postgres -c 'psql $db_name -c "create user $db_user;"' || true
        su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
        su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
        su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
        su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
        su postgres -c 'psql template1 -c "create extension if not exists vector;"'
        su postgres -c 'psql template1 -c "alter extension vector update;"' || true
        su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
        su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
        su postgres -c 'psql $db_name -c "create extension if not exists vector;"'
        su postgres -c 'psql $db_name -c "alter extension vector update;"' || true
        sudo -u postgres psql $db_name <<< "update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');" || true

  - 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=- --wal-method=fetch --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

  - exec:
      tag: db
      hook: postgres
      cmd:
        # give db a few secs to start up
        - "sleep 5"
        - /usr/local/bin/create_db
        - "echo postgres installed!"
