From a1edbe904b3019ec77b54fdd48c01183f4c73483 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Wed, 1 Mar 2023 10:57:53 +0100 Subject: legacy: rewrite ogChangeRepo Drop ogChangeRepo Bash script in favor of a native Python approach. Use only necessary subprocess calls instead of bringing all the logic of this function into a Bash script black box. ogChangeRepo unmounts the current OpenGnsys image samba folder (/opt/opengnsys/images) and mounts (connects to) a new directory using the new provided ip address. Keeping access mode from previous mount. If anything goes wrong when mounting the new directory, it will fallback to mounting the previous directory. If no previous OpenGnsys image samba directory is detected, this functions tries to mount the new directory anyway. In this case, it will raise CalledProcessError if something goes wrong. --- src/live/ogOperations.py | 2 +- src/utils/legacy.py | 58 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py index 6f52a5a..cb8957d 100644 --- a/src/live/ogOperations.py +++ b/src/live/ogOperations.py @@ -401,7 +401,7 @@ class OgLiveOperations: self._ogbrowser_clear_logs() self._restartBrowser(self._url_log) - if ogChangeRepo(repo).returncode != 0: + if ogChangeRepo(repo, smb_user=self._smb_user, smb_pass=self._smb_pass).returncode != 0: self._restartBrowser(self._url) logging.error('ogChangeRepo could not change repository to %s', repo) raise ValueError(f'Error: Cannot change repository to {repo}') diff --git a/src/utils/legacy.py b/src/utils/legacy.py index 96b32eb..509b9b0 100644 --- a/src/utils/legacy.py +++ b/src/utils/legacy.py @@ -5,7 +5,10 @@ import subprocess import shlex import shutil -from subprocess import PIPE, DEVNULL +from subprocess import PIPE, DEVNULL, CalledProcessError + +from src.utils.fs import umount + def ogGetImageInfo(path): """ @@ -44,17 +47,60 @@ def cambiar_acceso(mode='rw', user='opengnsys', pwd='og'): p = subprocess.run(cmd, stdout=DEVNULL, stderr=DEVNULL) return p.returncode == 0 -def ogChangeRepo(ip): +def ogChangeRepo(ip, smb_user='opengnsys', smb_pass='og'): """ - Bash function 'ogGetImageInfo' wrapper (client/engine/Net.lib) + Umount current Samba directory of OpenGnsys images and mount new Samba + directory (preserving previous access mode). + + If the mount fails, fallback to the image directory that was mounted before + calling this function. + + If there is no previous image directory mount, then simply try mounting the + new Samba directory with readonly mode option. + + Any CalledProcessError raised by the fallback mount subprocess should be + handled by the caller of this function. """ + def fsopts_mode(fsopts): + for opt in fsopts.split(','): + if opt in ['rw', 'ro']: + return opt + + def process_mntent(line): + name, mntdir, fstype, opts, freq, passno = line.split(' ') + mode = fsopts_mode(opts) + return name, mntdir, fsopts_mode(opts) + try: ipaddr = ipaddress.ip_address(ip) except ValueError as e: - raise + raise ValueError('Invalid ip address') + + mounted = False + with open('/etc/mtab') as f: + for line in f: + if 'ogimages' in line: + orig_name, mntdir, mode = process_mntent(line) + mounted = True + break + + new_name = f'//{ip}/ogimages' + if not mounted: + orig_name = new_name + mntdir = '/opt/opengnsys/images' + mode = 'ro' + else: + umount(mntdir) - return subprocess.run(f'ogChangeRepo {ipaddr}', - shell=True) + cmd = f'mount.cifs -o {mode},username={smb_user},password={smb_pass} {new_name} /opt/opengnsys/images' + try: + result = subprocess.run(shlex.split(cmd), check=True) + except CalledProcessError as e: + logging.error(f'Error mounting new image directory: {e}') + cmd = f'mount.cifs -o {mode},username={smb_user},password={smb_pass} {orig_name} /opt/opengnsys/images' + result = subprocess.run(shlex.split(cmd), check=True) + finally: + return result def restoreImageCustom(repo_ip, image_name, disk, partition, method): -- cgit v1.2.3-18-g5258