blob: 4116d6564d4ee91ff85cd24ade61d631638a82de [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
5
Sapan Bhatia13c7f112013-09-02 14:19:35 -04006class FailedDependency(Exception):
Tony Mackce79de02013-09-24 10:12:33 -04007 pass
Sapan Bhatia13c7f112013-09-02 14:19:35 -04008
Sapan Bhatia24836f12013-08-27 10:16:05 -04009class SyncStep:
Tony Mackce79de02013-09-24 10:12:33 -040010 """ A PlanetStack Sync step.
Sapan Bhatia24836f12013-08-27 10:16:05 -040011
Tony Mackce79de02013-09-24 10:12:33 -040012 Attributes:
13 psmodel Model name the step synchronizes
14 dependencies list of names of models that must be synchronized first if the current model depends on them
15 """
16 slow=False
17 def get_prop(prop):
18 try:
19 sync_config_dir = Config().sync_config_dir
20 except:
21 sync_config_dir = '/etc/planetstack/sync'
22 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
23 return open(prop_config_path).read().rstrip()
Sapan Bhatia24836f12013-08-27 10:16:05 -040024
Tony Mackce79de02013-09-24 10:12:33 -040025 def __init__(self, **args):
26 """Initialize a sync step
27 Keyword arguments:
28 name -- Name of the step
29 provides -- PlanetStack models sync'd by this step
30 """
31 dependencies = []
Tony Mack387a73f2013-09-18 07:59:14 -040032 self.driver = args.get('driver')
Tony Mackce79de02013-09-24 10:12:33 -040033 try:
34 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
35 except:
36 self.soft_deadline = 5 # 5 seconds
Sapan Bhatia24836f12013-08-27 10:16:05 -040037
Tony Mackce79de02013-09-24 10:12:33 -040038 return
Sapan Bhatia24836f12013-08-27 10:16:05 -040039
Tony Mackce79de02013-09-24 10:12:33 -040040 def fetch_pending(self):
Tony Mack66646d52013-09-24 21:47:12 -040041 return []
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040042 #return Sliver.objects.filter(ip=None)
Tony Mackce79de02013-09-24 10:12:33 -040043
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040044 def check_dependencies(self, obj, failed):
Tony Mackce79de02013-09-24 10:12:33 -040045 for dep in self.dependencies:
Tony Mack4fa85fb2013-09-25 14:39:57 -040046 peer_object = getattr(obj, dep.lower())
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040047 if (peer_object.pk==failed.pk):
Tony Mackce79de02013-09-24 10:12:33 -040048 raise DependencyFailed
Sapan Bhatia24836f12013-08-27 10:16:05 -040049
Tony Mackce79de02013-09-24 10:12:33 -040050 def call(self, failed=[]):
51 pending = self.fetch_pending()
52 for o in pending:
Tony Mack68e818d2013-09-25 13:34:17 -040053 try:
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040054 for f in failed:
55 self.check_dependencies(o,f) # Raises exception if failed
Tony Mack68e818d2013-09-25 13:34:17 -040056 self.sync_record(o)
57 o.enacted = datetime.now() # Is this the same timezone? XXX
58 o.save(update_fields=['enacted'])
59 except:
60 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040061
Tony Mackce79de02013-09-24 10:12:33 -040062 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -040063
Tony Mack16f04742013-09-25 08:53:28 -040064 def __call__(self, **args):
65 return self.call(**args)