Exemplo n.º 1
0
def bm_interface_destroy(context, if_id, session=None):
    # Delete physically since it has unique colmuns
    if not session:
        session = get_session()
    with session.begin():
        vif_ref = bm_interface_get(context, if_id, session)
        session.delete(vif_ref)
Exemplo n.º 2
0
def bm_node_create(context, values, session=None):
    if not session:
        session = get_session()
    bm_node_ref = models.BareMetalNode()
    bm_node_ref.update(values)
    bm_node_ref.save(session=session)
    return bm_node_ref
Exemplo n.º 3
0
def bm_pxe_ip_destroy(context, ip_id, session=None):
    # Delete physically since it has unique columns
    if not session:
        session = get_session()
    with session.begin():
        ip_ref = bm_pxe_ip_get(context, ip_id, session)
        session.delete(ip_ref)
Exemplo n.º 4
0
def bm_pxe_ip_associate(context, bm_node_id):
    session = get_session()
    with session.begin():
        # Check if the node really exists
        node_ref = model_query(context, models.BareMetalNode,
                               read_deleted="no", session=session).\
                     filter_by(id=bm_node_id).\
                     first()
        if not node_ref:
            raise exception.NovaException("bm_node %s not found" % bm_node_id)
        # Check if the node already has a pxe_ip
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=bm_node_id).\
                         first()
        if ip_ref:
            return ip_ref.id
        # with_lockmode('update') and filter_by(bm_node_id=None) will lock all
        # records. It may cause a performance problem in high-concurrency
        # environment.
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=None).\
                         with_lockmode('update').\
                         first()
        if not ip_ref:
            raise exception.NovaException("free bm_pxe_ip not found")
        ip_ref.bm_node_id = bm_node_id
        session.add(ip_ref)
        return ip_ref.id
Exemplo n.º 5
0
def bm_pxe_ip_associate(context, bm_node_id):
    session = get_session()
    with session.begin():
        # Check if the node really exists
        node_ref = model_query(context, models.BareMetalNode,
                               read_deleted="no", session=session).\
                     filter_by(id=bm_node_id).\
                     first()
        if not node_ref:
            raise exception.InstanceNotFound(instance_id=bm_node_id)

        # Check if the node already has a pxe_ip
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=bm_node_id).\
                         first()
        if ip_ref:
            return ip_ref.id

        # with_lockmode('update') and filter_by(bm_node_id=None) will lock all
        # records. It may cause a performance problem in high-concurrency
        # environment.
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=None).\
                         with_lockmode('update').\
                         first()

        # this exception is not caught in nova/compute/manager
        if not ip_ref:
            raise exception.NovaException(_("No more PXE IPs available"))

        ip_ref.bm_node_id = bm_node_id
        session.add(ip_ref)
        return ip_ref.id
Exemplo n.º 6
0
def bm_pxe_ip_destroy(context, ip_id, session=None):
    # Delete physically since it has unique columns
    if not session:
        session = get_session()
    with session.begin():
        ip_ref = bm_pxe_ip_get(context, ip_id, session)
        session.delete(ip_ref)
Exemplo n.º 7
0
def bm_interface_set_vif_uuid(context, if_id, vif_uuid):
    session = db_session.get_session()
    with session.begin():
        bm_interface = model_query(context, models.BareMetalInterface,
                                read_deleted="no", session=session).\
                         filter_by(id=if_id).\
                         with_lockmode('update').\
                         first()
        if not bm_interface:
            raise exception.NovaException(
                _("Baremetal interface %s "
                  "not found") % if_id)

        bm_interface.vif_uuid = vif_uuid
        try:
            session.add(bm_interface)
            session.flush()
        except exception.DBError, e:
            # TODO(deva): clean up when db layer raises DuplicateKeyError
            if str(e).find('IntegrityError') != -1:
                raise exception.NovaException(
                    _("Baremetal interface %s "
                      "already in use") % vif_uuid)
            else:
                raise e
Exemplo n.º 8
0
def bm_node_associate_and_update(context, node_uuid, values):
    """Associate an instance to a node safely

    Associate an instance to a node only if that node is not yet assocated.
    Allow the caller to set any other fields they require in the same
    operation. For example, this is used to set the node's task_state to
    BUILDING at the beginning of driver.spawn().

    """
    if 'instance_uuid' not in values:
        raise exception.NovaException(_(
            "instance_uuid must be supplied to bm_node_associate_and_update"))

    session = db_session.get_session()
    with session.begin():
        query = model_query(context, models.BareMetalNode,
                                session=session, read_deleted="no").\
                        filter_by(uuid=node_uuid)

        count = query.filter_by(instance_uuid=None).\
                        update(values, synchronize_session=False)
        if count != 1:
            raise exception.NovaException(_(
                "Failed to associate instance %(i_uuid)s to baremetal node "
                "%(n_uuid)s.") % {'i_uuid': values['instance_uuid'],
                                  'n_uuid': node_uuid})
        ref = query.first()
    return ref
Exemplo n.º 9
0
def model_query(context, *args, **kwargs):
    """Query helper that accounts for context's `read_deleted` field.

    :param context: context to query under
    :param session: if present, the session to use
    :param read_deleted: if present, overrides context's read_deleted field.
    :param project_only: if present and context is user-type, then restrict
            query to match the context's project_id.
    """
    session = kwargs.get('session') or db_session.get_session()
    read_deleted = kwargs.get('read_deleted') or context.read_deleted
    project_only = kwargs.get('project_only')

    query = session.query(*args)

    if read_deleted == 'no':
        query = query.filter_by(deleted=False)
    elif read_deleted == 'yes':
        pass  # omit the filter to include deleted and active
    elif read_deleted == 'only':
        query = query.filter_by(deleted=True)
    else:
        raise Exception(
                _("Unrecognized read_deleted value '%s'") % read_deleted)

    if project_only and nova.context.is_user_context(context):
        query = query.filter_by(project_id=context.project_id)

    return query
Exemplo n.º 10
0
def bm_interface_destroy(context, if_id, session=None):
    # Delete physically since it has unique colmuns
    if not session:
        session = get_session()
    with session.begin():
        vif_ref = bm_interface_get(context, if_id, session)
        session.delete(vif_ref)
Exemplo n.º 11
0
def bm_node_create(context, values, session=None):
    if not session:
        session = get_session()
    bm_node_ref = models.BareMetalNode()
    bm_node_ref.update(values)
    bm_node_ref.save(session=session)
    return bm_node_ref
Exemplo n.º 12
0
def bm_pxe_ip_associate(context, bm_node_id):
    session = get_session()
    with session.begin():
        # Check if the node really exists
        node_ref = model_query(context, models.BareMetalNode,
                               read_deleted="no", session=session).\
                     filter_by(id=bm_node_id).\
                     first()
        if not node_ref:
            raise exception.NovaException("bm_node %s not found" % bm_node_id)
        # Check if the node already has a pxe_ip
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=bm_node_id).\
                         first()
        if ip_ref:
            return ip_ref.id
        # with_lockmode('update') and filter_by(bm_node_id=None) will lock all
        # records. It may cause a performance problem in high-concurrency
        # environment.
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(bm_node_id=None).\
                         with_lockmode('update').\
                         first()
        if not ip_ref:
            raise exception.NovaException("free bm_pxe_ip not found")
        ip_ref.bm_node_id = bm_node_id
        session.add(ip_ref)
        return ip_ref.id
Exemplo n.º 13
0
def bm_pxe_ip_disassociate(context, bm_node_id, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ip = bm_pxe_ip_get_by_bm_node_id(context, bm_node_id, session=session)
        if ip:
            ip.bm_node_id = None
            ip.save(session=session)
Exemplo n.º 14
0
def bm_pxe_ip_disassociate(context, bm_node_id, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ip = bm_pxe_ip_get_by_bm_node_id(context, bm_node_id, session=session)
        if ip:
            ip.bm_node_id = None
            ip.save(session=session)
Exemplo n.º 15
0
def bm_pxe_ip_create(context, address, server_address, session=None):
    if not session:
        session = get_session()
    ref = models.BareMetalPxeIp()
    ref.address = address
    ref.server_address = server_address
    ref.save(session=session)
    return ref
Exemplo n.º 16
0
def bm_pxe_ip_create(context, address, server_address, session=None):
    if not session:
        session = get_session()
    ref = models.BareMetalPxeIp()
    ref.address = address
    ref.server_address = server_address
    ref.save(session=session)
    return ref
Exemplo n.º 17
0
def bm_interface_create(context, bm_node_id, address, datapath_id, port_no,
                        session=None):
    if not session:
        session = get_session()
    ref = models.BareMetalInterface()
    ref.bm_node_id = bm_node_id
    ref.address = address
    ref.datapath_id = datapath_id
    ref.port_no = port_no
    ref.save(session=session)
    return ref.id
Exemplo n.º 18
0
def bm_deployment_create(context, key, image_path, pxe_config_path, root_mb,
                         swap_mb, session=None):
    if not session:
        session = get_session()
    ref = models.BareMetalDeployment()
    ref.key = key
    ref.image_path = image_path
    ref.pxe_config_path = pxe_config_path
    ref.root_mb = root_mb
    ref.swap_mb = swap_mb
    ref.save(session=session)
    return ref.id
Exemplo n.º 19
0
def bm_interface_set_vif_uuid(context, if_id, vif_uuid, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = model_query(context, models.BareMetalInterface,
                          read_deleted="no", session=session).\
                         filter_by(id=if_id).\
                         with_lockmode('update').\
                         first()
        if not ref:
            raise exception.NovaException()
        ref.vif_uuid = vif_uuid
        session.add(ref)
Exemplo n.º 20
0
def bm_interface_set_vif_uuid(context, if_id, vif_uuid, session=None):
    if not session:
        session = get_session()
    with session.begin():
        ref = model_query(context, models.BareMetalInterface,
                          read_deleted="no", session=session).\
                         filter_by(id=if_id).\
                         with_lockmode('update').\
                         first()
        if not ref:
            raise exception.NovaException()
        ref.vif_uuid = vif_uuid
        session.add(ref)
Exemplo n.º 21
0
Arquivo: api.py Projeto: qwefi/nova
def bm_node_destroy(context, bm_node_id):
    # First, delete all interfaces belonging to the node.
    # Delete physically since these have unique columns.
    session = db_session.get_session()
    with session.begin():
        rows = model_query(context, models.BareMetalNode, read_deleted="no").\
            filter_by(uuid=bm_node_id).\
            update({'deleted': True,
                    'deleted_at': timeutils.utcnow(),
                    'updated_at': literal_column('updated_at')})

        if not rows:
            raise exception.NodeNotFound(node_id=bm_node_id)
Exemplo n.º 22
0
Arquivo: api.py Projeto: StackOps/nova
def bm_node_destroy(context, bm_node_id):
    # First, delete all interfaces belonging to the node.
    # Delete physically since these have unique columns.
    session = db_session.get_session()
    with session.begin():
        model_query(context, models.BareMetalInterface, read_deleted="no").filter_by(bm_node_id=bm_node_id).delete()
        rows = (
            model_query(context, models.BareMetalNode, read_deleted="no")
            .filter_by(id=bm_node_id)
            .update({"deleted": True, "deleted_at": timeutils.utcnow(), "updated_at": literal_column("updated_at")})
        )

        if not rows:
            raise exception.NodeNotFound(node_id=bm_node_id)
Exemplo n.º 23
0
def bm_pxe_ip_destroy_by_address(context, address, session=None):
    # Delete physically since it has unique columns
    if not session:
        session = get_session()
    with session.begin():
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(address=address).\
                         with_lockmode('update').\
                         first()
        if not ip_ref:
            return None
        session.delete(ip_ref)
        return ip_ref
Exemplo n.º 24
0
def bm_pxe_ip_destroy_by_address(context, address, session=None):
    # Delete physically since it has unique columns
    if not session:
        session = get_session()
    with session.begin():
        ip_ref = model_query(context, models.BareMetalPxeIp,
                             read_deleted="no", session=session).\
                         filter_by(address=address).\
                         with_lockmode('update').\
                         first()
        if not ip_ref:
            return None
        session.delete(ip_ref)
        return ip_ref
Exemplo n.º 25
0
def bm_interface_create(context,
                        bm_node_id,
                        address,
                        datapath_id,
                        port_no,
                        session=None):
    if not session:
        session = get_session()
    ref = models.BareMetalInterface()
    ref.bm_node_id = bm_node_id
    ref.address = address
    ref.datapath_id = datapath_id
    ref.port_no = port_no
    ref.save(session=session)
    return ref.id
Exemplo n.º 26
0
def bm_node_destroy(context, bm_node_id):
    # First, delete all interfaces belonging to the node.
    # Delete physically since these have unique columns.
    session = db_session.get_session()
    with session.begin():
        model_query(context, models.BareMetalInterface, read_deleted="no").\
            filter_by(bm_node_id=bm_node_id).\
            delete()
        rows = model_query(context, models.BareMetalNode, read_deleted="no").\
            filter_by(id=bm_node_id).\
            update({'deleted': True,
                    'deleted_at': timeutils.utcnow(),
                    'updated_at': literal_column('updated_at')})

        if not rows:
            raise exception.NodeNotFound(node_id=bm_node_id)
Exemplo n.º 27
0
def bm_deployment_create(context,
                         key,
                         image_path,
                         pxe_config_path,
                         root_mb,
                         swap_mb,
                         session=None):
    if not session:
        session = get_session()
    ref = models.BareMetalDeployment()
    ref.key = key
    ref.image_path = image_path
    ref.pxe_config_path = pxe_config_path
    ref.root_mb = root_mb
    ref.swap_mb = swap_mb
    ref.save(session=session)
    return ref.id
Exemplo n.º 28
0
def bm_interface_set_vif_uuid(context, if_id, vif_uuid):
    session = get_session()
    with session.begin():
        ref = model_query(context, models.BareMetalInterface,
                          read_deleted="no", session=session).\
                         filter_by(id=if_id).\
                         with_lockmode('update').\
                         first()
        if not ref:
            raise exception.NovaException('interface id=%s is not found' %
                                          if_id)
        ref.vif_uuid = vif_uuid
        try:
            session.add(ref)
            session.flush()
        except IntegrityError:
            raise exception.NovaException('vif_uuid %s is already assigned' %
                                          vif_uuid)
Exemplo n.º 29
0
def bm_interface_set_vif_uuid(context, if_id, vif_uuid):
    session = get_session()
    with session.begin():
        ref = model_query(context, models.BareMetalInterface,
                          read_deleted="no", session=session).\
                         filter_by(id=if_id).\
                         with_lockmode('update').\
                         first()
        if not ref:
            raise exception.NovaException('interface id=%s is not found' %
                                          if_id)
        ref.vif_uuid = vif_uuid
        try:
            session.add(ref)
            session.flush()
        except IntegrityError:
            raise exception.NovaException('vif_uuid %s is already assigned' %
                                          vif_uuid)
Exemplo n.º 30
0
def bm_interface_set_vif_uuid(context, if_id, vif_uuid):
    session = db_session.get_session()
    with session.begin():
        bm_interface = (
            model_query(context, models.BareMetalInterface, read_deleted="no", session=session)
            .filter_by(id=if_id)
            .with_lockmode("update")
            .first()
        )
        if not bm_interface:
            raise exception.NovaException(_("Baremetal interface %s " "not found") % if_id)

        bm_interface.vif_uuid = vif_uuid
        try:
            session.add(bm_interface)
            session.flush()
        except db_exc.DBError as e:
            # TODO(deva): clean up when db layer raises DuplicateKeyError
            if str(e).find("IntegrityError") != -1:
                raise exception.NovaException(_("Baremetal interface %s " "already in use") % vif_uuid)
            raise
Exemplo n.º 31
0
def _save(ref, session=None):
    if not session:
        session = db_session.get_session()
    # We must not call ref.save() with session=None, otherwise NovaBase
    # uses nova-db's session, which cannot access bm-db.
    ref.save(session=session)
Exemplo n.º 32
0
def bm_node_update(context, bm_node_id, values, session=None):
    if not session:
        session = get_session()
    bm_node_ref = bm_node_get(context, bm_node_id, session=session)
    bm_node_ref.update(values)
    bm_node_ref.save(session=session)
Exemplo n.º 33
0
def bm_node_update(context, bm_node_id, values, session=None):
    if not session:
        session = get_session()
    bm_node_ref = bm_node_get(context, bm_node_id, session=session)
    bm_node_ref.update(values)
    bm_node_ref.save(session=session)