#!/bin/bash set -e cd /app # Cleanup any leftover pid file if [ -f /app/tmp/pids/server.pid ]; then rm /app/tmp/pids/server.pid 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}" ]; then DATABASE_ADAPTER=${DATABASE_ADAPTER:-mysql2} DATABASE_HOST=${DATABASE_HOST:-${MYSQL_PORT_3306_TCP_ADDR}} DATABASE_PORT=${DATABASE_PORT:-${MYSQL_PORT_3306_TCP_PORT}} DATABASE_ENCODING=${DATABASE_ENCODING:-utf8mb4} elif [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then DATABASE_ADAPTER=${DATABASE_ADAPTER:-postgresql} DATABASE_HOST=${DATABASE_HOST:-${POSTGRES_PORT_5432_TCP_ADDR}} DATABASE_PORT=${DATABASE_PORT:-${POSTGRES_PORT_5432_TCP_PORT}} 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}} IFS="=" grep = /app/.env.example | sed -e 's/^#\([^ ]\)/\1/' | grep -v -e '^#' | \ while read var value ; do eval "echo \"$var=\${$var:-\${HUGINN_$var-\$value}}\"" done | grep -v -e ^= > /app/.env eval "echo RAILS_ENV=${RAILS_ENV}" >> .env eval "echo START_MYSQL=${START_MYSQL}" >> .env echo "ON_HEROKU=true" >> .env echo "RAILS_SERVE_STATIC_FILES=true" >> .env chmod ugo+r /app/.env source /app/.env sudo -u huginn -H bundle install --deployment --without test # 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 # initialize supervisord config cat > /etc/supervisor/conf.d/supervisord.conf < /tmp/bootstrap.sh #!/bin/bash -e source /app/.env echo DATABASE_HOST=\${DATABASE_HOST} # start mysql server if \${DATABASE_HOST} is the .env.example default if [ "\${START_MYSQL}" = "true" ]; then echo "DATABASE_SOCKET=/var/run/mysqld/mysqld.sock" >> .env if [ "\${DATABASE_ADAPTER}" = "postgresql" ]; then echo "DATABASE_ADAPTER 'postgresql' is not supported internally. Please provide DATABASE_HOST." exit 1 fi # configure supervisord to start mysql (manual) cat > /etc/supervisor/conf.d/mysqld.conf </dev/null # wait for mysql server to start (max 120 seconds) timeout=120 while ! mysqladmin -u root status >/dev/null 2>&1 && ! mysqladmin -u root --password=\${DATABASE_PASSWORD} status >/dev/null 2>&1 do (( timeout = timeout - 1 )) if [ \$timeout -eq 0 ]; then echo "Failed to start mysql server" exit 1 fi echo -n . sleep 1 done if ! echo "USE \${DATABASE_NAME}" | mysql -u\${DATABASE_USERNAME:-root} \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} >/dev/null 2>&1; then echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('\${DATABASE_PASSWORD:\$DATABASE_PASSWORD}');" | mysql -u root fi fi supervisorctl start foreman >/dev/null BOOTSTRAP chmod 755 /tmp/bootstrap.sh cat > /etc/supervisor/conf.d/bootstrap.conf < /tmp/foreman.sh #!/bin/bash -e source /app/.env # The database may need to start up for a bit first if [ -n "\${INTENTIONALLY_SLEEP}" ]; then echo "Intentionally sleeping \${INTENTIONALLY_SLEEP}" sleep \${INTENTIONALLY_SLEEP} fi if [ -n "\${DATABASE_INITIAL_CONNECT_MAX_RETRIES}" ]; then max=\${DATABASE_INITIAL_CONNECT_MAX_RETRIES} count=0 while ! rake database_test:ping > /dev/null 2>&1 && [[ \$count -le \$max ]] ; do count=\$[\$count+1] echo "Retry \$count of \$max attempting to connect to \$DATABASE_HOST. Sleeping \${DATABASE_INITIAL_CONNECT_SLEEP:5}" sleep \${DATABASE_INITIAL_CONNECT_SLEEP:5} done fi # We may need to try and create a database sudo -u huginn -EH bundle exec rake db:create # Assuming we have a created database, run the migrations and seed it idempotently. if [ -z "\${DO_NOT_MIGRATE}" ]; then sudo -u huginn -EH bundle exec rake db:migrate fi if [ -z "\${DO_NOT_SEED}" ]; then sudo -u huginn -EH bash -xc 'bundle exec rake db:seed &' fi # Fixup the Procfile and prepare the PORT if [ -n "\${DO_NOT_RUN_JOBS}" ]; then perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile fi perl -pi -e 's/rails server -b0.0.0.0\$/rails server -b 0.0.0.0 -p \\\$PORT/' /app/Procfile export PORT="\${PORT:-3000}" # Start huginn exec sudo -u huginn -EH bash -xc 'exec bundle exec foreman start' FOREMAN chmod 755 /tmp/foreman.sh cat > /etc/supervisor/conf.d/foreman.conf <