Refactoring the docker image to exec supervisord and properly handle DO_NOT_RUN_JOBS

This commit is contained in:
Ian Blenke 2015-01-12 21:36:58 -05:00
parent 5f017cde49
commit bff5e75ec0
3 changed files with 72 additions and 10 deletions

View file

@ -13,10 +13,12 @@ RUN apt-get update && \
libgdbm-dev libreadline-dev libncurses5-dev libffi-dev \
libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev \
graphviz libgraphviz-dev \
ruby2.1 ruby2.1-dev supervisor && \
ruby2.1 ruby2.1-dev supervisor python-pip && \
gem install --no-ri --no-rdoc bundler && \
rm -rf /var/lib/apt/lists/*
RUN pip install supervisor-stdout
ADD scripts/ /scripts
RUN chmod 755 /scripts/setup /scripts/init

View file

@ -38,9 +38,29 @@ case "${DATABASE_ADAPTER}" in
*) echo "Unsupported database adapter. Available adapters are mysql2, and postgres." && exit 1 ;;
esac
# start supervisord
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
# initialize supervisord config
cat > /etc/supervisor/conf.d/supervisord.conf <<EOF
[supervisord]
nodaemon = true
user = root
logfile = /dev/stdout
loglevel = debug
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[eventlistener:stdout]
command = supervisor_stdout
buffer_size = 100
events = PROCESS_LOG
result_handler = supervisor_stdout:event_handler
EOF
# prepare the supervisord bootstrap service script
cat <<BOOTSTRAP > /tmp/bootstrap.sh
#!/bin/bash -xe
# start mysql server if ${DATABASE_HOST} is localhost
if [ "${DATABASE_HOST}" == "localhost" ]; then
if [ "${DATABASE_ADAPTER}" == "postgres" ]; then
@ -57,8 +77,8 @@ command=/usr/bin/mysqld_safe
user=root
autostart=false
autorestart=true
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
stdout_events_enabled=true
stderr_events_enabled=true
EOF
supervisorctl reload
@ -92,7 +112,29 @@ EOF
echo "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON \`${DATABASE_NAME}\`.* TO 'root'@'localhost';" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD}
fi
fi
supervisorctl start huginn >/dev/null
BOOTSTRAP
chmod 755 /tmp/bootstrap.sh
cat > /etc/supervisor/conf.d/bootstrap.conf <<EOF
[program:bootstrap]
command=/tmp/bootstrap.sh
priority=10
directory=/app
process_name=%(program_name)s
autostart=true
autorestart=false
stdout_events_enabled=true
stderr_events_enabled=true
stopsignal=TERM
exitcodes=0
startsecs=0
stopwaitsecs=1
EOF
cat <<FOREMAN > /tmp/foreman.sh
#!/bin/bash
# Assuming we have a created database, run the migrations and seed it idempotently.
[ -z "${DO_NOT_MIGRATE}" ] && sudo -u huginn -EH bundle exec rake db:migrate
[ -z "${DO_NOT_SEED}" ] && sudo -u huginn -EH bundle exec rake db:seed
@ -100,12 +142,30 @@ fi
[ -n "$INTENTIONALLY_SLEEP" ] && sleep $INTENTIONALLY_SLEEP
# Fixup the Procfile and prepare the PORT
[ -z "${DO_NOT_RUN_JOBS}" ] && perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
[ -n "${DO_NOT_RUN_JOBS}" ] && perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
perl -pi -e 's/rails server$/rails server -p \$PORT/' /app/Procfile
export PORT
# Start huginn
sudo -u huginn -EH bundle exec foreman start
exec sudo -u huginn -EH bundle exec foreman start
FOREMAN
# As the ENTRYPOINT script, when this exits the docker container will Exit.
exit 0
chmod 755 /tmp/foreman.sh
cat > /etc/supervisor/conf.d/foreman.conf <<EOF
[program:rails]
command=/tmp/foreman.sh
priority=10
directory=/app
process_name=%(program_name)s
autostart=false
autorestart=true
stdout_events_enabled=true
stderr_events_enabled=true
stopsignal=TERM
startsecs=0
stopwaitsecs=1
EOF
# start supervisord
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

View file

@ -7,7 +7,7 @@ set -e
ON_HEROKU=${ON_HEROKU:-true}
# Shallow clone the huginn project repo
git clone --depth 1 https://github.com/cantino/huginn /app
git clone --depth 1 https://github.com/ianblenke/huginn /app
cd /app