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

     1  package ecs
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/denverdino/aliyungo/common"
     8  	"github.com/denverdino/aliyungo/ecs"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  type stepConfigAlicloudEIP struct {
    14  	AssociatePublicIpAddress bool
    15  	RegionId                 string
    16  	InternetChargeType       string
    17  	InternetMaxBandwidthOut  int
    18  	allocatedId              string
    19  }
    20  
    21  func (s *stepConfigAlicloudEIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    22  	client := state.Get("client").(*ecs.Client)
    23  	ui := state.Get("ui").(packer.Ui)
    24  	instance := state.Get("instance").(*ecs.InstanceAttributesType)
    25  	ui.Say("Allocating eip")
    26  	ipaddress, allocateId, err := client.AllocateEipAddress(&ecs.AllocateEipAddressArgs{
    27  		RegionId: common.Region(s.RegionId), InternetChargeType: common.InternetChargeType(s.InternetChargeType),
    28  		Bandwidth: s.InternetMaxBandwidthOut,
    29  	})
    30  	if err != nil {
    31  		state.Put("error", err)
    32  		ui.Say(fmt.Sprintf("Error allocating eip: %s", err))
    33  		return multistep.ActionHalt
    34  	}
    35  	s.allocatedId = allocateId
    36  	if err = client.WaitForEip(common.Region(s.RegionId), allocateId,
    37  		ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
    38  		state.Put("error", err)
    39  		ui.Say(fmt.Sprintf("Error allocating eip: %s", err))
    40  		return multistep.ActionHalt
    41  	}
    42  
    43  	if err = client.AssociateEipAddress(allocateId, instance.InstanceId); err != nil {
    44  		state.Put("error", err)
    45  		ui.Say(fmt.Sprintf("Error binding eip: %s", err))
    46  		return multistep.ActionHalt
    47  	}
    48  
    49  	if err = client.WaitForEip(common.Region(s.RegionId), allocateId,
    50  		ecs.EipStatusInUse, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
    51  		state.Put("error", err)
    52  		ui.Say(fmt.Sprintf("Error associating eip: %s", err))
    53  		return multistep.ActionHalt
    54  	}
    55  	ui.Say(fmt.Sprintf("Allocated eip %s", ipaddress))
    56  	state.Put("ipaddress", ipaddress)
    57  	return multistep.ActionContinue
    58  }
    59  
    60  func (s *stepConfigAlicloudEIP) Cleanup(state multistep.StateBag) {
    61  	if len(s.allocatedId) == 0 {
    62  		return
    63  	}
    64  
    65  	client := state.Get("client").(*ecs.Client)
    66  	instance := state.Get("instance").(*ecs.InstanceAttributesType)
    67  	ui := state.Get("ui").(packer.Ui)
    68  
    69  	message(state, "EIP")
    70  
    71  	if err := client.UnassociateEipAddress(s.allocatedId, instance.InstanceId); err != nil {
    72  		ui.Say(fmt.Sprintf("Failed to unassociate eip."))
    73  	}
    74  
    75  	if err := client.WaitForEip(common.Region(s.RegionId), s.allocatedId, ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
    76  		ui.Say(fmt.Sprintf("Timeout while unassociating eip."))
    77  	}
    78  	if err := client.ReleaseEipAddress(s.allocatedId); err != nil {
    79  		ui.Say(fmt.Sprintf("Failed to release eip."))
    80  	}
    81  
    82  }