2013-12-22 13:40:39 -05:00
|
|
|
package common
|
2013-06-12 21:07:08 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"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
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-12 21:07:08 -04:00
|
|
|
)
|
|
|
|
|
2013-07-04 17:44:48 -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 {
|
2014-03-27 02:11:34 -04:00
|
|
|
Format string
|
|
|
|
OutputDir string
|
2020-05-05 19:09:05 -04:00
|
|
|
OutputFilename string
|
2014-03-27 02:11:34 -04:00
|
|
|
ExportOpts []string
|
2018-02-26 11:47:59 -05:00
|
|
|
Bundling VBoxBundleConfig
|
2014-03-27 02:11:34 -04:00
|
|
|
SkipNatMapping bool
|
2016-12-29 08:19:59 -05:00
|
|
|
SkipExport bool
|
2013-12-22 13:40:39 -05:00
|
|
|
}
|
2013-06-12 21:07:08 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-02-26 11:47:59 -05:00
|
|
|
// 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" {
|
2018-02-26 11:47:59 -05:00
|
|
|
s.ExportOpts = append(s.ExportOpts, "--iso")
|
2018-11-27 12:33:29 -05:00
|
|
|
break
|
2018-02-26 11:47:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-22 13:40:39 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
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
|
|
|
|
2016-12-29 08:19:59 -05:00
|
|
|
// Skip export if requested
|
|
|
|
if s.SkipExport {
|
|
|
|
ui.Say("Skipping export of virtual machine...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-07-09 15:38:34 -04:00
|
|
|
ui.Say("Preparing to export machine...")
|
2014-03-27 02:11:34 -04:00
|
|
|
|
2015-06-10 13:50:08 -04:00
|
|
|
// Clear out the Packer-created forwarding rule
|
2020-01-09 11:37:30 -05:00
|
|
|
commPort := state.Get("commHostPort")
|
|
|
|
if !s.SkipNatMapping && commPort != 0 {
|
2014-03-27 02:11:34 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
2020-01-09 11:37:30 -05:00
|
|
|
"Deleting forwarded port mapping for the communicator (SSH, WinRM, etc) (host port %d)", commPort))
|
2015-09-17 07:42:58 -04:00
|
|
|
command := []string{"modifyvm", vmName, "--natpf1", "delete", "packercomm"}
|
2014-03-27 02:11:34 -04:00
|
|
|
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-04 17:44:48 -04:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
2014-03-13 23:12:50 -04:00
|
|
|
command = append(command, s.ExportOpts...)
|
2014-03-13 22:33:35 -04:00
|
|
|
|
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 {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error exporting virtual machine: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
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) {}
|