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