2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-06-27 17:36:39 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
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
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepExportVm) Run(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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Exporting vm...")
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
err = driver.ExportVirtualMachine(vmName, vmExportPath)
|
2015-06-21 07:36:07 -04:00
|
|
|
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
|
2015-06-27 17:36:39 -04:00
|
|
|
expPath := filepath.Join(vmExportPath, vmName)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
ui.Say("Coping to output dir...")
|
2015-10-30 04:23:30 -04:00
|
|
|
err = driver.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
errorMsg = "Error exporting vm: %s"
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepExportVm) Cleanup(state multistep.StateBag) {
|
|
|
|
// do nothing
|
|
|
|
}
|