packer-cn/builder/amazon/ebs/step_stop_instance.go

60 lines
1.5 KiB
Go
Raw Normal View History

package ebs
import (
"fmt"
2015-06-03 17:13:52 -04:00
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mitchellh/multistep"
awscommon "github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/packer"
)
2014-05-08 01:58:05 -04:00
type stepStopInstance struct {
SpotPrice string
}
2013-08-31 16:00:43 -04:00
func (s *stepStopInstance) Run(state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2)
instance := state.Get("instance").(*ec2.Instance)
ui := state.Get("ui").(packer.Ui)
2014-05-08 01:58:05 -04:00
// Skip when it is a spot instance
if s.SpotPrice != "" {
return multistep.ActionContinue
}
// Stop the instance so we can create an AMI from it
ui.Say("Stopping the source instance...")
_, err := ec2conn.StopInstances(&ec2.StopInstancesInput{
InstanceIDs: []*string{instance.InstanceID},
})
if err != nil {
err := fmt.Errorf("Error stopping instance: %s", err)
2013-08-31 16:00:43 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Wait for the instance to actual stop
ui.Say("Waiting for the instance to stop...")
stateChange := awscommon.StateChangeConf{
Pending: []string{"running", "stopping"},
Target: "stopped",
2015-06-10 13:33:01 -04:00
Refresh: awscommon.InstanceStateRefreshFunc(ec2conn, *instance.InstanceID),
StepState: state,
}
_, err = awscommon.WaitForState(&stateChange)
if err != nil {
err := fmt.Errorf("Error waiting for instance to stop: %s", err)
2013-08-31 16:00:43 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
2013-08-31 16:00:43 -04:00
func (s *stepStopInstance) Cleanup(multistep.StateBag) {
// No cleanup...
}