update.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. if [[ -z $(which curl) ]]; then echo "Cannot find curl, exiting."; exit 1; fi
  3. if [[ -z $(which docker-compose) ]]; then echo "Cannot find docker-compose, exiting."; exit 1; fi
  4. if [[ -z $(which docker) ]]; then echo "Cannot find docker, exiting."; exit 1; fi
  5. if [[ -z $(which git) ]]; then echo "Cannot find git, exiting."; exit 1; fi
  6. if [[ -z $(which awk) ]]; then echo "Cannot find awk, exiting."; exit 1; fi
  7. if [[ -z $(which sha1sum) ]]; then echo "Cannot find sha1sum, exiting."; exit 1; fi
  8. echo -en "Checking internet connection... "
  9. curl -o /dev/null google.com -sm3
  10. if [[ $? != 0 ]]; then
  11. echo -e "\e[31mfailed\e[0m"
  12. exit 1
  13. else
  14. echo -e "\e[32mOK\e[0m"
  15. fi
  16. set -o pipefail
  17. export LC_ALL=C
  18. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  19. BRANCH=$(git rev-parse --abbrev-ref HEAD)
  20. case "${1}" in
  21. --check|-c)
  22. echo "Checking remote code for updates..."
  23. git fetch
  24. if ! git diff origin/${BRANCH} --quiet; then
  25. echo "Updated code is available."
  26. else
  27. echo "No updates available."
  28. fi
  29. exit 0
  30. ;;
  31. esac
  32. TMPFILE=$(mktemp "${TMPDIR:-/tmp}/curldata.XXXXXX")
  33. echo -e "\e[32mChecking for newer update script...\e[0m"
  34. SHA1_1=$(sha1sum update.sh)
  35. git fetch origin ${BRANCH}
  36. git checkout origin/${BRANCH} update.sh
  37. SHA1_2=$(sha1sum update.sh)
  38. if [[ ${SHA1_1} != ${SHA1_2} ]]; then
  39. echo "update.sh changed, please run this script again, exiting."
  40. chmod +x update.sh
  41. exit 0
  42. fi
  43. rm -f mv ${TMPFILE}
  44. if [[ -f mailcow.conf ]]; then
  45. source mailcow.conf
  46. else
  47. echo -e "\e[31mNo mailcow.conf - is mailcow installed?\e[0m"
  48. exit 1
  49. fi
  50. read -r -p "Are you sure you want to update mailcow: dockerized? All containers will be stopped. [y/N] " response
  51. if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  52. echo "OK, exiting."
  53. exit 0
  54. fi
  55. echo -e "Stopping mailcow... "
  56. sleep 2
  57. docker-compose down
  58. # Silently fixing remote url from andryyy to mailcow
  59. git remote set-url origin https://github.com/mailcow/mailcow-dockerized
  60. echo -e "\e[32mCommitting current status...\e[0m"
  61. git add -u
  62. git commit -am "Before update on ${DATE}" > /dev/null
  63. echo -e "\e[32mFetching updated code from remote...\e[0m"
  64. git fetch origin ${BRANCH}
  65. echo -e "\e[32mMerging local with remote code (recursive, options: \"theirs\", \"patience\"...\e[0m"
  66. git merge -Xtheirs -Xpatience -m "After update on ${DATE}"
  67. # Need to use a variable to not pass return codes of if checks
  68. MERGE_RETURN=$?
  69. if [[ ${MERGE_RETURN} == 128 ]]; then
  70. 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"
  71. exit 1
  72. elif [[ ${MERGE_RETURN} == 1 ]]; then
  73. echo -e "\e[93mPotenial conflict, trying to fix...\e[0m"
  74. git status --porcelain | grep -E "UD|DU" | awk '{print $2}' | xargs rm -v
  75. git add -A
  76. git commit -m "After update on ${DATE}" > /dev/null
  77. git checkout .
  78. echo -e "\e[32mRemoved and recreated files if necessary.\e[0m"
  79. elif [[ ${MERGE_RETURN} != 0 ]]; then
  80. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  81. echo
  82. echo "Run docker-compose up -d to restart your stack without updates or try again after fixing the mentioned errors."
  83. exit 1
  84. fi
  85. echo -e "\e[32mFetching new images, if any...\e[0m"
  86. sleep 2
  87. docker-compose pull --parallel
  88. # Fix missing SSL, does not overwrite existing files
  89. [[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
  90. cp -n data/assets/ssl-example/*.pem data/assets/ssl/
  91. echo -e "\e[32mFetching new docker-compose version...\e[0m"
  92. sleep 2
  93. curl -L https://github.com/docker/compose/releases/download/$(curl -Ls https://www.servercow.de/docker-compose/latest.php)/docker-compose-$(uname -s)-$(uname -m) > $(which docker-compose)
  94. chmod +x $(which docker-compose)
  95. echo -e "\e[32mStarting mailcow...\e[0m"
  96. sleep 2
  97. docker-compose up -d --remove-orphans
  98. #echo -e "\e[32mCleaning up Docker objects...\e[0m"
  99. if docker images -f "dangling=true" | grep ago --quiet; then
  100. docker rmi -f $(docker images -f "dangling=true" -q)
  101. docker volume rm $(docker volume ls -qf dangling=true)
  102. fi
  103. echo "In case you encounter any problem, hard-reset to a state before updating mailcow:"
  104. echo
  105. git reflog --color=always | grep "Before update on "
  106. echo
  107. echo "Use \"git reset --hard hash-on-the-left\" and run docker-compose up -d afterwards."