From ea5114a897cc21ede8747885c8db2b580ad41696 Mon Sep 17 00:00:00 2001 From: Antonio Emmanuel Guerrero Silva Date: Wed, 13 Nov 2024 22:03:11 -0600 Subject: [PATCH] refs #1144 rebuild ogChangeRepo --- client/lib/engine/bin/NetLib.py | 91 ++++++++++++++++----------------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/client/lib/engine/bin/NetLib.py b/client/lib/engine/bin/NetLib.py index 32e9e5e..1f85ee0 100755 --- a/client/lib/engine/bin/NetLib.py +++ b/client/lib/engine/bin/NetLib.py @@ -16,63 +16,58 @@ import SystemLib #@param 2 Abreviatura Unidad Organizativa #@return Cambio recurso remoto en OGIMG. #*/ -def ogChangeRepo(): - SRCIMG = "" - NEWREPO = "" - REPO = "" - OGUNIT = "" +def ogChangeRepo(ip_repo, og_unit=None): + ogprotocol = "cifs" - if len(sys.argv) < 2: - print("Usage: ogChangeRepo IPREPO [ OgUnit ]") - print("Example: ogChangeRepo 10.1.120.3") - print("Example: ogChangeRepo 10.1.120.3 cdc") - return + try: + rw_option = ",rw" if "rw" in subprocess.getoutput("mount | grep 'ogimages'") else ",ro" - if sys.argv[1] == "help": - print("Usage: ogChangeRepo IPREPO [ OgUnit ]") - print("Example: ogChangeRepo 10.1.120.3") - print("Example: ogChangeRepo 10.1.120.3 cdc") - return + current_repo = ogGetRepoIp() + current_og_unit = subprocess.getoutput( + "df | awk -F ' ' '/ogimages/ {sub(\"//.*/ogimages\",\"\",$1); sub(\"/\",\"\",$1); print $1}'" + ).strip() - if len(sys.argv) >= 2: - NEWREPO = sys.argv[1] + new_repo = current_repo if ip_repo.upper() == "REPO" else ip_repo - # Opciones de montaje: lectura o escritura - subprocess.run(["mount", "|", "grep", "ogimages.*rw,"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - RW = ",rw" if subprocess.returncode == 0 else ",ro" + if new_repo == current_repo and og_unit == current_og_unit: + return 0 - # Si REPO tomamos el repositorio y la unidad organizativa actual - REPO = ogRepoIp() - OGUNIT = subprocess.run(["df", "|", "awk", "-F", " ", "'/ogimages/ {sub(\"//.*/ogimages\",\"\",$1); sub(\"/\",\"\",$1); print $1}'"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode().strip() + subprocess.run(["umount", ogGlobals.OGIMG], check=True) - # Parametros de entrada. Si $1 = "REPO" dejo el repositorio actual - if sys.argv[1].upper() == "REPO": - NEWREPO = REPO + src_img = "ogimages" if not og_unit else f"ogimages/{og_unit}" + + result = subprocess.run( + ["ogConnect", new_repo, ogprotocol, src_img, ogGlobals.OGIMG, rw_option], + text=True, + ) + + if result.returncode != 0: + subprocess.run( + ["ogConnect", current_repo, ogprotocol, src_img, ogGlobals.OGIMG, rw_option], + text=True, + ) + SystemLib.ogRaiseError( + "session", + ogGlobals.OG_ERR_REPO, + f"Error connecting to the new repository: {new_repo}", + ) + return 1 + + SystemLib.ogEcho( + "session", + "log", + f"Repository successfully changed to {new_repo} {og_unit or ''}".strip(), + ) - # Si $1 y $2 son el repositorio y la OU actual me salgo - if NEWREPO == REPO and sys.argv[2] == OGUNIT: return 0 - subprocess.run(["source", "/scripts/functions"], shell=True) - subprocess.run(["source", "/scripts/ogfunctions"], shell=True) - subprocess.run(["umount", OGIMG]) - - if sys.argv[2] == "": - SRCIMG = "ogimages" - else: - SRCIMG = "ogimages/" + sys.argv[2] - - subprocess.run(["eval", "$(grep \"OPTIONS=\" /scripts/ogfunctions)"]) - - SystemLib.ogEcho("session", "log", MSG_HELP_ogChangeRepo + " " + NEWREPO + " " + sys.argv[2].rstrip()) - ogConnect(NEWREPO, ogprotocol, SRCIMG, OGIMG, RW) - - # Si da error volvemos a montar el inicial - if subprocess.returncode != 0: - ogConnect(REPO, ogprotocol, SRCIMG, OGIMG, RW) - SystemLib.ogRaiseError("session", OG_ERR_REPO, NEWREPO) - return subprocess.returncode - + except Exception as e: + SystemLib.ogRaiseError( + "session", + ogGlobals.OG_ERR_FORMAT, + f"Error executing ogChangeRepo: {e}", + ) + return 1 #/** # ogGetGroupDir [ str_repo ]