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 | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 7 | """Chromium OS development server that can be used for all forms of update. |
| 8 | |
| 9 | This devserver can be used to perform system-wide autoupdate and update |
| 10 | of specific portage packages on devices running Chromium OS derived operating |
| 11 | systems. It mainly operates in two modes: |
| 12 | |
| 13 | 1) archive mode: In this mode, the devserver is configured to stage and |
| 14 | serve artifacts from Google Storage using the credentials provided to it before |
| 15 | it is run. The easiest way to understand this is that the devserver is |
| 16 | functioning as a local cache for artifacts produced and uploaded by build |
| 17 | servers. Users of this form of devserver can either download the artifacts |
| 18 | from the devservers static directory OR use the update RPC to perform a |
| 19 | system-wide autoupdate. Archive mode is always active. |
| 20 | |
| 21 | 2) artifact-generation mode: in this mode, the devserver will attempt to |
| 22 | generate update payloads and build artifacts when requested. This mode only |
| 23 | works in the Chromium OS chroot as it uses build tools only present in the |
| 24 | chroot (emerge, cros_generate_update_payload, etc.). By default, when a device |
| 25 | requests an update from this form of devserver, the devserver will attempt to |
| 26 | discover if a more recent build of the board has been built by the developer |
| 27 | and generate a payload that the requested system can autoupdate to. In addition, |
| 28 | it accepts gmerge requests from devices that will stage the newest version of |
joychen | 84d1377 | 2013-08-06 16:17:23 | [diff] [blame^] | 29 | a particular package from a developer's chroot onto a requesting device. |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 30 | |
| 31 | For example: |
| 32 | gmerge gmerge -d <devserver_url> |
| 33 | |
| 34 | devserver will see if a newer package of gmerge is available. If gmerge is |
| 35 | cros_work'd on, it will re-build gmerge. After this, gmerge will install that |
| 36 | version of gmerge that the devserver just created/found. |
| 37 | |
| 38 | For autoupdates, there are many more advanced options that can help specify |
| 39 | how to update and which payload to give to a requester. |
| 40 | """ |
| 41 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 42 | |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 43 | import json |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 44 | import optparse |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 45 | import os |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 46 | import re |
Simran Basi | 4baad08 | 2013-02-14 21:39:18 | [diff] [blame] | 47 | import shutil |
Mandeep Singh Baines | 38dcdda | 2012-12-08 01:55:33 | [diff] [blame] | 48 | import socket |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 49 | import subprocess |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 50 | import sys |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 51 | import tempfile |
Dan Shi | 59ae709 | 2013-06-04 21:37:27 | [diff] [blame] | 52 | import threading |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 53 | import types |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 54 | from logging import handlers |
| 55 | |
| 56 | import cherrypy |
| 57 | import cherrypy._cplogging |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 58 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 59 | import autoupdate |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 60 | import common_util |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 61 | import downloader |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 62 | import log_util |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 63 | import xbuddy |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 64 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 65 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 66 | def _Log(message, *args): |
| 67 | return log_util.LogWithTag('DEVSERVER', message, *args) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 68 | |
Frank Farzan | 4016087 | 2011-12-13 02:39:18 | [diff] [blame] | 69 | |
Chris Sosa | 417e55d | 2011-01-26 00:40:48 | [diff] [blame] | 70 | CACHED_ENTRIES = 12 |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 71 | |
Simran Basi | 4baad08 | 2013-02-14 21:39:18 | [diff] [blame] | 72 | TELEMETRY_FOLDER = 'telemetry_src' |
| 73 | TELEMETRY_DEPS = ['dep-telemetry_dep.tar.bz2', |
| 74 | 'dep-page_cycler_dep.tar.bz2', |
Simran Basi | 0d07868 | 2013-03-22 23:40:04 | [diff] [blame] | 75 | 'dep-chrome_test.tar.bz2', |
| 76 | 'dep-perf_data_dep.tar.bz2'] |
Simran Basi | 4baad08 | 2013-02-14 21:39:18 | [diff] [blame] | 77 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 78 | # Sets up global to share between classes. |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 79 | updater = None |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 80 | |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 81 | # Log rotation parameters. These settings correspond to once a week |
J. Richard Barnette | 6dfa534 | 2013-06-04 18:48:56 | [diff] [blame] | 82 | # at midnight between Friday and Saturday, with about three months |
| 83 | # of old logs kept for backup. |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 84 | # |
| 85 | # For more, see the documentation for |
| 86 | # logging.handlers.TimedRotatingFileHandler |
J. Richard Barnette | 6dfa534 | 2013-06-04 18:48:56 | [diff] [blame] | 87 | _LOG_ROTATION_TIME = 'W4' |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 88 | _LOG_ROTATION_BACKUP = 13 |
| 89 | |
Frank Farzan | 4016087 | 2011-12-13 02:39:18 | [diff] [blame] | 90 | |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 91 | class DevServerError(Exception): |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 92 | """Exception class used by this module.""" |
| 93 | pass |
| 94 | |
| 95 | |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 96 | def _LeadingWhiteSpaceCount(string): |
| 97 | """Count the amount of leading whitespace in a string. |
| 98 | |
| 99 | Args: |
| 100 | string: The string to count leading whitespace in. |
| 101 | Returns: |
| 102 | number of white space chars before characters start. |
| 103 | """ |
| 104 | matched = re.match('^\s+', string) |
| 105 | if matched: |
| 106 | return len(matched.group()) |
| 107 | |
| 108 | return 0 |
| 109 | |
| 110 | |
| 111 | def _PrintDocStringAsHTML(func): |
| 112 | """Make a functions docstring somewhat HTML style. |
| 113 | |
| 114 | Args: |
| 115 | func: The function to return the docstring from. |
| 116 | Returns: |
| 117 | A string that is somewhat formated for a web browser. |
| 118 | """ |
| 119 | # TODO(scottz): Make this parse Args/Returns in a prettier way. |
| 120 | # Arguments could be bolded and indented etc. |
| 121 | html_doc = [] |
| 122 | for line in func.__doc__.splitlines(): |
| 123 | leading_space = _LeadingWhiteSpaceCount(line) |
| 124 | if leading_space > 0: |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 125 | line = ' ' * leading_space + line |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 126 | |
| 127 | html_doc.append('<BR>%s' % line) |
| 128 | |
| 129 | return '\n'.join(html_doc) |
| 130 | |
| 131 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 132 | def _GetConfig(options): |
| 133 | """Returns the configuration for the devserver.""" |
Mandeep Singh Baines | 38dcdda | 2012-12-08 01:55:33 | [diff] [blame] | 134 | |
| 135 | # On a system with IPv6 not compiled into the kernel, |
| 136 | # AF_INET6 sockets will return a socket.error exception. |
| 137 | # On such systems, fall-back to IPv4. |
| 138 | socket_host = '::' |
| 139 | try: |
| 140 | socket.socket(socket.AF_INET6, socket.SOCK_STREAM) |
| 141 | except socket.error: |
| 142 | socket_host = '0.0.0.0' |
| 143 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 144 | base_config = { 'global': |
| 145 | { 'server.log_request_headers': True, |
| 146 | 'server.protocol_version': 'HTTP/1.1', |
Mandeep Singh Baines | 38dcdda | 2012-12-08 01:55:33 | [diff] [blame] | 147 | 'server.socket_host': socket_host, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 148 | 'server.socket_port': int(options.port), |
Chris Sosa | 374c62d | 2010-10-14 16:13:54 | [diff] [blame] | 149 | 'response.timeout': 6000, |
Chris Sosa | 6fe2394 | 2012-07-02 22:44:46 | [diff] [blame] | 150 | 'request.show_tracebacks': True, |
Chris Sosa | 72333d1 | 2012-06-13 18:28:05 | [diff] [blame] | 151 | 'server.socket_timeout': 60, |
joychen | ecc02aa | 2013-07-18 01:27:35 | [diff] [blame] | 152 | 'server.thread_pool': 2, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 153 | }, |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 154 | '/api': |
| 155 | { |
| 156 | # Gets rid of cherrypy parsing post file for args. |
| 157 | 'request.process_request_body': False, |
| 158 | }, |
Chris Sosa | a1ef010 | 2010-10-21 23:22:35 | [diff] [blame] | 159 | '/build': |
| 160 | { |
| 161 | 'response.timeout': 100000, |
| 162 | }, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 163 | '/update': |
| 164 | { |
| 165 | # Gets rid of cherrypy parsing post file for args. |
| 166 | 'request.process_request_body': False, |
Chris Sosa | f65f4b9 | 2010-10-21 22:57:51 | [diff] [blame] | 167 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 168 | }, |
| 169 | # Sets up the static dir for file hosting. |
| 170 | '/static': |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 171 | { 'tools.staticdir.dir': options.static_dir, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 172 | 'tools.staticdir.on': True, |
Chris Sosa | f65f4b9 | 2010-10-21 22:57:51 | [diff] [blame] | 173 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 174 | }, |
| 175 | } |
Chris Sosa | 5f118ef | 2012-07-12 18:37:50 | [diff] [blame] | 176 | if options.production: |
Alex Miller | 93beca5 | 2013-07-31 02:25:09 | [diff] [blame] | 177 | base_config['global'].update({'server.thread_pool': 150}) |
Scott Zawalski | 1c5e7cd | 2012-02-27 18:12:52 | [diff] [blame] | 178 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 179 | return base_config |
[email protected] | 6424466 | 2009-11-12 00:52:08 | [diff] [blame] | 180 | |
Darin Petkov | e17164a | 2010-08-11 20:24:41 | [diff] [blame] | 181 | |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 182 | def _GetRecursiveMemberObject(root, member_list): |
| 183 | """Returns an object corresponding to a nested member list. |
| 184 | |
| 185 | Args: |
| 186 | root: the root object to search |
| 187 | member_list: list of nested members to search |
| 188 | Returns: |
| 189 | An object corresponding to the member name list; None otherwise. |
| 190 | """ |
| 191 | for member in member_list: |
| 192 | next_root = root.__class__.__dict__.get(member) |
| 193 | if not next_root: |
| 194 | return None |
| 195 | root = next_root |
| 196 | return root |
| 197 | |
| 198 | |
| 199 | def _IsExposed(name): |
| 200 | """Returns True iff |name| has an `exposed' attribute and it is set.""" |
| 201 | return hasattr(name, 'exposed') and name.exposed |
| 202 | |
| 203 | |
Gilad Arnold | 748c832 | 2012-10-12 16:51:35 | [diff] [blame] | 204 | def _GetExposedMethod(root, nested_member, ignored=None): |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 205 | """Returns a CherryPy-exposed method, if such exists. |
| 206 | |
| 207 | Args: |
| 208 | root: the root object for searching |
| 209 | nested_member: a slash-joined path to the nested member |
| 210 | ignored: method paths to be ignored |
| 211 | Returns: |
| 212 | A function object corresponding to the path defined by |member_list| from |
| 213 | the |root| object, if the function is exposed and not ignored; None |
| 214 | otherwise. |
| 215 | """ |
Gilad Arnold | 748c832 | 2012-10-12 16:51:35 | [diff] [blame] | 216 | method = (not (ignored and nested_member in ignored) and |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 217 | _GetRecursiveMemberObject(root, nested_member.split('/'))) |
| 218 | if (method and type(method) == types.FunctionType and _IsExposed(method)): |
| 219 | return method |
| 220 | |
| 221 | |
Gilad Arnold | 748c832 | 2012-10-12 16:51:35 | [diff] [blame] | 222 | def _FindExposedMethods(root, prefix, unlisted=None): |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 223 | """Finds exposed CherryPy methods. |
| 224 | |
| 225 | Args: |
| 226 | root: the root object for searching |
| 227 | prefix: slash-joined chain of members leading to current object |
| 228 | unlisted: URLs to be excluded regardless of their exposed status |
| 229 | Returns: |
| 230 | List of exposed URLs that are not unlisted. |
| 231 | """ |
| 232 | method_list = [] |
| 233 | for member in sorted(root.__class__.__dict__.keys()): |
| 234 | prefixed_member = prefix + '/' + member if prefix else member |
Gilad Arnold | 748c832 | 2012-10-12 16:51:35 | [diff] [blame] | 235 | if unlisted and prefixed_member in unlisted: |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 236 | continue |
| 237 | member_obj = root.__class__.__dict__[member] |
| 238 | if _IsExposed(member_obj): |
| 239 | if type(member_obj) == types.FunctionType: |
| 240 | method_list.append(prefixed_member) |
| 241 | else: |
| 242 | method_list += _FindExposedMethods( |
| 243 | member_obj, prefixed_member, unlisted) |
| 244 | return method_list |
| 245 | |
| 246 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 247 | class ApiRoot(object): |
| 248 | """RESTful API for Dev Server information.""" |
| 249 | exposed = True |
| 250 | |
| 251 | @cherrypy.expose |
| 252 | def hostinfo(self, ip): |
| 253 | """Returns a JSON dictionary containing information about the given ip. |
| 254 | |
Gilad Arnold | 1b90839 | 2012-10-05 18:36:27 | [diff] [blame] | 255 | Args: |
| 256 | ip: address of host whose info is requested |
| 257 | Returns: |
| 258 | A JSON dictionary containing all or some of the following fields: |
| 259 | last_event_type (int): last update event type received |
| 260 | last_event_status (int): last update event status received |
| 261 | last_known_version (string): last known version reported in update ping |
| 262 | forced_update_label (string): update label to force next update ping to |
| 263 | use, set by setnextupdate |
| 264 | See the OmahaEvent class in update_engine/omaha_request_action.h for |
| 265 | event type and status code definitions. If the ip does not exist an empty |
| 266 | string is returned. |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 267 | |
Gilad Arnold | 1b90839 | 2012-10-05 18:36:27 | [diff] [blame] | 268 | Example URL: |
| 269 | http://myhost/api/hostinfo?ip=192.168.1.5 |
| 270 | """ |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 271 | return updater.HandleHostInfoPing(ip) |
| 272 | |
| 273 | @cherrypy.expose |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 274 | def hostlog(self, ip): |
Gilad Arnold | 1b90839 | 2012-10-05 18:36:27 | [diff] [blame] | 275 | """Returns a JSON object containing a log of host event. |
| 276 | |
| 277 | Args: |
| 278 | ip: address of host whose event log is requested, or `all' |
| 279 | Returns: |
| 280 | A JSON encoded list (log) of dictionaries (events), each of which |
| 281 | containing a `timestamp' and other event fields, as described under |
| 282 | /api/hostinfo. |
| 283 | |
| 284 | Example URL: |
| 285 | http://myhost/api/hostlog?ip=192.168.1.5 |
| 286 | """ |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 287 | return updater.HandleHostLogPing(ip) |
| 288 | |
| 289 | @cherrypy.expose |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 290 | def setnextupdate(self, ip): |
| 291 | """Allows the response to the next update ping from a host to be set. |
| 292 | |
| 293 | Takes the IP of the host and an update label as normally provided to the |
Gilad Arnold | 1b90839 | 2012-10-05 18:36:27 | [diff] [blame] | 294 | /update command. |
| 295 | """ |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 296 | body_length = int(cherrypy.request.headers['Content-Length']) |
| 297 | label = cherrypy.request.rfile.read(body_length) |
| 298 | |
| 299 | if label: |
| 300 | label = label.strip() |
| 301 | if label: |
| 302 | return updater.HandleSetUpdatePing(ip, label) |
| 303 | raise cherrypy.HTTPError(400, 'No label provided.') |
| 304 | |
| 305 | |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 306 | @cherrypy.expose |
| 307 | def fileinfo(self, *path_args): |
| 308 | """Returns information about a given staged file. |
| 309 | |
| 310 | Args: |
| 311 | path_args: path to the file inside the server's static staging directory |
| 312 | Returns: |
| 313 | A JSON encoded dictionary with information about the said file, which may |
| 314 | contain the following keys/values: |
Gilad Arnold | 1b90839 | 2012-10-05 18:36:27 | [diff] [blame] | 315 | size (int): the file size in bytes |
| 316 | sha1 (string): a base64 encoded SHA1 hash |
| 317 | sha256 (string): a base64 encoded SHA256 hash |
| 318 | |
| 319 | Example URL: |
| 320 | http://myhost/api/fileinfo/some/path/to/file |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 321 | """ |
| 322 | file_path = os.path.join(updater.static_dir, *path_args) |
| 323 | if not os.path.exists(file_path): |
| 324 | raise DevServerError('file not found: %s' % file_path) |
| 325 | try: |
| 326 | file_size = os.path.getsize(file_path) |
| 327 | file_sha1 = common_util.GetFileSha1(file_path) |
| 328 | file_sha256 = common_util.GetFileSha256(file_path) |
| 329 | except os.error, e: |
| 330 | raise DevServerError('failed to get info for file %s: %s' % |
Gilad Arnold | e74b381 | 2013-04-22 18:27:38 | [diff] [blame] | 331 | (file_path, e)) |
| 332 | |
| 333 | is_delta = autoupdate.Autoupdate.IsDeltaFormatFile(file_path) |
| 334 | |
| 335 | return json.dumps({ |
| 336 | autoupdate.Autoupdate.SIZE_ATTR: file_size, |
| 337 | autoupdate.Autoupdate.SHA1_ATTR: file_sha1, |
| 338 | autoupdate.Autoupdate.SHA256_ATTR: file_sha256, |
| 339 | autoupdate.Autoupdate.ISDELTA_ATTR: is_delta |
| 340 | }) |
Gilad Arnold | 55a2a37 | 2012-10-02 16:46:32 | [diff] [blame] | 341 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 342 | |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 343 | class DevServerRoot(object): |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 344 | """The Root Class for the Dev Server. |
| 345 | |
| 346 | CherryPy works as follows: |
| 347 | For each method in this class, cherrpy interprets root/path |
| 348 | as a call to an instance of DevServerRoot->method_name. For example, |
| 349 | a call to http://myhost/build will call build. CherryPy automatically |
| 350 | parses http args and places them as keyword arguments in each method. |
| 351 | For paths http://myhost/update/dir1/dir2, you can use *args so that |
| 352 | cherrypy uses the update method and puts the extra paths in args. |
| 353 | """ |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 354 | # Method names that should not be listed on the index page. |
| 355 | _UNLISTED_METHODS = ['index', 'doc'] |
| 356 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 357 | api = ApiRoot() |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 358 | |
Dan Shi | 59ae709 | 2013-06-04 21:37:27 | [diff] [blame] | 359 | # Number of threads that devserver is staging images. |
| 360 | _staging_thread_count = 0 |
| 361 | # Lock used to lock increasing/decreasing count. |
| 362 | _staging_thread_count_lock = threading.Lock() |
| 363 | |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 364 | def __init__(self, _xbuddy): |
Nick Sanders | 7dcaa2e | 2011-08-04 22:20:41 | [diff] [blame] | 365 | self._builder = None |
Simran Basi | 4baad08 | 2013-02-14 21:39:18 | [diff] [blame] | 366 | self._telemetry_lock_dict = common_util.LockDict() |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 367 | self._xbuddy = _xbuddy |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 368 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 369 | @cherrypy.expose |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 370 | def build(self, board, pkg, **kwargs): |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 371 | """Builds the package specified.""" |
Nick Sanders | 7dcaa2e | 2011-08-04 22:20:41 | [diff] [blame] | 372 | import builder |
| 373 | if self._builder is None: |
| 374 | self._builder = builder.Builder() |
David Rochberg | 7c79a81 | 2011-01-19 19:24:45 | [diff] [blame] | 375 | return self._builder.Build(board, pkg, kwargs) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 376 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 377 | @staticmethod |
| 378 | def _canonicalize_archive_url(archive_url): |
| 379 | """Canonicalizes archive_url strings. |
| 380 | |
| 381 | Raises: |
| 382 | DevserverError: if archive_url is not set. |
| 383 | """ |
| 384 | if archive_url: |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 385 | if not archive_url.startswith('gs://'): |
| 386 | raise DevServerError("Archive URL isn't from Google Storage.") |
| 387 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 388 | return archive_url.rstrip('/') |
| 389 | else: |
| 390 | raise DevServerError("Must specify an archive_url in the request") |
| 391 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 392 | @cherrypy.expose |
Frank Farzan | bcb571e | 2012-01-03 19:48:17 | [diff] [blame] | 393 | def download(self, **kwargs): |
| 394 | """Downloads and archives full/delta payloads from Google Storage. |
| 395 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 396 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 397 | This methods downloads artifacts. It may download artifacts in the |
| 398 | background in which case a caller should call wait_for_status to get |
| 399 | the status of the background artifact downloads. They should use the same |
| 400 | args passed to download. |
| 401 | |
Frank Farzan | bcb571e | 2012-01-03 19:48:17 | [diff] [blame] | 402 | Args: |
| 403 | archive_url: Google Storage URL for the build. |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 404 | artifacts: Comma separated list of artifacts to download. |
Frank Farzan | bcb571e | 2012-01-03 19:48:17 | [diff] [blame] | 405 | |
| 406 | Example URL: |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 407 | http://myhost/download?archive_url=gs://chromeos-image-archive/ |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 408 | x86-generic/R17-1208.0.0-a1-b338&artifacts=full_payload,test_suites, |
| 409 | stateful |
Frank Farzan | bcb571e | 2012-01-03 19:48:17 | [diff] [blame] | 410 | """ |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 411 | async = kwargs.get('async', False) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 412 | return self.stage(archive_url=kwargs.get('archive_url'), |
Dan Shi | ffaaf0c | 2013-08-07 04:22:31 | [diff] [blame] | 413 | artifacts='full_payload,test_suites,stateful', |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 414 | async=async) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 415 | |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 416 | @cherrypy.expose |
| 417 | def is_staged(self, **kwargs): |
| 418 | """Check if artifacts have been downloaded. |
| 419 | |
| 420 | @param archive_url: Google Storage URL for the build. |
| 421 | @param artifacts: Comma separated list of artifacts to download. |
| 422 | @returns: True of all artifacts are staged. |
| 423 | |
| 424 | Example: |
| 425 | To check if autotest and test_suites are staged: |
| 426 | http://devserver_url:<port>/is_staged?archive_url=gs://your_url/path& |
| 427 | artifacts=autotest,test_suites |
| 428 | """ |
| 429 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
| 430 | artifacts = kwargs.get('artifacts', '') |
| 431 | if not artifacts: |
| 432 | raise DevServerError('No artifacts specified.') |
| 433 | return str(downloader.Downloader(updater.static_dir, archive_url).IsStaged( |
| 434 | artifacts.split(','))) |
Dan Shi | 59ae709 | 2013-06-04 21:37:27 | [diff] [blame] | 435 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 436 | @cherrypy.expose |
| 437 | def stage(self, **kwargs): |
| 438 | """Downloads and caches the artifacts from Google Storage URL. |
| 439 | |
| 440 | Downloads and caches the artifacts Google Storage URL. Returns once these |
| 441 | have been downloaded on the devserver. A call to this will attempt to cache |
| 442 | non-specified artifacts in the background for the given from the given URL |
| 443 | following the principle of spatial locality. Spatial locality of different |
| 444 | artifacts is explicitly defined in the build_artifact module. |
| 445 | |
| 446 | These artifacts will then be available from the static/ sub-directory of |
| 447 | the devserver. |
| 448 | |
| 449 | Args: |
| 450 | archive_url: Google Storage URL for the build. |
| 451 | artifacts: Comma separated list of artifacts to download. |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 452 | async: True to return without waiting for download to complete. |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 453 | |
| 454 | Example: |
| 455 | To download the autotest and test suites tarballs: |
| 456 | http://devserver_url:<port>/stage?archive_url=gs://your_url/path& |
| 457 | artifacts=autotest,test_suites |
| 458 | To download the full update payload: |
| 459 | http://devserver_url:<port>/stage?archive_url=gs://your_url/path& |
| 460 | artifacts=full_payload |
| 461 | |
| 462 | For both these examples, one could find these artifacts at: |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 463 | http://devserver_url:<port>/static/<relative_path>* |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 464 | |
| 465 | Note for this example, relative path is the archive_url stripped of its |
| 466 | basename i.e. path/ in the examples above. Specific example: |
| 467 | |
| 468 | gs://chromeos-image-archive/x86-mario-release/R26-3920.0.0 |
| 469 | |
| 470 | Will get staged to: |
| 471 | |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 472 | http://devserver_url:<port>/static/x86-mario-release/R26-3920.0.0 |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 473 | """ |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 474 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 475 | artifacts = kwargs.get('artifacts', '') |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 476 | async = kwargs.get('async', False) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 477 | if not artifacts: |
| 478 | raise DevServerError('No artifacts specified.') |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 479 | |
Dan Shi | 59ae709 | 2013-06-04 21:37:27 | [diff] [blame] | 480 | with DevServerRoot._staging_thread_count_lock: |
| 481 | DevServerRoot._staging_thread_count += 1 |
| 482 | try: |
Dan Shi | f8eb0d1 | 2013-08-02 00:52:06 | [diff] [blame] | 483 | downloader.Downloader(updater.static_dir, |
| 484 | archive_url).Download(artifacts.split(','), async=async) |
Dan Shi | 59ae709 | 2013-06-04 21:37:27 | [diff] [blame] | 485 | finally: |
| 486 | with DevServerRoot._staging_thread_count_lock: |
| 487 | DevServerRoot._staging_thread_count -= 1 |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 488 | return 'Success' |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 489 | |
| 490 | @cherrypy.expose |
Simran Basi | 4baad08 | 2013-02-14 21:39:18 | [diff] [blame] | 491 | def setup_telemetry(self, **kwargs): |
| 492 | """Extracts and sets up telemetry |
| 493 | |
| 494 | This method goes through the telemetry deps packages, and stages them on |
| 495 | the devserver to be used by the drones and the telemetry tests. |
| 496 | |
| 497 | Args: |
| 498 | archive_url: Google Storage URL for the build. |
| 499 | |
| 500 | Returns: |
| 501 | Path to the source folder for the telemetry codebase once it is staged. |
| 502 | """ |
| 503 | archive_url = kwargs.get('archive_url') |
| 504 | self.stage(archive_url=archive_url, artifacts='autotest') |
| 505 | |
| 506 | build = '/'.join(downloader.Downloader.ParseUrl(archive_url)) |
| 507 | build_path = os.path.join(updater.static_dir, build) |
| 508 | deps_path = os.path.join(build_path, 'autotest/packages') |
| 509 | telemetry_path = os.path.join(build_path, TELEMETRY_FOLDER) |
| 510 | src_folder = os.path.join(telemetry_path, 'src') |
| 511 | |
| 512 | with self._telemetry_lock_dict.lock(telemetry_path): |
| 513 | if os.path.exists(src_folder): |
| 514 | # Telemetry is already fully stage return |
| 515 | return src_folder |
| 516 | |
| 517 | common_util.MkDirP(telemetry_path) |
| 518 | |
| 519 | # Copy over the required deps tar balls to the telemetry directory. |
| 520 | for dep in TELEMETRY_DEPS: |
| 521 | dep_path = os.path.join(deps_path, dep) |
Simran Basi | 0d07868 | 2013-03-22 23:40:04 | [diff] [blame] | 522 | if not os.path.exists(dep_path): |
| 523 | # This dep does not exist (could be new), do not extract it. |
| 524 | continue |
Simran Basi | 4baad08 | 2013-02-14 21:39:18 | [diff] [blame] | 525 | try: |
| 526 | common_util.ExtractTarball(dep_path, telemetry_path) |
| 527 | except common_util.CommonUtilError as e: |
| 528 | shutil.rmtree(telemetry_path) |
| 529 | raise DevServerError(str(e)) |
| 530 | |
| 531 | # By default all the tarballs extract to test_src but some parts of |
| 532 | # the telemetry code specifically hardcoded to exist inside of 'src'. |
| 533 | test_src = os.path.join(telemetry_path, 'test_src') |
| 534 | try: |
| 535 | shutil.move(test_src, src_folder) |
| 536 | except shutil.Error: |
| 537 | # This can occur if src_folder already exists. Remove and retry move. |
| 538 | shutil.rmtree(src_folder) |
| 539 | raise DevServerError('Failure in telemetry setup for build %s. Appears' |
| 540 | ' that the test_src to src move failed.' % build) |
| 541 | |
| 542 | return src_folder |
| 543 | |
| 544 | @cherrypy.expose |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 545 | def wait_for_status(self, **kwargs): |
| 546 | """Waits for background artifacts to be downloaded from Google Storage. |
| 547 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 548 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 549 | Args: |
| 550 | archive_url: Google Storage URL for the build. |
| 551 | |
| 552 | Example URL: |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 553 | http://myhost/wait_for_status?archive_url=gs://chromeos-image-archive/ |
| 554 | x86-generic/R17-1208.0.0-a1-b338 |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 555 | """ |
Dan Shi | ffaaf0c | 2013-08-07 04:22:31 | [diff] [blame] | 556 | async = kwargs.get('async', False) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 557 | return self.stage(archive_url=kwargs.get('archive_url'), |
Dan Shi | ffaaf0c | 2013-08-07 04:22:31 | [diff] [blame] | 558 | artifacts='full_payload,test_suites,autotest,stateful', |
| 559 | async=async) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 560 | |
| 561 | @cherrypy.expose |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 562 | def stage_debug(self, **kwargs): |
| 563 | """Downloads and stages debug symbol payloads from Google Storage. |
| 564 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 565 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
| 566 | This methods downloads the debug symbol build artifact |
| 567 | synchronously, and then stages it for use by symbolicate_dump. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 568 | |
| 569 | Args: |
| 570 | archive_url: Google Storage URL for the build. |
| 571 | |
| 572 | Example URL: |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 573 | http://myhost/stage_debug?archive_url=gs://chromeos-image-archive/ |
| 574 | x86-generic/R17-1208.0.0-a1-b338 |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 575 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 576 | return self.stage(archive_url=kwargs.get('archive_url'), |
| 577 | artifacts='symbols') |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 578 | |
| 579 | @cherrypy.expose |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 580 | def symbolicate_dump(self, minidump, **kwargs): |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 581 | """Symbolicates a minidump using pre-downloaded symbols, returns it. |
| 582 | |
| 583 | Callers will need to POST to this URL with a body of MIME-type |
| 584 | "multipart/form-data". |
| 585 | The body should include a single argument, 'minidump', containing the |
| 586 | binary-formatted minidump to symbolicate. |
| 587 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 588 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 589 | archive_url: Google Storage URL for the build. |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 590 | minidump: The binary minidump file to symbolicate. |
| 591 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 592 | # Ensure the symbols have been staged. |
| 593 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
| 594 | if self.stage(archive_url=archive_url, artifacts='symbols') != 'Success': |
| 595 | raise DevServerError('Failed to stage symbols for %s' % archive_url) |
| 596 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 597 | to_return = '' |
| 598 | with tempfile.NamedTemporaryFile() as local: |
| 599 | while True: |
| 600 | data = minidump.file.read(8192) |
| 601 | if not data: |
| 602 | break |
| 603 | local.write(data) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 604 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 605 | local.flush() |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 606 | |
| 607 | symbols_directory = os.path.join(downloader.Downloader.GetBuildDir( |
| 608 | updater.static_dir, archive_url), 'debug', 'breakpad') |
| 609 | |
| 610 | stackwalk = subprocess.Popen( |
| 611 | ['minidump_stackwalk', local.name, symbols_directory], |
| 612 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 613 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 614 | to_return, error_text = stackwalk.communicate() |
| 615 | if stackwalk.returncode != 0: |
| 616 | raise DevServerError("Can't generate stack trace: %s (rc=%d)" % ( |
| 617 | error_text, stackwalk.returncode)) |
| 618 | |
| 619 | return to_return |
| 620 | |
| 621 | @cherrypy.expose |
Scott Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 622 | def latestbuild(self, **params): |
| 623 | """Return a string representing the latest build for a given target. |
| 624 | |
| 625 | Args: |
| 626 | target: The build target, typically a combination of the board and the |
| 627 | type of build e.g. x86-mario-release. |
| 628 | milestone: The milestone to filter builds on. E.g. R16. Optional, if not |
| 629 | provided the latest RXX build will be returned. |
| 630 | Returns: |
| 631 | A string representation of the latest build if one exists, i.e. |
| 632 | R19-1993.0.0-a1-b1480. |
| 633 | An empty string if no latest could be found. |
| 634 | """ |
| 635 | if not params: |
| 636 | return _PrintDocStringAsHTML(self.latestbuild) |
| 637 | |
| 638 | if 'target' not in params: |
| 639 | raise cherrypy.HTTPError('500 Internal Server Error', |
| 640 | 'Error: target= is required!') |
| 641 | try: |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 642 | return common_util.GetLatestBuildVersion( |
Scott Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 643 | updater.static_dir, params['target'], |
| 644 | milestone=params.get('milestone')) |
Gilad Arnold | 17fe03d | 2012-10-02 17:05:01 | [diff] [blame] | 645 | except common_util.CommonUtilError as errmsg: |
Scott Zawalski | 1695453 | 2012-03-20 19:31:36 | [diff] [blame] | 646 | raise cherrypy.HTTPError('500 Internal Server Error', str(errmsg)) |
| 647 | |
| 648 | @cherrypy.expose |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 649 | def controlfiles(self, **params): |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 650 | """Return a control file or a list of all known control files. |
| 651 | |
| 652 | Example URL: |
| 653 | To List all control files: |
beeps | bd33724 | 2013-07-10 05:44:06 | [diff] [blame] | 654 | http://dev-server/controlfiles?suite_name=&build=daisy_spring-release/R29-4279.0.0 |
| 655 | To List all control files for, say, the bvt suite: |
| 656 | http://dev-server/controlfiles?suite_name=bvt&build=daisy_spring-release/R29-4279.0.0 |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 657 | To return the contents of a path: |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 658 | 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] | 659 | |
| 660 | Args: |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 661 | 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] | 662 | control_path: If you want the contents of a control file set this |
| 663 | to the path. E.g. client/site_tests/sleeptest/control |
| 664 | Optional, if not provided return a list of control files is returned. |
beeps | bd33724 | 2013-07-10 05:44:06 | [diff] [blame] | 665 | suite_name: If control_path is not specified but a suite_name is |
| 666 | specified, list the control files belonging to that suite instead of |
| 667 | all control files. The empty string for suite_name will list all control |
| 668 | files for the build. |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 669 | Returns: |
| 670 | Contents of a control file if control_path is provided. |
| 671 | A list of control files if no control_path is provided. |
| 672 | """ |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 673 | if not params: |
| 674 | return _PrintDocStringAsHTML(self.controlfiles) |
| 675 | |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 676 | if 'build' not in params: |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 677 | raise cherrypy.HTTPError('500 Internal Server Error', |
Scott Zawalski | 84a39c9 | 2012-01-13 20:12:42 | [diff] [blame] | 678 | 'Error: build= is required!') |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 679 | |
| 680 | if 'control_path' not in params: |
beeps | bd33724 | 2013-07-10 05:44:06 | [diff] [blame] | 681 | if 'suite_name' in params and params['suite_name']: |
| 682 | return common_util.GetControlFileListForSuite( |
| 683 | updater.static_dir, params['build'], params['suite_name']) |
| 684 | else: |
| 685 | return common_util.GetControlFileList( |
| 686 | updater.static_dir, params['build']) |
Scott Zawalski | 4647ce6 | 2012-01-03 22:17:28 | [diff] [blame] | 687 | else: |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 688 | return common_util.GetControlFile( |
| 689 | updater.static_dir, params['build'], params['control_path']) |
Frank Farzan | 4016087 | 2011-12-13 02:39:18 | [diff] [blame] | 690 | |
| 691 | @cherrypy.expose |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 692 | def stage_images(self, **kwargs): |
| 693 | """Downloads and stages a Chrome OS image from Google Storage. |
| 694 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 695 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 696 | This method downloads a zipped archive from a specified GS location, then |
| 697 | extracts and stages the specified list of images and stages them under |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 698 | static/BOARD/BUILD/. Download is synchronous. |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 699 | |
| 700 | Args: |
| 701 | archive_url: Google Storage URL for the build. |
| 702 | image_types: comma-separated list of images to download, may include |
| 703 | 'test', 'recovery', and 'base' |
| 704 | |
| 705 | Example URL: |
| 706 | http://myhost/stage_images?archive_url=gs://chromeos-image-archive/ |
| 707 | x86-generic/R17-1208.0.0-a1-b338&image_types=test,base |
| 708 | """ |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 709 | image_types = kwargs.get('image_types').split(',') |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 710 | image_types_list = [image + '_image' for image in image_types] |
| 711 | self.stage(archive_url=kwargs.get('archive_url'), artifacts=','.join( |
| 712 | image_types_list)) |
Gilad Arnold | 6f99b98 | 2012-09-12 17:49:40 | [diff] [blame] | 713 | |
| 714 | @cherrypy.expose |
joychen | eaf4cfc | 2013-07-02 15:38:57 | [diff] [blame] | 715 | def xbuddy(self, *args, **kwargs): |
| 716 | """The full xBuddy call, returns resource specified by path_parts. |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 717 | |
| 718 | Args: |
joychen | eaf4cfc | 2013-07-02 15:38:57 | [diff] [blame] | 719 | path_parts: the path following xbuddy/ in the call url is split into the |
| 720 | components of the path. |
| 721 | The path can be understood as a build_id/artifact, build_id is |
| 722 | composed of "board/version" |
| 723 | |
| 724 | path_parts[0], the board, is the familiar board name, optionally |
| 725 | suffixed. |
| 726 | path_parts[1], the version, can be the google storage version |
| 727 | number, and may also be any of a number of xBuddy defined version |
| 728 | aliases that will be translated into the latest built image that |
| 729 | fits the description. defaults to latest. |
| 730 | path_parts[2], the artifact, is one of a number of image or artifact |
| 731 | aliases used by xbuddy, defined in xbuddy:ALIASES. Defaults to test |
| 732 | |
| 733 | Kwargs: |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 734 | return_dir: {true|false} |
| 735 | if set to true, returns the url to the update.gz |
| 736 | instead. |
| 737 | |
| 738 | Example URL: |
joychen | eaf4cfc | 2013-07-02 15:38:57 | [diff] [blame] | 739 | http://host:port/xbuddy/x86-generic/R26-4000.0.0/test |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 740 | or |
joychen | eaf4cfc | 2013-07-02 15:38:57 | [diff] [blame] | 741 | http://host:port/xbuddy/x86-generic/R26-4000.0.0/test?return_dir=true |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 742 | |
| 743 | Returns: |
| 744 | A redirect to the image or update file on the devserver. |
| 745 | e.g. http://host:port/static/archive/x86-generic-release/ |
| 746 | R26-4000.0.0/chromium-test-image.bin |
| 747 | or if return_dir is True, return path to the folder where |
| 748 | image or update file is |
| 749 | http://host:port/static/x86-generic-release/R26-4000.0.0/ |
| 750 | """ |
| 751 | boolean_string = kwargs.get('return_dir') |
| 752 | return_dir = xbuddy.XBuddy.ParseBoolean(boolean_string) |
joychen | eaf4cfc | 2013-07-02 15:38:57 | [diff] [blame] | 753 | return_url = self._xbuddy.Get(args, |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 754 | return_dir) |
| 755 | if return_dir: |
joychen | eaf4cfc | 2013-07-02 15:38:57 | [diff] [blame] | 756 | directory = os.path.join(cherrypy.request.base, return_url) |
| 757 | _Log("Directory requested, returning: %s", directory) |
| 758 | return directory |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 759 | else: |
joychen | eaf4cfc | 2013-07-02 15:38:57 | [diff] [blame] | 760 | return_url = '/' + return_url |
| 761 | _Log("Payload requested, returning: %s", return_url) |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 762 | raise cherrypy.HTTPRedirect(return_url, 302) |
| 763 | |
| 764 | @cherrypy.expose |
| 765 | def xbuddy_list(self): |
| 766 | """Lists the currently available images & time since last access. |
| 767 | |
| 768 | @return: A string representation of a list of tuples |
| 769 | [(build_id, time since last access),...] |
| 770 | """ |
| 771 | return self._xbuddy.List() |
| 772 | |
| 773 | @cherrypy.expose |
| 774 | def xbuddy_capacity(self): |
| 775 | """Returns the number of images cached by xBuddy. |
| 776 | |
| 777 | @return: Capacity of this devserver. |
| 778 | """ |
| 779 | return self._xbuddy.Capacity() |
| 780 | |
| 781 | @cherrypy.expose |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 782 | def index(self): |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 783 | """Presents a welcome message and documentation links.""" |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 784 | return ('Welcome to the Dev Server!<br>\n' |
| 785 | '<br>\n' |
| 786 | 'Here are the available methods, click for documentation:<br>\n' |
| 787 | '<br>\n' |
| 788 | '%s' % |
| 789 | '<br>\n'.join( |
| 790 | [('<a href=doc/%s>%s</a>' % (name, name)) |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 791 | for name in _FindExposedMethods( |
| 792 | self, '', unlisted=self._UNLISTED_METHODS)])) |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 793 | |
| 794 | @cherrypy.expose |
| 795 | def doc(self, *args): |
| 796 | """Shows the documentation for available methods / URLs. |
| 797 | |
| 798 | Example: |
| 799 | http://myhost/doc/update |
| 800 | """ |
Gilad Arnold | d5ebaaa | 2012-10-02 18:52:38 | [diff] [blame] | 801 | name = '/'.join(args) |
| 802 | method = _GetExposedMethod(self, name) |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 803 | if not method: |
| 804 | raise DevServerError("No exposed method named `%s'" % name) |
| 805 | if not method.__doc__: |
| 806 | raise DevServerError("No documentation for exposed method `%s'" % name) |
| 807 | return '<pre>\n%s</pre>' % method.__doc__ |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 808 | |
Dale Curtis | c9aaf3a | 2011-08-09 22:47:40 | [diff] [blame] | 809 | @cherrypy.expose |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 810 | def update(self, *args): |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 811 | """Handles an update check from a Chrome OS client. |
| 812 | |
| 813 | The HTTP request should contain the standard Omaha-style XML blob. The URL |
| 814 | line may contain an additional intermediate path to the update payload. |
| 815 | |
joychen | b0dfe55 | 2013-07-30 17:02:06 | [diff] [blame] | 816 | Paths that can be handled by xbuddy are formatted: |
| 817 | http://myhost/update/xbuddy/board/version |
| 818 | |
Gilad Arnold | f8f769f | 2012-09-24 15:43:01 | [diff] [blame] | 819 | Example: |
| 820 | http://myhost/update/optional/path/to/payload |
| 821 | """ |
joychen | 346531c | 2013-07-24 23:55:56 | [diff] [blame] | 822 | if len(args) > 0 and args[0] == 'xbuddy': |
| 823 | # Interpret the rest of the path as an xbuddy path |
| 824 | label, found = self._xbuddy.Translate(args[1:] + ('full_payload',)) |
| 825 | if not found: |
| 826 | _Log("Update payload not found for %s, xBuddy looking it up.", label) |
| 827 | else: |
| 828 | label = '/'.join(args) |
| 829 | |
| 830 | _Log('Update label: %s', label) |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 831 | body_length = int(cherrypy.request.headers.get('Content-Length', 0)) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 832 | data = cherrypy.request.rfile.read(body_length) |
| 833 | return updater.HandleUpdatePing(data, label) |
| 834 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 835 | |
Dan Shi | f5ce2de | 2013-04-25 23:06:32 | [diff] [blame] | 836 | @cherrypy.expose |
| 837 | def check_health(self): |
| 838 | """Collect the health status of devserver to see if it's ready for staging. |
| 839 | |
| 840 | @return: A JSON dictionary containing all or some of the following fields: |
Dan Shi | 59ae709 | 2013-06-04 21:37:27 | [diff] [blame] | 841 | free_disk (int): free disk space in GB |
| 842 | staging_thread_count (int): number of devserver threads currently |
| 843 | staging an image |
Dan Shi | f5ce2de | 2013-04-25 23:06:32 | [diff] [blame] | 844 | """ |
| 845 | # Get free disk space. |
| 846 | stat = os.statvfs(updater.static_dir) |
| 847 | free_disk = stat.f_bsize * stat.f_bavail / 1000000000 |
| 848 | |
| 849 | return json.dumps({ |
| 850 | 'free_disk': free_disk, |
Dan Shi | 59ae709 | 2013-06-04 21:37:27 | [diff] [blame] | 851 | 'staging_thread_count': DevServerRoot._staging_thread_count, |
Dan Shi | f5ce2de | 2013-04-25 23:06:32 | [diff] [blame] | 852 | }) |
| 853 | |
| 854 | |
Chris Sosa | dbc2008 | 2012-12-10 21:39:11 | [diff] [blame] | 855 | def _CleanCache(cache_dir, wipe): |
| 856 | """Wipes any excess cached items in the cache_dir. |
| 857 | |
| 858 | Args: |
| 859 | cache_dir: the directory we are wiping from. |
| 860 | wipe: If True, wipe all the contents -- not just the excess. |
| 861 | """ |
| 862 | if wipe: |
| 863 | # Clear the cache and exit on error. |
| 864 | cmd = 'rm -rf %s/*' % cache_dir |
| 865 | if os.system(cmd) != 0: |
| 866 | _Log('Failed to clear the cache with %s' % cmd) |
| 867 | sys.exit(1) |
| 868 | else: |
| 869 | # Clear all but the last N cached updates |
| 870 | cmd = ('cd %s; ls -tr | head --lines=-%d | xargs rm -rf' % |
| 871 | (cache_dir, CACHED_ENTRIES)) |
| 872 | if os.system(cmd) != 0: |
| 873 | _Log('Failed to clean up old delta cache files with %s' % cmd) |
| 874 | sys.exit(1) |
| 875 | |
| 876 | |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 877 | def _AddTestingOptions(parser): |
| 878 | group = optparse.OptionGroup( |
| 879 | parser, 'Advanced Testing Options', 'These are used by test scripts and ' |
| 880 | 'developers writing integration tests utilizing the devserver. They are ' |
| 881 | 'not intended to be really used outside the scope of someone ' |
| 882 | 'knowledgable about the test.') |
| 883 | group.add_option('--exit', |
| 884 | action='store_true', |
| 885 | help='do not start the server (yet pregenerate/clear cache)') |
| 886 | group.add_option('--host_log', |
| 887 | action='store_true', default=False, |
| 888 | help='record history of host update events (/api/hostlog)') |
| 889 | group.add_option('--max_updates', |
| 890 | metavar='NUM', default= -1, type='int', |
| 891 | help='maximum number of update checks handled positively ' |
| 892 | '(default: unlimited)') |
| 893 | group.add_option('--private_key', |
| 894 | metavar='PATH', default=None, |
| 895 | help='path to the private key in pem format. If this is set ' |
| 896 | 'the devserver will generate update payloads that are ' |
| 897 | 'signed with this key.') |
| 898 | group.add_option('--proxy_port', |
| 899 | metavar='PORT', default=None, type='int', |
| 900 | help='port to have the client connect to -- basically the ' |
| 901 | 'devserver lies to the update to tell it to get the payload ' |
| 902 | 'from a different port that will proxy the request back to ' |
| 903 | 'the devserver. The proxy must be managed outside the ' |
| 904 | 'devserver.') |
| 905 | group.add_option('--remote_payload', |
| 906 | action='store_true', default=False, |
| 907 | help='Payload is being served from a remote machine') |
| 908 | group.add_option('-u', '--urlbase', |
| 909 | metavar='URL', |
| 910 | help='base URL for update images, other than the ' |
| 911 | 'devserver. Use in conjunction with remote_payload.') |
| 912 | parser.add_option_group(group) |
| 913 | |
| 914 | |
| 915 | def _AddUpdateOptions(parser): |
| 916 | group = optparse.OptionGroup( |
| 917 | parser, 'Autoupdate Options', 'These options can be used to change ' |
| 918 | 'how the devserver either generates or serve update payloads. Please ' |
| 919 | 'note that all of these option affect how a payload is generated and so ' |
| 920 | 'do not work in archive-only mode.') |
| 921 | group.add_option('--board', |
| 922 | help='By default the devserver will create an update ' |
| 923 | 'payload from the latest image built for the board ' |
| 924 | 'a device that is requesting an update has. When we ' |
| 925 | 'pre-generate an update (see below) and we do not specify ' |
| 926 | 'another update_type option like image or payload, the ' |
| 927 | 'devserver needs to know the board to generate the latest ' |
| 928 | 'image for. This is that board.') |
| 929 | group.add_option('--critical_update', |
| 930 | action='store_true', default=False, |
| 931 | help='Present update payload as critical') |
| 932 | group.add_option('--for_vm', |
| 933 | dest='vm', action='store_true', |
| 934 | help='DEPRECATED: see no_patch_kernel.') |
| 935 | group.add_option('--image', |
| 936 | metavar='FILE', |
| 937 | help='Generate and serve an update using this image to any ' |
| 938 | 'device that requests an update.') |
| 939 | group.add_option('--no_patch_kernel', |
| 940 | dest='patch_kernel', action='store_false', default=True, |
| 941 | help='When generating an update payload, do not patch the ' |
| 942 | 'kernel with kernel verification blob from the stateful ' |
| 943 | 'partition.') |
| 944 | group.add_option('--payload', |
| 945 | metavar='PATH', |
| 946 | help='use the update payload from specified directory ' |
| 947 | '(update.gz).') |
| 948 | group.add_option('-p', '--pregenerate_update', |
| 949 | action='store_true', default=False, |
| 950 | help='pre-generate the update payload before accepting ' |
| 951 | 'update requests. Useful to help debug payload generation ' |
| 952 | 'issues quickly. Also if an update payload will take a ' |
| 953 | 'long time to generate, a client may timeout if you do not' |
| 954 | 'pregenerate the update.') |
| 955 | group.add_option('--src_image', |
| 956 | metavar='PATH', default='', |
| 957 | help='If specified, delta updates will be generated using ' |
| 958 | 'this image as the source image. Delta updates are when ' |
| 959 | 'you are updating from a "source image" to a another ' |
| 960 | 'image.') |
| 961 | parser.add_option_group(group) |
| 962 | |
| 963 | |
| 964 | def _AddProductionOptions(parser): |
| 965 | group = optparse.OptionGroup( |
| 966 | parser, 'Advanced Server Options', 'These options can be used to changed ' |
| 967 | 'for advanced server behavior.') |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 968 | group.add_option('--clear_cache', |
| 969 | action='store_true', default=False, |
| 970 | help='At startup, removes all cached entries from the' |
| 971 | 'devserver\'s cache.') |
| 972 | group.add_option('--logfile', |
| 973 | metavar='PATH', |
| 974 | help='log output to this file instead of stdout') |
| 975 | group.add_option('--production', |
| 976 | action='store_true', default=False, |
| 977 | help='have the devserver use production values when ' |
| 978 | 'starting up. This includes using more threads and ' |
| 979 | 'performing less logging.') |
| 980 | parser.add_option_group(group) |
| 981 | |
| 982 | |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 983 | def _MakeLogHandler(logfile): |
| 984 | """Create a LogHandler instance used to log all messages.""" |
| 985 | hdlr_cls = handlers.TimedRotatingFileHandler |
| 986 | hdlr = hdlr_cls(logfile, when=_LOG_ROTATION_TIME, |
| 987 | backupCount=_LOG_ROTATION_BACKUP) |
| 988 | # The cherrypy documentation says to use the _cplogging module for |
| 989 | # this, even though it's named as a private module. |
| 990 | # pylint: disable=W0212 |
| 991 | hdlr.setFormatter(cherrypy._cplogging.logfmt) |
| 992 | return hdlr |
| 993 | |
| 994 | |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 995 | def main(): |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 996 | usage = '\n\n'.join(['usage: %prog [options]', __doc__]) |
Gilad Arnold | 286a006 | 2012-01-12 21:47:02 | [diff] [blame] | 997 | parser = optparse.OptionParser(usage=usage) |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 998 | |
| 999 | # get directory that the devserver is run from |
| 1000 | devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
joychen | 84d1377 | 2013-08-06 16:17:23 | [diff] [blame^] | 1001 | default_static_dir = '%s/static' % devserver_dir |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 1002 | parser.add_option('--static_dir', |
Gilad Arnold | 9714d9b | 2012-10-04 17:09:42 | [diff] [blame] | 1003 | metavar='PATH', |
joychen | 84d1377 | 2013-08-06 16:17:23 | [diff] [blame^] | 1004 | default=default_static_dir, |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 1005 | help='writable static directory') |
Gilad Arnold | 9714d9b | 2012-10-04 17:09:42 | [diff] [blame] | 1006 | parser.add_option('--port', |
| 1007 | default=8080, type='int', |
| 1008 | help='port for the dev server to use (default: 8080)') |
Gilad Arnold | 9714d9b | 2012-10-04 17:09:42 | [diff] [blame] | 1009 | parser.add_option('-t', '--test_image', |
| 1010 | action='store_true', |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 1011 | help='If set, look for the chromiumos_test_image.bin file ' |
| 1012 | 'when generating update payloads rather than the ' |
| 1013 | 'chromiumos_image.bin which is the default.') |
joychen | 5260b9a | 2013-07-16 21:48:01 | [diff] [blame] | 1014 | parser.add_option('-x', '--xbuddy_manage_builds', |
| 1015 | action='store_true', |
| 1016 | default=False, |
| 1017 | help='If set, allow xbuddy to manage images in' |
| 1018 | 'build/images.') |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 1019 | _AddProductionOptions(parser) |
| 1020 | _AddUpdateOptions(parser) |
| 1021 | _AddTestingOptions(parser) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 1022 | (options, _) = parser.parse_args() |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 1023 | |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 1024 | # Handle options that must be set globally in cherrypy. Do this |
| 1025 | # work up front, because calls to _Log() below depend on this |
| 1026 | # initialization. |
| 1027 | if options.production: |
| 1028 | cherrypy.config.update({'environment': 'production'}) |
| 1029 | if not options.logfile: |
| 1030 | cherrypy.config.update({'log.screen': True}) |
| 1031 | else: |
| 1032 | cherrypy.config.update({'log.error_file': '', |
| 1033 | 'log.access_file': ''}) |
| 1034 | hdlr = _MakeLogHandler(options.logfile) |
| 1035 | # Pylint can't seem to process these two calls properly |
| 1036 | # pylint: disable=E1101 |
| 1037 | cherrypy.log.access_log.addHandler(hdlr) |
| 1038 | cherrypy.log.error_log.addHandler(hdlr) |
| 1039 | # pylint: enable=E1101 |
| 1040 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 1041 | root_dir = os.path.realpath('%s/../..' % devserver_dir) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 1042 | |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 1043 | # TODO(sosa): Remove after deprecation. |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 1044 | if options.vm: |
| 1045 | options.patch_kernel = False |
| 1046 | |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 1047 | # set static_dir, from which everything will be served |
joychen | 84d1377 | 2013-08-06 16:17:23 | [diff] [blame^] | 1048 | options.static_dir = os.path.realpath(options.static_dir) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 1049 | |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 1050 | cache_dir = os.path.join(options.static_dir, 'cache') |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 1051 | # If our devserver is only supposed to serve payloads, we shouldn't be |
| 1052 | # mucking with the cache at all. If the devserver hadn't previously |
| 1053 | # generated a cache and is expected, the caller is using it wrong. |
joychen | 7c2054a | 2013-07-25 18:14:07 | [diff] [blame] | 1054 | if os.path.exists(cache_dir): |
Chris Sosa | dbc2008 | 2012-12-10 21:39:11 | [diff] [blame] | 1055 | _CleanCache(cache_dir, options.clear_cache) |
Chris Sosa | 6b8c374 | 2011-01-31 20:12:17 | [diff] [blame] | 1056 | else: |
| 1057 | os.makedirs(cache_dir) |
Don Garrett | f90edf0 | 2010-11-17 01:36:14 | [diff] [blame] | 1058 | |
Chris Sosa | dbc2008 | 2012-12-10 21:39:11 | [diff] [blame] | 1059 | _Log('Using cache directory %s' % cache_dir) |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 1060 | _Log('Source root is %s' % root_dir) |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 1061 | _Log('Serving from %s' % options.static_dir) |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 1062 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 1063 | # We allow global use here to share with cherrypy classes. |
| 1064 | # pylint: disable=W0603 |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 1065 | global updater |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 1066 | updater = autoupdate.Autoupdate( |
| 1067 | root_dir=root_dir, |
joychen | ed64b22 | 2013-06-21 23:39:34 | [diff] [blame] | 1068 | static_dir=options.static_dir, |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 1069 | urlbase=options.urlbase, |
| 1070 | test_image=options.test_image, |
Chris Sosa | 5d342a2 | 2010-09-28 23:54:41 | [diff] [blame] | 1071 | forced_image=options.image, |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 1072 | payload_path=options.payload, |
Don Garrett | 0ad0937 | 2010-12-07 00:20:30 | [diff] [blame] | 1073 | proxy_port=options.proxy_port, |
Chris Sosa | 4136e69 | 2010-10-29 06:42:37 | [diff] [blame] | 1074 | src_image=options.src_image, |
Chris Sosa | 3ae4dc1 | 2013-03-29 18:47:00 | [diff] [blame] | 1075 | patch_kernel=options.patch_kernel, |
Chris Sosa | 08d55a2 | 2011-01-20 00:08:02 | [diff] [blame] | 1076 | board=options.board, |
Chris Sosa | 0f1ec84 | 2011-02-15 00:33:22 | [diff] [blame] | 1077 | copy_to_static_root=not options.exit, |
| 1078 | private_key=options.private_key, |
Satoru Takabayashi | d733cbe | 2011-11-15 17:36:32 | [diff] [blame] | 1079 | critical_update=options.critical_update, |
Gilad Arnold | 0c9c860 | 2012-10-03 06:58:58 | [diff] [blame] | 1080 | remote_payload=options.remote_payload, |
Gilad Arnold | a564b4b | 2012-10-04 17:32:44 | [diff] [blame] | 1081 | max_updates=options.max_updates, |
Gilad Arnold | 8318eac | 2012-10-04 19:52:23 | [diff] [blame] | 1082 | host_log=options.host_log, |
Chris Sosa | 0f1ec84 | 2011-02-15 00:33:22 | [diff] [blame] | 1083 | ) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 1084 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 1085 | if options.pregenerate_update: |
| 1086 | updater.PreGenerateUpdate() |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 1087 | |
J. Richard Barnette | 3d977b8 | 2013-04-23 18:05:19 | [diff] [blame] | 1088 | if options.exit: |
| 1089 | return |
Chris Sosa | 2f1c41e | 2012-07-10 21:32:33 | [diff] [blame] | 1090 | |
joychen | 5260b9a | 2013-07-16 21:48:01 | [diff] [blame] | 1091 | _xbuddy = xbuddy.XBuddy(options.xbuddy_manage_builds, |
joychen | b0dfe55 | 2013-07-30 17:02:06 | [diff] [blame] | 1092 | options.board, |
joychen | 5260b9a | 2013-07-16 21:48:01 | [diff] [blame] | 1093 | root_dir=root_dir, |
joychen | b0dfe55 | 2013-07-30 17:02:06 | [diff] [blame] | 1094 | static_dir=options.static_dir, |
| 1095 | ) |
joychen | 3cb228e | 2013-06-12 19:13:13 | [diff] [blame] | 1096 | dev_server = DevServerRoot(_xbuddy) |
| 1097 | |
| 1098 | cherrypy.quickstart(dev_server, config=_GetConfig(options)) |
Chris Sosa | cde6bf4 | 2012-06-01 01:36:39 | [diff] [blame] | 1099 | |
| 1100 | |
| 1101 | if __name__ == '__main__': |
| 1102 | main() |