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 {
|
2018-07-07 07:27:09 -04:00
|
|
|
OutputDir string
|
|
|
|
SkipExport bool
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepExportVm) Run(ctx 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
|
|
|
if s.SkipExport {
|
|
|
|
ui.Say("Skipping export of virtual machine...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Exporting virtual machine...")
|
2018-07-07 07:27:09 -04:00
|
|
|
|
|
|
|
// The VM name is needed for the export command
|
|
|
|
var vmName string
|
|
|
|
if v, ok := state.GetOk("vmName"); ok {
|
|
|
|
vmName = v.(string)
|
|
|
|
}
|
|
|
|
|
2018-06-16 13:02:38 -04:00
|
|
|
// 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-08 06:57:15 -04:00
|
|
|
// Store the path to the export directory for later steps
|
2018-07-04 08:03:32 -04:00
|
|
|
exportPath := filepath.Join(s.OutputDir, vmName)
|
2018-07-08 06:57:15 -04:00
|
|
|
state.Put("export_path", exportPath)
|
2018-07-04 08:03:32 -04:00
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepExportVm) Cleanup(state multistep.StateBag) {
|
|
|
|
// do nothing
|
|
|
|
}
|