init 5.9KB

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