forked from ehazlett/shipyard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
92 lines (78 loc) · 2.18 KB
/
engine.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package shipyard
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/citadel/citadel"
)
const (
httpTimeout = time.Duration(1 * time.Second)
)
type (
Health struct {
Status string `json:"status,omitempty" gorethink:"status,omitempty"`
ResponseTime int64 `json:"response_time,omitempty" gorethink:"response_time,omitempty"`
}
Engine struct {
ID string `json:"id,omitempty" gorethink:"id,omitempty"`
SSLCertificate string `json:"ssl_cert,omitempty" gorethink:"ssl_cert,omitempty"`
SSLKey string `json:"ssl_key,omitempty" gorethink:"ssl_key,omitempty"`
CACertificate string `json:"ca_cert,omitempty" gorethink:"ca_cert,omitempty"`
Engine *citadel.Engine `json:"engine,omitempty" gorethink:"engine,omitempty"`
Health *Health `json:"health,omitempty" gorethink:"health,omitempty"`
DockerVersion string `json:"docker_version,omitempty"`
}
)
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, httpTimeout)
}
func (e *Engine) Certificate() (*tls.Certificate, error) {
if e.SSLCertificate == "" {
return nil, nil
}
cert, err := tls.X509KeyPair([]byte(e.SSLCertificate), []byte(e.SSLKey))
return &cert, err
}
func (e *Engine) Ping() (int, error) {
status := 0
addr := e.Engine.Addr
tlsConfig := &tls.Config{}
// check for https
if strings.Index(addr, "https") != -1 {
cert, err := e.Certificate()
if err != nil {
return 0, err
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{*cert},
}
// use custom ca cert if specified
if e.CACertificate != "" {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(e.CACertificate))
tlsConfig.RootCAs = caCertPool
}
}
// allow insecure
tlsConfig.InsecureSkipVerify = true
transport := http.Transport{
Dial: dialTimeout,
TLSClientConfig: tlsConfig,
}
client := http.Client{
Transport: &transport,
}
uri := fmt.Sprintf("%s/_ping", addr)
resp, err := client.Get(uri)
if err != nil {
return 0, err
} else {
defer resp.Body.Close()
status = resp.StatusCode
}
return status, nil
}