forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyperv.go
66 lines (52 loc) · 1.77 KB
/
hyperv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package vagrant
import (
"fmt"
"github.com/mitchellh/packer/packer"
"os"
"path/filepath"
"strings"
)
type HypervProvider struct{}
func (p *HypervProvider) KeepInputArtifact() bool {
return false
}
func (p *HypervProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
// Create the metadata
metadata = map[string]interface{}{"provider": "hyperv"}
// 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))
// Copy all of the original contents into the temporary directory
for _, path := range artifact.Files() {
ui.Message(fmt.Sprintf("Copying: %s", path))
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))
if err = CopyContents(dstPath, path); err != nil {
ui.Message(fmt.Sprintf("err in copying: %s to %s", path, dstPath))
return
}
ui.Message(fmt.Sprintf("Copyed %s to %s", path, dstPath))
}
return
}