init 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash
  2. set -e
  3. cd /app
  4. # Default to the environment variable values set in .env.example
  5. source /app/.env.example
  6. # is a mysql or postgresql database linked?
  7. # requires that the mysql or postgresql containers have exposed
  8. # port 3306 and 5432 respectively.
  9. if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ]; then
  10. HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-mysql2}
  11. HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${MYSQL_PORT_3306_TCP_ADDR}}
  12. HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${MYSQL_PORT_3306_TCP_PORT}}
  13. elif [ -n "${POSTGRESQL_PORT_5432_TCP_ADDR}" ]; then
  14. HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-postgres}
  15. HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${POSTGRESQL_PORT_5432_TCP_ADDR}}
  16. HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${POSTGRESQL_PORT_5432_TCP_PORT}}
  17. fi
  18. grep = /app/.env.example | sed -e 's/^#[^ ]//' | grep -v -e '^#' | cut -d= -f1 | \
  19. while read var ; do
  20. eval "echo \"$var=\\\"\${HUGINN_$var:-\$$var}\\\"\""
  21. done | grep -v -e ^= > /app/.env
  22. chmod ugo+r /app/.env
  23. source /app/.env
  24. DATABASE_HOST=${HUGINN_DATABASE_HOST:-${DATABASE_HOST:-localhost}}
  25. DATABASE_ENCODING=${HUGINN_DATABASE_ENCODING:-${DATABASE_ENCODING}}
  26. USE_GRAPHVIZ_DOT=${HUGINN_USE_GRAPHVIZ_DOT:-${USE_GRAPHVIZ_DOT}}
  27. # use default port number if it is still not set
  28. case "${DATABASE_ADAPTER}" in
  29. mysql2) DATABASE_PORT=${DATABASE_PORT:-3306} ;;
  30. postgres) DATABASE_PORT=${DATABASE_PORT:-5432} ;;
  31. *) echo "Unsupported database adapter. Available adapters are mysql2, and postgres." && exit 1 ;;
  32. esac
  33. # start supervisord
  34. /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
  35. # start mysql server if ${DATABASE_HOST} is localhost
  36. if [ "${DATABASE_HOST}" == "localhost" ]; then
  37. if [ "${DATABASE_ADAPTER}" == "postgres" ]; then
  38. echo "DATABASE_ADAPTER 'postgres' is not supported internally. Please provide DATABASE_HOST."
  39. exit 1
  40. fi
  41. # configure supervisord to start mysql (manual)
  42. cat > /etc/supervisor/conf.d/mysqld.conf <<EOF
  43. [program:mysqld]
  44. priority=20
  45. directory=/tmp
  46. command=/usr/bin/mysqld_safe
  47. user=root
  48. autostart=false
  49. autorestart=true
  50. stdout_logfile=/var/log/supervisor/%(program_name)s.log
  51. stderr_logfile=/var/log/supervisor/%(program_name)s.log
  52. EOF
  53. supervisorctl reload
  54. # fix permissions and ownership of /var/lib/mysql
  55. chown -R mysql:mysql /var/lib/mysql
  56. chmod 700 /var/lib/mysql
  57. # initialize MySQL data directory
  58. if [ ! -d /var/lib/mysql/mysql ]; then
  59. mysql_install_db --user=mysql
  60. fi
  61. echo "Starting mysql server..."
  62. supervisorctl start mysqld >/dev/null
  63. # wait for mysql server to start (max 120 seconds)
  64. timeout=120
  65. while ! mysqladmin -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} status >/dev/null 2>&1
  66. do
  67. timeout=$(expr $timeout - 1)
  68. if [ $timeout -eq 0 ]; then
  69. echo "Failed to start mysql server"
  70. exit 1
  71. fi
  72. sleep 1
  73. done
  74. if ! echo "USE ${DATABASE_NAME}" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} >/dev/null 2>&1; then
  75. DB_INIT="yes"
  76. echo "CREATE DATABASE IF NOT EXISTS \`${DATABASE_NAME}\` DEFAULT CHARACTER SET \`utf8\` COLLATE \`utf8_unicode_ci\`;" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD}
  77. 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}
  78. fi
  79. fi
  80. # Assuming we have a created database, run the migrations and seed it idempotently.
  81. [ -z "${DO_NOT_MIGRATE}" ] && sudo -u huginn -EH bundle exec rake db:migrate
  82. [ -z "${DO_NOT_SEED}" ] && sudo -u huginn -EH bundle exec rake db:seed
  83. [ -n "$INTENTIONALLY_SLEEP" ] && sleep $INTENTIONALLY_SLEEP
  84. # Fixup the Procfile and prepare the PORT
  85. [ -z "${DO_NOT_RUN_JOBS}" ] && perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
  86. perl -pi -e 's/rails server$/rails server -p \$PORT/' /app/Procfile
  87. export PORT
  88. # Start huginn
  89. sudo -u huginn -EH bundle exec foreman start
  90. # As the ENTRYPOINT script, when this exits the docker container will Exit.
  91. exit 0