2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type setpConfigAlicloudEIP struct {
|
|
|
|
AssociatePublicIpAddress bool
|
|
|
|
RegionId string
|
2017-05-25 06:27:54 -04:00
|
|
|
InternetChargeType string
|
2017-03-03 03:56:17 -05:00
|
|
|
allocatedId string
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *setpConfigAlicloudEIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say("Allocating eip")
|
2017-03-03 03:56:17 -05:00
|
|
|
ipaddress, allocateId, err := client.AllocateEipAddress(&ecs.AllocateEipAddressArgs{
|
2017-05-25 06:27:54 -04:00
|
|
|
RegionId: common.Region(s.RegionId), InternetChargeType: common.InternetChargeType(s.InternetChargeType),
|
2017-03-03 03:56:17 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Error allocating eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
s.allocatedId = allocateId
|
2017-05-25 22:59:53 -04:00
|
|
|
if err = client.WaitForEip(common.Region(s.RegionId), allocateId,
|
|
|
|
ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Error allocating eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = client.AssociateEipAddress(allocateId, instance.InstanceId); err != nil {
|
|
|
|
state.Put("error", err)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Error binding eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-05-25 22:59:53 -04:00
|
|
|
if err = client.WaitForEip(common.Region(s.RegionId), allocateId,
|
|
|
|
ecs.EipStatusInUse, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Error associating eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Allocated eip %s", ipaddress))
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("ipaddress", ipaddress)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *setpConfigAlicloudEIP) Cleanup(state multistep.StateBag) {
|
|
|
|
if len(s.allocatedId) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
message(state, "EIP")
|
|
|
|
|
|
|
|
if err := client.UnassociateEipAddress(s.allocatedId, instance.InstanceId); err != nil {
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed to unassociate eip."))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2017-05-25 21:49:35 -04:00
|
|
|
if err := client.WaitForEip(common.Region(s.RegionId), s.allocatedId, ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Timeout while unassociating eip."))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
if err := client.ReleaseEipAddress(s.allocatedId); err != nil {
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed to release eip."))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|