Keith Haddow | 58f36d1 | 2020-10-28 16:16:39 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Luis Hector Chavez | dca9dd7 | 2018-06-12 12:56:30 -0700 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 3 | # Copyright (c) 2009-2012 The Chromium OS Authors. All rights reserved. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 7 | """Chromium OS development server that can be used for all forms of update. |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 8 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 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. |
| 12 | |
| 13 | 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 | For autoupdates, there are many more advanced options that can help specify |
| 22 | how to update and which payload to give to a requester. |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 23 | """ |
| 24 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 25 | from __future__ import print_function |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 26 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 27 | import json |
David Riley | 2fcb012 | 2017-11-02 11:25:39 -0700 | [diff] [blame] | 28 | import optparse # pylint: disable=deprecated-module |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 29 | import os |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 30 | import re |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 31 | import shutil |
Mandeep Singh Baines | 38dcdda | 2012-12-07 17:55:33 -0800 | [diff] [blame] | 32 | import socket |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 33 | import subprocess |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 34 | import sys |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 35 | import tempfile |
Dan Shi | 59ae709 | 2013-06-04 14:37:27 -0700 | [diff] [blame] | 36 | import threading |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 37 | import types |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 38 | from logging import handlers |
| 39 | |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 40 | from six.moves import http_client |
| 41 | |
| 42 | # pylint: disable=no-name-in-module, import-error |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 43 | import cherrypy |
Chris Sosa | 855b893 | 2013-08-21 13:24:55 -0700 | [diff] [blame] | 44 | from cherrypy import _cplogging as cplogging |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 45 | from cherrypy.process import plugins |
| 46 | # pylint: enable=no-name-in-module, import-error |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 47 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 48 | import autoupdate |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 49 | import cherrypy_ext |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 50 | import health_checker |
| 51 | |
Richard Barnette | df35c32 | 2017-08-18 17:02:13 -0700 | [diff] [blame] | 52 | # This must happen before any local modules get a chance to import |
| 53 | # anything from chromite. Otherwise, really bad things will happen, and |
| 54 | # you will _not_ understand why. |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 55 | import setup_chromite # pylint: disable=unused-import |
Eliot Courtney | f39420b | 2020-10-27 18:34:04 +0900 | [diff] [blame] | 56 | from chromite.lib import cros_build_lib |
Amin Hassani | 3587fb3 | 2021-04-28 10:10:01 -0700 | [diff] [blame^] | 57 | from chromite.lib import cros_logging as logging |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 58 | from chromite.lib.xbuddy import android_build |
| 59 | from chromite.lib.xbuddy import artifact_info |
| 60 | from chromite.lib.xbuddy import build_artifact |
Achuith Bhandarkar | 662fb72 | 2019-10-31 16:12:49 -0700 | [diff] [blame] | 61 | from chromite.lib.xbuddy import common_util |
| 62 | from chromite.lib.xbuddy import devserver_constants |
| 63 | from chromite.lib.xbuddy import downloader |
| 64 | from chromite.lib.xbuddy import xbuddy |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 65 | |
Amin Hassani | 3587fb3 | 2021-04-28 10:10:01 -0700 | [diff] [blame^] | 66 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 67 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 68 | def _Log(message, *args): |
Amin Hassani | 3587fb3 | 2021-04-28 10:10:01 -0700 | [diff] [blame^] | 69 | return logging.info(message, *args) |
Frank Farzan | 4016087 | 2011-12-12 18:39:18 -0800 | [diff] [blame] | 70 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 71 | CACHED_ENTRIES = 12 |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 72 | |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 73 | TELEMETRY_FOLDER = 'telemetry_src' |
| 74 | TELEMETRY_DEPS = ['dep-telemetry_dep.tar.bz2', |
| 75 | 'dep-page_cycler_dep.tar.bz2', |
Simran Basi | 0d07868 | 2013-03-22 16:40:04 -0700 | [diff] [blame] | 76 | 'dep-chrome_test.tar.bz2', |
| 77 | 'dep-perf_data_dep.tar.bz2'] |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 78 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 79 | # Sets up global to share between classes. |
| 80 | updater = None |
| 81 | |
xixuan | 3d48bff | 2017-01-30 19:00:09 -0800 | [diff] [blame] | 82 | # Log rotation parameters. These settings correspond to twice a day once |
| 83 | # devserver is started, with about two weeks (28 backup files) of old logs |
| 84 | # kept for backup. |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 85 | # |
xixuan | 3d48bff | 2017-01-30 19:00:09 -0800 | [diff] [blame] | 86 | # For more, see the documentation in standard python library for |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 87 | # logging.handlers.TimedRotatingFileHandler |
xixuan | 3d48bff | 2017-01-30 19:00:09 -0800 | [diff] [blame] | 88 | _LOG_ROTATION_TIME = 'H' |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 89 | _LOG_ROTATION_INTERVAL = 12 # hours |
| 90 | _LOG_ROTATION_BACKUP = 28 # backup counts |
Frank Farzan | 4016087 | 2011-12-12 18:39:18 -0800 | [diff] [blame] | 91 | |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 92 | # Error msg for deprecated RPC usage. |
| 93 | DEPRECATED_RPC_ERROR_MSG = ('The %s RPC has been deprecated. Usage of this ' |
| 94 | 'RPC is discouraged. Please go to ' |
| 95 | 'go/devserver-deprecation for more information.') |
| 96 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 97 | |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 98 | class DevServerError(Exception): |
| 99 | """Exception class used by DevServer.""" |
| 100 | |
| 101 | |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 102 | class DeprecatedRPCError(DevServerError): |
| 103 | """Exception class used when an RPC is deprecated but is still being used.""" |
| 104 | |
| 105 | def __init__(self, rpc_name): |
| 106 | """Constructor for DeprecatedRPCError class. |
| 107 | |
| 108 | :param rpc_name: (str) name of the RPC that has been deprecated. |
| 109 | """ |
Amin Hassani | 6ecda23 | 2020-03-09 19:03:23 -0700 | [diff] [blame] | 110 | super(DeprecatedRPCError, self).__init__( |
| 111 | DEPRECATED_RPC_ERROR_MSG % rpc_name) |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 112 | self.rpc_name = rpc_name |
| 113 | |
| 114 | |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 115 | class DevServerHTTPError(cherrypy.HTTPError): |
| 116 | """Exception class to log the HTTPResponse before routing it to cherrypy.""" |
| 117 | def __init__(self, status, message): |
| 118 | """CherryPy error with logging. |
| 119 | |
| 120 | Args: |
| 121 | status: HTTPResponse status. |
| 122 | message: Message associated with the response. |
| 123 | """ |
| 124 | cherrypy.HTTPError.__init__(self, status, message) |
| 125 | _Log('HTTPError status: %s message: %s', status, message) |
| 126 | |
| 127 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 128 | def _canonicalize_archive_url(archive_url): |
| 129 | """Canonicalizes archive_url strings. |
| 130 | |
| 131 | Raises: |
| 132 | DevserverError: if archive_url is not set. |
| 133 | """ |
| 134 | if archive_url: |
| 135 | if not archive_url.startswith('gs://'): |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 136 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 137 | "Archive URL isn't from Google Storage (%s) ." % archive_url) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 138 | |
| 139 | return archive_url.rstrip('/') |
| 140 | else: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 141 | raise DevServerError('Must specify an archive_url in the request') |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 142 | |
| 143 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 144 | def _canonicalize_local_path(local_path): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 145 | """Canonicalizes |local_path| strings. |
| 146 | |
| 147 | Raises: |
| 148 | DevserverError: if |local_path| is not set. |
| 149 | """ |
| 150 | # Restrict staging of local content to only files within the static |
| 151 | # directory. |
| 152 | local_path = os.path.abspath(local_path) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 153 | if not local_path.startswith(updater.static_dir): |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 154 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 155 | 'Local path %s must be a subdirectory of the static' |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 156 | ' directory: %s' % (local_path, updater.static_dir)) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 157 | |
| 158 | return local_path.rstrip('/') |
| 159 | |
| 160 | |
| 161 | def _get_artifacts(kwargs): |
| 162 | """Returns a tuple of named and file artifacts given the stage rpc kwargs. |
| 163 | |
| 164 | Raises: |
| 165 | DevserverError if no artifacts would be returned. |
| 166 | """ |
| 167 | artifacts = kwargs.get('artifacts') |
| 168 | files = kwargs.get('files') |
| 169 | if not artifacts and not files: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 170 | raise DevServerError('No artifacts specified.') |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 171 | |
| 172 | # Note we NEED to coerce files to a string as we get raw unicode from |
| 173 | # cherrypy and we treat files as strings elsewhere in the code. |
| 174 | return (str(artifacts).split(',') if artifacts else [], |
| 175 | str(files).split(',') if files else []) |
| 176 | |
| 177 | |
Dan Shi | 61305df | 2015-10-26 16:52:35 -0700 | [diff] [blame] | 178 | def _is_android_build_request(kwargs): |
| 179 | """Check if a devserver call is for Android build, based on the arguments. |
| 180 | |
| 181 | This method exams the request's arguments (os_type) to determine if the |
| 182 | request is for Android build. If os_type is set to `android`, returns True. |
| 183 | If os_type is not set or has other values, returns False. |
| 184 | |
| 185 | Args: |
| 186 | kwargs: Keyword arguments for the request. |
| 187 | |
| 188 | Returns: |
| 189 | True if the request is for Android build. False otherwise. |
| 190 | """ |
| 191 | os_type = kwargs.get('os_type', None) |
| 192 | return os_type == 'android' |
| 193 | |
| 194 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 195 | def _get_downloader(kwargs): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 196 | """Returns the downloader based on passed in arguments. |
| 197 | |
| 198 | Args: |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 199 | kwargs: Keyword arguments for the request. |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 200 | """ |
| 201 | local_path = kwargs.get('local_path') |
| 202 | if local_path: |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 203 | local_path = _canonicalize_local_path(local_path) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 204 | |
| 205 | dl = None |
| 206 | if local_path: |
Prathmesh Prabhu | 58d0893 | 2018-01-19 15:08:19 -0800 | [diff] [blame] | 207 | delete_source = _parse_boolean_arg(kwargs, 'delete_source') |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 208 | dl = downloader.LocalDownloader(updater.static_dir, local_path, |
Prathmesh Prabhu | 58d0893 | 2018-01-19 15:08:19 -0800 | [diff] [blame] | 209 | delete_source=delete_source) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 210 | |
Dan Shi | 61305df | 2015-10-26 16:52:35 -0700 | [diff] [blame] | 211 | if not _is_android_build_request(kwargs): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 212 | archive_url = kwargs.get('archive_url') |
| 213 | if not archive_url and not local_path: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 214 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 215 | 'Requires archive_url or local_path to be specified.') |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 216 | if archive_url and local_path: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 217 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 218 | 'archive_url and local_path can not both be specified.') |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 219 | if not dl: |
| 220 | archive_url = _canonicalize_archive_url(archive_url) |
Luis Hector Chavez | dca9dd7 | 2018-06-12 12:56:30 -0700 | [diff] [blame] | 221 | dl = downloader.GoogleStorageDownloader( |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 222 | updater.static_dir, archive_url, |
Luis Hector Chavez | dca9dd7 | 2018-06-12 12:56:30 -0700 | [diff] [blame] | 223 | downloader.GoogleStorageDownloader.GetBuildIdFromArchiveURL( |
| 224 | archive_url)) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 225 | elif not dl: |
| 226 | target = kwargs.get('target', None) |
Dan Shi | 72b1613 | 2015-10-08 12:10:33 -0700 | [diff] [blame] | 227 | branch = kwargs.get('branch', None) |
Dan Shi | 61305df | 2015-10-26 16:52:35 -0700 | [diff] [blame] | 228 | build_id = kwargs.get('build_id', None) |
| 229 | if not target or not branch or not build_id: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 230 | raise DevServerError('target, branch, build ID must all be specified for ' |
| 231 | 'downloading Android build.') |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 232 | dl = downloader.AndroidBuildDownloader(updater.static_dir, branch, build_id, |
Dan Shi | 72b1613 | 2015-10-08 12:10:33 -0700 | [diff] [blame] | 233 | target) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 234 | |
| 235 | return dl |
| 236 | |
| 237 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 238 | def _get_downloader_and_factory(kwargs): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 239 | """Returns the downloader and artifact factory based on passed in arguments. |
| 240 | |
| 241 | Args: |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 242 | kwargs: Keyword arguments for the request. |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 243 | """ |
| 244 | artifacts, files = _get_artifacts(kwargs) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 245 | dl = _get_downloader(kwargs) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 246 | |
Achuith Bhandarkar | 2a1fcd8 | 2019-10-17 17:45:58 -0700 | [diff] [blame] | 247 | if (isinstance(dl, (downloader.GoogleStorageDownloader, |
| 248 | downloader.LocalDownloader))): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 249 | factory_class = build_artifact.ChromeOSArtifactFactory |
Dan Shi | 72b1613 | 2015-10-08 12:10:33 -0700 | [diff] [blame] | 250 | elif isinstance(dl, downloader.AndroidBuildDownloader): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 251 | factory_class = build_artifact.AndroidArtifactFactory |
| 252 | else: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 253 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 254 | 'Unrecognized value for downloader type: %s' % type(dl)) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 255 | |
| 256 | factory = factory_class(dl.GetBuildDir(), artifacts, files, dl.GetBuild()) |
| 257 | |
| 258 | return dl, factory |
| 259 | |
| 260 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 261 | def _LeadingWhiteSpaceCount(string): |
| 262 | """Count the amount of leading whitespace in a string. |
| 263 | |
| 264 | Args: |
| 265 | string: The string to count leading whitespace in. |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 266 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 267 | Returns: |
| 268 | number of white space chars before characters start. |
| 269 | """ |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 270 | matched = re.match(r'^\s+', string) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 271 | if matched: |
| 272 | return len(matched.group()) |
| 273 | |
| 274 | return 0 |
| 275 | |
| 276 | |
| 277 | def _PrintDocStringAsHTML(func): |
| 278 | """Make a functions docstring somewhat HTML style. |
| 279 | |
| 280 | Args: |
| 281 | func: The function to return the docstring from. |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 282 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 283 | Returns: |
| 284 | A string that is somewhat formated for a web browser. |
| 285 | """ |
| 286 | # TODO(scottz): Make this parse Args/Returns in a prettier way. |
| 287 | # Arguments could be bolded and indented etc. |
| 288 | html_doc = [] |
| 289 | for line in func.__doc__.splitlines(): |
| 290 | leading_space = _LeadingWhiteSpaceCount(line) |
| 291 | if leading_space > 0: |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 292 | line = ' ' * leading_space + line |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 293 | |
| 294 | html_doc.append('<BR>%s' % line) |
| 295 | |
| 296 | return '\n'.join(html_doc) |
| 297 | |
| 298 | |
Simran Basi | ef83d6a | 2014-08-28 14:32:01 -0700 | [diff] [blame] | 299 | def _GetUpdateTimestampHandler(static_dir): |
| 300 | """Returns a handler to update directory staged.timestamp. |
| 301 | |
| 302 | This handler resets the stage.timestamp whenever static content is accessed. |
| 303 | |
| 304 | Args: |
| 305 | static_dir: Directory from which static content is being staged. |
| 306 | |
| 307 | Returns: |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 308 | A cherrypy handler to update the timestamp of accessed content. |
Simran Basi | ef83d6a | 2014-08-28 14:32:01 -0700 | [diff] [blame] | 309 | """ |
| 310 | def UpdateTimestampHandler(): |
| 311 | if not '404' in cherrypy.response.status: |
| 312 | build_match = re.match(devserver_constants.STAGED_BUILD_REGEX, |
| 313 | cherrypy.request.path_info) |
| 314 | if build_match: |
| 315 | build_dir = os.path.join(static_dir, build_match.group('build')) |
| 316 | downloader.Downloader.TouchTimestampForStaged(build_dir) |
| 317 | return UpdateTimestampHandler |
| 318 | |
| 319 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 320 | def _GetConfig(options): |
| 321 | """Returns the configuration for the devserver.""" |
Mandeep Singh Baines | 38dcdda | 2012-12-07 17:55:33 -0800 | [diff] [blame] | 322 | |
Mandeep Singh Baines | 38dcdda | 2012-12-07 17:55:33 -0800 | [diff] [blame] | 323 | socket_host = '::' |
Yu-Ju Hong | c8d4af3 | 2013-11-12 15:14:26 -0800 | [diff] [blame] | 324 | # Fall back to IPv4 when python is not configured with IPv6. |
| 325 | if not socket.has_ipv6: |
Mandeep Singh Baines | 38dcdda | 2012-12-07 17:55:33 -0800 | [diff] [blame] | 326 | socket_host = '0.0.0.0' |
| 327 | |
Simran Basi | ef83d6a | 2014-08-28 14:32:01 -0700 | [diff] [blame] | 328 | # Adds the UpdateTimestampHandler to cherrypy's tools. This tools executes |
| 329 | # on the on_end_resource hook. This hook is called once processing is |
| 330 | # complete and the response is ready to be returned. |
| 331 | cherrypy.tools.update_timestamp = cherrypy.Tool( |
| 332 | 'on_end_resource', _GetUpdateTimestampHandler(options.static_dir)) |
| 333 | |
David Riley | 2fcb012 | 2017-11-02 11:25:39 -0700 | [diff] [blame] | 334 | base_config = { |
| 335 | 'global': { |
| 336 | 'server.log_request_headers': True, |
| 337 | 'server.protocol_version': 'HTTP/1.1', |
| 338 | 'server.socket_host': socket_host, |
| 339 | 'server.socket_port': int(options.port), |
| 340 | 'response.timeout': 6000, |
| 341 | 'request.show_tracebacks': True, |
| 342 | 'server.socket_timeout': 60, |
| 343 | 'server.thread_pool': 2, |
| 344 | 'engine.autoreload.on': False, |
| 345 | }, |
David Riley | 2fcb012 | 2017-11-02 11:25:39 -0700 | [diff] [blame] | 346 | '/build': { |
| 347 | 'response.timeout': 100000, |
| 348 | }, |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 349 | '/update': { |
| 350 | # Gets rid of cherrypy parsing post file for args. |
| 351 | 'request.process_request_body': False, |
| 352 | 'response.timeout': 10000, |
| 353 | }, |
David Riley | 2fcb012 | 2017-11-02 11:25:39 -0700 | [diff] [blame] | 354 | # Sets up the static dir for file hosting. |
| 355 | '/static': { |
| 356 | 'tools.staticdir.dir': options.static_dir, |
| 357 | 'tools.staticdir.on': True, |
| 358 | 'response.timeout': 10000, |
| 359 | 'tools.update_timestamp.on': True, |
| 360 | }, |
| 361 | } |
Chris Sosa | 5f118ef | 2012-07-12 11:37:50 -0700 | [diff] [blame] | 362 | if options.production: |
Alex Miller | 93beca5 | 2013-07-30 19:25:09 -0700 | [diff] [blame] | 363 | base_config['global'].update({'server.thread_pool': 150}) |
Scott Zawalski | 1c5e7cd | 2012-02-27 13:12:52 -0500 | [diff] [blame] | 364 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 365 | return base_config |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 366 | |
Darin Petkov | e17164a | 2010-08-11 13:24:41 -0700 | [diff] [blame] | 367 | |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 368 | def _GetRecursiveMemberObject(root, member_list): |
| 369 | """Returns an object corresponding to a nested member list. |
| 370 | |
| 371 | Args: |
| 372 | root: the root object to search |
| 373 | member_list: list of nested members to search |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 374 | |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 375 | Returns: |
| 376 | An object corresponding to the member name list; None otherwise. |
| 377 | """ |
| 378 | for member in member_list: |
| 379 | next_root = root.__class__.__dict__.get(member) |
| 380 | if not next_root: |
| 381 | return None |
| 382 | root = next_root |
| 383 | return root |
| 384 | |
| 385 | |
| 386 | def _IsExposed(name): |
| 387 | """Returns True iff |name| has an `exposed' attribute and it is set.""" |
| 388 | return hasattr(name, 'exposed') and name.exposed |
| 389 | |
| 390 | |
Congbin Guo | 6bc3218 | 2019-08-20 17:54:30 -0700 | [diff] [blame] | 391 | def _GetExposedMethod(nested_member): |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 392 | """Returns a CherryPy-exposed method, if such exists. |
| 393 | |
| 394 | Args: |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 395 | nested_member: a slash-joined path to the nested member |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 396 | |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 397 | Returns: |
Congbin Guo | 6bc3218 | 2019-08-20 17:54:30 -0700 | [diff] [blame] | 398 | A function object corresponding to the path defined by |nested_member| from |
| 399 | the app root object registered, if the function is exposed; None otherwise. |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 400 | """ |
Congbin Guo | 6bc3218 | 2019-08-20 17:54:30 -0700 | [diff] [blame] | 401 | for app in cherrypy.tree.apps.values(): |
| 402 | # Use the 'index' function doc as the doc of the app. |
| 403 | if nested_member == app.script_name.lstrip('/'): |
| 404 | nested_member = 'index' |
| 405 | |
| 406 | method = _GetRecursiveMemberObject(app.root, nested_member.split('/')) |
| 407 | if method and isinstance(method, types.FunctionType) and _IsExposed(method): |
| 408 | return method |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 409 | |
| 410 | |
Gilad Arnold | 748c832 | 2012-10-12 09:51:35 -0700 | [diff] [blame] | 411 | def _FindExposedMethods(root, prefix, unlisted=None): |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 412 | """Finds exposed CherryPy methods. |
| 413 | |
| 414 | Args: |
| 415 | root: the root object for searching |
| 416 | prefix: slash-joined chain of members leading to current object |
| 417 | unlisted: URLs to be excluded regardless of their exposed status |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 418 | |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 419 | Returns: |
| 420 | List of exposed URLs that are not unlisted. |
| 421 | """ |
| 422 | method_list = [] |
Congbin Guo | 6bc3218 | 2019-08-20 17:54:30 -0700 | [diff] [blame] | 423 | for member in root.__class__.__dict__.keys(): |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 424 | prefixed_member = prefix + '/' + member if prefix else member |
Gilad Arnold | 748c832 | 2012-10-12 09:51:35 -0700 | [diff] [blame] | 425 | if unlisted and prefixed_member in unlisted: |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 426 | continue |
| 427 | member_obj = root.__class__.__dict__[member] |
| 428 | if _IsExposed(member_obj): |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 429 | if isinstance(member_obj, types.FunctionType): |
Congbin Guo | 6bc3218 | 2019-08-20 17:54:30 -0700 | [diff] [blame] | 430 | # Regard the app name as exposed "method" name if it exposed 'index' |
| 431 | # function. |
| 432 | if prefix and member == 'index': |
| 433 | method_list.append(prefix) |
| 434 | else: |
| 435 | method_list.append(prefixed_member) |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 436 | else: |
| 437 | method_list += _FindExposedMethods( |
| 438 | member_obj, prefixed_member, unlisted) |
| 439 | return method_list |
| 440 | |
| 441 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 442 | def _parse_boolean_arg(kwargs, key): |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 443 | """Parse boolean arg from kwargs. |
| 444 | |
| 445 | Args: |
| 446 | kwargs: the parameters to be checked. |
| 447 | key: the key to be parsed. |
| 448 | |
| 449 | Returns: |
| 450 | The boolean value of kwargs[key], or False if key doesn't exist in kwargs. |
| 451 | |
| 452 | Raises: |
| 453 | DevServerHTTPError if kwargs[key] is not a boolean variable. |
| 454 | """ |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 455 | if key in kwargs: |
| 456 | if kwargs[key] == 'True': |
| 457 | return True |
| 458 | elif kwargs[key] == 'False': |
| 459 | return False |
| 460 | else: |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 461 | raise DevServerHTTPError(http_client.INTERNAL_SERVER_ERROR, |
| 462 | 'The value for key %s is not boolean.' % key) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 463 | else: |
| 464 | return False |
| 465 | |
xixuan | 447ad9d | 2017-02-28 14:46:20 -0800 | [diff] [blame] | 466 | |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 467 | def _parse_string_arg(kwargs, key): |
| 468 | """Parse string arg from kwargs. |
| 469 | |
| 470 | Args: |
| 471 | kwargs: the parameters to be checked. |
| 472 | key: the key to be parsed. |
| 473 | |
| 474 | Returns: |
| 475 | The string value of kwargs[key], or None if key doesn't exist in kwargs. |
| 476 | """ |
| 477 | if key in kwargs: |
| 478 | return kwargs[key] |
| 479 | else: |
| 480 | return None |
| 481 | |
xixuan | 447ad9d | 2017-02-28 14:46:20 -0800 | [diff] [blame] | 482 | |
xixuan | ac89ce8 | 2016-11-30 16:48:20 -0800 | [diff] [blame] | 483 | def _build_uri_from_build_name(build_name): |
| 484 | """Get build url from a given build name. |
| 485 | |
| 486 | Args: |
| 487 | build_name: the build name to be parsed, whose format is |
| 488 | 'board/release_version'. |
| 489 | |
| 490 | Returns: |
| 491 | The release_archive_url on Google Storage for this build name. |
| 492 | """ |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 493 | # TODO(ahassani): This function doesn't seem to be used anywhere since its |
| 494 | # previous use of lib.paygen.gspath was broken and it doesn't seem to be |
| 495 | # causing any runtime issues. So deprecate this in the future. |
| 496 | tokens = build_name.split('/') |
| 497 | return 'gs://chromeos-releases/stable-channel/%s/%s' % (tokens[0], tokens[1]) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 498 | |
xixuan | 447ad9d | 2017-02-28 14:46:20 -0800 | [diff] [blame] | 499 | |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 500 | def is_deprecated_server(): |
| 501 | """Gets whether the devserver has deprecated RPCs.""" |
| 502 | return cherrypy.config.get('infra_removal', False) |
| 503 | |
| 504 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 505 | class DevServerRoot(object): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 506 | """The Root Class for the Dev Server. |
| 507 | |
| 508 | CherryPy works as follows: |
| 509 | For each method in this class, cherrpy interprets root/path |
| 510 | as a call to an instance of DevServerRoot->method_name. For example, |
| 511 | a call to http://myhost/build will call build. CherryPy automatically |
| 512 | parses http args and places them as keyword arguments in each method. |
| 513 | For paths http://myhost/update/dir1/dir2, you can use *args so that |
| 514 | cherrypy uses the update method and puts the extra paths in args. |
| 515 | """ |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 516 | # Method names that should not be listed on the index page. |
| 517 | _UNLISTED_METHODS = ['index', 'doc'] |
| 518 | |
Dan Shi | 59ae709 | 2013-06-04 14:37:27 -0700 | [diff] [blame] | 519 | # Number of threads that devserver is staging images. |
| 520 | _staging_thread_count = 0 |
| 521 | # Lock used to lock increasing/decreasing count. |
| 522 | _staging_thread_count_lock = threading.Lock() |
| 523 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 524 | def __init__(self, _xbuddy): |
Nick Sanders | 7dcaa2e | 2011-08-04 15:20:41 -0700 | [diff] [blame] | 525 | self._builder = None |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 526 | self._telemetry_lock_dict = common_util.LockDict() |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 527 | self._xbuddy = _xbuddy |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 528 | |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 529 | @property |
| 530 | def staging_thread_count(self): |
| 531 | """Get the staging thread count.""" |
| 532 | return self._staging_thread_count |
Dan Shi | afd0e49 | 2015-05-27 14:23:51 -0700 | [diff] [blame] | 533 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 534 | @cherrypy.expose |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 535 | def build(self, board, pkg, **kwargs): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 536 | """Builds the package specified.""" |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 537 | if is_deprecated_server(): |
| 538 | raise DeprecatedRPCError('build') |
| 539 | |
Nick Sanders | 7dcaa2e | 2011-08-04 15:20:41 -0700 | [diff] [blame] | 540 | import builder |
| 541 | if self._builder is None: |
| 542 | self._builder = builder.Builder() |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 543 | return self._builder.Build(board, pkg, kwargs) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 544 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 545 | @cherrypy.expose |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 546 | def is_staged(self, **kwargs): |
| 547 | """Check if artifacts have been downloaded. |
| 548 | |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 549 | Examples: |
| 550 | To check if autotest and test_suites are staged: |
| 551 | http://devserver_url:<port>/is_staged?archive_url=gs://your_url/path& |
| 552 | artifacts=autotest,test_suites |
| 553 | |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 554 | Args: |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 555 | async: True to return without waiting for download to complete. |
| 556 | artifacts: Comma separated list of named artifacts to download. |
| 557 | These are defined in artifact_info and have their implementation |
| 558 | in build_artifact.py. |
| 559 | files: Comma separated list of file artifacts to stage. These |
| 560 | will be available as is in the corresponding static directory with no |
| 561 | custom post-processing. |
| 562 | |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 563 | Returns: |
| 564 | True of all artifacts are staged. |
Dan Shi | f8eb0d1 | 2013-08-01 17:52:06 -0700 | [diff] [blame] | 565 | """ |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 566 | dl, factory = _get_downloader_and_factory(kwargs) |
Aviv Keshet | 57d1817 | 2016-06-18 20:39:09 -0700 | [diff] [blame] | 567 | response = str(dl.IsStaged(factory)) |
| 568 | _Log('Responding to is_staged %s request with %r', kwargs, response) |
| 569 | return response |
Dan Shi | 59ae709 | 2013-06-04 14:37:27 -0700 | [diff] [blame] | 570 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 571 | @cherrypy.expose |
Prashanth B | a06d2d2 | 2014-03-07 15:35:19 -0800 | [diff] [blame] | 572 | def list_image_dir(self, **kwargs): |
| 573 | """Take an archive url and list the contents in its staged directory. |
| 574 | |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 575 | Examples: |
Prashanth B | a06d2d2 | 2014-03-07 15:35:19 -0800 | [diff] [blame] | 576 | To list the contents of where this devserver should have staged |
| 577 | gs://image-archive/<board>-release/<build> call: |
| 578 | http://devserver_url:<port>/list_image_dir?archive_url=<gs://..> |
| 579 | |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 580 | Args: |
| 581 | archive_url: Google Storage URL for the build. |
| 582 | |
Prashanth B | a06d2d2 | 2014-03-07 15:35:19 -0800 | [diff] [blame] | 583 | Returns: |
| 584 | A string with information about the contents of the image directory. |
| 585 | """ |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 586 | dl = _get_downloader(kwargs) |
Prashanth B | a06d2d2 | 2014-03-07 15:35:19 -0800 | [diff] [blame] | 587 | try: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 588 | image_dir_contents = dl.ListBuildDir() |
Prashanth B | a06d2d2 | 2014-03-07 15:35:19 -0800 | [diff] [blame] | 589 | except build_artifact.ArtifactDownloadError as e: |
| 590 | return 'Cannot list the contents of staged artifacts. %s' % e |
| 591 | if not image_dir_contents: |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 592 | return '%s has not been staged on this devserver.' % dl.DescribeSource() |
Prashanth B | a06d2d2 | 2014-03-07 15:35:19 -0800 | [diff] [blame] | 593 | return image_dir_contents |
| 594 | |
| 595 | @cherrypy.expose |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 596 | def stage(self, **kwargs): |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 597 | """Downloads and caches build artifacts. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 598 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 599 | Downloads and caches build artifacts, possibly from a Google Storage URL, |
Dan Shi | 72b1613 | 2015-10-08 12:10:33 -0700 | [diff] [blame] | 600 | or from Android's build server. Returns once these have been downloaded |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 601 | on the devserver. A call to this will attempt to cache non-specified |
| 602 | artifacts in the background for the given from the given URL following |
| 603 | the principle of spatial locality. Spatial locality of different |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 604 | artifacts is explicitly defined in the build_artifact module. |
| 605 | |
| 606 | These artifacts will then be available from the static/ sub-directory of |
| 607 | the devserver. |
| 608 | |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 609 | Examples: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 610 | To download the autotest and test suites tarballs: |
| 611 | http://devserver_url:<port>/stage?archive_url=gs://your_url/path& |
| 612 | artifacts=autotest,test_suites |
| 613 | To download the full update payload: |
| 614 | http://devserver_url:<port>/stage?archive_url=gs://your_url/path& |
| 615 | artifacts=full_payload |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 616 | To download just a file called blah.bin: |
| 617 | http://devserver_url:<port>/stage?archive_url=gs://your_url/path& |
| 618 | files=blah.bin |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 619 | |
| 620 | For both these examples, one could find these artifacts at: |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 621 | http://devserver_url:<port>/static/<relative_path>* |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 622 | |
| 623 | Note for this example, relative path is the archive_url stripped of its |
| 624 | basename i.e. path/ in the examples above. Specific example: |
| 625 | |
| 626 | gs://chromeos-image-archive/x86-mario-release/R26-3920.0.0 |
| 627 | |
| 628 | Will get staged to: |
| 629 | |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 630 | http://devserver_url:<port>/static/x86-mario-release/R26-3920.0.0 |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 631 | |
| 632 | Args: |
| 633 | archive_url: Google Storage URL for the build. |
| 634 | local_path: Local path for the build. |
| 635 | delete_source: Only meaningful with local_path. bool to indicate if the |
| 636 | source files should be deleted. This is especially useful when staging |
| 637 | a file locally in resource constrained environments as it allows us to |
| 638 | move the relevant files locally instead of copying them. |
| 639 | async: True to return without waiting for download to complete. |
| 640 | artifacts: Comma separated list of named artifacts to download. |
| 641 | These are defined in artifact_info and have their implementation |
| 642 | in build_artifact.py. |
| 643 | files: Comma separated list of files to stage. These |
| 644 | will be available as is in the corresponding static directory with no |
| 645 | custom post-processing. |
| 646 | clean: True to remove any previously staged artifacts first. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 647 | """ |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 648 | dl, factory = _get_downloader_and_factory(kwargs) |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 649 | |
Dan Shi | 59ae709 | 2013-06-04 14:37:27 -0700 | [diff] [blame] | 650 | with DevServerRoot._staging_thread_count_lock: |
| 651 | DevServerRoot._staging_thread_count += 1 |
| 652 | try: |
Laurence Goodby | f5c958d | 2016-01-14 18:23:56 -0800 | [diff] [blame] | 653 | boolean_string = kwargs.get('clean') |
| 654 | clean = xbuddy.XBuddy.ParseBoolean(boolean_string) |
| 655 | if clean and os.path.exists(dl.GetBuildDir()): |
| 656 | _Log('Removing %s' % dl.GetBuildDir()) |
| 657 | shutil.rmtree(dl.GetBuildDir()) |
Keith Haddow | 58f36d1 | 2020-10-28 16:16:39 +0000 | [diff] [blame] | 658 | dl.Download(factory) |
Dan Shi | 59ae709 | 2013-06-04 14:37:27 -0700 | [diff] [blame] | 659 | finally: |
| 660 | with DevServerRoot._staging_thread_count_lock: |
| 661 | DevServerRoot._staging_thread_count -= 1 |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 662 | return 'Success' |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 663 | |
| 664 | @cherrypy.expose |
Dan Shi | 2f13686 | 2016-02-11 15:38:38 -0800 | [diff] [blame] | 665 | def locate_file(self, **kwargs): |
| 666 | """Get the path to the given file name. |
| 667 | |
| 668 | This method looks up the given file name inside specified build artifacts. |
| 669 | One use case is to help caller to locate an apk file inside a build |
| 670 | artifact. The location of the apk file could be different based on the |
| 671 | branch and target. |
| 672 | |
| 673 | Args: |
| 674 | file_name: Name of the file to look for. |
| 675 | artifacts: A list of artifact names to search for the file. |
| 676 | |
| 677 | Returns: |
| 678 | Path to the file with the given name. It's relative to the folder for the |
| 679 | build, e.g., DATA/priv-app/sl4a/sl4a.apk |
Dan Shi | 2f13686 | 2016-02-11 15:38:38 -0800 | [diff] [blame] | 680 | """ |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 681 | if is_deprecated_server(): |
| 682 | raise DeprecatedRPCError('locate_file') |
| 683 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 684 | dl, _ = _get_downloader_and_factory(kwargs) |
Dan Shi | 2f13686 | 2016-02-11 15:38:38 -0800 | [diff] [blame] | 685 | try: |
Joe Brennan | 1691f8e | 2017-03-15 15:53:36 -0700 | [diff] [blame] | 686 | file_name = kwargs['file_name'] |
Dan Shi | 2f13686 | 2016-02-11 15:38:38 -0800 | [diff] [blame] | 687 | artifacts = kwargs['artifacts'] |
| 688 | except KeyError: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 689 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 690 | '`file_name` and `artifacts` are required to search ' |
| 691 | 'for a file in build artifacts.') |
Dan Shi | 2f13686 | 2016-02-11 15:38:38 -0800 | [diff] [blame] | 692 | build_path = dl.GetBuildDir() |
| 693 | for artifact in artifacts: |
| 694 | # Get the unzipped folder of the artifact. If it's not defined in |
| 695 | # ARTIFACT_UNZIP_FOLDER_MAP, assume the files are unzipped to the build |
| 696 | # directory directly. |
| 697 | folder = artifact_info.ARTIFACT_UNZIP_FOLDER_MAP.get(artifact, '') |
| 698 | artifact_path = os.path.join(build_path, folder) |
| 699 | for root, _, filenames in os.walk(artifact_path): |
Joe Brennan | 1691f8e | 2017-03-15 15:53:36 -0700 | [diff] [blame] | 700 | if file_name in set([f for f in filenames]): |
Dan Shi | 2f13686 | 2016-02-11 15:38:38 -0800 | [diff] [blame] | 701 | return os.path.relpath(os.path.join(root, file_name), build_path) |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 702 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 703 | 'File `%s` can not be found in artifacts: %s' % (file_name, artifacts)) |
Dan Shi | 2f13686 | 2016-02-11 15:38:38 -0800 | [diff] [blame] | 704 | |
| 705 | @cherrypy.expose |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 706 | def setup_telemetry(self, **kwargs): |
| 707 | """Extracts and sets up telemetry |
| 708 | |
| 709 | This method goes through the telemetry deps packages, and stages them on |
| 710 | the devserver to be used by the drones and the telemetry tests. |
| 711 | |
| 712 | Args: |
| 713 | archive_url: Google Storage URL for the build. |
| 714 | |
| 715 | Returns: |
| 716 | Path to the source folder for the telemetry codebase once it is staged. |
| 717 | """ |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 718 | dl = _get_downloader(kwargs) |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 719 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 720 | build_path = dl.GetBuildDir() |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 721 | deps_path = os.path.join(build_path, 'autotest/packages') |
| 722 | telemetry_path = os.path.join(build_path, TELEMETRY_FOLDER) |
| 723 | src_folder = os.path.join(telemetry_path, 'src') |
| 724 | |
| 725 | with self._telemetry_lock_dict.lock(telemetry_path): |
| 726 | if os.path.exists(src_folder): |
| 727 | # Telemetry is already fully stage return |
| 728 | return src_folder |
| 729 | |
| 730 | common_util.MkDirP(telemetry_path) |
| 731 | |
| 732 | # Copy over the required deps tar balls to the telemetry directory. |
| 733 | for dep in TELEMETRY_DEPS: |
| 734 | dep_path = os.path.join(deps_path, dep) |
Simran Basi | 0d07868 | 2013-03-22 16:40:04 -0700 | [diff] [blame] | 735 | if not os.path.exists(dep_path): |
| 736 | # This dep does not exist (could be new), do not extract it. |
| 737 | continue |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 738 | try: |
Eliot Courtney | f39420b | 2020-10-27 18:34:04 +0900 | [diff] [blame] | 739 | cros_build_lib.ExtractTarball(dep_path, telemetry_path) |
| 740 | except cros_build_lib.TarballError as e: |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 741 | shutil.rmtree(telemetry_path) |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 742 | raise DevServerError(str(e)) |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 743 | |
| 744 | # By default all the tarballs extract to test_src but some parts of |
| 745 | # the telemetry code specifically hardcoded to exist inside of 'src'. |
| 746 | test_src = os.path.join(telemetry_path, 'test_src') |
| 747 | try: |
| 748 | shutil.move(test_src, src_folder) |
| 749 | except shutil.Error: |
| 750 | # This can occur if src_folder already exists. Remove and retry move. |
| 751 | shutil.rmtree(src_folder) |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 752 | raise DevServerError( |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 753 | 'Failure in telemetry setup for build %s. Appears that the ' |
| 754 | 'test_src to src move failed.' % dl.GetBuild()) |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 755 | |
| 756 | return src_folder |
| 757 | |
| 758 | @cherrypy.expose |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 759 | def symbolicate_dump(self, minidump, **kwargs): |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 760 | """Symbolicates a minidump using pre-downloaded symbols, returns it. |
| 761 | |
| 762 | Callers will need to POST to this URL with a body of MIME-type |
| 763 | "multipart/form-data". |
| 764 | The body should include a single argument, 'minidump', containing the |
| 765 | binary-formatted minidump to symbolicate. |
| 766 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 767 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 768 | archive_url: Google Storage URL for the build. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 769 | minidump: The binary minidump file to symbolicate. |
| 770 | """ |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 771 | if is_deprecated_server(): |
| 772 | raise DeprecatedRPCError('symbolicate_dump') |
| 773 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 774 | # Ensure the symbols have been staged. |
Dan Shi | f08fe49 | 2016-10-04 14:39:25 -0700 | [diff] [blame] | 775 | # Try debug.tar.xz first, then debug.tgz |
| 776 | for artifact in (artifact_info.SYMBOLS_ONLY, artifact_info.SYMBOLS): |
| 777 | kwargs['artifacts'] = artifact |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 778 | dl = _get_downloader(kwargs) |
Dan Shi | f08fe49 | 2016-10-04 14:39:25 -0700 | [diff] [blame] | 779 | |
| 780 | try: |
| 781 | if self.stage(**kwargs) == 'Success': |
| 782 | break |
| 783 | except build_artifact.ArtifactDownloadError: |
| 784 | continue |
| 785 | else: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 786 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 787 | 'Failed to stage symbols for %s' % dl.DescribeSource()) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 788 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 789 | to_return = '' |
| 790 | with tempfile.NamedTemporaryFile() as local: |
| 791 | while True: |
| 792 | data = minidump.file.read(8192) |
| 793 | if not data: |
| 794 | break |
| 795 | local.write(data) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 796 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 797 | local.flush() |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 798 | |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 799 | symbols_directory = os.path.join(dl.GetBuildDir(), 'debug', 'breakpad') |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 800 | |
xixuan | ab74438 | 2017-04-27 10:41:27 -0700 | [diff] [blame] | 801 | # The location of minidump_stackwalk is defined in chromeos-admin. |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 802 | stackwalk = subprocess.Popen( |
xixuan | ab74438 | 2017-04-27 10:41:27 -0700 | [diff] [blame] | 803 | ['/usr/local/bin/minidump_stackwalk', local.name, symbols_directory], |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 804 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 805 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 806 | to_return, error_text = stackwalk.communicate() |
| 807 | if stackwalk.returncode != 0: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 808 | raise DevServerError( |
Congbin Guo | 4132a27 | 2019-08-20 12:32:14 -0700 | [diff] [blame] | 809 | "Can't generate stack trace: %s (rc=%d)" % (error_text, |
| 810 | stackwalk.returncode)) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 811 | |
| 812 | return to_return |
| 813 | |
| 814 | @cherrypy.expose |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 815 | def latestbuild(self, **kwargs): |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 816 | """Return a string representing the latest build for a given target. |
| 817 | |
| 818 | Args: |
| 819 | target: The build target, typically a combination of the board and the |
| 820 | type of build e.g. x86-mario-release. |
| 821 | milestone: The milestone to filter builds on. E.g. R16. Optional, if not |
| 822 | provided the latest RXX build will be returned. |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 823 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 824 | Returns: |
| 825 | A string representation of the latest build if one exists, i.e. |
| 826 | R19-1993.0.0-a1-b1480. |
| 827 | An empty string if no latest could be found. |
| 828 | """ |
Sanika Kulkarni | 07d47ed | 2020-08-06 14:56:46 -0700 | [diff] [blame] | 829 | if is_deprecated_server(): |
| 830 | raise DeprecatedRPCError('latestbuild') |
| 831 | |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 832 | if not kwargs: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 833 | return _PrintDocStringAsHTML(self.latestbuild) |
| 834 | |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 835 | if 'target' not in kwargs: |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 836 | raise DevServerHTTPError(http_client.INTERNAL_SERVER_ERROR, |
| 837 | 'Error: target= is required!') |
Dan Shi | 61305df | 2015-10-26 16:52:35 -0700 | [diff] [blame] | 838 | |
| 839 | if _is_android_build_request(kwargs): |
| 840 | branch = kwargs.get('branch', None) |
| 841 | target = kwargs.get('target', None) |
| 842 | if not target or not branch: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 843 | raise DevServerError('Both target and branch must be specified to query' |
| 844 | ' for the latest Android build.') |
Dan Shi | 61305df | 2015-10-26 16:52:35 -0700 | [diff] [blame] | 845 | return android_build.BuildAccessor.GetLatestBuildID(target, branch) |
| 846 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 847 | try: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 848 | return common_util.GetLatestBuildVersion( |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 849 | updater.static_dir, kwargs['target'], |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 850 | milestone=kwargs.get('milestone')) |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 851 | except common_util.CommonUtilError as errmsg: |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 852 | raise DevServerHTTPError(http_client.INTERNAL_SERVER_ERROR, |
| 853 | str(errmsg)) |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 854 | |
| 855 | @cherrypy.expose |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 856 | def list_suite_controls(self, **kwargs): |
| 857 | """Return a list of contents of all known control files. |
| 858 | |
| 859 | Example URL: |
| 860 | To List all control files' content: |
| 861 | http://dev-server/list_suite_controls?suite_name=bvt& |
| 862 | build=daisy_spring-release/R29-4279.0.0 |
| 863 | |
| 864 | Args: |
| 865 | build: The build i.e. x86-alex-release/R18-1514.0.0-a1-b1450. |
| 866 | suite_name: List the control files belonging to that suite. |
| 867 | |
| 868 | Returns: |
Dan Shi | a1cd652 | 2016-04-18 16:07:21 -0700 | [diff] [blame] | 869 | A dictionary of all control files's path to its content for given suite. |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 870 | """ |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 871 | if is_deprecated_server(): |
| 872 | raise DeprecatedRPCError('list_suite_controls') |
| 873 | |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 874 | if not kwargs: |
| 875 | return _PrintDocStringAsHTML(self.controlfiles) |
| 876 | |
| 877 | if 'build' not in kwargs: |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 878 | raise DevServerHTTPError(http_client.INTERNAL_SERVER_ERROR, |
| 879 | 'Error: build= is required!') |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 880 | |
| 881 | if 'suite_name' not in kwargs: |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 882 | raise DevServerHTTPError(http_client.INTERNAL_SERVER_ERROR, |
| 883 | 'Error: suite_name= is required!') |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 884 | |
| 885 | control_file_list = [ |
| 886 | line.rstrip() for line in common_util.GetControlFileListForSuite( |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 887 | updater.static_dir, kwargs['build'], |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 888 | kwargs['suite_name']).splitlines()] |
| 889 | |
Dan Shi | a1cd652 | 2016-04-18 16:07:21 -0700 | [diff] [blame] | 890 | control_file_content_dict = {} |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 891 | for control_path in control_file_list: |
Dan Shi | a1cd652 | 2016-04-18 16:07:21 -0700 | [diff] [blame] | 892 | control_file_content_dict[control_path] = (common_util.GetControlFile( |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 893 | updater.static_dir, kwargs['build'], control_path)) |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 894 | |
Dan Shi | a1cd652 | 2016-04-18 16:07:21 -0700 | [diff] [blame] | 895 | return json.dumps(control_file_content_dict) |
xixuan | 7efd000 | 2016-04-14 15:34:01 -0700 | [diff] [blame] | 896 | |
| 897 | @cherrypy.expose |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 898 | def controlfiles(self, **kwargs): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 899 | """Return a control file or a list of all known control files. |
| 900 | |
| 901 | Example URL: |
| 902 | To List all control files: |
beeps | bd33724 | 2013-07-09 22:44:06 -0700 | [diff] [blame] | 903 | http://dev-server/controlfiles?suite_name=&build=daisy_spring-release/R29-4279.0.0 |
| 904 | To List all control files for, say, the bvt suite: |
| 905 | http://dev-server/controlfiles?suite_name=bvt&build=daisy_spring-release/R29-4279.0.0 |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 906 | To return the contents of a path: |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 907 | http://dev-server/controlfiles?board=x86-alex-release&build=R18-1514.0.0&control_path=client/sleeptest/control |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 908 | |
| 909 | Args: |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 910 | build: The build i.e. x86-alex-release/R18-1514.0.0-a1-b1450. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 911 | control_path: If you want the contents of a control file set this |
| 912 | to the path. E.g. client/site_tests/sleeptest/control |
| 913 | Optional, if not provided return a list of control files is returned. |
beeps | bd33724 | 2013-07-09 22:44:06 -0700 | [diff] [blame] | 914 | suite_name: If control_path is not specified but a suite_name is |
| 915 | specified, list the control files belonging to that suite instead of |
| 916 | all control files. The empty string for suite_name will list all control |
| 917 | files for the build. |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 918 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 919 | Returns: |
| 920 | Contents of a control file if control_path is provided. |
| 921 | A list of control files if no control_path is provided. |
| 922 | """ |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 923 | if is_deprecated_server(): |
| 924 | raise DeprecatedRPCError('controlfiles') |
| 925 | |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 926 | if not kwargs: |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 927 | return _PrintDocStringAsHTML(self.controlfiles) |
| 928 | |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 929 | if 'build' not in kwargs: |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 930 | raise DevServerHTTPError(http_client.INTERNAL_SERVER_ERROR, |
| 931 | 'Error: build= is required!') |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 932 | |
Don Garrett | f84631a | 2014-01-07 18:21:26 -0800 | [diff] [blame] | 933 | if 'control_path' not in kwargs: |
| 934 | if 'suite_name' in kwargs and kwargs['suite_name']: |
beeps | bd33724 | 2013-07-09 22:44:06 -0700 | [diff] [blame] | 935 | return common_util.GetControlFileListForSuite( |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 936 | updater.static_dir, kwargs['build'], kwargs['suite_name']) |
beeps | bd33724 | 2013-07-09 22:44:06 -0700 | [diff] [blame] | 937 | else: |
| 938 | return common_util.GetControlFileList( |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 939 | updater.static_dir, kwargs['build']) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 940 | else: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 941 | return common_util.GetControlFile( |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 942 | updater.static_dir, kwargs['build'], kwargs['control_path']) |
Frank Farzan | 4016087 | 2011-12-12 18:39:18 -0800 | [diff] [blame] | 943 | |
| 944 | @cherrypy.expose |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 945 | def xbuddy_translate(self, *args, **kwargs): |
Yu-Ju Hong | 1bdb7a9 | 2014-04-10 16:02:11 -0700 | [diff] [blame] | 946 | """Translates an xBuddy path to a real path to artifact if it exists. |
| 947 | |
| 948 | Args: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 949 | args: An xbuddy path in the form of {local|remote}/build_id/artifact. |
| 950 | Local searches the devserver's static directory. Remote searches a |
| 951 | Google Storage image archive. |
| 952 | |
| 953 | Kwargs: |
| 954 | image_dir: Google Storage image archive to search in if requesting a |
| 955 | remote artifact. If none uses the default bucket. |
Yu-Ju Hong | 1bdb7a9 | 2014-04-10 16:02:11 -0700 | [diff] [blame] | 956 | |
| 957 | Returns: |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 958 | String in the format of build_id/artifact as stored on the local server |
| 959 | or in Google Storage. |
Yu-Ju Hong | 1bdb7a9 | 2014-04-10 16:02:11 -0700 | [diff] [blame] | 960 | """ |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 961 | if is_deprecated_server(): |
| 962 | raise DeprecatedRPCError('xbuddy_translate') |
| 963 | |
Simran Basi | 99e63c0 | 2014-05-20 10:39:52 -0700 | [diff] [blame] | 964 | build_id, filename = self._xbuddy.Translate( |
Gabe Black | 3b56720 | 2015-09-23 14:07:59 -0700 | [diff] [blame] | 965 | args, image_dir=kwargs.get('image_dir')) |
Yu-Ju Hong | 1bdb7a9 | 2014-04-10 16:02:11 -0700 | [diff] [blame] | 966 | response = os.path.join(build_id, filename) |
| 967 | _Log('Path translation requested, returning: %s', response) |
| 968 | return response |
| 969 | |
| 970 | @cherrypy.expose |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 971 | def xbuddy(self, *args, **kwargs): |
| 972 | """The full xBuddy call, returns resource specified by path_parts. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 973 | |
| 974 | Args: |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 975 | path_parts: the path following xbuddy/ in the call url is split into the |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 976 | components of the path. The path can be understood as |
| 977 | "{local|remote}/build_id/artifact" where build_id is composed of |
| 978 | "board/version." |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 979 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 980 | The first path element is optional, and can be "remote" or "local" |
| 981 | If local (the default), devserver will not attempt to access Google |
| 982 | Storage, and will only search the static directory for the files. |
| 983 | If remote, devserver will try to obtain the artifact off GS if it's |
| 984 | not found locally. |
| 985 | The board is the familiar board name, optionally suffixed. |
| 986 | The version can be the google storage version number, and may also be |
| 987 | any of a number of xBuddy defined version aliases that will be |
| 988 | translated into the latest built image that fits the description. |
| 989 | Defaults to latest. |
| 990 | The artifact is one of a number of image or artifact aliases used by |
| 991 | xbuddy, defined in xbuddy:ALIASES. Defaults to test. |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 992 | |
| 993 | Kwargs: |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 994 | return_dir: {true|false} |
| 995 | if set to true, returns the url to the update.gz |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 996 | relative_path: {true|false} |
| 997 | if set to true, returns the relative path to the payload |
| 998 | directory from static_dir. |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 999 | Example URL: |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 1000 | http://host:port/xbuddy/x86-generic/R26-4000.0.0/test |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1001 | or |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 1002 | http://host:port/xbuddy/x86-generic/R26-4000.0.0/test?return_dir=true |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1003 | |
| 1004 | Returns: |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 1005 | If |return_dir|, return a uri to the folder where the artifact is. E.g., |
| 1006 | http://host:port/static/x86-generic-release/R26-4000.0.0/ |
| 1007 | If |relative_path| is true, return a relative path the folder where the |
| 1008 | payloads are. E.g., |
| 1009 | archive/x86-generic-release/R26-4000.0.0 |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1010 | """ |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 1011 | if is_deprecated_server(): |
| 1012 | raise DeprecatedRPCError('xbuddy') |
| 1013 | |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 1014 | boolean_string = kwargs.get('return_dir') |
| 1015 | return_dir = xbuddy.XBuddy.ParseBoolean(boolean_string) |
| 1016 | boolean_string = kwargs.get('relative_path') |
| 1017 | relative_path = xbuddy.XBuddy.ParseBoolean(boolean_string) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 1018 | |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 1019 | if return_dir and relative_path: |
Amin Hassani | 722e096 | 2019-11-15 15:45:31 -0800 | [diff] [blame] | 1020 | raise DevServerHTTPError( |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 1021 | http_client.INTERNAL_SERVER_ERROR, |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 1022 | 'Cannot specify both return_dir and relative_path') |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 1023 | |
Amin Hassani | a4bc565 | 2020-10-19 12:44:24 -0700 | [diff] [blame] | 1024 | build_id, file_name = self._xbuddy.Get(args) |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 1025 | |
| 1026 | response = None |
| 1027 | if return_dir: |
| 1028 | response = os.path.join(cherrypy.request.base, 'static', build_id) |
| 1029 | _Log('Directory requested, returning: %s', response) |
| 1030 | elif relative_path: |
| 1031 | response = build_id |
| 1032 | _Log('Relative path requested, returning: %s', response) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1033 | else: |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 1034 | # Redirect to download the payload if no kwargs are set. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 1035 | build_id = '/' + os.path.join('static', build_id, file_name) |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 1036 | _Log('Payload requested, returning: %s', build_id) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 1037 | raise cherrypy.HTTPRedirect(build_id, 302) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1038 | |
Yu-Ju Hong | 51495eb | 2013-12-12 17:08:43 -0800 | [diff] [blame] | 1039 | return response |
| 1040 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1041 | @cherrypy.expose |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1042 | def xbuddy_capacity(self): |
Gilad Arnold | 452fd27 | 2014-02-04 11:09:28 -0800 | [diff] [blame] | 1043 | """Returns the number of images cached by xBuddy.""" |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 1044 | if is_deprecated_server(): |
| 1045 | raise DeprecatedRPCError('xbuddy_capacity') |
| 1046 | |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1047 | return self._xbuddy.Capacity() |
| 1048 | |
| 1049 | @cherrypy.expose |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 1050 | def index(self): |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 1051 | """Presents a welcome message and documentation links.""" |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 1052 | if is_deprecated_server(): |
| 1053 | raise DeprecatedRPCError('index') |
| 1054 | |
Congbin Guo | 6bc3218 | 2019-08-20 17:54:30 -0700 | [diff] [blame] | 1055 | html_template = ( |
| 1056 | 'Welcome to the Dev Server!<br>\n' |
| 1057 | '<br>\n' |
| 1058 | 'Here are the available methods, click for documentation:<br>\n' |
| 1059 | '<br>\n' |
| 1060 | '%s') |
| 1061 | |
| 1062 | exposed_methods = [] |
| 1063 | for app in cherrypy.tree.apps.values(): |
| 1064 | exposed_methods += _FindExposedMethods( |
| 1065 | app.root, app.script_name.lstrip('/'), |
| 1066 | unlisted=self._UNLISTED_METHODS) |
| 1067 | |
| 1068 | return html_template % '<br>\n'.join( |
| 1069 | ['<a href=doc/%s>%s</a>' % (name, name) |
| 1070 | for name in sorted(exposed_methods)]) |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 1071 | |
| 1072 | @cherrypy.expose |
| 1073 | def doc(self, *args): |
| 1074 | """Shows the documentation for available methods / URLs. |
| 1075 | |
Amin Hassani | 08e42d2 | 2019-06-03 00:31:30 -0700 | [diff] [blame] | 1076 | Examples: |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 1077 | http://myhost/doc/update |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 1078 | """ |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 1079 | if is_deprecated_server(): |
| 1080 | raise DeprecatedRPCError('doc') |
| 1081 | |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 1082 | name = '/'.join(args) |
Congbin Guo | 6bc3218 | 2019-08-20 17:54:30 -0700 | [diff] [blame] | 1083 | method = _GetExposedMethod(name) |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 1084 | if not method: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 1085 | raise DevServerError("No exposed method named `%s'" % name) |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 1086 | if not method.__doc__: |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 1087 | raise DevServerError("No documentation for exposed method `%s'" % name) |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 1088 | return '<pre>\n%s</pre>' % method.__doc__ |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 1089 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 1090 | @cherrypy.expose |
| 1091 | def update(self, *args, **kwargs): |
| 1092 | """Handles an update check from a Chrome OS client. |
| 1093 | |
| 1094 | The HTTP request should contain the standard Omaha-style XML blob. The URL |
| 1095 | line may contain an additional intermediate path to the update payload. |
| 1096 | |
| 1097 | This request can be handled in one of 4 ways, depending on the devsever |
| 1098 | settings and intermediate path. |
| 1099 | |
| 1100 | 1. No intermediate path. DEPRECATED |
| 1101 | |
| 1102 | 2. Path explicitly invokes XBuddy |
| 1103 | If there is a path given, it can explicitly invoke xbuddy by prefixing it |
| 1104 | with 'xbuddy'. This path is then used to acquire an image binary for the |
| 1105 | devserver to generate an update payload from. Devserver then serves this |
| 1106 | payload. |
| 1107 | |
| 1108 | 3. Path is left for the devserver to interpret. |
| 1109 | If the path given doesn't explicitly invoke xbuddy, devserver will attempt |
| 1110 | to generate a payload from the test image in that directory and serve it. |
| 1111 | |
| 1112 | Examples: |
| 1113 | 2. Explicitly invoke xbuddy |
| 1114 | update_engine_client --omaha_url= |
| 1115 | http://myhost/update/xbuddy/remote/board/version/dev |
| 1116 | This would go to GS to download the dev image for the board, from which |
| 1117 | the devserver would generate a payload to serve. |
| 1118 | |
| 1119 | 3. Give a path for devserver to interpret |
| 1120 | update_engine_client --omaha_url=http://myhost/update/some/random/path |
| 1121 | This would attempt, in order to: |
| 1122 | a) Generate an update from a test image binary if found in |
| 1123 | static_dir/some/random/path. |
| 1124 | b) Serve an update payload found in static_dir/some/random/path. |
| 1125 | c) Hope that some/random/path takes the form "board/version" and |
| 1126 | and attempt to download an update payload for that board/version |
| 1127 | from GS. |
| 1128 | """ |
| 1129 | label = '/'.join(args) |
| 1130 | body_length = int(cherrypy.request.headers.get('Content-Length', 0)) |
| 1131 | data = cherrypy.request.rfile.read(body_length) |
| 1132 | |
| 1133 | return updater.HandleUpdatePing(data, label, **kwargs) |
| 1134 | |
Dan Shi | f5ce2de | 2013-04-25 16:06:32 -0700 | [diff] [blame] | 1135 | |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 1136 | def _CleanCache(cache_dir, wipe): |
| 1137 | """Wipes any excess cached items in the cache_dir. |
| 1138 | |
| 1139 | Args: |
| 1140 | cache_dir: the directory we are wiping from. |
| 1141 | wipe: If True, wipe all the contents -- not just the excess. |
| 1142 | """ |
| 1143 | if wipe: |
| 1144 | # Clear the cache and exit on error. |
| 1145 | cmd = 'rm -rf %s/*' % cache_dir |
| 1146 | if os.system(cmd) != 0: |
| 1147 | _Log('Failed to clear the cache with %s' % cmd) |
| 1148 | sys.exit(1) |
| 1149 | else: |
| 1150 | # Clear all but the last N cached updates |
| 1151 | cmd = ('cd %s; ls -tr | head --lines=-%d | xargs rm -rf' % |
| 1152 | (cache_dir, CACHED_ENTRIES)) |
| 1153 | if os.system(cmd) != 0: |
| 1154 | _Log('Failed to clean up old delta cache files with %s' % cmd) |
| 1155 | sys.exit(1) |
| 1156 | |
| 1157 | |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1158 | def _AddTestingOptions(parser): |
| 1159 | group = optparse.OptionGroup( |
| 1160 | parser, 'Advanced Testing Options', 'These are used by test scripts and ' |
| 1161 | 'developers writing integration tests utilizing the devserver. They are ' |
| 1162 | 'not intended to be really used outside the scope of someone ' |
| 1163 | 'knowledgable about the test.') |
| 1164 | group.add_option('--exit', |
| 1165 | action='store_true', |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 1166 | help='do not start the server (yet clear cache)') |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1167 | parser.add_option_group(group) |
| 1168 | |
| 1169 | |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1170 | def _AddProductionOptions(parser): |
| 1171 | group = optparse.OptionGroup( |
| 1172 | parser, 'Advanced Server Options', 'These options can be used to changed ' |
| 1173 | 'for advanced server behavior.') |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1174 | group.add_option('--clear_cache', |
| 1175 | action='store_true', default=False, |
| 1176 | help='At startup, removes all cached entries from the' |
Amin Hassani | d4e3539 | 2019-10-03 11:02:44 -0700 | [diff] [blame] | 1177 | "devserver's cache.") |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1178 | group.add_option('--logfile', |
| 1179 | metavar='PATH', |
| 1180 | help='log output to this file instead of stdout') |
Chris Sosa | 855b893 | 2013-08-21 13:24:55 -0700 | [diff] [blame] | 1181 | group.add_option('--pidfile', |
| 1182 | metavar='PATH', |
| 1183 | help='path to output a pid file for the server.') |
Gilad Arnold | 11fbef4 | 2014-02-10 11:04:13 -0800 | [diff] [blame] | 1184 | group.add_option('--portfile', |
| 1185 | metavar='PATH', |
| 1186 | help='path to output the port number being served on.') |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1187 | group.add_option('--production', |
| 1188 | action='store_true', default=False, |
| 1189 | help='have the devserver use production values when ' |
| 1190 | 'starting up. This includes using more threads and ' |
| 1191 | 'performing less logging.') |
| 1192 | parser.add_option_group(group) |
| 1193 | |
| 1194 | |
Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 1195 | def MakeLogHandler(logfile): |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1196 | """Create a LogHandler instance used to log all messages.""" |
| 1197 | hdlr_cls = handlers.TimedRotatingFileHandler |
| 1198 | hdlr = hdlr_cls(logfile, when=_LOG_ROTATION_TIME, |
xixuan | 3d48bff | 2017-01-30 19:00:09 -0800 | [diff] [blame] | 1199 | interval=_LOG_ROTATION_INTERVAL, |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1200 | backupCount=_LOG_ROTATION_BACKUP) |
Chris Sosa | 855b893 | 2013-08-21 13:24:55 -0700 | [diff] [blame] | 1201 | hdlr.setFormatter(cplogging.logfmt) |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1202 | return hdlr |
| 1203 | |
| 1204 | |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 1205 | def main(): |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1206 | usage = '\n\n'.join(['usage: %prog [options]', __doc__]) |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 1207 | parser = optparse.OptionParser(usage=usage) |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 1208 | |
| 1209 | # get directory that the devserver is run from |
| 1210 | devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
joychen | 84d1377 | 2013-08-06 09:17:23 -0700 | [diff] [blame] | 1211 | default_static_dir = '%s/static' % devserver_dir |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 1212 | parser.add_option('--static_dir', |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 1213 | metavar='PATH', |
joychen | 84d1377 | 2013-08-06 09:17:23 -0700 | [diff] [blame] | 1214 | default=default_static_dir, |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 1215 | help='writable static directory') |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 1216 | parser.add_option('--port', |
| 1217 | default=8080, type='int', |
Gilad Arnold | af696d1 | 2014-02-14 13:13:28 -0800 | [diff] [blame] | 1218 | help=('port for the dev server to use; if zero, binds to ' |
| 1219 | 'an arbitrary available port (default: 8080)')) |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 1220 | parser.add_option('-t', '--test_image', |
| 1221 | action='store_true', |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 1222 | help='Deprecated.') |
joychen | 5260b9a | 2013-07-16 14:48:01 -0700 | [diff] [blame] | 1223 | parser.add_option('-x', '--xbuddy_manage_builds', |
| 1224 | action='store_true', |
| 1225 | default=False, |
| 1226 | help='If set, allow xbuddy to manage images in' |
| 1227 | 'build/images.') |
Dan Shi | 72b1613 | 2015-10-08 12:10:33 -0700 | [diff] [blame] | 1228 | parser.add_option('-a', '--android_build_credential', |
| 1229 | default=None, |
| 1230 | help='Path to a json file which contains the credential ' |
| 1231 | 'needed to access Android builds.') |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 1232 | parser.add_option('--infra_removal', |
| 1233 | action='store_true', default=False, |
| 1234 | help='If option is present, some RPCs will be disabled to ' |
| 1235 | 'help with infra removal efforts. See ' |
| 1236 | 'go/devserver-deprecation') |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1237 | _AddProductionOptions(parser) |
Chris Sosa | 3ae4dc1 | 2013-03-29 11:47:00 -0700 | [diff] [blame] | 1238 | _AddTestingOptions(parser) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 1239 | (options, _) = parser.parse_args() |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 1240 | |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1241 | # Handle options that must be set globally in cherrypy. Do this |
| 1242 | # work up front, because calls to _Log() below depend on this |
| 1243 | # initialization. |
| 1244 | if options.production: |
| 1245 | cherrypy.config.update({'environment': 'production'}) |
Sanika Kulkarni | d4496fd | 2020-02-04 17:26:25 -0800 | [diff] [blame] | 1246 | cherrypy.config.update({'infra_removal': options.infra_removal}) |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1247 | if not options.logfile: |
| 1248 | cherrypy.config.update({'log.screen': True}) |
| 1249 | else: |
| 1250 | cherrypy.config.update({'log.error_file': '', |
| 1251 | 'log.access_file': ''}) |
Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 1252 | hdlr = MakeLogHandler(options.logfile) |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1253 | # Pylint can't seem to process these two calls properly |
| 1254 | # pylint: disable=E1101 |
| 1255 | cherrypy.log.access_log.addHandler(hdlr) |
| 1256 | cherrypy.log.error_log.addHandler(hdlr) |
| 1257 | # pylint: enable=E1101 |
| 1258 | |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 1259 | # set static_dir, from which everything will be served |
joychen | 84d1377 | 2013-08-06 09:17:23 -0700 | [diff] [blame] | 1260 | options.static_dir = os.path.realpath(options.static_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 1261 | |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 1262 | cache_dir = os.path.join(options.static_dir, 'cache') |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1263 | # If our devserver is only supposed to serve payloads, we shouldn't be |
| 1264 | # mucking with the cache at all. If the devserver hadn't previously |
| 1265 | # generated a cache and is expected, the caller is using it wrong. |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 1266 | if os.path.exists(cache_dir): |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 1267 | _CleanCache(cache_dir, options.clear_cache) |
Chris Sosa | 6b8c374 | 2011-01-31 12:12:17 -0800 | [diff] [blame] | 1268 | else: |
| 1269 | os.makedirs(cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 1270 | |
Amin Hassani | ef52362 | 2020-07-06 12:09:23 -0700 | [diff] [blame] | 1271 | pkgroot_dir = os.path.join(options.static_dir, 'pkgroot') |
| 1272 | common_util.SymlinkFile('/build', pkgroot_dir) |
| 1273 | |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 1274 | _Log('Using cache directory %s' % cache_dir) |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 1275 | _Log('Serving from %s' % options.static_dir) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 1276 | |
Amin Hassani | e9ffb86 | 2019-09-25 17:10:40 -0700 | [diff] [blame] | 1277 | _xbuddy = xbuddy.XBuddy(manage_builds=options.xbuddy_manage_builds, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 1278 | static_dir=options.static_dir) |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 1279 | if options.clear_cache and options.xbuddy_manage_builds: |
| 1280 | _xbuddy.CleanCache() |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 1281 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 1282 | # We allow global use here to share with cherrypy classes. |
| 1283 | # pylint: disable=W0603 |
| 1284 | global updater |
| 1285 | updater = autoupdate.Autoupdate(_xbuddy, static_dir=options.static_dir) |
| 1286 | |
J. Richard Barnette | 3d977b8 | 2013-04-23 11:05:19 -0700 | [diff] [blame] | 1287 | if options.exit: |
| 1288 | return |
Chris Sosa | 2f1c41e | 2012-07-10 14:32:33 -0700 | [diff] [blame] | 1289 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 +0000 | [diff] [blame] | 1290 | dev_server = DevServerRoot(_xbuddy) |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 1291 | health_checker_app = health_checker.Root(dev_server, options.static_dir) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1292 | |
Chris Sosa | 855b893 | 2013-08-21 13:24:55 -0700 | [diff] [blame] | 1293 | if options.pidfile: |
| 1294 | plugins.PIDFile(cherrypy.engine, options.pidfile).subscribe() |
| 1295 | |
Gilad Arnold | 11fbef4 | 2014-02-10 11:04:13 -0800 | [diff] [blame] | 1296 | if options.portfile: |
| 1297 | cherrypy_ext.PortFile(cherrypy.engine, options.portfile).subscribe() |
| 1298 | |
Dan Shi | afd5c6c | 2016-01-07 10:27:03 -0800 | [diff] [blame] | 1299 | if (options.android_build_credential and |
| 1300 | os.path.exists(options.android_build_credential)): |
| 1301 | try: |
| 1302 | with open(options.android_build_credential) as f: |
| 1303 | android_build.BuildAccessor.credential_info = json.load(f) |
| 1304 | except ValueError as e: |
| 1305 | _Log('Failed to load the android build credential: %s. Error: %s.' % |
| 1306 | (options.android_build_credential, e)) |
Congbin Guo | 3afae6c | 2019-08-13 16:29:42 -0700 | [diff] [blame] | 1307 | |
| 1308 | cherrypy.tree.mount(health_checker_app, '/check_health', |
| 1309 | config=health_checker.get_config()) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 1310 | cherrypy.quickstart(dev_server, config=_GetConfig(options)) |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 1311 | |
| 1312 | |
| 1313 | if __name__ == '__main__': |
| 1314 | main() |