Skip to content

Commit

Permalink
Add buildx builder support to get_ssh_agent_image
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejbrown committed Aug 17, 2024
1 parent bd5c10e commit ca289e8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 17 deletions.
18 changes: 16 additions & 2 deletions buildrunner/docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def force_remove_container(docker_client, container):
)


def get_dockerfile(dockerfile: str, temp_dir: str = None) -> Tuple[str, bool]:
def get_dockerfile(
dockerfile: str, temp_dir: str = None, path: str = None
) -> Tuple[str, bool]:
"""
Check if the dockerfile exists, if not create a temporary file and write the dockerfile to it.
:param dockerfile: the dockerfile
Expand All @@ -117,7 +119,19 @@ def get_dockerfile(dockerfile: str, temp_dir: str = None) -> Tuple[str, bool]:
cleanup_dockerfile = False
curr_dockerfile = None

if dockerfile:
if path and path != "." and not dockerfile:
if os.path.exists(path):
if os.path.exists(os.path.join(path, "Dockerfile")):
curr_dockerfile = os.path.join(path, "Dockerfile")
else:
raise BuildRunnerConfigurationError(
f"Path {path} exists but does not contain a Dockerfile"
)
else:
raise BuildRunnerConfigurationError(
f"Path {path} does not exist, cannot find Dockerfile"
)
elif dockerfile:
if os.path.exists(dockerfile):
curr_dockerfile = dockerfile
else:
Expand Down
2 changes: 1 addition & 1 deletion buildrunner/docker/multiplatform_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def build_multiple_images(
else:
path = "."

dockerfile, cleanup_dockerfile = get_dockerfile(file)
dockerfile, cleanup_dockerfile = get_dockerfile(dockerfile=file, path=path)

# Track this newly built image
built_image = BuiltImageInfo(id=str(uuid.uuid4()))
Expand Down
42 changes: 32 additions & 10 deletions buildrunner/sshagent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
from paramiko.util import asbytes
from paramiko.message import Message

import buildrunner.config
import buildrunner.docker.builder as legacy_builder
from buildrunner.errors import (
BuildRunnerConfigurationError,
BuildRunnerProcessingError,
)
import buildrunner.docker.builder as legacy_builder

SSH_AGENT_PROXY_BUILD_CONTEXT = os.path.join(
os.path.dirname(__file__), "SSHAgentProxyImage"
Expand Down Expand Up @@ -88,7 +89,9 @@ class DockerSSHAgentProxy:
implementation that is managed by this class.
"""

def __init__(self, docker_client, log, docker_registry):
def __init__(
self, docker_client, log, docker_registry, multiplatform_image_builder
):
""" """
self.docker_client = docker_client
self.log = log
Expand All @@ -97,6 +100,7 @@ def __init__(self, docker_client, log, docker_registry):
self._ssh_agent_container = None
self._ssh_client = None
self._ssh_channel = None
self._multiplatform_image_builder = multiplatform_image_builder

def get_info(self):
"""
Expand Down Expand Up @@ -249,15 +253,33 @@ def get_ssh_agent_image(self):
"""
Get and/or create the image used to proxy the ssh agent to a container.
"""
buildrunner_config = buildrunner.config.BuildRunnerConfig.get_instance()
if not self._ssh_agent_image:
self.log.write("Creating ssh-agent image\n")
image = legacy_builder.build_image(
path=SSH_AGENT_PROXY_BUILD_CONTEXT,
docker_registry=self.docker_registry,
nocache=False,
pull=False,
)
self._ssh_agent_image = image
if buildrunner_config.run_config.use_legacy_builder:
self.log.write("Creating ssh-agent image\n")
image = legacy_builder.build_image(
path=SSH_AGENT_PROXY_BUILD_CONTEXT,
docker_registry=self.docker_registry,
nocache=False,
pull=False,
)
self._ssh_agent_image = image
else:
native_platform = (
self._multiplatform_image_builder.get_native_platform()
)
platforms = [native_platform]
built_images_info = (
self._multiplatform_image_builder.build_multiple_images(
platforms=platforms,
path=SSH_AGENT_PROXY_BUILD_CONTEXT,
cache=False,
pull=False,
use_threading=False,
)
)
assert len(built_images_info.built_images) == 1
self._ssh_agent_image = built_images_info.built_images[0].trunc_digest
return self._ssh_agent_image


Expand Down
1 change: 1 addition & 0 deletions buildrunner/steprunner/tasks/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ def run(self, context: dict): # pylint: disable=too-many-statements,too-many-br
self._docker_client,
self.step_runner.log,
buildrunner_config.global_config.docker_registry,
self.step_runner.multi_platform,
)
self._sshagent.start(
buildrunner_config.get_ssh_keys_from_aliases(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from setuptools import setup, find_packages


BASE_VERSION = "3.12"
BASE_VERSION = "3.13"

SOURCE_DIR = os.path.dirname(os.path.abspath(__file__))
BUILDRUNNER_DIR = os.path.join(SOURCE_DIR, "buildrunner")
Expand Down
17 changes: 17 additions & 0 deletions tests/test-files/test-ssh-buildx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use-legacy-builder: True
steps:
clone:
build:
dockerfile: |
FROM {{ DOCKER_REGISTRY }}/rockylinux:8.5
RUN yum install -y git-core openssh-clients && yum clean all
run:
ssh-keys: ['buildrunner-deploy']
cmds:
- mkdir ~/.ssh
- ssh-keyscan github.com > ~/.ssh/known_hosts
- chmod 700 ~/.ssh
- chmod 600 ~/.ssh/known_hosts
# Clone into temp directory since the "buildrunner" directory may already exist
- rm -rf /tmp/test-clone
- git clone git@github.com:adobe/buildrunner.git /tmp/test-clone
14 changes: 11 additions & 3 deletions tests/test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def fixture_set_env():


@pytest.mark.parametrize(
"use_legacy_builder, config,",
"description, use_legacy_builder, config,",
[
# Use default builder
(
"Use buildx builder with platform",
False,
"""
use-legacy-builder: false
Expand All @@ -74,6 +74,7 @@ def fixture_set_env():
""",
),
(
"Use buildx builder",
False,
"""
use-legacy-builder: false
Expand All @@ -87,6 +88,7 @@ def fixture_set_env():
""",
),
(
"Overwrite use-legacy-builder with platforms",
False,
"""
use-legacy-builder: true
Expand All @@ -103,6 +105,7 @@ def fixture_set_env():
""",
),
(
"Use buildx builder with platforms",
False,
"""
use-legacy-builder: false
Expand All @@ -119,6 +122,7 @@ def fixture_set_env():
""",
),
(
"Default builder with platforms",
False,
"""
steps:
Expand All @@ -133,8 +137,8 @@ def fixture_set_env():
- linux/arm64
""",
),
# Use legacy builder
(
"Default builder",
True,
"""
steps:
Expand All @@ -147,6 +151,7 @@ def fixture_set_env():
""",
),
(
"Use legacy builder with platform",
True,
"""
use-legacy-builder: true
Expand All @@ -161,6 +166,7 @@ def fixture_set_env():
""",
),
(
"Use legacy builder with use-legacy-builder",
True,
"""
use-legacy-builder: true
Expand All @@ -184,9 +190,11 @@ def fixture_set_env():
def test_builders(
mock_buildx_builder,
mock_legacy_build,
description,
use_legacy_builder,
config,
):
_ = description
with tempfile.TemporaryDirectory() as tmpdirname:
tmp_filename = f"{tmpdirname}/config.yaml"
with open(tmp_filename, "w") as f:
Expand Down

0 comments on commit ca289e8

Please sign in to comment.