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 12:59:47 -04:00
|
|
|
"log"
|
2013-10-09 13:14:09 -04:00
|
|
|
"path/filepath"
|
2014-03-13 23:26:26 -04:00
|
|
|
"strings"
|
2014-03-14 12:22:49 -04:00
|
|
|
"time"
|
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
|
|
|
|
ExportOpts []string
|
|
|
|
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
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepExport) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
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)
|
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-10-09 12:59:47 -04:00
|
|
|
// Wait a second to ensure VM is really shutdown.
|
|
|
|
log.Println("1 second timeout to ensure VM is really shutdown")
|
|
|
|
time.Sleep(1 * time.Second)
|
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
|
2017-06-02 15:58:34 -04:00
|
|
|
sshPort := state.Get("sshHostPort")
|
|
|
|
if !s.SkipNatMapping && sshPort != 0 {
|
2014-03-27 02:11:34 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
2017-06-02 15:58:34 -04:00
|
|
|
"Deleting forwarded port mapping for the communicator (SSH, WinRM, etc) (host port %d)", sshPort))
|
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
|
2013-12-22 13:40:39 -05:00
|
|
|
outputPath := filepath.Join(s.OutputDir, vmName+"."+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) {}
|