-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostprocessor.go
128 lines (97 loc) · 2.94 KB
/
postprocessor.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ovaforge
import (
"context"
"errors"
"path"
"strings"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/mapstructure"
"github.com/stephen-fox/ovaify"
"github.com/stephen-fox/vmwareify"
)
type PostProcessor struct {
Version string
config Configuration
}
func (o *PostProcessor) Configure(i ...interface{}) error {
var config Configuration
decodeErr := mapstructure.Decode(i, &config)
if decodeErr != nil && !strings.HasSuffix(decodeErr.Error(), "expected a map, got 'slice'") {
return decodeErr
}
o.config = config
return nil
}
func (o *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
return nil
}
func (o *PostProcessor) PostProcess(_ context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
result := &forgeArtifacts{}
var common []string
var ovfFilePaths []string
for _, filePath := range artifact.Files() {
if strings.HasSuffix(filePath, ".ovf") {
outputOvfFilePath, err := vmwareifyOvf(filePath, ui)
if err != nil {
return &forgeArtifacts{}, false, false, err
}
ovfFilePaths = append(ovfFilePaths, filePath)
ovfFilePaths = append(ovfFilePaths, outputOvfFilePath)
result.filePaths = append(result.filePaths, outputOvfFilePath)
} else {
common = append(common, filePath)
}
}
if len(ovfFilePaths) == 0 {
return &forgeArtifacts{}, false, false, errors.New("No .ovf artifacts were provided")
}
for _, ovfFilePath := range ovfFilePaths {
ovaFilePath, err := createOva(ovfFilePath, common, ui)
if err != nil {
return &forgeArtifacts{}, false, false, err
}
result.filePaths = append(result.filePaths, ovaFilePath)
}
return result, true, false, nil
}
type Configuration struct {
Temp bool `mapstructure:"temp"`
}
func (o *Configuration) Validate() error {
return nil
}
func vmwareifyOvf(filePath string, ui packer.Ui) (string, error) {
ui.Message("VMWareifying '" + filePath + "'...")
inputFilename := path.Base(filePath)
outputFilePath := path.Join(path.Dir(filePath), pathWithoutExtension(inputFilename) + "-vmware.ovf")
err := vmwareify.BasicConvert(filePath, outputFilePath)
if err != nil {
return "", err
}
ui.Message("Finished VMWareifying .ovf at '" + outputFilePath + "'")
return outputFilePath, nil
}
func createOva(ovfFilePath string, files []string, ui packer.Ui) (string, error) {
outputPath := pathWithoutExtension(ovfFilePath) + ".ova"
ui.Message("Creating .ova for '" + ovfFilePath + "' with files '" +
strings.Join(files, ", ") + "'...")
config := ovaify.OvaConfig{
OvfFilePath: ovfFilePath,
FilePathsToInclude: files,
OutputFilePath: outputPath,
}
err := ovaify.CreateOvaFile(config)
if err != nil {
return "", err
}
ui.Message("Finished creating .ova at '" + outputPath + "'")
return outputPath, nil
}
func pathWithoutExtension(filename string) string {
index := strings.LastIndex(filename, ".")
if index > 0 {
return filename[:index]
}
return ""
}