blob: bdf5ce395f6f69dabb3c2a6141477d0076287560 [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
5import traceback
6import commands
7import threading
8import json
9
10from datetime import datetime
11from collections import defaultdict
12from core.models import *
13from django.db.models import F, Q
Tony Mack387a73f2013-09-18 07:59:14 -040014#from openstack.manager import OpenStackManager
15from openstack.driver import OpenStackDriver
Sapan Bhatia24836f12013-08-27 10:16:05 -040016from util.logger import Logger, logging, logger
17#from timeout import timeout
Sapan Bhatia757e0b62013-09-02 16:55:00 -040018from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040019from observer.steps import *
Scott Baker45fb7a12013-12-31 00:56:19 -080020from syncstep import SyncStep
Sapan Bhatia24836f12013-08-27 10:16:05 -040021
Sapan Bhatia13c7f112013-09-02 14:19:35 -040022debug_mode = False
Sapan Bhatia24836f12013-08-27 10:16:05 -040023
Andy Bavier04111b72013-10-22 16:47:10 -040024logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040025
Sapan Bhatia13c7f112013-09-02 14:19:35 -040026class StepNotReady(Exception):
Tony Macke1509e82013-10-09 12:38:04 -040027 pass
Sapan Bhatia13c7f112013-09-02 14:19:35 -040028
Sapan Bhatia467b7ce2013-10-02 09:25:46 -040029def toposort(g, steps=None):
Tony Macke1509e82013-10-09 12:38:04 -040030 if (not steps):
31 keys = set(g.keys())
32 values = set({})
33 for v in g.values():
34 values=values | set(v)
35
36 steps=list(keys|values)
37 reverse = {}
Sapan Bhatia24836f12013-08-27 10:16:05 -040038
Tony Macke1509e82013-10-09 12:38:04 -040039 for k,v in g.items():
40 for rk in v:
41 try:
42 reverse[rk].append(k)
43 except:
44 reverse[rk]=k
Sapan Bhatia24836f12013-08-27 10:16:05 -040045
Tony Macke1509e82013-10-09 12:38:04 -040046 sources = []
47 for k,v in g.items():
48 if not reverse.has_key(k):
49 sources.append(k)
Sapan Bhatia24836f12013-08-27 10:16:05 -040050
51
Tony Macke1509e82013-10-09 12:38:04 -040052 for k,v in reverse.iteritems():
53 if (not v):
54 sources.append(k)
Sapan Bhatia24836f12013-08-27 10:16:05 -040055
Tony Macke1509e82013-10-09 12:38:04 -040056 order = []
57 marked = []
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040058
Tony Macke1509e82013-10-09 12:38:04 -040059 while sources:
60 n = sources.pop()
61 try:
62 for m in g[n]:
63 if m not in marked:
64 sources.append(m)
65 marked.append(m)
66 except KeyError:
67 pass
68 if (n in steps):
69 order.append(n)
Sapan Bhatia972a2e82013-10-02 00:03:02 -040070
Tony Macke1509e82013-10-09 12:38:04 -040071 order.reverse()
72 order.extend(set(steps)-set(order))
73 return order
Sapan Bhatia24836f12013-08-27 10:16:05 -040074
Scott Baker7771f412014-01-02 16:36:41 -080075class NoOpDriver:
76 def __init__(self):
77 self.enabled = True
78
Sapan Bhatia24836f12013-08-27 10:16:05 -040079class PlanetStackObserver:
Scott Baker45fb7a12013-12-31 00:56:19 -080080 #sync_steps = [SyncNetworks,SyncNetworkSlivers,SyncSites,SyncSitePrivileges,SyncSlices,SyncSliceMemberships,SyncSlivers,SyncSliverIps,SyncExternalRoutes,SyncUsers,SyncRoles,SyncNodes,SyncImages,GarbageCollector]
81 sync_steps = []
Sapan Bhatia24836f12013-08-27 10:16:05 -040082
Tony Macke1509e82013-10-09 12:38:04 -040083 def __init__(self):
84 # The Condition object that gets signalled by Feefie events
85 self.step_lookup = {}
Scott Baker45fb7a12013-12-31 00:56:19 -080086 self.load_sync_step_modules()
Tony Macke1509e82013-10-09 12:38:04 -040087 self.load_sync_steps()
88 self.event_cond = threading.Condition()
Scott Baker7771f412014-01-02 16:36:41 -080089
90
91 self.driver_kind = getattr(Config(), "observer_driver", "openstack")
92 if self.driver_kind=="openstack":
93 self.driver = OpenStackDriver()
94 else:
95 self.driver = NoOpDriver()
Sapan Bhatia24836f12013-08-27 10:16:05 -040096
Tony Macke1509e82013-10-09 12:38:04 -040097 def wait_for_event(self, timeout):
98 self.event_cond.acquire()
99 self.event_cond.wait(timeout)
100 self.event_cond.release()
Scott Baker45fb7a12013-12-31 00:56:19 -0800101
Tony Macke1509e82013-10-09 12:38:04 -0400102 def wake_up(self):
103 logger.info('Wake up routine called. Event cond %r'%self.event_cond)
104 self.event_cond.acquire()
105 self.event_cond.notify()
106 self.event_cond.release()
Sapan Bhatia24836f12013-08-27 10:16:05 -0400107
Scott Baker45fb7a12013-12-31 00:56:19 -0800108 def load_sync_step_modules(self, step_dir=None):
109 if step_dir is None:
Scott Baker7771f412014-01-02 16:36:41 -0800110 if hasattr(Config(), "observer_steps_dir"):
111 step_dir = Config().observer_steps_dir
Scott Baker45fb7a12013-12-31 00:56:19 -0800112 else:
113 step_dir = "/opt/planetstack/observer/steps"
114
115 for fn in os.listdir(step_dir):
116 pathname = os.path.join(step_dir,fn)
117 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
118 module = imp.load_source(fn[:-3],pathname)
119 for classname in dir(module):
120 c = getattr(module, classname, None)
121
122 # make sure 'c' is a descendent of SyncStep and has a
123 # provides field (this eliminates the abstract base classes
124 # since they don't have a provides)
125
126 if inspect.isclass(c) and issubclass(c, SyncStep) and hasattr(c,"provides") and (c not in self.sync_steps):
127 self.sync_steps.append(c)
128 logger.info('loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps]))
129 # print 'loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps])
130
Tony Macke1509e82013-10-09 12:38:04 -0400131 def load_sync_steps(self):
Tony Mack3587dc72013-10-14 22:01:43 -0400132 dep_path = Config().observer_dependency_graph
Scott Baker45fb7a12013-12-31 00:56:19 -0800133 logger.info('Loading model dependency graph from %s' % dep_path)
Tony Macke1509e82013-10-09 12:38:04 -0400134 try:
135 # This contains dependencies between records, not sync steps
136 self.model_dependency_graph = json.loads(open(dep_path).read())
137 except Exception,e:
138 raise e
Sapan Bhatia24836f12013-08-27 10:16:05 -0400139
Tony Macke1509e82013-10-09 12:38:04 -0400140 try:
141 backend_path = Config().observer_pl_dependency_graph
Scott Baker45fb7a12013-12-31 00:56:19 -0800142 logger.info('Loading backend dependency graph from %s' % backend_path)
Tony Macke1509e82013-10-09 12:38:04 -0400143 # This contains dependencies between backend records
144 self.backend_dependency_graph = json.loads(open(backend_path).read())
145 except Exception,e:
Scott Baker45fb7a12013-12-31 00:56:19 -0800146 logger.info('Backend dependency graph not loaded')
Tony Macke1509e82013-10-09 12:38:04 -0400147 # We can work without a backend graph
148 self.backend_dependency_graph = {}
Sapan Bhatia24836f12013-08-27 10:16:05 -0400149
Tony Macke1509e82013-10-09 12:38:04 -0400150 provides_dict = {}
151 for s in self.sync_steps:
152 self.step_lookup[s.__name__] = s
153 for m in s.provides:
154 try:
155 provides_dict[m.__name__].append(s.__name__)
156 except KeyError:
157 provides_dict[m.__name__]=[s.__name__]
Sapan Bhatia04c94ad2013-09-02 18:00:28 -0400158
Tony Macke1509e82013-10-09 12:38:04 -0400159
160 step_graph = {}
161 for k,v in self.model_dependency_graph.iteritems():
162 try:
163 for source in provides_dict[k]:
164 for m in v:
165 try:
166 for dest in provides_dict[m]:
167 # no deps, pass
168 try:
169 step_graph[source].append(dest)
170 except:
171 step_graph[source]=[dest]
172 except KeyError:
173 pass
174
175 except KeyError:
176 pass
177 # no dependencies, pass
178
179 #import pdb
180 #pdb.set_trace()
181 if (self.backend_dependency_graph):
182 backend_dict = {}
183 for s in self.sync_steps:
184 for m in s.serves:
185 backend_dict[m]=s.__name__
186
187 for k,v in backend_dependency_graph.iteritems():
188 try:
189 source = backend_dict[k]
190 for m in v:
191 try:
192 dest = backend_dict[m]
193 except KeyError:
194 # no deps, pass
195 pass
196 step_graph[source]=dest
197
198 except KeyError:
199 pass
200 # no dependencies, pass
Sapan Bhatia24836f12013-08-27 10:16:05 -0400201
Tony Macke1509e82013-10-09 12:38:04 -0400202 dependency_graph = step_graph
Sapan Bhatia24836f12013-08-27 10:16:05 -0400203
Tony Macke1509e82013-10-09 12:38:04 -0400204 self.ordered_steps = toposort(dependency_graph, map(lambda s:s.__name__,self.sync_steps))
205 print "Order of steps=",self.ordered_steps
206 self.load_run_times()
207
Sapan Bhatia24836f12013-08-27 10:16:05 -0400208
Tony Macke1509e82013-10-09 12:38:04 -0400209 def check_duration(self, step, duration):
210 try:
211 if (duration > step.deadline):
212 logger.info('Sync step %s missed deadline, took %.2f seconds'%(step.name,duration))
213 except AttributeError:
214 # S doesn't have a deadline
215 pass
Sapan Bhatia24836f12013-08-27 10:16:05 -0400216
Tony Macke1509e82013-10-09 12:38:04 -0400217 def update_run_time(self, step):
218 self.last_run_times[step.__name__]=time.time()
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400219
Tony Macke1509e82013-10-09 12:38:04 -0400220 def check_schedule(self, step):
221 time_since_last_run = time.time() - self.last_run_times.get(step.__name__, 0)
222 try:
223 if (time_since_last_run < step.requested_interval):
224 raise StepNotReady
225 except AttributeError:
226 logger.info('Step %s does not have requested_interval set'%step.__name__)
227 raise StepNotReady
228
229 def load_run_times(self):
230 try:
231 jrun_times = open('/tmp/observer_run_times').read()
232 self.last_run_times = json.loads(jrun_times)
233 except:
234 self.last_run_times={}
235 for e in self.ordered_steps:
236 self.last_run_times[e]=0
Sapan Bhatia36938ca2013-09-02 14:35:24 -0400237
238
Tony Macke1509e82013-10-09 12:38:04 -0400239 def save_run_times(self):
240 run_times = json.dumps(self.last_run_times)
241 open('/tmp/observer_run_times','w').write(run_times)
Sapan Bhatia36938ca2013-09-02 14:35:24 -0400242
Tony Macke1509e82013-10-09 12:38:04 -0400243 def check_class_dependency(self, step, failed_steps):
Tony Mack2fd8d302013-10-05 09:59:38 -0400244 step.dependenices = []
245 for obj in step.provides:
246 step.dependenices.extend(self.model_dependency_graph.get(obj.__name__, []))
247 for failed_step in failed_steps:
248 if (failed_step in step.dependencies):
249 raise StepNotReady
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400250
Tony Macke1509e82013-10-09 12:38:04 -0400251 def run(self):
Scott Baker7771f412014-01-02 16:36:41 -0800252 if not self.driver.enabled:
Tony Macke1509e82013-10-09 12:38:04 -0400253 return
Scott Baker7771f412014-01-02 16:36:41 -0800254 if (self.driver_kind=="openstack") and (not self.driver.has_openstack):
255 return
256
Tony Macke1509e82013-10-09 12:38:04 -0400257 while True:
258 try:
259 logger.info('Waiting for event')
260 tBeforeWait = time.time()
261 self.wait_for_event(timeout=30)
262 logger.info('Observer woke up')
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400263
Tony Macke1509e82013-10-09 12:38:04 -0400264 # Set of whole steps that failed
265 failed_steps = []
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400266
Tony Macke1509e82013-10-09 12:38:04 -0400267 # Set of individual objects within steps that failed
Tony Mack808b2392013-10-16 11:34:32 -0400268 failed_step_objects = set()
Sapan Bhatia24836f12013-08-27 10:16:05 -0400269
Tony Macke1509e82013-10-09 12:38:04 -0400270 for S in self.ordered_steps:
271 step = self.step_lookup[S]
272 start_time=time.time()
273
274 sync_step = step(driver=self.driver)
275 sync_step.__name__ = step.__name__
276 sync_step.dependencies = []
277 try:
278 mlist = sync_step.provides
279
280 for m in mlist:
281 sync_step.dependencies.extend(self.model_dependency_graph[m.__name__])
282 except KeyError:
283 pass
284 sync_step.debug_mode = debug_mode
Sapan Bhatia24836f12013-08-27 10:16:05 -0400285
Tony Macke1509e82013-10-09 12:38:04 -0400286 should_run = False
287 try:
288 # Various checks that decide whether
289 # this step runs or not
290 self.check_class_dependency(sync_step, failed_steps) # dont run Slices if Sites failed
291 self.check_schedule(sync_step) # dont run sync_network_routes if time since last run < 1 hour
292 should_run = True
293 except StepNotReady:
294 logging.info('Step not ready: %s'%sync_step.__name__)
295 failed_steps.append(sync_step)
296 except:
297 failed_steps.append(sync_step)
Sapan Bhatia24836f12013-08-27 10:16:05 -0400298
Tony Macke1509e82013-10-09 12:38:04 -0400299 if (should_run):
300 try:
301 duration=time.time() - start_time
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400302
Scott Baker45fb7a12013-12-31 00:56:19 -0800303 logger.info('Executing step %s' % sync_step.__name__)
304
Tony Macke1509e82013-10-09 12:38:04 -0400305 # ********* This is the actual sync step
306 #import pdb
307 #pdb.set_trace()
Tony Mack808b2392013-10-16 11:34:32 -0400308 failed_objects = sync_step(failed=list(failed_step_objects))
Sapan Bhatia13c7f112013-09-02 14:19:35 -0400309
310
Tony Macke1509e82013-10-09 12:38:04 -0400311 self.check_duration(sync_step, duration)
312 if failed_objects:
Tony Mack808b2392013-10-16 11:34:32 -0400313 failed_step_objects.update(failed_objects)
Tony Macke1509e82013-10-09 12:38:04 -0400314 self.update_run_time(sync_step)
315 except:
Tony Macke1509e82013-10-09 12:38:04 -0400316 failed_steps.append(S)
317 self.save_run_times()
318 except:
319 logger.log_exc("Exception in observer run loop")
320 traceback.print_exc()