"Fossies" - the Fresh Open Source Software Archive

Member "karbor-1.3.0/karbor/services/protection/bank_plugins/file_system_bank_plugin.py" (26 Mar 2019, 7244 Bytes) of package /linux/misc/openstack/karbor-1.3.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 "file_system_bank_plugin.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.0_vs_1.3.0.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    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, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 import errno
   13 import os
   14 
   15 from oslo_config import cfg
   16 from oslo_log import log as logging
   17 from oslo_serialization import jsonutils
   18 from oslo_utils import uuidutils
   19 
   20 from karbor import exception
   21 from karbor.i18n import _
   22 from karbor.services.protection.bank_plugin import BankPlugin
   23 
   24 import six
   25 
   26 file_system_bank_plugin_opts = [
   27     cfg.StrOpt('file_system_bank_path',
   28                help='The file system bank path to use.'),
   29     cfg.StrOpt('bank_object_container',
   30                default='karbor',
   31                help='The file system bank container to use.'),
   32 ]
   33 
   34 LOG = logging.getLogger(__name__)
   35 
   36 
   37 class FileSystemBankPlugin(BankPlugin):
   38     """File system bank plugin"""
   39     def __init__(self, config):
   40         super(FileSystemBankPlugin, self).__init__(config)
   41         self._config.register_opts(file_system_bank_plugin_opts,
   42                                    "file_system_bank_plugin")
   43         plugin_cfg = self._config.file_system_bank_plugin
   44         self.file_system_bank_path = plugin_cfg.file_system_bank_path
   45         self.bank_object_container = plugin_cfg.bank_object_container
   46 
   47         try:
   48             self._create_dir(self.file_system_bank_path)
   49             self.object_container_path = "/".join([self.file_system_bank_path,
   50                                                    self.bank_object_container])
   51             self._create_dir(self.object_container_path)
   52         except OSError as err:
   53             LOG.exception(_("Init file system bank failed. err: %s"), err)
   54 
   55         self.owner_id = uuidutils.generate_uuid()
   56 
   57     def _validate_path(self, path):
   58         if path.find('..') >= 0:
   59             msg = (_("The path(%s) is invalid.") % path)
   60             raise exception.InvalidInput(msg)
   61 
   62     def _create_dir(self, path):
   63         try:
   64             original_umask = None
   65             try:
   66                 original_umask = os.umask(0o022)
   67                 os.makedirs(path)
   68             finally:
   69                 os.umask(original_umask)
   70         except OSError as err:
   71             if err.errno == errno.EEXIST and os.path.isdir(path):
   72                 pass
   73             else:
   74                 LOG.exception(_("Create the directory failed. path: %s"), path)
   75                 raise
   76 
   77     def _write_object(self, path, data):
   78         obj_file_name = None
   79         try:
   80             obj_path = self.object_container_path + path.rsplit('/', 1)[0]
   81             obj_file_name = self.object_container_path + path
   82             self._create_dir(obj_path)
   83             mode = "wb"
   84             if isinstance(data, six.string_types):
   85                 mode = "w"
   86             with open(obj_file_name, mode=mode) as obj_file:
   87                 obj_file.write(data)
   88         except (OSError, IOError):
   89             LOG.exception(_("Write object failed. name: %s"), obj_file_name)
   90             raise
   91 
   92     def _get_object(self, path):
   93         obj_file_name = self.object_container_path + path
   94         if not os.path.isfile(obj_file_name):
   95             LOG.exception(_("Object is not a file. name: %s"), obj_file_name)
   96             raise OSError("Object is not a file")
   97         try:
   98             with open(obj_file_name, mode='r') as obj_file:
   99                 data = obj_file.read()
  100                 return data
  101         except OSError:
  102             LOG.exception(_("Get object failed. name: %s"), obj_file_name)
  103             raise
  104 
  105     def _delete_object(self, path):
  106         obj_path = self.object_container_path + path.rsplit('/', 1)[0]
  107         obj_file_name = self.object_container_path + path
  108         try:
  109             os.remove(obj_file_name)
  110             if not os.listdir(obj_path) and (
  111                     obj_path != self.object_container_path):
  112                 os.rmdir(obj_path)
  113         except OSError:
  114             LOG.exception(_("Delete the object failed. name: %s"),
  115                           obj_file_name)
  116             raise
  117 
  118     def _list_object(self, path):
  119         obj_file_path = self.object_container_path + path
  120         if not os.path.isdir(obj_file_path):
  121             LOG.debug(_("Path is not a directory. name: %s"), obj_file_path)
  122             return ()
  123         try:
  124             file_list = []
  125             for root, sub_dirs, files in os.walk(obj_file_path):
  126                 for file_path in files:
  127                     file_list.append(os.path.join(root, file_path))
  128             return file_list
  129         except OSError:
  130             LOG.exception(_("List the object failed. path: %s"), obj_file_path)
  131             raise
  132 
  133     def get_owner_id(self, context=None):
  134         return self.owner_id
  135 
  136     def update_object(self, key, value, context=None):
  137         LOG.debug("FsBank: update_object. key: %s", key)
  138         self._validate_path(key)
  139         try:
  140             if not isinstance(value, str):
  141                 value = jsonutils.dumps(value)
  142             self._write_object(path=key,
  143                                data=value)
  144         except OSError as err:
  145             LOG.error("Update object failed. err: %s", err)
  146             raise exception.BankUpdateObjectFailed(reason=err,
  147                                                    key=key)
  148 
  149     def delete_object(self, key, context=None):
  150         LOG.debug("FsBank: delete_object. key: %s", key)
  151         self._validate_path(key)
  152         try:
  153             self._delete_object(path=key)
  154         except OSError as err:
  155             LOG.error("Delete object failed. err: %s", err)
  156             raise exception.BankDeleteObjectFailed(reason=err,
  157                                                    key=key)
  158 
  159     def get_object(self, key, context=None):
  160         LOG.debug("FsBank: get_object. key: %s", key)
  161         self._validate_path(key)
  162         try:
  163             data = self._get_object(path=key)
  164         except OSError as err:
  165             LOG.error("Get object failed. err: %s", err)
  166             raise exception.BankGetObjectFailed(reason=err,
  167                                                 key=key)
  168         if isinstance(data, six.string_types):
  169             try:
  170                 data = jsonutils.loads(data)
  171             except ValueError:
  172                 pass
  173         return data
  174 
  175     def list_objects(self, prefix=None, limit=None, marker=None,
  176                      sort_dir=None, context=None):
  177         LOG.debug("FsBank: list_objects. key: %s", prefix)
  178         try:
  179             file_lists = self._list_object(prefix)
  180         except OSError as err:
  181             LOG.error("List objects failed. err: %s", err)
  182             raise exception.BankListObjectsFailed(reason=err)
  183         else:
  184             container_path_length = len(self.object_container_path)
  185             file_lists = [(
  186                 file_name[container_path_length:]) for file_name in file_lists]
  187             return file_lists[-limit:] if limit is not None else file_lists