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