blob: 7c42922b8e5d824555b8966d3392b671e8343b7f [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 Macka76b8952013-10-05 23:36:30 -04009from core.models import *
10
Tony Mackadb5d122014-04-21 21:46:45 -040011logger = Logger(logfile='/var/log/observer.log', level=logging.INFO)
Sapan Bhatiaf68688d2013-09-03 11:45:46 -040012
Tony Mack66646d52013-09-24 21:47:12 -040013class GarbageCollector(OpenStackSyncStep):
14 requested_interval = 86400
15 provides=[]
Sapan Bhatiaf68688d2013-09-03 11:45:46 -040016
Tony Mackb0e03bf2013-10-05 20:52:13 -040017 def call(self, **args):
Tony Mack387a73f2013-09-18 07:59:14 -040018 try:
Tony Mackadb5d122014-04-21 21:46:45 -040019 self.gc_networks()
20 #self.gc_user_tenant_roles()
Tony Mackfccb9eb2014-04-14 22:16:42 -040021 #self.gc_tenants()
22 #self.gc_users()
Tony Mackb61c81c2014-04-21 17:08:12 -040023 self.gc_slivers()
Tony Mackfccb9eb2014-04-14 22:16:42 -040024 #self.gc_sliver_ips()
Tony Mackfccb9eb2014-04-14 22:16:42 -040025 pass
Tony Mack387a73f2013-09-18 07:59:14 -040026 except:
Tony Mackadb5d122014-04-21 21:46:45 -040027 traceback.print_exc()
Tony Mack387a73f2013-09-18 07:59:14 -040028
Tony Mackadb5d122014-04-21 21:46:45 -040029 def gc_networks(self):
Tony Mack387a73f2013-09-18 07:59:14 -040030 """
Tony Mackadb5d122014-04-21 21:46:45 -040031 Remove all neutron networks that do not exist in the planetstack db.
Tony Mack387a73f2013-09-18 07:59:14 -040032 """
Tony Mackadb5d122014-04-21 21:46:45 -040033 # some networks cannot be deleted
34 system_networks = ['nat-net','private-admin']
35 for network_template in NetworkTemplate.objects.all():
36 if network_template.sharedNetworkName and \
37 network_template.sharedNetworkName not in system_networks:
38 system_networks.append(network_template.sharedNetworkName)
39
40 networks = Network.objects.filter(enacted__isnull=False)
41 networks_dict = {}
42 for network in networks:
43 networks_dict[network.name] = network
44
45 # some deployments are at the same url. Keep track of the urls we've visited
46 # to make sure we aren't making redundant calls
47 completed_urls = []
Tony Mackdd240952014-06-03 23:02:00 -040048 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -040049 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -040050 if deployment.auth_url in completed_urls:
Tony Mack387a73f2013-09-18 07:59:14 -040051 continue
Tony Mackadb5d122014-04-21 21:46:45 -040052 try:
53 driver = self.driver.admin_driver(deployment=deployment)
54 neutron_networks = driver.shell.quantum.list_networks()['networks']
55 for neutron_network in neutron_networks:
56 # skip system networks
57 if neutron_network['name'] in system_networks:
58 continue
59 if neutron_network['name'] not in networks_dict:
60 try:
61 logger.info("GarbageCollector: deleting network %s" % neutron_network['name'])
62 for subnet_id in neutron_network['subnets']:
63 driver.delete_subnet(subnet_id)
64 driver.delete_network(neutron_network['id'])
65 except:
66 logger.log_exc("GarbageCollector: delete network %s failed" % neutron_network['name'])
67 except:
68 logger.log_exc("GarbageCollector: Error at deployment %s" % deployment)
69
Tony Mackdd240952014-06-03 23:02:00 -040070 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -040071
72 def gc_tenants(self):
73 """
74 Remove sites and slices that no don't exist in openstack db if they
75 have an enacted time (enacted != None).
76 """
Tony Mackadb5d122014-04-21 21:46:45 -040077 # some tenants cannot be deleted
78 system_tenants = ['admin','service', 'invisible_to_admin']
Tony Mack387a73f2013-09-18 07:59:14 -040079 # get all sites that where enacted != null. We can assume these sites
80 # have previously been synced and need to be checed for deletion.
81 sites = Site.objects.filter(enacted__isnull=False)
82 site_dict = {}
83 for site in sites:
84 site_dict[site.login_base] = site
85
86 # get all slices that where enacted != null. We can assume these slices
87 # have previously been synced and need to be checed for deletion.
88 slices = Slice.objects.filter(enacted__isnull=False)
89 slice_dict = {}
90 for slice in slices:
91 slice_dict[slice.name] = slice
92
93 # delete keystone tenants that don't have a site record
Tony Mackadb5d122014-04-21 21:46:45 -040094 # some deployments are at the same url. Keep track of the urls we've visited
95 # to make sure we aren't making redundant calls
96 completed_urls = []
Tony Mackdd240952014-06-03 23:02:00 -040097 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -040098 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -040099 if deployment.auth_url in completed_urls:
Tony Mackadb5d122014-04-21 21:46:45 -0400100 continue
101
Tony Macke4be32f2014-03-11 20:45:25 -0400102 driver = self.driver.admin_driver(deployment=deployment)
103 tenants = driver.shell.keystone.tenants.findall()
Tony Macke4be32f2014-03-11 20:45:25 -0400104 for tenant in tenants:
105 if tenant.name in system_tenants:
106 continue
107 if tenant.name not in site_dict and tenant.name not in slice_dict:
108 try:
Tony Mackadb5d122014-04-21 21:46:45 -0400109 logger.info("GarbageCollector: deleting tenant: %s" % (tenant))
Tony Macke4be32f2014-03-11 20:45:25 -0400110 driver.delete_tenant(tenant.id)
Tony Macke4be32f2014-03-11 20:45:25 -0400111 except:
Tony Mackadb5d122014-04-21 21:46:45 -0400112 logger.log_exc("GarbageCollector: delete tenant failed: %s" % tenant)
Tony Mackdd240952014-06-03 23:02:00 -0400113 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -0400114
115 def gc_users(self):
116 """
Tony Mackf77f1402014-04-02 07:06:53 -0400117 Remove users that do not exist in openstack db if they have an
Tony Mack387a73f2013-09-18 07:59:14 -0400118 enacted time (enacted != None).
119 """
Tony Mackadb5d122014-04-21 21:46:45 -0400120 # some users cannot be deleted
121 system_users = ['admin', 'nova', 'quantum', 'neutron' 'glance', \
122 'cinder', 'swift', 'service', 'demo']
123
Tony Mack387a73f2013-09-18 07:59:14 -0400124 # get all users that where enacted != null. We can assume these users
125 # have previously been synced and need to be checed for deletion.
126 users = User.objects.filter(enacted__isnull=False)
127 user_dict = {}
128 for user in users:
129 user_dict[user.kuser_id] = user
130
131 # delete keystone users that don't have a user record
Tony Mackadb5d122014-04-21 21:46:45 -0400132 # some deployments are at the same url. Keep track of the urls we've visited
133 # to make sure we aren't making redundant calls
134 completed_urls = []
Tony Mackdd240952014-06-03 23:02:00 -0400135 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -0400136 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -0400137 if deployment.auth_url in completed_urls:
Tony Mackadb5d122014-04-21 21:46:45 -0400138 continue
139
Tony Macke4be32f2014-03-11 20:45:25 -0400140 driver = self.driver.admin_driver(deployment=deployment)
141 users = driver.shell.keystone.users.findall()
142 for user in users:
143 if user.name in system_users:
144 continue
145 if user.id not in user_dict:
146 try:
Tony Mackadb5d122014-04-21 21:46:45 -0400147 logger.info("GarbageCollector: deleting user: %s" % user)
Tony Macke4be32f2014-03-11 20:45:25 -0400148 self.driver.delete_user(user.id)
Tony Macke4be32f2014-03-11 20:45:25 -0400149 except:
Tony Mackadb5d122014-04-21 21:46:45 -0400150 logger.log_exc("GarbageCollector: delete user failed: %s" % user)
Tony Mackdd240952014-06-03 23:02:00 -0400151 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -0400152
153 def gc_user_tenant_roles(self):
154 """
155 Remove roles that don't exist in openstack db if they have
156 an enacted time (enacted != None).
157 """
158 # get all site privileges and slice memberships that have been enacted
159 user_tenant_roles = defaultdict(list)
160 for site_priv in SitePrivilege.objects.filter(enacted__isnull=False):
161 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 -0400162 for slice_memb in SlicePrivilege.objects.filter(enacted__isnull=False):
Tony Mack387a73f2013-09-18 07:59:14 -0400163 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 -0400164
165 # some deployments are at the same url. Keep track of the urls we've visited
166 # to make sure we aren't making redundant calls
167 completed_urls = []
Tony Mack387a73f2013-09-18 07:59:14 -0400168 # Some user tenant role aren't stored in planetstack but they must be preserved.
169 # Role that fall in this category are
170 # 1. Never remove a user's role that their home site
171 # 2. Never remove a user's role at a slice they've created.
172 # Keep track of all roles that must be preserved.
173 users = User.objects.all()
Tony Mackdd240952014-06-03 23:02:00 -0400174 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -0400175 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -0400176 if deployment.auth_url in completed_urls:
Tony Mackadb5d122014-04-21 21:46:45 -0400177 continue
178
Tony Macke4be32f2014-03-11 20:45:25 -0400179 driver = self.driver.admin_driver(deployment=deployment)
180 tenants = driver.shell.keystone.tenants.list()
181 for user in users:
182 # skip admin roles
183 if user.kuser_id == self.driver.admin_user.id:
184 continue
185
186 ignore_tenant_ids = []
187 k_user = driver.shell.keystone.users.find(id=user.kuser_id)
188 ignore_tenant_ids = [s['tenant_id'] for s in user.slices.values()]
189 if user.site:
190 ignore_tenant_ids.append(user.site.tenant_id)
Tony Mack387a73f2013-09-18 07:59:14 -0400191
Tony Macke4be32f2014-03-11 20:45:25 -0400192 # get user roles in keystone
193 for tenant in tenants:
194 # skip preserved tenant ids
195 if tenant.tenant_id in ignore_tenant_ids:
196 continue
197 # compare user tenant roles
198 user_tenant_role_ids = user_tenant_roles.get((user.kuser_id, tenant.id), [])
Tony Mack387a73f2013-09-18 07:59:14 -0400199
Tony Macke4be32f2014-03-11 20:45:25 -0400200 if user_tenant_role_ids:
201 # The user has roles at the tenant. Check if roles need to
202 # be updated.
203 k_user_roles = driver.shell.keystone.roles.roles_for_user(k_user, tenant)
204 for k_user_role in k_user_roles:
205 if k_user_role.role_id not in user_tenant_role_ids:
Tony Mackadb5d122014-04-21 21:46:45 -0400206 logger.info("GarbageCollector: removing user role %s for %s at %s" % \
Tony Macke4be32f2014-03-11 20:45:25 -0400207 (k_user_role, k_user.username, tenant.name))
Tony Mackadb5d122014-04-21 21:46:45 -0400208 driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant)
Tony Macke4be32f2014-03-11 20:45:25 -0400209 else:
210 # remove all roles the user has at the tenant.
211 for k_user_role in k_user_roles:
Tony Mackadb5d122014-04-21 21:46:45 -0400212 logger.info("GarbageCollector: removing user role %s for %s at %s" % \
Tony Macke4be32f2014-03-11 20:45:25 -0400213 (k_user_role, k_user.username, tenant.name))
Tony Mackadb5d122014-04-21 21:46:45 -0400214 driver.shell.keyston.remove_user_role(k_user, k_user_role, tenant)
Tony Mackdd240952014-06-03 23:02:00 -0400215 completed_urls.append(deployment.auth_url)
Tony Mack387a73f2013-09-18 07:59:14 -0400216
217 def gc_slivers(self):
218 """
219 Remove slivers that no don't exist in openstack db if they have
220 an enacted time (enacted != None).
221 """
222 # get all slivers where enacted != null. We can assume these users
223 # have previously been synced and need to be checed for deletion.
224 slivers = Sliver.objects.filter(enacted__isnull=False)
225 sliver_dict = {}
226 for sliver in slivers:
227 sliver_dict[sliver.instance_id] = sliver
228
Tony Mackadb5d122014-04-21 21:46:45 -0400229
230 # some deployments are at the same url. Keep track of the urls we've visited
231 # to make sure we aren't making redundant calls
232 completed_urls = []
Tony Mackdd240952014-06-03 23:02:00 -0400233 for deployment in Deployment.objects.all():
Tony Mackadb5d122014-04-21 21:46:45 -0400234 # skip deployments that we've already processed
Tony Mackdd240952014-06-03 23:02:00 -0400235 if deployment.auth_url in completed_urls:
Tony Mackb61c81c2014-04-21 17:08:12 -0400236 continue
Tony Mackadb5d122014-04-21 21:46:45 -0400237
238 try:
239 driver = self.driver.admin_driver(deployment=deployment)
240 for tenant in driver.shell.keystone.tenants.list():
241 if tenant.name in ['admin', 'services']:
242 continue
243 # delete sliver that don't have a sliver record
244 tenant_driver = self.driver.client_driver(tenant=tenant.name, deployment=deployment)
245 for instance in tenant_driver.shell.nova.servers.list():
246 if instance.id not in sliver_dict:
247 try:
248 logger.info("GarbageCollector: destroying sliver: %s %s" % (instance, instance.id))
249 tenant_driver.destroy_instance(instance.id)
250 except:
251 logger.log_exc("GarbageCollector: destroy sliver failed: %s" % instance)
252 except:
253 logger.log_exc("GarbageCollector: Error at deployment %s" % deployment)
Tony Mackdd240952014-06-03 23:02:00 -0400254 completed_urls.append(deployment.auth_url)
Tony Macke4be32f2014-03-11 20:45:25 -0400255
Tony Mack387a73f2013-09-18 07:59:14 -0400256
257 def gc_sliver_ips(self):
258 """
259 Update ips that have changed.
260 """
261 # fill in null ip addresses
262 slivers = Sliver.objects.filter(ip=None)
263 for sliver in slivers:
264 # update connection
Tony Macke4be32f2014-03-11 20:45:25 -0400265
266 driver = self.driver.client_driver(tenant=sliver.slice.name, deployment=sliver.node.deployment)
Tony Macka76b8952013-10-05 23:36:30 -0400267 servers = driver.shell.nova.servers.findall(id=sliver.instance_id)
Tony Mack387a73f2013-09-18 07:59:14 -0400268 if not servers:
269 continue
270 server = servers[0]
271 ips = server.addresses.get(sliver.slice.name, [])
272 if ips and sliver.ip != ips[0]['addr']:
273 sliver.ip = ips[0]['addr']
274 sliver.save()
275 logger.info("updated sliver ip: %s %s" % (sliver, ips[0]))
276
Tony Macke1509e82013-10-09 12:38:04 -0400277 def gc_nodes(self):
278 # collect local nodes
279 nodes = Node.objects.all()
280 nodes_dict = {}
281 for node in nodes:
282 nodes_dict[node.name] = node
283
284 # collect nova nodes:
Tony Macke1509e82013-10-09 12:38:04 -0400285 compute_nodes_dict = {}
Tony Mackdd240952014-06-03 23:02:00 -0400286 for deployment in Deployment.objets.all():
Tony Macke4be32f2014-03-11 20:45:25 -0400287 driver = self.driver.admin_driver(deployment=deployment)
288 compute_nodes = driver.nova.hypervisors.list()
289 for compute_node in compute_nodes:
290 compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node
Tony Macke1509e82013-10-09 12:38:04 -0400291
292 # remove old nodes
293 old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys())
294 Node.objects.filter(name__in=old_node_names).delete()
295
296 def gc_images(self):
Tony Mack46c2d502013-10-09 13:04:28 -0400297 # collect local images
298 images = Image.objects.all()
299 images_dict = {}
300 for image in images:
301 images_dict[image.name] = image
302
303 # collect glance images
Tony Mack46c2d502013-10-09 13:04:28 -0400304 glance_images_dict = {}
Tony Mackdd240952014-06-03 23:02:00 -0400305 for deployment in Deployment.objects.all():
Tony Macke4be32f2014-03-11 20:45:25 -0400306 driver = self.driver.admin_driver(deployment=deployment)
307 glance_images = driver.shell.glance.get_images()
308 for glance_image in glance_images:
309 glance_images_dict[glance_image['name']] = glance_image
Tony Mack46c2d502013-10-09 13:04:28 -0400310
311 # remove old images
312 old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys())
313 Image.objects.filter(name__in=old_image_names).delete()