blob: b3b11e75f7fa8cb007d6fde04fabc27b143158df [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Scott Baker86e132c2015-02-11 21:38:09 -08004from xos.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
Tony Mack06c8e472014-11-30 15:53:08 -05006from core.models import Controller
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04007from core.models.network import *
Tony Mack4d0d75c2015-03-29 08:32:21 -04008from util.logger import observer_logger as logger
Sapan Bhatia24836f12013-08-27 10:16:05 -04009
Tony Mackd8515472015-08-19 11:58:18 -040010class SyncNetworkInstances(OpenStackSyncStep):
Scott Baker378b3222014-08-12 18:00:35 -070011 requested_interval = 0 # 3600
Tony Mackd8515472015-08-19 11:58:18 -040012 provides=[NetworkInstance]
13 observes=NetworkInstance
Sapan Bhatia24836f12013-08-27 10:16:05 -040014
Scott Baker378b3222014-08-12 18:00:35 -070015 # 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 Baker378b3222014-08-12 18:00:35 -070018
19 def call(self, **args):
Tony Mackd8515472015-08-19 11:58:18 -040020 logger.info("sync'ing network instances")
Scott Baker78c6b982014-11-05 09:05:14 -080021
Tony Mackd8515472015-08-19 11:58:18 -040022 networkInstances = NetworkInstance.objects.all()
23 networkInstances_by_id = {}
24 networkInstances_by_port = {}
25 for networkInstance in networkInstances:
26 networkInstances_by_id[networkInstance.id] = networkInstance
27 networkInstances_by_port[networkInstance.port_id] = networkInstance
Sapan Bhatia24836f12013-08-27 10:16:05 -040028
Tony Mackdacfb982013-09-24 21:57:16 -040029 networks = Network.objects.all()
30 networks_by_id = {}
31 for network in networks:
Tony Mack06c8e472014-11-30 15:53:08 -050032 for nd in network.controllernetworks.all():
Scott Baker378b3222014-08-12 18:00:35 -070033 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 Bhatia24836f12013-08-27 10:16:05 -040038
Tony Mackd8515472015-08-19 11:58:18 -040039 instances = Instance.objects.all()
40 instances_by_instance_uuid = {}
41 for instance in instances:
42 instances_by_instance_uuid[instance.instance_uuid] = instance
Sapan Bhatia24836f12013-08-27 10:16:05 -040043
Tony Mack06c8e472014-11-30 15:53:08 -050044 # Get all ports in all controllers
Scott Baker5bbaa232014-08-14 17:23:15 -070045
46 ports_by_id = {}
Scott Baker1e464ff2015-02-23 18:08:03 -080047 templates_by_id = {}
Tony Mack06c8e472014-11-30 15:53:08 -050048 for controller in Controller.objects.all():
49 if not controller.admin_tenant:
50 logger.info("controller %s has no admin_tenant" % controller)
Scott Baker5bbaa232014-08-14 17:23:15 -070051 continue
52 try:
Tony Mack06c8e472014-11-30 15:53:08 -050053 driver = self.driver.admin_driver(controller = controller,tenant='admin')
Scott Baker5bbaa232014-08-14 17:23:15 -070054 ports = driver.shell.quantum.list_ports()["ports"]
55 except:
Tony Mack06c8e472014-11-30 15:53:08 -050056 logger.log_exc("failed to get ports from controller %s" % controller)
Scott Baker5bbaa232014-08-14 17:23:15 -070057 continue
58
59 for port in ports:
60 ports_by_id[port["id"]] = port
61
Scott Baker1e464ff2015-02-23 18:08:03 -080062 # public-nat and public-dedicated networks don't have a net-id anywhere
S.Çağlar Onurb6e63f02015-02-24 17:28:09 -050063 # 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"]:
Scott Baker1e464ff2015-02-23 18:08:03 -080073 templates_by_id[network["id"]] = template
74
Scott Bakere75d4412014-08-27 11:21:08 -070075 for port in ports_by_id.values():
Scott Baker378b3222014-08-12 18:00:35 -070076 #logger.info("port %s" % str(port))
Tony Mackd8515472015-08-19 11:58:18 -040077 if port["id"] in networkInstances_by_port:
Tony Mackdacfb982013-09-24 21:57:16 -040078 # we already have it
Scott Baker378b3222014-08-12 18:00:35 -070079 #logger.info("already accounted for port %s" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -040080 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040081
Tony Mackdacfb982013-09-24 21:57:16 -040082 if port["device_owner"] != "compute:nova":
83 # we only want the ports that connect to instances
Scott Baker378b3222014-08-12 18:00:35 -070084 #logger.info("port %s is not a compute port, it is a %s" % (port["id"], port["device_owner"]))
Tony Mackdacfb982013-09-24 21:57:16 -040085 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040086
Tony Mackd8515472015-08-19 11:58:18 -040087 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']))
Tony Mackdacfb982013-09-24 21:57:16 -040090 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040091
Scott Baker378b3222014-08-12 18:00:35 -070092 network = networks_by_id.get(port['network_id'], None)
93 if not network:
S.Çağlar Onurb6e63f02015-02-24 17:28:09 -050094 # maybe it's public-nat or public-dedicated. Search the templates for
Tony Mackd8515472015-08-19 11:58:18 -040095 # the id, then see if the instance's slice has some network that uses
S.Çağlar Onurb6e63f02015-02-24 17:28:09 -050096 # that template
97 template = templates_by_id.get(port['network_id'], None)
Tony Mackd8515472015-08-19 11:58:18 -040098 if template and instance.slice:
99 for candidate_network in instance.slice.networks.all():
S.Çağlar Onurb6e63f02015-02-24 17:28:09 -0500100 if candidate_network.template == template:
Scott Baker1e464ff2015-02-23 18:08:03 -0800101 network=candidate_network
102 if not network:
Scott Baker378b3222014-08-12 18:00:35 -0700103 logger.info("no network for port %s network %s" % (port["id"], port["network_id"]))
104
Tony Mackd8515472015-08-19 11:58:18 -0400105 # we know it's associated with a instance, but we don't know
Scott Baker378b3222014-08-12 18:00:35 -0700106 # which network it is part of.
107
108 continue
109
Scott Baker369f9b92015-01-03 12:03:38 -0800110 if network.template.shared_network_name:
Tony Mackdacfb982013-09-24 21:57:16 -0400111 # 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:
Tony Mackd8515472015-08-19 11:58:18 -0400117 if (candidate_network.owner == instance.slice):
Tony Mackdacfb982013-09-24 21:57:16 -0400118 print "found network", candidate_network
119 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -0400120
Tony Mackdacfb982013-09-24 21:57:16 -0400121 if not network:
Scott Baker378b3222014-08-12 18:00:35 -0700122 logger.info("failed to find the correct network for a shared template for port %s network %s" % (port["id"], port["network_id"]))
Tony Mackdacfb982013-09-24 21:57:16 -0400123 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -0400124
Tony Mackdacfb982013-09-24 21:57:16 -0400125 if not port["fixed_ips"]:
Scott Baker378b3222014-08-12 18:00:35 -0700126 logger.info("port %s has no fixed_ips" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -0400127 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -0400128
Scott Baker378b3222014-08-12 18:00:35 -0700129 ip=port["fixed_ips"][0]["ip_address"]
Tony Mackd8515472015-08-19 11:58:18 -0400130 logger.info("creating NetworkInstance (%s, %s, %s, %s)" % (str(network), str(instance), ip, str(port["id"])))
Sapan Bhatia24836f12013-08-27 10:16:05 -0400131
Tony Mackd8515472015-08-19 11:58:18 -0400132 ns = NetworkInstance(network=network,
133 instance=instance,
Scott Baker378b3222014-08-12 18:00:35 -0700134 ip=ip,
Tony Mackdacfb982013-09-24 21:57:16 -0400135 port_id=port["id"])
Scott Baker772fa3c2015-04-13 17:24:18 -0700136
137 try:
138 ns.save()
139 except:
Tony Mackd8515472015-08-19 11:58:18 -0400140 logger.log_exc("failed to save networkinstance %s" % str(ns))
Scott Baker772fa3c2015-04-13 17:24:18 -0700141 continue
Sapan Bhatia5f4aff22014-07-23 09:48:55 -0400142
Tony Mackd8515472015-08-19 11:58:18 -0400143 # For networkInstances that were created by the user, find that ones
Scott Baker6bff47f2015-08-18 23:03:02 -0700144 # that don't have neutron ports, and create them.
Tony Mackd8515472015-08-19 11:58:18 -0400145 for networkInstance in NetworkInstance.objects.filter(port_id__isnull=True, instance__isnull=False):
146 #logger.info("working on networkinstance %s" % networkInstance)
147 controller = instance.node.site_deployment.controller
Scott Baker6bff47f2015-08-18 23:03:02 -0700148 if controller:
Tony Mackd8515472015-08-19 11:58:18 -0400149 cn=networkInstance.network.controllernetworks.filter(controller=controller)
Scott Baker6bff47f2015-08-18 23:03:02 -0700150 if not cn:
Tony Mackd8515472015-08-19 11:58:18 -0400151 logger.log_exc("no controllernetwork for %s" % networkInstance)
Scott Baker6bff47f2015-08-18 23:03:02 -0700152 continue
153 cn=cn[0]
154 try:
155 driver = self.driver.admin_driver(controller = controller,tenant='admin')
Scott Baker4322d1b2015-08-18 23:12:33 -0700156 port = driver.shell.quantum.create_port({"port": {"network_id": cn.net_id}})["port"]
Tony Mackd8515472015-08-19 11:58:18 -0400157 networkInstance.port_id = port["id"]
Scott Baker4322d1b2015-08-18 23:12:33 -0700158 if port["fixed_ips"]:
Tony Mackd8515472015-08-19 11:58:18 -0400159 networkInstance.ip = port["fixed_ips"][0]["ip_address"]
Scott Baker6bff47f2015-08-18 23:03:02 -0700160 except:
Tony Mackd8515472015-08-19 11:58:18 -0400161 logger.log_exc("failed to create neutron port for %s" % networkInstance)
Scott Baker6bff47f2015-08-18 23:03:02 -0700162 continue
Tony Mackd8515472015-08-19 11:58:18 -0400163 networkInstance.save()
Scott Baker6bff47f2015-08-18 23:03:02 -0700164
Scott Baker5bbaa232014-08-14 17:23:15 -0700165 # Now, handle port forwarding
Tony Mackd8515472015-08-19 11:58:18 -0400166 # We get the list of NetworkInstances again, since we might have just
Scott Baker5bbaa232014-08-14 17:23:15 -0700167 # added a few. Then, for each one of them we find it's quantum port and
168 # make sure quantum's nat:forward_ports argument is the same.
169
Tony Mackd8515472015-08-19 11:58:18 -0400170 for networkInstance in NetworkInstance.objects.all():
Scott Baker5bbaa232014-08-14 17:23:15 -0700171 try:
Tony Mackd8515472015-08-19 11:58:18 -0400172 nat_list = networkInstance.network.nat_list
Scott Baker5bbaa232014-08-14 17:23:15 -0700173 except (TypeError, ValueError), e:
174 logger.info("Failed to decode nat_list: %s" % str(e))
175 continue
176
Tony Mackd8515472015-08-19 11:58:18 -0400177 if not networkInstance.port_id:
Scott Baker5bbaa232014-08-14 17:23:15 -0700178 continue
179
Tony Mackd8515472015-08-19 11:58:18 -0400180 neutron_port = ports_by_id.get(networkInstance.port_id, None)
Scott Baker5bbaa232014-08-14 17:23:15 -0700181 if not neutron_port:
182 continue
183
184 neutron_nat_list = neutron_port.get("nat:forward_ports", None)
185 if not neutron_nat_list:
186 # make sure that None and the empty set are treated identically
187 neutron_nat_list = []
188
189 if (neutron_nat_list != nat_list):
Tony Mackd8515472015-08-19 11:58:18 -0400190 logger.info("Setting nat:forward_ports for port %s network %s instance %s to %s" % (str(networkInstance.port_id), str(networkInstance.network.id), str(networkInstance.instance), str(nat_list)))
Scott Baker5bbaa232014-08-14 17:23:15 -0700191 try:
Tony Mackd8515472015-08-19 11:58:18 -0400192 driver = self.driver.admin_driver(controller=networkInstance.instance.node.site_deployment.controller,tenant='admin')
193 driver.shell.quantum.update_port(networkInstance.port_id, {"port": {"nat:forward_ports": nat_list}})
Scott Baker5bbaa232014-08-14 17:23:15 -0700194 except:
195 logger.log_exc("failed to update port with nat_list %s" % str(nat_list))
196 continue
197 else:
Tony Mackd8515472015-08-19 11:58:18 -0400198 #logger.info("port %s network %s instance %s nat %s is already set" % (str(networkInstance.port_id), str(networkInstance.network.id), str(networkInstance.instance), str(nat_list)))
Scott Baker5bbaa232014-08-14 17:23:15 -0700199 pass
200
Tony Mackd8515472015-08-19 11:58:18 -0400201 def delete_record(self, network_instance):
Sapan Bhatia5f4aff22014-07-23 09:48:55 -0400202 # Nothing to do, this is an OpenCloud object
203 pass
204