Revert "Updates scenario files for hunter branch"
[apex.git] / apex / common / parsers.py
1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import json
11 import logging
12 import pprint
13 import os
14 import re
15
16 from apex.common.exceptions import ApexDeployException
17
18 """Parser functions for overcloud/openstack output"""
19
20
21 def parse_nova_output(in_file):
22     """
23     Parses nova list output into a dictionary format for node name and ip
24     :param in_file: json format from openstack server list
25     :return: dictionary format for {"node name": "node ip"}
26     """
27     if not os.path.isfile(in_file):
28         raise FileNotFoundError(in_file)
29     node_dict = dict()
30     with open(in_file, 'r') as fh:
31         nova_list = json.load(fh)
32
33     for server in nova_list:
34         ip_match = re.search('([0-9]+\.){3}[0-9]+', server['Networks'])
35         if ip_match is None:
36             logging.error("Unable to find IP in nova output "
37                           "{}".format(pprint.pformat(server, indent=4)))
38             raise ApexDeployException("Unable to parse IP from nova output")
39         else:
40             node_dict[server['Name']] = ip_match.group(0)
41
42     if not node_dict:
43         raise ApexDeployException("No overcloud nodes found in: {}".format(
44             in_file))
45     return node_dict
46
47
48 def parse_overcloudrc(in_file):
49     """
50     Parses overcloudrc into a dictionary format for key and value
51     :param in_file:
52     :return: dictionary format for {"variable": "value"}
53     """
54     logging.debug("Parsing overcloudrc file {}".format(in_file))
55     if not os.path.isfile(in_file):
56         raise FileNotFoundError(in_file)
57     creds = {}
58     with open(in_file, 'r') as fh:
59         lines = fh.readlines()
60     kv_pattern = re.compile('^export\s+([^\s]+)=([^\s]+)$')
61     for line in lines:
62         if 'export' not in line:
63             continue
64         else:
65             res = re.search(kv_pattern, line.strip())
66             if res:
67                 creds[res.group(1)] = res.group(2)
68                 logging.debug("os cred found: {}, {}".format(res.group(1),
69                                                              res.group(2)))
70             else:
71                 logging.debug("os cred not found in: {}".format(line))
72
73     return creds
74
75
76 def parse_ifcfg_file(in_file):
77     """
78     Parses ifcfg file information
79     :param in_file:
80     :return: dictionary of ifcfg key value pairs
81     """
82     ifcfg_params = {
83         'IPADDR': '',
84         'NETMASK': '',
85         'GATEWAY': '',
86         'METRIC': '',
87         'DNS1': '',
88         'DNS2': '',
89         'PREFIX': ''
90     }
91     with open(in_file, 'r') as fh:
92         for line in fh:
93             for param in ifcfg_params.keys():
94                 match = re.search("^\s*{}=(.*)$".format(param), line)
95                 if match:
96                     ifcfg_params[param] = match.group(1)
97                     break
98     return ifcfg_params