blob: 700536c0c7093b8255452a908c6bfbe71a43297d [file] [log] [blame]
# Copyright 2023 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
class BuildProxyWrapApi(recipe_api.RecipeApi):
"""BuildProxyWrapApi allows callers to run steps with build proxy."""
@property
def _buildproxywrap_tool(self):
"""Fetch and provision buildproxywrap from CIPD."""
return self.m.ensure_tool(
"buildproxywrap", self.resource("buildproxywrap/tool_manifest.json")
)
@property
def _socat_tool(self):
"""Fetch and provision socat from CIPD."""
return self.m.ensure_tool(
"socat",
self.resource("socat/tool_manifest.json"),
executable_path="bin/socat",
)
def env(self, socket_dir):
"""Constructs the set of environment variables used by buildproxywrap.
Args:
socket_dir (str): The directory to use for build sockets.
Returns: A dictionary containing the new environment.
"""
config = self.m.file.read_json(
"read buildproxywrap config",
self.resource("config.json"),
test_data=[
{
"name": "foo",
"socket_file_name": "foo.sock",
"socket_path_env_var": "FOO",
"server_address": "foobar.com",
},
],
)
return {
entry["socket_path_env_var"]: self.m.path.join(
socket_dir, entry["socket_file_name"]
)
for entry in config
}
def proxied_step(self, step_name, inner_cmd, socket_dir, **kwargs):
"""
Constructs and returns a step that runs inner_cmd with buildproxywrap.
Args:
step_name(str): The name of the step.
inner_cmd (list[str]): The command to run inside of buildproxywrap.
socket_dir (str): The directory to use for build sockets.
**kwargs (dict): Passed through as kwargs to step.
Returns: A step that runs the inner_cmd inside of buildproxywrap.
"""
cmd = [
self._buildproxywrap_tool,
"-socat",
self._socat_tool,
"-socket_dir",
socket_dir,
"-cfg",
self.resource("config.json"),
"--",
]
cmd.extend(inner_cmd)
return self.m.step(
step_name,
cmd,
**kwargs,
)