67 lines
2.0 KiB
Bash
67 lines
2.0 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
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 "DEVISE_SECRET"
|
|
file_env "SECRET_COOKIE_TOKEN"
|
|
file_env "POSTGRES_PASSWORD"
|
|
file_env "SMTP_PASSWORD"
|
|
export DB_HOST="db"
|
|
export DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}"
|
|
|
|
if [ -n "$1" ]; then
|
|
echo "Running '$1'"
|
|
$1
|
|
else
|
|
|
|
echo "starting loomio!"
|
|
if [ "$TASK" = "worker" ]; then
|
|
bundle exec sidekiq
|
|
else
|
|
sudo apt update -y && sudo apt install -y postgresql-client
|
|
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 "$DB_HOST" -lqt | cut -d \| -f 1 | grep -wq "$POSTGRES_DB"; then
|
|
echo "database '$POSTGRES_DB' exists."
|
|
|
|
# check if the database contains tables
|
|
TABLE_COUNT=$(PGPASSWORD=$(cat /run/secrets/db_password) psql -U "$POSTGRES_USER" -h "$DB_HOST" -d "$POSTGRES_DB" -t -c "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public';")
|
|
|
|
if [ "$TABLE_COUNT" -eq 0 ]; then
|
|
echo "Database '$POSTGRES_DB' is empty, running db:setup."
|
|
bundle exec rake db:setup
|
|
else
|
|
echo "database '$POSTGRES_DB' not empty, running migrations."
|
|
bundle exec rake db:migrate
|
|
fi
|
|
else
|
|
echo "database '$POSTGRES_DB' does not exist, running db:setup."
|
|
bundle exec rake db:setup
|
|
fi
|
|
|
|
bundle exec puma -C config/puma.rb
|
|
fi
|
|
fi
|