update.sh 2.6 KB

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