Skip to content

Commit

Permalink
update vmwarevsphere to support datastore-clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
luthermonson authored and Alena Prokharchyk committed Nov 8, 2019
1 parent 1365624 commit 7d5e7e3
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 62 deletions.
47 changes: 26 additions & 21 deletions drivers/vmwarevsphere/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@ func (d *Driver) cloudInit(vm *object.VirtualMachine) error {
return d.cloudInitGuestInfo(vm)
}

if err := d.createCloudInitIso(); err != nil {
return err
}
if d.CloudConfig == "" {
if err := d.createCloudInitIso(); err != nil {
return err
}

dss, err := d.finder.DatastoreOrDefault(d.getCtx(), d.Datastore)
if err != nil {
return err
}
ds, err := d.getVmDatastore(vm)
if err != nil {
return err
}

err = d.uploadCloudInitIso(vm, d.datacenter, dss)
if err != nil {
return err
}
err = d.uploadCloudInitIso(vm, d.datacenter, ds)
if err != nil {
return err
}

err = d.mountCloudInitIso(vm, d.datacenter, dss)
if err != nil {
return err
err = d.mountCloudInitIso(vm, d.datacenter, ds)
if err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -78,13 +80,14 @@ func (d *Driver) cloudInitGuestInfo(vm *object.VirtualMachine) error {
return d.applyOpts(vm, opts)
}

func (d *Driver) uploadCloudInitIso(vm *object.VirtualMachine, dc *object.Datacenter, dss *object.Datastore) error {
path, err := d.getFolder(vm)
func (d *Driver) uploadCloudInitIso(vm *object.VirtualMachine, dc *object.Datacenter, ds *object.Datastore) error {
log.Infof("Uploading cloud-init.iso")
path, err := d.getVmFolder(vm)
if err != nil {
return err
}

dsurl, err := dss.URL(d.getCtx(), dc, filepath.Join(path, isoName))
dsurl, err := ds.URL(d.getCtx(), dc, filepath.Join(path, isoName))
if err != nil {
return err
}
Expand All @@ -102,19 +105,20 @@ func (d *Driver) uploadCloudInitIso(vm *object.VirtualMachine, dc *object.Datace
return nil
}

func (d *Driver) removeCloudInitIso(vm *object.VirtualMachine, dc *object.Datacenter, dss *object.Datastore) error {
func (d *Driver) removeCloudInitIso(vm *object.VirtualMachine, dc *object.Datacenter, ds *object.Datastore) error {
log.Infof("Removing cloud-init.iso")
c, err := d.getSoapClient()
if err != nil {
return err
}

path, err := d.getFolder(vm)
path, err := d.getVmFolder(vm)
if err != nil {
return err
}

m := object.NewFileManager(c.Client)
task, err := m.DeleteDatastoreFile(d.getCtx(), dss.Path(filepath.Join(path, isoName)), dc)
task, err := m.DeleteDatastoreFile(d.getCtx(), ds.Path(filepath.Join(path, isoName)), dc)
if err != nil {
return err
}
Expand All @@ -132,6 +136,7 @@ func (d *Driver) removeCloudInitIso(vm *object.VirtualMachine, dc *object.Datace
}

func (d *Driver) createCloudInitIso() error {
log.Infof("Creating cloud-init.iso")
//d.CloudConfig stat'ed and loaded in flag load.
sshkey, err := ioutil.ReadFile(d.publicSSHKeyPath())
if err != nil {
Expand Down Expand Up @@ -224,7 +229,7 @@ func (d *Driver) mountCloudInitIso(vm *object.VirtualMachine, dc *object.Datacen
return err
}

path, err := d.getFolder(vm)
path, err := d.getVmFolder(vm)
if err != nil {
return err
}
Expand Down
67 changes: 40 additions & 27 deletions drivers/vmwarevsphere/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,15 @@ func (d *Driver) postCreate(vm *object.VirtualMachine) error {
return d.Start()
}

func (d *Driver) createManual() error {
func (d *Driver) createLegacy() error {
c, err := d.getSoapClient()
if err != nil {
return err
}

dss, err := d.finder.DatastoreOrDefault(d.getCtx(), d.Datastore)
if err != nil {
return err
}

spec := types.VirtualMachineConfigSpec{
Name: d.MachineName,
GuestId: "otherLinux64Guest",
Files: &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", dss.Name())},
NumCPUs: int32(d.CPU),
MemoryMB: int64(d.Memory),
}
Expand Down Expand Up @@ -190,6 +184,15 @@ func (d *Driver) createManual() error {
}
}

ds, err := d.getDatastore(&spec)
if err != nil {
return err
}

spec.Files = &types.VirtualMachineFileInfo{
VmPathName: fmt.Sprintf("[%s]", ds.Name()),
}

task, err := folder.CreateVM(d.getCtx(), spec, d.resourcepool, d.hostsystem)
if err != nil {
return err
Expand All @@ -202,12 +205,12 @@ func (d *Driver) createManual() error {

log.Infof("Uploading Boot2docker ISO ...")
vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference))
vmPath, err := d.getFolder(vm)
vmPath, err := d.getVmFolder(vm)
if err != nil {
return err
}

dsurl, err := dss.URL(d.getCtx(), d.datacenter, filepath.Join(vmPath, isoFilename))
dsurl, err := ds.URL(d.getCtx(), d.datacenter, filepath.Join(vmPath, isoFilename))
if err != nil {
return err
}
Expand All @@ -222,18 +225,16 @@ func (d *Driver) createManual() error {
}

var add []types.BaseVirtualDevice

controller, err := devices.FindDiskController("scsi")
if err != nil {
return err
}

disk := devices.CreateDisk(controller, dss.Reference(),
dss.Path(fmt.Sprintf("%s/%s.vmdk", d.MachineName, d.MachineName)))
disk := devices.CreateDisk(controller, ds.Reference(),
ds.Path(fmt.Sprintf("%s/%s.vmdk", vmPath, d.MachineName)))

// Convert MB to KB
disk.CapacityInKB = int64(d.DiskSize) * 1024

add = append(add, disk)
ide, err := devices.FindIDEController("")
if err != nil {
Expand All @@ -245,8 +246,7 @@ func (d *Driver) createManual() error {
return err
}

add = append(add, devices.InsertIso(cdrom, dss.Path(fmt.Sprintf("%s/%s", d.MachineName, isoFilename))))

add = append(add, devices.InsertIso(cdrom, ds.Path(fmt.Sprintf("%s/%s", vmPath, isoFilename))))
if err := vm.AddDevice(d.getCtx(), add...); err != nil {
return err
}
Expand All @@ -273,11 +273,6 @@ func (d *Driver) createFromVmName() error {
return err
}

dss, err := d.finder.DatastoreOrDefault(d.getCtx(), d.Datastore)
if err != nil {
return err
}

var info *types.TaskInfo
ref := d.resourcepool.Reference()
spec := types.VirtualMachineCloneSpec{
Expand All @@ -286,12 +281,23 @@ func (d *Driver) createFromVmName() error {
},
Config: &types.VirtualMachineConfigSpec{
GuestId: "otherLinux64Guest",
Files: &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", dss.Name())},
NumCPUs: int32(d.CPU),
MemoryMB: int64(d.Memory),
},
}

ds, err := d.getDatastore(spec.Config)
if err != nil {
return err
}

spec.Config.Files = &types.VirtualMachineFileInfo{
VmPathName: fmt.Sprintf("[%s]", ds.Name()),
}

dsref := ds.Reference()
spec.Location.Datastore = &dsref

vm2Clone, err := d.fetchVM(d.CloneFrom)
if err != nil {
return err
Expand All @@ -301,8 +307,15 @@ func (d *Driver) createFromVmName() error {
if err != nil {
return err
}
folder := folders.VmFolder
if d.Folder != "" {
folder, err = d.finder.Folder(d.getCtx(), fmt.Sprintf("%s/%s", folders.VmFolder.InventoryPath, d.Folder))
if err != nil {
return err
}
}

task, err := vm2Clone.Clone(d.getCtx(), folders.VmFolder, d.MachineName, spec)
task, err := vm2Clone.Clone(d.getCtx(), folder, d.MachineName, spec)
if err != nil {
return err
}
Expand All @@ -327,11 +340,6 @@ func (d *Driver) createFromLibraryName() error {
return err
}

ds, err := d.finder.DatastoreOrDefault(d.getCtx(), d.Datastore)
if err != nil {
return err
}

folders, err := d.datacenter.Folders(d.getCtx())
if err != nil {
return err
Expand Down Expand Up @@ -374,6 +382,11 @@ func (d *Driver) createFromLibraryName() error {
hostId = d.hostsystem.Reference().Value
}

ds, err := d.getDatastore(&types.VirtualMachineConfigSpec{})
if err != nil {
return err
}

deploy := vcenter.Deploy{
DeploymentSpec: vcenter.DeploymentSpec{
Name: d.MachineName,
Expand Down
14 changes: 10 additions & 4 deletions drivers/vmwarevsphere/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,22 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
mcnflag.StringSliceFlag{
EnvVar: "VSPHERE_NETWORK",
Name: "vmwarevsphere-network",
Usage: "vSphere network where the docker VM will be attached",
Usage: "vSphere network where the virtual machine will be attached",
},
mcnflag.StringFlag{
EnvVar: "VSPHERE_DATASTORE",
Name: "vmwarevsphere-datastore",
Usage: "vSphere datastore for docker VM",
Usage: "vSphere datastore for virtual machine",
},
mcnflag.StringFlag{
EnvVar: "VSPHERE_DATASTORE_CLUSTER",
Name: "vmwarevsphere-datastore-cluster",
Usage: "vSphere datastore cluster for virtual machine",
},
mcnflag.StringFlag{
EnvVar: "VSPHERE_DATACENTER",
Name: "vmwarevsphere-datacenter",
Usage: "vSphere datacenter for docker VM",
Usage: "vSphere datacenter for virtual machine",
},
mcnflag.StringFlag{
EnvVar: "VSPHERE_FOLDER",
Expand Down Expand Up @@ -199,6 +204,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.Tags = flags.StringSlice("vmwarevsphere-tag")
d.CustomAttributes = flags.StringSlice("vmwarevsphere-custom-attribute")
d.Datastore = flags.String("vmwarevsphere-datastore")
d.DatastoreCluster = flags.String("vmwarevsphere-datastore-cluster")
d.Datacenter = flags.String("vmwarevsphere-datacenter")
// Sanitize input on ingress.
d.Folder = strings.Trim(flags.String("vmwarevsphere-folder"), "/")
Expand Down Expand Up @@ -234,7 +240,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
if d.CreationType != "legacy" {
d.CloneFrom = flags.String("vmwarevsphere-clone-from")
if d.CloneFrom == "" {
return fmt.Errorf("Creation type clone needs a VM name to clone from, use --vmwarevsphere-clone-from.")
return fmt.Errorf("creation type clone needs a VM name to clone from, use --vmwarevsphere-clone-from")
}
}

Expand Down
Loading

0 comments on commit 7d5e7e3

Please sign in to comment.