showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 1 | import os, BaseHTTPServer, cgi, threading, urllib |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 2 | import common |
| 3 | from autotest_lib.scheduler import scheduler_config |
| 4 | |
| 5 | _PORT = 13467 |
| 6 | |
| 7 | _HEADER = """ |
| 8 | <html> |
| 9 | <head><title>Scheduler status</title></head> |
| 10 | <body> |
| 11 | Actions:<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 | |
| 21 | class 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 | |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame^] | 52 | def _write_drone(self, hostname): |
| 53 | line = hostname |
| 54 | if not self.server._drone_manager.is_drone_enabled(hostname): |
| 55 | line += ' (disabled)' |
| 56 | self._write_line(line) |
| 57 | |
| 58 | |
| 59 | def _write_drone_list(self): |
| 60 | self._write_line('Drones:') |
| 61 | for hostname in self.server._drone_manager.drone_hostnames(): |
| 62 | self._write_drone(hostname) |
| 63 | self._write_line() |
| 64 | |
| 65 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 66 | def _execute_actions(self, arguments): |
| 67 | if 'reparse_config' in arguments: |
| 68 | scheduler_config.config.read_config() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame^] | 69 | self.server._drone_manager.refresh_disabled_drones() |
| 70 | self._write_line('Reparsed config!') |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 71 | self._write_line() |
| 72 | |
| 73 | |
| 74 | def do_GET(self): |
| 75 | self._send_headers() |
| 76 | self.wfile.write(_HEADER) |
| 77 | |
| 78 | arguments = self._parse_arguments() |
| 79 | self._execute_actions(arguments) |
| 80 | self._write_all_fields() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame^] | 81 | self._write_drone_list() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 82 | |
| 83 | self.wfile.write(_FOOTER) |
| 84 | |
| 85 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 86 | class StatusServer(BaseHTTPServer.HTTPServer): |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame^] | 87 | def __init__(self, drone_manager): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 88 | address = ('', _PORT) |
| 89 | # HTTPServer is an old-style class :( |
| 90 | BaseHTTPServer.HTTPServer.__init__(self, address, |
| 91 | StatusServerRequestHandler) |
| 92 | self._shutting_down = False |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame^] | 93 | self._drone_manager = drone_manager |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 94 | |
| 95 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 96 | def shutdown(self): |
| 97 | if self._shutting_down: |
| 98 | return |
| 99 | print 'Shutting down server...' |
| 100 | self._shutting_down = True |
| 101 | # make one last request to awaken the server thread and make it exit |
| 102 | urllib.urlopen('http://localhost:%s' % _PORT) |
| 103 | |
| 104 | |
| 105 | def _serve_until_shutdown(self): |
| 106 | print 'Status server running on', self.server_address |
| 107 | while not self._shutting_down: |
| 108 | self.handle_request() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def start(self): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 112 | self._thread = threading.Thread(target=self._serve_until_shutdown, |
| 113 | name='status_server') |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 114 | self._thread.start() |