-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnutanixAHV.go
552 lines (442 loc) · 12.4 KB
/
nutanixAHV.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package nutanixAHV
import (
"archive/tar"
"strconv"
//"errors"
"fmt"
"net"
"net/http"
"io/ioutil"
"io"
"os"
"time"
//"path/filepath"
// rename because duplicated package name
gssh "golang.org/x/crypto/ssh"
"github.com/Tfindelkind/ntnx-golang-client-sdk"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
)
const (
isoFilename = "boot2docker.iso"
driverName = "nutanixAHV"
defaultUser = "admin"
defaultPass = "nutanix/4u"
defaultMaxCapacityBytes = "20480"
defaultMemoryMB = "1024"
defaultVcpus = "1"
defaultNetworkName = "default"
defaultContainerName = "default"
defaultImageName = "boot2docker"
// REMOVE
defaultHost = "192.168.178.41"
)
const (
sshUser = "docker"
sshPassword = "tcuser"
sshPort = 22
)
const (
SCP_PUSH_BEGIN_FILE = "C"
SCP_PUSH_BEGIN_FOLDER = "D"
SCP_PUSH_BEGIN_END_FOLDER = " 0"
SCP_PUSH_END_FOLDER = "E"
SCP_PUSH_END = "\x00"
)
type Driver struct {
*drivers.BaseDriver
NutanixHost string
Username string
Password string
MemoryMB string
Vcpus string
MaxCapacityBytes string
NetworkName string
ContainerName string
ImageName string
Boot2DockerURL string
vmLoaded bool
nc ntnxAPI.NTNXConnection
vm ntnxAPI.VM
im ntnxAPI.Image
vdisk ntnxAPI.VDisk
nic ntnxAPI.Network
}
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
Name: "nutanixAHV-host",
Usage: "Nutanix Cluster or CVM IP/Name",
Value: defaultHost,
},
mcnflag.StringFlag{
Name: "nutanixAHV-username",
Usage: "Nutanix username",
Value: defaultUser,
},
mcnflag.StringFlag{
Name: "nutanixAHV-password",
Usage: "Nutanix password",
Value: defaultPass,
},
mcnflag.StringFlag{
Name: "nutanixAHV-memory-mb",
Usage: "Size of memory for host in MB",
Value: defaultMemoryMB,
},
mcnflag.StringFlag{
Name: "nutanixAHV-max-capacity-bytes",
Usage: "Size of disk for host in MB",
Value: defaultMaxCapacityBytes,
},
mcnflag.StringFlag{
Name: "nutanixAHV-vcpus",
Usage: "Number of Vcpus",
Value: defaultVcpus,
},
mcnflag.StringFlag{
Name: "nutanixAHV-network-name",
Usage: "Name of network to connect to",
Value: defaultNetworkName,
},
mcnflag.StringFlag{
Name: "nutanixAHV-container-name",
Usage: "Name of container used for vDisk",
Value: defaultContainerName,
},
mcnflag.StringFlag{
Name: "nutanixAHV-image-name",
Usage: "Name of image used for boot",
Value: defaultImageName,
},
mcnflag.StringFlag{
EnvVar: "NUTANIXAHV_BOOT2DOCKER_URL",
Name: "nutanixAHV-boot2docker-url",
Usage: "The URL of the boot2docker image. Defaults to the latest available version",
Value: "",
},
}
}
func (d *Driver) GetMachineName() string {
return d.MachineName
}
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
func (d *Driver) GetSSHPort() (int, error) {
if d.SSHPort == 0 {
d.SSHPort = sshPort
}
return d.SSHPort, nil
}
func (d *Driver) GetSSHUsername() string {
if d.SSHUser == "" {
d.SSHUser = sshUser
}
return d.SSHUser
}
func (d *Driver) DriverName() string {
return driverName
}
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
log.Debugf("SetConfigFromFlags called")
d.NutanixHost= flags.String("nutanixAHV-host")
d.Username = flags.String("nutanixAHV-username")
d.Password = flags.String("nutanixAHV-password")
d.MemoryMB = flags.String("nutanixAHV-memory-mb")
d.MaxCapacityBytes = flags.String("nutanixAHV-max-capacity-bytes")
d.Vcpus = flags.String("nutanixAHV-vcpus")
d.NetworkName = flags.String("nutanixAHV-network-name")
d.ContainerName = flags.String("nutanixAHV-container-name")
d.ImageName = flags.String("nutanixAHV-image-name")
d.Boot2DockerURL = flags.String("nutanixAHV-boot2docker-url")
d.SSHUser = sshUser
d.SSHPort = sshPort
return nil
}
func (d *Driver) GetURL() (string, error) {
log.Debugf("GetURL called")
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 // TODO - don't hardcode the port!
}
func (d *Driver) PreCreateCheck() error {
// TODO We could look at d.conn.GetCapabilities()
// parse the XML, and look for kvm
log.Debug("About to check libvirt version")
// TODO might want to check minimum version
/*_, err := d.conn.GetLibVersion()
if err != nil {
log.Warnf("Unable to get libvirt version")
return err
}
err = d.validatePrivateNetwork()
if err != nil {
return err
}
err = d.validateNetwork(d.Network)
if err != nil {
return err
}
// Others...? */
return nil
}
func (d *Driver) Create() error {
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return err
}
log.Infof("Creating SSH key..."+d.GetSSHKeyPath())
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
fmt.Println(d.GetSSHKeyPath()+".pub")
//if err := os.MkdirAll(d.ResolveStorePath("."), 0755); err != nil {
// return err
//}
log.Infof("Setup Nutanix REST connection...")
d.initConnection()
log.Infof("Creating VM...")
d.initVM()
fmt.Println("Insert boot2docker into CDROM ...")
d.im = ntnxAPI.Image { d.ImageName, "", "ISO_IMAGE", "", ntnxAPI.GetImageIDbyName(&d.nc,d.ImageName)}
ntnxAPI.CloneCDforVM(&d.nc,&d.vm,&d.im)
log.Infof("Creating VM data disk...")
// create vdisk struct with some empty values which will be set via CreateVDiskforVM later
d.vdisk = ntnxAPI.VDisk { d.ContainerName, ntnxAPI.GetContainerIDbyName(&d.nc,d.ContainerName), "", d.MaxCapacityBytes,"",false }
ntnxAPI.CreateVDiskforVM(&d.nc,&d.vm,&d.vdisk)
log.Debugf("Creating VM network...")
// create network struct with some empty values which will be set via CreateVNicforVM later
d.nic = ntnxAPI.Network { d.NetworkName, ntnxAPI.GetNetworkIDbyName(&d.nc, d.NetworkName), 0 }
ntnxAPI.CreateVNicforVM(&d.nc,&d.vm,&d.nic)
log.Infof("Provisioning certs and ssh keys...")
// Generate a tar keys bundle
d.Start()
log.Infof("Waiting for VM to come online...")
var ip string
var err error
for i := 1; i <= 60; i++ {
ip, err = ntnxAPI.GetVMIP(&d.nc,&d.vm)
if err != nil {
log.Debugf("Not there yet %d/%d, error: %s", i, 60, err)
time.Sleep(2 * time.Second)
continue
}
if ip != "" {
log.Debugf("Got an ip: %s", ip)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, 22), time.Duration(2*time.Second))
if err != nil {
log.Debugf("SSH Daemon not responding yet: %s", err)
time.Sleep(2 * time.Second)
continue
}
conn.Close()
break
}
}
// Generate a tar keys bundle
if err := d.generateKeyBundle(); err != nil {
return err
}
//uploading ssh keybundle tar xf /var/lib/boot2docker/userdata.tar -C /home/docker/ > /var/log/userdata.log 2>&1
config := &gssh.ClientConfig{
User: sshUser,
Auth: []gssh.AuthMethod {
gssh.Password(sshPassword),
},
}
client, err := gssh.Dial("tcp", ip+":"+strconv.Itoa(sshPort), config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
go func() {
fmt.Println(d.ResolveStorePath("userdata.tar"))
w, _ := session.StdinPipe()
defer w.Close()
fileSrc, err := os.Open(d.ResolveStorePath("userdata.tar"))
if err != nil {
log.Debugf("Failed to open source file: ", err)
}
defer fileSrc.Close()
//Get file size
srcStat, err := fileSrc.Stat()
if err != nil {
log.Debugf("Failed to stat file: ", err)
}
// According to https://blogs.oracle.com/janp/entry/how_the_scp_protocol_works
// Print the file content
mode := SCP_PUSH_BEGIN_FILE + GetPerm(fileSrc)
fmt.Fprintln(w, mode, srcStat.Size(), "userdata.tar")
io.Copy(w, fileSrc)
fmt.Fprint(w, SCP_PUSH_END)
}()
if err := session.Run("scp -tr ./"); err != nil {
panic("Failed to run: "+ err.Error())
}
session, err = client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
if err := session.Run("sudo mv ./userdata.tar /var/lib/boot2docker/ ; tar xf /var/lib/boot2docker/userdata.tar -C /home/docker/ > /var/log/userdata.log 2>&1"); err != nil {
panic("Failed to run: " + err.Error())
}
d.vmLoaded = true
return nil
}
func (d *Driver) Start() error {
log.Debugf("Starting VM %s", d.MachineName)
d.validateVMRef()
ntnxAPI.StartVM(&d.nc,&d.vm)
return nil
}
func (d *Driver) Stop() error {
ntnxAPI.StopVM(&d.nc,&d.vm)
return nil
}
func (d *Driver) Remove() error {
//if (ntnxAPI.VMExist(&d.nc,&d.vm)) {
// ntnxAPI.DeleteVM(&d.nc,&d.vm)
// }
return nil
}
func (d *Driver) Restart() error {
log.Debugf("Restarting VM %s", d.MachineName)
if err := d.Stop(); err != nil {
return err
}
return d.Start()
}
func (d *Driver) Kill() error {
/*log.Debugf("Killing VM %s", d.MachineName)
d.validateVMRef()
return d.VM.Destroy()
*/
return nil
}
func (d *Driver) GetState() (state.State, error) {
log.Debugf("Getting current state...")
d.validateVMRef()
states := ntnxAPI.GetVMState(&d.nc,&d.vm)
switch states {
case "on":
fmt.Println("on")
return state.Running, nil
case "off":
fmt.Println("off")
return state.Stopped, nil
}
return state.None, nil
}
func (d *Driver) validateVMRef() {
if !d.vmLoaded {
log.Debugf("Fetching VM...")
if (!ntnxAPI.VMExist(&d.nc,&d.vm)) {
fmt.Println("VM does not exist")
}
}
}
func (d *Driver) GetIP() (string, error) {
log.Debugf("GetIP called for %s", d.MachineName)
d.initConnection()
d.initVM()
ip, err := ntnxAPI.GetVMIP(&d.nc,&d.vm)
return ip, err
}
func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}
func (d *Driver) GetSSHKeyPath() string {
return d.ResolveStorePath("id_rsa")
}
// Make a boot2docker userdata.tar key bundle
func (d *Driver) generateKeyBundle() error {
log.Debugf("Creating Tar key bundle...")
magicString := "boot2docker, this is Nutanix speaking"
tf, err := os.Create(d.ResolveStorePath("userdata.tar"))
if err != nil {
return err
}
defer tf.Close()
var fileWriter = tf
tw := tar.NewWriter(fileWriter)
defer tw.Close()
// magicString first so we can figure out who originally wrote the tar.
file := &tar.Header{Name: magicString, Size: int64(len(magicString))}
if err := tw.WriteHeader(file); err != nil {
return err
}
if _, err := tw.Write([]byte(magicString)); err != nil {
return err
}
// .ssh/key.pub => authorized_keys
file = &tar.Header{Name: ".ssh", Typeflag: tar.TypeDir, Mode: 0700}
if err := tw.WriteHeader(file); err != nil {
return err
}
pubKey, err := ioutil.ReadFile(d.publicSSHKeyPath())
if err != nil {
return err
}
file = &tar.Header{Name: ".ssh/authorized_keys", Size: int64(len(pubKey)), Mode: 0644}
if err := tw.WriteHeader(file); err != nil {
return err
}
if _, err := tw.Write([]byte(pubKey)); err != nil {
return err
}
file = &tar.Header{Name: ".ssh/authorized_keys2", Size: int64(len(pubKey)), Mode: 0644}
if err := tw.WriteHeader(file); err != nil {
return err
}
if _, err := tw.Write([]byte(pubKey)); err != nil {
return err
}
if err := tw.Close(); err != nil {
return err
}
return nil
}
func (d *Driver) initConnection() error {
d.nc = ntnxAPI.NTNXConnection { defaultHost, defaultUser, defaultPass, "", http.Client{}}
ntnxAPI.EncodeCredentials(&d.nc)
ntnxAPI.CreateHttpClient(&d.nc)
return nil
}
func (d *Driver) initVM() error {
d.vm = ntnxAPI.VM { d.MemoryMB , d.MachineName, d.Vcpus, d.NetworkName, ""}
if UUID, err := ntnxAPI.GetVMIDbyName(&d.nc,d.MachineName); err != nil {
log.Infof("VM doesn't exist anymore or is not unique")
} else {
d.vm.UUID = UUID
}
return nil
}
func check(e error) {
if e != nil {
panic(e)
}
}
func GetPerm(f *os.File) (perm string) {
fileStat, _ := f.Stat()
mod := fileStat.Mode().Perm()
return fmt.Sprintf("%04o", uint32(mod))
}
func NewDriver(hostName, storePath string) drivers.Driver {
return &Driver{ }
}