-
Notifications
You must be signed in to change notification settings - Fork 50
/
libstorage_driver.go
114 lines (92 loc) · 2.89 KB
/
libstorage_driver.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package libstorage
import (
"crypto/tls"
"net"
"net/http"
"time"
log "github.com/Sirupsen/logrus"
"github.com/akutz/gofig"
"github.com/akutz/gotil"
apiclient "github.com/emccode/libstorage/api/client"
"github.com/emccode/libstorage/api/context"
"github.com/emccode/libstorage/api/types"
"github.com/emccode/libstorage/api/utils"
)
var (
// EnableInstanceIDHeaders is a flag indicating whether or not the
// client will automatically send the instance ID header(s) along with
// storage-related API requests. The default is enabled.
EnableInstanceIDHeaders = true
// EnableLocalDevicesHeaders is a flag indicating whether or not the
// client will automatically send the local devices header(s) along with
// storage-related API requests. The default is enabled.
EnableLocalDevicesHeaders = true
)
type driver struct {
client
}
func newDriver() types.StorageDriver {
return &driver{}
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
logFields := log.Fields{}
addr := config.GetString(types.ConfigHost)
d.ctx = ctx.WithValue(context.HostKey, addr)
ctx.Debug("got configured host address")
proto, lAddr, err := gotil.ParseAddress(addr)
if err != nil {
return err
}
tlsConfig, err := utils.ParseTLSConfig(config, logFields)
if err != nil {
return err
}
host := getHost(proto, lAddr, tlsConfig)
disableKeepAlive := config.GetBool(types.ConfigHTTPDisableKeepAlive)
lsxPath := config.GetString(types.ConfigExecutorPath)
logFields["host"] = host
logFields["disableKeepAlive"] = disableKeepAlive
logFields["lsxPath"] = lsxPath
httpTransport := &http.Transport{
Dial: func(string, string) (net.Conn, error) {
if tlsConfig == nil {
return net.Dial(proto, lAddr)
}
return tls.Dial(proto, lAddr, tlsConfig)
},
DisableKeepAlives: disableKeepAlive,
}
apiClient := apiclient.New(host, httpTransport)
logReq := config.GetBool(types.ConfigLogHTTPRequests)
logRes := config.GetBool(types.ConfigLogHTTPResponses)
apiClient.LogRequests(logReq)
apiClient.LogResponses(logRes)
logFields["enableInstanceIDHeaders"] = EnableInstanceIDHeaders
logFields["enableLocalDevicesHeaders"] = EnableLocalDevicesHeaders
logFields["logRequests"] = logReq
logFields["logResponses"] = logRes
newIIDCache := utils.NewStore
dur, err := time.ParseDuration(
config.GetString(types.ConfigClientCacheInstanceID))
if err != nil {
logFields["iidCacheDuration"] = dur.String()
newIIDCache = func() types.Store {
return utils.NewTTLStore(dur, true)
}
}
d.client = client{
APIClient: apiClient,
ctx: ctx,
config: config,
serviceCache: &lss{Store: utils.NewStore()},
lsxCache: &lss{Store: utils.NewStore()},
instanceIDCache: &lss{Store: newIIDCache()},
}
if err := d.dial(ctx); err != nil {
return err
}
logFields["server"] = d.ServerName()
d.ctx.WithFields(logFields).Info(
"successfully dialed libStorage")
return nil
}