fab-manager/entrypoint.sh.tmpl

205 lines
5.7 KiB
Bash

#!/bin/bash
set -e
file_env() {
# 3wc: Load $VAR_FILE into $VAR - useful for secrets. See
# https://medium.com/@adrian.gheorghe.dev/using-docker-secrets-in-your-environment-variables-7a0609659aab
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
file_env "SECRET_KEY_BASE"
file_env "SMTP_PASSWORD"
db_setup() {
echo "Database 'fablab_production' is empty, running db setup."
bundle exec rails db:create
bundle exec rails db:schema:load
ADMIN_EMAIL="$ADMIN_INIT_EMAIL" ADMIN_PASSWORD="$(cat /run/secrets/admin_init_password)" bundle exec rake db:seed
}
wait_for_postgres() {
local retries=30
local count=0
>&2 echo "Waiting for PostgreSQL..."
until PGPASSWORD=$(cat /run/secrets/db_password) psql -U "$POSTGRES_USER" -h "$POSTGRES_HOST" -c '\l' &> /dev/null; do
count=$((count+1))
if [ "$count" -ge "$retries" ]; then
>&2 echo "Postgres is still unavailable after $retries attempts - exiting"
exit 1
fi
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - continuing"
}
wait_for_redis() {
local retries=30
local count=0
until redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" ping | grep -q "PONG"; do
count=$((count+1))
if [ "$count" -ge "$retries" ]; then
>&2 echo "Redis is still unavailable after $retries attempts - exiting"
exit 1
fi
>&2 echo "Redis is unavailable - sleeping"
sleep 1
done
>&2 echo "Redis is up - continuing"
}
wait_for_elasticsearch() {
local retries=30 # Number of attempts before timeout
local count=0
>&2 echo "Waiting for Elasticsearch..."
until curl -s -o /dev/null -w "%{http_code}" "http://$ELASTICSEARCH_HOST:9200" | grep -q "200"; do
count=$((count+1))
if [ "$count" -ge "$retries" ]; then
>&2 echo "Elasticsearch is still unavailable after $retries attempts - exiting"
exit 1
fi
>&2 echo "Elasticsearch is unavailable - sleeping"
sleep 1
done
>&2 echo "Elasticsearch is up - continuing"
}
echo "generate binstubs"
yes|bundle exec rails app:update:bin
echo "install redis tools"
apt-get update && apt-get install redis-tools -y
echo "set Env Variables"
if [[ -z "${FAB_MANAGER_LOCALE_PACK}" ]]; then
echo "FAB_MANAGER_LOCALE_PACK is not set or is empty, Manual locale env is used"
else
echo "FAB_MANAGER_LOCALE_PACK is set to '${FAB_MANAGER_LOCALE_PACK}'."
case "${FAB_MANAGER_LOCALE_PACK}" in
"en")
echo "English locale selected."
export RAILS_LOCALE=en
export APP_LOCALE=en
export MOMENT_LOCALE=en
export SUMMERNOTE_LOCALE=en-US
export ANGULAR_LOCALE=en-us
export FULLCALENDAR_LOCALE=en
export INTL_LOCALE=en-US
export INTL_CURRENCY=USD
export POSTGRESQL_LANGUAGE_ANALYZER=english
export WEEK_STARTING_DAY=monday
export D3_DATE_FORMAT=%m/%d/%Y
export UIB_DATE_FORMAT=MM/dd/yyyy
export EXCEL_DATE_FORMAT=MM/dd/yyyy
;;
"fr")
echo "French locale selected."
export RAILS_LOCALE=fr
export APP_LOCALE=fr
export MOMENT_LOCALE=fr
export SUMMERNOTE_LOCALE=fr-FR
export ANGULAR_LOCALE=fr-fr
export FULLCALENDAR_LOCALE=fr
export INTL_LOCALE=fr-FR
export INTL_CURRENCY=EUR
export POSTGRESQL_LANGUAGE_ANALYZER=french
export WEEK_STARTING_DAY=monday
export D3_DATE_FORMAT=%d/%m/%y
export UIB_DATE_FORMAT=dd/MM/yyyy
export EXCEL_DATE_FORMAT=dd/mm/yyyy
;;
"de")
echo "German locale selected."
export RAILS_LOCALE=de
export APP_LOCALE=de
export MOMENT_LOCALE=de
export SUMMERNOTE_LOCALE=de-DE
export ANGULAR_LOCALE=de-de
export FULLCALENDAR_LOCALE=de
export INTL_LOCALE=de-DE
export INTL_CURRENCY=EUR
export POSTGRESQL_LANGUAGE_ANALYZER=german
export WEEK_STARTING_DAY=monday
export D3_DATE_FORMAT=%d/%m/%Y
export UIB_DATE_FORMAT=dd/MM/yyyy
export EXCEL_DATE_FORMAT=dd/mm/yyyy
;;
*)
echo "Unknown locale Pack: ${!VAR_NAME}. Manual locale env is used"
;;
esac
fi
wait_for_postgres
wait_for_elasticsearch
wait_for_redis
echo "initialising fab-manager!"
# handle auth_provider.yml
[ -f /usr/src/app/authconfig/auth_provider.yml ] || touch /usr/src/app/authconfig/auth_provider.yml
ln -s /usr/src/app/authconfig/auth_provider.yml /usr/src/app/config/auth_provider.yml
bundle install
# running this code instaed of db:prepare in docker_start.sh in loomio container
# as postgres container creates empty db, somehow db:prepare cannot cope.
# therefore we run db:setup or db:migrate individually
if PGPASSWORD=$(cat /run/secrets/db_password) psql -U "$POSTGRES_USER" -h "$POSTGRES_HOST" -lqt | cut -d \| -f 1 | grep -wq "fablab_production"; then
echo "database fablab_production exists."
# check if the database contains tables
TABLE_COUNT=$(PGPASSWORD=$(cat /run/secrets/db_password) psql -U "$POSTGRES_USER" -h "$POSTGRES_HOST" -d "fablab_production" -t -c "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public';")
if [ "$TABLE_COUNT" -eq 0 ]; then
db_setup
else
echo "database 'fablab_production' not empty, running migrations."
bundle exec rails db:migrate
fi
else
db_setup
fi
bundle exec rake assets:precompile
bundle exec rake fablab:es:build_stats
echo "finished initialising fab-manager, starting supervisor!"
/usr/bin/supervisord -c /etc/supervisor/conf.d/fabmanager.conf