Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
change cli name for build and update cli option for init (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
yubozhao committed Apr 8, 2022
1 parent 397ad3e commit 27d4ded
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
54 changes: 28 additions & 26 deletions bentoctl/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os
import sys

import click
import yaml

from bentoctl import __version__
from bentoctl.cli.interactive import deployment_config_builder
from bentoctl.cli.operator_management import get_operator_management_subcommands
from bentoctl.cli.utils import BentoctlCommandGroup, handle_bentoctl_exceptions
from bentoctl.console import print_generated_files_list, prompt_user_for_filename
from bentoctl.deployment_config import DeploymentConfig
from bentoctl.docker_utils import build_docker_image, push_docker_image_to_repository
from bentoctl.docker_utils import build_docker_image, push_docker_image_to_repository, tag_docker_image
from bentoctl.exceptions import BentoctlException
from bentoctl.utils import TempDirectory, console

Expand All @@ -36,8 +34,9 @@ def bentoctl():

@bentoctl.command()
@click.option(
"--generate/--no-generate",
default=True,
"--do-not-generate",
is_flag=True,
default=False,
help="Generate template files based on the provided operator.",
)
@click.option(
Expand All @@ -47,7 +46,7 @@ def bentoctl():
default=os.curdir,
)
@handle_bentoctl_exceptions
def init(save_path, generate):
def init(save_path, do_not_generate):
"""
Start the interactive deployment config builder file.
Expand All @@ -71,7 +70,7 @@ def init(save_path, generate):
f"{os.path.relpath(config_path, save_path)}[/]"
)

if generate:
if not do_not_generate:
generated_files = deployment_config.generate()
print_generated_files_list(generated_files)

Expand Down Expand Up @@ -117,16 +116,19 @@ def generate(deployment_config_file, values_only, save_path):
help="path to deployment_config file",
required=True,
)
@click.option('--dry-run', is_flag=True, help='Dry run', default=False)
@handle_bentoctl_exceptions
def publish(
def build(
bento_tag,
deployment_config_file,
dry_run,
):
"""
publish the Docker image.
Build the Docker image for the given deployment config file and bento.
"""
deployment_config = DeploymentConfig.from_file(deployment_config_file)
deployment_config.set_bento(bento_tag)
local_docker_tag = deployment_config.get_local_docker_tag()
with TempDirectory() as dist_dir:
(
dockerfile_path,
Expand All @@ -135,29 +137,29 @@ def publish(
) = deployment_config.create_deployable(
destination_dir=dist_dir,
)
build_docker_image(
image_tag=local_docker_tag,
context_path=dockercontext_path,
dockerfile=dockerfile_path,
additional_build_args=build_args,
)
if not dry_run:
(
registry_url,
registry_username,
registry_password,
) = deployment_config.get_registry_info()

image_tag = deployment_config.generate_docker_image_tag(registry_url)

build_docker_image(
image_tag=image_tag,
context_path=dockercontext_path,
dockerfile=dockerfile_path,
additional_build_args=build_args,
repository_image_tag = deployment_config.generate_docker_image_tag(registry_url)
tag_docker_image(local_docker_tag, repository_image_tag)
push_docker_image_to_repository(
repository=repository_image_tag,
username=registry_username,
password=registry_password,
)

push_docker_image_to_repository(
repository=image_tag,
username=registry_username,
password=registry_password,
)
generated_files = deployment_config.generate(values_only=True)
print_generated_files_list(generated_files)

generated_files = deployment_config.generate(values_only=True)
print_generated_files_list(generated_files)
else:
console.print(f"[green]Create docker image: {local_docker_tag}[/]")

# subcommands
bentoctl.add_command(get_operator_management_subcommands())
4 changes: 4 additions & 0 deletions bentoctl/deployment_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,7 @@ def generate_docker_image_tag(self, registry_url: str) -> str:
image_tag = f"{registry_url.replace('https://', '')}/{self.repository_name}:{self.bento.tag.version}"
self.operator_spec["image_tag"] = image_tag
return image_tag

def generate_local_image_tag(self) -> str:
image_tag = f"{self.operator_name}-{self.bento.tag.name}:{self.bento.tag.version}"
return image_tag
18 changes: 13 additions & 5 deletions bentoctl/docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rich.live import Live

from bentoctl.console import console
from bentoctl.exceptions import BentoctlBuildException, BentoctlPushException
from bentoctl.exceptions import BentoctlDockerException


class DockerPushProgressBar:
Expand Down Expand Up @@ -66,16 +66,24 @@ def build_docker_image(
for line in output_stream:
print(line.get("stream", ""), end="")
if "errorDetail" in line: # incase error while building.
raise BentoctlBuildException(
raise BentoctlDockerException(
f"Failed to build docker image {image_tag}: {line['error']}"
)
console.print(":hammer: Image build!")
except (docker.errors.APIError, docker.errors.BuildError) as error:
raise BentoctlBuildException(
raise BentoctlDockerException(
f"Failed to build docker image {image_tag}: {error}"
)


def tag_docker_image(image_name, image_tag):
docker_client = docker.from_env()
try:
docker_client.images.tag(image_name, image_tag)
except docker.errors.APIError as error:
raise BentoctlDockerException(f"Failed to tag docker image {image_tag}: {error}")


def push_docker_image_to_repository(
repository, image_tag=None, username=None, password=None
):
Expand All @@ -95,9 +103,9 @@ def push_docker_image_to_repository(
elif "status" in line:
print(line.get("status"))
elif "errorDetail" in line:
raise BentoctlPushException(
raise BentoctlDockerException(
f"Failed to push docker image. {line['error']}"
)
console.print(":rocket: Image pushed!")
except docker.errors.APIError as error:
raise BentoctlPushException(f"Failed to push docker image {image_tag}: {error}")
raise BentoctlDockerException(f"Failed to push docker image {image_tag}: {error}")
10 changes: 2 additions & 8 deletions bentoctl/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,9 @@ def __init__(self, bento_tag: str):
super(BentoNotFound, self).__init__(self.msg)


class BentoctlBuildException(BentoctlException):
class BentoctlDockerException(BentoctlException):
"""
Raised when docker build failed
"""


class BentoctlPushException(BentoctlException):
"""
Raised when docker push failed
Raised when docker command fails
"""


Expand Down

0 comments on commit 27d4ded

Please sign in to comment.