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 |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 17 | from chromite.cbuildbot import commands |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 18 | from chromite.lib import build_target_util |
| 19 | from chromite.lib import chroot_lib |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 20 | from chromite.lib import constants |
| 21 | from chromite.lib import cros_build_lib |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 22 | from chromite.lib import cros_logging as logging |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 23 | from chromite.lib import sysroot_lib |
| 24 | from chromite.service import artifacts |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 25 | |
| 26 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 27 | def _GetImageDir(build_root, target): |
| 28 | """Return path containing images for the given build target. |
| 29 | |
Alex Klein | e2612a0 | 2019-04-18 13:51:06 -0600 | [diff] [blame] | 30 | TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case. |
| 31 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 32 | Args: |
| 33 | build_root (str): Path to checkout where build occurs. |
| 34 | target (str): Name of the build target. |
| 35 | |
| 36 | Returns: |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame^] | 37 | Path to the latest directory containing target images or None. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 38 | """ |
| 39 | image_dir = os.path.join(build_root, 'src/build/images', target, 'latest') |
| 40 | if not os.path.exists(image_dir): |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame^] | 41 | logging.warning('Expected to find image output for target %s at %s, but ' |
| 42 | 'path does not exist', target, image_dir) |
| 43 | return None |
| 44 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 45 | return image_dir |
| 46 | |
| 47 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 48 | @faux.all_empty |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 49 | @validate.require('build_target.name') |
| 50 | @validate.exists('output_dir') |
| 51 | @validate.validation_complete |
| 52 | def BundleImageArchives(input_proto, output_proto, _config): |
| 53 | """Create a .tar.xz archive for each image that has been created.""" |
| 54 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 55 | output_dir = input_proto.output_dir |
| 56 | image_dir = _GetImageDir(constants.SOURCE_ROOT, build_target.name) |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame^] | 57 | if image_dir is None: |
| 58 | return |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 59 | |
| 60 | archives = artifacts.ArchiveImages(image_dir, output_dir) |
| 61 | |
| 62 | for archive in archives: |
| 63 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 64 | |
| 65 | |
| 66 | @faux.all_empty |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 67 | @validate.require('build_target.name', 'output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 68 | @validate.exists('output_dir') |
| 69 | @validate.validation_complete |
| 70 | def BundleImageZip(input_proto, output_proto, _config): |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 71 | """Bundle image.zip. |
| 72 | |
| 73 | Args: |
| 74 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 75 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 76 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 77 | """ |
| 78 | target = input_proto.build_target.name |
| 79 | output_dir = input_proto.output_dir |
| 80 | image_dir = _GetImageDir(constants.SOURCE_ROOT, target) |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame^] | 81 | if image_dir is None: |
| 82 | return None |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 83 | |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 84 | archive = artifacts.BundleImageZip(output_dir, image_dir) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 85 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 86 | |
| 87 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 88 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 89 | @validate.require('build_target.name', 'output_dir') |
| 90 | @validate.exists('output_dir') |
| 91 | @validate.validation_complete |
| 92 | def BundleTestUpdatePayloads(input_proto, output_proto, _config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 93 | """Generate minimal update payloads for the build target for testing. |
| 94 | |
| 95 | Args: |
| 96 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 97 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 98 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 99 | """ |
| 100 | target = input_proto.build_target.name |
| 101 | output_dir = input_proto.output_dir |
| 102 | build_root = constants.SOURCE_ROOT |
| 103 | |
| 104 | # Use the first available image to create the update payload. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 105 | img_dir = _GetImageDir(build_root, target) |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame^] | 106 | if img_dir is None: |
| 107 | return None |
| 108 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 109 | img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV, |
| 110 | constants.IMAGE_TYPE_BASE] |
| 111 | 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] | 112 | 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] | 113 | 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] | 114 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 115 | if not valid_images: |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 116 | cros_build_lib.Die( |
| 117 | 'Expected to find an image of type among %r for target "%s" ' |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 118 | 'at path %s.', img_types, target, img_dir) |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 119 | image = valid_images[0] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 120 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 121 | payloads = artifacts.BundleTestUpdatePayloads(image, output_dir) |
| 122 | for payload in payloads: |
| 123 | output_proto.artifacts.add().path = payload |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 124 | |
| 125 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 126 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 127 | @validate.require('output_dir') |
| 128 | @validate.exists('output_dir') |
| 129 | def BundleAutotestFiles(input_proto, output_proto, config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 130 | """Tar the autotest files for a build target. |
| 131 | |
| 132 | Args: |
| 133 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 134 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 135 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 136 | """ |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 137 | output_dir = input_proto.output_dir |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 138 | target = input_proto.build_target.name |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 139 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 140 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 141 | if target: |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 142 | sysroot_path = os.path.join('/build', target) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 143 | else: |
| 144 | # New style call, use chroot and sysroot. |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 145 | sysroot_path = input_proto.sysroot.path |
| 146 | if not sysroot_path: |
| 147 | cros_build_lib.Die('sysroot.path is required.') |
| 148 | |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 149 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 150 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 151 | # TODO(saklein): Switch to the validate_only decorator when legacy handling |
| 152 | # is removed. |
| 153 | if config.validate_only: |
| 154 | return controller.RETURN_CODE_VALID_INPUT |
| 155 | |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 156 | if not sysroot.Exists(chroot=chroot): |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 157 | cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path) |
| 158 | |
| 159 | try: |
| 160 | # Note that this returns the full path to *multiple* tarballs. |
Alex Klein | e21a095 | 2019-08-23 16:08:16 -0600 | [diff] [blame] | 161 | archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 162 | except artifacts.Error as e: |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 163 | cros_build_lib.Die(e) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 164 | |
| 165 | for archive in archives.values(): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 166 | output_proto.artifacts.add().path = archive |
| 167 | |
| 168 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 169 | @faux.all_empty |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 170 | @validate.require('output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 171 | @validate.exists('output_dir') |
| 172 | def BundleTastFiles(input_proto, output_proto, config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 173 | """Tar the tast files for a build target. |
| 174 | |
| 175 | Args: |
| 176 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 177 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 178 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 179 | """ |
| 180 | target = input_proto.build_target.name |
| 181 | output_dir = input_proto.output_dir |
| 182 | build_root = constants.SOURCE_ROOT |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 183 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 184 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 185 | sysroot_path = input_proto.sysroot.path |
| 186 | |
| 187 | # TODO(saklein) Cleanup legacy handling after it has been switched over. |
| 188 | if target: |
| 189 | # Legacy handling. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 190 | chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot')) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 191 | sysroot_path = os.path.join('/build', target) |
| 192 | |
| 193 | # New handling - chroot & sysroot based. |
| 194 | # TODO(saklein) Switch this to the require decorator when legacy is removed. |
| 195 | if not sysroot_path: |
| 196 | cros_build_lib.Die('sysroot.path is required.') |
| 197 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 198 | # TODO(saklein): Switch to the validation_complete decorator when legacy |
| 199 | # handling is removed. |
| 200 | if config.validate_only: |
| 201 | return controller.RETURN_CODE_VALID_INPUT |
| 202 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 203 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 204 | if not sysroot.Exists(chroot=chroot): |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 205 | cros_build_lib.Die('Sysroot must exist.') |
| 206 | |
| 207 | archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 208 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 209 | if archive is None: |
| 210 | cros_build_lib.Die( |
| 211 | 'Could not bundle Tast files. ' |
| 212 | 'No Tast directories found for %s.', target) |
| 213 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 214 | output_proto.artifacts.add().path = archive |
| 215 | |
| 216 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 217 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 218 | @validate.require('build_target.name', 'output_dir') |
| 219 | @validate.exists('output_dir') |
| 220 | @validate.validation_complete |
| 221 | def BundlePinnedGuestImages(input_proto, output_proto, _config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 222 | """Tar the pinned guest images for a build target. |
| 223 | |
| 224 | Args: |
| 225 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 226 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 227 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 228 | """ |
| 229 | target = input_proto.build_target.name |
| 230 | output_dir = input_proto.output_dir |
| 231 | build_root = constants.SOURCE_ROOT |
| 232 | |
Alex Klein | e2612a0 | 2019-04-18 13:51:06 -0600 | [diff] [blame] | 233 | # TODO(crbug.com/954299): Replace with a chromite/service implementation. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 234 | archive = commands.BuildPinnedGuestImagesTarball(build_root, target, |
| 235 | output_dir) |
| 236 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 237 | if archive is None: |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 238 | logging.warning('Found no pinned guest images for %s.', target) |
| 239 | return |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 240 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 241 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 242 | |
| 243 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 244 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 245 | @validate.require('sysroot.path') |
| 246 | @validate.validation_complete |
| 247 | def FetchPinnedGuestImages(input_proto, output_proto, _config): |
Alex Klein | 7bf0ecb | 2019-06-25 10:04:15 -0600 | [diff] [blame] | 248 | """Get the pinned guest image information.""" |
| 249 | sysroot_path = input_proto.sysroot.path |
Alex Klein | 7bf0ecb | 2019-06-25 10:04:15 -0600 | [diff] [blame] | 250 | |
| 251 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 252 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 253 | |
| 254 | if not chroot.exists(): |
| 255 | cros_build_lib.Die('Chroot does not exist: %s', chroot.path) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 256 | elif not sysroot.Exists(chroot=chroot): |
Alex Klein | 7bf0ecb | 2019-06-25 10:04:15 -0600 | [diff] [blame] | 257 | cros_build_lib.Die('Sysroot does not exist: %s', |
| 258 | chroot.full_path(sysroot.path)) |
| 259 | |
| 260 | pins = artifacts.FetchPinnedGuestImages(chroot, sysroot) |
| 261 | |
| 262 | for pin in pins: |
| 263 | pinned_image = output_proto.pinned_images.add() |
| 264 | pinned_image.filename = pin.filename |
| 265 | pinned_image.uri = pin.uri |
| 266 | |
| 267 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 268 | @faux.all_empty |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 269 | @validate.require('output_dir', 'sysroot.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 270 | @validate.exists('output_dir') |
| 271 | @validate.validation_complete |
| 272 | def BundleFirmware(input_proto, output_proto, _config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 273 | """Tar the firmware images for a build target. |
| 274 | |
| 275 | Args: |
| 276 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 277 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 278 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 279 | """ |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 280 | output_dir = input_proto.output_dir |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 281 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 282 | sysroot_path = input_proto.sysroot.path |
| 283 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 284 | |
| 285 | if not chroot.exists(): |
| 286 | cros_build_lib.Die('Chroot does not exist: %s', chroot.path) |
| 287 | elif not sysroot.Exists(chroot=chroot): |
| 288 | cros_build_lib.Die('Sysroot does not exist: %s', |
| 289 | chroot.full_path(sysroot.path)) |
| 290 | |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 291 | archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 292 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 293 | if archive is None: |
| 294 | cros_build_lib.Die( |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 295 | 'Could not create firmware archive. No firmware found for %s.', |
| 296 | sysroot_path) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 297 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 298 | output_proto.artifacts.add().path = archive |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 299 | |
| 300 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 301 | @faux.all_empty |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 302 | @validate.exists('output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 303 | def BundleEbuildLogs(input_proto, output_proto, config): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 304 | """Tar the ebuild logs for a build target. |
| 305 | |
| 306 | Args: |
| 307 | input_proto (BundleRequest): The input proto. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 308 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 309 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 310 | """ |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 311 | output_dir = input_proto.output_dir |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 312 | sysroot_path = input_proto.sysroot.path |
| 313 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 314 | |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 315 | # TODO(mmortensen) Cleanup legacy handling after it has been switched over. |
| 316 | target = input_proto.build_target.name |
| 317 | if target: |
| 318 | # Legacy handling. |
| 319 | build_root = constants.SOURCE_ROOT |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 320 | chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot')) |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 321 | sysroot_path = os.path.join('/build', target) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 322 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 323 | # TODO(saklein): Switch to validation_complete decorator after legacy |
| 324 | # handling has been cleaned up. |
| 325 | if config.validate_only: |
| 326 | return controller.RETURN_CODE_VALID_INPUT |
| 327 | |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 328 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 329 | archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 330 | if archive is None: |
| 331 | cros_build_lib.Die( |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 332 | 'Could not create ebuild logs archive. No logs found for %s.', |
| 333 | sysroot.path) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 334 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 335 | |
| 336 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 337 | @faux.all_empty |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 338 | @validate.exists('output_dir') |
| 339 | @validate.validation_complete |
| 340 | def BundleChromeOSConfig(input_proto, output_proto, _config): |
| 341 | """Output the ChromeOS Config payload for a build target. |
| 342 | |
| 343 | Args: |
| 344 | input_proto (BundleRequest): The input proto. |
| 345 | output_proto (BundleResponse): The output proto. |
| 346 | _config (api_config.ApiConfig): The API call config. |
| 347 | """ |
| 348 | output_dir = input_proto.output_dir |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 349 | sysroot_path = input_proto.sysroot.path |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 350 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 351 | |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 352 | # TODO(mmortensen) Cleanup legacy handling after it has been switched over. |
| 353 | target = input_proto.build_target.name |
| 354 | if target: |
| 355 | # Legacy handling. |
| 356 | build_root = constants.SOURCE_ROOT |
| 357 | chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot')) |
| 358 | sysroot_path = os.path.join('/build', target) |
| 359 | |
| 360 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 361 | chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir) |
| 362 | if chromeos_config is None: |
| 363 | cros_build_lib.Die( |
| 364 | 'Could not create ChromeOS Config payload. No config found for %s.', |
| 365 | sysroot.path) |
| 366 | output_proto.artifacts.add().path = os.path.join(output_dir, chromeos_config) |
| 367 | |
| 368 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 369 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 370 | @validate.require('output_dir', 'sysroot.build_target.name', 'sysroot.path') |
| 371 | @validate.exists('output_dir') |
| 372 | @validate.validation_complete |
| 373 | def BundleSimpleChromeArtifacts(input_proto, output_proto, _config): |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 374 | """Create the simple chrome artifacts.""" |
| 375 | # Required args. |
| 376 | sysroot_path = input_proto.sysroot.path |
| 377 | build_target_name = input_proto.sysroot.build_target.name |
| 378 | output_dir = input_proto.output_dir |
| 379 | |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 380 | # Optional args. |
| 381 | chroot_path = input_proto.chroot.path or constants.DEFAULT_CHROOT_PATH |
| 382 | cache_dir = input_proto.chroot.cache_dir |
| 383 | |
| 384 | # Build out the argument instances. |
| 385 | build_target = build_target_util.BuildTarget(build_target_name) |
| 386 | chroot = chroot_lib.Chroot(path=chroot_path, cache_dir=cache_dir) |
| 387 | # Sysroot.path needs to be the fully qualified path, including the chroot. |
| 388 | full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep)) |
| 389 | sysroot = sysroot_lib.Sysroot(full_sysroot_path) |
| 390 | |
| 391 | # Quick sanity check that the sysroot exists before we go on. |
| 392 | if not sysroot.Exists(): |
| 393 | cros_build_lib.Die('The sysroot does not exist.') |
| 394 | |
| 395 | try: |
| 396 | results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot, |
| 397 | build_target, output_dir) |
| 398 | except artifacts.Error as e: |
| 399 | cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s', |
| 400 | type(e), e) |
| 401 | |
| 402 | for file_name in results: |
| 403 | output_proto.artifacts.add().path = file_name |
| 404 | |
| 405 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 406 | @faux.all_empty |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 407 | @validate.require('chroot.path', 'test_results_dir', 'output_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 408 | @validate.exists('output_dir') |
| 409 | @validate.validation_complete |
| 410 | def BundleVmFiles(input_proto, output_proto, _config): |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 411 | """Tar VM disk and memory files. |
| 412 | |
| 413 | Args: |
| 414 | input_proto (SysrootBundleRequest): The input proto. |
| 415 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 416 | _config (api_config.ApiConfig): The API call config. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 417 | """ |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 418 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 419 | test_results_dir = input_proto.test_results_dir |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 420 | output_dir = input_proto.output_dir |
| 421 | |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 422 | archives = artifacts.BundleVmFiles( |
| 423 | chroot, test_results_dir, output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 424 | for archive in archives: |
| 425 | output_proto.artifacts.add().path = archive |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 426 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 427 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 428 | _VALID_ARTIFACT_TYPES = [toolchain_pb2.BENCHMARK_AFDO, |
| 429 | toolchain_pb2.ORDERFILE] |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 430 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 431 | @validate.require('build_target.name', 'output_dir') |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 432 | @validate.is_in('artifact_type', _VALID_ARTIFACT_TYPES) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 433 | @validate.exists('output_dir') |
Tiancong Wang | 2ade793 | 2019-09-27 14:15:40 -0700 | [diff] [blame] | 434 | @validate.exists('chroot.chrome_dir') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 435 | @validate.validation_complete |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 436 | def BundleAFDOGenerationArtifacts(input_proto, output_proto, _config): |
| 437 | """Generic function for creating tarballs of both AFDO and orerfile. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 438 | |
| 439 | Args: |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 440 | input_proto (BundleChromeAFDORequest): The input proto. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 441 | output_proto (BundleResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 442 | _config (api_config.ApiConfig): The API call config. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 443 | """ |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 444 | |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 445 | # Required args. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 446 | build_target = build_target_util.BuildTarget(input_proto.build_target.name) |
Tiancong Wang | 2ade793 | 2019-09-27 14:15:40 -0700 | [diff] [blame] | 447 | chrome_root = input_proto.chroot.chrome_dir |
| 448 | if not chrome_root: |
| 449 | cros_build_lib.Die('chrome_root is not included in chroot') |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 450 | output_dir = input_proto.output_dir |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 451 | artifact_type = input_proto.artifact_type |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 452 | |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 453 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 454 | |
| 455 | try: |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 456 | is_orderfile = bool(artifact_type is toolchain_pb2.ORDERFILE) |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 457 | results = artifacts.BundleAFDOGenerationArtifacts( |
Tiancong Wang | 2ade793 | 2019-09-27 14:15:40 -0700 | [diff] [blame] | 458 | is_orderfile, chroot, chrome_root, |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 459 | build_target, output_dir) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 460 | except artifacts.Error as e: |
| 461 | cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s', |
| 462 | type(e), e) |
| 463 | |
| 464 | for file_name in results: |
| 465 | output_proto.artifacts.add().path = file_name |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 466 | |
| 467 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 468 | @faux.all_empty |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 469 | @validate.exists('output_dir') |
| 470 | def ExportCpeReport(input_proto, output_proto, config): |
| 471 | """Export a CPE report. |
| 472 | |
| 473 | Args: |
| 474 | input_proto (BundleRequest): The input proto. |
| 475 | output_proto (BundleResponse): The output proto. |
| 476 | config (api_config.ApiConfig): The API call config. |
| 477 | """ |
| 478 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 479 | output_dir = input_proto.output_dir |
| 480 | |
| 481 | if input_proto.build_target.name: |
| 482 | # Legacy handling - use the default sysroot path for the build target. |
| 483 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 484 | sysroot = sysroot_lib.Sysroot(build_target.root) |
| 485 | elif input_proto.sysroot.path: |
| 486 | sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) |
| 487 | else: |
| 488 | # TODO(saklein): Switch to validate decorators once legacy handling can be |
| 489 | # cleaned up. |
| 490 | cros_build_lib.Die('sysroot.path is required.') |
| 491 | |
| 492 | if config.validate_only: |
| 493 | return controller.RETURN_CODE_VALID_INPUT |
| 494 | |
| 495 | cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir) |
| 496 | |
| 497 | output_proto.artifacts.add().path = cpe_result.report |
| 498 | output_proto.artifacts.add().path = cpe_result.warnings |