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