Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjustments for Python 3 #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions circusweb/circushttpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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})
Expand Down Expand Up @@ -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()),
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions circusweb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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))

Expand Down
19 changes: 12 additions & 7 deletions circusweb/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 + \
Expand All @@ -75,20 +77,23 @@ 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:
if watcher in p.watchersWithPids:
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'])
4 changes: 2 additions & 2 deletions circusweb/stats_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion circusweb/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()))});
});
</script>
2 changes: 1 addition & 1 deletion circusweb/templates/sockets.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
</script>