docker-entrypoint.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env bash
  2. set -e
  3. : "${AUTOWIZARD_JSON:=''}"
  4. : "${AUTOWIZARD_RELATIVE_PATH:='var/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. : "${RAILS_ENV:=production}"
  22. : "${RAILS_LOG_TO_STDOUT:=true}"
  23. : "${RAILS_TRUSTED_PROXIES:=127.0.0.1,::1}"
  24. : "${RSYNC_ADDITIONAL_PARAMS:=--no-perms --no-owner}"
  25. : "${ZAMMAD_DIR:=/opt/zammad}"
  26. : "${ZAMMAD_RAILSSERVER_HOST:=zammad-railsserver}"
  27. : "${ZAMMAD_RAILSSERVER_PORT:=3000}"
  28. : "${ZAMMAD_READY_FILE:=${ZAMMAD_DIR}/var/zammad.ready}"
  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}"
  34. function check_zammad_ready {
  35. sleep 15
  36. until [ -f "${ZAMMAD_READY_FILE}" ]; do
  37. echo "waiting for init container to finish install or update..."
  38. sleep 10
  39. done
  40. }
  41. # zammad init
  42. if [ "$1" = 'zammad-init' ]; then
  43. # install / update zammad
  44. test -f "${ZAMMAD_READY_FILE}" && rm "${ZAMMAD_READY_FILE}"
  45. until (echo > /dev/tcp/"${POSTGRESQL_HOST}"/"${POSTGRESQL_PORT}") &> /dev/null; do
  46. echo "zammad railsserver waiting for postgresql server to be ready..."
  47. sleep 5
  48. done
  49. # check if database exists / update to new version
  50. echo "initialising / updating database..."
  51. if ! (bundle exec rails r 'puts User.any?' 2> /dev/null | grep -q true); then
  52. if [ "${POSTGRESQL_DB_CREATE}" == "true" ]; then
  53. bundle exec rake db:create
  54. fi
  55. bundle exec rake db:migrate
  56. bundle exec rake db:seed
  57. # create autowizard.json on first install
  58. if base64 -d <<< "${AUTOWIZARD_JSON}" &>> /dev/null; then
  59. echo "Saving autowizard json payload..."
  60. base64 -d <<< "${AUTOWIZARD_JSON}" > "${AUTOWIZARD_RELATIVE_PATH}"
  61. fi
  62. else
  63. bundle exec rails r "Rails.cache.clear"
  64. bundle exec rake db:migrate
  65. fi
  66. # es config
  67. echo "changing settings..."
  68. if [ "${ELASTICSEARCH_ENABLED}" == "false" ]; then
  69. bundle exec rails r "Setting.set('es_url', '')"
  70. else
  71. bundle exec rails r "Setting.set('es_url', '${ELASTICSEARCH_SCHEMA}://${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}')"
  72. bundle exec rails r "Setting.set('es_index', '${ELASTICSEARCH_NAMESPACE}')"
  73. if [ -n "${ELASTICSEARCH_USER}" ] && [ -n "${ELASTICSEARCH_PASS}" ]; then
  74. bundle exec rails r "Setting.set('es_user', \"${ELASTICSEARCH_USER}\")"
  75. bundle exec rails r "Setting.set('es_password', \"${ELASTICSEARCH_PASS}\")"
  76. fi
  77. until (echo > /dev/tcp/"${ELASTICSEARCH_HOST}/${ELASTICSEARCH_PORT}") &> /dev/null; do
  78. echo "zammad railsserver waiting for elasticsearch server to be ready..."
  79. sleep 5
  80. done
  81. if [ "${ELASTICSEARCH_SSL_VERIFY}" == "false" ]; then
  82. SSL_SKIP_VERIFY="-k"
  83. else
  84. SSL_SKIP_VERIFY=""
  85. fi
  86. if [ "${ELASTICSEARCH_REINDEX}" == "true" ]; then
  87. if ! curl -s "${SSL_SKIP_VERIFY}" "${ELASTICSEARCH_SCHEMA}://${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}/_cat/indices" | grep -q zammad; then
  88. echo "rebuilding es searchindex..."
  89. bundle exec rake zammad:searchindex:rebuild
  90. fi
  91. fi
  92. fi
  93. # create install ready file
  94. echo 'zammad-init' > "${ZAMMAD_READY_FILE}"
  95. # chown var
  96. chown -R zammad:zammad "${ZAMMAD_DIR}/var"
  97. # zammad nginx
  98. elif [ "$1" = 'zammad-nginx' ]; then
  99. check_zammad_ready
  100. # Make sure that also the nginx container has custom logo files.
  101. ZAMMAD_SAFE_MODE=1 bundle exec rails r "StaticAssets.sync"
  102. # configure nginx
  103. sed -e "s#\(listen\)\(.*\)80#\1\2${NGINX_PORT}#g" \
  104. -e "s#proxy_set_header X-Forwarded-Proto .*;#proxy_set_header X-Forwarded-Proto ${NGINX_SERVER_SCHEME};#g" \
  105. -e "s#server .*:3000#server ${ZAMMAD_RAILSSERVER_HOST}:${ZAMMAD_RAILSSERVER_PORT}#g" \
  106. -e "s#server .*:6042#server ${ZAMMAD_WEBSOCKET_HOST}:${ZAMMAD_WEBSOCKET_PORT}#g" \
  107. -e "s#server_name .*#server_name ${NGINX_SERVER_NAME};#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. # Pass all other container commands to shell
  128. else
  129. exec "$@"
  130. fi