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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
#
# Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
import os
import subprocess
import fdisk
from src.ogClient import ogClient
from src.ogRest import ThreadState
from src.live.partcodes import GUID_MAP
from src.utils.net import ethtool
from src.utils.menu import generate_menu
from src.utils.fs import mount_mkdir, umount, get_usedperc
from src.utils.probe import os_probe, cache_probe
from src.utils.disk import get_disks
OG_SHELL = '/bin/bash'
class OgLiveOperations:
def __init__(self, config):
self._url = config['opengnsys']['url']
self._url_log = config['opengnsys']['url_log']
def _restartBrowser(self, url):
try:
proc = subprocess.call(["pkill", "-9", "browser"])
proc = subprocess.Popen(["browser", "-qws", url])
except:
raise ValueError('Error: cannot restart browser')
def _refresh_payload_disk(self, cxt, part_setup, num_disk):
part_setup['disk'] = str(num_disk)
part_setup['disk_type'] = 'DISK'
part_setup['code'] = '2' if cxt.label.name == 'gpt' else '1'
part_setup['partition'] = '0'
part_setup['filesystem'] = ''
part_setup['os'] = ''
part_setup['size'] = str(cxt.nsectors * cxt.sector_size // 1024)
part_setup['used_size'] = '0'
def _refresh_payload_partition(self, cxt, pa, part_setup, disk):
parttype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_TYPEID)
fstype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_FSTYPE)
padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE)
size = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_SIZE)
partnum = pa.partno + 1
source = padev
target = padev.replace('dev', 'mnt')
if cxt.label.name == 'gpt':
code = GUID_MAP.get(parttype, 0x0)
else:
code = int(parttype, base=16)
if mount_mkdir(source, target):
probe_result = os_probe(target)
part_setup['os'] = probe_result
part_setup['used_size'] = get_usedperc(target)
umount(target)
else:
part_setup['os'] = ''
part_setup['used_size'] = '0'
part_setup['disk_type'] = ''
part_setup['partition'] = str(partnum)
part_setup['filesystem'] = fstype.upper() if fstype else 'EMPTY'
# part_setup['code'] = hex(code).removeprefix('0x')
part_setup['code'] = hex(code)[2:]
part_setup['size'] = str(int(size) // 1024)
def _refresh_part_setup_cache(self, cxt, pa, part_setup, cache):
padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE)
if padev == cache:
part_setup['filesystem'] = 'CACHE'
part_setup['code'] = 'ca'
def parseGetConf(self, out):
parsed = {'serial_number': '',
'disk_setup': list(),
'partition_setup': list()}
configs = out.split('\n')
for line in configs[:-1]:
if 'ser=' in line:
parsed['serial_number'] = line.partition('ser=')[2]
else:
part_setup = {}
params = dict(param.split('=') for param in line.split('\t'))
# Parse partition configuration.
part_setup['disk'] = params['disk']
part_setup['disk_type'] = params.get('dtype', '')
part_setup['partition'] = params['par']
part_setup['code'] = params['cpt']
part_setup['filesystem'] = params['fsi']
part_setup['os'] = params['soi']
part_setup['size'] = params['tam']
part_setup['used_size'] = params['uso']
if part_setup['partition'] == '0':
parsed['disk_setup'].append(part_setup)
else:
parsed['partition_setup'].append(part_setup)
return parsed
def poweroff(self):
if os.path.exists('/scripts/oginit'):
cmd = f'source {ogClient.OG_PATH}etc/preinit/loadenviron.sh; ' \
f'{ogClient.OG_PATH}scripts/poweroff'
subprocess.call([cmd], shell=True, executable=OG_SHELL)
else:
subprocess.call(['/sbin/poweroff'])
def reboot(self):
if os.path.exists('/scripts/oginit'):
cmd = f'source {ogClient.OG_PATH}etc/preinit/loadenviron.sh; ' \
f'{ogClient.OG_PATH}scripts/reboot'
subprocess.call([cmd], shell=True, executable=OG_SHELL)
else:
subprocess.call(['/sbin/reboot'])
def shellrun(self, request, ogRest):
cmd = request.getrun()
cmds = cmd.split(";|\n\r")
self._restartBrowser(self._url_log)
try:
ogRest.proc = subprocess.Popen(cmds,
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
(output, error) = ogRest.proc.communicate()
except:
raise ValueError('Error: Incorrect command value')
cmd_get_conf = f'{ogClient.OG_PATH}interfaceAdm/getConfiguration'
result = subprocess.check_output([cmd_get_conf], shell=True)
self._restartBrowser(self._url)
return output.decode('utf-8')
def session(self, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
cmd = f'{ogClient.OG_PATH}interfaceAdm/IniciarSesion {disk} {partition}'
try:
ogRest.proc = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
(output, error) = ogRest.proc.communicate()
except:
raise ValueError('Error: Incorrect command value')
return output.decode('utf-8')
def software(self, request, path, ogRest):
disk = request.getDisk()
partition = request.getPartition()
self._restartBrowser(self._url_log)
try:
cmd = f'{ogClient.OG_PATH}interfaceAdm/InventarioSoftware {disk} ' \
f'{partition} {path}'
ogRest.proc = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
(output, error) = ogRest.proc.communicate()
except:
raise ValueError('Error: Incorrect command value')
self._restartBrowser(self._url)
software = ''
with open(path, 'r') as f:
software = f.read()
return software
def hardware(self, path, ogRest):
self._restartBrowser(self._url_log)
try:
cmd = f'{ogClient.OG_PATH}interfaceAdm/InventarioHardware {path}'
ogRest.proc = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
(output, error) = ogRest.proc.communicate()
except:
raise ValueError('Error: Incorrect command value')
self._restartBrowser(self._url)
return output.decode('utf-8')
def setup(self, request, ogRest):
table_type = request.getType()
disk = request.getDisk()
cache = request.getCache()
cache_size = request.getCacheSize()
partlist = request.getPartitionSetup()
cfg = f'dis={disk}*che={cache}*tch={cache_size}!'
for part in partlist:
cfg += f'par={part["partition"]}*cpt={part["code"]}*' \
f'sfi={part["filesystem"]}*tam={part["size"]}*' \
f'ope={part["format"]}%'
if ogRest.terminated:
break
cmd = f'{ogClient.OG_PATH}interfaceAdm/Configurar {table_type} {cfg}'
try:
ogRest.proc = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
(output, error) = ogRest.proc.communicate()
except:
raise ValueError('Error: Incorrect command value')
cmd_get_conf = f'{ogClient.OG_PATH}interfaceAdm/getConfiguration'
result = subprocess.check_output([cmd_get_conf], shell=True)
self._restartBrowser(self._url)
return self.parseGetConf(result.decode('utf-8'))
def image_restore(self, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
name = request.getName()
repo = request.getRepo()
ctype = request.getType()
profile = request.getProfile()
cid = request.getId()
cmd = f'{ogClient.OG_PATH}interfaceAdm/RestaurarImagen {disk} {partition} ' \
f'{name} {repo} {ctype}'
self._restartBrowser(self._url_log)
try:
ogRest.proc = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
(output, error) = ogRest.proc.communicate()
if (ogRest.proc.returncode):
raise Exception
except:
raise ValueError('Error: Incorrect command value')
cmd_get_conf = f'{ogClient.OG_PATH}interfaceAdm/getConfiguration'
result = subprocess.check_output([cmd_get_conf], shell=True)
self._restartBrowser(self._url)
return output.decode('utf-8')
def image_create(self, path, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
name = request.getName()
repo = request.getRepo()
cmd_software = f'{ogClient.OG_PATH}interfaceAdm/InventarioSoftware {disk} ' \
f'{partition} {path}'
cmd_create_image = f'{ogClient.OG_PATH}interfaceAdm/CrearImagen {disk} ' \
f'{partition} {name} {repo}'
self._restartBrowser(self._url_log)
try:
ogRest.proc = subprocess.Popen([cmd_software],
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
(output, error) = ogRest.proc.communicate()
except:
raise ValueError('Error: Incorrect command value')
if ogRest.terminated:
return
try:
ogRest.proc = subprocess.Popen([cmd_create_image],
stdout=subprocess.PIPE,
shell=True,
executable=OG_SHELL)
ogRest.proc.communicate()
except:
raise ValueError('Error: Incorrect command value')
if ogRest.proc.returncode != 0:
raise ValueError('Error: Image creation failed')
with open('/tmp/image.info') as file_info:
line = file_info.readline().rstrip()
image_info = {}
(image_info['clonator'],
image_info['compressor'],
image_info['filesystem'],
image_info['datasize'],
image_info['clientname']) = line.split(':', 5)
os.remove('/tmp/image.info')
self._restartBrowser(self._url)
return image_info
def refresh(self, ogRest):
self._restartBrowser(self._url_log)
cache = cache_probe()
disks = get_disks()
parsed = { 'serial_number': '',
'disk_setup': [],
'partition_setup': []
}
for num_disk, disk in enumerate(get_disks(), start=1):
print(disk)
part_setup = {}
try:
cxt = fdisk.Context(device=f'/dev/{disk}')
except:
continue
self._refresh_payload_disk(cxt, part_setup, num_disk)
parsed['disk_setup'].append(part_setup)
for pa in cxt.partitions:
part_setup = part_setup.copy()
self._refresh_payload_partition(cxt, pa, part_setup, disk)
self._refresh_part_setup_cache(cxt, pa, part_setup, cache)
parsed['partition_setup'].append(part_setup)
generate_menu(parsed['partition_setup'])
self._restartBrowser(self._url)
return parsed
def probe(self, ogRest):
interface = os.getenv('DEVICE')
speed = ethtool(interface)
return {'status': 'OPG' if ogRest.state != ThreadState.BUSY else 'BSY',
'speed': speed}
|