github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_unmount_secondary_dvd_images.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 StepUnmountSecondaryDvdImages struct {
    12  }
    13  
    14  func (s *StepUnmountSecondaryDvdImages) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    15  	driver := state.Get("driver").(Driver)
    16  	ui := state.Get("ui").(packer.Ui)
    17  	vmName := state.Get("vmName").(string)
    18  
    19  	ui.Say("Unmount/delete secondary dvd drives...")
    20  
    21  	dvdControllersState := state.Get("secondary.dvd.properties")
    22  
    23  	if dvdControllersState == nil {
    24  		return multistep.ActionContinue
    25  	}
    26  
    27  	dvdControllers := dvdControllersState.([]DvdControllerProperties)
    28  
    29  	for _, dvdController := range dvdControllers {
    30  		if dvdController.Existing {
    31  			ui.Say(fmt.Sprintf("Unmounting secondary dvd drives controller %d location %d ...",
    32  				dvdController.ControllerNumber, dvdController.ControllerLocation))
    33  			err := driver.UnmountDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    34  			if err != nil {
    35  				err := fmt.Errorf("Error unmounting secondary dvd drive: %s", err)
    36  				state.Put("error", err)
    37  				ui.Error(err.Error())
    38  				return multistep.ActionHalt
    39  			}
    40  		} else {
    41  			ui.Say(fmt.Sprintf("Delete secondary dvd drives controller %d location %d ...",
    42  				dvdController.ControllerNumber, dvdController.ControllerLocation))
    43  			err := driver.DeleteDvdDrive(vmName, dvdController.ControllerNumber, dvdController.ControllerLocation)
    44  			if err != nil {
    45  				err := fmt.Errorf("Error deleting secondary dvd drive: %s", err)
    46  				state.Put("error", err)
    47  				ui.Error(err.Error())
    48  				return multistep.ActionHalt
    49  			}
    50  		}
    51  	}
    52  
    53  	state.Put("secondary.dvd.properties", nil)
    54  
    55  	return multistep.ActionContinue
    56  }
    57  
    58  func (s *StepUnmountSecondaryDvdImages) Cleanup(state multistep.StateBag) {
    59  }