github.phpd.cn/hashicorp/packer@v1.3.2/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 SSHPrivateIp bool 20 } 21 22 func (s *stepConfigAlicloudEIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 23 client := state.Get("client").(*ecs.Client) 24 ui := state.Get("ui").(packer.Ui) 25 instance := state.Get("instance").(*ecs.InstanceAttributesType) 26 27 if s.SSHPrivateIp { 28 ipaddress := instance.VpcAttributes.PrivateIpAddress.IpAddress 29 if len(ipaddress) == 0 { 30 ui.Say("Failed to get private ip of instance") 31 return multistep.ActionHalt 32 } 33 state.Put("ipaddress", ipaddress[0]) 34 return multistep.ActionContinue 35 } 36 37 ui.Say("Allocating eip") 38 ipaddress, allocateId, err := client.AllocateEipAddress(&ecs.AllocateEipAddressArgs{ 39 RegionId: common.Region(s.RegionId), InternetChargeType: common.InternetChargeType(s.InternetChargeType), 40 Bandwidth: s.InternetMaxBandwidthOut, 41 }) 42 if err != nil { 43 state.Put("error", err) 44 ui.Say(fmt.Sprintf("Error allocating eip: %s", err)) 45 return multistep.ActionHalt 46 } 47 s.allocatedId = allocateId 48 if err = client.WaitForEip(common.Region(s.RegionId), allocateId, 49 ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil { 50 state.Put("error", err) 51 ui.Say(fmt.Sprintf("Error allocating eip: %s", err)) 52 return multistep.ActionHalt 53 } 54 55 if err = client.AssociateEipAddress(allocateId, instance.InstanceId); err != nil { 56 state.Put("error", err) 57 ui.Say(fmt.Sprintf("Error binding eip: %s", err)) 58 return multistep.ActionHalt 59 } 60 61 if err = client.WaitForEip(common.Region(s.RegionId), allocateId, 62 ecs.EipStatusInUse, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil { 63 state.Put("error", err) 64 ui.Say(fmt.Sprintf("Error associating eip: %s", err)) 65 return multistep.ActionHalt 66 } 67 ui.Say(fmt.Sprintf("Allocated eip %s", ipaddress)) 68 state.Put("ipaddress", ipaddress) 69 return multistep.ActionContinue 70 } 71 72 func (s *stepConfigAlicloudEIP) Cleanup(state multistep.StateBag) { 73 if len(s.allocatedId) == 0 { 74 return 75 } 76 77 client := state.Get("client").(*ecs.Client) 78 instance := state.Get("instance").(*ecs.InstanceAttributesType) 79 ui := state.Get("ui").(packer.Ui) 80 81 message(state, "EIP") 82 83 if err := client.UnassociateEipAddress(s.allocatedId, instance.InstanceId); err != nil { 84 ui.Say(fmt.Sprintf("Failed to unassociate eip.")) 85 } 86 87 if err := client.WaitForEip(common.Region(s.RegionId), s.allocatedId, ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil { 88 ui.Say(fmt.Sprintf("Timeout while unassociating eip.")) 89 } 90 if err := client.ReleaseEipAddress(s.allocatedId); err != nil { 91 ui.Say(fmt.Sprintf("Failed to release eip.")) 92 } 93 94 }