blob: c41628a91dcc452f43a6753c93a561d4e6775e29 [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
6
Andy Bavier04111b72013-10-22 16:47:10 -04007logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -04008
Sapan Bhatia13c7f112013-09-02 14:19:35 -04009class FailedDependency(Exception):
Tony Mackce79de02013-09-24 10:12:33 -040010 pass
Sapan Bhatia13c7f112013-09-02 14:19:35 -040011
Sapan Bhatia24836f12013-08-27 10:16:05 -040012class SyncStep:
Tony Mackce79de02013-09-24 10:12:33 -040013 """ A PlanetStack Sync step.
Sapan Bhatia24836f12013-08-27 10:16:05 -040014
Tony Mackce79de02013-09-24 10:12:33 -040015 Attributes:
16 psmodel Model name the step synchronizes
17 dependencies list of names of models that must be synchronized first if the current model depends on them
18 """
19 slow=False
20 def get_prop(prop):
21 try:
22 sync_config_dir = Config().sync_config_dir
23 except:
24 sync_config_dir = '/etc/planetstack/sync'
25 prop_config_path = '/'.join(sync_config_dir,self.name,prop)
26 return open(prop_config_path).read().rstrip()
Sapan Bhatia24836f12013-08-27 10:16:05 -040027
Tony Mackce79de02013-09-24 10:12:33 -040028 def __init__(self, **args):
29 """Initialize a sync step
30 Keyword arguments:
31 name -- Name of the step
32 provides -- PlanetStack models sync'd by this step
33 """
34 dependencies = []
Tony Mack387a73f2013-09-18 07:59:14 -040035 self.driver = args.get('driver')
Tony Mackce79de02013-09-24 10:12:33 -040036 try:
37 self.soft_deadline = int(self.get_prop('soft_deadline_seconds'))
38 except:
39 self.soft_deadline = 5 # 5 seconds
Sapan Bhatia24836f12013-08-27 10:16:05 -040040
Tony Mackce79de02013-09-24 10:12:33 -040041 return
Sapan Bhatia24836f12013-08-27 10:16:05 -040042
Tony Mackce79de02013-09-24 10:12:33 -040043 def fetch_pending(self):
Tony Mack66646d52013-09-24 21:47:12 -040044 return []
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040045 #return Sliver.objects.filter(ip=None)
Tony Mackce79de02013-09-24 10:12:33 -040046
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040047 def check_dependencies(self, obj, failed):
Tony Mackce79de02013-09-24 10:12:33 -040048 for dep in self.dependencies:
Scott Baker105b6b72014-05-12 10:40:25 -070049 peer_name = dep[0].lower() + dep[1:] # django names are camelCased with the first letter lower
50 peer_object = getattr(obj, peer_name)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040051 if (peer_object.pk==failed.pk):
Tony Mack37146d22013-10-21 11:02:51 -040052 raise FailedDependency
Sapan Bhatia24836f12013-08-27 10:16:05 -040053
Tony Mackce79de02013-09-24 10:12:33 -040054 def call(self, failed=[]):
55 pending = self.fetch_pending()
56 for o in pending:
Tony Mack68e818d2013-09-25 13:34:17 -040057 try:
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040058 for f in failed:
Andy Baviere7abb622013-10-18 15:11:56 -040059 self.check_dependencies(o,f) # Raises exception if failed
Tony Mack68e818d2013-09-25 13:34:17 -040060 self.sync_record(o)
61 o.enacted = datetime.now() # Is this the same timezone? XXX
62 o.save(update_fields=['enacted'])
63 except:
Scott Baker105b6b72014-05-12 10:40:25 -070064 logger.log_exc("sync step %s failed!" % self.__name__)
Tony Mack68e818d2013-09-25 13:34:17 -040065 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040066
Tony Mackce79de02013-09-24 10:12:33 -040067 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -040068
Tony Mack16f04742013-09-25 08:53:28 -040069 def __call__(self, **args):
70 return self.call(**args)