Fix Metadata Generation

With the current model of dumping ansible facts with the template and
regexing it to make it JSON readable, metadata generation into files
is failing in some cases. It is better to have the template handle
creation of a serializable JSON object and that is what this commit does.
Also some unnecessary keys in lib/Metadata.py used to parse the JSON have
been removed since all of that is now handled by the template.

Change-Id: I7d4a4f9fde0af8a52aa72374614c20f4b8abc421
Co-Authored-By: Alex Krzos <akrzos@redhat.com>
This commit is contained in:
Sai Sindhur Malleni 2016-09-09 17:08:01 -04:00 committed by Alex Krzos
parent 9ad1592711
commit 5ba7f21ce6
2 changed files with 9 additions and 10 deletions

View File

@ -1,12 +1,13 @@
[
{% for host in groups['controller'] %}
{{hostvars[host]| to_nice_json}}
{{hostvars[host]| to_nice_json}},
{% endfor %}
{% for host in groups['compute'] %}
{{hostvars[host]| to_nice_json}}
{{hostvars[host]| to_nice_json}},
{% endfor %}
{% for host in groups['undercloud'] %}
{{hostvars[host]| to_nice_json}}
{% endfor %}
]

View File

@ -29,15 +29,13 @@ class Metadata:
except IOError:
print("Machine facts json is missing")
exit(1)
new_json = re.sub(r"}\n{", r"},\n{", json_str, re.M)
convert = "{ \"machines\": [" + new_json + "] }"
sys_data = {}
sys_data['system_data'] = json.loads(convert)
sys_data['system_data'] = json.loads(json_str)
return sys_data
def get_hardware_metadata(self, sys_data):
hard_dict = {}
for item in sys_data['system_data']['machines']:
for item in sys_data['system_data']:
if 'hardware_details' not in hard_dict:
hard_dict['hardware_details'] = []
hardware_dict = {}
@ -57,7 +55,7 @@ class Metadata:
def get_environment_metadata(self, sys_data):
env_dict = {}
for item in sys_data['system_data']['machines']:
for item in sys_data['system_data']:
if 'environment_setup' not in env_dict:
env_dict['environment_setup'] = {}
for key, value in item.items():
@ -67,7 +65,7 @@ class Metadata:
def get_software_metadata(self, sys_data):
soft_all_dict = {}
for item in sys_data['system_data']['machines']:
for item in sys_data['system_data']:
if 'software_details' not in soft_all_dict:
soft_all_dict['software_details'] = {}
nodes = ['controller', 'undercloud', 'compute']
@ -93,7 +91,7 @@ class Metadata:
def write_metadata_file(self, data, filename):
with open(filename, 'w') as json_file:
json.dump(data, json_file)
json.dump(data, json_file, indent=4, sort_keys=True)
def main():