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 |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 4 | from xos.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 5 | from observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 6 | from core.models import Controller |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 7 | from core.models.network import * |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 8 | from util.logger import Logger, logging |
| 9 | |
| 10 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 11 | |
| 12 | class SyncNetworkSlivers(OpenStackSyncStep): |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 13 | requested_interval = 0 # 3600 |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 14 | provides=[NetworkSliver] |
Sapan Bhatia | bb45dbe | 2015-02-08 06:31:10 +0000 | [diff] [blame] | 15 | observes=NetworkSliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 16 | |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 17 | # The way it works is to enumerate the all of the ports that quantum |
| 18 | # has, and then work backward from each port's network-id to determine |
| 19 | # which Network is associated from the port. |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 20 | |
| 21 | def call(self, **args): |
Scott Baker | 8ba5001 | 2014-11-05 09:05:14 -0800 | [diff] [blame] | 22 | logger.info("sync'ing network slivers") |
| 23 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 24 | networkSlivers = NetworkSliver.objects.all() |
| 25 | networkSlivers_by_id = {} |
| 26 | networkSlivers_by_port = {} |
| 27 | for networkSliver in networkSlivers: |
| 28 | networkSlivers_by_id[networkSliver.id] = networkSliver |
| 29 | networkSlivers_by_port[networkSliver.port_id] = networkSliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 30 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 31 | networks = Network.objects.all() |
| 32 | networks_by_id = {} |
| 33 | for network in networks: |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 34 | for nd in network.controllernetworks.all(): |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 35 | networks_by_id[nd.net_id] = network |
| 36 | |
| 37 | #logger.info("networks_by_id = ") |
| 38 | #for (network_id, network) in networks_by_id.items(): |
| 39 | # logger.info(" %s: %s" % (network_id, network.name)) |
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 | slivers = Sliver.objects.all() |
Scott Baker | 11a2f50 | 2015-01-30 00:02:31 -0800 | [diff] [blame] | 42 | slivers_by_instance_uuid = {} |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 43 | for sliver in slivers: |
Scott Baker | 11a2f50 | 2015-01-30 00:02:31 -0800 | [diff] [blame] | 44 | slivers_by_instance_uuid[sliver.instance_uuid] = sliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 45 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 46 | # Get all ports in all controllers |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 47 | |
| 48 | ports_by_id = {} |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 49 | for controller in Controller.objects.all(): |
| 50 | if not controller.admin_tenant: |
| 51 | logger.info("controller %s has no admin_tenant" % controller) |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 52 | continue |
| 53 | try: |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 54 | driver = self.driver.admin_driver(controller = controller,tenant='admin') |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 55 | ports = driver.shell.quantum.list_ports()["ports"] |
| 56 | except: |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 57 | logger.log_exc("failed to get ports from controller %s" % controller) |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 58 | continue |
| 59 | |
| 60 | for port in ports: |
| 61 | ports_by_id[port["id"]] = port |
| 62 | |
Scott Baker | e75d441 | 2014-08-27 11:21:08 -0700 | [diff] [blame] | 63 | for port in ports_by_id.values(): |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 64 | #logger.info("port %s" % str(port)) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 65 | if port["id"] in networkSlivers_by_port: |
| 66 | # we already have it |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 67 | #logger.info("already accounted for port %s" % port["id"]) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 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 port["device_owner"] != "compute:nova": |
| 71 | # we only want the ports that connect to instances |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 72 | #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] | 73 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 74 | |
Scott Baker | 11a2f50 | 2015-01-30 00:02:31 -0800 | [diff] [blame] | 75 | sliver = slivers_by_instance_uuid.get(port['device_id'], None) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 76 | if not sliver: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 77 | 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] | 78 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 79 | |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 80 | network = networks_by_id.get(port['network_id'], None) |
| 81 | if not network: |
| 82 | logger.info("no network for port %s network %s" % (port["id"], port["network_id"])) |
| 83 | |
| 84 | # we know it's associated with a sliver, but we don't know |
| 85 | # which network it is part of. |
| 86 | |
| 87 | continue |
| 88 | |
Scott Baker | 81fa17f | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 89 | if network.template.shared_network_name: |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 90 | # If it's a shared network template, then more than one network |
| 91 | # object maps to the quantum network. We have to do a whole bunch |
| 92 | # of extra work to find the right one. |
| 93 | networks = network.template.network_set.all() |
| 94 | network = None |
| 95 | for candidate_network in networks: |
| 96 | if (candidate_network.owner == sliver.slice): |
| 97 | print "found network", candidate_network |
| 98 | network = candidate_network |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 99 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 100 | if not network: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 101 | 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] | 102 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 103 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 104 | if not port["fixed_ips"]: |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 105 | logger.info("port %s has no fixed_ips" % port["id"]) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 106 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 107 | |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 108 | ip=port["fixed_ips"][0]["ip_address"] |
| 109 | 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] | 110 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 111 | ns = NetworkSliver(network=network, |
| 112 | sliver=sliver, |
Scott Baker | 378b322 | 2014-08-12 18:00:35 -0700 | [diff] [blame] | 113 | ip=ip, |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 114 | port_id=port["id"]) |
| 115 | ns.save() |
Sapan Bhatia | 5f4aff2 | 2014-07-23 09:48:55 -0400 | [diff] [blame] | 116 | |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 117 | # Now, handle port forwarding |
| 118 | # We get the list of NetworkSlivers again, since we might have just |
| 119 | # added a few. Then, for each one of them we find it's quantum port and |
| 120 | # make sure quantum's nat:forward_ports argument is the same. |
| 121 | |
| 122 | for networkSliver in NetworkSliver.objects.all(): |
| 123 | try: |
| 124 | nat_list = networkSliver.network.nat_list |
| 125 | except (TypeError, ValueError), e: |
| 126 | logger.info("Failed to decode nat_list: %s" % str(e)) |
| 127 | continue |
| 128 | |
| 129 | if not networkSliver.port_id: |
| 130 | continue |
| 131 | |
| 132 | neutron_port = ports_by_id.get(networkSliver.port_id, None) |
| 133 | if not neutron_port: |
| 134 | continue |
| 135 | |
| 136 | neutron_nat_list = neutron_port.get("nat:forward_ports", None) |
| 137 | if not neutron_nat_list: |
| 138 | # make sure that None and the empty set are treated identically |
| 139 | neutron_nat_list = [] |
| 140 | |
| 141 | if (neutron_nat_list != nat_list): |
| 142 | logger.info("Setting nat:forward_ports for port %s network %s sliver %s to %s" % (str(networkSliver.port_id), str(networkSliver.network.id), str(networkSliver.sliver), str(nat_list))) |
| 143 | try: |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 144 | driver = self.driver.admin_driver(controller=networkSliver.sliver.node.site_deployment.controller,tenant='admin') |
Scott Baker | 5bbaa23 | 2014-08-14 17:23:15 -0700 | [diff] [blame] | 145 | driver.shell.quantum.update_port(networkSliver.port_id, {"port": {"nat:forward_ports": nat_list}}) |
| 146 | except: |
| 147 | logger.log_exc("failed to update port with nat_list %s" % str(nat_list)) |
| 148 | continue |
| 149 | else: |
| 150 | #logger.info("port %s network %s sliver %s nat %s is already set" % (str(networkSliver.port_id), str(networkSliver.network.id), str(networkSliver.sliver), str(nat_list))) |
| 151 | pass |
| 152 | |
Sapan Bhatia | 5f4aff2 | 2014-07-23 09:48:55 -0400 | [diff] [blame] | 153 | def delete_record(self, network_sliver): |
| 154 | # Nothing to do, this is an OpenCloud object |
| 155 | pass |
| 156 | |