blob: bd04cedfe9269d72e244a8092e615e041665ad38 [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Sapan Bhatia66f4e612013-07-02 12:12:38 -04004from planetstack.config import Config
Scott Baker6ecd4262014-01-21 23:15:21 -08005from observer.deleter import Deleter
Tony Mack79a49c82013-06-15 23:51:57 -04006
Scott Bakerc1c45f82014-01-21 16:23:51 -08007import uuid
Sapan Bhatia9182b322013-06-25 16:22:14 -04008import os
Scott Baker6ecd4262014-01-21 23:15:21 -08009import imp
10import inspect
Sapan Bhatia9182b322013-06-25 16:22:14 -040011import base64
Tony Mack5c0c4552013-07-03 09:36:51 -040012from fofum import Fofum
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040013import json
Scott Baker6ecd4262014-01-21 23:15:21 -080014import traceback
Sapan Bhatia9182b322013-06-25 16:22:14 -040015
Scott Baker3d5a2782014-05-13 09:58:02 -070016random_client_id=None
17def get_random_client_id():
18 global random_client_id
19
20 if (random_client_id is None) and os.path.exists("/opt/planetstack/random_client_id"):
21 # try to use the last one we used, if we saved it
22 try:
23 random_client_id = open("/opt/planetstack/random_client_id","r").readline().strip()
24 print "get_random_client_id: loaded %s" % random_client_id
25 except:
26 print "get_random_client_id: failed to read /opt/planetstack/random_client_id"
27
28 if random_client_id is None:
29 random_client_id = base64.urlsafe_b64encode(os.urandom(12))
30 print "get_random_client_id: generated new id %s" % random_client_id
31
32 # try to save it for later (XXX: could race with another client here)
33 try:
34 open("/opt/planetstack/random_client_id","w").write("%s\n" % random_client_id)
35 except:
36 print "get_random_client_id: failed to write /opt/planetstack/random_client_id"
37
38 return random_client_id
39
40# decorator that marks dispatachable event methods
Tony Mack79a49c82013-06-15 23:51:57 -040041def event(func):
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040042 setattr(func, 'event', func.__name__)
Scott Baker3d5a2782014-05-13 09:58:02 -070043 return func
Tony Mack79a49c82013-06-15 23:51:57 -040044
45class EventHandler:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040046 # This code is currently not in use.
47 def __init__(self):
Scott Baker3d5a2782014-05-13 09:58:02 -070048 pass
Tony Mack79a49c82013-06-15 23:51:57 -040049
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040050 @staticmethod
51 def get_events():
52 events = []
53 for name in dir(EventHandler):
54 attribute = getattr(EventHandler, name)
55 if hasattr(attribute, 'event'):
56 events.append(getattr(attribute, 'event'))
57 return events
Tony Mack79a49c82013-06-15 23:51:57 -040058
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040059 def dispatch(self, event, *args, **kwds):
60 if hasattr(self, event):
61 return getattr(self, event)(*args, **kwds)
62
Scott Baker3d5a2782014-05-13 09:58:02 -070063
Sapan Bhatia66f4e612013-07-02 12:12:38 -040064class EventSender:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040065 def __init__(self,user=None,clientid=None):
66 try:
67 user = Config().feefie_client_user
68 except:
69 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -040070
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040071 try:
72 clid = Config().feefie_client_id
73 except:
Scott Baker3d5a2782014-05-13 09:58:02 -070074 clid = get_random_client_id()
75 print "EventSender: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia66f4e612013-07-02 12:12:38 -040076
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040077 self.fofum = Fofum(user=user)
78 self.fofum.make(clid)
79
Scott Bakerc1c45f82014-01-21 16:23:51 -080080 def fire(self,**kwargs):
81 kwargs["uuid"] = str(uuid.uuid1())
Scott Bakerc1c45f82014-01-21 16:23:51 -080082 self.fofum.fire(json.dumps(kwargs))
Tony Mack79a49c82013-06-15 23:51:57 -040083
84class EventListener:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040085 def __init__(self,wake_up=None):
86 self.handler = EventHandler()
87 self.wake_up = wake_up
Scott Baker6ecd4262014-01-21 23:15:21 -080088 self.deleters = {}
89 self.load_deleter_modules()
90
91 def load_deleter_modules(self, deleter_dir=None):
92 if deleter_dir is None:
93 if hasattr(Config(), "observer_deleters_dir"):
94 deleter_dir = Config().observer_deleters_dir
95 else:
96 deleter_dir = "/opt/planetstack/observer/deleters"
97
98 for fn in os.listdir(deleter_dir):
99 pathname = os.path.join(deleter_dir,fn)
100 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
101 module = imp.load_source(fn[:-3],pathname)
102 for classname in dir(module):
103 c = getattr(module, classname, None)
104
105 # make sure 'c' is a descendent of Deleter and has a
106 # provides field (this eliminates the abstract base classes
107 # since they don't have a provides)
108
109 if inspect.isclass(c) and issubclass(c, Deleter) and hasattr(c,"model") and c.model!=None:
110 modelName = c.model
111 if not modelName in self.deleters:
112 self.deleters[modelName] = []
113 if not (c in self.deleters[modelName]):
114 self.deleters[modelName].append(c)
115 print 'loaded deleters: %s' % ",".join(self.deleters.keys())
116
Tony Mack79a49c82013-06-15 23:51:57 -0400117
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400118 def handle_event(self, payload):
119 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400120
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400121 try:
Scott Bakerc1c45f82014-01-21 16:23:51 -0800122 deletion = payload_dict.get('delete_flag', False)
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400123 if (deletion):
124 model = payload_dict['model']
125 pk = payload_dict['pk']
Scott Baker6ecd4262014-01-21 23:15:21 -0800126 model_dict = payload_dict['model_dict']
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400127
Scott Baker6ecd4262014-01-21 23:15:21 -0800128 for deleter in self.deleters[model]:
129 try:
130 deleter()(pk, model_dict)
131 except:
132 # something is silently eating these
133 # exceptions...
134 traceback.print_exc()
135 raise
Scott Bakerc1c45f82014-01-21 16:23:51 -0800136
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400137 except:
138 deletion = False
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400139
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400140 if (not deletion and self.wake_up):
141 self.wake_up()
Scott Bakerc1c45f82014-01-21 16:23:51 -0800142
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400143 def run(self):
144 # This is our unique client id, to be used when firing and receiving events
145 # It needs to be generated once and placed in the config file
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400146
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400147 try:
148 user = Config().feefie_client_user
149 except:
150 user = 'pl'
Sapan Bhatiab9c59342013-09-03 10:21:13 -0400151
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400152 try:
153 clid = Config().feefie_client_id
154 except:
Scott Baker3d5a2782014-05-13 09:58:02 -0700155 clid = get_random_client_id()
156 print "EventListener: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia9faf7b02013-10-09 10:27:14 -0400157
158 f = Fofum(user=user)
159
160 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
161 listener_thread.start()