github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_export_vm.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type StepExportVm struct {
    13  	OutputDir  string
    14  	SkipExport bool
    15  }
    16  
    17  func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    18  	driver := state.Get("driver").(Driver)
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	if s.SkipExport {
    22  		ui.Say("Skipping export of virtual machine...")
    23  		return multistep.ActionContinue
    24  	}
    25  
    26  	ui.Say("Exporting virtual machine...")
    27  
    28  	// The VM name is needed for the export command
    29  	var vmName string
    30  	if v, ok := state.GetOk("vmName"); ok {
    31  		vmName = v.(string)
    32  	}
    33  
    34  	// The export process exports the VM to a folder named 'vmName' under
    35  	// the output directory. This contains the usual 'Snapshots', 'Virtual
    36  	// Hard Disks' and 'Virtual Machines' directories.
    37  	err := driver.ExportVirtualMachine(vmName, s.OutputDir)
    38  	if err != nil {
    39  		err = fmt.Errorf("Error exporting vm: %s", err)
    40  		state.Put("error", err)
    41  		ui.Error(err.Error())
    42  		return multistep.ActionHalt
    43  	}
    44  
    45  	// Store the path to the export directory for later steps
    46  	exportPath := filepath.Join(s.OutputDir, vmName)
    47  	state.Put("export_path", exportPath)
    48  
    49  	return multistep.ActionContinue
    50  }
    51  
    52  func (s *StepExportVm) Cleanup(state multistep.StateBag) {
    53  	// do nothing
    54  }