update.sh 8.1 KB

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