Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | 4fa85fb | 2013-09-25 14:39:57 -0400 | [diff] [blame] | 3 | from datetime import datetime |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 4 | from xos.config import Config |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 5 | from util.logger import Logger, logging |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 6 | from observer.steps import * |
Sapan Bhatia | d9468eb | 2014-08-20 03:03:12 -0400 | [diff] [blame] | 7 | from django.db.models import F, Q |
Scott Baker | 8bbc77c | 2015-06-22 10:56:16 -0700 | [diff] [blame] | 8 | from core.models import * |
| 9 | from django.db import reset_queries |
Sapan Bhatia | 7fdccee | 2015-09-16 16:42:38 +0200 | [diff] [blame] | 10 | from observer.ansible import * |
| 11 | |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 12 | import json |
| 13 | import time |
| 14 | import pdb |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 15 | |
Andy Bavier | 04111b7 | 2013-10-22 16:47:10 -0400 | [diff] [blame] | 16 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 17 | |
Sapan Bhatia | 2192fec | 2015-02-08 06:36:32 +0000 | [diff] [blame] | 18 | def f7(seq): |
| 19 | seen = set() |
| 20 | seen_add = seen.add |
| 21 | return [ x for x in seq if not (x in seen or seen_add(x))] |
| 22 | |
| 23 | def elim_dups(backend_str): |
| 24 | strs = backend_str.split(' // ') |
| 25 | strs2 = f7(strs) |
| 26 | return ' // '.join(strs2) |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 27 | |
Sapan Bhatia | 709bebd | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 28 | def deepgetattr(obj, attr): |
| 29 | return reduce(getattr, attr.split('.'), obj) |
| 30 | |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 31 | |
| 32 | class InnocuousException(Exception): |
| 33 | pass |
| 34 | |
Scott Baker | 7ed0b76 | 2015-08-25 16:23:37 -0700 | [diff] [blame] | 35 | class DeferredException(Exception): |
| 36 | pass |
| 37 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 38 | class FailedDependency(Exception): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 39 | pass |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 40 | |
Tony Mack | b469d24 | 2015-01-03 19:37:39 -0500 | [diff] [blame] | 41 | class SyncStep(object): |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 42 | """ An XOS Sync step. |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 43 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 44 | Attributes: |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 45 | psmodel Model name the step synchronizes |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 46 | dependencies list of names of models that must be synchronized first if the current model depends on them |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 47 | """ |
Scott Baker | 1a36d66 | 2015-10-12 18:28:00 -0700 | [diff] [blame^] | 48 | |
| 49 | # map_sync_outputs can return this value to cause a step to be marked |
| 50 | # successful without running ansible. Used for sync_network_controllers |
| 51 | # on nat networks. |
| 52 | SYNC_WITHOUT_RUNNING = "sync_without_running" |
| 53 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 54 | slow=False |
Tony Mack | fbdae1b | 2015-02-24 14:16:43 -0500 | [diff] [blame] | 55 | def get_prop(self, prop): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 56 | try: |
| 57 | sync_config_dir = Config().sync_config_dir |
| 58 | except: |
Scott Baker | b8059c9 | 2015-02-19 22:25:49 -0800 | [diff] [blame] | 59 | sync_config_dir = '/etc/xos/sync' |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 60 | prop_config_path = '/'.join(sync_config_dir,self.name,prop) |
| 61 | return open(prop_config_path).read().rstrip() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 62 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 63 | def __init__(self, **args): |
| 64 | """Initialize a sync step |
| 65 | Keyword arguments: |
| 66 | name -- Name of the step |
Scott Baker | 286a78f | 2015-02-18 16:13:48 -0800 | [diff] [blame] | 67 | provides -- XOS models sync'd by this step |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 68 | """ |
| 69 | dependencies = [] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 70 | self.driver = args.get('driver') |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 71 | self.error_map = args.get('error_map') |
| 72 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 73 | try: |
| 74 | self.soft_deadline = int(self.get_prop('soft_deadline_seconds')) |
| 75 | except: |
| 76 | self.soft_deadline = 5 # 5 seconds |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 77 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 78 | return |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 79 | |
Sapan Bhatia | e17bc5b | 2014-04-30 00:53:06 -0400 | [diff] [blame] | 80 | def fetch_pending(self, deletion=False): |
Sapan Bhatia | 2176566 | 2014-07-23 08:59:30 -0400 | [diff] [blame] | 81 | # This is the most common implementation of fetch_pending |
| 82 | # Steps should override it if they have their own logic |
| 83 | # for figuring out what objects are outstanding. |
Sapan Bhatia | ab18ee4 | 2015-08-19 12:20:30 -0400 | [diff] [blame] | 84 | |
| 85 | main_objs = self.observes |
| 86 | if (type(main_objs) is not list): |
Sapan Bhatia | 83e7a64 | 2015-09-10 11:08:35 -0400 | [diff] [blame] | 87 | main_objs=[main_objs] |
Sapan Bhatia | ab18ee4 | 2015-08-19 12:20:30 -0400 | [diff] [blame] | 88 | |
| 89 | objs = [] |
| 90 | for main_obj in main_objs: |
| 91 | if (not deletion): |
Sapan Bhatia | d063f5f | 2015-09-16 17:42:07 +0200 | [diff] [blame] | 92 | lobjs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False),Q(no_sync=False)) |
Sapan Bhatia | ab18ee4 | 2015-08-19 12:20:30 -0400 | [diff] [blame] | 93 | else: |
| 94 | lobjs = main_obj.deleted_objects.all() |
| 95 | objs.extend(lobjs) |
Sapan Bhatia | 2176566 | 2014-07-23 08:59:30 -0400 | [diff] [blame] | 96 | |
| 97 | return objs |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 98 | #return Instance.objects.filter(ip=None) |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 99 | |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 100 | def check_dependencies(self, obj, failed): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 101 | for dep in self.dependencies: |
Scott Baker | 105b6b7 | 2014-05-12 10:40:25 -0700 | [diff] [blame] | 102 | peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 103 | |
Sapan Bhatia | cfef6ef | 2014-08-20 03:04:03 -0400 | [diff] [blame] | 104 | try: |
Sapan Bhatia | 709bebd | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 105 | peer_object = deepgetattr(obj, peer_name) |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 106 | try: |
| 107 | peer_objects = peer_object.all() |
Sapan Bhatia | 709bebd | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 108 | except AttributeError: |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 109 | peer_objects = [peer_object] |
Sapan Bhatia | cfef6ef | 2014-08-20 03:04:03 -0400 | [diff] [blame] | 110 | except: |
Sapan Bhatia | 709bebd | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 111 | peer_objects = [] |
Sapan Bhatia | cfef6ef | 2014-08-20 03:04:03 -0400 | [diff] [blame] | 112 | |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 113 | if (hasattr(obj,'controller')): |
| 114 | try: |
| 115 | peer_objects = filter(lambda o:o.controller==obj.controller, peer_objects) |
| 116 | except AttributeError: |
| 117 | pass |
| 118 | |
Sapan Bhatia | 709bebd | 2015-02-08 06:35:36 +0000 | [diff] [blame] | 119 | if (failed in peer_objects): |
| 120 | if (obj.backend_status!=failed.backend_status): |
| 121 | obj.backend_status = failed.backend_status |
Sapan Bhatia | 7e482de | 2014-08-22 03:05:13 -0400 | [diff] [blame] | 122 | obj.save(update_fields=['backend_status']) |
Scott Baker | 4fd314e | 2015-03-04 21:31:14 -0800 | [diff] [blame] | 123 | raise FailedDependency("Failed dependency for %s:%s peer %s:%s failed %s:%s" % (obj.__class__.__name__, str(getattr(obj,"pk","no_pk")), peer_object.__class__.__name__, str(getattr(peer_object,"pk","no_pk")), failed.__class__.__name__, str(getattr(failed,"pk","no_pk")))) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 124 | |
Sapan Bhatia | 7fdccee | 2015-09-16 16:42:38 +0200 | [diff] [blame] | 125 | |
| 126 | def sync_record(self, o): |
| 127 | try: |
| 128 | controller = o.get_controller() |
| 129 | controller_register = json.loads(o.node.site_deployment.controller.backend_register) |
| 130 | |
| 131 | if (controller_register.get('disabled',False)): |
| 132 | raise InnocuousException('Controller %s is disabled'%sliver.node.site_deployment.controller.name) |
| 133 | except AttributeError: |
| 134 | pass |
| 135 | |
| 136 | tenant_fields = self.map_sync_inputs(o) |
Scott Baker | 1a36d66 | 2015-10-12 18:28:00 -0700 | [diff] [blame^] | 137 | if tenant_fields == SyncStep.SYNC_WITHOUT_RUNNING: |
| 138 | return |
Sapan Bhatia | 7fdccee | 2015-09-16 16:42:38 +0200 | [diff] [blame] | 139 | main_objs=self.observes |
| 140 | if (type(main_objs) is list): |
| 141 | main_objs=main_objs[0] |
| 142 | |
| 143 | path = ''.join(main_objs.__name__).lower() |
| 144 | res = run_template(self.playbook,tenant_fields,path=path) |
| 145 | |
| 146 | try: |
| 147 | self.map_sync_outputs(o,res) |
| 148 | except AttributeError: |
| 149 | pass |
| 150 | |
| 151 | def delete_record(self, o): |
| 152 | try: |
| 153 | controller = o.get_controller() |
| 154 | controller_register = json.loads(o.node.site_deployment.controller.backend_register) |
| 155 | |
| 156 | if (controller_register.get('disabled',False)): |
| 157 | raise InnocuousException('Controller %s is disabled'%sliver.node.site_deployment.controller.name) |
| 158 | except AttributeError: |
| 159 | pass |
| 160 | |
Scott Baker | f7cb913 | 2015-09-17 22:04:57 -0700 | [diff] [blame] | 161 | tenant_fields = self.map_delete_inputs(o) |
Sapan Bhatia | 7fdccee | 2015-09-16 16:42:38 +0200 | [diff] [blame] | 162 | |
| 163 | main_objs=self.observes |
| 164 | if (type(main_objs) is list): |
| 165 | main_objs=main_objs[0] |
| 166 | |
| 167 | path = ''.join(main_objs.__name__).lower() |
| 168 | |
| 169 | tenant_fields['delete']=True |
| 170 | res = run_template(self.playbook,tenant_fields,path=path) |
| 171 | try: |
| 172 | self.map_delete_outputs(o,res) |
| 173 | except AttributeError: |
| 174 | pass |
| 175 | |
Sapan Bhatia | 6082336 | 2014-04-30 00:52:32 -0400 | [diff] [blame] | 176 | def call(self, failed=[], deletion=False): |
| 177 | pending = self.fetch_pending(deletion) |
Sapan Bhatia | 7fdccee | 2015-09-16 16:42:38 +0200 | [diff] [blame] | 178 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 179 | for o in pending: |
Scott Baker | 8bbc77c | 2015-06-22 10:56:16 -0700 | [diff] [blame] | 180 | # another spot to clean up debug state |
| 181 | try: |
| 182 | reset_queries() |
| 183 | except: |
| 184 | # this shouldn't happen, but in case it does, catch it... |
| 185 | logger.log_exc("exception in reset_queries") |
| 186 | |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 187 | sync_failed = False |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 188 | try: |
Sapan Bhatia | 24a2a29 | 2015-02-10 17:21:33 -0500 | [diff] [blame] | 189 | backoff_disabled = Config().observer_backoff_disabled |
Sapan Bhatia | 9cd17be | 2015-02-10 17:16:07 -0500 | [diff] [blame] | 190 | except: |
| 191 | backoff_disabled = 0 |
| 192 | |
| 193 | try: |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 194 | scratchpad = json.loads(o.backend_register) |
| 195 | if (scratchpad): |
| 196 | next_run = scratchpad['next_run'] |
Sapan Bhatia | 9cd17be | 2015-02-10 17:16:07 -0500 | [diff] [blame] | 197 | if (not backoff_disabled and next_run>time.time()): |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 198 | sync_failed = True |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 199 | except: |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 200 | logger.log_exc("Exception while loading scratchpad") |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 201 | pass |
| 202 | |
| 203 | if (not sync_failed): |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 204 | try: |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 205 | for f in failed: |
| 206 | self.check_dependencies(o,f) # Raises exception if failed |
| 207 | if (deletion): |
| 208 | self.delete_record(o) |
| 209 | o.delete(purge=True) |
| 210 | else: |
| 211 | self.sync_record(o) |
| 212 | o.enacted = datetime.now() # Is this the same timezone? XXX |
| 213 | scratchpad = {'next_run':0, 'exponent':0} |
| 214 | o.backend_register = json.dumps(scratchpad) |
| 215 | o.backend_status = "1 - OK" |
| 216 | o.save(update_fields=['enacted','backend_status','backend_register']) |
Scott Baker | 7ed0b76 | 2015-08-25 16:23:37 -0700 | [diff] [blame] | 217 | except (InnocuousException,Exception,DeferredException) as e: |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 218 | logger.log_exc("sync step failed!") |
Sapan Bhatia | 2175c1d | 2015-02-08 06:31:42 +0000 | [diff] [blame] | 219 | try: |
| 220 | if (o.backend_status.startswith('2 - ')): |
| 221 | str_e = '%s // %r'%(o.backend_status[4:],e) |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 222 | str_e = elim_dups(str_e) |
Sapan Bhatia | 2175c1d | 2015-02-08 06:31:42 +0000 | [diff] [blame] | 223 | else: |
| 224 | str_e = '%r'%e |
| 225 | except: |
| 226 | str_e = '%r'%e |
| 227 | |
Sapan Bhatia | 9c308fc | 2014-08-22 03:07:59 -0400 | [diff] [blame] | 228 | try: |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 229 | error = self.error_map.map(str_e) |
Sapan Bhatia | 9c308fc | 2014-08-22 03:07:59 -0400 | [diff] [blame] | 230 | except: |
Sapan Bhatia | c368d4a | 2015-06-09 14:14:12 -0400 | [diff] [blame] | 231 | error = '%s'%str_e |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 232 | |
| 233 | if isinstance(e, InnocuousException) and not force_error: |
| 234 | o.backend_status = '1 - %s'%error |
| 235 | else: |
Sapan Bhatia | 5e2f87a | 2015-05-13 15:52:45 +0200 | [diff] [blame] | 236 | o.backend_status = '2 - %s'%error |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 237 | |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 238 | try: |
| 239 | scratchpad = json.loads(o.backend_register) |
| 240 | scratchpad['exponent'] |
| 241 | except: |
Sapan Bhatia | e6376de | 2015-05-13 15:51:03 +0200 | [diff] [blame] | 242 | logger.log_exc("Exception while updating scratchpad") |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 243 | scratchpad = {'next_run':0, 'exponent':0} |
| 244 | |
| 245 | # Second failure |
| 246 | if (scratchpad['exponent']): |
Scott Baker | 7ed0b76 | 2015-08-25 16:23:37 -0700 | [diff] [blame] | 247 | if isinstance(e,DeferredException): |
| 248 | delay = scratchpad['exponent'] * 60 # 1 minute |
| 249 | else: |
| 250 | delay = scratchpad['exponent'] * 600 # 10 minutes |
| 251 | # cap delays at 8 hours |
| 252 | if (delay>8*60*60): |
| 253 | delay=8*60*60 |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 254 | scratchpad['next_run'] = time.time() + delay |
| 255 | |
| 256 | scratchpad['exponent']+=1 |
| 257 | |
| 258 | o.backend_register = json.dumps(scratchpad) |
| 259 | |
| 260 | # TOFIX: |
| 261 | # DatabaseError: value too long for type character varying(140) |
| 262 | if (o.pk): |
| 263 | try: |
Sapan Bhatia | 2175c1d | 2015-02-08 06:31:42 +0000 | [diff] [blame] | 264 | o.backend_status = o.backend_status[:1024] |
Sapan Bhatia | 5e2f87a | 2015-05-13 15:52:45 +0200 | [diff] [blame] | 265 | o.save(update_fields=['backend_status','backend_register','updated']) |
Sapan Bhatia | 4700611 | 2015-01-29 20:55:40 +0000 | [diff] [blame] | 266 | except: |
| 267 | print "Could not update backend status field!" |
| 268 | pass |
| 269 | sync_failed = True |
| 270 | |
| 271 | |
| 272 | if (sync_failed): |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 273 | failed.append(o) |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 274 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 275 | return failed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 276 | |
Tony Mack | 16f0474 | 2013-09-25 08:53:28 -0400 | [diff] [blame] | 277 | def __call__(self, **args): |
| 278 | return self.call(**args) |