| Sapan Bhatia | fe16ae4 | 2016-01-14 11:44:43 -0500 | [diff] [blame^] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
| 4 | from xos.config import Config |
| 5 | from synchronizers.base.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models import Controller |
| 7 | from core.models.network import * |
| 8 | from util.logger import observer_logger as logger |
| 9 | |
| 10 | class SyncPorts(OpenStackSyncStep): |
| 11 | requested_interval = 0 # 3600 |
| 12 | provides=[Port] |
| 13 | observes=Port |
| 14 | |
| 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. |
| 18 | |
| 19 | def call(self, **args): |
| 20 | logger.info("sync'ing network instances") |
| 21 | |
| 22 | ports = Port.objects.all() |
| 23 | ports_by_id = {} |
| 24 | ports_by_neutron_port = {} |
| 25 | for port in ports: |
| 26 | ports_by_id[port.id] = port |
| 27 | ports_by_neutron_port[port.port_id] = port |
| 28 | |
| 29 | networks = Network.objects.all() |
| 30 | networks_by_id = {} |
| 31 | for network in networks: |
| 32 | for nd in network.controllernetworks.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)) |
| 38 | |
| 39 | instances = Instance.objects.all() |
| 40 | instances_by_instance_uuid = {} |
| 41 | for instance in instances: |
| 42 | instances_by_instance_uuid[instance.instance_uuid] = instance |
| 43 | |
| 44 | # Get all ports in all controllers |
| 45 | |
| 46 | ports_by_id = {} |
| 47 | templates_by_id = {} |
| 48 | for controller in Controller.objects.all(): |
| 49 | if not controller.admin_tenant: |
| 50 | logger.info("controller %s has no admin_tenant" % controller) |
| 51 | continue |
| 52 | try: |
| 53 | driver = self.driver.admin_driver(controller = controller) |
| 54 | ports = driver.shell.quantum.list_ports()["ports"] |
| 55 | except: |
| 56 | logger.log_exc("failed to get ports from controller %s" % controller) |
| 57 | continue |
| 58 | |
| 59 | for port in ports: |
| 60 | ports_by_id[port["id"]] = port |
| 61 | |
| 62 | # public-nat and public-dedicated networks don't have a net-id anywhere |
| 63 | # in the data model, so build up a list of which ids map to which network |
| 64 | # templates. |
| 65 | try: |
| 66 | neutron_networks = driver.shell.quantum.list_networks()["networks"] |
| 67 | except: |
| 68 | print "failed to get networks from controller %s" % controller |
| 69 | continue |
| 70 | for network in neutron_networks: |
| 71 | for template in NetworkTemplate.objects.all(): |
| 72 | if template.shared_network_name == network["name"]: |
| 73 | templates_by_id[network["id"]] = template |
| 74 | |
| 75 | for port in ports_by_id.values(): |
| 76 | #logger.info("port %s" % str(port)) |
| 77 | if port["id"] in ports_by_neutron_port: |
| 78 | # we already have it |
| 79 | #logger.info("already accounted for port %s" % port["id"]) |
| 80 | continue |
| 81 | |
| 82 | if port["device_owner"] != "compute:nova": |
| 83 | # we only want the ports that connect to instances |
| 84 | #logger.info("port %s is not a compute port, it is a %s" % (port["id"], port["device_owner"])) |
| 85 | continue |
| 86 | |
| 87 | instance = instances_by_instance_uuid.get(port['device_id'], None) |
| 88 | if not instance: |
| 89 | logger.info("no instance for port %s device_id %s" % (port["id"], port['device_id'])) |
| 90 | continue |
| 91 | |
| 92 | network = networks_by_id.get(port['network_id'], None) |
| 93 | if not network: |
| 94 | # maybe it's public-nat or public-dedicated. Search the templates for |
| 95 | # the id, then see if the instance's slice has some network that uses |
| 96 | # that template |
| 97 | template = templates_by_id.get(port['network_id'], None) |
| 98 | if template and instance.slice: |
| 99 | for candidate_network in instance.slice.networks.all(): |
| 100 | if candidate_network.template == template: |
| 101 | network=candidate_network |
| 102 | if not network: |
| 103 | logger.info("no network for port %s network %s" % (port["id"], port["network_id"])) |
| 104 | |
| 105 | # we know it's associated with a instance, but we don't know |
| 106 | # which network it is part of. |
| 107 | |
| 108 | continue |
| 109 | |
| 110 | if network.template.shared_network_name: |
| 111 | # If it's a shared network template, then more than one network |
| 112 | # object maps to the quantum network. We have to do a whole bunch |
| 113 | # of extra work to find the right one. |
| 114 | networks = network.template.network_set.all() |
| 115 | network = None |
| 116 | for candidate_network in networks: |
| 117 | if (candidate_network.owner == instance.slice): |
| 118 | logger.info("found network %s" % candidate_network) |
| 119 | network = candidate_network |
| 120 | |
| 121 | if not network: |
| 122 | logger.info("failed to find the correct network for a shared template for port %s network %s" % (port["id"], port["network_id"])) |
| 123 | continue |
| 124 | |
| 125 | if not port["fixed_ips"]: |
| 126 | logger.info("port %s has no fixed_ips" % port["id"]) |
| 127 | continue |
| 128 | |
| 129 | ip=port["fixed_ips"][0]["ip_address"] |
| 130 | mac=port["mac_address"] |
| 131 | logger.info("creating Port (%s, %s, %s, %s)" % (str(network), str(instance), ip, str(port["id"]))) |
| 132 | |
| 133 | ns = Port(network=network, |
| 134 | instance=instance, |
| 135 | ip=ip, |
| 136 | mac=mac, |
| 137 | port_id=port["id"]) |
| 138 | |
| 139 | try: |
| 140 | ns.save() |
| 141 | except: |
| 142 | logger.log_exc("failed to save port %s" % str(ns)) |
| 143 | continue |
| 144 | |
| 145 | # For ports that were created by the user, find that ones |
| 146 | # that don't have neutron ports, and create them. |
| 147 | for port in Port.objects.filter(Q(port_id__isnull=True), Q(instance__isnull=False) ): |
| 148 | logger.info("XXX working on port %s" % port) |
| 149 | controller = port.instance.node.site_deployment.controller |
| 150 | slice = port.instance.slice |
| 151 | |
| 152 | if controller: |
| 153 | cn=port.network.controllernetworks.filter(controller=controller) |
| 154 | if not cn: |
| 155 | logger.log_exc("no controllernetwork for %s" % port) |
| 156 | continue |
| 157 | cn=cn[0] |
| 158 | if cn.lazy_blocked: |
| 159 | cn.lazy_blocked=False |
| 160 | cn.save() |
| 161 | logger.info("deferring port %s because controllerNetwork was lazy-blocked" % port) |
| 162 | continue |
| 163 | if not cn.net_id: |
| 164 | logger.info("deferring port %s because controllerNetwork does not have a port-id yet" % port) |
| 165 | continue |
| 166 | try: |
| 167 | # We need to use a client driver that specifies the tenant |
| 168 | # of the destination instance. Nova-compute will not connect |
| 169 | # ports to instances if the port's tenant does not match |
| 170 | # the instance's tenant. |
| 171 | |
| 172 | # A bunch of stuff to compensate for OpenStackDriver.client_driveR() |
| 173 | # not being in working condition. |
| 174 | from openstack.client import OpenStackClient |
| 175 | from openstack.driver import OpenStackDriver |
| 176 | caller = port.network.owner.creator |
| 177 | auth = {'username': caller.email, |
| 178 | 'password': caller.remote_password, |
| 179 | 'tenant': slice.name} |
| 180 | client = OpenStackClient(controller=controller, **auth) # cacert=self.config.nova_ca_ssl_cert, |
| 181 | driver = OpenStackDriver(client=client) |
| 182 | |
| 183 | neutron_port = driver.shell.quantum.create_port({"port": {"network_id": cn.net_id}})["port"] |
| 184 | port.port_id = neutron_port["id"] |
| 185 | if neutron_port["fixed_ips"]: |
| 186 | port.ip = neutron_port["fixed_ips"][0]["ip_address"] |
| 187 | port.mac = neutron_port["mac_address"] |
| 188 | except: |
| 189 | logger.log_exc("failed to create neutron port for %s" % port) |
| 190 | continue |
| 191 | port.save() |
| 192 | |
| 193 | def delete_record(self, network_instance): |
| 194 | # Nothing to do, this is an OpenCloud object |
| 195 | pass |
| 196 | |