github.com/alouche/packer@v0.3.7/builder/virtualbox/step_export.go (about)

     1  package virtualbox
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"path/filepath"
     8  )
     9  
    10  // This step cleans up forwarded ports and exports the VM to an OVF.
    11  //
    12  // Uses:
    13  //
    14  // Produces:
    15  //   exportPath string - The path to the resulting export.
    16  type stepExport struct{}
    17  
    18  func (s *stepExport) Run(state multistep.StateBag) multistep.StepAction {
    19  	config := state.Get("config").(*config)
    20  	driver := state.Get("driver").(Driver)
    21  	ui := state.Get("ui").(packer.Ui)
    22  	vmName := state.Get("vmName").(string)
    23  
    24  	// Clear out the Packer-created forwarding rule
    25  	ui.Say("Preparing to export machine...")
    26  	ui.Message(fmt.Sprintf("Deleting forwarded port mapping for SSH (host port %d)", state.Get("sshHostPort")))
    27  	command := []string{"modifyvm", vmName, "--natpf1", "delete", "packerssh"}
    28  	if err := driver.VBoxManage(command...); err != nil {
    29  		err := fmt.Errorf("Error deleting port forwarding rule: %s", err)
    30  		state.Put("error", err)
    31  		ui.Error(err.Error())
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	// Remove the attached floppy disk, if it exists
    36  	if _, ok := state.GetOk("floppy_path"); ok {
    37  		ui.Message("Removing floppy drive...")
    38  		command := []string{
    39  			"storageattach", vmName,
    40  			"--storagectl", "Floppy Controller",
    41  			"--port", "0",
    42  			"--device", "0",
    43  			"--medium", "none",
    44  		}
    45  		if err := driver.VBoxManage(command...); err != nil {
    46  			state.Put("error", fmt.Errorf("Error removing floppy: %s", err))
    47  			return multistep.ActionHalt
    48  		}
    49  
    50  	}
    51  
    52  	// Export the VM to an OVF
    53  	outputPath := filepath.Join(config.OutputDir, vmName+"."+config.Format)
    54  
    55  	command = []string{
    56  		"export",
    57  		vmName,
    58  		"--output",
    59  		outputPath,
    60  	}
    61  
    62  	ui.Say("Exporting virtual machine...")
    63  	err := driver.VBoxManage(command...)
    64  	if err != nil {
    65  		err := fmt.Errorf("Error exporting virtual machine: %s", err)
    66  		state.Put("error", err)
    67  		ui.Error(err.Error())
    68  		return multistep.ActionHalt
    69  	}
    70  
    71  	state.Put("exportPath", outputPath)
    72  
    73  	return multistep.ActionContinue
    74  }
    75  
    76  func (s *stepExport) Cleanup(state multistep.StateBag) {}