2019-06-13 03:16:49 -04:00
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-12-17 05:25:56 -05:00
|
|
|
"time"
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
ucloudcommon "github.com/hashicorp/packer/builder/ucloud/common"
|
2019-06-13 03:16:49 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/services/uhost"
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/ucloud"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepStopInstance struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepStopInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-10-12 04:46:21 -04:00
|
|
|
client := state.Get("client").(*ucloudcommon.UCloudClient)
|
|
|
|
conn := client.UHostConn
|
2019-06-13 03:16:49 -04:00
|
|
|
instance := state.Get("instance").(*uhost.UHostInstanceSet)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
instance, err := client.DescribeUHostById(instance.UHostId)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on reading instance when stopping %q", instance.UHostId))
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
if instance.State != ucloudcommon.InstanceStateStopped {
|
2019-07-26 05:03:57 -04:00
|
|
|
stopReq := conn.NewStopUHostInstanceRequest()
|
2019-06-13 03:16:49 -04:00
|
|
|
stopReq.UHostId = ucloud.String(instance.UHostId)
|
|
|
|
ui.Say(fmt.Sprintf("Stopping instance %q", instance.UHostId))
|
|
|
|
err = retry.Config{
|
|
|
|
Tries: 5,
|
|
|
|
ShouldRetry: func(err error) bool {
|
|
|
|
return err != nil
|
|
|
|
},
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 2 * time.Second, MaxBackoff: 6 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
2019-07-26 05:03:57 -04:00
|
|
|
if _, err = conn.StopUHostInstance(stopReq); err != nil {
|
2019-06-13 03:16:49 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on stopping instance %q", instance.UHostId))
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = retry.Config{
|
|
|
|
Tries: 20,
|
|
|
|
ShouldRetry: func(err error) bool {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.IsExpectedStateError(err)
|
2019-06-13 03:16:49 -04:00
|
|
|
},
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 2 * time.Second, MaxBackoff: 6 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
2019-10-12 04:46:21 -04:00
|
|
|
instance, err := client.DescribeUHostById(instance.UHostId)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
if instance.State != ucloudcommon.InstanceStateStopped {
|
|
|
|
return ucloudcommon.NewExpectedStateError("instance", instance.UHostId)
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on waiting for stopping instance when stopping %q", instance.UHostId))
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 09:32:33 -04:00
|
|
|
ui.Message(fmt.Sprintf("Stopping instance %q complete", instance.UHostId))
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepStopInstance) Cleanup(multistep.StateBag) {
|
|
|
|
}
|