]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix cinder-volume-usage-audit
authorJulien Danjou <julien@danjou.info>
Thu, 20 Sep 2012 16:37:36 +0000 (18:37 +0200)
committerJulien Danjou <julien@danjou.info>
Fri, 21 Sep 2012 13:17:08 +0000 (15:17 +0200)
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 <julien@danjou.info>
bin/cinder-volume-usage-audit
cinder/openstack/common/context.py [new file with mode: 0644]
cinder/openstack/common/notifier/api.py
cinder/volume/utils.py
openstack-common.conf

index 97c822ef306b807ba7c32f16102adbd6db0d8224..1e840b5fc9c28f71e4306fc1ba04c661305f68b7 100755 (executable)
@@ -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 (file)
index 0000000..dd7dd04
--- /dev/null
@@ -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
index aea9346329329ed954efbca52f151d17bf97d923..8acd5785cf43e10a0edc7442b875ee6373b01103 100644 (file)
@@ -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
index d5a7420e0af1e95cfdb25967c4fb12761f4f6d64..a79a39a69e0b52ee08f4ca60b86ac30e13369d63 100644 (file)
@@ -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']),
index d75d4f79e91f10054e89568b23f276cd615b331c..091cab342049f7616384af14e9a24ed8fff20839 100644 (file)
@@ -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