2013-07-16 00:23:40 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-08-12 14:52:43 -04:00
|
|
|
"os"
|
2014-09-05 19:38:05 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
2015-06-13 18:16:12 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2015-05-27 14:35:56 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-07-16 00:23:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// RunConfig contains configuration for running an instance from a source
|
|
|
|
// AMI and details on how to access that launched image.
|
|
|
|
type RunConfig struct {
|
2013-12-27 22:54:35 -05:00
|
|
|
AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"`
|
|
|
|
AvailabilityZone string `mapstructure:"availability_zone"`
|
|
|
|
IamInstanceProfile string `mapstructure:"iam_instance_profile"`
|
|
|
|
InstanceType string `mapstructure:"instance_type"`
|
|
|
|
RunTags map[string]string `mapstructure:"run_tags"`
|
|
|
|
SourceAmi string `mapstructure:"source_ami"`
|
2014-05-07 13:13:27 -04:00
|
|
|
SpotPrice string `mapstructure:"spot_price"`
|
2014-09-06 13:44:12 -04:00
|
|
|
SpotPriceAutoProduct string `mapstructure:"spot_price_auto_product"`
|
2013-12-27 22:54:35 -05:00
|
|
|
SecurityGroupId string `mapstructure:"security_group_id"`
|
|
|
|
SecurityGroupIds []string `mapstructure:"security_group_ids"`
|
|
|
|
SubnetId string `mapstructure:"subnet_id"`
|
|
|
|
TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
|
|
|
|
UserData string `mapstructure:"user_data"`
|
|
|
|
UserDataFile string `mapstructure:"user_data_file"`
|
|
|
|
VpcId string `mapstructure:"vpc_id"`
|
2013-07-16 00:23:40 -04:00
|
|
|
|
2015-06-13 18:16:12 -04:00
|
|
|
// Communicator settings
|
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
SSHPrivateIp bool `mapstructure:"ssh_private_ip"`
|
2013-07-16 00:23:40 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 14:35:56 -04:00
|
|
|
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
|
2013-09-05 15:23:08 -04:00
|
|
|
if c.TemporaryKeyPairName == "" {
|
2014-09-05 19:38:05 -04:00
|
|
|
c.TemporaryKeyPairName = fmt.Sprintf(
|
|
|
|
"packer %s", uuid.TimeOrderedUUID())
|
2013-09-05 08:28:31 -04:00
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
// Validation
|
2015-06-13 18:16:12 -04:00
|
|
|
errs := c.Comm.Prepare(ctx)
|
2013-07-16 00:23:40 -04:00
|
|
|
if c.SourceAmi == "" {
|
|
|
|
errs = append(errs, errors.New("A source_ami must be specified"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InstanceType == "" {
|
|
|
|
errs = append(errs, errors.New("An instance_type must be specified"))
|
|
|
|
}
|
|
|
|
|
2014-09-06 13:44:12 -04:00
|
|
|
if c.SpotPrice == "auto" {
|
|
|
|
if c.SpotPriceAutoProduct == "" {
|
|
|
|
errs = append(errs, errors.New(
|
|
|
|
"spot_price_auto_product must be specified when spot_price is auto"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 14:52:43 -04:00
|
|
|
if c.UserData != "" && c.UserDataFile != "" {
|
|
|
|
errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
|
|
|
|
} else if c.UserDataFile != "" {
|
|
|
|
if _, err := os.Stat(c.UserDataFile); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:52:16 -04:00
|
|
|
if c.SecurityGroupId != "" {
|
|
|
|
if len(c.SecurityGroupIds) > 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("Only one of security_group_id or security_group_ids can be specified."))
|
|
|
|
} else {
|
|
|
|
c.SecurityGroupIds = []string{c.SecurityGroupId}
|
|
|
|
c.SecurityGroupId = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
return errs
|
|
|
|
}
|