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 |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 9 | import pdb |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 10 | |
| 11 | from datetime import datetime |
| 12 | from collections import defaultdict |
| 13 | from core.models import * |
| 14 | from django.db.models import F, Q |
Scott Baker | 2e6c9a4 | 2014-09-05 14:48:38 -0700 | [diff] [blame^] | 15 | from django.db import connection |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 16 | #from openstack.manager import OpenStackManager |
| 17 | from openstack.driver import OpenStackDriver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 18 | from util.logger import Logger, logging, logger |
| 19 | #from timeout import timeout |
Sapan Bhatia | 757e0b6 | 2013-09-02 16:55:00 -0400 | [diff] [blame] | 20 | from planetstack.config import Config |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 21 | from observer.steps import * |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 22 | from syncstep import SyncStep |
Sapan Bhatia | 45cbbc3 | 2014-03-11 17:48:30 -0400 | [diff] [blame] | 23 | from toposort import toposort |
Sapan Bhatia | 13d8915 | 2014-07-23 10:35:33 -0400 | [diff] [blame] | 24 | from observer.error_mapper import * |
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 | debug_mode = False |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 27 | |
Andy Bavier | 04111b7 | 2013-10-22 16:47:10 -0400 | [diff] [blame] | 28 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 29 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 30 | class StepNotReady(Exception): |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 31 | pass |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 32 | |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 33 | class NoOpDriver: |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 34 | def __init__(self): |
| 35 | self.enabled = True |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 36 | self.dependency_graph = None |
| 37 | |
| 38 | STEP_STATUS_WORKING=1 |
| 39 | STEP_STATUS_OK=2 |
| 40 | STEP_STATUS_KO=3 |
| 41 | |
| 42 | def invert_graph(g): |
| 43 | ig = {} |
| 44 | for k,v in g.items(): |
| 45 | for v0 in v: |
| 46 | try: |
| 47 | ig[v0].append(k) |
| 48 | except: |
| 49 | ig=[k] |
| 50 | return ig |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 51 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 52 | class PlanetStackObserver: |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 53 | #sync_steps = [SyncNetworks,SyncNetworkSlivers,SyncSites,SyncSitePrivileges,SyncSlices,SyncSliceMemberships,SyncSlivers,SyncSliverIps,SyncExternalRoutes,SyncUsers,SyncRoles,SyncNodes,SyncImages,GarbageCollector] |
| 54 | sync_steps = [] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 55 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 56 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 57 | def __init__(self): |
| 58 | # The Condition object that gets signalled by Feefie events |
| 59 | self.step_lookup = {} |
| 60 | self.load_sync_step_modules() |
| 61 | self.load_sync_steps() |
| 62 | self.event_cond = threading.Condition() |
Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 63 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 64 | self.driver_kind = getattr(Config(), "observer_driver", "openstack") |
| 65 | if self.driver_kind=="openstack": |
| 66 | self.driver = OpenStackDriver() |
| 67 | else: |
| 68 | self.driver = NoOpDriver() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 69 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 70 | def wait_for_event(self, timeout): |
| 71 | self.event_cond.acquire() |
| 72 | self.event_cond.wait(timeout) |
| 73 | self.event_cond.release() |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 74 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 75 | def wake_up(self): |
| 76 | logger.info('Wake up routine called. Event cond %r'%self.event_cond) |
| 77 | self.event_cond.acquire() |
| 78 | self.event_cond.notify() |
| 79 | self.event_cond.release() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 80 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 81 | def load_sync_step_modules(self, step_dir=None): |
| 82 | if step_dir is None: |
| 83 | if hasattr(Config(), "observer_steps_dir"): |
| 84 | step_dir = Config().observer_steps_dir |
| 85 | else: |
| 86 | step_dir = "/opt/planetstack/observer/steps" |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 87 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 88 | for fn in os.listdir(step_dir): |
| 89 | pathname = os.path.join(step_dir,fn) |
| 90 | if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"): |
| 91 | module = imp.load_source(fn[:-3],pathname) |
| 92 | for classname in dir(module): |
| 93 | c = getattr(module, classname, None) |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 94 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 95 | # make sure 'c' is a descendent of SyncStep and has a |
| 96 | # provides field (this eliminates the abstract base classes |
| 97 | # since they don't have a provides) |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 98 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 99 | if inspect.isclass(c) and issubclass(c, SyncStep) and hasattr(c,"provides") and (c not in self.sync_steps): |
| 100 | self.sync_steps.append(c) |
| 101 | logger.info('loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps])) |
| 102 | # print 'loaded sync steps: %s' % ",".join([x.__name__ for x in self.sync_steps]) |
Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 103 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 104 | def load_sync_steps(self): |
| 105 | dep_path = Config().observer_dependency_graph |
| 106 | logger.info('Loading model dependency graph from %s' % dep_path) |
| 107 | try: |
| 108 | # This contains dependencies between records, not sync steps |
| 109 | self.model_dependency_graph = json.loads(open(dep_path).read()) |
| 110 | except Exception,e: |
| 111 | raise e |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 112 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 113 | try: |
| 114 | backend_path = Config().observer_pl_dependency_graph |
| 115 | logger.info('Loading backend dependency graph from %s' % backend_path) |
| 116 | # This contains dependencies between backend records |
| 117 | self.backend_dependency_graph = json.loads(open(backend_path).read()) |
| 118 | except Exception,e: |
| 119 | logger.info('Backend dependency graph not loaded') |
| 120 | # We can work without a backend graph |
| 121 | self.backend_dependency_graph = {} |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 122 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 123 | provides_dict = {} |
| 124 | for s in self.sync_steps: |
| 125 | self.step_lookup[s.__name__] = s |
| 126 | for m in s.provides: |
| 127 | try: |
| 128 | provides_dict[m.__name__].append(s.__name__) |
| 129 | except KeyError: |
| 130 | provides_dict[m.__name__]=[s.__name__] |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 131 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 132 | step_graph = {} |
| 133 | for k,v in self.model_dependency_graph.iteritems(): |
| 134 | try: |
| 135 | for source in provides_dict[k]: |
| 136 | for m in v: |
| 137 | try: |
| 138 | for dest in provides_dict[m]: |
| 139 | # no deps, pass |
| 140 | try: |
| 141 | if (dest not in step_graph[source]): |
| 142 | step_graph[source].append(dest) |
| 143 | except: |
| 144 | step_graph[source]=[dest] |
| 145 | except KeyError: |
| 146 | pass |
| 147 | |
| 148 | except KeyError: |
| 149 | pass |
| 150 | # no dependencies, pass |
| 151 | |
| 152 | #import pdb |
| 153 | #pdb.set_trace() |
| 154 | if (self.backend_dependency_graph): |
| 155 | backend_dict = {} |
| 156 | for s in self.sync_steps: |
| 157 | for m in s.serves: |
| 158 | backend_dict[m]=s.__name__ |
| 159 | |
| 160 | for k,v in backend_dependency_graph.iteritems(): |
| 161 | try: |
| 162 | source = backend_dict[k] |
| 163 | for m in v: |
| 164 | try: |
| 165 | dest = backend_dict[m] |
| 166 | except KeyError: |
| 167 | # no deps, pass |
| 168 | pass |
| 169 | step_graph[source]=dest |
| 170 | |
| 171 | except KeyError: |
| 172 | pass |
| 173 | # no dependencies, pass |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 174 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 175 | self.dependency_graph = step_graph |
| 176 | self.deletion_dependency_graph = invert_graph(step_graph) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 177 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 178 | self.ordered_steps = toposort(self.dependency_graph, map(lambda s:s.__name__,self.sync_steps)) |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 179 | print "Order of steps=",self.ordered_steps |
| 180 | self.load_run_times() |
| 181 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 182 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 183 | def check_duration(self, step, duration): |
| 184 | try: |
| 185 | if (duration > step.deadline): |
| 186 | logger.info('Sync step %s missed deadline, took %.2f seconds'%(step.name,duration)) |
| 187 | except AttributeError: |
| 188 | # S doesn't have a deadline |
| 189 | pass |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 190 | |
Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 191 | def update_run_time(self, step, deletion): |
| 192 | if (not deletion): |
| 193 | self.last_run_times[step.__name__]=time.time() |
| 194 | else: |
| 195 | self.last_deletion_run_times[step.__name__]=time.time() |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 196 | |
Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 197 | |
| 198 | def check_schedule(self, step, deletion): |
| 199 | last_run_times = self.last_run_times if not deletion else self.last_deletion_run_times |
| 200 | |
| 201 | time_since_last_run = time.time() - last_run_times.get(step.__name__, 0) |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 202 | try: |
| 203 | if (time_since_last_run < step.requested_interval): |
| 204 | raise StepNotReady |
| 205 | except AttributeError: |
| 206 | logger.info('Step %s does not have requested_interval set'%step.__name__) |
| 207 | raise StepNotReady |
| 208 | |
| 209 | def load_run_times(self): |
| 210 | try: |
| 211 | jrun_times = open('/tmp/observer_run_times').read() |
| 212 | self.last_run_times = json.loads(jrun_times) |
| 213 | except: |
| 214 | self.last_run_times={} |
| 215 | for e in self.ordered_steps: |
| 216 | self.last_run_times[e]=0 |
Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 217 | try: |
| 218 | jrun_times = open('/tmp/observer_deletion_run_times').read() |
| 219 | self.last_deletion_run_times = json.loads(jrun_times) |
| 220 | except: |
| 221 | self.last_deletion_run_times={} |
| 222 | for e in self.ordered_steps: |
| 223 | self.last_deletion_run_times[e]=0 |
| 224 | |
Sapan Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 225 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 226 | def save_run_times(self): |
| 227 | run_times = json.dumps(self.last_run_times) |
| 228 | open('/tmp/observer_run_times','w').write(run_times) |
Sapan Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 229 | |
Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 230 | deletion_run_times = json.dumps(self.last_deletion_run_times) |
| 231 | open('/tmp/observer_deletion_run_times','w').write(deletion_run_times) |
| 232 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 233 | def check_class_dependency(self, step, failed_steps): |
| 234 | step.dependenices = [] |
| 235 | for obj in step.provides: |
| 236 | step.dependenices.extend(self.model_dependency_graph.get(obj.__name__, [])) |
| 237 | for failed_step in failed_steps: |
| 238 | if (failed_step in step.dependencies): |
| 239 | raise StepNotReady |
| 240 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 241 | def sync(self, S, deletion): |
Scott Baker | 2e6c9a4 | 2014-09-05 14:48:38 -0700 | [diff] [blame^] | 242 | try: |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 243 | step = self.step_lookup[S] |
| 244 | start_time=time.time() |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 245 | |
| 246 | logger.info("Starting to work on step %s" % step.__name__) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 247 | |
| 248 | dependency_graph = self.dependency_graph if not deletion else self.deletion_dependency_graph |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 249 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 250 | # Wait for step dependencies to be met |
| 251 | try: |
| 252 | deps = self.dependency_graph[S] |
| 253 | has_deps = True |
| 254 | except KeyError: |
| 255 | has_deps = False |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 256 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 257 | if (has_deps): |
| 258 | for d in deps: |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 259 | if d==step.__name__: |
| 260 | logger.info(" step %s self-wait skipped" % step.__name__) |
| 261 | continue |
| 262 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 263 | cond = self.step_conditions[d] |
| 264 | cond.acquire() |
| 265 | if (self.step_status[d] is STEP_STATUS_WORKING): |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 266 | logger.info(" step %s wait on dep %s" % (step.__name__, d)) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 267 | cond.wait() |
| 268 | cond.release() |
| 269 | go = self.step_status[d] == STEP_STATUS_OK |
| 270 | else: |
| 271 | go = True |
| 272 | |
| 273 | if (not go): |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 274 | # SMBAKER: sync_step was not defined here, so I changed |
| 275 | # this from 'sync_step' to 'step'. Verify. |
| 276 | self.failed_steps.append(step) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 277 | my_status = STEP_STATUS_KO |
| 278 | else: |
| 279 | sync_step = step(driver=self.driver,error_map=self.error_mapper) |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 280 | sync_step.__name__ = step.__name__ |
| 281 | sync_step.dependencies = [] |
| 282 | try: |
| 283 | mlist = sync_step.provides |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 284 | |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 285 | for m in mlist: |
| 286 | sync_step.dependencies.extend(self.model_dependency_graph[m.__name__]) |
| 287 | except KeyError: |
| 288 | pass |
| 289 | sync_step.debug_mode = debug_mode |
| 290 | |
| 291 | should_run = False |
| 292 | try: |
| 293 | # Various checks that decide whether |
| 294 | # this step runs or not |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 295 | self.check_class_dependency(sync_step, self.failed_steps) # dont run Slices if Sites failed |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 296 | self.check_schedule(sync_step, deletion) # dont run sync_network_routes if time since last run < 1 hour |
| 297 | should_run = True |
| 298 | except StepNotReady: |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 299 | logger.info('Step not ready: %s'%sync_step.__name__) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 300 | self.failed_steps.append(sync_step) |
| 301 | my_status = STEP_STATUS_KO |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 302 | except Exception,e: |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 303 | logger.error('%r' % e) |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 304 | logger.log_exc("sync step failed: %r. Deletion: %r"%(sync_step,deletion)) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 305 | self.failed_steps.append(sync_step) |
| 306 | my_status = STEP_STATUS_KO |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 307 | |
| 308 | if (should_run): |
| 309 | try: |
| 310 | duration=time.time() - start_time |
| 311 | |
| 312 | logger.info('Executing step %s' % sync_step.__name__) |
| 313 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 314 | failed_objects = sync_step(failed=list(self.failed_step_objects), deletion=deletion) |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 315 | |
| 316 | self.check_duration(sync_step, duration) |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 317 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 318 | if failed_objects: |
| 319 | self.failed_step_objects.update(failed_objects) |
| 320 | |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 321 | logger.info("Step %r succeeded" % step) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 322 | my_status = STEP_STATUS_OK |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 323 | self.update_run_time(sync_step,deletion) |
| 324 | except Exception,e: |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 325 | 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 Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 326 | logger.log_exc(e) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 327 | self.failed_steps.append(S) |
| 328 | my_status = STEP_STATUS_KO |
| 329 | else: |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 330 | logger.info("Step %r succeeded due to non-run" % step) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 331 | my_status = STEP_STATUS_OK |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 332 | |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 333 | try: |
| 334 | my_cond = self.step_conditions[S] |
| 335 | my_cond.acquire() |
| 336 | self.step_status[S]=my_status |
| 337 | my_cond.notify_all() |
| 338 | my_cond.release() |
| 339 | except KeyError,e: |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 340 | logger.info('Step %r is a leaf' % step) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 341 | pass |
Scott Baker | 2e6c9a4 | 2014-09-05 14:48:38 -0700 | [diff] [blame^] | 342 | finally: |
| 343 | connection.close() |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 344 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 345 | def run(self): |
| 346 | if not self.driver.enabled: |
| 347 | return |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 348 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 349 | if (self.driver_kind=="openstack") and (not self.driver.has_openstack): |
| 350 | return |
| 351 | |
| 352 | while True: |
| 353 | try: |
Sapan Bhatia | 31ebe5c | 2014-04-29 00:24:09 -0400 | [diff] [blame] | 354 | error_map_file = getattr(Config(), "error_map_path", "/opt/planetstack/error_map.txt") |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 355 | self.error_mapper = ErrorMapper(error_map_file) |
| 356 | |
| 357 | # Set of whole steps that failed |
| 358 | self.failed_steps = [] |
| 359 | |
| 360 | # Set of individual objects within steps that failed |
| 361 | self.failed_step_objects = set() |
| 362 | |
| 363 | # Set up conditions and step status |
| 364 | # This is needed for steps to run in parallel |
| 365 | # while obeying dependencies. |
| 366 | |
| 367 | providers = set() |
| 368 | for v in self.dependency_graph.values(): |
| 369 | if (v): |
| 370 | providers.update(v) |
| 371 | |
| 372 | self.step_conditions = {} |
| 373 | self.step_status = {} |
| 374 | for p in list(providers): |
| 375 | self.step_conditions[p] = threading.Condition() |
| 376 | self.step_status[p] = STEP_STATUS_WORKING |
| 377 | |
Sapan Bhatia | 31ebe5c | 2014-04-29 00:24:09 -0400 | [diff] [blame] | 378 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 379 | logger.info('Waiting for event') |
| 380 | tBeforeWait = time.time() |
Sapan Bhatia | 13d8915 | 2014-07-23 10:35:33 -0400 | [diff] [blame] | 381 | self.wait_for_event(timeout=30) |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 382 | logger.info('Observer woke up') |
| 383 | |
Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 384 | # Two passes. One for sync, the other for deletion. |
Sapan Bhatia | 0f727b8 | 2014-08-18 02:44:20 -0400 | [diff] [blame] | 385 | for deletion in [False,True]: |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 386 | threads = [] |
Sapan Bhatia | e82f5e5 | 2014-07-23 10:02:45 -0400 | [diff] [blame] | 387 | logger.info('Deletion=%r...'%deletion) |
Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 388 | schedule = self.ordered_steps if not deletion else reversed(self.ordered_steps) |
| 389 | |
| 390 | for S in schedule: |
| 391 | thread = threading.Thread(target=self.sync, args=(S, deletion)) |
| 392 | |
| 393 | logger.info('Deletion=%r...'%deletion) |
| 394 | threads.append(thread) |
Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 395 | |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 396 | # Start threads |
| 397 | for t in threads: |
| 398 | t.start() |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 399 | |
Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 400 | # Wait for all threads to finish before continuing with the run loop |
| 401 | for t in threads: |
| 402 | t.join() |
Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 403 | |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 404 | self.save_run_times() |
| 405 | except Exception, e: |
Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 406 | logger.error('Core error. This seems like a misconfiguration or bug: %r. This error will not be relayed to the user!' % e) |
Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 407 | logger.log_exc("Exception in observer run loop") |
| 408 | traceback.print_exc() |