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