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