init 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/^#\([^ ]\)/\1/' | 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. # initialize supervisord config
  34. cat > /etc/supervisor/conf.d/supervisord.conf <<EOF
  35. [supervisord]
  36. nodaemon = true
  37. user = root
  38. stdout_logfile = AUTO
  39. [unix_http_server]
  40. file=/var/run/supervisor.sock ; (the path to the socket file)
  41. [eventlistener:stdout]
  42. command = supervisor_stdout
  43. buffer_size = 100
  44. events = PROCESS_LOG
  45. result_handler = supervisor_stdout:event_handler
  46. EOF
  47. # prepare the supervisord bootstrap service script
  48. cat <<BOOTSTRAP > /tmp/bootstrap.sh
  49. #!/bin/bash -e
  50. source /app/.env
  51. echo DATABASE_HOST=\${DATABASE_HOST}
  52. # start mysql server if \${DATABASE_HOST} is the .env.example default
  53. if [ "\${DATABASE_HOST}" = "your-domain-here.com" -o "\${DATABASE_HOST}" = "" ]; then
  54. if [ "\${DATABASE_ADAPTER}" = "postgres" ]; then
  55. echo "DATABASE_ADAPTER 'postgres' is not supported internally. Please provide DATABASE_HOST."
  56. exit 1
  57. fi
  58. # configure supervisord to start mysql (manual)
  59. cat > /etc/supervisor/conf.d/mysqld.conf <<EOF
  60. [program:mysqld]
  61. priority=20
  62. directory=/tmp
  63. command=/usr/bin/mysqld_safe
  64. user=root
  65. autostart=false
  66. autorestart=true
  67. stdout_events_enabled=true
  68. stderr_events_enabled=true
  69. EOF
  70. supervisorctl reread
  71. supervisorctl add mysqld
  72. # fix permissions and ownership of /var/lib/mysql
  73. chown -R mysql:mysql /var/lib/mysql
  74. chmod 700 /var/lib/mysql
  75. # initialize MySQL data directory
  76. if [ ! -d /var/lib/mysql/mysql ]; then
  77. mysql_install_db --user=mysql
  78. fi
  79. echo "Starting mysql server..."
  80. supervisorctl start mysqld >/dev/null
  81. # wait for mysql server to start (max 120 seconds)
  82. timeout=120
  83. while ! mysqladmin -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} status >/dev/null 2>&1
  84. do
  85. (( timeout = timeout - 1 ))
  86. if [ \$timeout -eq 0 ]; then
  87. echo "Failed to start mysql server"
  88. exit 1
  89. fi
  90. echo -n .
  91. sleep 1
  92. done
  93. if ! echo "USE \${DATABASE_NAME}" | mysql -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} >/dev/null 2>&1; then
  94. DB_INIT="yes"
  95. echo "CREATE DATABASE IF NOT EXISTS \\\`\${DATABASE_NAME}\\\` DEFAULT CHARACTER SET \\\`utf8\\\` COLLATE \\\`utf8_unicode_ci\\\`;" | mysql -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD}
  96. 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}
  97. fi
  98. fi
  99. supervisorctl start foreman >/dev/null
  100. BOOTSTRAP
  101. chmod 755 /tmp/bootstrap.sh
  102. cat > /etc/supervisor/conf.d/bootstrap.conf <<EOF
  103. [program:bootstrap]
  104. command=/tmp/bootstrap.sh
  105. priority=10
  106. directory=/app
  107. process_name=%(program_name)s
  108. autostart=true
  109. autorestart=false
  110. stdout_events_enabled=true
  111. stderr_events_enabled=true
  112. stopsignal=TERM
  113. exitcodes=0
  114. startsecs=0
  115. stopwaitsecs=1
  116. EOF
  117. cat <<FOREMAN > /tmp/foreman.sh
  118. #!/bin/bash -e
  119. source /app/.env
  120. # Assuming we have a created database, run the migrations and seed it idempotently.
  121. [ -z "\${DO_NOT_MIGRATE}" ] && sudo -u huginn -EH bundle exec rake db:migrate
  122. [ -z "\${DO_NOT_SEED}" ] && sudo -u huginn -EH bundle exec rake db:seed
  123. [ -n "\$INTENTIONALLY_SLEEP" ] && sleep \$INTENTIONALLY_SLEEP
  124. # Fixup the Procfile and prepare the PORT
  125. [ -n "\${DO_NOT_RUN_JOBS}" ] && perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
  126. perl -pi -e 's/rails server\$/rails server -b 0.0.0.0 -p \\\$PORT/' /app/Procfile
  127. export PORT
  128. # Start huginn
  129. exec sudo -u huginn -EH bundle exec foreman start
  130. FOREMAN
  131. chmod 755 /tmp/foreman.sh
  132. cat > /etc/supervisor/conf.d/foreman.conf <<EOF
  133. [program:foreman]
  134. command=/tmp/foreman.sh
  135. priority=10
  136. directory=/app
  137. process_name=%(program_name)s
  138. autostart=false
  139. autorestart=false
  140. stdout_events_enabled=true
  141. stderr_events_enabled=true
  142. stopsignal=TERM
  143. startsecs=0
  144. stopwaitsecs=1
  145. EOF
  146. # start supervisord
  147. exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf