github.com/MOXA-ISD/edge-library-libcompose@v0.4.1-0.20200417083957-c90441e63650/docker/client/client.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  
    10  	cliconfig "github.com/docker/cli/cli/config"
    11  	dockerclient "github.com/docker/docker/client"
    12  	"github.com/docker/docker/pkg/homedir"
    13  	"github.com/docker/go-connections/sockets"
    14  	"github.com/docker/go-connections/tlsconfig"
    15  	"github.com/docker/libcompose/version"
    16  )
    17  
    18  const (
    19  	// DefaultAPIVersion is the default docker API version set by libcompose
    20  	DefaultAPIVersion   = "v1.20"
    21  	defaultTrustKeyFile = "key.json"
    22  	defaultCaFile       = "ca.pem"
    23  	defaultKeyFile      = "key.pem"
    24  	defaultCertFile     = "cert.pem"
    25  )
    26  
    27  var (
    28  	dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
    29  )
    30  
    31  func init() {
    32  	if dockerCertPath == "" {
    33  		dockerCertPath = cliconfig.Dir()
    34  	}
    35  }
    36  
    37  // Options holds docker client options (host, tls, ..)
    38  type Options struct {
    39  	TLS        bool
    40  	TLSVerify  bool
    41  	TLSOptions tlsconfig.Options
    42  	TrustKey   string
    43  	Host       string
    44  	APIVersion string
    45  }
    46  
    47  var singleton *dockerclient.Client
    48  
    49  // Create creates a docker client based on the specified options.
    50  func Create(c Options) (dockerclient.APIClient, error) {
    51  	if singleton != nil {
    52  		return singleton, nil
    53  	}
    54  	if c.Host == "" {
    55  		if os.Getenv("DOCKER_API_VERSION") == "" {
    56  			os.Setenv("DOCKER_API_VERSION", DefaultAPIVersion)
    57  		}
    58  		myclient, err := dockerclient.NewEnvClient()
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  		singleton = myclient
    63  		return myclient, nil
    64  	}
    65  
    66  	apiVersion := c.APIVersion
    67  	if apiVersion == "" {
    68  		apiVersion = DefaultAPIVersion
    69  	}
    70  
    71  	if c.TLSOptions.CAFile == "" {
    72  		c.TLSOptions.CAFile = filepath.Join(dockerCertPath, defaultCaFile)
    73  	}
    74  	if c.TLSOptions.CertFile == "" {
    75  		c.TLSOptions.CertFile = filepath.Join(dockerCertPath, defaultCertFile)
    76  	}
    77  	if c.TLSOptions.KeyFile == "" {
    78  		c.TLSOptions.KeyFile = filepath.Join(dockerCertPath, defaultKeyFile)
    79  	}
    80  	if c.TrustKey == "" {
    81  		c.TrustKey = filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
    82  	}
    83  	if c.TLSVerify {
    84  		c.TLS = true
    85  	}
    86  	if c.TLS {
    87  		c.TLSOptions.InsecureSkipVerify = !c.TLSVerify
    88  	}
    89  
    90  	var httpClient *http.Client
    91  	if c.TLS {
    92  		config, err := tlsconfig.Client(c.TLSOptions)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		tr := &http.Transport{
    97  			TLSClientConfig: config,
    98  		}
    99  		clientURL, err := dockerclient.ParseHostURL(c.Host)
   100  		if err != nil {
   101  			return nil, err
   102  		}
   103  
   104  		if err := sockets.ConfigureTransport(tr, clientURL.Scheme, clientURL.Host); err != nil {
   105  			return nil, err
   106  		}
   107  
   108  		httpClient = &http.Client{
   109  			Transport: tr,
   110  		}
   111  	}
   112  
   113  	customHeaders := map[string]string{}
   114  	customHeaders["User-Agent"] = fmt.Sprintf("Libcompose-Client/%s (%s)", version.VERSION, runtime.GOOS)
   115  
   116  	myclient, err := dockerclient.NewClient(c.Host, apiVersion, httpClient, customHeaders)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	singleton = myclient
   121  	return myclient, nil
   122  }