-
Notifications
You must be signed in to change notification settings - Fork 1
/
ranchervm.go
428 lines (391 loc) · 11.7 KB
/
ranchervm.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package ranchervm
import (
"fmt"
"math/rand"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
api "github.com/rancher/vm/pkg/apis/ranchervm/v1alpha1"
"github.com/rancher/vm/pkg/server"
"github.com/rancher/vm/pkg/server/client"
)
const (
defaultSSHUser = "docker"
)
// Driver is the RancherVM Driver struct
type Driver struct {
*drivers.BaseDriver
Endpoint string
InsecureSkipVerify bool
AccessKey string
SecretKey string
CPU int
MemoryMiB int
Image string
SSHKeyName string
SSHKeyDelete bool
EnableNoVNC bool
NodeName string
LonghornBacking bool
LonghornVolumeSize string
LonghornFrontend string
LonghornReplicaCount int
LonghornReplicaTimeout int
client *client.RancherVMClient
}
// NewDriver constructs a new RancherVM Driver
func NewDriver(hostName, storePath string) drivers.Driver {
return &Driver{
BaseDriver: &drivers.BaseDriver{
SSHUser: defaultSSHUser,
MachineName: hostName,
StorePath: storePath,
},
}
}
func (d *Driver) getClient() *client.RancherVMClient {
if d.client == nil {
endpoint := strings.TrimSuffix(d.Endpoint, "/")
d.client = client.NewRancherVMClient(endpoint, d.AccessKey, d.SecretKey, d.InsecureSkipVerify)
}
return d.client
}
func randomString(n int, alphabet string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, n)
for i := range b {
b[i] = alphabet[r.Intn(len(alphabet))]
}
return string(b)
}
func generateSSHKeyName(name string) string {
suffix := randomString(5, "0123456789abcdef")
return strings.Join([]string{name, suffix}, "-")
}
func generateSSHKey(path string) (string, error) {
if _, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return "", fmt.Errorf("Desired directory for SSH keys does not exist: %s", err)
}
kp, err := ssh.NewKeyPair()
if err != nil {
return "", fmt.Errorf("Error generating key pair: %s", err)
}
if err := kp.WriteToFile(path, fmt.Sprintf("%s.pub", path)); err != nil {
return "", fmt.Errorf("Error writing keys to file(s): %s", err)
}
return string(kp.PublicKey), nil
}
return "", fmt.Errorf("Key pair already exists: %s", path)
}
// Create a host using the driver's config
func (d *Driver) Create() error {
if d.SSHKeyName == "" {
keyName := generateSSHKeyName(d.MachineName)
publicKey, err := generateSSHKey(d.GetSSHKeyPath())
if err != nil {
return err
}
err = d.getClient().CredentialCreate(keyName, publicKey)
if err != nil {
return err
}
d.SSHKeyName = keyName
// If keypair name wasn't specified, we'll assume the keypair is
// disposable and delete it when the machine is deleted
d.SSHKeyDelete = true
// FIXME: ranchervm creates vm pod before informer cache receives the new credential
time.Sleep(3 * time.Second)
} else {
credential, err := d.getClient().CredentialGet(d.SSHKeyName)
if err != nil {
return err
}
if credential == nil {
publicKey, err := generateSSHKey(d.GetSSHKeyPath())
if err != nil {
return err
}
err = d.getClient().CredentialCreate(d.SSHKeyName, publicKey)
if err != nil {
// A race exists when creating many machines concurrently with
// a named, but not yet generated keypair. We must therefore
// tolerate any 409 conflict errors received in this context.
if !strings.Contains(err.Error(), http.StatusText(http.StatusConflict)) {
return err
}
}
// When generating a named keypair, do NOT automatically delete it
// because the keypair is expected to be reused by other machines
d.SSHKeyDelete = false
// FIXME: ranchervm creates vm pod before informer cache receives the new credential
time.Sleep(3 * time.Second)
} else {
// TODO: verify we have the private key. The public key might've
// been uploaded manually by user, in which case we can't use it
}
}
volume := api.VolumeSource{}
if d.LonghornBacking {
volume.Longhorn = &api.LonghornVolumeSource{
Size: d.LonghornVolumeSize,
Frontend: d.LonghornFrontend,
BaseImage: d.Image,
NumberOfReplicas: d.LonghornReplicaCount,
StaleReplicaTimeout: d.LonghornReplicaTimeout,
}
} else {
volume.EmptyDir = &api.EmptyDirVolumeSource{}
}
return d.getClient().InstanceCreate(server.Instance{
Name: d.MachineName,
Cpus: d.CPU,
Memory: d.MemoryMiB,
Image: d.Image,
Action: string(api.ActionStart),
PublicKeys: []string{d.SSHKeyName},
HostedNovnc: d.EnableNoVNC,
NodeName: d.NodeName,
Volume: volume,
}, 1)
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "ranchervm"
}
// GetCreateFlags returns the mcnflag.Flag slice representing the flags
// that can be set, their descriptions and defaults.
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: "ranchervm-endpoint",
Usage: "RancherVM endpoint",
Value: "",
},
mcnflag.StringFlag{
Name: "ranchervm-access-key",
Usage: "Rancher API Access Key",
Value: "",
},
mcnflag.StringFlag{
Name: "ranchervm-secret-key",
Usage: "Rancher API Secret Key",
Value: "",
},
mcnflag.BoolFlag{
Name: "ranchervm-insecure-skip-verify",
Usage: "Skip TLS certificate verification for HTTP requests to RancherVM",
},
mcnflag.StringFlag{
Name: "ranchervm-ssh-user",
Usage: "SSH user",
Value: "ubuntu",
},
mcnflag.IntFlag{
Name: "ranchervm-ssh-port",
Usage: "SSH port",
Value: drivers.DefaultSSHPort,
},
mcnflag.StringFlag{
Name: "ranchervm-ssh-key-name",
Usage: "Use a shared SSH key",
},
mcnflag.StringFlag{
Name: "ranchervm-ssh-key-path",
Usage: "Path to private SSH key",
},
mcnflag.IntFlag{
Name: "ranchervm-cpu-count",
Usage: "Number of CPUs",
Value: 1,
},
mcnflag.IntFlag{
Name: "ranchervm-memory-mib",
Usage: "Memory in MiB",
Value: 1024,
},
mcnflag.StringFlag{
Name: "ranchervm-image",
Usage: "Docker image containing qcow2 disk image",
Value: "llparse/vm-ubuntu:rancher-2.1.1",
},
mcnflag.BoolFlag{
Name: "ranchervm-novnc",
Usage: "Enable NoVNC, a browser-based VNC client accessible from Rancher UI",
},
mcnflag.StringFlag{
Name: "ranchervm-node-name",
Usage: "Name of Kubernetes node to schedule machine to",
},
mcnflag.BoolFlag{
Name: "ranchervm-longhorn",
Usage: "Use Longhorn storage provider instead of host filesystem",
},
// TODO longhorn should eventually infer size from disk image
mcnflag.StringFlag{
Name: "ranchervm-longhorn-image-size",
Usage: "Size of the qcow2 disk image, currently required by Longhorn",
Value: "50Gi",
},
mcnflag.StringFlag{
Name: "ranchervm-longhorn-frontend",
Usage: "Frontend to expose Longhorn volume with: blockdev, iscsi",
Value: "blockdev",
},
mcnflag.IntFlag{
Name: "ranchervm-longhorn-replica-count",
Usage: "Number of replicas to back Longhorn volume with",
Value: 3,
},
mcnflag.IntFlag{
Name: "ranchervm-longhorn-replica-timeout",
Usage: "Time (in seconds) to wait before replacing an unresponsive replica",
Value: 30,
},
}
}
// GetIP returns an IP or hostname that this host is available at
// e.g. 1.2.3.4 or docker-host-d60b70a14d3a.cloudapp.net
func (d *Driver) GetIP() (string, error) {
instance, err := d.getClient().InstanceGet(d.MachineName)
if err != nil {
return "", err
}
if instance.Status.IP == "" {
return "", fmt.Errorf("IP address is not set")
}
d.IPAddress = instance.Status.IP
return instance.Status.IP, nil
}
// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
// GetURL returns a Docker compatible host URL for connecting to this host
// e.g. tcp://1.2.3.4:2376
func (d *Driver) GetURL() (string, error) {
ip, err := d.GetIP()
if err != nil {
log.Warnf("Failed to get IP: %s", err)
return "", err
}
if ip == "" {
return "", nil
}
return fmt.Sprintf("tcp://%s:2376", ip), nil
}
// GetState returns the state that the host is in (running, stopped, etc)
func (d *Driver) GetState() (state.State, error) {
instance, err := d.getClient().InstanceGet(d.MachineName)
if err != nil {
return state.None, err
}
switch instance.Status.State {
case api.StatePending:
return state.Starting, nil
case api.StateRunning:
return state.Running, nil
case api.StateStopping:
return state.Stopping, nil
case api.StateStopped:
return state.Stopped, nil
case api.StateTerminating:
return state.Stopped, nil
case api.StateTerminated:
return state.Stopped, nil
case api.StateMigrating:
return state.Running, nil
case api.StateError:
return state.Error, nil
}
return state.None, nil
}
// Kill stops a host forcefully
func (d *Driver) Kill() error {
return d.getClient().InstanceStop(d.MachineName)
}
// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
func (d *Driver) PreCreateCheck() error {
instance, err := d.getClient().InstanceGet(d.MachineName)
if err != nil {
return err
}
if instance != nil {
return fmt.Errorf("MachineName %s already taken", d.MachineName)
}
return nil
}
// Remove a host
func (d *Driver) Remove() error {
if d.SSHKeyDelete {
if err := d.getClient().CredentialDelete(d.SSHKeyName); err != nil {
return err
}
}
if err := d.getClient().InstanceDelete(d.MachineName); err != nil {
return err
}
t := time.NewTicker(3 * time.Second)
for _ = range t.C {
if instance, err := d.getClient().InstanceGet(d.MachineName); err != nil {
return err
} else if instance == nil {
break
}
}
return nil
}
// ResolveStorePath returns a unique or shared store path
func (d *Driver) ResolveStorePath(file string) string {
if d.SSHKeyName == "" {
return filepath.Join(d.StorePath, "machines", d.MachineName, file)
}
return filepath.Join(d.StorePath, "machines",
strings.Join([]string{d.DriverName(), d.SSHKeyName, file}, "."))
}
// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
func (d *Driver) Restart() error {
if err := d.Stop(); err != nil {
return err
}
return d.Start()
}
// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.Endpoint = flags.String("ranchervm-endpoint")
d.AccessKey = flags.String("ranchervm-access-key")
d.SecretKey = flags.String("ranchervm-secret-key")
d.InsecureSkipVerify = flags.Bool("ranchervm-insecure-skip-verify")
d.CPU = flags.Int("ranchervm-cpu-count")
d.MemoryMiB = flags.Int("ranchervm-memory-mib")
d.Image = flags.String("ranchervm-image")
d.EnableNoVNC = flags.Bool("ranchervm-novnc")
d.NodeName = flags.String("ranchervm-node-name")
d.SSHKeyName = flags.String("ranchervm-ssh-key-name")
d.SSHKeyPath = flags.String("ranchervm-ssh-key-path")
d.SSHUser = flags.String("ranchervm-ssh-user")
d.SSHPort = flags.Int("ranchervm-ssh-port")
d.LonghornBacking = flags.Bool("ranchervm-longhorn")
d.LonghornVolumeSize = flags.String("ranchervm-longhorn-image-size")
d.LonghornFrontend = flags.String("ranchervm-longhorn-frontend")
d.LonghornReplicaCount = flags.Int("ranchervm-longhorn-replica-count")
d.LonghornReplicaTimeout = flags.Int("ranchervm-longhorn-replica-timeout")
return nil
}
// Start a host
func (d *Driver) Start() error {
return d.getClient().InstanceStart(d.MachineName)
}
// Stop a host gracefully
func (d *Driver) Stop() error {
return d.getClient().InstanceStop(d.MachineName)
}