github.com/rothwerx/packer@v0.9.0/builder/parallels/iso/step_attach_iso.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
     7  	"github.com/mitchellh/packer/packer"
     8  	"log"
     9  )
    10  
    11  // This step attaches the ISO to the virtual machine.
    12  //
    13  // Uses:
    14  //   driver Driver
    15  //   isoPath string
    16  //   ui packer.Ui
    17  //   vmName string
    18  //
    19  // Produces:
    20  //	 attachedIso bool
    21  type stepAttachISO struct{}
    22  
    23  func (s *stepAttachISO) Run(state multistep.StateBag) multistep.StepAction {
    24  	driver := state.Get("driver").(parallelscommon.Driver)
    25  	isoPath := state.Get("iso_path").(string)
    26  	ui := state.Get("ui").(packer.Ui)
    27  	vmName := state.Get("vmName").(string)
    28  
    29  	// Attach the disk to the cdrom0 device. We couldn't use a separated device because it is failed to boot in PD9 [GH-1667]
    30  	ui.Say("Attaching ISO to the default CD/DVD ROM device...")
    31  	command := []string{
    32  		"set", vmName,
    33  		"--device-set", "cdrom0",
    34  		"--image", isoPath,
    35  		"--enable", "--connect",
    36  	}
    37  	if err := driver.Prlctl(command...); err != nil {
    38  		err := fmt.Errorf("Error attaching ISO: %s", err)
    39  		state.Put("error", err)
    40  		ui.Error(err.Error())
    41  		return multistep.ActionHalt
    42  	}
    43  
    44  	// Set some state so we know to remove
    45  	state.Put("attachedIso", true)
    46  
    47  	return multistep.ActionContinue
    48  }
    49  
    50  func (s *stepAttachISO) Cleanup(state multistep.StateBag) {
    51  	if _, ok := state.GetOk("attachedIso"); !ok {
    52  		return
    53  	}
    54  
    55  	driver := state.Get("driver").(parallelscommon.Driver)
    56  	ui := state.Get("ui").(packer.Ui)
    57  	vmName := state.Get("vmName").(string)
    58  
    59  	// Detach ISO by setting an empty string image.
    60  	log.Println("Detaching ISO from the default CD/DVD ROM device...")
    61  	command := []string{
    62  		"set", vmName,
    63  		"--device-set", "cdrom0",
    64  		"--image", "", "--disconnect", "--enable",
    65  	}
    66  
    67  	if err := driver.Prlctl(command...); err != nil {
    68  		ui.Error(fmt.Sprintf("Error detaching ISO: %s", err))
    69  	}
    70  }