1 | import ipaddress |
---|
2 | import logging |
---|
3 | import os |
---|
4 | import subprocess |
---|
5 | import shlex |
---|
6 | import shutil |
---|
7 | |
---|
8 | from subprocess import PIPE |
---|
9 | |
---|
10 | def ogGetImageInfo(path): |
---|
11 | """ |
---|
12 | Bash function 'ogGetImageInfo' wrapper (client/engine/Image.lib) |
---|
13 | """ |
---|
14 | proc = subprocess.run(f'ogGetImageInfo {path}', |
---|
15 | stdout=PIPE, shell=True, |
---|
16 | encoding='utf-8') |
---|
17 | |
---|
18 | if proc.stdout.count(':') != 3: |
---|
19 | return '' |
---|
20 | |
---|
21 | image_info = {} |
---|
22 | (image_info['clonator'], |
---|
23 | image_info['compressor'], |
---|
24 | image_info['filesystem'], |
---|
25 | image_info['datasize']) = proc.stdout.rstrip().split(':', 4) |
---|
26 | image_info['clientname'] = os.getenv('HOSTNAME') |
---|
27 | return image_info |
---|
28 | |
---|
29 | |
---|
30 | def cambiar_acceso(mode='rw', user='opengnsys', pwd='og'): |
---|
31 | """ |
---|
32 | 'CambiarAcceso' wrapper (admin/Interface/CambiarAcceso) |
---|
33 | """ |
---|
34 | if mode not in ['rw', 'ro']: |
---|
35 | raise ValueError('Invalid remount mode option') |
---|
36 | |
---|
37 | cmd = shlex.split(f'mount -o remount,{mode},username={user},password={pwd} /opt/opengnsys/images') |
---|
38 | ret = True |
---|
39 | try: |
---|
40 | subprocess.run(cmd, check=True) |
---|
41 | except CalledProcessError: |
---|
42 | ret = False |
---|
43 | finally: |
---|
44 | return ret |
---|
45 | |
---|
46 | |
---|
47 | def ogChangeRepo(ip): |
---|
48 | """ |
---|
49 | Bash function 'ogGetImageInfo' wrapper (client/engine/Net.lib) |
---|
50 | """ |
---|
51 | try: |
---|
52 | ipaddr = ipaddress.ip_address(ip) |
---|
53 | except ValueError as e: |
---|
54 | raise |
---|
55 | |
---|
56 | return subprocess.run(f'ogChangeRepo {ipaddr}', |
---|
57 | shell=True) |
---|
58 | |
---|
59 | |
---|
60 | def restoreImageCustom(repo_ip, image_name, disk, partition, method): |
---|
61 | """ |
---|
62 | """ |
---|
63 | if not shutil.which('restoreImageCustom'): |
---|
64 | logging.error('Invalid restoreImageCustom invocation') |
---|
65 | raise ValueError('Error: restoreImageCustom not found') |
---|
66 | |
---|
67 | if ogChangeRepo(repo).returncode != 0: |
---|
68 | logging.error('ogChangeRepo could not change repository to %s', repo) |
---|
69 | raise ValueError(f'Error: Cannot change repository to {repo}') |
---|
70 | |
---|
71 | cmd = f'restoreImageCustom {repo_ip} {image_name} {disk} {partition} {method}' |
---|
72 | with open('/tmp/command.log', 'wb', 0) as logfile: |
---|
73 | try: |
---|
74 | proc = subprocess.run(cmd, |
---|
75 | stdout=logfile, |
---|
76 | encoding='utf-8', |
---|
77 | shell=True) |
---|
78 | except: |
---|
79 | logging.error('Exception when running restoreImageCustom subprocess') |
---|
80 | raise ValueError('Error: Incorrect command value') |
---|
81 | return proc.returncode |
---|
82 | |
---|
83 | |
---|
84 | def configureOs(disk, partition): |
---|
85 | """ |
---|
86 | """ |
---|
87 | if shutil.which('configureOsCustom'): |
---|
88 | cmd_configure = f"configureOsCustom {disk} {partition}" |
---|
89 | else: |
---|
90 | cmd_configure = f"configureOs {disk} {partition}" |
---|
91 | |
---|
92 | try: |
---|
93 | proc = subprocess.run(cmd_configure, |
---|
94 | stdout=PIPE, |
---|
95 | encoding='utf-8', |
---|
96 | shell=True) |
---|
97 | out = proc.stdout |
---|
98 | except: |
---|
99 | logging.error('Exception when running configureOs subprocess') |
---|
100 | raise ValueError('Error: Incorrect command value') |
---|
101 | |
---|
102 | return out |
---|
103 | |
---|
104 | |
---|
105 | def ogCopyEfiBootLoader(disk, partition): |
---|
106 | cmd = f'ogCopyEfiBootLoader {disk} {partition}' |
---|
107 | try: |
---|
108 | proc = subprocess.run(cmd, |
---|
109 | shell=True) |
---|
110 | except: |
---|
111 | logging.error('Exception when running ogCopyEfiBootLoader subprocess') |
---|
112 | raise ValueError('Subprocess error: ogCopyEfiBootloader') |
---|