github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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  	DisableStopInstance      bool              `mapstructure:"disable_stop_instance"`
    27  	SecurityGroupId          string            `mapstructure:"security_group_id"`
    28  	SecurityGroupIds         []string          `mapstructure:"security_group_ids"`
    29  	SubnetId                 string            `mapstructure:"subnet_id"`
    30  	TemporaryKeyPairName     string            `mapstructure:"temporary_key_pair_name"`
    31  	UserData                 string            `mapstructure:"user_data"`
    32  	UserDataFile             string            `mapstructure:"user_data_file"`
    33  	WindowsPasswordTimeout   time.Duration     `mapstructure:"windows_password_timeout"`
    34  	VpcId                    string            `mapstructure:"vpc_id"`
    35  
    36  	// Communicator settings
    37  	Comm           communicator.Config `mapstructure:",squash"`
    38  	SSHKeyPairName string              `mapstructure:"ssh_keypair_name"`
    39  	SSHPrivateIp   bool                `mapstructure:"ssh_private_ip"`
    40  }
    41  
    42  func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
    43  	// if we are not given an explicit keypairname, create a temporary one
    44  	if c.SSHKeyPairName == "" {
    45  		c.TemporaryKeyPairName = fmt.Sprintf(
    46  			"packer %s", uuid.TimeOrderedUUID())
    47  	}
    48  
    49  	if c.WindowsPasswordTimeout == 0 {
    50  		c.WindowsPasswordTimeout = 10 * time.Minute
    51  	}
    52  
    53  	// Validation
    54  	errs := c.Comm.Prepare(ctx)
    55  	if c.SourceAmi == "" {
    56  		errs = append(errs, errors.New("A source_ami must be specified"))
    57  	}
    58  
    59  	if c.InstanceType == "" {
    60  		errs = append(errs, errors.New("An instance_type must be specified"))
    61  	}
    62  
    63  	if c.SpotPrice == "auto" {
    64  		if c.SpotPriceAutoProduct == "" {
    65  			errs = append(errs, errors.New(
    66  				"spot_price_auto_product must be specified when spot_price is auto"))
    67  		}
    68  	}
    69  
    70  	if c.UserData != "" && c.UserDataFile != "" {
    71  		errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
    72  	} else if c.UserDataFile != "" {
    73  		if _, err := os.Stat(c.UserDataFile); err != nil {
    74  			errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
    75  		}
    76  	}
    77  
    78  	if c.SecurityGroupId != "" {
    79  		if len(c.SecurityGroupIds) > 0 {
    80  			errs = append(errs, fmt.Errorf("Only one of security_group_id or security_group_ids can be specified."))
    81  		} else {
    82  			c.SecurityGroupIds = []string{c.SecurityGroupId}
    83  			c.SecurityGroupId = ""
    84  		}
    85  	}
    86  
    87  	return errs
    88  }