11#!/usr/bin/env python3
22
33import asyncio
4+ import functools
45import json
56import os
67import time
1819
1920
2021async def make_env_json (filename ):
21- loop = asyncio .new_event_loop ()
22- asyncio .set_event_loop (loop )
22+ loop = asyncio .get_event_loop ()
2323 foreman = Foreman (
2424 Config ["foreman_api_url" ],
2525 Config ["foreman_username" ],
@@ -35,10 +35,7 @@ async def make_env_json(filename):
3535 now = time .time ()
3636 old_jsons = [file for file in os .listdir (Config ["json_web_path" ]) if ":" in file ]
3737 for file in old_jsons :
38- if (
39- os .stat (os .path .join (Config ["json_web_path" ], file )).st_mtime
40- < now - Config ["json_retention_days" ] * 86400
41- ):
38+ if os .stat (os .path .join (Config ["json_web_path" ], file )).st_mtime < now - Config ["json_retention_days" ] * 86400 :
4239 os .remove (os .path .join (Config ["json_web_path" ], file ))
4340
4441 for cloud in cloud_list :
@@ -53,9 +50,7 @@ async def make_env_json(filename):
5350 if Config ["foreman_unavailable" ]:
5451 overcloud = {"result" : "true" }
5552 else :
56- overcloud = loop .run_until_complete (
57- foreman .get_host_param (host .name , "overcloud" )
58- )
53+ overcloud = await foreman .get_host_param (host .name , "overcloud" )
5954
6055 if not overcloud :
6156 overcloud = {"result" : "true" }
@@ -76,10 +71,7 @@ async def make_env_json(filename):
7671 if interface .pxe_boot :
7772 mac .append (interface .mac_address )
7873 if filename == "ocpinventory" :
79- mac = [
80- interface .mac_address
81- for interface in sorted (host .interfaces , key = lambda k : k .name )
82- ]
74+ mac = [interface .mac_address for interface in sorted (host .interfaces , key = lambda k : k .name )]
8375 data ["nodes" ].append (
8476 {
8577 "name" : host .name ,
@@ -105,9 +97,7 @@ async def make_env_json(filename):
10597 Config ["json_web_path" ],
10698 "%s_%s.json_%s" % (cloud .name , filename , now .strftime ("%Y-%m-%d_%H:%M:%S" )),
10799 )
108- json_file = os .path .join (
109- Config ["json_web_path" ], "%s_%s.json" % (cloud .name , filename )
110- )
100+ json_file = os .path .join (Config ["json_web_path" ], "%s_%s.json" % (cloud .name , filename ))
111101 with open (new_json_file , "w+" ) as _json_file :
112102 _json_file .seek (0 )
113103 _json_file .write (content )
@@ -116,10 +106,20 @@ async def make_env_json(filename):
116106
117107
118108def main ():
109+ tasks = []
110+ loop = asyncio .get_event_loop ()
111+ if not loop :
112+ loop = asyncio .new_event_loop ()
113+ asyncio .set_event_loop (loop )
119114 if Config ["openstack_management" ]:
120- asyncio .get_event_loop ().run_until_complete (make_env_json ("instackenv" ))
115+ fn = functools .partial (make_env_json , "instackenv" )
116+ tasks .append (fn )
121117 if Config ["openshift_management" ]:
122- asyncio .get_event_loop ().run_until_complete (make_env_json ("ocpinventory" ))
118+ fn = functools .partial (make_env_json , "ocpinventory" )
119+ tasks .append (fn )
120+ loop .run_until_complete (asyncio .gather (* [task () for task in tasks ]))
121+
122+ loop .close ()
123123
124124
125125if __name__ == "__main__" :
0 commit comments