run.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash
  2. export FQDN
  3. export DOMAIN
  4. export VMAILUID
  5. export VMAILGID
  6. export VMAIL_SUBDIR
  7. export DBDRIVER
  8. export DBHOST
  9. export DBPORT
  10. export DBNAME
  11. export DBUSER
  12. export REDIS_HOST
  13. export REDIS_PORT
  14. export REDIS_PASS
  15. export REDIS_NUMB
  16. export DISABLE_CLAMAV
  17. export DISABLE_DNS_RESOLVER
  18. FQDN=${FQDN:-$(hostname --fqdn)}
  19. DOMAIN=${DOMAIN:-$(hostname --domain)}
  20. VMAILUID=${VMAILUID:-1024}
  21. VMAILGID=${VMAILGID:-1024}
  22. VMAIL_SUBDIR=${VMAIL_SUBDIR:-"mail"}
  23. DBDRIVER=${DBDRIVER:-mysql}
  24. DBHOST=${DBHOST:-mariadb}
  25. DBNAME=${DBNAME:-postfix}
  26. DBUSER=${DBUSER:-postfix}
  27. if [ "$DBDRIVER" = "ldap" ]; then
  28. DBPORT=${DBPORT:-389}
  29. else
  30. DBPORT=${DBPORT:-3306}
  31. fi
  32. REDIS_HOST=${REDIS_HOST:-redis}
  33. REDIS_PORT=${REDIS_PORT:-6379}
  34. REDIS_PASS=$([ -f "$REDIS_PASS" ] && cat "$REDIS_PASS" || echo "${REDIS_PASS:-}")
  35. REDIS_NUMB=${REDIS_NUMB:-0}
  36. DISABLE_CLAMAV=${DISABLE_CLAMAV:-false} # --
  37. DISABLE_DNS_RESOLVER=${DISABLE_DNS_RESOLVER:-false} # --
  38. if [ "$DBDRIVER" = "ldap" ]; then
  39. export LDAP_BIND
  40. export LDAP_BIND_DN
  41. export LDAP_BIND_PW
  42. LDAP_BIND=${LDAP_BIND:-true}
  43. LDAP_BIND_DN=${LDAP_BIND_DN:-}
  44. LDAP_BIND_PW=$([ -f "$LDAP_BIND_PW" ] && cat "$LDAP_BIND_PW" || echo "${LDAP_BIND_PW:-}")
  45. if [ "$LDAP_BIND" = true ]; then
  46. if [ -z "$LDAP_BIND_DN" ]; then
  47. echo "[ERROR] LDAP_BIND_ED must be set !"
  48. exit 1
  49. fi
  50. if [ -z "$LDAP_BIND_PW" ]; then
  51. echo "[ERROR] LDAP_BIND_PW must be set !"
  52. exit 1
  53. fi
  54. fi
  55. else
  56. if [ -z "$DBPASS" ]; then
  57. echo "[ERROR] MariaDB/PostgreSQL database password must be set !"
  58. exit 1
  59. fi
  60. fi
  61. if [ -z "$RSPAMD_PASSWORD" ]; then
  62. echo "[ERROR] Rspamd password must be set !"
  63. exit 1
  64. fi
  65. if [ -z "$FQDN" ]; then
  66. echo "[ERROR] The fully qualified domain name must be set !"
  67. exit 1
  68. fi
  69. if [ -z "$DOMAIN" ]; then
  70. echo "[ERROR] The domain name must be set !"
  71. exit 1
  72. fi
  73. # https://github.com/docker-library/redis/issues/53
  74. if [[ "$REDIS_PORT" =~ [^[:digit:]] ]]; then
  75. REDIS_PORT=6379
  76. fi
  77. # DATABASES HOSTNAME CHECKING
  78. # We need to set these in the hosts file before Unbound takes over for DNS
  79. # ---------------------------------------------------------------------------------------------
  80. # Check mariadb/postgres hostname
  81. grep -q "${DBHOST}" /etc/hosts
  82. if [ $? -ne 0 ]; then
  83. echo "[INFO] MariaDB/PostgreSQL hostname not found in /etc/hosts"
  84. IP=$(dig A ${DBHOST} +short +search)
  85. if [ -n "$IP" ]; then
  86. echo "[INFO] Container IP found, adding a new record in /etc/hosts"
  87. echo "${IP} ${DBHOST}" >> /etc/hosts
  88. else
  89. echo "[ERROR] Container IP not found with embedded DNS server... Abort !"
  90. echo "[ERROR] Check your DBHOST environment variable"
  91. exit 1
  92. fi
  93. else
  94. echo "[INFO] MariaDB/PostgreSQL hostname found in /etc/hosts"
  95. fi
  96. # Check redis hostname
  97. grep -q "${REDIS_HOST}" /etc/hosts
  98. if [ $? -ne 0 ]; then
  99. echo "[INFO] Redis hostname not found in /etc/hosts"
  100. IP=$(dig A ${REDIS_HOST} +short +search)
  101. if [ -n "$IP" ]; then
  102. echo "[INFO] Container IP found, adding a new record in /etc/hosts"
  103. echo "${IP} ${REDIS_HOST}" >> /etc/hosts
  104. else
  105. echo "[ERROR] Container IP not found with embedded DNS server... Abort !"
  106. echo "[ERROR] Check your REDIS_HOST environment variable"
  107. exit 1
  108. fi
  109. else
  110. echo "[INFO] Redis hostname found in /etc/hosts"
  111. fi
  112. # SETUP CONFIG FILES
  113. # ---------------------------------------------------------------------------------------------
  114. certs_helper.sh update_certs
  115. # Make sure that configuration is only run once
  116. if [ ! -f "/etc/configuration_built" ]; then
  117. touch "/etc/configuration_built"
  118. setup.sh
  119. fi
  120. # Unrecoverable errors detection
  121. if [ -f "/etc/setup-error" ]; then
  122. echo "[ERROR] One or more unrecoverable errors have occurred during initial setup. See above to find the cause."
  123. exit 1
  124. fi
  125. # LAUNCH ALL SERVICES
  126. # ---------------------------------------------------------------------------------------------
  127. echo "[INFO] Starting services"
  128. exec s6-svscan /services