blob: c1647e8e37e61c8a8f9d9338435832543232816f [file] [log] [blame]
Tony Mack89f70f12013-05-10 20:20:03 -04001import os
Siobhan Tullybf1153a2013-05-27 20:53:48 -04002#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
Tony Mackc261bd22013-05-10 21:04:42 -04003import string
4import random
Tony Mack869866b2013-06-14 18:16:10 -04005import hashlib
Tony Mack79a49c82013-06-15 23:51:57 -04006from datetime import datetime
Tony Mack89f70f12013-05-10 20:20:03 -04007
Tony Mack585160e2013-05-07 11:15:59 -04008from netaddr import IPAddress, IPNetwork
Siobhan Tully30fd4292013-05-10 08:59:56 -04009from planetstack import settings
Tony Mack735493a2013-04-04 23:54:28 -040010from django.core import management
Tony Mackb0d97422013-06-10 09:57:45 -040011from core.models import *
Siobhan Tully73291342013-05-10 10:50:08 -040012from planetstack.config import Config
Tony Mackd685bfa2013-05-02 10:09:51 -040013try:
Siobhan Tully30fd4292013-05-10 08:59:56 -040014 from openstack.client import OpenStackClient
15 from openstack.driver import OpenStackDriver
Tony Mackd685bfa2013-05-02 10:09:51 -040016 has_openstack = True
17except:
Siobhan Tully30fd4292013-05-10 08:59:56 -040018 has_openstack = False
Tony Mack951dab42013-05-02 19:51:45 -040019
Tony Mack89f70f12013-05-10 20:20:03 -040020manager_enabled = Config().api_nova_enabled
Tony Mack02755d42013-05-02 00:00:10 -040021
Tony Mackc261bd22013-05-10 21:04:42 -040022
23def random_string(size=6):
24 return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(size))
25
Tony Mack02755d42013-05-02 00:00:10 -040026def require_enabled(callable):
Tony Mack02755d42013-05-02 00:00:10 -040027 def wrapper(*args, **kwds):
Tony Mack951dab42013-05-02 19:51:45 -040028 if manager_enabled and has_openstack:
Tony Mack02755d42013-05-02 00:00:10 -040029 return callable(*args, **kwds)
30 else:
31 return None
32 return wrapper
Tony Mack735493a2013-04-04 23:54:28 -040033
34
Tony Mack02755d42013-05-02 00:00:10 -040035class OpenStackManager:
Tony Mack735493a2013-04-04 23:54:28 -040036
Tony Mack02755d42013-05-02 00:00:10 -040037 def __init__(self, auth={}, caller=None):
Tony Mack620f0f32013-05-03 14:18:31 -040038 self.client = None
39 self.driver = None
40 self.caller = None
Tony Mack951dab42013-05-02 19:51:45 -040041 self.has_openstack = has_openstack
Tony Mack620f0f32013-05-03 14:18:31 -040042 self.enabled = manager_enabled
43
44 if has_openstack and manager_enabled:
45 if auth:
Tony Mack41945e02013-05-09 19:25:10 -040046 try:
47 self.init_user(auth, caller)
48 except:
49 # if this fails then it meanse the caller doesn't have a
50 # role at the slice's tenant. if the caller is an admin
51 # just use the admin client/manager.
52 if caller and caller.is_admin:
53 self.init_admin()
54 else: raise
Tony Mack620f0f32013-05-03 14:18:31 -040055 else:
Tony Mack41945e02013-05-09 19:25:10 -040056 self.init_admin()
57
58 @require_enabled
Tony Mackc59fcaf2013-06-10 11:14:04 -040059 def init_caller(self, caller, tenant):
60 auth = {'username': caller.email,
Tony Mack869866b2013-06-14 18:16:10 -040061 'password': hashlib.md5(caller.password).hexdigest()[:6],
Tony Mackc59fcaf2013-06-10 11:14:04 -040062 'tenant': tenant}
Tony Mack41945e02013-05-09 19:25:10 -040063 self.client = OpenStackClient(**auth)
64 self.driver = OpenStackDriver(client=self.client)
65 self.caller = caller
66
67 @require_enabled
Tony Mackc59fcaf2013-06-10 11:14:04 -040068 def init_admin(self, tenant=None):
Tony Mack41945e02013-05-09 19:25:10 -040069 # use the admin credentials
Tony Mackc59fcaf2013-06-10 11:14:04 -040070 self.client = OpenStackClient(tenant=tenant)
Tony Mack41945e02013-05-09 19:25:10 -040071 self.driver = OpenStackDriver(client=self.client)
72 self.caller = self.driver.admin_user
Siobhan Tully73291342013-05-10 10:50:08 -040073 self.caller.kuser_id = self.caller.id
Tony Mack735493a2013-04-04 23:54:28 -040074
Tony Mack02755d42013-05-02 00:00:10 -040075 @require_enabled
76 def save_role(self, role):
Siobhan Tully47ae1b52013-05-10 15:53:14 -040077 if not role.role:
Tony Mack02755d42013-05-02 00:00:10 -040078 keystone_role = self.driver.create_role(role.role_type)
Siobhan Tully47ae1b52013-05-10 15:53:14 -040079 role.role = keystone_role.id
Tony Mack02755d42013-05-02 00:00:10 -040080
81 @require_enabled
82 def delete_role(self, role):
Siobhan Tully47ae1b52013-05-10 15:53:14 -040083 if role.role:
84 self.driver.delete_role({'id': role.role})
Tony Mackd685bfa2013-05-02 10:09:51 -040085
86 @require_enabled
Tony Mackb0d97422013-06-10 09:57:45 -040087 def save_key(self, key, name):
88 key_fields = {'name': name,
89 'public_key': key}
90 nova_key = self.driver.create_keypair(**key_fields)
Tony Mackd685bfa2013-05-02 10:09:51 -040091
92 @require_enabled
93 def delete_key(self, key):
Tony Mack71c685f2013-05-10 20:34:18 -040094 if key.nkey_id:
95 self.driver.delete_keypair(key.nkey_id)
Tony Mackd685bfa2013-05-02 10:09:51 -040096
97 @require_enabled
98 def save_user(self, user):
Tony Mack6795fbe2013-06-11 10:10:24 -040099 name = user.email[:user.email.find('@')]
100 user_fields = {'name': name,
101 'email': user.email,
Tony Mack869866b2013-06-14 18:16:10 -0400102 'password': hashlib.md5(user.password).hexdigest()[:6],
Tony Mack6795fbe2013-06-11 10:10:24 -0400103 'enabled': True}
Siobhan Tully30fd4292013-05-10 08:59:56 -0400104 if not user.kuser_id:
Tony Mackd685bfa2013-05-02 10:09:51 -0400105 keystone_user = self.driver.create_user(**user_fields)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400106 user.kuser_id = keystone_user.id
Tony Mack6795fbe2013-06-11 10:10:24 -0400107 else:
108 self.driver.update_user(user.kuser_id, user_fields)
Tony Mackb0d97422013-06-10 09:57:45 -0400109
Tony Mack386419d2013-05-05 11:48:43 -0400110 if user.site:
Siobhan Tully73291342013-05-10 10:50:08 -0400111 self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user')
Tony Mack386419d2013-05-05 11:48:43 -0400112 if user.is_admin:
Siobhan Tully73291342013-05-10 10:50:08 -0400113 self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin')
Tony Mack386419d2013-05-05 11:48:43 -0400114 else:
Tony Mack85d18832013-05-09 17:02:31 -0400115 # may have admin role so attempt to remove it
Siobhan Tully73291342013-05-10 10:50:08 -0400116 self.driver.delete_user_role(user.kuser_id, user.site.tenant_id, 'admin')
Tony Mack6795fbe2013-06-11 10:10:24 -0400117
118 if user.public_key:
119 self.init_caller(user, user.site.login_base)
120 self.save_key(user.public_key, user.keyname)
121 self.init_admin()
Tony Mack79a49c82013-06-15 23:51:57 -0400122
123 user.save()
124 user.enacted = datetime.now()
125 user.save(update_fields=['enacted'])
Tony Mack386419d2013-05-05 11:48:43 -0400126
Tony Mackd685bfa2013-05-02 10:09:51 -0400127 @require_enabled
128 def delete_user(self, user):
Siobhan Tully30fd4292013-05-10 08:59:56 -0400129 if user.kuser_id:
130 self.driver.delete_user(user.kuser_id)
Tony Mackd685bfa2013-05-02 10:09:51 -0400131
Tony Mack60722062013-05-02 10:57:04 -0400132 @require_enabled
Tony Macked163d72013-05-02 20:05:42 -0400133 def save_site(self, site, add_role=True):
Tony Mack60722062013-05-02 10:57:04 -0400134 if not site.tenant_id:
135 tenant = self.driver.create_tenant(tenant_name=site.login_base,
136 description=site.name,
137 enabled=site.enabled)
138 site.tenant_id = tenant.id
139 # give caller an admin role at the tenant they've created
Siobhan Tully30fd4292013-05-10 08:59:56 -0400140 self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin')
Tony Mack60722062013-05-02 10:57:04 -0400141
142 # update the record
143 if site.id and site.tenant_id:
144 self.driver.update_tenant(site.tenant_id,
145 description=site.name,
146 enabled=site.enabled)
147
Tony Mack79a49c82013-06-15 23:51:57 -0400148 # commit the updated record
149 site.save()
150 site.enacted = datetime.now()
151 site.save(update_fields=['enacted']) # enusre enacted > updated
152
153
Tony Mack60722062013-05-02 10:57:04 -0400154 @require_enabled
155 def delete_site(self, site):
156 if site.tenant_id:
157 self.driver.delete_tenant(site.tenant_id)
Tony Mackd685bfa2013-05-02 10:09:51 -0400158
Tony Mack93048c22013-05-02 11:20:26 -0400159 @require_enabled
Tony Mack79a49c82013-06-15 23:51:57 -0400160 def save_site_privilege(self, site_priv):
161 if site_priv.user.kuser_id and site_priv.site.tenant_id:
162 self.driver.add_user_role(site_priv.user.kuser_id,
163 site_priv.site.tenant_id,
164 site_priv.role.role_type)
165 site_priv.enacted = datetime.now()
166 site_priv.save(update_fields=['enacted'])
167
168
169 @require_enabled
170 def delete_site_privilege(self, site_priv):
171 self.driver.delete_user_role(site_priv.user.kuser_id,
172 site_priv.site.tenant_id,
173 site_priv.role.role_type)
174
175 @require_enabled
Tony Mack93048c22013-05-02 11:20:26 -0400176 def save_slice(self, slice):
177 if not slice.tenant_id:
178 nova_fields = {'tenant_name': slice.name,
179 'description': slice.description,
180 'enabled': slice.enabled}
181 tenant = self.driver.create_tenant(**nova_fields)
182 slice.tenant_id = tenant.id
183
184 # give caller an admin role at the tenant they've created
Siobhan Tully30fd4292013-05-10 08:59:56 -0400185 self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin')
Tony Mack93048c22013-05-02 11:20:26 -0400186
187 # refresh credentials using this tenant
188 self.driver.shell.connect(username=self.driver.shell.keystone.username,
189 password=self.driver.shell.keystone.password,
190 tenant=tenant.name)
191
192 # create network
193 network = self.driver.create_network(slice.name)
194 slice.network_id = network['id']
195
196 # create router
197 router = self.driver.create_router(slice.name)
198 slice.router_id = router['id']
199
Tony Mack585160e2013-05-07 11:15:59 -0400200 # create subnet
201 next_subnet = self.get_next_subnet()
202 cidr = str(next_subnet.cidr)
203 ip_version = next_subnet.version
204 start = str(next_subnet[2])
205 end = str(next_subnet[-2])
206 subnet = self.driver.create_subnet(name=slice.name,
207 network_id = network['id'],
208 cidr_ip = cidr,
209 ip_version = ip_version,
210 start = start,
211 end = end)
212 slice.subnet_id = subnet['id']
213 # add subnet as interface to slice's router
214 self.driver.add_router_interface(router['id'], subnet['id'])
Tony Mackf180f212013-05-28 09:19:01 -0400215 # add external route
Tony Mack79a49c82013-06-15 23:51:57 -0400216 self.driver.add_external_route(subnet)
217
Tony Mack585160e2013-05-07 11:15:59 -0400218
Tony Mack93048c22013-05-02 11:20:26 -0400219 if slice.id and slice.tenant_id:
220 self.driver.update_tenant(slice.tenant_id,
221 description=slice.description,
Tony Mack79a49c82013-06-15 23:51:57 -0400222 enabled=slice.enabled)
223
224 slice.save()
225 slice.enacted = datetime.now()
226 slice.save(update_fields=['enacted'])
Tony Mack93048c22013-05-02 11:20:26 -0400227
228 @require_enabled
229 def delete_slice(self, slice):
230 if slice.tenant_id:
Tony Mack79a49c82013-06-15 23:51:57 -0400231 self._delete_slice(slice.tenant_id, slice.network_id,
232 slice.router_id, slice.subnet_id)
233 @require_enabled
234 def _delete_slice(self, tenant_id, network_id, router_id, subnet_id):
235 self.driver.delete_router_interface(slice.router_id, slice.subnet_id)
236 self.driver.delete_subnet(slice.subnet_id)
237 self.driver.delete_router(slice.router_id)
238 self.driver.delete_network(slice.network_id)
239 self.driver.delete_tenant(slice.tenant_id)
240 # delete external route
241 subnet = None
242 subnets = self.driver.shell.quantum.list_subnets()['subnets']
243 for snet in subnets:
244 if snet['id'] == slice.subnet_id:
245 subnet = snet
246 if subnet:
247 self.driver.delete_external_route(subnet)
248
249
250 @require_enabled
251 def save_slice_membership(self, slice_memb):
252 if slice_memb.user.kuser_id and slice_memb.slice.tenant_id:
253 self.driver.add_user_role(slice_memb.user.kuser_id,
254 slice_memb.slice.tenant_id,
255 slice_memb.role.role_type)
256 slice_memb.enacted = datetime.now()
257 slice_memb.save(update_fields=['enacted'])
258
259
260 @require_enabled
261 def delete_slice_membership(self, slice_memb):
262 self.driver.delete_user_role(slice_memb.user.kuser_id,
263 slice_memb.slice.tenant_id,
264 slice_memb.role.role_type)
Tony Mack93048c22013-05-02 11:20:26 -0400265
Tony Mack585160e2013-05-07 11:15:59 -0400266
Tony Mackc59fcaf2013-06-10 11:14:04 -0400267 @require_enabled
Tony Mack585160e2013-05-07 11:15:59 -0400268 def get_next_subnet(self):
269 # limit ourself to 10.0.x.x for now
270 valid_subnet = lambda net: net.startswith('10.0')
271 subnets = self.driver.shell.quantum.list_subnets()['subnets']
272 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
273 if valid_subnet(subnet['cidr'])]
274 ints.sort()
275 last_ip = IPAddress(ints[-1])
276 last_network = IPNetwork(str(last_ip) + "/24")
277 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
278 return next_network
279
Tony Mack951dab42013-05-02 19:51:45 -0400280 @require_enabled
281 def save_subnet(self, subnet):
282 if not subnet.subnet_id:
283 quantum_subnet = self.driver.create_subnet(name= subnet.slice.name,
284 network_id=subnet.slice.network_id,
285 cidr_ip = subnet.cidr,
286 ip_version=subnet.ip_version,
287 start = subnet.start,
288 end = subnet.end)
289 subnet.subnet_id = quantum_subnet['id']
290 # add subnet as interface to slice's router
291 self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id)
292 #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
293 #commands.getstatusoutput(add_route)
294
295
296 @require_enabled
297 def delete_subnet(self, subnet):
298 if subnet.subnet_id:
299 self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id)
300 self.driver.delete_subnet(subnet.subnet_id)
301 #del_route = 'route del -net %s' % self.cidr
302 #commands.getstatusoutput(del_route)
smbaker8f5cf5f2013-05-05 13:58:16 -0700303
Scott Baker8a6d5122013-08-07 18:57:15 -0700304 def get_requested_networks(self, slice):
305 network_ids = [x.network_id for x in slice.networks.all()]
306
307 if slice.network_id is not None:
308 network_ids.append(slice.network_id)
309
310 networks = []
311 for network_id in network_ids:
Scott Baker57368722013-08-13 18:04:38 -0700312 networks.append({"net-id": network_id})
Scott Baker8a6d5122013-08-07 18:57:15 -0700313
314 return networks
315
Tony Mack951dab42013-05-02 19:51:45 -0400316 @require_enabled
317 def save_sliver(self, sliver):
Scott Baker3a0e66b2013-09-12 11:21:34 -0700318 metadata_update = {}
319 if ("numberCores" in sliver.changed_fields):
320 metadata_update["cpu_cores"] = str(sliver.numberCores)
321
322 for tag in sliver.slice.tags.all():
323 if tag.name.startswith("sysctl-"):
324 metadata_update[tag.name] = tag.value
325
Tony Mack951dab42013-05-02 19:51:45 -0400326 if not sliver.instance_id:
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700327 nics = self.get_requested_networks(sliver.slice)
Scott Baker3a0e66b2013-09-12 11:21:34 -0700328 for nic in nics:
329 # If a network hasn't been instantiated yet, then we'll fail
330 # during slice creation. Defer saving the sliver for now.
331 if not nic.get("net-id", None):
332 sliver.save() # in case it hasn't been saved yet
333 return
Tony Mack5ff5c452013-06-11 11:19:32 -0400334 slice_memberships = SliceMembership.objects.filter(slice=sliver.slice)
Tony Mackdfefe9d2013-06-24 09:39:40 -0400335 pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key]
Scott Baker8a6d5122013-08-07 18:57:15 -0700336 pubkeys.append(sliver.creator.public_key)
Tony Mack951dab42013-05-02 19:51:45 -0400337 instance = self.driver.spawn_instance(name=sliver.name,
Tony Mackb0d97422013-06-10 09:57:45 -0400338 key_name = sliver.creator.keyname,
Tony Mack951dab42013-05-02 19:51:45 -0400339 image_id = sliver.image.image_id,
Tony Mack5ff5c452013-06-11 11:19:32 -0400340 hostname = sliver.node.name,
Scott Baker8a6d5122013-08-07 18:57:15 -0700341 pubkeys = pubkeys,
Scott Baker3a0e66b2013-09-12 11:21:34 -0700342 nics = nics,
343 metadata = metadata_update )
Tony Mack951dab42013-05-02 19:51:45 -0400344 sliver.instance_id = instance.id
345 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
Scott Baker3a0e66b2013-09-12 11:21:34 -0700346 else:
347 if metadata_update:
348 self.driver.update_instance_metadata(sliver.instance_id, metadata_update)
smbaker8f5cf5f2013-05-05 13:58:16 -0700349
Tony Mack79a49c82013-06-15 23:51:57 -0400350 sliver.save()
351 sliver.enacted = datetime.now()
352 sliver.save(update_fields=['enacted'])
353
Tony Mack951dab42013-05-02 19:51:45 -0400354 @require_enabled
355 def delete_sliver(self, sliver):
356 if sliver.instance_id:
357 self.driver.destroy_instance(sliver.instance_id)
358
359
Tony Mack735493a2013-04-04 23:54:28 -0400360 def refresh_nodes(self):
361 # collect local nodes
Tony Mack735493a2013-04-04 23:54:28 -0400362 nodes = Node.objects.all()
363 nodes_dict = {}
364 for node in nodes:
Tony Mack48952032013-04-12 11:49:34 -0400365 if 'viccidev10' not in node.name:
366 nodes_dict[node.name] = node
367
Siobhan Tullybf1153a2013-05-27 20:53:48 -0400368 deployment = Deployment.objects.filter(name='VICCI')[0]
Tony Mack48952032013-04-12 11:49:34 -0400369 login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws']
370 sites = Site.objects.filter(login_base__in=login_bases)
Tony Mack735493a2013-04-04 23:54:28 -0400371 # collect nova nodes:
Tony Mack48952032013-04-12 11:49:34 -0400372 compute_nodes = self.client.nova.hypervisors.list()
373
Tony Mack735493a2013-04-04 23:54:28 -0400374 compute_nodes_dict = {}
375 for compute_node in compute_nodes:
376 compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node
377
378 # add new nodes:
379 new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys())
Tony Mack51f113d2013-04-13 02:02:22 -0400380 i = 0
381 max = len(sites)
Tony Mack735493a2013-04-04 23:54:28 -0400382 for name in new_node_names:
Tony Mack51f113d2013-04-13 02:02:22 -0400383 if i == max:
384 i = 0
385 site = sites[i]
386 node = Node(name=compute_nodes_dict[name].hypervisor_hostname,
387 site=site,
Tony Mackb0d97422013-06-10 09:57:45 -0400388 deployment=deployment)
Tony Mack735493a2013-04-04 23:54:28 -0400389 node.save()
Tony Mack51f113d2013-04-13 02:02:22 -0400390 i+=1
Tony Mack735493a2013-04-04 23:54:28 -0400391
392 # remove old nodes
393 old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys())
394 Node.objects.filter(name__in=old_node_names).delete()
395
Tony Mack735493a2013-04-04 23:54:28 -0400396 def refresh_images(self):
Tony Mack89f70f12013-05-10 20:20:03 -0400397 from core.models.image import Image
Tony Mack735493a2013-04-04 23:54:28 -0400398 # collect local images
Tony Mack735493a2013-04-04 23:54:28 -0400399 images = Image.objects.all()
Scott Baker8a6d5122013-08-07 18:57:15 -0700400 images_dict = {}
Tony Mack735493a2013-04-04 23:54:28 -0400401 for image in images:
402 images_dict[image.name] = image
403
404 # collect glance images
Tony Mack48952032013-04-12 11:49:34 -0400405 glance_images = self.client.glance.get_images()
Tony Mack735493a2013-04-04 23:54:28 -0400406 glance_images_dict = {}
407 for glance_image in glance_images:
408 glance_images_dict[glance_image['name']] = glance_image
409
410 # add new images
411 new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys())
412 for name in new_image_names:
413 image = Image(image_id=glance_images_dict[name]['id'],
414 name=glance_images_dict[name]['name'],
415 disk_format=glance_images_dict[name]['disk_format'],
416 container_format=glance_images_dict[name]['container_format'])
417 image.save()
418
419 # remove old images
420 old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys())
421 Image.objects.filter(name__in=old_image_names).delete()
Tony Mack02755d42013-05-02 00:00:10 -0400422
Scott Baker8a6d5122013-08-07 18:57:15 -0700423 @require_enabled
424 def save_network(self, network):
425 if not network.network_id:
Scott Baker369f9b92015-01-03 12:03:38 -0800426 if network.template.shared_network_name:
427 network.network_id = network.template.shared_network_id
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700428 (network.subnet_id, network.subnet) = self.driver.get_network_subnet(network.network_id)
429 else:
430 network_name = network.name
Scott Baker8a6d5122013-08-07 18:57:15 -0700431
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700432 # create network
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700433 os_network = self.driver.create_network(network_name, shared=True)
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700434 network.network_id = os_network['id']
Scott Baker8a6d5122013-08-07 18:57:15 -0700435
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700436 # create router
437 router = self.driver.create_router(network_name)
438 network.router_id = router['id']
Scott Baker8a6d5122013-08-07 18:57:15 -0700439
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700440 # create subnet
441 next_subnet = self.get_next_subnet()
442 cidr = str(next_subnet.cidr)
443 ip_version = next_subnet.version
444 start = str(next_subnet[2])
445 end = str(next_subnet[-2])
446 subnet = self.driver.create_subnet(name=network_name,
447 network_id = network.network_id,
448 cidr_ip = cidr,
449 ip_version = ip_version,
450 start = start,
451 end = end)
452 network.subnet = cidr
453 network.subnet_id = subnet['id']
454 # add subnet as interface to slice's router
455 self.driver.add_router_interface(router['id'], subnet['id'])
456 # add external route
457 self.driver.add_external_route(subnet)
Scott Baker8a6d5122013-08-07 18:57:15 -0700458
459 network.save()
460 network.enacted = datetime.now()
461 network.save(update_fields=['enacted'])
462
463 def delete_network(self, network):
464 if (network.router_id) and (network.subnet_id):
465 self.driver.delete_router_interface(network.router_id, network.subnet_id)
466 if network.subnet_id:
467 self.driver.delete_subnet(network.subnet_id)
468 if network.router_id:
469 self.driver.delete_router(network.router_id)
470 if network.network_id:
471 self.driver.delete_network(network.network_id)
472
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700473 def save_network_template(self, template):
Scott Baker369f9b92015-01-03 12:03:38 -0800474 if (template.shared_network_name) and (not template.shared_network_id):
475 os_networks = self.driver.shell.quantum.list_networks(name=template.shared_network_name)['networks']
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700476 if os_networks:
Scott Baker369f9b92015-01-03 12:03:38 -0800477 template.shared_network_id = os_networks[0]["id"]
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700478
479 template.save()
480 template.enacted = datetime.now()
481 template.save(update_fields=['enacted'])
482
Scott Baker8a6d5122013-08-07 18:57:15 -0700483 def find_or_make_template_for_network(self, name):
484 """ Given a network name, try to guess the right template for it """
485
486 # templates for networks we may encounter
487 if name=='nat-net':
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700488 template_dict = None # {"name": "private-nat", "visibility": "private", "translation": "nat"}
Scott Baker8a6d5122013-08-07 18:57:15 -0700489 elif name=='sharednet1':
490 template_dict = {"name": "dedicated-public", "visibility": "public", "translation": "none"}
491 else:
492 template_dict = {"name": "private", "visibility": "private", "translation": "none"}
493
494 # if we have an existing template return it
495 templates = NetworkTemplate.objects.filter(name=template_dict["name"])
496 if templates:
497 return templates[0]
498
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700499 if template_dict == None:
500 return None
501
Scott Baker8a6d5122013-08-07 18:57:15 -0700502 template = NetworkTemplate(**template_dict)
503 template.save()
504 return template
505
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700506 def refresh_network_templates(self):
507 for template in NetworkTemplate.objects.all():
Scott Baker369f9b92015-01-03 12:03:38 -0800508 if (template.shared_network_name) and (not template.shared_network_id):
509 # this will cause us to try to fill in the shared_network_id
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700510 self.save_network_template(template)
511
Scott Baker8a6d5122013-08-07 18:57:15 -0700512 def refresh_networks(self):
513 # get a list of all networks in the model
514
515 networks = Network.objects.all()
516 networks_by_name = {}
517 networks_by_id = {}
518 for network in networks:
519 networks_by_name[network.name] = network
520 networks_by_id[network.network_id] = network
521
522 # Get a list of all shared networks in OS
523
524 os_networks = self.driver.shell.quantum.list_networks()['networks']
525 os_networks_by_name = {}
526 os_networks_by_id = {}
527 for os_network in os_networks:
528 os_networks_by_name[os_network['name']] = os_network
529 os_networks_by_id[os_network['id']] = os_network
530
531 for (uuid, os_network) in os_networks_by_id.items():
532 #print "checking OS network", os_network['name']
533 if (os_network['shared']) and (uuid not in networks_by_id):
534 # Only automatically create shared networks. This is for Andy's
535 # nat-net and sharednet1.
536
537 owner_slice = Slice.objects.get(tenant_id = os_network['tenant_id'])
538 template = self.find_or_make_template_for_network(os_network['name'])
539
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700540 if (template is None):
541 # This is our way of saying we don't want to auto-instantiate
542 # this network type.
543 continue
544
545 (subnet_id, subnet) = self.driver.get_network_subnet(os_network['id'])
Scott Baker8a6d5122013-08-07 18:57:15 -0700546
547 if owner_slice:
548 #print "creating model object for OS network", os_network['name']
549 new_network = Network(name = os_network['name'],
550 template = template,
551 owner = owner_slice,
552 network_id = uuid,
553 subnet_id = subnet_id)
554 new_network.save()
555
556 for (network_id, network) in networks_by_id.items():
557 # If the network disappeared from OS, then reset its network_id to None
558 if (network.network_id is not None) and (network.network_id not in os_networks_by_id):
559 network.network_id = None
560
561 # If no OS object exists, then saving the network will create one
562 if (network.network_id is None):
563 #print "creating OS network for", network.name
564 self.save_network(network)
565 else:
566 pass #print "network", network.name, "has its OS object"
567
Tony Mack02755d42013-05-02 00:00:10 -0400568