def add_vm(self, instance_uuid, session=None): session = self.get_session(session) instance = None try: instance = self.get_by_instance(instance_uuid) except Exception: LOG.debug("Fine, instance does not already exists") if instance: LOG.warn("InstanceAlreadyExists:" + instance_uuid + ", deleting first") self.delete_entity_by_id(entity_id=instance.id) instance_data = get_instance(instance_uuid) if not instance_data: LOG.debug("Not found for %s", instance_uuid) entity = None _raise_entity_not_found("Openstack instance", instance_uuid) entity = models.VmExpire() entity.instance_id = instance_uuid entity.instance_name = instance_data["display_name"] entity.project_id = instance_data["tenant_id"] entity.user_id = instance_data["user_id"] entity.expire = int( time.mktime(datetime.datetime.now().timetuple()) + (CONF.max_vm_duration * 3600 * 24)) entity.notified = False entity.notified_last = False project_domain = None try: project_domain = get_project_domain(entity.project_id) except Exception: LOG.exception('Failed to get domain for project') exclude_repo = get_vmexclude_repository() if project_domain: exclude_id = exclude_repo.get_exclude_by_id(project_domain) if exclude_id: LOG.debug('domain %s is excluded, skipping' % (project_domain)) _raise_entity_invalid(instance_uuid, "domain is excluded") exclude_id = exclude_repo.get_exclude_by_id(entity.project_id) if exclude_id: LOG.debug('project %s is excluded, skipping' % (entity.project_id)) _raise_entity_invalid(instance_uuid, "project is excluded") exclude_id = exclude_repo.get_exclude_by_id(entity.user_id) if exclude_id: LOG.debug('user %s is excluded, skipping' % (entity.user_id)) _raise_entity_invalid(instance_uuid, "user is excluded") instance = self.create_from(entity, session) LOG.debug("NewInstanceExpiration:" + instance_uuid) return instance
def create_vmexpire_model(prefix=None): if not prefix: prefix = '12345' entity = models.VmExpire() entity.user_id = prefix + 'user' entity.project_id = prefix + 'project' entity.instance_id = prefix + 'instance' entity.instance_name = prefix entity.notified = False entity.notified_last = False entity.expire = time.mktime(datetime.datetime.now().timetuple()) return entity
def create_vmexpire(instance=None, session=None): expire = models.VmExpire() expire.project_id = instance.project_id expire.instance_id = instance.instance_id expire.instance_name = instance.instance_name expire.user_id = instance.user_id expire.expire = time.mktime( datetime.datetime.now().timetuple()) + 2 * 3600 * 24 expire.notified = False expire.notified_last = False container_repo = repositories.get_vmexpire_repository() container_repo.create_from(expire, session=session) return expire
def info(self, ctxt, publisher_id, event_type, payload, metadata): if event_type == 'instance.create.end': LOG.debug(event_type + ':' + payload['nova_object.data']['uuid']) repo = repositories.get_vmexpire_repository() instance = None instance_uuid = str(payload['nova_object.data']['uuid']) try: instance = repo.get_by_instance(instance_uuid) except Exception: LOG.debug("Fine, instance does not already exists") if instance: LOG.warn("InstanceAlreadyExists:" + instance_uuid + ", deleting first") repo.delete_entity_by_id(entity_id=instance.id) entity = models.VmExpire() entity.instance_id = instance_uuid if payload['nova_object.data']['display_name']: display_name = payload['nova_object.data']['display_name'] entity.instance_name = display_name else: entity.instance_name = instance_uuid entity.project_id = payload['nova_object.data']['tenant_id'] entity.user_id = payload['nova_object.data']['user_id'] entity.expire = int( time.mktime(datetime.datetime.now().timetuple()) + (CONF.max_vm_duration * 3600 * 24)) entity.notified = False entity.notified_last = False project_domain = None try: project_domain = get_project_domain(entity.project_id) except Exception: LOG.exception('Failed to get domain for project') exclude_repo = repositories.get_vmexclude_repository() if project_domain: exclude_id = exclude_repo.get_exclude_by_id(project_domain) if exclude_id: LOG.debug('domain %s is excluded, skipping' % (project_domain)) return exclude_id = exclude_repo.get_exclude_by_id(entity.project_id) if exclude_id: LOG.debug('project %s is excluded, skipping' % (entity.project_id)) return exclude_id = exclude_repo.get_exclude_by_id(entity.user_id) if exclude_id: LOG.debug('user %s is excluded, skipping' % (entity.user_id)) return instance = repo.create_from(entity) LOG.debug("NewInstanceExpiration:" + instance_uuid) elif event_type == 'instance.delete.end': instance_uuid = str(payload['nova_object.data']['uuid']) LOG.debug(event_type + ':' + instance_uuid) repo = repositories.get_vmexpire_repository() try: instance = repo.get_by_instance(instance_uuid) repo.delete_entity_by_id(entity_id=instance.id) LOG.debug("Delete id:" + instance.id) except Exception: LOG.warn('Failed to delete: ' + instance_uuid) LOG.debug(publisher_id) LOG.debug(event_type) LOG.debug(str(metadata)) LOG.debug(json.dumps(payload, indent=4))