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