From 2b5bc3cc8b8b68eec5251b237181019efd5abe70 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 20 Sep 2012 18:37:36 +0200 Subject: [PATCH] Fix cinder-volume-usage-audit It seems many point in the internal API and openstack.common changed, but this binary has been forgotten. Also, context ie needed when using the notifier, so import it. This fixes bug #1053502 Change-Id: I306b8671e7b0ed8c2ce2548a51a4c5e6393e13cb Signed-off-by: Julien Danjou (cherry picked from commit 186143ecc81369c02aca51b81feb976259bcad41) --- bin/cinder-volume-usage-audit | 22 +++---- cinder/openstack/common/context.py | 81 +++++++++++++++++++++++++ cinder/openstack/common/notifier/api.py | 8 +-- cinder/volume/utils.py | 2 +- openstack-common.conf | 2 +- 5 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 cinder/openstack/common/context.py diff --git a/bin/cinder-volume-usage-audit b/bin/cinder-volume-usage-audit index 97c822ef3..1e840b5fc 100755 --- a/bin/cinder-volume-usage-audit +++ b/bin/cinder-volume-usage-audit @@ -59,29 +59,21 @@ import cinder.volume.utils FLAGS = flags.FLAGS - -def output(msg): - if not FLAGS.silent: - print msg - - if __name__ == '__main__': - rpc.register_opts(FLAGS) admin_context = context.get_admin_context() - utils.default_flagfile() - flags.FLAGS(sys.argv) - logging.setup() + flags.parse_args(sys.argv) + logging.setup("cinder") begin, end = utils.last_completed_audit_period() - output("Starting volume usage audit") - output("Creating usages for %s until %s" % (str(begin), str(end))) + print "Starting volume usage audit" + print "Creating usages for %s until %s" % (str(begin), str(end)) volumes = db.volume_get_active_by_window(admin_context, begin, end) - output("Found %d volumes" % len(volumes)) + print "Found %d volumes" % len(volumes) for volume_ref in volumes: try: cinder.volume.utils.notify_usage_exists( admin_context, volume_ref) except Exception, e: - output(traceback.format_exc(e)) - output("Volume usage audit completed") + print traceback.format_exc(e) + print "Volume usage audit completed" diff --git a/cinder/openstack/common/context.py b/cinder/openstack/common/context.py new file mode 100644 index 000000000..dd7dd04c3 --- /dev/null +++ b/cinder/openstack/common/context.py @@ -0,0 +1,81 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Simple class that stores security context information in the web request. + +Projects should subclass this class if they wish to enhance the request +context or provide additional information in their specific WSGI pipeline. +""" + +import itertools +import uuid + + +def generate_request_id(): + return 'req-' + str(uuid.uuid4()) + + +class RequestContext(object): + + """ + Stores information about the security context under which the user + accesses the system, as well as additional request information. + """ + + def __init__(self, auth_tok=None, user=None, tenant=None, is_admin=False, + read_only=False, show_deleted=False, request_id=None): + self.auth_tok = auth_tok + self.user = user + self.tenant = tenant + self.is_admin = is_admin + self.read_only = read_only + self.show_deleted = show_deleted + if not request_id: + request_id = generate_request_id() + self.request_id = request_id + + def to_dict(self): + return {'user': self.user, + 'tenant': self.tenant, + 'is_admin': self.is_admin, + 'read_only': self.read_only, + 'show_deleted': self.show_deleted, + 'auth_token': self.auth_tok, + 'request_id': self.request_id} + + +def get_admin_context(show_deleted="no"): + context = RequestContext(None, + tenant=None, + is_admin=True, + show_deleted=show_deleted) + return context + + +def get_context_from_function_and_args(function, args, kwargs): + """Find an arg of type RequestContext and return it. + + This is useful in a couple of decorators where we don't + know much about the function we're wrapping. + """ + + for arg in itertools.chain(kwargs.values(), args): + if isinstance(arg, RequestContext): + return arg + + return None diff --git a/cinder/openstack/common/notifier/api.py b/cinder/openstack/common/notifier/api.py index aea934632..8acd5785c 100644 --- a/cinder/openstack/common/notifier/api.py +++ b/cinder/openstack/common/notifier/api.py @@ -16,7 +16,7 @@ import uuid from cinder.openstack.common import cfg -from cinder import context +from cinder.openstack.common import context from cinder.openstack.common.gettextutils import _ from cinder.openstack.common import importutils from cinder.openstack.common import jsonutils @@ -139,8 +139,8 @@ def notify(context, publisher_id, event_type, priority, payload): driver.notify(context, msg) except Exception, e: LOG.exception(_("Problem '%(e)s' attempting to " - "send to notification system. Payload=%(payload)s") % - locals()) + "send to notification system. " + "Payload=%(payload)s") % locals()) _drivers = None @@ -169,7 +169,7 @@ def add_driver(notification_driver): except ImportError as e: LOG.exception(_("Failed to load notifier %s. " "These notifications will not be sent.") % - notification_driver) + notification_driver) else: # Driver is already loaded; just add the object. _drivers[notification_driver] = notification_driver diff --git a/cinder/volume/utils.py b/cinder/volume/utils.py index d5a7420e0..a79a39a69 100644 --- a/cinder/volume/utils.py +++ b/cinder/volume/utils.py @@ -56,7 +56,7 @@ def _usage_from_volume(context, volume_ref, **kw): tenant_id=volume_ref['project_id'], user_id=volume_ref['user_id'], volume_id=volume_ref['id'], - volume_type=volume_ref['volume_type'], + volume_type=volume_ref['volume_type_id'], display_name=volume_ref['display_name'], launched_at=null_safe_str(volume_ref['launched_at']), created_at=null_safe_str(volume_ref['created_at']), diff --git a/openstack-common.conf b/openstack-common.conf index d75d4f79e..091cab342 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,7 +1,7 @@ [DEFAULT] # The list of modules to copy from openstack-common -modules=cfg,exception,excutils,gettextutils,importutils,iniparser,jsonutils,local,rpc,timeutils,log,setup,notifier +modules=cfg,exception,excutils,gettextutils,importutils,iniparser,jsonutils,local,rpc,timeutils,log,setup,notifier,context # The base module to hold the copy of openstack.common base=cinder -- 2.45.2