init 6.2 KB

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