refs #1150 add a temporal ogConnect() and have ogChangeRepo() use it

pull/1/head
Natalia Serrano 2024-11-14 17:55:09 +01:00
parent 646d1da734
commit b755de21be
1 changed files with 36 additions and 28 deletions

View File

@ -2,11 +2,28 @@ import subprocess
import sys import sys
import os import os
import json import json
import re
import ogGlobals import ogGlobals
import SystemLib import SystemLib
import FileLib import FileLib
def _ogConnect_options():
## the original bash code does: eval $(grep "OPTIONS=" /scripts/ogfunctions)
## this is highly insecure but we need to keep it for now
opt = subprocess.run (['grep', '-o', 'OPTIONS=.*', '/scripts/ogfunctions'], capture_output=True, text=True).stdout.strip()
exec (opt, globals())
return OPTIONS
## defined in /scripts/ogfunctions. We can't tackle that yet.
## this is a quick implementation just to unblock development
def _ogConnect (server, protocol, src, dst, options, readonly):
if 'smb' != protocol: return None ## only supported value right now
write_option = ',ro' if readonly else ',rw'
options += write_option
return not subprocess.run (['mount.cifs', f'//{server}/{src}', dst] + options.split()).returncode
#/** #/**
# ogChangeRepo IPREPO [ OgUnit ] # ogChangeRepo IPREPO [ OgUnit ]
#@brief Cambia el repositorio para el recurso remoto images. #@brief Cambia el repositorio para el recurso remoto images.
@ -15,57 +32,48 @@ import FileLib
#@return Cambio recurso remoto en OGIMG. #@return Cambio recurso remoto en OGIMG.
#*/ #*/
def ogChangeRepo(ip_repo, og_unit=None): def ogChangeRepo(ip_repo, og_unit=None):
ogprotocol = "cifs" ogprotocol = os.environ['ogprotocol'] or 'smb'
if og_unit:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_GENERIC, 'the og_unit parameter became unsupported')
return None
try: try:
rw_option = ",rw" if "rw" in subprocess.getoutput("mount | grep 'ogimages'") else ",ro" mount = subprocess.run (['mount'], capture_output=True, text=True).stdout
ro = not not list (filter (lambda line: re.search (r'ogimages.*\bro,', line), mount.splitlines()))
current_repo = ogGetRepoIp() current_repo = ogGetRepoIp()
current_og_unit = subprocess.getoutput(
"df | awk -F ' ' '/ogimages/ {sub(\"//.*/ogimages\",\"\",$1); sub(\"/\",\"\",$1); print $1}'"
).strip()
new_repo = current_repo if ip_repo.upper() == "REPO" else ip_repo new_repo = current_repo if ip_repo.upper() == "REPO" else ip_repo
if new_repo == current_repo: return True
if new_repo == current_repo and og_unit == current_og_unit:
return 0
subprocess.run(["umount", ogGlobals.OGIMG], check=True) subprocess.run(["umount", ogGlobals.OGIMG], check=True)
src_img = "ogimages" if not og_unit else f"ogimages/{og_unit}" SystemLib.ogEcho (['session', 'log'], 'info', f'{ogGlobals.lang.MSG_HELP_ogChangeRepo} {new_repo}')
options = _ogConnect_options()
result = subprocess.run( if not _ogConnect (new_repo, ogprotocol, 'ogimages', ogGlobals.OGIMG, options, ro):
["ogConnect", new_repo, ogprotocol, src_img, ogGlobals.OGIMG, rw_option], _ogConnect (current_repo, ogprotocol, 'ogimages', ogGlobals.OGIMG, options, ro)
text=True,
)
if result.returncode != 0:
subprocess.run(
["ogConnect", current_repo, ogprotocol, src_img, ogGlobals.OGIMG, rw_option],
text=True,
)
SystemLib.ogRaiseError( SystemLib.ogRaiseError(
"session", "session",
ogGlobals.OG_ERR_REPO, ogGlobals.OG_ERR_REPO,
f"Error connecting to the new repository: {new_repo}", f"Error connecting to the new repository: {new_repo}",
) )
return 1 return False
SystemLib.ogEcho( SystemLib.ogEcho(
"session", ["session", "log"],
"log", 'info',
f"Repository successfully changed to {new_repo} {og_unit or ''}".strip(), f"Repository successfully changed to {new_repo}".strip(),
) )
return 0 return True
except Exception as e: except Exception as e:
SystemLib.ogRaiseError( SystemLib.ogRaiseError(
"session", "session",
ogGlobals.OG_ERR_FORMAT, ogGlobals.OG_ERR_GENERIC,
f"Error executing ogChangeRepo: {e}", f"Error executing ogChangeRepo: {e}",
) )
return 1 return None
#/** #/**
# ogGetGroupDir [ str_repo ] # ogGetGroupDir [ str_repo ]