Winand

Deluge setup script for WD My Book Live

Oct 1st, 2012
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. CLEANUP_MSG="#Install failed / Cleanup"
  4. CLEANUP_CPT="##INSTALLATION FAILED"
  5.  
  6. #Cleanup on error
  7. cleanup() {
  8.     echo $CLEANUP_MSG
  9.     pkill -f deluge-state-monitor.sh
  10.     killall deluged deluge-web
  11.     echo "#Purge packages"
  12.     apt-get -y purge deluge-web
  13.     apt-get -y purge deluged
  14.     apt-get -y purge python-libtorrent
  15.     apt-get autoremove
  16.     #Restore sources list if needed
  17.     [ -f /etc/apt/sources.list.saved ] && \
  18.         mv -f /etc/apt/sources.list.saved /etc/apt/sources.list
  19.     #Remove startup links and config
  20.     update-rc.d -f deluge-daemon remove
  21.     rm -rf ~/.config/deluge /etc/init.d/deluge-daemon
  22.     echo $CLEANUP_CPT
  23.     exit 1
  24. }
  25.  
  26. #Check if deluge is installed
  27. test_inst() {
  28.     inst_allr=0
  29.     dpkg -s python-libtorrent 2>/dev/null|grep "install ok installed">/dev/null
  30.     [ $? -eq 0 ] && echo "##python-libtorrent is installed" && (( inst_allr++ ))
  31.     dpkg -s deluged 2>/dev/null|grep "install ok installed">/dev/null
  32.     [ $? -eq 0 ] && echo "##deluged is installed" && (( inst_allr++ ))
  33.     dpkg -s deluge-web 2>/dev/null|grep "install ok installed">/dev/null
  34.     [ $? -eq 0 ] && echo "##deluge-web is installed" && (( inst_allr++ ))
  35.     return $inst_allr
  36. }
  37.  
  38. #Purge deluge
  39. case "$1" in
  40. clean)
  41.     CLEANUP_MSG="#Purging deluge..."
  42.     CLEANUP_CPT="##PURGING COMPLETE"
  43.     cleanup
  44.     exit 0
  45. esac
  46.  
  47. #exit if deluge is already installed
  48. echo "#Pre-install check"
  49. test_inst
  50. [ $? -ne 0 ] && echo "##Failed! Remove above packages first" && exit 1
  51.  
  52. echo "#Backup sources list"
  53. mv /etc/apt/sources.list /etc/apt/sources.list.saved
  54. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  55.  
  56. echo "#Update sources list"
  57. echo deb http://ftp.us.debian.org/debian/ squeeze main >> /etc/apt/sources.list
  58. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  59.  
  60. echo "#Update packages list"
  61. apt-get update
  62. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  63.  
  64. echo "#Install libtorrent"
  65. apt-get --allow-unauthenticated install python-libtorrent
  66. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  67.  
  68. echo "#Update sources list"
  69. echo deb http://ppa.launchpad.net/deluge-team/ppa/ubuntu jaunty main > /etc/apt/sources.list
  70. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  71. if [ "$(dpkg-query -W -f='${Version}' python 2>/dev/null| cut -b -3)" == "2.5" ]; then
  72.     echo deb http://archive.debian.org/debian/ lenny main >> /etc/apt/sources.list
  73. else #python ver is not 2.5, perhaps 2.6
  74.     echo deb http://ftp.us.debian.org/debian/ squeeze main >> /etc/apt/sources.list
  75. fi
  76. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  77.  
  78. echo "#Update packages list"
  79. apt-get update
  80. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  81.  
  82. echo "#Install deluged and deluge-web"
  83. apt-get --allow-unauthenticated install deluged deluge-web
  84. [ $? -ne 0 ] && echo "##Failed!" && cleanup
  85.  
  86. mv -f /etc/apt/sources.list.saved /etc/apt/sources.list
  87. [ $? -ne 0 ] && echo "##Failed to restore sources list (ignored)"
  88.  
  89. echo "#Post-install check"
  90. test_inst
  91. [ $? -ne 3 ] && echo "##Failed!" && cleanup
  92.  
  93. #Create autostart script
  94. rm -f /etc/init.d/deluge-daemon
  95. echo '#!/bin/sh
  96. ### BEGIN INIT INFO
  97. # Provides: deluge-daemon
  98. # Required-Start: $local_fs $remote_fs
  99. # Required-Stop: $local_fs $remote_fs
  100. # Should-Start: $network
  101. # Should-Stop: $network
  102. # Default-Start: 2 3 4 5
  103. # Default-Stop: 0 1 6
  104. # Short-Description: Daemonized version of deluge and webui.
  105. # Description: Starts the deluge daemon
  106. ### END INIT INFO
  107.  
  108. # Author: Adolfo R. Brandes
  109.  
  110. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  111. DESC="Deluge Daemon"
  112. NAME1="deluged"
  113. NAME2="deluge"
  114. DAEMON1=/usr/bin/deluged
  115. DAEMON1_ARGS="-d" # Consult `man deluged` for more options
  116. DAEMON2=/usr/bin/deluge-web
  117. DAEMON2_ARGS="" # Consult `man deluge-web` for more options
  118. PIDFILE1=/var/run/$NAME1.pid
  119. PIDFILE2=/var/run/$NAME2.pid
  120. UMASK=022 # Change this to 0 if running deluged as its own user
  121. DELUGED_USER=root
  122. SCRIPTNAME=/etc/init.d/deluge-daemon
  123.  
  124. # Exit if the package is not installed
  125. [ -x "$DAEMON1" -a -x "$DAEMON2" ] || exit 0
  126.  
  127. # Load the VERBOSE setting and other rcS variables
  128. [ -f /etc/default/rcS ] && . /etc/default/rcS
  129.  
  130. # Define LSB log_* functions.
  131. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  132. . /lib/lsb/init-functions
  133.  
  134. #
  135. # Function that starts the daemon/service
  136. #
  137. do_start() {
  138.     # Return
  139.     # 0 if daemon has been started
  140.     # 1 if daemon was already running
  141.     # 2 if daemon could not be started
  142.     start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --exec $DAEMON1 \
  143.         --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK --test > /dev/null
  144.     RETVAL1="$?"
  145.     start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --exec $DAEMON2 \
  146.         --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK --test > /dev/null
  147.     RETVAL2="$?"
  148.     [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 1
  149.  
  150.     start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --make-pidfile --exec $DAEMON1 \
  151.         --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON1_ARGS
  152.     RETVAL1="$?"
  153.     sleep 2
  154.     start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --make-pidfile --exec $DAEMON2 \
  155.         --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON2_ARGS
  156.     RETVAL2="$?"
  157.     [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 2
  158.    
  159.     sh /$DELUGED_USER/.config/deluge/deluge-state-monitor.sh&
  160. }
  161.  
  162. #
  163. # Function that stops the daemon/service
  164. #
  165. do_stop() {
  166.     # Return
  167.     # 0 if daemon has been stopped
  168.     # 1 if daemon was already stopped
  169.     # 2 if daemon could not be stopped
  170.     # other if a failure occurred
  171.  
  172.     pkill -f deluge-state-monitor.sh
  173.    
  174.     start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE2
  175.     RETVAL2="$?"
  176.     start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE1
  177.     RETVAL1="$?"
  178.     [ "$RETVAL1" = "2" -o "$RETVAL2" = "2" ] && return 2
  179.  
  180.     rm -f $PIDFILE1 $PIDFILE2
  181.  
  182.     [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] && return 0 || return 1
  183. }
  184.  
  185. case "$1" in
  186. start)
  187.     [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME1"
  188.     do_start
  189.     case "$?" in
  190.         0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  191.         2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  192.     esac
  193.     ;;
  194. stop)
  195.     [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME1"
  196.     do_stop
  197.     case "$?" in
  198.         0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  199.         2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  200.     esac
  201.     ;;
  202. restart|force-reload)
  203.     log_daemon_msg "Restarting $DESC" "$NAME1"
  204.     do_stop
  205.     case "$?" in
  206.     0|1)
  207.         do_start
  208.         case "$?" in
  209.             0) log_end_msg 0 ;;
  210.             1) log_end_msg 1 ;; # Old process is still running
  211.             *) log_end_msg 1 ;; # Failed to start
  212.         esac
  213.         ;;
  214.     *)
  215.         # Failed to stop
  216.         log_end_msg 1
  217.         ;;
  218.     esac
  219.     ;;
  220. *)
  221.     echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  222.     exit 3
  223.     ;;
  224. esac
  225. :' >/etc/init.d/deluge-daemon
  226. chmod 755 /etc/init.d/deluge-daemon
  227. update-rc.d deluge-daemon defaults
  228.  
  229. echo "#Setting up configuration..."
  230. deluged
  231. deluge-web&
  232. sleep 7 #Wait 7 sec to make sure that programs have started
  233. killall deluged deluge-web
  234. [ ! -d ~/.config/deluge ] && echo "##Failed! Config dir not found" && cleanup
  235.  
  236. echo "#Set deluged login/password"
  237. echo "#Note: this setting is for desktop client, not web ui"
  238. while [ 1 ]; do
  239.     echo -n ">Login: "; read log1
  240.     echo -n ">Confirm: "; read log2
  241.     if [ $log1 -a $log1 = $log2 ]; then break;
  242.     else echo "##Try again!"; fi
  243. done
  244. while [ 1 ]; do
  245.     echo -n ">Password: "; read pas1
  246.     echo -n ">Confirm: "; read pas2
  247.     if [ $pas1 -a $pas1 = $pas2 ]; then break;
  248.     else echo "##Try again!"; fi
  249. done
  250. echo "$log1:$pas1:10" >> ~/.config/deluge/auth
  251.  
  252. #Allow remote connections
  253. conf_file=~/.config/deluge/core.conf
  254. if [ -f $conf_file ]; then
  255.     sed -i 's/"allow_remote": false/"allow_remote": true/g' $conf_file
  256.     [ $? -ne 0 ] && echo "##Failed to allow remote access (ignored)"
  257.     sed -i 's/"download_location": "\/root"/"download_location": "\/shares\/Public"/g' $conf_file
  258.     [ $? -ne 0 ] && echo "##Failed to change download location (ignored)"
  259. else echo "##Failed to allow remote access and change download location (ignored)"; fi
  260. #Set default connection for web-ui
  261. conf_file=~/.config/deluge/web.conf
  262. if [ -f $conf_file ]; then
  263.     sed -i 's/"default_daemon": ""/"default_daemon": "127.0.0.1:58846"/g' $conf_file
  264.     [ $? -ne 0 ] && echo "##Failed to set default daemon for web-ui (ignored)"
  265. else echo "##Failed to set default daemon for web-ui (ignored)"; fi
  266.  
  267. #Move (if possible) state dir to /var/log so HDD can sleep
  268. echo "#Moving state dir to ram-disk..."
  269. if [ -d /var/log/deluge-state ]; then
  270.     rm -rf ~/.config/deluge/state
  271. elif [ -d ~/.config/deluge/state ]; then
  272.     rm -f /var/log/deluge-state #paranoid
  273.     mv -f ~/.config/deluge/state /var/log/deluge-state
  274. else
  275.     rm -rf ~/.config/deluge/state /var/log/deluge-state
  276.     mkdir /var/log/deluge-state
  277.     echo "##Couldn't move state dir to /var/log! New dir created"
  278. fi
  279. ln -s /var/log/deluge-state ~/.config/deluge/state
  280.  
  281. #Backup torrent list on changes
  282. echo "#Generating state-backup monitor..."
  283. echo 'RAMLCT=/var/log/deluge-state
  284. HDDLCT=/var/log.hdd/deluge-state
  285. [ ! -d $RAMLCT ] && exit 1 #deluge-state dir is present
  286. function do_save() {
  287.     CHKSUM=$(ls $RAMLCT/*.torrent | sum | awk "{print \$1}")
  288.     [ $CHKSUM != $OLDCHKSUM ] && \
  289.         rm -rf $HDDLCT && cp -rf $RAMLCT $HDDLCT
  290.     OLDCHKSUM=$CHKSUM
  291. }
  292. trap "do_save; exit 0" SIGTERM #force copy on exit
  293. OLDCHKSUM=$(ls $RAMLCT/*.torrent | sum | awk "{print \$1}")
  294. while :; do
  295.     sleep 60& wait $!
  296.     [ -f /tmp/standby ] && continue #skip when sleeping
  297.     do_save
  298. done' >~/.config/deluge/deluge-state-monitor.sh
  299. chmod 755 ~/.config/deluge/deluge-state-monitor.sh
  300.  
  301. /etc/init.d/deluge-daemon start
  302. echo "##INSTALLATION COMPLETE"
Advertisement
Add Comment
Please, Sign In to add comment