Esempio n. 1
0
def _get_node_id(node, handler, node_map):
    candidates = set()
    for port in node.get('ports', []):
        try:
            candidates.add(node_map['mac'][port['address'].lower()])
        except KeyError:
            pass

    unique_id = handler.unique_id_from_fields(node)
    if unique_id:
        try:
            candidates.add(node_map['pm_addr'][unique_id])
        except KeyError:
            pass

    uuid = node.get('uuid')
    if uuid and uuid in node_map['uuids']:
        candidates.add(uuid)

    if len(candidates) > 1:
        raise exception.InvalidNode('Several candidates found for the same '
                                    'node data: %s' % candidates,
                                    node=node)
    if candidates:
        return list(candidates)[0]
Esempio n. 2
0
def _get_node_id(node, handler, node_map):
    candidates = set()
    for port in node.get('ports', []):
        try:
            candidates.add(node_map['mac'][port['address'].lower()])
        except AttributeError as e:
            raise SystemExit(
                "Node data has an unexpected value for the mac or port"
                " address, or is missing. If the mac and port address"
                " is defined, make sure it is approriately quoted."
                " Error {} -- node {}".format(str(e), node))
        except KeyError:
            pass

    unique_id = handler.unique_id_from_fields(node)
    if unique_id:
        try:
            candidates.add(node_map['pm_addr'][unique_id])
        except KeyError:
            pass

    uuid = node.get('uuid')
    if uuid and uuid in node_map['uuids']:
        candidates.add(uuid)

    if len(candidates) > 1:
        raise exception.InvalidNode('Several candidates found for the same '
                                    'node data: %s' % candidates,
                                    node=node)
    if candidates:
        return list(candidates)[0]
Esempio n. 3
0
def _find_node_handler(fields):
    try:
        driver = fields['pm_type']
    except KeyError:
        raise exception.InvalidNode('pm_type (ironic driver to use) is '
                                    'required', node=fields)
    return _find_driver_handler(driver)
Esempio n. 4
0
def find_driver_handler(driver):
    for driver_tpl, handler in DRIVER_INFO.items():
        if re.search(driver_tpl, driver) is not None:
            return handler

    # FIXME(dtantsur): handle all drivers without hardcoding them
    raise exception.InvalidNode('unknown pm_type (ironic driver to use): '
                                '%s' % driver)
Esempio n. 5
0
    def validate(self, node):
        """Validate node record supplied by a user.

        :param node: node record before convert()
        :raises: exception.InvalidNode
        """
        missing = []
        for field in self._mandatory_fields:
            if not node.get(field):
                missing.append(field)

        if missing:
            raise exception.InvalidNode(
                'The following fields are missing: %s' % ', '.join(missing))
Esempio n. 6
0
def validate_nodes(nodes_list):
    """Validate all nodes list.

    :param nodes_list: The list of nodes to register.
    :raises: InvalidNode on one or more invalid nodes
    """
    failures = []
    unique_ids = set()
    names = set()
    macs = set()
    for index, node in enumerate(nodes_list):
        # Remove any comment
        node.pop("_comment", None)

        handler = _find_node_handler(node)

        try:
            handler.validate(node)
        except exception.InvalidNode as exc:
            failures.append((index, exc))

        for port in node.get('ports', ()):
            if not netutils.is_valid_mac(port['address']):
                failures.append(
                    (index, 'MAC address %s is invalid' % port['address']))

            if port['address'] in macs:
                failures.append(
                    (index, 'MAC %s is not unique' % port['address']))
            else:
                macs.add(port['address'])

        unique_id = handler.unique_id_from_fields(node)
        if unique_id:
            if unique_id in unique_ids:
                failures.append(
                    (index,
                     "Node identified by %s is already present" % unique_id))
            else:
                unique_ids.add(unique_id)

        if node.get('name'):
            if node['name'] in names:
                failures.append(
                    (index, 'Name "%s" is not unique' % node['name']))
            else:
                names.add(node['name'])

        if node.get('platform') and not node.get('arch'):
            failures.append(
                (index,
                 'You have specified a platform without an architecture'))

        try:
            capabilities_to_dict(node.get('capabilities'))
        except (ValueError, TypeError):
            failures.append(
                (index, 'Invalid capabilities: %s' % node.get('capabilities')))

        if node.get('root_device') is not None:
            if not isinstance(node['root_device'], dict):
                failures.append(
                    (index, 'Invalid root device: expected dict, got %s' %
                     node['root_device']))

        for field in node:
            converted = handler.convert_key(field)
            if (converted is None and field not in _NON_DRIVER_FIELDS
                    and field not in _SPECIAL_NON_DRIVER_FIELDS):
                failures.append((index, 'Unknown field %s' % field))

    if failures:
        raise exception.InvalidNode('\n'.join('node #%d: %s' % tpl
                                              for tpl in failures))
Esempio n. 7
0
 def test_wait_for_provision_state_not_found(self):
     baremetal_client = mock.Mock()
     baremetal_client.node.get.side_effect = exception.InvalidNode("boom")
     self.assertRaises(exception.InvalidNode,
                       nodes.wait_for_provision_state, baremetal_client,
                       'UUID', "enroll")
Esempio n. 8
0
def validate_nodes(nodes_list):
    """Validate all nodes list.

    :param nodes_list: The list of nodes to register.
    :raises: InvalidNode on one or more invalid nodes
    """
    failures = []
    unique_ids = set()
    names = set()
    macs = set()
    for index, node in enumerate(nodes_list):
        # Remove any comment
        node.pop("_comment", None)

        handler = _find_node_handler(node)

        try:
            handler.validate(node)
        except exception.InvalidNode as exc:
            failures.append((index, exc))

        for mac in node.get('mac', ()):
            if not netutils.is_valid_mac(mac):
                failures.append((index, 'MAC address %s is invalid' % mac))

            if mac in macs:
                failures.append((index, 'MAC %s is not unique' % mac))
            else:
                macs.add(mac)

        unique_id = handler.unique_id_from_fields(node)
        if unique_id:
            if unique_id in unique_ids:
                failures.append(
                    (index,
                     "Node identified by %s is already present" % unique_id))
            else:
                unique_ids.add(unique_id)

        if node.get('name'):
            if node['name'] in names:
                failures.append(
                    (index, 'Name "%s" is not unique' % node['name']))
            else:
                names.add(node['name'])

        try:
            capabilities_to_dict(node.get('capabilities'))
        except (ValueError, TypeError):
            failures.append(
                (index, 'Invalid capabilities: %s' % node.get('capabilities')))

        for field in node:
            converted = handler.convert_key(field)
            if (converted is None and field not in _NON_DRIVER_FIELDS
                    and field not in ('mac', 'pm_type')):
                failures.append((index, 'Unknown field %s' % field))

    if failures:
        raise exception.InvalidNode('\n'.join('node #%d: %s' % tpl
                                              for tpl in failures))
Esempio n. 9
0
 def validate(self, node):
     super(SshDriverInfo, self).validate(node)
     if not node.get('mac'):
         raise exception.InvalidNode(
             'Nodes with SSH drivers require at least one MAC')