blob: d7c2b640ac40d916327b1ab218eb58ef26dd5658 [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
Alex Kleincb541e82019-06-26 15:06:11 -060010import functools
Evan Hernandezf388cbf2019-04-01 11:15:23 -060011import os
12
Alex Klein2b236722019-06-19 15:44:26 -060013from chromite.api import validate
Alex Klein238d8862019-05-07 11:32:46 -060014from chromite.api.controller import controller_util
Evan Hernandezf388cbf2019-04-01 11:15:23 -060015from chromite.cbuildbot import commands
Alex Klein6504eca2019-04-18 15:37:56 -060016from chromite.cbuildbot.stages import vm_test_stages
Alex Klein2275d692019-04-23 16:04:12 -060017from chromite.lib import build_target_util
18from chromite.lib import chroot_lib
Evan Hernandezf388cbf2019-04-01 11:15:23 -060019from chromite.lib import constants
20from chromite.lib import cros_build_lib
Evan Hernandezde445982019-04-22 13:42:34 -060021from chromite.lib import cros_logging as logging
Alex Klein2275d692019-04-23 16:04:12 -060022from chromite.lib import sysroot_lib
23from chromite.service import artifacts
Evan Hernandezf388cbf2019-04-01 11:15:23 -060024
25
Evan Hernandez9f125ac2019-04-08 17:18:47 -060026def _GetImageDir(build_root, target):
27 """Return path containing images for the given build target.
28
Alex Kleine2612a02019-04-18 13:51:06 -060029 TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case.
30
Evan Hernandez9f125ac2019-04-08 17:18:47 -060031 Args:
32 build_root (str): Path to checkout where build occurs.
33 target (str): Name of the build target.
34
35 Returns:
36 Path to the directory containing target images.
37
38 Raises:
39 DieSystemExit: If the image dir does not exist.
40 """
41 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
42 if not os.path.exists(image_dir):
43 cros_build_lib.Die('Expected to find image output for target %s at %s, '
44 'but path does not exist' % (target, image_dir))
45 return image_dir
46
47
48def BundleImageZip(input_proto, output_proto):
49 """Bundle image.zip.
50
51 Args:
52 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060053 output_proto (BundleResponse): The output proto.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060054 """
55 target = input_proto.build_target.name
56 output_dir = input_proto.output_dir
57 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
58 archive = commands.BuildImageZip(output_dir, image_dir)
59 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
60
61
Evan Hernandezf388cbf2019-04-01 11:15:23 -060062def BundleTestUpdatePayloads(input_proto, output_proto):
63 """Generate minimal update payloads for the build target for testing.
64
65 Args:
66 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060067 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060068 """
69 target = input_proto.build_target.name
70 output_dir = input_proto.output_dir
71 build_root = constants.SOURCE_ROOT
72
73 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060074 img_dir = _GetImageDir(build_root, target)
Alex Kleincb541e82019-06-26 15:06:11 -060075 img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
76 constants.IMAGE_TYPE_BASE]
77 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
78 img_paths = map(functools.partial(os.path.join, img_dir), img_names)
79 valid_images = filter(os.path.exists, img_paths)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060080
Alex Kleincb541e82019-06-26 15:06:11 -060081 if not valid_images:
Evan Hernandezf388cbf2019-04-01 11:15:23 -060082 cros_build_lib.Die(
83 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -060084 'at path %s.', img_types, target, img_dir)
Alex Kleincb541e82019-06-26 15:06:11 -060085 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -060086
Alex Kleincb541e82019-06-26 15:06:11 -060087 payloads = artifacts.BundleTestUpdatePayloads(image, output_dir)
88 for payload in payloads:
89 output_proto.artifacts.add().path = payload
Evan Hernandezf388cbf2019-04-01 11:15:23 -060090
91
92def BundleAutotestFiles(input_proto, output_proto):
93 """Tar the autotest files for a build target.
94
95 Args:
96 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060097 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060098 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -060099 output_dir = input_proto.output_dir
Alex Klein238d8862019-05-07 11:32:46 -0600100 if not output_dir:
101 cros_build_lib.Die('output_dir is required.')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600102
Alex Klein238d8862019-05-07 11:32:46 -0600103 target = input_proto.build_target.name
104 if target:
105 # Legacy call, build out sysroot path from default source root and the
106 # build target.
107 target = input_proto.build_target.name
108 build_root = constants.SOURCE_ROOT
109 sysroot_path = os.path.join(build_root, constants.DEFAULT_CHROOT_DIR,
110 'build', target)
111 sysroot = sysroot_lib.Sysroot(sysroot_path)
112 else:
113 # New style call, use chroot and sysroot.
114 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600115
Alex Klein238d8862019-05-07 11:32:46 -0600116 sysroot_path = input_proto.sysroot.path
117 if not sysroot_path:
118 cros_build_lib.Die('sysroot.path is required.')
119
120 # Since we're staying outside the chroot, prepend the chroot path to the
121 # sysroot path so we have a valid full path to the sysroot.
122 sysroot = sysroot_lib.Sysroot(os.path.join(chroot.path,
123 sysroot_path.lstrip(os.sep)))
124
125 if not sysroot.Exists():
126 cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path)
127
128 try:
129 # Note that this returns the full path to *multiple* tarballs.
130 archives = artifacts.BundleAutotestFiles(sysroot, output_dir)
131 except artifacts.Error as e:
132 cros_build_lib.Die(e.message)
133
134 for archive in archives.values():
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600135 output_proto.artifacts.add().path = archive
136
137
138def BundleTastFiles(input_proto, output_proto):
139 """Tar the tast files for a build target.
140
141 Args:
142 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600143 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600144 """
145 target = input_proto.build_target.name
146 output_dir = input_proto.output_dir
147 build_root = constants.SOURCE_ROOT
Evan Hernandez36589c62019-04-05 18:14:42 -0600148 cwd = os.path.join(build_root, 'chroot/build', target, 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600149
150 # Note that unlike the functions below, this returns the full path
151 # to the tarball.
Alex Kleine2612a02019-04-18 13:51:06 -0600152 # TODO(crbug.com/954294): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600153 archive = commands.BuildTastBundleTarball(build_root, cwd, output_dir)
154
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600155 if archive is None:
156 cros_build_lib.Die(
157 'Could not bundle Tast files. '
158 'No Tast directories found for %s.', target)
159
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600160 output_proto.artifacts.add().path = archive
161
162
163def BundlePinnedGuestImages(input_proto, output_proto):
164 """Tar the pinned guest images for a build target.
165
166 Args:
167 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600168 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600169 """
170 target = input_proto.build_target.name
171 output_dir = input_proto.output_dir
172 build_root = constants.SOURCE_ROOT
173
Alex Kleine2612a02019-04-18 13:51:06 -0600174 # TODO(crbug.com/954299): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600175 archive = commands.BuildPinnedGuestImagesTarball(build_root, target,
176 output_dir)
177
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600178 if archive is None:
Evan Hernandezde445982019-04-22 13:42:34 -0600179 logging.warning('Found no pinned guest images for %s.', target)
180 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600181
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600182 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
183
184
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600185def FetchPinnedGuestImages(input_proto, output_proto):
186 """Get the pinned guest image information."""
187 sysroot_path = input_proto.sysroot.path
188 if not sysroot_path:
189 cros_build_lib.Die('sysroot.path is required.')
190
191 chroot = controller_util.ParseChroot(input_proto.chroot)
192 sysroot = sysroot_lib.Sysroot(sysroot_path)
193
194 if not chroot.exists():
195 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
196
197 if not sysroot.Exists(chroot=chroot):
198 cros_build_lib.Die('Sysroot does not exist: %s',
199 chroot.full_path(sysroot.path))
200
201 pins = artifacts.FetchPinnedGuestImages(chroot, sysroot)
202
203 for pin in pins:
204 pinned_image = output_proto.pinned_images.add()
205 pinned_image.filename = pin.filename
206 pinned_image.uri = pin.uri
207
208
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600209def BundleFirmware(input_proto, output_proto):
210 """Tar the firmware images for a build target.
211
212 Args:
213 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600214 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600215 """
Michael Mortensen568ec132019-06-27 16:56:21 +0000216 target = input_proto.build_target.name
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600217 output_dir = input_proto.output_dir
Michael Mortensen568ec132019-06-27 16:56:21 +0000218 build_root = constants.SOURCE_ROOT
219
220 # TODO(crbug.com/954300): Replace with a chromite/service implementation.
221 archive = commands.BuildFirmwareArchive(build_root, target, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600222
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600223 if archive is None:
224 cros_build_lib.Die(
Michael Mortensen568ec132019-06-27 16:56:21 +0000225 'Could not create firmware archive. No firmware found for %s.', target)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600226
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600227 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
228
229
230def BundleEbuildLogs(input_proto, output_proto):
231 """Tar the ebuild logs for a build target.
232
233 Args:
234 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600235 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600236 """
237 target = input_proto.build_target.name
238 output_dir = input_proto.output_dir
Evan Hernandeza478d802019-04-08 15:08:24 -0600239
240 # commands.BuildEbuildLogsTarball conflates "buildroot" with sysroot.
241 # Adjust accordingly...
242 build_root = os.path.join(constants.SOURCE_ROOT, 'chroot', 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600243
Alex Kleine2612a02019-04-18 13:51:06 -0600244 # TODO(crbug.com/954303): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600245 archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir)
246
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600247 if archive is None:
248 cros_build_lib.Die(
249 'Could not create ebuild logs archive. No logs found for %s.', target)
250
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600251 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600252
253
Alex Klein2275d692019-04-23 16:04:12 -0600254def BundleSimpleChromeArtifacts(input_proto, output_proto):
255 """Create the simple chrome artifacts."""
256 # Required args.
257 sysroot_path = input_proto.sysroot.path
258 build_target_name = input_proto.sysroot.build_target.name
259 output_dir = input_proto.output_dir
260
261 if not build_target_name:
262 cros_build_lib.Die('build_target.name is required')
263 if not output_dir:
264 cros_build_lib.Die('output_dir is required.')
265 if not os.path.exists(output_dir):
266 cros_build_lib.Die('output_dir (%s) does not exist.', output_dir)
267 if not sysroot_path:
268 cros_build_lib.Die('sysroot.path is required.')
269
270 # Optional args.
271 chroot_path = input_proto.chroot.path or constants.DEFAULT_CHROOT_PATH
272 cache_dir = input_proto.chroot.cache_dir
273
274 # Build out the argument instances.
275 build_target = build_target_util.BuildTarget(build_target_name)
276 chroot = chroot_lib.Chroot(path=chroot_path, cache_dir=cache_dir)
277 # Sysroot.path needs to be the fully qualified path, including the chroot.
278 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
279 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
280
281 # Quick sanity check that the sysroot exists before we go on.
282 if not sysroot.Exists():
283 cros_build_lib.Die('The sysroot does not exist.')
284
285 try:
286 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
287 build_target, output_dir)
288 except artifacts.Error as e:
289 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
290 type(e), e)
291
292 for file_name in results:
293 output_proto.artifacts.add().path = file_name
294
295
Alex Klein2b236722019-06-19 15:44:26 -0600296@validate.require('chroot.path', 'sysroot.path', 'test_results_dir',
297 'output_dir')
Alex Klein6504eca2019-04-18 15:37:56 -0600298def BundleVmFiles(input_proto, output_proto):
299 """Tar VM disk and memory files.
300
301 Args:
302 input_proto (SysrootBundleRequest): The input proto.
303 output_proto (BundleResponse): The output proto.
304 """
305 chroot = input_proto.chroot.path
Alex Klein2b236722019-06-19 15:44:26 -0600306 sysroot = input_proto.sysroot.path.lstrip(os.sep)
307 test_results_dir = input_proto.test_results_dir.lstrip(os.sep)
Alex Klein6504eca2019-04-18 15:37:56 -0600308 output_dir = input_proto.output_dir
309
Alex Klein6504eca2019-04-18 15:37:56 -0600310 # TODO(crbug.com/954344): Replace with a chromite/service implementation.
Alex Klein2b236722019-06-19 15:44:26 -0600311 image_dir = os.path.join(chroot, sysroot, test_results_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600312 archives = vm_test_stages.ArchiveVMFilesFromImageDir(image_dir, output_dir)
313 for archive in archives:
314 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700315
316def BundleOrderfileGenerationArtifacts(input_proto, output_proto):
317 """Create tarballs of all the artifacts of orderfile_generate builder.
318
319 Args:
320 input_proto (BundleRequest): The input proto.
321 output_proto (BundleResponse): The output proto.
322 """
323 # Required args.
324 build_target_name = input_proto.build_target.name
325 output_dir = input_proto.output_dir
326 chrome_version = input_proto.chrome_version
327
328 if not build_target_name:
329 cros_build_lib.Die('build_target.name is required.')
330 if not chrome_version:
331 cros_build_lib.Die('chrome_version is required.')
332 if not output_dir:
333 cros_build_lib.Die('output_dir is required.')
334 elif not os.path.isdir(output_dir):
335 cros_build_lib.Die('output_dir does not exist.')
336
337 chroot = controller_util.ParseChroot(input_proto.chroot)
338
339 try:
340 results = artifacts.BundleOrderfileGenerationArtifacts(
341 chroot, input_proto.build_target, chrome_version, output_dir)
342 except artifacts.Error as e:
343 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
344 type(e), e)
345
346 for file_name in results:
347 output_proto.artifacts.add().path = file_name