-
Notifications
You must be signed in to change notification settings - Fork 50
/
libstorage_client.go
225 lines (179 loc) · 4.68 KB
/
libstorage_client.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package libstorage
import (
"crypto/md5"
"crypto/tls"
"errors"
"fmt"
"io"
"os"
log "github.com/Sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"
"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/types"
"github.com/codedellemc/libstorage/api/utils"
)
type client struct {
types.APIClient
ctx types.Context
config gofig.Config
clientType types.ClientType
lsxCache *lss
serviceCache *lss
supportedCache types.Store
instanceIDCache types.Store
}
var errExecutorNotSupported = errors.New("executor not supported")
func (c *client) isController() bool {
return c.clientType == types.ControllerClient
}
func (c *client) dial(ctx types.Context) error {
ctx.WithField("path", lsxMutex).Info("lsx lock file path")
svcInfos, err := c.Services(ctx)
if err != nil {
return err
}
// controller clients do not have any additional dialer logic
if c.isController() {
return nil
}
store := utils.NewStore()
c.ctx = c.ctx.WithValue(context.ServerKey, c.ServerName())
if !c.config.GetBool(types.ConfigExecutorNoDownload) {
ctx.Info("initializing executors cache")
if _, err := c.Executors(ctx); err != nil {
return err
}
if err := c.updateExecutor(ctx); err != nil {
return err
}
}
for service, _ := range svcInfos {
ctx := c.ctx.WithValue(context.ServiceKey, service)
ctx.Info("initializing supported cache")
supported, err := c.Supported(ctx, store)
if err != nil {
return goof.WithError("error initializing supported cache", err)
}
if !supported {
ctx.Warn("executor not supported")
continue
}
ctx.Info("initializing instance ID cache")
if _, err := c.InstanceID(ctx, store); err != nil {
if err == types.ErrNotImplemented {
ctx.WithError(err).Warn("cannot get instance ID")
continue
}
return goof.WithError("error initializing instance ID cache", err)
}
}
return nil
}
func getHost(proto, lAddr string, tlsConfig *tls.Config) string {
if tlsConfig != nil && tlsConfig.ServerName != "" {
return tlsConfig.ServerName
} else if proto == "unix" {
return "libstorage-server"
} else {
return lAddr
}
}
func (c *client) getServiceInfo(service string) (*types.ServiceInfo, error) {
if si := c.serviceCache.GetServiceInfo(service); si != nil {
return si, nil
}
return nil, goof.WithField("name", service, "unknown service")
}
func (c *client) updateExecutor(ctx types.Context) error {
if c.isController() {
return utils.NewUnsupportedForClientTypeError(
c.clientType, "updateExecutor")
}
ctx.Debug("updating executor")
lsxi := c.lsxCache.GetExecutorInfo(types.LSX.Name())
if lsxi == nil {
return goof.WithField("lsx", types.LSX, "unknown executor")
}
ctx.Debug("waiting on executor lock")
if err := c.lsxMutexWait(); err != nil {
return err
}
defer func() {
ctx.Debug("signalling executor lock")
if err := c.lsxMutexSignal(); err != nil {
panic(err)
}
}()
if !types.LSX.Exists() {
ctx.Debug("executor does not exist, download executor")
return c.downloadExecutor(ctx)
}
ctx.Debug("executor exists, getting local checksum")
checksum, err := c.getExecutorChecksum(ctx)
if err != nil {
return err
}
if lsxi.MD5Checksum != checksum {
ctx.WithFields(log.Fields{
"remoteChecksum": lsxi.MD5Checksum,
"localChecksum": checksum,
}).Debug("executor checksums do not match, download executor")
return c.downloadExecutor(ctx)
}
return nil
}
func (c *client) getExecutorChecksum(ctx types.Context) (string, error) {
if c.isController() {
return "", utils.NewUnsupportedForClientTypeError(
c.clientType, "getExecutorChecksum")
}
ctx.Debug("getting executor checksum")
f, err := os.Open(types.LSX.String())
if err != nil {
return "", err
}
defer f.Close()
h := md5.New()
buf := make([]byte, 1024)
for {
n, err := f.Read(buf)
if err == io.EOF {
break
}
if err != nil {
return "", err
}
if _, err := h.Write(buf[:n]); err != nil {
return "", err
}
}
sum := fmt.Sprintf("%x", h.Sum(nil))
ctx.WithField("localChecksum", sum).Debug("got local executor checksum")
return sum, nil
}
func (c *client) downloadExecutor(ctx types.Context) error {
if c.isController() {
return utils.NewUnsupportedForClientTypeError(
c.clientType, "downloadExecutor")
}
ctx.Debug("downloading executor")
f, err := os.OpenFile(
types.LSX.String(),
os.O_CREATE|os.O_RDWR|os.O_TRUNC,
0755)
if err != nil {
return err
}
defer f.Close()
rdr, err := c.APIClient.ExecutorGet(ctx, types.LSX.Name())
n, err := io.Copy(f, rdr)
if err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
ctx.WithField("bytes", n).Debug("downloaded executor")
return nil
}