#!/bin/bash set -e export LC_ALL=en_US.UTF-8 cd /app if [ -f /app/tmp/pids/delayed_job.pid ]; then rm /app/tmp/pids/delayed_job.pid fi export HOME=/app if ! whoami &> /dev/null; then if [ -w /etc/passwd ]; then echo "${USER_NAME:-default}:x:$(id -u):0:${USER_NAME:-default} user:${HOME}:/sbin/nologin" >> /etc/passwd fi fi # 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}" || ("${DATABASE_ADAPTER}" == "mysql2" && -n "${DATABASE_HOST}") ]]; then DATABASE_ADAPTER=${DATABASE_ADAPTER:-mysql2} DATABASE_HOST=${DATABASE_HOST:-${MYSQL_PORT_3306_TCP_ADDR}} DATABASE_PORT=${DATABASE_PORT:-${MYSQL_PORT_3306_TCP_PORT:-3306}} DATABASE_ENCODING=${DATABASE_ENCODING:-utf8mb4} elif [[ -n "${POSTGRES_PORT_5432_TCP_ADDR}" || ("${DATABASE_ADAPTER}" == "postgresql" && -n "${DATABASE_HOST}") ]]; then DATABASE_ADAPTER=${DATABASE_ADAPTER:-postgresql} DATABASE_HOST=${DATABASE_HOST:-${POSTGRES_PORT_5432_TCP_ADDR}} DATABASE_PORT=${DATABASE_PORT:-${POSTGRES_PORT_5432_TCP_PORT:-5432}} DATABASE_ENCODING=utf8 else START_MYSQL=${START_MYSQL:-true} fi USE_GRAPHVIZ_DOT=${HUGINN_USE_GRAPHVIZ_DOT:-${USE_GRAPHVIZ_DOT:-dot}} DATABASE_HOST=${HUGINN_DATABASE_HOST:-${DATABASE_HOST:-localhost}} DATABASE_PORT=${HUGINN_DATABASE_PORT:-${DATABASE_PORT}} DATABASE_ENCODING=${HUGINN_DATABASE_ENCODING:-${DATABASE_ENCODING}} DATABASE_PASSWORD=${HUGINN_DATABASE_PASSWORD:-${DATABASE_PASSWORD:-password}} DATABASE_NAME=${HUGINN_DATABASE_NAME:-${DATABASE_NAME:-huginn_production}} RAILS_ENV=${HUGINN_RAILS_ENV:-${RAILS_ENV:-production}} RAILS_LOG_TO_STDOUT=${RAILS_LOG_TO_STDOUT:-true} RAILS_SERVE_STATIC_FILES=${RAILS_SERVE_STATIC_FILES:-true} IP="${IP:-0.0.0.0}" PORT=${HUGINN_PORT:-${PORT:-3000}} PORT="$(echo $PORT|awk -F ':' '{print $NF}')" HUGINN_PORT=${PORT} cp -a /app/.env.example /tmp/ # Do we have a Rails secret yet? # If not, generate one, but persist it for the container's lifetime. if [ -z "$APP_SECRET_TOKEN" ] && grep -q ^APP_SECRET_TOKEN=REPLACE_ME_NOW /tmp/.env.example; then [ -e '/tmp/secret' ] && secret="$(cat /tmp/secret)" if [ -z "$secret" ]; then echo 'Generating random APP_SECRET_TOKEN.' secret=$(dd if=/dev/urandom bs=36 count=1 | openssl base64) echo "$secret" > /tmp/secret fi sed -i "s:REPLACE_ME_NOW!:$secret:" /tmp/.env.example fi # Add ENV variables to .env.example which are not present in it but usable cat >> /tmp/.env.example <> /tmp/.env.example quote_if_necessary() { if grep -q -E -e '^".*" *$' -e "^'.*' *$" <<<"$*"; then # preserve single or double quotes if they are provided echo "$*"; else # otherwise double quote the string after escaping \, $, ` echo "\"$(sed -e 's/\\/\\\\/g' -e 's/\$/\\\$/g' -e 's/`/\\`/g' <<<"$*")\"" fi } # Default to the environment variable values set in .env.example sed -n -r -e 's/^#?([A-Za-z0-9_]+=.*)/\1/p' /tmp/.env.example | \ while IFS='=' read var value ; do huginn_var="HUGINN_$var" val="${!huginn_var:-${!var:-$value}}" echo "$var=$(quote_if_necessary $val)" done > /tmp/.env source /tmp/.env if [ "${START_MYSQL}" = "true" ]; then echo 'DATABASE_SOCKET=/app/tmp/sockets/mysqld.sock' >> /tmp/.env DATABASE_SOCKET='/app/tmp/sockets/mysqld.sock' fi [ -e '/app/.env' ] || cp -a /tmp/.env /app/ chmod ugo+r /app/.env rm -f /tmp/.env.example # use default port number if it is still not set case "${DATABASE_ADAPTER}" in mysql2) DATABASE_PORT=${DATABASE_PORT:-3306} ;; postgresql) DATABASE_PORT=${DATABASE_PORT:-5432} ;; *) echo "Unsupported database adapter. Available adapters are mysql2, and postgresql." && exit 1 ;; esac bundle install --without test development --path vendor/bundle # Configure the unicorn server sed -r config/unicorn.rb.example \ -e "s/^ *listen .+/listen '$IP:$PORT'/" \ -e 's;^ *stderr_path .+;stderr_path "/dev/stderr";' \ -e 's;^ *stdout_path .+;stdout_path "/dev/stdout";' \ -e '/^ *pid .+/d' > config/unicorn.rb mkdir -p tmp/pids tmp/cache tmp/sockets log