github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/amazon/common/ami_config.go (about) 1 package common 2 3 import ( 4 "fmt" 5 6 "github.com/mitchellh/packer/template/interpolate" 7 ) 8 9 // AMIConfig is for common configuration related to creating AMIs. 10 type AMIConfig struct { 11 AMIName string `mapstructure:"ami_name"` 12 AMIDescription string `mapstructure:"ami_description"` 13 AMIVirtType string `mapstructure:"ami_virtualization_type"` 14 AMIUsers []string `mapstructure:"ami_users"` 15 AMIGroups []string `mapstructure:"ami_groups"` 16 AMIProductCodes []string `mapstructure:"ami_product_codes"` 17 AMIRegions []string `mapstructure:"ami_regions"` 18 AMISkipRegionValidation bool `mapstructure:"skip_region_validation"` 19 AMITags map[string]string `mapstructure:"tags"` 20 AMIEnhancedNetworking bool `mapstructure:"enhanced_networking"` 21 AMIForceDeregister bool `mapstructure:"force_deregister"` 22 AMIEncryptBootVolume bool `mapstructure:"encrypt_boot"` 23 } 24 25 func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error { 26 var errs []error 27 if c.AMIName == "" { 28 errs = append(errs, fmt.Errorf("ami_name must be specified")) 29 } 30 31 if len(c.AMIRegions) > 0 { 32 regionSet := make(map[string]struct{}) 33 regions := make([]string, 0, len(c.AMIRegions)) 34 35 for _, region := range c.AMIRegions { 36 // If we already saw the region, then don't look again 37 if _, ok := regionSet[region]; ok { 38 continue 39 } 40 41 // Mark that we saw the region 42 regionSet[region] = struct{}{} 43 44 if !c.AMISkipRegionValidation { 45 // Verify the region is real 46 if valid := ValidateRegion(region); valid == false { 47 errs = append(errs, fmt.Errorf("Unknown region: %s", region)) 48 continue 49 } 50 } 51 52 regions = append(regions, region) 53 } 54 55 c.AMIRegions = regions 56 } 57 58 if len(c.AMIUsers) > 0 && c.AMIEncryptBootVolume { 59 errs = append(errs, fmt.Errorf("Cannot share AMI with encrypted boot volume")) 60 } 61 62 if len(errs) > 0 { 63 return errs 64 } 65 66 return nil 67 }