def test_get_resource_lock_name(self): resource_type = 'ironic_node' resource_uuid = '12345' self.assertEqual( resource_type + '-' + resource_uuid, utils.get_resource_lock_name(resource_type, resource_uuid))
def create(self, context=None): updates = self.obj_get_changes() with utils.lock(utils.get_resource_lock_name(updates['resource_type'], updates['resource_uuid']), external=True): LOG.info('Creating offer') if updates['start_time'] >= updates['end_time']: raise exception.InvalidTimeRange( resource='offer', start_time=str(updates['start_time']), end_time=str(updates['end_time'])) if updates.get('parent_lease_uuid'): # offer is a child of an existing lease parent_lease = lease_obj.Lease.get( updates['parent_lease_uuid']) if parent_lease.status != statuses.ACTIVE: raise exception.LeaseNotActive( updates['parent_lease_uuid']) parent_lease.verify_child_availability(updates['start_time'], updates['end_time']) else: ro = get_resource_object(updates['resource_type'], updates['resource_uuid']) ro.verify_availability(updates['start_time'], updates['end_time']) db_offer = self.dbapi.offer_create(updates) self._from_db_object(context, self, db_offer)
def expire(self, context=None): LOG.info('Expiring offer %s', self.uuid) leases = lease_obj.Lease.get_all( { 'offer_uuid': self.uuid, 'status': statuses.LEASE_CAN_DELETE }, None) for lease in leases: lease.expire(context) with utils.lock(utils.get_resource_lock_name(self.resource_type, self.resource_uuid), external=True): self.status = statuses.EXPIRED self.save(context)
def cancel(self): LOG.info('Deleting offer %s', self.uuid) leases = lease_obj.Lease.get_all( { 'offer_uuid': self.uuid, 'status': statuses.LEASE_CAN_DELETE }, None) for lease in leases: lease.cancel() with utils.lock(utils.get_resource_lock_name(self.resource_type, self.resource_uuid), external=True): self.status = statuses.DELETED self.save(None)
def fulfill(self, context=None): with utils.lock(utils.get_resource_lock_name(self.resource_type, self.resource_uuid), external=True): LOG.info('Fulfilling lease %s', self.uuid) try: resource = self.resource_object() resource.set_lease(self) # activate lease self.status = statuses.ACTIVE self.fulfill_time = datetime.datetime.now() except Exception as e: LOG.info('Error fulfilling lease: %s: %s' % (type(e).__name__, e)) LOG.info('Setting lease status to WAIT') self.status = statuses.WAIT_FULFILL self.save(context)
def create(self, context=None): updates = self.obj_get_changes() with utils.lock(utils.get_resource_lock_name(updates['resource_type'], updates['resource_uuid']), external=True): if updates['start_time'] >= updates['end_time']: raise exception.InvalidTimeRange( resource='lease', start_time=str(updates['start_time']), end_time=str(updates['end_time'])) # check availability if 'offer_uuid' in updates: # lease is being created from an offer related_offer = offer_obj.Offer.get(updates['offer_uuid']) if related_offer.status != statuses.AVAILABLE: raise exception.OfferNotAvailable( offer_uuid=related_offer.uuid, status=related_offer.status) related_offer.verify_availability(updates['start_time'], updates['end_time']) elif 'parent_lease_uuid' in updates: # lease is a child of an existing lease parent_lease = Lease.get(updates['parent_lease_uuid']) if parent_lease.status != statuses.ACTIVE: raise exception.LeaseNotActive( updates['parent_lease_uuid']) parent_lease.verify_child_availability(updates['start_time'], updates['end_time']) else: ro = get_resource_object(updates['resource_type'], updates['resource_uuid']) ro.verify_availability(updates['start_time'], updates['end_time']) db_lease = self.dbapi.lease_create(updates) self._from_db_object(context, self, db_lease)
def create(self, context=None): updates = self.obj_get_changes() @utils.synchronized(utils.get_resource_lock_name( updates['resource_type'], updates['resource_uuid']), external=True) def _create_offer(): if updates['start_time'] >= updates['end_time']: raise exception.InvalidTimeRange( resource='contract', start_time=str(updates['start_time']), end_time=str(updates['end_time'])) self.dbapi.offer_verify_resource_availability( updates['resource_type'], updates['resource_uuid'], updates['start_time'], updates['end_time']) db_offer = self.dbapi.offer_create(updates) self._from_db_object(context, self, db_offer) _create_offer()
def expire(self, context=None): leases = Lease.get_all( { 'parent_lease_uuid': self.uuid, 'status': statuses.LEASE_CAN_DELETE }, None) for lease in leases: lease.expire(context) offers = offer_obj.Offer.get_all( { 'parent_lease_uuid': self.uuid, 'status': statuses.OFFER_CAN_DELETE }, None) for offer in offers: offer.expire(context) with utils.lock(utils.get_resource_lock_name(self.resource_type, self.resource_uuid), external=True): LOG.info('Expiring lease %s', self.uuid) try: resource = self.resource_object() if resource.get_lease_uuid() == self.uuid: resource.expire_lease(self) if self.parent_lease_uuid is not None: parent_lease = Lease.get(self.parent_lease_uuid) resource.set_lease(parent_lease) # expire lease self.status = statuses.EXPIRED self.expire_time = datetime.datetime.now() except Exception as e: LOG.info('Error expiring lease: %s: %s' % (type(e).__name__, e)) LOG.info('Setting lease status to WAIT') self.status = statuses.WAIT_EXPIRE self.save(context)