Commit 1e1fa3de authored by Henry Huang's avatar Henry Huang

Add the cleanup for existing spot request

parent 5d410bdd
...@@ -22,7 +22,7 @@ type StepRunSourceInstance struct { ...@@ -22,7 +22,7 @@ type StepRunSourceInstance struct {
Tags map[string]string Tags map[string]string
UserData string UserData string
UserDataFile string UserDataFile string
spotRequest *ec2.SpotRequestResult
instance *ec2.Instance instance *ec2.Instance
} }
...@@ -92,7 +92,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi ...@@ -92,7 +92,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt return multistep.ActionHalt
} }
instanceId = []string{runResp.Instances[0].InstanceId} instanceId = []string{runResp.Instances[0].InstanceId}
} else { } else {
runOpts := &ec2.RequestSpotInstances{ runOpts := &ec2.RequestSpotInstances{
SpotPrice: s.SpotPrice, SpotPrice: s.SpotPrice,
...@@ -136,7 +135,8 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi ...@@ -136,7 +135,8 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
instanceId = []string{spotResp.SpotRequestResults[0].InstanceId} s.spotRequest = &spotResp.SpotRequestResults[0]
instanceId = []string{s.spotRequest.InstanceId}
} }
instanceResp, err := ec2conn.Instances(instanceId, nil) instanceResp, err := ec2conn.Instances(instanceId, nil)
...@@ -198,19 +198,35 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi ...@@ -198,19 +198,35 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
} }
func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) { func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) {
if s.instance == nil {
return
}
ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Cancel the spot request if it exists
if s.spotRequest != nil {
ui.Say("Cancelling the spot request...")
if _, err := ec2conn.CancelSpotRequests([]string{s.spotRequest.SpotRequestId}); err != nil {
ui.Error(fmt.Sprintf("Error cancelling the spot request, may still be around: %s", err))
return
}
stateChange := StateChangeConf{
Pending: []string{"active", "open"},
Refresh: SpotRequestStateRefreshFunc(ec2conn, s.spotRequest.SpotRequestId),
Target: "cancelled",
}
WaitForState(&stateChange)
}
// Terminate the source instance if it exists
if s.instance != nil {
ui.Say("Terminating the source AWS instance...") ui.Say("Terminating the source AWS instance...")
if _, err := ec2conn.TerminateInstances([]string{s.instance.InstanceId}); err != nil { if _, err := ec2conn.TerminateInstances([]string{s.instance.InstanceId}); err != nil {
ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err)) ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err))
return return
} }
stateChange := StateChangeConf{ stateChange := StateChangeConf{
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"}, Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
Refresh: InstanceStateRefreshFunc(ec2conn, s.instance), Refresh: InstanceStateRefreshFunc(ec2conn, s.instance),
...@@ -218,4 +234,5 @@ func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) { ...@@ -218,4 +234,5 @@ func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) {
} }
WaitForState(&stateChange) WaitForState(&stateChange)
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment