def remind_about_instances(): """Run periodically to send reminding emails""" cs = admin_client_set() server_mgr = cs.compute.servers user_mgr = cs.identity_admin.users for instance_data in InstanceDataDAO.remind_list(datetime.utcnow()): try: try: server = server_mgr.get(instance_data.instance_id) except osc_exc.NotFound: InstanceDataDAO.delete(instance_data.instance_id) continue try: user = user_mgr.get(server.user_id) except osc_exc.NotFound: pass else: send_instance_reminder(user.email, server.name, server.id, instance_data.expires_at, greeting=getattr(user, 'fullname', '')) InstanceDataDAO.update(instance_data.instance_id, remind_at=None) except Exception: current_app.logger.exception( 'Failed to send reminder email about instance %r' % instance_data.instance_id)
def instance_data_gc(): """Remove instance data for already deleted servers""" server_mgr = admin_client_set().compute.servers for instance_data in InstanceDataDAO.list_all(): try: server_mgr.get(instance_data.instance_id) except osc_exc.NotFound: InstanceDataDAO.delete(instance_data.instance_id) except Exception: current_app.logger.exception('Failed to delete data ' 'for non-existing instance %r' % instance_data.instance_id)
def _do_remove_instance(instance_id): """The real instance removal implementation""" try: fetch_instance(instance_id).delete() except osc_exc.NotFound: abort(404) InstanceDataDAO.delete(instance_id) try: return fetch_instance(instance_id) except HTTPException: return None
def rip_expired_instances(): """Run periodically to remove expired instances""" server_mgr = admin_client_set().compute.servers for instance_data in InstanceDataDAO.expired_list(datetime.utcnow()): try: server_mgr.delete(instance_data.instance_id) AuditDAO.create_record(dict( resource=url_for('instances.delete_instance', instance_id=instance_data.instance_id), method='DELETE', response_status=200, message='Automatically deleted expired instance', )) except osc_exc.NotFound: InstanceDataDAO.delete(instance_data.instance_id) except Exception: current_app.logger.exception('Failed to delete expired instance %r' % instance_data.instance_id)
def rip_expired_instances(): """Run periodically to remove expired instances""" server_mgr = admin_client_set().compute.servers for instance_data in InstanceDataDAO.expired_list(datetime.utcnow()): try: server_mgr.delete(instance_data.instance_id) AuditDAO.create_record( dict( resource=url_for('instances.delete_instance', instance_id=instance_data.instance_id), method='DELETE', response_status=200, message='Automatically deleted expired instance', )) except osc_exc.NotFound: InstanceDataDAO.delete(instance_data.instance_id) except Exception: current_app.logger.exception( 'Failed to delete expired instance %r' % instance_data.instance_id)
def test_delete_deletes(self): self.assertTrue(InstanceDataDAO.delete(self.instance_id)) self.assertEquals(None, InstanceDataDAO.get(self.instance_id))