blob: 8ffa8f411c1b1c628f99589bcd96efc4e787d7aa [file] [log] [blame]
Evan Hernandezf388cbf2019-04-01 11:15:23 -06001# -*- 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
8from __future__ import print_function
9
10import os
11
12from chromite.cbuildbot import commands
13from chromite.lib import constants
14from chromite.lib import cros_build_lib
15from chromite.lib import osutils
16
17
Evan Hernandez9f125ac2019-04-08 17:18:47 -060018def _GetImageDir(build_root, target):
19 """Return path containing images for the given build target.
20
Alex Kleine2612a02019-04-18 13:51:06 -060021 TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case.
22
Evan Hernandez9f125ac2019-04-08 17:18:47 -060023 Args:
24 build_root (str): Path to checkout where build occurs.
25 target (str): Name of the build target.
26
27 Returns:
28 Path to the directory containing target images.
29
30 Raises:
31 DieSystemExit: If the image dir does not exist.
32 """
33 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
34 if not os.path.exists(image_dir):
35 cros_build_lib.Die('Expected to find image output for target %s at %s, '
36 'but path does not exist' % (target, image_dir))
37 return image_dir
38
39
40def BundleImageZip(input_proto, output_proto):
41 """Bundle image.zip.
42
43 Args:
44 input_proto (BundleRequest): The input proto.
45 output_proto (BundleRequest): The output proto.
46 """
47 target = input_proto.build_target.name
48 output_dir = input_proto.output_dir
49 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
50 archive = commands.BuildImageZip(output_dir, image_dir)
51 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
52
53
Evan Hernandezf388cbf2019-04-01 11:15:23 -060054def BundleTestUpdatePayloads(input_proto, output_proto):
55 """Generate minimal update payloads for the build target for testing.
56
57 Args:
58 input_proto (BundleRequest): The input proto.
59 output_proto (BundleRequest): The output proto.
60 """
61 target = input_proto.build_target.name
62 output_dir = input_proto.output_dir
63 build_root = constants.SOURCE_ROOT
64
65 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060066 img_dir = _GetImageDir(build_root, target)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060067 img_types = [
68 constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
69 constants.IMAGE_TYPE_BASE
70 ]
71 img_paths = []
72 for img_type in img_types:
Evan Hernandez9f125ac2019-04-08 17:18:47 -060073 img_path = os.path.join(img_dir, constants.IMAGE_TYPE_TO_NAME[img_type])
Evan Hernandezf388cbf2019-04-01 11:15:23 -060074 if os.path.exists(img_path):
75 img_paths.append(img_path)
76
77 if not img_paths:
78 cros_build_lib.Die(
79 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -060080 'at path %s.', img_types, target, img_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060081 img = img_paths[0]
82
83 # Unfortunately, the relevant commands.py functions do not return
84 # a list of generated files. As a workaround, we have commands.py
85 # put the files in a separate temporary directory so we can catalog them,
86 # then move them to the output dir.
Alex Kleine2612a02019-04-18 13:51:06 -060087 # TODO(crbug.com/954283): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060088 with osutils.TempDir() as temp:
89 commands.GeneratePayloads(img, temp, full=True, stateful=True, delta=True)
90 commands.GenerateQuickProvisionPayloads(img, temp)
91 for path in osutils.DirectoryIterator(temp):
92 if os.path.isfile(path):
93 rel_path = os.path.relpath(path, temp)
94 output_proto.artifacts.add().path = os.path.join(output_dir, rel_path)
Evan Hernandezb04e2aa2019-04-08 16:55:54 -060095 osutils.CopyDirContents(temp, output_dir, allow_nonempty=True)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060096
97
98def BundleAutotestFiles(input_proto, output_proto):
99 """Tar the autotest files for a build target.
100
101 Args:
102 input_proto (BundleRequest): The input proto.
103 output_proto (BundleRequest): The output proto.
104 """
105 target = input_proto.build_target.name
106 output_dir = input_proto.output_dir
107 build_root = constants.SOURCE_ROOT
Evan Hernandez36589c62019-04-05 18:14:42 -0600108 cwd = os.path.join(build_root, 'chroot/build', target, 'usr/local/build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600109
110 # Note that unlike the functions below, this returns the full path
111 # to *multiple* tarballs.
Alex Kleine2612a02019-04-18 13:51:06 -0600112 # TODO(crbug.com/954289): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600113 archives = commands.BuildAutotestTarballsForHWTest(build_root, cwd,
114 output_dir)
115
116 for archive in archives:
117 output_proto.artifacts.add().path = archive
118
119
120def BundleTastFiles(input_proto, output_proto):
121 """Tar the tast files for a build target.
122
123 Args:
124 input_proto (BundleRequest): The input proto.
125 output_proto (BundleRequest): The output proto.
126 """
127 target = input_proto.build_target.name
128 output_dir = input_proto.output_dir
129 build_root = constants.SOURCE_ROOT
Evan Hernandez36589c62019-04-05 18:14:42 -0600130 cwd = os.path.join(build_root, 'chroot/build', target, 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600131
132 # Note that unlike the functions below, this returns the full path
133 # to the tarball.
Alex Kleine2612a02019-04-18 13:51:06 -0600134 # TODO(crbug.com/954294): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600135 archive = commands.BuildTastBundleTarball(build_root, cwd, output_dir)
136
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600137 if archive is None:
138 cros_build_lib.Die(
139 'Could not bundle Tast files. '
140 'No Tast directories found for %s.', target)
141
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600142 output_proto.artifacts.add().path = archive
143
144
145def BundlePinnedGuestImages(input_proto, output_proto):
146 """Tar the pinned guest images for a build target.
147
148 Args:
149 input_proto (BundleRequest): The input proto.
150 output_proto (BundleRequest): The output proto.
151 """
152 target = input_proto.build_target.name
153 output_dir = input_proto.output_dir
154 build_root = constants.SOURCE_ROOT
155
Alex Kleine2612a02019-04-18 13:51:06 -0600156 # TODO(crbug.com/954299): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600157 archive = commands.BuildPinnedGuestImagesTarball(build_root, target,
158 output_dir)
159
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600160 if archive is None:
161 cros_build_lib.Die(
162 'Could not bundle pinned guest images. '
163 'No relevant files found for %s.', target)
164
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600165 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
166
167
168def BundleFirmware(input_proto, output_proto):
169 """Tar the firmware images for a build target.
170
171 Args:
172 input_proto (BundleRequest): The input proto.
173 output_proto (BundleRequest): The output proto.
174 """
175 target = input_proto.build_target.name
176 output_dir = input_proto.output_dir
177 build_root = constants.SOURCE_ROOT
178
Alex Kleine2612a02019-04-18 13:51:06 -0600179 # TODO(crbug.com/954300): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600180 archive = commands.BuildFirmwareArchive(build_root, target, output_dir)
181
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600182 if archive is None:
183 cros_build_lib.Die(
184 'Could not create firmware archive. No firmware found for %s.', target)
185
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600186 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
187
188
189def BundleEbuildLogs(input_proto, output_proto):
190 """Tar the ebuild logs for a build target.
191
192 Args:
193 input_proto (BundleRequest): The input proto.
194 output_proto (BundleRequest): The output proto.
195 """
196 target = input_proto.build_target.name
197 output_dir = input_proto.output_dir
Evan Hernandeza478d802019-04-08 15:08:24 -0600198
199 # commands.BuildEbuildLogsTarball conflates "buildroot" with sysroot.
200 # Adjust accordingly...
201 build_root = os.path.join(constants.SOURCE_ROOT, 'chroot', 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600202
Alex Kleine2612a02019-04-18 13:51:06 -0600203 # TODO(crbug.com/954303): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600204 archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir)
205
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600206 if archive is None:
207 cros_build_lib.Die(
208 'Could not create ebuild logs archive. No logs found for %s.', target)
209
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600210 output_proto.artifacts.add().path = os.path.join(output_dir, archive)