Last active
January 28, 2021 12:43
-
-
Save alyssadev/4ca6ffbc974b815683cd2df7c44189f5 to your computer and use it in GitHub Desktop.
Provisions a droplet on DigitalOcean to download a given torrent file, then reupload to a given rclone remote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Provisions a droplet on DigitalOcean to download a given torrent file, then reupload to a given rclone remote | |
# Prints estimated USD cost of transaction when complete | |
# uses your user's rclone.conf, so set up the remote locally and make sure it works | |
# torrentdl.sh https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent b2:my-linux-isos/ubuntu/ | |
# Default values | |
IMAGE="ubuntu-20-04-x64" | |
REGION="sgp1" # overridden by ping check | |
HOSTNAME="torrent-dl" | |
URLREGEX='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$' | |
TORRENT="$1" | |
DESTREMOTE="$2" | |
echo "Checking dependencies (wget, installed doctl, configured rclone)" | |
[ -s "$HOME/.config/rclone/rclone.conf" ] || (echo "Set up rclone (from rclone.org, not from package manager) with a remote, or write to ~/.config/rclone/rclone.conf, then run this script again"; exit) | |
if type doctl >/dev/null 2>&1; then | |
if ! doctl compute droplet list >/dev/null 2>&1; then | |
echo doctl not authenticated, running doctl auth init | |
doctl auth init | |
fi | |
else | |
echo 'doctl not found, attempting to install with "go get github.com/digitalocean/doctl/cmd/doctl"' | |
if ! go version >/dev/null 2>&1; then | |
echo golang-go not found, installing gvm and go1.15.6 to ~/.gvm in 5 seconds. Ctrl-C to abort | |
sleep 5 | |
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer) | |
source ~/.gvm/scripts/gvm | |
gvm install go1.4 -B | |
gvm use go1.4 | |
export GOROOT_BOOTSTRAP=$GOROOT | |
gvm install go1.15.6 | |
gvm use --default go1.15.6 | |
fi | |
go get github.com/digitalocean/doctl/cmd/doctl | |
doctl auth init | |
fi | |
if [ -f $HOME/.config/doctl/closest-dc ]; then | |
REGION=`cat $HOME/.config/doctl/closest-dc` | |
else | |
echo "Finding your closest datacenter..." | |
REGION=`doctl compute region list --no-header | grep -v false | awk '{print $1}' | \ | |
while read p; do ping -c4 speedtest-$p.digitalocean.com | grep --color=never rtt | awk -v p=" $p" -F'/' '{print $5 p}'; done | \ | |
sort | head -n1 | awk '{print $2}'` | |
echo $REGION > $HOME/.config/doctl/closest-dc | |
fi | |
if [[ $TORRENT =~ $URLREGEX ]]; then | |
if ! type wget >/dev/null 2>&1; then | |
echo wget not found for downloading torrent file from url, installing from apt using sudo. | |
echo "If this isn't right for your environment, install wget before running script again" | |
sudo apt-get install -y wget || exit | |
fi | |
echo Downloading torrent from url... | |
wget "$TORRENT" -c | |
export TORRENT=`basename "$TORRENT"` | |
fi | |
TORRENTSIZE=`head -1 "$TORRENT" | grep -aoE '6:lengthi[0-9]+' | cut -di -f2 | awk '{s+=$1} END {print s}'` | |
if (($TORRENTSIZE<=30000000000)); then # 30GB | |
SIZE="s-1vcpu-2gb" | |
elif (($TORRENTSIZE<=60000000000)); then # 60GB | |
SIZE="s-2vcpu-4gb" | |
elif (($TORRENTSIZE<=140000000000)); then # 140GB | |
SIZE="s-4vcpu-8gb" | |
else # up to 300GB | |
SIZE="s-8vcpu-16gb" | |
fi | |
if [ -v "$1" ]; then | |
echo Usage: $0 https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent b2:my-linux-isos/ubuntu/ | |
exit | |
fi | |
echo "Downloading '$TORRENT' on $REGION:$IMAGE:$SIZE with hostname $HOSTNAME, then uploading to '$DESTREMOTE'" | |
#read -p "Press enter to continue, ctrl-c to abort..." | |
echo "Started at `date`" | |
export STARTTIME=`date +%s` | |
echo "Provisioning droplet..." | |
export SSHKEYS=`doctl compute ssh-key list --no-header --format ID | tr '\n' ',' | sed 's/,$/\n/'` | |
export IP=$( set -x; doctl compute droplet create $HOSTNAME --ssh-keys $SSHKEYS --region $REGION --size $SIZE --image $IMAGE --wait --no-header --format PublicIPv4 ) | |
echo -n "Droplet at $IP, waiting for connection" | |
until (ssh -o StrictHostKeyChecking=no root@$IP mkdir -p /root/.config/rclone /root/t 2>/dev/null >/dev/null); do | |
echo -n "." | |
sleep 1 | |
done | |
ssh-keyscan -H $IP 2>/dev/null >> ~/.ssh/known_hosts | |
echo | |
echo Copying rclone conf and torrent to droplet... | |
scp ~/.config/rclone/rclone.conf root@$IP:/root/.config/rclone/rclone.conf | |
scp "$TORRENT" root@$IP:/root/t/ | |
echo Installing dependencies... | |
ssh -t root@$IP "rm /var/lib/man-db/auto-update; until (apt-get update; apt-get install -y aria2); do sleep 1; done; curl https://rclone.org/install.sh | sudo bash" | |
#ssh -t root@$IP "apt-get update; apt-get install -y aria2; curl https://rclone.org/install.sh | sudo bash" | |
echo Downloading torrent... | |
ssh -t root@$IP aria2c -d "/root/t/" --enable-dht=false --enable-peer-exchange=false --seed-time=0 "/root/t/*.torrent" | |
echo Uploading torrent to rclone remote... | |
ssh -t root@$IP rclone copy /root/t/ "$DESTREMOTE" -P --fast-list --transfers=$(($LINES-5)) | |
echo Job complete, deleting droplet | |
$(set -x; doctl compute droplet delete -f $HOSTNAME) | |
echo "Finished at `date`" | |
export ENDTIME=`date +%s` | |
echo "Total run time: $(($ENDTIME-$STARTTIME))" | |
echo -n "Total (estimated) cost: USD $" | |
doctl compute size list | grep $SIZE | awk '{print $5}' | while read p; do python3 -c "print(($p/2592000)*($ENDTIME-$STARTTIME))"; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment