blob: dc147664f99ea476b9781410d6ae9d2b098a973d [file] [log] [blame]
Sapan Bhatia0235e982014-09-03 13:14:40 -04001from django.db.models.signals import post_save
2from django.dispatch import receiver
3import pdb
Sapan Bhatia475c5972014-11-05 10:32:41 -05004from dependency_walker import *
Sapan Bhatia3a5980e2014-09-15 03:48:11 -04005import model_policies
Tony Macka7dbd422015-01-05 22:48:11 -05006from util.logger import logger
Sapan Bhatiad49ba532015-01-23 16:09:28 +00007from datetime import datetime
8import time
9from core.models import *
10from django.db.transaction import atomic
11from django.db.models import F, Q
Sapan Bhatiae2ace182015-01-16 22:17:42 +000012
Scott Baker79eaac22014-10-21 15:05:29 -070013modelPolicyEnabled = True
14
15def EnableModelPolicy(x):
16 global modelPolicyEnabled
17 modelPolicyEnabled = x
18
Sapan Bhatia475c5972014-11-05 10:32:41 -050019def update_dep(d, o):
Sapan Bhatia142314e2014-12-21 02:32:50 -050020 try:
21 if (d.updated < o.updated):
22 d.save(update_fields=['updated'])
23 except AttributeError,e:
Sapan Bhatia142314e2014-12-21 02:32:50 -050024 raise e
Sapan Bhatia475c5972014-11-05 10:32:41 -050025
26def delete_if_inactive(d, o):
27 #print "Deleting %s (%s)"%(d,d.__class__.__name__)
Sapan Bhatiad49ba532015-01-23 16:09:28 +000028 # d.delete()
Sapan Bhatia475c5972014-11-05 10:32:41 -050029 return
30
Sapan Bhatiad49ba532015-01-23 16:09:28 +000031@atomic
32def execute_model_policy(instance, deleted):
33 # Automatic dirtying
34 walk_inv_deps(update_dep, instance)
Sapan Bhatiae2ace182015-01-16 22:17:42 +000035
Sapan Bhatiad49ba532015-01-23 16:09:28 +000036 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 Bhatiae2ace182015-01-16 22:17:42 +000043 try:
44 policy_handler = getattr(model_policies, policy_name, None)
Sapan Bhatiad49ba532015-01-23 16:09:28 +000045 logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler))
Sapan Bhatiae2ace182015-01-16 22:17:42 +000046 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 Bhatiae2ace182015-01-16 22:17:42 +000051
Sapan Bhatiad49ba532015-01-23 16:09:28 +000052 instance.policed=datetime.now()
53 instance.save(update_fields=['policed'])
Sapan Bhatiae2ace182015-01-16 22:17:42 +000054
Sapan Bhatiad49ba532015-01-23 16:09:28 +000055def run_policy():
Sapan Bhatiaf0ad9802015-01-29 20:44:46 +000056 from core.models import Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice,ControllerUser,ControllerSite
Sapan Bhatiad49ba532015-01-23 16:09:28 +000057 while (True):
58 start = time.time()
Sapan Bhatiaf0ad9802015-01-29 20:44:46 +000059 models = [Slice, Controller, Network, User, SlicePrivilege, Site, SitePrivilege, Image, ControllerSlice, ControllerSite, ControllerUser]
Sapan Bhatiad49ba532015-01-23 16:09:28 +000060 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 Bhatia475c5972014-11-05 10:32:41 -050065
Sapan Bhatiad49ba532015-01-23 16:09:28 +000066 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)