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') |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 93 | self.wait_for_event(timeout=300) |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame^] | 94 | time.sleep(300) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 95 | logger.info('Observer woken up') |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 96 | except: |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame^] | 97 | logger.log_exc("Exception in observer run loop") |
| 98 | traceback.print_exc() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 99 | |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 100 | def sync_roles(self): |
| 101 | """ |
| 102 | save all role that don't already exist in keystone. Remove keystone roles that |
| 103 | don't exist in planetstack |
| 104 | """ |
| 105 | # sync all roles that don't already in keystone |
| 106 | keystone_roles = self.manager.driver.shell.keystone.roles.findall() |
| 107 | keystone_role_names = [kr.name for kr in keystone_roles] |
| 108 | pending_roles = Role.objects.all() |
| 109 | pending_role_names = [r.role_type for r in pending_roles] |
| 110 | for role in pending_roles: |
| 111 | if role.role_type not in keystone_role_names: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 112 | try: |
| 113 | self.manager.save_role(role) |
| 114 | logger.info("save role: %s" % (role)) |
| 115 | except: |
| 116 | logger.log_exc("save role failed: %s" % role) |
| 117 | traceback.print_exc() |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 118 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 119 | # don't delete roles for now |
| 120 | """ |
| 121 | # delete keystone roles that don't exist in planetstack |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 122 | for keystone_role in keystone_roles: |
| 123 | if keystone_role.name == 'admin': |
| 124 | continue |
| 125 | if keystone_role.name not in pending_role_names: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 126 | try: |
| 127 | self.manager.driver.delete_role({id: keystone_role.id}) |
| 128 | except: |
| 129 | traceback.print_exc() |
| 130 | """ |
Tony Mack | f8d4a46 | 2013-06-10 23:25:30 -0400 | [diff] [blame] | 131 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 132 | def sync_tenants(self): |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 133 | """ |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 134 | Save all sites and sliceswhere enacted < updated or enacted == None. |
| 135 | Remove sites and slices that no don't exist in openstack db if they |
| 136 | have an enacted time (enacted != None). |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 137 | """ |
| 138 | # get all sites that need to be synced (enacted < updated or enacted is None) |
| 139 | pending_sites = Site.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 140 | for site in pending_sites: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 141 | try: |
| 142 | self.manager.save_site(site) |
| 143 | logger.info("saved site %s" % site) |
| 144 | except: |
| 145 | logger.log_exc("save site failed: %s" % site) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 146 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 147 | # get all slices that need to be synced (enacted < updated or enacted is None) |
| 148 | pending_slices = Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 149 | for slice in pending_slices: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 150 | try: |
| 151 | self.manager.init_caller(slice.creator, slice.creator.site.login_base) |
| 152 | self.manager.save_slice(slice) |
| 153 | logger.info("saved slice %s" % slice) |
| 154 | except: |
| 155 | logger.log_exc("save slice failed: %s" % slice) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 156 | |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 157 | # get all sites that where enacted != null. We can assume these sites |
| 158 | # have previously been synced and need to be checed for deletion. |
| 159 | sites = Site.objects.filter(enacted__isnull=False) |
| 160 | site_dict = {} |
| 161 | for site in sites: |
| 162 | site_dict[site.login_base] = site |
| 163 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 164 | # get all slices that where enacted != null. We can assume these slices |
| 165 | # have previously been synced and need to be checed for deletion. |
| 166 | slices = Slice.objects.filter(enacted__isnull=False) |
| 167 | slice_dict = {} |
| 168 | for slice in slices: |
| 169 | slice_dict[slice.name] = slice |
| 170 | |
| 171 | # delete keystone tenants that don't have a site record |
| 172 | tenants = self.manager.driver.shell.keystone.tenants.findall() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 173 | system_tenants = ['admin','service'] |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 174 | for tenant in tenants: |
Tony Mack | 586d5c9 | 2013-06-20 12:04:30 -0400 | [diff] [blame] | 175 | if tenant.name in system_tenants: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 176 | continue |
| 177 | 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] | 178 | try: |
| 179 | self.manager.driver.delete_tenant(tenant.id) |
| 180 | logger.info("deleted tenant: %s" % (tenant)) |
| 181 | except: |
| 182 | logger.log_exc("delete tenant failed: %s" % tenant) |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 183 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 184 | |
| 185 | def sync_users(self): |
| 186 | """ |
| 187 | save all users where enacted < updated or enacted == None. Remove users that |
| 188 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 189 | """ |
| 190 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 191 | pending_users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 192 | for user in pending_users: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 193 | try: |
| 194 | self.manager.save_user(user) |
| 195 | logger.info("saved user: %s" % (user)) |
| 196 | except: |
| 197 | logger.log_exc("save user failed: %s" %user) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 198 | |
| 199 | # get all users that where enacted != null. We can assume these users |
| 200 | # have previously been synced and need to be checed for deletion. |
| 201 | users = User.objects.filter(enacted__isnull=False) |
| 202 | user_dict = {} |
| 203 | for user in users: |
| 204 | user_dict[user.kuser_id] = user |
| 205 | |
| 206 | # delete keystone users that don't have a user record |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 207 | system_users = ['admin', 'nova', 'quantum', 'glance', 'cinder', 'swift', 'service'] |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 208 | users = self.manager.driver.shell.keystone.users.findall() |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 209 | for user in users: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 210 | if user.name in system_users: |
Tony Mack | db9be93 | 2013-06-10 13:42:42 -0400 | [diff] [blame] | 211 | continue |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 212 | if user.id not in user_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 213 | try: |
| 214 | #self.manager.driver.delete_user(user.id) |
| 215 | logger.info("deleted user: %s" % user) |
| 216 | except: |
| 217 | logger.log_exc("delete user failed: %s" % user) |
| 218 | |
| 219 | |
| 220 | def sync_user_tenant_roles(self): |
| 221 | """ |
| 222 | Save all site privileges and slice memberships wheree enacted < updated or |
| 223 | enacted == None. Remove ones that don't exist in openstack db if they have |
| 224 | an enacted time (enacted != None). |
| 225 | """ |
| 226 | # sync site privileges |
| 227 | pending_site_privileges = SitePrivilege.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 228 | for site_priv in pending_site_privileges: |
| 229 | try: |
| 230 | self.manager.save_site_privilege(site_priv) |
| 231 | logger.info("saved site privilege: %s" % (site_priv)) |
| 232 | except: logger.log_exc("save site privilege failed: %s " % site_priv) |
| 233 | |
| 234 | # sync slice memberships |
| 235 | pending_slice_memberships = SliceMembership.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 236 | for slice_memb in pending_slice_memberships: |
| 237 | try: |
| 238 | self.manager.save_slice_membership(slice_memb) |
| 239 | logger.info("saved slice membership: %s" % (slice_memb)) |
| 240 | except: logger.log_exc("save slice membership failed: %s" % slice_memb) |
| 241 | |
| 242 | # get all site privileges and slice memberships that have been enacted |
| 243 | user_tenant_roles = defaultdict(list) |
| 244 | for site_priv in SitePrivilege.objects.filter(enacted__isnull=False): |
| 245 | user_tenant_roles[(site_priv.user.kuser_id, site_priv.site.tenant_id)].append(site_priv.role.role) |
| 246 | for slice_memb in SliceMembership.objects.filter(enacted__isnull=False): |
| 247 | user_tenant_roles[(slice_memb.user.kuser_id, slice_memb.slice.tenant_id)].append(slice_memb.role.role) |
| 248 | |
| 249 | # Some user tenant role aren't stored in planetstack but they must be preserved. |
| 250 | # Role that fall in this category are |
| 251 | # 1. Never remove a user's role that their home site |
| 252 | # 2. Never remove a user's role at a slice they've created. |
| 253 | # Keep track of all roles that must be preserved. |
| 254 | users = User.objects.all() |
| 255 | preserved_roles = {} |
| 256 | for user in users: |
| 257 | tenant_ids = [s['tenant_id'] for s in user.slices.values()] |
| 258 | tenant_ids.append(user.site.tenant_id) |
| 259 | preserved_roles[user.kuser_id] = tenant_ids |
| 260 | |
| 261 | |
| 262 | # begin removing user tenant roles from keystone. This is stored in the |
| 263 | # Metadata table. |
| 264 | for metadata in self.manager.driver.shell.keystone_db.get_metadata(): |
| 265 | # skip admin roles |
| 266 | if metadata.user_id == self.manager.driver.admin_user.id: |
| 267 | continue |
| 268 | # skip preserved tenant ids |
| 269 | if metadata.user_id in preserved_roles and \ |
| 270 | metadata.tenant_id in preserved_roles[metadata.user_id]: |
| 271 | continue |
| 272 | # get roles for user at this tenant |
| 273 | user_tenant_role_ids = user_tenant_roles.get((metadata.user_id, metadata.tenant_id), []) |
| 274 | |
| 275 | if user_tenant_role_ids: |
| 276 | # The user has roles at the tenant. Check if roles need to |
| 277 | # be updated. |
| 278 | user_keystone_role_ids = metadata.data.get('roles', []) |
| 279 | for role_id in user_keystone_role_ids: |
| 280 | if role_id not in user_tenant_role_ids: |
| 281 | user_keystone_role_ids.pop(user_keystone_role_ids.index(role_id)) |
| 282 | else: |
| 283 | # The user has no roles at this tenant. |
| 284 | metadata.data['roles'] = [] |
| 285 | #session.add(metadata) |
| 286 | logger.info("pruning metadata for %s at %s" % (metadata.user_id, metadata.tenant_id)) |
| 287 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 288 | def sync_slivers(self): |
| 289 | """ |
| 290 | save all slivers where enacted < updated or enacted == None. Remove slivers that |
| 291 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 292 | """ |
| 293 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 294 | pending_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 295 | for sliver in pending_slivers: |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 296 | if sliver.creator: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 297 | try: |
| 298 | # update manager context |
| 299 | self.manager.init_caller(sliver.creator, sliver.slice.name) |
| 300 | self.manager.save_sliver(sliver) |
Tony Mack | e3f8d86 | 2013-07-01 20:40:55 -0400 | [diff] [blame] | 301 | logger.info("saved sliver: %s" % (sliver)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 302 | except: |
| 303 | logger.log_exc("save sliver failed: %s" % sliver) |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 304 | |
Tony Mack | aa8ad5f | 2013-06-24 13:50:06 -0400 | [diff] [blame] | 305 | # get all slivers where enacted != null. We can assume these users |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 306 | # have previously been synced and need to be checed for deletion. |
| 307 | slivers = Sliver.objects.filter(enacted__isnull=False) |
| 308 | sliver_dict = {} |
| 309 | for sliver in slivers: |
| 310 | sliver_dict[sliver.instance_id] = sliver |
| 311 | |
| 312 | # delete sliver that don't have a sliver record |
| 313 | ctx = self.manager.driver.shell.nova_db.ctx |
| 314 | instances = self.manager.driver.shell.nova_db.instance_get_all(ctx) |
| 315 | for instance in instances: |
Tony Mack | aa8ad5f | 2013-06-24 13:50:06 -0400 | [diff] [blame] | 316 | if instance.uuid not in sliver_dict: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 317 | try: |
| 318 | # lookup tenant and update context |
| 319 | tenant = self.manager.driver.shell.keystone.tenants.find(id=instance.project_id) |
| 320 | self.manager.init_admin(tenant=tenant.name) |
Tony Mack | aa8ad5f | 2013-06-24 13:50:06 -0400 | [diff] [blame] | 321 | self.manager.driver.destroy_instance(instance.uuid) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 322 | logger.info("destroyed sliver: %s" % (instance)) |
| 323 | except: |
| 324 | logger.log_exc("destroy sliver failed: %s" % instance) |
| 325 | |
| 326 | |
| 327 | def sync_sliver_ips(self): |
| 328 | # fill in null ip addresses |
| 329 | slivers = Sliver.objects.filter(ip=None) |
| 330 | for sliver in slivers: |
| 331 | # update connection |
| 332 | self.manager.init_admin(tenant=sliver.slice.name) |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 333 | servers = self.manager.driver.shell.nova.servers.findall(id=sliver.instance_id) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 334 | if not servers: |
| 335 | continue |
| 336 | server = servers[0] |
| 337 | ips = server.addresses.get(sliver.slice.name, []) |
| 338 | if not ips: |
| 339 | continue |
| 340 | sliver.ip = ips[0]['addr'] |
| 341 | sliver.save() |
| 342 | logger.info("saved sliver ip: %s %s" % (sliver, ips[0])) |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 343 | |
| 344 | def sync_external_routes(self): |
| 345 | routes = self.manager.driver.get_external_routes() |
Tony Mack | 16f51e0 | 2013-07-01 10:33:29 -0400 | [diff] [blame] | 346 | subnets = self.manager.driver.shell.quantum.list_subnets()['subnets'] |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 347 | for subnet in subnets: |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame^] | 348 | try: |
| 349 | self.manager.driver.add_external_route(subnet, routes) |
| 350 | except: |
Tony Mack | 16f51e0 | 2013-07-01 10:33:29 -0400 | [diff] [blame] | 351 | logger.log_exc("failed to add external route for subnet %s" % subnet) |
Scott Baker | 78c87be | 2013-08-09 15:42:30 -0700 | [diff] [blame^] | 352 | |
| 353 | def sync_networks(self): |
| 354 | """ |
| 355 | save all networks where enacted < updated or enacted == None. Remove networks that |
| 356 | no don't exist in openstack db if they have an enacted time (enacted != None). |
| 357 | """ |
| 358 | # get all users that need to be synced (enacted < updated or enacted is None) |
| 359 | pending_networks = Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 360 | for network in pending_networks: |
| 361 | if network.owner and network.owner.creator: |
| 362 | try: |
| 363 | # update manager context |
| 364 | self.manager.init_caller(network.owner.creator, network.owner.name) |
| 365 | self.manager.save_network(network) |
| 366 | logger.info("saved network: %s" % (network)) |
| 367 | except: |
| 368 | logger.log_exc("save network failed: %s" % network) |
| 369 | |
| 370 | # get all networks where enacted != null. We can assume these users |
| 371 | # have previously been synced and need to be checed for deletion. |
| 372 | networks = Network.objects.filter(enacted__isnull=False) |
| 373 | network_dict = {} |
| 374 | for network in networks: |
| 375 | network_dict[network.network_id] = network |
| 376 | |
| 377 | # TODO: delete Network objects if quantum network doesn't exist |
| 378 | # (need to write self.manager.driver.shell.quantum_db) |
| 379 | |