2019-02-04 13:58:26 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-21 11:40:14 -04:00
|
|
|
"github.com/antihax/optional"
|
2019-02-04 13:58:26 -05:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-08-20 21:37:09 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-02-04 13:58:26 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepStopBSUBackedVm struct {
|
|
|
|
Skip bool
|
|
|
|
DisableStopVm bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepStopBSUBackedVm) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-08-21 11:40:14 -04:00
|
|
|
oscconn := state.Get("osc").(*osc.APIClient)
|
2020-08-20 21:37:09 -04:00
|
|
|
vm := state.Get("vm").(osc.Vm)
|
2019-02-04 13:58:26 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
// Skip when it is a spot vm
|
|
|
|
if s.Skip {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if !s.DisableStopVm {
|
|
|
|
// Stop the vm so we can create an AMI from it
|
|
|
|
ui.Say("Stopping the source vm...")
|
|
|
|
|
|
|
|
// Amazon EC2 API follows an eventual consistency model.
|
|
|
|
|
|
|
|
// This means that if you run a command to modify or describe a resource
|
|
|
|
// that you just created, its ID might not have propagated throughout
|
|
|
|
// the system, and you will get an error responding that the resource
|
|
|
|
// does not exist.
|
|
|
|
|
|
|
|
// Work around this by retrying a few times, up to about 5 minutes.
|
|
|
|
err := common.Retry(10, 60, 6, func(i uint) (bool, error) {
|
|
|
|
ui.Message(fmt.Sprintf("Stopping vm, attempt %d", i+1))
|
|
|
|
|
2020-08-21 11:40:14 -04:00
|
|
|
_, _, err = oscconn.VmApi.StopVms(context.Background(), &osc.StopVmsOpts{
|
|
|
|
StopVmsRequest: optional.NewInterface(osc.StopVmsRequest{
|
|
|
|
VmIds: []string{vm.VmId},
|
|
|
|
}),
|
2019-02-04 13:58:26 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// success
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
if awsErr.Code() == "InvalidVmID.NotFound" {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Error stopping vm; will retry ..."+
|
|
|
|
"Error: %s", err))
|
|
|
|
// retry
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// errored, but not in expected way. Don't want to retry
|
|
|
|
return true, err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error stopping vm: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ui.Say("Automatic vm stop disabled. Please stop vm manually.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the vm to actually stop
|
|
|
|
ui.Say("Waiting for the vm to stop...")
|
2020-08-21 11:40:14 -04:00
|
|
|
err = waitUntilOscVmStopped(oscconn, vm.VmId)
|
2019-02-04 13:58:26 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for vm to stop: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepStopBSUBackedVm) Cleanup(multistep.StateBag) {
|
|
|
|
// No cleanup...
|
|
|
|
}
|