Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from planetstack.config import Config |
| 4 | |
| 5 | class SyncNetworkSlivers(OpenStackSyncStep): |
Sapan Bhatia | 2ef36c8 | 2013-09-02 14:30:37 -0400 | [diff] [blame] | 6 | requested_interval = 3600 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 7 | provides=[NetworkSliver] |
| 8 | |
| 9 | def call(self): |
| 10 | networkSlivers = NetworkSliver.objects.all() |
| 11 | networkSlivers_by_id = {} |
| 12 | networkSlivers_by_port = {} |
| 13 | for networkSliver in networkSlivers: |
| 14 | networkSlivers_by_id[networkSliver.id] = networkSliver |
| 15 | networkSlivers_by_port[networkSliver.port_id] = networkSliver |
| 16 | |
| 17 | networks = Network.objects.all() |
| 18 | networks_by_id = {} |
| 19 | for network in networks: |
| 20 | networks_by_id[network.network_id] = network |
| 21 | |
| 22 | slivers = Sliver.objects.all() |
| 23 | slivers_by_instance_id = {} |
| 24 | for sliver in slivers: |
| 25 | slivers_by_instance_id[sliver.instance_id] = sliver |
| 26 | |
| 27 | ports = self.manager.driver.shell.quantum.list_ports()["ports"] |
| 28 | for port in ports: |
| 29 | if port["id"] in networkSlivers_by_port: |
| 30 | # we already have it |
| 31 | print "already accounted for port", port["id"] |
| 32 | continue |
| 33 | |
| 34 | if port["device_owner"] != "compute:nova": |
| 35 | # we only want the ports that connect to instances |
| 36 | continue |
| 37 | |
| 38 | network = networks_by_id.get(port['network_id'], None) |
| 39 | if not network: |
| 40 | #print "no network for port", port["id"], "network", port["network_id"] |
| 41 | continue |
| 42 | |
| 43 | sliver = slivers_by_instance_id.get(port['device_id'], None) |
| 44 | if not sliver: |
| 45 | print "no sliver for port", port["id"], "device_id", port['device_id'] |
| 46 | continue |
| 47 | |
| 48 | if network.template.sharedNetworkId is not None: |
| 49 | # If it's a shared network template, then more than one network |
| 50 | # object maps to the quantum network. We have to do a whole bunch |
| 51 | # of extra work to find the right one. |
| 52 | networks = network.template.network_set.all() |
| 53 | network = None |
| 54 | for candidate_network in networks: |
| 55 | if (candidate_network.owner == sliver.slice): |
| 56 | print "found network", candidate_network |
| 57 | network = candidate_network |
| 58 | |
| 59 | if not network: |
| 60 | print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"] |
| 61 | continue |
| 62 | |
| 63 | if not port["fixed_ips"]: |
| 64 | print "port", port["id"], "has no fixed_ips" |
| 65 | continue |
| 66 | |
| 67 | # print "XXX", port |
| 68 | |
| 69 | ns = NetworkSliver(network=network, |
| 70 | sliver=sliver, |
| 71 | ip=port["fixed_ips"][0]["ip_address"], |
| 72 | port_id=port["id"]) |
| 73 | ns.save() |