Skip to content

Commit

Permalink
Merge pull request #254 from jiaqiluo/fix-vsphere
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaqiluo authored Jul 31, 2024
2 parents 62bec00 + 05caa37 commit 42a1241
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions drivers/vmwarevsphere/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func (d *Driver) createLegacy() error {
if err != nil {
return err
}
log.Info("Fetching MachineID ...")
// save the machine id as soon as the VM is created
if _, err = d.GetMachineId(); err != nil {
// no need to return the error, it is not a blocker for creating the machine,
// we will fetch the machineID again after starting the VM
log.Warnf("[createLegacy] failed to fetch MachineID for %s: %v", d.MachineName, err)
}

log.Infof("Uploading Boot2docker ISO ...")
vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference))
Expand Down Expand Up @@ -274,6 +281,14 @@ func (d *Driver) createFromVmName() error {
return err
}

log.Info("Fetching MachineID ...")
// save the machine id as soon as the VM is created
if _, err = d.GetMachineId(); err != nil {
// no need to return the error, it is not a blocker for creating the machine,
// we will fetch the machineID again after starting the VM
log.Warnf("[createFromVmName] failed to fetch MachineID for %s: %v", d.MachineName, err)
}

// Retrieve the new VM
vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference))
if err := d.addNetworks(vm, d.networks); err != nil {
Expand Down Expand Up @@ -357,6 +372,14 @@ func (d *Driver) createFromLibraryName() error {
return err
}

log.Info("Fetching MachineID ...")
// save the machine id as soon as the VM is created
if _, err = d.GetMachineId(); err != nil {
// no need to return the error, it is not a blocker for creating the machine,
// we will fetch the machineID again after starting the VM
log.Warnf("[createFromLibraryName] failed to fetch MachineID for %s: %v", d.MachineName, err)
}

// At this point, the VM is deployed from content library with defaults from template
// Reconfiguration of the VM based on driver inputs follows

Expand Down
1 change: 1 addition & 0 deletions drivers/vmwarevsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ func (d *Driver) Kill() error {
func (d *Driver) Remove() error {
if d.MachineId == "" {
// no guid from config, nothing in vsphere to delete
log.Info("MachineID is empty, skipping removing VM")
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions libmachine/libmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ func (api *Client) Create(h *host.Host) error {
log.Info("Creating machine...")

if err := api.performCreate(h); err != nil {
if h.Driver.DriverName() == "vmwarevsphere" {
// it is possible that the VM is instantiated but fails to bootstrap,
// save the machine to the store, so the VM and associated resources can be found and destroyed later
if err := api.Save(h); err != nil {
log.Warnf("Error saving host to store after creation fails: %s", err)
}
}
return fmt.Errorf("Error creating machine: %s", err)
}

Expand Down

0 comments on commit 42a1241

Please sign in to comment.