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