freedombone-mirrors 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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@freedombone.net>
  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. # used to mirror a single application
  37. SYNC_SINGLE_APP=
  38. # if this is blank then just use the default repos
  39. FRIENDS_MIRRORS_SERVER=
  40. UTILS_REPOS=
  41. INSTALLED_APPS_REPOS=
  42. MY_MIRRORS_PASSWORD=
  43. FRIENDS_MIRRORS_PASSWORD=
  44. NEW_MIRRORS='no'
  45. FRIENDS_MIRRORS_SSH_PORT=2222
  46. MAIN_COMMAND=/usr/local/bin/${PROJECT_NAME}
  47. if [ ! -f $MAIN_COMMAND ]; then
  48. MAIN_COMMAND=/usr/bin/${PROJECT_NAME}
  49. fi
  50. # local repos for utils
  51. UTILS_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'))
  52. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  53. for f in $UTILS_FILES
  54. do
  55. source $f
  56. done
  57. # obtain the mirrors password if it exists
  58. read_config_param MY_MIRRORS_PASSWORD
  59. read_config_param FRIENDS_MIRRORS_SERVER
  60. read_config_param FRIENDS_MIRRORS_PASSWORD
  61. read_config_param FRIENDS_MIRRORS_SSH_PORT
  62. function show_help {
  63. echo ''
  64. echo $"${PROJECT_NAME}-mirrors --sync [domain/url] -p [password]"
  65. echo ''
  66. echo $'Creates or syncs with a set of git repositories'
  67. echo ''
  68. echo $' --help Show help'
  69. echo $' -n|--new [yes|no] Start a new mirrors'
  70. echo $" -p|--password [password] Friend's mirrors user password"
  71. echo $" -m|--mypassword [password] Local mirrors user password"
  72. echo $" --port [number] Friend's server ssh port number"
  73. echo $" -s|--sync [domain] Friend's server domain to sync with"
  74. echo ''
  75. exit 0
  76. }
  77. function create_mirrors_user {
  78. if [ -d /home/mirrors ]; then
  79. return
  80. fi
  81. create_password=1
  82. if [ ${#MY_MIRRORS_PASSWORD} -ge ${MINIMUM_PASSWORD_LENGTH} ]; then
  83. create_password=
  84. fi
  85. if [ $create_password ]; then
  86. MY_MIRRORS_PASSWORD="$(openssl rand -base64 20 | cut -c1-18)"
  87. fi
  88. chmod 600 /etc/shadow
  89. chmod 600 /etc/gshadow
  90. useradd -m -p "$MY_MIRRORS_PASSWORD" -s /bin/bash mirrors
  91. chmod 0000 /etc/shadow
  92. chmod 0000 /etc/gshadow
  93. # remove any existing user files
  94. rm -rf /home/mirrors/*
  95. # store the mirrors password
  96. write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
  97. }
  98. function enable_mirrors_via_onion {
  99. if ! grep -q 'Host *.onion' /home/mirrors/.ssh/config; then
  100. if [ ! -d /home/mirrors/.ssh ]; then
  101. mkdir /home/mirrors/.ssh
  102. fi
  103. echo 'Host *.onion' >> /home/mirrors/.ssh/config
  104. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/mirrors/.ssh/config
  105. chown mirrors:mirrors /home/mirrors/.ssh
  106. chown mirrors:mirrors /home/mirrors/.ssh/config
  107. fi
  108. }
  109. function update_installed_single_repo {
  110. # only deal with a single app
  111. filename=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${SYNC_SINGLE_APP}
  112. if [ ! -f $filename ]; then
  113. echo $"The app $SYNC_SINGLE_APP was not found"
  114. exit 36822
  115. fi
  116. APP_REPOS=($(cat ${MAIN_COMMAND} $filename | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
  117. for line in "${APP_REPOS[@]}"
  118. do
  119. INSTALLED_APPS_REPOS+=("${line}")
  120. done
  121. }
  122. function update_installed_apps_repos {
  123. INSTALLED_APPS_REPOS=()
  124. function_check app_is_installed
  125. if [ $SYNC_SINGLE_APP ]; then
  126. update_installed_single_repo
  127. return
  128. fi
  129. # all apps currently installed
  130. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  131. # for all the app scripts
  132. for filename in $FILES
  133. do
  134. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  135. if [[ "$(app_is_installed ${app_name})" == "1" ]]; then
  136. APP_REPOS=($(cat ${MAIN_COMMAND} $filename | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
  137. for line in "${APP_REPOS[@]}"
  138. do
  139. INSTALLED_APPS_REPOS+=("${line}")
  140. done
  141. fi
  142. done
  143. }
  144. function update_repos_from_friend_base {
  145. syncrepos=${1}
  146. new_repos=()
  147. for line in $syncrepos
  148. do
  149. repo_name=$(echo "$line" | awk -F '=' '{print $1}')
  150. mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
  151. friends_repo_url="ssh://mirrors@${FRIENDS_MIRRORS_SERVER}:${FRIENDS_MIRRORS_SSH_PORT}/home/mirrors/${mirrors_name}"
  152. new_line="${repo_name}=\"${friends_repo_url}\""
  153. new_repos+=($new_line)
  154. done
  155. }
  156. function update_repos_from_friend {
  157. if [ ! $FRIENDS_MIRRORS_SERVER ]; then
  158. return
  159. fi
  160. if [ ${#FRIENDS_MIRRORS_SERVER} -lt 2 ]; then
  161. return
  162. fi
  163. update_repos_from_friend_base "${UTILS_REPOS[@]}"
  164. UTILS_REPOS=("${new_repos[@]}")
  165. update_repos_from_friend_base "${INSTALLED_APPS_REPOS[@]}"
  166. INSTALLED_APPS_REPOS=("${new_repos[@]}")
  167. }
  168. function sync_mirrors_base {
  169. syncrepos=${1}
  170. for line in $syncrepos
  171. do
  172. repo_name=$(echo "$line" | awk -F '=' '{print $1}')
  173. repo_url=$(echo "$line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  174. mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
  175. if [[ ${mirrors_name} != 'debian' ]]; then
  176. if [[ $NEW_MIRRORS == 'yes' ]]; then
  177. if [ -d /home/mirrors/${mirrors_name} ]; then
  178. rm -rf /home/mirrors/${mirrors_name}
  179. fi
  180. fi
  181. if [ ! -d /home/mirrors/${mirrors_name} ]; then
  182. if [[ ${repo_url} != 'ssh:'* ]]; then
  183. git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
  184. else
  185. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
  186. fi
  187. if [ ! -d /home/mirrors/${mirrors_name} ]; then
  188. echo $"WARNING: failed to mirror repo ${repo_url}"
  189. fi
  190. else
  191. cd /home/mirrors/${mirrors_name}
  192. git remote set-url origin ${repo_url}
  193. if [[ ${repo_url} != 'ssh:'* ]]; then
  194. git fetch -p origin
  195. else
  196. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git fetch -p origin
  197. fi
  198. fi
  199. fi
  200. done
  201. chown -R mirrors:mirrors /home/mirrors
  202. }
  203. function sync_mirrors_repos {
  204. if [ ! $SYNC_SINGLE_APP ]; then
  205. sync_mirrors_base "${UTILS_REPOS[@]}"
  206. fi
  207. sync_mirrors_base "${INSTALLED_APPS_REPOS[@]}"
  208. }
  209. while [[ $# > 1 ]]
  210. do
  211. key="$1"
  212. case $key in
  213. --help)
  214. show_help
  215. ;;
  216. -s|--sync)
  217. shift
  218. # use repos on another server
  219. FRIENDS_MIRRORS_SERVER="$1"
  220. ;;
  221. -m|--mypass|--mypassword)
  222. shift
  223. MY_MIRRORS_PASSWORD="$1"
  224. write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
  225. ;;
  226. -p|--pass|--password)
  227. shift
  228. FRIENDS_MIRRORS_PASSWORD="$1"
  229. write_config_param "FRIENDS_MIRRORS_PASSWORD" "${FRIENDS_MIRRORS_PASSWORD}"
  230. ;;
  231. -n|--new)
  232. shift
  233. NEW_MIRRORS="$1"
  234. ;;
  235. --port)
  236. shift
  237. FRIENDS_MIRRORS_SSH_PORT=${1}
  238. ;;
  239. -a|--app)
  240. shift
  241. SYNC_SINGLE_APP="${1}"
  242. ;;
  243. *)
  244. # unknown option
  245. ;;
  246. esac
  247. shift
  248. done
  249. create_mirrors_user
  250. enable_mirrors_via_onion
  251. update_installed_apps_repos
  252. update_repos_from_friend
  253. sync_mirrors_repos
  254. exit 0