Last active 1611700355

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