blob: c4215ac72a6d4e6972a5616e5d5cd759830c9f8b [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Tony Mack79a49c82013-06-15 23:51:57 -04004from core.models import *
smbakerd39abb62013-08-05 22:21:09 -07005#from openstack.manager import OpenStackManager
Sapan Bhatia66f4e612013-07-02 12:12:38 -04006from planetstack.config import Config
Sapan Bhatiadbaf1932013-09-03 11:28:52 -04007from observer.deleters import deleters
Tony Mack79a49c82013-06-15 23:51:57 -04008
Sapan Bhatia9182b322013-06-25 16:22:14 -04009import os
10import base64
Tony Mack5c0c4552013-07-03 09:36:51 -040011from fofum import Fofum
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040012import json
Sapan Bhatia9182b322013-06-25 16:22:14 -040013
Sapan Bhatiab9c59342013-09-03 10:21:13 -040014# decorator that marks dispatachable event methods
Tony Mack79a49c82013-06-15 23:51:57 -040015def event(func):
Sapan Bhatiab9c59342013-09-03 10:21:13 -040016 setattr(func, 'event', func.__name__)
17 return func
Tony Mack79a49c82013-06-15 23:51:57 -040018
19class EventHandler:
Sapan Bhatiab9c59342013-09-03 10:21:13 -040020 # This code is currently not in use.
21 def __init__(self):
22 pass #self.manager = OpenStackManager()
Tony Mack79a49c82013-06-15 23:51:57 -040023
Sapan Bhatiab9c59342013-09-03 10:21:13 -040024 @staticmethod
25 def get_events():
26 events = []
27 for name in dir(EventHandler):
28 attribute = getattr(EventHandler, name)
29 if hasattr(attribute, 'event'):
30 events.append(getattr(attribute, 'event'))
31 return events
Tony Mack79a49c82013-06-15 23:51:57 -040032
Sapan Bhatiab9c59342013-09-03 10:21:13 -040033 def dispatch(self, event, *args, **kwds):
34 if hasattr(self, event):
35 return getattr(self, event)(*args, **kwds)
36
37
Sapan Bhatia66f4e612013-07-02 12:12:38 -040038class EventSender:
Sapan Bhatiab9c59342013-09-03 10:21:13 -040039 def __init__(self,user=None,clientid=None):
40 try:
41 clid = Config().feefie_client_id
42 user = Config().feefie_client_user
43 except:
44 clid = 'planetstack_core_team'
45 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -040046
Sapan Bhatiab9c59342013-09-03 10:21:13 -040047 self.fofum = Fofum(user=user)
48 self.fofum.make(clid)
Sapan Bhatia66f4e612013-07-02 12:12:38 -040049
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040050 def fire(self,**args):
51 self.fofum.fire(json.dumps(args))
Tony Mack79a49c82013-06-15 23:51:57 -040052
53class EventListener:
Sapan Bhatiab9c59342013-09-03 10:21:13 -040054 def __init__(self,wake_up=None):
55 self.handler = EventHandler()
56 self.wake_up = wake_up
Tony Mack79a49c82013-06-15 23:51:57 -040057
Sapan Bhatiab9c59342013-09-03 10:21:13 -040058 def handle_event(self, payload):
59 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -040060
Sapan Bhatiab9c59342013-09-03 10:21:13 -040061 try:
62 deletion = payload_dict['deletion_flag']
63 if (deletion):
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040064 model = payload_dict['model']
65 pk = payload_dict['pk']
66
67 for deleter in deleters[model]:
68 deleter(pk)
Sapan Bhatiab9c59342013-09-03 10:21:13 -040069 except:
70 deletion = False
Sapan Bhatia66f4e612013-07-02 12:12:38 -040071
Sapan Bhatiab9c59342013-09-03 10:21:13 -040072 if (not deletion and self.wake_up):
73 self.wake_up()
74
Sapan Bhatia9182b322013-06-25 16:22:14 -040075
Sapan Bhatiab9c59342013-09-03 10:21:13 -040076 def run(self):
77 # This is our unique client id, to be used when firing and receiving events
78 # It needs to be generated once and placed in the config file
Sapan Bhatia66f4e612013-07-02 12:12:38 -040079
Sapan Bhatiab9c59342013-09-03 10:21:13 -040080 try:
81 clid = Config().feefie_client_id
82 user = Config().feefie_client_user
83 except:
84 clid = 'planetstack_core_team'
85 user = 'pl'
86
87 f = Fofum(user=user)
88
89 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
90 listener_thread.start()