Няма описание http://j1x-huginn.herokuapp.com

init 7.3KB

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