2013-07-16 00:23:40 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-08-12 14:52:43 -04:00
|
|
|
"os"
|
2013-07-16 00:23:40 -04:00
|
|
|
"time"
|
2014-09-05 19:38:05 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
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
|
|
|
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
|
|
|
SSHUsername string `mapstructure:"ssh_username"`
|
2014-03-24 07:47:00 -04:00
|
|
|
SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"`
|
2015-01-13 12:20:31 -05:00
|
|
|
SSHKeyPairName string `mapstructure:"ssh_keypair_name"`
|
2014-09-05 00:48:14 -04:00
|
|
|
SSHPrivateIp bool `mapstructure:"ssh_private_ip"`
|
2013-12-27 22:54:35 -05:00
|
|
|
SSHPort int `mapstructure:"ssh_port"`
|
|
|
|
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
|
|
|
|
|
|
|
// Unexported fields that are calculated from others
|
|
|
|
sshTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:17:23 -04:00
|
|
|
func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
|
2013-08-08 17:33:29 -04:00
|
|
|
if t == nil {
|
|
|
|
var err error
|
2013-08-15 22:17:23 -04:00
|
|
|
t, err = packer.NewConfigTemplate()
|
2013-08-08 17:33:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 13:15:17 -04:00
|
|
|
templates := map[string]*string{
|
|
|
|
"iam_instance_profile": &c.IamInstanceProfile,
|
|
|
|
"instance_type": &c.InstanceType,
|
2014-09-05 19:30:22 -04:00
|
|
|
"spot_price": &c.SpotPrice,
|
2014-09-06 13:44:12 -04:00
|
|
|
"spot_price_auto_product": &c.SpotPriceAutoProduct,
|
2014-07-08 13:15:17 -04:00
|
|
|
"ssh_timeout": &c.RawSSHTimeout,
|
|
|
|
"ssh_username": &c.SSHUsername,
|
|
|
|
"ssh_private_key_file": &c.SSHPrivateKeyFile,
|
2015-01-13 12:20:31 -05:00
|
|
|
"ssh_keypair_name": &c.SSHKeyPairName,
|
2014-07-08 13:15:17 -04:00
|
|
|
"source_ami": &c.SourceAmi,
|
|
|
|
"subnet_id": &c.SubnetId,
|
|
|
|
"temporary_key_pair_name": &c.TemporaryKeyPairName,
|
|
|
|
"vpc_id": &c.VpcId,
|
|
|
|
"availability_zone": &c.AvailabilityZone,
|
|
|
|
"user_data": &c.UserData,
|
|
|
|
"user_data_file": &c.UserDataFile,
|
|
|
|
"security_group_id": &c.SecurityGroupId,
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := make([]error, 0)
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = t.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
// Defaults
|
|
|
|
if c.SSHPort == 0 {
|
|
|
|
c.SSHPort = 22
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RawSSHTimeout == "" {
|
2014-03-03 20:31:14 -05:00
|
|
|
c.RawSSHTimeout = "5m"
|
2013-07-16 00:23:40 -04:00
|
|
|
}
|
|
|
|
|
2015-01-13 12:20:31 -05:00
|
|
|
// if we are not given an explicit keypairname, create a temporary one
|
|
|
|
if c.SSHKeyPairName == "" {
|
2015-01-13 16:27:33 -05:00
|
|
|
c.TemporaryKeyPairName = fmt.Sprintf(
|
2014-09-05 19:38:05 -04:00
|
|
|
"packer %s", uuid.TimeOrderedUUID())
|
2013-09-05 08:28:31 -04:00
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
// Validation
|
|
|
|
var err error
|
|
|
|
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-07-16 00:23:40 -04:00
|
|
|
if c.SSHUsername == "" {
|
|
|
|
errs = append(errs, errors.New("An ssh_username must be specified"))
|
|
|
|
}
|
|
|
|
|
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 = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceTemplates := map[string][]string{
|
|
|
|
"security_group_ids": c.SecurityGroupIds,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, slice := range sliceTemplates {
|
|
|
|
for i, elem := range slice {
|
|
|
|
var err error
|
|
|
|
slice[i], err = t.Process(elem, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 22:54:35 -05:00
|
|
|
newTags := make(map[string]string)
|
|
|
|
for k, v := range c.RunTags {
|
|
|
|
k, err := t.Process(k, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs,
|
|
|
|
fmt.Errorf("Error processing tag key %s: %s", k, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := t.Process(v, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs,
|
|
|
|
fmt.Errorf("Error processing tag value '%s': %s", v, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newTags[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
c.RunTags = newTags
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunConfig) SSHTimeout() time.Duration {
|
|
|
|
return c.sshTimeout
|
|
|
|
}
|