blob: 752cb8605a1b5c3611e9075474e35069eeff107d [file] [log] [blame]
Scott Baker45fb7a12013-12-31 00:56:19 -08001import os
2import imp
3import inspect
Sapan Bhatia24836f12013-08-27 10:16:05 -04004import time
Sapan Bhatia6b6c2182015-01-27 03:58:11 +00005import sys
Sapan Bhatia24836f12013-08-27 10:16:05 -04006import traceback
7import commands
8import threading
9import json
Sapan Bhatiaab202a62014-09-03 11:30:21 -040010import pdb
Sapan Bhatia6b6c2182015-01-27 03:58:11 +000011import pprint
12
Sapan Bhatia24836f12013-08-27 10:16:05 -040013
14from datetime import datetime
15from collections import defaultdict
16from core.models import *
17from django.db.models import F, Q
Scott Bakerc7ca6552014-09-05 14:48:38 -070018from django.db import connection
Tony Mack387a73f2013-09-18 07:59:14 -040019#from openstack.manager import OpenStackManager
20from openstack.driver import OpenStackDriver
Sapan Bhatia24836f12013-08-27 10:16:05 -040021from util.logger import Logger, logging, logger
22#from timeout import timeout
Scott Baker76a840e2015-02-11 21:38:09 -080023from xos.config import Config, XOS_DIR
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040024from observer.steps import *
Scott Baker45fb7a12013-12-31 00:56:19 -080025from syncstep import SyncStep
Sapan Bhatia45cbbc32014-03-11 17:48:30 -040026from toposort import toposort
Sapan Bhatia13d89152014-07-23 10:35:33 -040027from observer.error_mapper import *
Sapan Bhatiacb6f8d62015-01-17 01:03:52 +000028from openstack_observer.openstacksyncstep import OpenStackSyncStep
29
Sapan Bhatia24836f12013-08-27 10:16:05 -040030
Sapan Bhatia13c7f112013-09-02 14:19:35 -040031debug_mode = False
Sapan Bhatia24836f12013-08-27 10:16:05 -040032
Sapan Bhatiacb6f8d62015-01-17 01:03:52 +000033class bcolors:
34 HEADER = '\033[95m'
35 OKBLUE = '\033[94m'
36 OKGREEN = '\033[92m'
37 WARNING = '\033[93m'
38 FAIL = '\033[91m'
39 ENDC = '\033[0m'
40 BOLD = '\033[1m'
41 UNDERLINE = '\033[4m'
42
Andy Bavier04111b72013-10-22 16:47:10 -040043logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040044
Sapan Bhatia13c7f112013-09-02 14:19:35 -040045class StepNotReady(Exception):
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040046 pass
Sapan Bhatia24836f12013-08-27 10:16:05 -040047
Scott Baker7771f412014-01-02 16:36:41 -080048class NoOpDriver:
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040049 def __init__(self):
50 self.enabled = True
Sapan Bhatiaab202a62014-09-03 11:30:21 -040051 self.dependency_graph = None
52
53STEP_STATUS_WORKING=1
54STEP_STATUS_OK=2
55STEP_STATUS_KO=3
56
57def invert_graph(g):
58 ig = {}
59 for k,v in g.items():
60 for v0 in v:
61 try:
62 ig[v0].append(k)
63 except:
64 ig=[k]
65 return ig
Scott Baker7771f412014-01-02 16:36:41 -080066
Sapan Bhatia24836f12013-08-27 10:16:05 -040067class PlanetStackObserver:
Tony Macka7dbd422015-01-05 22:48:11 -050068 #sync_steps = [SyncNetworks,SyncNetworkSlivers,SyncSites,SyncSitePrivilege,SyncSlices,SyncSliceMemberships,SyncSlivers,SyncSliverIps,SyncExternalRoutes,SyncUsers,SyncRoles,SyncNodes,SyncImages,GarbageCollector]
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040069 sync_steps = []
Sapan Bhatia24836f12013-08-27 10:16:05 -040070
Sapan Bhatiaab202a62014-09-03 11:30:21 -040071
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040072 def __init__(self):
73 # The Condition object that gets signalled by Feefie events
74 self.step_lookup = {}
75 self.load_sync_step_modules()
76 self.load_sync_steps()
77 self.event_cond = threading.Condition()
Scott Baker7771f412014-01-02 16:36:41 -080078
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040079 self.driver_kind = getattr(Config(), "observer_driver", "openstack")
80 if self.driver_kind=="openstack":
81 self.driver = OpenStackDriver()
82 else:
83 self.driver = NoOpDriver()
Sapan Bhatia24836f12013-08-27 10:16:05 -040084
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040085 def wait_for_event(self, timeout):
86 self.event_cond.acquire()
87 self.event_cond.wait(timeout)
88 self.event_cond.release()
Scott Baker45fb7a12013-12-31 00:56:19 -080089
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040090 def wake_up(self):
91 logger.info('Wake up routine called. Event cond %r'%self.event_cond)
92 self.event_cond.acquire()
93 self.event_cond.notify()
94 self.event_cond.release()
Sapan Bhatia24836f12013-08-27 10:16:05 -040095
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040096 def load_sync_step_modules(self, step_dir=None):
97 if step_dir is None:
98 if hasattr(Config(), "observer_steps_dir"):
99 step_dir = Config().observer_steps_dir
100 else:
Scott Bakerd9e01232015-02-04 16:59:45 -0800101 step_dir = XOS_DIR + "/observer/steps"
Scott Baker45fb7a12013-12-31 00:56:19 -0800102
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400103 for fn in os.listdir(step_dir):
104 pathname = os.path.join(step_dir,fn)
105 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
106 module = imp.load_source(fn[:-3],pathname)
107 for classname in dir(module):
108 c = getattr(module, classname, None)
Scott Baker45fb7a12013-12-31 00:56:19 -0800109
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400110 # make sure 'c' is a descendent of SyncStep and has a
111 # provides field (this eliminates the abstract base classes
112 # since they don't have a provides)
Scott Baker45fb7a12013-12-31 00:56:19 -0800113
Sapan Bhatia43c7f8c2015-01-17 01:04:10 +0000114 if inspect.isclass(c) and (issubclass(c, SyncStep) or issubclass(c,OpenStackSyncStep)) and hasattr(c,"provides") and (c not in self.sync_steps):
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400115 self.sync_steps.append(c)
116 logger.info('loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps]))
117 # print 'loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps])
Scott Baker45fb7a12013-12-31 00:56:19 -0800118
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400119 def load_sync_steps(self):
120 dep_path = Config().observer_dependency_graph
121 logger.info('Loading model dependency graph from %s' % dep_path)
122 try:
123 # This contains dependencies between records, not sync steps
124 self.model_dependency_graph = json.loads(open(dep_path).read())
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000125 for left,lst in self.model_dependency_graph.items():
126 new_lst = []
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000127 for k in lst:
128 try:
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000129 tup = (k,k.lower())
130 new_lst.append(tup)
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000131 deps = self.model_dependency_graph[k]
132 except:
133 self.model_dependency_graph[k] = []
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000134
135 self.model_dependency_graph[left] = new_lst
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400136 except Exception,e:
137 raise e
Sapan Bhatia24836f12013-08-27 10:16:05 -0400138
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400139 try:
140 backend_path = Config().observer_pl_dependency_graph
141 logger.info('Loading backend dependency graph from %s' % backend_path)
142 # This contains dependencies between backend records
143 self.backend_dependency_graph = json.loads(open(backend_path).read())
Sapan Bhatia0926e652015-01-29 20:51:13 +0000144 for k,v in self.backend_dependency_graph.items():
145 try:
146 self.model_dependency_graph[k].extend(v)
147 except KeyError:
148 self.model_dependency_graphp[k] = v
149
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400150 except Exception,e:
151 logger.info('Backend dependency graph not loaded')
152 # We can work without a backend graph
153 self.backend_dependency_graph = {}
Sapan Bhatia24836f12013-08-27 10:16:05 -0400154
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400155 provides_dict = {}
156 for s in self.sync_steps:
157 self.step_lookup[s.__name__] = s
158 for m in s.provides:
159 try:
160 provides_dict[m.__name__].append(s.__name__)
161 except KeyError:
162 provides_dict[m.__name__]=[s.__name__]
Sapan Bhatia04c94ad2013-09-02 18:00:28 -0400163
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400164 step_graph = {}
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000165 for k,v in self.model_dependency_graph.items():
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400166 try:
167 for source in provides_dict[k]:
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000168 if (not v):
169 step_graph[source] = []
170
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000171 for m,_ in v:
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400172 try:
173 for dest in provides_dict[m]:
174 # no deps, pass
175 try:
176 if (dest not in step_graph[source]):
177 step_graph[source].append(dest)
178 except:
179 step_graph[source]=[dest]
180 except KeyError:
181 pass
182
183 except KeyError:
184 pass
185 # no dependencies, pass
186
Sapan Bhatia24836f12013-08-27 10:16:05 -0400187
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400188 self.dependency_graph = step_graph
189 self.deletion_dependency_graph = invert_graph(step_graph)
Sapan Bhatia24836f12013-08-27 10:16:05 -0400190
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000191 pp = pprint.PrettyPrinter(indent=4)
192 pp.pprint(step_graph)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400193 self.ordered_steps = toposort(self.dependency_graph, map(lambda s:s.__name__,self.sync_steps))
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000194 #self.ordered_steps = ['SyncRoles', 'SyncControllerSites', 'SyncControllerSitePrivileges','SyncImages', 'SyncControllerImages','SyncControllerUsers','SyncControllerUserSitePrivileges','SyncControllerSlices', 'SyncControllerSlicePrivileges', 'SyncControllerUserSlicePrivileges', 'SyncControllerNetworks','SyncSlivers']
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000195 #self.ordered_steps = ['SyncControllerSites','SyncControllerUsers','SyncControllerSlices','SyncControllerNetworks']
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000196
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400197 print "Order of steps=",self.ordered_steps
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000198
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400199 self.load_run_times()
200
Sapan Bhatia24836f12013-08-27 10:16:05 -0400201
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400202 def check_duration(self, step, duration):
203 try:
204 if (duration > step.deadline):
205 logger.info('Sync step %s missed deadline, took %.2f seconds'%(step.name,duration))
206 except AttributeError:
207 # S doesn't have a deadline
208 pass
Sapan Bhatia24836f12013-08-27 10:16:05 -0400209
Sapan Bhatia285decb2014-04-30 00:31:44 -0400210 def update_run_time(self, step, deletion):
211 if (not deletion):
212 self.last_run_times[step.__name__]=time.time()
213 else:
214 self.last_deletion_run_times[step.__name__]=time.time()
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400215
Sapan Bhatia285decb2014-04-30 00:31:44 -0400216
217 def check_schedule(self, step, deletion):
218 last_run_times = self.last_run_times if not deletion else self.last_deletion_run_times
219
220 time_since_last_run = time.time() - last_run_times.get(step.__name__, 0)
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400221 try:
222 if (time_since_last_run < step.requested_interval):
223 raise StepNotReady
224 except AttributeError:
225 logger.info('Step %s does not have requested_interval set'%step.__name__)
226 raise StepNotReady
227
228 def load_run_times(self):
229 try:
230 jrun_times = open('/tmp/observer_run_times').read()
231 self.last_run_times = json.loads(jrun_times)
232 except:
233 self.last_run_times={}
234 for e in self.ordered_steps:
235 self.last_run_times[e]=0
Sapan Bhatia285decb2014-04-30 00:31:44 -0400236 try:
237 jrun_times = open('/tmp/observer_deletion_run_times').read()
238 self.last_deletion_run_times = json.loads(jrun_times)
239 except:
240 self.last_deletion_run_times={}
241 for e in self.ordered_steps:
242 self.last_deletion_run_times[e]=0
243
Sapan Bhatia36938ca2013-09-02 14:35:24 -0400244
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400245 def save_run_times(self):
246 run_times = json.dumps(self.last_run_times)
247 open('/tmp/observer_run_times','w').write(run_times)
Sapan Bhatia36938ca2013-09-02 14:35:24 -0400248
Sapan Bhatia285decb2014-04-30 00:31:44 -0400249 deletion_run_times = json.dumps(self.last_deletion_run_times)
250 open('/tmp/observer_deletion_run_times','w').write(deletion_run_times)
251
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400252 def check_class_dependency(self, step, failed_steps):
253 step.dependenices = []
254 for obj in step.provides:
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000255 lst = self.model_dependency_graph.get(obj.__name__, [])
256 nlst = map(lambda(a,b):b,lst)
257 step.dependenices.extend(nlst)
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400258 for failed_step in failed_steps:
259 if (failed_step in step.dependencies):
260 raise StepNotReady
261
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400262 def sync(self, S, deletion):
Scott Bakerc7ca6552014-09-05 14:48:38 -0700263 try:
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400264 step = self.step_lookup[S]
265 start_time=time.time()
Scott Bakeradc73172014-09-04 10:36:51 -0700266
267 logger.info("Starting to work on step %s" % step.__name__)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400268
269 dependency_graph = self.dependency_graph if not deletion else self.deletion_dependency_graph
Sapan Bhatia51f48932014-08-25 04:17:12 -0400270
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400271 # Wait for step dependencies to be met
272 try:
273 deps = self.dependency_graph[S]
274 has_deps = True
275 except KeyError:
276 has_deps = False
Sapan Bhatia51f48932014-08-25 04:17:12 -0400277
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000278 go = True
Sapan Bhatia475c5972014-11-05 10:32:41 -0500279
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000280 failed_dep = None
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400281 if (has_deps):
282 for d in deps:
Scott Bakeradc73172014-09-04 10:36:51 -0700283 if d==step.__name__:
284 logger.info(" step %s self-wait skipped" % step.__name__)
Sapan Bhatia475c5972014-11-05 10:32:41 -0500285 go = True
Scott Bakeradc73172014-09-04 10:36:51 -0700286 continue
287
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400288 cond = self.step_conditions[d]
289 cond.acquire()
290 if (self.step_status[d] is STEP_STATUS_WORKING):
Scott Bakeradc73172014-09-04 10:36:51 -0700291 logger.info(" step %s wait on dep %s" % (step.__name__, d))
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400292 cond.wait()
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000293 elif self.step_status[d] == STEP_STATUS_OK:
294 go = True
295 else:
296 go = False
297 failed_dep = d
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400298 cond.release()
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000299 if (not go):
300 break
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400301 else:
302 go = True
303
304 if (not go):
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000305 print bcolors.FAIL + "Step %r skipped on %r" % (step,failed_dep) + bcolors.ENDC
Scott Bakeradc73172014-09-04 10:36:51 -0700306 # SMBAKER: sync_step was not defined here, so I changed
307 # this from 'sync_step' to 'step'. Verify.
308 self.failed_steps.append(step)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400309 my_status = STEP_STATUS_KO
310 else:
311 sync_step = step(driver=self.driver,error_map=self.error_mapper)
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000312 sync_step. __name__= step.__name__
Sapan Bhatia51f48932014-08-25 04:17:12 -0400313 sync_step.dependencies = []
314 try:
315 mlist = sync_step.provides
Scott Bakeradc73172014-09-04 10:36:51 -0700316
Sapan Bhatia51f48932014-08-25 04:17:12 -0400317 for m in mlist:
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000318 lst = self.model_dependency_graph[m.__name__]
319 nlst = map(lambda(a,b):b,lst)
320 sync_step.dependencies.extend(nlst)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400321 except KeyError:
322 pass
323 sync_step.debug_mode = debug_mode
324
325 should_run = False
326 try:
327 # Various checks that decide whether
328 # this step runs or not
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400329 self.check_class_dependency(sync_step, self.failed_steps) # dont run Slices if Sites failed
Sapan Bhatia51f48932014-08-25 04:17:12 -0400330 self.check_schedule(sync_step, deletion) # dont run sync_network_routes if time since last run < 1 hour
331 should_run = True
332 except StepNotReady:
Scott Bakeradc73172014-09-04 10:36:51 -0700333 logger.info('Step not ready: %s'%sync_step.__name__)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400334 self.failed_steps.append(sync_step)
335 my_status = STEP_STATUS_KO
Sapan Bhatia51f48932014-08-25 04:17:12 -0400336 except Exception,e:
Scott Bakeradc73172014-09-04 10:36:51 -0700337 logger.error('%r' % e)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400338 logger.log_exc("sync step failed: %r. Deletion: %r"%(sync_step,deletion))
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400339 self.failed_steps.append(sync_step)
340 my_status = STEP_STATUS_KO
Sapan Bhatia51f48932014-08-25 04:17:12 -0400341
342 if (should_run):
343 try:
344 duration=time.time() - start_time
345
346 logger.info('Executing step %s' % sync_step.__name__)
347
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000348 print bcolors.OKBLUE + "Executing step %s" % sync_step.__name__ + bcolors.ENDC
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400349 failed_objects = sync_step(failed=list(self.failed_step_objects), deletion=deletion)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400350
351 self.check_duration(sync_step, duration)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400352
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400353 if failed_objects:
354 self.failed_step_objects.update(failed_objects)
355
Scott Bakeradc73172014-09-04 10:36:51 -0700356 logger.info("Step %r succeeded" % step)
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000357 print bcolors.OKGREEN + "Step %r succeeded" % step + bcolors.ENDC
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400358 my_status = STEP_STATUS_OK
Sapan Bhatia51f48932014-08-25 04:17:12 -0400359 self.update_run_time(sync_step,deletion)
360 except Exception,e:
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000361 print bcolors.FAIL + "Model step %r failed" % (step) + bcolors.ENDC
Scott Bakeradc73172014-09-04 10:36:51 -0700362 logger.error('Model step %r failed. This seems like a misconfiguration or bug: %r. This error will not be relayed to the user!' % (step, e))
Sapan Bhatia51f48932014-08-25 04:17:12 -0400363 logger.log_exc(e)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400364 self.failed_steps.append(S)
365 my_status = STEP_STATUS_KO
366 else:
Scott Bakeradc73172014-09-04 10:36:51 -0700367 logger.info("Step %r succeeded due to non-run" % step)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400368 my_status = STEP_STATUS_OK
Scott Bakeradc73172014-09-04 10:36:51 -0700369
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400370 try:
371 my_cond = self.step_conditions[S]
372 my_cond.acquire()
373 self.step_status[S]=my_status
374 my_cond.notify_all()
375 my_cond.release()
376 except KeyError,e:
Scott Bakeradc73172014-09-04 10:36:51 -0700377 logger.info('Step %r is a leaf' % step)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400378 pass
Scott Bakerc7ca6552014-09-05 14:48:38 -0700379 finally:
380 connection.close()
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400381
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400382 def run(self):
383 if not self.driver.enabled:
384 return
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400385
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400386 if (self.driver_kind=="openstack") and (not self.driver.has_openstack):
387 return
388
389 while True:
390 try:
Sapan Bhatiae122dcf2015-01-29 20:54:17 +0000391 loop_start = time.time()
Scott Bakerd9e01232015-02-04 16:59:45 -0800392 error_map_file = getattr(Config(), "error_map_path", XOS_DIR + "/error_map.txt")
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400393 self.error_mapper = ErrorMapper(error_map_file)
394
395 # Set of whole steps that failed
396 self.failed_steps = []
397
398 # Set of individual objects within steps that failed
399 self.failed_step_objects = set()
400
401 # Set up conditions and step status
402 # This is needed for steps to run in parallel
403 # while obeying dependencies.
404
405 providers = set()
406 for v in self.dependency_graph.values():
407 if (v):
408 providers.update(v)
409
410 self.step_conditions = {}
411 self.step_status = {}
412 for p in list(providers):
413 self.step_conditions[p] = threading.Condition()
414 self.step_status[p] = STEP_STATUS_WORKING
415
Sapan Bhatia31ebe5c2014-04-29 00:24:09 -0400416
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400417 logger.info('Waiting for event')
418 tBeforeWait = time.time()
Sapan Bhatia13d89152014-07-23 10:35:33 -0400419 self.wait_for_event(timeout=30)
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400420 logger.info('Observer woke up')
421
Sapan Bhatia285decb2014-04-30 00:31:44 -0400422 # Two passes. One for sync, the other for deletion.
Sapan Bhatia0f727b82014-08-18 02:44:20 -0400423 for deletion in [False,True]:
Sapan Bhatia51f48932014-08-25 04:17:12 -0400424 threads = []
Sapan Bhatiae82f5e52014-07-23 10:02:45 -0400425 logger.info('Deletion=%r...'%deletion)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400426 schedule = self.ordered_steps if not deletion else reversed(self.ordered_steps)
427
428 for S in schedule:
429 thread = threading.Thread(target=self.sync, args=(S, deletion))
430
431 logger.info('Deletion=%r...'%deletion)
432 threads.append(thread)
Sapan Bhatia285decb2014-04-30 00:31:44 -0400433
Sapan Bhatia51f48932014-08-25 04:17:12 -0400434 # Start threads
435 for t in threads:
436 t.start()
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400437
Sapan Bhatia51f48932014-08-25 04:17:12 -0400438 # Wait for all threads to finish before continuing with the run loop
439 for t in threads:
440 t.join()
Sapan Bhatia285decb2014-04-30 00:31:44 -0400441
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400442 self.save_run_times()
Sapan Bhatiae122dcf2015-01-29 20:54:17 +0000443 loop_end = time.time()
444 open('/tmp/observer_last_run','w').write(json.dumps({'last_run': loop_end, 'last_duration':loop_end - loop_start}))
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400445 except Exception, e:
Scott Bakeradc73172014-09-04 10:36:51 -0700446 logger.error('Core error. This seems like a misconfiguration or bug: %r. This error will not be relayed to the user!' % e)
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400447 logger.log_exc("Exception in observer run loop")
448 traceback.print_exc()