forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixer_pp_vagrant_override.go
75 lines (63 loc) · 1.84 KB
/
fixer_pp_vagrant_override.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
package fix
import (
"github.com/mitchellh/mapstructure"
)
// FixerVagrantPPOvveride is a Fixer that replaces the provider-specific
// overrides for the Vagrant post-processor with the new style introduced
// as part of Packer 0.5.0.
type FixerVagrantPPOverride struct{}
func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]interface{}, error) {
// Our template type we'll use for this fixer only
type template struct {
PostProcessors []interface{} `mapstructure:"post-processors"`
}
// Decode the input into our structure, if we can
var tpl template
if err := mapstructure.Decode(input, &tpl); err != nil {
return nil, err
}
// Go through each post-processor and get out all the complex configs
pps := make([]map[string]interface{}, 0, len(tpl.PostProcessors))
for _, rawPP := range tpl.PostProcessors {
switch pp := rawPP.(type) {
case string:
case map[string]interface{}:
pps = append(pps, pp)
case []interface{}:
for _, innerRawPP := range pp {
if innerPP, ok := innerRawPP.(map[string]interface{}); ok {
pps = append(pps, innerPP)
}
}
}
}
// Go through each post-processor and make the fix if necessary
possible := []string{"aws", "digitalocean", "virtualbox", "vmware"}
for _, pp := range pps {
typeRaw, ok := pp["type"]
if !ok {
continue
}
if typeName, ok := typeRaw.(string); !ok {
continue
} else if typeName != "vagrant" {
continue
}
overrides := make(map[string]interface{})
for _, name := range possible {
if _, ok := pp[name]; !ok {
continue
}
overrides[name] = pp[name]
delete(pp, name)
}
if len(overrides) > 0 {
pp["override"] = overrides
}
}
input["post-processors"] = tpl.PostProcessors
return input, nil
}
func (FixerVagrantPPOverride) Synopsis() string {
return `Fixes provider-specific overrides for Vagrant post-processor`
}