def _run_execute(
         self,
         cmd: str,
         timeout: Optional[float] = None,  # pylint: disable=too-many-arguments
         ignore_status: bool = False,
         verbose: bool = True,
         new_session: bool = False,
         watchers: Optional[List[StreamWatcher]] = None):
     # TODO: This should be removed than sudo calls will be done in more organized way.
     tmp = cmd.split(maxsplit=3)
     if tmp[0] == 'sudo':
         deprecation(
             "Using `sudo' in cmd string is deprecated.  Use `remoter.sudo()' instead."
         )
         frame = inspect.stack()[1]
         self.log.error(
             "Cut off `sudo' from the cmd string: %s (%s:%s: %s)", cmd,
             frame.filename, frame.lineno, frame.code_context[0].rstrip())
         if tmp[1] == '-u':
             cmd = tmp[3]
         else:
             cmd = cmd[cmd.find('sudo') + 5:]
     # Session should be created for each run
     return super()._run_execute(cmd,
                                 timeout=timeout,
                                 ignore_status=ignore_status,
                                 verbose=verbose,
                                 new_session=True,
                                 watchers=watchers)
示例#2
0
    def __call__(self, cmd, timeout=10):
        deprecation("consider to use Docker Python module instead of using Docker CLI commands")

        res = LOCALRUNNER.run('docker {}'.format(cmd), ignore_status=True, timeout=timeout)
        if res.exit_status:
            if 'No such container:' in res.stderr:
                raise NotFound(res.stderr)
            raise DockerException('command: {}, error: {}, output: {}'.format(cmd, res.stderr, res.stdout))
        return res
    def run(self,
            cmd,
            timeout=300,
            ignore_status=False,
            verbose=True,
            new_session=False,
            log_file=None,
            retry=1,
            watchers=None):
        watchers = self._setup_watchers(verbose=verbose,
                                        log_file=log_file,
                                        additional_watchers=watchers)

        # TODO: This should be removed than sudo calls will be done in more organized way.
        if cmd.startswith("sudo "):
            deprecation(
                "Using `sudo' in cmd string is deprecated.  Use `remoter.sudo()' instead."
            )
            frame = inspect.stack()[1]
            self.log.error(
                "Cut off `sudo' from the cmd string: %s (%s:%s: %s)", cmd,
                frame.filename, frame.lineno, frame.code_context[0].rstrip())
            cmd = cmd[5:]

        @retrying(n=retry or 1)
        def _run():
            start_time = time.perf_counter()
            if verbose:
                self.log.debug('Running command "{}"...'.format(cmd))
            try:
                connection = self._create_connection(
                ) if new_session else self.connection
                res = connection.run(command=cmd,
                                     warn=ignore_status,
                                     hide=True,
                                     watchers=watchers,
                                     timeout=timeout)
                res.duration = time.perf_counter() - start_time
                res.exit_status = res.exited
                return res
            except (Failure, UnexpectedExit) as details:
                if hasattr(details, "result"):
                    self._print_command_results(details.result, verbose,
                                                ignore_status)
                raise

        result = _run()
        self._print_command_results(result, verbose, ignore_status)

        return result