github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/amazon/common/step_stop_ebs_instance.go (about) 1 package common 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/service/ec2" 7 "github.com/mitchellh/multistep" 8 "github.com/mitchellh/packer/packer" 9 ) 10 11 type StepStopEBSBackedInstance struct { 12 SpotPrice string 13 DisableStopInstance bool 14 } 15 16 func (s *StepStopEBSBackedInstance) Run(state multistep.StateBag) multistep.StepAction { 17 ec2conn := state.Get("ec2").(*ec2.EC2) 18 instance := state.Get("instance").(*ec2.Instance) 19 ui := state.Get("ui").(packer.Ui) 20 21 // Skip when it is a spot instance 22 if s.SpotPrice != "" && s.SpotPrice != "0" { 23 return multistep.ActionContinue 24 } 25 26 var err error 27 28 if !s.DisableStopInstance { 29 // Stop the instance so we can create an AMI from it 30 ui.Say("Stopping the source instance...") 31 _, err = ec2conn.StopInstances(&ec2.StopInstancesInput{ 32 InstanceIds: []*string{instance.InstanceId}, 33 }) 34 if err != nil { 35 err := fmt.Errorf("Error stopping instance: %s", err) 36 state.Put("error", err) 37 ui.Error(err.Error()) 38 return multistep.ActionHalt 39 } 40 } else { 41 ui.Say("Automatic instance stop disabled. Please stop instance manually.") 42 } 43 44 // Wait for the instance to actual stop 45 ui.Say("Waiting for the instance to stop...") 46 stateChange := StateChangeConf{ 47 Pending: []string{"running", "stopping"}, 48 Target: "stopped", 49 Refresh: InstanceStateRefreshFunc(ec2conn, *instance.InstanceId), 50 StepState: state, 51 } 52 _, err = WaitForState(&stateChange) 53 if err != nil { 54 err := fmt.Errorf("Error waiting for instance to stop: %s", err) 55 state.Put("error", err) 56 ui.Error(err.Error()) 57 return multistep.ActionHalt 58 } 59 60 return multistep.ActionContinue 61 } 62 63 func (s *StepStopEBSBackedInstance) Cleanup(multistep.StateBag) { 64 // No cleanup... 65 }