2016-02-01 11:05:29 +01:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
|
|
|
# Mirror git repos which the project depends on
|
|
|
|
#
|
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2016-02-13 23:09:27 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2016-02-01 11:05:29 +01:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2016-02-13 23:09:27 +01:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
2016-02-01 11:05:29 +01:00
|
|
|
#
|
2016-02-13 23:09:27 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-01 11:05:29 +01:00
|
|
|
|
|
|
|
PROJECT_NAME='freedombone'
|
|
|
|
|
|
|
|
export TEXTDOMAIN=${PROJECT_NAME}-mirrors
|
|
|
|
export TEXTDOMAINDIR="/usr/share/locale"
|
|
|
|
|
|
|
|
# Minimum number of characters in a password
|
2016-07-06 21:27:18 +02:00
|
|
|
MINIMUM_PASSWORD_LENGTH=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-passwords | grep 'MINIMUM_PASSWORD_LENGTH=' | head -n 1 | awk -F '=' '{print $2}')
|
2016-02-01 11:05:29 +01:00
|
|
|
|
|
|
|
CONFIGURATION_FILE="/root/${PROJECT_NAME}.cfg"
|
|
|
|
|
|
|
|
# if this is blank then just use the default repos
|
|
|
|
FRIENDS_MIRRORS_SERVER=
|
|
|
|
REPOS=
|
|
|
|
MY_MIRRORS_PASSWORD=
|
|
|
|
FRIENDS_MIRRORS_PASSWORD=
|
|
|
|
NEW_MIRRORS='no'
|
|
|
|
FRIENDS_MIRRORS_SSH_PORT=2222
|
|
|
|
|
|
|
|
MAIN_COMMAND=/usr/local/bin/${PROJECT_NAME}
|
|
|
|
if [ ! -f $MAIN_COMMAND ]; then
|
|
|
|
MAIN_COMMAND=/usr/bin/${PROJECT_NAME}
|
|
|
|
fi
|
|
|
|
|
2016-09-29 22:08:29 +02:00
|
|
|
REPOS=($(cat ${MAIN_COMMAND} /usr/share/${PROJECT_NAME}/utils/${MAIN_COMMAND}-utils-* /usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-* | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
|
2016-07-03 18:04:14 +02:00
|
|
|
|
2016-02-01 11:05:29 +01:00
|
|
|
|
|
|
|
# obtain the mirrors password if it exists
|
|
|
|
if [ -f $CONFIGURATION_FILE ]; then
|
|
|
|
if ! grep -q "MY_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then
|
|
|
|
MY_MIRRORS_PASSWORD=$(grep "MY_MIRRORS_PASSWORD" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
|
|
|
|
fi
|
|
|
|
if ! grep -q "FRIENDS_MIRRORS_SERVER" $CONFIGURATION_FILE; then
|
|
|
|
FRIENDS_MIRRORS_SERVER=$(grep "FRIENDS_MIRRORS_SERVER" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
|
|
|
|
fi
|
|
|
|
if ! grep -q "FRIENDS_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then
|
|
|
|
FRIENDS_MIRRORS_PASSWORD=$(grep "FRIENDS_MIRRORS_PASSWORD" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
|
|
|
|
fi
|
|
|
|
if ! grep -q "FRIENDS_MIRRORS_SSH_PORT" $CONFIGURATION_FILE; then
|
|
|
|
FRIENDS_MIRRORS_SSH_PORT=$(grep "FRIENDS_MIRRORS_SSH_PORT" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
function show_help {
|
|
|
|
echo ''
|
|
|
|
echo $"${PROJECT_NAME}-mirrors --sync [domain/url] -p [password]"
|
|
|
|
echo ''
|
|
|
|
echo $'Creates or syncs with a set of git repositories'
|
|
|
|
echo ''
|
|
|
|
echo $' --help Show help'
|
|
|
|
echo $' -n|--new [yes|no] Start a new mirrors'
|
2016-02-01 12:57:35 +01:00
|
|
|
echo $" -p|--password [password] Friend's mirrors user password"
|
|
|
|
echo $" -m|--mypassword [password] Local mirrors user password"
|
2016-02-01 11:05:29 +01:00
|
|
|
echo $" --port [number] Friend's server ssh port number"
|
|
|
|
echo $" -s|--sync [domain] Friend's server domain to sync with"
|
|
|
|
echo ''
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_mirrors_user {
|
|
|
|
if [ -d /home/mirrors ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2016-10-02 16:23:04 +02:00
|
|
|
create_password=1
|
|
|
|
if [ ${#MY_MIRRORS_PASSWORD} -ge ${MINIMUM_PASSWORD_LENGTH} ]; then
|
|
|
|
create_password=
|
2016-02-01 11:05:29 +01:00
|
|
|
fi
|
|
|
|
|
2016-10-02 16:23:04 +02:00
|
|
|
if [ $create_password ]; then
|
2016-02-01 11:05:29 +01:00
|
|
|
MY_MIRRORS_PASSWORD="$(openssl rand -base64 20 | cut -c1-18)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
useradd -m -p "$MY_MIRRORS_PASSWORD" -s /bin/bash mirrors
|
|
|
|
|
|
|
|
# remove any existing user files
|
|
|
|
rm -rf /home/mirrors/*
|
|
|
|
|
|
|
|
# store the mirrors password
|
|
|
|
if [ -f $CONFIGURATION_FILE ]; then
|
|
|
|
if ! grep -q "MY_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then
|
|
|
|
echo "MY_MIRRORS_PASSWORD=$MY_MIRRORS_PASSWORD" >> $CONFIGURATION_FILE
|
|
|
|
else
|
|
|
|
sed -i "s|MY_MIRRORS_PASSWORD=.*|MY_MIRRORS_PASSWORD=${MY_MIRRORS_PASSWORD}|g" $CONFIGURATION_FILE
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function enable_mirrors_via_onion {
|
|
|
|
if ! grep -q 'Host *.onion' /home/mirrors/.ssh/config; then
|
|
|
|
if [ ! -d /home/mirrors/.ssh ]; then
|
|
|
|
mkdir /home/mirrors/.ssh
|
|
|
|
fi
|
|
|
|
echo 'Host *.onion' >> /home/mirrors/.ssh/config
|
|
|
|
echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/mirrors/.ssh/config
|
|
|
|
chown mirrors:mirrors /home/mirrors/.ssh
|
|
|
|
chown mirrors:mirrors /home/mirrors/.ssh/config
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_repos_from_friend {
|
|
|
|
if [ ! $FRIENDS_MIRRORS_SERVER ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ ${#FRIENDS_MIRRORS_SERVER} -lt 2 ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
new_repos=()
|
|
|
|
for line in "${REPOS[@]}"
|
|
|
|
do
|
|
|
|
repo_name=$(echo "$line" | awk -F '=' '{print $1}')
|
|
|
|
mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
|
|
|
|
#repo_url=$(echo "$line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
|
|
|
|
friends_repo_url="ssh://mirrors@${FRIENDS_MIRRORS_SERVER}:${FRIENDS_MIRRORS_SSH_PORT}/home/mirrors/${mirrors_name}"
|
|
|
|
new_line="${repo_name}=\"${friends_repo_url}\""
|
|
|
|
new_repos+=($new_line)
|
|
|
|
done
|
|
|
|
REPOS=("${new_repos[@]}")
|
|
|
|
}
|
|
|
|
|
|
|
|
function sync_mirrors_repos {
|
|
|
|
for line in "${REPOS[@]}"
|
|
|
|
do
|
|
|
|
repo_name=$(echo "$line" | awk -F '=' '{print $1}')
|
|
|
|
repo_url=$(echo "$line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
|
|
|
|
mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
|
|
|
|
if [[ ${mirrors_name} != 'debian' ]]; then
|
|
|
|
if [[ $NEW_MIRRORS == 'yes' ]]; then
|
|
|
|
if [ -d /home/mirrors/${mirrors_name} ]; then
|
|
|
|
rm -rf /home/mirrors/${mirrors_name}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ ! -d /home/mirrors/${mirrors_name} ]; then
|
|
|
|
if [[ ${repo_url} != 'ssh:'* ]]; then
|
|
|
|
git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
|
|
|
|
else
|
|
|
|
sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
|
|
|
|
fi
|
|
|
|
if [ ! -d /home/mirrors/${mirrors_name} ]; then
|
|
|
|
echo $"WARNING: failed to mirror repo ${repo_url}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
cd /home/mirrors/${mirrors_name}
|
|
|
|
git remote set-url origin ${repo_url}
|
|
|
|
if [[ ${repo_url} != 'ssh:'* ]]; then
|
|
|
|
git fetch -p origin
|
|
|
|
else
|
|
|
|
sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git fetch -p origin
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
chown -R mirrors:mirrors /home/mirrors
|
|
|
|
}
|
|
|
|
|
|
|
|
while [[ $# > 1 ]]
|
|
|
|
do
|
|
|
|
key="$1"
|
|
|
|
|
|
|
|
case $key in
|
|
|
|
--help)
|
|
|
|
show_help
|
|
|
|
;;
|
|
|
|
-s|--sync)
|
|
|
|
shift
|
|
|
|
# use repos on another server
|
|
|
|
FRIENDS_MIRRORS_SERVER="$1"
|
|
|
|
;;
|
|
|
|
-m|--mypass|--mypassword)
|
|
|
|
shift
|
|
|
|
MY_MIRRORS_PASSWORD="$1"
|
|
|
|
if [ -f $CONFIGURATION_FILE ]; then
|
|
|
|
if ! grep -q "MY_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then
|
|
|
|
echo "MY_MIRRORS_PASSWORD=$MY_MIRRORS_PASSWORD" >> $CONFIGURATION_FILE
|
|
|
|
else
|
|
|
|
sed -i "s|MY_MIRRORS_PASSWORD=.*|MY_MIRRORS_PASSWORD=${MY_MIRRORS_PASSWORD}|g" $CONFIGURATION_FILE
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
-p|--pass|--password)
|
|
|
|
shift
|
|
|
|
FRIENDS_MIRRORS_PASSWORD="$1"
|
|
|
|
if [ -f $CONFIGURATION_FILE ]; then
|
|
|
|
if ! grep -q "FRIENDS_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then
|
|
|
|
echo "FRIENDS_MIRRORS_PASSWORD=$FRIENDS_MIRRORS_PASSWORD" >> $CONFIGURATION_FILE
|
|
|
|
else
|
|
|
|
sed -i "s|FRIENDS_MIRRORS_PASSWORD=.*|FRIENDS_MIRRORS_PASSWORD=${FRIENDS_MIRRORS_PASSWORD}|g" $CONFIGURATION_FILE
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
-n|--new)
|
|
|
|
shift
|
|
|
|
NEW_MIRRORS="$1"
|
|
|
|
;;
|
|
|
|
--port)
|
|
|
|
shift
|
|
|
|
FRIENDS_MIRRORS_SSH_PORT=${1}
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# unknown option
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
create_mirrors_user
|
|
|
|
enable_mirrors_via_onion
|
|
|
|
update_repos_from_friend
|
|
|
|
sync_mirrors_repos
|
|
|
|
|
|
|
|
exit 0
|