2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2019-04-25 22:37:49 -04:00
|
|
|
"strconv"
|
2017-06-01 20:14:51 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
|
|
|
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepStopAlicloudInstance struct {
|
2018-09-25 07:21:28 -04:00
|
|
|
ForceStop bool
|
|
|
|
DisableStop bool
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepStopAlicloudInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2018-09-25 07:21:28 -04:00
|
|
|
if !s.DisableStop {
|
|
|
|
ui.Say(fmt.Sprintf("Stopping instance: %s", instance.InstanceId))
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
stopInstanceRequest := ecs.CreateStopInstanceRequest()
|
|
|
|
stopInstanceRequest.InstanceId = instance.InstanceId
|
|
|
|
stopInstanceRequest.ForceStop = requests.Boolean(strconv.FormatBool(s.ForceStop))
|
|
|
|
if _, err := client.StopInstance(stopInstanceRequest); err != nil {
|
|
|
|
return halt(state, err, "Error stopping alicloud instance")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:21:28 -04:00
|
|
|
ui.Say(fmt.Sprintf("Waiting instance stopped: %s", instance.InstanceId))
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
_, err := client.WaitForInstanceStatus(instance.RegionId, instance.InstanceId, InstanceStatusStopped)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Error waiting for alicloud instance to stop")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepStopAlicloudInstance) Cleanup(multistep.StateBag) {
|
|
|
|
// No cleanup...
|
|
|
|
}
|