268 lines
12 KiB
Python
268 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Este script crea los archivos ".size", ".sum", ".full.sum" y ".torrent" para la imagen que recibe como parámetro.
|
|
Hace casi lo mismo que el script bash original (cuyo nombre es "torrent-creator"), pero añade el archivo ".size".
|
|
Al acabar, llama al script "updateRepoInfo.py", para actualizar la información del repositorio.
|
|
|
|
Debería ser llamado por ogCore u ogLive cada vez que se cree una imagen.
|
|
|
|
Parámetros
|
|
------------
|
|
sys.argv[1] - Nombre completo de la imagen.
|
|
- Ejemplo1: image1.img
|
|
- Ejemplo2: /opt/opengnsys/ogrepository/images/image1.img
|
|
|
|
Sintaxis
|
|
----------
|
|
./createTorrentSum.py image_name|/image_path/image_name
|
|
|
|
Ejemplos
|
|
---------
|
|
./createTorrentSum.py image1.img
|
|
./createTorrentSum.py /opt/opengnsys/ogrepository/images/image1.img
|
|
"""
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# IMPORTS
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import hashlib
|
|
from systemd import journal
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# VARIABLES
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
script_name = os.path.basename(__file__)
|
|
repo_path = '/opt/opengnsys/ogrepository/images/' # No borrar la barra final
|
|
config_file = '/opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg'
|
|
update_repo_script = '/opt/opengnsys/ogrepository/bin/updateRepoInfo.py'
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# FUNCTIONS
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
|
|
def show_help():
|
|
""" Imprime la ayuda, cuando se ejecuta el script con el parámetro "help".
|
|
"""
|
|
help_text = f"""
|
|
Sintaxis: {script_name} image_name|/image_path/image_name
|
|
Ejemplo1: {script_name} image1.img
|
|
Ejemplo2: {script_name} /opt/opengnsys/ogrepository/images/image1.img
|
|
"""
|
|
print(help_text)
|
|
|
|
|
|
def check_params():
|
|
""" Comprueba que se haya enviado la cantidad correcta de parámetros, y en el formato correcto.
|
|
Si no es así, muestra un mensaje de error, y sale del script.
|
|
LLama a la función "show_help" cuando se ejecuta el script con el parámetro "help".
|
|
"""
|
|
# Si se ejecuta el script con el parámetro "help", se muestra la ayuda, y se sale del script:
|
|
if len(sys.argv) == 2 and sys.argv[1] == "help":
|
|
show_help()
|
|
sys.exit(0)
|
|
# Si se ejecuta el script con más o menos de 1 parámetro, se muestra un error y la ayuda, y se sale del script:
|
|
elif len(sys.argv) != 2:
|
|
print(f"{script_name} Error: Formato incorrecto: Se debe especificar 1 parámetro")
|
|
show_help()
|
|
sys.exit(1)
|
|
|
|
|
|
def build_file_path():
|
|
""" Construye la ruta completa al archivo de imagen
|
|
(agregando "/opt/opengnsys/ogrepository/images/" si no se ha especificado en el parámetro).
|
|
"""
|
|
param_path = sys.argv[1]
|
|
# Construimos la ruta completa:
|
|
if not param_path.startswith(repo_path):
|
|
file_path = os.path.join(repo_path, param_path)
|
|
else:
|
|
file_path = param_path
|
|
return file_path
|
|
|
|
|
|
def get_md5_sum(file_path, megabytes=1):
|
|
""" Calcula y retorna el hash MD5 del último MB del archivo de imagen que recibe como parámetro.
|
|
Se utiliza para crear el archivo ".sum" (para transferencias Unicast y Multicast).
|
|
"""
|
|
hash_md5 = hashlib.md5()
|
|
with open(file_path, "rb") as f:
|
|
f.seek(-megabytes * 1024 * 1024, os.SEEK_END)
|
|
data = f.read(megabytes * 1024 * 1024)
|
|
hash_md5.update(data)
|
|
return hash_md5.hexdigest()
|
|
|
|
|
|
def get_md5_fullsum(file_path):
|
|
""" Calcula y retorna el hash MD5 del archivo de imagen que recibe como parámetro.
|
|
Se utiliza para crear el archivo ".full.sum" (para transferencias P2P).
|
|
"""
|
|
hash_md5 = hashlib.md5()
|
|
with open(file_path, "rb") as f:
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
hash_md5.update(chunk)
|
|
return hash_md5.hexdigest()
|
|
|
|
|
|
def get_IPlocal():
|
|
""" Retorna la IP asociada a la variable "IPlocal", del archivo '/opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg'
|
|
(que corresponde a la IP del repositorio).
|
|
"""
|
|
with open(config_file, 'r') as file:
|
|
for line in file:
|
|
if line.startswith('IPlocal'):
|
|
IPlocal = line.split('=')[1].strip()
|
|
return IPlocal
|
|
|
|
|
|
def create_torrent(file_path, torrent_file, datafullsum):
|
|
""" Crea un archivo ".torrent" para la imagen que recibe como primer parámetro.
|
|
Obtiene la IP del repositorio llamando a la función "get_IPlocal",
|
|
que a su vez la obtiene del archivo '/opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg'.
|
|
"""
|
|
# Almacenamos la IP del repositorio, y construimos la URL del tracker:
|
|
repo_ip = get_IPlocal()
|
|
tracker_url = f"http://{repo_ip}:6969/announce"
|
|
|
|
# Creamos una lista con el comando para crear el torrrent, y lo imprimimos con espacios:
|
|
splitted_cmd = f"nice -n 0 ctorrent -t {file_path} -u {tracker_url} -s {torrent_file} -c {datafullsum} -l 4194304".split()
|
|
print(f"Sending command: {' '.join(splitted_cmd)}")
|
|
|
|
# Ejecutamos el comando en el sistema, e imprimimos el resultado:
|
|
try:
|
|
journal.send(f"createTorrentSum.py: Running command: {' '.join(splitted_cmd)}", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
result = subprocess.run(splitted_cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
journal.send(f"createTorrentSum.py: Command ReturnCode: {result.returncode}", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"ReturnCode: {result.returncode}")
|
|
except subprocess.CalledProcessError as error:
|
|
journal.send(f"createTorrentSum.py: Command error: {error}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"ReturnCode: {error.returncode}")
|
|
print(f"Error Output: {error.stderr.decode()}")
|
|
except Exception as error:
|
|
journal.send(f"createTorrentSum.py: Command exception: {error}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"Se ha producido un error inesperado: {error}")
|
|
|
|
|
|
def update_repo_info():
|
|
""" Actualiza la información del repositorio, ejecutando el script "updateRepoInfo.py".
|
|
Como se ve, es necesario que el script se ejecute como sudo, o dará error.
|
|
"""
|
|
try:
|
|
journal.send("createTorrentSum.py: Running script 'updateRepoInfo.py'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
result = subprocess.run(['sudo', 'python3', update_repo_script], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
except subprocess.CalledProcessError as error:
|
|
journal.send(f"createTorrentSum.py: 'updateRepoInfo.py' error: {error}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"Error Output: {error.stderr.decode()}")
|
|
sys.exit(3)
|
|
except Exception as error:
|
|
journal.send(f"createTorrentSum.py: 'updateRepoInfo.py' exception: {error}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print(f"Se ha producido un error inesperado: {error}")
|
|
sys.exit(4)
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
# MAIN
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
|
|
def main():
|
|
"""
|
|
"""
|
|
# Evaluamos si se ha enviado la cantidad correcta de parámetros, y en el formato correcto:
|
|
check_params()
|
|
|
|
# Obtenemos la ruta completa de la imagen:
|
|
file_path = build_file_path()
|
|
|
|
# Si no existe el archivo de imagen, imprimimos un mensaje de error y salimos del script:
|
|
if not os.path.exists(file_path):
|
|
journal.send("createTorrentSum.py: Image not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Image file doesn't exist")
|
|
sys.exit(2)
|
|
|
|
# Si la imagen está bloqueada, imprimimos un mensaje de error y salimos del script:
|
|
if os.path.exists(f"{file_path}.lock"):
|
|
journal.send("createTorrentSum.py: Image is locked", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Image is locked")
|
|
sys.exit(3)
|
|
|
|
# Creamos un archivo de bloqueo:
|
|
journal.send("createTorrentSum.py: Creating '.lock' file...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
open(f"{file_path}.lock", "w").close()
|
|
|
|
# Construimos las rutas completas de los archivos ".size", ".sum", ".full.sum" y ".torrent":
|
|
size_file = f"{file_path}.size"
|
|
sum_file = f"{file_path}.sum"
|
|
fullsum_file = f"{file_path}.full.sum"
|
|
torrent_file = f"{file_path}.torrent"
|
|
|
|
# Creamos el archivo ".size" (pque almacenará el tamaño del archivo), siempre que no exista:
|
|
if not os.path.exists(size_file):
|
|
journal.send("createTorrentSum.py: Creating '.size' file...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Creating '.size' file...")
|
|
with open(size_file, 'w') as file:
|
|
datasize = os.path.getsize(file_path)
|
|
file.write(str(datasize))
|
|
else:
|
|
journal.send("createTorrentSum.py: '.size' file exists", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Size file exists")
|
|
|
|
# Creamos el archivo ".sum" (para transferencias Unicast y Multicast), siempre que no exista:
|
|
if not os.path.exists(sum_file):
|
|
journal.send("createTorrentSum.py: Creating '.sum' file...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Creating '.sum' file...")
|
|
with open(sum_file, 'w') as file:
|
|
datasum = get_md5_sum(file_path)
|
|
file.write(datasum)
|
|
else:
|
|
journal.send("createTorrentSum.py: '.sum' file exists", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Sum file exists")
|
|
|
|
# Creamos el archivo ".full.sum" (para transferencias P2P), siempre que no exista:
|
|
if not os.path.exists(fullsum_file):
|
|
journal.send("createTorrentSum.py: Creating '.full.sum' file...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Creating '.ful.sum' file...")
|
|
with open(fullsum_file, 'w') as file:
|
|
datafullsum = get_md5_fullsum(file_path)
|
|
file.write(datafullsum)
|
|
else:
|
|
journal.send("createTorrentSum.py: '.full.sum' file exists", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Fullsum file exists")
|
|
|
|
# Creamos el archivo ".torrent" (siempre que no exista):
|
|
if not os.path.exists(torrent_file):
|
|
journal.send("createTorrentSum.py: Creating '.torrent' file...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
create_torrent(file_path, torrent_file, datafullsum)
|
|
else:
|
|
journal.send("createTorrentSum.py: '.torrent' file exists", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Torrent file exists")
|
|
|
|
# Eliminamos el archivo de bloqueo:
|
|
journal.send("createTorrentSum.py: Removing '.lock' file...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
os.remove(f"{file_path}.lock")
|
|
|
|
# Actualizamos la información del repositorio, ejecutando el script "updateRepoInfo.py":
|
|
journal.send("createTorrentSum.py: Updating repository info...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api")
|
|
print("Updating Repository Info...")
|
|
update_repo_info()
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# --------------------------------------------------------------------------------------------
|