update.sh 8.6 KB

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