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