blob: 6183fc4967754dc2b80633c4d2c3917f58f05382 [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:
37 Path to the directory containing target images.
38
39 Raises:
40 DieSystemExit: If the image dir does not exist.
41 """
42 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
43 if not os.path.exists(image_dir):
44 cros_build_lib.Die('Expected to find image output for target %s at %s, '
45 'but path does not exist' % (target, image_dir))
46 return image_dir
47
48
Alex Klein076841b2019-08-29 15:19:39 -060049@faux.all_empty
Alex Kleind91e95a2019-09-17 10:39:02 -060050@validate.require('build_target.name')
51@validate.exists('output_dir')
52@validate.validation_complete
53def BundleImageArchives(input_proto, output_proto, _config):
54 """Create a .tar.xz archive for each image that has been created."""
55 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
56 output_dir = input_proto.output_dir
57 image_dir = _GetImageDir(constants.SOURCE_ROOT, build_target.name)
58
59 archives = artifacts.ArchiveImages(image_dir, output_dir)
60
61 for archive in archives:
62 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
63
64
65@faux.all_empty
Michael Mortensen01910922019-07-24 14:48:10 -060066@validate.require('build_target.name', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -060067@validate.exists('output_dir')
68@validate.validation_complete
69def BundleImageZip(input_proto, output_proto, _config):
Evan Hernandez9f125ac2019-04-08 17:18:47 -060070 """Bundle image.zip.
71
72 Args:
73 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060074 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060075 _config (api_config.ApiConfig): The API call config.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060076 """
77 target = input_proto.build_target.name
78 output_dir = input_proto.output_dir
79 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
Alex Klein231d2da2019-07-22 16:44:45 -060080
Michael Mortensen01910922019-07-24 14:48:10 -060081 archive = artifacts.BundleImageZip(output_dir, image_dir)
Evan Hernandez9f125ac2019-04-08 17:18:47 -060082 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
83
84
Alex Klein076841b2019-08-29 15:19:39 -060085@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -060086@validate.require('build_target.name', 'output_dir')
87@validate.exists('output_dir')
88@validate.validation_complete
89def BundleTestUpdatePayloads(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -060090 """Generate minimal update payloads for the build target for testing.
91
92 Args:
93 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060094 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060095 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060096 """
97 target = input_proto.build_target.name
98 output_dir = input_proto.output_dir
99 build_root = constants.SOURCE_ROOT
100
101 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600102 img_dir = _GetImageDir(build_root, target)
Alex Kleincb541e82019-06-26 15:06:11 -0600103 img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
104 constants.IMAGE_TYPE_BASE]
105 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
Mike Frysinger66ce4132019-07-17 22:52:52 -0400106 img_paths = [os.path.join(img_dir, x) for x in img_names]
Mike Frysingera552be42018-08-17 14:39:32 -0400107 valid_images = [x for x in img_paths if os.path.exists(x)]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600108
Alex Kleincb541e82019-06-26 15:06:11 -0600109 if not valid_images:
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600110 cros_build_lib.Die(
111 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600112 'at path %s.', img_types, target, img_dir)
Alex Kleincb541e82019-06-26 15:06:11 -0600113 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600114
Alex Kleincb541e82019-06-26 15:06:11 -0600115 payloads = artifacts.BundleTestUpdatePayloads(image, output_dir)
116 for payload in payloads:
117 output_proto.artifacts.add().path = payload
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600118
119
Alex Klein076841b2019-08-29 15:19:39 -0600120@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600121@validate.require('output_dir')
122@validate.exists('output_dir')
123def BundleAutotestFiles(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600124 """Tar the autotest files for a build target.
125
126 Args:
127 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600128 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600129 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600130 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600131 output_dir = input_proto.output_dir
Alex Klein238d8862019-05-07 11:32:46 -0600132 target = input_proto.build_target.name
Alex Kleine21a0952019-08-23 16:08:16 -0600133 chroot = controller_util.ParseChroot(input_proto.chroot)
134
Alex Klein238d8862019-05-07 11:32:46 -0600135 if target:
Alex Kleine21a0952019-08-23 16:08:16 -0600136 sysroot_path = os.path.join('/build', target)
Alex Klein238d8862019-05-07 11:32:46 -0600137 else:
138 # New style call, use chroot and sysroot.
Alex Klein238d8862019-05-07 11:32:46 -0600139 sysroot_path = input_proto.sysroot.path
140 if not sysroot_path:
141 cros_build_lib.Die('sysroot.path is required.')
142
Alex Kleine21a0952019-08-23 16:08:16 -0600143 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein238d8862019-05-07 11:32:46 -0600144
Alex Klein231d2da2019-07-22 16:44:45 -0600145 # TODO(saklein): Switch to the validate_only decorator when legacy handling
146 # is removed.
147 if config.validate_only:
148 return controller.RETURN_CODE_VALID_INPUT
149
Alex Kleine21a0952019-08-23 16:08:16 -0600150 if not sysroot.Exists(chroot=chroot):
Alex Klein238d8862019-05-07 11:32:46 -0600151 cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path)
152
153 try:
154 # Note that this returns the full path to *multiple* tarballs.
Alex Kleine21a0952019-08-23 16:08:16 -0600155 archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600156 except artifacts.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -0400157 cros_build_lib.Die(e)
Alex Klein238d8862019-05-07 11:32:46 -0600158
159 for archive in archives.values():
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600160 output_proto.artifacts.add().path = archive
161
162
Alex Klein076841b2019-08-29 15:19:39 -0600163@faux.all_empty
Alex Kleinb9d810b2019-07-01 12:38:02 -0600164@validate.require('output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600165@validate.exists('output_dir')
166def BundleTastFiles(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600167 """Tar the tast files for a build target.
168
169 Args:
170 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600171 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600172 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600173 """
174 target = input_proto.build_target.name
175 output_dir = input_proto.output_dir
176 build_root = constants.SOURCE_ROOT
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600177
Alex Kleinb9d810b2019-07-01 12:38:02 -0600178 chroot = controller_util.ParseChroot(input_proto.chroot)
179 sysroot_path = input_proto.sysroot.path
180
181 # TODO(saklein) Cleanup legacy handling after it has been switched over.
182 if target:
183 # Legacy handling.
Alex Klein171da612019-08-06 14:00:42 -0600184 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Alex Kleinb9d810b2019-07-01 12:38:02 -0600185 sysroot_path = os.path.join('/build', target)
186
187 # New handling - chroot & sysroot based.
188 # TODO(saklein) Switch this to the require decorator when legacy is removed.
189 if not sysroot_path:
190 cros_build_lib.Die('sysroot.path is required.')
191
Alex Klein231d2da2019-07-22 16:44:45 -0600192 # TODO(saklein): Switch to the validation_complete decorator when legacy
193 # handling is removed.
194 if config.validate_only:
195 return controller.RETURN_CODE_VALID_INPUT
196
Alex Kleinb9d810b2019-07-01 12:38:02 -0600197 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600198 if not sysroot.Exists(chroot=chroot):
Alex Kleinb9d810b2019-07-01 12:38:02 -0600199 cros_build_lib.Die('Sysroot must exist.')
200
201 archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600202
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600203 if archive is None:
204 cros_build_lib.Die(
205 'Could not bundle Tast files. '
206 'No Tast directories found for %s.', target)
207
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600208 output_proto.artifacts.add().path = archive
209
210
Alex Klein076841b2019-08-29 15:19:39 -0600211@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600212@validate.require('build_target.name', 'output_dir')
213@validate.exists('output_dir')
214@validate.validation_complete
215def BundlePinnedGuestImages(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600216 """Tar the pinned guest images for a build target.
217
218 Args:
219 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600220 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600221 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600222 """
223 target = input_proto.build_target.name
224 output_dir = input_proto.output_dir
225 build_root = constants.SOURCE_ROOT
226
Alex Kleine2612a02019-04-18 13:51:06 -0600227 # TODO(crbug.com/954299): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600228 archive = commands.BuildPinnedGuestImagesTarball(build_root, target,
229 output_dir)
230
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600231 if archive is None:
Evan Hernandezde445982019-04-22 13:42:34 -0600232 logging.warning('Found no pinned guest images for %s.', target)
233 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600234
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600235 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
236
237
Alex Klein076841b2019-08-29 15:19:39 -0600238@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600239@validate.require('sysroot.path')
240@validate.validation_complete
241def FetchPinnedGuestImages(input_proto, output_proto, _config):
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600242 """Get the pinned guest image information."""
243 sysroot_path = input_proto.sysroot.path
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600244
245 chroot = controller_util.ParseChroot(input_proto.chroot)
246 sysroot = sysroot_lib.Sysroot(sysroot_path)
247
248 if not chroot.exists():
249 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
Alex Klein231d2da2019-07-22 16:44:45 -0600250 elif not sysroot.Exists(chroot=chroot):
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600251 cros_build_lib.Die('Sysroot does not exist: %s',
252 chroot.full_path(sysroot.path))
253
254 pins = artifacts.FetchPinnedGuestImages(chroot, sysroot)
255
256 for pin in pins:
257 pinned_image = output_proto.pinned_images.add()
258 pinned_image.filename = pin.filename
259 pinned_image.uri = pin.uri
260
261
Alex Klein076841b2019-08-29 15:19:39 -0600262@faux.all_empty
Michael Mortensen38675192019-06-28 16:52:55 +0000263@validate.require('output_dir', 'sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600264@validate.exists('output_dir')
265@validate.validation_complete
266def BundleFirmware(input_proto, output_proto, _config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600267 """Tar the firmware images for a build target.
268
269 Args:
270 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600271 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600272 _config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600273 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600274 output_dir = input_proto.output_dir
Michael Mortensen38675192019-06-28 16:52:55 +0000275 chroot = controller_util.ParseChroot(input_proto.chroot)
276 sysroot_path = input_proto.sysroot.path
277 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600278
279 if not chroot.exists():
280 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
281 elif not sysroot.Exists(chroot=chroot):
282 cros_build_lib.Die('Sysroot does not exist: %s',
283 chroot.full_path(sysroot.path))
284
Michael Mortensen38675192019-06-28 16:52:55 +0000285 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600286
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600287 if archive is None:
288 cros_build_lib.Die(
Michael Mortensen38675192019-06-28 16:52:55 +0000289 'Could not create firmware archive. No firmware found for %s.',
290 sysroot_path)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600291
Alex Klein231d2da2019-07-22 16:44:45 -0600292 output_proto.artifacts.add().path = archive
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600293
294
Alex Klein076841b2019-08-29 15:19:39 -0600295@faux.all_empty
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600296@validate.exists('output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600297def BundleEbuildLogs(input_proto, output_proto, config):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600298 """Tar the ebuild logs for a build target.
299
300 Args:
301 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600302 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600303 config (api_config.ApiConfig): The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600304 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600305 output_dir = input_proto.output_dir
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600306 sysroot_path = input_proto.sysroot.path
307 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandeza478d802019-04-08 15:08:24 -0600308
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600309 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
310 target = input_proto.build_target.name
311 if target:
312 # Legacy handling.
313 build_root = constants.SOURCE_ROOT
Alex Klein171da612019-08-06 14:00:42 -0600314 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600315 sysroot_path = os.path.join('/build', target)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600316
Alex Klein231d2da2019-07-22 16:44:45 -0600317 # TODO(saklein): Switch to validation_complete decorator after legacy
318 # handling has been cleaned up.
319 if config.validate_only:
320 return controller.RETURN_CODE_VALID_INPUT
321
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600322 sysroot = sysroot_lib.Sysroot(sysroot_path)
323 archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600324 if archive is None:
325 cros_build_lib.Die(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600326 'Could not create ebuild logs archive. No logs found for %s.',
327 sysroot.path)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600328 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600329
330
Alex Klein076841b2019-08-29 15:19:39 -0600331@faux.all_empty
Andrew Lamb811aead2019-08-12 10:25:05 -0600332@validate.exists('output_dir')
333@validate.validation_complete
334def BundleChromeOSConfig(input_proto, output_proto, _config):
335 """Output the ChromeOS Config payload for a build target.
336
337 Args:
338 input_proto (BundleRequest): The input proto.
339 output_proto (BundleResponse): The output proto.
340 _config (api_config.ApiConfig): The API call config.
341 """
342 output_dir = input_proto.output_dir
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600343 sysroot_path = input_proto.sysroot.path
Andrew Lamb811aead2019-08-12 10:25:05 -0600344 chroot = controller_util.ParseChroot(input_proto.chroot)
345
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600346 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
347 target = input_proto.build_target.name
348 if target:
349 # Legacy handling.
350 build_root = constants.SOURCE_ROOT
351 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
352 sysroot_path = os.path.join('/build', target)
353
354 sysroot = sysroot_lib.Sysroot(sysroot_path)
Andrew Lamb811aead2019-08-12 10:25:05 -0600355 chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir)
356 if chromeos_config is None:
357 cros_build_lib.Die(
358 'Could not create ChromeOS Config payload. No config found for %s.',
359 sysroot.path)
360 output_proto.artifacts.add().path = os.path.join(output_dir, chromeos_config)
361
362
Alex Klein076841b2019-08-29 15:19:39 -0600363@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600364@validate.require('output_dir', 'sysroot.build_target.name', 'sysroot.path')
365@validate.exists('output_dir')
366@validate.validation_complete
367def BundleSimpleChromeArtifacts(input_proto, output_proto, _config):
Alex Klein2275d692019-04-23 16:04:12 -0600368 """Create the simple chrome artifacts."""
369 # Required args.
370 sysroot_path = input_proto.sysroot.path
371 build_target_name = input_proto.sysroot.build_target.name
372 output_dir = input_proto.output_dir
373
Alex Klein2275d692019-04-23 16:04:12 -0600374 # Optional args.
375 chroot_path = input_proto.chroot.path or constants.DEFAULT_CHROOT_PATH
376 cache_dir = input_proto.chroot.cache_dir
377
378 # Build out the argument instances.
379 build_target = build_target_util.BuildTarget(build_target_name)
380 chroot = chroot_lib.Chroot(path=chroot_path, cache_dir=cache_dir)
381 # Sysroot.path needs to be the fully qualified path, including the chroot.
382 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
383 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
384
385 # Quick sanity check that the sysroot exists before we go on.
386 if not sysroot.Exists():
387 cros_build_lib.Die('The sysroot does not exist.')
388
389 try:
390 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
391 build_target, output_dir)
392 except artifacts.Error as e:
393 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
394 type(e), e)
395
396 for file_name in results:
397 output_proto.artifacts.add().path = file_name
398
399
Alex Klein076841b2019-08-29 15:19:39 -0600400@faux.all_empty
Michael Mortensen51f06722019-07-18 09:55:50 -0600401@validate.require('chroot.path', 'test_results_dir', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600402@validate.exists('output_dir')
403@validate.validation_complete
404def BundleVmFiles(input_proto, output_proto, _config):
Alex Klein6504eca2019-04-18 15:37:56 -0600405 """Tar VM disk and memory files.
406
407 Args:
408 input_proto (SysrootBundleRequest): The input proto.
409 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600410 _config (api_config.ApiConfig): The API call config.
Alex Klein6504eca2019-04-18 15:37:56 -0600411 """
Michael Mortensen51f06722019-07-18 09:55:50 -0600412 chroot = controller_util.ParseChroot(input_proto.chroot)
413 test_results_dir = input_proto.test_results_dir
Alex Klein6504eca2019-04-18 15:37:56 -0600414 output_dir = input_proto.output_dir
415
Michael Mortensen51f06722019-07-18 09:55:50 -0600416 archives = artifacts.BundleVmFiles(
417 chroot, test_results_dir, output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600418 for archive in archives:
419 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700420
Alex Klein231d2da2019-07-22 16:44:45 -0600421
Tiancong Wang24a3df72019-08-20 15:48:51 -0700422_VALID_ARTIFACT_TYPES = [toolchain_pb2.BENCHMARK_AFDO,
423 toolchain_pb2.ORDERFILE]
Alex Klein076841b2019-08-29 15:19:39 -0600424@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600425@validate.require('build_target.name', 'output_dir')
Tiancong Wang50b80a92019-08-01 14:46:15 -0700426@validate.is_in('artifact_type', _VALID_ARTIFACT_TYPES)
Alex Klein231d2da2019-07-22 16:44:45 -0600427@validate.exists('output_dir')
428@validate.validation_complete
Tiancong Wang50b80a92019-08-01 14:46:15 -0700429def BundleAFDOGenerationArtifacts(input_proto, output_proto, _config):
430 """Generic function for creating tarballs of both AFDO and orerfile.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700431
432 Args:
Tiancong Wang50b80a92019-08-01 14:46:15 -0700433 input_proto (BundleChromeAFDORequest): The input proto.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700434 output_proto (BundleResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600435 _config (api_config.ApiConfig): The API call config.
Tiancong Wangc4805b72019-06-11 12:12:03 -0700436 """
Tiancong Wang50b80a92019-08-01 14:46:15 -0700437
Tiancong Wangc4805b72019-06-11 12:12:03 -0700438 # Required args.
Alex Klein231d2da2019-07-22 16:44:45 -0600439 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700440 output_dir = input_proto.output_dir
Tiancong Wang50b80a92019-08-01 14:46:15 -0700441 artifact_type = input_proto.artifact_type
Tiancong Wangc4805b72019-06-11 12:12:03 -0700442
Tiancong Wangc4805b72019-06-11 12:12:03 -0700443 chroot = controller_util.ParseChroot(input_proto.chroot)
444
445 try:
Tiancong Wang24a3df72019-08-20 15:48:51 -0700446 is_orderfile = bool(artifact_type is toolchain_pb2.ORDERFILE)
Tiancong Wang50b80a92019-08-01 14:46:15 -0700447 results = artifacts.BundleAFDOGenerationArtifacts(
448 is_orderfile, chroot,
449 build_target, output_dir)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700450 except artifacts.Error as e:
451 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
452 type(e), e)
453
454 for file_name in results:
455 output_proto.artifacts.add().path = file_name
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600456
457
Alex Klein076841b2019-08-29 15:19:39 -0600458@faux.all_empty
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600459@validate.exists('output_dir')
460def ExportCpeReport(input_proto, output_proto, config):
461 """Export a CPE report.
462
463 Args:
464 input_proto (BundleRequest): The input proto.
465 output_proto (BundleResponse): The output proto.
466 config (api_config.ApiConfig): The API call config.
467 """
468 chroot = controller_util.ParseChroot(input_proto.chroot)
469 output_dir = input_proto.output_dir
470
471 if input_proto.build_target.name:
472 # Legacy handling - use the default sysroot path for the build target.
473 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
474 sysroot = sysroot_lib.Sysroot(build_target.root)
475 elif input_proto.sysroot.path:
476 sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
477 else:
478 # TODO(saklein): Switch to validate decorators once legacy handling can be
479 # cleaned up.
480 cros_build_lib.Die('sysroot.path is required.')
481
482 if config.validate_only:
483 return controller.RETURN_CODE_VALID_INPUT
484
485 cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir)
486
487 output_proto.artifacts.add().path = cpe_result.report
488 output_proto.artifacts.add().path = cpe_result.warnings