Sapan Bhatia | f68688d | 2013-09-03 11:45:46 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from planetstack.config import Config |
| 4 | |
| 5 | class GarbageCollector(SyncStep): |
| 6 | requested_interval = 86400 |
| 7 | provides=[] |
| 8 | |
| 9 | def call(self): |
| 10 | pass |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 11 | |
| 12 | def run(self): |
| 13 | try: |
| 14 | logger.info('gc start') |
| 15 | #self.sync_roles() |
| 16 | self.gc_tenants() |
| 17 | self.gc_users() |
| 18 | self.gc_user_tenant_roles() |
| 19 | self.gc_slivers() |
| 20 | self.gc_sliver_ips() |
| 21 | self.gc_external_routes() |
| 22 | except: |
| 23 | traceback.print_exc() |
| 24 | |
| 25 | def gc_roles(self): |
| 26 | """ |
| 27 | all role that don't already exist in keystone. Remove keystone roles that |
| 28 | don't exist in planetstack |
| 29 | """ |
| 30 | # sync all roles that don't already in keystone |
| 31 | keystone_roles = self.driver.shell.keystone.roles.findall() |
| 32 | keystone_role_names = [kr.name for kr in keystone_roles] |
| 33 | pending_roles = Role.objects.all() |
| 34 | pending_role_names = [r.role_type for r in pending_roles] |
| 35 | # don't delete roles for now |
| 36 | """ |
| 37 | # delete keystone roles that don't exist in planetstack |
| 38 | for keystone_role in keystone_roles: |
| 39 | if keystone_role.name == 'admin': |
| 40 | continue |
| 41 | if keystone_role.name not in pending_role_names: |
| 42 | try: |
| 43 | self.manager.driver.delete_role({id: keystone_role.id}) |
| 44 | except: |
| 45 | traceback.print_exc() |
| 46 | """ |
| 47 | |
| 48 | def gc_tenants(self): |
| 49 | """ |
| 50 | Remove sites and slices that no don't exist in openstack db if they |
| 51 | have an enacted time (enacted != None). |
| 52 | """ |
| 53 | # get all sites that where enacted != null. We can assume these sites |
| 54 | # have previously been synced and need to be checed for deletion. |
| 55 | sites = Site.objects.filter(enacted__isnull=False) |
| 56 | site_dict = {} |
| 57 | for site in sites: |
| 58 | site_dict[site.login_base] = site |
| 59 | |
| 60 | # get all slices that where enacted != null. We can assume these slices |
| 61 | # have previously been synced and need to be checed for deletion. |
| 62 | slices = Slice.objects.filter(enacted__isnull=False) |
| 63 | slice_dict = {} |
| 64 | for slice in slices: |
| 65 | slice_dict[slice.name] = slice |
| 66 | |
| 67 | # delete keystone tenants that don't have a site record |
| 68 | tenants = self.driver.shell.keystone.tenants.findall() |
| 69 | system_tenants = ['admin','service'] |
| 70 | for tenant in tenants: |
| 71 | if tenant.name in system_tenants: |
| 72 | continue |
| 73 | if tenant.name not in site_dict and tenant.name not in slice_dict: |
| 74 | try: |
| 75 | self.driver.delete_tenant(tenant.id) |
| 76 | logger.info("deleted tenant: %s" % (tenant)) |
| 77 | except: |
| 78 | logger.log_exc("delete tenant failed: %s" % tenant) |
| 79 | |
| 80 | |
| 81 | def gc_users(self): |
| 82 | """ |
| 83 | Remove users that no don't exist in openstack db if they have an |
| 84 | enacted time (enacted != None). |
| 85 | """ |
| 86 | # get all users that where enacted != null. We can assume these users |
| 87 | # have previously been synced and need to be checed for deletion. |
| 88 | users = User.objects.filter(enacted__isnull=False) |
| 89 | user_dict = {} |
| 90 | for user in users: |
| 91 | user_dict[user.kuser_id] = user |
| 92 | |
| 93 | # delete keystone users that don't have a user record |
| 94 | system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service'] |
| 95 | users = self.driver.shell.keystone.users.findall() |
| 96 | for user in users: |
| 97 | if user.name in system_users: |
| 98 | continue |
| 99 | if user.id not in user_dict: |
| 100 | try: |
| 101 | #self.manager.driver.delete_user(user.id) |
| 102 | logger.info("deleted user: %s" % user) |
| 103 | except: |
| 104 | logger.log_exc("delete user failed: %s" % user) |
| 105 | |
| 106 | |
| 107 | def gc_user_tenant_roles(self): |
| 108 | """ |
| 109 | Remove roles that don't exist in openstack db if they have |
| 110 | an enacted time (enacted != None). |
| 111 | """ |
| 112 | # get all site privileges and slice memberships that have been enacted |
| 113 | user_tenant_roles = defaultdict(list) |
| 114 | for site_priv in SitePrivilege.objects.filter(enacted__isnull=False): |
| 115 | user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role) |
| 116 | for slice_memb in SliceMembership.objects.filter(enacted__isnull=False): |
| 117 | user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role) |
| 118 | |
| 119 | # Some user tenant role aren't stored in planetstack but they must be preserved. |
| 120 | # Role that fall in this category are |
| 121 | # 1. Never remove a user's role that their home site |
| 122 | # 2. Never remove a user's role at a slice they've created. |
| 123 | # Keep track of all roles that must be preserved. |
| 124 | users = User.objects.all() |
| 125 | preserved_roles = {} |
| 126 | for user in users: |
| 127 | tenant_ids = [s['tenant_id'] for s in user.slices.values()] |
| 128 | tenant_ids.append(user.site.tenant_id) |
| 129 | preserved_roles[user.kuser_id] = tenant_ids |
| 130 | |
| 131 | |
| 132 | # begin removing user tenant roles from keystone. This is stored in the |
| 133 | # Metadata table. |
| 134 | for metadata in self.driver.shell.keystone_db.get_metadata(): |
| 135 | # skip admin roles |
| 136 | if metadata.user_id == self.manager.driver.admin_user.id: |
| 137 | continue |
| 138 | # skip preserved tenant ids |
| 139 | if metadata.user_id in preserved_roles and \ |
| 140 | metadata.tenant_id in preserved_roles[metadata.user_id]: |
| 141 | continue |
| 142 | # get roles for user at this tenant |
| 143 | user_tenant_role_ids = user_tenant_roles.get((metadata.user_id, metadata.tenant_id), []) |
| 144 | |
| 145 | if user_tenant_role_ids: |
| 146 | # The user has roles at the tenant. Check if roles need to |
| 147 | # be updated. |
| 148 | user_keystone_role_ids = metadata.data.get('roles', []) |
| 149 | for role_id in user_keystone_role_ids: |
| 150 | if role_id not in user_tenant_role_ids: |
| 151 | user_keystone_role_ids.pop(user_keystone_role_ids.index(role_id)) |
| 152 | else: |
| 153 | # The user has no roles at this tenant. |
| 154 | metadata.data['roles'] = [] |
| 155 | #session.add(metadata) |
| 156 | logger.info("pruning metadata for %s at %s" % (metadata.user_id, metadata.tenant_id)) |
| 157 | |
| 158 | def gc_slivers(self): |
| 159 | """ |
| 160 | Remove slivers that no don't exist in openstack db if they have |
| 161 | an enacted time (enacted != None). |
| 162 | """ |
| 163 | # get all slivers where enacted != null. We can assume these users |
| 164 | # have previously been synced and need to be checed for deletion. |
| 165 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 166 | sliver_dict = {} |
| 167 | for sliver in slivers: |
| 168 | sliver_dict[sliver.instance_id] = sliver |
| 169 | |
| 170 | # delete sliver that don't have a sliver record |
| 171 | ctx = self.driver.shell.nova_db.ctx |
| 172 | instances = self.driver.shell.nova_db.instance_get_all(ctx) |
| 173 | for instance in instances: |
| 174 | if instance.uuid not in sliver_dict: |
| 175 | try: |
| 176 | # lookup tenant and update context |
| 177 | tenant = self.driver.shell.keystone.tenants.find(id=instance.project_id) |
| 178 | driver = self.driver.client_driver(tenant=tenant.name) |
| 179 | driver.destroy_instance(instance.uuid) |
| 180 | logger.info("destroyed sliver: %s" % (instance)) |
| 181 | except: |
| 182 | logger.log_exc("destroy sliver failed: %s" % instance) |
| 183 | |
| 184 | |
| 185 | def gc_sliver_ips(self): |
| 186 | """ |
| 187 | Update ips that have changed. |
| 188 | """ |
| 189 | # fill in null ip addresses |
| 190 | slivers = Sliver.objects.filter(ip=None) |
| 191 | for sliver in slivers: |
| 192 | # update connection |
| 193 | self.manager.init_admin(tenant=sliver.slice.name) |
| 194 | servers = self.manager.driver.shell.nova.servers.findall(id=sliver.instance_id) |
| 195 | if not servers: |
| 196 | continue |
| 197 | server = servers[0] |
| 198 | ips = server.addresses.get(sliver.slice.name, []) |
| 199 | if ips and sliver.ip != ips[0]['addr']: |
| 200 | sliver.ip = ips[0]['addr'] |
| 201 | sliver.save() |
| 202 | logger.info("updated sliver ip: %s %s" % (sliver, ips[0])) |
| 203 | |
| 204 | def gc_external_routes(self): |
| 205 | pass |