1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 from __future__ import absolute_import, print_function
6 from future import standard_library
14 from ._version import __version__
16 _logger = logging.getLogger('status.Handler')
19 class Server(socketserver.ThreadingMixIn, http.server.HTTPServer, object):
20 def __init__(self, config):
21 port = config.getint('Manage', 'port')
22 self.enabled = port >= 0
24 _logger.warning("Management server disabled. "+
25 "Use [Manage] config section to enable.")
28 self._tracker = tracker
29 super(Server, self).__init__(
30 (config.get('Manage', 'address'), port), Handler)
31 self._thread = threading.Thread(target=self.serve_forever)
32 self._thread.daemon = True
39 class Handler(http.server.BaseHTTPRequestHandler, object):
41 if self.path == '/status.json':
42 self.send_response(200)
43 self.send_header('Content-type', 'application/json')
45 self.wfile.write(tracker.get_json())
46 elif self.path == '/_health/ping':
47 code, msg = self.check_auth()
50 self.send_response(code)
53 self.send_response(200)
54 self.send_header('Content-type', 'application/json')
56 self.wfile.write(json.dumps({"health":"OK"}))
58 self.send_response(404)
60 def log_message(self, fmt, *args, **kwargs):
61 _logger.info(fmt, *args, **kwargs)
64 mgmt_token = self.server._config.get('Manage', 'ManagementToken')
65 auth_header = self.headers.get('Authorization', None)
68 return 404, "disabled"
69 elif auth_header == None:
70 return 401, "authorization required"
71 elif auth_header != 'Bearer '+mgmt_token:
72 return 403, "authorization error"
75 class Tracker(object):
77 self._mtx = threading.Lock()
79 self._version = {'Version' : __version__}
83 return json.dumps(dict(self._latest, **self._version))
87 return self._latest.keys()
89 def update(self, updates):
91 self._latest.update(updates)