Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Chris Sosa | 781ba6d | 2012-04-11 19:44:43 | [diff] [blame] | 3 | # Copyright (c) 2009-2012 The Chromium OS Authors. All rights reserved. |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 7 | """A CherryPy-based webserver to host images and build packages.""" |
| 8 | |
| 9 | import cherrypy |
Chris Sosa | 781ba6d | 2012-04-11 19:44:43 | [diff] [blame] | 10 | import logging |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 11 | import multiprocessing |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 12 | import optparse |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 13 | import os |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 14 | import re |
[email protected] | 4dc2581 | 2009-10-27 23:46:26 | [diff] [blame] | 15 | import sys |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 16 | import subprocess |
| 17 | import tempfile |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 18 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 19 | import autoupdate |
Scott Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 20 | import devserver_util |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 21 | import downloader |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 22 | |
Frank Farzan | 4016087 | 2011-12-13 02:39:18 | [diff] [blame] | 23 | |
Chris Sosa | 417e55d | 2011-01-26 00:40:48 | [diff] [blame] | 24 | CACHED_ENTRIES = 12 |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 25 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 26 | # Sets up global to share between classes. |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 27 | global updater |
| 28 | updater = None |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 29 | |
Frank Farzan | 4016087 | 2011-12-13 02:39:18 | [diff] [blame] | 30 | |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 31 | class DevServerError(Exception): |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 32 | """Exception class used by this module.""" |
| 33 | pass |
| 34 | |
| 35 | |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 36 | def _LeadingWhiteSpaceCount(string): |
| 37 | """Count the amount of leading whitespace in a string. |
| 38 | |
| 39 | Args: |
| 40 | string: The string to count leading whitespace in. |
| 41 | Returns: |
| 42 | number of white space chars before characters start. |
| 43 | """ |
| 44 | matched = re.match('^\s+', string) |
| 45 | if matched: |
| 46 | return len(matched.group()) |
| 47 | |
| 48 | return 0 |
| 49 | |
| 50 | |
| 51 | def _PrintDocStringAsHTML(func): |
| 52 | """Make a functions docstring somewhat HTML style. |
| 53 | |
| 54 | Args: |
| 55 | func: The function to return the docstring from. |
| 56 | Returns: |
| 57 | A string that is somewhat formated for a web browser. |
| 58 | """ |
| 59 | # TODO(scottz): Make this parse Args/Returns in a prettier way. |
| 60 | # Arguments could be bolded and indented etc. |
| 61 | html_doc = [] |
| 62 | for line in func.__doc__.splitlines(): |
| 63 | leading_space = _LeadingWhiteSpaceCount(line) |
| 64 | if leading_space > 0: |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 65 | line = ' ' * leading_space + line |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 66 | |
| 67 | html_doc.append('<BR>%s' % line) |
| 68 | |
| 69 | return '\n'.join(html_doc) |
| 70 | |
| 71 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 72 | def _GetConfig(options): |
| 73 | """Returns the configuration for the devserver.""" |
| 74 | base_config = { 'global': |
| 75 | { 'server.log_request_headers': True, |
| 76 | 'server.protocol_version': 'HTTP/1.1', |
Aaron Plattner | 2bfab98 | 2011-05-20 16:01:08 | [diff] [blame] | 77 | 'server.socket_host': '::', |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 78 | 'server.socket_port': int(options.port), |
Chris Sosa | 374c62d | 2010-10-14 16:13:54 | [diff] [blame] | 79 | 'response.timeout': 6000, |
Chris Sosa | 72333d1 | 2012-06-13 18:28:05 | [diff] [blame] | 80 | 'server.socket_timeout': 60, |
Zdenek Behan | 1347a31 | 2011-02-10 02:59:17 | [diff] [blame] | 81 | 'tools.staticdir.root': |
| 82 | os.path.dirname(os.path.abspath(sys.argv[0])), |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 83 | }, |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 84 | '/api': |
| 85 | { |
| 86 | # Gets rid of cherrypy parsing post file for args. |
| 87 | 'request.process_request_body': False, |
| 88 | }, |
Chris Sosa | a1ef010 | 2010-10-21 23:22:35 | [diff] [blame] | 89 | '/build': |
| 90 | { |
| 91 | 'response.timeout': 100000, |
| 92 | }, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 93 | '/update': |
| 94 | { |
| 95 | # Gets rid of cherrypy parsing post file for args. |
| 96 | 'request.process_request_body': False, |
Chris Sosa | f65f4b9 | 2010-10-21 22:57:51 | [diff] [blame] | 97 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 98 | }, |
| 99 | # Sets up the static dir for file hosting. |
| 100 | '/static': |
| 101 | { 'tools.staticdir.dir': 'static', |
| 102 | 'tools.staticdir.on': True, |
Chris Sosa | f65f4b9 | 2010-10-21 22:57:51 | [diff] [blame] | 103 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 104 | }, |
| 105 | } |
Scott Zawalski | 1c5e7cd | 2012-02-27 18:12:52 | [diff] [blame] | 106 | |
| 107 | if options.log_dir: |
| 108 | base_config['global']['log.access_file'] = os.path.join( |
| 109 | options.log_dir, 'devserver_access.log') |
| 110 | base_config['global']['log.error_file'] = os.path.join( |
| 111 | options.log_dir, 'devserver_error.log') |
| 112 | |
Chris Sosa | 417e55d | 2011-01-26 00:40:48 | [diff] [blame] | 113 | if options.production: |
| 114 | base_config['global']['server.environment'] = 'production' |
| 115 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 116 | return base_config |
[email protected] | 6424466 | 2009-11-12 00:52:08 | [diff] [blame] | 117 | |
Darin Petkov | e17164a | 2010-08-11 20:24:41 | [diff] [blame] | 118 | |
Zdenek Behan | 608f46c | 2011-02-18 23:47:16 | [diff] [blame] | 119 | def _PrepareToServeUpdatesOnly(image_dir, static_dir): |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 120 | """Sets up symlink to image_dir for serving purposes.""" |
| 121 | assert os.path.exists(image_dir), '%s must exist.' % image_dir |
| 122 | # If we're serving out of an archived build dir (e.g. a |
| 123 | # buildbot), prepare this webserver's magic 'static/' dir with a |
| 124 | # link to the build archive. |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 125 | cherrypy.log('Preparing autoupdate for "serve updates only" mode.', |
| 126 | 'DEVSERVER') |
Zdenek Behan | 608f46c | 2011-02-18 23:47:16 | [diff] [blame] | 127 | if os.path.lexists('%s/archive' % static_dir): |
| 128 | if image_dir != os.readlink('%s/archive' % static_dir): |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 129 | cherrypy.log('removing stale symlink to %s' % image_dir, 'DEVSERVER') |
Zdenek Behan | 608f46c | 2011-02-18 23:47:16 | [diff] [blame] | 130 | os.unlink('%s/archive' % static_dir) |
| 131 | os.symlink(image_dir, '%s/archive' % static_dir) |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 132 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 133 | else: |
Zdenek Behan | 608f46c | 2011-02-18 23:47:16 | [diff] [blame] | 134 | os.symlink(image_dir, '%s/archive' % static_dir) |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 135 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 136 | cherrypy.log('archive dir: %s ready to be used to serve images.' % image_dir, |
| 137 | 'DEVSERVER') |
| 138 | |
| 139 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 140 | class ApiRoot(object): |
| 141 | """RESTful API for Dev Server information.""" |
| 142 | exposed = True |
| 143 | |
| 144 | @cherrypy.expose |
| 145 | def hostinfo(self, ip): |
| 146 | """Returns a JSON dictionary containing information about the given ip. |
| 147 | |
| 148 | Not all information may be known at the time the request is made. The |
| 149 | possible keys are: |
| 150 | |
| 151 | last_event_type: int |
| 152 | Last update event type received. |
| 153 | |
| 154 | last_event_status: int |
| 155 | Last update event status received. |
| 156 | |
| 157 | last_known_version: string |
| 158 | Last known version recieved for update ping. |
| 159 | |
| 160 | forced_update_label: string |
| 161 | Update label to force next update ping to use. Set by setnextupdate. |
| 162 | |
| 163 | See the OmahaEvent class in update_engine/omaha_request_action.h for status |
| 164 | code definitions. If the ip does not exist an empty string is returned.""" |
| 165 | return updater.HandleHostInfoPing(ip) |
| 166 | |
| 167 | @cherrypy.expose |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 168 | def hostlog(self, ip): |
| 169 | """Returns a JSON object containing a log of events pertaining to a |
| 170 | particular host, or all hosts. Log events contain a timestamp and any |
| 171 | subset of the attributes listed for the hostinfo method.""" |
| 172 | return updater.HandleHostLogPing(ip) |
| 173 | |
| 174 | @cherrypy.expose |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 175 | def setnextupdate(self, ip): |
| 176 | """Allows the response to the next update ping from a host to be set. |
| 177 | |
| 178 | Takes the IP of the host and an update label as normally provided to the |
| 179 | /update command.""" |
| 180 | body_length = int(cherrypy.request.headers['Content-Length']) |
| 181 | label = cherrypy.request.rfile.read(body_length) |
| 182 | |
| 183 | if label: |
| 184 | label = label.strip() |
| 185 | if label: |
| 186 | return updater.HandleSetUpdatePing(ip, label) |
| 187 | raise cherrypy.HTTPError(400, 'No label provided.') |
| 188 | |
| 189 | |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 190 | class DevServerRoot(object): |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 191 | """The Root Class for the Dev Server. |
| 192 | |
| 193 | CherryPy works as follows: |
| 194 | For each method in this class, cherrpy interprets root/path |
| 195 | as a call to an instance of DevServerRoot->method_name. For example, |
| 196 | a call to http://myhost/build will call build. CherryPy automatically |
| 197 | parses http args and places them as keyword arguments in each method. |
| 198 | For paths http://myhost/update/dir1/dir2, you can use *args so that |
| 199 | cherrypy uses the update method and puts the extra paths in args. |
| 200 | """ |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 201 | api = ApiRoot() |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 202 | |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 203 | def __init__(self): |
Nick Sanders | 7dcaa2e | 2011-08-04 22:20:41 | [diff] [blame] | 204 | self._builder = None |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 205 | self._lock_dict_lock = multiprocessing.Lock() |
| 206 | self._lock_dict = {} |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 207 | self._downloader_dict = {} |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 208 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 209 | @cherrypy.expose |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 210 | def build(self, board, pkg, **kwargs): |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 211 | """Builds the package specified.""" |
Nick Sanders | 7dcaa2e | 2011-08-04 22:20:41 | [diff] [blame] | 212 | import builder |
| 213 | if self._builder is None: |
| 214 | self._builder = builder.Builder() |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 215 | return self._builder.Build(board, pkg, kwargs) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 216 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 217 | def _get_lock_for_archive_url(self, archive_url): |
| 218 | """Return a multiprocessing lock to use per archive_url. |
| 219 | |
| 220 | Use this lock to protect critical zones per archive_url. |
| 221 | |
| 222 | Usage: |
| 223 | with DevserverInstance._get_lock_for_archive_url(archive_url): |
| 224 | # CRITICAL AREA FOR ARCHIVE_URL. |
| 225 | |
| 226 | Returns: |
| 227 | A multiprocessing lock that is archive_url specific. |
| 228 | """ |
| 229 | with self._lock_dict_lock: |
| 230 | lock = self._lock_dict.get(archive_url) |
| 231 | if lock: |
| 232 | return lock |
| 233 | else: |
| 234 | lock = multiprocessing.Lock() |
| 235 | self._lock_dict[archive_url] = lock |
| 236 | return lock |
| 237 | |
| 238 | @staticmethod |
| 239 | def _canonicalize_archive_url(archive_url): |
| 240 | """Canonicalizes archive_url strings. |
| 241 | |
| 242 | Raises: |
| 243 | DevserverError: if archive_url is not set. |
| 244 | """ |
| 245 | if archive_url: |
| 246 | return archive_url.rstrip('/') |
| 247 | else: |
| 248 | raise DevServerError("Must specify an archive_url in the request") |
| 249 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 250 | @cherrypy.expose |
Frank Farzan | bcb571e | 2012-01-03 19:48:17 | [diff] [blame] | 251 | def download(self, **kwargs): |
| 252 | """Downloads and archives full/delta payloads from Google Storage. |
| 253 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 254 | This methods downloads artifacts. It may download artifacts in the |
| 255 | background in which case a caller should call wait_for_status to get |
| 256 | the status of the background artifact downloads. They should use the same |
| 257 | args passed to download. |
| 258 | |
Frank Farzan | bcb571e | 2012-01-03 19:48:17 | [diff] [blame] | 259 | Args: |
| 260 | archive_url: Google Storage URL for the build. |
| 261 | |
| 262 | Example URL: |
| 263 | 'http://myhost/download?archive_url=gs://chromeos-image-archive/' |
| 264 | 'x86-generic/R17-1208.0.0-a1-b338' |
| 265 | """ |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 266 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 267 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 268 | # Guarantees that no two downloads for the same url can run this code |
| 269 | # at the same time. |
| 270 | with self._get_lock_for_archive_url(archive_url): |
| 271 | try: |
| 272 | # If we are currently downloading, return. Note, due to the above lock |
| 273 | # we know that the foreground artifacts must have finished downloading |
| 274 | # and returned Success if this downloader instance exists. |
| 275 | if (self._downloader_dict.get(archive_url) or |
| 276 | downloader.Downloader.BuildStaged(archive_url, updater.static_dir)): |
| 277 | cherrypy.log('Build %s has already been processed.' % archive_url, |
| 278 | 'DEVSERVER') |
| 279 | return 'Success' |
| 280 | |
| 281 | downloader_instance = downloader.Downloader(updater.static_dir) |
| 282 | self._downloader_dict[archive_url] = downloader_instance |
| 283 | return downloader_instance.Download(archive_url, background=True) |
| 284 | |
| 285 | except: |
| 286 | # On any exception, reset the state of the downloader_dict. |
| 287 | self._downloader_dict[archive_url] = None |
Chris Sosa | 4d9c4d4 | 2012-06-29 22:23:23 | [diff] [blame^] | 288 | raise |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 289 | |
| 290 | @cherrypy.expose |
| 291 | def wait_for_status(self, **kwargs): |
| 292 | """Waits for background artifacts to be downloaded from Google Storage. |
| 293 | |
| 294 | Args: |
| 295 | archive_url: Google Storage URL for the build. |
| 296 | |
| 297 | Example URL: |
| 298 | 'http://myhost/wait_for_status?archive_url=gs://chromeos-image-archive/' |
| 299 | 'x86-generic/R17-1208.0.0-a1-b338' |
| 300 | """ |
| 301 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
| 302 | downloader_instance = self._downloader_dict.get(archive_url) |
| 303 | if downloader_instance: |
| 304 | status = downloader_instance.GetStatusOfBackgroundDownloads() |
Chris Sosa | 781ba6d | 2012-04-11 19:44:43 | [diff] [blame] | 305 | self._downloader_dict[archive_url] = None |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 306 | return status |
| 307 | else: |
| 308 | # We may have previously downloaded but removed the downloader instance |
| 309 | # from the cache. |
| 310 | if downloader.Downloader.BuildStaged(archive_url, updater.static_dir): |
| 311 | logging.info('%s not found in downloader cache but previously staged.', |
| 312 | archive_url) |
| 313 | return 'Success' |
| 314 | else: |
| 315 | raise DevServerError('No download for the given archive_url found.') |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 316 | |
| 317 | @cherrypy.expose |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 318 | def stage_debug(self, **kwargs): |
| 319 | """Downloads and stages debug symbol payloads from Google Storage. |
| 320 | |
| 321 | This methods downloads the debug symbol build artifact synchronously, |
| 322 | and then stages it for use by symbolicate_dump/. |
| 323 | |
| 324 | Args: |
| 325 | archive_url: Google Storage URL for the build. |
| 326 | |
| 327 | Example URL: |
| 328 | 'http://myhost/stage_debug?archive_url=gs://chromeos-image-archive/' |
| 329 | 'x86-generic/R17-1208.0.0-a1-b338' |
| 330 | """ |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 331 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 332 | return downloader.SymbolDownloader(updater.static_dir).Download(archive_url) |
| 333 | |
| 334 | @cherrypy.expose |
| 335 | def symbolicate_dump(self, minidump): |
| 336 | """Symbolicates a minidump using pre-downloaded symbols, returns it. |
| 337 | |
| 338 | Callers will need to POST to this URL with a body of MIME-type |
| 339 | "multipart/form-data". |
| 340 | The body should include a single argument, 'minidump', containing the |
| 341 | binary-formatted minidump to symbolicate. |
| 342 | |
| 343 | It is up to the caller to ensure that the symbols they want are currently |
| 344 | staged. |
| 345 | |
| 346 | Args: |
| 347 | minidump: The binary minidump file to symbolicate. |
| 348 | """ |
| 349 | to_return = '' |
| 350 | with tempfile.NamedTemporaryFile() as local: |
| 351 | while True: |
| 352 | data = minidump.file.read(8192) |
| 353 | if not data: |
| 354 | break |
| 355 | local.write(data) |
| 356 | local.flush() |
| 357 | stackwalk = subprocess.Popen(['minidump_stackwalk', |
| 358 | local.name, |
| 359 | updater.static_dir + '/debug/breakpad'], |
| 360 | stdout=subprocess.PIPE, |
| 361 | stderr=subprocess.PIPE) |
| 362 | to_return, error_text = stackwalk.communicate() |
| 363 | if stackwalk.returncode != 0: |
| 364 | raise DevServerError("Can't generate stack trace: %s (rc=%d)" % ( |
| 365 | error_text, stackwalk.returncode)) |
| 366 | |
| 367 | return to_return |
| 368 | |
| 369 | @cherrypy.expose |
Scott Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 370 | def latestbuild(self, **params): |
| 371 | """Return a string representing the latest build for a given target. |
| 372 | |
| 373 | Args: |
| 374 | target: The build target, typically a combination of the board and the |
| 375 | type of build e.g. x86-mario-release. |
| 376 | milestone: The milestone to filter builds on. E.g. R16. Optional, if not |
| 377 | provided the latest RXX build will be returned. |
| 378 | Returns: |
| 379 | A string representation of the latest build if one exists, i.e. |
| 380 | R19-1993.0.0-a1-b1480. |
| 381 | An empty string if no latest could be found. |
| 382 | """ |
| 383 | if not params: |
| 384 | return _PrintDocStringAsHTML(self.latestbuild) |
| 385 | |
| 386 | if 'target' not in params: |
| 387 | raise cherrypy.HTTPError('500 Internal Server Error', |
| 388 | 'Error: target= is required!') |
| 389 | try: |
| 390 | return devserver_util.GetLatestBuildVersion( |
| 391 | updater.static_dir, params['target'], |
| 392 | milestone=params.get('milestone')) |
| 393 | except devserver_util.DevServerUtilError as errmsg: |
| 394 | raise cherrypy.HTTPError('500 Internal Server Error', str(errmsg)) |
| 395 | |
| 396 | @cherrypy.expose |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 397 | def controlfiles(self, **params): |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 398 | """Return a control file or a list of all known control files. |
| 399 | |
| 400 | Example URL: |
| 401 | To List all control files: |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 402 | http://dev-server/controlfiles?board=x86-alex-release&build=R18-1514.0.0 |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 403 | To return the contents of a path: |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 404 | http://dev-server/controlfiles?board=x86-alex-release&build=R18-1514.0.0&control_path=client/sleeptest/control |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 405 | |
| 406 | Args: |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 407 | build: The build i.e. x86-alex-release/R18-1514.0.0-a1-b1450. |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 408 | control_path: If you want the contents of a control file set this |
| 409 | to the path. E.g. client/site_tests/sleeptest/control |
| 410 | Optional, if not provided return a list of control files is returned. |
| 411 | Returns: |
| 412 | Contents of a control file if control_path is provided. |
| 413 | A list of control files if no control_path is provided. |
| 414 | """ |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 415 | if not params: |
| 416 | return _PrintDocStringAsHTML(self.controlfiles) |
| 417 | |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 418 | if 'build' not in params: |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 419 | raise cherrypy.HTTPError('500 Internal Server Error', |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 420 | 'Error: build= is required!') |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 421 | |
| 422 | if 'control_path' not in params: |
| 423 | return devserver_util.GetControlFileList(updater.static_dir, |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 424 | params['build']) |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 425 | else: |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 426 | return devserver_util.GetControlFile(updater.static_dir, params['build'], |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 427 | params['control_path']) |
Frank Farzan | 4016087 | 2011-12-13 02:39:18 | [diff] [blame] | 428 | |
| 429 | @cherrypy.expose |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 430 | def index(self): |
| 431 | return 'Welcome to the Dev Server!' |
| 432 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 433 | @cherrypy.expose |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 434 | def update(self, *args): |
| 435 | label = '/'.join(args) |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 436 | body_length = int(cherrypy.request.headers.get('Content-Length', 0)) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 437 | data = cherrypy.request.rfile.read(body_length) |
| 438 | return updater.HandleUpdatePing(data, label) |
| 439 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 440 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 441 | def main(): |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 442 | usage = 'usage: %prog [options]' |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 443 | parser = optparse.OptionParser(usage=usage) |
Sean O'Connor | e38ea15 | 2010-04-16 20:50:40 | [diff] [blame] | 444 | parser.add_option('--archive_dir', dest='archive_dir', |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 445 | help='serve archived builds only.') |
Chris Sosa | e67b78f1 | 2010-11-05 00:33:16 | [diff] [blame] | 446 | parser.add_option('--board', dest='board', |
| 447 | help='When pre-generating update, board for latest image.') |
Don Garrett | 0c880e2 | 2010-11-18 02:13:37 | [diff] [blame] | 448 | parser.add_option('--clear_cache', action='store_true', default=False, |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 449 | help='Clear out all cached udpates and exit') |
Greg Spencer | c8b59b2 | 2011-03-15 21:15:23 | [diff] [blame] | 450 | parser.add_option('--client_prefix', dest='client_prefix_deprecated', |
| 451 | help='No longer used. It is still here so we don\'t break ' |
| 452 | 'scripts that used it.', default='') |
Satoru Takabayashi | d733cbe | 2011-11-15 17:36:32 | [diff] [blame] | 453 | parser.add_option('--critical_update', dest='critical_update', |
| 454 | action='store_true', default=False, |
| 455 | help='Present update payload as critical') |
Zdenek Behan | 5d21a2a | 2011-02-12 01:06:01 | [diff] [blame] | 456 | parser.add_option('--data_dir', dest='data_dir', |
| 457 | help='Writable directory where static lives', |
| 458 | default=os.path.dirname(os.path.abspath(sys.argv[0]))) |
Don Garrett | 0c880e2 | 2010-11-18 02:13:37 | [diff] [blame] | 459 | parser.add_option('--exit', action='store_true', default=False, |
| 460 | help='Don\'t start the server (still pregenerate or clear' |
| 461 | 'cache).') |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 462 | parser.add_option('--factory_config', dest='factory_config', |
| 463 | help='Config file for serving images from factory floor.') |
Chris Sosa | 4136e69 | 2010-10-29 06:42:37 | [diff] [blame] | 464 | parser.add_option('--for_vm', dest='vm', default=False, action='store_true', |
| 465 | help='Update is for a vm image.') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 466 | parser.add_option('--image', dest='image', |
| 467 | help='Force update using this image.') |
Chris Sosa | 2c048f1 | 2010-10-27 23:05:27 | [diff] [blame] | 468 | parser.add_option('-p', '--pregenerate_update', action='store_true', |
| 469 | default=False, help='Pre-generate update payload.') |
Don Garrett | 0c880e2 | 2010-11-18 02:13:37 | [diff] [blame] | 470 | parser.add_option('--payload', dest='payload', |
| 471 | help='Use update payload from specified directory.') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 472 | parser.add_option('--port', default=8080, |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 473 | help='Port for the dev server to use (default: 8080).') |
Chris Sosa | 0f1ec84 | 2011-02-15 00:33:22 | [diff] [blame] | 474 | parser.add_option('--private_key', default=None, |
| 475 | help='Path to the private key in pem format.') |
Chris Sosa | 417e55d | 2011-01-26 00:40:48 | [diff] [blame] | 476 | parser.add_option('--production', action='store_true', default=False, |
| 477 | help='Have the devserver use production values.') |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 478 | parser.add_option('--proxy_port', default=None, |
| 479 | help='Port to have the client connect to (testing support)') |
Chris Sosa | 62f720b | 2010-10-27 04:39:48 | [diff] [blame] | 480 | parser.add_option('--src_image', default='', |
| 481 | help='Image on remote machine for generating delta update.') |
Sean O'Connor | 1f7fd36 | 2010-04-07 23:34:52 | [diff] [blame] | 482 | parser.add_option('-t', action='store_true', dest='test_image') |
| 483 | parser.add_option('-u', '--urlbase', dest='urlbase', |
| 484 | help='base URL, other than devserver, for update images.') |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 485 | parser.add_option('--validate_factory_config', action="store_true", |
| 486 | dest='validate_factory_config', |
| 487 | help='Validate factory config file, then exit.') |
Scott Zawalski | 1c5e7cd | 2012-02-27 18:12:52 | [diff] [blame] | 488 | parser.add_option('-l', '--log-dir', default=None, |
Chris Sosa | b65973e | 2012-03-30 01:31:02 | [diff] [blame] | 489 | help=('Specify a directory for error and access logs. ' |
Scott Zawalski | 1c5e7cd | 2012-02-27 18:12:52 | [diff] [blame] | 490 | 'Default None, i.e. no logging.')) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 491 | (options, _) = parser.parse_args() |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 492 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 493 | devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 494 | root_dir = os.path.realpath('%s/../..' % devserver_dir) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 495 | serve_only = False |
| 496 | |
Zdenek Behan | 608f46c | 2011-02-18 23:47:16 | [diff] [blame] | 497 | static_dir = os.path.realpath('%s/static' % options.data_dir) |
| 498 | os.system('mkdir -p %s' % static_dir) |
| 499 | |
Scott Zawalski | 1c5e7cd | 2012-02-27 18:12:52 | [diff] [blame] | 500 | if options.log_dir and not os.path.isdir(options.log_dir): |
| 501 | parser.error('%s is not a valid dir, provide a valid dir to --log-dir' % |
| 502 | options.log_dir) |
| 503 | |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 504 | if options.archive_dir: |
Zdenek Behan | 608f46c | 2011-02-18 23:47:16 | [diff] [blame] | 505 | # TODO(zbehan) Remove legacy support: |
| 506 | # archive_dir is the directory where static/archive will point. |
| 507 | # If this is an absolute path, all is fine. If someone calls this |
| 508 | # using a relative path, that is relative to src/platform/dev/. |
| 509 | # That use case is unmaintainable, but since applications use it |
| 510 | # with =./static, instead of a boolean flag, we'll make this relative |
| 511 | # to devserver_dir to keep these unbroken. For now. |
| 512 | archive_dir = options.archive_dir |
| 513 | if not os.path.isabs(archive_dir): |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 514 | archive_dir = os.path.realpath(os.path.join(devserver_dir, archive_dir)) |
Zdenek Behan | 608f46c | 2011-02-18 23:47:16 | [diff] [blame] | 515 | _PrepareToServeUpdatesOnly(archive_dir, static_dir) |
Zdenek Behan | 6d93e55 | 2011-03-02 21:35:49 | [diff] [blame] | 516 | static_dir = os.path.realpath(archive_dir) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 517 | serve_only = True |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 518 | |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 519 | cache_dir = os.path.join(static_dir, 'cache') |
| 520 | cherrypy.log('Using cache directory %s' % cache_dir, 'DEVSERVER') |
| 521 | |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 522 | if os.path.exists(cache_dir): |
Chris Sosa | 6b8c374 | 2011-01-31 20:12:17 | [diff] [blame] | 523 | if options.clear_cache: |
| 524 | # Clear the cache and exit on error. |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 525 | cmd = 'rm -rf %s/*' % cache_dir |
| 526 | if os.system(cmd) != 0: |
Chris Sosa | 6b8c374 | 2011-01-31 20:12:17 | [diff] [blame] | 527 | cherrypy.log('Failed to clear the cache with %s' % cmd, |
| 528 | 'DEVSERVER') |
| 529 | sys.exit(1) |
| 530 | |
| 531 | else: |
| 532 | # Clear all but the last N cached updates |
| 533 | cmd = ('cd %s; ls -tr | head --lines=-%d | xargs rm -rf' % |
| 534 | (cache_dir, CACHED_ENTRIES)) |
| 535 | if os.system(cmd) != 0: |
| 536 | cherrypy.log('Failed to clean up old delta cache files with %s' % cmd, |
| 537 | 'DEVSERVER') |
| 538 | sys.exit(1) |
| 539 | else: |
| 540 | os.makedirs(cache_dir) |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 541 | |
Greg Spencer | c8b59b2 | 2011-03-15 21:15:23 | [diff] [blame] | 542 | if options.client_prefix_deprecated: |
| 543 | cherrypy.log('The --client_prefix argument is DEPRECATED, ' |
| 544 | 'and is no longer needed.', 'DEVSERVER') |
| 545 | |
Zdenek Behan | 5d21a2a | 2011-02-12 01:06:01 | [diff] [blame] | 546 | cherrypy.log('Data dir is %s' % options.data_dir, 'DEVSERVER') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 547 | cherrypy.log('Source root is %s' % root_dir, 'DEVSERVER') |
| 548 | cherrypy.log('Serving from %s' % static_dir, 'DEVSERVER') |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 549 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 550 | global updater |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 551 | updater = autoupdate.Autoupdate( |
| 552 | root_dir=root_dir, |
| 553 | static_dir=static_dir, |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 554 | serve_only=serve_only, |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 555 | urlbase=options.urlbase, |
| 556 | test_image=options.test_image, |
| 557 | factory_config_path=options.factory_config, |
Chris Sosa | 5d342a2 | 2010-09-28 23:54:41 | [diff] [blame] | 558 | forced_image=options.image, |
Don Garrett | 0c880e2 | 2010-11-18 02:13:37 | [diff] [blame] | 559 | forced_payload=options.payload, |
Chris Sosa | 62f720b | 2010-10-27 04:39:48 | [diff] [blame] | 560 | port=options.port, |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 561 | proxy_port=options.proxy_port, |
Chris Sosa | 4136e69 | 2010-10-29 06:42:37 | [diff] [blame] | 562 | src_image=options.src_image, |
Chris Sosa | e67b78f1 | 2010-11-05 00:33:16 | [diff] [blame] | 563 | vm=options.vm, |
Chris Sosa | 08d55a2 | 2011-01-20 00:08:02 | [diff] [blame] | 564 | board=options.board, |
Chris Sosa | 0f1ec84 | 2011-02-15 00:33:22 | [diff] [blame] | 565 | copy_to_static_root=not options.exit, |
| 566 | private_key=options.private_key, |
Satoru Takabayashi | d733cbe | 2011-11-15 17:36:32 | [diff] [blame] | 567 | critical_update=options.critical_update, |
Chris Sosa | 0f1ec84 | 2011-02-15 00:33:22 | [diff] [blame] | 568 | ) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 569 | |
| 570 | # Sanity-check for use of validate_factory_config. |
| 571 | if not options.factory_config and options.validate_factory_config: |
| 572 | parser.error('You need a factory_config to validate.') |
[email protected] | 6424466 | 2009-11-12 00:52:08 | [diff] [blame] | 573 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 574 | if options.factory_config: |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 575 | updater.ImportFactoryConfigFile(options.factory_config, |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 576 | options.validate_factory_config) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 577 | # We don't run the dev server with this option. |
| 578 | if options.validate_factory_config: |
| 579 | sys.exit(0) |
Chris Sosa | 2c048f1 | 2010-10-27 23:05:27 | [diff] [blame] | 580 | elif options.pregenerate_update: |
Chris Sosa | e67b78f1 | 2010-11-05 00:33:16 | [diff] [blame] | 581 | if not updater.PreGenerateUpdate(): |
| 582 | sys.exit(1) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 583 | |
Don Garrett | 0c880e2 | 2010-11-18 02:13:37 | [diff] [blame] | 584 | # If the command line requested after setup, it's time to do it. |
| 585 | if not options.exit: |
| 586 | cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options)) |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 587 | |
| 588 | |
| 589 | if __name__ == '__main__': |
| 590 | main() |