init 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. # Cleanup any leftover pid file
  7. if [ -f /app/tmp/pids/server.pid ]; then
  8. rm /app/tmp/pids/server.pid
  9. fi
  10. # is a mysql or postgresql database linked?
  11. # requires that the mysql or postgresql containers have exposed
  12. # port 3306 and 5432 respectively.
  13. if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ]; then
  14. HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-mysql2}
  15. HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${MYSQL_PORT_3306_TCP_ADDR}}
  16. HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${MYSQL_PORT_3306_TCP_PORT}}
  17. elif [ -n "${POSTGRESQL_PORT_5432_TCP_ADDR}" ]; then
  18. HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-postgres}
  19. HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${POSTGRESQL_PORT_5432_TCP_ADDR}}
  20. HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${POSTGRESQL_PORT_5432_TCP_PORT}}
  21. fi
  22. grep = /app/.env.example | sed -e 's/^#\([^ ]\)/\1/' | grep -v -e '^#' | cut -d= -f1 | \
  23. while read var ; do
  24. eval "echo \"$var=\\\"\${HUGINN_$var:-\$$var}\\\"\""
  25. done | grep -v -e ^= > /app/.env
  26. chmod ugo+r /app/.env
  27. source /app/.env
  28. DATABASE_HOST=${HUGINN_DATABASE_HOST:-${DATABASE_HOST:-localhost}}
  29. DATABASE_ENCODING=${HUGINN_DATABASE_ENCODING:-${DATABASE_ENCODING}}
  30. USE_GRAPHVIZ_DOT=${HUGINN_USE_GRAPHVIZ_DOT:-${USE_GRAPHVIZ_DOT}}
  31. # use default port number if it is still not set
  32. case "${DATABASE_ADAPTER}" in
  33. mysql2) DATABASE_PORT=${DATABASE_PORT:-3306} ;;
  34. postgres) DATABASE_PORT=${DATABASE_PORT:-5432} ;;
  35. *) echo "Unsupported database adapter. Available adapters are mysql2, and postgres." && exit 1 ;;
  36. esac
  37. # initialize supervisord config
  38. cat > /etc/supervisor/conf.d/supervisord.conf <<EOF
  39. [supervisord]
  40. nodaemon = true
  41. user = root
  42. stdout_logfile = AUTO
  43. [unix_http_server]
  44. file=/var/run/supervisor.sock ; (the path to the socket file)
  45. [eventlistener:stdout]
  46. command = supervisor_stdout
  47. buffer_size = 100
  48. events = PROCESS_LOG
  49. result_handler = supervisor_stdout:event_handler
  50. EOF
  51. # prepare the supervisord bootstrap service script
  52. cat <<BOOTSTRAP > /tmp/bootstrap.sh
  53. #!/bin/bash -e
  54. source /app/.env
  55. echo DATABASE_HOST=\${DATABASE_HOST}
  56. # start mysql server if \${DATABASE_HOST} is the .env.example default
  57. if [ "\${DATABASE_HOST}" = "your-domain-here.com" -o "\${DATABASE_HOST}" = "" ]; then
  58. if [ "\${DATABASE_ADAPTER}" = "postgres" ]; then
  59. echo "DATABASE_ADAPTER 'postgres' is not supported internally. Please provide DATABASE_HOST."
  60. exit 1
  61. fi
  62. # configure supervisord to start mysql (manual)
  63. cat > /etc/supervisor/conf.d/mysqld.conf <<EOF
  64. [program:mysqld]
  65. priority=20
  66. directory=/tmp
  67. command=/usr/bin/mysqld_safe
  68. user=root
  69. autostart=false
  70. autorestart=true
  71. stdout_events_enabled=true
  72. stderr_events_enabled=true
  73. EOF
  74. supervisorctl reread
  75. supervisorctl add mysqld
  76. # fix permissions and ownership of /var/lib/mysql
  77. chown -R mysql:mysql /var/lib/mysql
  78. chmod 700 /var/lib/mysql
  79. # initialize MySQL data directory
  80. if [ ! -d /var/lib/mysql/mysql ]; then
  81. mysql_install_db --user=mysql
  82. fi
  83. echo "Starting mysql server..."
  84. supervisorctl start mysqld >/dev/null
  85. # wait for mysql server to start (max 120 seconds)
  86. timeout=120
  87. while ! mysqladmin -u\${DATABASE_USERNAME:-root} \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} status >/dev/null 2>&1
  88. do
  89. (( timeout = timeout - 1 ))
  90. if [ \$timeout -eq 0 ]; then
  91. echo "Failed to start mysql server"
  92. exit 1
  93. fi
  94. echo -n .
  95. sleep 1
  96. done
  97. if ! echo "USE \${DATABASE_NAME}" | mysql -u\${DATABASE_USERNAME:-root} \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} >/dev/null 2>&1; then
  98. DB_INIT="yes"
  99. echo "CREATE DATABASE IF NOT EXISTS \\\`\${DATABASE_NAME}\\\` DEFAULT CHARACTER SET \\\`utf8\\\` COLLATE \\\`utf8_unicode_ci\\\`;" | mysql -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD}
  100. echo "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON \\\`${DATABASE_NAME}\\\`.* TO 'root'@'localhost';" | mysql -u\${DATABASE_USERNAME:-root} \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD}
  101. fi
  102. fi
  103. supervisorctl start foreman >/dev/null
  104. BOOTSTRAP
  105. chmod 755 /tmp/bootstrap.sh
  106. cat > /etc/supervisor/conf.d/bootstrap.conf <<EOF
  107. [program:bootstrap]
  108. command=/tmp/bootstrap.sh
  109. priority=10
  110. directory=/app
  111. process_name=%(program_name)s
  112. autostart=true
  113. autorestart=false
  114. stdout_events_enabled=true
  115. stderr_events_enabled=true
  116. stopsignal=TERM
  117. exitcodes=0
  118. startsecs=0
  119. stopwaitsecs=1
  120. EOF
  121. cat <<FOREMAN > /tmp/foreman.sh
  122. #!/bin/bash -e
  123. source /app/.env
  124. # The database may need to start up for a bit first
  125. if [ -n "\${INTENTIONALLY_SLEEP}" ]; then
  126. echo "Intentionally sleeping \${INTENTIONALLY_SLEEP}"
  127. sleep \${INTENTIONALLY_SLEEP}
  128. fi
  129. if [ -n "\${DATABASE_INITIAL_CONNECT_MAX_RETRIES}" ]; then
  130. max=\${DATABASE_INITIAL_CONNECT_MAX_RETRIES}
  131. count=0
  132. while ! rake database_test:ping > /dev/null 2>&1 && [[ \$count -le \$max ]] ; do
  133. count=\$[\$count+1]
  134. echo "Retry \$count of \$max attempting to connect to \$DATABASE_HOST. Sleeping \${DATABASE_INITIAL_CONNECT_SLEEP:5}"
  135. sleep \${DATABASE_INITIAL_CONNECT_SLEEP:5}
  136. done
  137. fi
  138. # We may need to try and create a database
  139. if [ -n "\${CREATE_DB}" ]; then
  140. sudo -u huginn -EH bundle exec rake db:create
  141. fi
  142. # Assuming we have a created database, run the migrations and seed it idempotently.
  143. if [ -z "\${DO_NOT_MIGRATE}" ]; then
  144. sudo -u huginn -EH bundle exec rake db:migrate
  145. fi
  146. if [ -z "\${DO_NOT_SEED}" ]; then
  147. sudo -u huginn -EH bash -xc 'bundle exec rake db:seed &'
  148. fi
  149. # Fixup the Procfile and prepare the PORT
  150. if [ -n "\${DO_NOT_RUN_JOBS}" ]; then
  151. perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
  152. fi
  153. perl -pi -e 's/rails server -b0.0.0.0\$/rails server -b 0.0.0.0 -p \\\$PORT/' /app/Procfile
  154. export PORT="\${PORT:-3000}"
  155. # Start huginn
  156. exec sudo -u huginn -EH bash -xc 'exec bundle exec foreman start'
  157. FOREMAN
  158. chmod 755 /tmp/foreman.sh
  159. cat > /etc/supervisor/conf.d/foreman.conf <<EOF
  160. [program:foreman]
  161. command=/tmp/foreman.sh
  162. priority=10
  163. directory=/app
  164. process_name=%(program_name)s
  165. autostart=false
  166. autorestart=false
  167. stdout_events_enabled=true
  168. stderr_events_enabled=true
  169. stopsignal=TERM
  170. startsecs=0
  171. stopwaitsecs=1
  172. EOF
  173. # start supervisord
  174. exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf