From a9f914ea97fb67b716e4f71b7713872633dbe3d9 Mon Sep 17 00:00:00 2001 From: Patrick Robinson Date: Fri, 20 May 2016 19:54:45 +1000 Subject: [PATCH] Add shutdown_behaviour option This means an instance will terminate upon shutdown. --- builder/amazon/common/run_config.go | 9 ++++ .../amazon/common/step_run_source_instance.go | 52 ++++++++++--------- builder/amazon/ebs/builder.go | 31 +++++------ builder/amazon/instance/builder.go | 29 ++++++----- 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 1dbe3fca0..452bf0951 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "time" + "regexp" "github.com/mitchellh/packer/common/uuid" "github.com/mitchellh/packer/helper/communicator" @@ -32,6 +33,7 @@ type RunConfig struct { UserDataFile string `mapstructure:"user_data_file"` WindowsPasswordTimeout time.Duration `mapstructure:"windows_password_timeout"` VpcId string `mapstructure:"vpc_id"` + InstanceInitiatedShutdownBehavior string `mapstructure:"shutdown_behaviour"` // Communicator settings Comm communicator.Config `mapstructure:",squash"` @@ -84,5 +86,12 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { } } + m, _ := regexp.MatchString("(stop|terminate)", c.InstanceInitiatedShutdownBehavior) + if c.InstanceInitiatedShutdownBehavior == "" { + c.InstanceInitiatedShutdownBehavior = "stop" + } else if !m { + errs = append(errs, fmt.Errorf("shutdown_behaviour only accepts 'stop' or 'terminate' values.")) + } + return errs } diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 238450fa8..aac13fb88 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -16,21 +16,22 @@ import ( ) type StepRunSourceInstance struct { - AssociatePublicIpAddress bool - AvailabilityZone string - BlockDevices BlockDevices - Debug bool - EbsOptimized bool - ExpectedRootDevice string - InstanceType string - IamInstanceProfile string - SourceAMI string - SpotPrice string - SpotPriceProduct string - SubnetId string - Tags map[string]string - UserData string - UserDataFile string + AssociatePublicIpAddress bool + AvailabilityZone string + BlockDevices BlockDevices + Debug bool + EbsOptimized bool + ExpectedRootDevice string + InstanceType string + IamInstanceProfile string + SourceAMI string + SpotPrice string + SpotPriceProduct string + SubnetId string + Tags map[string]string + UserData string + UserDataFile string + InstanceInitiatedShutdownBehavior string instanceId string spotRequest *ec2.SpotInstanceRequest @@ -137,16 +138,17 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi if spotPrice == "" || spotPrice == "0" { runOpts := &ec2.RunInstancesInput{ - KeyName: &keyName, - ImageId: &s.SourceAMI, - InstanceType: &s.InstanceType, - UserData: &userData, - MaxCount: aws.Int64(1), - MinCount: aws.Int64(1), - IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile}, - BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(), - Placement: &ec2.Placement{AvailabilityZone: &s.AvailabilityZone}, - EbsOptimized: &s.EbsOptimized, + KeyName: &keyName, + ImageId: &s.SourceAMI, + InstanceType: &s.InstanceType, + UserData: &userData, + MaxCount: aws.Int64(1), + MinCount: aws.Int64(1), + IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile}, + BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(), + Placement: &ec2.Placement{AvailabilityZone: &s.AvailabilityZone}, + EbsOptimized: &s.EbsOptimized, + InstanceInitiatedShutdownBehavior: &s.InstanceInitiatedShutdownBehavior, } if s.SubnetId != "" && s.AssociatePublicIpAddress { diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index a4e9ebc5e..638efa5a3 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -117,21 +117,22 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe BlockDevices: b.config.BlockDevices, }, &awscommon.StepRunSourceInstance{ - Debug: b.config.PackerDebug, - ExpectedRootDevice: "ebs", - SpotPrice: b.config.SpotPrice, - SpotPriceProduct: b.config.SpotPriceAutoProduct, - InstanceType: b.config.InstanceType, - UserData: b.config.UserData, - UserDataFile: b.config.UserDataFile, - SourceAMI: b.config.SourceAmi, - IamInstanceProfile: b.config.IamInstanceProfile, - SubnetId: b.config.SubnetId, - AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, - EbsOptimized: b.config.EbsOptimized, - AvailabilityZone: b.config.AvailabilityZone, - BlockDevices: b.config.BlockDevices, - Tags: b.config.RunTags, + Debug: b.config.PackerDebug, + ExpectedRootDevice: "ebs", + SpotPrice: b.config.SpotPrice, + SpotPriceProduct: b.config.SpotPriceAutoProduct, + InstanceType: b.config.InstanceType, + UserData: b.config.UserData, + UserDataFile: b.config.UserDataFile, + SourceAMI: b.config.SourceAmi, + IamInstanceProfile: b.config.IamInstanceProfile, + SubnetId: b.config.SubnetId, + AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, + EbsOptimized: b.config.EbsOptimized, + AvailabilityZone: b.config.AvailabilityZone, + BlockDevices: b.config.BlockDevices, + Tags: b.config.RunTags, + InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior, }, &stepTagEBSVolumes{ VolumeRunTags: b.config.VolumeRunTags, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 96b8fa9cc..514ff6da7 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -204,20 +204,21 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe VpcId: b.config.VpcId, }, &awscommon.StepRunSourceInstance{ - Debug: b.config.PackerDebug, - SpotPrice: b.config.SpotPrice, - SpotPriceProduct: b.config.SpotPriceAutoProduct, - InstanceType: b.config.InstanceType, - IamInstanceProfile: b.config.IamInstanceProfile, - UserData: b.config.UserData, - UserDataFile: b.config.UserDataFile, - SourceAMI: b.config.SourceAmi, - SubnetId: b.config.SubnetId, - AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, - EbsOptimized: b.config.EbsOptimized, - AvailabilityZone: b.config.AvailabilityZone, - BlockDevices: b.config.BlockDevices, - Tags: b.config.RunTags, + Debug: b.config.PackerDebug, + SpotPrice: b.config.SpotPrice, + SpotPriceProduct: b.config.SpotPriceAutoProduct, + InstanceType: b.config.InstanceType, + IamInstanceProfile: b.config.IamInstanceProfile, + UserData: b.config.UserData, + UserDataFile: b.config.UserDataFile, + SourceAMI: b.config.SourceAmi, + SubnetId: b.config.SubnetId, + AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, + EbsOptimized: b.config.EbsOptimized, + AvailabilityZone: b.config.AvailabilityZone, + BlockDevices: b.config.BlockDevices, + Tags: b.config.RunTags, + InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior, }, &awscommon.StepGetPassword{ Debug: b.config.PackerDebug,