freedomboneeee/src/freedombone-app-tahoelafs

384 lines
12 KiB
Plaintext
Raw Normal View History

2016-08-28 00:40:21 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
2017-03-04 00:25:38 +01:00
# Distributed storage system introducer
# http://tahoe-lafs.readthedocs.io/en/latest/anonymity-configuration.html
2016-08-28 00:40:21 +02:00
#
# License
# =======
#
2016-10-31 17:24:49 +01:00
# Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
2016-08-28 00:40:21 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# 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/>.
2017-03-03 19:48:33 +01:00
VARIANTS='full full-vim cloud'
2016-08-28 00:40:21 +02:00
IN_DEFAULT_INSTALL=0
2017-03-05 14:07:45 +01:00
SHOW_ON_ABOUT=0
TAHOELAFS_REPO="https://github.com/tahoe-lafs/tahoe-lafs"
2017-03-03 19:48:33 +01:00
TAHOELAFS_COMMIT='bb782b0331a60de438136a593bba18338d8d866b'
2016-08-28 00:40:21 +02:00
TAHOELAFS_PORT=50213
2017-03-04 00:25:38 +01:00
TAHOELAFS_ONION_PORT=8096
2017-03-03 19:56:32 +01:00
2016-08-28 00:40:21 +02:00
TAHOELAFS_SHARED_DIR='Shared'
2017-03-04 19:18:07 +01:00
TAHOE_COMMAND="cd /home/tahoelafs/tahoelafs && venv/bin/tahoe"
2016-08-28 00:40:21 +02:00
2017-03-04 00:25:38 +01:00
tahoelafs_variables=(ONION_ONLY
TAHOELAFS_REPO
2016-10-05 23:33:41 +02:00
TAHOELAFS_PORT)
2017-03-04 00:25:38 +01:00
function tahoelafs_setup_config {
config_file=$1
2017-03-05 20:49:18 +01:00
nick="$2"
2017-03-04 00:25:38 +01:00
if ! grep -q "[node]" $config_file; then
echo '' >> $config_file
echo '[node]' >> $config_file
fi
if ! grep -q "[connections]" $config_file; then
echo '' >> $config_file
echo '[connections]' >> $config_file
fi
if ! grep -q "reveal-IP-address" $config_file; then
sed -i '/[node]/a reveal-IP-address = False' $config_file
else
2017-03-04 23:55:30 +01:00
sed -i 's|reveal-IP-address.*|reveal-IP-address = False|g' $config_file
2017-03-04 00:25:38 +01:00
fi
if ! grep -q "tcp =" $config_file; then
sed -i '/[connections]/a tcp = tor' $config_file
else
2017-03-04 23:55:30 +01:00
sed -i 's|tcp =.*|tcp = tor|g' $config_file
2017-03-04 00:25:38 +01:00
fi
if ! grep -q "tub.location =" $config_file; then
2017-03-04 23:55:30 +01:00
sed -i '/[node]/a tub.location = disabled' $config_file
2017-03-04 00:25:38 +01:00
fi
if ! grep -q "tub.port =" $config_file; then
2017-03-04 23:55:30 +01:00
sed -i "/[node]/a tub.port = tcp:${TAHOELAFS_ONION_PORT}:interface=127.0.0.1" $config_file
2017-03-04 00:25:38 +01:00
fi
2017-03-04 23:55:30 +01:00
sed -i "s|tub.port.*|tub.port = tcp:${TAHOELAFS_ONION_PORT}:interface=127.0.0.1|g" $config_file
sed -i "s|tub.location.*|tub.location = tor:${TAHOELAFS_ONION_HOSTNAME}:${TAHOELAFS_PORT}|g" $config_file
2017-03-04 00:25:38 +01:00
2017-03-05 20:49:18 +01:00
sed -i "s|nickname =.*|nickname = ${NICK}|g" $config_file
2017-03-04 00:25:38 +01:00
2017-03-05 20:49:18 +01:00
chown -R tahoelafs:debian-tor /home/tahoelafs
2017-03-04 00:25:38 +01:00
}
function install_interactive_tahoelafs {
echo -n ''
APP_INSTALLED=1
}
2016-08-28 00:40:21 +02:00
function upgrade_tahoelafs {
2017-03-03 19:48:33 +01:00
systemctl stop tahoelafs
function_check set_repo_commit
set_repo_commit /home/tahoelafs/tahoelafs "tahoelafs commit" "$TAHOELAFS_COMMIT" $TAHOELAFS_REPO
cd /home/tahoelafs/tahoelafs
2017-03-04 18:49:44 +01:00
git submodule update --init --recursive
2017-03-03 19:48:33 +01:00
virtualenv venv
venv/bin/pip install --editable .
2017-03-05 15:01:44 +01:00
chown -R tahoelafs:debian-tor /home/tahoelafs
2017-03-03 19:48:33 +01:00
systemctl start tahoelafs
2016-08-28 00:40:21 +02:00
}
function backup_local_tahoelafs {
2017-03-05 20:49:18 +01:00
source_directory=/home/tahoelafs
2016-08-29 12:04:37 +02:00
if [ ! -d $source_directory ]; then
return
fi
systemctl stop tahoelafs
dest_directory=tahoelafs
function_check backup_directory_to_usb
backup_directory_to_usb $source_directory $dest_directory
systemctl start tahoelafs
2016-08-28 00:40:21 +02:00
}
function restore_local_tahoelafs {
2016-08-29 12:04:37 +02:00
echo $"Restoring Tahoe-LAFS introducer"
2017-03-05 20:49:18 +01:00
systemctl stop tahoelafs-storage
systemctl stop tahoelafs-introducer
2016-08-29 12:04:37 +02:00
temp_restore_dir=/root/temptahoelafs
restore_directory_from_usb $temp_restore_dir tahoelafs
2017-03-05 20:49:18 +01:00
mv /home/tahoelafs /home/tahoelafs-old
cp -r $temp_restore_dir/home/tahoelafs /home/tahoelafs
2016-08-29 12:21:39 +02:00
if [ ! "$?" = "0" ]; then
2017-03-05 20:49:18 +01:00
mv /home/tahoelafs-old /home/tahoelafs
2016-08-29 12:21:39 +02:00
exit 246833
fi
2017-03-05 20:49:18 +01:00
rm -rf /home/tahoelafs-old
2017-03-05 15:01:44 +01:00
chown -R tahoelafs:debian-tor /home/tahoelafs
2017-03-05 20:49:18 +01:00
systemctl start tahoelafs-introducer
systemctl start tahoelafs-storage
2016-08-29 12:21:39 +02:00
echo $"Restore complete"
2016-08-28 00:40:21 +02:00
}
function backup_remote_tahoelafs {
2017-03-05 20:49:18 +01:00
source_directory=/home/tahoelafs
2016-08-29 12:21:39 +02:00
if [ ! -d $source_directory ]; then
return
fi
2017-03-05 20:49:18 +01:00
systemctl stop tahoelafs-storage
systemctl stop tahoelafs-introducer
2016-08-29 12:21:39 +02:00
dest_directory=tahoelafs
function_check backup_directory_to_usb
backup_directory_to_friend $source_directory $dest_directory
2017-03-05 20:49:18 +01:00
systemctl start tahoelafs-introducer
systemctl start tahoelafs-storage
2016-08-28 00:40:21 +02:00
}
function restore_remote_tahoelafs {
2016-08-29 12:21:39 +02:00
echo $"Restoring Tahoe-LAFS introducer"
2017-03-05 20:49:18 +01:00
systemctl stop tahoelafs-storage
systemctl stop tahoelafs-introducer
2016-08-29 12:21:39 +02:00
temp_restore_dir=/root/temptahoelafs
restore_directory_from_friend $temp_restore_dir tahoelafs
2017-03-05 20:49:18 +01:00
mv /home/tahoelafs /home/tahoelafs-old
cp -r $temp_restore_dir/home/tahoelafs /home/tahoelafs
2016-08-29 12:21:39 +02:00
if [ ! "$?" = "0" ]; then
2017-03-05 20:49:18 +01:00
mv /home/tahoelafs-old /home/tahoelafs
2016-08-29 12:21:39 +02:00
exit 623925
fi
2017-03-05 20:49:18 +01:00
rm -rf /home/tahoelafs-old
2017-03-05 15:01:44 +01:00
chown -R tahoelafs:debian-tor /home/tahoelafs
2017-03-05 20:49:18 +01:00
systemctl start tahoelafs-introducer
systemctl start tahoelafs-storage
2016-08-29 12:21:39 +02:00
echo $"Restore complete"
2016-08-28 00:40:21 +02:00
}
2017-03-04 00:30:55 +01:00
function reconfigure_tahoelafs {
echo -n ''
}
2016-08-28 00:40:21 +02:00
function remove_tahoelafs {
2017-03-05 13:09:45 +01:00
if [ -f /etc/nginx/sites-available/tahoelafs ]; then
nginx_dissite tahoelafs
rm /etc/nginx/sites-available/tahoelafs
if [ -d /var/www/tahoelafs ]; then
rm -rf /var/www/tahoelafs
fi
systemctl reload nginx
fi
2017-03-05 20:49:18 +01:00
systemctl stop tahoelafs-storage
systemctl disable tahoelafs-storage
rm /etc/systemd/system/tahoelafs-storage.service
2017-03-04 18:52:15 +01:00
2017-03-05 20:49:18 +01:00
systemctl stop tahoelafs-introducer
systemctl disable tahoelafs-introducer
rm /etc/systemd/system/tahoelafs-introducer.service
#firewall_remove ${TAHOELAFS_PORT}
rm -rf /var/lib/tahoelafs
2016-10-17 15:44:49 +02:00
remove_completion_param install_tahoelafs
2017-03-05 20:49:18 +01:00
#remove_completion_param configure_firewall_for_tahoelafs
2017-03-04 00:25:38 +01:00
function_check remove_onion_service
remove_onion_service tahoelafs ${TAHOELAFS_ONION_PORT}
deluser tahoelafs
2017-03-04 18:52:15 +01:00
if [ -d /home/tahoelafs ]; then
rm -rf /home/tahoelafs
fi
2017-03-04 00:28:52 +01:00
remove_app tahoelafs
2016-08-28 00:40:21 +02:00
}
function configure_firewall_for_tahoelafs {
2016-10-16 20:50:56 +02:00
if [[ $(is_completed $FUNCNAME) == "1" ]]; then
2016-08-28 00:40:21 +02:00
return
fi
2016-10-22 11:39:34 +02:00
firewall_add Tahoe-LAFS ${TAHOELAFS_PORT}
2016-10-16 20:50:56 +02:00
mark_completed $FUNCNAME
2016-08-28 00:40:21 +02:00
}
2017-03-05 20:49:18 +01:00
function install_tahoelafs_to_directory {
tahoe_dir=$1
git_clone $TAHOELAFS_REPO $tahoe_dir
cd $tahoe_dir
git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT
git submodule update --init --recursive
virtualenv venv --distribute
venv/bin/pip uninstall --yes setuptools
venv/bin/pip install setuptools==11.3
venv/bin/pip install six==1.10.0 packaging==16.8 attrs==16.3.0 appdirs==1.4.2 pycrypto==2.1.0 cffi==1.9.1
venv/bin/pip install cryptography==1.7.2 markerlib==0.6.0 distribute==0.7.3
venv/bin/pip install txtorcon==0.18.0
venv/bin/pip install --editable .
}
function create_tahoelafs_introducer {
introducer_dir="$1"
2017-03-05 21:38:37 +01:00
if [ -f ${introducer_dir}/tahoe.cfg ]; then
2017-03-05 20:49:18 +01:00
return
fi
2017-03-05 21:46:53 +01:00
su -c "mkdir ${introducer_dir}" - tahoelafs
2017-03-05 20:49:18 +01:00
su -c "$TAHOE_COMMAND create-introducer -C ${introducer_dir} --hide-ip --hostname=127.0.0.1" - tahoelafs
}
2017-03-05 14:07:45 +01:00
2017-03-05 20:49:18 +01:00
function create_tahoelafs_storage_node {
# Nodes can store introducer
node_dir="$1"
furl="$2"
if [ ${#furl} -eq 0 ]; then
return
2017-03-05 14:07:45 +01:00
fi
2017-03-05 20:49:18 +01:00
2017-03-05 21:38:37 +01:00
if [ -f ${node_dir}/tahoe.cfg ]; then
2017-03-05 20:49:18 +01:00
return
fi
2017-03-05 21:46:53 +01:00
su -c "mkdir ${node_dir}" - tahoelafs
2017-03-05 20:49:18 +01:00
su -c "$TAHOE_COMMAND create-node -C ${node_dir} --introducer=\"$furl\" --listen=tor --hide-ip --hostname=127.0.0.1" - tahoelafs
}
function create_tahoelafs_client {
# Clients have no storage
client_dir="$1"
furl="$2"
if [ ${#furl} -eq 0 ]; then
return
fi
2017-03-05 21:38:37 +01:00
if [ -f ${client_dir}/tahoe.cfg ]; then
2017-03-05 20:49:18 +01:00
return
fi
2017-03-05 21:46:53 +01:00
su -c "mkdir ${client_dir}" - tahoelafs
2017-03-05 20:49:18 +01:00
su -c "$TAHOE_COMMAND create-client -C ${client_dir} --introducer=\"$furl\" --listen=tor --hide-ip --hostname=127.0.0.1" - tahoelafs
sed -i 's|reveal-IP-address =.*|reveal-IP-address = False|g' $client_dir/tahoe.cfg
sed -i 's|tub.port =.*|tub.port = disabled|g' $client_dir/tahoe.cfg
sed -i 's|tub.location =.*|tub.location = disabled|g' $client_dir/tahoe.cfg
}
function get_tahoelafs_introducer {
echo "$(cat /home/tahoelafs/.tahoe-introducer/private/introducer.furl)"
}
function create_tahoelafs_daemon {
daemon_name=$1
2017-03-05 22:11:15 +01:00
TAHOELAFS_DAEMON_FILE=/etc/systemd/system/tahoelafs-${daemon_name}.service
2017-03-05 22:05:03 +01:00
echo "Creating daemon: $TAHOELAFS_DAEMON_FILE"
2017-03-05 20:49:18 +01:00
echo '[Unit]' > $TAHOELAFS_DAEMON_FILE
echo "Description=Tahoe-LAFS ${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
echo 'After=syslog.target' >> $TAHOELAFS_DAEMON_FILE
echo 'After=network.target' >> $TAHOELAFS_DAEMON_FILE
echo '' >> $TAHOELAFS_DAEMON_FILE
echo '[Service]' >> $TAHOELAFS_DAEMON_FILE
echo 'Type=simple' >> $TAHOELAFS_DAEMON_FILE
echo "User=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
2017-03-05 22:09:38 +01:00
echo "Group=debian-tor" >> $TAHOELAFS_DAEMON_FILE
2017-03-05 20:49:18 +01:00
echo "WorkingDirectory=/home/tahoelafs/tahoelafs" >> $TAHOELAFS_DAEMON_FILE
echo "ExecStart=/home/tahoelafs/tahoelafs/venv/bin/tahoe run /home/tahoelafs/${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
echo "ExecStop=/home/tahoelafs/tahoelafs/venv/bin/tahoe stop /home/tahoelafs/${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
echo 'Restart=on-failure' >> $TAHOELAFS_DAEMON_FILE
echo 'RestartSec=10' >> $TAHOELAFS_DAEMON_FILE
echo "Environment=\"USER=tahoelafs\" \"HOME=/home/tahoelafs\"" >> $TAHOELAFS_DAEMON_FILE
echo '' >> $TAHOELAFS_DAEMON_FILE
echo '[Install]' >> $TAHOELAFS_DAEMON_FILE
echo 'WantedBy=multi-user.target' >> $TAHOELAFS_DAEMON_FILE
systemctl enable tahoelafs-${daemon_name}
systemctl daemon-reload
systemctl start tahoelafs-${daemon_name}
2017-03-05 14:07:45 +01:00
}
2016-08-28 00:40:21 +02:00
function install_tahoelafs {
if [ $INSTALLING_MESH ]; then
return
fi
2017-03-04 18:49:44 +01:00
apt-get -yq install build-essential python-pip python-dev libffi-dev libssl-dev
2017-03-04 19:18:07 +01:00
apt-get -yq install libcrypto++-dev python-pycryptopp python-cffi python-virtualenv
# create a user to run the introducer
if [ ! -d /home/tahoelafs ]; then
# add a gogs user account
adduser --disabled-login --gecos 'tahoe-lafs' tahoelafs
2017-03-05 15:01:44 +01:00
adduser tahoelafs debian-tor
fi
if [ -d /home/tahoelafs/Maildir ]; then
rm -rf /home/tahoelafs/Maildir
fi
2017-03-05 20:49:18 +01:00
install_tahoelafs_to_directory /home/tahoelafs/tahoelafs
#configure_firewall_for_tahoelafs
2017-03-04 21:45:19 +01:00
# remove files we don't need
2017-03-04 21:49:49 +01:00
rm -rf /home/tahoelafs/.mutt
2017-03-04 21:45:19 +01:00
rm /home/tahoelafs/.emacs-mutt
rm /home/tahoelafs/.muttrc
rm /home/tahoelafs/.mutt-alias
rm /home/tahoelafs/.procmailrc
# set permissions
2017-03-05 15:01:44 +01:00
chown -R tahoelafs:debian-tor /home/tahoelafs
2017-03-04 21:45:19 +01:00
# create the introducer config
2017-03-05 20:49:18 +01:00
create_tahoelafs_introducer /home/tahoelafs/introducer
TAHOELAFS_INTRODUCER_CONFIG=/home/tahoelafs/introducer/tahoe.cfg
if [ ! -f $TAHOELAFS_INTRODUCER_CONFIG ]; then
2017-03-05 21:38:37 +01:00
echo $'Unable to create introducer'
exit 62831
fi
2017-03-04 21:45:19 +01:00
# create an onion address
2017-03-04 00:25:38 +01:00
TAHOELAFS_ONION_HOSTNAME=$(add_onion_service tahoelafs ${TAHOELAFS_PORT} ${TAHOELAFS_ONION_PORT})
2017-03-05 20:49:18 +01:00
# start the introducer
tahoelafs_setup_config $TAHOELAFS_INTRODUCER_CONFIG ${PROJECT_NAME}-introducer
su -c '/home/tahoelafs/tahoelafs/venv/bin/python2 /home/tahoelafs/tahoelafs/venv/bin/tahoe start /home/tahoelafs/introducer' - tahoelafs
TAHOELAFS_INTRODUCER=/home/tahoelafs/introducer/tahoe-introducer.tac
2017-03-05 15:01:44 +01:00
if [ ! -f $TAHOELAFS_INTRODUCER ]; then
echo $'Introducer file not found'
2017-03-05 21:38:37 +01:00
exit 9654845
2017-03-05 15:01:44 +01:00
fi
2017-03-05 22:05:03 +01:00
create_tahoelafs_daemon "introducer"
2017-03-05 15:01:44 +01:00
2017-03-05 20:49:18 +01:00
# start the storage node
2017-03-05 22:05:03 +01:00
if [ ! -d /home/tahoelafs/storage ]; then
mkdir /home/tahoelafs/storage
fi
create_tahoelafs_storage_node /home/tahoelafs/storage "$(get_tahoelafs_introducer)"
TAHOELAFS_STORAGE_CONFIG=/home/tahoelafs/storage/tahoe.cfg
if [ ! -f $TAHOELAFS_STORAGE_CONFIG ]; then
echo $'Unable to create storage node'
exit 782523
fi
2017-03-05 20:49:18 +01:00
su -c '/home/tahoelafs/tahoelafs/venv/bin/python2 /home/tahoelafs/tahoelafs/venv/bin/tahoe start /home/tahoelafs/storage' - tahoelafs
2017-03-05 22:05:03 +01:00
create_tahoelafs_daemon "storage"
2017-03-03 19:48:33 +01:00
2017-03-05 20:49:18 +01:00
set_completion_param "tahoelafs commit" "$TAHOELAFS_COMMIT"
2017-03-05 13:09:45 +01:00
APP_INSTALLED=1
2016-08-28 00:40:21 +02:00
}
# NOTE: deliberately no exit 0