diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2023-03-01 10:57:53 +0100 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-03-02 09:33:33 +0100 |
commit | a1edbe904b3019ec77b54fdd48c01183f4c73483 (patch) | |
tree | 7d2280c13a1d9df4e0e97c6c61acbd504f523d56 /src/utils/legacy.py | |
parent | 1858950af194ef73d3ee589ae7104f7d3def9f38 (diff) |
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.
Diffstat (limited to 'src/utils/legacy.py')
-rw-r--r-- | src/utils/legacy.py | 58 |
1 files changed, 52 insertions, 6 deletions
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): |