+###############################################################################
+# Copyright (c) 2015 Ericsson AB and others.
+# szilard.cserey@ericsson.com
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+###############################################################################
+
+
import yaml
import io
+import netaddr
+
class DeploymentEnvironmentAdapter(object):
- def __init__(self):
+
+ def __init__(self, yaml_path):
self.dea_struct = None
- self.blade_ids_per_shelves = {}
- self.blades_per_shelves = {}
- self.shelf_ids = []
- self.networks = {}
+ self.parse_yaml(yaml_path)
+ self.network_names = []
+ self.collect_network_names()
+
+ def modify_ip(self, ip_addr, index, val):
+ ip_str = str(netaddr.IPAddress(ip_addr))
+ decimal_list = map(int, ip_str.split('.'))
+ decimal_list[index] = val
+ return '.'.join(map(str, decimal_list))
def parse_yaml(self, yaml_path):
with io.open(yaml_path) as yaml_file:
self.dea_struct = yaml.load(yaml_file)
- self.collect_shelf_and_blade_info()
- self.collect_network_info()
-
- def get_no_of_blades(self):
- no_of_blades = 0
- for shelf in self.dea_struct['shelf']:
- no_of_blades += len(shelf['blade'])
- return no_of_blades
-
- def get_server_type(self):
- return self.dea_struct['server']['type']
-
- def get_server_info(self):
- return (self.dea_struct['server']['type'],
- self.dea_struct['server']['mgmt_ip'],
- self.dea_struct['server']['username'],
- self.dea_struct['server']['password'])
-
- def get_environment_name(self):
- return self.dea_struct['name']
-
- def get_shelf_ids(self):
- return self.shelf_ids
-
- def get_blade_ids_per_shelf(self, shelf_id):
- return self.blade_ids_per_shelves[shelf_id]
-
- def get_blade_ids_per_shelves(self):
- return self.blade_ids_per_shelves
-
- def collect_shelf_and_blade_info(self):
- self.blade_ids_per_shelves = {}
- self.blades_per_shelves = {}
- self.shelf_ids = []
- for shelf in self.dea_struct['shelf']:
- self.shelf_ids.append(shelf['id'])
- blade_ids = self.blade_ids_per_shelves[shelf['id']] = []
- blades = self.blades_per_shelves[shelf['id']] = {}
- for blade in shelf['blade']:
- blade_ids.append(blade['id'])
- blades[blade['id']] = blade
-
- def is_controller(self, shelf_id, blade_id):
- blade = self.blades[shelf_id][blade_id]
- return (True if 'role' in blade and blade['role'] == 'controller'
- else False)
-
- def is_compute_host(self, shelf_id, blade_id):
- blade = self.blades[shelf_id][blade_id]
- return True if 'role' not in blade else False
-
- def collect_network_info(self):
- self.networks = {}
- for network in self.dea_struct['network']:
- self.networks[network['name']] = network
-
- def get_networks(self):
- return self.networks
\ No newline at end of file
+
+ def get_env_name(self):
+ return self.get_property('environment')['name']
+
+ def get_env_mode(self):
+ return self.get_property('environment')['mode']
+
+ def get_env_net_segment_type(self):
+ return self.get_property('environment')['net_segment_type']
+
+ def get_fuel_config(self):
+ return self.dea_struct['fuel']
+
+ def get_fuel_ip(self):
+ fuel_conf = self.get_fuel_config()
+ return fuel_conf['ADMIN_NETWORK']['ipaddress']
+
+ def get_fuel_netmask(self):
+ fuel_conf = self.get_fuel_config()
+ return fuel_conf['ADMIN_NETWORK']['netmask']
+
+ def get_fuel_gateway(self):
+ ip = self.get_fuel_ip()
+ return self.modify_ip(ip, 3, 1)
+
+ def get_fuel_hostname(self):
+ fuel_conf = self.get_fuel_config()
+ return fuel_conf['HOSTNAME']
+
+ def get_fuel_dns(self):
+ fuel_conf = self.get_fuel_config()
+ return fuel_conf['DNS_UPSTREAM']
+
+ def get_node_property(self, node_id, property_name):
+ for node in self.dea_struct['nodes']:
+ if node['id'] == node_id and property_name in node:
+ return node[property_name]
+
+ def get_node_role(self, node_id):
+ return self.get_node_property(node_id, 'role')
+
+ def get_node_ids(self):
+ node_ids = []
+ for node in self.dea_struct['nodes']:
+ node_ids.append(node['id'])
+ return node_ids
+
+ def get_property(self, property_name):
+ return self.dea_struct[property_name]
+
+ def collect_network_names(self):
+ self.network_names = []
+ for network in self.dea_struct['network']['networks']:
+ self.network_names.append(network['name'])
+
+ def get_network_names(self):
+ return self.network_names
+
+ def get_dns_list(self):
+ settings = self.get_property('settings')
+ dns_list = settings['editable']['external_dns']['dns_list']['value']
+ return [d.strip() for d in dns_list.split(',')]
+
+ def get_ntp_list(self):
+ settings = self.get_property('settings')
+ ntp_list = settings['editable']['external_ntp']['ntp_list']['value']
+ return [n.strip() for n in ntp_list.split(',')]