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