blob: 8faae81a392114b296dfdd209fa08bb28d0eda7a [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 Bhatiaa19736a2016-01-15 11:21:20 -05004from generate.dependency_walker import *
Sapan Bhatia7edfe7f2016-01-14 14:16:33 -05005from synchronizers.openstack import model_policies
Scott Baker3586c552016-01-14 15:30:20 -08006from xos.logger import logger
Sapan Bhatiab3507b92015-01-23 16:09:28 +00007from datetime import datetime
8import time
9from core.models import *
Scott Baker5b961e02015-06-22 10:56:16 -070010from django.db import reset_queries
Sapan Bhatiab3507b92015-01-23 16:09:28 +000011from django.db.transaction import atomic
12from django.db.models import F, Q
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000013
Scott Bakerf3428702014-10-21 15:05:29 -070014modelPolicyEnabled = True
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020015bad_instances=[]
Scott Bakerf3428702014-10-21 15:05:29 -070016
17def EnableModelPolicy(x):
18 global modelPolicyEnabled
19 modelPolicyEnabled = x
20
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020021def 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 Bhatia14356b72014-11-05 10:32:41 -050032def update_dep(d, o):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020033 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 Baker5b961e02015-06-22 10:56:16 -070043 except Exception,e:
Sapan Bhatia91c497e2016-04-06 19:04:05 +020044 logger.info('Could not save %r. Exception: %r'%(d,e), extra=d.tologdict())
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020045
Sapan Bhatia14356b72014-11-05 10:32:41 -050046def delete_if_inactive(d, o):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020047 try:
48 d.delete()
49 print "Deleted %s (%s)"%(d,d.__class__.__name__)
50 except:
51 pass
52 return
53
Sapan Bhatia14356b72014-11-05 10:32:41 -050054
Sapan Bhatia8863b082016-03-23 19:32:14 +010055#@atomic
Sapan Bhatiab3507b92015-01-23 16:09:28 +000056def execute_model_policy(instance, deleted):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020057 # Automatic dirtying
58 if (instance in bad_instances):
59 return
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000060
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020061 # These are the models whose children get deleted when they are
Tony Mackd8515472015-08-19 11:58:18 -040062 delete_policy_models = ['Slice','Instance','Network']
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020063 sender_name = instance.__class__.__name__
64 policy_name = 'model_policy_%s'%sender_name
65 noargs = False
Sapan Bhatiab3507b92015-01-23 16:09:28 +000066
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020067 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 Bhatia7a1e6be2015-01-16 22:17:42 +000072
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020073
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 Bhatiab3507b92015-01-23 16:09:28 +000091 instance.save(update_fields=['policed'])
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020092 except:
93 logging.error('Object %r is defective'%instance)
94 bad_instances.append(instance)
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000095
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020096def noop(o,p):
97 pass
98
Sapan Bhatiab3507b92015-01-23 16:09:28 +000099def run_policy():
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200100 while (True):
101 start = time.time()
Scott Baker30627782015-09-17 16:58:36 -0700102 run_policy_once()
103 if (time.time()-start<1):
104 time.sleep(1)
105
106def run_policy_once():
107 from core.models import Instance,Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice,ControllerUser,ControllerSite
Sapan Bhatiaf6d6be72015-11-25 16:38:02 +0100108 models = [Controller, Site, SitePrivilege, Image, ControllerSlice, ControllerSite, ControllerUser, User, Slice, Network, Instance, SlicePrivilege]
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200109 objects = []
110 deleted_objects = []
Sapan Bhatia14356b72014-11-05 10:32:41 -0500111
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200112 for m in models:
Scott Baker6380cbf2016-04-14 23:41:07 -0700113 res = m.objects.filter((Q(policed__lt=F('updated')) | Q(policed=None)) & Q(no_policy=False))
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200114 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 Bhatia35dab9d2015-05-27 19:11:12 +0200124 # 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 Bhatiaa4fadac2015-05-09 18:10:17 +0200134
Scott Baker5b961e02015-06-22 10:56:16 -0700135 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")