2013-07-16 00:23:40 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-12 20:05:31 -04:00
|
|
|
"net"
|
2013-08-12 14:52:43 -04:00
|
|
|
"os"
|
2016-05-20 05:54:45 -04:00
|
|
|
"regexp"
|
2018-05-12 11:20:01 -04:00
|
|
|
"strings"
|
2016-06-14 17:34:01 -04:00
|
|
|
"time"
|
2014-09-05 19:38:05 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-07-16 00:23:40 -04:00
|
|
|
)
|
|
|
|
|
2016-06-14 17:34:01 -04:00
|
|
|
var reShutdownBehavior = regexp.MustCompile("^(stop|terminate)$")
|
|
|
|
|
2016-08-20 19:34:22 -04:00
|
|
|
type AmiFilterOptions struct {
|
2016-08-20 19:08:45 -04:00
|
|
|
Filters map[*string]*string
|
|
|
|
Owners []*string
|
|
|
|
MostRecent bool `mapstructure:"most_recent"`
|
2016-08-20 14:58:36 -04:00
|
|
|
}
|
|
|
|
|
2016-08-20 19:34:22 -04:00
|
|
|
func (d *AmiFilterOptions) Empty() bool {
|
2016-08-20 19:08:45 -04:00
|
|
|
return len(d.Owners) == 0 && len(d.Filters) == 0
|
2016-08-20 14:58:36 -04:00
|
|
|
}
|
|
|
|
|
2018-08-13 20:01:13 -04:00
|
|
|
func (d *AmiFilterOptions) NoOwner() bool {
|
|
|
|
return len(d.Owners) == 0
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-06-14 17:34:01 -04:00
|
|
|
AssociatePublicIpAddress bool `mapstructure:"associate_public_ip_address"`
|
|
|
|
AvailabilityZone string `mapstructure:"availability_zone"`
|
2018-05-12 11:14:48 -04:00
|
|
|
DisableStopInstance bool `mapstructure:"disable_stop_instance"`
|
2016-06-14 17:34:01 -04:00
|
|
|
EbsOptimized bool `mapstructure:"ebs_optimized"`
|
2018-05-13 10:21:30 -04:00
|
|
|
EnableT2Unlimited bool `mapstructure:"enable_t2_unlimited"`
|
2016-06-14 17:34:01 -04:00
|
|
|
IamInstanceProfile string `mapstructure:"iam_instance_profile"`
|
2018-05-12 11:14:48 -04:00
|
|
|
InstanceInitiatedShutdownBehavior string `mapstructure:"shutdown_behavior"`
|
2016-06-14 17:34:01 -04:00
|
|
|
InstanceType string `mapstructure:"instance_type"`
|
|
|
|
RunTags map[string]string `mapstructure:"run_tags"`
|
2018-05-12 11:14:48 -04:00
|
|
|
SecurityGroupId string `mapstructure:"security_group_id"`
|
|
|
|
SecurityGroupIds []string `mapstructure:"security_group_ids"`
|
2016-06-14 17:34:01 -04:00
|
|
|
SourceAmi string `mapstructure:"source_ami"`
|
2016-08-20 19:34:22 -04:00
|
|
|
SourceAmiFilter AmiFilterOptions `mapstructure:"source_ami_filter"`
|
2016-06-14 17:34:01 -04:00
|
|
|
SpotPrice string `mapstructure:"spot_price"`
|
|
|
|
SpotPriceAutoProduct string `mapstructure:"spot_price_auto_product"`
|
2017-10-12 17:33:01 -04:00
|
|
|
SpotTags map[string]string `mapstructure:"spot_tags"`
|
2016-06-14 17:34:01 -04:00
|
|
|
SubnetId string `mapstructure:"subnet_id"`
|
|
|
|
TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
|
2018-05-12 11:14:48 -04:00
|
|
|
TemporarySGSourceCidr string `mapstructure:"temporary_security_group_source_cidr"`
|
2016-06-14 17:34:01 -04:00
|
|
|
UserData string `mapstructure:"user_data"`
|
|
|
|
UserDataFile string `mapstructure:"user_data_file"`
|
|
|
|
VpcId string `mapstructure:"vpc_id"`
|
2018-05-12 11:14:48 -04:00
|
|
|
WindowsPasswordTimeout time.Duration `mapstructure:"windows_password_timeout"`
|
2013-07-16 00:23:40 -04:00
|
|
|
|
2015-06-13 18:16:12 -04:00
|
|
|
// Communicator settings
|
2015-06-15 18:21:58 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
SSHKeyPairName string `mapstructure:"ssh_keypair_name"`
|
2017-11-21 21:59:15 -05:00
|
|
|
SSHInterface string `mapstructure:"ssh_interface"`
|
2013-07-16 00:23:40 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 14:35:56 -04:00
|
|
|
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
|
2016-10-02 16:20:36 -04:00
|
|
|
// If we are not given an explicit ssh_keypair_name or
|
|
|
|
// ssh_private_key_file, then create a temporary one, but only if the
|
|
|
|
// temporary_key_pair_name has not been provided and we are not using
|
|
|
|
// ssh_password.
|
|
|
|
if c.SSHKeyPairName == "" && c.TemporaryKeyPairName == "" &&
|
|
|
|
c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" {
|
|
|
|
|
|
|
|
c.TemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
|
2013-09-05 08:28:31 -04:00
|
|
|
}
|
|
|
|
|
2015-06-14 01:35:45 -04:00
|
|
|
if c.WindowsPasswordTimeout == 0 {
|
2017-02-24 06:08:17 -05:00
|
|
|
c.WindowsPasswordTimeout = 20 * time.Minute
|
2015-06-14 01:35:45 -04:00
|
|
|
}
|
|
|
|
|
2017-01-18 14:34:36 -05:00
|
|
|
if c.RunTags == nil {
|
|
|
|
c.RunTags = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:26:55 -05:00
|
|
|
// Validation
|
|
|
|
errs := c.Comm.Prepare(ctx)
|
2017-11-21 21:49:38 -05:00
|
|
|
|
2018-03-13 23:28:19 -04:00
|
|
|
// Validating ssh_interface
|
2017-11-27 20:26:55 -05:00
|
|
|
if c.SSHInterface != "public_ip" &&
|
|
|
|
c.SSHInterface != "private_ip" &&
|
|
|
|
c.SSHInterface != "public_dns" &&
|
|
|
|
c.SSHInterface != "private_dns" &&
|
2017-11-27 17:46:01 -05:00
|
|
|
c.SSHInterface != "" {
|
2018-05-24 16:05:03 -04:00
|
|
|
errs = append(errs, fmt.Errorf("Unknown interface type: %s", c.SSHInterface))
|
2017-11-27 17:46:01 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 16:14:16 -04:00
|
|
|
if c.SSHKeyPairName != "" {
|
|
|
|
if c.Comm.Type == "winrm" && c.Comm.WinRMPassword == "" && c.Comm.SSHPrivateKey == "" {
|
2018-05-15 05:07:09 -04:00
|
|
|
errs = append(errs, fmt.Errorf("ssh_private_key_file must be provided to retrieve the winrm password when using ssh_keypair_name."))
|
2017-03-13 16:14:16 -04:00
|
|
|
} else if c.Comm.SSHPrivateKey == "" && !c.Comm.SSHAgentAuth {
|
2018-05-15 05:07:09 -04:00
|
|
|
errs = append(errs, fmt.Errorf("ssh_private_key_file must be provided or ssh_agent_auth enabled when ssh_keypair_name is specified."))
|
2017-03-13 16:14:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-20 19:34:22 -04:00
|
|
|
if c.SourceAmi == "" && c.SourceAmiFilter.Empty() {
|
2018-05-15 05:07:09 -04:00
|
|
|
errs = append(errs, fmt.Errorf("A source_ami or source_ami_filter must be specified"))
|
2013-07-16 00:23:40 -04:00
|
|
|
}
|
|
|
|
|
2018-08-13 20:01:13 -04:00
|
|
|
if c.SourceAmi == "" && c.SourceAmiFilter.NoOwner() {
|
|
|
|
errs = append(errs, fmt.Errorf("For security reasons, your source AMI filter must declare an owner."))
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
if c.InstanceType == "" {
|
2018-05-15 05:07:09 -04:00
|
|
|
errs = append(errs, fmt.Errorf("An instance_type must be specified"))
|
2013-07-16 00:23:40 -04:00
|
|
|
}
|
|
|
|
|
2014-09-06 13:44:12 -04:00
|
|
|
if c.SpotPrice == "auto" {
|
|
|
|
if c.SpotPriceAutoProduct == "" {
|
2018-05-15 05:07:09 -04:00
|
|
|
errs = append(errs, fmt.Errorf(
|
2014-09-06 13:44:12 -04:00
|
|
|
"spot_price_auto_product must be specified when spot_price is auto"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 19:54:51 -04:00
|
|
|
if c.SpotPriceAutoProduct != "" {
|
|
|
|
if c.SpotPrice != "auto" {
|
2018-05-15 05:07:09 -04:00
|
|
|
errs = append(errs, fmt.Errorf(
|
2018-05-13 19:54:51 -04:00
|
|
|
"spot_price should be set to auto when spot_price_auto_product is specified"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 02:22:15 -04:00
|
|
|
if c.SpotTags != nil {
|
|
|
|
if c.SpotPrice == "" || c.SpotPrice == "0" {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"spot_tags should not be set when not requesting a spot instance"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 20:05:31 -04:00
|
|
|
if c.TemporarySGSourceCidr == "" {
|
|
|
|
c.TemporarySGSourceCidr = "0.0.0.0/0"
|
|
|
|
} else {
|
|
|
|
if _, _, err := net.ParseCIDR(c.TemporarySGSourceCidr); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Error parsing temporary_security_group_source_cidr: %s", err.Error()))
|
|
|
|
}
|
2017-09-25 20:57:56 -04:00
|
|
|
}
|
|
|
|
|
2016-05-20 05:54:45 -04:00
|
|
|
if c.InstanceInitiatedShutdownBehavior == "" {
|
|
|
|
c.InstanceInitiatedShutdownBehavior = "stop"
|
2016-05-20 16:33:41 -04:00
|
|
|
} else if !reShutdownBehavior.MatchString(c.InstanceInitiatedShutdownBehavior) {
|
2016-12-14 15:50:26 -05:00
|
|
|
errs = append(errs, fmt.Errorf("shutdown_behavior only accepts 'stop' or 'terminate' values."))
|
2016-05-20 05:54:45 -04:00
|
|
|
}
|
|
|
|
|
2018-05-12 11:20:01 -04:00
|
|
|
if c.EnableT2Unlimited {
|
|
|
|
if c.SpotPrice != "" {
|
|
|
|
errs = append(errs, fmt.Errorf("Error: T2 Unlimited cannot be used in conjuction with Spot Instances"))
|
|
|
|
}
|
|
|
|
firstDotIndex := strings.Index(c.InstanceType, ".")
|
|
|
|
if firstDotIndex == -1 {
|
|
|
|
errs = append(errs, fmt.Errorf("Error determining main Instance Type from: %s", c.InstanceType))
|
|
|
|
} else if c.InstanceType[0:firstDotIndex] != "t2" {
|
|
|
|
errs = append(errs, fmt.Errorf("Error: T2 Unlimited enabled with a non-T2 Instance Type: %s", c.InstanceType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:23:40 -04:00
|
|
|
return errs
|
|
|
|
}
|
2017-12-08 17:56:19 -05:00
|
|
|
|
|
|
|
func (c *RunConfig) IsSpotInstance() bool {
|
|
|
|
return c.SpotPrice != "" && c.SpotPrice != "0"
|
|
|
|
}
|