init 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 --without test development --path vendor/bundle
  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/supervisord.conf <<EOF
  51. [unix_http_server]
  52. file=/dev/shm/supervisor.sock ; (the path to the socket file)
  53. chmod=0700 ; sockef file mode (default 0700)
  54. [supervisord]
  55. nodaemon = true
  56. user = root
  57. stdout_logfile = AUTO
  58. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  59. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  60. childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
  61. ; the below section must remain in the config file for RPC
  62. ; (supervisorctl/web interface) to work, additional interfaces may be
  63. ; added by defining them in separate rpcinterface: sections
  64. [rpcinterface:supervisor]
  65. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  66. [supervisorctl]
  67. serverurl=unix:///dev/shm/supervisor.sock ; use a unix:// URL for a unix socket
  68. [eventlistener:stdout]
  69. command = supervisor_stdout
  70. buffer_size = 100
  71. events = PROCESS_LOG
  72. result_handler = supervisor_stdout:event_handler
  73. ; The [include] section can just contain the "files" setting. This
  74. ; setting can list multiple files (separated by whitespace or
  75. ; newlines). It can also contain wildcards. The filenames are
  76. ; interpreted as relative to this file. Included files *cannot*
  77. ; include files themselves.
  78. [include]
  79. files = /etc/supervisor/conf.d/*.conf
  80. EOF
  81. # prepare the supervisord bootstrap service script
  82. cat <<BOOTSTRAP > /tmp/bootstrap.sh
  83. #!/bin/bash -e
  84. source /app/.env
  85. echo DATABASE_HOST=\${DATABASE_HOST}
  86. # start mysql server if \${DATABASE_HOST} is the .env.example default
  87. if [ "\${START_MYSQL}" = "true" ]; then
  88. echo "DATABASE_SOCKET=/var/run/mysqld/mysqld.sock" >> .env
  89. if [ "\${DATABASE_ADAPTER}" = "postgresql" ]; then
  90. echo "DATABASE_ADAPTER 'postgresql' is not supported internally. Please provide DATABASE_HOST."
  91. exit 1
  92. fi
  93. # configure supervisord to start mysql (manual)
  94. cat > /etc/supervisor/conf.d/mysqld.conf <<EOF
  95. [program:mysqld]
  96. priority=20
  97. directory=/tmp
  98. command=/usr/bin/mysqld_safe
  99. user=root
  100. autostart=false
  101. autorestart=true
  102. stdout_events_enabled=true
  103. stderr_events_enabled=true
  104. EOF
  105. supervisorctl reread
  106. supervisorctl add mysqld
  107. # fix permissions and ownership of /var/lib/mysql
  108. chown -R mysql:mysql /var/lib/mysql
  109. chmod 700 /var/lib/mysql
  110. # initialize MySQL data directory
  111. if [ ! -d /var/lib/mysql/mysql ]; then
  112. mysql_install_db --user=mysql
  113. fi
  114. echo "Starting mysql server..."
  115. supervisorctl start mysqld >/dev/null
  116. # wait for mysql server to start (max 120 seconds)
  117. timeout=120
  118. while ! mysqladmin -u root status >/dev/null 2>&1 && ! mysqladmin -u root --password=\${DATABASE_PASSWORD} status >/dev/null 2>&1
  119. do
  120. (( timeout = timeout - 1 ))
  121. if [ \$timeout -eq 0 ]; then
  122. echo "Failed to start mysql server"
  123. exit 1
  124. fi
  125. echo -n .
  126. sleep 1
  127. done
  128. if ! echo "USE \${DATABASE_NAME}" | mysql -u\${DATABASE_USERNAME:-root} \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} >/dev/null 2>&1; then
  129. echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('\${DATABASE_PASSWORD:\$DATABASE_PASSWORD}');" | mysql -u root
  130. fi
  131. fi
  132. supervisorctl start foreman >/dev/null
  133. BOOTSTRAP
  134. chmod 755 /tmp/bootstrap.sh
  135. cat > /etc/supervisor/conf.d/bootstrap.conf <<EOF
  136. [program:bootstrap]
  137. command=/tmp/bootstrap.sh
  138. priority=10
  139. directory=/app
  140. process_name=%(program_name)s
  141. autostart=true
  142. autorestart=false
  143. stdout_events_enabled=true
  144. stderr_events_enabled=true
  145. stopsignal=TERM
  146. exitcodes=0
  147. startsecs=0
  148. stopwaitsecs=1
  149. EOF
  150. cat <<FOREMAN > /tmp/foreman.sh
  151. #!/bin/bash -e
  152. source /app/.env
  153. # The database may need to start up for a bit first
  154. if [ -n "\${INTENTIONALLY_SLEEP}" ]; then
  155. echo "Intentionally sleeping \${INTENTIONALLY_SLEEP}"
  156. sleep \${INTENTIONALLY_SLEEP}
  157. fi
  158. if [ -n "\${DATABASE_INITIAL_CONNECT_MAX_RETRIES}" ]; then
  159. max=\${DATABASE_INITIAL_CONNECT_MAX_RETRIES}
  160. count=0
  161. while ! rake database_test:ping > /dev/null 2>&1 && [[ \$count -le \$max ]] ; do
  162. count=\$[\$count+1]
  163. echo "Retry \$count of \$max attempting to connect to \$DATABASE_HOST. Sleeping \${DATABASE_INITIAL_CONNECT_SLEEP:5}"
  164. sleep \${DATABASE_INITIAL_CONNECT_SLEEP:5}
  165. done
  166. fi
  167. # We may need to try and create a database
  168. sudo -u huginn -EH bundle exec rake db:create
  169. # Assuming we have a created database, run the migrations and seed it idempotently.
  170. if [ -z "\${DO_NOT_MIGRATE}" ]; then
  171. sudo -u huginn -EH bundle exec rake db:migrate
  172. fi
  173. if [ -z "\${DO_NOT_SEED}" ]; then
  174. sudo -u huginn -EH bash -xc 'bundle exec rake db:seed &'
  175. fi
  176. # Fixup the Procfile and prepare the PORT
  177. if [ -n "\${DO_NOT_RUN_JOBS}" ]; then
  178. perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
  179. fi
  180. perl -pi -e 's/rails server -b0.0.0.0\$/rails server -b 0.0.0.0 -p \\\$PORT/' /app/Procfile
  181. export PORT="\${PORT:-3000}"
  182. # Start huginn
  183. exec sudo -u huginn -EH bash -xc 'exec bundle exec foreman start'
  184. FOREMAN
  185. chmod 755 /tmp/foreman.sh
  186. cat > /etc/supervisor/conf.d/foreman.conf <<EOF
  187. [program:foreman]
  188. command=/tmp/foreman.sh
  189. priority=10
  190. directory=/app
  191. process_name=%(program_name)s
  192. autostart=false
  193. autorestart=false
  194. stdout_events_enabled=true
  195. stderr_events_enabled=true
  196. stopsignal=TERM
  197. startsecs=0
  198. stopwaitsecs=1
  199. EOF
  200. # start supervisord
  201. exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf