2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 07:36:07 -04:00
|
|
|
"fmt"
|
2018-07-04 08:03:32 -04:00
|
|
|
"path/filepath"
|
2017-04-05 15:00:53 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-05 15:00:53 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepExportVm struct {
|
2015-06-27 17:36:39 -04:00
|
|
|
OutputDir string
|
|
|
|
SkipCompaction bool
|
2017-10-12 15:16:24 -04:00
|
|
|
SkipExport bool
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2018-06-16 13:02:38 -04:00
|
|
|
// Get the VM name; Get the temp directory used to store the VMs files
|
|
|
|
// during the build process
|
|
|
|
var vmName, tmpPath string
|
|
|
|
if v, ok := state.GetOk("vmName"); ok {
|
|
|
|
vmName = v.(string)
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
2018-06-16 13:02:38 -04:00
|
|
|
if v, ok := state.GetOk("packerTempDir"); ok {
|
|
|
|
tmpPath = v.(string)
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2018-06-16 13:02:38 -04:00
|
|
|
// Compact disks first so the export process has less to do
|
2015-06-27 17:36:39 -04:00
|
|
|
if s.SkipCompaction {
|
|
|
|
ui.Say("Skipping disk compaction...")
|
|
|
|
} else {
|
|
|
|
ui.Say("Compacting disks...")
|
2018-06-16 13:02:38 -04:00
|
|
|
err := driver.CompactDisks(tmpPath)
|
2015-06-27 17:36:39 -04:00
|
|
|
if err != nil {
|
2018-06-16 13:02:38 -04:00
|
|
|
err := fmt.Errorf("Error compacting disks: %s", err)
|
2015-06-27 17:36:39 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2018-06-16 13:02:38 -04:00
|
|
|
if s.SkipExport {
|
|
|
|
ui.Say("Skipping export of virtual machine...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Exporting virtual machine...")
|
|
|
|
// The export process exports the VM to a folder named 'vmName' under
|
|
|
|
// the output directory. This contains the usual 'Snapshots', 'Virtual
|
|
|
|
// Hard Disks' and 'Virtual Machines' directories.
|
|
|
|
err := driver.ExportVirtualMachine(vmName, s.OutputDir)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error exporting vm: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
2018-06-16 13:02:38 -04:00
|
|
|
|
2018-07-04 08:03:32 -04:00
|
|
|
// Shuffle around the exported folders to maintain backwards
|
|
|
|
// compatibility. This moves the 'Snapshots', 'Virtual Hard Disks' and
|
|
|
|
// 'Virtual Machines' directories from <output directory>/<vm name> so
|
|
|
|
// they appear directly under <output directory>. The empty '<output
|
|
|
|
// directory>/<vm name>' directory is removed when complete.
|
|
|
|
// The 'Snapshots' folder will not be moved into the output directory
|
|
|
|
// if it is empty.
|
|
|
|
exportPath := filepath.Join(s.OutputDir, vmName)
|
|
|
|
err = driver.PreserveLegacyExportBehaviour(exportPath, s.OutputDir)
|
|
|
|
if err != nil {
|
|
|
|
// No need to halt here; Just warn the user instead
|
|
|
|
err = fmt.Errorf("WARNING: Error restoring legacy export dir structure: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepExportVm) Cleanup(state multistep.StateBag) {
|
|
|
|
// do nothing
|
|
|
|
}
|