notebook/IPython/html/services/clusters/handlers.py

60 lines
1.7 KiB
Python
Raw Normal View History

"""Tornado handlers for cluster web service."""
2013-05-03 02:15:32 +08:00
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
2013-05-03 02:15:32 +08:00
import json
2013-05-03 02:15:32 +08:00
from tornado import web
2013-05-17 06:52:27 +08:00
from ...base.handlers import IPythonHandler
2013-05-03 02:15:32 +08:00
#-----------------------------------------------------------------------------
# Cluster handlers
#-----------------------------------------------------------------------------
class MainClusterHandler(IPythonHandler):
@web.authenticated
def get(self):
self.finish(json.dumps(self.cluster_manager.list_profiles()))
2013-05-03 02:15:32 +08:00
class ClusterProfileHandler(IPythonHandler):
@web.authenticated
def get(self, profile):
self.finish(json.dumps(self.cluster_manager.profile_info(profile)))
2013-05-03 02:15:32 +08:00
class ClusterActionHandler(IPythonHandler):
@web.authenticated
def post(self, profile, action):
cm = self.cluster_manager
if action == 'start':
2013-05-20 13:22:01 +08:00
n = self.get_argument('n', default=None)
if not n:
2013-05-03 02:15:32 +08:00
data = cm.start_cluster(profile)
else:
data = cm.start_cluster(profile, int(n))
if action == 'stop':
data = cm.stop_cluster(profile)
self.finish(json.dumps(data))
2013-05-16 01:55:43 +08:00
#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
_cluster_action_regex = r"(?P<action>start|stop)"
_profile_regex = r"(?P<profile>[^\/]+)" # there is almost no text that is invalid
default_handlers = [
(r"/clusters", MainClusterHandler),
(r"/clusters/%s/%s" % (_profile_regex, _cluster_action_regex), ClusterActionHandler),
(r"/clusters/%s" % _profile_regex, ClusterProfileHandler),
]