live: rewrite hardware inventory command

Replace legacy shell script InventarioHardware for helper functions
from hw_inventory.py

Use get_hardware_inventory to obtain a HardwareInventory object with
the hardware information. Map the HardwareInventory object to a legacy
response string with the legacy_list_hardware_inventory function.

Remove "Chrd-*" file reading logic, it's no longer needed. Legacy shell
script InventarioHardware uses that file.

Expect a change in the structure of hardware inventory response payload
in the future. This patch does not address the HTTP response containing
the hardware inventory as a '\n' separated string of hardware elements.
more_events
Jose M. Guisado 2023-04-17 13:02:54 +02:00
parent 49a86bddd9
commit ee80dc89ad
2 changed files with 16 additions and 20 deletions

View File

@ -31,6 +31,7 @@ from src.utils.disk import *
from src.utils.cache import generate_cache_txt, umount_cache, init_cache from src.utils.cache import generate_cache_txt, umount_cache, init_cache
from src.utils.tiptorrent import * from src.utils.tiptorrent import *
from src.utils.sw_inventory import get_package_set from src.utils.sw_inventory import get_package_set
from src.utils.hw_inventory import get_hardware_inventory, legacy_list_hardware_inventory
OG_SHELL = '/bin/bash' OG_SHELL = '/bin/bash'
@ -274,24 +275,21 @@ class OgLiveOperations:
# "{package_name} {package_version}" # "{package_name} {package_version}"
return '\n'.join(map(str,pkgset)) return '\n'.join(map(str,pkgset))
def hardware(self, path, ogRest): def hardware(self, ogRest):
self._restartBrowser(self._url_log) self._restartBrowser(self._url_log)
logging.info('Running hardware inventory command')
try: try:
cmd = f'{ogClient.OG_PATH}interfaceAdm/InventarioHardware {path}' inventory = get_hardware_inventory()
ogRest.proc = subprocess.Popen([cmd], except ValueError as e:
stdout=subprocess.PIPE, logging.error('Error occurred while running get_hardware_inventory')
shell=True, raise e
executable=OG_SHELL) finally:
(output, error) = ogRest.proc.communicate()
except:
logging.error('Exception when running hardware inventory subprocess')
raise ValueError('Error: Incorrect command value')
self._restartBrowser(self._url) self._restartBrowser(self._url)
logging.info('Hardware inventory command OK') result = legacy_list_hardware_inventory(inventory)
return output.decode('utf-8') logging.info('Successful hardware inventory command execution')
return result
def setup(self, request, ogRest): def setup(self, request, ogRest):
table_type = request.getType() table_type = request.getType()

View File

@ -135,16 +135,15 @@ class ogThread():
client.send(response.get()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
def hardware(client, path, ogRest): def hardware(client, ogRest):
try: try:
ogRest.operations.hardware(path, ogRest) result = ogRest.operations.hardware(ogRest)
except Exception as e: except Exception as e:
ogRest.send_internal_server_error(client, exc=e) ogRest.send_internal_server_error(client, exc=e)
return return
json_body = jsonBody() json_body = jsonBody()
with open(path, 'r') as f: json_body.add_element('hardware', result)
json_body.add_element('hardware', f.read())
response = restResponse(ogResponses.OK, json_body) response = restResponse(ogResponses.OK, json_body)
client.send(response.get()) client.send(response.get())
@ -399,8 +398,7 @@ class ogRest():
threading.Thread(target=ogThread.software, args=(client, request, path, self,)).start() threading.Thread(target=ogThread.software, args=(client, request, path, self,)).start()
def process_hardware(self, client): def process_hardware(self, client):
path = '/tmp/Chrd-' + client.ip threading.Thread(target=ogThread.hardware, args=(client, self,)).start()
threading.Thread(target=ogThread.hardware, args=(client, path, self,)).start()
def process_schedule(self, client): def process_schedule(self, client):
response = restResponse(ogResponses.OK) response = restResponse(ogResponses.OK)