github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/parallels/common/step_attach_floppy.go (about)

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