示例#1
0
 def setup(self):
     os.environ = {
         'SERF_SELF_NAME': 'null',
         'SERF_SELF_ROLE': 'web',
         'SERF_EVENT': 'member-join',
     }
     self.handler = SerfHandlerProxy()
     self.sample = SerfHandler()
     self.sample.member_join = MagicMock(return_value=True)
示例#2
0
 def setup(self):
     os.environ = {
         'SERF_SELF_NAME': 'null',
         'SERF_SELF_ROLE': 'null',
         'SERF_EVENT': 'member-join',
     }
     self.handler = SerfHandlerProxy()
     self.handler.log = MagicMock(return_value=True)
     assert len(self.handler.handlers) == 0
示例#3
0
 def test_method_implemented(self):
     os.environ = {
         'SERF_SELF_NAME': 'null',
         'SERF_SELF_ROLE': 'null',
         'SERF_EVENT': 'user',
         'SERF_USER_EVENT': 'implemented',
     }
     handler = SerfHandlerProxy()
     sample = SerfHandler()
     sample.implemented = MagicMock(return_value=True)
     assert len(handler.handlers) == 0
     handler.register('default', sample)
     assert len(handler.handlers) == 1
     handler.run()
     sample.implemented.assert_called_with()
示例#4
0
 def setup(self):
     os.environ = {
         'SERF_SELF_NAME': 'null',
         'SERF_SELF_ROLE': 'null',
         'SERF_EVENT': 'member-join',
     }
     self.handler = SerfHandlerProxy()
     self.handler.log = MagicMock(return_value=True)
     assert len(self.handler.handlers) == 0
示例#5
0
 def setup(self):
     os.environ = {
         'SERF_SELF_NAME': 'null',
         'SERF_SELF_ROLE': 'web',
         'SERF_EVENT': 'member-join',
     }
     self.handler = SerfHandlerProxy()
     self.sample = SerfHandler()
     self.sample.member_join = MagicMock(return_value=True)
示例#6
0
 def test_method_implemented(self):
     os.environ = {
         'SERF_SELF_NAME': 'null',
         'SERF_SELF_ROLE': 'null',
         'SERF_EVENT': 'user',
         'SERF_USER_EVENT': 'implemented',
     }
     handler = SerfHandlerProxy()
     sample = SerfHandler()
     sample.implemented = MagicMock(return_value=True)
     assert len(handler.handlers) == 0
     handler.register('default', sample)
     assert len(handler.handlers) == 1
     handler.run()
     sample.implemented.assert_called_with()
示例#7
0
class TestSerfHandlerProxyStandardEvent:
    def setup(self):
        os.environ = {
            'SERF_SELF_NAME': 'null',
            'SERF_SELF_ROLE': 'web',
            'SERF_EVENT': 'member-join',
        }
        self.handler = SerfHandlerProxy()
        self.sample = SerfHandler()
        self.sample.member_join = MagicMock(return_value=True)

    def test_standard_events_called(self):
        self.handler.register('default', self.sample)
        assert len(self.handler.handlers) == 1
        assert 'default' in self.handler.handlers
        self.handler.run()
        self.sample.member_join.assert_called_with()

    def test_role_registration(self):
        self.handler.register('web', self.sample)
        assert len(self.handler.handlers) == 1
        assert 'web' in self.handler.handlers
        self.handler.run()
        self.sample.member_join.assert_called_with()
示例#8
0
class TestSerfHandlerProxyStandardEvent:
    def setup(self):
        os.environ = {
            'SERF_SELF_NAME': 'null',
            'SERF_SELF_ROLE': 'web',
            'SERF_EVENT': 'member-join',
        }
        self.handler = SerfHandlerProxy()
        self.sample = SerfHandler()
        self.sample.member_join = MagicMock(return_value=True)

    def test_standard_events_called(self):
        self.handler.register('default', self.sample)
        assert len(self.handler.handlers) == 1
        assert 'default' in self.handler.handlers
        self.handler.run()
        self.sample.member_join.assert_called_with()

    def test_role_registration(self):
        self.handler.register('web', self.sample)
        assert len(self.handler.handlers) == 1
        assert 'web' in self.handler.handlers
        self.handler.run()
        self.sample.member_join.assert_called_with()
示例#9
0
class TestSerfHandlerNegativeCases:
    def setup(self):
        os.environ = {
            'SERF_SELF_NAME': 'null',
            'SERF_SELF_ROLE': 'null',
            'SERF_EVENT': 'member-join',
        }
        self.handler = SerfHandlerProxy()
        self.handler.log = MagicMock(return_value=True)
        assert len(self.handler.handlers) == 0

    def test_no_handler(self):
        self.handler.run()
        self.handler.log.assert_called_with("no handler for role")

    def test_no_method_implemented(self):
        self.handler.register('default', SerfHandler())
        assert len(self.handler.handlers) == 1
        self.handler.run()
        self.handler.log.assert_called_with("event not implemented by class")
示例#10
0
class TestSerfHandlerNegativeCases:

    def setup(self):
        os.environ = {
            'SERF_SELF_NAME': 'null',
            'SERF_SELF_ROLE': 'null',
            'SERF_EVENT': 'member-join',
        }
        self.handler = SerfHandlerProxy()
        self.handler.log = MagicMock(return_value=True)
        assert len(self.handler.handlers) == 0

    def test_no_handler(self):
        self.handler.run()
        self.handler.log.assert_called_with("no handler for role")

    def test_no_method_implemented(self):
        self.handler.register('default', SerfHandler())
        assert len(self.handler.handlers) == 1
        self.handler.run()
        self.handler.log.assert_called_with("event not implemented by class")
示例#11
0
#!/usr/bin/env python
import os
import time
import multiprocessing

from serf_master import SerfHandlerProxy
from base_handler import BaseHandler
import config
try:
    from my_handler import MyHandler
except ImportError:
    MyHandler = BaseHandler

if __name__ == '__main__':
    handler = SerfHandlerProxy()
    role = config.role
    handler.register(role, MyHandler())
    handler.run()
示例#12
0
#!/usr/bin/env python
import os
import time
import multiprocessing

from serf_master import SerfHandlerProxy
from base_handler import BaseHandler
import config
try:
    from my_handler import MyHandler
except ImportError:
    MyHandler = BaseHandler


if __name__ == '__main__':
    handler = SerfHandlerProxy()
    role = config.role
    handler.register(role, MyHandler())
    handler.run()
示例#13
0
            try:
                msg_dict = json.loads(payload)

                # ignore self serf event
                serf_node_name = base64.b64decode(msg_dict[serf_msg_serf])
                if serf_node_name == get_serf_node():
                    f.write('self vnodeinfo event detected, ignore...\n')
                    return

                f.write('vnodeinfo event detected...\n')

                self._send_vnodeinfo(msg_dict, rpc_addr)

            except Exception:
                f.write(traceback.format_exc())

    def member_join(self):
        with open(self.logfile, 'a+') as f:
            try:
                payload = read_from_stdin()
                f.write("member join detected, payload: {}\n".format(payload))
            except Exception:
                f.write(traceback.format_exc())
        # maybe rebalance the load balancer


if __name__ == '__main__':
    handler = SerfHandlerProxy()
    handler.register('default', DefaultHandler())
    handler.run()
示例#14
0
文件: handler.py 项目: shinofara/serf
        for line in iter(sys.stdin.readline, ""):
            print line
            agent = line.split("\t")
            node = agent[0] #node
            ip = agent[1] #node
            role = agent[2] #node

            if role == "app":
                ips.append(ip)

        db["ips"] = json.dumps(ips)
        db.sync()

        html = tpl.render({'backends':ips})

        f = open("/etc/nginx/conf.d/proxy.conf", "w")
        f.write(html)
        f.close()
        
        cmd = 'supervisorctl restart nginx'
        ret  =  subprocess.check_output( cmd.split(" ") )
        print ret

if __name__ == '__main__':
    handler = SerfHandlerProxy()

    # 存在するappとdefaultのみ許可される
    handler.register('default', AppHandler())
    handler.run()
示例#15
0
from serfclient.client import SerfClient
from serf_master import SerfHandler, SerfHandlerProxy

logging.basicConfig(filename='/var/log/serf-test.log', level=logging.DEBUG)

class BaseHandler(SerfHandler):
	def info(self):
		logging.debug("info called")
		client = SerfClient()
		client.event('collect', self.name)	

class LbHandler(BaseHandler):
	def collect(self):
		command_client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
		command_client.connect("/tmp/serf.sock")
		payload = "".join(fileinput.input())
		logging.debug("collect called")
		logging.info(payload)
        	command_client.send(payload)

class WebHandler(BaseHandler):
	pass


if __name__ == '__main__':
	handler = SerfHandlerProxy()
	handler.register('lb', LbHandler())
	handler.register('web', WebHandler())
	handler.register('default', WebHandler())
	handler.run()