update.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 git) ]]; then echo "Cannot find git, exiting."; exit 1; fi
  13. set -o pipefail
  14. export LC_ALL=C
  15. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  16. BRANCH=$(git rev-parse --abbrev-ref HEAD)
  17. if [[ -f mailcow.conf ]]; then
  18. source mailcow.conf
  19. else
  20. echo -e "\e[31mNo mailcow.conf - is mailcow installed?\e[0m"
  21. exit 1
  22. fi
  23. read -r -p "Are you sure? [y/N] " response
  24. if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  25. echo "OK, exiting."
  26. exit 0
  27. fi
  28. echo -e "Stopping mailcow... "
  29. # Stopping mailcow
  30. docker-compose down
  31. # Silently fixing remote url from andryyy to mailcow
  32. git remote set-url origin https://github.com/mailcow/mailcow-dockerized
  33. echo -e "\e[32mCommitting current status...\e[90m"
  34. git add -u
  35. git commit -am "Before update on ${DATE}" > /dev/null
  36. echo -e "\e[32mFetching updated code from remote...\e[90m"
  37. git fetch origin ${BRANCH}
  38. echo -e "\e[32mMerging local with remote code (recursive, options: \"theirs\", \"patience\"...\e[90m"
  39. git merge -Xtheirs -Xpatience -m "After update on ${DATE}"
  40. if [[ $? == 1 ]]; then
  41. echo -e "\e[31mRun into conflict, trying to fix...\e[90m"
  42. git status --porcelain | grep -E "UD|DU" | awk '{print $2}' | xargs rm -v
  43. git add -A
  44. git commit -m "After update on ${DATE}" > /dev/null
  45. git checkout .
  46. echo -e "\e[32mRemoved and recreated files if necessary.\e[90m"
  47. fi
  48. echo -e "\e[32mFetching new images, if any...\e[0m"
  49. docker-compose pull
  50. echo
  51. #echo -e "\e[32mHashes to revert to:\e[0m"
  52. #git reflog --color=always | grep "Before update on "
  53. # TODO: Menu, select hard reset, select reset to "before update" etc.
  54. #git reset --hard origin/${BRANCH}
  55. # Fix missing SSL, does not overwrite existing files
  56. [[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
  57. cp -n data/assets/ssl-example/*.pem data/assets/ssl/
  58. 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)
  59. chmod +x $(which docker-compose)
  60. docker-compose up -d