freedomboneeee/src/freedombone-mirrors

289 lines
8.7 KiB
Plaintext
Raw Normal View History

2016-02-01 11:05:29 +01:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Mirror git repos which the project depends on
#
# License
# =======
#
2016-10-31 17:24:49 +01:00
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
2016-02-01 11:05:29 +01:00
#
# 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
2016-10-10 23:50:59 +02:00
CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
2016-02-01 11:05:29 +01:00
2016-11-03 12:44:11 +01:00
# used to mirror a single application
SYNC_SINGLE_APP=
2016-02-01 11:05:29 +01:00
# if this is blank then just use the default repos
FRIENDS_MIRRORS_SERVER=
UTILS_REPOS=
INSTALLED_APPS_REPOS=
2016-02-01 11:05:29 +01:00
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
# local repos for utils
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'))
2016-07-03 18:04:14 +02:00
2016-10-13 23:26:09 +02:00
UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
for f in $UTILS_FILES
do
source $f
2016-10-13 23:26:09 +02:00
done
2016-02-01 11:05:29 +01:00
# obtain the mirrors password if it exists
2016-10-13 23:26:09 +02:00
read_config_param MY_MIRRORS_PASSWORD
read_config_param FRIENDS_MIRRORS_SERVER
read_config_param FRIENDS_MIRRORS_PASSWORD
read_config_param FRIENDS_MIRRORS_SSH_PORT
2016-02-01 11:05:29 +01:00
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'
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
2017-05-06 17:21:27 +02:00
MY_MIRRORS_PASSWORD=$(openssl rand -base64 64 | tr -dc A-Za-z0-9 | head -c 18)
2016-02-01 11:05:29 +01:00
fi
chmod 600 /etc/shadow
chmod 600 /etc/gshadow
2016-02-01 11:05:29 +01:00
useradd -m -p "$MY_MIRRORS_PASSWORD" -s /bin/bash mirrors
chmod 0000 /etc/shadow
chmod 0000 /etc/gshadow
2016-02-01 11:05:29 +01:00
# remove any existing user files
rm -rf /home/mirrors/*
# store the mirrors password
2016-10-13 23:26:09 +02:00
write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
2016-02-01 11:05:29 +01:00
}
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
}
2016-11-03 12:49:59 +01:00
function update_installed_single_repo {
# only deal with a single app
filename=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${SYNC_SINGLE_APP}
if [ ! -f $filename ]; then
echo $"The app $SYNC_SINGLE_APP was not found"
exit 36822
fi
APP_REPOS=($(cat ${MAIN_COMMAND} $filename | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
for line in "${APP_REPOS[@]}"
do
INSTALLED_APPS_REPOS+=("${line}")
done
}
function update_installed_apps_repos {
INSTALLED_APPS_REPOS=()
2016-02-01 11:05:29 +01:00
function_check app_is_installed
2016-11-03 12:44:11 +01:00
if [ $SYNC_SINGLE_APP ]; then
2016-11-03 12:49:59 +01:00
update_installed_single_repo
2016-11-03 12:44:11 +01:00
return
fi
# all apps currently installed
FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
# for all the app scripts
for filename in $FILES
do
app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
if [[ "$(app_is_installed ${app_name})" == "1" ]]; then
APP_REPOS=($(cat ${MAIN_COMMAND} $filename | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
for line in "${APP_REPOS[@]}"
do
INSTALLED_APPS_REPOS+=("${line}")
done
fi
done
}
function update_repos_from_friend_base {
syncrepos=${1}
2016-02-01 11:05:29 +01:00
new_repos=()
for line in $syncrepos
2016-02-01 11:05:29 +01:00
do
repo_name=$(echo "$line" | awk -F '=' '{print $1}')
mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
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
}
function update_repos_from_friend {
if [ ! $FRIENDS_MIRRORS_SERVER ]; then
return
fi
if [ ${#FRIENDS_MIRRORS_SERVER} -lt 2 ]; then
return
fi
update_repos_from_friend_base "${UTILS_REPOS[@]}"
UTILS_REPOS=("${new_repos[@]}")
update_repos_from_friend_base "${INSTALLED_APPS_REPOS[@]}"
INSTALLED_APPS_REPOS=("${new_repos[@]}")
}
function sync_mirrors_base {
syncrepos=${1}
for line in $syncrepos
2016-02-01 11:05:29 +01:00
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
}
function sync_mirrors_repos {
2016-11-03 12:44:11 +01:00
if [ ! $SYNC_SINGLE_APP ]; then
sync_mirrors_base "${UTILS_REPOS[@]}"
fi
sync_mirrors_base "${INSTALLED_APPS_REPOS[@]}"
}
2016-02-01 11:05:29 +01:00
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"
2016-10-13 23:26:09 +02:00
write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
2016-02-01 11:05:29 +01:00
;;
-p|--pass|--password)
shift
FRIENDS_MIRRORS_PASSWORD="$1"
2016-10-13 23:26:09 +02:00
write_config_param "FRIENDS_MIRRORS_PASSWORD" "${FRIENDS_MIRRORS_PASSWORD}"
2016-02-01 11:05:29 +01:00
;;
-n|--new)
shift
NEW_MIRRORS="$1"
;;
--port)
shift
FRIENDS_MIRRORS_SSH_PORT=${1}
;;
2016-11-03 12:44:11 +01:00
-a|--app)
shift
SYNC_SINGLE_APP="${1}"
;;
2016-02-01 11:05:29 +01:00
*)
# unknown option
;;
esac
shift
done
create_mirrors_user
enable_mirrors_via_onion
update_installed_apps_repos
2016-02-01 11:05:29 +01:00
update_repos_from_friend
sync_mirrors_repos
exit 0