| # 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 |
| |
| # Default LKGS version. |
| LKGS_VERSION = 'git_revision:073b49ada1681f04993bda3383771f62770ca6f6' |
| |
| |
| class LkgsApi(recipe_api.RecipeApi): |
| """APIs for deriving the last-known-good-snapshot for a builder.""" |
| |
| def __init__(self, *args, **kwargs): |
| super(LkgsApi, self).__init__(*args, **kwargs) |
| self._lkgs_tool = None |
| |
| def __call__(self, step_name, builder, output_file, gitiles_ref=None): |
| """Retrieves the last-known-"good"-(jiri-)snapshot given a builder. |
| |
| Args: |
| step_name (str): The name of the step produced. |
| builder (seq[str]): A list of fully-qualified buildbucket v2 builder ID, |
| consisting of <project>/<project-namespaced bucket>/<builder name>. For example: |
| ['fuchsia/ci/garnet-x64-release-qemu_kvm']. |
| output_file (Path or Placeholder): The location to dump the retrieved |
| snapshot. |
| gitiles_ref (str): Optionally filter builds for matching gitiles ref, |
| e.g. refs/heads/master. By default, do not filter. |
| """ |
| self._ensure() |
| |
| step_args = [ |
| self._lkgs_tool, |
| '-output-file', |
| output_file, |
| ] |
| |
| for b in builder: |
| step_args += [ |
| '-builder-id', |
| b, |
| ] |
| |
| if gitiles_ref: |
| step_args += [ |
| '-gitiles-ref', |
| gitiles_ref, |
| ] |
| |
| return self.m.step(step_name, step_args) |
| |
| def _ensure(self): |
| """Ensures that the lkgs tool is installed.""" |
| if not self._lkgs_tool: |
| with self.m.step.nest('ensure lkgs'): |
| with self.m.context(infra_steps=True): |
| pkgs = self.m.cipd.EnsureFile() |
| pkgs.add_package('fuchsia/infra/lkgs/${platform}', LKGS_VERSION) |
| cipd_dir = self.m.path['start_dir'].join('cipd', 'lkgs') |
| self.m.cipd.ensure(cipd_dir, pkgs) |
| self._lkgs_tool = cipd_dir.join('lkgs') |