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 |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 4 | from collections import defaultdict |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 5 | from core.models import * |
| 6 | from django.db.models import F, Q |
| 7 | from openstack.manager import OpenStackManager |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 8 | from util.logger import Logger, logging |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 9 | |
| 10 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 11 | logger = Logger(logfile='observer.log', level=logging.INFO) |
| 12 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 13 | class OpenStackObserver: |
| 14 | |
| 15 | def __init__(self): |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 16 | self.manager = OpenStackManager() |
| 17 | |
| 18 | def run(self): |
| 19 | if not self.manager.enabled or not self.manager.has_openstack: |
| 20 | return |
| 21 | while True: |
| 22 | try: |
Tony Mack | 6795fbe | 2013-06-11 10:10:24 -0400 | [diff] [blame] | 23 | #self.sync_roles() |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 24 | self.sync_tenants() |
| 25 | self.sync_users() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 26 | self.sync_user_tenant_roles() |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 27 | self.sync_slivers() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 28 | self.sync_sliver_ips() |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 29 | time.sleep(7) |
| 30 | except: |
| 31 | traceback.print_exc() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 32 | |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 33 | def sync_roles(self): |
| 34 | """ |
| 35 | save all role that don't already exist in keystone. Remove keystone roles that |
| 36 | don't exist in planetstack |
| 37 | """ |
| 38 | # sync all roles that don't already in keystone |
| 39 | keystone_roles = self.manager.driver.shell.keystone.roles.findall() |
| 40 | keystone_role_names = [kr.name for kr in keystone_roles] |
| 41 | pending_roles = Role.objects.all() |
| 42 | pending_role_names = [r.role_type for r in pending_roles] |
| 43 | for role in pending_roles: |
| 44 | if role.role_type not in keystone_role_names: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 45 | try: |
| 46 | self.manager.save_role(role) |
| 47 | logger.info("save role: %s" % (role)) |
| 48 | except: |
| 49 | logger.log_exc("save role failed: %s" % role) |
| 50 | traceback.print_exc() |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 51 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 52 | # don't delete roles for now |
| 53 | """ |
| 54 | # delete keystone roles that don't exist in planetstack |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 55 | for keystone_role in keystone_roles: |
| 56 | if keystone_role.name == 'admin': |
| 57 | continue |
| 58 | if keystone_role.name not in pending_role_names: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 59 | try: |
| 60 | self.manager.driver.delete_role({id: keystone_role.id}) |
| 61 | except: |
| 62 | traceback.print_exc() |
| 63 | """ |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 64 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 65 | def sync_tenants(self): |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 66 | """ |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 67 | Save all sites and sliceswhere enacted < updated or enacted == None. |
| 68 | Remove sites and slices that no don't exist in openstack db if they |
| 69 | have an enacted time (enacted != None). |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 70 | """ |
| 71 | # get all sites that need to be synced (enacted < updated or enacted is None) |
| 72 | pending_sites = Site.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 73 | for site in pending_sites: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 74 | try: |
| 75 | self.manager.save_site(site) |
| 76 | logger.info("saved site %s" % site) |
| 77 | except: |
| 78 | logger.log_exc("save site failed: %s" % site) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 79 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 80 | # get all slices that need to be synced (enacted < updated or enacted is None) |
| 81 | pending_slices = Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 82 | for slice in pending_slices: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 83 | try: |
| 84 | self.manager.init_caller(slice.creator, slice.creator.site.login_base) |
| 85 | self.manager.save_slice(slice) |
| 86 | logger.info("saved slice %s" % slice) |
| 87 | except: |
| 88 | logger.log_exc("save slice failed: %s" % slice) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 89 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 90 | # get all sites that where enacted != null. We can assume these sites |
| 91 | # have previously been synced and need to be checed for deletion. |
| 92 | sites = Site.objects.filter(enacted__isnull=False) |
| 93 | site_dict = {} |
| 94 | for site in sites: |
| 95 | site_dict[site.login_base] = site |
| 96 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 97 | # get all slices that where enacted != null. We can assume these slices |
| 98 | # have previously been synced and need to be checed for deletion. |
| 99 | slices = Slice.objects.filter(enacted__isnull=False) |
| 100 | slice_dict = {} |
| 101 | for slice in slices: |
| 102 | slice_dict[slice.name] = slice |
| 103 | |
| 104 | # delete keystone tenants that don't have a site record |
| 105 | tenants = self.manager.driver.shell.keystone.tenants.findall() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 106 | system_tenants = ['admin','service'] |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 107 | for tenant in tenants: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 108 | if tenant.name == 'admin': |
| 109 | continue |
| 110 | if tenant.name not in site_dict and tenant.name not in slice_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 111 | try: |
| 112 | self.manager.driver.delete_tenant(tenant.id) |
| 113 | logger.info("deleted tenant: %s" % (tenant)) |
| 114 | except: |
| 115 | logger.log_exc("delete tenant failed: %s" % tenant) |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 116 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 117 | |
| 118 | def sync_users(self): |
| 119 | """ |
| 120 | save all users where enacted < updated or enacted == None. Remove users that |
| 121 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 122 | """ |
| 123 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 124 | pending_users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 125 | for user in pending_users: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 126 | try: |
| 127 | self.manager.save_user(user) |
| 128 | logger.info("saved user: %s" % (user)) |
| 129 | except: |
| 130 | logger.log_exc("save user failed: %s" %user) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 131 | |
| 132 | # get all users that where enacted != null. We can assume these users |
| 133 | # have previously been synced and need to be checed for deletion. |
| 134 | users = User.objects.filter(enacted__isnull=False) |
| 135 | user_dict = {} |
| 136 | for user in users: |
| 137 | user_dict[user.kuser_id] = user |
| 138 | |
| 139 | # delete keystone users that don't have a user record |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 140 | system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service'] |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 141 | users = self.manager.driver.shell.keystone.users.findall() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 142 | for user in users: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 143 | if user.name in system_users: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 144 | continue |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 145 | if user.id not in user_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 146 | try: |
| 147 | #self.manager.driver.delete_user(user.id) |
| 148 | logger.info("deleted user: %s" % user) |
| 149 | except: |
| 150 | logger.log_exc("delete user failed: %s" % user) |
| 151 | |
| 152 | |
| 153 | def sync_user_tenant_roles(self): |
| 154 | """ |
| 155 | Save all site privileges and slice memberships wheree enacted < updated or |
| 156 | enacted == None. Remove ones that don't exist in openstack db if they have |
| 157 | an enacted time (enacted != None). |
| 158 | """ |
| 159 | # sync site privileges |
| 160 | pending_site_privileges = SitePrivilege.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 161 | for site_priv in pending_site_privileges: |
| 162 | try: |
| 163 | self.manager.save_site_privilege(site_priv) |
| 164 | logger.info("saved site privilege: %s" % (site_priv)) |
| 165 | except: logger.log_exc("save site privilege failed: %s " % site_priv) |
| 166 | |
| 167 | # sync slice memberships |
| 168 | pending_slice_memberships = SliceMembership.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 169 | for slice_memb in pending_slice_memberships: |
| 170 | try: |
| 171 | self.manager.save_slice_membership(slice_memb) |
| 172 | logger.info("saved slice membership: %s" % (slice_memb)) |
| 173 | except: logger.log_exc("save slice membership failed: %s" % slice_memb) |
| 174 | |
| 175 | # get all site privileges and slice memberships that have been enacted |
| 176 | user_tenant_roles = defaultdict(list) |
| 177 | for site_priv in SitePrivilege.objects.filter(enacted__isnull=False): |
| 178 | user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role) |
| 179 | for slice_memb in SliceMembership.objects.filter(enacted__isnull=False): |
| 180 | user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role) |
| 181 | |
| 182 | # Some user tenant role aren't stored in planetstack but they must be preserved. |
| 183 | # Role that fall in this category are |
| 184 | # 1. Never remove a user's role that their home site |
| 185 | # 2. Never remove a user's role at a slice they've created. |
| 186 | # Keep track of all roles that must be preserved. |
| 187 | users = User.objects.all() |
| 188 | preserved_roles = {} |
| 189 | for user in users: |
| 190 | tenant_ids = [s['tenant_id'] for s in user.slices.values()] |
| 191 | tenant_ids.append(user.site.tenant_id) |
| 192 | preserved_roles[user.kuser_id] = tenant_ids |
| 193 | |
| 194 | |
| 195 | # begin removing user tenant roles from keystone. This is stored in the |
| 196 | # Metadata table. |
| 197 | for metadata in self.manager.driver.shell.keystone_db.get_metadata(): |
| 198 | # skip admin roles |
| 199 | if metadata.user_id == self.manager.driver.admin_user.id: |
| 200 | continue |
| 201 | # skip preserved tenant ids |
| 202 | if metadata.user_id in preserved_roles and \ |
| 203 | metadata.tenant_id in preserved_roles[metadata.user_id]: |
| 204 | continue |
| 205 | # get roles for user at this tenant |
| 206 | user_tenant_role_ids = user_tenant_roles.get((metadata.user_id, metadata.tenant_id), []) |
| 207 | |
| 208 | if user_tenant_role_ids: |
| 209 | # The user has roles at the tenant. Check if roles need to |
| 210 | # be updated. |
| 211 | user_keystone_role_ids = metadata.data.get('roles', []) |
| 212 | for role_id in user_keystone_role_ids: |
| 213 | if role_id not in user_tenant_role_ids: |
| 214 | user_keystone_role_ids.pop(user_keystone_role_ids.index(role_id)) |
| 215 | else: |
| 216 | # The user has no roles at this tenant. |
| 217 | metadata.data['roles'] = [] |
| 218 | #session.add(metadata) |
| 219 | logger.info("pruning metadata for %s at %s" % (metadata.user_id, metadata.tenant_id)) |
| 220 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 221 | def sync_slivers(self): |
| 222 | """ |
| 223 | save all slivers where enacted < updated or enacted == None. Remove slivers that |
| 224 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 225 | """ |
| 226 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 227 | pending_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 228 | for sliver in pending_slivers: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 229 | if not sliver.instance_id and sliver.creator: |
| 230 | try: |
| 231 | # update manager context |
| 232 | self.manager.init_caller(sliver.creator, sliver.slice.name) |
| 233 | self.manager.save_sliver(sliver) |
| 234 | logger.info("saved sliver: %s %s" % (sliver)) |
| 235 | except: |
| 236 | logger.log_exc("save sliver failed: %s" % sliver) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 237 | |
| 238 | # get all slivers that where enacted != null. We can assume these users |
| 239 | # have previously been synced and need to be checed for deletion. |
| 240 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 241 | sliver_dict = {} |
| 242 | for sliver in slivers: |
| 243 | sliver_dict[sliver.instance_id] = sliver |
| 244 | |
| 245 | # delete sliver that don't have a sliver record |
| 246 | ctx = self.manager.driver.shell.nova_db.ctx |
| 247 | instances = self.manager.driver.shell.nova_db.instance_get_all(ctx) |
| 248 | for instance in instances: |
| 249 | if instance.id not in sliver_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame^] | 250 | try: |
| 251 | # lookup tenant and update context |
| 252 | tenant = self.manager.driver.shell.keystone.tenants.find(id=instance.project_id) |
| 253 | self.manager.init_admin(tenant=tenant.name) |
| 254 | self.manager.driver.destroy_instance(instance.id) |
| 255 | logger.info("destroyed sliver: %s" % (instance)) |
| 256 | except: |
| 257 | logger.log_exc("destroy sliver failed: %s" % instance) |
| 258 | |
| 259 | |
| 260 | def sync_sliver_ips(self): |
| 261 | # fill in null ip addresses |
| 262 | slivers = Sliver.objects.filter(ip=None) |
| 263 | for sliver in slivers: |
| 264 | # update connection |
| 265 | self.manager.init_admin(tenant=sliver.slice.name) |
| 266 | servers = self.manager.client.nova.servers.findall(id=sliver.instance_id) |
| 267 | if not servers: |
| 268 | continue |
| 269 | server = servers[0] |
| 270 | ips = server.addresses.get(sliver.slice.name, []) |
| 271 | if not ips: |
| 272 | continue |
| 273 | sliver.ip = ips[0]['addr'] |
| 274 | sliver.save() |
| 275 | logger.info("saved sliver ip: %s %s" % (sliver, ips[0])) |