Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 3 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.network import * |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 7 | |
| 8 | class SyncNetworkSlivers(OpenStackSyncStep): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 9 | requested_interval = 3600 |
| 10 | provides=[NetworkSliver] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 11 | |
Tony Mack | 31078dd | 2013-10-02 12:46:21 -0400 | [diff] [blame] | 12 | def fetch_pending(self): |
| 13 | return NetworkSliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 14 | |
Tony Mack | 16f0474 | 2013-09-25 08:53:28 -0400 | [diff] [blame] | 15 | def call(self, failed=[]): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 22 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 23 | networks = Network.objects.all() |
| 24 | networks_by_id = {} |
| 25 | for network in networks: |
| 26 | networks_by_id[network.network_id] = network |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 27 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 28 | slivers = Sliver.objects.all() |
| 29 | slivers_by_instance_id = {} |
| 30 | for sliver in slivers: |
| 31 | slivers_by_instance_id[sliver.instance_id] = sliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 32 | |
Tony Mack | 9976dfd | 2014-06-03 21:04:35 -0400 | [diff] [blame] | 33 | driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.node.deployment.name) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 34 | ports = driver.shell.quantum.list_ports()["ports"] |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 40 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 41 | if port["device_owner"] != "compute:nova": |
| 42 | # we only want the ports that connect to instances |
| 43 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 44 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 49 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 54 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 65 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 69 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 70 | if not port["fixed_ips"]: |
| 71 | print "port", port["id"], "has no fixed_ips" |
| 72 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 73 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 74 | # print "XXX", port |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 75 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 76 | ns = NetworkSliver(network=network, |
| 77 | sliver=sliver, |
| 78 | ip=port["fixed_ips"][0]["ip_address"], |
| 79 | port_id=port["id"]) |
| 80 | ns.save() |