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