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"
|
2017-05-25 21:49:35 -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 stepRunAlicloudInstance struct {
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepRunAlicloudInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
startInstanceRequest := ecs.CreateStartInstanceRequest()
|
|
|
|
startInstanceRequest.InstanceId = instance.InstanceId
|
|
|
|
if _, err := client.StartInstance(startInstanceRequest); err != nil {
|
|
|
|
return halt(state, err, "Error starting instance")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2018-09-25 07:21:28 -04:00
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Starting instance: %s", instance.InstanceId))
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
_, err := client.WaitForInstanceStatus(instance.RegionId, instance.InstanceId, InstanceStatusRunning)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Timeout waiting for instance to start")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepRunAlicloudInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ClientWrapper)
|
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
|
|
|
|
|
|
|
describeInstancesRequest := ecs.CreateDescribeInstancesRequest()
|
|
|
|
describeInstancesRequest.InstanceIds = fmt.Sprintf("[\"%s\"]", instance.InstanceId)
|
|
|
|
instancesResponse, _ := client.DescribeInstances(describeInstancesRequest)
|
|
|
|
|
|
|
|
if len(instancesResponse.Instances.Instance) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceAttribute := instancesResponse.Instances.Instance[0]
|
|
|
|
if instanceAttribute.Status == InstanceStatusStarting || instanceAttribute.Status == InstanceStatusRunning {
|
|
|
|
stopInstanceRequest := ecs.CreateStopInstanceRequest()
|
|
|
|
stopInstanceRequest.InstanceId = instance.InstanceId
|
|
|
|
stopInstanceRequest.ForceStop = requests.NewBoolean(true)
|
|
|
|
if _, err := client.StopInstance(stopInstanceRequest); err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Error stopping instance %s, it may still be around %s", instance.InstanceId, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.WaitForInstanceStatus(instance.RegionId, instance.InstanceId, InstanceStatusStopped)
|
|
|
|
if err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Error stopping instance %s, it may still be around %s", instance.InstanceId, err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|