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 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.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 * |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 7 | |
Andy Bavier | 04111b7 | 2013-10-22 16:47:10 -0400 | [diff] [blame] | 8 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 9 | |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 10 | class FailedDependency(Exception): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 11 | pass |
Sapan Bhatia | 13c7f11 | 2013-09-02 14:19:35 -0400 | [diff] [blame] | 12 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 13 | class SyncStep: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 14 | """ A PlanetStack Sync step. |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 15 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 16 | Attributes: |
| 17 | psmodel Model name the step synchronizes |
| 18 | dependencies list of names of models that must be synchronized first if the current model depends on them |
| 19 | """ |
| 20 | slow=False |
| 21 | def get_prop(prop): |
| 22 | try: |
| 23 | sync_config_dir = Config().sync_config_dir |
| 24 | except: |
| 25 | sync_config_dir = '/etc/planetstack/sync' |
| 26 | prop_config_path = '/'.join(sync_config_dir,self.name,prop) |
| 27 | return open(prop_config_path).read().rstrip() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 28 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 29 | def __init__(self, **args): |
| 30 | """Initialize a sync step |
| 31 | Keyword arguments: |
| 32 | name -- Name of the step |
| 33 | provides -- PlanetStack models sync'd by this step |
| 34 | """ |
| 35 | dependencies = [] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 36 | self.driver = args.get('driver') |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 37 | self.error_map = args.get('error_map') |
| 38 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 39 | try: |
| 40 | self.soft_deadline = int(self.get_prop('soft_deadline_seconds')) |
| 41 | except: |
| 42 | self.soft_deadline = 5 # 5 seconds |
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 | return |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 45 | |
Sapan Bhatia | e17bc5b | 2014-04-30 00:53:06 -0400 | [diff] [blame^] | 46 | def fetch_pending(self, deletion=False): |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 47 | return [] |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 48 | #return Sliver.objects.filter(ip=None) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 49 | |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 50 | def check_dependencies(self, obj, failed): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 51 | for dep in self.dependencies: |
Scott Baker | 105b6b7 | 2014-05-12 10:40:25 -0700 | [diff] [blame] | 52 | peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower |
| 53 | peer_object = getattr(obj, peer_name) |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 54 | if (peer_object.pk==failed.pk): |
Tony Mack | 37146d2 | 2013-10-21 11:02:51 -0400 | [diff] [blame] | 55 | raise FailedDependency |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 56 | |
Sapan Bhatia | 6082336 | 2014-04-30 00:52:32 -0400 | [diff] [blame] | 57 | def call(self, failed=[], deletion=False): |
| 58 | pending = self.fetch_pending(deletion) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 59 | for o in pending: |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 60 | try: |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 61 | for f in failed: |
Andy Bavier | e7abb62 | 2013-10-18 15:11:56 -0400 | [diff] [blame] | 62 | self.check_dependencies(o,f) # Raises exception if failed |
Sapan Bhatia | 6082336 | 2014-04-30 00:52:32 -0400 | [diff] [blame] | 63 | if (deletion): |
| 64 | self.delete_record(o) |
| 65 | o.delete(purge=True) |
| 66 | else: |
| 67 | self.sync_record(o) |
| 68 | o.enacted = datetime.now() # Is this the same timezone? XXX |
| 69 | o.backend_status = "OK" |
| 70 | o.save(update_fields=['enacted']) |
Sapan Bhatia | eba0843 | 2014-04-28 23:58:36 -0400 | [diff] [blame] | 71 | except Exception,e: |
| 72 | try: |
| 73 | o.backend_status = self.error_map.map(str(e)) |
| 74 | except: |
| 75 | o.backend_status = str(e) |
| 76 | |
| 77 | o.save(update_fields=['backend_status']) |
| 78 | |
| 79 | logger.log_exc("sync step failed!") |
Tony Mack | 68e818d | 2013-09-25 13:34:17 -0400 | [diff] [blame] | 80 | failed.append(o) |
Sapan Bhatia | ca2e21f | 2013-10-02 01:10:02 -0400 | [diff] [blame] | 81 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 82 | return failed |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 83 | |
Tony Mack | 16f0474 | 2013-09-25 08:53:28 -0400 | [diff] [blame] | 84 | def __call__(self, **args): |
| 85 | return self.call(**args) |