Ejemplo n.º 1
0
    def execute(self,
                *,
                command: Sequence[str],
                instance_name: str,
                hide_output: bool = False) -> Optional[bytes]:
        """Passthrough for running multipass exec.

        :param list command: the command to exectute on the instance.
        :param str instance_name: the name of the instance to execute command.
        """
        cmd = [self.provider_cmd, "exec", instance_name, "--"] + list(command)
        output = None
        try:
            if hide_output:
                output = _run_output(cmd)
            else:
                _run(cmd, stdin=None)
        except subprocess.CalledProcessError as process_error:
            raise errors.ProviderExecError(
                provider_name=self.provider_name,
                command=command,
                exit_code=process_error.returncode,
            ) from process_error

        return output
Ejemplo n.º 2
0
    def execute(self, *, command: List[str], instance_name: str) -> None:
        """Passthrough for running multipass exec.

        :param list command: the command to exectute on the instance.
        :param str instance_name: the name of the instance to execute command.
        """
        cmd = [self.provider_cmd, 'exec', instance_name, '--'] + command
        try:
            _run(cmd)
        except subprocess.CalledProcessError as process_error:
            raise errors.ProviderExecError(
                provider_name=self.provider_name,
                command=command,
                exit_code=process_error.returncode) from process_error
Ejemplo n.º 3
0
 def run(self, command: Sequence[str], hide_output: bool = False):
     cmd = ["lxc", "exec", self._instance_name, "--"] + list(command)
     if hide_output:
         runnable = (
             _run_output
         )  # type: Callable[[Sequence[str]], Union[bytes, None]]
     else:
         runnable = _run
     try:
         runnable(cmd)
     except subprocess.CalledProcessError as process_error:
         raise _provider_errors.ProviderExecError(
             provider_name="lxd",
             command=command,
             exit_code=process_error.returncode) from process_error
    def execute(
        self, *, command: List[str], instance_name: str, hide_output: bool = False
    ) -> None:
        """Passthrough for running multipass exec.

        :param list command: the command to exectute on the instance.
        :param str instance_name: the name of the instance to execute command.
        """
        cmd = [self.provider_cmd, "exec", instance_name, "--"] + command
        if hide_output:
            runnable = _run_output  # type: Callable[[List[Any]], Union[bytes, None]]
        else:
            runnable = _run
        try:
            runnable(cmd)
        except subprocess.CalledProcessError as process_error:
            raise errors.ProviderExecError(
                provider_name=self.provider_name,
                command=command,
                exit_code=process_error.returncode,
            ) from process_error
Ejemplo n.º 5
0
    def execute(self, *, command: List[str]) -> None:
        # Properly quote and join the command
        command_string = " ".join([shlex.quote(c) for c in command])

        channel = self._ssh_client.get_transport().open_session()
        channel.get_pty()
        channel.exec_command(command_string)
        channel.settimeout(0.0)

        exit_code = None
        while exit_code is None:
            if channel.recv_ready():
                sys.stdout.write(channel.recv(1024).decode())
                sys.stdout.flush()
            if channel.exit_status_ready():
                exit_code = channel.recv_exit_status()
                logger.debug("Command {!r} returned exit code: {}".format(
                    command_string, exit_code))

        if exit_code != 0:
            raise errors.ProviderExecError(provider_name=self.provider_name,
                                           command=command,
                                           exit_code=exit_code)