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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
import ipaddress
import logging
import os
import subprocess
import shlex
import shutil
from subprocess import PIPE, DEVNULL
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' (admin/Interface/CambiarAcceso) rewrite into native Python.
Remount the (remote) samba directory that contains the OpenGnsys images.
Specify access mode ('rw', or 'ro') with mode parameter (default 'rw').
Specify samba credentials with user and pwd parameter.
Return True if exit-code was 0. Return False otherwise.
"""
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')
p = subprocess.run(cmd, stdout=DEVNULL, stderr=DEVNULL)
return p.returncode == 0
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)
def restoreImageCustom(repo_ip, image_name, disk, partition, method):
"""
"""
if not shutil.which('restoreImageCustom'):
logging.error('Invalid restoreImageCustom invocation')
raise ValueError('Error: restoreImageCustom not found')
if ogChangeRepo(repo).returncode != 0:
logging.error('ogChangeRepo could not change repository to %s', repo)
raise ValueError(f'Error: Cannot change repository to {repo}')
cmd = f'restoreImageCustom {repo_ip} {image_name} {disk} {partition} {method}'
with open('/tmp/command.log', 'wb', 0) as logfile:
try:
proc = subprocess.run(cmd,
stdout=logfile,
encoding='utf-8',
shell=True)
except:
logging.error('Exception when running restoreImageCustom subprocess')
raise ValueError('Error: Incorrect command value')
return proc.returncode
def configureOs(disk, partition):
"""
"""
if shutil.which('configureOsCustom'):
cmd_configure = f"configureOsCustom {disk} {partition}"
else:
cmd_configure = f"configureOs {disk} {partition}"
try:
proc = subprocess.run(cmd_configure,
stdout=PIPE,
encoding='utf-8',
shell=True)
out = proc.stdout
except:
logging.error('Exception when running configureOs subprocess')
raise ValueError('Error: Incorrect command value')
return out
def ogCopyEfiBootLoader(disk, partition):
cmd = f'ogCopyEfiBootLoader {disk} {partition}'
try:
proc = subprocess.run(cmd,
shell=True)
except:
logging.error('Exception when running ogCopyEfiBootLoader subprocess')
raise ValueError('Subprocess error: ogCopyEfiBootloader')
|