Skip to content

Commit

Permalink
mgr/dashboard: Takeover PR #20799 and adapt comments.
Browse files Browse the repository at this point in the history
Signed-off-by: Volker Theile <vtheile@suse.com>
  • Loading branch information
votdev committed Apr 11, 2018
1 parent b99b29b commit aac311d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 31 deletions.
23 changes: 6 additions & 17 deletions src/pybind/mgr/dashboard/controllers/rgw.py
Expand Up @@ -2,12 +2,11 @@
from __future__ import absolute_import

import json

import cherrypy

from . import ApiController, BaseController, RESTController, AuthRequired
from .. import logger
from ..services.ceph_service import CephService
from ..tools import ApiController, RESTController, AuthRequired
from ..services.rgw_client import RgwClient
from ..rest_client import RequestException
from ..exceptions import NoCredentialsException
Expand All @@ -22,20 +21,12 @@ class Rgw(RESTController):
@ApiController('rgw/daemon')
@AuthRequired()
class RgwDaemon(RESTController):

def list(self):
daemons = []
for hostname, server in CephService.get_service_map('rgw').items():
for service in server['services']:
metadata = service['metadata']
status = service['status']
if 'json' in status:
try:
status = json.loads(status['json'])
except ValueError:
logger.warning("%s had invalid status json", service['id'])
status = {}
else:
logger.warning('%s has no key "json" in status', service['id'])

# extract per-daemon service data and health
daemon = {
Expand Down Expand Up @@ -71,25 +62,23 @@ def get(self, svc_id):

daemon['rgw_metadata'] = metadata
daemon['rgw_status'] = status

return daemon


@ApiController('rgw/proxy')
@ApiController('rgw/proxy/{path:.*}')
@AuthRequired()
class RgwProxy(RESTController):
class RgwProxy(BaseController):
@cherrypy.expose
def default(self, *vpath, **params):
def __call__(self, path, **params):
try:
rgw_client = RgwClient.admin_instance()

except NoCredentialsException as e:
cherrypy.response.headers['Content-Type'] = 'application/json'
cherrypy.response.status = 401
return json.dumps({'message': e.message})
return json.dumps({'message': str(e)}).encode('utf-8')

method = cherrypy.request.method
path = '/'.join(vpath)
data = None

if cherrypy.request.body.length:
Expand Down
3 changes: 1 addition & 2 deletions src/pybind/mgr/dashboard/rest_client.py
Expand Up @@ -15,7 +15,6 @@

from .tools import build_url
import inspect
import itertools
import re
import requests
from requests.exceptions import ConnectionError, InvalidURL
Expand Down Expand Up @@ -474,7 +473,7 @@ def func_wrapper(self, *args, **kwargs):
method = api_kwargs.get('method', None)
resp_structure = api_kwargs.get('resp_structure', None)
args_name = inspect.getargspec(func).args
args_dict = dict(itertools.izip(args_name[1:], args))
args_dict = dict(zip(args_name[1:], args))
for key, val in kwargs:
args_dict[key] = val
return func(
Expand Down
61 changes: 52 additions & 9 deletions src/pybind/mgr/dashboard/services/rgw_client.py
Expand Up @@ -11,21 +11,64 @@


def _determine_rgw_addr():
"""
Get a RGW daemon to determine the configured host (IP address) and port.
Note, the service id of the RGW daemons may differ depending on the setup.
Example 1:
{
...
'services': {
'rgw': {
'daemons': {
'summary': '',
'0': {
...
'metadata': {
'frontend_config#0': 'civetweb port=7280',
}
...
}
}
}
}
}
Example 2:
{
...
'services': {
'rgw': {
'daemons': {
'summary': '',
'rgw': {
...
'metadata': {
'frontend_config#0': 'civetweb port=8000',
}
...
}
}
}
}
}
"""
service_map = mgr.get('service_map')
if not dict_contains_path(service_map, ['services', 'rgw', 'daemons']):
raise LookupError('No RGW found.')
daemon = None
daemons = service_map['services']['rgw']['daemons']
for key in daemons.keys():
if dict_contains_path(daemons[key], ['metadata', 'frontend_config#0']):
daemon = daemons[key]
break
if daemon is None:
raise LookupError('No RGW daemon found.')

if not dict_contains_path(service_map, ['services', 'rgw', 'daemons', 'rgw']):
msg = 'No RGW found.'
raise LookupError(msg)

daemon = service_map['services']['rgw']['daemons']['rgw']
addr = daemon['addr'].split(':')[0]
match = re.search(r'port=(\d+)',
daemon['metadata']['frontend_config#0'])
match = re.search(r'port=(\d+)', daemon['metadata']['frontend_config#0'])
if match:
port = int(match.group(1))
else:
msg = 'Failed to determine RGW port'
raise LookupError(msg)
raise LookupError('Failed to determine RGW port')

return addr, port

Expand Down
1 change: 0 additions & 1 deletion src/pybind/mgr/dashboard/tests/test_tools.py
Expand Up @@ -153,5 +153,4 @@ def test_dict_contains_path(self):
self.assertTrue(dict_contains_path(x, ['a', 'b', 'c']))
self.assertTrue(dict_contains_path(x, ['a']))
self.assertFalse(dict_contains_path(x, ['a', 'c']))

self.assertTrue(dict_contains_path(x, []))
6 changes: 4 additions & 2 deletions src/pybind/mgr/dashboard/tools.py
Expand Up @@ -8,10 +8,10 @@
import time
import threading
import socket
from six.moves import urllib
import cherrypy

from . import logger
from six.moves import urllib


class RequestLoggingTool(cherrypy.Tool):
Expand Down Expand Up @@ -673,13 +673,15 @@ def build_url(host, scheme=None, port=None):

def dict_contains_path(dct, keys):
"""
Tests wheter the keys exist recursively in `dictionary`.
Tests whether the keys exist recursively in `dictionary`.
:type dct: dict
:type keys: list
:rtype: bool
"""
if keys:
if not isinstance(dct, dict):
return False
key = keys.pop(0)
if key in dct:
dct = dct[key]
Expand Down

0 comments on commit aac311d

Please sign in to comment.