Ejemplo n.º 1
0
 def push_file(self, source: str, destination: str):
     cmd = [
         "lxc",
         "file",
         "push",
         source,
         "{}{}".format(self._instance_name, destination),
     ]
     try:
         _run(cmd)
     except subprocess.CalledProcessError as process_error:
         raise _provider_errors.ProviderFileCopyError(
             provider_name="lxd",
             exit_code=process_error.returncode) from process_error
Ejemplo n.º 2
0
    def copy_files(self, *, source: str, destination: str) -> None:
        """Passthrough for running multipass copy-files.

        :param str source: the source file to copy, using syntax expected
                           by multipass.
        :param str destination: the destination of the copied file, using
                                syntax expected by multipass.
        """
        cmd = [self.provider_cmd, "copy-files", source, destination]
        try:
            _run(cmd)
        except subprocess.CalledProcessError as process_error:
            raise errors.ProviderFileCopyError(
                provider_name=self.provider_name, exit_code=process_error.returncode
            ) from process_error
Ejemplo n.º 3
0
    def pull_file(self,
                  *,
                  source: str,
                  destination: IO,
                  bufsize: int = 1024) -> None:
        """Passthrough for pulling a file through `multipass transfer`

        :param str or IO source: the source file to copy, using syntax expected
                                 by multipass
        :param str or IO destination: a file-like object to write to
        """
        assert isinstance(destination, io.IOBase)

        # can't use std{in,out}=open(...) due to LP#1849753
        p = _popen(
            [self.provider_cmd, "transfer", source, "-"],
            stdin=subprocess.DEVNULL,
            stdout=subprocess.PIPE,
        )

        while True:
            written = p.stdout.read(bufsize)
            if written:
                destination.write(written)
            if len(written) < bufsize:
                logger.debug("Finished streaming standard output")
                break

        while True:
            try:
                out, err = p.communicate(timeout=1)
            except subprocess.TimeoutExpired:
                pass
            else:
                if out:
                    destination.write(out)

                if p.returncode == 0:
                    logger.debug("Process completed")
                    break

                elif p.returncode is not None:
                    raise errors.ProviderFileCopyError(
                        provider_name=self.provider_name,
                        exit_code=p.returncode)
Ejemplo n.º 4
0
    def push_file(self,
                  *,
                  source: IO,
                  destination: str,
                  bufsize: int = 1024) -> None:
        """Passthrough for pushing a file through `multipass transfer`.

        :param IO source: a file-like object to read from
        :param str destination: the destination of the copied file, using syntax
                                expected by multipass
        """
        assert isinstance(source, io.IOBase)

        # can't use std{in,out}=open(...) due to LP#1849753
        p = _popen([self.provider_cmd, "transfer", "-", destination],
                   stdin=subprocess.PIPE)

        while True:
            read = source.read(bufsize)
            if read:
                p.stdin.write(read)
            if len(read) < bufsize:
                logger.debug("Finished streaming source file")
                break

        while True:
            try:
                out, err = p.communicate(timeout=1)
            except subprocess.TimeoutExpired:
                pass
            else:
                if p.returncode == 0:
                    logger.debug("Process completed")
                    break

                elif p.returncode is not None:
                    raise errors.ProviderFileCopyError(
                        provider_name=self.provider_name,
                        exit_code=p.returncode)