Merge pull request #496 from ianblenke/replace-dockerfile

Added ianblenke/docker-huginn as docker/ subdirectory
This commit is contained in:
Andrew Cantino 2014-09-23 20:23:06 -07:00
commit ae15ede9e3
6 changed files with 319 additions and 0 deletions

30
docker/Dockerfile Normal file
View file

@ -0,0 +1,30 @@
FROM ubuntu:14.04
MAINTAINER Andrew Cantino
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
apt-get install -y software-properties-common && \
add-apt-repository -y ppa:git-core/ppa && \
add-apt-repository -y ppa:brightbox/ruby-ng && \
apt-get update && \
apt-get install -y build-essential checkinstall postgresql-client \
git-core mysql-server redis-server python2.7 python-docutils \
libmysqlclient-dev libpq-dev zlib1g-dev libyaml-dev libssl-dev \
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 && \
gem install --no-ri --no-rdoc bundler && \
rm -rf /var/lib/apt/lists/*
ADD scripts/ /scripts
RUN chmod 755 /scripts/setup /scripts/init
RUN /scripts/setup
VOLUME /var/lib/mysql
EXPOSE 5000
CMD ["/scripts/init"]

2
docker/Makefile Normal file
View file

@ -0,0 +1,2 @@
build:
docker build -t cantino/huginn .

137
docker/README.md Normal file
View file

@ -0,0 +1,137 @@
Huginn for docker with multiple container linkage
=================================================
This image runs a linkable [Huginn](https://github.com/cantino/huginn) instance.
There is an automated build repository on docker hub for [cantino/huginn](https://registry.hub.docker.com/builds/github/cantino/huginn/).
This was patterned after [sameersbn/gitlab](https://registry.hub.docker.com/u/sameersbn/gitlab) by [ianblenke/huginn](http://github.com/ianblenke/huginn), and imported here for official generation of a docker hub auto-build image.
The scripts/init script generates a .env file containing the variables as passed as per normal Huginn documentation.
The same environment variables that would be used for Heroku PaaS deployment are used by this script.
The scripts/init script is aware of mysql and postgres linked containers through the environment variables:
MYSQL_PORT_3306_TCP_ADDR
MYSQL_PORT_3306_TCP_PORT
and
POSTGRESQL_PORT_5432_TCP_ADDR
POSTGRESQL_PORT_5432_TCP_PORT
Its recommended to use an image that allows you to create a database via environmental variables at docker run, like `paintedfox / postgresql` or `centurylink / mysql`, so the db is populated when this script runs.
If you do not link a database container, a built-in mysql database will be started.
There is an exported docker volume of /var/lib/mysql to allow persistence of that mysql database.
Additionally, the database variables may be overridden from the above as per the standard Huginn documentation:
HUGINN_DATABASE_ADAPTER #(must be either 'postgres' or 'mysql2')
HUGINN_DATABASE_HOST
HUGINN_DATABASE_PORT
This script will run database migrations (rake db:migrate) which should be idempotent.
It will also seed the database (rake db:seed) unless this is defined:
DO_NOT_SEED
This same seeding initially defines the "admin" user with a default password of "password" as per the standard Huginn documentation.
If you do not wish to have the default 6 agents, you will want to set the above environment variable after your initially deploy, otherwise they will be added automatically the next time a container pointing at the database is spun up.
The CMD launches Huginn via the scripts/init script. This may become the ENTRYPOINT later. It does take under a minute for Huginn to come up. Use environmental variables that match your DB's creds to ensure it works.
## Usage
Simple stand-alone usage:
docker run -it -p 5000:5000 cantino/huginn
To link to another mysql container, for example:
docker run --rm --name newcentury_mysql -p 3306 \
-e HUGINN_MYSQL_DATABASE=huginn \
-e HUGINN_MYSQL_USER=huginn \
-e HUGINN_MYSQL_PASSWORD=somethingsecret \
-e HUGINN_MYSQL_ROOT_PASSWORD=somethingevenmoresecret \
cantino/huginn
docker run --rm --name huginn --link newcentury_mysql:MYSQL -p 5000:5000 \
-e HUGINN_DATABASE_NAME=huginn \
-e HUGINN_DATABASE_USER=huginn \
-e HUGINN_DATABASE_PASSWORD=somethingsecret \
cantino/huginn
To link to another container named 'postgres':
docker run --rm --name huginn --link POSTGRES:mysql -p 5000:5000 -e "DATABASE_USER=huginn" -e "DATABASE_PASSWORD=pass@word" cantino/huginn
## Environment Variables
Other Huginn 12factored environment variables of note, as generated and put into the .env file as per Huginn documentation,
with an additional `HUGINN_` prefix to the variable.
These are:
HUGINN_APP_SECRET_TOKEN
HUGINN_DOMAIN
HUGINN_ASSET_HOST
HUGINN_DATABASE_ADAPTER
HUGINN_DATABASE_ENCODING
HUGINN_DATABASE_RECONNECT
HUGINN_DATABASE_NAME
HUGINN_DATABASE_POOL
HUGINN_DATABASE_USERNAME
HUGINN_DATABASE_PASSWORD
HUGINN_DATABASE_HOST
HUGINN_DATABASE_PORT
HUGINN_DATABASE_SOCKET
HUGINN_RAILS_ENV
HUGINN_FORCE_SSL
HUGINN_INVITATION_CODE
HUGINN_SMTP_DOMAIM
HUGINN_SMTP_USER_NAME
HUGINN_SMTP_PASSWORD
HUGINN_SMTP_SERVER
HUGINN_SMTP_PORT
HUGINN_SMTP_AUTHENTICATION
HUGINN_SMTP_ENABLE_STARTTLS_AUTO
HUGINN_EMAIL_FROM_ADDRESS
HUGINN_AGENT_LOG_LENGTH
HUGINN_TWITTER_OAUTH_KEY
HUGINN_TWITTER_OAUTH_SECRET
HUGINN_THIRTY_SEVEN_SIGNALS_OAUTH_KEY
HUGINN_THIRTY_SEVEN_SIGNALS_OAUTH_SECRET
HUGINN_GITHUB_OAUTH_KEY
HUGINN_GITHUB_OAUTH_SECRET
HUGINN_AWS_ACCESS_KEY_ID
HUGINN_AWS_ACCESS_KEY
HUGINN_AWS_SANDBOX
HUGINN_FARADAY_HTTP_BACKEND
HUGINN_DEFAULT_HTTP_USER_AGENT
HUGINN_ALLOW_JSONPATH_EVAL
HUGINN_ENABLE_INSECURE_AGENTS
HUGGIN_ENABLE_SECOND_PRECISION_SCHEDULE
HUGINN_USE_GRAPHVIZ_DOT
HUGINN_TIMEZONE
HUGGIN_FAILED_JOBS_TO_KEEP
The above environment variables will override the defaults. The defaults are read from the [.env.example](https://github.com/cantino/huginn/blob/master/.env.example) file.
For variables in the .env.example that are commented out, the default is to not include that variable in the generated .env file.
## Building on your own
You don't need to do this on your own, because there is an [automated build](https://registry.hub.docker.com/u/cantino/huginn/) for this repository, but if you really want:
docker build --rm=true --tag={yourname}/huginn .
## Source
The source is [available on GitHub](https://github.com/cantino/huginn/).
Please feel free to submit pull requests and/or fork at your leisure.

111
docker/scripts/init Executable file
View file

@ -0,0 +1,111 @@
#!/bin/bash
set -e
cd /app
# Default to the environment variable values set in .env.example
source /app/.env.example
# is a mysql or postgresql database linked?
# requires that the mysql or postgresql containers have exposed
# port 3306 and 5432 respectively.
if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ]; then
HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-mysql2}
HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${MYSQL_PORT_3306_TCP_ADDR}}
HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${MYSQL_PORT_3306_TCP_PORT}}
elif [ -n "${POSTGRESQL_PORT_5432_TCP_ADDR}" ]; then
HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-postgres}
HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${POSTGRESQL_PORT_5432_TCP_ADDR}}
HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${POSTGRESQL_PORT_5432_TCP_PORT}}
fi
grep = ../.env.example | sed -e 's/^#//' | grep -v -e '^#' | cut -d= -f1 | \
while read var ; do
echo "$var=\${HUGINN_$var:-\$$var}"
done > /app/.env
chmod ugo+r /app/.env
source /app/.env
DATABASE_HOST=${HUGINN_DATABASE_HOST:-${DATABASE_HOST:-localhost}}
DATABASE_ENCODING=${HUGINN_DATABASE_ENCODING:-${DATABASE_ENCODING}}
USE_GRAPHVIZ_DOT=${HUGINN_USE_GRAPHVIZ_DOT:-${USE_GRAPHVIZ_DOT}}
# use default port number if it is still not set
case "${DATABASE_ADAPTER}" in
mysql2) DATABASE_PORT=${DATABASE_PORT:-3306} ;;
postgres) DATABASE_PORT=${DATABASE_PORT:-5432} ;;
*) echo "Unsupported database adapter. Available adapters are mysql2, and postgres." && exit 1 ;;
esac
# start supervisord
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
# start mysql server if ${DATABASE_HOST} is localhost
if [ "${DATABASE_HOST}" == "localhost" ]; then
if [ "${DATABASE_ADAPTER}" == "postgres" ]; then
echo "DATABASE_ADAPTER 'postgres' is not supported internally. Please provide DATABASE_HOST."
exit 1
fi
# configure supervisord to start mysql (manual)
cat > /etc/supervisor/conf.d/mysqld.conf <<EOF
[program:mysqld]
priority=20
directory=/tmp
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
EOF
supervisorctl reload
# fix permissions and ownership of /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
chmod 700 /var/lib/mysql
# initialize MySQL data directory
if [ ! -d /var/lib/mysql/mysql ]; then
mysql_install_db --user=mysql
fi
echo "Starting mysql server..."
supervisorctl start mysqld >/dev/null
# wait for mysql server to start (max 120 seconds)
timeout=120
while ! mysqladmin -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} status >/dev/null 2>&1
do
timeout=$(expr $timeout - 1)
if [ $timeout -eq 0 ]; then
echo "Failed to start mysql server"
exit 1
fi
sleep 1
done
if ! echo "USE ${DATABASE_NAME}" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} >/dev/null 2>&1; then
DB_INIT="yes"
echo "CREATE DATABASE IF NOT EXISTS \`${DATABASE_NAME}\` DEFAULT CHARACTER SET \`utf8\` COLLATE \`utf8_unicode_ci\`;" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD}
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
# 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
[ -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
perl -pi -e 's/rails server$/rails server -p \$PORT/' /app/Procfile
export PORT
# Start huginn
sudo -u huginn -EH bundle exec foreman start
# As the ENTRYPOINT script, when this exits the docker container will Exit.
exit 0

39
docker/scripts/setup Executable file
View file

@ -0,0 +1,39 @@
#!/bin/bash
set -e
# Initialize variables used by Huginn at installation time
# Huginn is 12factor aware, embrace that fact for use inside of docker
ON_HEROKU=${ON_HEROKU:-true}
# Shallow clone the huginn project repo
git clone --depth 1 https://github.com/cantino/huginn /app
cd /app
# add a huginn group and user
adduser --group huginn
adduser --disabled-login --ingroup huginn --gecos 'Huginn' --no-create-home --home /app huginn
adduser huginn sudo
passwd -d huginn
# Change the ownership to huginn
chown -R huginn:huginn /app
# create required tmp and log directories
sudo -u huginn -H mkdir -p tmp/pids tmp/cache tmp/sockets log
chmod -R u+rwX log tmp
# install gems required by Huginn, use local cache if available
if [ -d "/scripts/cache" ]; then
mv /scripts/cache vendor/
chown -R huginn:huginn vendor/cache
fi
sudo -u huginn -H bundle install --deployment --without development test
# silence setlocale message (THANKS DEBIAN!)
cat > /etc/default/locale <<EOF
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
EOF