update.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/bash
  2. #exit on error and pipefail
  3. set -o pipefail
  4. for bin in curl docker-compose docker git awk sha1sum; do
  5. if [[ -z $(which ${bin}) ]]; then echo "Cannot find ${bin}, exiting..."; exit 1; fi
  6. done
  7. export LC_ALL=C
  8. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  9. BRANCH=$(git rev-parse --abbrev-ref HEAD)
  10. while (($#)); do
  11. case "${1}" in
  12. --check|-c)
  13. echo "Checking remote code for updates..."
  14. git fetch origin #${BRANCH}
  15. if [[ -z $(git log HEAD --pretty=format:"%H" | grep $(git rev-parse origin/${BRANCH})) ]]; then
  16. echo "Updated code is available."
  17. exit 0
  18. else
  19. echo "No updates available."
  20. exit 3
  21. fi
  22. ;;
  23. --ours)
  24. MERGE_STRATEGY=ours
  25. ;;
  26. esac
  27. done
  28. [[ ! -f mailcow.conf ]] && { echo "mailcow.conf is missing"; exit 1;}
  29. if grep --help 2>&1 | head -n 1 | grep -q -i "busybox"; then echo "BusybBox grep detected, please install gnu grep, \"apk add --no-cache --upgrade grep\""; exit 1; fi
  30. if cp --help 2>&1 | head -n 1 | grep -q -i "busybox"; then echo "BusybBox cp detected, please install coreutils, \"apk add --no-cache --upgrade coreutils\""; exit 1; fi
  31. CONFIG_ARRAY=(
  32. "SKIP_LETS_ENCRYPT"
  33. "USE_WATCHDOG"
  34. "WATCHDOG_NOTIFY_EMAIL"
  35. "SKIP_CLAMD"
  36. "SKIP_IP_CHECK"
  37. "ADDITIONAL_SAN"
  38. "DOVEADM_PORT"
  39. "IPV4_NETWORK"
  40. "IPV6_NETWORK"
  41. "LOG_LINES"
  42. "SNAT_TO_SOURCE"
  43. "SYSCTL_IPV6_DISABLED"
  44. "SQL_PORT"
  45. )
  46. sed -i '$a\' mailcow.conf
  47. for option in ${CONFIG_ARRAY[@]}; do
  48. if [[ ${option} == "ADDITIONAL_SAN" ]]; then
  49. if ! grep -q ${option} mailcow.conf; then
  50. echo "Adding new option \"${option}\" to mailcow.conf"
  51. echo "${option}=" >> mailcow.conf
  52. fi
  53. elif [[ ${option} == "SYSCTL_IPV6_DISABLED" ]]; then
  54. if ! grep -q ${option} mailcow.conf; then
  55. echo "Adding new option \"${option}\" to mailcow.conf"
  56. echo "# Disable IPv6" >> mailcow.conf
  57. echo "# mailcow-network will still be created as IPv6 enabled, all containers will be created" >> mailcow.conf
  58. echo "# without IPv6 support." >> mailcow.conf
  59. echo "# Use 1 for disabled, 0 for enabled" >> mailcow.conf
  60. echo "SYSCTL_IPV6_DISABLED=0" >> mailcow.conf
  61. fi
  62. elif [[ ${option} == "COMPOSE_PROJECT_NAME" ]]; then
  63. if ! grep -q ${option} mailcow.conf; then
  64. echo "Adding new option \"${option}\" to mailcow.conf"
  65. echo "COMPOSE_PROJECT_NAME=mailcowdockerized" >> mailcow.conf
  66. fi
  67. elif [[ ${option} == "DOVEADM_PORT" ]]; then
  68. if ! grep -q ${option} mailcow.conf; then
  69. echo "Adding new option \"${option}\" to mailcow.conf"
  70. echo "DOVEADM_PORT=127.0.0.1:19991" >> mailcow.conf
  71. fi
  72. elif [[ ${option} == "WATCHDOG_NOTIFY_EMAIL" ]]; then
  73. if ! grep -q ${option} mailcow.conf; then
  74. echo "Adding new option \"${option}\" to mailcow.conf"
  75. echo "WATCHDOG_NOTIFY_EMAIL=" >> mailcow.conf
  76. fi
  77. elif [[ ${option} == "LOG_LINES" ]]; then
  78. if ! grep -q ${option} mailcow.conf; then
  79. echo "Adding new option \"${option}\" to mailcow.conf"
  80. echo '# Max log lines per service to keep in Redis logs' >> mailcow.conf
  81. echo "LOG_LINES=9999" >> mailcow.conf
  82. fi
  83. elif [[ ${option} == "IPV4_NETWORK" ]]; then
  84. if ! grep -q ${option} mailcow.conf; then
  85. echo "Adding new option \"${option}\" to mailcow.conf"
  86. echo '# Internal IPv4 /24 subnet, format n.n.n. (expands to n.n.n.0/24)' >> mailcow.conf
  87. echo "IPV4_NETWORK=172.22.1" >> mailcow.conf
  88. fi
  89. elif [[ ${option} == "IPV6_NETWORK" ]]; then
  90. if ! grep -q ${option} mailcow.conf; then
  91. echo "Adding new option \"${option}\" to mailcow.conf"
  92. echo '# Internal IPv6 subnet in fc00::/7' >> mailcow.conf
  93. echo "IPV6_NETWORK=fd4d:6169:6c63:6f77::/64" >> mailcow.conf
  94. fi
  95. elif [[ ${option} == "SQL_PORT" ]]; then
  96. if ! grep -q ${option} mailcow.conf; then
  97. echo "Adding new option \"${option}\" to mailcow.conf"
  98. echo '# Bind SQL to 127.0.0.1 on port 13306' >> mailcow.conf
  99. echo "SQL_PORT=127.0.0.1:13306" >> mailcow.conf
  100. fi
  101. elif [[ ${option} == "SNAT_TO_SOURCE" ]]; then
  102. if ! grep -q ${option} mailcow.conf; then
  103. echo "Adding new option \"${option}\" to mailcow.conf"
  104. echo '# Use this IP for outgoing connections (SNAT)' >> mailcow.conf
  105. echo "#SNAT_TO_SOURCE=" >> mailcow.conf
  106. fi
  107. elif ! grep -q ${option} mailcow.conf; then
  108. echo "Adding new option \"${option}\" to mailcow.conf"
  109. echo "${option}=n" >> mailcow.conf
  110. fi
  111. done
  112. echo -en "Checking internet connection... "
  113. curl -o /dev/null google.com -sm3
  114. if [[ $? != 0 ]]; then
  115. echo -e "\e[31mfailed\e[0m"
  116. exit 1
  117. else
  118. echo -e "\e[32mOK\e[0m"
  119. fi
  120. echo -e "\e[32mChecking for newer update script...\e[0m"
  121. SHA1_1=$(sha1sum update.sh)
  122. git fetch origin #${BRANCH}
  123. git checkout origin/${BRANCH} update.sh
  124. SHA1_2=$(sha1sum update.sh)
  125. if [[ ${SHA1_1} != ${SHA1_2} ]]; then
  126. echo "update.sh changed, please run this script again, exiting."
  127. chmod +x update.sh
  128. exit 0
  129. fi
  130. if [[ -f mailcow.conf ]]; then
  131. source mailcow.conf
  132. else
  133. echo -e "\e[31mNo mailcow.conf - is mailcow installed?\e[0m"
  134. exit 1
  135. fi
  136. read -r -p "Are you sure you want to update mailcow: dockerized? All containers will be stopped. [y/N] " response
  137. if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  138. echo "OK, exiting."
  139. exit 0
  140. fi
  141. echo -e "Stopping mailcow... "
  142. sleep 2
  143. docker-compose down
  144. # Silently fixing remote url from andryyy to mailcow
  145. git remote set-url origin https://github.com/mailcow/mailcow-dockerized
  146. echo -e "\e[32mCommitting current status...\e[0m"
  147. git update-index --assume-unchanged data/conf/rspamd/override.d/worker-controller-password.inc
  148. git add -u
  149. git commit -am "Before update on ${DATE}" > /dev/null
  150. echo -e "\e[32mFetching updated code from remote...\e[0m"
  151. git fetch origin #${BRANCH}
  152. echo -e "\e[32mMerging local with remote code (recursive, strategy: \"${MERGE_STRATEGY:-theirs}\", options: \"patience\"...\e[0m"
  153. git config merge.defaultToUpstream true
  154. git merge -X${MERGE_STRATEGY:-theirs} -Xpatience -m "After update on ${DATE}"
  155. # Need to use a variable to not pass return codes of if checks
  156. MERGE_RETURN=$?
  157. if [[ ${MERGE_RETURN} == 128 ]]; then
  158. echo -e "\e[31m\nOh no, what happened?\n=> You most likely added files to your local mailcow instance that were now added to the official mailcow repository. Please move them to another location before updating mailcow.\e[0m"
  159. exit 1
  160. elif [[ ${MERGE_RETURN} == 1 ]]; then
  161. echo -e "\e[93mPotenial conflict, trying to fix...\e[0m"
  162. git status --porcelain | grep -E "UD|DU" | awk '{print $2}' | xargs rm -v
  163. git add -A
  164. git commit -m "After update on ${DATE}" > /dev/null
  165. git checkout .
  166. echo -e "\e[32mRemoved and recreated files if necessary.\e[0m"
  167. elif [[ ${MERGE_RETURN} != 0 ]]; then
  168. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  169. echo
  170. echo "Run docker-compose up -d to restart your stack without updates or try again after fixing the mentioned errors."
  171. exit 1
  172. fi
  173. echo -e "\e[32mFetching new docker-compose version...\e[0m"
  174. sleep 2
  175. if [[ ! -z $(which pip) && $(pip list --local | grep -c docker-compose) == 1 ]]; then
  176. true
  177. #prevent breaking a working docker-compose installed with pip
  178. elif [[ $(curl -sL -w "%{http_code}" https://www.servercow.de/docker-compose/latest.php -o /dev/null) == "200" ]]; then
  179. LATEST_COMPOSE=$(curl -#L https://www.servercow.de/docker-compose/latest.php)
  180. curl -#L https://github.com/docker/compose/releases/download/${LATEST_COMPOSE}/docker-compose-$(uname -s)-$(uname -m) > $(which docker-compose)
  181. chmod +x $(which docker-compose)
  182. else
  183. echo -e "\e[33mCannot determine latest docker-compose version, skipping...\e[0m"
  184. fi
  185. echo -e "\e[32mFetching new images, if any...\e[0m"
  186. sleep 2
  187. docker-compose pull --parallel
  188. # Fix missing SSL, does not overwrite existing files
  189. [[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
  190. cp -n data/assets/ssl-example/*.pem data/assets/ssl/
  191. echo -e "Fixing project name... "
  192. sed -i 's#COMPOSEPROJECT_NAME#COMPOSE_PROJECT_NAME#g' mailcow.conf
  193. sed -i '/COMPOSE_PROJECT_NAME=/s/-//g' mailcow.conf
  194. echo -e "Fixing PHP-FPM worker ports for Nginx sites..."
  195. sed -i 's#9000#9002#g' data/conf/nginx/*.conf
  196. sed -i 's#9000#9002#g' data/conf/nginx/*.custom
  197. echo -e "\e[32mStarting mailcow...\e[0m"
  198. sleep 2
  199. docker-compose up -d --remove-orphans
  200. echo -e "\e[32mCollecting garbage...\e[0m"
  201. IMGS_TO_DELETE=()
  202. for container in $(grep -oP "image: \Kmailcow.+" docker-compose.yml); do
  203. REPOSITORY=${container/:*}
  204. TAG=${container/*:}
  205. V_MAIN=${container/*.}
  206. V_SUB=${container/*.}
  207. EXISTING_TAGS=$(docker images | grep ${REPOSITORY} | awk '{ print $2 }')
  208. for existing_tag in ${EXISTING_TAGS[@]}; do
  209. V_MAIN_EXISTING=${existing_tag/*.}
  210. V_SUB_EXISTING=${existing_tag/*.}
  211. # Not an integer
  212. [[ ! $V_MAIN_EXISTING =~ ^[0-9]+$ ]] && continue
  213. [[ ! $V_SUB_EXISTING =~ ^[0-9]+$ ]] && continue
  214. if [[ $V_MAIN_EXISTING == "latest" ]]; then
  215. echo "Found deprecated label \"latest\" for repository $REPOSITORY, it should be deleted."
  216. IMGS_TO_DELETE+=($REPOSITORY:$existing_tag)
  217. elif [[ $V_MAIN_EXISTING -lt $V_MAIN ]]; then
  218. echo "Found tag $existing_tag for $REPOSITORY, which is older than the current tag $TAG and should be deleted."
  219. IMGS_TO_DELETE+=($REPOSITORY:$existing_tag)
  220. elif [[ $V_SUB_EXISTING -lt $V_SUB ]]; then
  221. echo "Found tag $existing_tag for $REPOSITORY, which is older than the current tag $TAG and should be deleted."
  222. IMGS_TO_DELETE+=($REPOSITORY:$existing_tag)
  223. fi
  224. done
  225. done
  226. if [[ ! -z ${IMGS_TO_DELETE[*]} ]]; then
  227. echo "Run the following command to delete unused image tags:"
  228. echo
  229. echo " docker rmi ${IMGS_TO_DELETE[*]}"
  230. echo
  231. read -r -p "Do you want to delete old image tags right now? [y/N] " response
  232. if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  233. docker rmi ${IMGS_TO_DELETE[*]}
  234. else
  235. echo "OK, skipped."
  236. fi
  237. fi
  238. echo -e "\e[32mFurther cleanup...\e[0m"
  239. echo "If you want to cleanup further garbage collected by Docker, please make sure all containers are up and running before cleaning your system by executing \"docker system prune\""
  240. #echo "In case you encounter any problem, hard-reset to a state before updating mailcow:"
  241. #echo
  242. #git reflog --color=always | grep "Before update on "
  243. #echo
  244. #echo "Use \"git reset --hard hash-on-the-left\" and run docker-compose up -d afterwards."