-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
90 lines (73 loc) · 2.93 KB
/
module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
"""
openATTIC mgr plugin (based on CherryPy)
"""
from __future__ import absolute_import
import os
import cherrypy
from cherrypy import tools
from mgr_module import MgrModule
from .controllers.auth import Auth
from .tools import load_controllers
# cherrypy likes to sys.exit on error. don't let it take us down too!
def os_exit_noop(*args):
pass
os._exit = os_exit_noop
class Module(MgrModule):
"""
dashboard module entrypoint
"""
COMMANDS = [
{
"cmd": "dashboard set-login-credentials "
"name=username,type=CephString "
"name=password,type=CephString",
"desc": "Set the login credentials",
"perm": "w"
}
]
def __init__(self, *args, **kwargs):
super(Module, self).__init__(*args, **kwargs)
def serve(self):
server_addr = self.get_localized_config('server_addr', '::')
server_port = self.get_localized_config('server_port', '8080')
if server_addr is None:
raise RuntimeError(
'no server_addr configured; '
'try "ceph config-key put mgr/{}/{}/server_addr <ip>"'
.format(self.module_name, self.get_mgr_id()))
self.log.info("server_addr: %s server_port: %s" % (server_addr,
server_port))
cherrypy.config.update({
'server.socket_host': server_addr,
'server.socket_port': int(server_port),
})
cherrypy.tools.autenticate = cherrypy.Tool('before_handler',
Auth.check_auth)
cherrypy.tree.mount(Module.ApiRoot(self), "/api")
cherrypy.engine.start()
self.log.info("Waiting for engine...")
cherrypy.engine.block()
self.log.info("Engine done.")
def shutdown(self):
self.log.info("Stopping server...")
cherrypy.engine.exit()
self.log.info("Stopped server")
def handle_command(self, cmd):
if cmd['prefix'] == 'dashboard set-login-credentials':
self.set_localized_config('username', cmd['username'])
hashed_passwd = Auth.password_hash(cmd['password'])
self.set_localized_config('password', hashed_passwd)
return 0, 'Username and password updated', ''
else:
return (-errno.EINVAL, '', 'Command not found \'{0}\''.format(
cmd['prefix']))
class ApiRoot(object):
def __init__(self, mgrmod):
ctrls = load_controllers(mgrmod)
mgrmod.log.debug("loaded controllers: {}".format(ctrls))
for ctrl in ctrls:
mgrmod.log.warn("adding controller: {} -> {}"
.format(ctrl.__name__, ctrl._cp_path_))
ins = ctrl()
setattr(Module.ApiRoot, ctrl._cp_path_, ins)