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