summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/legacy.py58
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):