freedombone-utils-git 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Useful git functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. function git_clone {
  29. repo_url="$1"
  30. destination_dir="$2"
  31. echo "git clone $repo_url $destination_dir"
  32. git clone --recursive "$repo_url" "$destination_dir"
  33. }
  34. function git_pull {
  35. if [ ! "$1" ]; then
  36. echo $'git_pull no repo specified'
  37. fi
  38. git merge --abort
  39. git stash
  40. git remote set-url origin "$1"
  41. git checkout master
  42. git pull
  43. if [ "$2" ]; then
  44. # delete any existing branch
  45. git branch -D "$2"
  46. # check out the new branch
  47. if ! git checkout "$2" -b "$2"; then
  48. if [ -f /tmp/.upgrading ]; then
  49. rm /tmp/.upgrading
  50. fi
  51. echo $"Unable to checkout $1 $2"
  52. exit 72357
  53. fi
  54. fi
  55. }
  56. function commit_has_changed {
  57. repo_dir=$1
  58. repo_commit_name=$2
  59. repo_commit=$3
  60. if [ -d "$repo_dir" ]; then
  61. if grep -q "$repo_commit_name" "$COMPLETION_FILE"; then
  62. CURRENT_REPO_COMMIT=$(get_completion_param "$repo_commit_name")
  63. if [[ "$CURRENT_REPO_COMMIT" != "$repo_commit" ]]; then
  64. echo "1"
  65. return
  66. fi
  67. else
  68. echo "1"
  69. return
  70. fi
  71. fi
  72. echo "0"
  73. }
  74. # This ensures that a given repo is on a given commit
  75. # If it isn't then it attempts to upgrade
  76. function set_repo_commit {
  77. repo_dir=$1
  78. repo_commit_name=$2
  79. repo_commit=$3
  80. repo_url=$4
  81. if [[ $(commit_has_changed "$repo_dir" "$repo_commit_name" "$repo_commit") == "1" ]]; then
  82. cd "$repo_dir" || exit 3856835
  83. git_pull "$repo_url" "$repo_commit"
  84. # application specific stuff after updating the repo
  85. if [[ $repo_dir == *"www"* ]]; then
  86. chown -R www-data:www-data "$repo_dir"
  87. fi
  88. if [[ $repo_dir == *"gpgit" ]]; then
  89. if [ ! -f /usr/bin/gpgit.pl ]; then
  90. cp gpgit.pl /usr/bin/gpgit.pl
  91. else
  92. HASH1=$(sha256sum gpgit.pl | awk -F ' ' '{print $1}')
  93. HASH2=$(sha256sum /usr/bin/gpgit.pl | awk -F ' ' '{print $1}')
  94. if [[ "$HASH1" != "$HASH2" ]]; then
  95. cp gpgit.pl /usr/bin/gpgit.pl
  96. fi
  97. fi
  98. fi
  99. if [[ $repo_dir == *"cleanup-maildir" ]]; then
  100. if [ ! -f /usr/bin/cleanup-maildir ]; then
  101. cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
  102. else
  103. HASH1=$(sha256sum "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" | awk -F ' ' '{print $1}')
  104. HASH2=$(sha256sum /usr/bin/cleanup-maildir | awk -F ' ' '{print $1}')
  105. if [[ "$HASH1" != "$HASH2" ]]; then
  106. cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
  107. fi
  108. fi
  109. fi
  110. if [[ $repo_dir == *"nginx_ensite" ]]; then
  111. if [ ! -f /usr/local/bin/nginx_ensite ]; then
  112. make install
  113. fi
  114. fi
  115. if [[ $repo_dir == *"inadyn" ]]; then
  116. ./configure
  117. USE_OPENSSL=1 make
  118. make install
  119. systemctl restart inadyn
  120. fi
  121. function_check set_completion_param
  122. set_completion_param "${repo_commit_name}" "${repo_commit}"
  123. fi
  124. }
  125. function configure_firewall_for_git {
  126. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  127. return
  128. fi
  129. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  130. # docker does its own firewalling
  131. return
  132. fi
  133. if [[ $ONION_ONLY != "no" ]]; then
  134. return
  135. fi
  136. firewall_add Git 9418 tcp
  137. mark_completed "${FUNCNAME[0]}"
  138. }
  139. # NOTE: deliberately no exit 0