packer-cn/builder/virtualbox/common/step_export.go

94 lines
2.4 KiB
Go
Raw Normal View History

2013-12-22 13:40:39 -05:00
package common
2013-06-12 21:07:08 -04:00
import (
"context"
2013-06-12 21:07:08 -04:00
"fmt"
2013-10-09 13:14:09 -04:00
"path/filepath"
2014-03-13 23:26:26 -04:00
"strings"
2018-01-22 20:21:10 -05:00
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
2013-06-12 21:07:08 -04:00
)
// This step cleans up forwarded ports and exports the VM to an OVF.
2013-06-12 21:07:08 -04:00
//
// Uses:
//
// Produces:
// exportPath string - The path to the resulting export.
2013-12-22 13:40:39 -05:00
type StepExport struct {
Format string
OutputDir string
2020-05-05 19:09:05 -04:00
OutputFilename string
ExportOpts []string
Bundling VBoxBundleConfig
SkipNatMapping bool
SkipExport bool
2013-12-22 13:40:39 -05:00
}
2013-06-12 21:07:08 -04:00
func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
// If ISO export is configured, ensure this option is propagated to VBoxManage.
2018-11-27 12:33:29 -05:00
for _, option := range s.ExportOpts {
if option == "--iso" || option == "-I" {
s.ExportOpts = append(s.ExportOpts, "--iso")
2018-11-27 12:33:29 -05:00
break
}
}
2013-12-22 13:40:39 -05:00
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)
2013-08-31 15:44:58 -04:00
vmName := state.Get("vmName").(string)
2020-05-05 19:09:05 -04:00
if s.OutputFilename == "" {
s.OutputFilename = vmName
}
2013-06-12 21:07:08 -04:00
// Skip export if requested
if s.SkipExport {
ui.Say("Skipping export of virtual machine...")
return multistep.ActionContinue
}
ui.Say("Preparing to export machine...")
2015-06-10 13:50:08 -04:00
// Clear out the Packer-created forwarding rule
commPort := state.Get("commHostPort")
if !s.SkipNatMapping && commPort != 0 {
ui.Message(fmt.Sprintf(
"Deleting forwarded port mapping for the communicator (SSH, WinRM, etc) (host port %d)", commPort))
command := []string{"modifyvm", vmName, "--natpf1", "delete", "packercomm"}
if err := driver.VBoxManage(command...); err != nil {
err := fmt.Errorf("Error deleting port forwarding rule: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
2013-07-05 14:00:18 -04:00
// Export the VM to an OVF
2020-05-05 19:09:05 -04:00
outputPath := filepath.Join(s.OutputDir, s.OutputFilename+"."+s.Format)
2013-06-12 21:07:08 -04:00
2015-06-10 13:50:08 -04:00
command := []string{
2013-06-12 21:07:08 -04:00
"export",
vmName,
"--output",
outputPath,
}
command = append(command, s.ExportOpts...)
2013-06-12 21:07:08 -04:00
ui.Say("Exporting virtual machine...")
2014-03-13 23:26:26 -04:00
ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " ")))
2013-06-12 21:07:08 -04:00
err := driver.VBoxManage(command...)
if err != nil {
err := fmt.Errorf("Error exporting virtual machine: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
ui.Error(err.Error())
2013-06-12 21:07:08 -04:00
return multistep.ActionHalt
}
2013-08-31 15:44:58 -04:00
state.Put("exportPath", outputPath)
2013-06-12 21:07:08 -04:00
return multistep.ActionContinue
}
2013-12-22 13:40:39 -05:00
func (s *StepExport) Cleanup(state multistep.StateBag) {}