2013-07-15 02:02:18 -04:00
|
|
|
package ebs
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-19 23:54:02 -04:00
|
|
|
"fmt"
|
2013-05-21 03:55:32 -04:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2013-06-04 13:00:06 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-07-20 22:58:27 -04:00
|
|
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
2013-05-21 03:55:32 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2014-05-08 01:58:05 -04:00
|
|
|
type stepStopInstance struct {
|
|
|
|
SpotPrice string
|
2014-05-07 13:13:27 -04:00
|
|
|
}
|
2013-05-21 03:55:32 -04:00
|
|
|
|
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)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2014-05-08 01:58:05 -04:00
|
|
|
// Skip when it is a spot instance
|
|
|
|
if s.SpotPrice != "" {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2014-05-07 13:13:27 -04:00
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
// Stop the instance so we can create an AMI from it
|
|
|
|
ui.Say("Stopping the source instance...")
|
|
|
|
_, err := ec2conn.StopInstances(instance.InstanceId)
|
|
|
|
if err != nil {
|
2013-06-19 23:54:02 -04:00
|
|
|
err := fmt.Errorf("Error stopping instance: %s", err)
|
2013-08-31 16:00:43 -04:00
|
|
|
state.Put("error", err)
|
2013-05-21 03:55:32 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the instance to actual stop
|
|
|
|
ui.Say("Waiting for the instance to stop...")
|
2013-07-25 21:49:15 -04:00
|
|
|
stateChange := awscommon.StateChangeConf{
|
|
|
|
Pending: []string{"running", "stopping"},
|
|
|
|
Target: "stopped",
|
2013-07-29 21:55:11 -04:00
|
|
|
Refresh: awscommon.InstanceStateRefreshFunc(ec2conn, instance),
|
2013-07-25 21:49:15 -04:00
|
|
|
StepState: state,
|
|
|
|
}
|
2013-07-31 12:35:26 -04:00
|
|
|
_, err = awscommon.WaitForState(&stateChange)
|
2013-05-21 03:55:32 -04:00
|
|
|
if err != nil {
|
2013-06-19 23:54:02 -04:00
|
|
|
err := fmt.Errorf("Error waiting for instance to stop: %s", err)
|
2013-08-31 16:00:43 -04:00
|
|
|
state.Put("error", err)
|
2013-05-21 03:55:32 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionContinue
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 16:00:43 -04:00
|
|
|
func (s *stepStopInstance) Cleanup(multistep.StateBag) {
|
2013-05-21 03:55:32 -04:00
|
|
|
// No cleanup...
|
|
|
|
}
|