github.com/rothwerx/packer@v0.9.0/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 } 15 16 func (s *stepStopInstance) 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 // Stop the instance so we can create an AMI from it 27 ui.Say("Stopping the source instance...") 28 _, err := ec2conn.StopInstances(&ec2.StopInstancesInput{ 29 InstanceIds: []*string{instance.InstanceId}, 30 }) 31 if err != nil { 32 err := fmt.Errorf("Error stopping instance: %s", err) 33 state.Put("error", err) 34 ui.Error(err.Error()) 35 return multistep.ActionHalt 36 } 37 38 // Wait for the instance to actual stop 39 ui.Say("Waiting for the instance to stop...") 40 stateChange := awscommon.StateChangeConf{ 41 Pending: []string{"running", "stopping"}, 42 Target: "stopped", 43 Refresh: awscommon.InstanceStateRefreshFunc(ec2conn, *instance.InstanceId), 44 StepState: state, 45 } 46 _, err = awscommon.WaitForState(&stateChange) 47 if err != nil { 48 err := fmt.Errorf("Error waiting for instance to stop: %s", err) 49 state.Put("error", err) 50 ui.Error(err.Error()) 51 return multistep.ActionHalt 52 } 53 54 return multistep.ActionContinue 55 } 56 57 func (s *stepStopInstance) Cleanup(multistep.StateBag) { 58 // No cleanup... 59 }