2014-02-27 03:34:24 -05:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-02-27 03:34:24 -05:00
|
|
|
"fmt"
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2018-06-12 04:38:54 -04:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-02-27 03:34:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepAllocateIp struct {
|
2019-07-17 18:10:52 -04:00
|
|
|
FloatingIPNetwork string
|
|
|
|
FloatingIP string
|
|
|
|
ReuseIPs bool
|
|
|
|
InstanceFloatingIPNet string
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepAllocateIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2015-06-12 00:16:43 -04:00
|
|
|
server := state.Get("server").(*servers.Server)
|
|
|
|
|
2018-10-29 14:36:08 -04:00
|
|
|
var instanceIP floatingips.FloatingIP
|
|
|
|
|
|
|
|
// This is here in case we error out before putting instanceIp into the
|
|
|
|
// statebag below, because it is requested by Cleanup()
|
|
|
|
state.Put("access_ip", &instanceIP)
|
|
|
|
|
|
|
|
if s.FloatingIP == "" && !s.ReuseIPs && s.FloatingIPNetwork == "" {
|
|
|
|
ui.Message("Floating IP not required")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
// We need the v2 compute client
|
2018-06-12 04:38:54 -04:00
|
|
|
computeClient, err := config.computeV2Client()
|
2015-06-12 00:16:43 -04:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing compute client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-02-27 03:34:24 -05:00
|
|
|
|
2018-10-29 14:36:08 -04:00
|
|
|
// We need the v2 network client
|
|
|
|
networkClient, err := config.networkV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing network client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2018-06-12 04:38:54 -04:00
|
|
|
}
|
|
|
|
|
2018-08-17 00:55:31 -04:00
|
|
|
// Try to Use the OpenStack floating IP by checking provided parameters in
|
|
|
|
// the following order:
|
|
|
|
// - try to use "FloatingIP" ID directly if it's provided
|
|
|
|
// - try to find free floating IP in the project if "ReuseIPs" is set
|
|
|
|
// - create a new floating IP if "FloatingIPNetwork" is provided (it can be
|
|
|
|
// ID or name of the network).
|
2018-06-12 04:38:54 -04:00
|
|
|
if s.FloatingIP != "" {
|
2018-08-16 17:49:06 -04:00
|
|
|
// Try to use FloatingIP if it was provided by the user.
|
2018-06-12 04:38:54 -04:00
|
|
|
freeFloatingIP, err := CheckFloatingIP(networkClient, s.FloatingIP)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error using provided floating IP '%s': %s", s.FloatingIP, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2018-06-12 04:38:54 -04:00
|
|
|
instanceIP = *freeFloatingIP
|
|
|
|
ui.Message(fmt.Sprintf("Selected floating IP: '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
|
|
|
state.Put("floatingip_istemp", false)
|
|
|
|
} else if s.ReuseIPs {
|
|
|
|
// If ReuseIPs is set to true and we have a free floating IP, use it rather
|
|
|
|
// than creating one.
|
|
|
|
ui.Say(fmt.Sprint("Searching for unassociated floating IP"))
|
|
|
|
freeFloatingIP, err := FindFreeFloatingIP(networkClient)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error searching for floating IP: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceIP = *freeFloatingIP
|
|
|
|
ui.Message(fmt.Sprintf("Selected floating IP: '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
|
|
|
state.Put("floatingip_istemp", false)
|
2018-08-16 17:49:06 -04:00
|
|
|
} else if s.FloatingIPNetwork != "" {
|
|
|
|
// Lastly, if FloatingIPNetwork was provided by the user, we need to use it
|
|
|
|
// to allocate a new floating IP and associate it to the instance.
|
|
|
|
floatingNetwork, err := CheckFloatingIPNetwork(networkClient, s.FloatingIPNetwork)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error using the provided floating_ip_network: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2018-06-12 04:38:54 -04:00
|
|
|
}
|
|
|
|
|
2018-08-16 16:38:41 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating floating IP using network %s ...", floatingNetwork))
|
2018-06-12 04:38:54 -04:00
|
|
|
newIP, err := floatingips.Create(networkClient, floatingips.CreateOpts{
|
|
|
|
FloatingNetworkID: floatingNetwork,
|
|
|
|
}).Extract()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating floating IP from floating network '%s': %s", floatingNetwork, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2017-01-04 14:59:49 -05:00
|
|
|
}
|
2018-06-12 04:38:54 -04:00
|
|
|
|
|
|
|
instanceIP = *newIP
|
|
|
|
ui.Message(fmt.Sprintf("Created floating IP: '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
|
|
|
state.Put("floatingip_istemp", true)
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2018-08-16 17:49:06 -04:00
|
|
|
// Assoctate a floating IP if it was obtained in the previous steps.
|
2018-06-12 04:38:54 -04:00
|
|
|
if instanceIP.ID != "" {
|
|
|
|
ui.Say(fmt.Sprintf("Associating floating IP '%s' (%s) with instance port...",
|
|
|
|
instanceIP.ID, instanceIP.FloatingIP))
|
|
|
|
|
2019-07-17 18:00:28 -04:00
|
|
|
portID, err := GetInstancePortID(computeClient, server.ID, s.InstanceFloatingIPNet)
|
2018-06-12 04:38:54 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error getting interfaces of the instance '%s': %s", server.ID, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = floatingips.Update(networkClient, instanceIP.ID, floatingips.UpdateOpts{
|
|
|
|
PortID: &portID,
|
|
|
|
}).Extract()
|
2015-06-12 00:16:43 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf(
|
2018-06-12 04:38:54 -04:00
|
|
|
"Error associating floating IP '%s' (%s) with instance port '%s': %s",
|
|
|
|
instanceIP.ID, instanceIP.FloatingIP, portID, err)
|
2014-02-27 03:34:24 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2015-06-12 15:23:05 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
2018-06-12 04:38:54 -04:00
|
|
|
"Added floating IP '%s' (%s) to instance!", instanceIP.ID, instanceIP.FloatingIP))
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2018-06-12 04:38:54 -04:00
|
|
|
state.Put("access_ip", &instanceIP)
|
2014-02-27 03:34:24 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAllocateIp) Cleanup(state multistep.StateBag) {
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-06-12 04:38:54 -04:00
|
|
|
instanceIP := state.Get("access_ip").(*floatingips.FloatingIP)
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2018-10-29 14:36:08 -04:00
|
|
|
// Don't clean up if unless required
|
|
|
|
if instanceIP.ID == "" && instanceIP.FloatingIP == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-04 14:59:49 -05:00
|
|
|
// Don't delete pool addresses we didn't allocate
|
|
|
|
if state.Get("floatingip_istemp") == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:36:08 -04:00
|
|
|
// We need the v2 network client
|
|
|
|
client, err := config.networkV2Client()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
|
|
|
return
|
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2018-10-29 14:36:08 -04:00
|
|
|
if instanceIP.ID != "" {
|
2018-06-12 04:38:54 -04:00
|
|
|
if err := floatingips.Delete(client, instanceIP.ID).ExtractErr(); err != nil {
|
2015-06-12 00:16:43 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2018-06-12 04:38:54 -04:00
|
|
|
"Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
2014-02-27 03:34:24 -05:00
|
|
|
return
|
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2018-06-12 04:38:54 -04:00
|
|
|
ui.Say(fmt.Sprintf("Deleted temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
}
|