blob: b8832f01482af6acc99638d1e017b1f6d6d4e9fc [file] [log] [blame]
showard55b4b542009-01-08 23:30:30 +00001import os, BaseHTTPServer, cgi, threading, urllib
showardd1ee1dd2009-01-07 21:33:08 +00002import common
3from autotest_lib.scheduler import scheduler_config
4
5_PORT = 13467
6
7_HEADER = """
8<html>
9<head><title>Scheduler status</title></head>
10<body>
11Actions:<br>
12<a href="?reparse_config=1">Reparse global config values</a><br>
13<br>
14"""
15
16_FOOTER = """
17</body>
18</html>
19"""
20
21class StatusServerRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
22 def _send_headers(self):
23 self.send_response(200, 'OK')
24 self.send_header('Content-Type', 'text/html')
25 self.end_headers()
26
27
28 def _parse_arguments(self):
29 path_parts = self.path.split('?', 1)
30 if len(path_parts) == 1:
31 return {}
32
33 encoded_args = path_parts[1]
34 return cgi.parse_qs(encoded_args)
35
36
37 def _write_line(self, line=''):
38 self.wfile.write(line + '<br>\n')
39
40
41 def _write_field(self, field, value):
42 self._write_line('%s=%s' % (field, value))
43
44
45 def _write_all_fields(self):
46 self._write_line('Config values:')
47 for field in scheduler_config.SchedulerConfig.FIELDS:
48 self._write_field(field, getattr(scheduler_config.config, field))
49 self._write_line()
50
51
52 def _execute_actions(self, arguments):
53 if 'reparse_config' in arguments:
54 scheduler_config.config.read_config()
55 self._write_line('Updated config!')
56 self._write_line()
57
58
59 def do_GET(self):
60 self._send_headers()
61 self.wfile.write(_HEADER)
62
63 arguments = self._parse_arguments()
64 self._execute_actions(arguments)
65 self._write_all_fields()
66
67 self.wfile.write(_FOOTER)
68
69
showard55b4b542009-01-08 23:30:30 +000070class StatusServer(BaseHTTPServer.HTTPServer):
showardd1ee1dd2009-01-07 21:33:08 +000071 def __init__(self):
showard55b4b542009-01-08 23:30:30 +000072 address = ('', _PORT)
73 # HTTPServer is an old-style class :(
74 BaseHTTPServer.HTTPServer.__init__(self, address,
75 StatusServerRequestHandler)
76 self._shutting_down = False
showardd1ee1dd2009-01-07 21:33:08 +000077
78
showard55b4b542009-01-08 23:30:30 +000079 def shutdown(self):
80 if self._shutting_down:
81 return
82 print 'Shutting down server...'
83 self._shutting_down = True
84 # make one last request to awaken the server thread and make it exit
85 urllib.urlopen('http://localhost:%s' % _PORT)
86
87
88 def _serve_until_shutdown(self):
89 print 'Status server running on', self.server_address
90 while not self._shutting_down:
91 self.handle_request()
showardd1ee1dd2009-01-07 21:33:08 +000092
93
94 def start(self):
showard55b4b542009-01-08 23:30:30 +000095 self._thread = threading.Thread(target=self._serve_until_shutdown,
96 name='status_server')
showardd1ee1dd2009-01-07 21:33:08 +000097 self._thread.start()