2014-02-27 03:34:24 -05:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2014-02-27 03:34:24 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepAllocateIp struct {
|
|
|
|
FloatingIpPool string
|
|
|
|
FloatingIp string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAllocateIp) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-06-12 00:16:43 -04:00
|
|
|
config := state.Get("config").(Config)
|
|
|
|
server := state.Get("server").(*servers.Server)
|
|
|
|
|
|
|
|
// We need the v2 compute client
|
|
|
|
client, err := config.computeV2Client()
|
|
|
|
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
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
var instanceIp floatingips.FloatingIP
|
2015-06-12 10:02:04 -04:00
|
|
|
|
2014-02-27 03:34:24 -05:00
|
|
|
// This is here in case we error out before putting instanceIp into the
|
|
|
|
// statebag below, because it is requested by Cleanup()
|
2015-06-12 10:02:04 -04:00
|
|
|
state.Put("access_ip", &instanceIp)
|
2014-02-27 03:34:24 -05:00
|
|
|
|
|
|
|
if s.FloatingIp != "" {
|
2015-06-12 10:02:04 -04:00
|
|
|
instanceIp.IP = s.FloatingIp
|
2014-02-27 03:34:24 -05:00
|
|
|
} else if s.FloatingIpPool != "" {
|
2015-06-12 15:23:05 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating floating IP..."))
|
|
|
|
ui.Message(fmt.Sprintf("Pool: %s", s.FloatingIpPool))
|
2016-11-27 19:59:26 -05:00
|
|
|
newIp, err := floatingips.Create(client, floatingips.CreateOpts{
|
2015-06-12 00:16:43 -04:00
|
|
|
Pool: s.FloatingIpPool,
|
|
|
|
}).Extract()
|
2014-02-27 03:34:24 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating floating ip from pool '%s'", s.FloatingIpPool)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2015-06-12 10:02:04 -04:00
|
|
|
instanceIp = *newIp
|
2015-06-12 15:23:05 -04:00
|
|
|
ui.Message(fmt.Sprintf("Created floating IP: %s", instanceIp.IP))
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 10:02:04 -04:00
|
|
|
if instanceIp.IP != "" {
|
2015-06-12 15:23:05 -04:00
|
|
|
ui.Say(fmt.Sprintf("Associating floating IP with server..."))
|
|
|
|
ui.Message(fmt.Sprintf("IP: %s", instanceIp.IP))
|
2016-11-27 19:59:26 -05:00
|
|
|
err := floatingips.AssociateInstance(client, server.ID, floatingips.AssociateOpts{
|
|
|
|
FloatingIP: instanceIp.IP,
|
|
|
|
}).ExtractErr()
|
2015-06-12 00:16:43 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf(
|
2015-06-12 15:23:05 -04:00
|
|
|
"Error associating floating IP %s with instance: %s",
|
|
|
|
instanceIp.IP, 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(
|
|
|
|
"Added floating IP %s to instance!", instanceIp.IP))
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 10:02:04 -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) {
|
2015-06-12 00:16:43 -04:00
|
|
|
config := state.Get("config").(Config)
|
2014-02-27 03:34:24 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2016-11-27 19:59:26 -05:00
|
|
|
instanceIp := state.Get("access_ip").(*floatingips.FloatingIP)
|
2015-06-12 00:16:43 -04:00
|
|
|
|
|
|
|
// We need the v2 compute client
|
|
|
|
client, err := config.computeV2Client()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
2015-06-12 10:02:04 -04:00
|
|
|
"Error deleting temporary floating IP %s", instanceIp.IP))
|
2015-06-12 00:16:43 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.FloatingIpPool != "" && instanceIp.ID != "" {
|
2016-11-27 19:59:26 -05:00
|
|
|
if err := floatingips.Delete(client, instanceIp.ID).ExtractErr(); err != nil {
|
2015-06-12 00:16:43 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2015-06-12 10:02:04 -04:00
|
|
|
"Error deleting temporary floating IP %s", instanceIp.IP))
|
2014-02-27 03:34:24 -05:00
|
|
|
return
|
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2015-06-12 10:02:04 -04:00
|
|
|
ui.Say(fmt.Sprintf("Deleted temporary floating IP %s", instanceIp.IP))
|
2014-02-27 03:34:24 -05:00
|
|
|
}
|
|
|
|
}
|