update.sh 9.8 KB

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