showard | 4fd61be | 2009-01-13 00:09:52 +0000 | [diff] [blame] | 1 | import os, BaseHTTPServer, cgi, threading, urllib, fcntl |
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 | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 52 | 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: |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 56 | line += ' (disabled)' |
| 57 | self._write_line(line) |
| 58 | |
| 59 | |
| 60 | def _write_drone_list(self): |
| 61 | self._write_line('Drones:') |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 62 | for drone in self.server._drone_manager.get_drones(): |
| 63 | self._write_drone(drone) |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 64 | self._write_line() |
| 65 | |
| 66 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 67 | def _execute_actions(self, arguments): |
| 68 | if 'reparse_config' in arguments: |
| 69 | scheduler_config.config.read_config() |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 70 | self.server._drone_manager.refresh_drone_configs() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 71 | self._write_line('Reparsed config!') |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 72 | 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() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 82 | self._write_drone_list() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 83 | |
| 84 | self.wfile.write(_FOOTER) |
| 85 | |
| 86 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 87 | class StatusServer(BaseHTTPServer.HTTPServer): |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 88 | def __init__(self, drone_manager): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 89 | address = ('', _PORT) |
| 90 | # HTTPServer is an old-style class :( |
| 91 | BaseHTTPServer.HTTPServer.__init__(self, address, |
| 92 | StatusServerRequestHandler) |
| 93 | self._shutting_down = False |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 94 | self._drone_manager = drone_manager |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 95 | |
showard | 4fd61be | 2009-01-13 00:09:52 +0000 | [diff] [blame] | 96 | # 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 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 100 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 101 | 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() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def start(self): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 117 | self._thread = threading.Thread(target=self._serve_until_shutdown, |
| 118 | name='status_server') |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 119 | self._thread.start() |