Merge pull request #3352 from chalfant/disable-stop-instance

Disable stop instance
This commit is contained in:
Chris Bednarski 2016-05-24 15:40:54 -07:00
commit 42a9d9d5a1
4 changed files with 37 additions and 13 deletions

View File

@ -23,6 +23,7 @@ type RunConfig struct {
SourceAmi string `mapstructure:"source_ami"` SourceAmi string `mapstructure:"source_ami"`
SpotPrice string `mapstructure:"spot_price"` SpotPrice string `mapstructure:"spot_price"`
SpotPriceAutoProduct string `mapstructure:"spot_price_auto_product"` SpotPriceAutoProduct string `mapstructure:"spot_price_auto_product"`
DisableStopInstance bool `mapstructure:"disable_stop_instance"`
SecurityGroupId string `mapstructure:"security_group_id"` SecurityGroupId string `mapstructure:"security_group_id"`
SecurityGroupIds []string `mapstructure:"security_group_ids"` SecurityGroupIds []string `mapstructure:"security_group_ids"`
SubnetId string `mapstructure:"subnet_id"` SubnetId string `mapstructure:"subnet_id"`

View File

@ -150,7 +150,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
b.config.RunConfig.Comm.SSHUsername), b.config.RunConfig.Comm.SSHUsername),
}, },
&common.StepProvision{}, &common.StepProvision{},
&stepStopInstance{SpotPrice: b.config.SpotPrice}, &stepStopInstance{
SpotPrice: b.config.SpotPrice,
DisableStopInstance: b.config.DisableStopInstance,
},
// TODO(mitchellh): verify works with spots // TODO(mitchellh): verify works with spots
&stepModifyInstance{}, &stepModifyInstance{},
&awscommon.StepDeregisterAMI{ &awscommon.StepDeregisterAMI{

View File

@ -10,7 +10,8 @@ import (
) )
type stepStopInstance struct { type stepStopInstance struct {
SpotPrice string SpotPrice string
DisableStopInstance bool
} }
func (s *stepStopInstance) Run(state multistep.StateBag) multistep.StepAction { func (s *stepStopInstance) Run(state multistep.StateBag) multistep.StepAction {
@ -23,16 +24,22 @@ func (s *stepStopInstance) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionContinue return multistep.ActionContinue
} }
// Stop the instance so we can create an AMI from it var err error
ui.Say("Stopping the source instance...")
_, err := ec2conn.StopInstances(&ec2.StopInstancesInput{ if !s.DisableStopInstance {
InstanceIds: []*string{instance.InstanceId}, // Stop the instance so we can create an AMI from it
}) ui.Say("Stopping the source instance...")
if err != nil { _, err = ec2conn.StopInstances(&ec2.StopInstancesInput{
err := fmt.Errorf("Error stopping instance: %s", err) InstanceIds: []*string{instance.InstanceId},
state.Put("error", err) })
ui.Error(err.Error()) if err != nil {
return multistep.ActionHalt err := fmt.Errorf("Error stopping instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
} else {
ui.Say("Automatic instance stop disabled. Please stop instance manually.")
} }
// Wait for the instance to actual stop // Wait for the instance to actual stop

View File

@ -116,6 +116,19 @@ builder.
- `availability_zone` (string) - Destination availability zone to launch - `availability_zone` (string) - Destination availability zone to launch
instance in. Leave this empty to allow Amazon to auto-assign. instance in. Leave this empty to allow Amazon to auto-assign.
- `disable_stop_instance` (boolean) - Packer normally stops the build instance
after all provisioners have run. For Windows instances, it is sometimes
desirable to [run Sysprep](http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ami-create-standard.html)
which will stop the instance for you. If this is set to true, Packer *will not*
stop the instance and will wait for you to stop it manually. You can do this
with a [windows-shell provisioner](https://www.packer.io/docs/provisioners/windows-shell.html).
``` {.javascript}
{
"type": "windows-shell",
"inline": ["\"c:\\Program Files\\Amazon\\Ec2ConfigService\\ec2config.exe\" -sysprep"]
}```
- `ebs_optimized` (boolean) - Mark instance as [EBS - `ebs_optimized` (boolean) - Mark instance as [EBS
Optimized](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html). Optimized](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html).
Default `false`. Default `false`.
@ -224,7 +237,7 @@ Here is a basic example. You will need to provide access keys, and may need to c
environmental variables. See the configuration reference in the section above environmental variables. See the configuration reference in the section above
for more information on what environmental variables Packer will look for. for more information on what environmental variables Packer will look for.
Further information on locating AMI IDs and their relationship to instance types and regions can be found in the AWS EC2 Documentation [for Linux](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html) or [for Windows](http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/finding-an-ami.html). Further information on locating AMI IDs and their relationship to instance types and regions can be found in the AWS EC2 Documentation [for Linux](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html) or [for Windows](http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/finding-an-ami.html).
## Accessing the Instance to Debug ## Accessing the Instance to Debug