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

feat: add a debug mode for bentoctl #174

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
* **Customize deploy target** by creating bentoctl plugin from the [deployment operator template](https://github.com/bentoml/bentoctl-operator-template).

**Upcoming:**
* [Knative](https://github.com/bentoml/bentoctl/issues/79) (WIP)
* [Azure Functions](https://github.com/bentoml/azure-functions-deploy)
* [Knative](https://github.com/bentoml/bentoctl/issues/79)


## Install bentoctl
Expand Down
2 changes: 0 additions & 2 deletions bentoctl/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
push_docker_image_to_repository,
tag_docker_image,
)
from bentoctl.utils import get_debug_mode
from bentoctl.utils.terraform import (
is_terraform_applied,
terraform_apply,
Expand Down Expand Up @@ -147,7 +146,6 @@ def build(
generate_deployable_container(
tag=local_docker_tag,
deployment_config=deployment_config,
cleanup=get_debug_mode(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we clean up when not in debug mode? :)

)

if not dry_run:
Expand Down
9 changes: 7 additions & 2 deletions bentoctl/docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from bentoctl.console import console
from bentoctl.deployment_config import DeploymentConfig
from bentoctl.exceptions import BentoctlDockerException
from bentoctl.utils import get_debug_mode
from bentoctl.utils.temp_dir import TempDirectory

# default location were dockerfile can be found
Expand Down Expand Up @@ -51,9 +52,13 @@ def __rich_console__(self, *_):


def generate_deployable_container(
tag: str, deployment_config: DeploymentConfig, cleanup: bool
tag: str, deployment_config: DeploymentConfig
) -> None:
with TempDirectory(cleanup=cleanup) as dist_dir:
"""
Calls the operator and generates the deployable. If in debug more the generated
deployable will be moved to the current directory for easier debugging.
"""
with TempDirectory(debug=get_debug_mode()) as dist_dir:
Comment on lines +57 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving directory is a bit unnatural for debugging. Maybe preserving and printing the temp directory during debug should be sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the behavior that we have now (except printing which folder it is). I prefer to have --debug put the intermediate deployable in my cur-dir since it speeds up debugging, but it is a personal preference.

env = {"DOCKER_BUILDKIT": "1", "DOCKER_SCAN_SUGGEST": "false"}
buildx_args = {
"subprocess_env": env,
Expand Down
12 changes: 11 additions & 1 deletion bentoctl/utils/temp_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class TempDirectory(object):
def __init__(
self,
cleanup=True,
debug=False,
prefix="temp",
):

self._cleanup = cleanup
self._prefix = prefix
self.path = None
self.debug = debug

def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.path)
Expand All @@ -29,6 +31,8 @@ def __enter__(self) -> Path:
return Path(self.create())

def __exit__(self, exc_type, exc_val, exc_tb):
if self.debug:
self.move_to_curdir()
if self._cleanup:
self.cleanup()

Expand All @@ -46,4 +50,10 @@ def cleanup(self, ignore_errors=False):
"""
if self.path is not None and os.path.exists(self.path):
shutil.rmtree(self.path, ignore_errors=ignore_errors)
Comment on lines 50 to 52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If in debug mode, maybe we can print the temp dir instead of removing.

self.path = None

def move_to_curdir(self):
deployable_path = os.path.join(os.curdir, "bentoctl_deployable")
if os.path.exists(deployable_path):
shutil.rmtree(deployable_path)

shutil.copytree(self.path, deployable_path)