github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/alicloud/ecs/step_stop_instance.go (about)

     1  package ecs
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/denverdino/aliyungo/ecs"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type stepStopAlicloudInstance struct {
    13  	ForceStop bool
    14  }
    15  
    16  func (s *stepStopAlicloudInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    17  	client := state.Get("client").(*ecs.Client)
    18  	instance := state.Get("instance").(*ecs.InstanceAttributesType)
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	err := client.StopInstance(instance.InstanceId, s.ForceStop)
    22  	if err != nil {
    23  		if err != nil {
    24  			err := fmt.Errorf("Error stopping alicloud instance: %s", err)
    25  			state.Put("error", err)
    26  			ui.Error(err.Error())
    27  			return multistep.ActionHalt
    28  		}
    29  	}
    30  
    31  	err = client.WaitForInstance(instance.InstanceId, ecs.Stopped, ALICLOUD_DEFAULT_TIMEOUT)
    32  	if err != nil {
    33  		err := fmt.Errorf("Error waiting for alicloud instance to stop: %s", err)
    34  		state.Put("error", err)
    35  		ui.Error(err.Error())
    36  		return multistep.ActionHalt
    37  	}
    38  
    39  	return multistep.ActionContinue
    40  }
    41  
    42  func (s *stepStopAlicloudInstance) Cleanup(multistep.StateBag) {
    43  	// No cleanup...
    44  }