From da1fb55eb9e17e47013f3d66b0c1744ebe62ee81 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 2 Nov 2015 13:33:59 +1100 Subject: [PATCH] docker dev discourse image --- image/discourse_dev/Dockerfile | 50 +++++-- image/discourse_dev/build | 6 + image/discourse_dev/ensure-database | 8 ++ image/discourse_dev/postgres.template.yml | 152 ++++++++++++++++++++++ image/discourse_dev/redis.template.yml | 54 ++++++++ 5 files changed, 256 insertions(+), 14 deletions(-) create mode 100755 image/discourse_dev/build create mode 100755 image/discourse_dev/ensure-database create mode 100644 image/discourse_dev/postgres.template.yml create mode 100644 image/discourse_dev/redis.template.yml diff --git a/image/discourse_dev/Dockerfile b/image/discourse_dev/Dockerfile index 38a5623..6659669 100644 --- a/image/discourse_dev/Dockerfile +++ b/image/discourse_dev/Dockerfile @@ -1,17 +1,39 @@ -FROM samsaffron/discourse_fast_switch +FROM samsaffron/discourse_fast_switch:1.0.13 MAINTAINER Sam Saffron "https://twitter.com/samsaffron" -RUN apt-get install -y vim &&\ - wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2 -RUN tar -xjf phantomjs-1.9.7-linux-x86_64.tar.bz2 -RUN rm phantomjs-1.9.7-linux-x86_64.tar.bz2 -RUN cp phantomjs-1.9.7-linux-x86_64/bin/phantomjs /bin/phantomjs -RUN rm -fr phantomjs-1.9.7-linux-x86_64 -RUN cd opt && git clone https://github.com/SamSaffron/vim-setup.git -RUN cd opt/vim-setup && git submodule update --init -RUN ln -s /opt/vim-setup/vimrc /etc/vim/vimrc.local -RUN ln -s /opt/vim-setup/vimfiles/bundle /etc/vim/bundle -RUN apt-get -y install ctags -RUN apt-get -y install silversearcher-ag -RUN cd /var/www/discourse && rm -fr .bundle && sudo -u discourse bundle install --standalone +RUN use_22 +RUN rm -fr /usr/local/include/ruby-2.0.0 + +RUN chown -R discourse:discourse /usr/ruby_22 + +RUN rm /var/www/discourse/.bundle/config + +RUN cd /var/www/discourse && git pull && sudo -u discourse bundle install +RUN use_22 + +# CLEANUP +RUN rm -fr /var/www/discourse/vendor/bundle/ruby +RUN rm -fr /usr/ruby_20 + + +# get redis going +ADD redis.template.yml /pups/redis.yml +RUN /pups/bin/pups /pups/redis.yml + +RUN locale-gen en_US.UTF-8 +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 + +# get postgres going +ADD postgres.template.yml /pups/postgres.yml +RUN LANG=en_US.UTF-8 /pups/bin/pups /pups/postgres.yml + +# move pg out of the way +RUN mv /shared/postgres_data /shared/postgres_data_orig + +# We got gems ... we need no source in the image ... +RUN rm -fr /var/www/discourse + +ADD ensure-database /etc/runit/1.d/ensure-database diff --git a/image/discourse_dev/build b/image/discourse_dev/build new file mode 100755 index 0000000..85f20af --- /dev/null +++ b/image/discourse_dev/build @@ -0,0 +1,6 @@ +#!/bin/bash + +docker build -t discourse/dev:1.0.13 . +docker save discourse/dev:1.0.13 | ../docker-squash -t discourse/dev:1.0.13 -verbose -from root | docker load +docker tag discourse/dev:1.0.13 discourse/dev:latest + diff --git a/image/discourse_dev/ensure-database b/image/discourse_dev/ensure-database new file mode 100755 index 0000000..f5f2532 --- /dev/null +++ b/image/discourse_dev/ensure-database @@ -0,0 +1,8 @@ +#!/bin/bash + +if [ ! -f /shared/postgres_data/PG_VERSION ]; then + mkdir -p /shared/postgres_data + cp -R /shared/postgres_data_orig/* /shared/postgres_data + chown -R postgres:postgres /shared/postgres_data + chmod 700 /shared/postgres_data +fi diff --git a/image/discourse_dev/postgres.template.yml b/image/discourse_dev/postgres.template.yml new file mode 100644 index 0000000..1c13460 --- /dev/null +++ b/image/discourse_dev/postgres.template.yml @@ -0,0 +1,152 @@ +params: + db_synchronous_commit: "off" + db_shared_buffers: "512MB" + db_work_mem: "20MB" + db_default_text_search_config: "pg_catalog.english" + 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 + +env: + LANG: en_US.UTF-8 + +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: 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 + 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 LANG=en_US.UTF-8 /usr/lib/postgresql/9.3/bin/initdb --locale=C -E UTF-8 -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 = '*'" + + # 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: + - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup + + - replace: + filename: "/etc/postgresql/9.3/main/postgresql.conf" + from: /#?max_wal_senders *=.*/ + to: "max_wal_senders = $db_max_wal_senders" + + - replace: + filename: "/etc/postgresql/9.3/main/postgresql.conf" + from: /#?wal_level *=.*/ + 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" + 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: sudo -E -u postgres LANG=en_US.UTF-8 createdb discourse_development + - exec: sudo -E -u postgres LANG=en_US.UTF-8 createdb discourse_test + - exec: su postgres -c 'psql -c "create user $db_user with superuser"' + + - exec: + background: true + cmd: exec chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf + + - exec: cd /var/www/discourse && sudo -u discourse rake db:migrate + - exec: cd /var/www/discourse && sudo -u discourse RAILS_ENV=test rake db:migrate + - exec: + hook: postgres + cmd: "echo postgres installed!" diff --git a/image/discourse_dev/redis.template.yml b/image/discourse_dev/redis.template.yml new file mode 100644 index 0000000..249018a --- /dev/null +++ b/image/discourse_dev/redis.template.yml @@ -0,0 +1,54 @@ +run: + - file: + path: /etc/service/redis/run + chmod: "+x" + contents: | + #!/bin/sh + exec 2>&1 + exec chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf + + - file: + path: /etc/runit/3.d/10-redis + chmod: "+x" + contents: | + #!/bin/bash + sv stop redis + + - replace: + filename: "/etc/redis/redis.conf" + from: "daemonize yes" + to: "" + - replace: + filename: "/etc/redis/redis.conf" + from: /^pidfile.*$/ + to: "" + + - exec: + cmd: + - install -d -m 0755 -o redis -g redis /shared/redis_data + + - replace: + filename: "/etc/redis/redis.conf" + from: /^logfile.*$/ + to: "logfile \"\"" + + - replace: + filename: "/etc/redis/redis.conf" + from: /^dir .*$/ + to: "dir /shared/redis_data" + + - exec: + cmd: echo redis installed + hook: redis + +# we can not migrate without redis, launch it if needed +hooks: + before_code: + - exec: + background: true + cmd: exec chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf + after_code: + - replace: + filename: /etc/service/unicorn/run + from: "# redis" + to: sv start redis || exit 1 -- 2.25.1