refs #1144 rebuild ogChangeRepo

pull/1/head
Antonio Guerrero 2024-11-13 22:03:11 -06:00
parent c03455a32f
commit ea5114a897
1 changed files with 43 additions and 48 deletions

View File

@ -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 ]