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