Sapan Bhatia | f68688d | 2013-09-03 11:45:46 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | a76b895 | 2013-10-05 23:36:30 -0400 | [diff] [blame] | 3 | import traceback |
| 4 | from collections import defaultdict |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 5 | from django.db.models import F, Q |
Sapan Bhatia | f68688d | 2013-09-03 11:45:46 -0400 | [diff] [blame] | 6 | from planetstack.config import Config |
Tony Mack | a76b895 | 2013-10-05 23:36:30 -0400 | [diff] [blame] | 7 | from util.logger import Logger, logging |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 8 | from observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 9 | from deployment_auth import deployment_auth |
Tony Mack | a76b895 | 2013-10-05 23:36:30 -0400 | [diff] [blame] | 10 | from core.models import * |
| 11 | |
Andy Bavier | 04111b7 | 2013-10-22 16:47:10 -0400 | [diff] [blame] | 12 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | f68688d | 2013-09-03 11:45:46 -0400 | [diff] [blame] | 13 | |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 14 | class GarbageCollector(OpenStackSyncStep): |
| 15 | requested_interval = 86400 |
| 16 | provides=[] |
Sapan Bhatia | f68688d | 2013-09-03 11:45:46 -0400 | [diff] [blame] | 17 | |
Tony Mack | b0e03bf | 2013-10-05 20:52:13 -0400 | [diff] [blame] | 18 | def call(self, **args): |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 19 | try: |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 20 | #self.gc_roles() |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 21 | self.gc_tenants() |
| 22 | self.gc_users() |
| 23 | self.gc_user_tenant_roles() |
| 24 | self.gc_slivers() |
| 25 | self.gc_sliver_ips() |
| 26 | self.gc_external_routes() |
| 27 | except: |
| 28 | traceback.print_exc() |
| 29 | |
| 30 | def gc_roles(self): |
| 31 | """ |
| 32 | all role that don't already exist in keystone. Remove keystone roles that |
| 33 | don't exist in planetstack |
| 34 | """ |
| 35 | # sync all roles that don't already in keystone |
| 36 | keystone_roles = self.driver.shell.keystone.roles.findall() |
| 37 | keystone_role_names = [kr.name for kr in keystone_roles] |
| 38 | pending_roles = Role.objects.all() |
| 39 | pending_role_names = [r.role_type for r in pending_roles] |
| 40 | # don't delete roles for now |
| 41 | """ |
| 42 | # delete keystone roles that don't exist in planetstack |
| 43 | for keystone_role in keystone_roles: |
| 44 | if keystone_role.name == 'admin': |
| 45 | continue |
| 46 | if keystone_role.name not in pending_role_names: |
| 47 | try: |
Tony Mack | a76b895 | 2013-10-05 23:36:30 -0400 | [diff] [blame] | 48 | self.driver.delete_role({id: keystone_role.id}) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 49 | except: |
| 50 | traceback.print_exc() |
| 51 | """ |
| 52 | |
| 53 | def gc_tenants(self): |
| 54 | """ |
| 55 | Remove sites and slices that no don't exist in openstack db if they |
| 56 | have an enacted time (enacted != None). |
| 57 | """ |
| 58 | # get all sites that where enacted != null. We can assume these sites |
| 59 | # have previously been synced and need to be checed for deletion. |
| 60 | sites = Site.objects.filter(enacted__isnull=False) |
| 61 | site_dict = {} |
| 62 | for site in sites: |
| 63 | site_dict[site.login_base] = site |
| 64 | |
| 65 | # get all slices that where enacted != null. We can assume these slices |
| 66 | # have previously been synced and need to be checed for deletion. |
| 67 | slices = Slice.objects.filter(enacted__isnull=False) |
| 68 | slice_dict = {} |
| 69 | for slice in slices: |
| 70 | slice_dict[slice.name] = slice |
| 71 | |
| 72 | # delete keystone tenants that don't have a site record |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 73 | for deployment in deployment_auth: |
| 74 | driver = self.driver.admin_driver(deployment=deployment) |
| 75 | tenants = driver.shell.keystone.tenants.findall() |
| 76 | |
| 77 | system_tenants = ['admin','service', 'invisible_to_admin'] |
| 78 | for tenant in tenants: |
| 79 | if tenant.name in system_tenants: |
| 80 | continue |
| 81 | if tenant.name not in site_dict and tenant.name not in slice_dict: |
| 82 | try: |
| 83 | driver.delete_tenant(tenant.id) |
| 84 | logger.info("deleted tenant: %s" % (tenant)) |
| 85 | except: |
| 86 | logger.log_exc("delete tenant failed: %s" % tenant) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def gc_users(self): |
| 90 | """ |
| 91 | Remove users that no don't exist in openstack db if they have an |
| 92 | enacted time (enacted != None). |
| 93 | """ |
| 94 | # get all users that where enacted != null. We can assume these users |
| 95 | # have previously been synced and need to be checed for deletion. |
| 96 | users = User.objects.filter(enacted__isnull=False) |
| 97 | user_dict = {} |
| 98 | for user in users: |
| 99 | user_dict[user.kuser_id] = user |
| 100 | |
| 101 | # delete keystone users that don't have a user record |
Tony Mack | 9b6dde8 | 2013-10-06 11:05:49 -0400 | [diff] [blame] | 102 | system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service', 'demo'] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 103 | for deployment in deployment_auth: |
| 104 | driver = self.driver.admin_driver(deployment=deployment) |
| 105 | users = driver.shell.keystone.users.findall() |
| 106 | for user in users: |
| 107 | if user.name in system_users: |
| 108 | continue |
| 109 | if user.id not in user_dict: |
| 110 | try: |
| 111 | self.driver.delete_user(user.id) |
| 112 | logger.info("deleted user: %s" % user) |
| 113 | except: |
| 114 | logger.log_exc("delete user failed: %s" % user) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def gc_user_tenant_roles(self): |
| 118 | """ |
| 119 | Remove roles that don't exist in openstack db if they have |
| 120 | an enacted time (enacted != None). |
| 121 | """ |
| 122 | # get all site privileges and slice memberships that have been enacted |
| 123 | user_tenant_roles = defaultdict(list) |
| 124 | for site_priv in SitePrivilege.objects.filter(enacted__isnull=False): |
| 125 | user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role) |
Tony Mack | a76b895 | 2013-10-05 23:36:30 -0400 | [diff] [blame] | 126 | for slice_memb in SlicePrivilege.objects.filter(enacted__isnull=False): |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 127 | user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role) |
| 128 | |
| 129 | # Some user tenant role aren't stored in planetstack but they must be preserved. |
| 130 | # Role that fall in this category are |
| 131 | # 1. Never remove a user's role that their home site |
| 132 | # 2. Never remove a user's role at a slice they've created. |
| 133 | # Keep track of all roles that must be preserved. |
| 134 | users = User.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 135 | for deployment in deployment_auth: |
| 136 | driver = self.driver.admin_driver(deployment=deployment) |
| 137 | tenants = driver.shell.keystone.tenants.list() |
| 138 | for user in users: |
| 139 | # skip admin roles |
| 140 | if user.kuser_id == self.driver.admin_user.id: |
| 141 | continue |
| 142 | |
| 143 | ignore_tenant_ids = [] |
| 144 | k_user = driver.shell.keystone.users.find(id=user.kuser_id) |
| 145 | ignore_tenant_ids = [s['tenant_id'] for s in user.slices.values()] |
| 146 | if user.site: |
| 147 | ignore_tenant_ids.append(user.site.tenant_id) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 148 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 149 | # get user roles in keystone |
| 150 | for tenant in tenants: |
| 151 | # skip preserved tenant ids |
| 152 | if tenant.tenant_id in ignore_tenant_ids: |
| 153 | continue |
| 154 | # compare user tenant roles |
| 155 | user_tenant_role_ids = user_tenant_roles.get((user.kuser_id, tenant.id), []) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 156 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 157 | if user_tenant_role_ids: |
| 158 | # The user has roles at the tenant. Check if roles need to |
| 159 | # be updated. |
| 160 | k_user_roles = driver.shell.keystone.roles.roles_for_user(k_user, tenant) |
| 161 | for k_user_role in k_user_roles: |
| 162 | if k_user_role.role_id not in user_tenant_role_ids: |
| 163 | driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant) |
| 164 | logger.info("removed user role %s for %s at %s" % \ |
| 165 | (k_user_role, k_user.username, tenant.name)) |
| 166 | else: |
| 167 | # remove all roles the user has at the tenant. |
| 168 | for k_user_role in k_user_roles: |
| 169 | driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant) |
| 170 | logger.info("removed user role %s for %s at %s" % \ |
| 171 | (k_user_role, k_user.username, tenant.name)) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 172 | |
| 173 | def gc_slivers(self): |
| 174 | """ |
| 175 | Remove slivers that no don't exist in openstack db if they have |
| 176 | an enacted time (enacted != None). |
| 177 | """ |
| 178 | # get all slivers where enacted != null. We can assume these users |
| 179 | # have previously been synced and need to be checed for deletion. |
| 180 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 181 | sliver_dict = {} |
| 182 | for sliver in slivers: |
| 183 | sliver_dict[sliver.instance_id] = sliver |
| 184 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 185 | for tenant in self.driver.shell.keystone.tenants.list(): |
| 186 | # delete sliver that don't have a sliver record |
| 187 | tenant_driver = self.driver.client_driver(tenant=tenant.name, deployment=sliver.node.deployment) |
| 188 | for instance in tenant_driver.nova.servers.list(): |
| 189 | if instance.uuid not in sliver_dict: |
| 190 | try: |
| 191 | tenant_driver.destroy_instance(instance.uuid) |
| 192 | logger.info("destroyed sliver: %s" % (instance)) |
| 193 | except: |
| 194 | logger.log_exc("destroy sliver failed: %s" % instance) |
| 195 | |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 196 | |
| 197 | def gc_sliver_ips(self): |
| 198 | """ |
| 199 | Update ips that have changed. |
| 200 | """ |
| 201 | # fill in null ip addresses |
| 202 | slivers = Sliver.objects.filter(ip=None) |
| 203 | for sliver in slivers: |
| 204 | # update connection |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 205 | |
| 206 | driver = self.driver.client_driver(tenant=sliver.slice.name, deployment=sliver.node.deployment) |
Tony Mack | a76b895 | 2013-10-05 23:36:30 -0400 | [diff] [blame] | 207 | servers = driver.shell.nova.servers.findall(id=sliver.instance_id) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 208 | if not servers: |
| 209 | continue |
| 210 | server = servers[0] |
| 211 | ips = server.addresses.get(sliver.slice.name, []) |
| 212 | if ips and sliver.ip != ips[0]['addr']: |
| 213 | sliver.ip = ips[0]['addr'] |
| 214 | sliver.save() |
| 215 | logger.info("updated sliver ip: %s %s" % (sliver, ips[0])) |
| 216 | |
| 217 | def gc_external_routes(self): |
| 218 | pass |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 219 | |
| 220 | def gc_nodes(self): |
| 221 | # collect local nodes |
| 222 | nodes = Node.objects.all() |
| 223 | nodes_dict = {} |
| 224 | for node in nodes: |
| 225 | nodes_dict[node.name] = node |
| 226 | |
| 227 | # collect nova nodes: |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 228 | compute_nodes_dict = {} |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 229 | for deployment in deployment_auth: |
| 230 | driver = self.driver.admin_driver(deployment=deployment) |
| 231 | compute_nodes = driver.nova.hypervisors.list() |
| 232 | for compute_node in compute_nodes: |
| 233 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 234 | |
| 235 | # remove old nodes |
| 236 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 237 | Node.objects.filter(name__in=old_node_names).delete() |
| 238 | |
| 239 | def gc_images(self): |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 240 | # collect local images |
| 241 | images = Image.objects.all() |
| 242 | images_dict = {} |
| 243 | for image in images: |
| 244 | images_dict[image.name] = image |
| 245 | |
| 246 | # collect glance images |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 247 | glance_images_dict = {} |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame^] | 248 | for deployment in deployment_auth: |
| 249 | driver = self.driver.admin_driver(deployment=deployment) |
| 250 | glance_images = driver.shell.glance.get_images() |
| 251 | for glance_image in glance_images: |
| 252 | glance_images_dict[glance_image['name']] = glance_image |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 253 | |
| 254 | # remove old images |
| 255 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 256 | Image.objects.filter(name__in=old_image_names).delete() |