From 074f2b6aec69235097c214fb3a2a43e285a7eb22 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 9 Oct 2014 19:10:31 +1100 Subject: [PATCH] FIX: properly shut down services on "docker stop" This protects against corrupt dbs and speeds up boot / bootstrap --- image/base/Dockerfile | 9 +++++++-- image/base/boot | 10 ++++++++++ image/base/runit-2 | 2 +- image/base/runit-3 | 3 +++ image/discourse/Dockerfile | 6 +++--- launcher | 4 ++-- templates/postgres.template.yml | 8 ++++++++ templates/redis.template.yml | 7 +++++++ templates/sshd.template.yml | 7 +++++++ templates/web.template.yml | 14 ++++++++++++++ 10 files changed, 62 insertions(+), 8 deletions(-) create mode 100755 image/base/boot create mode 100755 image/base/runit-3 diff --git a/image/base/Dockerfile b/image/base/Dockerfile index a67d997..b22528c 100644 --- a/image/base/Dockerfile +++ b/image/base/Dockerfile @@ -2,7 +2,7 @@ # When new LTS ships we can upgrade # NAME: discourse_base -# VERSION: 1.0.4 +# VERSION: 1.0.5 FROM ubuntu:14.04 @@ -67,12 +67,17 @@ RUN echo 'gem: --no-document' >> /usr/local/etc/gemrc &&\ cd / && git clone https://github.com/SamSaffron/pups.git # clean up for docker squash -RUN rm -fr /usr/share/man && rm -fr /usr/share/doc +RUN rm -fr /usr/share/man && rm -fr /usr/share/doc && mkdir -p /etc/runit/3.d ADD runit-1 /etc/runit/1 ADD runit-1.d-cleanup-pids /etc/runit/1.d/cleanup-pids ADD runit-1.d-anacron /etc/runit/1.d/anacron ADD runit-2 /etc/runit/2 +ADD runit-3 /etc/runit/3 +ADD boot /sbin/boot ADD cron /etc/service/cron/run ADD rsyslog /etc/service/rsyslog/run + +# this is temporary, remove in 2015 +RUN cp /sbin/boot /sbin/runit diff --git a/image/base/boot b/image/base/boot new file mode 100755 index 0000000..38b5294 --- /dev/null +++ b/image/base/boot @@ -0,0 +1,10 @@ +#!/bin/bash + +# we use this to boot up cause runit will not handle TERM and will not exit when done + +/etc/runit/1 +/etc/runit/2& +RUNSVDIR=$! +echo "Started runsvdir, PID is $RUNSVDIR" +trap "echo Shutting Down && /etc/runit/3 && kill -HUP $RUNSVDIR && wait $RUNSVDIR" SIGTERM SIGHUP +wait $RUNSVDIR diff --git a/image/base/runit-2 b/image/base/runit-2 index b3ec10e..ba3cfb2 100755 --- a/image/base/runit-2 +++ b/image/base/runit-2 @@ -1,2 +1,2 @@ #!/bin/bash -/usr/bin/runsvdir -P /etc/service +exec /usr/bin/runsvdir -P /etc/service diff --git a/image/base/runit-3 b/image/base/runit-3 new file mode 100755 index 0000000..40e510c --- /dev/null +++ b/image/base/runit-3 @@ -0,0 +1,3 @@ +#!/bin/bash + +/bin/run-parts --verbose /etc/runit/3.d diff --git a/image/discourse/Dockerfile b/image/discourse/Dockerfile index bd15c9e..b612e5f 100644 --- a/image/discourse/Dockerfile +++ b/image/discourse/Dockerfile @@ -1,5 +1,5 @@ -# Current version 1.0.4 -FROM samsaffron/discourse_base:1.0.4 +# Current version 1.0.5 +FROM samsaffron/discourse_base:1.0.5 MAINTAINER Sam Saffron "https://twitter.com/samsaffron" @@ -11,7 +11,7 @@ RUN useradd discourse -s /bin/bash -m -U &&\ git remote set-branches --add origin tests-passed &&\ chown -R discourse:discourse /var/www/discourse &&\ cd /var/www/discourse &&\ - sudo -u discourse RAILS4=1 bundle install --deployment \ + sudo -u discourse bundle install --deployment \ --without test --without development &&\ find /var/www/discourse/vendor/bundle -name tmp -type d -exec rm -rf {} + diff --git a/launcher b/launcher index 797c58c..c39d8a9 100755 --- a/launcher +++ b/launcher @@ -13,7 +13,7 @@ config_file=containers/"$config".yml cidfile=cids/"$config".cid cidbootstrap=cids/"$config"_boostrap.cid local_discourse=local_discourse -image=samsaffron/discourse:1.0.4 +image=samsaffron/discourse:1.0.5 docker_path=`which docker.io || which docker` if [ "${SUPERVISED}" = "true" ]; then @@ -340,7 +340,7 @@ run_start(){ fi $docker_path run $links $attach_on_run $restart_policy "${env[@]}" -h "`hostname`-$config" -e DOCKER_HOST_IP=$docker_ip --name $config -t --cidfile $cidfile $ports \ - $volumes $local_discourse/$config /sbin/runit + $volumes $local_discourse/$config /sbin/boot exit 0 else diff --git a/templates/postgres.template.yml b/templates/postgres.template.yml index 0b2a323..cb835c6 100644 --- a/templates/postgres.template.yml +++ b/templates/postgres.template.yml @@ -27,6 +27,14 @@ run: #!/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 + - file: path: /root/upgrade_postgres chmod: "+x" diff --git a/templates/redis.template.yml b/templates/redis.template.yml index 1c61d0c..249018a 100644 --- a/templates/redis.template.yml +++ b/templates/redis.template.yml @@ -7,6 +7,13 @@ run: 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" diff --git a/templates/sshd.template.yml b/templates/sshd.template.yml index 9f365c4..5b7f4b1 100644 --- a/templates/sshd.template.yml +++ b/templates/sshd.template.yml @@ -23,6 +23,13 @@ run: exec 2>&1 exec /usr/sbin/sshd -D -e + - file: + path: /etc/runit/3.d/99-ssh + chmod: "+x" + contents: | + #!/bin/bash + sv stop sshd + - exec: hook: sshd cmd: diff --git a/templates/web.template.yml b/templates/web.template.yml index fb2518e..532a1b1 100644 --- a/templates/web.template.yml +++ b/templates/web.template.yml @@ -54,6 +54,20 @@ run: mkdir -p /var/log/nginx exec /usr/sbin/nginx + - file: + path: /etc/runit/3.d/01-nginx + chmod: "+x" + contents: | + #!/bin/bash + sv stop nginx + + - file: + path: /etc/runit/3.d/02-unicorn + chmod: "+x" + contents: | + #!/bin/bash + sv stop unicorn + - exec: cd: $home hook: code -- 2.25.1