github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/openstack/step_allocate_ip.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 6 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips" 7 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" 8 "github.com/mitchellh/multistep" 9 "github.com/mitchellh/packer/packer" 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 floatingips.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 := floatingips.Create(client, floatingips.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 := floatingips.AssociateInstance(client, server.ID, floatingips.AssociateOpts{ 59 FloatingIP: instanceIp.IP, 60 }).ExtractErr() 61 if err != nil { 62 err := fmt.Errorf( 63 "Error associating floating IP %s with instance: %s", 64 instanceIp.IP, err) 65 state.Put("error", err) 66 ui.Error(err.Error()) 67 return multistep.ActionHalt 68 } 69 70 ui.Message(fmt.Sprintf( 71 "Added floating IP %s to instance!", instanceIp.IP)) 72 } 73 74 state.Put("access_ip", &instanceIp) 75 return multistep.ActionContinue 76 } 77 78 func (s *StepAllocateIp) Cleanup(state multistep.StateBag) { 79 config := state.Get("config").(Config) 80 ui := state.Get("ui").(packer.Ui) 81 instanceIp := state.Get("access_ip").(*floatingips.FloatingIP) 82 83 // We need the v2 compute client 84 client, err := config.computeV2Client() 85 if err != nil { 86 ui.Error(fmt.Sprintf( 87 "Error deleting temporary floating IP %s", instanceIp.IP)) 88 return 89 } 90 91 if s.FloatingIpPool != "" && instanceIp.ID != "" { 92 if err := floatingips.Delete(client, instanceIp.ID).ExtractErr(); err != nil { 93 ui.Error(fmt.Sprintf( 94 "Error deleting temporary floating IP %s", instanceIp.IP)) 95 return 96 } 97 98 ui.Say(fmt.Sprintf("Deleted temporary floating IP %s", instanceIp.IP)) 99 } 100 }