| 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 |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 5 | import sys |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 6 | import traceback |
| 7 | import commands |
| 8 | import threading |
| 9 | import json |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 10 | import pdb |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 11 | import pprint |
| 12 | |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 13 | |
| 14 | from datetime import datetime |
| 15 | from collections import defaultdict |
| 16 | from core.models import * |
| 17 | from django.db.models import F, Q |
| Scott Baker | 2e6c9a4 | 2014-09-05 14:48:38 -0700 | [diff] [blame] | 18 | from django.db import connection |
| Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 19 | #from openstack.manager import OpenStackManager |
| 20 | from openstack.driver import OpenStackDriver |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 21 | from util.logger import Logger, logging, logger |
| 22 | #from timeout import timeout |
| Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 23 | from xos.config import Config, XOS_DIR |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 24 | from observer.steps import * |
| Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 25 | from syncstep import SyncStep |
| Sapan Bhatia | 45cbbc3 | 2014-03-11 17:48:30 -0400 | [diff] [blame] | 26 | from toposort import toposort |
| Sapan Bhatia | 13d8915 | 2014-07-23 10:35:33 -0400 | [diff] [blame] | 27 | from observer.error_mapper import * |
| Sapan Bhatia | 8dde6b9 | 2015-01-17 01:03:52 +0000 | [diff] [blame] | 28 | from openstack_observer.openstacksyncstep import OpenStackSyncStep |
| 29 | |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 30 | |
| Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 31 | debug_mode = False |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 32 | |
| Sapan Bhatia | 8dde6b9 | 2015-01-17 01:03:52 +0000 | [diff] [blame] | 33 | class 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 Bavier | 04111b7 | 2013-10-22 16:47:10 -0400 | [diff] [blame] | 43 | logger = Logger(level=logging.INFO) |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 44 | |
| Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 45 | class StepNotReady(Exception): |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 46 | pass |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 47 | |
| Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 48 | class NoOpDriver: |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 49 | def __init__(self): |
| 50 | self.enabled = True |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 51 | self.dependency_graph = None |
| 52 | |
| 53 | STEP_STATUS_WORKING=1 |
| 54 | STEP_STATUS_OK=2 |
| 55 | STEP_STATUS_KO=3 |
| 56 | |
| 57 | def 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 Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 64 | ig[v0]=[k] |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 65 | return ig |
| Scott Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 66 | |
| Scott Baker | 0355284 | 2015-02-18 16:13:48 -0800 | [diff] [blame] | 67 | class XOSObserver: |
| Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 68 | #sync_steps = [SyncNetworks,SyncNetworkSlivers,SyncSites,SyncSitePrivilege,SyncSlices,SyncSliceMemberships,SyncSlivers,SyncSliverIps,SyncExternalRoutes,SyncUsers,SyncRoles,SyncNodes,SyncImages,GarbageCollector] |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 69 | sync_steps = [] |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 70 | |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 71 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 72 | 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 Baker | 7771f41 | 2014-01-02 16:36:41 -0800 | [diff] [blame] | 78 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 79 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 84 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 85 | def wait_for_event(self, timeout): |
| 86 | self.event_cond.acquire() |
| 87 | self.event_cond.wait(timeout) |
| 88 | self.event_cond.release() |
| Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 89 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 90 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 95 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 96 | 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 Baker | 7069c16 | 2015-02-04 16:59:45 -0800 | [diff] [blame] | 101 | step_dir = XOS_DIR + "/observer/steps" |
| Scott Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 102 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 103 | 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 Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 109 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 110 | # 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 Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 113 | |
| Sapan Bhatia | b3c46bc | 2015-01-17 01:04:10 +0000 | [diff] [blame] | 114 | if inspect.isclass(c) and (issubclass(c, SyncStep) or issubclass(c,OpenStackSyncStep)) and hasattr(c,"provides") and (c not in self.sync_steps): |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 115 | 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 Baker | 45fb7a1 | 2013-12-31 00:56:19 -0800 | [diff] [blame] | 118 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 119 | 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 Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 125 | for left,lst in self.model_dependency_graph.items(): |
| 126 | new_lst = [] |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 127 | for k in lst: |
| 128 | try: |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 129 | tup = (k,k.lower()) |
| 130 | new_lst.append(tup) |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 131 | deps = self.model_dependency_graph[k] |
| 132 | except: |
| 133 | self.model_dependency_graph[k] = [] |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 134 | |
| 135 | self.model_dependency_graph[left] = new_lst |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 136 | except Exception,e: |
| 137 | raise e |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 138 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 139 | 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 Bhatia | ed6fc2d | 2015-01-29 20:51:13 +0000 | [diff] [blame] | 144 | 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 Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 150 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 154 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 155 | 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 Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 163 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 164 | step_graph = {} |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 165 | for k,v in self.model_dependency_graph.items(): |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 166 | try: |
| 167 | for source in provides_dict[k]: |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 168 | if (not v): |
| 169 | step_graph[source] = [] |
| 170 | |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 171 | for m,_ in v: |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 172 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 187 | |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 188 | self.dependency_graph = step_graph |
| 189 | self.deletion_dependency_graph = invert_graph(step_graph) |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 190 | |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 191 | pp = pprint.PrettyPrinter(indent=4) |
| 192 | pp.pprint(step_graph) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 193 | self.ordered_steps = toposort(self.dependency_graph, map(lambda s:s.__name__,self.sync_steps)) |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 194 | #self.ordered_steps = ['SyncRoles', 'SyncControllerSites', 'SyncControllerSitePrivileges','SyncImages', 'SyncControllerImages','SyncControllerUsers','SyncControllerUserSitePrivileges','SyncControllerSlices', 'SyncControllerSlicePrivileges', 'SyncControllerUserSlicePrivileges', 'SyncControllerNetworks','SyncSlivers'] |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 195 | #self.ordered_steps = ['SyncControllerSites','SyncControllerUsers','SyncControllerSlices','SyncControllerNetworks'] |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 196 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 197 | print "Order of steps=",self.ordered_steps |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 198 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 199 | self.load_run_times() |
| 200 | |
| Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 201 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 202 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 209 | |
| Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 210 | 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 Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 215 | |
| Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 216 | |
| 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 Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 221 | 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 Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 236 | 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 Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 244 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 245 | 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 Bhatia | 36938ca | 2013-09-02 14:35:24 -0400 | [diff] [blame] | 248 | |
| Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 249 | deletion_run_times = json.dumps(self.last_deletion_run_times) |
| 250 | open('/tmp/observer_deletion_run_times','w').write(deletion_run_times) |
| 251 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 252 | def check_class_dependency(self, step, failed_steps): |
| 253 | step.dependenices = [] |
| 254 | for obj in step.provides: |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 255 | lst = self.model_dependency_graph.get(obj.__name__, []) |
| 256 | nlst = map(lambda(a,b):b,lst) |
| 257 | step.dependenices.extend(nlst) |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 258 | for failed_step in failed_steps: |
| 259 | if (failed_step in step.dependencies): |
| 260 | raise StepNotReady |
| 261 | |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 262 | def sync(self, S, deletion): |
| Scott Baker | 2e6c9a4 | 2014-09-05 14:48:38 -0700 | [diff] [blame] | 263 | try: |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 264 | step = self.step_lookup[S] |
| 265 | start_time=time.time() |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 266 | |
| Scott Baker | fb162a1 | 2015-03-09 16:25:11 -0700 | [diff] [blame^] | 267 | logger.info("Starting to work on step %s, deletion=%s" % (step.__name__, str(deletion))) |
| 268 | |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 269 | dependency_graph = self.dependency_graph if not deletion else self.deletion_dependency_graph |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 270 | step_conditions = self.step_conditions# if not deletion else self.deletion_step_conditions |
| 271 | step_status = self.step_status# if not deletion else self.deletion_step_status |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 272 | |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 273 | # Wait for step dependencies to be met |
| 274 | try: |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 275 | deps = dependency_graph[S] |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 276 | has_deps = True |
| 277 | except KeyError: |
| 278 | has_deps = False |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 279 | |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 280 | go = True |
| Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 281 | |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 282 | failed_dep = None |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 283 | if (has_deps): |
| 284 | for d in deps: |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 285 | if d==step.__name__: |
| 286 | logger.info(" step %s self-wait skipped" % step.__name__) |
| Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 287 | go = True |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 288 | continue |
| 289 | |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 290 | cond = step_conditions[d] |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 291 | cond.acquire() |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 292 | if (step_status[d] is STEP_STATUS_WORKING): |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 293 | logger.info(" step %s wait on dep %s" % (step.__name__, d)) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 294 | cond.wait() |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 295 | elif step_status[d] == STEP_STATUS_OK: |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 296 | go = True |
| 297 | else: |
| 298 | go = False |
| 299 | failed_dep = d |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 300 | cond.release() |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 301 | if (not go): |
| 302 | break |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 303 | else: |
| 304 | go = True |
| 305 | |
| 306 | if (not go): |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 307 | print bcolors.FAIL + "Step %r skipped on %r" % (step,failed_dep) + bcolors.ENDC |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 308 | # SMBAKER: sync_step was not defined here, so I changed |
| 309 | # this from 'sync_step' to 'step'. Verify. |
| 310 | self.failed_steps.append(step) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 311 | my_status = STEP_STATUS_KO |
| 312 | else: |
| 313 | sync_step = step(driver=self.driver,error_map=self.error_mapper) |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 314 | sync_step. __name__= step.__name__ |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 315 | sync_step.dependencies = [] |
| 316 | try: |
| 317 | mlist = sync_step.provides |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 318 | |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 319 | for m in mlist: |
| Sapan Bhatia | 082ee36 | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 320 | lst = self.model_dependency_graph[m.__name__] |
| 321 | nlst = map(lambda(a,b):b,lst) |
| 322 | sync_step.dependencies.extend(nlst) |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 323 | except KeyError: |
| 324 | pass |
| 325 | sync_step.debug_mode = debug_mode |
| 326 | |
| 327 | should_run = False |
| 328 | try: |
| 329 | # Various checks that decide whether |
| 330 | # this step runs or not |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 331 | 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] | 332 | self.check_schedule(sync_step, deletion) # dont run sync_network_routes if time since last run < 1 hour |
| 333 | should_run = True |
| 334 | except StepNotReady: |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 335 | logger.info('Step not ready: %s'%sync_step.__name__) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 336 | self.failed_steps.append(sync_step) |
| 337 | my_status = STEP_STATUS_KO |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 338 | except Exception,e: |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 339 | logger.error('%r' % e) |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 340 | logger.log_exc("sync step failed: %r. Deletion: %r"%(sync_step,deletion)) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 341 | self.failed_steps.append(sync_step) |
| 342 | my_status = STEP_STATUS_KO |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 343 | |
| 344 | if (should_run): |
| 345 | try: |
| 346 | duration=time.time() - start_time |
| 347 | |
| 348 | logger.info('Executing step %s' % sync_step.__name__) |
| 349 | |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 350 | print bcolors.OKBLUE + "Executing step %s" % sync_step.__name__ + bcolors.ENDC |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 351 | failed_objects = sync_step(failed=list(self.failed_step_objects), deletion=deletion) |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 352 | |
| 353 | self.check_duration(sync_step, duration) |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 354 | |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 355 | if failed_objects: |
| 356 | self.failed_step_objects.update(failed_objects) |
| 357 | |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 358 | logger.info("Step %r succeeded" % step) |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 359 | print bcolors.OKGREEN + "Step %r succeeded" % step + bcolors.ENDC |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 360 | my_status = STEP_STATUS_OK |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 361 | self.update_run_time(sync_step,deletion) |
| 362 | except Exception,e: |
| Sapan Bhatia | 122595c | 2015-01-27 03:58:11 +0000 | [diff] [blame] | 363 | print bcolors.FAIL + "Model step %r failed" % (step) + bcolors.ENDC |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 364 | 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] | 365 | logger.log_exc(e) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 366 | self.failed_steps.append(S) |
| 367 | my_status = STEP_STATUS_KO |
| 368 | else: |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 369 | logger.info("Step %r succeeded due to non-run" % step) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 370 | my_status = STEP_STATUS_OK |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 371 | |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 372 | try: |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 373 | my_cond = step_conditions[S] |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 374 | my_cond.acquire() |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 375 | step_status[S]=my_status |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 376 | my_cond.notify_all() |
| 377 | my_cond.release() |
| 378 | except KeyError,e: |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 379 | logger.info('Step %r is a leaf' % step) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 380 | pass |
| Scott Baker | 2e6c9a4 | 2014-09-05 14:48:38 -0700 | [diff] [blame] | 381 | finally: |
| 382 | connection.close() |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 383 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 384 | def run(self): |
| 385 | if not self.driver.enabled: |
| 386 | return |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 387 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 388 | if (self.driver_kind=="openstack") and (not self.driver.has_openstack): |
| 389 | return |
| 390 | |
| 391 | while True: |
| 392 | try: |
| Sapan Bhatia | 1e92125 | 2015-01-29 20:54:17 +0000 | [diff] [blame] | 393 | loop_start = time.time() |
| Scott Baker | 7069c16 | 2015-02-04 16:59:45 -0800 | [diff] [blame] | 394 | error_map_file = getattr(Config(), "error_map_path", XOS_DIR + "/error_map.txt") |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 395 | self.error_mapper = ErrorMapper(error_map_file) |
| 396 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 397 | logger.info('Waiting for event') |
| 398 | tBeforeWait = time.time() |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 399 | self.wait_for_event(timeout=5) |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 400 | logger.info('Observer woke up') |
| 401 | |
| Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 402 | # Two passes. One for sync, the other for deletion. |
| Sapan Bhatia | 68c9928 | 2015-03-04 10:21:06 -0500 | [diff] [blame] | 403 | for deletion in [False]:#[False,True]: |
| 404 | # Set of individual objects within steps that failed |
| 405 | self.failed_step_objects = set() |
| 406 | |
| 407 | # Set up conditions and step status |
| 408 | # This is needed for steps to run in parallel |
| 409 | # while obeying dependencies. |
| 410 | |
| 411 | providers = set() |
| 412 | dependency_graph = self.dependency_graph if not deletion else self.deletion_dependency_graph |
| 413 | |
| 414 | for v in dependency_graph.values(): |
| 415 | if (v): |
| 416 | providers.update(v) |
| 417 | |
| 418 | self.step_conditions = {} |
| 419 | self.step_status = {} |
| 420 | |
| 421 | for p in list(providers): |
| 422 | self.step_conditions[p] = threading.Condition() |
| 423 | |
| 424 | self.step_status[p] = STEP_STATUS_WORKING |
| 425 | |
| 426 | self.failed_steps = [] |
| 427 | |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 428 | threads = [] |
| Sapan Bhatia | e82f5e5 | 2014-07-23 10:02:45 -0400 | [diff] [blame] | 429 | logger.info('Deletion=%r...'%deletion) |
| Sapan Bhatia | ab202a6 | 2014-09-03 11:30:21 -0400 | [diff] [blame] | 430 | schedule = self.ordered_steps if not deletion else reversed(self.ordered_steps) |
| 431 | |
| 432 | for S in schedule: |
| 433 | thread = threading.Thread(target=self.sync, args=(S, deletion)) |
| 434 | |
| 435 | logger.info('Deletion=%r...'%deletion) |
| 436 | threads.append(thread) |
| Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 437 | |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 438 | # Start threads |
| 439 | for t in threads: |
| 440 | t.start() |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 441 | |
| Sapan Bhatia | 51f4893 | 2014-08-25 04:17:12 -0400 | [diff] [blame] | 442 | # Wait for all threads to finish before continuing with the run loop |
| 443 | for t in threads: |
| 444 | t.join() |
| Sapan Bhatia | 285decb | 2014-04-30 00:31:44 -0400 | [diff] [blame] | 445 | |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 446 | self.save_run_times() |
| Sapan Bhatia | 1e92125 | 2015-01-29 20:54:17 +0000 | [diff] [blame] | 447 | loop_end = time.time() |
| 448 | open('/tmp/observer_last_run','w').write(json.dumps({'last_run': loop_end, 'last_duration':loop_end - loop_start})) |
| Sapan Bhatia | f73664b | 2014-04-28 13:07:18 -0400 | [diff] [blame] | 449 | except Exception, e: |
| Scott Baker | 72aa930 | 2014-09-04 10:36:51 -0700 | [diff] [blame] | 450 | 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] | 451 | logger.log_exc("Exception in observer run loop") |
| 452 | traceback.print_exc() |