blob: a63ff3c127ec108accd284a6d90fda1bace6e1dc [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:
Sapan Bhatia47944b32015-03-04 10:21:06 -050064 ig[v0]=[k]
Sapan Bhatiaab202a62014-09-03 11:30:21 -040065 return ig
Scott Baker7771f412014-01-02 16:36:41 -080066
Scott Baker286a78f2015-02-18 16:13:48 -080067class XOSObserver:
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")
Scott Bakerf0db81c2015-03-31 15:06:55 -070080 self.observer_name = getattr(Config(), "observer_name", "")
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040081 if self.driver_kind=="openstack":
82 self.driver = OpenStackDriver()
83 else:
84 self.driver = NoOpDriver()
Sapan Bhatia24836f12013-08-27 10:16:05 -040085
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040086 def wait_for_event(self, timeout):
87 self.event_cond.acquire()
88 self.event_cond.wait(timeout)
89 self.event_cond.release()
Scott Baker45fb7a12013-12-31 00:56:19 -080090
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040091 def wake_up(self):
92 logger.info('Wake up routine called. Event cond %r'%self.event_cond)
93 self.event_cond.acquire()
94 self.event_cond.notify()
95 self.event_cond.release()
Sapan Bhatia24836f12013-08-27 10:16:05 -040096
Sapan Bhatiaf73664b2014-04-28 13:07:18 -040097 def load_sync_step_modules(self, step_dir=None):
98 if step_dir is None:
99 if hasattr(Config(), "observer_steps_dir"):
100 step_dir = Config().observer_steps_dir
101 else:
Scott Bakerd9e01232015-02-04 16:59:45 -0800102 step_dir = XOS_DIR + "/observer/steps"
Scott Baker45fb7a12013-12-31 00:56:19 -0800103
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400104 for fn in os.listdir(step_dir):
105 pathname = os.path.join(step_dir,fn)
106 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
107 module = imp.load_source(fn[:-3],pathname)
108 for classname in dir(module):
109 c = getattr(module, classname, None)
Scott Baker45fb7a12013-12-31 00:56:19 -0800110
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400111 # make sure 'c' is a descendent of SyncStep and has a
112 # provides field (this eliminates the abstract base classes
113 # since they don't have a provides)
Scott Baker45fb7a12013-12-31 00:56:19 -0800114
Sapan Bhatia43c7f8c2015-01-17 01:04:10 +0000115 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 -0400116 self.sync_steps.append(c)
117 logger.info('loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps]))
118 # print 'loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps])
Scott Baker45fb7a12013-12-31 00:56:19 -0800119
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400120 def load_sync_steps(self):
121 dep_path = Config().observer_dependency_graph
122 logger.info('Loading model dependency graph from %s' % dep_path)
123 try:
124 # This contains dependencies between records, not sync steps
125 self.model_dependency_graph = json.loads(open(dep_path).read())
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000126 for left,lst in self.model_dependency_graph.items():
127 new_lst = []
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000128 for k in lst:
129 try:
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000130 tup = (k,k.lower())
131 new_lst.append(tup)
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000132 deps = self.model_dependency_graph[k]
133 except:
134 self.model_dependency_graph[k] = []
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000135
136 self.model_dependency_graph[left] = new_lst
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400137 except Exception,e:
138 raise e
Sapan Bhatia24836f12013-08-27 10:16:05 -0400139
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400140 try:
141 backend_path = Config().observer_pl_dependency_graph
142 logger.info('Loading backend dependency graph from %s' % backend_path)
143 # This contains dependencies between backend records
144 self.backend_dependency_graph = json.loads(open(backend_path).read())
Sapan Bhatia0926e652015-01-29 20:51:13 +0000145 for k,v in self.backend_dependency_graph.items():
146 try:
147 self.model_dependency_graph[k].extend(v)
148 except KeyError:
149 self.model_dependency_graphp[k] = v
150
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400151 except Exception,e:
152 logger.info('Backend dependency graph not loaded')
153 # We can work without a backend graph
154 self.backend_dependency_graph = {}
Sapan Bhatia24836f12013-08-27 10:16:05 -0400155
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400156 provides_dict = {}
157 for s in self.sync_steps:
158 self.step_lookup[s.__name__] = s
159 for m in s.provides:
160 try:
161 provides_dict[m.__name__].append(s.__name__)
162 except KeyError:
163 provides_dict[m.__name__]=[s.__name__]
Sapan Bhatia04c94ad2013-09-02 18:00:28 -0400164
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400165 step_graph = {}
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000166 for k,v in self.model_dependency_graph.items():
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400167 try:
168 for source in provides_dict[k]:
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000169 if (not v):
170 step_graph[source] = []
171
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000172 for m,_ in v:
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400173 try:
174 for dest in provides_dict[m]:
175 # no deps, pass
176 try:
177 if (dest not in step_graph[source]):
178 step_graph[source].append(dest)
179 except:
180 step_graph[source]=[dest]
181 except KeyError:
182 pass
183
184 except KeyError:
185 pass
186 # no dependencies, pass
187
Sapan Bhatia24836f12013-08-27 10:16:05 -0400188
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400189 self.dependency_graph = step_graph
190 self.deletion_dependency_graph = invert_graph(step_graph)
Sapan Bhatia24836f12013-08-27 10:16:05 -0400191
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000192 pp = pprint.PrettyPrinter(indent=4)
193 pp.pprint(step_graph)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400194 self.ordered_steps = toposort(self.dependency_graph, map(lambda s:s.__name__,self.sync_steps))
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000195 #self.ordered_steps = ['SyncRoles', 'SyncControllerSites', 'SyncControllerSitePrivileges','SyncImages', 'SyncControllerImages','SyncControllerUsers','SyncControllerUserSitePrivileges','SyncControllerSlices', 'SyncControllerSlicePrivileges', 'SyncControllerUserSlicePrivileges', 'SyncControllerNetworks','SyncSlivers']
Sapan Bhatia9028c9a2015-05-09 18:14:40 +0200196 #self.ordered_steps = ['SyncControllerSites','SyncRoles','SyncControllerUsers','SyncControllerSlices','SyncControllerNetworks']
197 #self.ordered_steps = ['SyncControllerNetworks']
Scott Bakerf0db81c2015-03-31 15:06:55 -0700198 #self.ordered_steps = ['SyncSlivers','SyncNetworkSlivers']
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000199
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400200 print "Order of steps=",self.ordered_steps
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000201
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400202 self.load_run_times()
203
Sapan Bhatia24836f12013-08-27 10:16:05 -0400204
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400205 def check_duration(self, step, duration):
206 try:
207 if (duration > step.deadline):
208 logger.info('Sync step %s missed deadline, took %.2f seconds'%(step.name,duration))
209 except AttributeError:
210 # S doesn't have a deadline
211 pass
Sapan Bhatia24836f12013-08-27 10:16:05 -0400212
Sapan Bhatia285decb2014-04-30 00:31:44 -0400213 def update_run_time(self, step, deletion):
214 if (not deletion):
215 self.last_run_times[step.__name__]=time.time()
216 else:
217 self.last_deletion_run_times[step.__name__]=time.time()
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400218
Sapan Bhatia285decb2014-04-30 00:31:44 -0400219
220 def check_schedule(self, step, deletion):
221 last_run_times = self.last_run_times if not deletion else self.last_deletion_run_times
222
223 time_since_last_run = time.time() - last_run_times.get(step.__name__, 0)
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400224 try:
225 if (time_since_last_run < step.requested_interval):
226 raise StepNotReady
227 except AttributeError:
228 logger.info('Step %s does not have requested_interval set'%step.__name__)
229 raise StepNotReady
230
231 def load_run_times(self):
232 try:
Scott Bakerf0db81c2015-03-31 15:06:55 -0700233 jrun_times = open('/tmp/%sobserver_run_times'%self.observer_name).read()
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400234 self.last_run_times = json.loads(jrun_times)
235 except:
236 self.last_run_times={}
237 for e in self.ordered_steps:
238 self.last_run_times[e]=0
Sapan Bhatia285decb2014-04-30 00:31:44 -0400239 try:
Scott Bakerf0db81c2015-03-31 15:06:55 -0700240 jrun_times = open('/tmp/%sobserver_deletion_run_times'%self.observer_name).read()
Sapan Bhatia285decb2014-04-30 00:31:44 -0400241 self.last_deletion_run_times = json.loads(jrun_times)
242 except:
243 self.last_deletion_run_times={}
244 for e in self.ordered_steps:
245 self.last_deletion_run_times[e]=0
246
Sapan Bhatia36938ca2013-09-02 14:35:24 -0400247
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400248 def save_run_times(self):
249 run_times = json.dumps(self.last_run_times)
Scott Bakerf0db81c2015-03-31 15:06:55 -0700250 open('/tmp/%sobserver_run_times'%self.observer_name,'w').write(run_times)
Sapan Bhatia36938ca2013-09-02 14:35:24 -0400251
Sapan Bhatia285decb2014-04-30 00:31:44 -0400252 deletion_run_times = json.dumps(self.last_deletion_run_times)
Scott Bakerf0db81c2015-03-31 15:06:55 -0700253 open('/tmp/%sobserver_deletion_run_times'%self.observer_name,'w').write(deletion_run_times)
Sapan Bhatia285decb2014-04-30 00:31:44 -0400254
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400255 def check_class_dependency(self, step, failed_steps):
256 step.dependenices = []
257 for obj in step.provides:
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000258 lst = self.model_dependency_graph.get(obj.__name__, [])
259 nlst = map(lambda(a,b):b,lst)
260 step.dependenices.extend(nlst)
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400261 for failed_step in failed_steps:
262 if (failed_step in step.dependencies):
263 raise StepNotReady
264
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400265 def sync(self, S, deletion):
Scott Bakerc7ca6552014-09-05 14:48:38 -0700266 try:
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400267 step = self.step_lookup[S]
268 start_time=time.time()
Scott Bakeradc73172014-09-04 10:36:51 -0700269
Scott Bakerc9e18622015-03-09 16:25:11 -0700270 logger.info("Starting to work on step %s, deletion=%s" % (step.__name__, str(deletion)))
Scott Bakerf0db81c2015-03-31 15:06:55 -0700271
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400272 dependency_graph = self.dependency_graph if not deletion else self.deletion_dependency_graph
Sapan Bhatia47944b32015-03-04 10:21:06 -0500273 step_conditions = self.step_conditions# if not deletion else self.deletion_step_conditions
274 step_status = self.step_status# if not deletion else self.deletion_step_status
Sapan Bhatia51f48932014-08-25 04:17:12 -0400275
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400276 # Wait for step dependencies to be met
277 try:
Sapan Bhatia47944b32015-03-04 10:21:06 -0500278 deps = dependency_graph[S]
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400279 has_deps = True
280 except KeyError:
281 has_deps = False
Sapan Bhatia51f48932014-08-25 04:17:12 -0400282
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000283 go = True
Sapan Bhatia475c5972014-11-05 10:32:41 -0500284
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000285 failed_dep = None
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400286 if (has_deps):
287 for d in deps:
Scott Bakeradc73172014-09-04 10:36:51 -0700288 if d==step.__name__:
289 logger.info(" step %s self-wait skipped" % step.__name__)
Sapan Bhatia475c5972014-11-05 10:32:41 -0500290 go = True
Scott Bakeradc73172014-09-04 10:36:51 -0700291 continue
292
Sapan Bhatia47944b32015-03-04 10:21:06 -0500293 cond = step_conditions[d]
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400294 cond.acquire()
Sapan Bhatia47944b32015-03-04 10:21:06 -0500295 if (step_status[d] is STEP_STATUS_WORKING):
Scott Bakeradc73172014-09-04 10:36:51 -0700296 logger.info(" step %s wait on dep %s" % (step.__name__, d))
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400297 cond.wait()
Sapan Bhatia47944b32015-03-04 10:21:06 -0500298 elif step_status[d] == STEP_STATUS_OK:
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000299 go = True
300 else:
301 go = False
302 failed_dep = d
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400303 cond.release()
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000304 if (not go):
305 break
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400306 else:
307 go = True
308
309 if (not go):
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000310 print bcolors.FAIL + "Step %r skipped on %r" % (step,failed_dep) + bcolors.ENDC
Scott Bakeradc73172014-09-04 10:36:51 -0700311 # SMBAKER: sync_step was not defined here, so I changed
312 # this from 'sync_step' to 'step'. Verify.
313 self.failed_steps.append(step)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400314 my_status = STEP_STATUS_KO
315 else:
316 sync_step = step(driver=self.driver,error_map=self.error_mapper)
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000317 sync_step. __name__= step.__name__
Sapan Bhatia51f48932014-08-25 04:17:12 -0400318 sync_step.dependencies = []
319 try:
320 mlist = sync_step.provides
Scott Bakeradc73172014-09-04 10:36:51 -0700321
Sapan Bhatia51f48932014-08-25 04:17:12 -0400322 for m in mlist:
Sapan Bhatia709bebd2015-02-08 06:35:36 +0000323 lst = self.model_dependency_graph[m.__name__]
324 nlst = map(lambda(a,b):b,lst)
325 sync_step.dependencies.extend(nlst)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400326 except KeyError:
327 pass
328 sync_step.debug_mode = debug_mode
329
330 should_run = False
331 try:
332 # Various checks that decide whether
333 # this step runs or not
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400334 self.check_class_dependency(sync_step, self.failed_steps) # dont run Slices if Sites failed
Sapan Bhatia51f48932014-08-25 04:17:12 -0400335 self.check_schedule(sync_step, deletion) # dont run sync_network_routes if time since last run < 1 hour
336 should_run = True
337 except StepNotReady:
Scott Bakeradc73172014-09-04 10:36:51 -0700338 logger.info('Step not ready: %s'%sync_step.__name__)
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 except Exception,e:
Scott Bakeradc73172014-09-04 10:36:51 -0700342 logger.error('%r' % e)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400343 logger.log_exc("sync step failed: %r. Deletion: %r"%(sync_step,deletion))
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400344 self.failed_steps.append(sync_step)
345 my_status = STEP_STATUS_KO
Sapan Bhatia51f48932014-08-25 04:17:12 -0400346
347 if (should_run):
348 try:
349 duration=time.time() - start_time
350
351 logger.info('Executing step %s' % sync_step.__name__)
352
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000353 print bcolors.OKBLUE + "Executing step %s" % sync_step.__name__ + bcolors.ENDC
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400354 failed_objects = sync_step(failed=list(self.failed_step_objects), deletion=deletion)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400355
356 self.check_duration(sync_step, duration)
Sapan Bhatia51f48932014-08-25 04:17:12 -0400357
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400358 if failed_objects:
359 self.failed_step_objects.update(failed_objects)
360
Scott Bakeradc73172014-09-04 10:36:51 -0700361 logger.info("Step %r succeeded" % step)
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000362 print bcolors.OKGREEN + "Step %r succeeded" % step + bcolors.ENDC
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400363 my_status = STEP_STATUS_OK
Sapan Bhatia51f48932014-08-25 04:17:12 -0400364 self.update_run_time(sync_step,deletion)
365 except Exception,e:
Sapan Bhatia6b6c2182015-01-27 03:58:11 +0000366 print bcolors.FAIL + "Model step %r failed" % (step) + bcolors.ENDC
Scott Bakeradc73172014-09-04 10:36:51 -0700367 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 -0400368 logger.log_exc(e)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400369 self.failed_steps.append(S)
370 my_status = STEP_STATUS_KO
371 else:
Scott Bakeradc73172014-09-04 10:36:51 -0700372 logger.info("Step %r succeeded due to non-run" % step)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400373 my_status = STEP_STATUS_OK
Scott Bakeradc73172014-09-04 10:36:51 -0700374
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400375 try:
Sapan Bhatia47944b32015-03-04 10:21:06 -0500376 my_cond = step_conditions[S]
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400377 my_cond.acquire()
Sapan Bhatia47944b32015-03-04 10:21:06 -0500378 step_status[S]=my_status
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400379 my_cond.notify_all()
380 my_cond.release()
381 except KeyError,e:
Scott Bakeradc73172014-09-04 10:36:51 -0700382 logger.info('Step %r is a leaf' % step)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400383 pass
Scott Bakerc7ca6552014-09-05 14:48:38 -0700384 finally:
385 connection.close()
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400386
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400387 def run(self):
388 if not self.driver.enabled:
389 return
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400390
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400391 if (self.driver_kind=="openstack") and (not self.driver.has_openstack):
392 return
393
394 while True:
395 try:
Sapan Bhatiae122dcf2015-01-29 20:54:17 +0000396 loop_start = time.time()
Scott Bakerd9e01232015-02-04 16:59:45 -0800397 error_map_file = getattr(Config(), "error_map_path", XOS_DIR + "/error_map.txt")
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400398 self.error_mapper = ErrorMapper(error_map_file)
399
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400400 logger.info('Waiting for event')
401 tBeforeWait = time.time()
Sapan Bhatia47944b32015-03-04 10:21:06 -0500402 self.wait_for_event(timeout=5)
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400403 logger.info('Observer woke up')
404
Sapan Bhatia285decb2014-04-30 00:31:44 -0400405 # Two passes. One for sync, the other for deletion.
Scott Bakerf0db81c2015-03-31 15:06:55 -0700406 for deletion in [False,True]:
Sapan Bhatia47944b32015-03-04 10:21:06 -0500407 # Set of individual objects within steps that failed
408 self.failed_step_objects = set()
409
410 # Set up conditions and step status
411 # This is needed for steps to run in parallel
412 # while obeying dependencies.
413
414 providers = set()
415 dependency_graph = self.dependency_graph if not deletion else self.deletion_dependency_graph
416
417 for v in dependency_graph.values():
418 if (v):
419 providers.update(v)
420
421 self.step_conditions = {}
422 self.step_status = {}
423
424 for p in list(providers):
425 self.step_conditions[p] = threading.Condition()
426
427 self.step_status[p] = STEP_STATUS_WORKING
428
429 self.failed_steps = []
430
Sapan Bhatia51f48932014-08-25 04:17:12 -0400431 threads = []
Sapan Bhatiae82f5e52014-07-23 10:02:45 -0400432 logger.info('Deletion=%r...'%deletion)
Sapan Bhatiaab202a62014-09-03 11:30:21 -0400433 schedule = self.ordered_steps if not deletion else reversed(self.ordered_steps)
434
435 for S in schedule:
436 thread = threading.Thread(target=self.sync, args=(S, deletion))
437
438 logger.info('Deletion=%r...'%deletion)
439 threads.append(thread)
Sapan Bhatia285decb2014-04-30 00:31:44 -0400440
Sapan Bhatia51f48932014-08-25 04:17:12 -0400441 # Start threads
442 for t in threads:
443 t.start()
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400444
Sapan Bhatia51f48932014-08-25 04:17:12 -0400445 # Wait for all threads to finish before continuing with the run loop
446 for t in threads:
447 t.join()
Sapan Bhatia285decb2014-04-30 00:31:44 -0400448
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400449 self.save_run_times()
Sapan Bhatiae122dcf2015-01-29 20:54:17 +0000450 loop_end = time.time()
Scott Baker21430542015-03-31 15:18:50 -0700451 open('/tmp/%sobserver_last_run'%self.observer_name,'w').write(json.dumps({'last_run': loop_end, 'last_duration':loop_end - loop_start}))
Sapan Bhatiaf73664b2014-04-28 13:07:18 -0400452 except Exception, e:
Scott Bakeradc73172014-09-04 10:36:51 -0700453 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 -0400454 logger.log_exc("Exception in observer run loop")
455 traceback.print_exc()