def get_bridge_port_device(bridge): """Return the nics list that belongs to a port of 'bridge'. Args: bridge (str): the bridge name. Returns: List[str]: the nic list. """ # br --- v --- bond --- nic1 if encode_value(bridge) not in map(encode_value, bridges()): raise ValueError('unknown bridge %s' % bridge) nics_list = [] for port in ports(bridge): if encode_value(port) in map(encode_value, vlans()): device = get_vlan_device(port) if encode_value(device) in map(encode_value, bondings()): nics_list.extend(slaves(device)) else: nics_list.append(device) if encode_value(port) in map(encode_value, bondings()): nics_list.extend(slaves(port)) else: nics_list.append(port) return nics_list
def validate_and_get_bond_info(self, params, cfgmap): if cfgmap[TYPE] == IFACE_BOND: cfgInterfacesHelper.clean_slaves(cfgmap, params) bond_info = {} wok_log.info('Validating bond info given for interface') if DEVICE not in cfgmap: wok_log.error("Missing parameter: DEVICE") raise MissingParameter("GINNET0025E") cfgInterfacesHelper.validate_device_name(cfgmap[DEVICE]) if BONDINFO not in params[BASIC_INFO]: wok_log.error("Missing parameter: BONDINFO") raise MissingParameter("GINNET0032E") bondinfo = params[BASIC_INFO][BONDINFO] if BONDING_MASTER in bondinfo: if not bondinfo[BONDING_MASTER] == "yes": wok_log.error("'yes' or 'no' is allowed value for the " "BONDING_MASTER") raise MissingParameter("GINNET0033E") else: bond_info[BONDING_MASTER] = bondinfo[BONDING_MASTER] else: wok_log.error("Missing parameter: BONDING_MASTER") raise MissingParameter("GINNET0034E") if BONDING_OPTS in params[BASIC_INFO][BONDINFO]: bond_opt_value = "" bondopts = bondinfo[BONDING_OPTS] if cfgInterfacesHelper.validate_bond_opts(bondopts, params): for bond_opt_key in BONDING_OPTS_LIST: if bond_opt_key in bondinfo[BONDING_OPTS]: value = bondinfo[BONDING_OPTS][bond_opt_key] if type(value) is not list: bond_opt_value = \ bond_opt_value + bond_opt_key + "=" + \ encode_value(bondinfo[BONDING_OPTS][ bond_opt_key]) + " " else: values_as_str = map(encode_value, value) values_as_str = encode_value(values_as_str) v = values_as_str.replace(" ", "") bond_opt_value = \ bond_opt_value + bond_opt_key + "=" + v + " " bond_opt_value = '"' + bond_opt_value + '"' bond_info[BONDING_OPTS] = bond_opt_value if SLAVES not in bondinfo: wok_log.error("Missing parameter(s): SLAVE") raise MissingParameter("GINNET0036E") if len(bondinfo[SLAVES]) == 0: wok_log.error("Minimum one slave has to be given for the bond " "interface") raise MissingParameter("GINNET0037E") name = cfgmap[DEVICE] self.create_slaves(name, params) bond_info[TYPE] = params[BASIC_INFO][TYPE] return bond_info
def get_interface_info(iface): """Returns information about the interface iface. Args: iface (str): the interface name. Returns: dict: a dict containing the interface info. Format: { 'device': (str), 'name': (str), 'type': (str), 'status': (str), 'link_detected': (str), 'ipaddr': (str), 'netmask': (str), 'macaddr': (str), 'module': (str), 'nic_type': (str) } """ if encode_value(iface) not in map(encode_value, ethtool.get_devices()): raise ValueError('unknown interface: %s' % iface) ipaddr = '' netmask = '' try: ipaddr = ethtool.get_ipaddr(encode_value(iface)) netmask = ethtool.get_netmask(encode_value(iface)) except IOError: pass kernel_module = get_interface_kernel_module(iface) iface_type = get_interface_type(iface) nic_type = 'N/A' if iface_type is not 'nic' \ else get_nic_type(iface, kernel_module) return {'device': iface, 'name': iface, 'type': iface_type, 'status': operstate(iface), 'link_detected': link_detected(iface), 'ipaddr': ipaddr, 'netmask': netmask, 'macaddr': macaddr(iface), 'module': kernel_module, 'nic_type': nic_type}
def _get_resources(self, flag_filter): try: get_list = getattr(self.model, model_fn(self, 'get_list')) idents = get_list(*self.model_args, **flag_filter) res_list = [] for ident in idents: # internal text, get_list changes ident to unicode for sorted args = self.resource_args + [ident] res = self.resource(self.model, *args) try: res.lookup() except Exception as e: # In case of errors when fetching a resource info, pass and # log the error, so, other resources are returned # Encoding error message as ident is also encoded value. # This has to be done to avoid unicode error, # as combination of encoded and unicode value results into # unicode error. wok_log.error("Problem in lookup of resource '%s'. " "Detail: %s" % (ident, encode_value(e.message))) continue res_list.append(res) return res_list except AttributeError: return []
def get_routes_ipformat(self, routecfg_path): """ Constructs a list of route map with key,value information for the below format of routes. :param: route file which has information in ip format :return: list of dictionaries of the key,value information of the routes. Ex: 10.10.10.0/24 via 192.168.0.10 dev eth0 172.16.1.10/32 via 192.168.0.10 dev eth0 """ with open(routecfg_path, "r") as routecfg_file: line = routecfg_file.read() cfg_map = [] try: each_route = [x for x in (y.split() for y in line.split('\n')) if x] for options in each_route: if len(options) >= 3: route_info = {'ADDRESS': options[0].split('/')[0], 'NETMASK': options[0].split('/')[1], 'GATEWAY': options[2]} if metric in options and options[3] == metric: route_info['METRIC'] = options[4] elif metric in options and options[5] == metric: route_info['METRIC'] = options[6] cfg_map.append(route_info) else: wok_log.warn("Skipping the invalid route information" + encode_value(options)) except Exception, e: raise OperationFailed("GINNET0030E", {'err': e.message}) return cfg_map
def _get_resources(self, flag_filter): try: get_list = getattr(self.model, model_fn(self, 'get_list')) idents = get_list(*self.model_args, **flag_filter) res_list = [] for ident in idents: # internal text, get_list changes ident to unicode for sorted args = self.resource_args + [ident] res = self.resource(self.model, *args) try: res.lookup() except Exception as e: # In case of errors when fetching a resource info, pass and # log the error, so, other resources are returned # log the error, so, other resources are returned. # Encoding error message as ident is also encoded value. # This has to be done to avoid unicode error, # as combination of encoded and unicode value results into # unicode error. wok_log.error("Problem in lookup of resource '%s'. " "Detail: %s" % (ident, encode_value(e.message))) continue res_list.append(res) return res_list except AttributeError: return []
def get_routes_directiveformat(self, routecfg_path): """ This method reads from routes file and contructs key,value information. for the below format. :param :route file path which has info in network directives format :return: dictionary consisting of route information read from file Ex: ADDRESS0=10.10.10.13 NETMASK0=255.255.255.254 GATEWAY0=10.10.10.15 METRIC0=1 """ with open(routecfg_path, "r") as routecfg_file: line = routecfg_file.read() cfgroutes_info = {} route_input = line.split() for elem in route_input: try: cfgroutes_info[elem.split('=')[0]] = elem.split('=')[1] except Exception, e: wok_log.error( 'Exception occured while reading the route ' 'information' + encode_value(e.message)) raise OperationFailed("GINNET0030E", {'Error': e.message})
def is_nic(iface): """Checks if iface is a nic. Args: iface (str): name of the interface. Returns: bool: True if iface is a nic, False otherwise. """ return encode_value(iface) in map(encode_value, nics())
def is_wlan(iface): """Checks if iface is a wlan. Args: iface (str): interface to be checked. Returns: bool: True if iface is a wlan, False otherwise. """ return encode_value(iface) in map(encode_value, wlans())
def is_bridge(iface): """Checks if iface is a bridge. Args: iface (str): name of the interface. Returns: bool: True if iface is a bridge, False otherwise. """ return encode_value(iface) in map(encode_value, bridges())
def is_vlan(iface): """Checks if iface is a vlan. Args: iface (str): name of the interface. Returns: bool: True if iface is a vlan, False otherwise. """ return encode_value(iface) in map(encode_value, vlans())
def _is_dev_leaf(devNodePath, name=None, devs=None): try: if devs: for dev in devs: if encode_value(name) == dev["pkname"]: return True return False # By default, lsblk prints a device information followed by children # device information childrenCount = len(_get_lsblk_devs(["NAME"], [devNodePath])) - 1 except OperationFailed as e: # lsblk is known to fail on multipath devices # Assume these devices contain children wok_log.error("Error getting device info for %s: %s", devNodePath, e) return False return childrenCount == 0
def validate_bond_for_vlan(self, parent_iface): """ method to validate in the case of VLANs over bonds, it is important that the bond has slaves and that they are up, and vlan can not be configured over bond with the fail_over_mac=follow option. :param parent_iface: """ if encode_value(parent_iface) in ethtool.get_devices(): try: with open(FAIL_OVER_MAC % parent_iface) as dev_file: fail_over_mac = dev_file.readline().strip() except IOError: fail_over_mac = "n/a" if fail_over_mac == "follow 2": raise OperationFailed("GINNET0046E") else: """TODO: Need an investigation on, if parent of type bond is not active whether we can still create vlan interface or not. If 'yes', then can include code to validate the fail_over_mac in ifcfg persistant file""" wok_log.error("Parent interface of type 'Bond' is not active") raise OperationFailed("GINNET0051E") cfgdata = CfginterfaceModel().get_cfginterface_info(parent_iface) cfgInterfacesHelper.validate_dict_bond_for_vlan(cfgdata) slave_list = cfgdata[BASIC_INFO][BONDINFO][SLAVES] if len(slave_list) != 0: active_slave_found = True for slave in slave_list: # Fix ginger issue #144 if netinfo.operstate(slave) == STATE_DOWN: active_slave_found = False else: active_slave_found = True wok_log.info("One active slave is found:" + slave) break if (not active_slave_found): raise OperationFailed("GINNET0047E") else: wok_log.error("Minimum one slave has to be given for the bond") raise OperationFailed("GINNET0037E") return