blob: d8c3c61a93c853fb79ded9f94285db9864af40e8 [file] [log] [blame]
Tony Mackc59fcaf2013-06-10 11:14:04 -04001import time
Tony Mack0553f282013-06-10 22:54:50 -04002import traceback
Tony Mack70780e92013-06-30 12:46:16 -04003import commands
Sapan Bhatia66f4e612013-07-02 12:12:38 -04004import threading
5
Tony Mackc59fcaf2013-06-10 11:14:04 -04006from datetime import datetime
Tony Mack79a49c82013-06-15 23:51:57 -04007from collections import defaultdict
Tony Mackc59fcaf2013-06-10 11:14:04 -04008from core.models import *
9from django.db.models import F, Q
10from openstack.manager import OpenStackManager
Sapan Bhatia43c3a772013-07-03 11:19:07 -040011from util.logger import Logger, logging, logger
Tony Mack5c0c4552013-07-03 09:36:51 -040012#from timeout import timeout
Tony Mackc59fcaf2013-06-10 11:14:04 -040013
14
Tony Mack79a49c82013-06-15 23:51:57 -040015logger = Logger(logfile='observer.log', level=logging.INFO)
16
Tony Mackc59fcaf2013-06-10 11:14:04 -040017class OpenStackObserver:
18
19 def __init__(self):
Tony Mack0553f282013-06-10 22:54:50 -040020 self.manager = OpenStackManager()
Sapan Bhatia66f4e612013-07-02 12:12:38 -040021 # The Condition object that gets signalled by Feefie events
22 self.event_cond = threading.Condition()
23
24 def wait_for_event(self, timeout):
25 self.event_cond.acquire()
26 self.event_cond.wait(timeout)
27 self.event_cond.release()
28
29 def wake_up(self):
Sapan Bhatia43c3a772013-07-03 11:19:07 -040030 logger.info('Wake up routine called. Event cond %r'%self.event_cond)
Sapan Bhatia66f4e612013-07-02 12:12:38 -040031 self.event_cond.acquire()
32 self.event_cond.notify()
33 self.event_cond.release()
Tony Mack0553f282013-06-10 22:54:50 -040034
35 def run(self):
36 if not self.manager.enabled or not self.manager.has_openstack:
37 return
38 while True:
39 try:
Scott Baker39671c72013-08-25 23:16:15 -070040 start_time=time.time()
Sapan Bhatia43c3a772013-07-03 11:19:07 -040041 logger.info('Observer run loop')
Tony Mack6795fbe2013-06-11 10:10:24 -040042 #self.sync_roles()
Scott Baker78c87be2013-08-09 15:42:30 -070043
Sapan Bhatia43c3a772013-07-03 11:19:07 -040044 logger.info('Calling sync tenants')
Scott Baker78c87be2013-08-09 15:42:30 -070045 try:
46 self.sync_tenants()
47 except:
48 logger.log_exc("Exception in sync_tenants")
49 traceback.print_exc()
Scott Baker39671c72013-08-25 23:16:15 -070050 finish_time = time.time()
51 logger.info('Sync tenants took %f seconds'%(finish_time-start_time))
Scott Baker78c87be2013-08-09 15:42:30 -070052
53 logger.info('Calling sync users')
54 try:
55 self.sync_users()
56 except:
57 logger.log_exc("Exception in sync_users")
58 traceback.print_exc()
Scott Baker39671c72013-08-25 23:16:15 -070059 finish_time = time.time()
60 logger.info('Sync users took %f seconds'%(finish_time-start_time))
Scott Baker78c87be2013-08-09 15:42:30 -070061
62 logger.info('Calling sync tenant roles')
63 try:
64 self.sync_user_tenant_roles()
65 except:
66 logger.log_exc("Exception in sync_users")
67 traceback.print_exc()
68
69 logger.info('Calling sync slivers')
70 try:
71 self.sync_slivers()
72 except:
73 logger.log_exc("Exception in sync slivers")
74 traceback.print_exc()
Scott Baker39671c72013-08-25 23:16:15 -070075 finish_time = time.time()
76 logger.info('Sync slivers took %f seconds'%(finish_time-start_time))
Scott Baker78c87be2013-08-09 15:42:30 -070077
78 logger.info('Calling sync sliver ips')
79 try:
80 self.sync_sliver_ips()
81 except:
82 logger.log_exc("Exception in sync_sliver_ips")
83 traceback.print_exc()
Scott Baker39671c72013-08-25 23:16:15 -070084 finish_time = time.time()
85 logger.info('Sync sliver ips took %f seconds'%(finish_time-start_time))
Scott Baker78c87be2013-08-09 15:42:30 -070086
87 logger.info('Calling sync networks')
88 try:
89 self.sync_networks()
90 except:
91 logger.log_exc("Exception in sync_networks")
92 traceback.print_exc()
Scott Baker39671c72013-08-25 23:16:15 -070093 finish_time = time.time()
94 logger.info('Sync networks took %f seconds'%(finish_time-start_time))
Scott Baker78c87be2013-08-09 15:42:30 -070095
Scott Baker1f299722013-08-19 17:59:26 -070096 logger.info('Calling sync network slivers')
97 try:
98 self.sync_network_slivers()
99 except:
100 logger.log_exc("Exception in sync_network_slivers")
101 traceback.print_exc()
Scott Baker39671c72013-08-25 23:16:15 -0700102 finish_time = time.time()
103 logger.info('Sync network sliver ips took %f seconds'%(finish_time-start_time))
Scott Baker1f299722013-08-19 17:59:26 -0700104
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400105 logger.info('Calling sync external routes')
Scott Baker78c87be2013-08-09 15:42:30 -0700106 try:
107 self.sync_external_routes()
108 except:
109 logger.log_exc("Exception in sync_external_routes")
110 traceback.print_exc()
Scott Baker39671c72013-08-25 23:16:15 -0700111 finish_time = time.time()
112 logger.info('Sync external routes took %f seconds'%(finish_time-start_time))
Scott Baker78c87be2013-08-09 15:42:30 -0700113
114 logger.info('Waiting for event')
Scott Baker4b075bd2013-08-13 18:05:32 -0700115 tBeforeWait = time.time()
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400116 self.wait_for_event(timeout=300)
Scott Baker4b075bd2013-08-13 18:05:32 -0700117
118 # Enforce 5 minutes between wakeups
119 tSleep = 300 - (time.time() - tBeforeWait)
120 if tSleep > 0:
121 logger.info('Sleeping for %d seconds' % tSleep)
Scott Bakere5097f02013-08-14 10:47:32 -0700122 time.sleep(tSleep)
Scott Baker4b075bd2013-08-13 18:05:32 -0700123
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400124 logger.info('Observer woken up')
Tony Mack0553f282013-06-10 22:54:50 -0400125 except:
Scott Baker78c87be2013-08-09 15:42:30 -0700126 logger.log_exc("Exception in observer run loop")
127 traceback.print_exc()
Tony Mackc59fcaf2013-06-10 11:14:04 -0400128
Tony Mackf8d4a462013-06-10 23:25:30 -0400129 def sync_roles(self):
130 """
131 save all role that don't already exist in keystone. Remove keystone roles that
132 don't exist in planetstack
133 """
134 # sync all roles that don't already in keystone
135 keystone_roles = self.manager.driver.shell.keystone.roles.findall()
136 keystone_role_names = [kr.name for kr in keystone_roles]
137 pending_roles = Role.objects.all()
138 pending_role_names = [r.role_type for r in pending_roles]
139 for role in pending_roles:
140 if role.role_type not in keystone_role_names:
Tony Mack79a49c82013-06-15 23:51:57 -0400141 try:
142 self.manager.save_role(role)
143 logger.info("save role: %s" % (role))
144 except:
145 logger.log_exc("save role failed: %s" % role)
146 traceback.print_exc()
Tony Mackf8d4a462013-06-10 23:25:30 -0400147
Tony Mack79a49c82013-06-15 23:51:57 -0400148 # don't delete roles for now
149 """
150 # delete keystone roles that don't exist in planetstack
Tony Mackf8d4a462013-06-10 23:25:30 -0400151 for keystone_role in keystone_roles:
152 if keystone_role.name == 'admin':
153 continue
154 if keystone_role.name not in pending_role_names:
Tony Mack79a49c82013-06-15 23:51:57 -0400155 try:
156 self.manager.driver.delete_role({id: keystone_role.id})
157 except:
158 traceback.print_exc()
159 """
Tony Mackf8d4a462013-06-10 23:25:30 -0400160
Tony Mackdb9be932013-06-10 13:42:42 -0400161 def sync_tenants(self):
Tony Mackc59fcaf2013-06-10 11:14:04 -0400162 """
Tony Mackdb9be932013-06-10 13:42:42 -0400163 Save all sites and sliceswhere enacted < updated or enacted == None.
164 Remove sites and slices that no don't exist in openstack db if they
165 have an enacted time (enacted != None).
Tony Mackc59fcaf2013-06-10 11:14:04 -0400166 """
167 # get all sites that need to be synced (enacted < updated or enacted is None)
168 pending_sites = Site.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
169 for site in pending_sites:
Tony Mack79a49c82013-06-15 23:51:57 -0400170 try:
171 self.manager.save_site(site)
172 logger.info("saved site %s" % site)
173 except:
174 logger.log_exc("save site failed: %s" % site)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400175
Tony Mackc59fcaf2013-06-10 11:14:04 -0400176 # get all slices that need to be synced (enacted < updated or enacted is None)
177 pending_slices = Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
178 for slice in pending_slices:
Tony Mack79a49c82013-06-15 23:51:57 -0400179 try:
180 self.manager.init_caller(slice.creator, slice.creator.site.login_base)
181 self.manager.save_slice(slice)
182 logger.info("saved slice %s" % slice)
183 except:
184 logger.log_exc("save slice failed: %s" % slice)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400185
Tony Mackdb9be932013-06-10 13:42:42 -0400186 # get all sites that where enacted != null. We can assume these sites
187 # have previously been synced and need to be checed for deletion.
188 sites = Site.objects.filter(enacted__isnull=False)
189 site_dict = {}
190 for site in sites:
191 site_dict[site.login_base] = site
192
Tony Mackc59fcaf2013-06-10 11:14:04 -0400193 # get all slices that where enacted != null. We can assume these slices
194 # have previously been synced and need to be checed for deletion.
195 slices = Slice.objects.filter(enacted__isnull=False)
196 slice_dict = {}
197 for slice in slices:
198 slice_dict[slice.name] = slice
199
200 # delete keystone tenants that don't have a site record
201 tenants = self.manager.driver.shell.keystone.tenants.findall()
Tony Mack79a49c82013-06-15 23:51:57 -0400202 system_tenants = ['admin','service']
Tony Mackc59fcaf2013-06-10 11:14:04 -0400203 for tenant in tenants:
Tony Mack586d5c92013-06-20 12:04:30 -0400204 if tenant.name in system_tenants:
Tony Mackdb9be932013-06-10 13:42:42 -0400205 continue
206 if tenant.name not in site_dict and tenant.name not in slice_dict:
Tony Mack79a49c82013-06-15 23:51:57 -0400207 try:
208 self.manager.driver.delete_tenant(tenant.id)
209 logger.info("deleted tenant: %s" % (tenant))
210 except:
211 logger.log_exc("delete tenant failed: %s" % tenant)
Tony Mackdb9be932013-06-10 13:42:42 -0400212
Tony Mackc59fcaf2013-06-10 11:14:04 -0400213
214 def sync_users(self):
215 """
216 save all users where enacted < updated or enacted == None. Remove users that
217 no don't exist in openstack db if they have an enacted time (enacted != None).
218 """
219 # get all users that need to be synced (enacted < updated or enacted is None)
220 pending_users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
221 for user in pending_users:
Tony Mack79a49c82013-06-15 23:51:57 -0400222 try:
223 self.manager.save_user(user)
224 logger.info("saved user: %s" % (user))
225 except:
226 logger.log_exc("save user failed: %s" %user)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400227
228 # get all users that where enacted != null. We can assume these users
229 # have previously been synced and need to be checed for deletion.
230 users = User.objects.filter(enacted__isnull=False)
231 user_dict = {}
232 for user in users:
233 user_dict[user.kuser_id] = user
234
235 # delete keystone users that don't have a user record
Tony Mack79a49c82013-06-15 23:51:57 -0400236 system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service']
Tony Mack0553f282013-06-10 22:54:50 -0400237 users = self.manager.driver.shell.keystone.users.findall()
Tony Mackc59fcaf2013-06-10 11:14:04 -0400238 for user in users:
Tony Mack79a49c82013-06-15 23:51:57 -0400239 if user.name in system_users:
Tony Mackdb9be932013-06-10 13:42:42 -0400240 continue
Tony Mackc59fcaf2013-06-10 11:14:04 -0400241 if user.id not in user_dict:
Tony Mack79a49c82013-06-15 23:51:57 -0400242 try:
243 #self.manager.driver.delete_user(user.id)
244 logger.info("deleted user: %s" % user)
245 except:
246 logger.log_exc("delete user failed: %s" % user)
247
248
249 def sync_user_tenant_roles(self):
250 """
251 Save all site privileges and slice memberships wheree enacted < updated or
252 enacted == None. Remove ones that don't exist in openstack db if they have
253 an enacted time (enacted != None).
254 """
255 # sync site privileges
256 pending_site_privileges = SitePrivilege.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
257 for site_priv in pending_site_privileges:
258 try:
259 self.manager.save_site_privilege(site_priv)
260 logger.info("saved site privilege: %s" % (site_priv))
261 except: logger.log_exc("save site privilege failed: %s " % site_priv)
262
263 # sync slice memberships
264 pending_slice_memberships = SliceMembership.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
265 for slice_memb in pending_slice_memberships:
266 try:
267 self.manager.save_slice_membership(slice_memb)
268 logger.info("saved slice membership: %s" % (slice_memb))
269 except: logger.log_exc("save slice membership failed: %s" % slice_memb)
270
271 # get all site privileges and slice memberships that have been enacted
272 user_tenant_roles = defaultdict(list)
273 for site_priv in SitePrivilege.objects.filter(enacted__isnull=False):
274 user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role)
275 for slice_memb in SliceMembership.objects.filter(enacted__isnull=False):
276 user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role)
277
278 # Some user tenant role aren't stored in planetstack but they must be preserved.
279 # Role that fall in this category are
280 # 1. Never remove a user's role that their home site
281 # 2. Never remove a user's role at a slice they've created.
282 # Keep track of all roles that must be preserved.
283 users = User.objects.all()
284 preserved_roles = {}
285 for user in users:
286 tenant_ids = [s['tenant_id'] for s in user.slices.values()]
287 tenant_ids.append(user.site.tenant_id)
288 preserved_roles[user.kuser_id] = tenant_ids
289
290
291 # begin removing user tenant roles from keystone. This is stored in the
292 # Metadata table.
293 for metadata in self.manager.driver.shell.keystone_db.get_metadata():
294 # skip admin roles
295 if metadata.user_id == self.manager.driver.admin_user.id:
296 continue
297 # skip preserved tenant ids
298 if metadata.user_id in preserved_roles and \
299 metadata.tenant_id in preserved_roles[metadata.user_id]:
300 continue
301 # get roles for user at this tenant
302 user_tenant_role_ids = user_tenant_roles.get((metadata.user_id, metadata.tenant_id), [])
303
304 if user_tenant_role_ids:
305 # The user has roles at the tenant. Check if roles need to
306 # be updated.
307 user_keystone_role_ids = metadata.data.get('roles', [])
308 for role_id in user_keystone_role_ids:
309 if role_id not in user_tenant_role_ids:
310 user_keystone_role_ids.pop(user_keystone_role_ids.index(role_id))
311 else:
312 # The user has no roles at this tenant.
313 metadata.data['roles'] = []
314 #session.add(metadata)
315 logger.info("pruning metadata for %s at %s" % (metadata.user_id, metadata.tenant_id))
316
Tony Mackc59fcaf2013-06-10 11:14:04 -0400317 def sync_slivers(self):
318 """
319 save all slivers where enacted < updated or enacted == None. Remove slivers that
320 no don't exist in openstack db if they have an enacted time (enacted != None).
321 """
322 # get all users that need to be synced (enacted < updated or enacted is None)
323 pending_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
324 for sliver in pending_slivers:
Tony Mack9b7a8bd2013-06-24 15:08:01 -0400325 if sliver.creator:
Tony Mack79a49c82013-06-15 23:51:57 -0400326 try:
327 # update manager context
328 self.manager.init_caller(sliver.creator, sliver.slice.name)
329 self.manager.save_sliver(sliver)
Tony Macke3f8d862013-07-01 20:40:55 -0400330 logger.info("saved sliver: %s" % (sliver))
Tony Mack79a49c82013-06-15 23:51:57 -0400331 except:
332 logger.log_exc("save sliver failed: %s" % sliver)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400333
Tony Mackaa8ad5f2013-06-24 13:50:06 -0400334 # get all slivers where enacted != null. We can assume these users
Tony Mackc59fcaf2013-06-10 11:14:04 -0400335 # have previously been synced and need to be checed for deletion.
336 slivers = Sliver.objects.filter(enacted__isnull=False)
337 sliver_dict = {}
338 for sliver in slivers:
339 sliver_dict[sliver.instance_id] = sliver
340
341 # delete sliver that don't have a sliver record
Scott Bakerf4df9522013-08-19 17:56:45 -0700342 ctx = self.manager.driver.shell.nova_db.ctx
Tony Mackc59fcaf2013-06-10 11:14:04 -0400343 instances = self.manager.driver.shell.nova_db.instance_get_all(ctx)
344 for instance in instances:
Tony Mackaa8ad5f2013-06-24 13:50:06 -0400345 if instance.uuid not in sliver_dict:
Tony Mack79a49c82013-06-15 23:51:57 -0400346 try:
Scott Baker39671c72013-08-25 23:16:15 -0700347 # lookup tenant and update context
348 try:
349 tenant = self.manager.driver.shell.keystone.tenants.find(id=instance.project_id)
350 tenant_name = tenant.name
351 except:
352 tenant_name = None
353 logger.info("exception while retrieving tenant %s. Deleting instance using root tenant." % instance.project_id)
354 self.manager.init_admin(tenant=tenant_name)
Tony Mackaa8ad5f2013-06-24 13:50:06 -0400355 self.manager.driver.destroy_instance(instance.uuid)
Scott Baker39671c72013-08-25 23:16:15 -0700356 logger.info("destroyed sliver: %s" % (instance.uuid))
Tony Mack79a49c82013-06-15 23:51:57 -0400357 except:
358 logger.log_exc("destroy sliver failed: %s" % instance)
359
360
361 def sync_sliver_ips(self):
362 # fill in null ip addresses
363 slivers = Sliver.objects.filter(ip=None)
364 for sliver in slivers:
365 # update connection
366 self.manager.init_admin(tenant=sliver.slice.name)
Tony Mack70780e92013-06-30 12:46:16 -0400367 servers = self.manager.driver.shell.nova.servers.findall(id=sliver.instance_id)
Tony Mack79a49c82013-06-15 23:51:57 -0400368 if not servers:
369 continue
370 server = servers[0]
371 ips = server.addresses.get(sliver.slice.name, [])
372 if not ips:
373 continue
374 sliver.ip = ips[0]['addr']
375 sliver.save()
376 logger.info("saved sliver ip: %s %s" % (sliver, ips[0]))
Tony Mack70780e92013-06-30 12:46:16 -0400377
378 def sync_external_routes(self):
379 routes = self.manager.driver.get_external_routes()
Tony Mack16f51e02013-07-01 10:33:29 -0400380 subnets = self.manager.driver.shell.quantum.list_subnets()['subnets']
Tony Mack70780e92013-06-30 12:46:16 -0400381 for subnet in subnets:
Scott Baker78c87be2013-08-09 15:42:30 -0700382 try:
383 self.manager.driver.add_external_route(subnet, routes)
384 except:
Tony Mack16f51e02013-07-01 10:33:29 -0400385 logger.log_exc("failed to add external route for subnet %s" % subnet)
Scott Baker78c87be2013-08-09 15:42:30 -0700386
Scott Bakerf4df9522013-08-19 17:56:45 -0700387 def sync_network_slivers(self):
388 networkSlivers = NetworkSliver.objects.all()
389 networkSlivers_by_id = {}
390 networkSlivers_by_port = {}
391 for networkSliver in networkSlivers:
392 networkSlivers_by_id[networkSliver.id] = networkSliver
393 networkSlivers_by_port[networkSliver.port_id] = networkSliver
394
395 networks = Network.objects.all()
396 networks_by_id = {}
397 for network in networks:
398 networks_by_id[network.network_id] = network
399
400 slivers = Sliver.objects.all()
401 slivers_by_instance_id = {}
402 for sliver in slivers:
403 slivers_by_instance_id[sliver.instance_id] = sliver
404
405 ports = self.manager.driver.shell.quantum.list_ports()["ports"]
406 for port in ports:
Scott Bakerf4df9522013-08-19 17:56:45 -0700407 if port["id"] in networkSlivers_by_port:
408 # we already have it
409 print "already accounted for port", port["id"]
410 continue
411
412 if port["device_owner"] != "compute:nova":
413 # we only want the ports that connect to instances
414 continue
415
416 network = networks_by_id.get(port['network_id'], None)
417 if not network:
418 #print "no network for port", port["id"], "network", port["network_id"]
419 continue
420
421 sliver = slivers_by_instance_id.get(port['device_id'], None)
422 if not sliver:
423 print "no sliver for port", port["id"], "device_id", port['device_id']
424 continue
425
426 if network.template.sharedNetworkId is not None:
427 # If it's a shared network template, then more than one network
428 # object maps to the quantum network. We have to do a whole bunch
429 # of extra work to find the right one.
430 networks = network.template.network_set.all()
431 network = None
432 for candidate_network in networks:
433 if (candidate_network.owner == sliver.slice):
434 print "found network", candidate_network
435 network = candidate_network
436
437 if not network:
438 print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"]
439 continue
440
441 if not port["fixed_ips"]:
442 print "port", port["id"], "has no fixed_ips"
443 continue
444
Scott Baker1f299722013-08-19 17:59:26 -0700445# print "XXX", port
Scott Bakerf4df9522013-08-19 17:56:45 -0700446
447 ns = NetworkSliver(network=network,
448 sliver=sliver,
449 ip=port["fixed_ips"][0]["ip_address"],
450 port_id=port["id"])
451 ns.save()
452
Scott Baker78c87be2013-08-09 15:42:30 -0700453 def sync_networks(self):
454 """
455 save all networks where enacted < updated or enacted == None. Remove networks that
456 no don't exist in openstack db if they have an enacted time (enacted != None).
457 """
458 # get all users that need to be synced (enacted < updated or enacted is None)
459 pending_networks = Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
460 for network in pending_networks:
461 if network.owner and network.owner.creator:
462 try:
463 # update manager context
464 self.manager.init_caller(network.owner.creator, network.owner.name)
465 self.manager.save_network(network)
466 logger.info("saved network: %s" % (network))
467 except:
468 logger.log_exc("save network failed: %s" % network)
469
470 # get all networks where enacted != null. We can assume these users
471 # have previously been synced and need to be checed for deletion.
472 networks = Network.objects.filter(enacted__isnull=False)
473 network_dict = {}
474 for network in networks:
475 network_dict[network.network_id] = network
476
477 # TODO: delete Network objects if quantum network doesn't exist
478 # (need to write self.manager.driver.shell.quantum_db)
479