コード例 #1
0
  def CreateFile(cls, path, size, create_folders=False,
                 _file_path_acceptance_fn=None):
    """Create a new file and its file device helper.

    @param size: the size in MiBs the file should be truncated to.
    @param create_folders: create the directories for the path if necessary
                           (using L{ganeti.utils.io.Makedirs})

    @rtype: FileDeviceHelper
    @return: The FileDeviceHelper object representing the object.
    @raise errors.FileStoragePathError: if the file path is disallowed by policy

    """

    if not _file_path_acceptance_fn:
      _file_path_acceptance_fn = CheckFileStoragePathAcceptance
    _file_path_acceptance_fn(path)

    if create_folders:
      folder = os.path.dirname(path)
      io.Makedirs(folder)

    try:
      fd = os.open(path, os.O_RDWR | os.O_CREAT | os.O_EXCL)
      f = os.fdopen(fd, "w")
      f.truncate(size * 1024 * 1024)
      f.close()
    except EnvironmentError as err:
      base.ThrowError("%s: can't create: %s", path, str(err))

    return FileDeviceHelper(path,
                            _file_path_acceptance_fn=_file_path_acceptance_fn)
コード例 #2
0
    def Mount(self):
        """Try and mount the volume. No-op if the volume is already mounted.

    @raises BlockDeviceError: if the mount was unsuccessful

    @rtype: context manager
    @return: A simple context manager that lets you use this volume for
             short lived operations like so::

              with volume.mount():
                # Do operations on volume
              # Volume is now unmounted

    """
        class _GlusterVolumeContextManager(object):
            def __init__(self, volume):
                self.volume = volume

            def __enter__(self):
                # We're already mounted.
                return self

            def __exit__(self, *exception_information):
                self.volume.Unmount()
                return False  # do not swallow exceptions.

        if self._IsMounted():
            return _GlusterVolumeContextManager(self)

        command = [
            "mount", "-t", "glusterfs",
            self._GetFUSEMountString(), self.mount_point
        ]

        io.Makedirs(self.mount_point)
        self._run_cmd(
            " ".join(command),
            # Why set cwd? Because it's an area we control. If,
            # for some unfortunate reason, this folder exists:
            #   "/%s/" % _GetFUSEMountString()
            # ...then the gluster parser gets confused and treats
            # _GetFUSEMountString() as your mount point and
            # self.mount_point becomes a syntax error.
            cwd=self.mount_point)

        # mount.glusterfs exits with code 0 even after failure.
        # https://bugzilla.redhat.com/show_bug.cgi?id=1031973
        if not self._IsMounted():
            reasons = self._GuessMountFailReasons()
            if not reasons:
                reasons = "%r failed." % (" ".join(command))
            base.ThrowError("%r: mount failure: %s", self.mount_point, reasons)

        return _GlusterVolumeContextManager(self)