github.phpd.cn/hashicorp/packer@v1.3.2/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 ...",
    31  			dvdController.ControllerNumber, dvdController.ControllerLocation))
    32  		err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    33  		if err != nil {
    34  			err := fmt.Errorf("Error unmounting os dvd drive: %s", err)
    35  			state.Put("error", err)
    36  			ui.Error(err.Error())
    37  			return multistep.ActionHalt
    38  		}
    39  	} else {
    40  		ui.Say(fmt.Sprintf("Delete os dvd drives controller %d location %d ...",
    41  			dvdController.ControllerNumber, dvdController.ControllerLocation))
    42  		err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    43  		if err != nil {
    44  			err := fmt.Errorf("Error deleting os dvd drive: %s", err)
    45  			state.Put("error", err)
    46  			ui.Error(err.Error())
    47  			return multistep.ActionHalt
    48  		}
    49  	}
    50  
    51  	state.Put("os.dvd.properties", nil)
    52  
    53  	return multistep.ActionContinue
    54  }
    55  
    56  func (s *StepUnmountDvdDrive) Cleanup(state multistep.StateBag) {
    57  }