blob: 73bb1149105e233999b645aedac71cc89d7466d3 [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:
Sapan Bhatia43c3a772013-07-03 11:19:07 -040040 logger.info('Observer run loop')
Tony Mack6795fbe2013-06-11 10:10:24 -040041 #self.sync_roles()
Scott Baker78c87be2013-08-09 15:42:30 -070042
Sapan Bhatia43c3a772013-07-03 11:19:07 -040043 logger.info('Calling sync tenants')
Scott Baker78c87be2013-08-09 15:42:30 -070044 try:
45 self.sync_tenants()
46 except:
47 logger.log_exc("Exception in sync_tenants")
48 traceback.print_exc()
49
50 logger.info('Calling sync users')
51 try:
52 self.sync_users()
53 except:
54 logger.log_exc("Exception in sync_users")
55 traceback.print_exc()
56
57 logger.info('Calling sync tenant roles')
58 try:
59 self.sync_user_tenant_roles()
60 except:
61 logger.log_exc("Exception in sync_users")
62 traceback.print_exc()
63
64 logger.info('Calling sync slivers')
65 try:
66 self.sync_slivers()
67 except:
68 logger.log_exc("Exception in sync slivers")
69 traceback.print_exc()
70
71 logger.info('Calling sync sliver ips')
72 try:
73 self.sync_sliver_ips()
74 except:
75 logger.log_exc("Exception in sync_sliver_ips")
76 traceback.print_exc()
77
78 logger.info('Calling sync networks')
79 try:
80 self.sync_networks()
81 except:
82 logger.log_exc("Exception in sync_networks")
83 traceback.print_exc()
84
Scott Baker1f299722013-08-19 17:59:26 -070085 logger.info('Calling sync network slivers')
86 try:
87 self.sync_network_slivers()
88 except:
89 logger.log_exc("Exception in sync_network_slivers")
90 traceback.print_exc()
91
Sapan Bhatia43c3a772013-07-03 11:19:07 -040092 logger.info('Calling sync external routes')
Scott Baker78c87be2013-08-09 15:42:30 -070093 try:
94 self.sync_external_routes()
95 except:
96 logger.log_exc("Exception in sync_external_routes")
97 traceback.print_exc()
98
99 logger.info('Waiting for event')
Scott Baker4b075bd2013-08-13 18:05:32 -0700100 tBeforeWait = time.time()
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400101 self.wait_for_event(timeout=300)
Scott Baker4b075bd2013-08-13 18:05:32 -0700102
103 # Enforce 5 minutes between wakeups
104 tSleep = 300 - (time.time() - tBeforeWait)
105 if tSleep > 0:
106 logger.info('Sleeping for %d seconds' % tSleep)
Scott Bakere5097f02013-08-14 10:47:32 -0700107 time.sleep(tSleep)
Scott Baker4b075bd2013-08-13 18:05:32 -0700108
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400109 logger.info('Observer woken up')
Tony Mack0553f282013-06-10 22:54:50 -0400110 except:
Scott Baker78c87be2013-08-09 15:42:30 -0700111 logger.log_exc("Exception in observer run loop")
112 traceback.print_exc()
Tony Mackc59fcaf2013-06-10 11:14:04 -0400113
Tony Mackf8d4a462013-06-10 23:25:30 -0400114 def sync_roles(self):
115 """
116 save all role that don't already exist in keystone. Remove keystone roles that
117 don't exist in planetstack
118 """
119 # sync all roles that don't already in keystone
120 keystone_roles = self.manager.driver.shell.keystone.roles.findall()
121 keystone_role_names = [kr.name for kr in keystone_roles]
122 pending_roles = Role.objects.all()
123 pending_role_names = [r.role_type for r in pending_roles]
124 for role in pending_roles:
125 if role.role_type not in keystone_role_names:
Tony Mack79a49c82013-06-15 23:51:57 -0400126 try:
127 self.manager.save_role(role)
128 logger.info("save role: %s" % (role))
129 except:
130 logger.log_exc("save role failed: %s" % role)
131 traceback.print_exc()
Tony Mackf8d4a462013-06-10 23:25:30 -0400132
Tony Mack79a49c82013-06-15 23:51:57 -0400133 # don't delete roles for now
134 """
135 # delete keystone roles that don't exist in planetstack
Tony Mackf8d4a462013-06-10 23:25:30 -0400136 for keystone_role in keystone_roles:
137 if keystone_role.name == 'admin':
138 continue
139 if keystone_role.name not in pending_role_names:
Tony Mack79a49c82013-06-15 23:51:57 -0400140 try:
141 self.manager.driver.delete_role({id: keystone_role.id})
142 except:
143 traceback.print_exc()
144 """
Tony Mackf8d4a462013-06-10 23:25:30 -0400145
Tony Mackdb9be932013-06-10 13:42:42 -0400146 def sync_tenants(self):
Tony Mackc59fcaf2013-06-10 11:14:04 -0400147 """
Tony Mackdb9be932013-06-10 13:42:42 -0400148 Save all sites and sliceswhere enacted < updated or enacted == None.
149 Remove sites and slices that no don't exist in openstack db if they
150 have an enacted time (enacted != None).
Tony Mackc59fcaf2013-06-10 11:14:04 -0400151 """
152 # get all sites that need to be synced (enacted < updated or enacted is None)
153 pending_sites = Site.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
154 for site in pending_sites:
Tony Mack79a49c82013-06-15 23:51:57 -0400155 try:
156 self.manager.save_site(site)
157 logger.info("saved site %s" % site)
158 except:
159 logger.log_exc("save site failed: %s" % site)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400160
Tony Mackc59fcaf2013-06-10 11:14:04 -0400161 # get all slices that need to be synced (enacted < updated or enacted is None)
162 pending_slices = Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
163 for slice in pending_slices:
Tony Mack79a49c82013-06-15 23:51:57 -0400164 try:
165 self.manager.init_caller(slice.creator, slice.creator.site.login_base)
166 self.manager.save_slice(slice)
167 logger.info("saved slice %s" % slice)
168 except:
169 logger.log_exc("save slice failed: %s" % slice)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400170
Tony Mackdb9be932013-06-10 13:42:42 -0400171 # get all sites that where enacted != null. We can assume these sites
172 # have previously been synced and need to be checed for deletion.
173 sites = Site.objects.filter(enacted__isnull=False)
174 site_dict = {}
175 for site in sites:
176 site_dict[site.login_base] = site
177
Tony Mackc59fcaf2013-06-10 11:14:04 -0400178 # get all slices that where enacted != null. We can assume these slices
179 # have previously been synced and need to be checed for deletion.
180 slices = Slice.objects.filter(enacted__isnull=False)
181 slice_dict = {}
182 for slice in slices:
183 slice_dict[slice.name] = slice
184
185 # delete keystone tenants that don't have a site record
186 tenants = self.manager.driver.shell.keystone.tenants.findall()
Tony Mack79a49c82013-06-15 23:51:57 -0400187 system_tenants = ['admin','service']
Tony Mackc59fcaf2013-06-10 11:14:04 -0400188 for tenant in tenants:
Tony Mack586d5c92013-06-20 12:04:30 -0400189 if tenant.name in system_tenants:
Tony Mackdb9be932013-06-10 13:42:42 -0400190 continue
191 if tenant.name not in site_dict and tenant.name not in slice_dict:
Tony Mack79a49c82013-06-15 23:51:57 -0400192 try:
193 self.manager.driver.delete_tenant(tenant.id)
194 logger.info("deleted tenant: %s" % (tenant))
195 except:
196 logger.log_exc("delete tenant failed: %s" % tenant)
Tony Mackdb9be932013-06-10 13:42:42 -0400197
Tony Mackc59fcaf2013-06-10 11:14:04 -0400198
199 def sync_users(self):
200 """
201 save all users where enacted < updated or enacted == None. Remove users that
202 no don't exist in openstack db if they have an enacted time (enacted != None).
203 """
204 # get all users that need to be synced (enacted < updated or enacted is None)
205 pending_users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
206 for user in pending_users:
Tony Mack79a49c82013-06-15 23:51:57 -0400207 try:
208 self.manager.save_user(user)
209 logger.info("saved user: %s" % (user))
210 except:
211 logger.log_exc("save user failed: %s" %user)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400212
213 # get all users that where enacted != null. We can assume these users
214 # have previously been synced and need to be checed for deletion.
215 users = User.objects.filter(enacted__isnull=False)
216 user_dict = {}
217 for user in users:
218 user_dict[user.kuser_id] = user
219
220 # delete keystone users that don't have a user record
Tony Mack79a49c82013-06-15 23:51:57 -0400221 system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service']
Tony Mack0553f282013-06-10 22:54:50 -0400222 users = self.manager.driver.shell.keystone.users.findall()
Tony Mackc59fcaf2013-06-10 11:14:04 -0400223 for user in users:
Tony Mack79a49c82013-06-15 23:51:57 -0400224 if user.name in system_users:
Tony Mackdb9be932013-06-10 13:42:42 -0400225 continue
Tony Mackc59fcaf2013-06-10 11:14:04 -0400226 if user.id not in user_dict:
Tony Mack79a49c82013-06-15 23:51:57 -0400227 try:
228 #self.manager.driver.delete_user(user.id)
229 logger.info("deleted user: %s" % user)
230 except:
231 logger.log_exc("delete user failed: %s" % user)
232
233
234 def sync_user_tenant_roles(self):
235 """
236 Save all site privileges and slice memberships wheree enacted < updated or
237 enacted == None. Remove ones that don't exist in openstack db if they have
238 an enacted time (enacted != None).
239 """
240 # sync site privileges
241 pending_site_privileges = SitePrivilege.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
242 for site_priv in pending_site_privileges:
243 try:
244 self.manager.save_site_privilege(site_priv)
245 logger.info("saved site privilege: %s" % (site_priv))
246 except: logger.log_exc("save site privilege failed: %s " % site_priv)
247
248 # sync slice memberships
249 pending_slice_memberships = SliceMembership.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
250 for slice_memb in pending_slice_memberships:
251 try:
252 self.manager.save_slice_membership(slice_memb)
253 logger.info("saved slice membership: %s" % (slice_memb))
254 except: logger.log_exc("save slice membership failed: %s" % slice_memb)
255
256 # get all site privileges and slice memberships that have been enacted
257 user_tenant_roles = defaultdict(list)
258 for site_priv in SitePrivilege.objects.filter(enacted__isnull=False):
259 user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role)
260 for slice_memb in SliceMembership.objects.filter(enacted__isnull=False):
261 user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role)
262
263 # Some user tenant role aren't stored in planetstack but they must be preserved.
264 # Role that fall in this category are
265 # 1. Never remove a user's role that their home site
266 # 2. Never remove a user's role at a slice they've created.
267 # Keep track of all roles that must be preserved.
268 users = User.objects.all()
269 preserved_roles = {}
270 for user in users:
271 tenant_ids = [s['tenant_id'] for s in user.slices.values()]
272 tenant_ids.append(user.site.tenant_id)
273 preserved_roles[user.kuser_id] = tenant_ids
274
275
276 # begin removing user tenant roles from keystone. This is stored in the
277 # Metadata table.
278 for metadata in self.manager.driver.shell.keystone_db.get_metadata():
279 # skip admin roles
280 if metadata.user_id == self.manager.driver.admin_user.id:
281 continue
282 # skip preserved tenant ids
283 if metadata.user_id in preserved_roles and \
284 metadata.tenant_id in preserved_roles[metadata.user_id]:
285 continue
286 # get roles for user at this tenant
287 user_tenant_role_ids = user_tenant_roles.get((metadata.user_id, metadata.tenant_id), [])
288
289 if user_tenant_role_ids:
290 # The user has roles at the tenant. Check if roles need to
291 # be updated.
292 user_keystone_role_ids = metadata.data.get('roles', [])
293 for role_id in user_keystone_role_ids:
294 if role_id not in user_tenant_role_ids:
295 user_keystone_role_ids.pop(user_keystone_role_ids.index(role_id))
296 else:
297 # The user has no roles at this tenant.
298 metadata.data['roles'] = []
299 #session.add(metadata)
300 logger.info("pruning metadata for %s at %s" % (metadata.user_id, metadata.tenant_id))
301
Tony Mackc59fcaf2013-06-10 11:14:04 -0400302 def sync_slivers(self):
303 """
304 save all slivers where enacted < updated or enacted == None. Remove slivers that
305 no don't exist in openstack db if they have an enacted time (enacted != None).
306 """
307 # get all users that need to be synced (enacted < updated or enacted is None)
308 pending_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
309 for sliver in pending_slivers:
Tony Mack9b7a8bd2013-06-24 15:08:01 -0400310 if sliver.creator:
Tony Mack79a49c82013-06-15 23:51:57 -0400311 try:
312 # update manager context
313 self.manager.init_caller(sliver.creator, sliver.slice.name)
314 self.manager.save_sliver(sliver)
Tony Macke3f8d862013-07-01 20:40:55 -0400315 logger.info("saved sliver: %s" % (sliver))
Tony Mack79a49c82013-06-15 23:51:57 -0400316 except:
317 logger.log_exc("save sliver failed: %s" % sliver)
Tony Mackc59fcaf2013-06-10 11:14:04 -0400318
Tony Mackaa8ad5f2013-06-24 13:50:06 -0400319 # get all slivers where enacted != null. We can assume these users
Tony Mackc59fcaf2013-06-10 11:14:04 -0400320 # have previously been synced and need to be checed for deletion.
321 slivers = Sliver.objects.filter(enacted__isnull=False)
322 sliver_dict = {}
323 for sliver in slivers:
324 sliver_dict[sliver.instance_id] = sliver
325
326 # delete sliver that don't have a sliver record
Scott Bakerf4df9522013-08-19 17:56:45 -0700327 ctx = self.manager.driver.shell.nova_db.ctx
Tony Mackc59fcaf2013-06-10 11:14:04 -0400328 instances = self.manager.driver.shell.nova_db.instance_get_all(ctx)
329 for instance in instances:
Tony Mackaa8ad5f2013-06-24 13:50:06 -0400330 if instance.uuid not in sliver_dict:
Tony Mack79a49c82013-06-15 23:51:57 -0400331 try:
332 # lookup tenant and update context
333 tenant = self.manager.driver.shell.keystone.tenants.find(id=instance.project_id)
334 self.manager.init_admin(tenant=tenant.name)
Tony Mackaa8ad5f2013-06-24 13:50:06 -0400335 self.manager.driver.destroy_instance(instance.uuid)
Tony Mack79a49c82013-06-15 23:51:57 -0400336 logger.info("destroyed sliver: %s" % (instance))
337 except:
338 logger.log_exc("destroy sliver failed: %s" % instance)
339
340
341 def sync_sliver_ips(self):
342 # fill in null ip addresses
343 slivers = Sliver.objects.filter(ip=None)
344 for sliver in slivers:
345 # update connection
346 self.manager.init_admin(tenant=sliver.slice.name)
Tony Mack70780e92013-06-30 12:46:16 -0400347 servers = self.manager.driver.shell.nova.servers.findall(id=sliver.instance_id)
Tony Mack79a49c82013-06-15 23:51:57 -0400348 if not servers:
349 continue
350 server = servers[0]
351 ips = server.addresses.get(sliver.slice.name, [])
352 if not ips:
353 continue
354 sliver.ip = ips[0]['addr']
355 sliver.save()
356 logger.info("saved sliver ip: %s %s" % (sliver, ips[0]))
Tony Mack70780e92013-06-30 12:46:16 -0400357
358 def sync_external_routes(self):
359 routes = self.manager.driver.get_external_routes()
Tony Mack16f51e02013-07-01 10:33:29 -0400360 subnets = self.manager.driver.shell.quantum.list_subnets()['subnets']
Tony Mack70780e92013-06-30 12:46:16 -0400361 for subnet in subnets:
Scott Baker78c87be2013-08-09 15:42:30 -0700362 try:
363 self.manager.driver.add_external_route(subnet, routes)
364 except:
Tony Mack16f51e02013-07-01 10:33:29 -0400365 logger.log_exc("failed to add external route for subnet %s" % subnet)
Scott Baker78c87be2013-08-09 15:42:30 -0700366
Scott Bakerf4df9522013-08-19 17:56:45 -0700367 def sync_network_slivers(self):
368 networkSlivers = NetworkSliver.objects.all()
369 networkSlivers_by_id = {}
370 networkSlivers_by_port = {}
371 for networkSliver in networkSlivers:
372 networkSlivers_by_id[networkSliver.id] = networkSliver
373 networkSlivers_by_port[networkSliver.port_id] = networkSliver
374
375 networks = Network.objects.all()
376 networks_by_id = {}
377 for network in networks:
378 networks_by_id[network.network_id] = network
379
380 slivers = Sliver.objects.all()
381 slivers_by_instance_id = {}
382 for sliver in slivers:
383 slivers_by_instance_id[sliver.instance_id] = sliver
384
385 ports = self.manager.driver.shell.quantum.list_ports()["ports"]
386 for port in ports:
Scott Bakerf4df9522013-08-19 17:56:45 -0700387 if port["id"] in networkSlivers_by_port:
388 # we already have it
389 print "already accounted for port", port["id"]
390 continue
391
392 if port["device_owner"] != "compute:nova":
393 # we only want the ports that connect to instances
394 continue
395
396 network = networks_by_id.get(port['network_id'], None)
397 if not network:
398 #print "no network for port", port["id"], "network", port["network_id"]
399 continue
400
401 sliver = slivers_by_instance_id.get(port['device_id'], None)
402 if not sliver:
403 print "no sliver for port", port["id"], "device_id", port['device_id']
404 continue
405
406 if network.template.sharedNetworkId is not None:
407 # If it's a shared network template, then more than one network
408 # object maps to the quantum network. We have to do a whole bunch
409 # of extra work to find the right one.
410 networks = network.template.network_set.all()
411 network = None
412 for candidate_network in networks:
413 if (candidate_network.owner == sliver.slice):
414 print "found network", candidate_network
415 network = candidate_network
416
417 if not network:
418 print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"]
419 continue
420
421 if not port["fixed_ips"]:
422 print "port", port["id"], "has no fixed_ips"
423 continue
424
Scott Baker1f299722013-08-19 17:59:26 -0700425# print "XXX", port
Scott Bakerf4df9522013-08-19 17:56:45 -0700426
427 ns = NetworkSliver(network=network,
428 sliver=sliver,
429 ip=port["fixed_ips"][0]["ip_address"],
430 port_id=port["id"])
431 ns.save()
432
Scott Baker78c87be2013-08-09 15:42:30 -0700433 def sync_networks(self):
434 """
435 save all networks where enacted < updated or enacted == None. Remove networks that
436 no don't exist in openstack db if they have an enacted time (enacted != None).
437 """
438 # get all users that need to be synced (enacted < updated or enacted is None)
439 pending_networks = Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
440 for network in pending_networks:
441 if network.owner and network.owner.creator:
442 try:
443 # update manager context
444 self.manager.init_caller(network.owner.creator, network.owner.name)
445 self.manager.save_network(network)
446 logger.info("saved network: %s" % (network))
447 except:
448 logger.log_exc("save network failed: %s" % network)
449
450 # get all networks where enacted != null. We can assume these users
451 # have previously been synced and need to be checed for deletion.
452 networks = Network.objects.filter(enacted__isnull=False)
453 network_dict = {}
454 for network in networks:
455 network_dict[network.network_id] = network
456
457 # TODO: delete Network objects if quantum network doesn't exist
458 # (need to write self.manager.driver.shell.quantum_db)
459