Sapan Bhatia | 0235e98 | 2014-09-03 13:14:40 -0400 | [diff] [blame] | 1 | from django.db.models.signals import post_save |
| 2 | from django.dispatch import receiver |
| 3 | import pdb |
Sapan Bhatia | a19736a | 2016-01-15 11:21:20 -0500 | [diff] [blame] | 4 | from generate.dependency_walker import * |
Sapan Bhatia | 7edfe7f | 2016-01-14 14:16:33 -0500 | [diff] [blame] | 5 | from synchronizers.openstack import model_policies |
Scott Baker | 3586c55 | 2016-01-14 15:30:20 -0800 | [diff] [blame] | 6 | from xos.logger import logger |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 7 | from datetime import datetime |
| 8 | import time |
| 9 | from core.models import * |
Scott Baker | 5b961e0 | 2015-06-22 10:56:16 -0700 | [diff] [blame] | 10 | from django.db import reset_queries |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 11 | from django.db.transaction import atomic |
| 12 | from django.db.models import F, Q |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 13 | |
Scott Baker | f342870 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 14 | modelPolicyEnabled = True |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 15 | bad_instances=[] |
Scott Baker | f342870 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 16 | |
| 17 | def EnableModelPolicy(x): |
| 18 | global modelPolicyEnabled |
| 19 | modelPolicyEnabled = x |
| 20 | |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 21 | def update_wp(d, o): |
| 22 | try: |
| 23 | save_fields = [] |
| 24 | if (d.write_protect != o.write_protect): |
| 25 | d.write_protect = o.write_protect |
| 26 | save_fields.append('write_protect') |
| 27 | if (save_fields): |
| 28 | d.save(update_fields=save_fields) |
| 29 | except AttributeError,e: |
| 30 | raise e |
| 31 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 32 | def update_dep(d, o): |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 33 | try: |
| 34 | print 'Trying to update %s'%d |
| 35 | save_fields = [] |
| 36 | if (d.updated < o.updated): |
| 37 | save_fields = ['updated'] |
| 38 | |
| 39 | if (save_fields): |
| 40 | d.save(update_fields=save_fields) |
| 41 | except AttributeError,e: |
| 42 | raise e |
Scott Baker | 5b961e0 | 2015-06-22 10:56:16 -0700 | [diff] [blame] | 43 | except Exception,e: |
Sapan Bhatia | 91c497e | 2016-04-06 19:04:05 +0200 | [diff] [blame] | 44 | logger.info('Could not save %r. Exception: %r'%(d,e), extra=d.tologdict()) |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 45 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 46 | def delete_if_inactive(d, o): |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 47 | try: |
| 48 | d.delete() |
| 49 | print "Deleted %s (%s)"%(d,d.__class__.__name__) |
| 50 | except: |
| 51 | pass |
| 52 | return |
| 53 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 54 | |
Sapan Bhatia | 8863b08 | 2016-03-23 19:32:14 +0100 | [diff] [blame] | 55 | #@atomic |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 56 | def execute_model_policy(instance, deleted): |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 57 | # Automatic dirtying |
| 58 | if (instance in bad_instances): |
| 59 | return |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 60 | |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 61 | # These are the models whose children get deleted when they are |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 62 | delete_policy_models = ['Slice','Instance','Network'] |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 63 | sender_name = instance.__class__.__name__ |
| 64 | policy_name = 'model_policy_%s'%sender_name |
| 65 | noargs = False |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 66 | |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 67 | if (not deleted): |
| 68 | walk_inv_deps(update_dep, instance) |
| 69 | walk_deps(update_wp, instance) |
| 70 | elif (sender_name in delete_policy_models): |
| 71 | walk_inv_deps(delete_if_inactive, instance) |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 72 | |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 73 | |
| 74 | |
| 75 | try: |
| 76 | policy_handler = getattr(model_policies, policy_name, None) |
| 77 | logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler)) |
| 78 | if policy_handler is not None: |
| 79 | if (deleted): |
| 80 | try: |
| 81 | policy_handler.handle_delete(instance) |
| 82 | except AttributeError: |
| 83 | pass |
| 84 | else: |
| 85 | policy_handler.handle(instance) |
| 86 | except: |
| 87 | logger.log_exc("Model Policy Error:") |
| 88 | |
| 89 | try: |
| 90 | instance.policed=datetime.now() |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 91 | instance.save(update_fields=['policed']) |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 92 | except: |
| 93 | logging.error('Object %r is defective'%instance) |
| 94 | bad_instances.append(instance) |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 95 | |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 96 | def noop(o,p): |
| 97 | pass |
| 98 | |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 99 | def run_policy(): |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 100 | while (True): |
| 101 | start = time.time() |
Scott Baker | 3062778 | 2015-09-17 16:58:36 -0700 | [diff] [blame] | 102 | run_policy_once() |
| 103 | if (time.time()-start<1): |
| 104 | time.sleep(1) |
| 105 | |
| 106 | def run_policy_once(): |
| 107 | from core.models import Instance,Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice,ControllerUser,ControllerSite |
Sapan Bhatia | f6d6be7 | 2015-11-25 16:38:02 +0100 | [diff] [blame] | 108 | models = [Controller, Site, SitePrivilege, Image, ControllerSlice, ControllerSite, ControllerUser, User, Slice, Network, Instance, SlicePrivilege] |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 109 | objects = [] |
| 110 | deleted_objects = [] |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 111 | |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 112 | for m in models: |
Scott Baker | 6380cbf | 2016-04-14 23:41:07 -0700 | [diff] [blame^] | 113 | res = m.objects.filter((Q(policed__lt=F('updated')) | Q(policed=None)) & Q(no_policy=False)) |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 114 | objects.extend(res) |
| 115 | res = m.deleted_objects.filter(Q(policed__lt=F('updated')) | Q(policed=None)) |
| 116 | deleted_objects.extend(res) |
| 117 | |
| 118 | for o in objects: |
| 119 | execute_model_policy(o, o.deleted) |
| 120 | |
| 121 | for o in deleted_objects: |
| 122 | execute_model_policy(o, True) |
| 123 | |
Sapan Bhatia | 35dab9d | 2015-05-27 19:11:12 +0200 | [diff] [blame] | 124 | # Reap non-sync'd models here |
| 125 | reaped = [Slice] |
| 126 | |
| 127 | for m in reaped: |
| 128 | dobjs = m.deleted_objects.all() |
| 129 | for d in dobjs: |
| 130 | deps = walk_inv_deps(noop, d) |
| 131 | if (not deps): |
| 132 | print 'Purging object %r'%d |
| 133 | d.delete(purge=True) |
Sapan Bhatia | a4fadac | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 134 | |
Scott Baker | 5b961e0 | 2015-06-22 10:56:16 -0700 | [diff] [blame] | 135 | try: |
| 136 | reset_queries() |
| 137 | except: |
| 138 | # this shouldn't happen, but in case it does, catch it... |
| 139 | logger.log_exc("exception in reset_queries") |