forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixer_comm_config.go
102 lines (78 loc) · 2.89 KB
/
fixer_comm_config.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
package fix
import (
"strings"
"github.com/mitchellh/mapstructure"
)
// FixerCommConfig removes ssh prefix from communicator port forwarding config
// for variables host_port_min, host_port_max, skip_nat_mapping
type FixerCommConfig struct{}
func (FixerCommConfig) Fix(input map[string]interface{}) (map[string]interface{}, error) {
type template struct {
Builders []interface{}
}
// Decode the input into our structure, if we can
var tpl template
if err := mapstructure.WeakDecode(input, &tpl); err != nil {
return nil, err
}
for i, raw := range tpl.Builders {
var builders map[string]interface{}
if err := mapstructure.Decode(raw, &builders); err != nil {
// Ignore errors, could be a non-map
continue
}
// only virtualbox builders
builderType := builders["type"].(string)
if ok := strings.HasPrefix(builderType, "virtualbox"); !ok {
continue
}
// ssh_host_port_min to host_port_min
if _, ok := builders["host_port_min"]; ok {
// drop ssh_host_port_min if it is also included
if _, sshHostPortMinIncluded := builders["ssh_host_port_min"]; sshHostPortMinIncluded {
delete(builders, "ssh_host_port_min")
}
} else if _, ok := builders["ssh_host_port_min"]; ok {
// replace ssh_host_port_min with host_port_min
sshHostPortMinRaw := builders["ssh_host_port_min"]
delete(builders, "ssh_host_port_min")
builders["host_port_min"] = sshHostPortMinRaw
}
// ssh_host_port_max to host_port_max
if _, ok := builders["host_port_max"]; ok {
// drop ssh_host_port_max if it is also included
if _, sshHostPortMaxIncluded := builders["ssh_host_port_max"]; sshHostPortMaxIncluded {
delete(builders, "ssh_host_port_max")
}
} else if _, ok := builders["ssh_host_port_max"]; ok {
// replace ssh_host_port_max with host_port_max
sshHostPortMaxRaw := builders["ssh_host_port_max"]
delete(builders, "ssh_host_port_max")
builders["host_port_max"] = sshHostPortMaxRaw
}
// ssh_skip_nat_mapping to skip_nat_mapping
if _, ok := builders["skip_nat_mapping"]; ok {
// drop ssh_skip_nat_mapping if it is also included
if _, sshSkipNatMappingIncluded := builders["ssh_skip_nat_mapping"]; sshSkipNatMappingIncluded {
delete(builders, "ssh_skip_nat_mapping")
}
} else if _, ok := builders["ssh_skip_nat_mapping"]; ok {
// replace ssh_skip_nat_mapping with skip_nat_mapping
sshSkipNatMappingRaw := builders["ssh_skip_nat_mapping"]
sshSkipNatMappingBool, ok := sshSkipNatMappingRaw.(bool)
if ok {
delete(builders, "ssh_skip_nat_mapping")
builders["skip_nat_mapping"] = sshSkipNatMappingBool
}
}
// Write all changes back to template
tpl.Builders[i] = builders
}
if len(tpl.Builders) > 0 {
input["builders"] = tpl.Builders
}
return input, nil
}
func (FixerCommConfig) Synopsis() string {
return `Remove ssh prefixes from communicator port forwarding configuration (host_port_min, host_port_max, skip_nat_mapping)`
}