blob: 431623d1d286e2d7fd91efa8ac1ab7a37ffcb17c [file] [log] [blame]
Sapan Bhatiaf68688d2013-09-03 11:45:46 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Sapan Bhatiaf68688d2013-09-03 11:45:46 -04004from planetstack.config import Config
Tony Mack66646d52013-09-24 21:47:12 -04005from observer.openstacksyncstep import OpenStackSyncStep
Sapan Bhatiaf68688d2013-09-03 11:45:46 -04006
Tony Mack66646d52013-09-24 21:47:12 -04007class GarbageCollector(OpenStackSyncStep):
8 requested_interval = 86400
9 provides=[]
Sapan Bhatiaf68688d2013-09-03 11:45:46 -040010
Tony Mack66646d52013-09-24 21:47:12 -040011 def call(self):
Tony Mack387a73f2013-09-18 07:59:14 -040012 try:
13 logger.info('gc start')
14 #self.sync_roles()
15 self.gc_tenants()
16 self.gc_users()
17 self.gc_user_tenant_roles()
18 self.gc_slivers()
19 self.gc_sliver_ips()
20 self.gc_external_routes()
21 except:
22 traceback.print_exc()
23
24 def gc_roles(self):
25 """
26 all role that don't already exist in keystone. Remove keystone roles that
27 don't exist in planetstack
28 """
29 # sync all roles that don't already in keystone
30 keystone_roles = self.driver.shell.keystone.roles.findall()
31 keystone_role_names = [kr.name for kr in keystone_roles]
32 pending_roles = Role.objects.all()
33 pending_role_names = [r.role_type for r in pending_roles]
34 # don't delete roles for now
35 """
36 # delete keystone roles that don't exist in planetstack
37 for keystone_role in keystone_roles:
38 if keystone_role.name == 'admin':
39 continue
40 if keystone_role.name not in pending_role_names:
41 try:
42 self.manager.driver.delete_role({id: keystone_role.id})
43 except:
44 traceback.print_exc()
45 """
46
47 def gc_tenants(self):
48 """
49 Remove sites and slices that no don't exist in openstack db if they
50 have an enacted time (enacted != None).
51 """
52 # get all sites that where enacted != null. We can assume these sites
53 # have previously been synced and need to be checed for deletion.
54 sites = Site.objects.filter(enacted__isnull=False)
55 site_dict = {}
56 for site in sites:
57 site_dict[site.login_base] = site
58
59 # get all slices that where enacted != null. We can assume these slices
60 # have previously been synced and need to be checed for deletion.
61 slices = Slice.objects.filter(enacted__isnull=False)
62 slice_dict = {}
63 for slice in slices:
64 slice_dict[slice.name] = slice
65
66 # delete keystone tenants that don't have a site record
67 tenants = self.driver.shell.keystone.tenants.findall()
68 system_tenants = ['admin','service']
69 for tenant in tenants:
70 if tenant.name in system_tenants:
71 continue
72 if tenant.name not in site_dict and tenant.name not in slice_dict:
73 try:
74 self.driver.delete_tenant(tenant.id)
75 logger.info("deleted tenant: %s" % (tenant))
76 except:
77 logger.log_exc("delete tenant failed: %s" % tenant)
78
79
80 def gc_users(self):
81 """
82 Remove users that no don't exist in openstack db if they have an
83 enacted time (enacted != None).
84 """
85 # get all users that where enacted != null. We can assume these users
86 # have previously been synced and need to be checed for deletion.
87 users = User.objects.filter(enacted__isnull=False)
88 user_dict = {}
89 for user in users:
90 user_dict[user.kuser_id] = user
91
92 # delete keystone users that don't have a user record
93 system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service']
94 users = self.driver.shell.keystone.users.findall()
95 for user in users:
96 if user.name in system_users:
97 continue
98 if user.id not in user_dict:
99 try:
100 #self.manager.driver.delete_user(user.id)
101 logger.info("deleted user: %s" % user)
102 except:
103 logger.log_exc("delete user failed: %s" % user)
104
105
106 def gc_user_tenant_roles(self):
107 """
108 Remove roles that don't exist in openstack db if they have
109 an enacted time (enacted != None).
110 """
111 # get all site privileges and slice memberships that have been enacted
112 user_tenant_roles = defaultdict(list)
113 for site_priv in SitePrivilege.objects.filter(enacted__isnull=False):
114 user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role)
115 for slice_memb in SliceMembership.objects.filter(enacted__isnull=False):
116 user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role)
117
118 # Some user tenant role aren't stored in planetstack but they must be preserved.
119 # Role that fall in this category are
120 # 1. Never remove a user's role that their home site
121 # 2. Never remove a user's role at a slice they've created.
122 # Keep track of all roles that must be preserved.
123 users = User.objects.all()
124 preserved_roles = {}
125 for user in users:
126 tenant_ids = [s['tenant_id'] for s in user.slices.values()]
127 tenant_ids.append(user.site.tenant_id)
128 preserved_roles[user.kuser_id] = tenant_ids
129
130
131 # begin removing user tenant roles from keystone. This is stored in the
132 # Metadata table.
133 for metadata in self.driver.shell.keystone_db.get_metadata():
134 # skip admin roles
135 if metadata.user_id == self.manager.driver.admin_user.id:
136 continue
137 # skip preserved tenant ids
138 if metadata.user_id in preserved_roles and \
139 metadata.tenant_id in preserved_roles[metadata.user_id]:
140 continue
141 # get roles for user at this tenant
142 user_tenant_role_ids = user_tenant_roles.get((metadata.user_id, metadata.tenant_id), [])
143
144 if user_tenant_role_ids:
145 # The user has roles at the tenant. Check if roles need to
146 # be updated.
147 user_keystone_role_ids = metadata.data.get('roles', [])
148 for role_id in user_keystone_role_ids:
149 if role_id not in user_tenant_role_ids:
150 user_keystone_role_ids.pop(user_keystone_role_ids.index(role_id))
151 else:
152 # The user has no roles at this tenant.
153 metadata.data['roles'] = []
154 #session.add(metadata)
155 logger.info("pruning metadata for %s at %s" % (metadata.user_id, metadata.tenant_id))
156
157 def gc_slivers(self):
158 """
159 Remove slivers that no don't exist in openstack db if they have
160 an enacted time (enacted != None).
161 """
162 # get all slivers where enacted != null. We can assume these users
163 # have previously been synced and need to be checed for deletion.
164 slivers = Sliver.objects.filter(enacted__isnull=False)
165 sliver_dict = {}
166 for sliver in slivers:
167 sliver_dict[sliver.instance_id] = sliver
168
169 # delete sliver that don't have a sliver record
170 ctx = self.driver.shell.nova_db.ctx
171 instances = self.driver.shell.nova_db.instance_get_all(ctx)
172 for instance in instances:
173 if instance.uuid not in sliver_dict:
174 try:
175 # lookup tenant and update context
176 tenant = self.driver.shell.keystone.tenants.find(id=instance.project_id)
177 driver = self.driver.client_driver(tenant=tenant.name)
178 driver.destroy_instance(instance.uuid)
179 logger.info("destroyed sliver: %s" % (instance))
180 except:
181 logger.log_exc("destroy sliver failed: %s" % instance)
182
183
184 def gc_sliver_ips(self):
185 """
186 Update ips that have changed.
187 """
188 # fill in null ip addresses
189 slivers = Sliver.objects.filter(ip=None)
190 for sliver in slivers:
191 # update connection
192 self.manager.init_admin(tenant=sliver.slice.name)
193 servers = self.manager.driver.shell.nova.servers.findall(id=sliver.instance_id)
194 if not servers:
195 continue
196 server = servers[0]
197 ips = server.addresses.get(sliver.slice.name, [])
198 if ips and sliver.ip != ips[0]['addr']:
199 sliver.ip = ips[0]['addr']
200 sliver.save()
201 logger.info("updated sliver ip: %s %s" % (sliver, ips[0]))
202
203 def gc_external_routes(self):
204 pass