def execute(self, command): """ Pass a command into a host container. Raises a CommandExecError() if the command returns a non-zero return code. :param command: The command to execute. :return: The output from the command with leading and trailing whitespace removed. """ etcd_auth = "ETCD_AUTHORITY=%s:2379" % get_ip() # Export the environment, in case the command has multiple parts, e.g. # use of | or ; command = "export %s; %s" % (etcd_auth, command) if self.dind: command = self.escape_bash_single_quotes(command) command = "docker exec -it %s bash -c '%s'" % (self.name, command) try: output = check_output(command, shell=True, stderr=STDOUT) except CalledProcessError as e: # Wrap the original exception with one that gives a better error # message (including command output). raise CommandExecError(e) else: return output.strip()
def log_and_run(command, raise_exception_on_failure=True): def log_output(results): if results is None: logger.info(" # <no output>") lines = results.split("\n") for line in lines: logger.info(" # %s", line.rstrip()) try: logger.info("%s", command) try: results = check_output(command, shell=True, stderr=STDOUT).rstrip() finally: # Restore terminal settings in case the command we ran manipulated them. Note: # under concurrent access, this is still not a perfect solution since another thread's # child process may break the settings again before we log below. termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, _term_settings) log_output(results) return results except CalledProcessError as e: # Wrap the original exception with one that gives a better error # message (including command output). logger.info(" # Return code: %s", e.returncode) log_output(e.output) if raise_exception_on_failure: raise CommandExecError(e)
def log_and_run(command): try: logger.info(command) return check_output(command, shell=True, stderr=STDOUT).rstrip() except CalledProcessError as e: # Wrap the original exception with one that gives a better error # message (including command output). raise CommandExecError(e)