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

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  // StepAttachFloppy is a step that attaches a floppy to the virtual machine.
    12  //
    13  // Uses:
    14  //   driver Driver
    15  //   ui packer.Ui
    16  //   vmName string
    17  //
    18  // Produces:
    19  type StepAttachFloppy struct {
    20  	floppyPath string
    21  }
    22  
    23  // Run adds a virtual FDD device to the VM and attaches the image.
    24  // If the image is not specified, then this step will be skipped.
    25  func (s *StepAttachFloppy) Run(state multistep.StateBag) multistep.StepAction {
    26  	// Determine if we even have a floppy disk to attach
    27  	var floppyPath string
    28  	if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
    29  		floppyPath = floppyPathRaw.(string)
    30  	} else {
    31  		log.Println("No floppy disk, not attaching.")
    32  		return multistep.ActionContinue
    33  	}
    34  
    35  	driver := state.Get("driver").(Driver)
    36  	ui := state.Get("ui").(packer.Ui)
    37  	vmName := state.Get("vmName").(string)
    38  
    39  	ui.Say("Deleting any current floppy disk...")
    40  	// Delete the floppy disk controller
    41  	delCommand := []string{
    42  		"set", vmName,
    43  		"--device-del", "fdd0",
    44  	}
    45  	// This will almost certainly fail with 'The fdd0 device does not exist.'
    46  	driver.Prlctl(delCommand...)
    47  
    48  	ui.Say("Attaching floppy disk...")
    49  	// Attaching the floppy disk
    50  	addCommand := []string{
    51  		"set", vmName,
    52  		"--device-add", "fdd",
    53  		"--image", floppyPath,
    54  		"--connect",
    55  	}
    56  	if err := driver.Prlctl(addCommand...); err != nil {
    57  		state.Put("error", fmt.Errorf("Error adding floppy: %s", err))
    58  		return multistep.ActionHalt
    59  	}
    60  
    61  	// Track the path so that we can unregister it from Parallels later
    62  	s.floppyPath = floppyPath
    63  
    64  	return multistep.ActionContinue
    65  }
    66  
    67  // Cleanup removes the virtual FDD device attached to the VM.
    68  func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) {
    69  	driver := state.Get("driver").(Driver)
    70  	vmName := state.Get("vmName").(string)
    71  
    72  	if s.floppyPath == "" {
    73  		return
    74  	}
    75  
    76  	log.Println("Detaching floppy disk...")
    77  	command := []string{
    78  		"set", vmName,
    79  		"--device-del", "fdd0",
    80  	}
    81  	driver.Prlctl(command...)
    82  }