blob: c77c8d51b088aff7d71a1be5b5169897ceade81f [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)
Scott Baker0652f4e2014-08-19 15:49:27 -070063 if (peer_object and peer_object.pk==failed.pk and type(peer_object)==type(failed)):
64 raise FailedDependency("Failed dependency for %s:%s peer %s:%s failed %s:%s" % (obj.__class__.__name__, str(obj.pk\
65), peer_object.__class__.__name__, str(peer_object.pk), failed.__class__.__name__, str(failed.pk)))
Sapan Bhatia24836f12013-08-27 10:16:05 -040066
Sapan Bhatia60823362014-04-30 00:52:32 -040067 def call(self, failed=[], deletion=False):
68 pending = self.fetch_pending(deletion)
Tony Mackce79de02013-09-24 10:12:33 -040069 for o in pending:
Tony Mack68e818d2013-09-25 13:34:17 -040070 try:
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040071 for f in failed:
Andy Baviere7abb622013-10-18 15:11:56 -040072 self.check_dependencies(o,f) # Raises exception if failed
Sapan Bhatia60823362014-04-30 00:52:32 -040073 if (deletion):
74 self.delete_record(o)
75 o.delete(purge=True)
76 else:
77 self.sync_record(o)
78 o.enacted = datetime.now() # Is this the same timezone? XXX
79 o.backend_status = "OK"
80 o.save(update_fields=['enacted'])
Sapan Bhatiaeba08432014-04-28 23:58:36 -040081 except Exception,e:
82 try:
83 o.backend_status = self.error_map.map(str(e))
84 except:
85 o.backend_status = str(e)
86
87 o.save(update_fields=['backend_status'])
88
89 logger.log_exc("sync step failed!")
Tony Mack68e818d2013-09-25 13:34:17 -040090 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040091
Tony Mackce79de02013-09-24 10:12:33 -040092 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -040093
Tony Mack16f04742013-09-25 08:53:28 -040094 def __call__(self, **args):
95 return self.call(**args)