original_docstring: docker-py has issues running a container with a TTY attached, so for
      consistency we execute 'docker run' directly in both interactive and
      non-interactive modes.
  
      In non-interactive mode when the run is complete, stop the container and
      remove it (with docker-py).
      
docstring: docker-py has issues running a container with a TTY attached, so for
  consistency we execute 'docker run' directly in both interactive and
  non-interactive modes.
  
  In non-interactive mode when the run is complete, stop the container and
  remove it (with docker-py).
docstring_tokens: docker - py has issues running a container with a TTY attached so for consistency we execute ' docker run ' directly in both interactive and non - interactive modes . In non - interactive mode when the run is complete stop the container and remove it ( with docker - py ) .
code: def run_docker_container(
      docker_client,
      service,
      instance,
      docker_hash,
      volumes,
      interactive,
      command,
      healthcheck,
      healthcheck_only,
      user_port,
      instance_config,
      soa_dir=DEFAULT_SOA_DIR,
      dry_run=False,
      json_dict=False,
      framework=None,
  ):
      """docker-py has issues running a container with a TTY attached, so for
      consistency we execute 'docker run' directly in both interactive and
      non-interactive modes.
  
      In non-interactive mode when the run is complete, stop the container and
      remove it (with docker-py).
      """
      if user_port:
          if check_if_port_free(user_port):
              chosen_port = user_port
          else:
              paasta_print(
                  PaastaColors.red(
                      "The chosen port is already in use!\n"
                      "Try specifying another one, or omit (--port|-o) and paasta will find a free one for you",
                  ),
                  file=sys.stderr,
              )
              sys.exit(1)
      else:
          chosen_port = pick_random_port(service)
      environment = instance_config.get_env_dictionary()
      local_run_environment = get_local_run_environment_vars(
          instance_config=instance_config,
          port0=chosen_port,
          framework=framework,
      )
      environment.update(local_run_environment)
      net = instance_config.get_net()
      memory = instance_config.get_mem()
      container_name = get_container_name()
      docker_params = instance_config.format_docker_parameters()
  
      healthcheck_mode, healthcheck_data = get_healthcheck_for_instance(
          service, instance, instance_config, chosen_port, soa_dir=soa_dir,
      )
      if healthcheck_mode is None:
          container_port = None
          interactive = True
      elif not user_port and not healthcheck and not healthcheck_only:
          container_port = None
      else:
          try:
              container_port = instance_config.get_container_port()
          except AttributeError:
              container_port = None
  
      docker_run_args = dict(
          memory=memory,
          chosen_port=chosen_port,
          container_port=container_port,
          container_name=container_name,
          volumes=volumes,
          env=environment,
          interactive=interactive,
          docker_hash=docker_hash,
          command=command,
          net=net,
          docker_params=docker_params,
      )
      docker_run_cmd = get_docker_run_cmd(**docker_run_args)
      joined_docker_run_cmd = ' '.join(docker_run_cmd)
  
      if dry_run:
          if json_dict:
              paasta_print(json.dumps(docker_run_args))
          else:
              paasta_print(json.dumps(docker_run_cmd))
          return 0
      else:
          paasta_print('Running docker command:\n%s' % PaastaColors.grey(joined_docker_run_cmd))
  
      if interactive:
          # NOTE: This immediately replaces us with the docker run cmd. Docker
          # run knows how to clean up the running container in this situation.
          execlp('paasta_docker_wrapper', *docker_run_cmd)
          # For testing, when execlp is patched out and doesn't replace us, we
          # still want to bail out.
          return 0
  
      container_started = False
      container_id = None
      try:
          (returncode, output) = _run(docker_run_cmd)
          if returncode != 0:
              paasta_print(
                  'Failure trying to start your container!'
                  'Returncode: %d'
                  'Output:'
                  '%s'
                  ''
                  'Fix that problem and try again.'
                  'http://y/paasta-troubleshooting'
                  % (returncode, output),
                  sep='\n',
              )
              # Container failed to start so no need to cleanup; just bail.
              sys.exit(1)
          container_started = True
          container_id = get_container_id(docker_client, container_name)
          paasta_print('Found our container running with CID %s' % container_id)
  
          # If the service has a healthcheck, simulate it
          if (healthcheck_only or healthcheck) and healthcheck_mode is not None:
              healthcheck_result = simulate_healthcheck_on_service(
                  instance_config=instance_config,
                  docker_client=docker_client,
                  container_id=container_id,
                  healthcheck_mode=healthcheck_mode,
                  healthcheck_data=healthcheck_data,
                  healthcheck_enabled=healthcheck,
              )
  
          def _output_stdout_and_exit_code():
              returncode = docker_client.inspect_container(container_id)['State']['ExitCode']
              paasta_print('Container exited: %d)' % returncode)
              paasta_print('Here is the stdout and stderr:\n\n')
              paasta_print(
                  docker_client.attach(container_id, stderr=True, stream=False, logs=True),
              )
  
          if healthcheck_only:
              if container_started:
                  _output_stdout_and_exit_code()
                  _cleanup_container(docker_client, container_id)
              if healthcheck_mode is None:
                  paasta_print('--healthcheck-only, but no healthcheck is defined for this instance!')
                  sys.exit(1)
              elif healthcheck_result is True:
                  sys.exit(0)
              else:
                  sys.exit(1)
  
          running = docker_client.inspect_container(container_id)['State']['Running']
          if running:
              paasta_print('Your service is now running! Tailing stdout and stderr:')
              for line in docker_client.attach(container_id, stderr=True, stream=True, logs=True):
                  paasta_print(line)
          else:
              _output_stdout_and_exit_code()
              returncode = 3
  
      except KeyboardInterrupt:
          returncode = 3
  
      # Cleanup if the container exits on its own or interrupted.
      if container_started:
          returncode = docker_client.inspect_container(container_id)['State']['ExitCode']
          _cleanup_container(docker_client, container_id)
      return returncode
code_tokens: def run_docker_container ( docker_client , service , instance , docker_hash , volumes , interactive , command , healthcheck , healthcheck_only , user_port , instance_config , soa_dir = DEFAULT_SOA_DIR , dry_run = False , json_dict = False , framework = None , ) : if user_port : if check_if_port_free ( user_port ) : chosen_port = user_port else : paasta_print ( PaastaColors . red ( "The chosen port is already in use!\n" "Try specifying another one, or omit (--port|-o) and paasta will find a free one for you" , ) , file = sys . stderr , ) sys . exit ( 1 ) else : chosen_port = pick_random_port ( service ) environment = instance_config . get_env_dictionary ( ) local_run_environment = get_local_run_environment_vars ( instance_config = instance_config , port0 = chosen_port , framework = framework , ) environment . update ( local_run_environment ) net = instance_config . get_net ( ) memory = instance_config . get_mem ( ) container_name = get_container_name ( ) docker_params = instance_config . format_docker_parameters ( ) healthcheck_mode , healthcheck_data = get_healthcheck_for_instance ( service , instance , instance_config , chosen_port , soa_dir = soa_dir , ) if healthcheck_mode is None : container_port = None interactive = True elif not user_port and not healthcheck and not healthcheck_only : container_port = None else : try : container_port = instance_config . get_container_port ( ) except AttributeError : container_port = None docker_run_args = dict ( memory = memory , chosen_port = chosen_port , container_port = container_port , container_name = container_name , volumes = volumes , env = environment , interactive = interactive , docker_hash = docker_hash , command = command , net = net , docker_params = docker_params , ) docker_run_cmd = get_docker_run_cmd ( ** docker_run_args ) joined_docker_run_cmd = ' ' . join ( docker_run_cmd ) if dry_run : if json_dict : paasta_print ( json . dumps ( docker_run_args ) ) else : paasta_print ( json . dumps ( docker_run_cmd ) ) return 0 else : paasta_print ( 'Running docker command:\n%s' % PaastaColors . grey ( joined_docker_run_cmd ) ) if interactive : execlp ( 'paasta_docker_wrapper' , * docker_run_cmd ) return 0 container_started = False container_id = None try : ( returncode , output ) = _run ( docker_run_cmd ) if returncode != 0 : paasta_print ( 'Failure trying to start your container!' 'Returncode: %d' 'Output:' '%s' '' 'Fix that problem and try again.' 'http://y/paasta-troubleshooting' % ( returncode , output ) , sep = '\n' , ) sys . exit ( 1 ) container_started = True container_id = get_container_id ( docker_client , container_name ) paasta_print ( 'Found our container running with CID %s' % container_id ) if ( healthcheck_only or healthcheck ) and healthcheck_mode is not None : healthcheck_result = simulate_healthcheck_on_service ( instance_config = instance_config , docker_client = docker_client , container_id = container_id , healthcheck_mode = healthcheck_mode , healthcheck_data = healthcheck_data , healthcheck_enabled = healthcheck , ) def _output_stdout_and_exit_code ( ) : returncode = docker_client . inspect_container ( container_id ) [ 'State' ] [ 'ExitCode' ] paasta_print ( 'Container exited: %d)' % returncode ) paasta_print ( 'Here is the stdout and stderr:\n\n' ) paasta_print ( docker_client . attach ( container_id , stderr = True , stream = False , logs = True ) , ) if healthcheck_only : if container_started : _output_stdout_and_exit_code ( ) _cleanup_container ( docker_client , container_id ) if healthcheck_mode is None : paasta_print ( '--healthcheck-only, but no healthcheck is defined for this instance!' ) sys . exit ( 1 ) elif healthcheck_result is True : sys . exit ( 0 ) else : sys . exit ( 1 ) running = docker_client . inspect_container ( container_id ) [ 'State' ] [ 'Running' ] if running : paasta_print ( 'Your service is now running! Tailing stdout and stderr:' ) for line in docker_client . attach ( container_id , stderr = True , stream = True , logs = True ) : paasta_print ( line ) else : _output_stdout_and_exit_code ( ) returncode = 3 except KeyboardInterrupt : returncode = 3 if container_started : returncode = docker_client . inspect_container ( container_id ) [ 'State' ] [ 'ExitCode' ] _cleanup_container ( docker_client , container_id ) return returncode
short_docstring: docker-py has issues running a container with a TTY attached, so for
  consistency we execute 'docker run' directly in both interactive and
  non-interactive modes.
short_docstring_tokens: docker - py has issues running a container with a TTY attached so for consistency we execute ' docker run ' directly in both interactive and non - interactive modes .
comment: """docker-py has issues running a container with a TTY attached, so for
      consistency we execute 'docker run' directly in both interactive and
      non-interactive modes.
  
      In non-interactive mode when the run is complete, stop the container and
      remove it (with docker-py).
      """$SEP$# NOTE: This immediately replaces us with the docker run cmd. Docker$SEP$# run knows how to clean up the running container in this situation.$SEP$# For testing, when execlp is patched out and doesn't replace us, we$SEP$# still want to bail out.$SEP$# Container failed to start so no need to cleanup; just bail.$SEP$# If the service has a healthcheck, simulate it$SEP$# Cleanup if the container exits on its own or interrupted.