Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 1 | import time |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame^] | 2 | import traceback |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 3 | from datetime import datetime |
| 4 | from core.models import * |
| 5 | from django.db.models import F, Q |
| 6 | from openstack.manager import OpenStackManager |
| 7 | |
| 8 | |
| 9 | class OpenStackObserver: |
| 10 | |
| 11 | def __init__(self): |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame^] | 12 | self.manager = OpenStackManager() |
| 13 | |
| 14 | def run(self): |
| 15 | if not self.manager.enabled or not self.manager.has_openstack: |
| 16 | return |
| 17 | while True: |
| 18 | try: |
| 19 | #self.sync_roles() |
| 20 | self.sync_tenants() |
| 21 | self.sync_users() |
| 22 | #self.sync_user_tenant_roles() |
| 23 | #self.sync_slivers() |
| 24 | time.sleep(7) |
| 25 | except: |
| 26 | traceback.print_exc() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 27 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 28 | def sync_tenants(self): |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 29 | """ |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 30 | Save all sites and sliceswhere enacted < updated or enacted == None. |
| 31 | Remove sites and slices that no don't exist in openstack db if they |
| 32 | have an enacted time (enacted != None). |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 33 | """ |
| 34 | # get all sites that need to be synced (enacted < updated or enacted is None) |
| 35 | pending_sites = Site.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 36 | for site in pending_sites: |
| 37 | self.manager.save_site(site) |
| 38 | site.enacted = datetime.now() |
| 39 | site.save() |
| 40 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 41 | # get all slices that need to be synced (enacted < updated or enacted is None) |
| 42 | pending_slices = Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 43 | for slice in pending_slices: |
| 44 | self.manager.save_slice(slice) |
| 45 | slice.enacted = datetime.now() |
| 46 | slice.save() |
| 47 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 48 | # get all sites that where enacted != null. We can assume these sites |
| 49 | # have previously been synced and need to be checed for deletion. |
| 50 | sites = Site.objects.filter(enacted__isnull=False) |
| 51 | site_dict = {} |
| 52 | for site in sites: |
| 53 | site_dict[site.login_base] = site |
| 54 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 55 | # get all slices that where enacted != null. We can assume these slices |
| 56 | # have previously been synced and need to be checed for deletion. |
| 57 | slices = Slice.objects.filter(enacted__isnull=False) |
| 58 | slice_dict = {} |
| 59 | for slice in slices: |
| 60 | slice_dict[slice.name] = slice |
| 61 | |
| 62 | # delete keystone tenants that don't have a site record |
| 63 | tenants = self.manager.driver.shell.keystone.tenants.findall() |
| 64 | for tenant in tenants: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 65 | if tenant.name == 'admin': |
| 66 | continue |
| 67 | if tenant.name not in site_dict and tenant.name not in slice_dict: |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame^] | 68 | #print "delete " + tenant.name |
| 69 | pass |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 70 | #self.manager.driver.delete_tenant(tenant.id) |
| 71 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 72 | |
| 73 | def sync_users(self): |
| 74 | """ |
| 75 | save all users where enacted < updated or enacted == None. Remove users that |
| 76 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 77 | """ |
| 78 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 79 | pending_users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 80 | for user in pending_users: |
| 81 | self.manager.save_user(user) |
| 82 | user.enacted = datetime.now() |
| 83 | user.save() |
| 84 | |
| 85 | # get all users that where enacted != null. We can assume these users |
| 86 | # have previously been synced and need to be checed for deletion. |
| 87 | users = User.objects.filter(enacted__isnull=False) |
| 88 | user_dict = {} |
| 89 | for user in users: |
| 90 | user_dict[user.kuser_id] = user |
| 91 | |
| 92 | # delete keystone users that don't have a user record |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame^] | 93 | users = self.manager.driver.shell.keystone.users.findall() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 94 | for user in users: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 95 | if user.name == 'admin': |
| 96 | continue |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 97 | if user.id not in user_dict: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 98 | pass |
| 99 | #self.manager.driver.delete_user(user.id) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 100 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 101 | |
| 102 | def sync_slivers(self): |
| 103 | """ |
| 104 | save all slivers where enacted < updated or enacted == None. Remove slivers that |
| 105 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 106 | """ |
| 107 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 108 | pending_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 109 | for sliver in pending_slivers: |
| 110 | if sliver.creator: |
| 111 | # update manager context |
| 112 | self.manager.init_caller(sliver.creator) |
| 113 | self.manager.save_sliver(sliver) |
| 114 | sliver.enacted = datetime.now() |
| 115 | sliver.save() |
| 116 | |
| 117 | # get all slivers that where enacted != null. We can assume these users |
| 118 | # have previously been synced and need to be checed for deletion. |
| 119 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 120 | sliver_dict = {} |
| 121 | for sliver in slivers: |
| 122 | sliver_dict[sliver.instance_id] = sliver |
| 123 | |
| 124 | # delete sliver that don't have a sliver record |
| 125 | ctx = self.manager.driver.shell.nova_db.ctx |
| 126 | instances = self.manager.driver.shell.nova_db.instance_get_all(ctx) |
| 127 | for instance in instances: |
| 128 | if instance.id not in sliver_dict: |
| 129 | # lookup tenant and update context |
| 130 | tenant = self.manager.driver.shell.keystone.tenants.findall(id=instance.tenant_id) |
| 131 | self.manager.init_admin(tenant=tenant.name) |
| 132 | self.manager.driver.destroy_instance(instance.id) |