Skip to content

Commit

Permalink
fix: instack_env loop execution
Browse files Browse the repository at this point in the history
closes: #504

Change-Id: I16e7174e04a6062164ad0a85ce85186434438910
  • Loading branch information
grafuls committed Jun 21, 2024
1 parent 7ed66d7 commit e6f3258
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/quads/tools/create_input_assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<a href=%s target=_blank>%s%s%s</a>" % (_link, style_tag_start, _text, style_tag_end)

Expand Down
36 changes: 18 additions & 18 deletions src/quads/tools/make_instackenv_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import asyncio
import functools
import json
import os
import time
Expand All @@ -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"],
Expand All @@ -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:
Expand All @@ -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"}
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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__":
Expand Down
29 changes: 4 additions & 25 deletions tests/tools/test_instackenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)

0 comments on commit e6f3258

Please sign in to comment.