mirror of https://git.48k.eu/ogclient
Encapsulate operations in classes
parent
c279325919
commit
99ae598fbd
|
@ -12,7 +12,8 @@ from src.ogConfig import *
|
||||||
|
|
||||||
OG_SHELL = '/bin/bash'
|
OG_SHELL = '/bin/bash'
|
||||||
|
|
||||||
def parseGetConf(out):
|
class OgLinuxOperations:
|
||||||
|
def parseGetConf(self, out):
|
||||||
parsed = {'serial_number': '',
|
parsed = {'serial_number': '',
|
||||||
'disk_setup': '',
|
'disk_setup': '',
|
||||||
'partition_setup': list()}
|
'partition_setup': list()}
|
||||||
|
@ -37,7 +38,7 @@ def parseGetConf(out):
|
||||||
parsed['partition_setup'].append(part_setup)
|
parsed['partition_setup'].append(part_setup)
|
||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
def poweroff():
|
def poweroff(self):
|
||||||
if os.path.exists('/scripts/oginit'):
|
if os.path.exists('/scripts/oginit'):
|
||||||
cmd = f'source {ogConfig.OG_PATH}etc/preinit/loadenviron.sh; ' \
|
cmd = f'source {ogConfig.OG_PATH}etc/preinit/loadenviron.sh; ' \
|
||||||
f'{ogConfig.OG_PATH}scripts/poweroff'
|
f'{ogConfig.OG_PATH}scripts/poweroff'
|
||||||
|
@ -45,7 +46,7 @@ def poweroff():
|
||||||
else:
|
else:
|
||||||
subprocess.call(['/sbin/poweroff'])
|
subprocess.call(['/sbin/poweroff'])
|
||||||
|
|
||||||
def reboot():
|
def reboot(self):
|
||||||
if os.path.exists('/scripts/oginit'):
|
if os.path.exists('/scripts/oginit'):
|
||||||
cmd = f'source {ogConfig.OG_PATH}etc/preinit/loadenviron.sh; ' \
|
cmd = f'source {ogConfig.OG_PATH}etc/preinit/loadenviron.sh; ' \
|
||||||
f'{ogConfig.OG_PATH}scripts/reboot'
|
f'{ogConfig.OG_PATH}scripts/reboot'
|
||||||
|
@ -53,7 +54,7 @@ def reboot():
|
||||||
else:
|
else:
|
||||||
subprocess.call(['/sbin/reboot'])
|
subprocess.call(['/sbin/reboot'])
|
||||||
|
|
||||||
def execCMD(request, ogRest):
|
def execCMD(self, request, ogRest):
|
||||||
cmd = request.getrun()
|
cmd = request.getrun()
|
||||||
cmds = cmd.split(";|\n\r")
|
cmds = cmd.split(";|\n\r")
|
||||||
try:
|
try:
|
||||||
|
@ -67,7 +68,7 @@ def execCMD(request, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def session(request, ogRest):
|
def session(self, request, ogRest):
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
partition = request.getPartition()
|
partition = request.getPartition()
|
||||||
cmd = f'{ogConfig.OG_PATH}interfaceAdm/IniciarSesion {disk} {partition}'
|
cmd = f'{ogConfig.OG_PATH}interfaceAdm/IniciarSesion {disk} {partition}'
|
||||||
|
@ -83,7 +84,7 @@ def session(request, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def software(request, path, ogRest):
|
def software(self, request, path, ogRest):
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
partition = request.getPartition()
|
partition = request.getPartition()
|
||||||
|
|
||||||
|
@ -101,7 +102,7 @@ def software(request, path, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def hardware(path, ogRest):
|
def hardware(self, path, ogRest):
|
||||||
try:
|
try:
|
||||||
cmd = f'{ogConfig.OG_PATH}interfaceAdm/InventarioHardware {path}'
|
cmd = f'{ogConfig.OG_PATH}interfaceAdm/InventarioHardware {path}'
|
||||||
ogRest.proc = subprocess.Popen([cmd],
|
ogRest.proc = subprocess.Popen([cmd],
|
||||||
|
@ -114,7 +115,7 @@ def hardware(path, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def setup(request, ogRest):
|
def setup(self, request, ogRest):
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
cache = request.getCache()
|
cache = request.getCache()
|
||||||
cache_size = request.getCacheSize()
|
cache_size = request.getCacheSize()
|
||||||
|
@ -141,9 +142,9 @@ def setup(request, ogRest):
|
||||||
|
|
||||||
cmd_get_conf = f'{ogConfig.OG_PATH}interfaceAdm/getConfiguration'
|
cmd_get_conf = f'{ogConfig.OG_PATH}interfaceAdm/getConfiguration'
|
||||||
result = subprocess.check_output([cmd_get_conf], shell=True)
|
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()
|
disk = request.getDisk()
|
||||||
partition = request.getPartition()
|
partition = request.getPartition()
|
||||||
name = request.getName()
|
name = request.getName()
|
||||||
|
@ -165,7 +166,7 @@ def image_restore(request, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def image_create(path, request, ogRest):
|
def image_create(self, path, request, ogRest):
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
partition = request.getPartition()
|
partition = request.getPartition()
|
||||||
name = request.getName()
|
name = request.getName()
|
||||||
|
@ -198,7 +199,7 @@ def image_create(path, request, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def refresh(ogRest):
|
def refresh(self, ogRest):
|
||||||
try:
|
try:
|
||||||
cmd = f'{ogConfig.OG_PATH}interfaceAdm/getConfiguration'
|
cmd = f'{ogConfig.OG_PATH}interfaceAdm/getConfiguration'
|
||||||
ogRest.proc = subprocess.Popen([cmd],
|
ogRest.proc = subprocess.Popen([cmd],
|
||||||
|
@ -209,4 +210,4 @@ def refresh(ogRest):
|
||||||
except:
|
except:
|
||||||
raise ValueError('Error: Incorrect command value')
|
raise ValueError('Error: Incorrect command value')
|
||||||
|
|
||||||
return parseGetConf(output.decode('utf-8'))
|
return self.parseGetConf(output.decode('utf-8'))
|
||||||
|
|
|
@ -19,9 +19,6 @@ import pathlib
|
||||||
import re
|
import re
|
||||||
import math
|
import math
|
||||||
|
|
||||||
IP = '127.0.0.1'
|
|
||||||
VIRTUAL_PORT = 4444
|
|
||||||
|
|
||||||
class OgQMP:
|
class OgQMP:
|
||||||
class State(enum.Enum):
|
class State(enum.Enum):
|
||||||
CONNECTING = 0
|
CONNECTING = 0
|
||||||
|
@ -59,8 +56,13 @@ class OgQMP:
|
||||||
self.state = self.State.FORCE_DISCONNECTED
|
self.state = self.State.FORCE_DISCONNECTED
|
||||||
self.sock.close()
|
self.sock.close()
|
||||||
|
|
||||||
def poweroff():
|
class OgVirtualOperations:
|
||||||
qmp = OgQMP(IP, VIRTUAL_PORT)
|
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.connect()
|
||||||
qmp.recv()
|
qmp.recv()
|
||||||
qmp.send(str({"execute": "qmp_capabilities"}))
|
qmp.send(str({"execute": "qmp_capabilities"}))
|
||||||
|
@ -68,8 +70,8 @@ def poweroff():
|
||||||
qmp.send(str({"execute": "system_powerdown"}))
|
qmp.send(str({"execute": "system_powerdown"}))
|
||||||
qmp.disconnect()
|
qmp.disconnect()
|
||||||
|
|
||||||
def reboot():
|
def reboot(self):
|
||||||
qmp = OgQMP(IP, VIRTUAL_PORT)
|
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
|
||||||
qmp.connect()
|
qmp.connect()
|
||||||
qmp.recv()
|
qmp.recv()
|
||||||
qmp.send(str({"execute": "qmp_capabilities"}))
|
qmp.send(str({"execute": "qmp_capabilities"}))
|
||||||
|
@ -77,7 +79,7 @@ def reboot():
|
||||||
qmp.send(str({"execute": "system_reset"}))
|
qmp.send(str({"execute": "system_reset"}))
|
||||||
qmp.disconnect()
|
qmp.disconnect()
|
||||||
|
|
||||||
def session(request, ogRest):
|
def session(self, request, ogRest):
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
partition = request.getPartition()
|
partition = request.getPartition()
|
||||||
|
|
||||||
|
@ -91,7 +93,7 @@ def session(request, ogRest):
|
||||||
f'-display gtk -cpu host -m {vm_ram_mib}M -boot c')
|
f'-display gtk -cpu host -m {vm_ram_mib}M -boot c')
|
||||||
subprocess.Popen([cmd], shell=True)
|
subprocess.Popen([cmd], shell=True)
|
||||||
|
|
||||||
def refresh(ogRest):
|
def refresh(self, ogRest):
|
||||||
path = 'partitions.json'
|
path = 'partitions.json'
|
||||||
temp_mount_dir = 'mnt'
|
temp_mount_dir = 'mnt'
|
||||||
try:
|
try:
|
||||||
|
@ -148,7 +150,7 @@ def refresh(ogRest):
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def setup(request, ogRest):
|
def setup(self, request, ogRest):
|
||||||
path = 'partitions.json'
|
path = 'partitions.json'
|
||||||
refresh(ogRest)
|
refresh(ogRest)
|
||||||
|
|
||||||
|
@ -188,14 +190,14 @@ def setup(request, ogRest):
|
||||||
|
|
||||||
return refresh(ogRest)
|
return refresh(ogRest)
|
||||||
|
|
||||||
def image_create(path, request, ogRest):
|
def image_create(self, path, request, ogRest):
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
partition = request.getPartition()
|
partition = request.getPartition()
|
||||||
name = request.getName()
|
name = request.getName()
|
||||||
repo = request.getRepo()
|
repo = request.getRepo()
|
||||||
|
|
||||||
# Check if VM is running.
|
# Check if VM is running.
|
||||||
qmp = OgQMP(IP, VIRTUAL_PORT)
|
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
|
||||||
if qmp.connect() != None:
|
if qmp.connect() != None:
|
||||||
qmp.disconnect()
|
qmp.disconnect()
|
||||||
return None
|
return None
|
||||||
|
@ -212,7 +214,7 @@ def image_create(path, request, ogRest):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def image_restore(request, ogRest):
|
def image_restore(self, request, ogRest):
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
partition = request.getPartition()
|
partition = request.getPartition()
|
||||||
name = request.getName()
|
name = request.getName()
|
||||||
|
@ -223,7 +225,7 @@ def image_restore(request, ogRest):
|
||||||
cid = request.getId()
|
cid = request.getId()
|
||||||
|
|
||||||
# Check if VM is running.
|
# Check if VM is running.
|
||||||
qmp = OgQMP(IP, VIRTUAL_PORT)
|
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
|
||||||
if qmp.connect() != None:
|
if qmp.connect() != None:
|
||||||
qmp.disconnect()
|
qmp.disconnect()
|
||||||
return None
|
return None
|
||||||
|
@ -243,7 +245,7 @@ def image_restore(request, ogRest):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def software(request, path, ogRest):
|
def software(self, request, path, ogRest):
|
||||||
DPKG_PATH = '/var/lib/dpkg/status'
|
DPKG_PATH = '/var/lib/dpkg/status'
|
||||||
|
|
||||||
disk = request.getDisk()
|
disk = request.getDisk()
|
||||||
|
@ -314,7 +316,7 @@ def software(request, path, ogRest):
|
||||||
f.write(f'{program}\n')
|
f.write(f'{program}\n')
|
||||||
f.truncate()
|
f.truncate()
|
||||||
|
|
||||||
def parse_pci(path='/usr/share/misc/pci.ids'):
|
def parse_pci(self, path='/usr/share/misc/pci.ids'):
|
||||||
data = {}
|
data = {}
|
||||||
with open(path, 'r') as f:
|
with open(path, 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
|
@ -338,8 +340,8 @@ def parse_pci(path='/usr/share/misc/pci.ids'):
|
||||||
data[fields[0]] = {'name': fields[1]}
|
data[fields[0]] = {'name': fields[1]}
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def hardware(path, ogRest):
|
def hardware(self, path, ogRest):
|
||||||
qmp = OgQMP(IP, VIRTUAL_PORT)
|
qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
|
||||||
qmp.connect()
|
qmp.connect()
|
||||||
qmp.recv()
|
qmp.recv()
|
||||||
qmp.send(str({"execute": "qmp_capabilities"}))
|
qmp.send(str({"execute": "qmp_capabilities"}))
|
||||||
|
@ -349,8 +351,7 @@ def hardware(path, ogRest):
|
||||||
data = data['return'][0]['devices']
|
data = data['return'][0]['devices']
|
||||||
pci_list = parse_pci()
|
pci_list = parse_pci()
|
||||||
device_names = {}
|
device_names = {}
|
||||||
for device in da
|
for device in data:
|
||||||
ta:
|
|
||||||
vendor_id = hex(device['id']['vendor'])[2:]
|
vendor_id = hex(device['id']['vendor'])[2:]
|
||||||
device_id = hex(device['id']['device'])[2:]
|
device_id = hex(device['id']['device'])[2:]
|
||||||
subvendor_id = hex(device['id']['subsystem-vendor'])[2:]
|
subvendor_id = hex(device['id']['subsystem-vendor'])[2:]
|
||||||
|
|
Loading…
Reference in New Issue