blob: dcfea7db80316457798b540fd0991aef9534f14c [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mack4fa85fb2013-09-25 14:39:57 -04003from datetime import datetime
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Andy Baviere7abb622013-10-18 15:11:56 -04005from util.logger import Logger, logging
Sapan Bhatiaeba08432014-04-28 23:58:36 -04006from observer.steps import *
Andy Baviere7abb622013-10-18 15:11:56 -04007
Andy Bavier04111b72013-10-22 16:47:10 -04008logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -04009
Sapan Bhatia13c7f112013-09-02 14:19:35 -040010class FailedDependency(Exception):
Tony Mackce79de02013-09-24 10:12:33 -040011 pass
Sapan Bhatia13c7f112013-09-02 14:19:35 -040012
Sapan Bhatia24836f12013-08-27 10:16:05 -040013class SyncStep:
Tony Mackce79de02013-09-24 10:12:33 -040014 """ A PlanetStack Sync step.
Sapan Bhatia24836f12013-08-27 10:16:05 -040015
Tony Mackce79de02013-09-24 10:12:33 -040016 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 Bhatia24836f12013-08-27 10:16:05 -040028
Tony Mackce79de02013-09-24 10:12:33 -040029 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 Mack387a73f2013-09-18 07:59:14 -040036 self.driver = args.get('driver')
Sapan Bhatiaeba08432014-04-28 23:58:36 -040037 self.error_map = args.get('error_map')
38
Tony Mackce79de02013-09-24 10:12:33 -040039 try:
40 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
41 except:
42 self.soft_deadline = 5 # 5 seconds
Sapan Bhatia24836f12013-08-27 10:16:05 -040043
Tony Mackce79de02013-09-24 10:12:33 -040044 return
Sapan Bhatia24836f12013-08-27 10:16:05 -040045
Sapan Bhatiae17bc5b2014-04-30 00:53:06 -040046 def fetch_pending(self, deletion=False):
Sapan Bhatia21765662014-07-23 08:59:30 -040047 # This is the most common implementation of fetch_pending
48 # Steps should override it if they have their own logic
49 # for figuring out what objects are outstanding.
50 main_obj = self.provides[0]
51 if (not deleted):
52 objs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
53 else:
54 objs = main_obj.deleted_objects.all()
55
56 return objs
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040057 #return Sliver.objects.filter(ip=None)
Tony Mackce79de02013-09-24 10:12:33 -040058
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040059 def check_dependencies(self, obj, failed):
Tony Mackce79de02013-09-24 10:12:33 -040060 for dep in self.dependencies:
Scott Baker105b6b72014-05-12 10:40:25 -070061 peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower
62 peer_object = getattr(obj, peer_name)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040063 if (peer_object.pk==failed.pk):
Tony Mack37146d22013-10-21 11:02:51 -040064 raise FailedDependency
Sapan Bhatia24836f12013-08-27 10:16:05 -040065
Sapan Bhatia60823362014-04-30 00:52:32 -040066 def call(self, failed=[], deletion=False):
67 pending = self.fetch_pending(deletion)
Tony Mackce79de02013-09-24 10:12:33 -040068 for o in pending:
Tony Mack68e818d2013-09-25 13:34:17 -040069 try:
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040070 for f in failed:
Andy Baviere7abb622013-10-18 15:11:56 -040071 self.check_dependencies(o,f) # Raises exception if failed
Sapan Bhatia60823362014-04-30 00:52:32 -040072 if (deletion):
73 self.delete_record(o)
74 o.delete(purge=True)
75 else:
76 self.sync_record(o)
77 o.enacted = datetime.now() # Is this the same timezone? XXX
78 o.backend_status = "OK"
79 o.save(update_fields=['enacted'])
Sapan Bhatiaeba08432014-04-28 23:58:36 -040080 except Exception,e:
81 try:
82 o.backend_status = self.error_map.map(str(e))
83 except:
84 o.backend_status = str(e)
85
86 o.save(update_fields=['backend_status'])
87
88 logger.log_exc("sync step failed!")
Tony Mack68e818d2013-09-25 13:34:17 -040089 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040090
Tony Mackce79de02013-09-24 10:12:33 -040091 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -040092
Tony Mack16f04742013-09-25 08:53:28 -040093 def __call__(self, **args):
94 return self.call(**args)