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