Skip to content

Commit

Permalink
v2.3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Jan 31, 2024
1 parent 9e66ae1 commit 8d090e1
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 6 deletions.
2 changes: 2 additions & 0 deletions phi/aws/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def create_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
from phi.cli.console import print_info, print_heading, confirm_yes_no
from phi.aws.resource.types import AwsResourceInstallOrder
Expand Down Expand Up @@ -451,6 +452,7 @@ def update_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
from phi.cli.console import print_info, print_heading, confirm_yes_no
from phi.aws.resource.types import AwsResourceInstallOrder
Expand Down
23 changes: 23 additions & 0 deletions phi/cli/ws/ws_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def up(
"--force",
help="Force create resources where applicable.",
),
pull: Optional[bool] = typer.Option(
None,
"-p",
"--pull",
help="Pull images where applicable.",
),
):
"""\b
Create resources for the active workspace
Expand Down Expand Up @@ -267,6 +273,7 @@ def up(
logger.debug(f"\tdry_run : {dry_run}")
logger.debug(f"\tauto_confirm : {auto_confirm}")
logger.debug(f"\tforce : {force}")
logger.debug(f"\tpull : {pull}")
print_heading("Starting workspace: {}".format(str(active_ws_config.ws_root_path.stem)))
start_workspace(
phi_config=phi_config,
Expand All @@ -279,6 +286,7 @@ def up(
dry_run=dry_run,
auto_confirm=auto_confirm,
force=force,
pull=pull,
)


Expand Down Expand Up @@ -493,6 +501,12 @@ def patch(
"--force",
help="Force",
),
pull: Optional[bool] = typer.Option(
None,
"-p",
"--pull",
help="Pull images where applicable.",
),
):
"""\b
Update resources for the active workspace.
Expand Down Expand Up @@ -601,6 +615,7 @@ def patch(
logger.debug(f"\tdry_run : {dry_run}")
logger.debug(f"\tauto_confirm : {auto_confirm}")
logger.debug(f"\tforce : {force}")
logger.debug(f"\tpull : {pull}")
print_heading("Updating workspace: {}".format(str(active_ws_config.ws_root_path.stem)))
update_workspace(
phi_config=phi_config,
Expand All @@ -613,6 +628,7 @@ def patch(
dry_run=dry_run,
auto_confirm=auto_confirm,
force=force,
pull=pull,
)


Expand Down Expand Up @@ -659,6 +675,12 @@ def restart(
"--force",
help="Force",
),
pull: Optional[bool] = typer.Option(
None,
"-p",
"--pull",
help="Pull images where applicable.",
),
):
"""\b
Restarts the active workspace. i.e. runs `phi ws down` and then `phi ws up`.
Expand Down Expand Up @@ -697,6 +719,7 @@ def restart(
auto_confirm=auto_confirm,
print_debug_log=print_debug_log,
force=force,
pull=pull,
)


Expand Down
3 changes: 3 additions & 0 deletions phi/docker/resource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class DockerResource(ResourceBase):
short_id: Optional[str] = None
attrs: Optional[Dict[str, Any]] = None

# Pull latest image before create/update
pull: Optional[bool] = None

docker_client: Optional[DockerApiClient] = None

@staticmethod
Expand Down
7 changes: 2 additions & 5 deletions phi/docker/resource/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,14 @@ def run_container(self, docker_client: DockerApiClient) -> Optional[Any]:
with Progress(
SpinnerColumn(spinner_name="dots"), TextColumn("{task.description}"), transient=True
) as progress:
if self.force:
if self.pull:
try:
pull_image_task = progress.add_task("Downloading Image...") # noqa: F841
_api_client.images.pull(self.image, platform=self.platform)
progress.update(pull_image_task, completed=True)
except Exception as pull_exc:
logger.debug(f"Could not pull image: {self.image}: {pull_exc}")
if self.force:
run_container_task = progress.add_task("Running Container...") # noqa: F841
else:
run_container_task = progress.add_task("Downloading Image...") # noqa: F841
run_container_task = progress.add_task("Running Container...") # noqa: F841
container_object = _api_client.containers.run(
name=self.name,
image=self.image,
Expand Down
82 changes: 82 additions & 0 deletions phi/docker/resource/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class DockerImage(DockerResource):
extra_hosts: Optional[Dict[str, Any]] = None
# Platform in the format os[/arch[/variant]].
platform: Optional[str] = None
# List of platforms to use for build, uses buildx_image if multi-platform build is enabled.
platforms: Optional[List[str]] = None
# Isolation technology used during build. Default: None.
isolation: Optional[str] = None
# If True, and if the docker client configuration file (~/.docker/config.json by default)
Expand All @@ -84,7 +86,87 @@ def get_image_str(self) -> str:
def get_resource_name(self) -> str:
return self.get_image_str()

def buildx(self, docker_client: Optional[DockerApiClient] = None) -> Optional[Any]:
"""Builds the image using buildx
Args:
docker_client: The DockerApiClient for the current cluster
Options: https://docs.docker.com/engine/reference/commandline/buildx_build/#options
"""
try:
import subprocess

tag = self.get_image_str()
nocache = self.skip_docker_cache or self.force
pull = self.pull or self.force

print_info(f"Building image: {tag}")
if self.path is not None:
print_info(f"\t path: {self.path}")
if self.dockerfile is not None:
print_info(f" dockerfile: {self.dockerfile}")
print_info(f" platforms: {self.platforms}")
logger.debug(f"nocache: {nocache}")
logger.debug(f"pull: {pull}")

command = ["docker", "buildx", "build"]

# Add tag
command.extend(["--tag", tag])

# Add dockerfile option, if set
if self.dockerfile is not None:
command.extend(["--file", self.dockerfile])

# Add build arguments
if self.buildargs:
for key, value in self.buildargs.items():
command.extend(["--build-arg", f"{key}={value}"])

# Add no-cache option, if set
if nocache:
command.append("--no-cache")

if not self.rm:
command.append("--rm=false")

if self.platforms:
command.append("--platform={}".format(",".join(self.platforms)))

if self.pull:
command.append("--pull")

if self.push_image:
command.append("--push")
else:
command.append("--load")

# Add path
if self.path is not None:
command.append(self.path)

# Run the command
logger.debug("Running command: {}".format(" ".join(command)))
result = subprocess.run(command)

# Handling output and errors
if result.returncode == 0:
print_info("Docker image built successfully.")
_docker_client = docker_client or self.get_docker_client()
return self._read(docker_client=_docker_client)
else:
logger.error("Error in building Docker image:")
return False
except Exception as e:
logger.error(e)
return None

def build_image(self, docker_client: DockerApiClient) -> Optional[Any]:
if self.platforms is not None:
logger.debug("Using buildx for multi-platform build")
return self.buildx(docker_client=docker_client)

from docker import DockerClient
from docker.errors import BuildError, APIError
from rich import box
Expand Down
6 changes: 6 additions & 0 deletions phi/docker/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def create_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
from phi.cli.console import print_info, print_heading, confirm_yes_no
from phi.docker.resource.types import DockerContainer, DockerResourceInstallOrder
Expand Down Expand Up @@ -184,6 +185,8 @@ def create_resources(
print_info(f"\n-==+==- {resource.get_resource_type()}: {resource.get_resource_name()}")
if force is True:
resource.force = True
if pull is True:
resource.pull = True
if isinstance(resource, DockerContainer):
if resource.network is None and self.network is not None:
resource.network = self.network
Expand Down Expand Up @@ -404,6 +407,7 @@ def update_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
from phi.cli.console import print_info, print_heading, confirm_yes_no
from phi.docker.resource.types import DockerContainer, DockerResourceInstallOrder
Expand Down Expand Up @@ -554,6 +558,8 @@ def update_resources(
print_info(f"\n-==+==- {resource.get_resource_type()}: {resource.get_resource_name()}")
if force is True:
resource.force = True
if pull is True:
resource.pull = True
if isinstance(resource, DockerContainer):
if resource.network is None and self.network is not None:
resource.network = self.network
Expand Down
2 changes: 2 additions & 0 deletions phi/infra/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def create_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
raise NotImplementedError

Expand All @@ -39,6 +40,7 @@ def update_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
raise NotImplementedError

Expand Down
2 changes: 2 additions & 0 deletions phi/k8s/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def create_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
from phi.cli.console import print_info, print_heading, confirm_yes_no
from phi.k8s.resource.types import K8sResourceInstallOrder
Expand Down Expand Up @@ -480,6 +481,7 @@ def update_resources(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = None,
) -> Tuple[int, int]:
from phi.cli.console import print_info, print_heading, confirm_yes_no
from phi.k8s.resource.types import K8sResourceInstallOrder
Expand Down
22 changes: 22 additions & 0 deletions phi/utils/shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List

from phi.utils.log import logger


def run_shell_command(args: List[str], tail: int = 100) -> str:
logger.info(f"Running shell command: {args}")

import subprocess

try:
result = subprocess.run(args, capture_output=True, text=True)
logger.debug(f"Result: {result}")
logger.debug(f"Return code: {result.returncode}")
if result.returncode != 0:
return f"Error: {result.stderr}"

# return only the last n lines of the output
return "\n".join(result.stdout.split("\n")[-tail:])
except Exception as e:
logger.warning(f"Failed to run shell command: {e}")
return f"Error: {e}"
4 changes: 4 additions & 0 deletions phi/workspace/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ def start_workspace(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = False,
) -> None:
"""Start a Phi Workspace. This is called from `phi ws up`"""
if ws_config is None:
Expand Down Expand Up @@ -440,6 +441,7 @@ def start_workspace(
dry_run=dry_run,
auto_confirm=auto_confirm,
force=force,
pull=pull,
)
if _num_resources_created > 0:
num_rgs_created += 1
Expand Down Expand Up @@ -586,6 +588,7 @@ def update_workspace(
dry_run: Optional[bool] = False,
auto_confirm: Optional[bool] = False,
force: Optional[bool] = None,
pull: Optional[bool] = False,
) -> None:
"""Update a Phi Workspace. This is called from `phi ws patch`"""
if ws_config is None:
Expand Down Expand Up @@ -621,6 +624,7 @@ def update_workspace(
dry_run=dry_run,
auto_confirm=auto_confirm,
force=force,
pull=pull,
)
if _num_resources_updated > 0:
num_rgs_updated += 1
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "phidata"
version = "2.3.11"
version = "2.3.12"
description = "Build AI Assistants using function calling"
requires-python = ">=3.8"
readme = "README.md"
Expand Down

0 comments on commit 8d090e1

Please sign in to comment.