Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 1 | import os |
| 2 | import imp |
| 3 | import inspect |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | import time |
| 5 | import traceback |
| 6 | import commands |
| 7 | import threading |
| 8 | import json |
| 9 | |
| 10 | from datetime import datetime |
| 11 | from collections import defaultdict |
| 12 | from core.models import * |
| 13 | from django.db.models import F, Q |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 14 | #from openstack.manager import OpenStackManager |
| 15 | from openstack.driver import OpenStackDriver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 16 | from util.logger import Logger, logging, logger |
| 17 | #from timeout import timeout |
Sapan Bhatia | 757e0b6 | 2013-09-02 16:55:00 -0400 | [diff] [blame] | 18 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 19 | from observer.steps import * |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 20 | from syncstep import SyncStep |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 21 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 22 | debug_mode = False |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 23 | |
Andy Bavier | 04111b7 | 2013-10-22 16:47:10 -0400 | [diff] [blame] | 24 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 25 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 26 | class StepNotReady(Exception): |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 27 | pass |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 28 | |
Sapan Bhatia | 467b7ce | 2013-10-02 09:25:46 -0400 | [diff] [blame] | 29 | def toposort(g, steps=None): |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 30 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 38 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 39 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 45 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 46 | sources = [] |
| 47 | for k,v in g.items(): |
| 48 | if not reverse.has_key(k): |
| 49 | sources.append(k) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 50 | |
| 51 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 52 | for k,v in reverse.iteritems(): |
| 53 | if (not v): |
| 54 | sources.append(k) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 55 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 56 | order = [] |
| 57 | marked = [] |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 58 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 59 | 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 Bhatia | 972a2e8 | 2013-10-02 00:03:02 -0400 | [diff] [blame] | 70 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 71 | order.reverse() |
| 72 | order.extend(set(steps)-set(order)) |
| 73 | return order |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 74 | |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 75 | class NoOpDriver: |
| 76 | def __init__(self): |
| 77 | self.enabled = True |
| 78 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 79 | class PlanetStackObserver: |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 80 | #sync_steps = [SyncNetworks,SyncNetworkSlivers,SyncSites,SyncSitePrivileges,SyncSlices,SyncSliceMemberships,SyncSlivers,SyncSliverIps,SyncExternalRoutes,SyncUsers,SyncRoles,SyncNodes,SyncImages,GarbageCollector] |
| 81 | sync_steps = [] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 82 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 83 | def __init__(self): |
| 84 | # The Condition object that gets signalled by Feefie events |
| 85 | self.step_lookup = {} |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 86 | self.load_sync_step_modules() |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 87 | self.load_sync_steps() |
| 88 | self.event_cond = threading.Condition() |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 89 | |
| 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 96 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 97 | def wait_for_event(self, timeout): |
| 98 | self.event_cond.acquire() |
| 99 | self.event_cond.wait(timeout) |
| 100 | self.event_cond.release() |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 101 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 102 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 107 | |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 108 | def load_sync_step_modules(self, step_dir=None): |
| 109 | if step_dir is None: |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 110 | if hasattr(Config(), "observer_steps_dir"): |
| 111 | step_dir = Config().observer_steps_dir |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 112 | 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 Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 131 | def load_sync_steps(self): |
Tony Mack | 3587dc7 | 2013-10-14 22:01:43 -0400 | [diff] [blame] | 132 | dep_path = Config().observer_dependency_graph |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 133 | logger.info('Loading model dependency graph from %s' % dep_path) |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 134 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 139 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 140 | try: |
| 141 | backend_path = Config().observer_pl_dependency_graph |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 142 | logger.info('Loading backend dependency graph from %s' % backend_path) |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 143 | # This contains dependencies between backend records |
| 144 | self.backend_dependency_graph = json.loads(open(backend_path).read()) |
| 145 | except Exception,e: |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 146 | logger.info('Backend dependency graph not loaded') |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 147 | # We can work without a backend graph |
| 148 | self.backend_dependency_graph = {} |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 149 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 150 | 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 Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 158 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 159 | |
| 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 201 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 202 | dependency_graph = step_graph |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 203 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 204 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 208 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 209 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 216 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 217 | def update_run_time(self, step): |
| 218 | self.last_run_times[step.__name__]=time.time() |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 219 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 220 | 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 Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 237 | |
| 238 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 239 | 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 Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 242 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 243 | def check_class_dependency(self, step, failed_steps): |
Tony Mack | 2fd8d30 | 2013-10-05 09:59:38 -0400 | [diff] [blame] | 244 | 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 Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 250 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 251 | def run(self): |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 252 | if not self.driver.enabled: |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 253 | return |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 254 | if (self.driver_kind=="openstack") and (not self.driver.has_openstack): |
| 255 | return |
| 256 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 257 | 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 Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 263 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 264 | # Set of whole steps that failed |
| 265 | failed_steps = [] |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 266 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 267 | # Set of individual objects within steps that failed |
Tony Mack | 808b239 | 2013-10-16 11:34:32 -0400 | [diff] [blame] | 268 | failed_step_objects = set() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 269 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 270 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 285 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 286 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 298 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 299 | if (should_run): |
| 300 | try: |
| 301 | duration=time.time() - start_time |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 302 | |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 303 | logger.info('Executing step %s' % sync_step.__name__) |
| 304 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 305 | # ********* This is the actual sync step |
| 306 | #import pdb |
| 307 | #pdb.set_trace() |
Tony Mack | 808b239 | 2013-10-16 11:34:32 -0400 | [diff] [blame] | 308 | failed_objects = sync_step(failed=list(failed_step_objects)) |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 309 | |
| 310 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 311 | self.check_duration(sync_step, duration) |
| 312 | if failed_objects: |
Tony Mack | 808b239 | 2013-10-16 11:34:32 -0400 | [diff] [blame] | 313 | failed_step_objects.update(failed_objects) |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 314 | self.update_run_time(sync_step) |
| 315 | except: |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 316 | failed_steps.append(S) |
| 317 | self.save_run_times() |
| 318 | except: |
| 319 | logger.log_exc("Exception in observer run loop") |
| 320 | traceback.print_exc() |