Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Implements ArtifactService.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 13 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 14 | from chromite.api import validate |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 15 | from chromite.api.controller import controller_util |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 16 | from chromite.api.gen.chromite.api import toolchain_pb2 |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 17 | from chromite.lib import chroot_lib |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 18 | from chromite.lib import constants |
| 19 | from chromite.lib import cros_build_lib |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 20 | from chromite.lib import cros_logging as logging |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 21 | from chromite.lib import sysroot_lib |
| 22 | from chromite.service import artifacts |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 23 | |
| 24 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 25 | def _GetImageDir(build_root, target): |
| 26 | """Return path containing images for the given build target. |
| 27 | |
Alex Klein | e2612a0 | 2019-04-18 13:51:06 -0600 | [diff] [blame] | 28 | TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case. |
| 29 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 30 | Args: |
| 31 | build_root (str): Path to checkout where build occurs. |
| 32 | target (str): Name of the build target. |
| 33 | |
| 34 | Returns: |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 35 | Path to the latest directory containing target images or None. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 36 | """ |
| 37 | image_dir = os.path.join(build_root, 'src/build/images', target, 'latest') |
| 38 | if not os.path.exists(image_dir): |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 39 | logging.warning('Expected to find image output for target %s at %s, but ' |
| 40 | 'path does not exist', target, image_dir) |
| 41 | return None |
| 42 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 43 | return image_dir |
| 44 | |
| 45 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 46 | def _BundleImageArchivesResponse(input_proto, output_proto, _config): |
| 47 | """Add artifact paths to a successful response.""" |
| 48 | output_proto.artifacts.add().path = os.path.join(input_proto.output_dir, |
| 49 | 'path0.tar.xz') |
| 50 | output_proto.artifacts.add().path = os.path.join(input_proto.output_dir, |
| 51 | 'path1.tar.xz') |
| 52 | |
| 53 | |
| 54 | @faux.success(_BundleImageArchivesResponse) |
| 55 | @faux.empty_error |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 56 | @validate.require('build_target.name') |
| 57 | @validate.exists('output_dir') |
| 58 | @validate.validation_complete |
| 59 | def BundleImageArchives(input_proto, output_proto, _config): |
| 60 | """Create a .tar.xz archive for each image that has been created.""" |
| 61 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 62 | output_dir = input_proto.output_dir |
| 63 | image_dir = _GetImageDir(constants.SOURCE_ROOT, build_target.name) |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 64 | if image_dir is None: |
| 65 | return |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 66 | |
| 67 | archives = artifacts.ArchiveImages(image_dir, output_dir) |
| 68 | |
| 69 | for archive in archives: |
| 70 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 71 | |
| 72 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 73 | def _BundleImageZipResponse(input_proto, output_proto, _config): |
| 74 | """Add artifact zip files to a successful response.""" |
| 75 | output_proto.artifacts.add().path = os.path.join(input_proto.output_dir, |
| 76 | 'image.zip') |
| 77 | |
| 78 | |
| 79 | @faux.success(_BundleImageZipResponse) |
| 80 | @faux.empty_error |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 81 | @validate.require('build_target.name', 'output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 82 | @validate.exists('output_dir') |
| 83 | @validate.validation_complete |
| 84 | def BundleImageZip(input_proto, output_proto, _config): |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 85 | """Bundle image.zip. |
| 86 | |
| 87 | Args: |
| 88 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 89 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 90 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 91 | """ |
| 92 | target = input_proto.build_target.name |
| 93 | output_dir = input_proto.output_dir |
| 94 | image_dir = _GetImageDir(constants.SOURCE_ROOT, target) |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 95 | if image_dir is None: |
| 96 | return None |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 97 | |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 98 | archive = artifacts.BundleImageZip(output_dir, image_dir) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 99 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 100 | |
| 101 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 102 | def _BundleTestUpdatePayloadsResponse(input_proto, output_proto, _config): |
| 103 | """Add test payload files to a successful response.""" |
| 104 | output_proto.artifacts.add().path = os.path.join(input_proto.output_dir, |
| 105 | 'payload1.bin') |
| 106 | |
| 107 | |
| 108 | @faux.success(_BundleTestUpdatePayloadsResponse) |
| 109 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 110 | @validate.require('build_target.name', 'output_dir') |
| 111 | @validate.exists('output_dir') |
| 112 | @validate.validation_complete |
| 113 | def BundleTestUpdatePayloads(input_proto, output_proto, _config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 114 | """Generate minimal update payloads for the build target for testing. |
| 115 | |
| 116 | Args: |
| 117 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 118 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 119 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 120 | """ |
| 121 | target = input_proto.build_target.name |
| 122 | output_dir = input_proto.output_dir |
| 123 | build_root = constants.SOURCE_ROOT |
| 124 | |
| 125 | # Use the first available image to create the update payload. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 126 | img_dir = _GetImageDir(build_root, target) |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 127 | if img_dir is None: |
| 128 | return None |
| 129 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 130 | img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV, |
| 131 | constants.IMAGE_TYPE_BASE] |
| 132 | img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types] |
Mike Frysinger | 66ce413 | 2019-07-17 22:52:52 -0400 | [diff] [blame] | 133 | img_paths = [os.path.join(img_dir, x) for x in img_names] |
Mike Frysinger | a552be4 | 2018-08-17 14:39:32 -0400 | [diff] [blame] | 134 | valid_images = [x for x in img_paths if os.path.exists(x)] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 135 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 136 | if not valid_images: |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 137 | cros_build_lib.Die( |
| 138 | 'Expected to find an image of type among %r for target "%s" ' |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 139 | 'at path %s.', img_types, target, img_dir) |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 140 | image = valid_images[0] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 141 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 142 | payloads = artifacts.BundleTestUpdatePayloads(image, output_dir) |
| 143 | for payload in payloads: |
| 144 | output_proto.artifacts.add().path = payload |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 145 | |
| 146 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 147 | def _BundleAutotestFilesResponse(input_proto, output_proto, _config): |
| 148 | """Add test autotest files to a successful response.""" |
| 149 | output_proto.artifacts.add().path = os.path.join(input_proto.output_dir, |
| 150 | 'autotest-a.tar.gz') |
| 151 | |
| 152 | |
| 153 | @faux.success(_BundleAutotestFilesResponse) |
| 154 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 155 | @validate.require('output_dir') |
| 156 | @validate.exists('output_dir') |
| 157 | def BundleAutotestFiles(input_proto, output_proto, config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 158 | """Tar the autotest files for a build target. |
| 159 | |
| 160 | Args: |
| 161 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 162 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 163 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 164 | """ |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 165 | output_dir = input_proto.output_dir |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 166 | target = input_proto.build_target.name |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 167 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 168 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 169 | if target: |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 170 | sysroot_path = os.path.join('/build', target) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 171 | else: |
| 172 | # New style call, use chroot and sysroot. |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 173 | sysroot_path = input_proto.sysroot.path |
| 174 | if not sysroot_path: |
| 175 | cros_build_lib.Die('sysroot.path is required.') |
| 176 | |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 177 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 178 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 179 | # TODO(saklein): Switch to the validate_only decorator when legacy handling |
| 180 | # is removed. |
| 181 | if config.validate_only: |
| 182 | return controller.RETURN_CODE_VALID_INPUT |
| 183 | |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 184 | if not sysroot.Exists(chroot=chroot): |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 185 | cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path) |
| 186 | |
| 187 | try: |
| 188 | # Note that this returns the full path to *multiple* tarballs. |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 189 | archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 190 | except artifacts.Error as e: |
Alex Klein | 0389031 | 2020-06-30 09:59:50 -0600 | [diff] [blame] | 191 | logging.warning(e) |
| 192 | return |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 193 | |
| 194 | for archive in archives.values(): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 195 | output_proto.artifacts.add().path = archive |
| 196 | |
| 197 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 198 | def _BundleTastFilesResponse(input_proto, output_proto, _config): |
| 199 | """Add test tast files to a successful response.""" |
| 200 | output_proto.artifacts.add().path = os.path.join(input_proto.output_dir, |
| 201 | 'tast_bundles.tar.gz') |
| 202 | |
| 203 | |
| 204 | @faux.success(_BundleTastFilesResponse) |
| 205 | @faux.empty_error |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 206 | @validate.require('output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 207 | @validate.exists('output_dir') |
| 208 | def BundleTastFiles(input_proto, output_proto, config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 209 | """Tar the tast files for a build target. |
| 210 | |
| 211 | Args: |
| 212 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 213 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 214 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 215 | """ |
| 216 | target = input_proto.build_target.name |
| 217 | output_dir = input_proto.output_dir |
| 218 | build_root = constants.SOURCE_ROOT |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 219 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 220 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 221 | sysroot_path = input_proto.sysroot.path |
| 222 | |
| 223 | # TODO(saklein) Cleanup legacy handling after it has been switched over. |
| 224 | if target: |
| 225 | # Legacy handling. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 226 | chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot')) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 227 | sysroot_path = os.path.join('/build', target) |
| 228 | |
| 229 | # New handling - chroot & sysroot based. |
| 230 | # TODO(saklein) Switch this to the require decorator when legacy is removed. |
| 231 | if not sysroot_path: |
| 232 | cros_build_lib.Die('sysroot.path is required.') |
| 233 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 234 | # TODO(saklein): Switch to the validation_complete decorator when legacy |
| 235 | # handling is removed. |
| 236 | if config.validate_only: |
| 237 | return controller.RETURN_CODE_VALID_INPUT |
| 238 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 239 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 240 | if not sysroot.Exists(chroot=chroot): |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 241 | cros_build_lib.Die('Sysroot must exist.') |
| 242 | |
| 243 | archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 244 | |
LaMont Jones | b9793cd | 2020-06-11 08:14:46 -0600 | [diff] [blame] | 245 | if archive: |
| 246 | output_proto.artifacts.add().path = archive |
| 247 | else: |
| 248 | logging.warning('Found no tast files for %s.', target) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 249 | |
| 250 | |
Fergus Dall | 34d74e1 | 2020-09-29 15:52:32 +1000 | [diff] [blame^] | 251 | def BundlePinnedGuestImages(_input_proto, _output_proto, _config): |
| 252 | # TODO(crbug/1034529): Remove this endpoint |
| 253 | pass |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 254 | |
Fergus Dall | 34d74e1 | 2020-09-29 15:52:32 +1000 | [diff] [blame^] | 255 | def FetchPinnedGuestImageUris(_input_proto, _output_proto, _config): |
| 256 | # TODO(crbug/1034529): Remove this endpoint |
| 257 | pass |
Alex Klein | 7bf0ecb | 2019-06-25 10:04:15 -0600 | [diff] [blame] | 258 | |
| 259 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 260 | def _BundleFirmwareResponse(input_proto, output_proto, _config): |
| 261 | """Add test firmware image files to a successful response.""" |
| 262 | output_proto.artifacts.add().path = os.path.join( |
| 263 | input_proto.output_dir, 'firmware.tar.gz') |
| 264 | |
| 265 | |
| 266 | @faux.success(_BundleFirmwareResponse) |
| 267 | @faux.empty_error |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 268 | @validate.require('output_dir', 'sysroot.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 269 | @validate.exists('output_dir') |
| 270 | @validate.validation_complete |
| 271 | def BundleFirmware(input_proto, output_proto, _config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 272 | """Tar the firmware images for a build target. |
| 273 | |
| 274 | Args: |
| 275 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 276 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 277 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 278 | """ |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 279 | output_dir = input_proto.output_dir |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 280 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 281 | sysroot_path = input_proto.sysroot.path |
| 282 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 283 | |
| 284 | if not chroot.exists(): |
| 285 | cros_build_lib.Die('Chroot does not exist: %s', chroot.path) |
| 286 | elif not sysroot.Exists(chroot=chroot): |
| 287 | cros_build_lib.Die('Sysroot does not exist: %s', |
| 288 | chroot.full_path(sysroot.path)) |
| 289 | |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 290 | archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 291 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 292 | if archive is None: |
| 293 | cros_build_lib.Die( |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 294 | 'Could not create firmware archive. No firmware found for %s.', |
| 295 | sysroot_path) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 296 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 297 | output_proto.artifacts.add().path = archive |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 298 | |
| 299 | |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 300 | def _BundleFpmcuUnittestsResponse(input_proto, output_proto, _config): |
| 301 | """Add fingerprint MCU unittest binaries to a successful response.""" |
| 302 | output_proto.artifacts.add().path = os.path.join( |
| 303 | input_proto.output_dir, 'fpmcu_unittests.tar.gz') |
| 304 | |
| 305 | |
| 306 | @faux.success(_BundleFpmcuUnittestsResponse) |
| 307 | @faux.empty_error |
| 308 | @validate.require('output_dir', 'sysroot.path') |
| 309 | @validate.exists('output_dir') |
| 310 | @validate.validation_complete |
| 311 | def BundleFpmcuUnittests(input_proto, output_proto, _config): |
| 312 | """Tar the fingerprint MCU unittest binaries for a build target. |
| 313 | |
| 314 | Args: |
| 315 | input_proto (BundleRequest): The input proto. |
| 316 | output_proto (BundleResponse): The output proto. |
| 317 | _config (api_config.ApiConfig): The API call config. |
| 318 | """ |
| 319 | output_dir = input_proto.output_dir |
| 320 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 321 | sysroot_path = input_proto.sysroot.path |
| 322 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 323 | |
| 324 | if not chroot.exists(): |
| 325 | cros_build_lib.Die('Chroot does not exist: %s', chroot.path) |
| 326 | elif not sysroot.Exists(chroot=chroot): |
| 327 | cros_build_lib.Die('Sysroot does not exist: %s', |
| 328 | chroot.full_path(sysroot.path)) |
| 329 | |
| 330 | archive = artifacts.BundleFpmcuUnittests(chroot, sysroot, output_dir) |
| 331 | |
| 332 | if archive is None: |
| 333 | logging.warning( |
| 334 | 'No fpmcu unittests found for %s.', sysroot_path) |
| 335 | return |
| 336 | |
| 337 | output_proto.artifacts.add().path = archive |
| 338 | |
| 339 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 340 | def _BundleEbuildLogsResponse(input_proto, output_proto, _config): |
| 341 | """Add test log files to a successful response.""" |
| 342 | output_proto.artifacts.add().path = os.path.join( |
| 343 | input_proto.output_dir, 'ebuild-logs.tar.gz') |
| 344 | |
| 345 | |
| 346 | @faux.success(_BundleEbuildLogsResponse) |
| 347 | @faux.empty_error |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 348 | @validate.exists('output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 349 | def BundleEbuildLogs(input_proto, output_proto, config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 350 | """Tar the ebuild logs for a build target. |
| 351 | |
| 352 | Args: |
| 353 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 354 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 355 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 356 | """ |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 357 | output_dir = input_proto.output_dir |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 358 | sysroot_path = input_proto.sysroot.path |
| 359 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 360 | |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 361 | # TODO(mmortensen) Cleanup legacy handling after it has been switched over. |
| 362 | target = input_proto.build_target.name |
| 363 | if target: |
| 364 | # Legacy handling. |
| 365 | build_root = constants.SOURCE_ROOT |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 366 | chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot')) |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 367 | sysroot_path = os.path.join('/build', target) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 368 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 369 | # TODO(saklein): Switch to validation_complete decorator after legacy |
| 370 | # handling has been cleaned up. |
| 371 | if config.validate_only: |
| 372 | return controller.RETURN_CODE_VALID_INPUT |
| 373 | |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 374 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 375 | archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 376 | if archive is None: |
| 377 | cros_build_lib.Die( |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 378 | 'Could not create ebuild logs archive. No logs found for %s.', |
| 379 | sysroot.path) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 380 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 381 | |
| 382 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 383 | def _BundleChromeOSConfigResponse(input_proto, output_proto, _config): |
| 384 | """Add test config files to a successful response.""" |
| 385 | output_proto.artifacts.add().path = os.path.join( |
| 386 | input_proto.output_dir, 'config.yaml') |
| 387 | |
| 388 | |
| 389 | @faux.success(_BundleChromeOSConfigResponse) |
| 390 | @faux.empty_error |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 391 | @validate.exists('output_dir') |
| 392 | @validate.validation_complete |
| 393 | def BundleChromeOSConfig(input_proto, output_proto, _config): |
| 394 | """Output the ChromeOS Config payload for a build target. |
| 395 | |
| 396 | Args: |
| 397 | input_proto (BundleRequest): The input proto. |
| 398 | output_proto (BundleResponse): The output proto. |
| 399 | _config (api_config.ApiConfig): The API call config. |
| 400 | """ |
| 401 | output_dir = input_proto.output_dir |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 402 | sysroot_path = input_proto.sysroot.path |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 403 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 404 | |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 405 | # TODO(mmortensen) Cleanup legacy handling after it has been switched over. |
| 406 | target = input_proto.build_target.name |
| 407 | if target: |
| 408 | # Legacy handling. |
| 409 | build_root = constants.SOURCE_ROOT |
| 410 | chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot')) |
| 411 | sysroot_path = os.path.join('/build', target) |
| 412 | |
| 413 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 414 | chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir) |
| 415 | if chromeos_config is None: |
| 416 | cros_build_lib.Die( |
| 417 | 'Could not create ChromeOS Config payload. No config found for %s.', |
| 418 | sysroot.path) |
| 419 | output_proto.artifacts.add().path = os.path.join(output_dir, chromeos_config) |
| 420 | |
| 421 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 422 | def _BundleSimpleChromeArtifactsResponse(input_proto, output_proto, _config): |
| 423 | """Add test simple chrome files to a successful response.""" |
| 424 | output_proto.artifacts.add().path = os.path.join( |
| 425 | input_proto.output_dir, 'simple_chrome.txt') |
| 426 | |
| 427 | |
| 428 | @faux.success(_BundleSimpleChromeArtifactsResponse) |
| 429 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 430 | @validate.require('output_dir', 'sysroot.build_target.name', 'sysroot.path') |
| 431 | @validate.exists('output_dir') |
| 432 | @validate.validation_complete |
| 433 | def BundleSimpleChromeArtifacts(input_proto, output_proto, _config): |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 434 | """Create the simple chrome artifacts.""" |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 435 | sysroot_path = input_proto.sysroot.path |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 436 | output_dir = input_proto.output_dir |
| 437 | |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 438 | # Build out the argument instances. |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 439 | build_target = controller_util.ParseBuildTarget( |
| 440 | input_proto.sysroot.build_target) |
| 441 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 442 | # Sysroot.path needs to be the fully qualified path, including the chroot. |
| 443 | full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep)) |
| 444 | sysroot = sysroot_lib.Sysroot(full_sysroot_path) |
| 445 | |
| 446 | # Quick sanity check that the sysroot exists before we go on. |
| 447 | if not sysroot.Exists(): |
| 448 | cros_build_lib.Die('The sysroot does not exist.') |
| 449 | |
| 450 | try: |
| 451 | results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot, |
| 452 | build_target, output_dir) |
| 453 | except artifacts.Error as e: |
| 454 | cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s', |
| 455 | type(e), e) |
| 456 | |
| 457 | for file_name in results: |
| 458 | output_proto.artifacts.add().path = file_name |
| 459 | |
| 460 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 461 | def _BundleVmFilesResponse(input_proto, output_proto, _config): |
| 462 | """Add test vm files to a successful response.""" |
| 463 | output_proto.artifacts.add().path = os.path.join( |
| 464 | input_proto.output_dir, 'f1.tar') |
| 465 | |
| 466 | |
| 467 | @faux.success(_BundleVmFilesResponse) |
| 468 | @faux.empty_error |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 469 | @validate.require('chroot.path', 'test_results_dir', 'output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 470 | @validate.exists('output_dir') |
| 471 | @validate.validation_complete |
| 472 | def BundleVmFiles(input_proto, output_proto, _config): |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 473 | """Tar VM disk and memory files. |
| 474 | |
| 475 | Args: |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 476 | input_proto (BundleVmFilesRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 477 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 478 | _config (api_config.ApiConfig): The API call config. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 479 | """ |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 480 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 481 | test_results_dir = input_proto.test_results_dir |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 482 | output_dir = input_proto.output_dir |
| 483 | |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 484 | archives = artifacts.BundleVmFiles( |
| 485 | chroot, test_results_dir, output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 486 | for archive in archives: |
| 487 | output_proto.artifacts.add().path = archive |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 488 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 489 | def _BundleAFDOGenerationArtifactsResponse(input_proto, output_proto, _config): |
| 490 | """Add test tarball AFDO file to a successful response.""" |
| 491 | output_proto.artifacts.add().path = os.path.join( |
| 492 | input_proto.output_dir, 'artifact1') |
| 493 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 494 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 495 | _VALID_ARTIFACT_TYPES = [toolchain_pb2.BENCHMARK_AFDO, |
| 496 | toolchain_pb2.ORDERFILE] |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 497 | @faux.success(_BundleAFDOGenerationArtifactsResponse) |
| 498 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 499 | @validate.require('build_target.name', 'output_dir') |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 500 | @validate.is_in('artifact_type', _VALID_ARTIFACT_TYPES) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 501 | @validate.exists('output_dir') |
Tiancong Wang | 2ade793 | 2019-09-27 14:15:40 -0700 | [diff] [blame] | 502 | @validate.exists('chroot.chrome_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 503 | @validate.validation_complete |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 504 | def BundleAFDOGenerationArtifacts(input_proto, output_proto, _config): |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 505 | """Generic function for creating tarballs of both AFDO and orderfile. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 506 | |
| 507 | Args: |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 508 | input_proto (BundleChromeAFDORequest): The input proto. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 509 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 510 | _config (api_config.ApiConfig): The API call config. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 511 | """ |
Tiancong Wang | 2ade793 | 2019-09-27 14:15:40 -0700 | [diff] [blame] | 512 | chrome_root = input_proto.chroot.chrome_dir |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 513 | output_dir = input_proto.output_dir |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 514 | artifact_type = input_proto.artifact_type |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 515 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 516 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 517 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 518 | |
| 519 | try: |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 520 | is_orderfile = bool(artifact_type is toolchain_pb2.ORDERFILE) |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 521 | results = artifacts.BundleAFDOGenerationArtifacts( |
Tiancong Wang | 2ade793 | 2019-09-27 14:15:40 -0700 | [diff] [blame] | 522 | is_orderfile, chroot, chrome_root, |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 523 | build_target, output_dir) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 524 | except artifacts.Error as e: |
| 525 | cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s', |
| 526 | type(e), e) |
| 527 | |
| 528 | for file_name in results: |
| 529 | output_proto.artifacts.add().path = file_name |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 530 | |
| 531 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 532 | def _ExportCpeReportResponse(input_proto, output_proto, _config): |
| 533 | """Add test cpe results to a successful response.""" |
| 534 | output_proto.artifacts.add().path = os.path.join( |
| 535 | input_proto.output_dir, 'cpe_report.txt') |
| 536 | output_proto.artifacts.add().path = os.path.join( |
| 537 | input_proto.output_dir, 'cpe_warnings.txt') |
| 538 | |
| 539 | |
| 540 | @faux.success(_ExportCpeReportResponse) |
| 541 | @faux.empty_error |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 542 | @validate.exists('output_dir') |
| 543 | def ExportCpeReport(input_proto, output_proto, config): |
| 544 | """Export a CPE report. |
| 545 | |
| 546 | Args: |
| 547 | input_proto (BundleRequest): The input proto. |
| 548 | output_proto (BundleResponse): The output proto. |
| 549 | config (api_config.ApiConfig): The API call config. |
| 550 | """ |
| 551 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 552 | output_dir = input_proto.output_dir |
| 553 | |
| 554 | if input_proto.build_target.name: |
| 555 | # Legacy handling - use the default sysroot path for the build target. |
| 556 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 557 | sysroot = sysroot_lib.Sysroot(build_target.root) |
| 558 | elif input_proto.sysroot.path: |
| 559 | sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) |
| 560 | else: |
| 561 | # TODO(saklein): Switch to validate decorators once legacy handling can be |
| 562 | # cleaned up. |
| 563 | cros_build_lib.Die('sysroot.path is required.') |
| 564 | |
| 565 | if config.validate_only: |
| 566 | return controller.RETURN_CODE_VALID_INPUT |
| 567 | |
| 568 | cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir) |
| 569 | |
| 570 | output_proto.artifacts.add().path = cpe_result.report |
| 571 | output_proto.artifacts.add().path = cpe_result.warnings |
Shao-Chuan Lee | a44dddc | 2020-10-30 17:16:55 +0900 | [diff] [blame] | 572 | |
| 573 | |
| 574 | def _BundleGceTarballResponse(input_proto, output_proto, _config): |
| 575 | """Add artifact tarball to a successful response.""" |
| 576 | output_proto.artifacts.add().path = os.path.join(input_proto.output_dir, |
| 577 | constants.TEST_IMAGE_GCE_TAR) |
| 578 | |
| 579 | |
| 580 | @faux.success(_BundleGceTarballResponse) |
| 581 | @faux.empty_error |
| 582 | @validate.require('build_target.name', 'output_dir') |
| 583 | @validate.exists('output_dir') |
| 584 | @validate.validation_complete |
| 585 | def BundleGceTarball(input_proto, output_proto, _config): |
| 586 | """Bundle the test image into a tarball suitable for importing into GCE. |
| 587 | |
| 588 | Args: |
| 589 | input_proto (BundleRequest): The input proto. |
| 590 | output_proto (BundleResponse): The output proto. |
| 591 | _config (api_config.ApiConfig): The API call config. |
| 592 | """ |
| 593 | target = input_proto.build_target.name |
| 594 | output_dir = input_proto.output_dir |
| 595 | image_dir = _GetImageDir(constants.SOURCE_ROOT, target) |
| 596 | if image_dir is None: |
| 597 | return None |
| 598 | |
| 599 | tarball = artifacts.BundleGceTarball(output_dir, image_dir) |
| 600 | output_proto.artifacts.add().path = tarball |