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 |
| 14 | |
| 15 | def EnableModelPolicy(x): |
| 16 | global modelPolicyEnabled |
| 17 | modelPolicyEnabled = x |
| 18 | |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 19 | def update_dep(d, o): |
Sapan Bhatia | 142314e | 2014-12-21 02:32:50 -0500 | [diff] [blame] | 20 | try: |
| 21 | if (d.updated < o.updated): |
| 22 | d.save(update_fields=['updated']) |
| 23 | except AttributeError,e: |
Sapan Bhatia | 142314e | 2014-12-21 02:32:50 -0500 | [diff] [blame] | 24 | raise e |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 25 | |
| 26 | def delete_if_inactive(d, o): |
| 27 | #print "Deleting %s (%s)"%(d,d.__class__.__name__) |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 28 | # d.delete() |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 29 | return |
| 30 | |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 31 | @atomic |
| 32 | def execute_model_policy(instance, deleted): |
| 33 | # Automatic dirtying |
| 34 | walk_inv_deps(update_dep, instance) |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 35 | |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 36 | sender_name = instance.__class__.__name__ |
| 37 | policy_name = 'model_policy_%s'%sender_name |
| 38 | noargs = False |
| 39 | |
| 40 | if deleted: |
| 41 | walk_inv_deps(delete_if_inactive, instance) |
| 42 | else: |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 43 | try: |
| 44 | policy_handler = getattr(model_policies, policy_name, None) |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 45 | logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler)) |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 46 | if policy_handler is not None: |
| 47 | policy_handler.handle(instance) |
| 48 | except: |
| 49 | logger.log_exc("Model Policy Error:") |
| 50 | print "Policy Exceution Error" |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 51 | |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 52 | instance.policed=datetime.now() |
| 53 | instance.save(update_fields=['policed']) |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 54 | |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 55 | def run_policy(): |
| 56 | from core.models import Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice |
| 57 | while (True): |
| 58 | start = time.time() |
| 59 | models = [Slice, Controller, Network, User, SlicePrivilege, Site, SitePrivilege, Image, ControllerSlice] |
| 60 | objects = [] |
| 61 | |
| 62 | for m in models: |
| 63 | res = m.objects.filter(Q(policed__lt=F('updated')) | Q(policed=None)) |
| 64 | objects.extend(res) |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 65 | |
Sapan Bhatia | d49ba53 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 66 | for o in objects: |
| 67 | print "Working on %r"%o |
| 68 | execute_model_policy(o, False) |
| 69 | |
| 70 | |
| 71 | if (time.time()-start<1): |
| 72 | time.sleep(1) |