showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 1 | import os, BaseHTTPServer, cgi, threading, urllib, fcntl, logging |
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): |
showard | 1b7142d | 2010-01-15 00:21:37 +0000 | [diff] [blame^] | 53 | if drone.allowed_users: |
| 54 | allowed_users = ', '.join(drone.allowed_users) |
| 55 | else: |
| 56 | allowed_users = 'all' |
| 57 | line = ('%s: %s/%s processes, users: %s' |
| 58 | % (drone.hostname, drone.active_processes, drone.max_processes, |
| 59 | allowed_users)) |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 60 | if not drone.enabled: |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 61 | line += ' (disabled)' |
| 62 | self._write_line(line) |
| 63 | |
| 64 | |
| 65 | def _write_drone_list(self): |
| 66 | self._write_line('Drones:') |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 67 | for drone in self.server._drone_manager.get_drones(): |
| 68 | self._write_drone(drone) |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 69 | self._write_line() |
| 70 | |
| 71 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 72 | def _execute_actions(self, arguments): |
| 73 | if 'reparse_config' in arguments: |
| 74 | scheduler_config.config.read_config() |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 75 | self.server._drone_manager.refresh_drone_configs() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 76 | self._write_line('Reparsed config!') |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 77 | self._write_line() |
| 78 | |
| 79 | |
| 80 | def do_GET(self): |
| 81 | self._send_headers() |
| 82 | self.wfile.write(_HEADER) |
| 83 | |
| 84 | arguments = self._parse_arguments() |
| 85 | self._execute_actions(arguments) |
| 86 | self._write_all_fields() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 87 | self._write_drone_list() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 88 | |
| 89 | self.wfile.write(_FOOTER) |
| 90 | |
| 91 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 92 | class StatusServer(BaseHTTPServer.HTTPServer): |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 93 | def __init__(self, drone_manager): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 94 | address = ('', _PORT) |
| 95 | # HTTPServer is an old-style class :( |
| 96 | BaseHTTPServer.HTTPServer.__init__(self, address, |
| 97 | StatusServerRequestHandler) |
| 98 | self._shutting_down = False |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 99 | self._drone_manager = drone_manager |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 100 | |
showard | 4fd61be | 2009-01-13 00:09:52 +0000 | [diff] [blame] | 101 | # ensure the listening socket is not inherited by child processes |
| 102 | old_flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD) |
| 103 | fcntl.fcntl(self.fileno(), fcntl.F_SETFD, old_flags | fcntl.FD_CLOEXEC) |
| 104 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 105 | |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 106 | def shutdown(self): |
| 107 | if self._shutting_down: |
| 108 | return |
showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 109 | logging.info('Shutting down server...') |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 110 | self._shutting_down = True |
| 111 | # make one last request to awaken the server thread and make it exit |
| 112 | urllib.urlopen('http://localhost:%s' % _PORT) |
| 113 | |
| 114 | |
| 115 | def _serve_until_shutdown(self): |
showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 116 | logging.info('Status server running on %s', self.server_address) |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 117 | while not self._shutting_down: |
| 118 | self.handle_request() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def start(self): |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 122 | self._thread = threading.Thread(target=self._serve_until_shutdown, |
| 123 | name='status_server') |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 124 | self._thread.start() |