예제 #1
0
파일: api.py 프로젝트: blamarvt/melange
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)
예제 #2
0
파일: api.py 프로젝트: blamarvt/melange
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, []
예제 #3
0
파일: models.py 프로젝트: klmitch/melange
 def create(cls, **values):
     if 'id' not in values or values['id'] is None:
         values['id'] = utils.generate_uuid()
     values['created_at'] = utils.utcnow()
     instance = cls(**values).save()
     instance._notify_fields("create")
     return instance
예제 #4
0
 def save(self):
     if not self.is_valid():
         raise InvalidModelError(self.errors)
     self._convert_columns_to_proper_type()
     self._before_save()
     self['updated_at'] = utils.utcnow()
     return db.db_api.save(self)
예제 #5
0
파일: models.py 프로젝트: roaet/melange
 def save(self):
     if not self.is_valid():
         raise InvalidModelError(self.errors)
     self._convert_columns_to_proper_type()
     self._before_save()
     self['updated_at'] = utils.utcnow()
     LOG.debug("Saving %s: %s" % (self.__class__.__name__, self.__dict__))
     return db.db_api.save(self)
예제 #6
0
파일: models.py 프로젝트: roaet/melange
def deallocated_by_date():
    days = config.Config.get('keep_deallocated_ips_for_days')
    if days is None:
        seconds = config.Config.get('keep_deallocated_ips_for_seconds', 172800)
    else:
        seconds = int(days) * 86400
    LOG.debug("Delete delay = ", seconds)
    return utils.utcnow() - datetime.timedelta(seconds=int(seconds))
예제 #7
0
 def save(self):
     if not self.is_valid():
         raise InvalidModelError(self.errors)
     self._convert_columns_to_proper_type()
     self._before_save()
     self['updated_at'] = utils.utcnow()
     LOG.debug("Saving %s: %s" % (self.__class__.__name__, self.__dict__))
     return db.db_api.save(self)
예제 #8
0
 def _generate_message(self, event_type, priority, payload):
     return {
         "message_id": str(utils.generate_uuid()),
         "publisher_id": socket.gethostname(),
         "event_type": event_type,
         "priority": priority,
         "payload": payload,
         "timestamp": str(utils.utcnow()),
     }
예제 #9
0
 def _generate_message(self, event_type, priority, payload):
     return {
         "message_id": str(utils.generate_uuid()),
         "publisher_id": socket.gethostname(),
         "event_type": event_type,
         "priority": priority,
         "payload": payload,
         "timestamp": str(utils.utcnow()),
     }
예제 #10
0
 def _setup_expected_message(self, priority, event,
                             message):
     self.setup_uuid_with("test_uuid")
     return {'event_type': event,
             'timestamp': str(utils.utcnow()),
             'priority': priority,
             'message_id': "test_uuid",
             'payload': message,
             'publisher_id': socket.gethostname(),
             }
예제 #11
0
 def _setup_expected_message(self, priority, event, message):
     self.setup_uuid_with("test_uuid")
     return {
         'event_type': event,
         'timestamp': str(utils.utcnow()),
         'priority': priority,
         'message_id': "test_uuid",
         'payload': message,
         'publisher_id': socket.gethostname(),
     }
예제 #12
0
파일: models.py 프로젝트: roaet/melange
 def deallocate(self):
     LOG.debug("Marking IP address for deallocation: %r" % self)
     self.update(marked_for_deallocation=True,
                 deallocated_at=utils.utcnow(),
                 interface_id=None)
예제 #13
0
 def deallocate(self):
     LOG.debug("Marking IP address for deallocation: %r" % self)
     self.update(marked_for_deallocation=True,
                 deallocated_at=utils.utcnow(),
                 interface_id=None)
예제 #14
0
 def create(cls, **values):
     values['id'] = utils.generate_uuid()
     values['created_at'] = utils.utcnow()
     instance = cls(**values).save()
     instance._notify_fields("create")
     return instance
예제 #15
0
def deallocated_by_date():
    days = config.Config.get('keep_deallocated_ips_for_days', 2)
    return utils.utcnow() - datetime.timedelta(days=int(days))
예제 #16
0
 def deallocate(self):
     self.update(marked_for_deallocation=True,
                 deallocated_at=utils.utcnow(),
                 interface_id=None)
예제 #17
0
파일: api.py 프로젝트: blamarvt/melange
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