update.sh 8.6 KB

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