-
Notifications
You must be signed in to change notification settings - Fork 107
/
util.go
65 lines (57 loc) · 1.62 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"errors"
"os"
"path"
"github.com/fsouza/go-dockerclient"
)
func connect() (*docker.Client, error) {
// grab directly from docker daemon
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
endpoint = "unix:///var/run/docker.sock"
}
var client *docker.Client
var err error
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")
}
} else {
client, err = docker.NewClient(endpoint)
if err != nil {
return nil, err
}
}
return client, nil
}
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
}