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