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