Encapsulate operations in classes

more_events
Roberto Hueso Gomez 2020-04-08 13:30:16 +02:00
parent c279325919
commit 99ae598fbd
2 changed files with 491 additions and 489 deletions

View File

@ -12,7 +12,8 @@ from src.ogConfig import *
OG_SHELL = '/bin/bash'
def parseGetConf(out):
class OgLinuxOperations:
def parseGetConf(self, out):
parsed = {'serial_number': '',
'disk_setup': '',
'partition_setup': list()}
@ -37,7 +38,7 @@ def parseGetConf(out):
parsed['partition_setup'].append(part_setup)
return parsed
def poweroff():
def poweroff(self):
if os.path.exists('/scripts/oginit'):
cmd = f'source {ogConfig.OG_PATH}etc/preinit/loadenviron.sh; ' \
f'{ogConfig.OG_PATH}scripts/poweroff'
@ -45,7 +46,7 @@ def poweroff():
else:
subprocess.call(['/sbin/poweroff'])
def reboot():
def reboot(self):
if os.path.exists('/scripts/oginit'):
cmd = f'source {ogConfig.OG_PATH}etc/preinit/loadenviron.sh; ' \
f'{ogConfig.OG_PATH}scripts/reboot'
@ -53,7 +54,7 @@ def reboot():
else:
subprocess.call(['/sbin/reboot'])
def execCMD(request, ogRest):
def execCMD(self, request, ogRest):
cmd = request.getrun()
cmds = cmd.split(";|\n\r")
try:
@ -67,7 +68,7 @@ def execCMD(request, ogRest):
return output.decode('utf-8')
def session(request, ogRest):
def session(self, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
cmd = f'{ogConfig.OG_PATH}interfaceAdm/IniciarSesion {disk} {partition}'
@ -83,7 +84,7 @@ def session(request, ogRest):
return output.decode('utf-8')
def software(request, path, ogRest):
def software(self, request, path, ogRest):
disk = request.getDisk()
partition = request.getPartition()
@ -101,7 +102,7 @@ def software(request, path, ogRest):
return output.decode('utf-8')
def hardware(path, ogRest):
def hardware(self, path, ogRest):
try:
cmd = f'{ogConfig.OG_PATH}interfaceAdm/InventarioHardware {path}'
ogRest.proc = subprocess.Popen([cmd],
@ -114,7 +115,7 @@ def hardware(path, ogRest):
return output.decode('utf-8')
def setup(request, ogRest):
def setup(self, request, ogRest):
disk = request.getDisk()
cache = request.getCache()
cache_size = request.getCacheSize()
@ -141,9 +142,9 @@ def setup(request, ogRest):
cmd_get_conf = f'{ogConfig.OG_PATH}interfaceAdm/getConfiguration'
result = subprocess.check_output([cmd_get_conf], shell=True)
return parseGetConf(result.decode('utf-8'))
return self.parseGetConf(result.decode('utf-8'))
def image_restore(request, ogRest):
def image_restore(self, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
name = request.getName()
@ -165,7 +166,7 @@ def image_restore(request, ogRest):
return output.decode('utf-8')
def image_create(path, request, ogRest):
def image_create(self, path, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
name = request.getName()
@ -198,7 +199,7 @@ def image_create(path, request, ogRest):
return output.decode('utf-8')
def refresh(ogRest):
def refresh(self, ogRest):
try:
cmd = f'{ogConfig.OG_PATH}interfaceAdm/getConfiguration'
ogRest.proc = subprocess.Popen([cmd],
@ -209,4 +210,4 @@ def refresh(ogRest):
except:
raise ValueError('Error: Incorrect command value')
return parseGetConf(output.decode('utf-8'))
return self.parseGetConf(output.decode('utf-8'))

View File

@ -19,9 +19,6 @@ import pathlib
import re
import math
IP = '127.0.0.1'
VIRTUAL_PORT = 4444
class OgQMP:
class State(enum.Enum):
CONNECTING = 0
@ -59,8 +56,13 @@ class OgQMP:
self.state = self.State.FORCE_DISCONNECTED
self.sock.close()
def poweroff():
qmp = OgQMP(IP, VIRTUAL_PORT)
class OgVirtualOperations:
def __init__(self):
self.IP = '127.0.0.1'
self.VIRTUAL_PORT = 4444
def poweroff(self):
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
qmp.connect()
qmp.recv()
qmp.send(str({"execute": "qmp_capabilities"}))
@ -68,8 +70,8 @@ def poweroff():
qmp.send(str({"execute": "system_powerdown"}))
qmp.disconnect()
def reboot():
qmp = OgQMP(IP, VIRTUAL_PORT)
def reboot(self):
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
qmp.connect()
qmp.recv()
qmp.send(str({"execute": "qmp_capabilities"}))
@ -77,7 +79,7 @@ def reboot():
qmp.send(str({"execute": "system_reset"}))
qmp.disconnect()
def session(request, ogRest):
def session(self, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
@ -91,7 +93,7 @@ def session(request, ogRest):
f'-display gtk -cpu host -m {vm_ram_mib}M -boot c')
subprocess.Popen([cmd], shell=True)
def refresh(ogRest):
def refresh(self, ogRest):
path = 'partitions.json'
temp_mount_dir = 'mnt'
try:
@ -148,7 +150,7 @@ def refresh(ogRest):
return data
def setup(request, ogRest):
def setup(self, request, ogRest):
path = 'partitions.json'
refresh(ogRest)
@ -188,14 +190,14 @@ def setup(request, ogRest):
return refresh(ogRest)
def image_create(path, request, ogRest):
def image_create(self, path, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
name = request.getName()
repo = request.getRepo()
# Check if VM is running.
qmp = OgQMP(IP, VIRTUAL_PORT)
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
if qmp.connect() != None:
qmp.disconnect()
return None
@ -212,7 +214,7 @@ def image_create(path, request, ogRest):
return True
def image_restore(request, ogRest):
def image_restore(self, request, ogRest):
disk = request.getDisk()
partition = request.getPartition()
name = request.getName()
@ -223,7 +225,7 @@ def image_restore(request, ogRest):
cid = request.getId()
# Check if VM is running.
qmp = OgQMP(IP, VIRTUAL_PORT)
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
if qmp.connect() != None:
qmp.disconnect()
return None
@ -243,7 +245,7 @@ def image_restore(request, ogRest):
return True
def software(request, path, ogRest):
def software(self, request, path, ogRest):
DPKG_PATH = '/var/lib/dpkg/status'
disk = request.getDisk()
@ -314,7 +316,7 @@ def software(request, path, ogRest):
f.write(f'{program}\n')
f.truncate()
def parse_pci(path='/usr/share/misc/pci.ids'):
def parse_pci(self, path='/usr/share/misc/pci.ids'):
data = {}
with open(path, 'r') as f:
for line in f:
@ -338,8 +340,8 @@ def parse_pci(path='/usr/share/misc/pci.ids'):
data[fields[0]] = {'name': fields[1]}
return data
def hardware(path, ogRest):
qmp = OgQMP(IP, VIRTUAL_PORT)
def hardware(self, path, ogRest):
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
qmp.connect()
qmp.recv()
qmp.send(str({"execute": "qmp_capabilities"}))
@ -349,8 +351,7 @@ def hardware(path, ogRest):
data = data['return'][0]['devices']
pci_list = parse_pci()
device_names = {}
for device in da
ta:
for device in data:
vendor_id = hex(device['id']['vendor'])[2:]
device_id = hex(device['id']['device'])[2:]
subvendor_id = hex(device['id']['subsystem-vendor'])[2:]