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