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

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  type StepCollateArtifacts struct {
    12  	OutputDir  string
    13  	SkipExport bool
    14  }
    15  
    16  // Runs the step required to collate all build artifacts under the
    17  // specified output directory
    18  func (s *StepCollateArtifacts) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    19  	driver := state.Get("driver").(Driver)
    20  	ui := state.Get("ui").(packer.Ui)
    21  
    22  	ui.Say("Collating build artifacts...")
    23  
    24  	if s.SkipExport {
    25  		// Get the path to the main build directory from the statebag
    26  		var buildDir string
    27  		if v, ok := state.GetOk("build_dir"); ok {
    28  			buildDir = v.(string)
    29  		}
    30  		// If the user has chosen to skip a full export of the VM the only
    31  		// artifacts that they are interested in will be the VHDs. The
    32  		// called function searches for all disks under the given source
    33  		// directory and moves them to a 'Virtual Hard Disks' folder under
    34  		// the destination directory
    35  		err := driver.MoveCreatedVHDsToOutputDir(buildDir, s.OutputDir)
    36  		if err != nil {
    37  			err = fmt.Errorf("Error moving VHDs from build dir to output dir: %s", err)
    38  			state.Put("error", err)
    39  			ui.Error(err.Error())
    40  			return multistep.ActionHalt
    41  		}
    42  	} else {
    43  		// Get the full path to the export directory from the statebag
    44  		var exportPath string
    45  		if v, ok := state.GetOk("export_path"); ok {
    46  			exportPath = v.(string)
    47  		}
    48  		// The export process exports the VM into a folder named 'vm name'
    49  		// under the output directory. However, to maintain backwards
    50  		// compatibility, we now need to shuffle around the exported folders
    51  		// so the 'Snapshots', 'Virtual Hard Disks' and 'Virtual Machines'
    52  		// directories appear *directly* under <output directory>.
    53  		// The empty '<output directory>/<vm name>' directory is removed
    54  		// when complete.
    55  		// The 'Snapshots' folder will not be moved into the output
    56  		// directory if it is empty.
    57  		err := driver.PreserveLegacyExportBehaviour(exportPath, s.OutputDir)
    58  		if err != nil {
    59  			// No need to halt here; Just warn the user instead
    60  			err = fmt.Errorf("WARNING: Error restoring legacy export dir structure: %s", err)
    61  			ui.Error(err.Error())
    62  		}
    63  	}
    64  
    65  	return multistep.ActionContinue
    66  }
    67  
    68  // Cleanup does nothing
    69  func (s *StepCollateArtifacts) Cleanup(state multistep.StateBag) {}