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