forked from deis/deis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
92 lines (77 loc) · 2.01 KB
/
etcd.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 config
import (
"fmt"
"net"
"net/http"
"strings"
"time"
fleetEtcd "github.com/coreos/fleet/etcd"
"github.com/coreos/fleet/ssh"
"github.com/coreos/go-etcd/etcd"
"github.com/deis/deis/deisctl/backend/fleet"
)
func getTunnelFlag() string {
tun := fleet.Flags.Tunnel
if tun != "" && !strings.Contains(tun, ":") {
tun += ":22"
}
return tun
}
func getChecker() *ssh.HostKeyChecker {
if !fleet.Flags.StrictHostKeyChecking {
return nil
}
keyFile := ssh.NewHostKeyFile(fleet.Flags.KnownHostsFile)
return ssh.NewHostKeyChecker(keyFile)
}
type etcdClient struct {
etcd *etcd.Client
}
func (c *etcdClient) Get(key string) (string, error) {
sort, recursive := true, false
resp, err := c.etcd.Get(key, sort, recursive)
if err != nil {
return "", err
}
return resp.Node.Value, nil
}
func (c *etcdClient) Set(key string, value string) (string, error) {
resp, err := c.etcd.Set(key, value, 0) // don't use TTLs
if err != nil {
return "", err
}
return resp.Node.Value, nil
}
func getEtcdClient() (*etcdClient, error) {
var dial func(string, string) (net.Conn, error)
tun := getTunnelFlag()
if tun != "" {
sshClient, err := ssh.NewSSHClient("core", tun, getChecker(), false)
if err != nil {
return nil, fmt.Errorf("failed initializing SSH client: %v", err)
}
dial = func(network, addr string) (net.Conn, error) {
tcpaddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
return sshClient.DialTCP(network, nil, tcpaddr)
}
}
tlsConfig, err := fleetEtcd.ReadTLSConfigFiles(fleet.Flags.EtcdCAFile,
fleet.Flags.EtcdCertFile, fleet.Flags.EtcdKeyFile)
if err != nil {
return nil, err
}
trans := http.Transport{
Dial: dial,
TLSClientConfig: tlsConfig,
}
timeout := time.Duration(fleet.Flags.RequestTimeout*1000) * time.Millisecond
machines := []string{fleet.Flags.Endpoint}
c := etcd.NewClient(machines)
c.SetDialTimeout(timeout)
// use custom transport with SSH tunnel capability
c.SetTransport(&trans)
return &etcdClient{etcd: c}, nil
}