From e6f3258f463f9fcee0b6038e7d0eeacb2d1bf27d Mon Sep 17 00:00:00 2001 From: Gonzalo Rafuls Date: Fri, 21 Jun 2024 14:18:49 +0200 Subject: [PATCH] fix: instack_env loop execution closes: https://github.com/redhat-performance/quads/issues/504 Change-Id: I16e7174e04a6062164ad0a85ce85186434438910 --- src/quads/tools/create_input_assignments.py | 2 +- src/quads/tools/make_instackenv_json.py | 36 ++++++++++----------- tests/tools/test_instackenv.py | 29 +++-------------- 3 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/quads/tools/create_input_assignments.py b/src/quads/tools/create_input_assignments.py index e38986781..fdc095a90 100755 --- a/src/quads/tools/create_input_assignments.py +++ b/src/quads/tools/create_input_assignments.py @@ -128,7 +128,7 @@ def print_summary(): def get_json_link(cloud_name, filename, is_valid, style_tag_start, style_tag_end): if cloud_name == "cloud01": return "" - _link = os.path.join(Config["quads_url"], "cloud", filename) if is_valid else "#" + _link = os.path.join(Config["quads_url"], "instack", filename) if is_valid else "#" _text = "download" if is_valid else "validating" return "%s%s%s" % (_link, style_tag_start, _text, style_tag_end) diff --git a/src/quads/tools/make_instackenv_json.py b/src/quads/tools/make_instackenv_json.py index 9819f9f63..87abbe8ba 100755 --- a/src/quads/tools/make_instackenv_json.py +++ b/src/quads/tools/make_instackenv_json.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import asyncio +import functools import json import os import time @@ -18,8 +19,7 @@ async def make_env_json(filename): - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + loop = asyncio.get_event_loop() foreman = Foreman( Config["foreman_api_url"], Config["foreman_username"], @@ -35,10 +35,7 @@ async def make_env_json(filename): now = time.time() old_jsons = [file for file in os.listdir(Config["json_web_path"]) if ":" in file] for file in old_jsons: - if ( - os.stat(os.path.join(Config["json_web_path"], file)).st_mtime - < now - Config["json_retention_days"] * 86400 - ): + if os.stat(os.path.join(Config["json_web_path"], file)).st_mtime < now - Config["json_retention_days"] * 86400: os.remove(os.path.join(Config["json_web_path"], file)) for cloud in cloud_list: @@ -53,9 +50,7 @@ async def make_env_json(filename): if Config["foreman_unavailable"]: overcloud = {"result": "true"} else: - overcloud = loop.run_until_complete( - foreman.get_host_param(host.name, "overcloud") - ) + overcloud = await foreman.get_host_param(host.name, "overcloud") if not overcloud: overcloud = {"result": "true"} @@ -76,10 +71,7 @@ async def make_env_json(filename): if interface.pxe_boot: mac.append(interface.mac_address) if filename == "ocpinventory": - mac = [ - interface.mac_address - for interface in sorted(host.interfaces, key=lambda k: k.name) - ] + mac = [interface.mac_address for interface in sorted(host.interfaces, key=lambda k: k.name)] data["nodes"].append( { "name": host.name, @@ -105,9 +97,7 @@ async def make_env_json(filename): Config["json_web_path"], "%s_%s.json_%s" % (cloud.name, filename, now.strftime("%Y-%m-%d_%H:%M:%S")), ) - json_file = os.path.join( - Config["json_web_path"], "%s_%s.json" % (cloud.name, filename) - ) + json_file = os.path.join(Config["json_web_path"], "%s_%s.json" % (cloud.name, filename)) with open(new_json_file, "w+") as _json_file: _json_file.seek(0) _json_file.write(content) @@ -116,10 +106,20 @@ async def make_env_json(filename): def main(): + tasks = [] + loop = asyncio.get_event_loop() + if not loop: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) if Config["openstack_management"]: - asyncio.get_event_loop().run_until_complete(make_env_json("instackenv")) + fn = functools.partial(make_env_json, "instackenv") + tasks.append(fn) if Config["openshift_management"]: - asyncio.get_event_loop().run_until_complete(make_env_json("ocpinventory")) + fn = functools.partial(make_env_json, "ocpinventory") + tasks.append(fn) + loop.run_until_complete(asyncio.gather(*[task() for task in tasks])) + + loop.close() if __name__ == "__main__": diff --git a/tests/tools/test_instackenv.py b/tests/tools/test_instackenv.py index 74c3280f3..7ae26ce5d 100644 --- a/tests/tools/test_instackenv.py +++ b/tests/tools/test_instackenv.py @@ -9,34 +9,13 @@ class TestInstackenv(TestBase): def test_make_instackenv_json(self): Config.__setattr__("openstack_management", True) + Config.__setattr__("openshift_management", True) Config.__setattr__("foreman_unavailable", True) - Config.__setattr__( - "json_web_path", os.path.join(os.path.dirname(__file__), "artifacts/") - ) + Config.__setattr__("json_web_path", os.path.join(os.path.dirname(__file__), "artifacts/")) main() - assert list( - open( - os.path.join( - os.path.dirname(__file__), "artifacts/cloud99_instackenv.json" - ) - ) - ) == list( + assert list(open(os.path.join(os.path.dirname(__file__), "artifacts/cloud99_instackenv.json"))) == list( open(os.path.join(os.path.dirname(__file__), "fixtures/cloud99_env.json")) ) - - def test_make_ocpinventory_json(self): - Config.__setattr__("openshift_management", True) - Config.__setattr__("foreman_unavailable", True) - Config.__setattr__( - "json_web_path", os.path.join(os.path.dirname(__file__), "artifacts/") - ) - main() - assert list( - open( - os.path.join( - os.path.dirname(__file__), "artifacts/cloud99_ocpinventory.json" - ) - ) - ) == list( + assert list(open(os.path.join(os.path.dirname(__file__), "artifacts/cloud99_ocpinventory.json"))) == list( open(os.path.join(os.path.dirname(__file__), "fixtures/cloud99_env.json")) )