Outils du site

Outils pour utilisateurs


Panneau latéral

P2PFR:Wiki

p2p:rtorrent:pyroscope-rtorrent-ps:script-rtmanager (lu 2582 fois)

Ceci est une ancienne révision du document !


rtmanager.sh

Un script fait maison pour accueillir les utilisateurs d'une session SSH sur la seedbox, c'est pas du très propre, encore en développement, et je n'ai pas de dépot git.

Date: 24 janvier 2017

rtmanager.sh
#!/bin/bash
 
 
### ENV + AUTH
 
 
RTBIN="/home/rtorrentuser/bin"
RTCFGOPTS='--config-dir /home/rtorrentuser/.pyroscope -D rtorrent_rc=/home/rtorrentuser/.rtorrent.rc'
RTCMD="$RTBIN/rtcontrol $RTCFGOPTS"
 
function rt_is_admin() {
 
	if [ "$USER" = "rtorrentuser" ]; then
		return 0
	else
		return 1
	fi
}
 
if [ "$USER" = "rtorrentuser" ]; then
	if [ "$#" -eq 0  ]; then
		RTUSER="flexget"
	else
		RTUSER="$1"
	fi
else
		RTUSER="$USER"
fi
 
RTPATHCOMPLETE="/mnt/local/1/downloads/rtorrent/complete"
RTPATHWORK="/mnt/local/1/downloads/rtorrent/work"
RTPATHWATCH="/mnt/local/1/downloads/rtorrent/watch"
RTPATHWATCHHANDLED="/mnt/local/1/downloads/rtorrent/watch_handled"
RTUSERS="david fab kris"
 
 
### COULEURS
 
function test_colors() {
	echo -en "$C0$1$C0\t"
	echo -en "$C1$1$C0\t"
	echo -en "$C2$1$C0\t"
	echo -en "$C3$1$C0\n"
	echo -en "$C4$1$C0\t"
	echo -en "$C5$1$C0\t"
	echo -en "$C6$1$C0\t"
	echo -en "$C7$1$C0\n"
	echo -en "$C8$1$C0\t"
	echo -en "$C9$1$C0\t"
	echo -en "$CA$1$C0\t"
	echo -en "$CB$1$C0\n"
	echo -en "$CC$1$C0\t"
	echo -en "$CD$1$C0\t"
	echo -en "$CE$1$C0\t"
	echo -en "$CF$1$C0\n"
	echo -en "$CG$1$C0\t"
}
 
C0="\033[0m" # No color
C1="\033[0;30m" # Black
C2="\033[0;31m" # Red
C3="\033[0;32m" # Green
C4="\033[0;33m" # Brown
C5="\033[0;34m" # Blue
C6="\033[0;35m" # Purple
C7="\033[0;36m" # Cyan
C8="\033[0;37m" # Light Gray
C9="\033[1;30m" # Dark Gray
CA="\033[1;31m" # Light Red
CB="\033[1;32m" # Light Green
CC="\033[1;33m" # Yellow
CD="\033[1;34m" # Light Blue
CE="\033[1;35m" # Light Purple
CF="\033[1;36m" # Light Cyan
CG="\033[1;37m" # White
 
 
### RTCONTROL
 
 
function rt_print_user_torrents() {
	#$RTCMD -q "custom_targetdir=*$RTPATHCOMPLETE/$RTUSER/*"
	$RTCMD -qO flistall.txt "custom_targetdir=*$RTPATHCOMPLETE/$RTUSER/*"
}
 
function rt_print_user_incomplete() {
	$RTCMD -q "custom_targetdir=*$RTPATHCOMPLETE/$RTUSER/*" "is_complete=no"
}
 
function rt_print_user_complete() {
	$RTCMD -q "custom_targetdir=*$RTPATHCOMPLETE/$RTUSER/*" "is_complete=yes"
}
 
function rt_print_user_trackers() {
	$RTCMD -q "custom_targetdir=*$RTPATHCOMPLETE/$RTUSER/*" -o alias -s alias \* | uniq -c
}
 
function rt_print_download_details() {
	$RTCMD -q ""
}
 
function rt_print_orphans() {
	$RTCMD -qO orphans.txt \*
}
 
function rt_user_search() {
 
	if [ $# -gt 1 ]; then
		return_value=true
		__ret1="$1"
		__ret2="$2"
		shift 2
	fi
 
 
	echo "Rechercher des téléchargements selon quel motif ?"
	echo "Exemple: bisounours"
	echo "Exemple: cité*peur"
 
	echo -n "rechercher> "
	read critere
 
	if [ "$critere" != "" ]; then
 
		critere="*$critere*" # on jokerise, pas pratique sinon
 
		resultats=$(mktemp)
		$RTCMD -q "custom_targetdir=*$RTPATHCOMPLETE/$RTUSER/*" "$critere" | tee $resultats
		let nbresu=$(cat $resultats | wc -l)/2
	fi
 
	if [ "$nbresu" = "0" ]; then
		echo "Aucun résultat trouvé."
	else
		echo "Il y a $nbresu résultat(s)."
	fi
 
 
	[ $return_value ] && eval $__ret1="'$critere'" && eval $__ret2="'$nbresu'"
}
 
function rt_del_user_torrents() {
 
	action="purge"
 
	if [ "$1" = "cull" ]; then
		action="cull"
	fi
 
 
	rt_user_search critere nbresu
 
	if [ "$nbresu" -gt "0" ]; then
 
		let cptsuppr=0
 
		echo "Êtes-vous sûr de vouloir supprimer ces téléchargements ? (o/O/y/Y/n/N)"
		if [ "$action" = "cull" ]; then
			echo -n '!'
		fi
 
		echo -n "supprimer> "
		read confirmation
 
		case $confirmation in
			o|O|y|Y)
				$RTCMD -q "realpath=*$RTPATHCOMPLETE/$RTUSER/*" "$critere" --"$action" --yes && let cptsuppr="$cptsuppr+1" # > /dev/null
											# --cull ou --purge)
				;;
			n|N|*)
				echo "Rien." > /dev/null	
				;;
		esac
 
		if [ "$cptsuppr" -eq "0" ]; then
				echo "Aucun téléchargement supprimé."
		else
				echo "$cptsuppr téléchargement(s) supprimé(s)"
		fi
 
	fi
}
 
# idem que _event.download.finished, sauf qu'on fait le choix de virer les symlinks de /work/
function rt_move_then_restart() {
 
	# vérifier si ce qu'on souhaite déplacer est en fait un lien symbolique
	origin=$($RTCMD "hash=$1" -qo path.strip)
 
	is_sym=0
 
	if [ -L "$origin" ]; then
		is_sym=1
	fi
 
 
        $RTCMD "hash=$1" -q --stop
        $RTCMD "hash=$1" -q --exec "directory.set={{item.custom_targetdir}}" -o custom_targetdir --yes
 
	if [ "$is_sym" -eq "1" ]; then
		rm "$origin"
	else
        	$RTCMD "hash=$1" -q --spawn "mv {{item.path}} {{item.custom_targetdir}}" -o custom_targetdir
	fi
 
        sleep 1
 
        $RTCMD "hash=$1" -q --start
}
 
function rt_test_permissions() {
 
        dir1="$($RTCMD "hash=$1" -qo custom_targetdir)"
 
	if [ ! -w "$dir1" ]; then
		echo "$dir1 n'est pas accessible en écriture."
		return 1
	fi
 
	return 0
}
 
# Si _event.download.finished n'a pas fait son boulot, on peut le faire a posteriori avec cette fonction
# Inconvénient : se lance avec les permissions de $USER
function rt_move_work_torrents() {
	hashlist=$($RTCMD -q "path=*$RTPATHWORK/*" -o hash)
 
	echo "$hashlist"
 
	let cpt=0
	let igncpt=0
 
	if [ "$hashlist" != "" ]; then
 
		for h in "$hashlist"
		do
			if rt_test_permissions "$h"; then
				rt_move_then_restart "$h" && let cpt="$cpt+1"
			else 
				let ign="$ign+1"
			fi
		done
 
		echo "$cpt téléchargement(s) déplacé(s)."
	else
		echo "Aucun téléchargement déplacé."
	fi
 
	if [ $igncpt -gt 0 ]; then
		echo "$igncpt téléchargements ont été ignorés : pas les permissions requises"
	fi
 
}
 
# Il y a un problème avec cette fonction : si t'as pas les permissions en lecture, il va penser que le lien symbolique est cassé et doit être viré.
function rt_del_broken_symlinks() {
	cpt=0
 
	for f in $RTPATHWORK/*
	do
		if [ -h "$f" ]; then
			echo "est symb: $f"
		fi
 
		if [ -r "$f" ]; then
			echo "je peux lire: $f"
		fi
 
		if [ -w "$f" ]; then
			echo "je peux écrire dans: $f"
		fi
 
		if [ -e "$f" ]; then
			echo "il existe ! $f"
		fi
 
 
		# lien sym, lisible, n'existe pas
		if [ -h "$f" -a -r "$f" -a ! -e "$f" ]; then
			echo "bwana: $f"
#			rm "$f" && let cpt=$cpt+1
		fi
	done
 
	echo "$cpt liens cassé(s) supprimé(s)."
}
 
 
### BASHERIES
 
function list_users_complete() {
 
	find "$RTPATHCOMPLETE" -mindepth 2 -maxdepth 3 -path "$RTPATHCOMPLETE/$u*" ! -empty 2>/dev/null | cut -d '/' -f 8-
	# -printf "%H : %f\n"
 
}
 
function list_user_watch() {
 
	echo "Fichiers .torrent dans $RTUSER/watch :"
 
	find "$RTPATHWATCH/$RTUSER" -iname '*.torrent'	| cut -d '/' -f 9-
 
}
 
function list_user_watch_handled() {
 
	echo "Fichiers .torrent archivés de $RTUSER :"
 
	find "$RTPATHWATCHHANDLED/$RTUSER" -iname '*.torrent'	| cut -d '/' -f 9-
 
}
 
 
### MENU
 
 
function menu_welcome() {
 
clear
 
echo -e "Salut $CB$RTUSER$C0,\n"
echo -e "Voici quelques options pour gérer tes téléchargements :\n"
 
}
 
function menu_actions() {
 
echo -e "$CA[ACTIONS]$C0"
cat << EOF
 
	1 : Lister tous les téléchargements
	2 : Lister les téléchargements incomplets
	3 : Lister les téléchargements terminés
	4 : Rechercher des téléchargements selon leur nom
	5 : Statistiques des trackers
 
	6 : Supprimer des téléchargements sans supprimer les fichiers terminés
	7 : /!\ Supprimer des téléchargements, y compris les fichiers terminés
 
	8 : Lister les fichiers complets de tous les utilisateurs
	9 : Lister les fichiers .torrent dans watch
	10 : Lister les fichiers .torrent archivés
 
EOF
 
rt_is_admin && menu_actions_admin
 
echo -e "$CA[MENU]$C0"
cat << EOF
 
	q ou Q : Pour quitter ce menu (accès au shell)
	m ou M : Pour réafficher ce menu
	z ou Z : Pour afficher un dinosaure
 
EOF
 
}
 
function menu_actions_admin() {
 
echo -e "$CA[ADMIN]$C0"
cat << EOF
 
	a ou A : Lister les fichiers orphelins (non chargés dans rtorrent)
	b ou B : Déplacer les fichiers qui pour une raison X sont restés dans /work/
	c ou C : Retirer les liens symboliques cassés de /work/
 
EOF
 
}
 
function menu_dinosaur() {
	num=$(( ( RANDOM % 8 ) + 1 ))
 
#	cola=$(( ( RANDOM % 2 ) ))
	colb=$(( ( RANDOM % 8 ) ))
	cola=1
 
	echo -ne "\033[$cola;3$colb"m
	cat $RTBIN/dinosaur.$num
	echo -ne "\033[0m"
 
}
 
function menu_unknown_option() {
	echo "Option inconnue."
	sleep 1
	clear
	menu_actions
}
 
function menu_ask() {
 
	echo -n "> "
 
	read choice
 
	case $choice in
		1)
			rt_print_user_torrents
			;;
		2)
			rt_print_user_incomplete
			;;
		3)
			rt_print_user_complete
			;;
		4)
			rt_user_search
			;;
		5)
			rt_print_user_trackers
			;;
		6)
			rt_del_user_torrents purge
			;;
		7)
			rt_del_user_torrents cull
			;;
		8)
			list_users_complete
			;;
		9)
			list_user_watch
			;;
		10)
			list_user_watch_handled
			;;
		a|A)
			rt_is_admin && rt_print_orphans || menu_unknown_option
			;;
		b|B)
			rt_is_admin && rt_move_work_torrents || menu_unknown_option
			;;
		c|C)
			rt_is_admin && rt_del_broken_symlinks || menu_unknown_option
			;;
		q|Q)
			echo "Entrez rtmanager.sh pour relancer le script de gestion rtorrent."
			exit 0
			;;
		m|M)
			clear
			menu_actions
			menu_ask
			;;
		z|Z)
			menu_dinosaur
			;;
		"")
			menu_ask
			;;
		*)
			menu_unknown_option
			;;
	esac
}
 
menu_welcome
menu_actions
 
#test_colors "Youpi!"
 
while :
do
	menu_ask
done
p2p/rtorrent/pyroscope-rtorrent-ps/script-rtmanager.1485253530.txt.gz · Dernière modification: le 24/01/2017 à 11h25 de 111110101011