blob: f59a9cc3ba046cae66a01c52f17aa9cce6c17779 [file] [log] [blame]
Zack Williamsc7b10f52016-04-29 16:46:26 -07001from core.models import *
2from datetime import datetime
3from django.db import reset_queries
4from django.db.models import F, Q
Sapan Bhatia0235e982014-09-03 13:14:40 -04005from django.db.models.signals import post_save
Zack Williamsc7b10f52016-04-29 16:46:26 -07006from django.db.transaction import atomic
Sapan Bhatia0235e982014-09-03 13:14:40 -04007from django.dispatch import receiver
Zack Williamsaa4c87f2016-05-10 09:59:56 -07008from django.utils import timezone
Sapan Bhatiaa19736a2016-01-15 11:21:20 -05009from generate.dependency_walker import *
Sapan Bhatia7edfe7f2016-01-14 14:16:33 -050010from synchronizers.openstack import model_policies
Scott Baker3586c552016-01-14 15:30:20 -080011from xos.logger import logger
Zack Williamsc7b10f52016-04-29 16:46:26 -070012
13import pdb
Sapan Bhatiab3507b92015-01-23 16:09:28 +000014import time
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000015
Scott Bakerf3428702014-10-21 15:05:29 -070016modelPolicyEnabled = True
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020017bad_instances=[]
Scott Bakerf3428702014-10-21 15:05:29 -070018
19def EnableModelPolicy(x):
20 global modelPolicyEnabled
21 modelPolicyEnabled = x
22
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020023def update_wp(d, o):
24 try:
25 save_fields = []
26 if (d.write_protect != o.write_protect):
27 d.write_protect = o.write_protect
28 save_fields.append('write_protect')
29 if (save_fields):
30 d.save(update_fields=save_fields)
31 except AttributeError,e:
32 raise e
33
Sapan Bhatia14356b72014-11-05 10:32:41 -050034def update_dep(d, o):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020035 try:
36 print 'Trying to update %s'%d
37 save_fields = []
38 if (d.updated < o.updated):
39 save_fields = ['updated']
40
41 if (save_fields):
42 d.save(update_fields=save_fields)
43 except AttributeError,e:
44 raise e
Scott Baker5b961e02015-06-22 10:56:16 -070045 except Exception,e:
Sapan Bhatia91c497e2016-04-06 19:04:05 +020046 logger.info('Could not save %r. Exception: %r'%(d,e), extra=d.tologdict())
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020047
Sapan Bhatia14356b72014-11-05 10:32:41 -050048def delete_if_inactive(d, o):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020049 try:
50 d.delete()
51 print "Deleted %s (%s)"%(d,d.__class__.__name__)
52 except:
53 pass
54 return
55
Sapan Bhatia14356b72014-11-05 10:32:41 -050056
Sapan Bhatia8863b082016-03-23 19:32:14 +010057#@atomic
Sapan Bhatiab3507b92015-01-23 16:09:28 +000058def execute_model_policy(instance, deleted):
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020059 # Automatic dirtying
60 if (instance in bad_instances):
61 return
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000062
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020063 # These are the models whose children get deleted when they are
Tony Mackd8515472015-08-19 11:58:18 -040064 delete_policy_models = ['Slice','Instance','Network']
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020065 sender_name = instance.__class__.__name__
66 policy_name = 'model_policy_%s'%sender_name
67 noargs = False
Sapan Bhatiab3507b92015-01-23 16:09:28 +000068
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020069 if (not deleted):
70 walk_inv_deps(update_dep, instance)
71 walk_deps(update_wp, instance)
72 elif (sender_name in delete_policy_models):
73 walk_inv_deps(delete_if_inactive, instance)
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000074
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020075
76
77 try:
78 policy_handler = getattr(model_policies, policy_name, None)
79 logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler))
80 if policy_handler is not None:
81 if (deleted):
82 try:
83 policy_handler.handle_delete(instance)
84 except AttributeError:
85 pass
86 else:
87 policy_handler.handle(instance)
88 except:
89 logger.log_exc("Model Policy Error:")
90
91 try:
Sapan Bhatia80b83cb2016-05-06 15:20:13 -040092 instance.policed=timezone.now()
Sapan Bhatiab3507b92015-01-23 16:09:28 +000093 instance.save(update_fields=['policed'])
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020094 except:
Zack Williamsc7b10f52016-04-29 16:46:26 -070095 logger.error('Object %r is defective'%instance)
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +020096 bad_instances.append(instance)
Sapan Bhatia7a1e6be2015-01-16 22:17:42 +000097
Sapan Bhatia35dab9d2015-05-27 19:11:12 +020098def noop(o,p):
99 pass
100
Sapan Bhatiab3507b92015-01-23 16:09:28 +0000101def run_policy():
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200102 while (True):
103 start = time.time()
Scott Baker30627782015-09-17 16:58:36 -0700104 run_policy_once()
105 if (time.time()-start<1):
106 time.sleep(1)
107
108def run_policy_once():
109 from core.models import Instance,Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice,ControllerUser,ControllerSite
Sapan Bhatiaf6d6be72015-11-25 16:38:02 +0100110 models = [Controller, Site, SitePrivilege, Image, ControllerSlice, ControllerSite, ControllerUser, User, Slice, Network, Instance, SlicePrivilege]
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200111 objects = []
112 deleted_objects = []
Sapan Bhatia14356b72014-11-05 10:32:41 -0500113
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200114 for m in models:
Scott Baker6380cbf2016-04-14 23:41:07 -0700115 res = m.objects.filter((Q(policed__lt=F('updated')) | Q(policed=None)) & Q(no_policy=False))
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200116 objects.extend(res)
117 res = m.deleted_objects.filter(Q(policed__lt=F('updated')) | Q(policed=None))
118 deleted_objects.extend(res)
119
120 for o in objects:
121 execute_model_policy(o, o.deleted)
122
123 for o in deleted_objects:
124 execute_model_policy(o, True)
125
Sapan Bhatia35dab9d2015-05-27 19:11:12 +0200126 # Reap non-sync'd models here
127 reaped = [Slice]
128
129 for m in reaped:
130 dobjs = m.deleted_objects.all()
131 for d in dobjs:
132 deps = walk_inv_deps(noop, d)
133 if (not deps):
134 print 'Purging object %r'%d
135 d.delete(purge=True)
Sapan Bhatiaa4fadac2015-05-09 18:10:17 +0200136
Scott Baker5b961e02015-06-22 10:56:16 -0700137 try:
138 reset_queries()
139 except:
140 # this shouldn't happen, but in case it does, catch it...
141 logger.log_exc("exception in reset_queries")
Zack Williamsaa4c87f2016-05-10 09:59:56 -0700142