def eval_command(self, args): """Start a previously set up exec instance""" try: detach = args["detach"] del args["detach"] dockerpty.start_exec(self.client, self.settings["exec_create"], interactive=args["interactive"]) except APIError as e: raise e
def ssh(self, container_name): # get command and options container = self.find_container(name=container_name) if container: # there is such active container self.logger.debug('Run /bin/bash into container.') # create new exec exec_id = container.create_exec('/bin/bash', stdin=True, stdout=True, tty=True) # check PTY for more information, this is actually passing TTY around, and dockerpty helps much dockerpty.start_exec(container.client, exec_id) else: self.logger.debug('There is no active container.')
def exec_command(self): first_container_name, hostname = self.get_ecs_hostname_of_task() if self.container is None: container = first_container_name else: container = self.container docker_url = '%s:%d' % (hostname, self.port) client = docker.APIClient( docker_url, version=self.api_version, ) container_id = self.find_container_id(client, container) resp = client.exec_create( container_id, self.command, stdin=self.stdin, tty=self.tty ) exec_id = resp['Id'] dockerpty.start_exec(client, exec_id, interactive=self.stdin)
def __call__(self, container, command, detach, stdin, tty, user): """ :param container(str): Target container where exec instance will be created :param command(str or list): Command to be executed :param detach(bool): If true, detach from the exec command :param stdin(bool): Keep stdin open even if not attached :param tty(bool): Allocate a pseudo-TTY :param user(str): User to execute command as """ cli = self.swarm.client if cli is not None: try: if stdin and tty: ret = cli.exec_create(container, command, stdin=True, stdout=True, stderr=True, tty=True, user=user) dockerpty.start_exec(cli, ret['Id']) else: ret = cli.exec_create(container, command, user=user) for line in cli.exec_start(ret['Id'], detach=detach, stream=True): print(line.strip()) except (errors.NotFound, errors.APIError, errors.DockerException) as e: pyprint(e.explanation) finally: cli.close()