github.com/timsutton/packer@v1.3.2/builder/parallels/iso/step_attach_iso.go (about)

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