github.phpd.cn/hashicorp/packer@v1.3.2/fix/fixer_vmware_compaction.go (about)

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