예제 #1
0
def _PrepareFileBasedStorage(enabled_disk_templates,
                             file_storage_dir,
                             default_dir,
                             file_disk_template,
                             _storage_path_acceptance_fn,
                             init_fn=_InitFileStorageDir,
                             acceptance_fn=None):
    """Checks if a file-base storage type is enabled and inits the dir.

  @type enabled_disk_templates: list of string
  @param enabled_disk_templates: list of enabled disk templates
  @type file_storage_dir: string
  @param file_storage_dir: the file storage directory
  @type default_dir: string
  @param default_dir: default file storage directory when C{file_storage_dir}
      is 'None'
  @type file_disk_template: string
  @param file_disk_template: a disk template whose storage type is 'ST_FILE',
      'ST_SHARED_FILE' or 'ST_GLUSTER'
  @type _storage_path_acceptance_fn: function
  @param _storage_path_acceptance_fn: checks whether the given file-based
      storage directory is acceptable
  @see: C{cluster.CheckFileBasedStoragePathVsEnabledDiskTemplates} for details

  @rtype: string
  @returns: the name of the actual file storage directory

  """
    assert (file_disk_template in utils.storage.GetDiskTemplatesOfStorageTypes(
        constants.ST_FILE, constants.ST_SHARED_FILE, constants.ST_GLUSTER))

    file_storage_enabled = file_disk_template in enabled_disk_templates

    if file_storage_dir is None:
        if file_storage_enabled:
            file_storage_dir = default_dir
        else:
            file_storage_dir = ""

    if not acceptance_fn:
        acceptance_fn = \
            lambda path: filestorage.CheckFileStoragePathAcceptance(
                path, exact_match_ok=True)

    _storage_path_acceptance_fn(logging.warning, file_storage_dir,
                                enabled_disk_templates)

    if file_storage_enabled:
        try:
            acceptance_fn(file_storage_dir)
        except errors.FileStoragePathError as e:
            raise errors.OpPrereqError(str(e))
        result_file_storage_dir = init_fn(file_storage_dir)
    else:
        result_file_storage_dir = file_storage_dir
    return result_file_storage_dir
  def testAllowedPath(self):
    tmpfile = self._CreateTempFile()

    utils.WriteFile(tmpfile, data="""
      /srv/storage
      """)

    filestorage.CheckFileStoragePathAcceptance(
        "/srv/storage/inst1", _filename=tmpfile)

    # No additional path component
    self.assertRaises(errors.FileStoragePathError,
                      filestorage.CheckFileStoragePathAcceptance,
                      "/srv/storage", _filename=tmpfile)

    # Forbidden path
    self.assertRaises(errors.FileStoragePathError,
                      filestorage.CheckFileStoragePathAcceptance,
                      "/usr/lib64/xyz", _filename=tmpfile)