def test_compare_two_macs(self): equal_macs = ( ("00-1B-77-49-54-FD", "00-1B-77-49-54-FD"), ("00-1B-77-49-54-FD", "00-1b-77-49-54-fd"), ("00-1B-77-49-54-FD", "001b:7749:54fd"), ("00-1B-77-49-54-FD", "1B:7749:54FD"), ("00-1B-77-49-54-FD", "001b774954fd"), ("00-1B-77-49-54-FD", netaddr.EUI("00-1B-77-49-54-FD")), ("aa:bb:cc:dd:ee:0f", "aa:bb:cc:dd:ee:f"), ) for mac1, mac2 in equal_macs: self.assertTrue(utils.is_same_mac(mac1, mac2)) self.assertFalse(utils.is_same_mac("AA-BB-CC-11-22-33", "11:22:33:AA:BB:CC"))
def test_compare_two_macs(self): equal_macs = ( ('00-1B-77-49-54-FD', '00-1B-77-49-54-FD'), ('00-1B-77-49-54-FD', '00-1b-77-49-54-fd'), ('00-1B-77-49-54-FD', '001b:7749:54fd'), ('00-1B-77-49-54-FD', '1B:7749:54FD'), ('00-1B-77-49-54-FD', '001b774954fd'), ('00-1B-77-49-54-FD', netaddr.EUI('00-1B-77-49-54-FD')), ('aa:bb:cc:dd:ee:0f', 'aa:bb:cc:dd:ee:f'), ) for mac1, mac2 in equal_macs: self.assertTrue(utils.is_same_mac(mac1, mac2)) self.assertFalse( utils.is_same_mac('AA-BB-CC-11-22-33', '11:22:33:AA:BB:CC'))
def update_interfaces_info(cls, node, update_by_agent=False): """Update interfaces in case of correct interfaces in meta field in node's model """ try: cls.check_interfaces_correctness(node) except errors.InvalidInterfacesInfo as e: logger.debug("Cannot update interfaces: %s", e.message) return pxe_iface_name = cls._get_pxe_iface_name(node) for interface in node.meta["interfaces"]: # set 'pxe' property for appropriate iface if interface['name'] == pxe_iface_name: interface['pxe'] = True # try to get interface by mac address interface_db = next(( n for n in node.nic_interfaces if utils.is_same_mac(n.mac, interface['mac'])), None) # try to get interface instance by interface name. this protects # us from loosing nodes when some NICs was replaced with a new one interface_db = interface_db or next(( n for n in node.nic_interfaces if n.name == interface['name']), None) if interface_db: cls.__update_existing_interface(interface_db.id, interface, update_by_agent) else: cls.__add_new_interface(node, interface) cls.__delete_not_found_interfaces(node, node.meta["interfaces"])
def update_interfaces_info(cls, node): """Update interfaces in case of correct interfaces in meta field in node's model """ try: cls.check_interfaces_correctness(node) except errors.InvalidInterfacesInfo as e: logger.debug("Cannot update interfaces: %s", e.message) return for interface in node.meta["interfaces"]: # try to get interface by mac address interface_db = next( (n for n in node.nic_interfaces if utils.is_same_mac(n.mac, interface['mac'])), None) # try to get interface instance by interface name. this protects # us from loosing nodes when some NICs was replaced with a new one interface_db = interface_db or next( (n for n in node.nic_interfaces if n.name == interface['name']), None) if interface_db: cls.__update_existing_interface(interface_db.id, interface) else: cls.__add_new_interface(node, interface) cls.__delete_not_found_interfaces(node, node.meta["interfaces"])
def check_dhcp_resp(cls, **kwargs): """Receiver method for check_dhcp task For example of kwargs check FakeCheckingDhcpThread """ logger.info( "RPC method check_dhcp_resp received: %s", jsonutils.dumps(kwargs) ) messages = [] result = collections.defaultdict(list) message_template = ( u"Node {node_name} discovered DHCP server " u"via {iface} with following parameters: IP: {server_id}, " u"MAC: {mac}. This server will conflict with the installation.") task_uuid = kwargs.get('task_uuid') nodes = kwargs.get('nodes', []) error_msg = kwargs.get('error') status = kwargs.get('status') progress = kwargs.get('progress') nodes_uids = [node['uid'] for node in nodes] nodes_db = db().query(Node).filter(Node.id.in_(nodes_uids)).all() nodes_map = dict((str(node.id), node) for node in nodes_db) master_network_mac = settings.ADMIN_NETWORK['mac'] logger.debug('Mac addr on master node %s', master_network_mac) for node in nodes: if node['status'] == 'ready': for row in node.get('data', []): if not net_utils.is_same_mac(row['mac'], master_network_mac): node_db = nodes_map.get(node['uid']) if node_db: row['node_name'] = node_db.name message = message_template.format(**row) messages.append(message) result[node['uid']].append(row) else: logger.warning( 'Received message from nonexistent node. ' 'Message %s', row) status = status if not messages else "error" error_msg = '\n'.join(messages) if messages else error_msg logger.debug('Check dhcp message %s', error_msg) task = objects.Task.get_by_uuid(task_uuid, fail_if_not_found=True) objects.Task.update_verify_networks(task, status, progress, error_msg, result)
def check_interfaces_correctness(cls, node): """Check that * interface list in meta field is not empty * at least one interface has ip which includes to admin subnet. It can happens in case if agent was running, but network interfaces were not configured yet. * there're no networks assigned to removed interface """ if not node.meta: raise errors.InvalidInterfacesInfo( u'Meta field for node "%s" is empty' % node.full_name) if not node.meta.get('interfaces'): raise errors.InvalidInterfacesInfo( u'Cannot find interfaces field "%s" in meta' % node.full_name) interfaces = node.meta['interfaces'] admin_interface = None for interface in interfaces: ip_addr = interface.get('ip') if cls.is_ip_belongs_to_admin_subnet(ip_addr, node.id): # Interface was founded admin_interface = interface break elif utils.is_same_mac(interface['mac'], node.mac): admin_interface = interface break if not admin_interface: raise errors.InvalidInterfacesInfo( u'Cannot find interface with ip which ' 'includes to admin subnet "%s"' % node.full_name) # raise exception if an interface is about to remove, # but has assigned network and it's already deployed interfaces = [i['name'] for i in interfaces] for iface in node.nic_interfaces: if iface.name not in interfaces and iface.assigned_networks_list: raise errors.InvalidInterfacesInfo( u'Could not remove interface "{0}", since it is assigned ' u'to one or more networks'.format(iface.name) )
def test_compare_macs_raise_exception(self): with self.assertRaises(ValueError): utils.is_same_mac('QWERTY', 'ASDF')
def test_compare_macs_raise_exception(self): with self.assertRaises(ValueError): utils.is_same_mac("QWERTY", "ASDF")