Skip to content

Commit

Permalink
[Tools] Add locate method
Browse files Browse the repository at this point in the history
cocaine-tool locate returns a description of a specified service
  • Loading branch information
noxiouz committed Mar 27, 2015
1 parent 4062e25 commit c586466
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
23 changes: 20 additions & 3 deletions cocaine/tools/actions/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ def execute(self):
raise NotImplementedError()


class Locate(object):
def __init__(self, locator, name):
self.locator = locator
self.name = name

@coroutine
def execute(self):
channel = yield self.locator.resolve(self.name)
endpoints, version, api = yield channel.rx.get()
result = {
"endpoints": ["%s:%d" % (addr, port) for addr, port in endpoints],
"version": version,
"api": dict((num, method[0]) for num, method in api.items())
}
raise gen.Return(result)


class NodeInfo(Node):
def __init__(self, node, locator, storage, name=None, expand=False):
super(NodeInfo, self).__init__(node)
Expand Down Expand Up @@ -114,21 +131,21 @@ def execute(self):
response['response'] = result
yield response

def getService(self):
def get_service(self):
try:
service = Service(self.serviceName, host=self.host, port=self.port)
return service
except Exception as err:
raise ServiceCallError(self.serviceName, err)

def getMethod(self, service):
def get_method(self, service):
try:
method = service.__getattribute__(self.methodName)
return method
except AttributeError:
raise ServiceError(self.serviceName, 'method "{0}" is not found'.format(self.methodName), 1)

def parseArguments(self):
def parse_arguments(self):
if not self.args:
return ()

Expand Down
2 changes: 1 addition & 1 deletion cocaine/tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _processResult(self, result):

NG_ACTIONS = {
'info': JsonToolHandler(common.NodeInfo),
'call': CallActionCli(common.Call),
'locate': JsonToolHandler(common.Locate),

'app:check': ToolHandler(app.Check),
'app:list': JsonToolHandler(app.List),
Expand Down
10 changes: 10 additions & 0 deletions cocaine/tools/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ class dispatcher:
# proxyDispatcher = Dispatcher()


@d.command(name='locate', usage='[--name=NAME]')
def locate(options,
name=('n', '', 'service name')):
"""Show information about requested service"""
options.executor.executeAction('locate', **{
'name': name,
'locator': options.locator,
})


@d.command(name='info', usage='[--name=NAME]')
def info(options,
name=('n', '', 'application name'),
Expand Down

0 comments on commit c586466

Please sign in to comment.