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 _logger = logging.getLogger('status.Handler')
17 class Server(socketserver.ThreadingMixIn, http.server.HTTPServer, object):
18 def __init__(self, config):
19 port = config.getint('Manage', 'port')
20 self.enabled = port >= 0
22 _logger.warning("Management server disabled. "+
23 "Use [Manage] config section to enable.")
26 self._tracker = tracker
27 super(Server, self).__init__(
28 (config.get('Manage', 'address'), port), Handler)
29 self._thread = threading.Thread(target=self.serve_forever)
30 self._thread.daemon = True
37 class Handler(http.server.BaseHTTPRequestHandler, object):
39 if self.path == '/status.json':
40 self.send_response(200)
41 self.send_header('Content-type', 'application/json')
43 self.wfile.write(tracker.get_json())
44 elif self.path == '/_health/ping':
45 code, msg = self.check_auth()
48 self.send_response(code)
51 self.send_response(200)
52 self.send_header('Content-type', 'application/json')
54 self.wfile.write(json.dumps({"health":"OK"}))
56 self.send_response(404)
58 def log_message(self, fmt, *args, **kwargs):
59 _logger.info(fmt, *args, **kwargs)
62 mgmt_token = self.server._config.get('Manage', 'ManagementToken')
63 auth_header = self.headers.get('Authorization', None)
66 return 404, "disabled"
67 elif auth_header == None:
68 return 401, "authorization required"
69 elif auth_header != 'Bearer '+mgmt_token:
70 return 403, "authorization error"
73 class Tracker(object):
75 self._mtx = threading.Lock()
80 return json.dumps(self._latest)
84 return self._latest.keys()
86 def update(self, updates):
88 self._latest.update(updates)