blob: 6b49ba30275ef5b80bf49c0b379cbdee9b0a3548 [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
Michael Mortensen38675192019-06-28 16:52:55 +0000209@validate.require('output_dir', 'sysroot.path')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600210def BundleFirmware(input_proto, output_proto):
211 """Tar the firmware images for a build target.
212
213 Args:
214 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600215 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600216 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600217 output_dir = input_proto.output_dir
Michael Mortensen38675192019-06-28 16:52:55 +0000218 chroot = controller_util.ParseChroot(input_proto.chroot)
219 sysroot_path = input_proto.sysroot.path
220 sysroot = sysroot_lib.Sysroot(sysroot_path)
221 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, 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 Mortensen38675192019-06-28 16:52:55 +0000225 'Could not create firmware archive. No firmware found for %s.',
226 sysroot_path)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600227
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600228 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
229
230
231def BundleEbuildLogs(input_proto, output_proto):
232 """Tar the ebuild logs for a build target.
233
234 Args:
235 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600236 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600237 """
238 target = input_proto.build_target.name
239 output_dir = input_proto.output_dir
Evan Hernandeza478d802019-04-08 15:08:24 -0600240
241 # commands.BuildEbuildLogsTarball conflates "buildroot" with sysroot.
242 # Adjust accordingly...
243 build_root = os.path.join(constants.SOURCE_ROOT, 'chroot', 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600244
Alex Kleine2612a02019-04-18 13:51:06 -0600245 # TODO(crbug.com/954303): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600246 archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir)
247
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600248 if archive is None:
249 cros_build_lib.Die(
250 'Could not create ebuild logs archive. No logs found for %s.', target)
251
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600252 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600253
254
Alex Klein2275d692019-04-23 16:04:12 -0600255def BundleSimpleChromeArtifacts(input_proto, output_proto):
256 """Create the simple chrome artifacts."""
257 # Required args.
258 sysroot_path = input_proto.sysroot.path
259 build_target_name = input_proto.sysroot.build_target.name
260 output_dir = input_proto.output_dir
261
262 if not build_target_name:
263 cros_build_lib.Die('build_target.name is required')
264 if not output_dir:
265 cros_build_lib.Die('output_dir is required.')
266 if not os.path.exists(output_dir):
267 cros_build_lib.Die('output_dir (%s) does not exist.', output_dir)
268 if not sysroot_path:
269 cros_build_lib.Die('sysroot.path is required.')
270
271 # Optional args.
272 chroot_path = input_proto.chroot.path or constants.DEFAULT_CHROOT_PATH
273 cache_dir = input_proto.chroot.cache_dir
274
275 # Build out the argument instances.
276 build_target = build_target_util.BuildTarget(build_target_name)
277 chroot = chroot_lib.Chroot(path=chroot_path, cache_dir=cache_dir)
278 # Sysroot.path needs to be the fully qualified path, including the chroot.
279 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
280 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
281
282 # Quick sanity check that the sysroot exists before we go on.
283 if not sysroot.Exists():
284 cros_build_lib.Die('The sysroot does not exist.')
285
286 try:
287 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
288 build_target, output_dir)
289 except artifacts.Error as e:
290 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
291 type(e), e)
292
293 for file_name in results:
294 output_proto.artifacts.add().path = file_name
295
296
Alex Klein2b236722019-06-19 15:44:26 -0600297@validate.require('chroot.path', 'sysroot.path', 'test_results_dir',
298 'output_dir')
Alex Klein6504eca2019-04-18 15:37:56 -0600299def BundleVmFiles(input_proto, output_proto):
300 """Tar VM disk and memory files.
301
302 Args:
303 input_proto (SysrootBundleRequest): The input proto.
304 output_proto (BundleResponse): The output proto.
305 """
306 chroot = input_proto.chroot.path
Alex Klein2b236722019-06-19 15:44:26 -0600307 sysroot = input_proto.sysroot.path.lstrip(os.sep)
308 test_results_dir = input_proto.test_results_dir.lstrip(os.sep)
Alex Klein6504eca2019-04-18 15:37:56 -0600309 output_dir = input_proto.output_dir
310
Alex Klein6504eca2019-04-18 15:37:56 -0600311 # TODO(crbug.com/954344): Replace with a chromite/service implementation.
Alex Klein2b236722019-06-19 15:44:26 -0600312 image_dir = os.path.join(chroot, sysroot, test_results_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600313 archives = vm_test_stages.ArchiveVMFilesFromImageDir(image_dir, output_dir)
314 for archive in archives:
315 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700316
317def BundleOrderfileGenerationArtifacts(input_proto, output_proto):
318 """Create tarballs of all the artifacts of orderfile_generate builder.
319
320 Args:
321 input_proto (BundleRequest): The input proto.
322 output_proto (BundleResponse): The output proto.
323 """
324 # Required args.
325 build_target_name = input_proto.build_target.name
326 output_dir = input_proto.output_dir
327 chrome_version = input_proto.chrome_version
328
329 if not build_target_name:
330 cros_build_lib.Die('build_target.name is required.')
331 if not chrome_version:
332 cros_build_lib.Die('chrome_version is required.')
333 if not output_dir:
334 cros_build_lib.Die('output_dir is required.')
335 elif not os.path.isdir(output_dir):
336 cros_build_lib.Die('output_dir does not exist.')
337
338 chroot = controller_util.ParseChroot(input_proto.chroot)
339
340 try:
341 results = artifacts.BundleOrderfileGenerationArtifacts(
342 chroot, input_proto.build_target, chrome_version, output_dir)
343 except artifacts.Error as e:
344 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
345 type(e), e)
346
347 for file_name in results:
348 output_proto.artifacts.add().path = file_name