101 lines
4.8 KiB
Python
101 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Este script inicia el tracker "bttrack" (o lo reinicia, si ya estuviera iniciado), finalizando previamente cualquier proceso activo, y borrando el archivo "/tmp/dstate".
|
|
En principio, debería hacer lo mismo que el script bash original (cuyo nombre es "torrent-tracker"), que se ejecutaba por cron cada hora.
|
|
Creemos que debe ser llamado únicamente cuando se quiera hacer una descarga mediante P2P (junto al script "runTorrentSeeder.py").
|
|
NOTA: El paquete no hace una búsqueda recursiva, por lo que se debe especificar el subdirectorio correspondiente a la OU, si es el caso.
|
|
|
|
No recibe ningún parámetro.
|
|
"""
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# IMPORTS
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import time
|
|
from systemd import journal
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# VARIABLES
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
repo_path = '/opt/opengnsys/ogrepository/images' # En este caso, no lleva barra final
|
|
|
|
bttrack_port = 6969
|
|
bttrack_dfile = '/tmp/dstate'
|
|
bttrack_log = '/opt/opengnsys/ogrepository/log/bttrack.log'
|
|
bttrack_interval = 10
|
|
bttrack_allow_get = 0 # Este valor impide la descarga desde clientes no autorizados
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# FUNCTIONS
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
|
|
def run_bttrack(repo_path):
|
|
""" Ejecuta el comando "bttrack", con sus parámetros correspondientes.
|
|
Además, captura el resultado y los posibles errores, y los imprime.
|
|
"""
|
|
# Creamos una lista con el comando "bttrack" y sus parámetros, y lo imprimimos con espacios:
|
|
splitted_cmd = f"bttrack --port {bttrack_port} --dfile {bttrack_dfile} --save_dfile_interval {bttrack_interval} --reannounce_interval {bttrack_interval} --logfile {bttrack_log} --allowed_dir {repo_path} --allow_get {bttrack_allow_get}".split()
|
|
print(f"Sending command: {' '.join(splitted_cmd)}")
|
|
journal.send(f"runTorrentTracker.py: Running command: {' '.join(splitted_cmd)}", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
|
|
# Ejecutamos el comando "bttrack" en el sistema, e imprimimos el resultado:
|
|
try:
|
|
result = subprocess.run(splitted_cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
journal.send(f"runTorrentTracker.py: Command ReturnCode: {result.returncode}", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"Bttrack ReturnCode: {result.returncode}")
|
|
except subprocess.CalledProcessError as error:
|
|
journal.send("runTorrentTracker.py: Process finalized", PRIORITY=journal.LOG_WARNING, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"Bttrack ReturnCode: {error.returncode}")
|
|
print(f"Bttrack Error Output: {error.stderr.decode()}")
|
|
except Exception as error:
|
|
journal.send(f"runTorrentTracker.py: Command exception: {error}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"Unexpected bttrack error: {error}")
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# MAIN
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
|
|
def main():
|
|
"""
|
|
"""
|
|
# Finalizamos el proceso "bttrack" (en caso de que estuviera corriendo):
|
|
try:
|
|
journal.send("runTorrentTracker.py: Killing process 'bttrack'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
subprocess.run(f"pkill bttrack".split(), check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
except Exception as error_description:
|
|
journal.send("runTorrentTracker.py: No 'bttrack' process running", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"No bttrack process running? Returned error: {error_description}")
|
|
|
|
# Si existe el archivo "/tmp/dstate", lo eliminamos:
|
|
if os.path.exists(bttrack_dfile):
|
|
journal.send("runTorrentTracker.py: Removing '/tmp/dstate'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
os.remove(bttrack_dfile)
|
|
|
|
# Esperamos 2 segundos:
|
|
time.sleep(2)
|
|
|
|
# Ejecutamos el comando "bttrack" (para hacer tracking de los torrents):
|
|
run_bttrack(repo_path)
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# --------------------------------------------------------------------------------------------
|