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