Torrent Distribution with WPKG

Generating a torrent

I used python3 and py3createtorrent [1].

The following line generates a Matlab torrent seed file, with 8MiB chunks, one level up from the R2013a directory, so that the torrent creates "R2013a" when unpacked. It excludes all the win64 folders from the torrent so this particular example is for x86 only.

cd /mnt/REDACT/Matlab/
python3 /usr/local/bin/py3createtorrent/py3createtorrent.py -p 8192 -f -v R2013a localhost --exclude-pattern=win64$ -o /mnt/REDACT/torrents/Matlab#R2013a.x86.torrent

The Batch File

This batch file is placed at a path on your software repository.

TRACKER should be set to your bittorrent tracker, unless you use LPD (local peer discovery), which is enabled in the aria2c command.

ARIA should be set to the path to your aria2c binary.

Call script with PATH\TO\torrent.cmd PATH\TO\file.torrent PATH\TO\localsavepath.

@echo off

set TORRENTFILE=%~1
set DEST=%~2

set TRACKER=http://SDFSDFSDFSDF:6969

:: subroutine handling
if "%3"=="seed" goto seed

:: allow aria2c free rein
netsh advfirewall firewall add rule name="aria-in" program="%ARIA%" dir=in action=allow enable=yes >NUL
netsh advfirewall firewall add rule name="aria-out" program="%ARIA%" dir=out action=allow enable=yes >NUL
goto fetch

:seed
cd "%DEST%\.."
%ARIA% "%TORRENTFILE%" --seed-ratio=0 --enable-dht=false --bt-enable-lpd=true --bt-seed-unverified=true --seed-time=10 --bt-exclude-tracker=* --bt-tracker=%TRACKER%/announce 
exit

:fetch
if not exist "%DEST%" mkdir "%DEST%"
cd %DEST%\..
%ARIA% "%TORRENTFILE%" -V --bt-stop-timeout=180 --bt-enable-lpd=true --allow-overwrite=true --enable-dht=false --seed-time=0 --file-allocation=none --bt-exclude-tracker=* --bt-tracker=%TRACKER%/announce
set RETURN=%ERRORLEVEL%

:: be a good neighbour - seed for 10 minutes to provide help to peers, particularly to nearby peers
if %RETURN%==0 (
	start /min %0 "%TORRENTFILE%" "%DEST%" seed
	:: future use - setup mode nexgen fix seeding for imaging; spin up startup seeds
	echo "%TORRENTFILE%","%DEST%" >> c:\nexgen\local-cache\completed-torrents.cmd
)

:: if RETURN=1, the most likely error is some problem with the DEST; get it on the next run
if %RETURN%==1 rmdir /s /q "%DEST%"

:end
netsh advfirewall firewall delete rule name="aria-in" >NUL
netsh advfirewall firewall delete rule name="aria-out" >NUL
exit /B %RETURN%

Setting up the server

You need a server somewhere set up with some kind of tracker (unless you use LPD); I used opentracker, which is trivially easy to set up and runs on port 6969. [2], and aria2c, available as a package from your local vendor.


On the server, you need access to a seed copy of the software. I did this by mounting it via CIFS from our central server (as I need to support robocopy still for other departments), but a local copy somewhere on the machine is also perfectly good; better, even.

We decided to put the torrent files on a separate folder in our repository.

Then, start the torrent. For example:

aria2c -T /mnt/REDACT/torrents/Matlab#R2012a.x64.torrent -d /mnt/REDACT/Matlab --bt-seed-unverified --seed-ratio=0 --bt-enable-lpd --bt-exclude-tracker=\* --bt-tracker=http://REDACT:6969/announce

I have a startup script for this but I have to sanitize it.

Testing

A simple batch file for testing.

@echo off
set ARIA=c:\REDACT\aria2\amd64\aria2c.exe
set REPO=\\REDACT\sdp$

set PKG_PATH=MATLAB\R2013a
set TORRENT=%REPO%\torrents\Matlab#R2013a.x64.torrent

set DEST=c:\REDACT\local-cache\%PKG_PATH%

if "%1"=="seed" (
	start /min %~dp0torrent.cmd "%TORRENT%" "%DEST%" seed
) else (
	rmdir /s /q %DEST%
	%~dp0torrent.cmd "%TORRENT%" "%DEST%"
)

Example WPKG

<?xml version="1.0" encoding="UTF-8"?>
<packages>

<package
  id="torrent_test"
  name="Torrent download tester"
  revision="2013060605"
  reboot="false"
  priority="0">
			
    <variable name="PKG_PATH" value="Adobe_Creative_Suite\CS5.5\CS5_5_DP_64" />
    <variable name="LOG_PREFIX" value="%LOGPATH%\%LOGPREFIX%-torrent" />
    <variable name="LOG_TORRENT" value="%LOG_PREFIX%-dl.log" /> 
	
    <check type="file" condition="exists" path="%LOCALCACHE%\%PKG_PATH%" />
	
    <install cmd='cmd /c rmdir /s /q %LOCALCACHE%\%PKG_PATH%' />
    <install cmd='%TORRENTCMD% "%SDP%\torrents\Adobe_Creative_Suite#CS5.5#CS5_5_DP_64.torrent" "%LOCALCACHE%\%PKG_PATH%" > %LOG_TORRENT%' />
	
    <upgrade include='install' />
</package>

</packages>