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.
more_events
Jose M. Guisado 2023-03-01 10:57:53 +01:00
parent 1858950af1
commit a1edbe904b
2 changed files with 53 additions and 7 deletions

View File

@ -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}')

View File

@ -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')
return subprocess.run(f'ogChangeRepo {ipaddr}',
shell=True)
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)
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):