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 | |
| 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 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame^] | 70 | class StatusServer(BaseHTTPServer.HTTPServer): |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 71 | def __init__(self): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame^] | 72 | address = ('', _PORT) |
| 73 | # HTTPServer is an old-style class :( |
| 74 | BaseHTTPServer.HTTPServer.__init__(self, address, |
| 75 | StatusServerRequestHandler) |
| 76 | self._shutting_down = False |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 77 | |
| 78 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame^] | 79 | 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() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def start(self): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame^] | 95 | self._thread = threading.Thread(target=self._serve_until_shutdown, |
| 96 | name='status_server') |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 97 | self._thread.start() |