def get_active_by_window(cls, context, begin, end): volumes = db.volume_get_active_by_window(context, begin, end) expected_attrs = cls._get_expected_attrs(context) return base.obj_make_list(context, cls(context), objects.Volume, volumes, expected_attrs=expected_attrs)
def test_volume_get_active_by_window(self): # Find all all volumes valid within a timeframe window. try: # Not in window db.volume_create( self.context, { 'id': 1, 'host': 'devstack', 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 2, 1, 1, 1, 1), } ) except exception.VolumeNotFound: pass try: # In - deleted in window db.volume_create( self.context, { 'id': 2, 'host': 'devstack', 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 3, 10, 1, 1, 1), } ) except exception.VolumeNotFound: pass try: # In - deleted after window db.volume_create( self.context, { 'id': 3, 'host': 'devstack', 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 5, 1, 1, 1, 1), } ) except exception.VolumeNotFound: pass # In - created in window db.volume_create( self.context, { 'id': 4, 'host': 'devstack', 'created_at': datetime.datetime(1, 3, 10, 1, 1, 1), } ) # Not of window. db.volume_create( self.context, { 'id': 5, 'host': 'devstack', 'created_at': datetime.datetime(1, 5, 1, 1, 1, 1), } ) volumes = db.volume_get_active_by_window( self.context, datetime.datetime(1, 3, 1, 1, 1, 1), datetime.datetime(1, 4, 1, 1, 1, 1)) self.assertEqual(len(volumes), 3) self.assertEqual(volumes[0].id, u'2') self.assertEqual(volumes[1].id, u'3') self.assertEqual(volumes[2].id, u'4')
def test_volume_get_active_by_window(self): # Find all all volumes valid within a timeframe window. try: # Not in window db.volume_create( self.context, { 'id': 1, 'host': 'devstack', 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 2, 1, 1, 1, 1), }) except exception.VolumeNotFound: pass try: # In - deleted in window db.volume_create( self.context, { 'id': 2, 'host': 'devstack', 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 3, 10, 1, 1, 1), }) except exception.VolumeNotFound: pass try: # In - deleted after window db.volume_create( self.context, { 'id': 3, 'host': 'devstack', 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 5, 1, 1, 1, 1), }) except exception.VolumeNotFound: pass # In - created in window db.volume_create( self.context, { 'id': 4, 'host': 'devstack', 'created_at': datetime.datetime(1, 3, 10, 1, 1, 1), }) # Not of window. db.volume_create( self.context, { 'id': 5, 'host': 'devstack', 'created_at': datetime.datetime(1, 5, 1, 1, 1, 1), }) volumes = db.volume_get_active_by_window( self.context, datetime.datetime(1, 3, 1, 1, 1, 1), datetime.datetime(1, 4, 1, 1, 1, 1)) self.assertEqual(len(volumes), 3) self.assertEqual(volumes[0].id, u'2') self.assertEqual(volumes[1].id, u'3') self.assertEqual(volumes[2].id, u'4')
def main(): objects.register_all() admin_context = context.get_admin_context() CONF(sys.argv[1:], project='cinder', version=version.version_string()) logging.setup(CONF, "cinder") LOG = logging.getLogger("cinder") rpc.init(CONF) begin, end = utils.last_completed_audit_period() if CONF.start_time: begin = datetime.datetime.strptime(CONF.start_time, "%Y-%m-%d %H:%M:%S") if CONF.end_time: end = datetime.datetime.strptime(CONF.end_time, "%Y-%m-%d %H:%M:%S") if not end > begin: msg = _("The end time (%(end)s) must be after the start " "time (%(start)s).") % {'start': begin, 'end': end} LOG.error(msg) sys.exit(-1) LOG.debug("Starting volume usage audit") msg = _("Creating usages for %(begin_period)s until %(end_period)s") LOG.debug(msg, {"begin_period": str(begin), "end_period": str(end)}) extra_info = { 'audit_period_beginning': str(begin), 'audit_period_ending': str(end), } volumes = db.volume_get_active_by_window(admin_context, begin, end) LOG.debug("Found %d volumes"), len(volumes) for volume_ref in volumes: try: LOG.debug("Send exists notification for <volume_id: " "%(volume_id)s> <project_id %(project_id)s> " "<%(extra_info)s>", {'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': extra_info}) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'exists', extra_usage_info=extra_info) except Exception as exc_msg: LOG.exception(_LE("Exists volume notification failed: %s"), exc_msg, resource=volume_ref) if (CONF.send_actions and volume_ref.created_at > begin and volume_ref.created_at < end): try: local_extra_info = { 'audit_period_beginning': str(volume_ref.created_at), 'audit_period_ending': str(volume_ref.created_at), } LOG.debug("Send create notification for " "<volume_id: %(volume_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'create.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'create.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Create volume notification failed: %s"), exc_msg, resource=volume_ref) if (CONF.send_actions and volume_ref.deleted_at and volume_ref.deleted_at > begin and volume_ref.deleted_at < end): try: local_extra_info = { 'audit_period_beginning': str(volume_ref.deleted_at), 'audit_period_ending': str(volume_ref.deleted_at), } LOG.debug("Send delete notification for " "<volume_id: %(volume_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'delete.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'delete.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Delete volume notification failed: %s"), exc_msg, resource=volume_ref) snapshots = objects.SnapshotList.get_active_by_window(admin_context, begin, end) LOG.debug("Found %d snapshots"), len(snapshots) for snapshot_ref in snapshots: try: LOG.debug("Send notification for <snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': extra_info}) cinder.volume.utils.notify_about_snapshot_usage(admin_context, snapshot_ref, 'exists', extra_info) except Exception as exc_msg: LOG.exception(_LE("Exists snapshot notification failed: %s"), exc_msg, resource=snapshot_ref) if (CONF.send_actions and snapshot_ref.created_at > begin and snapshot_ref.created_at < end): try: local_extra_info = { 'audit_period_beginning': str(snapshot_ref.created_at), 'audit_period_ending': str(snapshot_ref.created_at), } LOG.debug("Send create notification for " "<snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'create.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'create.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Create snapshot notification failed: %s"), exc_msg, resource=snapshot_ref) if (CONF.send_actions and snapshot_ref.deleted_at and snapshot_ref.deleted_at > begin and snapshot_ref.deleted_at < end): try: local_extra_info = { 'audit_period_beginning': str(snapshot_ref.deleted_at), 'audit_period_ending': str(snapshot_ref.deleted_at), } LOG.debug("Send delete notification for " "<snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'delete.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'delete.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Delete snapshot notification failed: %s"), exc_msg, resource=snapshot_ref) LOG.debug("Volume usage audit completed")
def main(): objects.register_all() admin_context = context.get_admin_context() CONF(sys.argv[1:], project='cinder', version=version.version_string()) logging.setup(CONF, "cinder") LOG = logging.getLogger("cinder") rpc.init(CONF) begin, end = utils.last_completed_audit_period() if CONF.start_time: begin = datetime.datetime.strptime(CONF.start_time, "%Y-%m-%d %H:%M:%S") if CONF.end_time: end = datetime.datetime.strptime(CONF.end_time, "%Y-%m-%d %H:%M:%S") if not end > begin: msg = _("The end time (%(end)s) must be after the start " "time (%(start)s).") % { 'start': begin, 'end': end } LOG.error(msg) sys.exit(-1) LOG.debug("Starting volume usage audit") msg = _("Creating usages for %(begin_period)s until %(end_period)s") LOG.debug(msg, {"begin_period": str(begin), "end_period": str(end)}) extra_info = { 'audit_period_beginning': str(begin), 'audit_period_ending': str(end), } volumes = db.volume_get_active_by_window(admin_context, begin, end) LOG.debug("Found %d volumes", len(volumes)) for volume_ref in volumes: try: LOG.debug( "Send exists notification for <volume_id: " "%(volume_id)s> <project_id %(project_id)s> " "<%(extra_info)s>", { 'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': extra_info }) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'exists', extra_usage_info=extra_info) except Exception as exc_msg: LOG.exception(_LE("Exists volume notification failed: %s"), exc_msg, resource=volume_ref) if (CONF.send_actions and volume_ref.created_at > begin and volume_ref.created_at < end): try: local_extra_info = { 'audit_period_beginning': str(volume_ref.created_at), 'audit_period_ending': str(volume_ref.created_at), } LOG.debug( "Send create notification for " "<volume_id: %(volume_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", { 'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': local_extra_info }) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'create.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'create.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Create volume notification failed: %s"), exc_msg, resource=volume_ref) if (CONF.send_actions and volume_ref.deleted_at and volume_ref.deleted_at > begin and volume_ref.deleted_at < end): try: local_extra_info = { 'audit_period_beginning': str(volume_ref.deleted_at), 'audit_period_ending': str(volume_ref.deleted_at), } LOG.debug( "Send delete notification for " "<volume_id: %(volume_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", { 'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': local_extra_info }) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'delete.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'delete.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Delete volume notification failed: %s"), exc_msg, resource=volume_ref) snapshots = objects.SnapshotList.get_active_by_window( admin_context, begin, end) LOG.debug("Found %d snapshots", len(snapshots)) for snapshot_ref in snapshots: try: LOG.debug( "Send notification for <snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", { 'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': extra_info }) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'exists', extra_info) except Exception as exc_msg: LOG.exception(_LE("Exists snapshot notification failed: %s"), exc_msg, resource=snapshot_ref) if (CONF.send_actions and snapshot_ref.created_at > begin and snapshot_ref.created_at < end): try: local_extra_info = { 'audit_period_beginning': str(snapshot_ref.created_at), 'audit_period_ending': str(snapshot_ref.created_at), } LOG.debug( "Send create notification for " "<snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", { 'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': local_extra_info }) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'create.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'create.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Create snapshot notification failed: %s"), exc_msg, resource=snapshot_ref) if (CONF.send_actions and snapshot_ref.deleted_at and snapshot_ref.deleted_at > begin and snapshot_ref.deleted_at < end): try: local_extra_info = { 'audit_period_beginning': str(snapshot_ref.deleted_at), 'audit_period_ending': str(snapshot_ref.deleted_at), } LOG.debug( "Send delete notification for " "<snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", { 'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': local_extra_info }) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'delete.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'delete.end', extra_usage_info=local_extra_info) except Exception as exc_msg: LOG.exception(_LE("Delete snapshot notification failed: %s"), exc_msg, resource=snapshot_ref) LOG.debug("Volume usage audit completed")
def main(): admin_context = context.get_admin_context() CONF(sys.argv[1:], project='cinder', version=version.version_string()) logging.setup("cinder") LOG = logging.getLogger("cinder") rpc.init(CONF) begin, end = utils.last_completed_audit_period() if CONF.start_time: begin = datetime.strptime(CONF.start_time, "%Y-%m-%d %H:%M:%S") if CONF.end_time: end = datetime.strptime(CONF.end_time, "%Y-%m-%d %H:%M:%S") if not end > begin: msg = _("The end time (%(end)s) must be after the start " "time (%(start)s).") % {'start': begin, 'end': end} print(msg) LOG.error(msg) sys.exit(-1) print(_("Starting volume usage audit")) msg = _("Creating usages for %(begin_period)s until %(end_period)s") print(msg % {"begin_period": str(begin), "end_period": str(end)}) extra_info = { 'audit_period_beginning': str(begin), 'audit_period_ending': str(end), } volumes = db.volume_get_active_by_window(admin_context, begin, end) print(_("Found %d volumes") % len(volumes)) for volume_ref in volumes: try: LOG.debug("Send exists notification for <volume_id: " "%(volume_id)s> <project_id %(project_id)s> " "<%(extra_info)s>" % {'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': extra_info}) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'exists', extra_usage_info=extra_info) except Exception as e: LOG.error(_LE("Failed to send exists notification" " for volume %s.") % volume_ref.id) print(traceback.format_exc(e)) if (CONF.send_actions and volume_ref.created_at > begin and volume_ref.created_at < end): try: local_extra_info = { 'audit_period_beginning': str(volume_ref.created_at), 'audit_period_ending': str(volume_ref.created_at), } LOG.debug("Send create notification for " "<volume_id: %(volume_id)s> " "<project_id %(project_id)s> <%(extra_info)s>" % {'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'create.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'create.end', extra_usage_info=local_extra_info) except Exception as e: LOG.error(_LE("Failed to send create notification for " "volume %s.") % volume_ref.id) print(traceback.format_exc(e)) if (CONF.send_actions and volume_ref.deleted_at and volume_ref.deleted_at > begin and volume_ref.deleted_at < end): try: local_extra_info = { 'audit_period_beginning': str(volume_ref.deleted_at), 'audit_period_ending': str(volume_ref.deleted_at), } LOG.debug("Send delete notification for " "<volume_id: %(volume_id)s> " "<project_id %(project_id)s> <%(extra_info)s>" % {'volume_id': volume_ref.id, 'project_id': volume_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'delete.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'delete.end', extra_usage_info=local_extra_info) except Exception as e: LOG.error(_LE("Failed to send delete notification for volume " "%s.") % volume_ref.id) print(traceback.format_exc(e)) snapshots = db.snapshot_get_active_by_window(admin_context, begin, end) print(_("Found %d snapshots") % len(snapshots)) for snapshot_ref in snapshots: try: LOG.debug("Send notification for <snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>" % {'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': extra_info}) cinder.volume.utils.notify_about_snapshot_usage(admin_context, snapshot_ref, 'exists', extra_info) except Exception as e: LOG.error(_LE("Failed to send exists notification " "for snapshot %s.") % snapshot_ref.id) print(traceback.format_exc(e)) if (CONF.send_actions and snapshot_ref.created_at > begin and snapshot_ref.created_at < end): try: local_extra_info = { 'audit_period_beginning': str(snapshot_ref.created_at), 'audit_period_ending': str(snapshot_ref.created_at), } LOG.debug("Send create notification for " "<snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>" % {'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'create.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'create.end', extra_usage_info=local_extra_info) except Exception as e: LOG.error(_LE("Failed to send create notification for snapshot" "%s.") % snapshot_ref.id) print(traceback.format_exc(e)) if (CONF.send_actions and snapshot_ref.deleted_at and snapshot_ref.deleted_at > begin and snapshot_ref.deleted_at < end): try: local_extra_info = { 'audit_period_beginning': str(snapshot_ref.deleted_at), 'audit_period_ending': str(snapshot_ref.deleted_at), } LOG.debug("Send delete notification for " "<snapshot_id: %(snapshot_id)s> " "<project_id %(project_id)s> <%(extra_info)s>" % {'snapshot_id': snapshot_ref.id, 'project_id': snapshot_ref.project_id, 'extra_info': local_extra_info}) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'delete.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_snapshot_usage( admin_context, snapshot_ref, 'delete.end', extra_usage_info=local_extra_info) except Exception as e: LOG.error(_LE("Failed to send delete notification for snapshot" "%s.") % snapshot_ref.id) print(traceback.format_exc(e)) print(_("Volume usage audit completed"))
def test_volume_get_active_by_window(self): # Find all all volumes valid within a timeframe window. try: # Not in window db.volume_create( self.context, { "id": 1, "host": "devstack", "created_at": datetime.datetime(1, 1, 1, 1, 1, 1), "deleted": True, "status": "deleted", "deleted_at": datetime.datetime(1, 2, 1, 1, 1, 1), }, ) except exception.VolumeNotFound: pass try: # In - deleted in window db.volume_create( self.context, { "id": 2, "host": "devstack", "created_at": datetime.datetime(1, 1, 1, 1, 1, 1), "deleted": True, "status": "deleted", "deleted_at": datetime.datetime(1, 3, 10, 1, 1, 1), }, ) except exception.VolumeNotFound: pass try: # In - deleted after window db.volume_create( self.context, { "id": 3, "host": "devstack", "created_at": datetime.datetime(1, 1, 1, 1, 1, 1), "deleted": True, "status": "deleted", "deleted_at": datetime.datetime(1, 5, 1, 1, 1, 1), }, ) except exception.VolumeNotFound: pass # In - created in window db.volume_create( self.context, {"id": 4, "host": "devstack", "created_at": datetime.datetime(1, 3, 10, 1, 1, 1)} ) # Not of window. db.volume_create(self.context, {"id": 5, "host": "devstack", "created_at": datetime.datetime(1, 5, 1, 1, 1, 1)}) volumes = db.volume_get_active_by_window( self.context, datetime.datetime(1, 3, 1, 1, 1, 1), datetime.datetime(1, 4, 1, 1, 1, 1) ) self.assertEqual(len(volumes), 3) self.assertEqual(volumes[0].id, u"2") self.assertEqual(volumes[1].id, u"3") self.assertEqual(volumes[2].id, u"4")