github.com/aclaygray/packer@v1.3.2/post-processor/vagrant/hyperv.go (about)

     1  package vagrant
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type HypervProvider struct{}
    13  
    14  func (p *HypervProvider) KeepInputArtifact() bool {
    15  	return false
    16  }
    17  
    18  func (p *HypervProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
    19  	// Create the metadata
    20  	metadata = map[string]interface{}{"provider": "hyperv"}
    21  
    22  	// ui.Message(fmt.Sprintf("artifacts all: %+v", artifact))
    23  	var outputDir string
    24  
    25  	// Vargant requires specific dir structure for hyperv
    26  	// hyperv builder creates the structure in the output dir
    27  	// we have to keep the structure in a temp dir
    28  	// hack little bit but string in artifact usually have output dir
    29  	artifactString := artifact.String()
    30  	d := strings.Split(artifactString, ": ")
    31  	outputDir = d[1]
    32  	// ui.Message(fmt.Sprintf("artifact dir from string: %s", outputDir))
    33  
    34  	// Copy all of the original contents into the temporary directory
    35  	for _, path := range artifact.Files() {
    36  		ui.Message(fmt.Sprintf("Copying: %s", path))
    37  
    38  		var rel string
    39  
    40  		rel, err = filepath.Rel(outputDir, filepath.Dir(path))
    41  		// ui.Message(fmt.Sprintf("rel is: %s", rel))
    42  
    43  		if err != nil {
    44  			ui.Message(fmt.Sprintf("err in: %s", rel))
    45  			return
    46  		}
    47  
    48  		dstDir := filepath.Join(dir, rel)
    49  		// ui.Message(fmt.Sprintf("dstdir is: %s", dstDir))
    50  		if _, err = os.Stat(dstDir); err != nil {
    51  			if err = os.MkdirAll(dstDir, 0755); err != nil {
    52  				ui.Message(fmt.Sprintf("err in creating: %s", dstDir))
    53  				return
    54  			}
    55  		}
    56  
    57  		dstPath := filepath.Join(dstDir, filepath.Base(path))
    58  
    59  		// We prefer to link the files where possible because they are often very huge.
    60  		// Some filesystem configurations do not allow hardlinks. As the possibilities
    61  		// of mounting different devices in different paths are flexible, we just try to
    62  		// link the file and copy if the link fails, thereby automatically optimizing with a safe fallback.
    63  		if err = LinkFile(dstPath, path); err != nil {
    64  			// ui.Message(fmt.Sprintf("err in linking: %s to %s", path, dstPath))
    65  			if err = CopyContents(dstPath, path); err != nil {
    66  				ui.Message(fmt.Sprintf("err in copying: %s to %s", path, dstPath))
    67  				return
    68  			}
    69  		}
    70  
    71  		ui.Message(fmt.Sprintf("Copied %s to %s", path, dstPath))
    72  	}
    73  
    74  	return
    75  }