|
1 | 1 | # Copyright (c) 2025 Airbyte, Inc., all rights reserved.
|
2 |
| -"""Docker image commands. |
| 2 | +"""Airbyte CDK 'image' commands. |
3 | 3 |
|
4 |
| -Coming soon. |
5 |
| -
|
6 |
| -This module is planned to provide a command line interface (CLI) for building |
7 |
| -Docker images for Airbyte CDK connectors. |
| 4 | +The `airbyte-cdk image build` command provides a simple way to work with Airbyte |
| 5 | +connector images. |
8 | 6 | """
|
9 | 7 |
|
10 |
| -import click |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import rich_click as click |
| 12 | + |
| 13 | +from airbyte_cdk.cli.airbyte_cdk._util import resolve_connector_name_and_directory |
| 14 | +from airbyte_cdk.models.connector_metadata import MetadataFile |
| 15 | +from airbyte_cdk.utils.docker import ( |
| 16 | + ConnectorImageBuildError, |
| 17 | + build_connector_image, |
| 18 | + verify_docker_installation, |
| 19 | +) |
11 | 20 |
|
12 | 21 |
|
13 | 22 | @click.group(
|
14 | 23 | name="image",
|
15 | 24 | help=__doc__.replace("\n", "\n\n"), # Render docstring as help text (markdown)
|
16 | 25 | )
|
17 | 26 | def image_cli_group() -> None:
|
18 |
| - """Docker image commands.""" |
19 |
| - pass |
| 27 | + """Commands for working with connector Docker images.""" |
| 28 | + |
| 29 | + |
| 30 | +@image_cli_group.command() |
| 31 | +@click.option( |
| 32 | + "--connector-name", |
| 33 | + type=str, |
| 34 | + help="Name of the connector to test. Ignored if --connector-directory is provided.", |
| 35 | +) |
| 36 | +@click.option( |
| 37 | + "--connector-directory", |
| 38 | + type=click.Path(exists=True, file_okay=False, path_type=Path), |
| 39 | + help="Path to the connector directory.", |
| 40 | +) |
| 41 | +@click.option("--tag", default="dev", help="Tag to apply to the built image (default: dev)") |
| 42 | +@click.option("--no-verify", is_flag=True, help="Skip verification of the built image") |
| 43 | +def build( |
| 44 | + connector_name: str | None = None, |
| 45 | + connector_directory: Path | None = None, |
| 46 | + *, |
| 47 | + tag: str = "dev", |
| 48 | + no_verify: bool = False, |
| 49 | +) -> None: |
| 50 | + """Build a connector Docker image. |
| 51 | +
|
| 52 | + This command builds a Docker image for a connector, using either |
| 53 | + the connector's Dockerfile or a base image specified in the metadata. |
| 54 | + The image is built for both AMD64 and ARM64 architectures. |
| 55 | + """ |
| 56 | + if not verify_docker_installation(): |
| 57 | + click.echo( |
| 58 | + "Docker is not installed or not running. Please install Docker and try again.", err=True |
| 59 | + ) |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + connector_name, connector_directory = resolve_connector_name_and_directory( |
| 63 | + connector_name=connector_name, |
| 64 | + connector_directory=connector_directory, |
| 65 | + ) |
| 66 | + |
| 67 | + metadata_file_path: Path = connector_directory / "metadata.yaml" |
| 68 | + try: |
| 69 | + metadata = MetadataFile.from_file(metadata_file_path) |
| 70 | + except (FileNotFoundError, ValueError) as e: |
| 71 | + click.echo( |
| 72 | + f"Error loading metadata file '{metadata_file_path}': {e!s}", |
| 73 | + err=True, |
| 74 | + ) |
| 75 | + sys.exit(1) |
| 76 | + click.echo(f"Building Image for Connector: {metadata.data.dockerRepository}:{tag}") |
| 77 | + try: |
| 78 | + build_connector_image( |
| 79 | + connector_directory=connector_directory, |
| 80 | + connector_name=connector_name, |
| 81 | + metadata=metadata, |
| 82 | + tag=tag, |
| 83 | + no_verify=no_verify, |
| 84 | + ) |
| 85 | + except ConnectorImageBuildError as e: |
| 86 | + click.echo( |
| 87 | + f"Error building connector image: {e!s}", |
| 88 | + err=True, |
| 89 | + ) |
| 90 | + sys.exit(1) |
20 | 91 |
|
21 | 92 |
|
22 | 93 | __all__ = [
|
|
0 commit comments