"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-10.0.0/zaqar/storage/swift/driver.py" (13 May 2020, 4887 Bytes) of package /linux/misc/openstack/zaqar-10.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "driver.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 9.0.0_vs_10.0.0.

    1 # Licensed under the Apache License, Version 2.0 (the "License");
    2 # you may not use this file except in compliance with the License.
    3 # You may obtain a copy of the License at
    4 #
    5 #    http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS,
    9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   10 # implied.
   11 # See the License for the specific language governing permissions and
   12 # limitations under the License.
   13 
   14 import logging
   15 from osprofiler import profiler
   16 from six.moves import urllib
   17 
   18 from keystoneauth1.identity import generic
   19 from keystoneauth1 import session as keystone_session
   20 from oslo_log import log as oslo_logging
   21 import swiftclient
   22 
   23 from zaqar.common import decorators
   24 from zaqar.conf import drivers_message_store_swift
   25 from zaqar import storage
   26 from zaqar.storage.swift import controllers
   27 
   28 LOG = oslo_logging.getLogger(__name__)
   29 
   30 
   31 class DataDriver(storage.DataDriverBase):
   32 
   33     _DRIVER_OPTIONS = [(drivers_message_store_swift.GROUP_NAME,
   34                         drivers_message_store_swift.ALL_OPTS)]
   35 
   36     def __init__(self, conf, cache, control_driver):
   37         super(DataDriver, self).__init__(conf, cache, control_driver)
   38         self.swift_conf = self.conf[drivers_message_store_swift.GROUP_NAME]
   39         if not self.conf.debug:
   40             # Reduce swiftclient logging, in particular to remove 404s
   41             logging.getLogger("swiftclient").setLevel(logging.WARNING)
   42 
   43     @property
   44     def capabilities(self):
   45         return (
   46             storage.Capabilities.AOD,
   47             storage.Capabilities.DURABILITY,
   48         )
   49 
   50     @decorators.lazy_property(write=False)
   51     def connection(self):
   52         return _ClientWrapper(self.swift_conf)
   53 
   54     def is_alive(self):
   55         try:
   56             self.connection.get_capabilities()
   57             return True
   58         except Exception:
   59             LOG.exception('Aliveness check failed:')
   60             return False
   61 
   62     @decorators.lazy_property(write=False)
   63     def message_controller(self):
   64         controller = controllers.MessageController(self)
   65         if (self.conf.profiler.enabled and
   66                 self.conf.profiler.trace_message_store):
   67             return profiler.trace_cls("swift_message_controller")(controller)
   68         else:
   69             return controller
   70 
   71     @decorators.lazy_property(write=False)
   72     def subscription_controller(self):
   73         controller = controllers.SubscriptionController(self)
   74         if (self.conf.profiler.enabled and
   75                 self.conf.profiler.trace_message_store):
   76             return profiler.trace_cls("swift_subscription_"
   77                                       "controller")(controller)
   78         else:
   79             return controller
   80 
   81     @decorators.lazy_property(write=False)
   82     def claim_controller(self):
   83         controller = controllers.ClaimController(self)
   84         if (self.conf.profiler.enabled and
   85                 self.conf.profiler.trace_message_store):
   86             return profiler.trace_cls("swift_claim_controller")(controller)
   87         else:
   88             return controller
   89 
   90     def _health(self):
   91         raise NotImplementedError("No health checks")
   92 
   93     def close(self):
   94         pass
   95 
   96 
   97 class _ClientWrapper(object):
   98     """Wrapper around swiftclient.Connection.
   99 
  100     This wraps swiftclient.Connection to give the same API, but provide a
  101     thread-safe alternative with a different object for every method call. It
  102     maintains performance by managing authentication itself, and passing the
  103     token afterwards.
  104     """
  105 
  106     def __init__(self, conf):
  107         self.conf = conf
  108         self.endpoint = None
  109         self.parsed_url = urllib.parse.urlparse(conf.uri)
  110         self.session = None
  111 
  112     def _init_auth(self):
  113         auth = generic.Password(
  114             username=self.parsed_url.username,
  115             password=self.parsed_url.password,
  116             project_name=self.parsed_url.path[1:],
  117             user_domain_id=self.conf.user_domain_id,
  118             user_domain_name=self.conf.user_domain_name,
  119             project_domain_id=self.conf.project_domain_id,
  120             project_domain_name=self.conf.project_domain_name,
  121             auth_url=self.conf.auth_url)
  122         self.session = keystone_session.Session(auth=auth)
  123         self.endpoint = self.session.get_endpoint(
  124             service_type='object-store',
  125             interface=self.conf.interface,
  126             region_name=self.conf.region_name
  127         )
  128 
  129     def __getattr__(self, attr):
  130         if self.session is None:
  131             self._init_auth()
  132         os_options = {
  133             'object_storage_url': self.endpoint
  134         }
  135         client = swiftclient.Connection(session=self.session,
  136                                         insecure=self.conf.insecure,
  137                                         os_options=os_options)
  138         return getattr(client, attr)