blob: 0d9198db43e65a5adea606e0adcf4b04be6fa0a4 [file] [log] [blame]
showard4fd61be2009-01-13 00:09:52 +00001import os, BaseHTTPServer, cgi, threading, urllib, fcntl
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
showard324bf812009-01-20 23:23:38 +000052 def _write_drone(self, drone):
53 line = '%s %s/%s processes' % (drone.hostname, drone.active_processes,
54 drone.max_processes)
55 if not drone.enabled:
showardc5afc462009-01-13 00:09:39 +000056 line += ' (disabled)'
57 self._write_line(line)
58
59
60 def _write_drone_list(self):
61 self._write_line('Drones:')
showard324bf812009-01-20 23:23:38 +000062 for drone in self.server._drone_manager.get_drones():
63 self._write_drone(drone)
showardc5afc462009-01-13 00:09:39 +000064 self._write_line()
65
66
showardd1ee1dd2009-01-07 21:33:08 +000067 def _execute_actions(self, arguments):
68 if 'reparse_config' in arguments:
69 scheduler_config.config.read_config()
showard324bf812009-01-20 23:23:38 +000070 self.server._drone_manager.refresh_drone_configs()
showardc5afc462009-01-13 00:09:39 +000071 self._write_line('Reparsed config!')
showardd1ee1dd2009-01-07 21:33:08 +000072 self._write_line()
73
74
75 def do_GET(self):
76 self._send_headers()
77 self.wfile.write(_HEADER)
78
79 arguments = self._parse_arguments()
80 self._execute_actions(arguments)
81 self._write_all_fields()
showardc5afc462009-01-13 00:09:39 +000082 self._write_drone_list()
showardd1ee1dd2009-01-07 21:33:08 +000083
84 self.wfile.write(_FOOTER)
85
86
showard55b4b542009-01-08 23:30:30 +000087class StatusServer(BaseHTTPServer.HTTPServer):
showardc5afc462009-01-13 00:09:39 +000088 def __init__(self, drone_manager):
showard55b4b542009-01-08 23:30:30 +000089 address = ('', _PORT)
90 # HTTPServer is an old-style class :(
91 BaseHTTPServer.HTTPServer.__init__(self, address,
92 StatusServerRequestHandler)
93 self._shutting_down = False
showardc5afc462009-01-13 00:09:39 +000094 self._drone_manager = drone_manager
showardd1ee1dd2009-01-07 21:33:08 +000095
showard4fd61be2009-01-13 00:09:52 +000096 # ensure the listening socket is not inherited by child processes
97 old_flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD)
98 fcntl.fcntl(self.fileno(), fcntl.F_SETFD, old_flags | fcntl.FD_CLOEXEC)
99
showardd1ee1dd2009-01-07 21:33:08 +0000100
showard55b4b542009-01-08 23:30:30 +0000101 def shutdown(self):
102 if self._shutting_down:
103 return
104 print 'Shutting down server...'
105 self._shutting_down = True
106 # make one last request to awaken the server thread and make it exit
107 urllib.urlopen('http://localhost:%s' % _PORT)
108
109
110 def _serve_until_shutdown(self):
111 print 'Status server running on', self.server_address
112 while not self._shutting_down:
113 self.handle_request()
showardd1ee1dd2009-01-07 21:33:08 +0000114
115
116 def start(self):
showard55b4b542009-01-08 23:30:30 +0000117 self._thread = threading.Thread(target=self._serve_until_shutdown,
118 name='status_server')
showardd1ee1dd2009-01-07 21:33:08 +0000119 self._thread.start()