Files
Packer-Cn/builder/yandex/step_teardown_instance.go
T

58 lines
1.5 KiB
Go
Raw Normal View History

2019-04-04 16:17:51 +03:00
package yandex
import (
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
ycsdk "github.com/yandex-cloud/go-sdk"
)
type StepTeardownInstance struct{}
2019-04-04 16:17:51 +03:00
func (s *StepTeardownInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2019-04-04 16:17:51 +03:00
sdk := state.Get("sdk").(*ycsdk.SDK)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
instanceID := state.Get("instance_id").(string)
2019-06-06 16:41:58 +03:00
ui.Say("Stopping instance...")
2019-04-04 16:17:51 +03:00
ctx, cancel := context.WithTimeout(ctx, c.StateTimeout)
defer cancel()
2019-06-06 16:41:58 +03:00
op, err := sdk.WrapOperation(sdk.Compute().Instance().Stop(ctx, &compute.StopInstanceRequest{
InstanceId: instanceID,
}))
if err != nil {
return stepHaltWithError(state, fmt.Errorf("Error stopping instance: %s", err))
}
err = op.Wait(ctx)
if err != nil {
return stepHaltWithError(state, fmt.Errorf("Error stopping instance: %s", err))
}
2019-04-04 16:17:51 +03:00
2019-06-06 16:41:58 +03:00
ui.Say("Deleting instance...")
op, err = sdk.WrapOperation(sdk.Compute().Instance().Delete(ctx, &compute.DeleteInstanceRequest{
2019-04-04 16:17:51 +03:00
InstanceId: instanceID,
}))
if err != nil {
return stepHaltWithError(state, fmt.Errorf("Error deleting instance: %s", err))
}
err = op.Wait(ctx)
if err != nil {
return stepHaltWithError(state, fmt.Errorf("Error deleting instance: %s", err))
}
ui.Message("Instance has been deleted!")
state.Put("instance_id", "")
return multistep.ActionContinue
}
func (s *StepTeardownInstance) Cleanup(state multistep.StateBag) {
2019-04-04 16:17:51 +03:00
// no cleanup
}