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 | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 4 | from dependency_walker import * |
Sapan Bhatia | 3a5980e | 2014-09-15 03:48:11 -0400 | [diff] [blame] | 5 | import model_policies |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 6 | from util.logger import logger |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 7 | from datetime import datetime |
| 8 | import time |
| 9 | from core.models import * |
| 10 | from django.db.transaction import atomic |
| 11 | from django.db.models import F, Q |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 12 | |
Scott Baker | 79eaac2 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 13 | modelPolicyEnabled = True |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 14 | bad_instances=[] |
Scott Baker | 79eaac2 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 15 | |
| 16 | def EnableModelPolicy(x): |
| 17 | global modelPolicyEnabled |
| 18 | modelPolicyEnabled = x |
| 19 | |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 20 | def update_wp(d, o): |
| 21 | try: |
| 22 | save_fields = [] |
| 23 | if (d.write_protect != o.write_protect): |
| 24 | d.write_protect = o.write_protect |
| 25 | save_fields.append('write_protect') |
| 26 | if (save_fields): |
| 27 | d.save(update_fields=save_fields) |
| 28 | except AttributeError,e: |
| 29 | raise e |
| 30 | |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 31 | def update_dep(d, o): |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 32 | try: |
| 33 | print 'Trying to update %s'%d |
| 34 | save_fields = [] |
| 35 | if (d.updated < o.updated): |
| 36 | save_fields = ['updated'] |
| 37 | |
| 38 | if (save_fields): |
| 39 | d.save(update_fields=save_fields) |
| 40 | except AttributeError,e: |
| 41 | raise e |
| 42 | |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 43 | def delete_if_inactive(d, o): |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 44 | try: |
| 45 | d.delete() |
| 46 | print "Deleted %s (%s)"%(d,d.__class__.__name__) |
| 47 | except: |
| 48 | pass |
| 49 | return |
| 50 | |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 51 | |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 52 | @atomic |
| 53 | def execute_model_policy(instance, deleted): |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 54 | # Automatic dirtying |
| 55 | if (instance in bad_instances): |
| 56 | return |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 57 | |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 58 | # These are the models whose children get deleted when they are |
| 59 | delete_policy_models = ['Slice','Sliver','Network'] |
| 60 | sender_name = instance.__class__.__name__ |
| 61 | policy_name = 'model_policy_%s'%sender_name |
| 62 | noargs = False |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 63 | |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 64 | if (not deleted): |
| 65 | walk_inv_deps(update_dep, instance) |
| 66 | walk_deps(update_wp, instance) |
| 67 | elif (sender_name in delete_policy_models): |
| 68 | walk_inv_deps(delete_if_inactive, instance) |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 69 | |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 70 | |
| 71 | |
| 72 | try: |
| 73 | policy_handler = getattr(model_policies, policy_name, None) |
| 74 | logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler)) |
| 75 | if policy_handler is not None: |
| 76 | if (deleted): |
| 77 | try: |
| 78 | policy_handler.handle_delete(instance) |
| 79 | except AttributeError: |
| 80 | pass |
| 81 | else: |
| 82 | policy_handler.handle(instance) |
| 83 | except: |
| 84 | logger.log_exc("Model Policy Error:") |
| 85 | |
| 86 | try: |
| 87 | instance.policed=datetime.now() |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 88 | instance.save(update_fields=['policed']) |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 89 | except: |
| 90 | logging.error('Object %r is defective'%instance) |
| 91 | bad_instances.append(instance) |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 92 | |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 93 | def run_policy(): |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 94 | from core.models import Sliver,Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice,ControllerUser,ControllerSite |
| 95 | while (True): |
| 96 | start = time.time() |
| 97 | models = [Sliver,Slice, Controller, Network, User, SlicePrivilege, Site, SitePrivilege, Image, ControllerSlice, ControllerSite, ControllerUser] |
| 98 | objects = [] |
| 99 | deleted_objects = [] |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 100 | |
Sapan Bhatia | 538067c | 2015-05-09 18:10:17 +0200 | [diff] [blame] | 101 | for m in models: |
| 102 | res = m.objects.filter(Q(policed__lt=F('updated')) | Q(policed=None)) |
| 103 | objects.extend(res) |
| 104 | res = m.deleted_objects.filter(Q(policed__lt=F('updated')) | Q(policed=None)) |
| 105 | deleted_objects.extend(res) |
| 106 | |
| 107 | for o in objects: |
| 108 | execute_model_policy(o, o.deleted) |
| 109 | |
| 110 | for o in deleted_objects: |
| 111 | execute_model_policy(o, True) |
| 112 | |
| 113 | |
| 114 | if (time.time()-start<1): |
| 115 | time.sleep(1) |