| # Copyright 2019 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 |
| |
| from RECIPE_MODULES.fuchsia.utils import cached_property |
| |
| # Pinned gcloud version. |
| GCLOUD_VERSION = "version:266.0.0.chromium0" |
| |
| |
| class GCloudApi(recipe_api.RecipeApi): |
| """GCloudApi provides support for common gcloud operations.""" |
| |
| def __init__(self, *args, **kwargs): |
| super(GCloudApi, self).__init__(*args, **kwargs) |
| self.gcloud_patched = False |
| |
| @cached_property |
| def _gcloud_executable(self): |
| gcloud_path = self.m.cipd.ensure_tool( |
| "infra/gcloud/${platform}", |
| GCLOUD_VERSION, |
| executable_path="lib/gcloud.py", |
| ) |
| return gcloud_path |
| |
| def __call__(self, *args, **kwargs): |
| """Executes specified gcloud command.""" |
| step_name = kwargs.pop("step_name", "gcloud %s" % args[0]) |
| return self.m.python(step_name, self._gcloud_executable, args, **kwargs) |
| |
| def container_image_exists(self, image): |
| step_result = self( |
| "container", |
| "images", |
| "describe", |
| image, |
| ok_ret="any", |
| step_name="check existence of %s" % image, |
| ) |
| return step_result.retcode == 0 |
| |
| def patch_gcloud_invoker(self): |
| """GCloud invoker has issues when running on bots, this API |
| patches the invoker to make it compatible with bots' python binary. |
| """ |
| if self.gcloud_patched or not self._gcloud_executable: |
| return |
| gcloud_path = self.m.path.join( |
| self.m.path.dirname(self.m.path.dirname(self._gcloud_executable)), |
| "bin/gcloud", |
| ) |
| self.m.file.remove("remove gcloud wrapper", gcloud_path) |
| self.m.file.copy( |
| "copy patched gcloud", |
| self.resource("gcloud"), |
| gcloud_path, |
| ) |
| self.gcloud_patched = True |