def validate_ip_in_network(ip, cidr): try: if netaddr.IPAddress(ip) not in netaddr.IPNetwork(cidr): raise configerror.ConfigError('%s does not belong to network %s' % (ip, cidr)) except Exception as exp: raise configerror.ConfigError(str(exp))
def get_provider_network_vlan_ranges(self, network): """ Get vlan ranges for the given provider network Arguments: Provider network name Returns: Vlan ranges for the provider network Raises: ConfigError in-case of an error """ networks = self.get_provider_networks() if network not in networks: raise configerror.ConfigError( 'Missing configuration for provider network %s' % network) if 'vlan_ranges' not in self.config[ self.ROOT]['provider_networks'][network]: raise configerror.ConfigError( 'Missing vlan ranges configuration for provider network %s' % network) return self.config[ self.ROOT]['provider_networks'][network]['vlan_ranges']
def get_host_interface(self, host, network): """ get the host interface allocated from a specific network Argument: hostname: The name of the host networkname: The name of the network Return: The interface for the host Raise: ConfigError in-case of an error """ self._validate_network(network) hostnetconfigkey = host + '.' + self.DOMAIN if hostnetconfigkey not in self.config: raise configerror.ConfigError( 'No network configuration available for %s' % host) if network not in self.config[hostnetconfigkey]: raise configerror.ConfigError( 'No network configuration available for %s' % host) if 'interface' not in self.config[hostnetconfigkey][network]: raise configerror.ConfigError( 'No interface assigned for %s in network %s' % (host, network)) return self.config[hostnetconfigkey][network]['interface']
def get_host_mask(self, host, network): """ get the network mask for the host Argument: hostname: The name of the host networkname: The name of the network Return: The network mask Raise: ConfigError in-case of an error """ self._validate_network(network) hostnetconfigkey = host + '.' + self.DOMAIN if hostnetconfigkey not in self.config: raise configerror.ConfigError( 'No network configuration available for %s' % host) if network not in self.config[hostnetconfigkey]: raise configerror.ConfigError( 'No network configuration available for %s' % host) if 'mask' not in self.config[hostnetconfigkey][network]: raise configerror.ConfigError( 'No mask assigned for %s in network %s' % (host, network)) return self.config[hostnetconfigkey][network]['mask']
def _validate_network_profile(self, profile): bondinginterfaces = None try: bondinginterfaces = self.get_profile_bonding_interfaces(profile) except configerror.ConfigError: pass if bondinginterfaces: utils.validate_list_items_unique(bondinginterfaces) for bond in bondinginterfaces: bondedinterfaces = self.get_profile_bonded_interfaces( profile, bond) utils.validate_list_items_unique(bondedinterfaces) if len(bondedinterfaces) < 2: raise configerror.ConfigError( 'Number of bonded interfaces should be at least 2 in %s' % bond) mappedinterfaces = self.get_profile_network_mapped_interfaces(profile) utils.validate_list_items_unique(mappedinterfaces) netconf = self.confman.get_networking_config_handler() validnetworks = netconf.get_networks() for interface in mappedinterfaces: networks = self.get_profile_interface_mapped_networks( profile, interface) utils.validate_list_items_unique(networks) for network in networks: if network not in validnetworks: raise configerror.ConfigError( 'Network %s is not valid' % network)
def validate_cidr(cidr): try: tok = cidr.split('/') if len(tok) != 2: raise configerror.ConfigError('Invalid cidr address %s' % cidr) validate_ipv4_address(tok[0]) except Exception as exp: raise configerror.ConfigError(str(exp))
def validate_ipv4_address(address): try: socket.inet_pton(socket.AF_INET, address) except AttributeError: try: socket.inet_aton(address) except socket.error: raise configerror.ConfigError('Invalid ip %s' % address) if address.count('.') != 3: raise configerror.ConfigError('Invalid ip %s' % address) except socket.error: raise configerror.ConfigError('Invalid ip %s' % address)
def _validate_network(self, network, domain=None): networks = self.get_networks() if network not in networks: raise configerror.ConfigError('Invalid network name %s' % network) if NETWORK_DOMAINS not in self.config[self.ROOT][network]: raise configerror.ConfigError('No network domains for network %s' % network) if domain and domain not in self.config[ self.ROOT][network][NETWORK_DOMAINS]: raise configerror.ConfigError('Invalid network domain name %s' % domain)
def get_network_vlan_id(self, network, domain): """ get the network vlan id Arguments: Network name Network domain Return: The vlan id Raise: ConfigError in-case of an error """ self._validate_network(network, domain) if 'vlan' not in self.config[ self.ROOT][network][NETWORK_DOMAINS][domain]: raise configerror.ConfigError('No vlan specified for %s in %s' % (network, domain)) return self.config[self.ROOT][network][NETWORK_DOMAINS][domain]['vlan']
def get_network_gateway(self, network, domain): """ get the network gateway Arguments: Network name Network domain Return: The gateway address Raise: ConfigError in-case of an error """ self._validate_network(network, domain) if 'gateway' not in self.config[ self.ROOT][network][NETWORK_DOMAINS][domain]: raise configerror.ConfigError( 'No gateway configured for network %s in %s' % (network, domain)) return self.config[ self.ROOT][network][NETWORK_DOMAINS][domain]['gateway']
def get_network_cidr(self, network, domain): """ get the network cidr Arguments: Network name Network domain Return: The cidr address Raise: ConfigError in-case of an error """ self._validate_network(network, domain) if 'cidr' not in self.config[ self.ROOT][network][NETWORK_DOMAINS][domain]: raise configerror.ConfigError('No CIDR for network %s in %s' % (network, domain)) return self.config[self.ROOT][network][NETWORK_DOMAINS][domain]['cidr']
def allocate_static_ip(self, ip, network, domain=None): """ allocate the given ip in some network Arguments: Ip address Network name Network domain Return: The allocated ip address Raise: ConfigError in-case of an error """ self._validate_network(network, domain) try: self.freepool[network][domain].remove(ip) return ip except Exception: raise configerror.ConfigError( 'Failed to allocate %s for network %s in %s' % (ip, network, domain))
def allocate_ip(self, network, domain): """ get a new free ip in some network Arguments: Network name Network domain Return: The free ip address Raise: ConfigError in-case of an error """ self._validate_network(network, domain) try: iterator = self.freepool[network][domain].__iter__() ip = str(iterator.next()) self.freepool[network][domain].remove(ip) return ip except Exception: raise configerror.ConfigError( 'Failed to allocate ip for network %s in %s' % (network, domain))
def get_host_having_hwmgmt_address(self, hwmgmtips): """ get the node name matching an ipmi address Argument: The ipmi address Return: The node name Raise: ConfigError in-case of an error """ import ipaddress hosts = self.get_hosts() for host in hosts: ip = self.get_hwmgmt_ip(host) for hwmgtip in hwmgmtips: addr = ipaddress.ip_address(unicode(hwmgtip)) if addr.version == 6: hwmgtip = addr.compressed ip = ipaddress.ip_address(unicode(ip)) ip = ip.compressed if ip == hwmgtip: return host raise configerror.ConfigError( 'No hosts are matching the provided hw address %s' % hwmgmtips)
def get_host_networks(self, hostname): """ get the host networks Argument: The host name Return: A list of network names Raise: ConfigError in-case of an error """ node_network_profiles = self.get_network_profiles(hostname) netprofconf = self.confman.get_network_profiles_config_handler() result = [] for profile in node_network_profiles: interfaces = netprofconf.get_profile_network_mapped_interfaces( profile) for interface in interfaces: networks = netprofconf.get_profile_interface_mapped_networks( profile, interface) for network in networks: if network not in result: result.append(network) if not result: raise configerror.ConfigError('No networks found for host %s' % hostname) return result
def get_host_network_interface(self, host, network): """ get the host interface used for some network Argument: the host name the network name Return: The interface name Raise: ConfigError in-case of an error """ node_network_profiles = self.get_network_profiles(host) netprofconf = self.confman.get_network_profiles_config_handler() for profile in node_network_profiles: interfaces = netprofconf.get_profile_network_mapped_interfaces( profile) for interface in interfaces: networks = netprofconf.get_profile_interface_mapped_networks( profile, interface) if network in networks: return interface raise configerror.ConfigError( 'No interfaces found for network %s in host %s' % (network, host))
def get_storage_profile_hosts(self, profile): """ get hosts having some storage profile Argument: storage profile name Return: A list of host names Raise: ConfigError in-case of an error """ hosts = self.get_hosts() result = [] for host in hosts: try: node_storage_profiles = self.get_storage_profiles(host) if profile in node_storage_profiles: result.append(host) except configerror.ConfigError: pass if not result: raise configerror.ConfigError('No hosts found for profile %s' % profile) return result
def _validate_storage_backend(self): backend = self.get_storage_backend() storageconf = self.confman.get_storage_config_handler() backends = storageconf.get_storage_backends() if backend not in backends: raise configerror.ConfigError('Invalid storage backend %s' % backend)
def _validate_time_zone(self): import pytz try: zone = self.get_zone() pytz.timezone(zone) except: raise configerror.ConfigError('The timezone %s is not valid' % zone)
def validate(self): self.validate_root() if 'zone' not in self.config[self.ROOT]: raise configerror.ConfigError('No zone configuration found') if 'ntp_servers' not in self.config[self.ROOT]: raise configerror.ConfigError('No ntp servers found') if 'auth_type' not in self.config[self.ROOT]: self.config[self.ROOT]['auth_type'] = 'crypto' if 'serverkeys_path' not in self.config[self.ROOT]: self.config[self.ROOT]['serverkeys_path'] = '' self._validate_time_zone() self._validate_ntp_servers()
def __init__(self, configjson): #validate according to schema self.configmap = {} try: self.configjson = configjson self._load_config_handlers() except Exception as exp: raise configerror.ConfigError(str(exp))
def _get_profile_provider_network_interface_dict(self, profile, interface): self.is_valid_profile(profile) interfaces = self.get_profile_provider_network_interfaces(profile) if interface not in interfaces: raise configerror.ConfigError( 'Profile %s has no provider interface with name %s' % (profile, interface)) return self.config[self.ROOT][profile]['provider_network_interfaces'][interface]
def get_managements_network_domain(self): managements = self.get_service_profile_hosts('management') domains = set() for management in managements: domains.add(self.get_host_network_domain(management)) if len(domains) != 1: raise configerror.ConfigError( 'Management in different networking domains not supported') return domains.pop()
def _validate_service_profiles(self, hostname): node_profiles = self.get_service_profiles(hostname) utils.validate_list_items_unique(node_profiles) service_profiles_lib = profiles.Profiles() serviceprofiles = service_profiles_lib.get_service_profiles() for profile in node_profiles: if profile not in serviceprofiles: raise configerror.ConfigError( 'Invalid service profile %s specified for host %s' % (profile, hostname))
def _validate_network_profiles(self, hostname): node_profiles = self.get_network_profiles(hostname) utils.validate_list_items_unique(profiles) netprofconf = self.confman.get_network_profiles_config_handler() netprofiles = netprofconf.get_network_profiles() for profile in node_profiles: if profile not in netprofiles: raise configerror.ConfigError( 'Invalid network profile %s specified for host %s' % (profile, hostname))
def get_controllers_network_domain(self): controllers = self.get_service_profile_hosts('controller') domains = set() for controller in controllers: domains.add(self.get_host_network_domain(controller)) if len(domains) != 1: raise configerror.ConfigError( 'Controllers in different networking domains not supported') return domains.pop()
def get_network_routes(self, network, domain): self._validate_network(network, domain) if 'routes' not in self.config[ self.ROOT][network][NETWORK_DOMAINS][domain]: raise configerror.ConfigError( 'No routes configured for network %s in %s' % (network, domain)) return self.config[ self.ROOT][network][NETWORK_DOMAINS][domain]['routes']
def _validate_storage_backends(self): backends = self.get_storage_backends() for backend in backends: if backend == 'ceph': self._validate_ceph_backend() elif backend == 'external_ceph': self._validate_external_ceph_backend() elif backend == 'lvm': self._validate_lvm_backend() else: raise configerror.ConfigError('Invalid backend %s specified' % backend)
def get_user_password(self, user): """ get the password for a user Return: A string representing the password Raise: ConfigError in-case of an error """ raise configerror.ConfigError('Invalid user %s' % user)
def get_ceph_osd_pool_size(self): """ get the ceph osd pool size Return: The ceph osd pool size Raise: ConfigError in-case of an error """ self.validate_root() backends = self.get_storage_backends() if 'ceph' not in backends: raise configerror.ConfigError('No ceph backend configured') if 'osd_pool_default_size' not in self.config[self.ROOT]['backends']['ceph']: raise configerror.ConfigError('No ceph osd configuration found') return self.config[self.ROOT]['backends']['ceph']['osd_pool_default_size']