github.com/sneal/packer@v0.5.2/builder/virtualbox/common/step_remove_devices.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  )
     8  
     9  // This step removes any devices (floppy disks, ISOs, etc.) from the
    10  // machine that we may have added.
    11  //
    12  // Uses:
    13  //   driver Driver
    14  //   ui packer.Ui
    15  //   vmName string
    16  //
    17  // Produces:
    18  type StepRemoveDevices struct{}
    19  
    20  func (s *StepRemoveDevices) Run(state multistep.StateBag) multistep.StepAction {
    21  	driver := state.Get("driver").(Driver)
    22  	ui := state.Get("ui").(packer.Ui)
    23  	vmName := state.Get("vmName").(string)
    24  
    25  	// Remove the attached floppy disk, if it exists
    26  	if _, ok := state.GetOk("floppy_path"); ok {
    27  		ui.Message("Removing floppy drive...")
    28  		command := []string{
    29  			"storageattach", vmName,
    30  			"--storagectl", "Floppy Controller",
    31  			"--port", "0",
    32  			"--device", "0",
    33  			"--medium", "none",
    34  		}
    35  		if err := driver.VBoxManage(command...); err != nil {
    36  			err := fmt.Errorf("Error removing floppy: %s", err)
    37  			state.Put("error", err)
    38  			ui.Error(err.Error())
    39  			return multistep.ActionHalt
    40  		}
    41  	}
    42  
    43  	if _, ok := state.GetOk("attachedIso"); ok {
    44  		command := []string{
    45  			"storageattach", vmName,
    46  			"--storagectl", "IDE Controller",
    47  			"--port", "0",
    48  			"--device", "1",
    49  			"--medium", "none",
    50  		}
    51  
    52  		if err := driver.VBoxManage(command...); err != nil {
    53  			err := fmt.Errorf("Error detaching ISO: %s", err)
    54  			state.Put("error", err)
    55  			ui.Error(err.Error())
    56  			return multistep.ActionHalt
    57  		}
    58  	}
    59  
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *StepRemoveDevices) Cleanup(state multistep.StateBag) {
    64  }