Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 1 | import time |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 2 | import traceback |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 3 | import commands |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 4 | import threading |
| 5 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 6 | from datetime import datetime |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 7 | from collections import defaultdict |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 8 | from core.models import * |
| 9 | from django.db.models import F, Q |
| 10 | from openstack.manager import OpenStackManager |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 11 | from util.logger import Logger, logging, logger |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 12 | #from timeout import timeout |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 13 | |
| 14 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 15 | logger = Logger(logfile='observer.log', level=logging.INFO) |
| 16 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 17 | class OpenStackObserver: |
| 18 | |
| 19 | def __init__(self): |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 20 | self.manager = OpenStackManager() |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 21 | # The Condition object that gets signalled by Feefie events |
| 22 | self.event_cond = threading.Condition() |
| 23 | |
| 24 | def wait_for_event(self, timeout): |
| 25 | self.event_cond.acquire() |
| 26 | self.event_cond.wait(timeout) |
| 27 | self.event_cond.release() |
| 28 | |
| 29 | def wake_up(self): |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 30 | logger.info('Wake up routine called. Event cond %r'%self.event_cond) |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 31 | self.event_cond.acquire() |
| 32 | self.event_cond.notify() |
| 33 | self.event_cond.release() |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 34 | |
| 35 | def run(self): |
| 36 | if not self.manager.enabled or not self.manager.has_openstack: |
| 37 | return |
| 38 | while True: |
| 39 | try: |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 40 | logger.info('Observer run loop') |
Tony Mack | 6795fbe | 2013-06-11 10:10:24 -0400 | [diff] [blame] | 41 | #self.sync_roles() |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame] | 42 | |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 43 | logger.info('Calling sync tenants') |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame] | 44 | try: |
| 45 | self.sync_tenants() |
| 46 | except: |
| 47 | logger.log_exc("Exception in sync_tenants") |
| 48 | traceback.print_exc() |
| 49 | |
| 50 | logger.info('Calling sync users') |
| 51 | try: |
| 52 | self.sync_users() |
| 53 | except: |
| 54 | logger.log_exc("Exception in sync_users") |
| 55 | traceback.print_exc() |
| 56 | |
| 57 | logger.info('Calling sync tenant roles') |
| 58 | try: |
| 59 | self.sync_user_tenant_roles() |
| 60 | except: |
| 61 | logger.log_exc("Exception in sync_users") |
| 62 | traceback.print_exc() |
| 63 | |
| 64 | logger.info('Calling sync slivers') |
| 65 | try: |
| 66 | self.sync_slivers() |
| 67 | except: |
| 68 | logger.log_exc("Exception in sync slivers") |
| 69 | traceback.print_exc() |
| 70 | |
| 71 | logger.info('Calling sync sliver ips') |
| 72 | try: |
| 73 | self.sync_sliver_ips() |
| 74 | except: |
| 75 | logger.log_exc("Exception in sync_sliver_ips") |
| 76 | traceback.print_exc() |
| 77 | |
| 78 | logger.info('Calling sync networks') |
| 79 | try: |
| 80 | self.sync_networks() |
| 81 | except: |
| 82 | logger.log_exc("Exception in sync_networks") |
| 83 | traceback.print_exc() |
| 84 | |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 85 | logger.info('Calling sync external routes') |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame] | 86 | try: |
| 87 | self.sync_external_routes() |
| 88 | except: |
| 89 | logger.log_exc("Exception in sync_external_routes") |
| 90 | traceback.print_exc() |
| 91 | |
| 92 | logger.info('Waiting for event') |
Scott Baker | 4b075bd | 2013-08-13 18:05:32 -0700 | [diff] [blame] | 93 | tBeforeWait = time.time() |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 94 | self.wait_for_event(timeout=300) |
Scott Baker | 4b075bd | 2013-08-13 18:05:32 -0700 | [diff] [blame] | 95 | |
| 96 | # Enforce 5 minutes between wakeups |
| 97 | tSleep = 300 - (time.time() - tBeforeWait) |
| 98 | if tSleep > 0: |
| 99 | logger.info('Sleeping for %d seconds' % tSleep) |
Scott Baker | e5097f0 | 2013-08-14 10:47:32 -0700 | [diff] [blame] | 100 | time.sleep(tSleep) |
Scott Baker | 4b075bd | 2013-08-13 18:05:32 -0700 | [diff] [blame] | 101 | |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 102 | logger.info('Observer woken up') |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 103 | except: |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame] | 104 | logger.log_exc("Exception in observer run loop") |
| 105 | traceback.print_exc() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 106 | |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 107 | def sync_roles(self): |
| 108 | """ |
| 109 | save all role that don't already exist in keystone. Remove keystone roles that |
| 110 | don't exist in planetstack |
| 111 | """ |
| 112 | # sync all roles that don't already in keystone |
| 113 | keystone_roles = self.manager.driver.shell.keystone.roles.findall() |
| 114 | keystone_role_names = [kr.name for kr in keystone_roles] |
| 115 | pending_roles = Role.objects.all() |
| 116 | pending_role_names = [r.role_type for r in pending_roles] |
| 117 | for role in pending_roles: |
| 118 | if role.role_type not in keystone_role_names: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 119 | try: |
| 120 | self.manager.save_role(role) |
| 121 | logger.info("save role: %s" % (role)) |
| 122 | except: |
| 123 | logger.log_exc("save role failed: %s" % role) |
| 124 | traceback.print_exc() |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 125 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 126 | # don't delete roles for now |
| 127 | """ |
| 128 | # delete keystone roles that don't exist in planetstack |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 129 | for keystone_role in keystone_roles: |
| 130 | if keystone_role.name == 'admin': |
| 131 | continue |
| 132 | if keystone_role.name not in pending_role_names: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 133 | try: |
| 134 | self.manager.driver.delete_role({id: keystone_role.id}) |
| 135 | except: |
| 136 | traceback.print_exc() |
| 137 | """ |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 138 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 139 | def sync_tenants(self): |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 140 | """ |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 141 | Save all sites and sliceswhere enacted < updated or enacted == None. |
| 142 | Remove sites and slices that no don't exist in openstack db if they |
| 143 | have an enacted time (enacted != None). |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 144 | """ |
| 145 | # get all sites that need to be synced (enacted < updated or enacted is None) |
| 146 | pending_sites = Site.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 147 | for site in pending_sites: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 148 | try: |
| 149 | self.manager.save_site(site) |
| 150 | logger.info("saved site %s" % site) |
| 151 | except: |
| 152 | logger.log_exc("save site failed: %s" % site) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 153 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 154 | # get all slices that need to be synced (enacted < updated or enacted is None) |
| 155 | pending_slices = Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 156 | for slice in pending_slices: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 157 | try: |
| 158 | self.manager.init_caller(slice.creator, slice.creator.site.login_base) |
| 159 | self.manager.save_slice(slice) |
| 160 | logger.info("saved slice %s" % slice) |
| 161 | except: |
| 162 | logger.log_exc("save slice failed: %s" % slice) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 163 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 164 | # get all sites that where enacted != null. We can assume these sites |
| 165 | # have previously been synced and need to be checed for deletion. |
| 166 | sites = Site.objects.filter(enacted__isnull=False) |
| 167 | site_dict = {} |
| 168 | for site in sites: |
| 169 | site_dict[site.login_base] = site |
| 170 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 171 | # get all slices that where enacted != null. We can assume these slices |
| 172 | # have previously been synced and need to be checed for deletion. |
| 173 | slices = Slice.objects.filter(enacted__isnull=False) |
| 174 | slice_dict = {} |
| 175 | for slice in slices: |
| 176 | slice_dict[slice.name] = slice |
| 177 | |
| 178 | # delete keystone tenants that don't have a site record |
| 179 | tenants = self.manager.driver.shell.keystone.tenants.findall() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 180 | system_tenants = ['admin','service'] |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 181 | for tenant in tenants: |
Tony Mack | 586d5c9 | 2013-06-20 12:04:30 -0400 | [diff] [blame] | 182 | if tenant.name in system_tenants: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 183 | continue |
| 184 | if tenant.name not in site_dict and tenant.name not in slice_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 185 | try: |
| 186 | self.manager.driver.delete_tenant(tenant.id) |
| 187 | logger.info("deleted tenant: %s" % (tenant)) |
| 188 | except: |
| 189 | logger.log_exc("delete tenant failed: %s" % tenant) |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 190 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 191 | |
| 192 | def sync_users(self): |
| 193 | """ |
| 194 | save all users where enacted < updated or enacted == None. Remove users that |
| 195 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 196 | """ |
| 197 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 198 | pending_users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 199 | for user in pending_users: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 200 | try: |
| 201 | self.manager.save_user(user) |
| 202 | logger.info("saved user: %s" % (user)) |
| 203 | except: |
| 204 | logger.log_exc("save user failed: %s" %user) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 205 | |
| 206 | # get all users that where enacted != null. We can assume these users |
| 207 | # have previously been synced and need to be checed for deletion. |
| 208 | users = User.objects.filter(enacted__isnull=False) |
| 209 | user_dict = {} |
| 210 | for user in users: |
| 211 | user_dict[user.kuser_id] = user |
| 212 | |
| 213 | # delete keystone users that don't have a user record |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 214 | system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service'] |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 215 | users = self.manager.driver.shell.keystone.users.findall() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 216 | for user in users: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 217 | if user.name in system_users: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 218 | continue |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 219 | if user.id not in user_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 220 | try: |
| 221 | #self.manager.driver.delete_user(user.id) |
| 222 | logger.info("deleted user: %s" % user) |
| 223 | except: |
| 224 | logger.log_exc("delete user failed: %s" % user) |
| 225 | |
| 226 | |
| 227 | def sync_user_tenant_roles(self): |
| 228 | """ |
| 229 | Save all site privileges and slice memberships wheree enacted < updated or |
| 230 | enacted == None. Remove ones that don't exist in openstack db if they have |
| 231 | an enacted time (enacted != None). |
| 232 | """ |
| 233 | # sync site privileges |
| 234 | pending_site_privileges = SitePrivilege.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 235 | for site_priv in pending_site_privileges: |
| 236 | try: |
| 237 | self.manager.save_site_privilege(site_priv) |
| 238 | logger.info("saved site privilege: %s" % (site_priv)) |
| 239 | except: logger.log_exc("save site privilege failed: %s " % site_priv) |
| 240 | |
| 241 | # sync slice memberships |
| 242 | pending_slice_memberships = SliceMembership.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 243 | for slice_memb in pending_slice_memberships: |
| 244 | try: |
| 245 | self.manager.save_slice_membership(slice_memb) |
| 246 | logger.info("saved slice membership: %s" % (slice_memb)) |
| 247 | except: logger.log_exc("save slice membership failed: %s" % slice_memb) |
| 248 | |
| 249 | # get all site privileges and slice memberships that have been enacted |
| 250 | user_tenant_roles = defaultdict(list) |
| 251 | for site_priv in SitePrivilege.objects.filter(enacted__isnull=False): |
| 252 | user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role) |
| 253 | for slice_memb in SliceMembership.objects.filter(enacted__isnull=False): |
| 254 | user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role) |
| 255 | |
| 256 | # Some user tenant role aren't stored in planetstack but they must be preserved. |
| 257 | # Role that fall in this category are |
| 258 | # 1. Never remove a user's role that their home site |
| 259 | # 2. Never remove a user's role at a slice they've created. |
| 260 | # Keep track of all roles that must be preserved. |
| 261 | users = User.objects.all() |
| 262 | preserved_roles = {} |
| 263 | for user in users: |
| 264 | tenant_ids = [s['tenant_id'] for s in user.slices.values()] |
| 265 | tenant_ids.append(user.site.tenant_id) |
| 266 | preserved_roles[user.kuser_id] = tenant_ids |
| 267 | |
| 268 | |
| 269 | # begin removing user tenant roles from keystone. This is stored in the |
| 270 | # Metadata table. |
| 271 | for metadata in self.manager.driver.shell.keystone_db.get_metadata(): |
| 272 | # skip admin roles |
| 273 | if metadata.user_id == self.manager.driver.admin_user.id: |
| 274 | continue |
| 275 | # skip preserved tenant ids |
| 276 | if metadata.user_id in preserved_roles and \ |
| 277 | metadata.tenant_id in preserved_roles[metadata.user_id]: |
| 278 | continue |
| 279 | # get roles for user at this tenant |
| 280 | user_tenant_role_ids = user_tenant_roles.get((metadata.user_id, metadata.tenant_id), []) |
| 281 | |
| 282 | if user_tenant_role_ids: |
| 283 | # The user has roles at the tenant. Check if roles need to |
| 284 | # be updated. |
| 285 | user_keystone_role_ids = metadata.data.get('roles', []) |
| 286 | for role_id in user_keystone_role_ids: |
| 287 | if role_id not in user_tenant_role_ids: |
| 288 | user_keystone_role_ids.pop(user_keystone_role_ids.index(role_id)) |
| 289 | else: |
| 290 | # The user has no roles at this tenant. |
| 291 | metadata.data['roles'] = [] |
| 292 | #session.add(metadata) |
| 293 | logger.info("pruning metadata for %s at %s" % (metadata.user_id, metadata.tenant_id)) |
| 294 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 295 | def sync_slivers(self): |
| 296 | """ |
| 297 | save all slivers where enacted < updated or enacted == None. Remove slivers that |
| 298 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 299 | """ |
| 300 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 301 | pending_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 302 | for sliver in pending_slivers: |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 303 | if sliver.creator: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 304 | try: |
| 305 | # update manager context |
| 306 | self.manager.init_caller(sliver.creator, sliver.slice.name) |
| 307 | self.manager.save_sliver(sliver) |
Tony Mack | e3f8d86 | 2013-07-01 20:40:55 -0400 | [diff] [blame] | 308 | logger.info("saved sliver: %s" % (sliver)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 309 | except: |
| 310 | logger.log_exc("save sliver failed: %s" % sliver) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 311 | |
Tony Mack | aa8ad5f | 2013-06-24 13:50:06 -0400 | [diff] [blame] | 312 | # get all slivers where enacted != null. We can assume these users |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 313 | # have previously been synced and need to be checed for deletion. |
| 314 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 315 | sliver_dict = {} |
| 316 | for sliver in slivers: |
| 317 | sliver_dict[sliver.instance_id] = sliver |
| 318 | |
| 319 | # delete sliver that don't have a sliver record |
Scott Baker | f4df952 | 2013-08-19 17:56:45 -0700 | [diff] [blame^] | 320 | ctx = self.manager.driver.shell.nova_db.ctx |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 321 | instances = self.manager.driver.shell.nova_db.instance_get_all(ctx) |
| 322 | for instance in instances: |
Tony Mack | aa8ad5f | 2013-06-24 13:50:06 -0400 | [diff] [blame] | 323 | if instance.uuid not in sliver_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 324 | try: |
| 325 | # lookup tenant and update context |
| 326 | tenant = self.manager.driver.shell.keystone.tenants.find(id=instance.project_id) |
| 327 | self.manager.init_admin(tenant=tenant.name) |
Tony Mack | aa8ad5f | 2013-06-24 13:50:06 -0400 | [diff] [blame] | 328 | self.manager.driver.destroy_instance(instance.uuid) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 329 | logger.info("destroyed sliver: %s" % (instance)) |
| 330 | except: |
| 331 | logger.log_exc("destroy sliver failed: %s" % instance) |
| 332 | |
| 333 | |
| 334 | def sync_sliver_ips(self): |
| 335 | # fill in null ip addresses |
| 336 | slivers = Sliver.objects.filter(ip=None) |
| 337 | for sliver in slivers: |
| 338 | # update connection |
| 339 | self.manager.init_admin(tenant=sliver.slice.name) |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 340 | servers = self.manager.driver.shell.nova.servers.findall(id=sliver.instance_id) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 341 | if not servers: |
| 342 | continue |
| 343 | server = servers[0] |
| 344 | ips = server.addresses.get(sliver.slice.name, []) |
| 345 | if not ips: |
| 346 | continue |
| 347 | sliver.ip = ips[0]['addr'] |
| 348 | sliver.save() |
| 349 | logger.info("saved sliver ip: %s %s" % (sliver, ips[0])) |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 350 | |
| 351 | def sync_external_routes(self): |
| 352 | routes = self.manager.driver.get_external_routes() |
Tony Mack | 16f51e0 | 2013-07-01 10:33:29 -0400 | [diff] [blame] | 353 | subnets = self.manager.driver.shell.quantum.list_subnets()['subnets'] |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 354 | for subnet in subnets: |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame] | 355 | try: |
| 356 | self.manager.driver.add_external_route(subnet, routes) |
| 357 | except: |
Tony Mack | 16f51e0 | 2013-07-01 10:33:29 -0400 | [diff] [blame] | 358 | logger.log_exc("failed to add external route for subnet %s" % subnet) |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame] | 359 | |
Scott Baker | f4df952 | 2013-08-19 17:56:45 -0700 | [diff] [blame^] | 360 | def sync_network_slivers(self): |
| 361 | networkSlivers = NetworkSliver.objects.all() |
| 362 | networkSlivers_by_id = {} |
| 363 | networkSlivers_by_port = {} |
| 364 | for networkSliver in networkSlivers: |
| 365 | networkSlivers_by_id[networkSliver.id] = networkSliver |
| 366 | networkSlivers_by_port[networkSliver.port_id] = networkSliver |
| 367 | |
| 368 | networks = Network.objects.all() |
| 369 | networks_by_id = {} |
| 370 | for network in networks: |
| 371 | networks_by_id[network.network_id] = network |
| 372 | |
| 373 | slivers = Sliver.objects.all() |
| 374 | slivers_by_instance_id = {} |
| 375 | for sliver in slivers: |
| 376 | slivers_by_instance_id[sliver.instance_id] = sliver |
| 377 | |
| 378 | ports = self.manager.driver.shell.quantum.list_ports()["ports"] |
| 379 | for port in ports: |
| 380 | # print port |
| 381 | if port["id"] in networkSlivers_by_port: |
| 382 | # we already have it |
| 383 | print "already accounted for port", port["id"] |
| 384 | continue |
| 385 | |
| 386 | if port["device_owner"] != "compute:nova": |
| 387 | # we only want the ports that connect to instances |
| 388 | continue |
| 389 | |
| 390 | network = networks_by_id.get(port['network_id'], None) |
| 391 | if not network: |
| 392 | #print "no network for port", port["id"], "network", port["network_id"] |
| 393 | continue |
| 394 | |
| 395 | sliver = slivers_by_instance_id.get(port['device_id'], None) |
| 396 | if not sliver: |
| 397 | print "no sliver for port", port["id"], "device_id", port['device_id'] |
| 398 | continue |
| 399 | |
| 400 | if network.template.sharedNetworkId is not None: |
| 401 | # If it's a shared network template, then more than one network |
| 402 | # object maps to the quantum network. We have to do a whole bunch |
| 403 | # of extra work to find the right one. |
| 404 | networks = network.template.network_set.all() |
| 405 | network = None |
| 406 | for candidate_network in networks: |
| 407 | if (candidate_network.owner == sliver.slice): |
| 408 | print "found network", candidate_network |
| 409 | network = candidate_network |
| 410 | |
| 411 | if not network: |
| 412 | print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"] |
| 413 | continue |
| 414 | |
| 415 | if not port["fixed_ips"]: |
| 416 | print "port", port["id"], "has no fixed_ips" |
| 417 | continue |
| 418 | |
| 419 | print "XXX", port |
| 420 | |
| 421 | ns = NetworkSliver(network=network, |
| 422 | sliver=sliver, |
| 423 | ip=port["fixed_ips"][0]["ip_address"], |
| 424 | port_id=port["id"]) |
| 425 | ns.save() |
| 426 | |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame] | 427 | def sync_networks(self): |
| 428 | """ |
| 429 | save all networks where enacted < updated or enacted == None. Remove networks that |
| 430 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 431 | """ |
| 432 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 433 | pending_networks = Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 434 | for network in pending_networks: |
| 435 | if network.owner and network.owner.creator: |
| 436 | try: |
| 437 | # update manager context |
| 438 | self.manager.init_caller(network.owner.creator, network.owner.name) |
| 439 | self.manager.save_network(network) |
| 440 | logger.info("saved network: %s" % (network)) |
| 441 | except: |
| 442 | logger.log_exc("save network failed: %s" % network) |
| 443 | |
| 444 | # get all networks where enacted != null. We can assume these users |
| 445 | # have previously been synced and need to be checed for deletion. |
| 446 | networks = Network.objects.filter(enacted__isnull=False) |
| 447 | network_dict = {} |
| 448 | for network in networks: |
| 449 | network_dict[network.network_id] = network |
| 450 | |
| 451 | # TODO: delete Network objects if quantum network doesn't exist |
| 452 | # (need to write self.manager.driver.shell.quantum_db) |
| 453 | |