2014-06-23 14:19:06 -04:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-09 09:54:55 -04:00
|
|
|
"os"
|
2014-06-23 14:19:06 -04:00
|
|
|
"path/filepath"
|
2015-10-09 09:54:55 -04:00
|
|
|
"strings"
|
2017-08-02 07:27:22 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-06-23 14:19:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type HypervProvider struct{}
|
|
|
|
|
|
|
|
func (p *HypervProvider) KeepInputArtifact() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func (p *HypervProvider) Process(ui packersdk.Ui, artifact packersdk.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
|
2014-06-23 14:19:06 -04:00
|
|
|
// Create the metadata
|
|
|
|
metadata = map[string]interface{}{"provider": "hyperv"}
|
|
|
|
|
2015-10-09 09:54:55 -04:00
|
|
|
// ui.Message(fmt.Sprintf("artifacts all: %+v", artifact))
|
|
|
|
var outputDir string
|
|
|
|
|
|
|
|
// Vargant requires specific dir structure for hyperv
|
|
|
|
// hyperv builder creates the structure in the output dir
|
|
|
|
// we have to keep the structure in a temp dir
|
|
|
|
// hack little bit but string in artifact usually have output dir
|
|
|
|
artifactString := artifact.String()
|
|
|
|
d := strings.Split(artifactString, ": ")
|
|
|
|
outputDir = d[1]
|
|
|
|
// ui.Message(fmt.Sprintf("artifact dir from string: %s", outputDir))
|
|
|
|
|
2014-06-23 14:19:06 -04:00
|
|
|
// Copy all of the original contents into the temporary directory
|
|
|
|
for _, path := range artifact.Files() {
|
|
|
|
ui.Message(fmt.Sprintf("Copying: %s", path))
|
|
|
|
|
2015-10-09 09:54:55 -04:00
|
|
|
var rel string
|
|
|
|
|
|
|
|
rel, err = filepath.Rel(outputDir, filepath.Dir(path))
|
|
|
|
// ui.Message(fmt.Sprintf("rel is: %s", rel))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Message(fmt.Sprintf("err in: %s", rel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dstDir := filepath.Join(dir, rel)
|
|
|
|
// ui.Message(fmt.Sprintf("dstdir is: %s", dstDir))
|
|
|
|
if _, err = os.Stat(dstDir); err != nil {
|
|
|
|
if err = os.MkdirAll(dstDir, 0755); err != nil {
|
|
|
|
ui.Message(fmt.Sprintf("err in creating: %s", dstDir))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dstPath := filepath.Join(dstDir, filepath.Base(path))
|
|
|
|
|
2017-08-02 07:27:22 -04:00
|
|
|
// We prefer to link the files where possible because they are often very huge.
|
|
|
|
// Some filesystem configurations do not allow hardlinks. As the possibilities
|
|
|
|
// of mounting different devices in different paths are flexible, we just try to
|
|
|
|
// link the file and copy if the link fails, thereby automatically optimizing with a safe fallback.
|
|
|
|
if err = LinkFile(dstPath, path); err != nil {
|
|
|
|
// ui.Message(fmt.Sprintf("err in linking: %s to %s", path, dstPath))
|
|
|
|
if err = CopyContents(dstPath, path); err != nil {
|
|
|
|
ui.Message(fmt.Sprintf("err in copying: %s to %s", path, dstPath))
|
|
|
|
return
|
|
|
|
}
|
2014-06-23 14:19:06 -04:00
|
|
|
}
|
2017-08-02 07:27:22 -04:00
|
|
|
|
2017-10-26 08:42:49 -04:00
|
|
|
ui.Message(fmt.Sprintf("Copied %s to %s", path, dstPath))
|
2014-06-23 14:19:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|