blob: 717f35c95f1756289ac9411b15b261dbe467ef9c [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 Macke4be32f2014-03-11 20:45:25 -04009from deployment_auth import deployment_auth
Tony Macka76b8952013-10-05 23:36:30 -040010from core.models import *
11
Andy Bavier04111b72013-10-22 16:47:10 -040012logger = Logger(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 Macke1509e82013-10-09 12:38:04 -040020 #self.gc_roles()
Tony Mack387a73f2013-09-18 07:59:14 -040021 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 Macka76b8952013-10-05 23:36:30 -040048 self.driver.delete_role({id: keystone_role.id})
Tony Mack387a73f2013-09-18 07:59:14 -040049 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 Macke4be32f2014-03-11 20:45:25 -040073 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 Mack387a73f2013-09-18 07:59:14 -040087
88
89 def gc_users(self):
90 """
Tony Mackf77f1402014-04-02 07:06:53 -040091 Remove users that do not exist in openstack db if they have an
Tony Mack387a73f2013-09-18 07:59:14 -040092 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 Mack9b6dde82013-10-06 11:05:49 -0400102 system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service', 'demo']
Tony Macke4be32f2014-03-11 20:45:25 -0400103 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 Mack387a73f2013-09-18 07:59:14 -0400115
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 Macka76b8952013-10-05 23:36:30 -0400126 for slice_memb in SlicePrivilege.objects.filter(enacted__isnull=False):
Tony Mack387a73f2013-09-18 07:59:14 -0400127 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 Macke4be32f2014-03-11 20:45:25 -0400135 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 Mack387a73f2013-09-18 07:59:14 -0400148
Tony Macke4be32f2014-03-11 20:45:25 -0400149 # 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 Mack387a73f2013-09-18 07:59:14 -0400156
Tony Macke4be32f2014-03-11 20:45:25 -0400157 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 Mack387a73f2013-09-18 07:59:14 -0400172
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 Macke4be32f2014-03-11 20:45:25 -0400185 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 Mack387a73f2013-09-18 07:59:14 -0400196
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 Macke4be32f2014-03-11 20:45:25 -0400205
206 driver = self.driver.client_driver(tenant=sliver.slice.name, deployment=sliver.node.deployment)
Tony Macka76b8952013-10-05 23:36:30 -0400207 servers = driver.shell.nova.servers.findall(id=sliver.instance_id)
Tony Mack387a73f2013-09-18 07:59:14 -0400208 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 Macke1509e82013-10-09 12:38:04 -0400219
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 Macke1509e82013-10-09 12:38:04 -0400228 compute_nodes_dict = {}
Tony Macke4be32f2014-03-11 20:45:25 -0400229 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 Macke1509e82013-10-09 12:38:04 -0400234
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 Mack46c2d502013-10-09 13:04:28 -0400240 # 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 Mack46c2d502013-10-09 13:04:28 -0400247 glance_images_dict = {}
Tony Macke4be32f2014-03-11 20:45:25 -0400248 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 Mack46c2d502013-10-09 13:04:28 -0400253
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()