def _time_error(LOG, begin, end): 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") begin = begin.replace(tzinfo=iso8601.Utc()) end = end.replace(tzinfo=iso8601.Utc()) 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) return begin, end
def issue_fields(issue): result = {} # Add basic fields for field_name, dotted_field in as_is_fields.items(): result[field_name] = retrieve_dotnotation_field(issue, dotted_field) # Add user transitions for field_name, field_value in issue['__user_transitions'].items(): result[field_name] = field_value # Calculate # of days in current status result['days_in_current_status'] = ( datetime.datetime.now(iso8601.Utc()) - parse_iso( issue['__transitions'][-1]['when'])).total_seconds() / (24 * 3600) # Add character lenghts for some fields description_field = retrieve_dotnotation_field(issue, 'fields.description') summary_field = retrieve_dotnotation_field(issue, 'fields.summary') result['description_length'] = len( description_field) if description_field else None result['summary_length'] = len(summary_field) if summary_field else None return result
def now_utc(): """Return the current datetime with tzinfo set to UTC""" return datetime.now(iso8601.Utc())
def add_utc_tzinfo(d): """Add a UTC timezone to the given datetime""" return d.replace(tzinfo=iso8601.Utc())
def fake_utcnow(with_timezone=False): tzinfo = iso8601.Utc() if with_timezone else None return datetime.datetime(2012, 10, 29, 13, 42, 11, tzinfo=tzinfo)
endpoint = keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL') if options.debug: logging.basicConfig(level=logging.DEBUG) glance = client.Client('2', endpoint=endpoint, token=auth_token) glance.format = 'json' # Fetch the list of files in store_directory matching the UUID regex uuid_re = re.compile( r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}') files = [(x, os.path.join(store_directory, x)) for x in os.listdir(store_directory) if uuid_re.match(x)] files = [(x, os.path.getsize(p), datetime.fromtimestamp(os.path.getmtime(p), iso8601.Utc())) for x, p in files if os.path.isfile(p)] # Fetch the list of glance images glance_images = [] kwargs = { 'sort_key': 'id', 'sort_dir': 'asc', 'owner': None, 'filters': {}, 'is_public': None } for x in glance.images.list(**kwargs): if x.status == 'active': tz_aware_time = parser.parse(x.created_at) glance_images.append((x.id, x.size, tz_aware_time))
endpoint = keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL') if options.debug: logging.basicConfig(level=logging.DEBUG) glance = client.Client('2', endpoint=endpoint, token=auth_token) glance.format = 'json' # Fetch the list of files in store_directory matching the UUID regex uuid_re = re.compile( r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}') files = [(x, os.path.join(store_directory, x)) for x in os.listdir(store_directory) if uuid_re.match(x)] files = [(x, os.path.getsize(p), datetime.fromtimestamp(os.path.getmtime(p), iso8601.Utc())) for x, p in files if os.path.isfile(p)] # Fetch the list of glance images glance_images = [] kwargs = { 'sort_key': 'id', 'sort_dir': 'asc', 'owner': None, 'filters': {}, 'is_public': None } for x in glance.images.list(**kwargs): if x.status == 'active': tz_aware_time = parser.parse(x.created_at) glance_images.append((x.id, x.size, tz_aware_time, is_remote_image(x)))
def stub_utcnow(with_timezone=False): tzinfo = iso8601.Utc() if with_timezone else None return datetime.datetime(2013, 7, 3, 0, 0, 2, tzinfo=tzinfo)
def fake_utcnow(with_timezone=False): tzinfo = iso8601.Utc() if with_timezone else None return datetime.datetime(2016, 6, 1, 2, 46, 30, tzinfo=tzinfo)
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") begin = begin.replace(tzinfo=iso8601.Utc()) end = end.replace(tzinfo=iso8601.Utc()) 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.info(_LI("Starting volume usage audit")) msg = _LI("Creating usages for %(begin_period)s until %(end_period)s") LOG.info(msg, {"begin_period": str(begin), "end_period": str(end)}) extra_info = { 'audit_period_beginning': str(begin), 'audit_period_ending': str(end), } volumes = objects.VolumeList.get_all_active_by_window(admin_context, begin, end) LOG.info(_LI("Found %d volumes"), len(volumes)) for volume_ref in volumes: try: cinder.volume.utils.notify_about_volume_usage( admin_context, volume_ref, 'exists', extra_usage_info=extra_info) LOG.debug("Sent 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}) 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), } 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) LOG.debug("Sent 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}) 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), } 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) LOG.debug("Sent 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}) except Exception as exc_msg: LOG.exception(_LE("Delete volume notification failed: %s"), exc_msg, resource=volume_ref) snapshots = objects.SnapshotList.get_all_active_by_window(admin_context, begin, end) LOG.info(_LI("Found %d snapshots"), len(snapshots)) for snapshot_ref in snapshots: try: cinder.volume.utils.notify_about_snapshot_usage(admin_context, snapshot_ref, 'exists', extra_info) LOG.debug("Sent 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}) 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), } 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) LOG.debug("Sent 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}) 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), } 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) LOG.debug("Sent 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}) except Exception as exc_msg: LOG.exception(_LE("Delete snapshot notification failed: %s"), exc_msg, resource=snapshot_ref) backups = objects.BackupList.get_all_active_by_window(admin_context, begin, end) LOG.info(_LI("Found %d backups"), len(backups)) for backup_ref in backups: try: cinder.volume.utils.notify_about_backup_usage(admin_context, backup_ref, 'exists', extra_info) LOG.debug("Sent notification for <backup_id: %(backup_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'backup_id': backup_ref.id, 'project_id': backup_ref.project_id, 'extra_info': extra_info}) except Exception as exc_msg: LOG.error(_LE("Exists backups notification failed: %s"), exc_msg) if (CONF.send_actions and backup_ref.created_at > begin and backup_ref.created_at < end): try: local_extra_info = { 'audit_period_beginning': str(backup_ref.created_at), 'audit_period_ending': str(backup_ref.created_at), } cinder.volume.utils.notify_about_backup_usage( admin_context, backup_ref, 'create.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_backup_usage( admin_context, backup_ref, 'create.end', extra_usage_info=local_extra_info) LOG.debug("Sent create notification for " "<backup_id: %(backup_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'backup_id': backup_ref.id, 'project_id': backup_ref.project_id, 'extra_info': local_extra_info}) except Exception as exc_msg: LOG.error(_LE("Create backup notification failed: %s"), exc_msg) if (CONF.send_actions and backup_ref.deleted_at and backup_ref.deleted_at > begin and backup_ref.deleted_at < end): try: local_extra_info = { 'audit_period_beginning': str(backup_ref.deleted_at), 'audit_period_ending': str(backup_ref.deleted_at), } cinder.volume.utils.notify_about_backup_usage( admin_context, backup_ref, 'delete.start', extra_usage_info=local_extra_info) cinder.volume.utils.notify_about_backup_usage( admin_context, backup_ref, 'delete.end', extra_usage_info=local_extra_info) LOG.debug("Sent delete notification for " "<backup_id: %(backup_id)s> " "<project_id %(project_id)s> <%(extra_info)s>", {'backup_id': backup_ref.id, 'project_id': backup_ref.project_id, 'extra_info': local_extra_info}) except Exception as exc_msg: LOG.error(_LE("Delete backup notification failed: %s"), exc_msg) LOG.info(_LI("Volume usage audit completed"))
def now(): """Return datetime instance for current time in UTC. """ return datetime.datetime.utcnow().replace(tzinfo=iso8601.Utc())