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