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 | 16f0474 | 2013-09-25 08:53:28 -0400 | [diff] [blame] | 12 | def call(self, failed=[]): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 13 | networkSlivers = NetworkSliver.objects.all() |
| 14 | networkSlivers_by_id = {} |
| 15 | networkSlivers_by_port = {} |
| 16 | for networkSliver in networkSlivers: |
| 17 | networkSlivers_by_id[networkSliver.id] = networkSliver |
| 18 | networkSlivers_by_port[networkSliver.port_id] = networkSliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 19 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 20 | networks = Network.objects.all() |
| 21 | networks_by_id = {} |
| 22 | for network in networks: |
| 23 | networks_by_id[network.network_id] = network |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 24 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 25 | slivers = Sliver.objects.all() |
| 26 | slivers_by_instance_id = {} |
| 27 | for sliver in slivers: |
| 28 | slivers_by_instance_id[sliver.instance_id] = sliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 29 | |
Tony Mack | 9976dfd | 2014-06-03 21:04:35 -0400 | [diff] [blame] | 30 | 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] | 31 | ports = driver.shell.quantum.list_ports()["ports"] |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 32 | for port in ports: |
| 33 | if port["id"] in networkSlivers_by_port: |
| 34 | # we already have it |
| 35 | print "already accounted for port", port["id"] |
| 36 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 37 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 38 | if port["device_owner"] != "compute:nova": |
| 39 | # we only want the ports that connect to instances |
| 40 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 41 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 42 | network = networks_by_id.get(port['network_id'], None) |
| 43 | if not network: |
| 44 | #print "no network for port", port["id"], "network", port["network_id"] |
| 45 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 46 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 47 | sliver = slivers_by_instance_id.get(port['device_id'], None) |
| 48 | if not sliver: |
| 49 | print "no sliver for port", port["id"], "device_id", port['device_id'] |
| 50 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 51 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 52 | if network.template.sharedNetworkId is not None: |
| 53 | # If it's a shared network template, then more than one network |
| 54 | # object maps to the quantum network. We have to do a whole bunch |
| 55 | # of extra work to find the right one. |
| 56 | networks = network.template.network_set.all() |
| 57 | network = None |
| 58 | for candidate_network in networks: |
| 59 | if (candidate_network.owner == sliver.slice): |
| 60 | print "found network", candidate_network |
| 61 | network = candidate_network |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 62 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 63 | if not network: |
| 64 | print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"] |
| 65 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 66 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 67 | if not port["fixed_ips"]: |
| 68 | print "port", port["id"], "has no fixed_ips" |
| 69 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 70 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 71 | # print "XXX", port |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 72 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 73 | ns = NetworkSliver(network=network, |
| 74 | sliver=sliver, |
| 75 | ip=port["fixed_ips"][0]["ip_address"], |
| 76 | port_id=port["id"]) |
| 77 | ns.save() |
Sapan Bhatia | 5f4aff2 | 2014-07-23 09:48:55 -0400 | [diff] [blame] | 78 | |
| 79 | def delete_record(self, network_sliver): |
| 80 | # Nothing to do, this is an OpenCloud object |
| 81 | pass |
| 82 | |