Update PostgreSQL to 9.5 in base image.
authorGuo Xiang Tan <tgx_world@hotmail.com>
Mon, 16 May 2016 04:13:54 +0000 (12:13 +0800)
committerGuo Xiang Tan <tgx_world@hotmail.com>
Mon, 16 May 2016 04:19:18 +0000 (12:19 +0800)
image/base/Dockerfile
image/base/install-imagemagick
image/base/install-pngcrush
image/discourse/Dockerfile
templates/new.postgres.template.yml [new file with mode: 0644]
templates/postgres.9.3.template.yml [new file with mode: 0644]

index 076f19fde1bfec2d6ad3d378c3b231ec62c507af..e3c463af0e8100e9e8f269d39333c5d16a4cb317 100644 (file)
@@ -1,11 +1,11 @@
-# Official repo only has a ppa for postgresql 9.3 at the moment (14/3/2014)
-# When new LTS ships we can upgrade
-
 # NAME:     discourse_base
-# VERSION:  1.0.17
+# VERSION:  1.0.18
 
 FROM ubuntu:14.04
 
+ENV PG_MAJOR 9.5
+ENV PG_VERSION 9.5.3-1.pgdg14.04+1
+
 MAINTAINER Sam Saffron "https://twitter.com/samsaffron"
 
 RUN echo "1.0.13" > /VERSION
@@ -30,8 +30,8 @@ RUN echo "debconf debconf/frontend select Teletype" | debconf-set-selections &&\
                        libxslt-dev libcurl4-openssl-dev \
                        libssl-dev libyaml-dev libtool \
                        libxml2-dev gawk parallel \
-                       postgresql-9.3 postgresql-client-9.3 \
-                       postgresql-contrib-9.3 libpq-dev libreadline-dev \
+                       postgresql-${PG_MAJOR}=${PG_VERSION} postgresql-client-${PG_MAJOR}=${PG_VERSION} \
+                       postgresql-contrib-${PG_MAJOR}=${PG_VERSION} libpq-dev libreadline-dev \
                        nginx language-pack-en sudo cron anacron \
                        psmisc rsyslog vim whois &&\
     sed -i -e 's/start -q anacron/anacron -s/' /etc/cron.d/anacron &&\
index c4acaad06170ef7d0c40954ae7913aedfe029567..f28c03d27fd7b046bc1c922e8c0f0b6170e3e386 100755 (executable)
@@ -17,7 +17,7 @@ cd $WDIR/libpng
 make all && make install
 
 # Build and install ImageMagick
-wget -O $WDIR/ImageMagick.tar.gz "http://www.imagemagick.org/download/ImageMagick.tar.gz"
+wget -O $WDIR/ImageMagick.tar.gz "http://www.imagemagick.org/download/ImageMagick-6.9.4-1.tar.gz"
 IMDIR=$WDIR/$(tar tzf $WDIR/ImageMagick.tar.gz --wildcards "ImageMagick-*/configure" |cut -d/ -f1)
 tar zxf $WDIR/ImageMagick.tar.gz -C $WDIR
 cd $IMDIR
index b6e469edf9edae30812073407c756a46c82434c3..8c68666d774440e168fe3eb520d1f807558516cf 100755 (executable)
@@ -2,7 +2,7 @@
 set -e
 PNGCRUSH_VERSION=1.7.92
 cd /tmp
-curl -O http://iweb.dl.sourceforge.net/project/pmt/pngcrush/$PNGCRUSH_VERSION/pngcrush-$PNGCRUSH_VERSION.tar.gz
+wget http://iweb.dl.sourceforge.net/project/pmt/pngcrush/$PNGCRUSH_VERSION/pngcrush-$PNGCRUSH_VERSION.tar.gz
 tar zxf pngcrush-$PNGCRUSH_VERSION.tar.gz
 cd pngcrush-$PNGCRUSH_VERSION
 make && cp -f pngcrush /usr/local/bin
index 9dc2e30a91233b975853a7db40048618013631b2..cdcd650413412f9f4fdf3703d5580d374114c1ed 100644 (file)
@@ -1,5 +1,5 @@
-# Current version 1.0.17
-FROM discourse/base:1.0.17
+# Current version 1.0.18
+FROM discourse/base:1.0.18
 
 MAINTAINER Sam Saffron "https://twitter.com/samsaffron"
 
diff --git a/templates/new.postgres.template.yml b/templates/new.postgres.template.yml
new file mode 100644 (file)
index 0000000..5281487
--- /dev/null
@@ -0,0 +1,247 @@
+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
+       from: "# postgres"
+       to: sv start postgres || exit 1
+
+run:
+  - exec: mkdir -p /shared/postgres_run
+  - exec: chown postgres:postgres /shared/postgres_run
+  - exec: chmod 775 /shared/postgres_run
+  - exec: rm -fr /var/run/postgresql
+  - exec: ln -s /shared/postgres_run /var/run/postgresql
+  - 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.5-main.pg_stat_tmp
+  - exec: chown postgres:postgres /shared/postgres_run/9.5-main.pg_stat_tmp
+  - file:
+     path: /etc/service/postgres/run
+     chmod: "+x"
+     contents: |
+        #!/bin/sh
+        exec 2>&1
+        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.5/bin/postmaster -D /etc/postgresql/9.5/main
+
+  - file:
+     path: /etc/runit/3.d/99-postgres
+     chmod: "+x"
+     contents: |
+       #!/bin/bash
+       sv stop postgres
+
+  - file:
+     path: /root/upgrade_postgres
+     chmod: "+x"
+     contents: |
+       #!/bin/bash
+       PG_MAJOR_OLD=`cat /shared/postgres_data/PG_VERSION`
+
+       if [ ! "9.5" = "$PG_MAJOR_OLD" ]; then
+         echo Upgrading PostgreSQL to from version ${PG_MAJOR_OLD} to 9.5
+         rm -fr /shared/postgres_data_new
+         install -d -m 0755 -o postgres -g postgres /shared/postgres_data_new && sudo -u postgres /usr/lib/postgresql/9.5/bin/initdb -D /shared/postgres_data_new || exit 0
+         apt-get update
+         apt-get install -y postgresql-${PG_MAJOR_OLD}
+         /etc/init.d/postgresql stop
+         rm -fr /shared/postgres_data/postmaster.pid
+         cd ~postgres
+         cp /etc/postgresql/${PG_MAJOR_OLD}/main/*.conf /shared/postgres_data
+         echo  >> /shared/postgres_data/postgresql.conf
+         echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
+         SUCCESS=true
+         sudo -u postgres /usr/lib/postgresql/9.5/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/${PG_MAJOR_OLD}/bin -B /usr/lib/postgresql/9.5/bin || SUCCESS=false
+
+         if [[ "$SUCCESS" == 'false' ]]; then
+           echo -------------------------------------------------------------------------------------
+           echo UPGRADE OF POSTGRES FAILED
+           echo
+           echo You are going to need to export your data and import into a clean instance:
+           echo
+
+           if [ "$PG_MAJOR_OLD" = "9.2" ]; then
+             echo Add the line: 'base_image: "samsaffron/discourse:0.1.1"'
+             echo Change: "templates/postgres.template.yml TO templates/postgres.9.2.template.yml"
+             echo
+           fi
+
+           if [ "$PG_MAJOR_OLD" = "9.3" ]; then
+             echo Add the line: 'base_image: "discourse/discourse:1.0.17"'
+             echo Change: "templates/postgres.template.yml TO templates/postgres.9.3.template.yml"
+             echo
+           fi
+
+           echo Run "./launcher bootstrap" again
+           echo
+           echo When your instance is running:
+           echo - Go to the admin UI, enable readonly and backup your site
+           echo
+           echo Then destroy your container "./launcher destroy standalone"
+           echo Undo the base_image and postgres template from your container config
+           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)"
+           echo Restart your container: "./launcher restart"
+           echo -------------------------------------------------------------------------------------
+           exit 1
+         fi
+
+         mv /shared/postgres_data /shared/postgres_data_old
+         mv /shared/postgres_data_new /shared/postgres_data
+
+         echo -------------------------------------------------------------------------------------
+         echo UPGRADE OF POSTGRES COMPLETE
+         echo
+         echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
+         echo
+         echo Please run "./launcher bootstrap" again
+         echo -------------------------------------------------------------------------------------
+         exit 1
+       fi
+  - exec:
+      cmd:
+        - chown -R root /var/lib/postgresql/9.5/main
+        - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/9.5/bin/initdb -D /shared/postgres_data || exit 0"
+        - chown -R postgres:postgres /shared/postgres_data
+        - chown -R postgres:postgres /var/run/postgresql
+
+  - exec: /root/upgrade_postgres
+  - exec: rm /root/upgrade_postgres
+
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: "data_directory = '/var/lib/postgresql/9.5/main'"
+      to: "data_directory = '/shared/postgres_data'"
+
+  # listen on all interfaces
+  - replace:
+      filename: "/etc/postgresql/9.5/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/9.5/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.5/main/postgresql.conf"
+      from: /#?shared_buffers *=.*/
+      to: "shared_buffers = $db_shared_buffers"
+
+  # default is 1MB which is too small
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: /#?work_mem *=.*/
+      to: "work_mem = $db_work_mem"
+
+  # allow for other
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: /#?default_text_search_config *=.*/
+      to: "default_text_search_config = '$db_default_text_search_config'"
+
+  # Necessary to enable backups
+  - exec:
+      cmd:
+        - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
+
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: /#?max_wal_senders *=.*/
+      to: "max_wal_senders = $db_max_wal_senders"
+
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: /#?wal_level *=.*/
+      to: "wal_level = $db_wal_level"
+
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: /#?checkpoint_segments *=.*/
+      to: "checkpoint_segments = $db_checkpoint_segments"
+
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: /#?logging_collector *=.*/
+      to: "logging_collector = $db_logging_collector"
+
+  - replace:
+      filename: "/etc/postgresql/9.5/main/postgresql.conf"
+      from: /#?log_min_duration_statement *=.*/
+      to: "log_min_duration_statement = $db_log_min_duration_statement"
+
+  - replace:
+      filename: "/etc/postgresql/9.5/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/9.5/main/pg_hba.conf"
+      from: /^host.*all.*all.*127.*$/
+      to: "host all all 0.0.0.0/0 md5"
+
+  - exec:
+      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/9.5/bin/postmaster -D /etc/postgresql/9.5/main
+
+  # give db a few secs to start up
+  - exec: "sleep 5"
+
+  - 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: |
+        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
+
+  - 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
+
+  - exec:
+      hook: postgres
+      cmd: "echo postgres installed!"
diff --git a/templates/postgres.9.3.template.yml b/templates/postgres.9.3.template.yml
new file mode 100644 (file)
index 0000000..cd8e280
--- /dev/null
@@ -0,0 +1,75 @@
+hooks:
+  before_code:
+    - replace:
+       filename: /etc/service/unicorn/run
+       from: "# postgres"
+       to: sv start postgres || exit 1
+
+run:
+  - exec: mkdir -p /shared/postgres_run
+  - exec: chown postgres:postgres /shared/postgres_run
+  - exec: chmod 775 /shared/postgres_run
+  - exec: rm -fr /var/run/postgresql
+  - exec: ln -s /shared/postgres_run /var/run/postgresql
+  - 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
+        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
+
+  - exec:
+      cmd:
+        - chown -R root /var/lib/postgresql/9.3/main
+        - "[ ! -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"
+        - chown -R postgres:postgres /shared/postgres_data
+        - chown -R postgres:postgres /var/run/postgresql
+
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: "data_directory = '/var/lib/postgresql/9.3/main'"
+      to: "data_directory = '/shared/postgres_data'"
+
+  # listen on all interfaces
+  - replace:
+      filename: "/etc/postgresql/9.3/main/postgresql.conf"
+      from: /#?listen_addresses *=.*/
+      to: "listen_addresses = '*'"
+
+  - replace:
+      filename: "/etc/postgresql/9.3/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/9.3/main/pg_hba.conf"
+      from: /^host.*all.*all.*127.*$/
+      to: "host all all 0.0.0.0/0 md5"
+
+  - exec:
+      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/9.3/bin/postmaster -D /etc/postgresql/9.3/main
+
+  # give db a few secs to start up
+  - exec: "sleep 5"
+
+  - exec:
+      hook: postgres
+      cmd: "echo postgres installed!"