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"
|
|
|
|
)
|
|
|
|
|
2013-05-21 03:56:27 -04:00
|
|
|
type stepStopInstance struct{}
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2013-06-04 13:00:06 -04:00
|
|
|
func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepAction {
|
2013-05-21 03:55:32 -04:00
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
|
|
|
instance := state["instance"].(*ec2.Instance)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
state["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{
|
|
|
|
Conn: ec2conn,
|
|
|
|
Pending: []string{"running", "stopping"},
|
|
|
|
Target: "stopped",
|
2013-07-29 21:47:43 -04:00
|
|
|
Refresh: awscommon.InstanceStateRefreshFunc(ec2conn, instance),
|
2013-07-25 21:49:15 -04:00
|
|
|
StepState: state,
|
|
|
|
}
|
2013-07-29 21:47:43 -04:00
|
|
|
instanceRaw, err := awscommon.WaitForState(&stateChange)
|
|
|
|
instance = instanceRaw.(*ec2.Instance)
|
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)
|
|
|
|
state["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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepStopInstance) Cleanup(map[string]interface{}) {
|
|
|
|
// No cleanup...
|
|
|
|
}
|