2015-05-21 21:48:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-21 23:33:45 +02:00
|
|
|
"errors"
|
2015-05-21 21:48:06 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2015-05-21 23:33:45 +02:00
|
|
|
|
|
|
|
"github.com/fsouza/go-dockerclient"
|
2015-05-21 21:48:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func connect() (*docker.Client, error) {
|
|
|
|
|
|
|
|
// grab directly from docker daemon
|
2015-05-21 23:42:53 +02:00
|
|
|
var endpoint string
|
|
|
|
if env_endpoint := os.Getenv("DOCKER_HOST"); len(env_endpoint) > 0 {
|
|
|
|
endpoint = env_endpoint
|
|
|
|
} else if len(globalOptions.Host) > 0 {
|
|
|
|
endpoint = globalOptions.Host
|
|
|
|
} else {
|
|
|
|
// assume local socket
|
2015-05-21 21:48:06 +02:00
|
|
|
endpoint = "unix:///var/run/docker.sock"
|
|
|
|
}
|
|
|
|
|
|
|
|
var client *docker.Client
|
|
|
|
var err error
|
2015-05-21 23:33:45 +02:00
|
|
|
dockerTlsVerifyEnv := os.Getenv("DOCKER_TLS_VERIFY")
|
|
|
|
if dockerTlsVerifyEnv == "1" || globalOptions.TLSVerify {
|
|
|
|
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); len(dockerCertPath) > 0 {
|
|
|
|
cert := path.Join(dockerCertPath, "cert.pem")
|
|
|
|
key := path.Join(dockerCertPath, "key.pem")
|
|
|
|
ca := path.Join(dockerCertPath, "ca.pem")
|
|
|
|
client, err = docker.NewTLSClient(endpoint, cert, key, ca)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if len(globalOptions.TLSCert) > 0 && len(globalOptions.TLSKey) > 0 && len(globalOptions.TLSCaCert) > 0 {
|
|
|
|
client, err = docker.NewTLSClient(endpoint, globalOptions.TLSCert, globalOptions.TLSKey, globalOptions.TLSCaCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("TLS Verification requested but certs not specified")
|
2015-05-21 21:48:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
client, err = docker.NewClient(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
2016-05-01 22:53:06 +02:00
|
|
|
|
|
|
|
func getAPIVersion(client *docker.Client) ([]int, error) {
|
|
|
|
env, err := client.Version()
|
|
|
|
if err != nil {
|
|
|
|
return []int{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ver, err := docker.NewAPIVersion(env.Get("ApiVersion"))
|
|
|
|
if err != nil {
|
|
|
|
return []int{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ver, nil
|
|
|
|
}
|