github.com/rothwerx/packer@v0.9.0/builder/openstack/step_allocate_ip.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 6 "github.com/mitchellh/multistep" 7 "github.com/mitchellh/packer/packer" 8 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip" 9 "github.com/rackspace/gophercloud/openstack/compute/v2/servers" 10 ) 11 12 type StepAllocateIp struct { 13 FloatingIpPool string 14 FloatingIp string 15 } 16 17 func (s *StepAllocateIp) Run(state multistep.StateBag) multistep.StepAction { 18 ui := state.Get("ui").(packer.Ui) 19 config := state.Get("config").(Config) 20 server := state.Get("server").(*servers.Server) 21 22 // We need the v2 compute client 23 client, err := config.computeV2Client() 24 if err != nil { 25 err = fmt.Errorf("Error initializing compute client: %s", err) 26 state.Put("error", err) 27 return multistep.ActionHalt 28 } 29 30 var instanceIp floatingip.FloatingIP 31 32 // This is here in case we error out before putting instanceIp into the 33 // statebag below, because it is requested by Cleanup() 34 state.Put("access_ip", &instanceIp) 35 36 if s.FloatingIp != "" { 37 instanceIp.IP = s.FloatingIp 38 } else if s.FloatingIpPool != "" { 39 ui.Say(fmt.Sprintf("Creating floating IP...")) 40 ui.Message(fmt.Sprintf("Pool: %s", s.FloatingIpPool)) 41 newIp, err := floatingip.Create(client, floatingip.CreateOpts{ 42 Pool: s.FloatingIpPool, 43 }).Extract() 44 if err != nil { 45 err := fmt.Errorf("Error creating floating ip from pool '%s'", s.FloatingIpPool) 46 state.Put("error", err) 47 ui.Error(err.Error()) 48 return multistep.ActionHalt 49 } 50 51 instanceIp = *newIp 52 ui.Message(fmt.Sprintf("Created floating IP: %s", instanceIp.IP)) 53 } 54 55 if instanceIp.IP != "" { 56 ui.Say(fmt.Sprintf("Associating floating IP with server...")) 57 ui.Message(fmt.Sprintf("IP: %s", instanceIp.IP)) 58 err := floatingip.Associate(client, server.ID, instanceIp.IP).ExtractErr() 59 if err != nil { 60 err := fmt.Errorf( 61 "Error associating floating IP %s with instance: %s", 62 instanceIp.IP, err) 63 state.Put("error", err) 64 ui.Error(err.Error()) 65 return multistep.ActionHalt 66 } 67 68 ui.Message(fmt.Sprintf( 69 "Added floating IP %s to instance!", instanceIp.IP)) 70 } 71 72 state.Put("access_ip", &instanceIp) 73 return multistep.ActionContinue 74 } 75 76 func (s *StepAllocateIp) Cleanup(state multistep.StateBag) { 77 config := state.Get("config").(Config) 78 ui := state.Get("ui").(packer.Ui) 79 instanceIp := state.Get("access_ip").(*floatingip.FloatingIP) 80 81 // We need the v2 compute client 82 client, err := config.computeV2Client() 83 if err != nil { 84 ui.Error(fmt.Sprintf( 85 "Error deleting temporary floating IP %s", instanceIp.IP)) 86 return 87 } 88 89 if s.FloatingIpPool != "" && instanceIp.ID != "" { 90 if err := floatingip.Delete(client, instanceIp.ID).ExtractErr(); err != nil { 91 ui.Error(fmt.Sprintf( 92 "Error deleting temporary floating IP %s", instanceIp.IP)) 93 return 94 } 95 96 ui.Say(fmt.Sprintf("Deleted temporary floating IP %s", instanceIp.IP)) 97 } 98 }