From 20bc7dd17379ddbbaf14595282a081f493aa589d Mon Sep 17 00:00:00 2001 From: Yash Pratap Date: Sun, 1 Oct 2023 12:44:38 -0400 Subject: [PATCH 1/4] phi stop --- phi/cli/entrypoint.py | 244 +++++++++++++++++++++--------------------- phi/cli/operator.py | 166 +++++++++++++--------------- 2 files changed, 195 insertions(+), 215 deletions(-) diff --git a/phi/cli/entrypoint.py b/phi/cli/entrypoint.py index 1eb3d2b68..267b3f81a 100644 --- a/phi/cli/entrypoint.py +++ b/phi/cli/entrypoint.py @@ -386,128 +386,128 @@ def start( ) -# @phi_cli.command(short_help="Stop resources defined in a resources.py file") -# def stop( -# resources_file: str = typer.Argument( -# "resources.py", -# help="Path to workspace file.", -# show_default=False, -# ), -# env_filter: Optional[str] = typer.Option( -# None, "-e", "--env", metavar="", help="Filter the environment to deploy" -# ), -# config_filter: Optional[str] = typer.Option( -# None, "-c", "--config", metavar="", help="Filter the config to deploy" -# ), -# name_filter: Optional[str] = typer.Option( -# None, "-n", "--name", metavar="", help="Filter using resource name" -# ), -# type_filter: Optional[str] = typer.Option( -# None, -# "-t", -# "--type", -# metavar="", -# help="Filter using resource type", -# ), -# group_filter: Optional[str] = typer.Option( -# None, "-g", "--group", metavar="", help="Filter using group name" -# ), -# dry_run: bool = typer.Option( -# False, -# "-dr", -# "--dry-run", -# help="Print which resources will be deployed and exit.", -# ), -# auto_confirm: bool = typer.Option( -# False, -# "-y", -# "--yes", -# help="Skip the confirmation before deploying resources.", -# ), -# print_debug_log: bool = typer.Option( -# False, -# "-d", -# "--debug", -# help="Print debug logs.", -# ), -# force: bool = typer.Option( -# False, -# "-f", -# "--force", -# help="Force", -# ), -# ): -# """\b -# Start resources defined in a resources.py file -# \b -# Examples: -# > `phi ws start` -> Start resources defined in a resources.py file -# > `phi ws start workspace.py` -> Start resources defined in a workspace.py file -# """ -# from pathlib import Path -# from phi.conf.phi_conf import PhiConf -# -# from phi.cli.operator import stop_resources -# from phi.utils.load_env import load_env -# from phi.utils.cli_console import print_error, print_conf_not_available_msg -# from phi.workspace.ws_enums import WorkspaceConfigType -# -# if print_debug_log: -# set_log_level_to_debug() -# -# phi_conf: Optional[PhiConf] = PhiConf.get_saved_conf() -# if not phi_conf: -# print_conf_not_available_msg() -# return -# -# # Load environment from .env -# load_env( -# env={ -# "PHI_WS_FORCE": str(force), -# }, -# dotenv_dir=Path(".").resolve(), -# ) -# -# target_env: Optional[str] = None -# target_config_str: Optional[str] = None -# target_config: Optional[WorkspaceConfigType] = None -# target_name: Optional[str] = None -# target_type: Optional[str] = None -# target_group: Optional[str] = None -# -# if env_filter is not None and isinstance(env_filter, str): -# target_env = env_filter -# if config_filter is not None and isinstance(config_filter, str): -# target_config_str = config_filter -# if name_filter is not None and isinstance(name_filter, str): -# target_name = name_filter -# if type_filter is not None and isinstance(type_filter, str): -# target_type = type_filter -# if group_filter is not None and isinstance(group_filter, str): -# target_group = group_filter -# -# if target_config_str is not None: -# if target_config_str.lower() not in WorkspaceConfigType.values_list(): -# print_error( -# f"{target_config_str} not supported", -# f"please choose from: {WorkspaceConfigType.values_list()}" -# ) -# return -# target_config = WorkspaceConfigType.from_str(target_config_str) -# -# resources_file_path: Path = Path(".").resolve().joinpath(resources_file) -# stop_resources( -# resources_file_path=resources_file_path, -# target_env=target_env, -# target_config=target_config, -# target_name=target_name, -# target_type=target_type, -# target_group=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# -# +@phi_cli.command(short_help="Stop resources defined in a resources.py file") +def stop( + resources_file: str = typer.Argument( + "resources.py", + help="Path to workspace file.", + show_default=False, + ), + env_filter: Optional[str] = typer.Option(None, "-e", "--env", metavar="", help="Filter the environment to deploy"), + infra_filter: Optional[str] = typer.Option(None, "-i", "--infra", metavar="", help="Filter the infra to deploy."), + config_filter: Optional[str] = typer.Option(None, "-c", "--config", metavar="", help="Filter the config to deploy"), + group_filter: Optional[str] = typer.Option( + None, "-g", "--group", metavar="", help="Filter resources using group name." + ), + name_filter: Optional[str] = typer.Option(None, "-n", "--name", metavar="", help="Filter using resource name"), + type_filter: Optional[str] = typer.Option( + None, + "-t", + "--type", + metavar="", + help="Filter using resource type", + ), + dry_run: bool = typer.Option( + False, + "-dr", + "--dry-run", + help="Print resources and exit.", + ), + auto_confirm: bool = typer.Option( + False, + "-y", + "--yes", + help="Skip the confirmation before deploying resources.", + ), + print_debug_log: bool = typer.Option( + False, + "-d", + "--debug", + help="Print debug logs.", + ), + force: bool = typer.Option( + False, + "-f", + "--force", + help="Force", + ), +): + """\b + Start resources defined in a resources.py file + \b + Examples: + > `phi ws start` -> Start resources defined in a resources.py file + > `phi ws start workspace.py` -> Start resources defined in a workspace.py file + """ + if print_debug_log: + set_log_level_to_debug() + # from pathlib import Path + # from phi.conf.phi_conf import PhiConf + + # from phi.cli.operator import stop_resources + # from phi.utils.load_env import load_env + # from phi.utils.cli_console import print_error, print_conf_not_available_msg + # from phi.workspace.ws_enums import WorkspaceConfigType + + from pathlib import Path + from phi.cli.config import PhiCliConfig + from phi.cli.console import log_config_not_available_msg + from phi.cli.operator import stop_resources + from phi.infra.type import InfraType + + phi_config: Optional[PhiCliConfig] = PhiCliConfig.from_saved_config() + if not phi_config: + log_config_not_available_msg() + return + + # # Load environment from .env + # load_env( + # env={ + # "PHI_WS_FORCE": str(force), + # }, + # dotenv_dir=Path(".").resolve(), + # ) + + target_env: Optional[str] = None + target_infra_str: Optional[str] = None + target_infra: Optional[InfraType] = None + target_group: Optional[str] = None + target_name: Optional[str] = None + target_type: Optional[str] = None + + if env_filter is not None and isinstance(env_filter, str): + target_env = env_filter + if infra_filter is not None and isinstance(infra_filter, str): + target_infra_str = infra_filter + if group_filter is not None and isinstance(group_filter, str): + target_group = group_filter + if name_filter is not None and isinstance(name_filter, str): + target_name = name_filter + if type_filter is not None and isinstance(type_filter, str): + target_type = type_filter + + if target_infra_str is not None: + try: + target_infra = InfraType(target_infra_str.lower()) + except KeyError: + logger.error(f"{target_infra_str} is not supported") + return + + resources_file_path: Path = Path(".").resolve().joinpath(resources_file) + stop_resources( + phi_config=phi_config, + resources_file_path=resources_file_path, + target_env=target_env, + target_infra=target_infra, + target_group=target_group, + target_name=target_name, + target_type=target_type, + dry_run=dry_run, + auto_confirm=auto_confirm, + force=force, + ) + + # @phi_cli.command(short_help="Update resources defined in a resources.py file") # def patch( # resources_file: str = typer.Argument( diff --git a/phi/cli/operator.py b/phi/cli/operator.py index 7c6d2a7da..7406d7a51 100644 --- a/phi/cli/operator.py +++ b/phi/cli/operator.py @@ -236,99 +236,79 @@ def start_resources( logger.error("Some resources failed to create, please check logs") -# def stop_resources( -# resources_file_path: Path, -# target_env: Optional[str] = None, -# target_config: Optional[Any] = None, -# target_name: Optional[str] = None, -# target_type: Optional[str] = None, -# target_group: Optional[str] = None, -# dry_run: Optional[bool] = False, -# auto_confirm: Optional[bool] = False, -# ) -> None: -# print_heading(f"Stopping resources in: {resources_file_path}") -# logger.debug(f"\ttarget_env : {target_env}") -# logger.debug(f"\ttarget_config: {target_config}") -# logger.debug(f"\ttarget_name : {target_name}") -# logger.debug(f"\ttarget_type : {target_type}") -# logger.debug(f"\ttarget_group : {target_group}") -# logger.debug(f"\tdry_run : {dry_run}") -# logger.debug(f"\tauto_confirm : {auto_confirm}") -# -# from phidata.aws.config import AwsConfig -# from phidata.docker.config import DockerConfig -# from phidata.infra.config import InfraConfig -# from phidata.k8s.config import K8sConfig -# from phidata.workspace import WorkspaceConfig -# -# from phi.utils.prep_infra_config import filter_and_prep_configs -# -# if not resources_file_path.exists(): -# logger.error(f"File does not exist: {resources_file_path}") -# return -# -# ws_config: WorkspaceConfig = WorkspaceConfig.from_file(resources_file_path) -# # Set the local environment variables before processing configs -# ws_config.set_local_env() -# -# configs_to_shutdown: List[InfraConfig] = filter_and_prep_configs( -# ws_config=ws_config, -# target_env=target_env, -# target_config=target_config, -# order="create", -# ) -# -# num_configs_to_shutdown = len(configs_to_shutdown) -# num_configs_shutdown = 0 -# for config in configs_to_shutdown: -# logger.debug(f"Deploying {config.__class__.__name__}") -# if isinstance(config, DockerConfig): -# from phi.docker.docker_operator import shutdown_docker_config -# -# shutdown_docker_config( -# config=config, -# name_filter=target_name, -# type_filter=target_type, -# app_filter=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# num_configs_shutdown += 1 -# if isinstance(config, K8sConfig): -# from phi.k8s.k8s_operator import shutdown_k8s_config -# -# shutdown_k8s_config( -# config=config, -# name_filter=target_name, -# type_filter=target_type, -# app_filter=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# num_configs_shutdown += 1 -# if isinstance(config, AwsConfig): -# from phi.aws.aws_operator import shutdown_aws_config -# -# shutdown_aws_config( -# config=config, -# name_filter=target_name, -# type_filter=target_type, -# app_filter=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# num_configs_shutdown += 1 -# # white space between runs -# print_info("") -# -# print_info(f"# Configs shutdown: {num_configs_shutdown}/{num_configs_to_shutdown}\n") -# if num_configs_to_shutdown == num_configs_shutdown: -# if not dry_run: -# print_subheading("Workspace shutdown success") -# else: -# logger.error("Workspace shutdown failed") -# -# +def stop_resources( + phi_config: PhiCliConfig, + resources_file_path: Path, + target_env: Optional[str] = None, + target_infra: Optional[InfraType] = None, + target_group: Optional[str] = None, + target_name: Optional[str] = None, + target_type: Optional[str] = None, + dry_run: Optional[bool] = False, + auto_confirm: Optional[bool] = False, + force: Optional[bool] = None, +) -> None: + print_heading(f"Stopping resources in: {resources_file_path}") + logger.debug(f"\ttarget_env : {target_env}") + logger.debug(f"\ttarget_infra : {target_infra}") + logger.debug(f"\ttarget_name : {target_name}") + logger.debug(f"\ttarget_type : {target_type}") + logger.debug(f"\ttarget_group : {target_group}") + logger.debug(f"\tdry_run : {dry_run}") + logger.debug(f"\tauto_confirm : {auto_confirm}") + logger.debug(f"\tforce : {force}") + + from phi.workspace.config import WorkspaceConfig + + if not resources_file_path.exists(): + logger.error(f"File does not exist: {resources_file_path}") + return + + # Get resource groups to shutdown + resource_groups_to_shutdown: List[InfraResources] = WorkspaceConfig.get_resources_from_file( + resource_file=resources_file_path, + env=target_env, + infra=target_infra, + order="create", + ) + + # Track number of resource groups deleted + num_rgs_delete = 0 + num_rgs_to_shutdown = len(resource_groups_to_shutdown) + # Track number of resources created + num_resources_shutdown = 0 + num_resources_to_shutdown = 0 + + if num_rgs_to_shutdown == 0: + print_info("No resources to delete") + return + + for rg in resource_groups_to_shutdown: + num_resources_shutdown, num_resources_to_shutdown = rg.delete_resources( + group_filter=target_group, + name_filter=target_name, + type_filter=target_type, + dry_run=dry_run, + auto_confirm=auto_confirm, + force=force, + ) + if num_resources_shutdown > 0: + num_rgs_delete += 1 + num_resources_to_shutdown += num_resources_shutdown + num_resources_to_shutdown += num_resources_to_shutdown + logger.debug(f"Deleted {num_resources_to_shutdown} resources in {num_rgs_delete} resource groups") + + if dry_run: + return + + if num_resources_to_shutdown == 0: + return + + print_heading(f"\n--**-- ResourceGroups deleted: {num_rgs_delete}/{num_rgs_to_shutdown}\n") + if num_resources_shutdown != num_resources_to_shutdown: + logger.error("Some resources failed to delete, please check logs") + + # def patch_resources( # resources_file_path: Path, # target_env: Optional[str] = None, From dbddd63c7c3054e7f04007a0ebac15ed0eb099cc Mon Sep 17 00:00:00 2001 From: Yash Pratap Date: Mon, 2 Oct 2023 11:17:52 -0400 Subject: [PATCH 2/4] update and restart --- phi/cli/entrypoint.py | 413 +++++++++++++++++++----------------------- phi/cli/operator.py | 228 +++++++++++++---------- 2 files changed, 317 insertions(+), 324 deletions(-) diff --git a/phi/cli/entrypoint.py b/phi/cli/entrypoint.py index 267b3f81a..fa14dc333 100644 --- a/phi/cli/entrypoint.py +++ b/phi/cli/entrypoint.py @@ -441,13 +441,6 @@ def stop( """ if print_debug_log: set_log_level_to_debug() - # from pathlib import Path - # from phi.conf.phi_conf import PhiConf - - # from phi.cli.operator import stop_resources - # from phi.utils.load_env import load_env - # from phi.utils.cli_console import print_error, print_conf_not_available_msg - # from phi.workspace.ws_enums import WorkspaceConfigType from pathlib import Path from phi.cli.config import PhiCliConfig @@ -460,14 +453,6 @@ def stop( log_config_not_available_msg() return - # # Load environment from .env - # load_env( - # env={ - # "PHI_WS_FORCE": str(force), - # }, - # dotenv_dir=Path(".").resolve(), - # ) - target_env: Optional[str] = None target_infra_str: Optional[str] = None target_infra: Optional[InfraType] = None @@ -508,215 +493,195 @@ def stop( ) -# @phi_cli.command(short_help="Update resources defined in a resources.py file") -# def patch( -# resources_file: str = typer.Argument( -# "resources.py", -# help="Path to workspace file.", -# show_default=False, -# ), -# env_filter: Optional[str] = typer.Option( -# None, "-e", "--env", metavar="", help="Filter the environment to deploy" -# ), -# config_filter: Optional[str] = typer.Option( -# None, "-c", "--config", metavar="", help="Filter the config to deploy" -# ), -# name_filter: Optional[str] = typer.Option( -# None, "-n", "--name", metavar="", help="Filter using resource name" -# ), -# type_filter: Optional[str] = typer.Option( -# None, -# "-t", -# "--type", -# metavar="", -# help="Filter using resource type", -# ), -# group_filter: Optional[str] = typer.Option( -# None, "-g", "--group", metavar="", help="Filter using group name" -# ), -# dry_run: bool = typer.Option( -# False, -# "-dr", -# "--dry-run", -# help="Print which resources will be deployed and exit.", -# ), -# auto_confirm: bool = typer.Option( -# False, -# "-y", -# "--yes", -# help="Skip the confirmation before deploying resources.", -# ), -# print_debug_log: bool = typer.Option( -# False, -# "-d", -# "--debug", -# help="Print debug logs.", -# ), -# force: bool = typer.Option( -# False, -# "-f", -# "--force", -# help="Force", -# ), -# ): -# """\b -# Start resources defined in a resources.py file -# \b -# Examples: -# > `phi ws start` -> Start resources defined in a resources.py file -# > `phi ws start workspace.py` -> Start resources defined in a workspace.py file -# """ -# from pathlib import Path -# from phi.conf.phi_conf import PhiConf -# -# from phi.cli.operator import patch_resources -# from phi.utils.load_env import load_env -# from phi.utils.cli_console import print_error, print_conf_not_available_msg -# from phi.workspace.ws_enums import WorkspaceConfigType -# -# if print_debug_log: -# set_log_level_to_debug() -# -# phi_conf: Optional[PhiConf] = PhiConf.get_saved_conf() -# if not phi_conf: -# print_conf_not_available_msg() -# return -# -# # Load environment from .env -# load_env( -# env={ -# "PHI_WS_FORCE": str(force), -# }, -# dotenv_dir=Path(".").resolve(), -# ) -# -# target_env: Optional[str] = None -# target_config_str: Optional[str] = None -# target_config: Optional[WorkspaceConfigType] = None -# target_name: Optional[str] = None -# target_type: Optional[str] = None -# target_group: Optional[str] = None -# -# if env_filter is not None and isinstance(env_filter, str): -# target_env = env_filter -# if config_filter is not None and isinstance(config_filter, str): -# target_config_str = config_filter -# if name_filter is not None and isinstance(name_filter, str): -# target_name = name_filter -# if type_filter is not None and isinstance(type_filter, str): -# target_type = type_filter -# if group_filter is not None and isinstance(group_filter, str): -# target_group = group_filter -# -# if target_config_str is not None: -# if target_config_str.lower() not in WorkspaceConfigType.values_list(): -# print_error( -# f"{target_config_str} not supported", -# f"please choose from: {WorkspaceConfigType.values_list()}" -# ) -# return -# target_config = WorkspaceConfigType.from_str(target_config_str) -# -# resources_file_path: Path = Path(".").resolve().joinpath(resources_file) -# patch_resources( -# resources_file_path=resources_file_path, -# target_env=target_env, -# target_config=target_config, -# target_name=target_name, -# target_type=target_type, -# target_group=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# -# -# @phi_cli.command(short_help="Restart resources defined in a resources.py file") -# def restart( -# resources_file: str = typer.Argument( -# "resources.py", -# help="Path to workspace file.", -# show_default=False, -# ), -# env_filter: Optional[str] = typer.Option( -# None, "-e", "--env", metavar="", help="Filter the environment to deploy" -# ), -# config_filter: Optional[str] = typer.Option( -# None, "-c", "--config", metavar="", help="Filter the config to deploy" -# ), -# name_filter: Optional[str] = typer.Option( -# None, "-n", "--name", metavar="", help="Filter using resource name" -# ), -# type_filter: Optional[str] = typer.Option( -# None, -# "-t", -# "--type", -# metavar="", -# help="Filter using resource type", -# ), -# group_filter: Optional[str] = typer.Option( -# None, "-g", "--group", metavar="", help="Filter using group name" -# ), -# dry_run: bool = typer.Option( -# False, -# "-dr", -# "--dry-run", -# help="Print which resources will be deployed and exit.", -# ), -# auto_confirm: bool = typer.Option( -# False, -# "-y", -# "--yes", -# help="Skip the confirmation before deploying resources.", -# ), -# print_debug_log: bool = typer.Option( -# False, -# "-d", -# "--debug", -# help="Print debug logs.", -# ), -# force: bool = typer.Option( -# False, -# "-f", -# "--force", -# help="Force", -# ), -# ): -# """\b -# Restart resources defined in a resources.py file -# \b -# Examples: -# > `phi ws restart` -> Start resources defined in a resources.py file -# > `phi ws restart workspace.py` -> Start resources defined in a workspace.py file -# """ -# from time import sleep -# from phi.utils.cli_console import print_info -# -# stop( -# resources_file=resources_file, -# env_filter=env_filter, -# config_filter=config_filter, -# name_filter=name_filter, -# type_filter=type_filter, -# group_filter=group_filter, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# print_debug_log=print_debug_log, -# force=force, -# ) -# print_info("Sleeping for 2 seconds..") -# sleep(2) -# start( -# resources_file=resources_file, -# env_filter=env_filter, -# config_filter=config_filter, -# name_filter=name_filter, -# type_filter=type_filter, -# group_filter=group_filter, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# print_debug_log=print_debug_log, -# force=force, -# ) +@phi_cli.command(short_help="Update resources defined in a resources.py file") +def patch( + resources_file: str = typer.Argument( + "resources.py", + help="Path to workspace file.", + show_default=False, + ), + env_filter: Optional[str] = typer.Option(None, "-e", "--env", metavar="", help="Filter the environment to deploy"), + infra_filter: Optional[str] = typer.Option(None, "-i", "--infra", metavar="", help="Filter the infra to deploy."), + config_filter: Optional[str] = typer.Option(None, "-c", "--config", metavar="", help="Filter the config to deploy"), + group_filter: Optional[str] = typer.Option( + None, "-g", "--group", metavar="", help="Filter resources using group name." + ), + name_filter: Optional[str] = typer.Option(None, "-n", "--name", metavar="", help="Filter using resource name"), + type_filter: Optional[str] = typer.Option( + None, + "-t", + "--type", + metavar="", + help="Filter using resource type", + ), + dry_run: bool = typer.Option( + False, + "-dr", + "--dry-run", + help="Print which resources will be deployed and exit.", + ), + auto_confirm: bool = typer.Option( + False, + "-y", + "--yes", + help="Skip the confirmation before deploying resources.", + ), + print_debug_log: bool = typer.Option( + False, + "-d", + "--debug", + help="Print debug logs.", + ), + force: bool = typer.Option( + False, + "-f", + "--force", + help="Force", + ), +): + """\b + Start resources defined in a resources.py file + \b + Examples: + > `phi ws start` -> Start resources defined in a resources.py file + > `phi ws start workspace.py` -> Start resources defined in a workspace.py file + """ + if print_debug_log: + set_log_level_to_debug() + + from pathlib import Path + from phi.cli.config import PhiCliConfig + from phi.cli.console import log_config_not_available_msg + from phi.cli.operator import patch_resources + from phi.infra.type import InfraType + + phi_config: Optional[PhiCliConfig] = PhiCliConfig.from_saved_config() + if not phi_config: + log_config_not_available_msg() + return + + target_env: Optional[str] = None + target_infra_str: Optional[str] = None + target_infra: Optional[InfraType] = None + target_group: Optional[str] = None + target_name: Optional[str] = None + target_type: Optional[str] = None + + if env_filter is not None and isinstance(env_filter, str): + target_env = env_filter + if infra_filter is not None and isinstance(infra_filter, str): + target_infra_str = infra_filter + if group_filter is not None and isinstance(group_filter, str): + target_group = group_filter + if name_filter is not None and isinstance(name_filter, str): + target_name = name_filter + if type_filter is not None and isinstance(type_filter, str): + target_type = type_filter + + if target_infra_str is not None: + try: + target_infra = InfraType(target_infra_str.lower()) + except KeyError: + logger.error(f"{target_infra_str} is not supported") + return + + resources_file_path: Path = Path(".").resolve().joinpath(resources_file) + patch_resources( + phi_config=phi_config, + resources_file_path=resources_file_path, + target_env=target_env, + target_infra=target_infra, + target_group=target_group, + target_name=target_name, + target_type=target_type, + dry_run=dry_run, + auto_confirm=auto_confirm, + force=force, + ) + + +@phi_cli.command(short_help="Restart resources defined in a resources.py file") +def restart( + resources_file: str = typer.Argument( + "resources.py", + help="Path to workspace file.", + show_default=False, + ), + env_filter: Optional[str] = typer.Option(None, "-e", "--env", metavar="", help="Filter the environment to deploy"), + infra_filter: Optional[str] = typer.Option(None, "-i", "--infra", metavar="", help="Filter the infra to deploy."), + config_filter: Optional[str] = typer.Option(None, "-c", "--config", metavar="", help="Filter the config to deploy"), + group_filter: Optional[str] = typer.Option( + None, "-g", "--group", metavar="", help="Filter resources using group name." + ), + name_filter: Optional[str] = typer.Option(None, "-n", "--name", metavar="", help="Filter using resource name"), + type_filter: Optional[str] = typer.Option( + None, + "-t", + "--type", + metavar="", + help="Filter using resource type", + ), + dry_run: bool = typer.Option( + False, + "-dr", + "--dry-run", + help="Print which resources will be deployed and exit.", + ), + auto_confirm: bool = typer.Option( + False, + "-y", + "--yes", + help="Skip the confirmation before deploying resources.", + ), + print_debug_log: bool = typer.Option( + False, + "-d", + "--debug", + help="Print debug logs.", + ), + force: bool = typer.Option( + False, + "-f", + "--force", + help="Force", + ), +): + """\b + Restart resources defined in a resources.py file + \b + Examples: + > `phi ws restart` -> Start resources defined in a resources.py file + > `phi ws restart workspace.py` -> Start resources defined in a workspace.py file + """ + from time import sleep + from phi.cli.console import print_info + + stop( + resources_file=resources_file, + env_filter=env_filter, + config_filter=config_filter, + name_filter=name_filter, + type_filter=type_filter, + group_filter=group_filter, + dry_run=dry_run, + auto_confirm=auto_confirm, + print_debug_log=print_debug_log, + force=force, + ) + print_info("Sleeping for 2 seconds..") + sleep(2) + start( + resources_file=resources_file, + env_filter=env_filter, + config_filter=config_filter, + name_filter=name_filter, + type_filter=type_filter, + group_filter=group_filter, + dry_run=dry_run, + auto_confirm=auto_confirm, + print_debug_log=print_debug_log, + force=force, + ) phi_cli.add_typer(ws_cli) diff --git a/phi/cli/operator.py b/phi/cli/operator.py index 7406d7a51..d037680f9 100644 --- a/phi/cli/operator.py +++ b/phi/cli/operator.py @@ -273,7 +273,7 @@ def stop_resources( ) # Track number of resource groups deleted - num_rgs_delete = 0 + num_rgs_shutdown = 0 num_rgs_to_shutdown = len(resource_groups_to_shutdown) # Track number of resources created num_resources_shutdown = 0 @@ -283,8 +283,9 @@ def stop_resources( print_info("No resources to delete") return + logger.debug(f"Deleting {num_rgs_to_shutdown} resource groups") for rg in resource_groups_to_shutdown: - num_resources_shutdown, num_resources_to_shutdown = rg.delete_resources( + _num_resources_shutdown, _num_resources_to_shutdown = rg.delete_resources( group_filter=target_group, name_filter=target_name, type_filter=target_type, @@ -292,111 +293,138 @@ def stop_resources( auto_confirm=auto_confirm, force=force, ) - if num_resources_shutdown > 0: - num_rgs_delete += 1 - num_resources_to_shutdown += num_resources_shutdown - num_resources_to_shutdown += num_resources_to_shutdown - logger.debug(f"Deleted {num_resources_to_shutdown} resources in {num_rgs_delete} resource groups") + if _num_resources_shutdown > 0: + num_rgs_shutdown += 1 + num_resources_shutdown += _num_resources_shutdown + num_resources_to_shutdown += _num_resources_to_shutdown + logger.debug(f"Deleted {num_resources_shutdown} resources in {num_rgs_shutdown} resource groups") if dry_run: return - if num_resources_to_shutdown == 0: + if num_resources_shutdown == 0: return - print_heading(f"\n--**-- ResourceGroups deleted: {num_rgs_delete}/{num_rgs_to_shutdown}\n") + print_heading(f"\n--**-- ResourceGroups deleted: {num_rgs_shutdown}/{num_rgs_to_shutdown}\n") if num_resources_shutdown != num_resources_to_shutdown: logger.error("Some resources failed to delete, please check logs") -# def patch_resources( -# resources_file_path: Path, -# target_env: Optional[str] = None, -# target_config: Optional[Any] = None, -# target_name: Optional[str] = None, -# target_type: Optional[str] = None, -# target_group: Optional[str] = None, -# dry_run: Optional[bool] = False, -# auto_confirm: Optional[bool] = False, -# ) -> None: -# print_heading(f"Updating resources in: {resources_file_path}") -# logger.debug(f"\ttarget_env : {target_env}") -# logger.debug(f"\ttarget_config: {target_config}") -# logger.debug(f"\ttarget_name : {target_name}") -# logger.debug(f"\ttarget_type : {target_type}") -# logger.debug(f"\ttarget_group : {target_group}") -# logger.debug(f"\tdry_run : {dry_run}") -# logger.debug(f"\tauto_confirm : {auto_confirm}") -# -# from phidata.aws.config import AwsConfig -# from phidata.docker.config import DockerConfig -# from phidata.infra.config import InfraConfig -# from phidata.k8s.config import K8sConfig -# from phidata.workspace import WorkspaceConfig -# -# from phi.utils.prep_infra_config import filter_and_prep_configs -# -# if not resources_file_path.exists(): -# logger.error(f"File does not exist: {resources_file_path}") -# return -# -# ws_config: WorkspaceConfig = WorkspaceConfig.from_file(resources_file_path) -# # Set the local environment variables before processing configs -# ws_config.set_local_env() -# -# configs_to_patch: List[InfraConfig] = filter_and_prep_configs( -# ws_config=ws_config, -# target_env=target_env, -# target_config=target_config, -# order="create", -# ) -# -# num_configs_to_patch = len(configs_to_patch) -# num_configs_patched = 0 -# for config in configs_to_patch: -# logger.debug(f"Deploying {config.__class__.__name__}") -# if isinstance(config, DockerConfig): -# from phi.docker.docker_operator import patch_docker_config -# -# patch_docker_config( -# config=config, -# name_filter=target_name, -# type_filter=target_type, -# app_filter=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# num_configs_patched += 1 -# if isinstance(config, K8sConfig): -# from phi.k8s.k8s_operator import patch_k8s_config -# -# patch_k8s_config( -# config=config, -# name_filter=target_name, -# type_filter=target_type, -# app_filter=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# num_configs_patched += 1 -# if isinstance(config, AwsConfig): -# from phi.aws.aws_operator import patch_aws_config -# -# patch_aws_config( -# config=config, -# name_filter=target_name, -# type_filter=target_type, -# app_filter=target_group, -# dry_run=dry_run, -# auto_confirm=auto_confirm, -# ) -# num_configs_patched += 1 -# # white space between runs -# print_info("") -# -# print_info(f"# Configs patched: {num_configs_patched}/{num_configs_to_patch}\n") -# if num_configs_to_patch == num_configs_patched: -# if not dry_run: -# print_subheading("Workspace patch success") -# else: -# logger.error("Workspace patch failed") +def patch_resources( + phi_config: PhiCliConfig, + resources_file_path: Path, + target_env: Optional[str] = None, + target_infra: Optional[InfraType] = None, + target_group: Optional[str] = None, + target_name: Optional[str] = None, + target_type: Optional[str] = None, + dry_run: Optional[bool] = False, + auto_confirm: Optional[bool] = False, + force: Optional[bool] = None, +) -> None: + print_heading(f"Updating resources in: {resources_file_path}") + logger.debug(f"\ttarget_env : {target_env}") + logger.debug(f"\ttarget_infra : {target_infra}") + logger.debug(f"\ttarget_name : {target_name}") + logger.debug(f"\ttarget_type : {target_type}") + logger.debug(f"\ttarget_group : {target_group}") + logger.debug(f"\tdry_run : {dry_run}") + logger.debug(f"\tauto_confirm : {auto_confirm}") + logger.debug(f"\tforce : {force}") + + from phi.workspace.config import WorkspaceConfig + + if not resources_file_path.exists(): + logger.error(f"File does not exist: {resources_file_path}") + return + + # Get resource groups to update + resource_groups_to_patch: List[InfraResources] = WorkspaceConfig.get_resources_from_file( + resource_file=resources_file_path, + env=target_env, + infra=target_infra, + order="create", + ) + + num_rgs_patched = 0 + num_rgs_to_patch = len(resource_groups_to_patch) + # Track number of resources updated + num_resources_patched = 0 + num_resources_to_patch = 0 + + if num_rgs_to_patch == 0: + print_info("No resources to patch") + return + + logger.debug(f"Deploying {num_rgs_to_patch} resource groups") + for rg in resource_groups_to_patch: + _num_resources_patched, _num_resources_to_patch = rg.update_resources( + group_filter=target_group, + name_filter=target_name, + type_filter=target_type, + dry_run=dry_run, + auto_confirm=auto_confirm, + force=force, + ) + if _num_resources_patched > 0: + num_rgs_patched += 1 + num_resources_patched += _num_resources_patched + num_resources_to_patch += _num_resources_to_patch + logger.debug(f"Deployed {num_resources_patched} resources in {num_rgs_patched} resource groups") + + if dry_run: + return + + if num_resources_patched == 0: + return + + print_heading(f"\n--**-- ResourceGroups patched: {num_rgs_patched}/{num_rgs_to_patch}\n") + if num_resources_patched != num_resources_to_patch: + logger.error("Some resources failed to patch, please check logs") + + # for rg in resource_groups_to_patch: + # if isinstance(config, DockerConfig): + # from phi.docker.docker_operator import patch_docker_config + + # patch_docker_config( + # config=config, + # name_filter=target_name, + # type_filter=target_type, + # app_filter=target_group, + # dry_run=dry_run, + # auto_confirm=auto_confirm, + # ) + # num_configs_patched += 1 + # if isinstance(config, K8sConfig): + # from phi.k8s.k8s_operator import patch_k8s_config + + # patch_k8s_config( + # config=config, + # name_filter=target_name, + # type_filter=target_type, + # app_filter=target_group, + # dry_run=dry_run, + # auto_confirm=auto_confirm, + # ) + # num_configs_patched += 1 + # if isinstance(config, AwsConfig): + # from phi.aws.aws_operator import patch_aws_config + + # patch_aws_config( + # config=config, + # name_filter=target_name, + # type_filter=target_type, + # app_filter=target_group, + # dry_run=dry_run, + # auto_confirm=auto_confirm, + # ) + # num_configs_patched += 1 + # # white space between runs + # print_info("") + + # print_info(f"# Configs patched: {num_configs_patched}/{num_configs_to_patch}\n") + # if num_configs_to_patch == num_configs_patched: + # if not dry_run: + # print_subheading("Workspace patch success") + # else: + # logger.error("Workspace patch failed") From bd4fc2d74aabaad021da9cc27c3da8336f034197 Mon Sep 17 00:00:00 2001 From: Yash Pratap Date: Mon, 2 Oct 2023 11:36:19 -0400 Subject: [PATCH 3/4] update --- phi/cli/entrypoint.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/phi/cli/entrypoint.py b/phi/cli/entrypoint.py index fa14dc333..65fd60cfd 100644 --- a/phi/cli/entrypoint.py +++ b/phi/cli/entrypoint.py @@ -659,10 +659,11 @@ def restart( stop( resources_file=resources_file, env_filter=env_filter, + infra_filter=infra_filter, config_filter=config_filter, + group_filter=group_filter, name_filter=name_filter, type_filter=type_filter, - group_filter=group_filter, dry_run=dry_run, auto_confirm=auto_confirm, print_debug_log=print_debug_log, @@ -673,10 +674,11 @@ def restart( start( resources_file=resources_file, env_filter=env_filter, + infra_filter=infra_filter, config_filter=config_filter, + group_filter=group_filter, name_filter=name_filter, type_filter=type_filter, - group_filter=group_filter, dry_run=dry_run, auto_confirm=auto_confirm, print_debug_log=print_debug_log, From 65fc6979132f6a72821405949d342fd531933162 Mon Sep 17 00:00:00 2001 From: Yash Pratap Date: Mon, 2 Oct 2023 15:22:36 -0400 Subject: [PATCH 4/4] update --- phi/cli/operator.py | 47 --------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/phi/cli/operator.py b/phi/cli/operator.py index d037680f9..878915874 100644 --- a/phi/cli/operator.py +++ b/phi/cli/operator.py @@ -381,50 +381,3 @@ def patch_resources( print_heading(f"\n--**-- ResourceGroups patched: {num_rgs_patched}/{num_rgs_to_patch}\n") if num_resources_patched != num_resources_to_patch: logger.error("Some resources failed to patch, please check logs") - - # for rg in resource_groups_to_patch: - # if isinstance(config, DockerConfig): - # from phi.docker.docker_operator import patch_docker_config - - # patch_docker_config( - # config=config, - # name_filter=target_name, - # type_filter=target_type, - # app_filter=target_group, - # dry_run=dry_run, - # auto_confirm=auto_confirm, - # ) - # num_configs_patched += 1 - # if isinstance(config, K8sConfig): - # from phi.k8s.k8s_operator import patch_k8s_config - - # patch_k8s_config( - # config=config, - # name_filter=target_name, - # type_filter=target_type, - # app_filter=target_group, - # dry_run=dry_run, - # auto_confirm=auto_confirm, - # ) - # num_configs_patched += 1 - # if isinstance(config, AwsConfig): - # from phi.aws.aws_operator import patch_aws_config - - # patch_aws_config( - # config=config, - # name_filter=target_name, - # type_filter=target_type, - # app_filter=target_group, - # dry_run=dry_run, - # auto_confirm=auto_confirm, - # ) - # num_configs_patched += 1 - # # white space between runs - # print_info("") - - # print_info(f"# Configs patched: {num_configs_patched}/{num_configs_to_patch}\n") - # if num_configs_to_patch == num_configs_patched: - # if not dry_run: - # print_subheading("Workspace patch success") - # else: - # logger.error("Workspace patch failed")