blob: 6feff14e0992a3b3a2ad9548908be70fd85c938e [file] [log] [blame]
Sapan Bhatiaf68688d2013-09-03 11:45:46 -04001import os
2import base64
Tony Macka76b8952013-10-05 23:36:30 -04003import traceback
4from collections import defaultdict
Tony Mackae7f30c2013-09-25 12:46:50 -04005from django.db.models import F, Q
Sapan Bhatiaf68688d2013-09-03 11:45:46 -04006from planetstack.config import Config
Tony Macka76b8952013-10-05 23:36:30 -04007from util.logger import Logger, logging
Tony Mack66646d52013-09-24 21:47:12 -04008from observer.openstacksyncstep import OpenStackSyncStep
Tony Mackdd240952014-06-03 23:02:00 -04009from core.models.deployment import Deployment
Tony Macka76b8952013-10-05 23:36:30 -040010from core.models import *
11
Tony Mackadb5d122014-04-21 21:46:45 -040012logger = Logger(logfile='/var/log/observer.log', level=logging.INFO)
Sapan Bhatiaf68688d2013-09-03 11:45:46 -040013
Tony Mack66646d52013-09-24 21:47:12 -040014class GarbageCollector(OpenStackSyncStep):
15 requested_interval = 86400
16 provides=[]
Sapan Bhatiaf68688d2013-09-03 11:45:46 -040017
Tony Mackb0e03bf2013-10-05 20:52:13 -040018 def call(self, **args):
Tony Mack387a73f2013-09-18 07:59:14 -040019 try:
Tony Mackadb5d122014-04-21 21:46:45 -040020 self.gc_networks()
21 #self.gc_user_tenant_roles()
Tony Mackfccb9eb2014-04-14 22:16:42 -040022 #self.gc_tenants()
23 #self.gc_users()
Tony Mackb61c81c2014-04-21 17:08:12 -040024 self.gc_slivers()
Tony Mackfccb9eb2014-04-14 22:16:42 -040025 #self.gc_sliver_ips()
Tony Mackfccb9eb2014-04-14 22:16:42 -040026 pass
Tony Mack387a73f2013-09-18 07:59:14 -040027 except:
Tony Mackadb5d122014-04-21 21:46:45 -040028 traceback.print_exc()
Tony Mack387a73f2013-09-18 07:59:14 -040029
Tony Mackadb5d122014-04-21 21:46:45 -040030 def gc_networks(self):
Tony Mack387a73f2013-09-18 07:59:14 -040031 """
Tony Mackadb5d122014-04-21 21:46:45 -040032 Remove all neutron networks that do not exist in the planetstack db.
Tony Mack387a73f2013-09-18 07:59:14 -040033 """
Tony Mackadb5d122014-04-21 21:46:45 -040034 # 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 = []
Tony Mackdd240952014-06-03 23:02:00 -040049 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -040050 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -040051 if deployment.auth_url in completed_urls:
Tony Mack387a73f2013-09-18 07:59:14 -040052 continue
Tony Mackadb5d122014-04-21 21:46:45 -040053 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
Tony Mackdd240952014-06-03 23:02:00 -040071 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -040072
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 Mackadb5d122014-04-21 21:46:45 -040078 # some tenants cannot be deleted
79 system_tenants = ['admin','service', 'invisible_to_admin']
Tony Mack387a73f2013-09-18 07:59:14 -040080 # 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 Mackadb5d122014-04-21 21:46:45 -040095 # 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 Mackdd240952014-06-03 23:02:00 -040098 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -040099 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -0400100 if deployment.auth_url in completed_urls:
Tony Mackadb5d122014-04-21 21:46:45 -0400101 continue
102
Tony Macke4be32f2014-03-11 20:45:25 -0400103 driver = self.driver.admin_driver(deployment=deployment)
104 tenants = driver.shell.keystone.tenants.findall()
Tony Macke4be32f2014-03-11 20:45:25 -0400105 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 Mackadb5d122014-04-21 21:46:45 -0400110 logger.info("GarbageCollector: deleting tenant: %s" % (tenant))
Tony Macke4be32f2014-03-11 20:45:25 -0400111 driver.delete_tenant(tenant.id)
Tony Macke4be32f2014-03-11 20:45:25 -0400112 except:
Tony Mackadb5d122014-04-21 21:46:45 -0400113 logger.log_exc("GarbageCollector: delete tenant failed: %s" % tenant)
Tony Mackdd240952014-06-03 23:02:00 -0400114 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -0400115
116 def gc_users(self):
117 """
Tony Mackf77f1402014-04-02 07:06:53 -0400118 Remove users that do not exist in openstack db if they have an
Tony Mack387a73f2013-09-18 07:59:14 -0400119 enacted time (enacted != None).
120 """
Tony Mackadb5d122014-04-21 21:46:45 -0400121 # some users cannot be deleted
122 system_users = ['admin', 'nova', 'quantum', 'neutron' 'glance', \
123 'cinder', 'swift', 'service', 'demo']
124
Tony Mack387a73f2013-09-18 07:59:14 -0400125 # 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 Mackadb5d122014-04-21 21:46:45 -0400133 # 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 Mackdd240952014-06-03 23:02:00 -0400136 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -0400137 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -0400138 if deployment.auth_url in completed_urls:
Tony Mackadb5d122014-04-21 21:46:45 -0400139 continue
140
Tony Macke4be32f2014-03-11 20:45:25 -0400141 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 Mackadb5d122014-04-21 21:46:45 -0400148 logger.info("GarbageCollector: deleting user: %s" % user)
Tony Macke4be32f2014-03-11 20:45:25 -0400149 self.driver.delete_user(user.id)
Tony Macke4be32f2014-03-11 20:45:25 -0400150 except:
Tony Mackadb5d122014-04-21 21:46:45 -0400151 logger.log_exc("GarbageCollector: delete user failed: %s" % user)
Tony Mackdd240952014-06-03 23:02:00 -0400152 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -0400153
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 Macka76b8952013-10-05 23:36:30 -0400163 for slice_memb in SlicePrivilege.objects.filter(enacted__isnull=False):
Tony Mack387a73f2013-09-18 07:59:14 -0400164 user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role)
Tony Mackadb5d122014-04-21 21:46:45 -0400165
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 Mack387a73f2013-09-18 07:59:14 -0400169 # 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 Mackdd240952014-06-03 23:02:00 -0400175 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -0400176 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -0400177 if deployment.auth_url in completed_urls:
Tony Mackadb5d122014-04-21 21:46:45 -0400178 continue
179
Tony Macke4be32f2014-03-11 20:45:25 -0400180 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 Mack387a73f2013-09-18 07:59:14 -0400192
Tony Macke4be32f2014-03-11 20:45:25 -0400193 # 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 Mack387a73f2013-09-18 07:59:14 -0400200
Tony Macke4be32f2014-03-11 20:45:25 -0400201 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 Mackadb5d122014-04-21 21:46:45 -0400207 logger.info("GarbageCollector: removing user role %s for %s at %s" % \
Tony Macke4be32f2014-03-11 20:45:25 -0400208 (k_user_role, k_user.username, tenant.name))
Tony Mackadb5d122014-04-21 21:46:45 -0400209 driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant)
Tony Macke4be32f2014-03-11 20:45:25 -0400210 else:
211 # remove all roles the user has at the tenant.
212 for k_user_role in k_user_roles:
Tony Mackadb5d122014-04-21 21:46:45 -0400213 logger.info("GarbageCollector: removing user role %s for %s at %s" % \
Tony Macke4be32f2014-03-11 20:45:25 -0400214 (k_user_role, k_user.username, tenant.name))
Tony Mackadb5d122014-04-21 21:46:45 -0400215 driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant)
Tony Mackdd240952014-06-03 23:02:00 -0400216 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -0400217
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 Mackadb5d122014-04-21 21:46:45 -0400230
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 = []
Tony Mackdd240952014-06-03 23:02:00 -0400234 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -0400235 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -0400236 if deployment.auth_url in completed_urls:
Tony Mackb61c81c2014-04-21 17:08:12 -0400237 continue
Tony Mackadb5d122014-04-21 21:46:45 -0400238
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)
Tony Mackdd240952014-06-03 23:02:00 -0400255 completed_urls.append(deployment.auth_url)
Tony Macke4be32f2014-03-11 20:45:25 -0400256
Tony Mack387a73f2013-09-18 07:59:14 -0400257
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 Macke4be32f2014-03-11 20:45:25 -0400266
267 driver = self.driver.client_driver(tenant=sliver.slice.name, deployment=sliver.node.deployment)
Tony Macka76b8952013-10-05 23:36:30 -0400268 servers = driver.shell.nova.servers.findall(id=sliver.instance_id)
Tony Mack387a73f2013-09-18 07:59:14 -0400269 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 Macke1509e82013-10-09 12:38:04 -0400278 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 Macke1509e82013-10-09 12:38:04 -0400286 compute_nodes_dict = {}
Tony Mackdd240952014-06-03 23:02:00 -0400287 for deployment in Deployment.objets.all():
Tony Macke4be32f2014-03-11 20:45:25 -0400288 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 Macke1509e82013-10-09 12:38:04 -0400292
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 Mack46c2d502013-10-09 13:04:28 -0400298 # 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 Mack46c2d502013-10-09 13:04:28 -0400305 glance_images_dict = {}
Tony Mackdd240952014-06-03 23:02:00 -0400306 for deployment in Deployment.objects.all():
Tony Macke4be32f2014-03-11 20:45:25 -0400307 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 Mack46c2d502013-10-09 13:04:28 -0400311
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()