2017-03-03 16:56:17 +08:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2017-03-03 16:56:17 +08:00
|
|
|
"fmt"
|
2017-05-25 18:49:35 -07:00
|
|
|
|
2017-03-03 16:56:17 +08:00
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 21:04:52 +08:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 16:56:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigAlicloudPublicIP struct {
|
2018-03-13 07:07:10 +00:00
|
|
|
publicIPAddress string
|
|
|
|
RegionId string
|
2018-09-11 16:56:57 +08:00
|
|
|
SSHPrivateIp bool
|
2017-03-03 16:56:17 +08:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:31:41 -08:00
|
|
|
func (s *stepConfigAlicloudPublicIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-03 16:56:17 +08:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
|
|
|
|
2018-09-11 16:56:57 +08:00
|
|
|
if s.SSHPrivateIp {
|
|
|
|
ipaddress := instance.InnerIpAddress.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
|
|
|
|
}
|
|
|
|
|
2017-03-03 16:56:17 +08:00
|
|
|
ipaddress, err := client.AllocatePublicIpAddress(instance.InstanceId)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2017-05-25 18:49:35 -07:00
|
|
|
ui.Say(fmt.Sprintf("Error allocating public ip: %s", err))
|
2017-03-03 16:56:17 +08:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-03-13 07:07:10 +00:00
|
|
|
s.publicIPAddress = ipaddress
|
2017-06-01 19:33:12 -07:00
|
|
|
ui.Say(fmt.Sprintf("Allocated public ip address %s.", ipaddress))
|
2017-03-03 16:56:17 +08:00
|
|
|
state.Put("ipaddress", ipaddress)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigAlicloudPublicIP) Cleanup(state multistep.StateBag) {
|
|
|
|
|
|
|
|
}
|