github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/hyperv/common/step_unmount_dvddrive.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  type StepUnmountDvdDrive struct {
    12  }
    13  
    14  func (s *StepUnmountDvdDrive) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    15  	driver := state.Get("driver").(Driver)
    16  	vmName := state.Get("vmName").(string)
    17  	ui := state.Get("ui").(packer.Ui)
    18  
    19  	ui.Say("Unmount/delete os dvd drive...")
    20  
    21  	dvdControllerState := state.Get("os.dvd.properties")
    22  
    23  	if dvdControllerState == nil {
    24  		return multistep.ActionContinue
    25  	}
    26  
    27  	dvdController := dvdControllerState.(DvdControllerProperties)
    28  
    29  	if dvdController.Existing {
    30  		ui.Say(fmt.Sprintf("Unmounting os dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
    31  		err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    32  		if err != nil {
    33  			err := fmt.Errorf("Error unmounting os dvd drive: %s", err)
    34  			state.Put("error", err)
    35  			ui.Error(err.Error())
    36  			return multistep.ActionHalt
    37  		}
    38  	} else {
    39  		ui.Say(fmt.Sprintf("Delete os dvd drives controller %d location %d ...", dvdController.ControllerNumber, dvdController.ControllerLocation))
    40  		err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    41  		if err != nil {
    42  			err := fmt.Errorf("Error deleting os dvd drive: %s", err)
    43  			state.Put("error", err)
    44  			ui.Error(err.Error())
    45  			return multistep.ActionHalt
    46  		}
    47  	}
    48  
    49  	state.Put("os.dvd.properties", nil)
    50  
    51  	return multistep.ActionContinue
    52  }
    53  
    54  func (s *StepUnmountDvdDrive) Cleanup(state multistep.StateBag) {
    55  }