blob: 2241dcde10d4dac5418190cad76779ff0fd51a21 [file] [log] [blame]
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from recipe_engine import recipe_api
BUILDSETLOOKUP_VERSION = 'git_revision:6d396fabbcf4777d512466990d247bdde55c3e46'
class BuildSetLookupApi(recipe_api.RecipeApi):
"""APIs for retrieving the build ID for a given buildset and builder ID."""
def __init__(self, *args, **kwargs):
super(BuildSetLookupApi, self).__init__(*args, **kwargs)
self._buildset_lookup_tool = None
def __call__(self,
step_name,
builder,
buildset,
build_status=None,
leak_to=None,
retries=0,
interval=0):
"""Retrieves the build ID for a given buildset and builder ID.
Args:
step_name (str): The name of the step produced.
builder (str): A fully-qualified buildbucket v2 builder ID,
consisting of <project>/<project-namespaced bucket>/<builder name>. For example:
'fuchsia/ci/garnet-x64-release-qemu_kvm'.
buildset (str): A fully-qualified buildbucket V2 buildset tag,
consisting of commit/gitiles/<host>/<repo>/+/<commit ID>. For example:
'commit/gitiles/fuchsia.googlesource.com/topaz/+/e3127e0bd6d57da7a5959ee70eb0a396590e6d53'.
build_status (int): The expected buildbucket status code of the build. If
None, use tool's default (12, SUCCESS).
leak_to (Path): If leak_to is provided, it must be a Path object. This path will be used in
place of a random temporary file, and the file will not be deleted at the end of the step.
retries (int): Number of times to retry lookup, in case builds are not yet
triggered.
interval (int): Interval between retries in seconds.
"""
self.ensure()
step_args = [
self._buildset_lookup_tool,
'-builder-id',
builder,
'-build-set',
buildset,
]
if build_status is not None:
step_args.extend(['-build-status', build_status])
for attempt in xrange(retries + 1):
try:
return self.m.step(
step_name,
step_args,
stdout=self.m.raw_io.output(leak_to=leak_to),
)
except self.m.step.StepFailure:
if attempt < retries:
self.m.time.sleep(interval)
else:
raise
def ensure(self):
"""Ensures that the buildset lookup tool is installed."""
if not self._buildset_lookup_tool:
with self.m.step.nest('ensure buildset lookup'):
with self.m.context(infra_steps=True):
pkgs = self.m.cipd.EnsureFile()
pkgs.add_package('fuchsia/infra/buildsetlookup/${platform}',
BUILDSETLOOKUP_VERSION)
cipd_dir = self.m.path['start_dir'].join('cipd', 'buildsetlookup')
self.m.cipd.ensure(cipd_dir, pkgs)
self._buildset_lookup_tool = cipd_dir.join('buildsetlookup')