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"
|
2015-06-27 17:36:39 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2015-06-27 17:36:39 -04:00
|
|
|
const (
|
2015-06-21 07:36:07 -04:00
|
|
|
vhdDir string = "Virtual Hard Disks"
|
2015-06-27 17:36:39 -04:00
|
|
|
vmDir string = "Virtual Machines"
|
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)
|
|
|
|
var err error
|
|
|
|
var errorMsg string
|
|
|
|
|
|
|
|
vmName := state.Get("vmName").(string)
|
2015-06-27 17:36:39 -04:00
|
|
|
tmpPath := state.Get("packerTempDir").(string)
|
2015-06-21 07:36:07 -04:00
|
|
|
outputPath := s.OutputDir
|
2017-10-12 15:16:24 -04:00
|
|
|
expPath := s.OutputDir
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
// create temp path to export vm
|
|
|
|
errorMsg = "Error creating temp export path: %s"
|
2015-06-27 17:36:39 -04:00
|
|
|
vmExportPath, err := ioutil.TempDir(tmpPath, "export")
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-10-12 15:16:24 -04:00
|
|
|
if !s.SkipExport {
|
|
|
|
ui.Say("Exporting vm...")
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2017-10-12 15:16:24 -04:00
|
|
|
err = driver.ExportVirtualMachine(vmName, vmExportPath)
|
|
|
|
if err != nil {
|
|
|
|
errorMsg = "Error exporting vm: %s"
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
// copy to output dir
|
|
|
|
expPath = filepath.Join(vmExportPath, vmName)
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2015-06-27 17:36:39 -04:00
|
|
|
if s.SkipCompaction {
|
|
|
|
ui.Say("Skipping disk compaction...")
|
|
|
|
} else {
|
|
|
|
ui.Say("Compacting disks...")
|
2015-10-30 04:23:30 -04:00
|
|
|
err = driver.CompactDisks(expPath, vhdDir)
|
2015-06-27 17:36:39 -04:00
|
|
|
if err != nil {
|
|
|
|
errorMsg = "Error compacting disks: %s"
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2017-10-12 15:16:24 -04:00
|
|
|
if !s.SkipExport {
|
|
|
|
ui.Say("Copying to output dir...")
|
|
|
|
err = driver.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir)
|
|
|
|
if err != nil {
|
|
|
|
errorMsg = "Error exporting vm: %s"
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepExportVm) Cleanup(state multistep.StateBag) {
|
|
|
|
// do nothing
|
|
|
|
}
|