blob: 60fe1bc059efc90a11f63eb5b09d2e7c7e69a0f0 [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 Klein2b236722019-06-19 15:44:26 -060012from chromite.api import validate
Alex Klein238d8862019-05-07 11:32:46 -060013from chromite.api.controller import controller_util
Evan Hernandezf388cbf2019-04-01 11:15:23 -060014from chromite.cbuildbot import commands
Alex Klein6504eca2019-04-18 15:37:56 -060015from chromite.cbuildbot.stages import vm_test_stages
Alex Klein2275d692019-04-23 16:04:12 -060016from chromite.lib import build_target_util
17from chromite.lib import chroot_lib
Evan Hernandezf388cbf2019-04-01 11:15:23 -060018from chromite.lib import constants
19from chromite.lib import cros_build_lib
Evan Hernandezde445982019-04-22 13:42:34 -060020from chromite.lib import cros_logging as logging
Alex Klein2275d692019-04-23 16:04:12 -060021from chromite.lib import sysroot_lib
22from chromite.service import artifacts
Evan Hernandezf388cbf2019-04-01 11:15:23 -060023
24
Evan Hernandez9f125ac2019-04-08 17:18:47 -060025def _GetImageDir(build_root, target):
26 """Return path containing images for the given build target.
27
Alex Kleine2612a02019-04-18 13:51:06 -060028 TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case.
29
Evan Hernandez9f125ac2019-04-08 17:18:47 -060030 Args:
31 build_root (str): Path to checkout where build occurs.
32 target (str): Name of the build target.
33
34 Returns:
35 Path to the directory containing target images.
36
37 Raises:
38 DieSystemExit: If the image dir does not exist.
39 """
40 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
41 if not os.path.exists(image_dir):
42 cros_build_lib.Die('Expected to find image output for target %s at %s, '
43 'but path does not exist' % (target, image_dir))
44 return image_dir
45
46
47def BundleImageZip(input_proto, output_proto):
48 """Bundle image.zip.
49
50 Args:
51 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060052 output_proto (BundleResponse): The output proto.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060053 """
54 target = input_proto.build_target.name
55 output_dir = input_proto.output_dir
56 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
57 archive = commands.BuildImageZip(output_dir, image_dir)
58 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
59
60
Evan Hernandezf388cbf2019-04-01 11:15:23 -060061def BundleTestUpdatePayloads(input_proto, output_proto):
62 """Generate minimal update payloads for the build target for testing.
63
64 Args:
65 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060066 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060067 """
68 target = input_proto.build_target.name
69 output_dir = input_proto.output_dir
70 build_root = constants.SOURCE_ROOT
71
72 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -060073 img_dir = _GetImageDir(build_root, target)
Alex Kleincb541e82019-06-26 15:06:11 -060074 img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
75 constants.IMAGE_TYPE_BASE]
76 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
Mike Frysinger66ce4132019-07-17 22:52:52 -040077 img_paths = [os.path.join(img_dir, x) for x in img_names]
Mike Frysingera552be42018-08-17 14:39:32 -040078 valid_images = [x for x in img_paths if os.path.exists(x)]
Evan Hernandezf388cbf2019-04-01 11:15:23 -060079
Alex Kleincb541e82019-06-26 15:06:11 -060080 if not valid_images:
Evan Hernandezf388cbf2019-04-01 11:15:23 -060081 cros_build_lib.Die(
82 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -060083 'at path %s.', img_types, target, img_dir)
Alex Kleincb541e82019-06-26 15:06:11 -060084 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -060085
Alex Kleincb541e82019-06-26 15:06:11 -060086 payloads = artifacts.BundleTestUpdatePayloads(image, output_dir)
87 for payload in payloads:
88 output_proto.artifacts.add().path = payload
Evan Hernandezf388cbf2019-04-01 11:15:23 -060089
90
91def BundleAutotestFiles(input_proto, output_proto):
92 """Tar the autotest files for a build target.
93
94 Args:
95 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -060096 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -060097 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -060098 output_dir = input_proto.output_dir
Alex Klein238d8862019-05-07 11:32:46 -060099 if not output_dir:
100 cros_build_lib.Die('output_dir is required.')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600101
Alex Klein238d8862019-05-07 11:32:46 -0600102 target = input_proto.build_target.name
103 if target:
104 # Legacy call, build out sysroot path from default source root and the
105 # build target.
106 target = input_proto.build_target.name
107 build_root = constants.SOURCE_ROOT
108 sysroot_path = os.path.join(build_root, constants.DEFAULT_CHROOT_DIR,
109 'build', target)
110 sysroot = sysroot_lib.Sysroot(sysroot_path)
111 else:
112 # New style call, use chroot and sysroot.
113 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600114
Alex Klein238d8862019-05-07 11:32:46 -0600115 sysroot_path = input_proto.sysroot.path
116 if not sysroot_path:
117 cros_build_lib.Die('sysroot.path is required.')
118
119 # Since we're staying outside the chroot, prepend the chroot path to the
120 # sysroot path so we have a valid full path to the sysroot.
121 sysroot = sysroot_lib.Sysroot(os.path.join(chroot.path,
122 sysroot_path.lstrip(os.sep)))
123
124 if not sysroot.Exists():
125 cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path)
126
127 try:
128 # Note that this returns the full path to *multiple* tarballs.
129 archives = artifacts.BundleAutotestFiles(sysroot, output_dir)
130 except artifacts.Error as e:
131 cros_build_lib.Die(e.message)
132
133 for archive in archives.values():
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600134 output_proto.artifacts.add().path = archive
135
136
Alex Kleinb9d810b2019-07-01 12:38:02 -0600137@validate.require('output_dir')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600138def 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 Hernandezf388cbf2019-04-01 11:15:23 -0600148
Alex Kleinb9d810b2019-07-01 12:38:02 -0600149 chroot = controller_util.ParseChroot(input_proto.chroot)
150 sysroot_path = input_proto.sysroot.path
151
152 # TODO(saklein) Cleanup legacy handling after it has been switched over.
153 if target:
154 # Legacy handling.
155 chroot.path = os.path.join(build_root, 'chroot')
156 sysroot_path = os.path.join('/build', target)
157
158 # New handling - chroot & sysroot based.
159 # TODO(saklein) Switch this to the require decorator when legacy is removed.
160 if not sysroot_path:
161 cros_build_lib.Die('sysroot.path is required.')
162
163 sysroot = sysroot_lib.Sysroot(sysroot_path)
164 if not sysroot.Exists(chroot):
165 cros_build_lib.Die('Sysroot must exist.')
166
167 archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600168
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600169 if archive is None:
170 cros_build_lib.Die(
171 'Could not bundle Tast files. '
172 'No Tast directories found for %s.', target)
173
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600174 output_proto.artifacts.add().path = archive
175
176
177def BundlePinnedGuestImages(input_proto, output_proto):
178 """Tar the pinned guest images for a build target.
179
180 Args:
181 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600182 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600183 """
184 target = input_proto.build_target.name
185 output_dir = input_proto.output_dir
186 build_root = constants.SOURCE_ROOT
187
Alex Kleine2612a02019-04-18 13:51:06 -0600188 # TODO(crbug.com/954299): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600189 archive = commands.BuildPinnedGuestImagesTarball(build_root, target,
190 output_dir)
191
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600192 if archive is None:
Evan Hernandezde445982019-04-22 13:42:34 -0600193 logging.warning('Found no pinned guest images for %s.', target)
194 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600195
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600196 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
197
198
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600199def FetchPinnedGuestImages(input_proto, output_proto):
200 """Get the pinned guest image information."""
201 sysroot_path = input_proto.sysroot.path
202 if not sysroot_path:
203 cros_build_lib.Die('sysroot.path is required.')
204
205 chroot = controller_util.ParseChroot(input_proto.chroot)
206 sysroot = sysroot_lib.Sysroot(sysroot_path)
207
208 if not chroot.exists():
209 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
210
211 if not sysroot.Exists(chroot=chroot):
212 cros_build_lib.Die('Sysroot does not exist: %s',
213 chroot.full_path(sysroot.path))
214
215 pins = artifacts.FetchPinnedGuestImages(chroot, sysroot)
216
217 for pin in pins:
218 pinned_image = output_proto.pinned_images.add()
219 pinned_image.filename = pin.filename
220 pinned_image.uri = pin.uri
221
222
Michael Mortensen38675192019-06-28 16:52:55 +0000223@validate.require('output_dir', 'sysroot.path')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600224def BundleFirmware(input_proto, output_proto):
225 """Tar the firmware images for a build target.
226
227 Args:
228 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600229 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600230 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600231 output_dir = input_proto.output_dir
Michael Mortensen38675192019-06-28 16:52:55 +0000232 chroot = controller_util.ParseChroot(input_proto.chroot)
233 sysroot_path = input_proto.sysroot.path
234 sysroot = sysroot_lib.Sysroot(sysroot_path)
235 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600236
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600237 if archive is None:
238 cros_build_lib.Die(
Michael Mortensen38675192019-06-28 16:52:55 +0000239 'Could not create firmware archive. No firmware found for %s.',
240 sysroot_path)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600241
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600242 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
243
244
245def BundleEbuildLogs(input_proto, output_proto):
246 """Tar the ebuild logs for a build target.
247
248 Args:
249 input_proto (BundleRequest): The input proto.
Alex Klein6504eca2019-04-18 15:37:56 -0600250 output_proto (BundleResponse): The output proto.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600251 """
252 target = input_proto.build_target.name
253 output_dir = input_proto.output_dir
Evan Hernandeza478d802019-04-08 15:08:24 -0600254
255 # commands.BuildEbuildLogsTarball conflates "buildroot" with sysroot.
256 # Adjust accordingly...
257 build_root = os.path.join(constants.SOURCE_ROOT, 'chroot', 'build')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600258
Alex Kleine2612a02019-04-18 13:51:06 -0600259 # TODO(crbug.com/954303): Replace with a chromite/service implementation.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600260 archive = commands.BuildEbuildLogsTarball(build_root, target, output_dir)
261
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600262 if archive is None:
263 cros_build_lib.Die(
264 'Could not create ebuild logs archive. No logs found for %s.', target)
265
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600266 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600267
268
Alex Klein2275d692019-04-23 16:04:12 -0600269def BundleSimpleChromeArtifacts(input_proto, output_proto):
270 """Create the simple chrome artifacts."""
271 # Required args.
272 sysroot_path = input_proto.sysroot.path
273 build_target_name = input_proto.sysroot.build_target.name
274 output_dir = input_proto.output_dir
275
276 if not build_target_name:
277 cros_build_lib.Die('build_target.name is required')
278 if not output_dir:
279 cros_build_lib.Die('output_dir is required.')
280 if not os.path.exists(output_dir):
281 cros_build_lib.Die('output_dir (%s) does not exist.', output_dir)
282 if not sysroot_path:
283 cros_build_lib.Die('sysroot.path is required.')
284
285 # Optional args.
286 chroot_path = input_proto.chroot.path or constants.DEFAULT_CHROOT_PATH
287 cache_dir = input_proto.chroot.cache_dir
288
289 # Build out the argument instances.
290 build_target = build_target_util.BuildTarget(build_target_name)
291 chroot = chroot_lib.Chroot(path=chroot_path, cache_dir=cache_dir)
292 # Sysroot.path needs to be the fully qualified path, including the chroot.
293 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
294 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
295
296 # Quick sanity check that the sysroot exists before we go on.
297 if not sysroot.Exists():
298 cros_build_lib.Die('The sysroot does not exist.')
299
300 try:
301 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
302 build_target, output_dir)
303 except artifacts.Error as e:
304 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
305 type(e), e)
306
307 for file_name in results:
308 output_proto.artifacts.add().path = file_name
309
310
Alex Klein2b236722019-06-19 15:44:26 -0600311@validate.require('chroot.path', 'sysroot.path', 'test_results_dir',
312 'output_dir')
Alex Klein6504eca2019-04-18 15:37:56 -0600313def BundleVmFiles(input_proto, output_proto):
314 """Tar VM disk and memory files.
315
316 Args:
317 input_proto (SysrootBundleRequest): The input proto.
318 output_proto (BundleResponse): The output proto.
319 """
320 chroot = input_proto.chroot.path
Alex Klein2b236722019-06-19 15:44:26 -0600321 sysroot = input_proto.sysroot.path.lstrip(os.sep)
322 test_results_dir = input_proto.test_results_dir.lstrip(os.sep)
Alex Klein6504eca2019-04-18 15:37:56 -0600323 output_dir = input_proto.output_dir
324
Alex Klein6504eca2019-04-18 15:37:56 -0600325 # TODO(crbug.com/954344): Replace with a chromite/service implementation.
Alex Klein2b236722019-06-19 15:44:26 -0600326 image_dir = os.path.join(chroot, sysroot, test_results_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600327 archives = vm_test_stages.ArchiveVMFilesFromImageDir(image_dir, output_dir)
328 for archive in archives:
329 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700330
331def BundleOrderfileGenerationArtifacts(input_proto, output_proto):
332 """Create tarballs of all the artifacts of orderfile_generate builder.
333
334 Args:
335 input_proto (BundleRequest): The input proto.
336 output_proto (BundleResponse): The output proto.
337 """
338 # Required args.
339 build_target_name = input_proto.build_target.name
340 output_dir = input_proto.output_dir
341 chrome_version = input_proto.chrome_version
342
343 if not build_target_name:
344 cros_build_lib.Die('build_target.name is required.')
345 if not chrome_version:
346 cros_build_lib.Die('chrome_version is required.')
347 if not output_dir:
348 cros_build_lib.Die('output_dir is required.')
349 elif not os.path.isdir(output_dir):
350 cros_build_lib.Die('output_dir does not exist.')
351
352 chroot = controller_util.ParseChroot(input_proto.chroot)
353
354 try:
355 results = artifacts.BundleOrderfileGenerationArtifacts(
356 chroot, input_proto.build_target, chrome_version, output_dir)
357 except artifacts.Error as e:
358 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
359 type(e), e)
360
361 for file_name in results:
362 output_proto.artifacts.add().path = file_name