github.com/hashicorp/packer@v1.14.3/fix/fixer_comm_config.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package fix 5 6 import ( 7 "strings" 8 9 "github.com/mitchellh/mapstructure" 10 ) 11 12 // FixerCommConfig removes ssh prefix from communicator port forwarding config 13 // for variables host_port_min, host_port_max, skip_nat_mapping 14 type FixerCommConfig struct{} 15 16 func (FixerCommConfig) DeprecatedOptions() map[string][]string { 17 return map[string][]string{ 18 "*": {"ssh_host_port_min", "ssh_host_port_max", 19 "ssh_skip_nat_mapping"}, 20 } 21 } 22 23 func (FixerCommConfig) Fix(input map[string]interface{}) (map[string]interface{}, error) { 24 type template struct { 25 Builders []interface{} 26 } 27 28 // Decode the input into our structure, if we can 29 var tpl template 30 if err := mapstructure.WeakDecode(input, &tpl); err != nil { 31 return nil, err 32 } 33 34 for i, raw := range tpl.Builders { 35 var builders map[string]interface{} 36 if err := mapstructure.Decode(raw, &builders); err != nil { 37 // Ignore errors, could be a non-map 38 continue 39 } 40 41 // only virtualbox builders 42 builderType := builders["type"].(string) 43 if ok := strings.HasPrefix(builderType, "virtualbox"); !ok { 44 continue 45 } 46 47 // ssh_host_port_min to host_port_min 48 if _, ok := builders["host_port_min"]; ok { 49 // drop ssh_host_port_min if it is also included 50 delete(builders, "ssh_host_port_min") 51 } else if _, ok := builders["ssh_host_port_min"]; ok { 52 // replace ssh_host_port_min with host_port_min 53 sshHostPortMinRaw := builders["ssh_host_port_min"] 54 delete(builders, "ssh_host_port_min") 55 builders["host_port_min"] = sshHostPortMinRaw 56 } 57 58 // ssh_host_port_max to host_port_max 59 if _, ok := builders["host_port_max"]; ok { 60 // drop ssh_host_port_max if it is also included 61 delete(builders, "ssh_host_port_max") 62 } else if _, ok := builders["ssh_host_port_max"]; ok { 63 // replace ssh_host_port_max with host_port_max 64 sshHostPortMaxRaw := builders["ssh_host_port_max"] 65 delete(builders, "ssh_host_port_max") 66 builders["host_port_max"] = sshHostPortMaxRaw 67 } 68 69 // ssh_skip_nat_mapping to skip_nat_mapping 70 if _, ok := builders["skip_nat_mapping"]; ok { 71 // drop ssh_skip_nat_mapping if it is also included 72 delete(builders, "ssh_skip_nat_mapping") 73 } else if _, ok := builders["ssh_skip_nat_mapping"]; ok { 74 // replace ssh_skip_nat_mapping with skip_nat_mapping 75 sshSkipNatMappingRaw := builders["ssh_skip_nat_mapping"] 76 sshSkipNatMappingBool, ok := sshSkipNatMappingRaw.(bool) 77 if ok { 78 delete(builders, "ssh_skip_nat_mapping") 79 builders["skip_nat_mapping"] = sshSkipNatMappingBool 80 } 81 } 82 83 // Write all changes back to template 84 tpl.Builders[i] = builders 85 } 86 87 if len(tpl.Builders) > 0 { 88 input["builders"] = tpl.Builders 89 } 90 91 return input, nil 92 } 93 94 func (FixerCommConfig) Synopsis() string { 95 return `Remove ssh prefixes from communicator port forwarding configuration (host_port_min, host_port_max, skip_nat_mapping)` 96 }