update.sh 4.1 KB

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