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