github.phpd.cn/hashicorp/packer@v1.3.2/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  	DisableStop bool
    15  }
    16  
    17  func (s *stepStopAlicloudInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    18  	client := state.Get("client").(*ecs.Client)
    19  	instance := state.Get("instance").(*ecs.InstanceAttributesType)
    20  	ui := state.Get("ui").(packer.Ui)
    21  
    22  	if !s.DisableStop {
    23  		ui.Say(fmt.Sprintf("Stopping instance: %s", instance.InstanceId))
    24  		err := client.StopInstance(instance.InstanceId, s.ForceStop)
    25  		if err != nil {
    26  			err := fmt.Errorf("Error stopping alicloud instance: %s", err)
    27  			state.Put("error", err)
    28  			ui.Error(err.Error())
    29  			return multistep.ActionHalt
    30  		}
    31  	}
    32  
    33  	ui.Say(fmt.Sprintf("Waiting instance stopped: %s", instance.InstanceId))
    34  
    35  	err := client.WaitForInstance(instance.InstanceId, ecs.Stopped, ALICLOUD_DEFAULT_TIMEOUT)
    36  	if err != nil {
    37  		err := fmt.Errorf("Error waiting for alicloud instance to stop: %s", err)
    38  		state.Put("error", err)
    39  		ui.Error(err.Error())
    40  		return multistep.ActionHalt
    41  	}
    42  
    43  	return multistep.ActionContinue
    44  }
    45  
    46  func (s *stepStopAlicloudInstance) Cleanup(multistep.StateBag) {
    47  	// No cleanup...
    48  }