diff --git a/circusweb/circushttpd.py b/circusweb/circushttpd.py index 73d541a..086153b 100644 --- a/circusweb/circushttpd.py +++ b/circusweb/circushttpd.py @@ -13,6 +13,7 @@ from circusweb import logger, __version__ from circus.exc import CallError from circus.util import LOG_LEVELS, configure_logger +from circus.py3compat import b from zmq.eventloop import ioloop from circusweb.util import AutoDiscovery, run_command from circusweb.session import (SessionManager, get_controller, @@ -57,6 +58,14 @@ } +def encode(s): + return b64encode(s.encode('utf-8')).decode('utf-8') + + +def decode(s): + return b64decode(s.encode('utf-8')).decode('utf-8') + + def require_logged_user(func): @wraps(func) @@ -77,7 +86,7 @@ class BaseHandler(tornado.web.RequestHandler): def prepare(self): session_id = self.get_secure_cookie('session_id') if not session_id or not SessionManager.get(session_id): - session_id = uuid4().hex + session_id = b(uuid4().hex) session = SessionManager.new(session_id) self.set_secure_cookie('session_id', session_id) else: @@ -95,7 +104,7 @@ def render_template(self, template_path, **data): server = '%s://%s/' % (self.request.protocol, self.request.host) namespace.update({'controller': get_controller(), 'version': __version__, - 'b64encode': b64encode, + 'b64encode': encode, 'dumps': json.dumps, 'session': self.session, 'messages': messages, 'SERVER': server}) @@ -206,7 +215,7 @@ def post(self, endpoint): 'add_watcher', kwargs=dict((k, v[0]) for k, v in self.request.arguments.iteritems()), - message='added a new watcher', endpoint=b64decode(endpoint), + message='added a new watcher', endpoint=decode(endpoint), redirect_url=self.reverse_url('watcher', endpoint, self.get_argument('name').lower()), @@ -221,7 +230,7 @@ class WatcherHandler(BaseHandler): @gen.coroutine def get(self, endpoint, name): controller = get_controller() - endpoint = b64decode(endpoint) + endpoint = decode(endpoint) pids = yield gen.Task(controller.get_pids, name, endpoint) self.finish(self.render_template('watcher.html', pids=pids, name=name, endpoint=endpoint)) @@ -235,7 +244,7 @@ class WatcherSwitchStatusHandler(BaseHandler): def get(self, endpoint, name): url = yield self.run_command(command='switch_status', message='status switched', - endpoint=b64decode(endpoint), + endpoint=decode(endpoint), args=(name,), redirect_url=self.reverse_url('index')) self.redirect(url) @@ -251,7 +260,7 @@ def get(self, endpoint, name, pid): url = yield self.run_command( command='killproc', message=msg.format(pid=pid), - endpoint=b64decode(endpoint), + endpoint=decode(endpoint), args=(name, pid), redirect_url=self.reverse_url('watcher', endpoint, name)) self.redirect(url) @@ -266,7 +275,7 @@ def get(self, endpoint, name): msg = 'removed one process from the {watcher} pool' url = yield self.run_command(command='decrproc', message=msg.format(watcher=name), - endpoint=b64decode(endpoint), + endpoint=decode(endpoint), args=(name,), redirect_url=self.reverse_url('watcher', endpoint, @@ -283,7 +292,7 @@ def get(self, endpoint, name): msg = 'added one process to the {watcher} pool' url = yield self.run_command(command='incrproc', message=msg.format(watcher=name), - endpoint=b64decode(endpoint), + endpoint=decode(endpoint), args=(name,), redirect_url=self.reverse_url('watcher', endpoint, @@ -301,7 +310,7 @@ def get(self, endpoint=None): sockets = {} if endpoint: - endpoint = b64decode(endpoint) + endpoint = decode(endpoint) sockets[endpoint] = yield gen.Task(controller.get_sockets, endpoint=endpoint) else: @@ -325,7 +334,7 @@ class ReloadconfigHandler(BaseHandler): def get(self, endpoint): url = yield self.run_command(command='reloadconfig', message='reload the configuration', - endpoint=b64decode(endpoint), + endpoint=decode(endpoint), args=[], redirect_url=self.reverse_url('index')) self.redirect(url) diff --git a/circusweb/client.py b/circusweb/client.py index 28dc2b8..5896627 100644 --- a/circusweb/client.py +++ b/circusweb/client.py @@ -6,7 +6,7 @@ from zmq.eventloop.zmqstream import ZMQStream from circus.exc import CallError -from circus.py3compat import string_types +from circus.py3compat import string_types, b from circus.util import get_connection from circus.client import CircusClient, make_message @@ -59,7 +59,7 @@ def call(self, cmd, callback): raise CallError(str(e)) socket = self.context.socket(zmq.DEALER) - socket.setsockopt(zmq.IDENTITY, uuid.uuid4().hex) + socket.setsockopt(zmq.IDENTITY, b(uuid.uuid4().hex)) socket.setsockopt(zmq.LINGER, 0) get_connection(socket, self.endpoint, self.ssh_server, self.ssh_keyfile) @@ -84,7 +84,7 @@ def recv_callback(msg): stream.on_recv(recv_callback) try: - socket.send(cmd) + socket.send(b(cmd)) except zmq.ZMQError as e: raise CallError(str(e)) diff --git a/circusweb/namespace.py b/circusweb/namespace.py index 2f3bcb5..fb988eb 100644 --- a/circusweb/namespace.py +++ b/circusweb/namespace.py @@ -28,7 +28,7 @@ def get_stats(self, watchers=[], watchersWithPids=[], for watcher_tuple in watchersWithPids: watcher, encoded_endpoint = watcher_tuple - endpoint = b64decode(encoded_endpoint) + endpoint = b64decode(encoded_endpoint).decode("utf-8") if watcher == "sockets": sockets = yield gen.Task(controller.get_sockets, endpoint=endpoint) @@ -54,18 +54,20 @@ def get_stats(self, watchers=[], watchersWithPids=[], @classmethod def consume_stats(cls, watcher, pid, stat, stat_endpoint): - stat_endpoint_b64 = b64encode(stat_endpoint) + stat_endpoint_b64 = b64encode(stat_endpoint.encode('utf-8')) for p in cls.participants[stat_endpoint]: if watcher == 'sockets': # if we get information about sockets and we explicitely # requested them, send back the information. if 'sockets' in p.watchersWithPids and 'fd' in stat: p.emit('socket-stats-{fd}-{endpoint}'.format( - fd=stat['fd'], endpoint=stat_endpoint_b64), + fd=stat['fd'], + endpoint=stat_endpoint_b64.decode('utf-8')), **stat) elif 'sockets' in p.watchers and 'addresses' in stat: p.emit('socket-stats-{endpoint}'.format( - endpoint=stat_endpoint_b64), reads=stat['reads'], + endpoint=stat_endpoint_b64.decode('utf-8')), + reads=stat['reads'], adresses=stat['addresses']) else: available_watchers = p.watchers + p.watchersWithPids + \ @@ -75,12 +77,14 @@ def consume_stats(cls, watcher, pid, stat, stat_endpoint): if (watcher == 'circus' and stat.get('name', None) in available_watchers): p.emit('stats-{watcher}-{endpoint}'.format( - watcher=stat['name'], endpoint=stat_endpoint_b64), + watcher=stat['name'], + endpoint=stat_endpoint_b64.decode('utf-8')), mem=stat['mem'], cpu=stat['cpu'], age=stat['age']) else: if pid is None: # means that it's the aggregation p.emit('stats-{watcher}-{endpoint}'.format( - watcher=watcher, endpoint=stat_endpoint_b64), + watcher=watcher, + endpoint=stat_endpoint_b64.decode('utf-8')), mem=stat['mem'], cpu=stat['cpu'], age=stat['age']) else: @@ -88,7 +92,8 @@ def consume_stats(cls, watcher, pid, stat, stat_endpoint): p.emit( 'stats-{watcher}-{pid}-{endpoint}'.format( watcher=watcher, pid=pid, - endpoint=stat_endpoint_b64), + endpoint=stat_endpoint_b64.decode( + 'utf-8')), mem=stat['mem'], cpu=stat['cpu'], age=stat['age']) diff --git a/circusweb/stats_client.py b/circusweb/stats_client.py index dda828c..bfdb785 100644 --- a/circusweb/stats_client.py +++ b/circusweb/stats_client.py @@ -17,7 +17,7 @@ def __init__(self, topics, loop, callback, context=None, self.pubsub_socket = self.context.socket(zmq.SUB) get_connection(self.pubsub_socket, self.endpoint, ssh_server) for topic in self.topics: - self.pubsub_socket.setsockopt(zmq.SUBSCRIBE, topic) + self.pubsub_socket.setsockopt(zmq.SUBSCRIBE, topic.encode('utf-8')) self.stream = ZMQStream(self.pubsub_socket, loop) self.stream.on_recv(self.process_message) self.callback = callback @@ -36,7 +36,7 @@ def __exit__(self, exc_type, exc_value, traceback): def process_message(self, msg): topic, stat = msg - topic = topic.split('.') + topic = topic.decode('utf-8').split('.') if len(topic) == 3: __, watcher, subtopic = topic self.callback(watcher, subtopic, json.loads(stat), self.endpoint) diff --git a/circusweb/templates/index.html b/circusweb/templates/index.html index 460499c..cb5f6a1 100644 --- a/circusweb/templates/index.html +++ b/circusweb/templates/index.html @@ -171,6 +171,6 @@ watchers.push(['${plugin}', ${dumps(b64encode(stat_endpoint))}]); %endfor % endfor - supervise(socket, watchers, undefined, ${dumps(list(endpoints.keys()))}, ${dumps(endpoints.values())}); + supervise(socket, watchers, undefined, ${dumps(list(endpoints.keys()))}, ${dumps(list(endpoints.values()))}); }); diff --git a/circusweb/templates/sockets.html b/circusweb/templates/sockets.html index 40ebdca..e13d2aa 100644 --- a/circusweb/templates/sockets.html +++ b/circusweb/templates/sockets.html @@ -48,6 +48,6 @@ watchers.push(['sockets', ${dumps(b64encode(controller.get_client(endpoint).stats_endpoint))}, ${dumps(b64encode(endpoint))}]); stats_endpoints.push(${dumps(controller.get_client(endpoint).stats_endpoint)}); % endfor - supervise(socket, [], watchers, ${dumps(sockets.keys())}, stats_endpoints); + supervise(socket, [], watchers, ${dumps(list(sockets.keys()))}, stats_endpoints); });