freedombone-mirrors 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Mirror git repos which the project depends on
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. PROJECT_NAME='freedombone'
  31. export TEXTDOMAIN=${PROJECT_NAME}-mirrors
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. # Minimum number of characters in a password
  34. MINIMUM_PASSWORD_LENGTH=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-passwords | grep 'MINIMUM_PASSWORD_LENGTH=' | head -n 1 | awk -F '=' '{print $2}')
  35. CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
  36. # if this is blank then just use the default repos
  37. FRIENDS_MIRRORS_SERVER=
  38. REPOS=
  39. MY_MIRRORS_PASSWORD=
  40. FRIENDS_MIRRORS_PASSWORD=
  41. NEW_MIRRORS='no'
  42. FRIENDS_MIRRORS_SSH_PORT=2222
  43. MAIN_COMMAND=/usr/local/bin/${PROJECT_NAME}
  44. if [ ! -f $MAIN_COMMAND ]; then
  45. MAIN_COMMAND=/usr/bin/${PROJECT_NAME}
  46. fi
  47. REPOS=($(cat ${MAIN_COMMAND} /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-* /usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-* | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
  48. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  49. for f in $UTILS_FILES
  50. do
  51. source $f
  52. done
  53. # obtain the mirrors password if it exists
  54. read_config_param MY_MIRRORS_PASSWORD
  55. read_config_param FRIENDS_MIRRORS_SERVER
  56. read_config_param FRIENDS_MIRRORS_PASSWORD
  57. read_config_param FRIENDS_MIRRORS_SSH_PORT
  58. function show_help {
  59. echo ''
  60. echo $"${PROJECT_NAME}-mirrors --sync [domain/url] -p [password]"
  61. echo ''
  62. echo $'Creates or syncs with a set of git repositories'
  63. echo ''
  64. echo $' --help Show help'
  65. echo $' -n|--new [yes|no] Start a new mirrors'
  66. echo $" -p|--password [password] Friend's mirrors user password"
  67. echo $" -m|--mypassword [password] Local mirrors user password"
  68. echo $" --port [number] Friend's server ssh port number"
  69. echo $" -s|--sync [domain] Friend's server domain to sync with"
  70. echo ''
  71. exit 0
  72. }
  73. function create_mirrors_user {
  74. if [ -d /home/mirrors ]; then
  75. return
  76. fi
  77. create_password=1
  78. if [ ${#MY_MIRRORS_PASSWORD} -ge ${MINIMUM_PASSWORD_LENGTH} ]; then
  79. create_password=
  80. fi
  81. if [ $create_password ]; then
  82. MY_MIRRORS_PASSWORD="$(openssl rand -base64 20 | cut -c1-18)"
  83. fi
  84. useradd -m -p "$MY_MIRRORS_PASSWORD" -s /bin/bash mirrors
  85. # remove any existing user files
  86. rm -rf /home/mirrors/*
  87. # store the mirrors password
  88. write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
  89. }
  90. function enable_mirrors_via_onion {
  91. if ! grep -q 'Host *.onion' /home/mirrors/.ssh/config; then
  92. if [ ! -d /home/mirrors/.ssh ]; then
  93. mkdir /home/mirrors/.ssh
  94. fi
  95. echo 'Host *.onion' >> /home/mirrors/.ssh/config
  96. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/mirrors/.ssh/config
  97. chown mirrors:mirrors /home/mirrors/.ssh
  98. chown mirrors:mirrors /home/mirrors/.ssh/config
  99. fi
  100. }
  101. function update_repos_from_friend {
  102. if [ ! $FRIENDS_MIRRORS_SERVER ]; then
  103. return
  104. fi
  105. if [ ${#FRIENDS_MIRRORS_SERVER} -lt 2 ]; then
  106. return
  107. fi
  108. new_repos=()
  109. for line in "${REPOS[@]}"
  110. do
  111. repo_name=$(echo "$line" | awk -F '=' '{print $1}')
  112. mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
  113. #repo_url=$(echo "$line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  114. friends_repo_url="ssh://mirrors@${FRIENDS_MIRRORS_SERVER}:${FRIENDS_MIRRORS_SSH_PORT}/home/mirrors/${mirrors_name}"
  115. new_line="${repo_name}=\"${friends_repo_url}\""
  116. new_repos+=($new_line)
  117. done
  118. REPOS=("${new_repos[@]}")
  119. }
  120. function sync_mirrors_repos {
  121. for line in "${REPOS[@]}"
  122. do
  123. repo_name=$(echo "$line" | awk -F '=' '{print $1}')
  124. repo_url=$(echo "$line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  125. mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
  126. if [[ ${mirrors_name} != 'debian' ]]; then
  127. if [[ $NEW_MIRRORS == 'yes' ]]; then
  128. if [ -d /home/mirrors/${mirrors_name} ]; then
  129. rm -rf /home/mirrors/${mirrors_name}
  130. fi
  131. fi
  132. if [ ! -d /home/mirrors/${mirrors_name} ]; then
  133. if [[ ${repo_url} != 'ssh:'* ]]; then
  134. git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
  135. else
  136. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
  137. fi
  138. if [ ! -d /home/mirrors/${mirrors_name} ]; then
  139. echo $"WARNING: failed to mirror repo ${repo_url}"
  140. fi
  141. else
  142. cd /home/mirrors/${mirrors_name}
  143. git remote set-url origin ${repo_url}
  144. if [[ ${repo_url} != 'ssh:'* ]]; then
  145. git fetch -p origin
  146. else
  147. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git fetch -p origin
  148. fi
  149. fi
  150. fi
  151. done
  152. chown -R mirrors:mirrors /home/mirrors
  153. }
  154. while [[ $# > 1 ]]
  155. do
  156. key="$1"
  157. case $key in
  158. --help)
  159. show_help
  160. ;;
  161. -s|--sync)
  162. shift
  163. # use repos on another server
  164. FRIENDS_MIRRORS_SERVER="$1"
  165. ;;
  166. -m|--mypass|--mypassword)
  167. shift
  168. MY_MIRRORS_PASSWORD="$1"
  169. write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
  170. ;;
  171. -p|--pass|--password)
  172. shift
  173. FRIENDS_MIRRORS_PASSWORD="$1"
  174. write_config_param "FRIENDS_MIRRORS_PASSWORD" "${FRIENDS_MIRRORS_PASSWORD}"
  175. ;;
  176. -n|--new)
  177. shift
  178. NEW_MIRRORS="$1"
  179. ;;
  180. --port)
  181. shift
  182. FRIENDS_MIRRORS_SSH_PORT=${1}
  183. ;;
  184. *)
  185. # unknown option
  186. ;;
  187. esac
  188. shift
  189. done
  190. create_mirrors_user
  191. enable_mirrors_via_onion
  192. update_repos_from_friend
  193. sync_mirrors_repos
  194. exit 0