Exemplo n.º 1
0
def delete_interface(interface):
    db_session = session.get_session()
    with db_session.begin():
        mac_qry = _query_by(ipam.models.MacAddress, db_session=db_session,
                            interface_id=interface.id)

        mac = mac_qry.with_lockmode('update').first()

        if mac:
            db_session.delete(mac)

        ips_qry = _query_by(ipam.models.IpAddress, db_session=db_session,
                            interface_id=interface.id)
        ips = ips_qry.with_lockmode('update').all()

        for ip in ips:
            LOG.debug("Marking IP address for deallocation: %r" % ip)
            ip.allocated = False
            ip.marked_for_deallocation = True
            ip.deallocated_at = utils.utcnow()
            ip.interface_id = None
            db_session.merge(ip)

        db_session.delete(interface)

        return mac, ips

    # NOTE(jkoelker) Failsafe return:
    return None, []
Exemplo n.º 2
0
def save(model):
    try:
        db_session = session.get_session()
        model = db_session.merge(model)
        db_session.flush()
        return model
    except sqlalchemy.exc.IntegrityError as error:
        raise exception.DBConstraintError(model_name=model.__class__.__name__,
                                          error=str(error.orig))
Exemplo n.º 3
0
def upgrade(migrate_engine):
    _db_connect()

    interface = session.get_session().execute(
        "SELECT COUNT(1) as count FROM interfaces "
        "WHERE device_id NOT REGEXP '.*-.*' AND device_id IS NOT NULL")
    print(interface)
    if interface.fetchone().count > 0:
        print """
Exemplo n.º 4
0
def upgrade(migrate_engine):
    _db_connect()

    interface = session.get_session().execute(
        "SELECT COUNT(1) as count FROM interfaces "
        "WHERE device_id NOT REGEXP '.*-.*' AND device_id IS NOT NULL")
    print(interface)
    if interface.fetchone().count > 0:
        print """
Exemplo n.º 5
0
Arquivo: api.py Projeto: roaet/melange
def save(model):
    try:
        db_session = session.get_session()
        model = db_session.merge(model)
        db_session.flush()
        return model
    except sqlalchemy.exc.IntegrityError as error:
        raise exception.DBConstraintError(model_name=model.__class__.__name__,
                                          error=str(error.orig))
Exemplo n.º 6
0
Arquivo: api.py Projeto: j2sol/melange
def pop_allocatable_address(address_model, **conditions):
    db_session = session.get_session()
    with db_session.begin():
        address_rec = _query_by(address_model, db_session=db_session, **conditions).with_lockmode("update").first()
        if not address_rec:
            return None

        delete(address_rec, db_session=db_session)
        return address_rec.address
Exemplo n.º 7
0
def pop_allocatable_address(address_model, **conditions):
    db_session = session.get_session()
    with db_session.begin():
        address_rec = _query_by(address_model, db_session=db_session,
                                **conditions).\
                         with_lockmode('update').first()
        if not address_rec:
            return None

        delete(address_rec, db_session=db_session)
        return address_rec.address
Exemplo n.º 8
0
def _allocate_allocatable_address(ip_block, interface,
                                  requested_address=None):
    """Slowly migrate off the AllocatableIps Table."""
    model = ipam.models.IpAddress

    db_session = session.get_session()
    db_session.begin()

    allocatable_qry = _query_by(ipam.models.AllocatableIp,
                                db_session=db_session,
                                ip_block_id=ip_block.id)
    if requested_address is not None:
        filter_kwargs = {'address': requested_address}
        allocatable_qry = allocatable_qry.filter(**filter_kwargs)

    allocatable_address = allocatable_qry.first()

    if not allocatable_address:
        db_session.commit()
        return

    ip = allocatable_address.address
    address = model(id=utils.generate_uuid(),
                    created_at=utils.utcnow(),
                    updated_at=utils.utcnow(),
                    address=str(ip),
                    ip_block_id=ip_block.id,
                    interface_id=interface.id,
                    used_by_tenant_id=interface.tenant_id,
                    allocated=True)
    db_session.merge(address)

    try:
        db_session.flush()
    except sqlalchemy.exc.IntegrityError:
        db_session.rollback()
        db_session.begin()
        db_session.delete(allocatable_address)
        db_session.commit()
        LOG.debug("Allocatable ip %s in block %s was a dupe. Deleted" %
                  (ip, ip_block.id))
        return _allocate_allocatable_address(ip_block, interface,
                                             requested_address)

    db_session.delete(allocatable_address)
    db_session.commit()
    return address

    db_session.delete(allocatable_qry.address)
Exemplo n.º 9
0
Arquivo: api.py Projeto: roaet/melange
def _base_query(cls):
    return session.get_session().query(cls)
Exemplo n.º 10
0
def delete(model):
    db_session = session.get_session()
    model = db_session.merge(model)
    db_session.delete(model)
    db_session.flush()
Exemplo n.º 11
0
def delete(model, db_session=None):
    db_session = db_session or session.get_session()
    model = db_session.merge(model)
    db_session.delete(model)
    db_session.flush()
Exemplo n.º 12
0
def _base_query(cls):
    return session.get_session().query(cls)
Exemplo n.º 13
0
def delete(model):
    db_session = session.get_session()
    model = db_session.merge(model)
    db_session.delete(model)
    db_session.flush()
Exemplo n.º 14
0
    from melange.common import config
    from melange.db import db_api
    from melange.ipam import models
    from melange.db.sqlalchemy import session
    from melange.openstack.common import config as openstack_config

    oparser = optparse.OptionParser()
    openstack_config.add_common_options(oparser)
    openstack_config.add_log_options(oparser)
    (options, args) = openstack_config.parse_options(oparser)

    if len(args) < 1:
        sys.exit("Please include the connection string for the nova DB")

    try:
        conf = config.load_app_environment(optparse.OptionParser())
        db_api.configure_db(conf, ipv4.plugin(), mac.plugin())
        nova_engine = session._create_engine({'sql_connection': args[0]})
        instances = nova_engine.execute("select id,uuid from instances")
        melange_session = session.get_session()

        print "-----"
        for instance in instances:
            print "updating %(id)s with %(uuid)s" % instance
            session._ENGINE.execute("update interfaces set "
                                    "device_id='%(uuid)s' "
                                    "where device_id=%(id)s" % instance)

    except RuntimeError as error:
        sys.exit("ERROR: %s" % error)
Exemplo n.º 15
0
    from melange.common import config
    from melange.db import db_api
    from melange.ipam import models
    from melange.db.sqlalchemy import session
    from melange.openstack.common import config as openstack_config

    oparser = optparse.OptionParser()
    openstack_config.add_common_options(oparser)
    openstack_config.add_log_options(oparser)
    (options, args) = openstack_config.parse_options(oparser)

    if len(args) < 1:
        sys.exit("Please include the connection string for the nova DB")

    try:
        conf = config.load_app_environment(optparse.OptionParser())
        db_api.configure_db(conf, ipv4.plugin(), mac.plugin())
        nova_engine = session._create_engine({'sql_connection': args[0]})
        instances = nova_engine.execute("select id,uuid from instances")
        melange_session = session.get_session()

        print "-----"
        for instance in instances:
            print "updating %(id)s with %(uuid)s" % instance
            session._ENGINE.execute("update interfaces set "
                                    "device_id='%(uuid)s' "
                                    "where device_id=%(id)s" % instance)

    except RuntimeError as error:
        sys.exit("ERROR: %s" % error)
Exemplo n.º 16
0
Arquivo: api.py Projeto: roaet/melange
def delete(model, db_session=None):
    db_session = db_session or session.get_session()
    model = db_session.merge(model)
    db_session.delete(model)
    db_session.flush()
Exemplo n.º 17
0
def allocate_ipv4_address(ip_block, interface, requested_address=None):
    model = ipam.models.IpAddress

    address = _allocate_allocatable_address(ip_block, interface,
                                            requested_address)

    if address:
        return address

    db_session = session.get_session()
    with db_session.begin():
        address_qry = _query_by(model, db_session=db_session,
                                allocated=False,
                                marked_for_deallocation=False,
                                ip_block_id=ip_block.id)

        if requested_address is not None:
            address_qry = address_qry.filter(address=requested_address)

        address = address_qry.with_lockmode('update').first()

        if address:
            address.allocated = True
            address.interface_id = interface.id
            address.used_by_tenant_id = interface.tenant_id
            address.updated_at = utils.utcnow()
            db_session.merge(address)
            return address

        else:
            ips = netaddr.IPNetwork(ip_block.cidr)
            counter = (ip_block.allocatable_ip_counter or ips[0].value)

            if counter >= ips[-1].value:
                ip_block.is_full = True
                # NOTE(jkoelker) explicit save() to flush the session prior
                #                to raising
                save(ip_block, db_session=db_session)
                raise exception.NoMoreAddressesError(_("IpBlock is full"))

            ip = netaddr.IPAddress(counter)

            # NOTE(jkoelker) HRM, this may need to be rethought order wise
            counter = counter + 1
            if counter >= ips[-1].value:
                ip_block.is_full = True

            ip_block.allocatable_ip_counter = counter
            db_session.merge(ip_block)

        # NOTE(jkoelker) SQLAlchemy models, how do you work? ;)
        address = model(id=utils.generate_uuid(),
                        created_at=utils.utcnow(),
                        updated_at=utils.utcnow(),
                        address=str(ip),
                        ip_block_id=ip_block.id,
                        interface_id=interface.id,
                        used_by_tenant_id=interface.tenant_id,
                        allocated=True)
        db_session.merge(address)

        return address