github.com/rothwerx/packer@v0.9.0/builder/amazon/common/run_config.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/mitchellh/packer/common/uuid"
    10  	"github.com/mitchellh/packer/helper/communicator"
    11  	"github.com/mitchellh/packer/template/interpolate"
    12  )
    13  
    14  // RunConfig contains configuration for running an instance from a source
    15  // AMI and details on how to access that launched image.
    16  type RunConfig struct {
    17  	AssociatePublicIpAddress bool              `mapstructure:"associate_public_ip_address"`
    18  	AvailabilityZone         string            `mapstructure:"availability_zone"`
    19  	EbsOptimized             bool              `mapstructure:"ebs_optimized"`
    20  	IamInstanceProfile       string            `mapstructure:"iam_instance_profile"`
    21  	InstanceType             string            `mapstructure:"instance_type"`
    22  	RunTags                  map[string]string `mapstructure:"run_tags"`
    23  	SourceAmi                string            `mapstructure:"source_ami"`
    24  	SpotPrice                string            `mapstructure:"spot_price"`
    25  	SpotPriceAutoProduct     string            `mapstructure:"spot_price_auto_product"`
    26  	SecurityGroupId          string            `mapstructure:"security_group_id"`
    27  	SecurityGroupIds         []string          `mapstructure:"security_group_ids"`
    28  	SubnetId                 string            `mapstructure:"subnet_id"`
    29  	TemporaryKeyPairName     string            `mapstructure:"temporary_key_pair_name"`
    30  	UserData                 string            `mapstructure:"user_data"`
    31  	UserDataFile             string            `mapstructure:"user_data_file"`
    32  	WindowsPasswordTimeout   time.Duration     `mapstructure:"windows_password_timeout"`
    33  	VpcId                    string            `mapstructure:"vpc_id"`
    34  
    35  	// Communicator settings
    36  	Comm           communicator.Config `mapstructure:",squash"`
    37  	SSHKeyPairName string              `mapstructure:"ssh_keypair_name"`
    38  	SSHPrivateIp   bool                `mapstructure:"ssh_private_ip"`
    39  }
    40  
    41  func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
    42  	// if we are not given an explicit keypairname, create a temporary one
    43  	if c.SSHKeyPairName == "" {
    44  		c.TemporaryKeyPairName = fmt.Sprintf(
    45  			"packer %s", uuid.TimeOrderedUUID())
    46  	}
    47  
    48  	if c.WindowsPasswordTimeout == 0 {
    49  		c.WindowsPasswordTimeout = 10 * time.Minute
    50  	}
    51  
    52  	// Validation
    53  	errs := c.Comm.Prepare(ctx)
    54  	if c.SourceAmi == "" {
    55  		errs = append(errs, errors.New("A source_ami must be specified"))
    56  	}
    57  
    58  	if c.InstanceType == "" {
    59  		errs = append(errs, errors.New("An instance_type must be specified"))
    60  	}
    61  
    62  	if c.SpotPrice == "auto" {
    63  		if c.SpotPriceAutoProduct == "" {
    64  			errs = append(errs, errors.New(
    65  				"spot_price_auto_product must be specified when spot_price is auto"))
    66  		}
    67  	}
    68  
    69  	if c.UserData != "" && c.UserDataFile != "" {
    70  		errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
    71  	} else if c.UserDataFile != "" {
    72  		if _, err := os.Stat(c.UserDataFile); err != nil {
    73  			errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
    74  		}
    75  	}
    76  
    77  	if c.SecurityGroupId != "" {
    78  		if len(c.SecurityGroupIds) > 0 {
    79  			errs = append(errs, fmt.Errorf("Only one of security_group_id or security_group_ids can be specified."))
    80  		} else {
    81  			c.SecurityGroupIds = []string{c.SecurityGroupId}
    82  			c.SecurityGroupId = ""
    83  		}
    84  	}
    85  
    86  	return errs
    87  }