Last active 1611801809

Provisions a droplet on DigitalOcean to download a given torrent file, then reupload to a given rclone remote

digitalocean-torrentdl.sh Raw
1#!/bin/bash
2# Provisions a droplet on DigitalOcean to download a given torrent file, then reupload to a given rclone remote
3# Prints estimated USD cost of transaction when complete
4# uses your user's rclone.conf, so set up the remote locally and make sure it works
5# torrentdl.sh https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent b2:my-linux-isos/ubuntu/
6
7# Default values
8IMAGE="ubuntu-20-04-x64"
9REGION="sgp1" # overridden by ping check
10HOSTNAME="torrent-dl"
11URLREGEX='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'
12TORRENT="$1"
13DESTREMOTE="$2"
14
15echo "Checking dependencies (wget, installed doctl, configured rclone)"
16[ -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)
17
18if type doctl >/dev/null 2>&1; then
19 if ! doctl compute droplet list >/dev/null 2>&1; then
20 echo doctl not authenticated, running doctl auth init
21 doctl auth init
22 fi
23else
24 echo 'doctl not found, attempting to install with "go get github.com/digitalocean/doctl/cmd/doctl"'
25 if ! go version >/dev/null 2>&1; then
26 echo golang-go not found, installing gvm and go1.15.6 to ~/.gvm in 5 seconds. Ctrl-C to abort
27 sleep 5
28 bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
29 source ~/.gvm/scripts/gvm
30 gvm install go1.4 -B
31 gvm use go1.4
32 export GOROOT_BOOTSTRAP=$GOROOT
33 gvm install go1.15.6
34 gvm use --default go1.15.6
35 fi
36 go get github.com/digitalocean/doctl/cmd/doctl
37 doctl auth init
38fi
39
40if [ -f $HOME/.config/doctl/closest-dc ]; then
41 REGION=`cat $HOME/.config/doctl/closest-dc`
42else
43 echo "Finding your closest datacenter..."
44 REGION=`doctl compute region list --no-header | grep -v false | awk '{print $1}' | \
45 while read p; do ping -c4 speedtest-$p.digitalocean.com | grep --color=never rtt | awk -v p=" $p" -F'/' '{print $5 p}'; done | \
46 sort | head -n1 | awk '{print $2}'`
47 echo $REGION > $HOME/.config/doctl/closest-dc
48fi
49
50if [[ $TORRENT =~ $URLREGEX ]]; then
51 if ! type wget >/dev/null 2>&1; then
52 echo wget not found for downloading torrent file from url, installing from apt using sudo.
53 echo "If this isn't right for your environment, install wget before running script again"
54 sudo apt-get install -y wget || exit
55 fi
56 echo Downloading torrent from url...
57 wget "$TORRENT" -c
58 export TORRENT=`basename "$TORRENT"`
59fi
60
61TORRENTSIZE=`head -1 "$TORRENT" | grep -aoE '6:lengthi[0-9]+' | cut -di -f2 | awk '{s+=$1} END {print s}'`
62if (($TORRENTSIZE<=30000000000)); then # 30GB
63 SIZE="s-1vcpu-2gb"
64elif (($TORRENTSIZE<=60000000000)); then # 60GB
65 SIZE="s-2vcpu-4gb"
66elif (($TORRENTSIZE<=140000000000)); then # 140GB
67 SIZE="s-4vcpu-8gb"
68else # up to 300GB
69 SIZE="s-8vcpu-16gb"
70fi
71
72if [ -v "$1" ]; then
73 echo Usage: $0 https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent b2:my-linux-isos/ubuntu/
74 exit
75fi
76
77echo "Downloading '$TORRENT' on $REGION:$IMAGE:$SIZE with hostname $HOSTNAME, then uploading to '$DESTREMOTE'"
78#read -p "Press enter to continue, ctrl-c to abort..."
79
80echo "Started at `date`"
81export STARTTIME=`date +%s`
82
83echo "Provisioning droplet..."
84export SSHKEYS=`doctl compute ssh-key list --no-header --format ID | tr '\n' ',' | sed 's/,$/\n/'`
85export IP=$( set -x; doctl compute droplet create $HOSTNAME --ssh-keys $SSHKEYS --region $REGION --size $SIZE --image $IMAGE --wait --no-header --format PublicIPv4 )
86
87echo -n "Droplet at $IP, waiting for connection"
88until (ssh -o StrictHostKeyChecking=no root@$IP mkdir -p /root/.config/rclone /root/t 2>/dev/null >/dev/null); do
89 echo -n "."
90 sleep 1
91done
92ssh-keyscan -H $IP 2>/dev/null >> ~/.ssh/known_hosts
93echo
94
95echo Copying rclone conf and torrent to droplet...
96scp ~/.config/rclone/rclone.conf root@$IP:/root/.config/rclone/rclone.conf
97scp "$TORRENT" root@$IP:/root/t/
98
99echo Installing dependencies...
100ssh -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"
101#ssh -t root@$IP "apt-get update; apt-get install -y aria2; curl https://rclone.org/install.sh | sudo bash"
102echo Downloading torrent...
103ssh -t root@$IP aria2c -d "/root/t/" --enable-dht=false --enable-peer-exchange=false --seed-time=0 "/root/t/*.torrent"
104echo Uploading torrent to rclone remote...
105ssh -t root@$IP rclone copy /root/t/ "$DESTREMOTE" -P --fast-list --transfers=$(($LINES-5))
106echo Job complete, deleting droplet
107$(set -x; doctl compute droplet delete -f $HOSTNAME)
108
109echo "Finished at `date`"
110export ENDTIME=`date +%s`
111echo "Total run time: $(($ENDTIME-$STARTTIME))"
112echo -n "Total (estimated) cost: USD $"
113doctl compute size list | grep $SIZE | awk '{print $5}' | while read p; do python3 -c "print(($p/2592000)*($ENDTIME-$STARTTIME))"; done