update.sh 9.3 KB

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