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 | |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 12 | logger = Logger(logfile='/var/log/observer.log', 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 | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 20 | self.gc_networks() |
| 21 | #self.gc_user_tenant_roles() |
Tony Mack | fccb9eb | 2014-04-14 22:16:42 -0400 | [diff] [blame] | 22 | #self.gc_tenants() |
| 23 | #self.gc_users() |
Tony Mack | b61c81c | 2014-04-21 17:08:12 -0400 | [diff] [blame] | 24 | self.gc_slivers() |
Tony Mack | fccb9eb | 2014-04-14 22:16:42 -0400 | [diff] [blame] | 25 | #self.gc_sliver_ips() |
Tony Mack | fccb9eb | 2014-04-14 22:16:42 -0400 | [diff] [blame] | 26 | pass |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 27 | except: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 28 | traceback.print_exc() |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 29 | |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 30 | def gc_networks(self): |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 31 | """ |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 32 | Remove all neutron networks that do not exist in the planetstack db. |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 33 | """ |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 34 | # some networks cannot be deleted |
| 35 | system_networks = ['nat-net','private-admin'] |
| 36 | for network_template in NetworkTemplate.objects.all(): |
| 37 | if network_template.sharedNetworkName and \ |
| 38 | network_template.sharedNetworkName not in system_networks: |
| 39 | system_networks.append(network_template.sharedNetworkName) |
| 40 | |
| 41 | networks = Network.objects.filter(enacted__isnull=False) |
| 42 | networks_dict = {} |
| 43 | for network in networks: |
| 44 | networks_dict[network.name] = network |
| 45 | |
| 46 | # some deployments are at the same url. Keep track of the urls we've visited |
| 47 | # to make sure we aren't making redundant calls |
| 48 | completed_urls = [] |
| 49 | for deployment in deployment_auth: |
| 50 | # skip deployments that we've already processed |
| 51 | if deployment_auth[deployment]['url'] in completed_urls: |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 52 | continue |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 53 | try: |
| 54 | driver = self.driver.admin_driver(deployment=deployment) |
| 55 | neutron_networks = driver.shell.quantum.list_networks()['networks'] |
| 56 | for neutron_network in neutron_networks: |
| 57 | # skip system networks |
| 58 | if neutron_network['name'] in system_networks: |
| 59 | continue |
| 60 | if neutron_network['name'] not in networks_dict: |
| 61 | try: |
| 62 | logger.info("GarbageCollector: deleting network %s" % neutron_network['name']) |
| 63 | for subnet_id in neutron_network['subnets']: |
| 64 | driver.delete_subnet(subnet_id) |
| 65 | driver.delete_network(neutron_network['id']) |
| 66 | except: |
| 67 | logger.log_exc("GarbageCollector: delete network %s failed" % neutron_network['name']) |
| 68 | except: |
| 69 | logger.log_exc("GarbageCollector: Error at deployment %s" % deployment) |
| 70 | |
| 71 | completed_urls.append(deployment_auth[deployment]['url']) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 72 | |
| 73 | def gc_tenants(self): |
| 74 | """ |
| 75 | Remove sites and slices that no don't exist in openstack db if they |
| 76 | have an enacted time (enacted != None). |
| 77 | """ |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 78 | # some tenants cannot be deleted |
| 79 | system_tenants = ['admin','service', 'invisible_to_admin'] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 80 | # get all sites that where enacted != null. We can assume these sites |
| 81 | # have previously been synced and need to be checed for deletion. |
| 82 | sites = Site.objects.filter(enacted__isnull=False) |
| 83 | site_dict = {} |
| 84 | for site in sites: |
| 85 | site_dict[site.login_base] = site |
| 86 | |
| 87 | # get all slices that where enacted != null. We can assume these slices |
| 88 | # have previously been synced and need to be checed for deletion. |
| 89 | slices = Slice.objects.filter(enacted__isnull=False) |
| 90 | slice_dict = {} |
| 91 | for slice in slices: |
| 92 | slice_dict[slice.name] = slice |
| 93 | |
| 94 | # delete keystone tenants that don't have a site record |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 95 | # some deployments are at the same url. Keep track of the urls we've visited |
| 96 | # to make sure we aren't making redundant calls |
| 97 | completed_urls = [] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 98 | for deployment in deployment_auth: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 99 | # skip deployments that we've already processed |
| 100 | if deployment_auth[deployment]['url'] in completed_urls: |
| 101 | continue |
| 102 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 103 | driver = self.driver.admin_driver(deployment=deployment) |
| 104 | tenants = driver.shell.keystone.tenants.findall() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 105 | for tenant in tenants: |
| 106 | if tenant.name in system_tenants: |
| 107 | continue |
| 108 | if tenant.name not in site_dict and tenant.name not in slice_dict: |
| 109 | try: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 110 | logger.info("GarbageCollector: deleting tenant: %s" % (tenant)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 111 | driver.delete_tenant(tenant.id) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 112 | except: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 113 | logger.log_exc("GarbageCollector: delete tenant failed: %s" % tenant) |
| 114 | completed_urls.append(deployment_auth[deployment]['url']) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 115 | |
| 116 | def gc_users(self): |
| 117 | """ |
Tony Mack | f77f140 | 2014-04-02 07:06:53 -0400 | [diff] [blame] | 118 | Remove users that do not exist in openstack db if they have an |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 119 | enacted time (enacted != None). |
| 120 | """ |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 121 | # some users cannot be deleted |
| 122 | system_users = ['admin', 'nova', 'quantum', 'neutron' 'glance', \ |
| 123 | 'cinder', 'swift', 'service', 'demo'] |
| 124 | |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 125 | # get all users that where enacted != null. We can assume these users |
| 126 | # have previously been synced and need to be checed for deletion. |
| 127 | users = User.objects.filter(enacted__isnull=False) |
| 128 | user_dict = {} |
| 129 | for user in users: |
| 130 | user_dict[user.kuser_id] = user |
| 131 | |
| 132 | # delete keystone users that don't have a user record |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 133 | # some deployments are at the same url. Keep track of the urls we've visited |
| 134 | # to make sure we aren't making redundant calls |
| 135 | completed_urls = [] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 136 | for deployment in deployment_auth: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 137 | # skip deployments that we've already processed |
| 138 | if deployment_auth[deployment]['url'] in completed_urls: |
| 139 | continue |
| 140 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 141 | driver = self.driver.admin_driver(deployment=deployment) |
| 142 | users = driver.shell.keystone.users.findall() |
| 143 | for user in users: |
| 144 | if user.name in system_users: |
| 145 | continue |
| 146 | if user.id not in user_dict: |
| 147 | try: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 148 | logger.info("GarbageCollector: deleting user: %s" % user) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 149 | self.driver.delete_user(user.id) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 150 | except: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 151 | logger.log_exc("GarbageCollector: delete user failed: %s" % user) |
| 152 | completed_urls.append(deployment_auth[deployment]['url']) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 153 | |
| 154 | def gc_user_tenant_roles(self): |
| 155 | """ |
| 156 | Remove roles that don't exist in openstack db if they have |
| 157 | an enacted time (enacted != None). |
| 158 | """ |
| 159 | # get all site privileges and slice memberships that have been enacted |
| 160 | user_tenant_roles = defaultdict(list) |
| 161 | for site_priv in SitePrivilege.objects.filter(enacted__isnull=False): |
| 162 | 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] | 163 | for slice_memb in SlicePrivilege.objects.filter(enacted__isnull=False): |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 164 | user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role) |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 165 | |
| 166 | # some deployments are at the same url. Keep track of the urls we've visited |
| 167 | # to make sure we aren't making redundant calls |
| 168 | completed_urls = [] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 169 | # Some user tenant role aren't stored in planetstack but they must be preserved. |
| 170 | # Role that fall in this category are |
| 171 | # 1. Never remove a user's role that their home site |
| 172 | # 2. Never remove a user's role at a slice they've created. |
| 173 | # Keep track of all roles that must be preserved. |
| 174 | users = User.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 175 | for deployment in deployment_auth: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 176 | # skip deployments that we've already processed |
| 177 | if deployment_auth[deployment]['url'] in completed_urls: |
| 178 | continue |
| 179 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 180 | driver = self.driver.admin_driver(deployment=deployment) |
| 181 | tenants = driver.shell.keystone.tenants.list() |
| 182 | for user in users: |
| 183 | # skip admin roles |
| 184 | if user.kuser_id == self.driver.admin_user.id: |
| 185 | continue |
| 186 | |
| 187 | ignore_tenant_ids = [] |
| 188 | k_user = driver.shell.keystone.users.find(id=user.kuser_id) |
| 189 | ignore_tenant_ids = [s['tenant_id'] for s in user.slices.values()] |
| 190 | if user.site: |
| 191 | ignore_tenant_ids.append(user.site.tenant_id) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 192 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 193 | # get user roles in keystone |
| 194 | for tenant in tenants: |
| 195 | # skip preserved tenant ids |
| 196 | if tenant.tenant_id in ignore_tenant_ids: |
| 197 | continue |
| 198 | # compare user tenant roles |
| 199 | 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] | 200 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 201 | if user_tenant_role_ids: |
| 202 | # The user has roles at the tenant. Check if roles need to |
| 203 | # be updated. |
| 204 | k_user_roles = driver.shell.keystone.roles.roles_for_user(k_user, tenant) |
| 205 | for k_user_role in k_user_roles: |
| 206 | if k_user_role.role_id not in user_tenant_role_ids: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 207 | logger.info("GarbageCollector: removing user role %s for %s at %s" % \ |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 208 | (k_user_role, k_user.username, tenant.name)) |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 209 | driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 210 | else: |
| 211 | # remove all roles the user has at the tenant. |
| 212 | for k_user_role in k_user_roles: |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 213 | logger.info("GarbageCollector: removing user role %s for %s at %s" % \ |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 214 | (k_user_role, k_user.username, tenant.name)) |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 215 | driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant) |
| 216 | completed_urls.append(deployment_auth[deployment]['url']) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 217 | |
| 218 | def gc_slivers(self): |
| 219 | """ |
| 220 | Remove slivers that no don't exist in openstack db if they have |
| 221 | an enacted time (enacted != None). |
| 222 | """ |
| 223 | # get all slivers where enacted != null. We can assume these users |
| 224 | # have previously been synced and need to be checed for deletion. |
| 225 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 226 | sliver_dict = {} |
| 227 | for sliver in slivers: |
| 228 | sliver_dict[sliver.instance_id] = sliver |
| 229 | |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 230 | |
| 231 | # some deployments are at the same url. Keep track of the urls we've visited |
| 232 | # to make sure we aren't making redundant calls |
| 233 | completed_urls = [] |
| 234 | for deployment in deployment_auth: |
| 235 | # skip deployments that we've already processed |
| 236 | if deployment_auth[deployment]['url'] in completed_urls: |
Tony Mack | b61c81c | 2014-04-21 17:08:12 -0400 | [diff] [blame] | 237 | continue |
Tony Mack | adb5d12 | 2014-04-21 21:46:45 -0400 | [diff] [blame^] | 238 | |
| 239 | try: |
| 240 | driver = self.driver.admin_driver(deployment=deployment) |
| 241 | for tenant in driver.shell.keystone.tenants.list(): |
| 242 | if tenant.name in ['admin', 'services']: |
| 243 | continue |
| 244 | # delete sliver that don't have a sliver record |
| 245 | tenant_driver = self.driver.client_driver(tenant=tenant.name, deployment=deployment) |
| 246 | for instance in tenant_driver.shell.nova.servers.list(): |
| 247 | if instance.id not in sliver_dict: |
| 248 | try: |
| 249 | logger.info("GarbageCollector: destroying sliver: %s %s" % (instance, instance.id)) |
| 250 | tenant_driver.destroy_instance(instance.id) |
| 251 | except: |
| 252 | logger.log_exc("GarbageCollector: destroy sliver failed: %s" % instance) |
| 253 | except: |
| 254 | logger.log_exc("GarbageCollector: Error at deployment %s" % deployment) |
| 255 | completed_urls.append(deployment_auth[deployment]['url']) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 256 | |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 257 | |
| 258 | def gc_sliver_ips(self): |
| 259 | """ |
| 260 | Update ips that have changed. |
| 261 | """ |
| 262 | # fill in null ip addresses |
| 263 | slivers = Sliver.objects.filter(ip=None) |
| 264 | for sliver in slivers: |
| 265 | # update connection |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 266 | |
| 267 | 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] | 268 | servers = driver.shell.nova.servers.findall(id=sliver.instance_id) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 269 | if not servers: |
| 270 | continue |
| 271 | server = servers[0] |
| 272 | ips = server.addresses.get(sliver.slice.name, []) |
| 273 | if ips and sliver.ip != ips[0]['addr']: |
| 274 | sliver.ip = ips[0]['addr'] |
| 275 | sliver.save() |
| 276 | logger.info("updated sliver ip: %s %s" % (sliver, ips[0])) |
| 277 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 278 | def gc_nodes(self): |
| 279 | # collect local nodes |
| 280 | nodes = Node.objects.all() |
| 281 | nodes_dict = {} |
| 282 | for node in nodes: |
| 283 | nodes_dict[node.name] = node |
| 284 | |
| 285 | # collect nova nodes: |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 286 | compute_nodes_dict = {} |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 287 | for deployment in deployment_auth: |
| 288 | driver = self.driver.admin_driver(deployment=deployment) |
| 289 | compute_nodes = driver.nova.hypervisors.list() |
| 290 | for compute_node in compute_nodes: |
| 291 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 292 | |
| 293 | # remove old nodes |
| 294 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 295 | Node.objects.filter(name__in=old_node_names).delete() |
| 296 | |
| 297 | def gc_images(self): |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 298 | # collect local images |
| 299 | images = Image.objects.all() |
| 300 | images_dict = {} |
| 301 | for image in images: |
| 302 | images_dict[image.name] = image |
| 303 | |
| 304 | # collect glance images |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 305 | glance_images_dict = {} |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 306 | for deployment in deployment_auth: |
| 307 | driver = self.driver.admin_driver(deployment=deployment) |
| 308 | glance_images = driver.shell.glance.get_images() |
| 309 | for glance_image in glance_images: |
| 310 | glance_images_dict[glance_image['name']] = glance_image |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 311 | |
| 312 | # remove old images |
| 313 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 314 | Image.objects.filter(name__in=old_image_names).delete() |