github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/iso/step_attach_iso.go (about)

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