forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_driver.go
363 lines (289 loc) · 9.09 KB
/
client_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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package rpcdriver
import (
"fmt"
"net/rpc"
"sync"
"time"
"io"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state"
"github.com/docker/machine/libmachine/version"
)
var (
heartbeatInterval = 5 * time.Second
)
type RPCClientDriverFactory interface {
NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver, error)
io.Closer
}
type DefaultRPCClientDriverFactory struct {
openedDrivers []*RPCClientDriver
openedDriversLock sync.Locker
}
func NewRPCClientDriverFactory() RPCClientDriverFactory {
return &DefaultRPCClientDriverFactory{
openedDrivers: []*RPCClientDriver{},
openedDriversLock: &sync.Mutex{},
}
}
type RPCClientDriver struct {
plugin localbinary.DriverPlugin
heartbeatDoneCh chan bool
Client *InternalClient
}
type RPCCall struct {
ServiceMethod string
Args interface{}
Reply interface{}
}
type InternalClient struct {
MachineName string
RPCClient *rpc.Client
rpcServiceName string
}
const (
RPCServiceNameV0 = `RpcServerDriver`
RPCServiceNameV1 = `RPCServerDriver`
HeartbeatMethod = `.Heartbeat`
GetVersionMethod = `.GetVersion`
CloseMethod = `.Close`
GetCreateFlagsMethod = `.GetCreateFlags`
SetConfigRawMethod = `.SetConfigRaw`
GetConfigRawMethod = `.GetConfigRaw`
DriverNameMethod = `.DriverName`
SetConfigFromFlagsMethod = `.SetConfigFromFlags`
GetURLMethod = `.GetURL`
GetMachineNameMethod = `.GetMachineName`
GetIPMethod = `.GetIP`
GetSSHHostnameMethod = `.GetSSHHostname`
GetSSHKeyPathMethod = `.GetSSHKeyPath`
GetSSHPortMethod = `.GetSSHPort`
GetSSHUsernameMethod = `.GetSSHUsername`
GetStateMethod = `.GetState`
PreCreateCheckMethod = `.PreCreateCheck`
CreateMethod = `.Create`
RemoveMethod = `.Remove`
StartMethod = `.Start`
StopMethod = `.Stop`
RestartMethod = `.Restart`
KillMethod = `.Kill`
UpgradeMethod = `.Upgrade`
)
func (ic *InternalClient) Call(serviceMethod string, args interface{}, reply interface{}) error {
if serviceMethod != HeartbeatMethod {
log.Debugf("(%s) Calling %+v", ic.MachineName, serviceMethod)
}
return ic.RPCClient.Call(ic.rpcServiceName+serviceMethod, args, reply)
}
func (ic *InternalClient) switchToV0() {
ic.rpcServiceName = RPCServiceNameV0
}
func NewInternalClient(rpcclient *rpc.Client) *InternalClient {
return &InternalClient{
RPCClient: rpcclient,
rpcServiceName: RPCServiceNameV1,
}
}
func (f *DefaultRPCClientDriverFactory) Close() error {
f.openedDriversLock.Lock()
defer f.openedDriversLock.Unlock()
for _, openedDriver := range f.openedDrivers {
if err := openedDriver.close(); err != nil {
// No need to display an error.
// There's nothing we can do and it doesn't add value to the user.
}
}
f.openedDrivers = []*RPCClientDriver{}
return nil
}
func (f *DefaultRPCClientDriverFactory) NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver, error) {
mcnName := ""
p, err := localbinary.NewPlugin(driverName)
if err != nil {
return nil, err
}
go func() {
if err := p.Serve(); err != nil {
// TODO: Is this best approach?
log.Warn(err)
return
}
}()
addr, err := p.Address()
if err != nil {
return nil, fmt.Errorf("Error attempting to get plugin server address for RPC: %s", err)
}
rpcclient, err := rpc.DialHTTP("tcp", addr)
if err != nil {
return nil, err
}
c := &RPCClientDriver{
Client: NewInternalClient(rpcclient),
heartbeatDoneCh: make(chan bool),
}
f.openedDriversLock.Lock()
f.openedDrivers = append(f.openedDrivers, c)
f.openedDriversLock.Unlock()
var serverVersion int
if err := c.Client.Call(GetVersionMethod, struct{}{}, &serverVersion); err != nil {
// this is the first call we make to the server. We try to play nice with old pre 0.5.1 client,
// by gracefully trying old RPCServiceName, we do this only once, and keep the result for future calls.
log.Debugf(err.Error())
log.Debugf("Client (%s) with %s does not work, re-attempting with %s", c.Client.MachineName, RPCServiceNameV1, RPCServiceNameV0)
c.Client.switchToV0()
if err := c.Client.Call(GetVersionMethod, struct{}{}, &serverVersion); err != nil {
return nil, err
}
}
if serverVersion != version.APIVersion {
return nil, fmt.Errorf("Driver binary uses an incompatible API version (%d)", serverVersion)
}
log.Debug("Using API Version ", serverVersion)
go func(c *RPCClientDriver) {
for {
select {
case <-c.heartbeatDoneCh:
return
case <-time.After(heartbeatInterval):
if err := c.Client.Call(HeartbeatMethod, struct{}{}, nil); err != nil {
log.Warnf("Error attempting heartbeat call to plugin server: %s", err)
}
}
}
}(c)
if err := c.SetConfigRaw(rawDriver); err != nil {
return nil, err
}
mcnName = c.GetMachineName()
p.MachineName = mcnName
c.Client.MachineName = mcnName
c.plugin = p
return c, nil
}
func (c *RPCClientDriver) MarshalJSON() ([]byte, error) {
return c.GetConfigRaw()
}
func (c *RPCClientDriver) UnmarshalJSON(data []byte) error {
return c.SetConfigRaw(data)
}
func (c *RPCClientDriver) close() error {
c.heartbeatDoneCh <- true
close(c.heartbeatDoneCh)
log.Debug("Making call to close driver server")
if err := c.Client.Call(CloseMethod, struct{}{}, nil); err != nil {
return err
}
log.Debug("Successfully made call to close driver server")
log.Debug("Making call to close connection to plugin binary")
return c.plugin.Close()
}
// Helper method to make requests which take no arguments and return simply a
// string, e.g. "GetIP".
func (c *RPCClientDriver) rpcStringCall(method string) (string, error) {
var info string
if err := c.Client.Call(method, struct{}{}, &info); err != nil {
return "", err
}
return info, nil
}
func (c *RPCClientDriver) GetCreateFlags() []mcnflag.Flag {
var flags []mcnflag.Flag
if err := c.Client.Call(GetCreateFlagsMethod, struct{}{}, &flags); err != nil {
log.Warnf("Error attempting call to get create flags: %s", err)
}
return flags
}
func (c *RPCClientDriver) SetConfigRaw(data []byte) error {
return c.Client.Call(SetConfigRawMethod, data, nil)
}
func (c *RPCClientDriver) GetConfigRaw() ([]byte, error) {
var data []byte
if err := c.Client.Call(GetConfigRawMethod, struct{}{}, &data); err != nil {
return nil, err
}
return data, nil
}
// DriverName returns the name of the driver
func (c *RPCClientDriver) DriverName() string {
driverName, err := c.rpcStringCall(DriverNameMethod)
if err != nil {
log.Warnf("Error attempting call to get driver name: %s", err)
}
return driverName
}
func (c *RPCClientDriver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return c.Client.Call(SetConfigFromFlagsMethod, &flags, nil)
}
func (c *RPCClientDriver) GetURL() (string, error) {
return c.rpcStringCall(GetURLMethod)
}
func (c *RPCClientDriver) GetMachineName() string {
name, err := c.rpcStringCall(GetMachineNameMethod)
if err != nil {
log.Warnf("Error attempting call to get machine name: %s", err)
}
return name
}
func (c *RPCClientDriver) GetIP() (string, error) {
return c.rpcStringCall(GetIPMethod)
}
func (c *RPCClientDriver) GetSSHHostname() (string, error) {
return c.rpcStringCall(GetSSHHostnameMethod)
}
// GetSSHKeyPath returns the key path
// TODO: This method doesn't even make sense to have with RPC.
func (c *RPCClientDriver) GetSSHKeyPath() string {
path, err := c.rpcStringCall(GetSSHKeyPathMethod)
if err != nil {
log.Warnf("Error attempting call to get SSH key path: %s", err)
}
return path
}
func (c *RPCClientDriver) GetSSHPort() (int, error) {
var port int
if err := c.Client.Call(GetSSHPortMethod, struct{}{}, &port); err != nil {
return 0, err
}
return port, nil
}
func (c *RPCClientDriver) GetSSHUsername() string {
username, err := c.rpcStringCall(GetSSHUsernameMethod)
if err != nil {
log.Warnf("Error attempting call to get SSH username: %s", err)
}
return username
}
func (c *RPCClientDriver) GetState() (state.State, error) {
var s state.State
if err := c.Client.Call(GetStateMethod, struct{}{}, &s); err != nil {
return state.Error, err
}
return s, nil
}
func (c *RPCClientDriver) PreCreateCheck() error {
return c.Client.Call(PreCreateCheckMethod, struct{}{}, nil)
}
func (c *RPCClientDriver) Create() error {
return c.Client.Call(CreateMethod, struct{}{}, nil)
}
func (c *RPCClientDriver) Remove() error {
return c.Client.Call(RemoveMethod, struct{}{}, nil)
}
func (c *RPCClientDriver) Start() error {
return c.Client.Call(StartMethod, struct{}{}, nil)
}
func (c *RPCClientDriver) Stop() error {
return c.Client.Call(StopMethod, struct{}{}, nil)
}
func (c *RPCClientDriver) Restart() error {
return c.Client.Call(RestartMethod, struct{}{}, nil)
}
func (c *RPCClientDriver) Kill() error {
return c.Client.Call(KillMethod, struct{}{}, nil)
}
func (c *RPCClientDriver) Upgrade() error {
return c.Client.Call(UpgradeMethod, struct{}{}, nil)
}