blob: ef31d41800995ceaf53cc56768748b4f6fba6a17 [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
Sapan Bhatia80b83cb2016-05-06 15:20:13 -04008from django.utils import timezone
Sapan Bhatiab3507b92015-01-23 16:09:28 +00009import time
10from core.models import *
Scott Baker5b961e02015-06-22 10:56:16 -070011from django.db import reset_queries
Sapan Bhatiab3507b92015-01-23 16:09:28 +000012from django.db.transaction import atomic
13from django.db.models import F, Q
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000014
Scott Bakerf3428702014-10-21 15:05:29 -070015modelPolicyEnabled = True
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020016bad_instances=[]
Scott Bakerf3428702014-10-21 15:05:29 -070017
18def EnableModelPolicy(x):
19 global modelPolicyEnabled
20 modelPolicyEnabled = x
21
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020022def update_wp(d, o):
23 try:
24 save_fields = []
25 if (d.write_protect != o.write_protect):
26 d.write_protect = o.write_protect
27 save_fields.append('write_protect')
28 if (save_fields):
29 d.save(update_fields=save_fields)
30 except AttributeError,e:
31 raise e
32
Sapan Bhatia14356b72014-11-05 10:32:41 -050033def update_dep(d, o):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020034 try:
35 print 'Trying to update %s'%d
36 save_fields = []
37 if (d.updated < o.updated):
38 save_fields = ['updated']
39
40 if (save_fields):
41 d.save(update_fields=save_fields)
42 except AttributeError,e:
43 raise e
Scott Baker5b961e02015-06-22 10:56:16 -070044 except Exception,e:
Sapan Bhatia91c497e2016-04-06 19:04:05 +020045 logger.info('Could not save %r. Exception: %r'%(d,e), extra=d.tologdict())
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020046
Sapan Bhatia14356b72014-11-05 10:32:41 -050047def delete_if_inactive(d, o):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020048 try:
49 d.delete()
50 print "Deleted %s (%s)"%(d,d.__class__.__name__)
51 except:
52 pass
53 return
54
Sapan Bhatia14356b72014-11-05 10:32:41 -050055
Sapan Bhatia8863b082016-03-23 19:32:14 +010056#@atomic
Sapan Bhatiab3507b92015-01-23 16:09:28 +000057def execute_model_policy(instance, deleted):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020058 # Automatic dirtying
59 if (instance in bad_instances):
60 return
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000061
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020062 # These are the models whose children get deleted when they are
Tony Mackd8515472015-08-19 11:58:18 -040063 delete_policy_models = ['Slice','Instance','Network']
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020064 sender_name = instance.__class__.__name__
65 policy_name = 'model_policy_%s'%sender_name
66 noargs = False
Sapan Bhatiab3507b92015-01-23 16:09:28 +000067
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020068 if (not deleted):
69 walk_inv_deps(update_dep, instance)
70 walk_deps(update_wp, instance)
71 elif (sender_name in delete_policy_models):
72 walk_inv_deps(delete_if_inactive, instance)
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000073
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020074
75
76 try:
77 policy_handler = getattr(model_policies, policy_name, None)
78 logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler))
79 if policy_handler is not None:
80 if (deleted):
81 try:
82 policy_handler.handle_delete(instance)
83 except AttributeError:
84 pass
85 else:
86 policy_handler.handle(instance)
87 except:
88 logger.log_exc("Model Policy Error:")
89
90 try:
Sapan Bhatia80b83cb2016-05-06 15:20:13 -040091 instance.policed=timezone.now()
Sapan Bhatiab3507b92015-01-23 16:09:28 +000092 instance.save(update_fields=['policed'])
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020093 except:
94 logging.error('Object %r is defective'%instance)
95 bad_instances.append(instance)
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000096
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020097def noop(o,p):
98 pass
99
Sapan Bhatiab3507b92015-01-23 16:09:28 +0000100def run_policy():
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200101 while (True):
102 start = time.time()
Scott Baker30627782015-09-17 16:58:36 -0700103 run_policy_once()
104 if (time.time()-start<1):
105 time.sleep(1)
106
107def run_policy_once():
108 from core.models import Instance,Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice,ControllerUser,ControllerSite
Sapan Bhatiaf6d6be72015-11-25 16:38:02 +0100109 models = [Controller, Site, SitePrivilege, Image, ControllerSlice, ControllerSite, ControllerUser, User, Slice, Network, Instance, SlicePrivilege]
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200110 objects = []
111 deleted_objects = []
Sapan Bhatia14356b72014-11-05 10:32:41 -0500112
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200113 for m in models:
Scott Baker6380cbf2016-04-14 23:41:07 -0700114 res = m.objects.filter((Q(policed__lt=F('updated')) | Q(policed=None)) & Q(no_policy=False))
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200115 objects.extend(res)
116 res = m.deleted_objects.filter(Q(policed__lt=F('updated')) | Q(policed=None))
117 deleted_objects.extend(res)
118
119 for o in objects:
120 execute_model_policy(o, o.deleted)
121
122 for o in deleted_objects:
123 execute_model_policy(o, True)
124
Sapan Bhatia35dab9d2015-05-27 19:11:12 +0200125 # Reap non-sync'd models here
126 reaped = [Slice]
127
128 for m in reaped:
129 dobjs = m.deleted_objects.all()
130 for d in dobjs:
131 deps = walk_inv_deps(noop, d)
132 if (not deps):
133 print 'Purging object %r'%d
134 d.delete(purge=True)
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200135
Scott Baker5b961e02015-06-22 10:56:16 -0700136 try:
137 reset_queries()
138 except:
139 # this shouldn't happen, but in case it does, catch it...
140 logger.log_exc("exception in reset_queries")