blob: fb8c77afd5ccb092fecdfe79389c1144fee92392 [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:
Tony Mack4fa85fb2013-09-25 14:39:57 -040049 peer_object = getattr(obj, dep.lower())
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040050 if (peer_object.pk==failed.pk):
Tony Mack37146d22013-10-21 11:02:51 -040051 raise FailedDependency
Sapan Bhatia24836f12013-08-27 10:16:05 -040052
Tony Mackce79de02013-09-24 10:12:33 -040053 def call(self, failed=[]):
54 pending = self.fetch_pending()
55 for o in pending:
Tony Mack68e818d2013-09-25 13:34:17 -040056 try:
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040057 for f in failed:
Andy Baviere7abb622013-10-18 15:11:56 -040058 self.check_dependencies(o,f) # Raises exception if failed
Tony Mack68e818d2013-09-25 13:34:17 -040059 self.sync_record(o)
60 o.enacted = datetime.now() # Is this the same timezone? XXX
61 o.save(update_fields=['enacted'])
62 except:
Andy Baviere7abb622013-10-18 15:11:56 -040063 logger.log_exc("sync step failed!")
Tony Mack68e818d2013-09-25 13:34:17 -040064 failed.append(o)
Sapan Bhatiaca2e21f2013-10-02 01:10:02 -040065
Tony Mackce79de02013-09-24 10:12:33 -040066 return failed
Sapan Bhatia24836f12013-08-27 10:16:05 -040067
Tony Mack16f04742013-09-25 08:53:28 -040068 def __call__(self, **args):
69 return self.call(**args)