blob: 21327d74903b7bade4efa7202edc14f13d97af61 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mack4fa85fb2013-09-25 14:39:57 -04003from datetime import datetime
Scott Baker76a840e2015-02-11 21:38:09 -08004from xos.config import Config
Andy Baviere7abb622013-10-18 15:11:56 -04005from util.logger import Logger, logging
Sapan Bhatiaeba08432014-04-28 23:58:36 -04006from observer.steps import *
Sapan Bhatiad9468eb2014-08-20 03:03:12 -04007from django.db.models import F, Q
Scott Baker8bbc77c2015-06-22 10:56:16 -07008from core.models import *
9from django.db import reset_queries
Sapan Bhatia7fdccee2015-09-16 16:42:38 +020010from observer.ansible import *
Sapan Bhatia6adccce2015-11-03 13:54:59 -050011from dependency_walker import *
Sapan Bhatia7fdccee2015-09-16 16:42:38 +020012
Sapan Bhatia47006112015-01-29 20:55:40 +000013import json
14import time
15import pdb
Andy Baviere7abb622013-10-18 15:11:56 -040016
Andy Bavier04111b72013-10-22 16:47:10 -040017logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040018
Sapan Bhatia2192fec2015-02-08 06:36:32 +000019def f7(seq):
20 seen = set()
21 seen_add = seen.add
22 return [ x for x in seq if not (x in seen or seen_add(x))]
23
24def elim_dups(backend_str):
25 strs = backend_str.split(' // ')
26 strs2 = f7(strs)
27 return ' // '.join(strs2)
Sapan Bhatiae6376de2015-05-13 15:51:03 +020028
Sapan Bhatia709bebd2015-02-08 06:35:36 +000029def deepgetattr(obj, attr):
30 return reduce(getattr, attr.split('.'), obj)
31
Sapan Bhatiae6376de2015-05-13 15:51:03 +020032
33class InnocuousException(Exception):
34 pass
35
Scott Baker7ed0b762015-08-25 16:23:37 -070036class DeferredException(Exception):
37 pass
38
Sapan Bhatia13c7f112013-09-02 14:19:35 -040039class FailedDependency(Exception):
Tony Mackce79de02013-09-24 10:12:33 -040040 pass
Sapan Bhatia13c7f112013-09-02 14:19:35 -040041
Tony Mackb469d242015-01-03 19:37:39 -050042class SyncStep(object):
Sapan Bhatiae6376de2015-05-13 15:51:03 +020043 """ An XOS Sync step.
Sapan Bhatia24836f12013-08-27 10:16:05 -040044
Tony Mackce79de02013-09-24 10:12:33 -040045 Attributes:
Sapan Bhatiae6376de2015-05-13 15:51:03 +020046 psmodel Model name the step synchronizes
Tony Mackce79de02013-09-24 10:12:33 -040047 dependencies list of names of models that must be synchronized first if the current model depends on them
Sapan Bhatiae6376de2015-05-13 15:51:03 +020048 """
Scott Baker1a36d662015-10-12 18:28:00 -070049
50 # map_sync_outputs can return this value to cause a step to be marked
51 # successful without running ansible. Used for sync_network_controllers
52 # on nat networks.
53 SYNC_WITHOUT_RUNNING = "sync_without_running"
54
Tony Mackce79de02013-09-24 10:12:33 -040055 slow=False
Tony Mackfbdae1b2015-02-24 14:16:43 -050056 def get_prop(self, prop):
Tony Mackce79de02013-09-24 10:12:33 -040057 try:
58 sync_config_dir = Config().sync_config_dir
59 except:
Scott Bakerb8059c92015-02-19 22:25:49 -080060 sync_config_dir = '/etc/xos/sync'
Tony Mackce79de02013-09-24 10:12:33 -040061 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
62 return open(prop_config_path).read().rstrip()
Sapan Bhatia24836f12013-08-27 10:16:05 -040063
Tony Mackce79de02013-09-24 10:12:33 -040064 def __init__(self, **args):
65 """Initialize a sync step
66 Keyword arguments:
67 name -- Name of the step
Scott Baker286a78f2015-02-18 16:13:48 -080068 provides -- XOS models sync'd by this step
Tony Mackce79de02013-09-24 10:12:33 -040069 """
70 dependencies = []
Tony Mack387a73f2013-09-18 07:59:14 -040071 self.driver = args.get('driver')
Sapan Bhatiaeba08432014-04-28 23:58:36 -040072 self.error_map = args.get('error_map')
73
Tony Mackce79de02013-09-24 10:12:33 -040074 try:
75 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
76 except:
77 self.soft_deadline = 5 # 5 seconds
Sapan Bhatia24836f12013-08-27 10:16:05 -040078
Tony Mackce79de02013-09-24 10:12:33 -040079 return
Sapan Bhatia24836f12013-08-27 10:16:05 -040080
Sapan Bhatiae17bc5b2014-04-30 00:53:06 -040081 def fetch_pending(self, deletion=False):
Sapan Bhatia21765662014-07-23 08:59:30 -040082 # This is the most common implementation of fetch_pending
83 # Steps should override it if they have their own logic
84 # for figuring out what objects are outstanding.
Sapan Bhatiaab18ee42015-08-19 12:20:30 -040085
86 main_objs = self.observes
87 if (type(main_objs) is not list):
Sapan Bhatia83e7a642015-09-10 11:08:35 -040088 main_objs=[main_objs]
Sapan Bhatiaab18ee42015-08-19 12:20:30 -040089
90 objs = []
91 for main_obj in main_objs:
92 if (not deletion):
Sapan Bhatiad063f5f2015-09-16 17:42:07 +020093 lobjs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False),Q(no_sync=False))
Sapan Bhatiaab18ee42015-08-19 12:20:30 -040094 else:
95 lobjs = main_obj.deleted_objects.all()
96 objs.extend(lobjs)
Sapan Bhatia21765662014-07-23 08:59:30 -040097
98 return objs
Tony Mack3de59e32015-08-19 11:58:18 -040099 #return Instance.objects.filter(ip=None)
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200100
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -0400101 def check_dependencies(self, obj, failed):
Tony Mackce79de02013-09-24 10:12:33 -0400102 for dep in self.dependencies:
Scott Baker105b6b72014-05-12 10:40:25 -0700103 peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200104
Sapan Bhatia6adccce2015-11-03 13:54:59 -0500105 peer_objects=[]
Sapan Bhatiacfef6ef2014-08-20 03:04:03 -0400106 try:
Sapan Bhatia6adccce2015-11-03 13:54:59 -0500107 peer_names = plural(peer_name)
108 peer_object_list=[]
109
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200110 try:
Sapan Bhatia6adccce2015-11-03 13:54:59 -0500111 peer_object_list.append(deepgetattr(obj, peer_name))
112 except:
113 pass
114
115 try:
116 peer_object_list.append(deepgetattr(obj, peer_names))
117 except:
118 pass
119
120 for peer_object in peer_object_list:
121 try:
122 peer_objects.extend(peer_object.all())
123 except AttributeError:
124 peer_objects.append(peer_object)
Sapan Bhatiacfef6ef2014-08-20 03:04:03 -0400125 except:
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000126 peer_objects = []
Sapan Bhatiacfef6ef2014-08-20 03:04:03 -0400127
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200128 if (hasattr(obj,'controller')):
129 try:
130 peer_objects = filter(lambda o:o.controller==obj.controller, peer_objects)
131 except AttributeError:
132 pass
133
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000134 if (failed in peer_objects):
135 if (obj.backend_status!=failed.backend_status):
136 obj.backend_status = failed.backend_status
Sapan Bhatia7e482de2014-08-22 03:05:13 -0400137 obj.save(update_fields=['backend_status'])
Scott Baker4fd314e2015-03-04 21:31:14 -0800138 raise FailedDependency("Failed dependency for %s:%s peer %s:%s failed %s:%s" % (obj.__class__.__name__, str(getattr(obj,"pk","no_pk")), peer_object.__class__.__name__, str(getattr(peer_object,"pk","no_pk")), failed.__class__.__name__, str(getattr(failed,"pk","no_pk"))))
Sapan Bhatia24836f12013-08-27 10:16:05 -0400139
Sapan Bhatia7fdccee2015-09-16 16:42:38 +0200140
141 def sync_record(self, o):
142 try:
143 controller = o.get_controller()
144 controller_register = json.loads(o.node.site_deployment.controller.backend_register)
145
146 if (controller_register.get('disabled',False)):
147 raise InnocuousException('Controller %s is disabled'%sliver.node.site_deployment.controller.name)
148 except AttributeError:
149 pass
150
151 tenant_fields = self.map_sync_inputs(o)
Scott Baker1a36d662015-10-12 18:28:00 -0700152 if tenant_fields == SyncStep.SYNC_WITHOUT_RUNNING:
153 return
Sapan Bhatia7fdccee2015-09-16 16:42:38 +0200154 main_objs=self.observes
155 if (type(main_objs) is list):
156 main_objs=main_objs[0]
157
158 path = ''.join(main_objs.__name__).lower()
159 res = run_template(self.playbook,tenant_fields,path=path)
160
161 try:
162 self.map_sync_outputs(o,res)
163 except AttributeError:
164 pass
165
166 def delete_record(self, o):
167 try:
168 controller = o.get_controller()
169 controller_register = json.loads(o.node.site_deployment.controller.backend_register)
170
171 if (controller_register.get('disabled',False)):
172 raise InnocuousException('Controller %s is disabled'%sliver.node.site_deployment.controller.name)
173 except AttributeError:
174 pass
175
Scott Bakerf7cb9132015-09-17 22:04:57 -0700176 tenant_fields = self.map_delete_inputs(o)
Sapan Bhatia7fdccee2015-09-16 16:42:38 +0200177
178 main_objs=self.observes
179 if (type(main_objs) is list):
180 main_objs=main_objs[0]
181
182 path = ''.join(main_objs.__name__).lower()
183
184 tenant_fields['delete']=True
185 res = run_template(self.playbook,tenant_fields,path=path)
186 try:
187 self.map_delete_outputs(o,res)
188 except AttributeError:
189 pass
190
Sapan Bhatia60823362014-04-30 00:52:32 -0400191 def call(self, failed=[], deletion=False):
Sapan Bhatia6adccce2015-11-03 13:54:59 -0500192 #if ('Instance' in self.__class__.__name__):
193 # pdb.set_trace()
194
Sapan Bhatia60823362014-04-30 00:52:32 -0400195 pending = self.fetch_pending(deletion)
Sapan Bhatia7fdccee2015-09-16 16:42:38 +0200196
Tony Mackce79de02013-09-24 10:12:33 -0400197 for o in pending:
Scott Baker8bbc77c2015-06-22 10:56:16 -0700198 # another spot to clean up debug state
199 try:
200 reset_queries()
201 except:
202 # this shouldn't happen, but in case it does, catch it...
203 logger.log_exc("exception in reset_queries")
204
Sapan Bhatia47006112015-01-29 20:55:40 +0000205 sync_failed = False
Tony Mack68e818d2013-09-25 13:34:17 -0400206 try:
Sapan Bhatia24a2a292015-02-10 17:21:33 -0500207 backoff_disabled = Config().observer_backoff_disabled
Sapan Bhatia9cd17be2015-02-10 17:16:07 -0500208 except:
209 backoff_disabled = 0
210
211 try:
Sapan Bhatia47006112015-01-29 20:55:40 +0000212 scratchpad = json.loads(o.backend_register)
213 if (scratchpad):
214 next_run = scratchpad['next_run']
Sapan Bhatia9cd17be2015-02-10 17:16:07 -0500215 if (not backoff_disabled and next_run>time.time()):
Sapan Bhatia47006112015-01-29 20:55:40 +0000216 sync_failed = True
Sapan Bhatia47006112015-01-29 20:55:40 +0000217 except:
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200218 logger.log_exc("Exception while loading scratchpad")
Sapan Bhatia47006112015-01-29 20:55:40 +0000219 pass
220
221 if (not sync_failed):
Sapan Bhatiaeba08432014-04-28 23:58:36 -0400222 try:
Sapan Bhatia47006112015-01-29 20:55:40 +0000223 for f in failed:
224 self.check_dependencies(o,f) # Raises exception if failed
225 if (deletion):
226 self.delete_record(o)
227 o.delete(purge=True)
228 else:
229 self.sync_record(o)
230 o.enacted = datetime.now() # Is this the same timezone? XXX
231 scratchpad = {'next_run':0, 'exponent':0}
232 o.backend_register = json.dumps(scratchpad)
233 o.backend_status = "1 - OK"
234 o.save(update_fields=['enacted','backend_status','backend_register'])
Scott Baker7ed0b762015-08-25 16:23:37 -0700235 except (InnocuousException,Exception,DeferredException) as e:
Sapan Bhatia47006112015-01-29 20:55:40 +0000236 logger.log_exc("sync step failed!")
Sapan Bhatia2175c1d2015-02-08 06:31:42 +0000237 try:
238 if (o.backend_status.startswith('2 - ')):
239 str_e = '%s // %r'%(o.backend_status[4:],e)
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200240 str_e = elim_dups(str_e)
Sapan Bhatia2175c1d2015-02-08 06:31:42 +0000241 else:
242 str_e = '%r'%e
243 except:
244 str_e = '%r'%e
245
Sapan Bhatia9c308fc2014-08-22 03:07:59 -0400246 try:
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200247 error = self.error_map.map(str_e)
Sapan Bhatia9c308fc2014-08-22 03:07:59 -0400248 except:
Sapan Bhatiac368d4a2015-06-09 14:14:12 -0400249 error = '%s'%str_e
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200250
251 if isinstance(e, InnocuousException) and not force_error:
252 o.backend_status = '1 - %s'%error
253 else:
Sapan Bhatia5e2f87a2015-05-13 15:52:45 +0200254 o.backend_status = '2 - %s'%error
Sapan Bhatiaeba08432014-04-28 23:58:36 -0400255
Sapan Bhatia47006112015-01-29 20:55:40 +0000256 try:
257 scratchpad = json.loads(o.backend_register)
258 scratchpad['exponent']
259 except:
Sapan Bhatiae6376de2015-05-13 15:51:03 +0200260 logger.log_exc("Exception while updating scratchpad")
Sapan Bhatia47006112015-01-29 20:55:40 +0000261 scratchpad = {'next_run':0, 'exponent':0}
262
263 # Second failure
264 if (scratchpad['exponent']):
Scott Baker7ed0b762015-08-25 16:23:37 -0700265 if isinstance(e,DeferredException):
266 delay = scratchpad['exponent'] * 60 # 1 minute
267 else:
268 delay = scratchpad['exponent'] * 600 # 10 minutes
269 # cap delays at 8 hours
270 if (delay>8*60*60):
271 delay=8*60*60
Sapan Bhatia47006112015-01-29 20:55:40 +0000272 scratchpad['next_run'] = time.time() + delay
273
274 scratchpad['exponent']+=1
275
276 o.backend_register = json.dumps(scratchpad)
277
278 # TOFIX:
279 # DatabaseError: value too long for type character varying(140)
280 if (o.pk):
281 try:
Sapan Bhatia2175c1d2015-02-08 06:31:42 +0000282 o.backend_status = o.backend_status[:1024]
Sapan Bhatia5e2f87a2015-05-13 15:52:45 +0200283 o.save(update_fields=['backend_status','backend_register','updated'])
Sapan Bhatia47006112015-01-29 20:55:40 +0000284 except:
285 print "Could not update backend status field!"
286 pass
287 sync_failed = True
288
289
290 if (sync_failed):
Tony Mack68e818d2013-09-25 13:34:17 -0400291 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -0400292
Tony Mackce79de02013-09-24 10:12:33 -0400293 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -0400294
Tony Mack16f04742013-09-25 08:53:28 -0400295 def __call__(self, **args):
296 return self.call(**args)