refs #1144 rebuild ogGetHostname

code-review
Antonio Guerrero 2024-11-14 07:50:51 -06:00
parent 8db98b1289
commit bc3d68e8fb
1 changed files with 29 additions and 33 deletions

View File

@ -7,8 +7,6 @@ import ogGlobals
import SystemLib import SystemLib
import FileLib import FileLib
#/** #/**
# ogChangeRepo IPREPO [ OgUnit ] # ogChangeRepo IPREPO [ OgUnit ]
#@brief Cambia el repositorio para el recurso remoto images. #@brief Cambia el repositorio para el recurso remoto images.
@ -110,44 +108,46 @@ def ogGetGroupName():
print(f"Error in ogGetGroupName: {e}") print(f"Error in ogGetGroupName: {e}")
return None return None
#/** #/**
# ogGetHostname # ogGetHostname
#@brief Muestra el nombre del cliente. #@brief Muestra el nombre del cliente.
#@return str_host - nombre de máquina #@return str_host - nombre de máquina
#*/ ## #*/ ##
def ogGetHostname(): def ogGetHostname():
HOST = "" try:
# 1. Get hostname from the HOSTNAME environment variable
host = os.getenv("HOSTNAME", "").strip()
if len(sys.argv) >= 2 and sys.argv[1] == "help": # 2. If not set, read from the DHCP leases file
SystemLib.ogHelp("ogGetHostname", "ogGetHostname", "ogGetHostname => pc1") if not host:
return dhcp_file = "/var/lib/dhcp3/dhclient.leases"
if os.path.exists(dhcp_file):
with open(dhcp_file, "r") as f:
for line in f:
if 'option host-name' in line:
host = line.split('"')[1].strip(";")
break
# Tomar nombre de la variable HOSTNAME # 3. If still not set, read from kernel parameters in /proc/cmdline
HOST = os.getenv("HOSTNAME") if not host:
cmdline_file = "/proc/cmdline"
if os.path.exists(cmdline_file):
with open(cmdline_file, "r") as f:
for entry in f.read().split():
if entry.startswith("hostname="):
host = entry.split("=")[1].strip()
break
# Si no, tomar del DHCP, opción host-name # 4. Update HOSTNAME environment variable if it differs
if not HOST: current_hostname = os.getenv("HOSTNAME", "")
with open("/var/lib/dhcp3/dhclient.leases", "r") as f: if host and current_hostname != host:
for line in f: os.environ["HOSTNAME"] = host
if "option host-name" in line:
HOST = line.split('"')[1]
break
# Si no, leer el parámetro del kernel hostname return host if host else None
if not HOST:
with open("/proc/cmdline", "r") as f:
cmdline = f.read()
HOST = re.search(r"hostname=([^ ]+)", cmdline)
if HOST:
HOST = HOST.group(1)
if HOSTNAME != HOST:
os.environ["HOSTNAME"] = HOST
if HOST:
print(HOST)
except Exception as e:
print(f"Error in ogGetHostname: {e}")
return None
#/** #/**
# ogGetIpAddress # ogGetIpAddress
@ -179,7 +179,6 @@ def ogGetIpAddress():
return 0 return 0
#/** #/**
# ogGetMacAddress # ogGetMacAddress
#@brief Muestra la dirección Ethernet del cliente. #@brief Muestra la dirección Ethernet del cliente.
@ -241,7 +240,6 @@ def ogGetNetInterface():
return 0 return 0
#/** #/**
# ogGetRepoIp # ogGetRepoIp
#@brief Muestra la dirección IP del repositorio de datos. #@brief Muestra la dirección IP del repositorio de datos.
@ -263,7 +261,6 @@ def ogGetRepoIp():
return None return None
#/** #/**
# ogGetServerIp # ogGetServerIp
#@brief Muestra la dirección IP del Servidor de OpenGnSys. #@brief Muestra la dirección IP del Servidor de OpenGnSys.
@ -305,7 +302,6 @@ def ogGetServerIp():
SystemLib.ogEcho("session", "info", "No valid file system found") SystemLib.ogEcho("session", "info", "No valid file system found")
return None return None
#/** #/**
# ogMakeGroupDir [ str_repo ] # ogMakeGroupDir [ str_repo ]
#@brief Crea el directorio para el grupo del cliente. #@brief Crea el directorio para el grupo del cliente.