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