diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2022-07-05 17:11:26 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2022-08-24 09:48:35 +0200 |
commit | 3550da73e6da062d69dd0d7f1f6889b684abb15d (patch) | |
tree | 56d75bcc720468d6bebe889d21e06148e7322033 /src/utils | |
parent | 74a61d6a7d71fa0bfb2a762bad7f754f33f63895 (diff) |
image_create: partial integration into pythonv1.2.2
Integrates some parts of this operation into native code, eg: the md5
checksum computation.
Wraps non native processes and commands using the subprocess module.
For example, legacy.py stores bash commands pending integration.
Supports python >=3.6, expected until more modern ogLives are put into
production environments.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/fs.py | 28 | ||||
-rw-r--r-- | src/utils/legacy.py | 55 |
2 files changed, 82 insertions, 1 deletions
diff --git a/src/utils/fs.py b/src/utils/fs.py index 191fb86..cd58383 100644 --- a/src/utils/fs.py +++ b/src/utils/fs.py @@ -6,10 +6,11 @@ # Free Software Foundation; either version 3 of the License, or # (at your option) any later version. +import logging import os import subprocess -from subprocess import DEVNULL +from subprocess import DEVNULL, PIPE import psutil @@ -74,3 +75,28 @@ def get_usedperc(mountpoint): except FileNotFoundError: return '0' return str(perc) + + +def ogReduceFs(disk, part): + """ + Bash function 'ogReduceFs' wrapper + """ + proc = subprocess.run(f'ogReduceFs {disk} {part}', + shell=True, stdout=PIPE) + + if proc.returncode == 0: + subprocess.run(f'ogUnmount {disk} {part}', + shell=True, stdout=PIPE) + return proc.stdout.decode().replace('\n', '') + + logging.warn(f'ogReduceFS exited with code {proc.returncode}') + return None + + +def ogExtendFs(disk, part): + """ + Bash function 'ogExtendFs' wrapper + """ + proc = subprocess.run(f'ogExtendFs {disk} {part}', + shell=True) + return proc.returncode diff --git a/src/utils/legacy.py b/src/utils/legacy.py new file mode 100644 index 0000000..7ea2bd6 --- /dev/null +++ b/src/utils/legacy.py @@ -0,0 +1,55 @@ +import ipaddress +import os +import subprocess +import shlex + +from subprocess import PIPE + +def ogGetImageInfo(path): + """ + Bash function 'ogGetImageInfo' wrapper (client/engine/Image.lib) + """ + proc = subprocess.run(f'ogGetImageInfo {path}', + stdout=PIPE, shell=True, + encoding='utf-8') + + if proc.stdout.count(':') != 3: + return '' + + image_info = {} + (image_info['clonator'], + image_info['compressor'], + image_info['filesystem'], + image_info['datasize']) = proc.stdout.rstrip().split(':', 4) + image_info['clientname'] = os.getenv('HOSTNAME') + return image_info + + +def cambiar_acceso(mode='rw', user='opengnsys', pwd='og'): + """ + 'CambiarAcceso' wrapper (admin/Interface/CambiarAcceso) + """ + if mode not in ['rw', 'ro']: + raise ValueError('Invalid remount mode option') + + cmd = shlex.split(f'mount -o remount,{mode},username={user},password={pwd} /opt/opengnsys/images') + ret = True + try: + subprocess.run(cmd, check=True) + except CalledProcessError: + ret = False + finally: + return ret + + +def ogChangeRepo(ip): + """ + Bash function 'ogGetImageInfo' wrapper (client/engine/Net.lib) + """ + try: + ipaddr = ipaddress.ip_address(ip) + except ValueError as e: + raise + + return subprocess.run(f'ogChangeRepo {ipaddr}', + shell=True) |