blob: fc0cb0b6cd21be3f3c0ce3455fadcd827191c7a0 [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):
Tony Mack66646d52013-09-24 21:47:12 -040047 return []
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040048 #return Sliver.objects.filter(ip=None)
Tony Mackce79de02013-09-24 10:12:33 -040049
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040050 def check_dependencies(self, obj, failed):
Tony Mackce79de02013-09-24 10:12:33 -040051 for dep in self.dependencies:
Scott Baker105b6b72014-05-12 10:40:25 -070052 peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower
53 peer_object = getattr(obj, peer_name)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040054 if (peer_object.pk==failed.pk):
Tony Mack37146d22013-10-21 11:02:51 -040055 raise FailedDependency
Sapan Bhatia24836f12013-08-27 10:16:05 -040056
Sapan Bhatia60823362014-04-30 00:52:32 -040057 def call(self, failed=[], deletion=False):
58 pending = self.fetch_pending(deletion)
Tony Mackce79de02013-09-24 10:12:33 -040059 for o in pending:
Tony Mack68e818d2013-09-25 13:34:17 -040060 try:
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040061 for f in failed:
Andy Baviere7abb622013-10-18 15:11:56 -040062 self.check_dependencies(o,f) # Raises exception if failed
Sapan Bhatia60823362014-04-30 00:52:32 -040063 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 Bhatiaeba08432014-04-28 23:58:36 -040071 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 Mack68e818d2013-09-25 13:34:17 -040080 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040081
Tony Mackce79de02013-09-24 10:12:33 -040082 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -040083
Tony Mack16f04742013-09-25 08:53:28 -040084 def __call__(self, **args):
85 return self.call(**args)