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