From b245b5b87791b9cb2dd4528c6d6035f626cd8be4 Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Tue, 12 Nov 2024 17:48:38 +0100 Subject: [PATCH] refs #1101 add ogGetRepoIp() --- client/lib/engine/bin/NetLib.py | 107 +++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 22 deletions(-) diff --git a/client/lib/engine/bin/NetLib.py b/client/lib/engine/bin/NetLib.py index 49ea950..f42961d 100755 --- a/client/lib/engine/bin/NetLib.py +++ b/client/lib/engine/bin/NetLib.py @@ -2,9 +2,19 @@ import subprocess import sys import os +import ogGlobals import NetLib import SystemLib + + +#/** +# ogChangeRepo IPREPO [ OgUnit ] +#@brief Cambia el repositorio para el recurso remoto images. +#@param 1 Ip Repositorio +#@param 2 Abreviatura Unidad Organizativa +#@return Cambio recurso remoto en OGIMG. +#*/ def ogChangeRepo(): SRCIMG = "" NEWREPO = "" @@ -61,7 +71,16 @@ def ogChangeRepo(): ogConnect(REPO, ogprotocol, SRCIMG, OGIMG, RW) SystemLib.ogRaiseError("session", OG_ERR_REPO, NEWREPO) return subprocess.returncode - + + +#/** +# ogGetGroupDir [ str_repo ] +#@brief Devuelve el camino del directorio para el grupo del cliente. +#@param str_repo repositorio de imágenes (opcional) +#@return path_dir - Camino al directorio del grupo. +#@note repo = { REPO, CACHE } REPO por defecto +#@exception OG_ERR_FORMAT formato incorrecto. +#*/ def ogGetGroupDir(): REPO = "" DIR = "" @@ -84,6 +103,12 @@ def ogGetGroupDir(): return 0 + +#/** +# ogGetGroupName +#@brief Devuelve el nombre del grupo al que pertenece el cliente. +#@return str_group - Nombre de grupo. +#*/ def ogGetGroupName(): if len(sys.argv) >= 2 and sys.argv[1] == "help": SystemLib.ogHelp("ogGetGroupName", "ogGetGroupName", "ogGetGroupName => Grupo1") @@ -94,6 +119,12 @@ def ogGetGroupName(): return 0 + +#/** +# ogGetHostname +#@brief Muestra el nombre del cliente. +#@return str_host - nombre de máquina +#*/ ## def ogGetHostname(): HOST = "" @@ -126,6 +157,13 @@ def ogGetHostname(): if HOST: print(HOST) + +#/** +# ogGetIpAddress +#@brief Muestra la dirección IP del sistema +#@return str_ip - Dirección IP +#@note Usa las variables utilizadas por el initrd "/etc/net-ethX.conf +#*/ ## def ogGetIpAddress(): IP = "" @@ -150,6 +188,12 @@ def ogGetIpAddress(): return 0 + +#/** +# ogGetMacAddress +#@brief Muestra la dirección Ethernet del cliente. +#@return str_ether - Dirección Ethernet +#*/ ## def ogGetMacAddress(): MAC = "" @@ -171,6 +215,13 @@ def ogGetMacAddress(): return 0 + +#/** +# ogGetNetInterface +#@brief Muestra la interfaz de red del sistema +#@return str_interface - interfaz de red +#@note Usa las variables utilizadas por el initrd "/etc/net-ethX.conf +#*/ ## def ogGetNetInterface(): if len(sys.argv) >= 2 and sys.argv[1] == "help": SystemLib.ogHelp("ogGetNetInterface", "ogGetNetInterface", "ogGetNetInterface => eth0") @@ -181,32 +232,35 @@ def ogGetNetInterface(): return 0 -def ogRepoIp(): - # Variables locales. - SOURCE = "" - FSTYPE = "" - # Mostrar ayuda. - if len(sys.argv) >= 2 and sys.argv[1] == "help": - SystemLib.ogHelp("ogRepoIp", "ogRepoIp", "ogRepoIp => 192.168.0.2") - return +#/** +# ogGetRepoIp +#@brief Muestra la dirección IP del repositorio de datos. +#@return str_ip - Dirección IP +#*/ ## +def ogGetRepoIp(): + out = subprocess.run (["findmnt", "--json", "--output", "SOURCE,FSTYPE", ogGlobals.OGIMG], capture_output=True, text=True).stdout + try: + j = json.loads (out) + except json.decoder.JSONDecodeError: + return None - # Obtener direcciones IP, según el tipo de montaje. - output = subprocess.run(["findmnt", "-P", "-o", "SOURCE,FSTYPE", OGIMG], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode().strip() - lines = output.split("\n") - for line in lines: - fields = line.split() - if len(fields) == 2: - if fields[1] == "nfs": - SOURCE = fields[0].split(":")[0] - elif fields[1] == "cifs": - SOURCE = fields[0].split("/")[2] + if 'filesystems' not in j: return None + source = j['filesystems']['source'] + fstype = j['filesystems']['fstype'] - if SOURCE: - print(SOURCE) + if 'nfs' == fstype: return source.split(":")[0] + elif 'cifs' == fstype: return source.split("/")[2] - return 0 + return None + +#/** +# ogGetServerIp +#@brief Muestra la dirección IP del Servidor de OpenGnSys. +#@return str_ip - Dirección IP +#@note Comprobacion segun protocolo de conexion al Repo +#*/ ## def ogGetServerIp(): # Variables locales. SOURCE = "" @@ -233,6 +287,15 @@ def ogGetServerIp(): return 0 + +#/** +# ogMakeGroupDir [ str_repo ] +#@brief Crea el directorio para el grupo del cliente. +#@param str_repo repositorio de imágenes (opcional) +#@return (nada) +#@note repo = { REPO, CACHE } REPO por defecto +#@exception OG_ERR_FORMAT formato incorrecto. +#*/ def ogMakeGroupDir(): REPO = "" DIR = ""