forked from lnxbil/docker-machine-driver-proxmox-ve
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproxmoxdriver.go
1294 lines (1109 loc) · 32.8 KB
/
proxmoxdriver.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dockermachinedriverproxmoxve
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
mrand "math/rand"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/crypto/ssh"
resty "gopkg.in/resty.v1"
sshrw "github.com/mosolovsa/go_cat_sshfilerw"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/mcnflag"
mssh "github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/labstack/gommon/log"
)
// Driver for Proxmox VE
type Driver struct {
*drivers.BaseDriver
driver *ProxmoxVE
// Top-level strategy for proisioning a new node
ProvisionStrategy string
// Basic Authentication for Proxmox VE
Host string // Host to connect to
Node string // optional, node to create VM on, host used if omitted but must match internal node name
User string // username
Password string // password
Realm string // realm, e.g. pam, pve, etc.
// File to load as boot image RancherOS/Boot2Docker
ImageFile string // in the format <storagename>:iso/<filename>.iso
Pool string // pool to add the VM to (necessary for users with only pool permission)
Storage string // internal PVE storage name
StorageType string // Type of the storage (currently QCOW2 and RAW)
DiskSize string // disk size in GB
Memory int // memory in GB
StorageFilename string
Onboot string // Specifies whether a VM will be started during system bootup.
Protection string // Sets the protection flag of the VM. This will disable the remove VM and remove disk operations.
Citype string // Specifies the cloud-init configuration format.
NUMA string // Enable/disable NUMA
CiEnabled string
NetModel string // Net Interface Model, [e1000, virtio, realtek, etc...]
NetFirewall string // Enable/disable firewall
NetMtu string // set nic MTU
NetBridge string // bridge applied to network interface
NetVlanTag int // vlan tag
ScsiController string
ScsiAttributes string
VMID string // VM ID only filled by create()
VMIDRange string // acceptable range of VMIDs
VMUUID string // UUID to confirm
CloneVMID string // VM ID to clone
CloneFull int // Make a full (detached) clone from parent (defaults to true if VMID is not a template, otherwise false)
GuestUsername string // user to log into the guest OS to copy the public key
GuestPassword string // password to log into the guest OS to copy the public key
GuestSSHPort int // ssh port to log into the guest OS to copy the public key
CPU string // Emulated CPU type.
CPUSockets string // The number of cpu sockets.
CPUCores string // The number of cores per socket.
driverDebug bool // driver debugging
restyDebug bool // enable resty debugging
}
func (d *Driver) debugf(format string, v ...interface{}) {
if d.driverDebug {
log.Infof(format, v...)
}
}
func (d *Driver) debug(v ...interface{}) {
if d.driverDebug {
log.Info(v...)
}
}
func (d *Driver) connectAPI() error {
if d.driver == nil {
d.debugf("Create called")
d.debugf("Connecting to %s as %s@%s with password '%s'", d.Host, d.User, d.Realm, d.Password)
c, err := GetProxmoxVEConnectionByValues(d.User, d.Password, d.Realm, d.Host)
d.driver = c
if err != nil {
return fmt.Errorf("Could not connect to host '%s' with '%s@%s'", d.Host, d.User, d.Realm)
}
if d.restyDebug {
c.EnableDebugging()
}
d.debugf("Connected to PVE version '" + d.driver.Version + "'")
}
return nil
}
// GetCreateFlags returns the argument flags for the program
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_PROXMOX_HOST",
Name: "proxmoxve-proxmox-host",
Usage: "Host to connect to",
Value: "192.168.1.253",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_PROXMOX_NODE",
Name: "proxmoxve-proxmox-node",
Usage: "Node to use (defaults to host)",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_PROVISION_STRATEGY",
Name: "proxmoxve-provision-strategy",
Usage: "Provision strategy (cdrom|clone)",
Value: "cdrom",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_PROXMOX_USER_NAME",
Name: "proxmoxve-proxmox-user-name",
Usage: "User to connect as",
Value: "root",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_PROXMOX_USER_PASSWORD",
Name: "proxmoxve-proxmox-user-password",
Usage: "Password to connect with",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_PROXMOX_REALM",
Name: "proxmoxve-proxmox-realm",
Usage: "Realm to connect to (default: pam)",
Value: "pam",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_PROXMOX_POOL",
Name: "proxmoxve-proxmox-pool",
Usage: "pool to attach to",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_VMID_RANGE",
Name: "proxmoxve-vm-vmid-range",
Usage: "range of acceptable vmid values <low>[:<high>]",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_STORAGE_PATH",
Name: "proxmoxve-vm-storage-path",
Usage: "storage to create the VM volume on",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_STORAGE_SIZE",
Name: "proxmoxve-vm-storage-size",
Usage: "disk size in GB",
Value: "16",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_STORAGE_TYPE",
Name: "proxmoxve-vm-storage-type",
Usage: "storage type to use (QCOW2 or RAW)",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_SCSI_CONTROLLER",
Name: "proxmoxve-vm-scsi-controller",
Usage: "scsi controller model (default: virtio-scsi-pci)",
Value: "virtio-scsi-pci",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_SCSI_ATTRIBUTES",
Name: "proxmoxve-vm-scsi-attributes",
Usage: "scsi0 attributes",
Value: "",
},
mcnflag.IntFlag{
EnvVar: "PROXMOXVE_VM_MEMORY",
Name: "proxmoxve-vm-memory",
Usage: "memory in GB",
Value: 8,
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_NUMA",
Name: "proxmoxve-vm-numa",
Usage: "enable/disable NUMA",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_CPU",
Name: "proxmoxve-vm-cpu",
Usage: "Emulatd CPU",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_CPU_SOCKETS",
Name: "proxmoxve-vm-cpu-sockets",
Usage: "number of cpus",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_CPU_CORES",
Name: "proxmoxve-vm-cpu-cores",
Usage: "number of cpu cores",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_CLONE_VNID",
Name: "proxmoxve-vm-clone-vmid",
Usage: "vmid to clone",
Value: "",
},
mcnflag.IntFlag{
EnvVar: "PROXMOXVE_VM_CLONE_FULL",
Name: "proxmoxve-vm-clone-full",
Usage: "make a full clone or not (0=false, 1=true, 2=use proxmox default logic",
Value: 2,
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_START_ONBOOT",
Name: "proxmoxve-vm-start-onboot",
Usage: "make the VM start automatically onboot (0=false, 1=true, ''=default)",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_PROTECTION",
Name: "proxmoxve-vm-protection",
Usage: "protect the VM and disks from removal (0=false, 1=true, ''=default)",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_CITYPE",
Name: "proxmoxve-vm-citype",
Usage: "cloud-init type (nocloud|configdrive2)",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_CIENABLED",
Name: "proxmoxve-vm-cienabled",
Usage: "cloud-init enabled (implied with clone strategy 0=false, 1=true, ''=default)",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_IMAGE_FILE",
Name: "proxmoxve-vm-image-file",
Usage: "storage of the image file (e.g. local:iso/rancheros-proxmoxve-autoformat.iso)",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_NET_MODEL",
Name: "proxmoxve-vm-net-model",
Usage: "Net Interface model, default virtio (e1000, virtio, realtek, etc...)",
Value: "virtio",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_NET_FIREWALL",
Name: "proxmoxve-vm-net-firewall",
Usage: "enable/disable firewall (0=false, 1=true, ''=default)",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_NET_MTU",
Name: "proxmoxve-vm-net-mtu",
Usage: "set nic mtu (''=default)",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_VM_NET_BRIDGE",
Name: "proxmoxve-vm-net-bridge",
Usage: "bridge to attach network to",
Value: "", // leave the flag default value blank to support the clone default behavior if not explicity set of 'use what is most appropriate'
},
mcnflag.IntFlag{
EnvVar: "PROXMOXVE_VM_NET_TAG",
Name: "proxmoxve-vm-net-tag",
Usage: "vlan tag",
Value: 0,
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_SSH_USERNAME",
Name: "proxmoxve-ssh-username",
Usage: "Username to log in to the guest OS (default docker for rancheros)",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "PROXMOXVE_SSH_PASSWORD",
Name: "proxmoxve-ssh-password",
Usage: "Password to log in to the guest OS (default tcuser for rancheros)",
Value: "",
},
mcnflag.IntFlag{
EnvVar: "PROXMOXVE_SSH_PORT",
Name: "proxmoxve-ssh-port",
Usage: "SSH port in the guest to log in to (defaults to 22)",
Value: 22,
},
mcnflag.BoolFlag{
EnvVar: "PROXMOXVE_DEBUG_RESTY",
Name: "proxmoxve-debug-resty",
Usage: "enables the resty debugging",
},
mcnflag.BoolFlag{
EnvVar: "PROXMOXVE_DEBUG_DRIVER",
Name: "proxmoxve-debug-driver",
Usage: "enables debugging in the driver",
},
}
}
func (d *Driver) ping() bool {
if d.driver == nil {
return false
}
command := NodesNodeQemuVMIDAgentPostParameter{Command: "ping"}
err := d.driver.NodesNodeQemuVMIDAgentPost(d.Node, d.VMID, &command)
if err != nil {
d.debug(err)
return false
}
return true
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "proxmoxve"
}
// SetConfigFromFlags configures all command line arguments
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.debug("SetConfigFromFlags called")
d.ProvisionStrategy = flags.String("proxmoxve-provision-strategy")
// PROXMOX API Connection settings
d.Host = flags.String("proxmoxve-proxmox-host")
d.Node = flags.String("proxmoxve-proxmox-node")
if len(d.Node) == 0 {
d.Node = d.Host
}
d.User = flags.String("proxmoxve-proxmox-user-name")
d.Password = flags.String("proxmoxve-proxmox-user-password")
d.Realm = flags.String("proxmoxve-proxmox-realm")
d.Pool = flags.String("proxmoxve-proxmox-pool")
// VM configuration
d.DiskSize = flags.String("proxmoxve-vm-storage-size")
d.Storage = flags.String("proxmoxve-vm-storage-path")
d.StorageType = strings.ToLower(flags.String("proxmoxve-vm-storage-type"))
d.Memory = flags.Int("proxmoxve-vm-memory")
d.Memory *= 1024
d.VMIDRange = flags.String("proxmoxve-vm-vmid-range")
d.CloneVMID = flags.String("proxmoxve-vm-clone-vmid")
d.CloneFull = flags.Int("proxmoxve-vm-clone-full")
d.Onboot = flags.String("proxmoxve-vm-start-onboot")
d.Protection = flags.String("proxmoxve-vm-protection")
d.Citype = flags.String("proxmoxve-vm-citype")
d.CiEnabled = flags.String("proxmoxve-vm-cienabled")
d.ImageFile = flags.String("proxmoxve-vm-image-file")
d.CPUSockets = flags.String("proxmoxve-vm-cpu-sockets")
d.CPU = flags.String("proxmoxve-vm-cpu")
d.CPUCores = flags.String("proxmoxve-vm-cpu-cores")
d.NetModel = flags.String("proxmoxve-vm-net-model")
d.NetFirewall = flags.String("proxmoxve-vm-net-firewall")
d.NetMtu = flags.String("proxmoxve-vm-net-mtu")
d.NetBridge = flags.String("proxmoxve-vm-net-bridge")
d.NetVlanTag = flags.Int("proxmoxve-vm-net-tag")
d.ScsiController = flags.String("proxmoxve-vm-scsi-controller")
d.ScsiAttributes = flags.String("proxmoxve-vm-scsi-attributes")
//SSH connection settings
d.GuestSSHPort = flags.Int("proxmoxve-ssh-port")
d.GuestUsername = flags.String("proxmoxve-ssh-username")
d.GuestPassword = flags.String("proxmoxve-ssh-password")
//SWARM Settings
d.SwarmMaster = flags.Bool("swarm-master")
d.SwarmHost = flags.String("swarm-host")
//Debug option
d.driverDebug = flags.Bool("proxmoxve-debug-driver")
d.restyDebug = flags.Bool("proxmoxve-debug-resty")
if d.restyDebug {
d.debug("enabling Resty debugging")
resty.SetLogger(log.Output())
resty.SetDebug(true)
}
return nil
}
// GetURL returns the URL for the target docker daemon
func (d *Driver) GetURL() (string, error) {
ip, err := d.GetIP()
if err != nil {
return "", err
}
if ip == "" {
return "", nil
}
return fmt.Sprintf("tcp://%s:2376", ip), nil
}
// GetMachineName returns the machine name
func (d *Driver) GetMachineName() string {
return d.MachineName
}
// GetNetBridge returns the bridge
func (d *Driver) GetNetBridge() string {
return d.NetBridge
}
// GetNetVlanTag returns the vlan tag
func (d *Driver) GetNetVlanTag() int {
return d.NetVlanTag
}
// GetIP returns the ip
func (d *Driver) GetIP() (string, error) {
d.connectAPI()
ip, err := d.driver.GetEth0IPv4(d.Node, d.VMID)
if err != nil {
// TODO: should we return the cached IP here?
return ip, err
}
// set/update IP if success
if d.IPAddress != ip {
d.debugf("driver IP is set as '%s'", ip)
d.IPAddress = ip
}
return ip, err
}
// GetSSHHostname returns the ssh host returned by the API
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
// GetSSHPort returns the ssh port, 22 if not specified
func (d *Driver) GetSSHPort() (int, error) {
return d.GuestSSHPort, nil
}
// GetSSHUsername returns the ssh user name, root if not specified
func (d *Driver) GetSSHUsername() string {
return d.GuestUsername
}
// GetState returns the state of the VM
func (d *Driver) GetState() (state.State, error) {
if len(d.VMID) < 1 {
return state.Error, errors.New("invalid VMID")
}
err := d.connectAPI()
if err != nil {
return state.Error, err
}
// sanity check the UUID
config, err := d.driver.GetConfig(d.Node, d.VMID)
if err != nil {
return state.Error, err
}
cVMMUUID := getUUIDFromSmbios1(config.Data.Smbios1)
if len(d.VMUUID) > 1 && d.VMUUID != cVMMUUID {
return state.Error, fmt.Errorf("UUID mismatch - %s (stored) vs %s (current)", d.VMUUID, cVMMUUID)
}
//Starting
//Running
//Stopped
//Paused
//Error
ip, err := d.GetIP()
// only consider fully running when IP is available
if err == nil && len(ip) > 0 {
return state.Running, nil
}
// starting if qemu-guest-agent is available but no IP
if d.ping() {
return state.Starting, nil
}
pveState, err := d.driver.NodesNodeQemuVMIDStatusCurrentGet(d.Node, d.VMID)
if err != nil {
return state.Error, err
}
switch pveState {
case "stopped":
return state.Stopped, nil
case "running":
return state.Starting, nil
}
return state.Error, fmt.Errorf("unkown error detecting VM state")
}
// PreCreateCheck is called to enforce pre-creation steps
func (d *Driver) PreCreateCheck() error {
err := d.connectAPI()
if err != nil {
return err
}
switch d.ProvisionStrategy {
case "cdrom":
// set defaults for cdrom
// replicating pre-clone behavior of setting a default on the parameter
if len(d.Storage) < 1 {
d.Storage = "local"
}
if len(d.StorageType) < 1 {
d.StorageType = "raw"
}
if len(d.NetBridge) < 1 {
d.NetBridge = "vmbr0"
}
if len(d.GuestUsername) < 1 {
d.GuestUsername = "docker"
}
if len(d.GuestPassword) < 1 {
d.GuestPassword = "tcuser"
}
// prepare StorageFilename
switch d.StorageType {
case "raw":
fallthrough
case "qcow2":
break
default:
return fmt.Errorf("storage type '%s' is not supported", d.StorageType)
}
storageType, err := d.driver.GetStorageType(d.Node, d.Storage)
if err != nil {
return err
}
filename := "-disk-0"
switch storageType {
case "lvmthin":
fallthrough
case "zfs":
fallthrough
case "ceph":
if d.StorageType != "raw" {
return fmt.Errorf("type '%s' on storage '%s' does only support raw", storageType, d.Storage)
}
case "nfs":
fallthrough
case "dir":
filename += "." + d.StorageType
}
// this is not the finale filename, it'll be constructed in Create()
d.StorageFilename = filename
case "clone":
break
default:
return fmt.Errorf("invalid provision strategy '%s'", d.ProvisionStrategy)
}
return nil
}
// Create creates a new VM with storage
func (d *Driver) Create() error {
// create and save a new SSH key pair
d.debug("creating new ssh keypair")
key, err := d.createSSHKey()
if err != nil {
return err
}
// !! Workaround for MC-7982.
key = strings.TrimSpace(key)
key = fmt.Sprintf("%s %s-%d", key, d.MachineName, time.Now().Unix())
// !! End workaround for MC-7982.
// add some random wait time here to help with race conditions in the proxmox api
// the app could be getting invoked several times in rapid succession so some small waits may be helpful
mrand.Seed(time.Now().UnixNano()) // Seed the random number generator using the current time (nanoseconds since epoch)
r := mrand.Intn(5000)
d.debugf("sleeping %d milliseconds before retrieving next ID", r)
time.Sleep(time.Duration(r) * time.Millisecond)
// get next available VMID
// NOTE: we want to lock in the ID as quickly as possible after retrieving (ie: invoke QemuPost or Clone ASAP to avoid race conditions with other instances)
d.debug("Retrieving next ID")
var id string
if len(d.VMIDRange) > 0 {
VMIDParts := strings.Split(d.VMIDRange, ":")
low, err := strconv.Atoi(VMIDParts[0])
if err != nil {
return err
}
var high = 0
if len(VMIDParts) > 1 {
high, err = strconv.Atoi(VMIDParts[1])
if err != nil {
return err
}
}
d.debugf("Looking for available VMID in range '%d:%d'", low, high)
var i = low
for len(id) < 1 {
id, err = d.driver.ClusterNextIDGet(i)
if err != nil && high > 0 && i > high {
return err
}
if high > 0 && i >= high {
return fmt.Errorf("no VMIDs available in range '%d:%d'", low, high)
}
i++
}
} else {
id, err = d.driver.ClusterNextIDGet(0)
if err != nil {
return err
}
}
d.debugf("Next ID is '%s'", id)
d.VMID = id
switch d.ProvisionStrategy {
case "cdrom":
// prefixing StorageFilename with VMID
d.StorageFilename = "vm-" + d.VMID + d.StorageFilename
volume := NodesNodeStorageStorageContentPostParameter{
Filename: d.StorageFilename,
Size: d.DiskSize + "G",
VMID: d.VMID,
}
d.debugf("Creating disk volume '%s' with size '%s'", volume.Filename, volume.Size)
diskname, err := d.driver.NodesNodeStorageStorageContentPost(d.Node, d.Storage, &volume)
if err != nil {
return err
}
if !strings.HasSuffix(diskname, d.StorageFilename) {
return fmt.Errorf("returned diskname is not correct: should be '%s' but was '%s'", d.StorageFilename, diskname)
}
npp := NodesNodeQemuPostParameter{
VMID: d.VMID,
Agent: "1",
Autostart: "1",
Onboot: d.Onboot,
Memory: d.Memory,
Sockets: d.CPUSockets,
Cores: d.CPUCores,
SCSI0: d.StorageFilename,
Ostype: "l26",
Name: d.BaseDriver.MachineName,
KVM: "1", // if you test in a nested environment, you may have to change this to 0 if you do not have nested virtualization
Scsihw: d.ScsiController,
Cdrom: d.ImageFile,
Pool: d.Pool,
Protection: d.Protection,
}
if d.CiEnabled == "1" {
npp.Citype = d.Citype
npp.Ide3 = d.Storage + ":cloudinit"
}
if len(d.ScsiAttributes) > 0 {
npp.SCSI0 += "," + d.ScsiAttributes
}
if len(d.NUMA) > 0 {
npp.NUMA = d.NUMA
}
if len(d.CPU) > 0 {
npp.CPU = d.CPU
}
npp.Net0, _ = d.generateNetString()
if d.StorageType == "qcow2" {
npp.SCSI0 = d.Storage + ":" + d.VMID + "/" + volume.Filename
} else if d.StorageType == "raw" {
if strings.HasSuffix(volume.Filename, ".raw") {
// raw files (having .raw) should have the VMID in the path
npp.SCSI0 = d.Storage + ":" + d.VMID + "/" + volume.Filename
} else {
npp.SCSI0 = d.Storage + ":" + volume.Filename
}
}
d.debugf("Creating VM '%s' with '%d' of memory", npp.VMID, npp.Memory)
taskid, err := d.driver.NodesNodeQemuPost(d.Node, &npp)
if err != nil {
return err
}
err = d.driver.WaitForTaskToComplete(d.Node, taskid)
if err != nil {
return err
}
if d.CiEnabled == "1" {
// specially handle setting sshkeys
// https://forum.proxmox.com/threads/how-to-use-pvesh-set-vms-sshkeys.52570/
taskid, err = d.driver.NodesNodeQemuVMIDConfigSetSSHKeys(d.Node, d.VMID, key)
if err != nil {
return err
}
err = d.driver.WaitForTaskToComplete(d.Node, taskid)
if err != nil {
return err
}
}
break
case "clone":
// clone
clone := NodesNodeQemuVMIDClonePostParameter{
Newid: d.VMID,
Name: d.BaseDriver.MachineName,
Pool: d.Pool,
}
switch d.CloneFull {
case 0:
clone.Full = "0"
break
case 1:
clone.Full = "1"
clone.Format = d.StorageType
clone.Storage = d.Storage
break
case 2:
clone.Format = d.StorageType
clone.Storage = d.Storage
break
}
d.debugf("cloning template id '%s' as vmid '%s'", d.CloneVMID, clone.Newid)
taskid, err := d.driver.NodesNodeQemuVMIDClonePost(d.Node, d.CloneVMID, &clone)
if err != nil {
return err
}
err = d.driver.WaitForTaskToComplete(d.Node, taskid)
if err != nil {
return err
}
// resize
resize := NodesNodeQemuVMIDResizePutParameter{
Disk: "scsi0",
Size: d.DiskSize + "G",
}
d.debugf("resizing disk '%s' on vmid '%s' to '%s'", resize.Disk, d.VMID, resize.Size)
err = d.driver.NodesNodeQemuVMIDResizePut(d.Node, d.VMID, &resize)
if err != nil {
return err
}
// set config values
d.debugf("setting VM config values for vmid '%s'", d.VMID)
npp := NodesNodeQemuPostParameter{
Agent: "1",
Autostart: "1",
Memory: d.Memory,
Sockets: d.CPUSockets,
Cores: d.CPUCores,
KVM: "1", // if you test in a nested environment, you may have to change this to 0 if you do not have nested virtualization,
Citype: d.Citype,
Onboot: d.Onboot,
Protection: d.Protection,
}
if len(d.NetBridge) > 0 {
npp.Net0, _ = d.generateNetString()
}
if len(d.NUMA) > 0 {
npp.NUMA = d.NUMA
}
if len(d.CPU) > 0 {
npp.CPU = d.CPU
}
taskid, err = d.driver.NodesNodeQemuVMIDConfigPost(d.Node, d.VMID, &npp)
if err != nil {
return err
}
// append newly minted ssh key to existing (if any)
d.debugf("retrieving existing cloud-init sshkeys from vmid '%s'", d.VMID)
config, err := d.driver.GetConfig(d.Node, d.CloneVMID)
if err != nil {
return err
}
var SSHKeys string
if len(config.Data.SSHKeys) > 0 {
SSHKeys, err = url.QueryUnescape(config.Data.SSHKeys)
if err != nil {
return err
}
SSHKeys = strings.TrimSpace(SSHKeys)
SSHKeys += "\n"
}
SSHKeys += key
SSHKeys = strings.TrimSpace(SSHKeys)
// specially handle setting sshkeys
// https://forum.proxmox.com/threads/how-to-use-pvesh-set-vms-sshkeys.52570/
taskid, err = d.driver.NodesNodeQemuVMIDConfigSetSSHKeys(d.Node, d.VMID, SSHKeys)
if err != nil {
return err
}
err = d.driver.WaitForTaskToComplete(d.Node, taskid)
if err != nil {
return err
}
break
default:
return fmt.Errorf("invalid provision strategy '%s'", d.ProvisionStrategy)
}
// Set the newly minted UUID
config, err := d.driver.GetConfig(d.Node, d.VMID)
if err != nil {
return err
}
d.VMUUID = getUUIDFromSmbios1(config.Data.Smbios1)
d.debugf("VM created with uuid '%s'", d.VMUUID)
// start the VM
err = d.Start()
if err != nil {
return err
}
// let VM start a settle a little
d.debugf("waiting for VM to start, wait 10 seconds")
time.Sleep(10 * time.Second)
// wait for qemu-guest-agent
err = d.waitForQemuGuestAgent()
if err != nil {
return err
}
// wait for network to come up
err = d.waitForNetwork()
// set the IPAddress
_, err = d.GetIP()
if err != nil {
return err
}
switch d.ProvisionStrategy {
case "cdrom":
if len(d.GuestPassword) > 0 {
return d.prepareSSHWithPassword()
}
return nil
case "clone":
fallthrough
default:
return nil
}
}
func (d *Driver) waitForQemuGuestAgent() error {
d.debugf("waiting for VM qemu-guest-agent to start")
d.connectAPI()
for !d.ping() {
d.debugf("waiting for VM qemu-guest-agent to start")
time.Sleep(5 * time.Second)
}
return nil
}
func (d *Driver) waitForNetwork() error {
d.debugf("waiting for VM network to start")
d.connectAPI()
var up = false
var ip string
var err error
for !up {
ip, err = d.driver.GetEth0IPv4(d.Node, d.VMID)
if err != nil {
d.debugf("waiting for VM network to start")
time.Sleep(5 * time.Second)
} else {
if len(ip) > 0 {
up = true
d.debugf("VM network started with ip: %s", ip)
} else {
d.debugf("waiting for VM network to start")
time.Sleep(5 * time.Second)
}
}
}
return nil
}
func (d *Driver) generateNetString() (string, error) {
var net string = fmt.Sprintf("model=%s,bridge=%s", d.NetModel, d.NetBridge)
if d.NetVlanTag != 0 {
net = fmt.Sprintf(net+",tag=%d", d.NetVlanTag)
}
if len(d.NetFirewall) > 0 {
net = fmt.Sprintf(net+",firewall=%s", d.NetFirewall)
}
if len(d.NetMtu) > 0 {
net = fmt.Sprintf(net+",mtu=%s", d.NetMtu)
}
return net, nil
}
func (d *Driver) prepareSSHWithPassword() error {
sshConfig := &ssh.ClientConfig{
User: d.GetSSHUsername(),
Auth: []ssh.AuthMethod{
ssh.Password(d.GuestPassword)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
sshbasedir := "/home/" + d.GetSSHUsername() + "/.ssh"
hostname, _ := d.GetSSHHostname()
port, _ := d.GetSSHPort()
clientstr := fmt.Sprintf("%s:%d", hostname, port)
d.debugf("Creating directory '%s'", sshbasedir)
conn, err := ssh.Dial("tcp", clientstr, sshConfig)