From 72a7123a0bbdd1faf88b35ab307626061d15704b Mon Sep 17 00:00:00 2001 From: Chris Chalfant Date: Mon, 14 Mar 2016 12:54:03 -0400 Subject: [PATCH] Add option to tell packer not to stop the instance It is sometimes desirable to sysprep a windows machine before creating an EC2 image. The AWS-approved way to do this is to run ec2configservice.exe -sysprep and let ec2configservice shut down the instance. This change adds an option to disable the stop instance call issued by packer so that the user can control when the machine is stopped. --- builder/amazon/common/run_config.go | 3 ++- builder/amazon/ebs/builder.go | 5 ++++- builder/amazon/ebs/step_stop_instance.go | 27 ++++++++++++++---------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 307a177ed..f5e237257 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -23,7 +23,8 @@ type RunConfig struct { SourceAmi string `mapstructure:"source_ami"` SpotPrice string `mapstructure:"spot_price"` SpotPriceAutoProduct string `mapstructure:"spot_price_auto_product"` - SecurityGroupId string `mapstructure:"security_group_id"` + DisableStopInstance bool `mapstructure:"disable_stop_instance"` + SecurityGroupId string `mapstructure:"security_group_id"` SecurityGroupIds []string `mapstructure:"security_group_ids"` SubnetId string `mapstructure:"subnet_id"` TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"` diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index d8e8cea28..ebf496f34 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -150,7 +150,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe b.config.RunConfig.Comm.SSHUsername), }, &common.StepProvision{}, - &stepStopInstance{SpotPrice: b.config.SpotPrice}, + &stepStopInstance{ + SpotPrice: b.config.SpotPrice, + DisableStopInstance: b.config.DisableStopInstance, + }, // TODO(mitchellh): verify works with spots &stepModifyInstance{}, &awscommon.StepDeregisterAMI{ diff --git a/builder/amazon/ebs/step_stop_instance.go b/builder/amazon/ebs/step_stop_instance.go index d6f135368..b536aaf1b 100644 --- a/builder/amazon/ebs/step_stop_instance.go +++ b/builder/amazon/ebs/step_stop_instance.go @@ -11,6 +11,7 @@ import ( type stepStopInstance struct { SpotPrice string + DisableStopInstance bool } func (s *stepStopInstance) Run(state multistep.StateBag) multistep.StepAction { @@ -23,17 +24,21 @@ func (s *stepStopInstance) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } - // Stop the instance so we can create an AMI from it - ui.Say("Stopping the source instance...") - _, err := ec2conn.StopInstances(&ec2.StopInstancesInput{ - InstanceIds: []*string{instance.InstanceId}, - }) - if err != nil { - err := fmt.Errorf("Error stopping instance: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + var err error + + if s.DisableStopInstance == false { + // Stop the instance so we can create an AMI from it + ui.Say("Stopping the source instance...") + _, err = ec2conn.StopInstances(&ec2.StopInstancesInput{ + InstanceIds: []*string{instance.InstanceId}, + }) + if err != nil { + err := fmt.Errorf("Error stopping instance: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + } // Wait for the instance to actual stop ui.Say("Waiting for the instance to stop...")