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"
|
2019-04-26 03:12:07 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/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
|
|
|
)
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
type stepConfigAlicloudEIP struct {
|
2017-03-03 03:56:17 -05:00
|
|
|
AssociatePublicIpAddress bool
|
|
|
|
RegionId string
|
2017-05-25 06:27:54 -04:00
|
|
|
InternetChargeType string
|
2018-06-23 04:34:45 -04:00
|
|
|
InternetMaxBandwidthOut int
|
2017-03-03 03:56:17 -05:00
|
|
|
allocatedId string
|
2018-09-11 04:56:57 -04:00
|
|
|
SSHPrivateIp bool
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
var allocateEipAddressRetryErrors = []string{
|
|
|
|
"LastTokenProcessing",
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConfigAlicloudEIP) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
2018-09-11 04:56:57 -04:00
|
|
|
|
|
|
|
if s.SSHPrivateIp {
|
|
|
|
ipaddress := instance.VpcAttributes.PrivateIpAddress.IpAddress
|
|
|
|
if len(ipaddress) == 0 {
|
|
|
|
ui.Say("Failed to get private ip of instance")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put("ipaddress", ipaddress[0])
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
ui.Say("Allocating eip...")
|
|
|
|
|
|
|
|
allocateEipAddressRequest := s.buildAllocateEipAddressRequest(state)
|
|
|
|
allocateEipAddressResponse, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
return client.AllocateEipAddress(allocateEipAddressRequest)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(allocateEipAddressRetryErrors, EvalRetryErrorType),
|
2017-03-03 03:56:17 -05:00
|
|
|
})
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Error allocating eip")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
ipaddress := allocateEipAddressResponse.(*ecs.AllocateEipAddressResponse).EipAddress
|
|
|
|
ui.Message(fmt.Sprintf("Allocated eip: %s", ipaddress))
|
|
|
|
|
|
|
|
allocateId := allocateEipAddressResponse.(*ecs.AllocateEipAddressResponse).AllocationId
|
2017-03-03 03:56:17 -05:00
|
|
|
s.allocatedId = allocateId
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
err = s.waitForEipStatus(client, instance.RegionId, s.allocatedId, EipStatusAvailable)
|
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "Error wait eip available timeout")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
associateEipAddressRequest := ecs.CreateAssociateEipAddressRequest()
|
|
|
|
associateEipAddressRequest.AllocationId = allocateId
|
|
|
|
associateEipAddressRequest.InstanceId = instance.InstanceId
|
|
|
|
if _, err := client.AssociateEipAddress(associateEipAddressRequest); err != nil {
|
|
|
|
e, ok := err.(errors.Error)
|
|
|
|
if !ok || e.ErrorCode() != "TaskConflict" {
|
|
|
|
return halt(state, err, "Error associating eip")
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Error(fmt.Sprintf("Error associate eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
err = s.waitForEipStatus(client, instance.RegionId, s.allocatedId, EipStatusInUse)
|
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "Error wait eip associated timeout")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("ipaddress", ipaddress)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
func (s *stepConfigAlicloudEIP) Cleanup(state multistep.StateBag) {
|
2017-03-03 03:56:17 -05:00
|
|
|
if len(s.allocatedId) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
cleanUpMessage(state, "EIP")
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
unassociateEipAddressRequest := ecs.CreateUnassociateEipAddressRequest()
|
|
|
|
unassociateEipAddressRequest.AllocationId = s.allocatedId
|
|
|
|
unassociateEipAddressRequest.InstanceId = instance.InstanceId
|
|
|
|
if _, err := client.UnassociateEipAddress(unassociateEipAddressRequest); err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Failed to unassociate eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
if err := s.waitForEipStatus(client, instance.RegionId, s.allocatedId, EipStatusAvailable); err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Timeout while unassociating eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
releaseEipAddressRequest := ecs.CreateReleaseEipAddressRequest()
|
|
|
|
releaseEipAddressRequest.AllocationId = s.allocatedId
|
|
|
|
if _, err := client.ReleaseEipAddress(releaseEipAddressRequest); err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Failed to release eip: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigAlicloudEIP) waitForEipStatus(client *ClientWrapper, regionId string, allocationId string, expectedStatus string) error {
|
|
|
|
describeEipAddressesRequest := ecs.CreateDescribeEipAddressesRequest()
|
|
|
|
describeEipAddressesRequest.RegionId = regionId
|
|
|
|
describeEipAddressesRequest.AllocationId = s.allocatedId
|
|
|
|
|
|
|
|
_, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
response, err := client.DescribeEipAddresses(describeEipAddressesRequest)
|
|
|
|
if err == nil && len(response.EipAddresses.EipAddress) == 0 {
|
|
|
|
err = fmt.Errorf("eip allocated is not find")
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
},
|
|
|
|
EvalFunc: func(response responses.AcsResponse, err error) WaitForExpectEvalResult {
|
|
|
|
if err != nil {
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
}
|
|
|
|
|
|
|
|
eipAddressesResponse := response.(*ecs.DescribeEipAddressesResponse)
|
|
|
|
eipAddresses := eipAddressesResponse.EipAddresses.EipAddress
|
|
|
|
|
|
|
|
for _, eipAddress := range eipAddresses {
|
|
|
|
if eipAddress.Status == expectedStatus {
|
|
|
|
return WaitForExpectSuccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
},
|
|
|
|
RetryTimes: shortRetryTimes,
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigAlicloudEIP) buildAllocateEipAddressRequest(state multistep.StateBag) *ecs.AllocateEipAddressRequest {
|
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
|
|
|
|
|
|
|
request := ecs.CreateAllocateEipAddressRequest()
|
|
|
|
request.ClientToken = uuid.TimeOrderedUUID()
|
|
|
|
request.RegionId = instance.RegionId
|
|
|
|
request.InternetChargeType = s.InternetChargeType
|
|
|
|
request.Bandwidth = string(convertNumber(s.InternetMaxBandwidthOut))
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
return request
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|