docker-entrypoint.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env bash
  2. set -e
  3. : "${AUTOWIZARD_JSON:=''}"
  4. : "${AUTOWIZARD_RELATIVE_PATH:='tmp/auto_wizard.json'}"
  5. : "${ELASTICSEARCH_ENABLED:=true}"
  6. : "${ELASTICSEARCH_HOST:=zammad-elasticsearch}"
  7. : "${ELASTICSEARCH_PORT:=9200}"
  8. : "${ELASTICSEARCH_SCHEMA:=http}"
  9. : "${ELASTICSEARCH_NAMESPACE:=zammad}"
  10. : "${ELASTICSEARCH_REINDEX:=true}"
  11. : "${ELASTICSEARCH_SSL_VERIFY:=true}"
  12. : "${NGINX_PORT:=8080}"
  13. : "${NGINX_SERVER_NAME:=_}"
  14. : "${NGINX_SERVER_SCHEME:=\$scheme}"
  15. : "${POSTGRESQL_DB:=zammad_production}"
  16. : "${POSTGRESQL_DB_CREATE:=true}"
  17. : "${POSTGRESQL_HOST:=zammad-postgresql}"
  18. : "${POSTGRESQL_PORT:=5432}"
  19. : "${POSTGRESQL_USER:=zammad}"
  20. : "${POSTGRESQL_PASS:=zammad}"
  21. : "${POSTGRESQL_OPTIONS:=}"
  22. : "${RAILS_ENV:=production}"
  23. : "${RAILS_LOG_TO_STDOUT:=true}"
  24. : "${RAILS_TRUSTED_PROXIES:=127.0.0.1,::1}"
  25. : "${ZAMMAD_DIR:=/opt/zammad}"
  26. : "${ZAMMAD_RAILSSERVER_HOST:=zammad-railsserver}"
  27. : "${ZAMMAD_RAILSSERVER_PORT:=3000}"
  28. : "${ZAMMAD_WEBSOCKET_HOST:=zammad-websocket}"
  29. : "${ZAMMAD_WEBSOCKET_PORT:=6042}"
  30. : "${ZAMMAD_WEB_CONCURRENCY:=0}"
  31. ESCAPED_POSTGRESQL_PASS=$(echo "$POSTGRESQL_PASS" | sed -e 's/[\/&]/\\&/g')
  32. export DATABASE_URL="postgres://${POSTGRESQL_USER}:${ESCAPED_POSTGRESQL_PASS}@${POSTGRESQL_HOST}:${POSTGRESQL_PORT}/${POSTGRESQL_DB}${POSTGRESQL_OPTIONS}"
  33. function check_zammad_ready {
  34. # Verify that migrations have been ran and seeds executed to process ENV vars like FQDN correctly.
  35. until bundle exec rails r 'ActiveRecord::Migration.check_pending!; Locale.any? || raise' &> /dev/null; do
  36. echo "waiting for init container to finish install or update..."
  37. sleep 5
  38. done
  39. }
  40. # zammad init
  41. if [ "$1" = 'zammad-init' ]; then
  42. # install / update zammad
  43. until (echo > /dev/tcp/"${POSTGRESQL_HOST}"/"${POSTGRESQL_PORT}") &> /dev/null; do
  44. echo "waiting for postgresql server to be ready..."
  45. sleep 5
  46. done
  47. # check if database exists / update to new version
  48. echo "initialising / updating database..."
  49. if ! (bundle exec rails r 'puts User.any?' 2> /dev/null | grep -q true); then
  50. if [ "${POSTGRESQL_DB_CREATE}" == "true" ]; then
  51. bundle exec rake db:create
  52. fi
  53. bundle exec rake db:migrate
  54. bundle exec rake db:seed
  55. # create autowizard.json on first install
  56. if base64 -d <<< "${AUTOWIZARD_JSON}" &>> /dev/null; then
  57. echo "Saving autowizard json payload..."
  58. base64 -d <<< "${AUTOWIZARD_JSON}" > "${AUTOWIZARD_RELATIVE_PATH}"
  59. fi
  60. else
  61. echo Clearing cache...
  62. bundle exec rails r "Rails.cache.clear"
  63. echo Executing migrations...
  64. bundle exec rake db:migrate
  65. echo Synchronizing locales...
  66. bundle exec rails r "Locale.sync"
  67. echo Synchronizing translations...
  68. bundle exec rails r "Translation.sync"
  69. fi
  70. # es config
  71. echo "changing settings..."
  72. if [ "${ELASTICSEARCH_ENABLED}" == "false" ]; then
  73. bundle exec rails r "Setting.set('es_url', '')"
  74. else
  75. bundle exec rails r "Setting.set('es_url', '${ELASTICSEARCH_SCHEMA}://${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}')"
  76. bundle exec rails r "Setting.set('es_index', '${ELASTICSEARCH_NAMESPACE}')"
  77. if [ -n "${ELASTICSEARCH_USER}" ] && [ -n "${ELASTICSEARCH_PASS}" ]; then
  78. bundle exec rails r "Setting.set('es_user', \"${ELASTICSEARCH_USER}\")"
  79. bundle exec rails r "Setting.set('es_password', \"${ELASTICSEARCH_PASS}\")"
  80. fi
  81. until (echo > /dev/tcp/"${ELASTICSEARCH_HOST}/${ELASTICSEARCH_PORT}") &> /dev/null; do
  82. echo "zammad railsserver waiting for elasticsearch server to be ready..."
  83. sleep 5
  84. done
  85. if [ "${ELASTICSEARCH_SSL_VERIFY}" == "false" ]; then
  86. SSL_SKIP_VERIFY="-k"
  87. else
  88. SSL_SKIP_VERIFY=""
  89. fi
  90. if [ "${ELASTICSEARCH_REINDEX}" == "true" ]; then
  91. if ! curl -s "${SSL_SKIP_VERIFY}" "${ELASTICSEARCH_SCHEMA}://${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}/_cat/indices" | grep -q zammad; then
  92. echo "rebuilding es searchindex..."
  93. bundle exec rake zammad:searchindex:rebuild
  94. fi
  95. fi
  96. fi
  97. # zammad nginx
  98. elif [ "$1" = 'zammad-nginx' ]; then
  99. check_zammad_ready
  100. # configure nginx
  101. sed -e "s#\(listen\)\(.*\)80#\1\2${NGINX_PORT}#g" \
  102. -e "s#proxy_set_header X-Forwarded-Proto .*;#proxy_set_header X-Forwarded-Proto ${NGINX_SERVER_SCHEME};#g" \
  103. -e "s#server .*:3000#server ${ZAMMAD_RAILSSERVER_HOST}:${ZAMMAD_RAILSSERVER_PORT}#g" \
  104. -e "s#server .*:6042#server ${ZAMMAD_WEBSOCKET_HOST}:${ZAMMAD_WEBSOCKET_PORT}#g" \
  105. -e "s#server_name .*#server_name ${NGINX_SERVER_NAME};#g" \
  106. -e 's#/var/log/nginx/zammad.\(access\|error\).log#/dev/stdout#g' < contrib/nginx/zammad.conf > /etc/nginx/sites-enabled/default
  107. echo "starting nginx..."
  108. exec /usr/sbin/nginx -g 'daemon off;'
  109. # zammad-railsserver
  110. elif [ "$1" = 'zammad-railsserver' ]; then
  111. check_zammad_ready
  112. echo "starting railsserver... with WEB_CONCURRENCY=${ZAMMAD_WEB_CONCURRENCY}"
  113. #shellcheck disable=SC2101
  114. exec bundle exec puma -b tcp://[::]:"${ZAMMAD_RAILSSERVER_PORT}" -w "${ZAMMAD_WEB_CONCURRENCY}" -e "${RAILS_ENV}"
  115. # zammad-scheduler
  116. elif [ "$1" = 'zammad-scheduler' ]; then
  117. check_zammad_ready
  118. echo "starting background services..."
  119. exec bundle exec script/background-worker.rb start
  120. # zammad-websocket
  121. elif [ "$1" = 'zammad-websocket' ]; then
  122. check_zammad_ready
  123. echo "starting websocket server..."
  124. exec bundle exec script/websocket-server.rb -b 0.0.0.0 -p "${ZAMMAD_WEBSOCKET_PORT}" start
  125. # zammad-backup
  126. elif [ "$1" = 'zammad-backup' ]; then
  127. check_zammad_ready
  128. echo "starting backup..."
  129. exec contrib/docker/backup.sh
  130. # Pass all other container commands to shell
  131. else
  132. exec "$@"
  133. fi