docker-entrypoint.sh 5.6 KB

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