b5b63f7ccebac3ab80b09c6f3890c4c39f1f7bb3
[fuel.git] / deploy / dea.py
1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
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
11 import yaml
12 import io
13 import netaddr
14
15
16 class DeploymentEnvironmentAdapter(object):
17
18     def __init__(self, yaml_path):
19         self.dea_struct = None
20         self.parse_yaml(yaml_path)
21         self.network_names = []
22         self.collect_network_names()
23
24     def modify_ip(self, ip_addr, index, val):
25         ip_str = str(netaddr.IPAddress(ip_addr))
26         decimal_list = map(int, ip_str.split('.'))
27         decimal_list[index] = val
28         return '.'.join(map(str, decimal_list))
29
30     def parse_yaml(self, yaml_path):
31         with io.open(yaml_path) as yaml_file:
32             self.dea_struct = yaml.load(yaml_file)
33
34     def get_env_name(self):
35         return self.get_property('environment')['name']
36
37     def get_env_net_segment_type(self):
38         return self.get_property('environment')['net_segment_type']
39
40     def get_fuel_config(self):
41         return self.dea_struct['fuel']
42
43     def get_fuel_ip(self):
44         fuel_conf = self.get_fuel_config()
45         return fuel_conf['ADMIN_NETWORK']['ipaddress']
46
47     def get_fuel_netmask(self):
48         fuel_conf = self.get_fuel_config()
49         return fuel_conf['ADMIN_NETWORK']['netmask']
50
51     def get_fuel_gateway(self):
52         ip = self.get_fuel_ip()
53         return self.modify_ip(ip, 3, 1)
54
55     def get_fuel_hostname(self):
56         fuel_conf = self.get_fuel_config()
57         return fuel_conf['HOSTNAME']
58
59     def get_fuel_dns(self):
60         fuel_conf = self.get_fuel_config()
61         return fuel_conf['DNS_UPSTREAM']
62
63     def get_node_property(self, node_id, property_name):
64         for node in self.dea_struct['nodes']:
65             if node['id'] == node_id and property_name in node:
66                 return node[property_name]
67
68     def get_node_role(self, node_id):
69         return self.get_node_property(node_id, 'role')
70
71     def get_node_ids(self):
72         node_ids = []
73         for node in self.dea_struct['nodes']:
74             node_ids.append(node['id'])
75         return node_ids
76
77     def get_property(self, property_name):
78         return self.dea_struct[property_name]
79
80     def collect_network_names(self):
81         self.network_names = []
82         for network in self.dea_struct['network']['networks']:
83             self.network_names.append(network['name'])
84
85     def get_network_names(self):
86         return self.network_names
87
88     def get_dns_list(self):
89         settings = self.get_property('settings')
90         dns_list = settings['editable']['external_dns']['dns_list']['value']
91         return [d.strip() for d in dns_list.split(',')]
92
93     def get_ntp_list(self):
94         settings = self.get_property('settings')
95         ntp_list = settings['editable']['external_ntp']['ntp_list']['value']
96         return [n.strip() for n in ntp_list.split(',')]
97
98     def get_blade_node_map(self):
99         return self.dea_struct['blade_node_map']