github.com/hashicorp/packer@v1.14.3/fix/fixer_vmware_compaction.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import (
     7  	"github.com/mitchellh/mapstructure"
     8  )
     9  
    10  // FixerVMwareCompaction adds "skip_compaction = true" to "vmware-iso" builders with incompatible disk_type_id
    11  type FixerVMwareCompaction struct{}
    12  
    13  func (FixerVMwareCompaction) DeprecatedOptions() map[string][]string {
    14  	return map[string][]string{}
    15  }
    16  
    17  func (FixerVMwareCompaction) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    18  	// The type we'll decode into; we only care about builders
    19  	type template struct {
    20  		Builders []map[string]interface{}
    21  	}
    22  
    23  	// Decode the input into our structure, if we can
    24  	var tpl template
    25  	if err := mapstructure.Decode(input, &tpl); err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	for _, builder := range tpl.Builders {
    30  		builderTypeRaw, ok := builder["type"]
    31  		if !ok {
    32  			continue
    33  		}
    34  
    35  		builderType, ok := builderTypeRaw.(string)
    36  		if !ok {
    37  			continue
    38  		}
    39  
    40  		if builderType != "vmware-iso" {
    41  			continue
    42  		}
    43  
    44  		builderRemoteTypeRaw, ok := builder["remote_type"]
    45  		if !ok {
    46  			continue
    47  		}
    48  
    49  		builderRemoteType, ok := builderRemoteTypeRaw.(string)
    50  		if !ok {
    51  			continue
    52  		}
    53  
    54  		if builderRemoteType != "esx5" {
    55  			continue
    56  		}
    57  
    58  		builderDiskTypeIdRaw, ok := builder["disk_type_id"]
    59  		// set to default when this fixer was added due to incompatibility of defaults
    60  		if !ok {
    61  			builderDiskTypeId := "zeroedthick"
    62  			builder["disk_type_id"] = builderDiskTypeId
    63  		}
    64  
    65  		if ok {
    66  			builderDiskTypeId, ok := builderDiskTypeIdRaw.(string)
    67  			if !ok {
    68  				continue
    69  			}
    70  			if builderDiskTypeId == "thin" {
    71  				continue
    72  			}
    73  		}
    74  
    75  		builderSkipCompactionRaw, ok := builder["skip_compaction"]
    76  		// already verified this is not creating a "thin" disk, will need to skip_compaction
    77  		if ok {
    78  			builderSkipCompaction, ok := builderSkipCompactionRaw.(bool)
    79  			if !ok {
    80  				continue
    81  			}
    82  			if !builderSkipCompaction {
    83  				builder["skip_compaction"] = !builderSkipCompaction
    84  			}
    85  			continue
    86  		}
    87  
    88  		builderSkipCompaction := true
    89  		builder["skip_compaction"] = builderSkipCompaction
    90  	}
    91  
    92  	input["builders"] = tpl.Builders
    93  	return input, nil
    94  }
    95  
    96  func (FixerVMwareCompaction) Synopsis() string {
    97  	return `Adds "skip_compaction = true" to "vmware-iso" builders with incompatible disk_type_id`
    98  }