blob: 72a740c91aee5b214a7aa69dfc01d2e6fdc8fea0 [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 *
Sapan Bhatiad9468eb2014-08-20 03:03:12 -04007from django.db.models import F, Q
Andy Baviere7abb622013-10-18 15:11:56 -04008
Andy Bavier04111b72013-10-22 16:47:10 -04009logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040010
Sapan Bhatia13c7f112013-09-02 14:19:35 -040011class FailedDependency(Exception):
Tony Mackce79de02013-09-24 10:12:33 -040012 pass
Sapan Bhatia13c7f112013-09-02 14:19:35 -040013
Tony Macke10fbe52015-01-03 19:37:39 -050014class SyncStep(object):
Tony Mackce79de02013-09-24 10:12:33 -040015 """ A PlanetStack Sync step.
Sapan Bhatia24836f12013-08-27 10:16:05 -040016
Tony Mackce79de02013-09-24 10:12:33 -040017 Attributes:
18 psmodel Model name the step synchronizes
19 dependencies list of names of models that must be synchronized first if the current model depends on them
20 """
21 slow=False
22 def get_prop(prop):
23 try:
24 sync_config_dir = Config().sync_config_dir
25 except:
26 sync_config_dir = '/etc/planetstack/sync'
27 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
28 return open(prop_config_path).read().rstrip()
Sapan Bhatia24836f12013-08-27 10:16:05 -040029
Tony Mackce79de02013-09-24 10:12:33 -040030 def __init__(self, **args):
31 """Initialize a sync step
32 Keyword arguments:
33 name -- Name of the step
34 provides -- PlanetStack models sync'd by this step
35 """
36 dependencies = []
Tony Mack387a73f2013-09-18 07:59:14 -040037 self.driver = args.get('driver')
Sapan Bhatiaeba08432014-04-28 23:58:36 -040038 self.error_map = args.get('error_map')
39
Tony Mackce79de02013-09-24 10:12:33 -040040 try:
41 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
42 except:
43 self.soft_deadline = 5 # 5 seconds
Sapan Bhatia24836f12013-08-27 10:16:05 -040044
Tony Mackce79de02013-09-24 10:12:33 -040045 return
Sapan Bhatia24836f12013-08-27 10:16:05 -040046
Sapan Bhatiae17bc5b2014-04-30 00:53:06 -040047 def fetch_pending(self, deletion=False):
Sapan Bhatia21765662014-07-23 08:59:30 -040048 # This is the most common implementation of fetch_pending
49 # Steps should override it if they have their own logic
50 # for figuring out what objects are outstanding.
51 main_obj = self.provides[0]
Sapan Bhatiad9468eb2014-08-20 03:03:12 -040052 if (not deletion):
Sapan Bhatia21765662014-07-23 08:59:30 -040053 objs = main_obj.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
54 else:
55 objs = main_obj.deleted_objects.all()
56
57 return objs
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040058 #return Sliver.objects.filter(ip=None)
Tony Mackce79de02013-09-24 10:12:33 -040059
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040060 def check_dependencies(self, obj, failed):
Tony Mackce79de02013-09-24 10:12:33 -040061 for dep in self.dependencies:
Scott Baker105b6b72014-05-12 10:40:25 -070062 peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower
Sapan Bhatiacfef6ef2014-08-20 03:04:03 -040063 try:
64 peer_object = getattr(obj, peer_name)
65 except:
66 peer_object = None
67
Scott Baker0652f4e2014-08-19 15:49:27 -070068 if (peer_object and peer_object.pk==failed.pk and type(peer_object)==type(failed)):
Sapan Bhatia7e482de2014-08-22 03:05:13 -040069 if (obj.backend_status!=peer_object.backend_status):
70 obj.backend_status = peer_object.backend_status
71 obj.save(update_fields=['backend_status'])
72 raise FailedDependency("Failed dependency for %s:%s peer %s:%s failed %s:%s" % (obj.__class__.__name__, str(obj.pk), peer_object.__class__.__name__, str(peer_object.pk), failed.__class__.__name__, str(failed.pk)))
Sapan Bhatia24836f12013-08-27 10:16:05 -040073
Sapan Bhatia60823362014-04-30 00:52:32 -040074 def call(self, failed=[], deletion=False):
75 pending = self.fetch_pending(deletion)
Tony Mackce79de02013-09-24 10:12:33 -040076 for o in pending:
Tony Mack68e818d2013-09-25 13:34:17 -040077 try:
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040078 for f in failed:
Andy Baviere7abb622013-10-18 15:11:56 -040079 self.check_dependencies(o,f) # Raises exception if failed
Sapan Bhatia60823362014-04-30 00:52:32 -040080 if (deletion):
81 self.delete_record(o)
82 o.delete(purge=True)
83 else:
84 self.sync_record(o)
85 o.enacted = datetime.now() # Is this the same timezone? XXX
86 o.backend_status = "OK"
87 o.save(update_fields=['enacted'])
Sapan Bhatiaeba08432014-04-28 23:58:36 -040088 except Exception,e:
Sapan Bhatia8fd852b2014-08-22 03:06:03 -040089 logger.log_exc("sync step failed!")
90 str_e = '%r'%e
Sapan Bhatiaeba08432014-04-28 23:58:36 -040091 try:
Sapan Bhatia8fd852b2014-08-22 03:06:03 -040092 o.backend_status = self.error_map.map(str_e)
Sapan Bhatiaeba08432014-04-28 23:58:36 -040093 except:
Sapan Bhatia8fd852b2014-08-22 03:06:03 -040094 o.backend_status = str_e
Sapan Bhatiaeba08432014-04-28 23:58:36 -040095
Sapan Bhatia9c308fc2014-08-22 03:07:59 -040096 # TOFIX:
97 # DatabaseError: value too long for type character varying(140)
Sapan Bhatiaf1d3d272014-08-18 02:24:22 -040098 if (o.pk):
Sapan Bhatia9c308fc2014-08-22 03:07:59 -040099 try:
100 o.save(update_fields=['backend_status'])
101 except:
102 print "Could not update backend status field!"
103 pass
Sapan Bhatiaeba08432014-04-28 23:58:36 -0400104
Tony Mack68e818d2013-09-25 13:34:17 -0400105 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -0400106
Tony Mackce79de02013-09-24 10:12:33 -0400107 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -0400108
Tony Mack16f04742013-09-25 08:53:28 -0400109 def __call__(self, **args):
110 return self.call(**args)