def on_get(self, meta, instance_id=None): # if null get all else get exclude instance vm_repo = self.vmexclude_repo instances = [] if instance_id is None: instances = vm_repo.get_type_entities() else: instance = vm_repo.get(entity_id=str(instance_id)) # url = hrefs.convert_vmexpire_to_href(instance.id) repo.commit() return { 'vmexclude': hrefs.convert_to_hrefs(instance.to_dict_fields()) } total = len(instances) instances_resp = [ hrefs.convert_to_hrefs(o.to_dict_fields()) for o in instances ] instances_resp_overall = hrefs.add_nav_hrefs( 'vmexcludes', 0, total, total, {'vmexcludes': instances_resp}) instances_resp_overall = hrefs.add_self_href( self.project_id + '/vmexcludes/', instances_resp_overall) instances_resp_overall.update({'total': total}) repo.commit() return instances_resp_overall
def add(self, excludeType=None, excludeId=None): if excludeId is None: print("id option is mandatory") return if excludeType not in VmExcludeCommands.EXCLUSION_TYPES: print("type is not valid") return repositories.setup_database_engine_and_factory() repo = repositories.get_vmexclude_repository() entity = VmExclude() entity.exclude_type = VmExcludeCommands.EXCLUSION_TYPES[excludeType] entity.exclude_id = excludeId try: entity = repo.create_exclude(entity) except Exception as e: print(str(e)) return repositories.commit() headers = ['id', 'type', 'exclude_id'] pt = prettytable.PrettyTable(headers) pt.add_row([ entity.id, VmExcludeCommands.EXCLUSION_MAP[entity.exclude_type], entity.exclude_id ]) if six.PY3: print(encodeutils.safe_encode(pt.get_string()).decode()) else: print(encodeutils.safe_encode(pt.get_string()))
def on_get(self, meta, instance_id=None): # if null get all else get expiration for instance # ctxt = controllers._get_vmexpire_context(pecan.request) vm_repo = self.vmexpire_repo instances = [] if instance_id is None: all_tenants = pecan.request.GET.get('all_tenants') if all_tenants is not None: ctxt = controllers._get_vmexpire_context(pecan.request) if ctxt.is_admin: instances = vm_repo.get_entities() else: pecan.response.status = 403 return "all_tenants is restricted to admin users" else: instances = vm_repo.get_project_entities(str(self.project_id)) else: instance = vm_repo.get(entity_id=str(instance_id)) # url = hrefs.convert_vmexpire_to_href(instance.id) repo.commit() return { 'vmexpire': hrefs.convert_to_hrefs(instance.to_dict_fields()) } total = len(instances) instances_resp = [ hrefs.convert_to_hrefs(o.to_dict_fields()) for o in instances ] instances_resp_overall = hrefs.add_nav_hrefs( 'vmexpires', 0, total, total, {'vmexpires': instances_resp}) instances_resp_overall = hrefs.add_self_href( self.project_id + '/vmexpires/', instances_resp_overall) instances_resp_overall.update({'total': total}) repo.commit() return instances_resp_overall
def on_delete(self, meta, instance_id): instance = self.vmexclude_repo.get(entity_id=instance_id) if instance: self.vmexclude_repo.delete_entity_by_id(entity_id=instance.id) repo.commit() pecan.response.status = 204 return
def clean_command(sql_url, min_num_days, verbose, log_file): """Clean command to clean up the database. :param sql_url: sql connection string to connect to a database :param min_num_days: clean up soft deletions older than this date :param verbose: If True, log and print more information :param log_file: If set, override the log_file configured """ if verbose: # The verbose flag prints out log events to the screen, otherwise # the log events will only go to the log file CONF.set_override('debug', True) if log_file: CONF.set_override('log_file', log_file) LOG.info("Cleaning up soft deletions in the barbican database") log.setup(CONF, 'osvmexpire') cleanup_total = 0 current_time = timeutils.utcnow() stop_watch = timeutils.StopWatch() stop_watch.start() try: if sql_url: CONF.set_override('sql_connection', sql_url) repo.setup_database_engine_and_factory() threshold_date = None if min_num_days >= 0: threshold_date = current_time - datetime.timedelta( days=min_num_days) else: threshold_date = current_time cleanup_total += cleanup_all(threshold_date=threshold_date) repo.commit() except Exception as ex: LOG.exception('Failed to clean up soft deletions in database.') repo.rollback() cleanup_total = 0 # rollback happened, no entries affected raise ex finally: stop_watch.stop() elapsed_time = stop_watch.elapsed() if verbose: CONF.clear_override('debug') if log_file: CONF.clear_override('log_file') repo.clear() if sql_url: CONF.clear_override('sql_connection') log.setup(CONF, 'osvmexpire') # reset the overrides LOG.info("Cleaning of database affected %s entries", cleanup_total) LOG.info('DB clean up finished in %s seconds', elapsed_time)
def tearDown(self): super(WhenTestingVmExpiresResource, self).tearDown() repo = repositories.get_vmexpire_repository() repo.delete_all_entities() repositories.commit() exclude_repo = repositories.get_vmexclude_repository() exclude_repo.delete_all_entities() repositories.commit()
def delete(self, excludeId=None): if excludeId is None: print("Missing mandatory id parameter") return repositories.setup_database_engine_and_factory() repo = repositories.get_vmexclude_repository() repo.delete_entity_by_id(excludeId) repositories.commit() print("Exclude deleted")
def extend(self, expirationid): if not expirationid: print("Missing id parameter") return repositories.setup_database_engine_and_factory() repo = repositories.get_vmexpire_repository() repo.extend_vm(entity_id=expirationid) repositories.commit() print("VM expiration successfully extended!")
def remove(self, expirationid): if not expirationid: print("Missing id paramerer") return repositories.setup_database_engine_and_factory() repo = repositories.get_vmexpire_repository() repo.delete_entity_by_id(entity_id=expirationid) repositories.commit() print("VM expiration successfully removed!")
def on_put(self, meta, instance_id): instance = None try: instance = self.vmexpire_repo.extend_vm(entity_id=instance_id) except Exception as e: pecan.response.status = 403 return str(e) # url = hrefs.convert_vmexpire_to_href(instance.id) repo.commit() pecan.response.status = 202 return {'vmexpire': hrefs.convert_to_hrefs(instance.to_dict_fields())}
def add(self, instanceid): if not instanceid: print("Missing id parameter") return repositories.setup_database_engine_and_factory() repo = repositories.get_vmexpire_repository() instance = repo.add_vm(instanceid) if not instance: print("Failure to add VM expiration, check logs") return repositories.commit() print("VM expiration successfully generated!")
def wrapper(*args, **kwargs): fn_name = find_function_name(fn, if_no_name='???') # Manage session/transaction. try: fn(*args, **kwargs) repositories.commit() LOG.debug("Completed worker task (post-commit): '%s'", fn_name) except Exception: """NOTE: Wrapped functions must process with care! Exceptions that reach here will revert the entire transaction, including any updates made to entities such as setting error codes and error messages. """ LOG.exception("Problem seen processing worker task: '%s'", fn_name) repositories.rollback() finally: repositories.clear()
def test_extend_reset_notified_vmexpire(self): entity = create_vmexpire_model() instance = create_vmexpire(entity) instance.notified = True instance.save() repositories.commit() _get_existing_resp = self.app.get('/' + entity.project_id + '/vmexpires/' + instance.id) _get_resp = self.app.put('/' + entity.project_id + '/vmexpires/' + instance.id, headers={'Content-Type': 'application/json'}) self.assertEqual(202, _get_resp.status_int) self.assertFalse(_get_resp.json['vmexpire']['notified']) self.assertIn('vmexpire', _get_resp.json) self.assertEqual(_get_resp.json['vmexpire']['instance_id'], _get_existing_resp.json['vmexpire']['instance_id']) prev_expire = _get_existing_resp.json['vmexpire']['expire'] new_expire = _get_resp.json['vmexpire']['expire'] self.assertTrue(new_expire > prev_expire)
def test_vm_expire_cleaned(self, mock_get, mock_post, mock_delete, mock_email): entity = create_vmexpire_model('12345') instance = create_vmexpire(entity) instance.expire = 1 instance.save() repositories.commit() cleaner_check(None) db_entity = get_vmexpire(entity.id) self.assertTrue(db_entity.notified) self.assertFalse(db_entity.notified_last) cleaner_check(None) self.assertTrue(db_entity.notified) self.assertTrue(db_entity.notified_last) cleaner_check(None) found = False try: db_entity = get_vmexpire(entity.id) found = True except Exception: found = False self.assertFalse(found)
def on_post(self, meta): instance = None data = api.load_body(pecan.request) entity_id = data.get('id') entity_type = data.get('type') if not entity_id or entity_type is None: pecan.response.status = 403 return 'Empty id or type' if entity_type not in ['domain', 'project', 'user']: pecan.response.status = 403 return 'Invalid type' else: entity_type = self.vmexclude_repo.get_exclude_type(entity_type) try: entity = self.vmexclude_repo.create_exclude_entity( entity_id, entity_type) instance = self.vmexclude_repo.create_exclude(entity) except Exception as e: pecan.response.status = 403 return str(e) # url = hrefs.convert_vmexpire_to_href(instance.id) repo.commit() pecan.response.status = 202 return {'vmexclude': hrefs.convert_to_hrefs(instance.to_dict_fields())}
def check(started_at): token = get_identity_token() conf_cleaner = config.CONF.cleaner LOG.debug("check instances") repo = repositories.get_vmexpire_repository() now = int(time.mktime(datetime.datetime.now().timetuple())) check_time = now + (conf_cleaner.notify_before_days * 3600 * 24) last_check_time = now + (conf_cleaner.notify_before_days_last * 3600 * 24) entities = repo.get_entities(expiration_filter=check_time) for entity in entities: if entity.expire < check_time and not entity.notified: # notify LOG.debug("First expiration notification %s" % (entity.id)) res = send_email(entity, token, delete=False) if res: entity.notified = True try: entity.save() repositories.commit() except Exception as e: LOG.exception("expiration save error: " + str(e)) repositories.rollback() elif entity.expire < last_check_time and not entity.notified_last: # notify_last LOG.debug("Last expiration notification %s" % (entity.id)) res = send_email(entity, token, delete=False) if res: entity.notified_last = True try: entity.save() repositories.commit() except Exception as e: LOG.exception("expiration save error: " + str(e)) repositories.rollback() elif entity.expire < now: # delete LOG.debug("Delete VM %s" % (entity.id)) res = delete_vm(entity.instance_id, entity.project_id, token) if res: try: repo.delete_entity_by_id(entity_id=entity.id) repositories.commit() except Exception as e: LOG.exception("expiration deletion error: " + str(e)) repositories.rollback() send_email(entity, token, delete=True)
def tearDown(self): super(WhenTestingVmExpiresResource, self).tearDown() repo = repositories.get_vmexpire_repository() repo.delete_all_entities() repositories.commit() repositories.CONF.max_vm_total_duration = self.max_vm_total_duration
def create_vmexpire(entity): repo = repositories.get_vmexpire_repository() instance = repo.create_from(entity) repositories.commit() return instance
def create_vmexclude(entity): repo = repositories.get_vmexclude_repository() instance = repo.create_exclude(entity) repositories.commit() return instance