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 | |
| 12 | from chromite.cbuildbot import commands |
| 13 | from chromite.lib import constants |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import osutils |
| 16 | |
| 17 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 18 | def _GetImageDir(build_root, target): |
| 19 | """Return path containing images for the given build target. |
| 20 | |
| 21 | Args: |
| 22 | build_root (str): Path to checkout where build occurs. |
| 23 | target (str): Name of the build target. |
| 24 | |
| 25 | Returns: |
| 26 | Path to the directory containing target images. |
| 27 | |
| 28 | Raises: |
| 29 | DieSystemExit: If the image dir does not exist. |
| 30 | """ |
| 31 | image_dir = os.path.join(build_root, 'src/build/images', target, 'latest') |
| 32 | if not os.path.exists(image_dir): |
| 33 | cros_build_lib.Die('Expected to find image output for target %s at %s, ' |
| 34 | 'but path does not exist' % (target, image_dir)) |
| 35 | return image_dir |
| 36 | |
| 37 | |
| 38 | def BundleImageZip(input_proto, output_proto): |
| 39 | """Bundle image.zip. |
| 40 | |
| 41 | Args: |
| 42 | input_proto (BundleRequest): The input proto. |
| 43 | output_proto (BundleRequest): The output proto. |
| 44 | """ |
| 45 | target = input_proto.build_target.name |
| 46 | output_dir = input_proto.output_dir |
| 47 | image_dir = _GetImageDir(constants.SOURCE_ROOT, target) |
| 48 | archive = commands.BuildImageZip(output_dir, image_dir) |
| 49 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 50 | |
| 51 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 52 | def BundleTestUpdatePayloads(input_proto, output_proto): |
| 53 | """Generate minimal update payloads for the build target for testing. |
| 54 | |
| 55 | Args: |
| 56 | input_proto (BundleRequest): The input proto. |
| 57 | output_proto (BundleRequest): The output proto. |
| 58 | """ |
| 59 | target = input_proto.build_target.name |
| 60 | output_dir = input_proto.output_dir |
| 61 | build_root = constants.SOURCE_ROOT |
| 62 | |
| 63 | # Use the first available image to create the update payload. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 64 | img_dir = _GetImageDir(build_root, target) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 65 | img_types = [ |
| 66 | constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV, |
| 67 | constants.IMAGE_TYPE_BASE |
| 68 | ] |
| 69 | img_paths = [] |
| 70 | for img_type in img_types: |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 71 | img_path = os.path.join(img_dir, constants.IMAGE_TYPE_TO_NAME[img_type]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 72 | if os.path.exists(img_path): |
| 73 | img_paths.append(img_path) |
| 74 | |
| 75 | if not img_paths: |
| 76 | cros_build_lib.Die( |
| 77 | 'Expected to find an image of type among %r for target "%s" ' |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 78 | 'at path %s.', img_types, target, img_dir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 79 | img = img_paths[0] |
| 80 | |
| 81 | # Unfortunately, the relevant commands.py functions do not return |
| 82 | # a list of generated files. As a workaround, we have commands.py |
| 83 | # put the files in a separate temporary directory so we can catalog them, |
| 84 | # then move them to the output dir. |
| 85 | # TODO(saklein): Repalce with a chromite/service implementation. |
| 86 | with osutils.TempDir() as temp: |
| 87 | commands.GeneratePayloads(img, temp, full=True, stateful=True, delta=True) |
| 88 | commands.GenerateQuickProvisionPayloads(img, temp) |
| 89 | for path in osutils.DirectoryIterator(temp): |
| 90 | if os.path.isfile(path): |
| 91 | rel_path = os.path.relpath(path, temp) |
| 92 | output_proto.artifacts.add().path = os.path.join(output_dir, rel_path) |
Evan Hernandez | b04e2aa | 2019-04-08 16:55:54 -0600 | [diff] [blame] | 93 | osutils.CopyDirContents(temp, output_dir, allow_nonempty=True) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def BundleAutotestFiles(input_proto, output_proto): |
| 97 | """Tar the autotest files for a build target. |
| 98 | |
| 99 | Args: |
| 100 | input_proto (BundleRequest): The input proto. |
| 101 | output_proto (BundleRequest): The output proto. |
| 102 | """ |
| 103 | target = input_proto.build_target.name |
| 104 | output_dir = input_proto.output_dir |
| 105 | build_root = constants.SOURCE_ROOT |
Evan Hernandez | 36589c6 | 2019-04-05 18:14:42 -0600 | [diff] [blame] | 106 | cwd = os.path.join(build_root, 'chroot/build', target, 'usr/local/build') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 107 | |
| 108 | # Note that unlike the functions below, this returns the full path |
| 109 | # to *multiple* tarballs. |
| 110 | # TODO(saklein): Replace with a chromite/service implementation. |
| 111 | archives = commands.BuildAutotestTarballsForHWTest(build_root, cwd, |
| 112 | output_dir) |
| 113 | |
| 114 | for archive in archives: |
| 115 | output_proto.artifacts.add().path = archive |
| 116 | |
| 117 | |
| 118 | def BundleTastFiles(input_proto, output_proto): |
| 119 | """Tar the tast files for a build target. |
| 120 | |
| 121 | Args: |
| 122 | input_proto (BundleRequest): The input proto. |
| 123 | output_proto (BundleRequest): The output proto. |
| 124 | """ |
| 125 | target = input_proto.build_target.name |
| 126 | output_dir = input_proto.output_dir |
| 127 | build_root = constants.SOURCE_ROOT |
Evan Hernandez | 36589c6 | 2019-04-05 18:14:42 -0600 | [diff] [blame] | 128 | cwd = os.path.join(build_root, 'chroot/build', target, 'build') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 129 | |
| 130 | # Note that unlike the functions below, this returns the full path |
| 131 | # to the tarball. |
| 132 | # TODO(saklein): Replace with a chromite/service implementation. |
| 133 | archive = commands.BuildTastBundleTarball(build_root, cwd, output_dir) |
| 134 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 135 | if archive is None: |
| 136 | cros_build_lib.Die( |
| 137 | 'Could not bundle Tast files. ' |
| 138 | 'No Tast directories found for %s.', target) |
| 139 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 140 | output_proto.artifacts.add().path = archive |
| 141 | |
| 142 | |
| 143 | def BundlePinnedGuestImages(input_proto, output_proto): |
| 144 | """Tar the pinned guest images for a build target. |
| 145 | |
| 146 | Args: |
| 147 | input_proto (BundleRequest): The input proto. |
| 148 | output_proto (BundleRequest): The output proto. |
| 149 | """ |
| 150 | target = input_proto.build_target.name |
| 151 | output_dir = input_proto.output_dir |
| 152 | build_root = constants.SOURCE_ROOT |
| 153 | |
| 154 | # TODO(saklein): Replace with a chromite/service implementation. |
| 155 | archive = commands.BuildPinnedGuestImagesTarball(build_root, target, |
| 156 | output_dir) |
| 157 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 158 | if archive is None: |
| 159 | cros_build_lib.Die( |
| 160 | 'Could not bundle pinned guest images. ' |
| 161 | 'No relevant files found for %s.', target) |
| 162 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 163 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 164 | |
| 165 | |
| 166 | def BundleFirmware(input_proto, output_proto): |
| 167 | """Tar the firmware images for a build target. |
| 168 | |
| 169 | Args: |
| 170 | input_proto (BundleRequest): The input proto. |
| 171 | output_proto (BundleRequest): The output proto. |
| 172 | """ |
| 173 | target = input_proto.build_target.name |
| 174 | output_dir = input_proto.output_dir |
| 175 | build_root = constants.SOURCE_ROOT |
| 176 | |
| 177 | # TODO(saklein): Replace with a chromite/service implementation. |
| 178 | archive = commands.BuildFirmwareArchive(build_root, target, output_dir) |
| 179 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 180 | if archive is None: |
| 181 | cros_build_lib.Die( |
| 182 | 'Could not create firmware archive. No firmware found for %s.', target) |
| 183 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 184 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
| 185 | |
| 186 | |
| 187 | def BundleEbuildLogs(input_proto, output_proto): |
| 188 | """Tar the ebuild logs for a build target. |
| 189 | |
| 190 | Args: |
| 191 | input_proto (BundleRequest): The input proto. |
| 192 | output_proto (BundleRequest): The output proto. |
| 193 | """ |
| 194 | target = input_proto.build_target.name |
| 195 | output_dir = input_proto.output_dir |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 196 | |
| 197 | # commands.BuildEbuildLogsTarball conflates "buildroot" with sysroot. |
| 198 | # Adjust accordingly... |
| 199 | build_root = os.path.join(constants.SOURCE_ROOT, 'chroot', 'build') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 200 | |
| 201 | # TODO(saklein): Replace with a chromite/service implementation. |
| 202 | archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir) |
| 203 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 204 | if archive is None: |
| 205 | cros_build_lib.Die( |
| 206 | 'Could not create ebuild logs archive. No logs found for %s.', target) |
| 207 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 208 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |