Exemple #1
0
def docker_client(environment, version=None, context=None, tls_version=None):
    """
    Returns a docker-py client configured using environment variables
    according to the same logic as the official Docker client.
    """
    try:
        kwargs = kwargs_from_env(environment=environment,
                                 ssl_version=tls_version)
    except TLSParameterError:
        raise UserError(
            "TLS configuration is invalid - make sure your DOCKER_TLS_VERIFY "
            "and DOCKER_CERT_PATH are set correctly.\n"
            "You might need to run `eval \"$(docker-machine env default)\"`")

    if not context:
        # check env for DOCKER_HOST and certs path
        host = kwargs.get("base_url", None)
        tls = kwargs.get("tls", None)
        verify = False if not tls else tls.verify
        if host:
            context = Context("compose", host=host, tls=verify)
        else:
            context = ContextAPI.get_current_context()
        if tls:
            context.set_endpoint("docker",
                                 host=host,
                                 tls_cfg=tls,
                                 skip_tls_verify=not verify)

    if not context.is_docker_host():
        raise UserError(
            "The platform targeted with the current context is not supported.\n"
            "Make sure the context in use targets a Docker Engine.\n")

    kwargs['base_url'] = context.Host
    if context.TLSConfig:
        kwargs['tls'] = context.TLSConfig

    if version:
        kwargs['version'] = version

    timeout = environment.get('COMPOSE_HTTP_TIMEOUT')
    if timeout:
        kwargs['timeout'] = int(timeout)
    else:
        kwargs['timeout'] = HTTP_TIMEOUT

    kwargs['user_agent'] = generate_user_agent()

    # Workaround for
    # https://pyinstaller.readthedocs.io/en/v3.3.1/runtime-information.html#ld-library-path-libpath-considerations
    if 'LD_LIBRARY_PATH_ORIG' in environment:
        kwargs['credstore_env'] = {
            'LD_LIBRARY_PATH': environment.get('LD_LIBRARY_PATH_ORIG'),
        }

    client = APIClient(**kwargs)
    client._original_base_url = kwargs.get('base_url')

    return client
Exemple #2
0
 def __init__(self,
              command,
              context_type=None,
              status=Status.SUCCESS,
              source=MetricsSource.CLI,
              uri=None):
     super().__init__()
     self.command = "compose " + command if command else "compose --help"
     self.context = context_type or ContextAPI.get_current_context(
     ).context_type or 'moby'
     self.source = source
     self.status = status.value
     self.uri = uri or os.environ.get("METRICS_SOCKET_FILE",
                                      METRICS_SOCKET_FILE)
     if IS_WINDOWS_PLATFORM:
         self.mount("http+unix://", NpipeHTTPAdapter(self.uri))
     else:
         self.mount("http+unix://", UnixHTTPAdapter(self.uri))