summaryrefslogtreecommitdiffstats
path: root/src/utils/legacy.py
blob: 7ea2bd60243f220838547d1c5f7fbc18918ce63e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)