github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/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  	"github.com/docker/docker/cliconfig"
    11  	"github.com/docker/docker/pkg/homedir"
    12  	"github.com/docker/engine-api/client"
    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.ConfigDir()
    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  // Create creates a docker client based on the specified options.
    48  func Create(c Options) (client.APIClient, error) {
    49  	if c.Host == "" {
    50  		if os.Getenv("DOCKER_API_VERSION") == "" {
    51  			os.Setenv("DOCKER_API_VERSION", DefaultAPIVersion)
    52  		}
    53  		client, err := client.NewEnvClient()
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  		return client, nil
    58  	}
    59  
    60  	apiVersion := c.APIVersion
    61  	if apiVersion == "" {
    62  		apiVersion = DefaultAPIVersion
    63  	}
    64  
    65  	if c.TLSOptions.CAFile == "" {
    66  		c.TLSOptions.CAFile = filepath.Join(dockerCertPath, defaultCaFile)
    67  	}
    68  	if c.TLSOptions.CertFile == "" {
    69  		c.TLSOptions.CertFile = filepath.Join(dockerCertPath, defaultCertFile)
    70  	}
    71  	if c.TLSOptions.KeyFile == "" {
    72  		c.TLSOptions.KeyFile = filepath.Join(dockerCertPath, defaultKeyFile)
    73  	}
    74  	if c.TrustKey == "" {
    75  		c.TrustKey = filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
    76  	}
    77  	if c.TLSVerify {
    78  		c.TLS = true
    79  	}
    80  	if c.TLS {
    81  		c.TLSOptions.InsecureSkipVerify = !c.TLSVerify
    82  	}
    83  
    84  	var httpClient *http.Client
    85  	if c.TLS {
    86  		config, err := tlsconfig.Client(c.TLSOptions)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		tr := &http.Transport{
    91  			TLSClientConfig: config,
    92  		}
    93  		proto, addr, _, err := client.ParseHost(c.Host)
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  
    98  		if err := sockets.ConfigureTransport(tr, proto, addr); err != nil {
    99  			return nil, err
   100  		}
   101  
   102  		httpClient = &http.Client{
   103  			Transport: tr,
   104  		}
   105  	}
   106  
   107  	customHeaders := map[string]string{}
   108  	customHeaders["User-Agent"] = fmt.Sprintf("Libcompose-Client/%s (%s)", version.VERSION, runtime.GOOS)
   109  
   110  	client, err := client.NewClient(c.Host, apiVersion, httpClient, customHeaders)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	return client, nil
   115  }