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: |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame^] | 19 | self.sync_roles() |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 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 | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame^] | 28 | def sync_roles(self): |
| 29 | """ |
| 30 | save all role that don't already exist in keystone. Remove keystone roles that |
| 31 | don't exist in planetstack |
| 32 | """ |
| 33 | # sync all roles that don't already in keystone |
| 34 | keystone_roles = self.manager.driver.shell.keystone.roles.findall() |
| 35 | keystone_role_names = [kr.name for kr in keystone_roles] |
| 36 | pending_roles = Role.objects.all() |
| 37 | pending_role_names = [r.role_type for r in pending_roles] |
| 38 | for role in pending_roles: |
| 39 | if role.role_type not in keystone_role_names: |
| 40 | self.manager.save_role(role) |
| 41 | |
| 42 | |
| 43 | # delete keystone roles that don't exist in planetstack |
| 44 | for keystone_role in keystone_roles: |
| 45 | if keystone_role.name == 'admin': |
| 46 | continue |
| 47 | if keystone_role.name not in pending_role_names: |
| 48 | pass |
| 49 | #self.manager.driver.delete_role({id: keystone_role.id}) |
| 50 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 51 | def sync_tenants(self): |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 52 | """ |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 53 | Save all sites and sliceswhere enacted < updated or enacted == None. |
| 54 | Remove sites and slices that no don't exist in openstack db if they |
| 55 | have an enacted time (enacted != None). |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 56 | """ |
| 57 | # get all sites that need to be synced (enacted < updated or enacted is None) |
| 58 | pending_sites = Site.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 59 | for site in pending_sites: |
| 60 | self.manager.save_site(site) |
| 61 | site.enacted = datetime.now() |
| 62 | site.save() |
| 63 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 64 | # get all slices that need to be synced (enacted < updated or enacted is None) |
| 65 | pending_slices = Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 66 | for slice in pending_slices: |
| 67 | self.manager.save_slice(slice) |
| 68 | slice.enacted = datetime.now() |
| 69 | slice.save() |
| 70 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 71 | # get all sites that where enacted != null. We can assume these sites |
| 72 | # have previously been synced and need to be checed for deletion. |
| 73 | sites = Site.objects.filter(enacted__isnull=False) |
| 74 | site_dict = {} |
| 75 | for site in sites: |
| 76 | site_dict[site.login_base] = site |
| 77 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 78 | # get all slices that where enacted != null. We can assume these slices |
| 79 | # have previously been synced and need to be checed for deletion. |
| 80 | slices = Slice.objects.filter(enacted__isnull=False) |
| 81 | slice_dict = {} |
| 82 | for slice in slices: |
| 83 | slice_dict[slice.name] = slice |
| 84 | |
| 85 | # delete keystone tenants that don't have a site record |
| 86 | tenants = self.manager.driver.shell.keystone.tenants.findall() |
| 87 | for tenant in tenants: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 88 | if tenant.name == 'admin': |
| 89 | continue |
| 90 | 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] | 91 | #print "delete " + tenant.name |
| 92 | pass |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 93 | #self.manager.driver.delete_tenant(tenant.id) |
| 94 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 95 | |
| 96 | def sync_users(self): |
| 97 | """ |
| 98 | save all users where enacted < updated or enacted == None. Remove users that |
| 99 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 100 | """ |
| 101 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 102 | pending_users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 103 | for user in pending_users: |
| 104 | self.manager.save_user(user) |
| 105 | user.enacted = datetime.now() |
| 106 | user.save() |
| 107 | |
| 108 | # get all users that where enacted != null. We can assume these users |
| 109 | # have previously been synced and need to be checed for deletion. |
| 110 | users = User.objects.filter(enacted__isnull=False) |
| 111 | user_dict = {} |
| 112 | for user in users: |
| 113 | user_dict[user.kuser_id] = user |
| 114 | |
| 115 | # delete keystone users that don't have a user record |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 116 | users = self.manager.driver.shell.keystone.users.findall() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 117 | for user in users: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 118 | if user.name == 'admin': |
| 119 | continue |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 120 | if user.id not in user_dict: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 121 | pass |
| 122 | #self.manager.driver.delete_user(user.id) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 123 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 124 | |
| 125 | def sync_slivers(self): |
| 126 | """ |
| 127 | save all slivers where enacted < updated or enacted == None. Remove slivers that |
| 128 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 129 | """ |
| 130 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 131 | pending_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 132 | for sliver in pending_slivers: |
| 133 | if sliver.creator: |
| 134 | # update manager context |
| 135 | self.manager.init_caller(sliver.creator) |
| 136 | self.manager.save_sliver(sliver) |
| 137 | sliver.enacted = datetime.now() |
| 138 | sliver.save() |
| 139 | |
| 140 | # get all slivers that where enacted != null. We can assume these users |
| 141 | # have previously been synced and need to be checed for deletion. |
| 142 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 143 | sliver_dict = {} |
| 144 | for sliver in slivers: |
| 145 | sliver_dict[sliver.instance_id] = sliver |
| 146 | |
| 147 | # delete sliver that don't have a sliver record |
| 148 | ctx = self.manager.driver.shell.nova_db.ctx |
| 149 | instances = self.manager.driver.shell.nova_db.instance_get_all(ctx) |
| 150 | for instance in instances: |
| 151 | if instance.id not in sliver_dict: |
| 152 | # lookup tenant and update context |
| 153 | tenant = self.manager.driver.shell.keystone.tenants.findall(id=instance.tenant_id) |
| 154 | self.manager.init_admin(tenant=tenant.name) |
| 155 | self.manager.driver.destroy_instance(instance.id) |