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 * |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 7 | from util.logger import Logger, logging |
| 8 | |
| 9 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 10 | |
| 11 | class SyncNetworkSlivers(OpenStackSyncStep): |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 12 | requested_interval = 0 # 3600 |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 13 | provides=[NetworkSliver] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 14 | |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 15 | # XXX smbaker: Note that this sync_step only functions for private networks. |
| 16 | # The way it works is to enumerate the all of the ports that quantum |
| 17 | # has, and then work backward from each port's network-id to determine |
| 18 | # which Network is associated from the port. |
| 19 | # |
| 20 | # There's a bug somewhere in NetworkDeployment where NAT and Dedicated |
| 21 | # networks are not getting assigned the correct network IDs. This means |
| 22 | # we can't reverse map them. |
| 23 | |
| 24 | def call(self, **args): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 25 | networkSlivers = NetworkSliver.objects.all() |
| 26 | networkSlivers_by_id = {} |
| 27 | networkSlivers_by_port = {} |
| 28 | for networkSliver in networkSlivers: |
| 29 | networkSlivers_by_id[networkSliver.id] = networkSliver |
| 30 | networkSlivers_by_port[networkSliver.port_id] = networkSliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 31 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 32 | networks = Network.objects.all() |
| 33 | networks_by_id = {} |
| 34 | for network in networks: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 35 | for nd in network.networkdeployments_set.all(): |
| 36 | networks_by_id[nd.net_id] = network |
| 37 | |
| 38 | #logger.info("networks_by_id = ") |
| 39 | #for (network_id, network) in networks_by_id.items(): |
| 40 | # logger.info(" %s: %s" % (network_id, network.name)) |
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 | slivers = Sliver.objects.all() |
| 43 | slivers_by_instance_id = {} |
| 44 | for sliver in slivers: |
| 45 | slivers_by_instance_id[sliver.instance_id] = sliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 46 | |
Tony Mack | 9976dfd | 2014-06-03 21:04:35 -0400 | [diff] [blame] | 47 | 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] | 48 | ports = driver.shell.quantum.list_ports()["ports"] |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 49 | for port in ports: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 50 | #logger.info("port %s" % str(port)) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 51 | if port["id"] in networkSlivers_by_port: |
| 52 | # we already have it |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 53 | #logger.info("already accounted for port %s" % port["id"]) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 54 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 55 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 56 | if port["device_owner"] != "compute:nova": |
| 57 | # we only want the ports that connect to instances |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 58 | #logger.info("port %s is not a compute port, it is a %s" % (port["id"], port["device_owner"])) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 59 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 60 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 61 | sliver = slivers_by_instance_id.get(port['device_id'], None) |
| 62 | if not sliver: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 63 | logger.info("no sliver for port %s device_id %s" % (port["id"], port['device_id'])) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 64 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 65 | |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 66 | network = networks_by_id.get(port['network_id'], None) |
| 67 | if not network: |
| 68 | logger.info("no network for port %s network %s" % (port["id"], port["network_id"])) |
| 69 | |
| 70 | # we know it's associated with a sliver, but we don't know |
| 71 | # which network it is part of. |
| 72 | |
| 73 | continue |
| 74 | |
| 75 | if network.template.sharedNetworkName: |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 76 | # If it's a shared network template, then more than one network |
| 77 | # object maps to the quantum network. We have to do a whole bunch |
| 78 | # of extra work to find the right one. |
| 79 | networks = network.template.network_set.all() |
| 80 | network = None |
| 81 | for candidate_network in networks: |
| 82 | if (candidate_network.owner == sliver.slice): |
| 83 | print "found network", candidate_network |
| 84 | network = candidate_network |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 85 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 86 | if not network: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 87 | logger.info("failed to find the correct network for a shared template for port %s network %s" % (port["id"], port["network_id"])) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 88 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 89 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 90 | if not port["fixed_ips"]: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 91 | logger.info("port %s has no fixed_ips" % port["id"]) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 92 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 93 | |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 94 | ip=port["fixed_ips"][0]["ip_address"] |
| 95 | logger.info("creating NetworkSliver (%s, %s, %s, %s)" % (str(network), str(sliver), ip, str(port["id"]))) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 96 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 97 | ns = NetworkSliver(network=network, |
| 98 | sliver=sliver, |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 99 | ip=ip, |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 100 | port_id=port["id"]) |
| 101 | ns.save() |
Sapan Bhatia | 5f4aff2 | 2014-07-23 09:48:55 -0400 | [diff] [blame] | 102 | |
| 103 | def delete_record(self, network_sliver): |
| 104 | # Nothing to do, this is an OpenCloud object |
| 105 | pass |
| 106 | |