blob: 11d88d998e1191a621514770779125f2477956ce [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
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060013from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060014from chromite.api import validate
Alex Klein238d8862019-05-07 11:32:46 -060015from chromite.api.controller import controller_util
Tiancong Wang24a3df72019-08-20 15:48:51 -070016from chromite.api.gen.chromite.api import toolchain_pb2
Evan Hernandezf388cbf2019-04-01 11:15:23 -060017from chromite.cbuildbot import commands
Alex Klein2275d692019-04-23 16:04:12 -060018from chromite.lib import build_target_util
19from chromite.lib import chroot_lib
Evan Hernandezf388cbf2019-04-01 11:15:23 -060020from chromite.lib import constants
21from chromite.lib import cros_build_lib
Evan Hernandezde445982019-04-22 13:42:34 -060022from chromite.lib import cros_logging as logging
Alex Klein2275d692019-04-23 16:04:12 -060023from chromite.lib import sysroot_lib
24from chromite.service import artifacts
Evan Hernandezf388cbf2019-04-01 11:15:23 -060025
26
Evan Hernandez9f125ac2019-04-08 17:18:47 -060027def _GetImageDir(build_root, target):
28 """Return path containing images for the given build target.
29
Alex Kleine2612a02019-04-18 13:51:06 -060030 TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case.
31
Evan Hernandez9f125ac2019-04-08 17:18:47 -060032 Args:
33 build_root (str): Path to checkout where build occurs.
34 target (str): Name of the build target.
35
36 Returns:
Alex Kleind2bf1462019-10-24 16:37:04 -060037 Path to the latest directory containing target images or None.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060038 """
39 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
40 if not os.path.exists(image_dir):
Alex Kleind2bf1462019-10-24 16:37:04 -060041 logging.warning('Expected to find image output for target %s at %s, but '
42 'path does not exist', target, image_dir)
43 return None
44
Evan Hernandez9f125ac2019-04-08 17:18:47 -060045 return image_dir
46
47
Alex Klein076841b2019-08-29 15:19:39 -060048@faux.all_empty
Alex Kleind91e95a2019-09-17 10:39:02 -060049@validate.require('build_target.name')
50@validate.exists('output_dir')
51@validate.validation_complete
52def BundleImageArchives(input_proto, output_proto, _config):
53 """Create a .tar.xz archive for each image that has been created."""
54 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
55 output_dir = input_proto.output_dir
56 image_dir = _GetImageDir(constants.SOURCE_ROOT, build_target.name)
Alex Kleind2bf1462019-10-24 16:37:04 -060057 if image_dir is None:
58 return
Alex Kleind91e95a2019-09-17 10:39:02 -060059
60 archives = artifacts.ArchiveImages(image_dir, output_dir)
61
62 for archive in archives:
63 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
64
65
66@faux.all_empty
Michael Mortensen01910922019-07-24 14:48:10 -060067@validate.require('build_target.name', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -060068@validate.exists('output_dir')
69@validate.validation_complete
70def BundleImageZip(input_proto, output_proto, _config):
Evan Hernandez9f125ac2019-04-08 17:18:47 -060071 """Bundle image.zip.
72
73 Args:
74 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060075 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060076 _config (api_config.ApiConfig): The API call config.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060077 """
78 target = input_proto.build_target.name
79 output_dir = input_proto.output_dir
80 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
Alex Kleind2bf1462019-10-24 16:37:04 -060081 if image_dir is None:
82 return None
Alex Klein231d2da2019-07-22 16:44:45 -060083
Michael Mortensen01910922019-07-24 14:48:10 -060084 archive = artifacts.BundleImageZip(output_dir, image_dir)
Evan Hernandez9f125ac2019-04-08 17:18:47 -060085 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
86
87
Alex Klein076841b2019-08-29 15:19:39 -060088@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -060089@validate.require('build_target.name', 'output_dir')
90@validate.exists('output_dir')
91@validate.validation_complete
92def BundleTestUpdatePayloads(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -060093 """Generate minimal update payloads for the build target for testing.
94
95 Args:
96 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060097 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060098 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060099 """
100 target = input_proto.build_target.name
101 output_dir = input_proto.output_dir
102 build_root = constants.SOURCE_ROOT
103
104 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600105 img_dir = _GetImageDir(build_root, target)
Alex Kleind2bf1462019-10-24 16:37:04 -0600106 if img_dir is None:
107 return None
108
Alex Kleincb541e82019-06-26 15:06:11 -0600109 img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
110 constants.IMAGE_TYPE_BASE]
111 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
Mike Frysinger66ce4132019-07-17 22:52:52 -0400112 img_paths = [os.path.join(img_dir, x) for x in img_names]
Mike Frysingera552be42018-08-17 14:39:32 -0400113 valid_images = [x for x in img_paths if os.path.exists(x)]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600114
Alex Kleincb541e82019-06-26 15:06:11 -0600115 if not valid_images:
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600116 cros_build_lib.Die(
117 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600118 'at path %s.', img_types, target, img_dir)
Alex Kleincb541e82019-06-26 15:06:11 -0600119 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600120
Alex Kleincb541e82019-06-26 15:06:11 -0600121 payloads = artifacts.BundleTestUpdatePayloads(image, output_dir)
122 for payload in payloads:
123 output_proto.artifacts.add().path = payload
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600124
125
Alex Klein076841b2019-08-29 15:19:39 -0600126@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600127@validate.require('output_dir')
128@validate.exists('output_dir')
129def BundleAutotestFiles(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600130 """Tar the autotest files for a build target.
131
132 Args:
133 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600134 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600135 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600136 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600137 output_dir = input_proto.output_dir
Alex Klein238d8862019-05-07 11:32:46 -0600138 target = input_proto.build_target.name
Alex Kleine21a0952019-08-23 16:08:16 -0600139 chroot = controller_util.ParseChroot(input_proto.chroot)
140
Alex Klein238d8862019-05-07 11:32:46 -0600141 if target:
Alex Kleine21a0952019-08-23 16:08:16 -0600142 sysroot_path = os.path.join('/build', target)
Alex Klein238d8862019-05-07 11:32:46 -0600143 else:
144 # New style call, use chroot and sysroot.
Alex Klein238d8862019-05-07 11:32:46 -0600145 sysroot_path = input_proto.sysroot.path
146 if not sysroot_path:
147 cros_build_lib.Die('sysroot.path is required.')
148
Alex Kleine21a0952019-08-23 16:08:16 -0600149 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein238d8862019-05-07 11:32:46 -0600150
Alex Klein231d2da2019-07-22 16:44:45 -0600151 # TODO(saklein): Switch to the validate_only decorator when legacy handling
152 # is removed.
153 if config.validate_only:
154 return controller.RETURN_CODE_VALID_INPUT
155
Alex Kleine21a0952019-08-23 16:08:16 -0600156 if not sysroot.Exists(chroot=chroot):
Alex Klein238d8862019-05-07 11:32:46 -0600157 cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path)
158
159 try:
160 # Note that this returns the full path to *multiple* tarballs.
Alex Kleine21a0952019-08-23 16:08:16 -0600161 archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600162 except artifacts.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -0400163 cros_build_lib.Die(e)
Alex Klein238d8862019-05-07 11:32:46 -0600164
165 for archive in archives.values():
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600166 output_proto.artifacts.add().path = archive
167
168
Alex Klein076841b2019-08-29 15:19:39 -0600169@faux.all_empty
Alex Kleinb9d810b2019-07-01 12:38:02 -0600170@validate.require('output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600171@validate.exists('output_dir')
172def BundleTastFiles(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600173 """Tar the tast files for a build target.
174
175 Args:
176 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600177 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600178 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600179 """
180 target = input_proto.build_target.name
181 output_dir = input_proto.output_dir
182 build_root = constants.SOURCE_ROOT
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600183
Alex Kleinb9d810b2019-07-01 12:38:02 -0600184 chroot = controller_util.ParseChroot(input_proto.chroot)
185 sysroot_path = input_proto.sysroot.path
186
187 # TODO(saklein) Cleanup legacy handling after it has been switched over.
188 if target:
189 # Legacy handling.
Alex Klein171da612019-08-06 14:00:42 -0600190 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Alex Kleinb9d810b2019-07-01 12:38:02 -0600191 sysroot_path = os.path.join('/build', target)
192
193 # New handling - chroot & sysroot based.
194 # TODO(saklein) Switch this to the require decorator when legacy is removed.
195 if not sysroot_path:
196 cros_build_lib.Die('sysroot.path is required.')
197
Alex Klein231d2da2019-07-22 16:44:45 -0600198 # TODO(saklein): Switch to the validation_complete decorator when legacy
199 # handling is removed.
200 if config.validate_only:
201 return controller.RETURN_CODE_VALID_INPUT
202
Alex Kleinb9d810b2019-07-01 12:38:02 -0600203 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600204 if not sysroot.Exists(chroot=chroot):
Alex Kleinb9d810b2019-07-01 12:38:02 -0600205 cros_build_lib.Die('Sysroot must exist.')
206
207 archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600208
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600209 if archive is None:
210 cros_build_lib.Die(
211 'Could not bundle Tast files. '
212 'No Tast directories found for %s.', target)
213
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600214 output_proto.artifacts.add().path = archive
215
216
Alex Klein076841b2019-08-29 15:19:39 -0600217@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600218@validate.require('build_target.name', 'output_dir')
219@validate.exists('output_dir')
220@validate.validation_complete
221def BundlePinnedGuestImages(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600222 """Tar the pinned guest images for a build target.
223
224 Args:
225 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600226 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600227 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600228 """
229 target = input_proto.build_target.name
230 output_dir = input_proto.output_dir
231 build_root = constants.SOURCE_ROOT
232
Alex Kleine2612a02019-04-18 13:51:06 -0600233 # TODO(crbug.com/954299): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600234 archive = commands.BuildPinnedGuestImagesTarball(build_root, target,
235 output_dir)
236
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600237 if archive is None:
Evan Hernandezde445982019-04-22 13:42:34 -0600238 logging.warning('Found no pinned guest images for %s.', target)
239 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600240
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600241 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
242
243
Alex Klein076841b2019-08-29 15:19:39 -0600244@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600245@validate.require('sysroot.path')
246@validate.validation_complete
247def FetchPinnedGuestImages(input_proto, output_proto, _config):
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600248 """Get the pinned guest image information."""
249 sysroot_path = input_proto.sysroot.path
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600250
251 chroot = controller_util.ParseChroot(input_proto.chroot)
252 sysroot = sysroot_lib.Sysroot(sysroot_path)
253
254 if not chroot.exists():
255 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
Alex Klein231d2da2019-07-22 16:44:45 -0600256 elif not sysroot.Exists(chroot=chroot):
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600257 cros_build_lib.Die('Sysroot does not exist: %s',
258 chroot.full_path(sysroot.path))
259
260 pins = artifacts.FetchPinnedGuestImages(chroot, sysroot)
261
262 for pin in pins:
263 pinned_image = output_proto.pinned_images.add()
264 pinned_image.filename = pin.filename
265 pinned_image.uri = pin.uri
266
267
Alex Klein076841b2019-08-29 15:19:39 -0600268@faux.all_empty
Michael Mortensen38675192019-06-28 16:52:55 +0000269@validate.require('output_dir', 'sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600270@validate.exists('output_dir')
271@validate.validation_complete
272def BundleFirmware(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600273 """Tar the firmware images for a build target.
274
275 Args:
276 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600277 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600278 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600279 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600280 output_dir = input_proto.output_dir
Michael Mortensen38675192019-06-28 16:52:55 +0000281 chroot = controller_util.ParseChroot(input_proto.chroot)
282 sysroot_path = input_proto.sysroot.path
283 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600284
285 if not chroot.exists():
286 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
287 elif not sysroot.Exists(chroot=chroot):
288 cros_build_lib.Die('Sysroot does not exist: %s',
289 chroot.full_path(sysroot.path))
290
Michael Mortensen38675192019-06-28 16:52:55 +0000291 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600292
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600293 if archive is None:
294 cros_build_lib.Die(
Michael Mortensen38675192019-06-28 16:52:55 +0000295 'Could not create firmware archive. No firmware found for %s.',
296 sysroot_path)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600297
Alex Klein231d2da2019-07-22 16:44:45 -0600298 output_proto.artifacts.add().path = archive
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600299
300
Alex Klein076841b2019-08-29 15:19:39 -0600301@faux.all_empty
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600302@validate.exists('output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600303def BundleEbuildLogs(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600304 """Tar the ebuild logs for a build target.
305
306 Args:
307 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600308 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600309 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600310 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600311 output_dir = input_proto.output_dir
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600312 sysroot_path = input_proto.sysroot.path
313 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandeza478d802019-04-08 15:08:24 -0600314
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600315 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
316 target = input_proto.build_target.name
317 if target:
318 # Legacy handling.
319 build_root = constants.SOURCE_ROOT
Alex Klein171da612019-08-06 14:00:42 -0600320 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600321 sysroot_path = os.path.join('/build', target)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600322
Alex Klein231d2da2019-07-22 16:44:45 -0600323 # TODO(saklein): Switch to validation_complete decorator after legacy
324 # handling has been cleaned up.
325 if config.validate_only:
326 return controller.RETURN_CODE_VALID_INPUT
327
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600328 sysroot = sysroot_lib.Sysroot(sysroot_path)
329 archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600330 if archive is None:
331 cros_build_lib.Die(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600332 'Could not create ebuild logs archive. No logs found for %s.',
333 sysroot.path)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600334 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600335
336
Alex Klein076841b2019-08-29 15:19:39 -0600337@faux.all_empty
Andrew Lamb811aead2019-08-12 10:25:05 -0600338@validate.exists('output_dir')
339@validate.validation_complete
340def BundleChromeOSConfig(input_proto, output_proto, _config):
341 """Output the ChromeOS Config payload for a build target.
342
343 Args:
344 input_proto (BundleRequest): The input proto.
345 output_proto (BundleResponse): The output proto.
346 _config (api_config.ApiConfig): The API call config.
347 """
348 output_dir = input_proto.output_dir
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600349 sysroot_path = input_proto.sysroot.path
Andrew Lamb811aead2019-08-12 10:25:05 -0600350 chroot = controller_util.ParseChroot(input_proto.chroot)
351
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600352 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
353 target = input_proto.build_target.name
354 if target:
355 # Legacy handling.
356 build_root = constants.SOURCE_ROOT
357 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
358 sysroot_path = os.path.join('/build', target)
359
360 sysroot = sysroot_lib.Sysroot(sysroot_path)
Andrew Lamb811aead2019-08-12 10:25:05 -0600361 chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir)
362 if chromeos_config is None:
363 cros_build_lib.Die(
364 'Could not create ChromeOS Config payload. No config found for %s.',
365 sysroot.path)
366 output_proto.artifacts.add().path = os.path.join(output_dir, chromeos_config)
367
368
Alex Klein076841b2019-08-29 15:19:39 -0600369@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600370@validate.require('output_dir', 'sysroot.build_target.name', 'sysroot.path')
371@validate.exists('output_dir')
372@validate.validation_complete
373def BundleSimpleChromeArtifacts(input_proto, output_proto, _config):
Alex Klein2275d692019-04-23 16:04:12 -0600374 """Create the simple chrome artifacts."""
375 # Required args.
376 sysroot_path = input_proto.sysroot.path
377 build_target_name = input_proto.sysroot.build_target.name
378 output_dir = input_proto.output_dir
379
Alex Klein2275d692019-04-23 16:04:12 -0600380 # Optional args.
381 chroot_path = input_proto.chroot.path or constants.DEFAULT_CHROOT_PATH
382 cache_dir = input_proto.chroot.cache_dir
383
384 # Build out the argument instances.
385 build_target = build_target_util.BuildTarget(build_target_name)
386 chroot = chroot_lib.Chroot(path=chroot_path, cache_dir=cache_dir)
387 # Sysroot.path needs to be the fully qualified path, including the chroot.
388 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
389 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
390
391 # Quick sanity check that the sysroot exists before we go on.
392 if not sysroot.Exists():
393 cros_build_lib.Die('The sysroot does not exist.')
394
395 try:
396 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
397 build_target, output_dir)
398 except artifacts.Error as e:
399 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
400 type(e), e)
401
402 for file_name in results:
403 output_proto.artifacts.add().path = file_name
404
405
Alex Klein076841b2019-08-29 15:19:39 -0600406@faux.all_empty
Michael Mortensen51f06722019-07-18 09:55:50 -0600407@validate.require('chroot.path', 'test_results_dir', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600408@validate.exists('output_dir')
409@validate.validation_complete
410def BundleVmFiles(input_proto, output_proto, _config):
Alex Klein6504eca2019-04-18 15:37:56 -0600411 """Tar VM disk and memory files.
412
413 Args:
Trent Begin008cade2019-10-31 13:40:59 -0600414 input_proto (BundleVmFilesRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600415 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600416 _config (api_config.ApiConfig): The API call config.
Alex Klein6504eca2019-04-18 15:37:56 -0600417 """
Michael Mortensen51f06722019-07-18 09:55:50 -0600418 chroot = controller_util.ParseChroot(input_proto.chroot)
419 test_results_dir = input_proto.test_results_dir
Alex Klein6504eca2019-04-18 15:37:56 -0600420 output_dir = input_proto.output_dir
421
Michael Mortensen51f06722019-07-18 09:55:50 -0600422 archives = artifacts.BundleVmFiles(
423 chroot, test_results_dir, output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600424 for archive in archives:
425 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700426
Alex Klein231d2da2019-07-22 16:44:45 -0600427
Tiancong Wang24a3df72019-08-20 15:48:51 -0700428_VALID_ARTIFACT_TYPES = [toolchain_pb2.BENCHMARK_AFDO,
429 toolchain_pb2.ORDERFILE]
Alex Klein076841b2019-08-29 15:19:39 -0600430@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600431@validate.require('build_target.name', 'output_dir')
Tiancong Wang50b80a92019-08-01 14:46:15 -0700432@validate.is_in('artifact_type', _VALID_ARTIFACT_TYPES)
Alex Klein231d2da2019-07-22 16:44:45 -0600433@validate.exists('output_dir')
Tiancong Wang2ade7932019-09-27 14:15:40 -0700434@validate.exists('chroot.chrome_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600435@validate.validation_complete
Tiancong Wang50b80a92019-08-01 14:46:15 -0700436def BundleAFDOGenerationArtifacts(input_proto, output_proto, _config):
437 """Generic function for creating tarballs of both AFDO and orerfile.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700438
439 Args:
Tiancong Wang50b80a92019-08-01 14:46:15 -0700440 input_proto (BundleChromeAFDORequest): The input proto.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700441 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600442 _config (api_config.ApiConfig): The API call config.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700443 """
Tiancong Wang50b80a92019-08-01 14:46:15 -0700444
Tiancong Wangc4805b72019-06-11 12:12:03 -0700445 # Required args.
Alex Klein231d2da2019-07-22 16:44:45 -0600446 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Tiancong Wang2ade7932019-09-27 14:15:40 -0700447 chrome_root = input_proto.chroot.chrome_dir
448 if not chrome_root:
449 cros_build_lib.Die('chrome_root is not included in chroot')
Tiancong Wangc4805b72019-06-11 12:12:03 -0700450 output_dir = input_proto.output_dir
Tiancong Wang50b80a92019-08-01 14:46:15 -0700451 artifact_type = input_proto.artifact_type
Tiancong Wangc4805b72019-06-11 12:12:03 -0700452
Tiancong Wangc4805b72019-06-11 12:12:03 -0700453 chroot = controller_util.ParseChroot(input_proto.chroot)
454
455 try:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700456 is_orderfile = bool(artifact_type is toolchain_pb2.ORDERFILE)
Tiancong Wang50b80a92019-08-01 14:46:15 -0700457 results = artifacts.BundleAFDOGenerationArtifacts(
Tiancong Wang2ade7932019-09-27 14:15:40 -0700458 is_orderfile, chroot, chrome_root,
Tiancong Wang50b80a92019-08-01 14:46:15 -0700459 build_target, output_dir)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700460 except artifacts.Error as e:
461 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
462 type(e), e)
463
464 for file_name in results:
465 output_proto.artifacts.add().path = file_name
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600466
467
Alex Klein076841b2019-08-29 15:19:39 -0600468@faux.all_empty
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600469@validate.exists('output_dir')
470def ExportCpeReport(input_proto, output_proto, config):
471 """Export a CPE report.
472
473 Args:
474 input_proto (BundleRequest): The input proto.
475 output_proto (BundleResponse): The output proto.
476 config (api_config.ApiConfig): The API call config.
477 """
478 chroot = controller_util.ParseChroot(input_proto.chroot)
479 output_dir = input_proto.output_dir
480
481 if input_proto.build_target.name:
482 # Legacy handling - use the default sysroot path for the build target.
483 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
484 sysroot = sysroot_lib.Sysroot(build_target.root)
485 elif input_proto.sysroot.path:
486 sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
487 else:
488 # TODO(saklein): Switch to validate decorators once legacy handling can be
489 # cleaned up.
490 cros_build_lib.Die('sysroot.path is required.')
491
492 if config.validate_only:
493 return controller.RETURN_CODE_VALID_INPUT
494
495 cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir)
496
497 output_proto.artifacts.add().path = cpe_result.report
498 output_proto.artifacts.add().path = cpe_result.warnings