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