Skip to content

Commit

Permalink
v2.3.52
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Mar 12, 2024
1 parent 6ee3d85 commit aefc309
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 135 deletions.
6 changes: 3 additions & 3 deletions phi/assistant/openai/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ def run(
return self
if self.required_action is not None:
if self.required_action.type == "submit_tool_outputs":
tool_calls: List[
RequiredActionFunctionToolCall
] = self.required_action.submit_tool_outputs.tool_calls
tool_calls: List[RequiredActionFunctionToolCall] = (
self.required_action.submit_tool_outputs.tool_calls
)

tool_outputs = []
for tool_call in tool_calls:
Expand Down
6 changes: 3 additions & 3 deletions phi/aws/resource/ecs/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def get_volume_definition(self) -> Dict[str, Any]:
if self.efs_volume_configuration is not None:
volume_definition["efsVolumeConfiguration"] = self.efs_volume_configuration
if self.fsx_windows_file_server_volume_configuration is not None:
volume_definition[
"fsxWindowsFileServerVolumeConfiguration"
] = self.fsx_windows_file_server_volume_configuration
volume_definition["fsxWindowsFileServerVolumeConfiguration"] = (
self.fsx_windows_file_server_volume_configuration
)

return volume_definition

Expand Down
20 changes: 12 additions & 8 deletions phi/aws/resource/elasticache/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,56 +401,60 @@ def post_delete(self, aws_client: AwsApiClient) -> bool:
logger.error(e)
return True

def get_cache_endpoint(self, aws_client: Optional[AwsApiClient] = None) -> Optional[List]:
def get_cache_endpoint(self, aws_client: Optional[AwsApiClient] = None) -> Optional[str]:
"""Returns the CacheCluster endpoint
Args:
aws_client: The AwsApiClient for the current cluster
"""
cache_endpoint = []
cache_endpoint = None
try:
client: AwsApiClient = aws_client or self.get_aws_client()
cache_cluster_id = self.get_cache_cluster_id()
describe_response = self.get_service_client(client).describe_cache_clusters(
CacheClusterId=cache_cluster_id, ShowCacheNodeInfo=True
)
logger.debug(f"CacheCluster: {describe_response}")
# logger.debug(f"CacheCluster: {describe_response}")
resource_list = describe_response.get("CacheClusters", None)

if resource_list is not None and isinstance(resource_list, list):
for resource in resource_list:
_cluster_identifier = resource.get("CacheClusterId", None)
if _cluster_identifier == cache_cluster_id:
for node in resource.get("CacheNodes", []):
cache_endpoint.append(node.get("Endpoint", {}).get("Address", None))
cache_endpoint = node.get("Endpoint", {}).get("Address", None)
if cache_endpoint is not None and isinstance(cache_endpoint, str):
return cache_endpoint
break
except Exception as e:
logger.error(f"Error reading {self.get_resource_type()}.")
logger.error(e)
return cache_endpoint

def get_cache_port(self, aws_client: Optional[AwsApiClient] = None) -> Optional[List]:
def get_cache_port(self, aws_client: Optional[AwsApiClient] = None) -> Optional[int]:
"""Returns the CacheCluster port
Args:
aws_client: The AwsApiClient for the current cluster
"""
cache_port = []
cache_port = None
try:
client: AwsApiClient = aws_client or self.get_aws_client()
cache_cluster_id = self.get_cache_cluster_id()
describe_response = self.get_service_client(client).describe_cache_clusters(
CacheClusterId=cache_cluster_id, ShowCacheNodeInfo=True
)
logger.debug(f"CacheCluster: {describe_response}")
# logger.debug(f"CacheCluster: {describe_response}")
resource_list = describe_response.get("CacheClusters", None)

if resource_list is not None and isinstance(resource_list, list):
for resource in resource_list:
_cluster_identifier = resource.get("CacheClusterId", None)
if _cluster_identifier == cache_cluster_id:
for node in resource.get("CacheNodes", []):
cache_port.append(node.get("Endpoint", {}).get("Port", None))
cache_port = node.get("Endpoint", {}).get("Port", None)
if cache_port is not None and isinstance(cache_port, int):
return cache_port
break
except Exception as e:
logger.error(f"Error reading {self.get_resource_type()}.")
Expand Down
2 changes: 1 addition & 1 deletion phi/aws/resource/rds/db_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def _read(self, aws_client: AwsApiClient) -> Optional[Any]:
try:
resource_identifier = self.get_db_instance_identifier()
describe_response = service_client.describe_db_instances(DBInstanceIdentifier=resource_identifier)
logger.debug(f"DbInstance: {describe_response}")
# logger.debug(f"DbInstance: {describe_response}")
resources_list = describe_response.get("DBInstances", None)

if resources_list is not None and isinstance(resources_list, list):
Expand Down
101 changes: 46 additions & 55 deletions phi/aws/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ class AwsResources(InfraResources):
apps: Optional[List[Union[AwsApp, AppGroup]]] = None
resources: Optional[List[Union[AwsResource, ResourceGroup]]] = None

aws_region: Optional[str] = None
aws_profile: Optional[str] = None

# -*- Cached Data
_aws_region: Optional[str] = None
_aws_profile: Optional[str] = None
_api_client: Optional[AwsApiClient] = None

@property
def aws_region(self) -> Optional[str]:
def get_aws_region(self) -> Optional[str]:
# Priority 1: Use aws_region from ResourceGroup (or cached value)
if self._aws_region:
return self._aws_region
if self.aws_region:
return self.aws_region

# Priority 2: Get aws_region from workspace settings
if self.workspace_settings is not None and self.workspace_settings.aws_region is not None:
self._aws_region = self.workspace_settings.aws_region
return self._aws_region
self.aws_region = self.workspace_settings.aws_region
return self.aws_region

# Priority 3: Get aws_region from env
from os import getenv
Expand All @@ -37,23 +37,18 @@ def aws_region(self) -> Optional[str]:
aws_region_env = getenv(AWS_REGION_ENV_VAR)
if aws_region_env is not None:
logger.debug(f"{AWS_REGION_ENV_VAR}: {aws_region_env}")
self._aws_region = aws_region_env
return self._aws_region

@aws_region.setter
def aws_region(self, value: str) -> None:
self._aws_region = value
self.aws_region = aws_region_env
return self.aws_region

@property
def aws_profile(self) -> Optional[str]:
def get_aws_profile(self) -> Optional[str]:
# Priority 1: Use aws_region from ResourceGroup (or cached value)
if self._aws_profile:
return self._aws_profile
if self.aws_profile:
return self.aws_profile

# Priority 2: Get aws_profile from workspace settings
if self.workspace_settings is not None and self.workspace_settings.aws_profile is not None:
self._aws_profile = self.workspace_settings.aws_profile
return self._aws_profile
self.aws_profile = self.workspace_settings.aws_profile
return self.aws_profile

# Priority 3: Get aws_profile from env
from os import getenv
Expand All @@ -62,17 +57,13 @@ def aws_profile(self) -> Optional[str]:
aws_profile_env = getenv(AWS_PROFILE_ENV_VAR)
if aws_profile_env is not None:
logger.debug(f"{AWS_PROFILE_ENV_VAR}: {aws_profile_env}")
self._aws_profile = aws_profile_env
return self._aws_profile

@aws_profile.setter
def aws_profile(self, value: str) -> None:
self._aws_profile = value
self.aws_profile = aws_profile_env
return self.aws_profile

@property
def aws_client(self) -> AwsApiClient:
if self._api_client is None:
self._api_client = AwsApiClient(aws_region=self.aws_region, aws_profile=self.aws_profile)
self._api_client = AwsApiClient(aws_region=self.get_aws_region(), aws_profile=self.get_aws_profile())
return self._api_client

def create_resources(
Expand Down Expand Up @@ -146,7 +137,7 @@ def create_resources(
for app in apps_to_create:
app.set_workspace_settings(workspace_settings=self.workspace_settings)
app_resources = app.get_resources(
build_context=AwsBuildContext(aws_region=self.aws_region, aws_profile=self.aws_profile)
build_context=AwsBuildContext(aws_region=self.get_aws_region(), aws_profile=self.get_aws_profile())
)
if len(app_resources) > 0:
# If the app has dependencies, add the resources from the
Expand All @@ -157,7 +148,7 @@ def create_resources(
dep.set_workspace_settings(workspace_settings=self.workspace_settings)
dep_resources = dep.get_resources(
build_context=AwsBuildContext(
aws_region=self.aws_region, aws_profile=self.aws_profile
aws_region=self.get_aws_region(), aws_profile=self.get_aws_profile()
)
)
if len(dep_resources) > 0:
Expand Down Expand Up @@ -214,10 +205,10 @@ def create_resources(
for resource in final_aws_resources:
print_info(f" -+-> {resource.get_resource_type()}: {resource.get_resource_name()}")
print_info("")
if self.aws_region:
print_info(f"Region: {self.aws_region}")
if self.aws_profile:
print_info(f"Profile: {self.aws_profile}")
if self.get_aws_region():
print_info(f"Region: {self.get_aws_region()}")
if self.get_aws_profile():
print_info(f"Profile: {self.get_aws_profile()}")
print_info(f"Total {num_resources_to_create} resources")
return 0, 0

Expand All @@ -227,10 +218,10 @@ def create_resources(
for resource in final_aws_resources:
print_info(f" -+-> {resource.get_resource_type()}: {resource.get_resource_name()}")
print_info("")
if self.aws_region:
print_info(f"Region: {self.aws_region}")
if self.aws_profile:
print_info(f"Profile: {self.aws_profile}")
if self.get_aws_region():
print_info(f"Region: {self.get_aws_region()}")
if self.get_aws_profile():
print_info(f"Profile: {self.get_aws_profile()}")
print_info(f"Total {num_resources_to_create} resources")
confirm = confirm_yes_no("\nConfirm deploy")
if not confirm:
Expand Down Expand Up @@ -334,7 +325,7 @@ def delete_resources(
for app in apps_to_delete:
app.set_workspace_settings(workspace_settings=self.workspace_settings)
app_resources = app.get_resources(
build_context=AwsBuildContext(aws_region=self.aws_region, aws_profile=self.aws_profile)
build_context=AwsBuildContext(aws_region=self.get_aws_region(), aws_profile=self.get_aws_profile())
)
if len(app_resources) > 0:
for app_resource in app_resources:
Expand Down Expand Up @@ -395,10 +386,10 @@ def delete_resources(
for resource in final_aws_resources:
print_info(f" -+-> {resource.get_resource_type()}: {resource.get_resource_name()}")
print_info("")
if self.aws_region:
print_info(f"Region: {self.aws_region}")
if self.aws_profile:
print_info(f"Profile: {self.aws_profile}")
if self.get_aws_region():
print_info(f"Region: {self.get_aws_region()}")
if self.get_aws_profile():
print_info(f"Profile: {self.get_aws_profile()}")
print_info(f"Total {num_resources_to_delete} resources")
return 0, 0

Expand All @@ -408,10 +399,10 @@ def delete_resources(
for resource in final_aws_resources:
print_info(f" -+-> {resource.get_resource_type()}: {resource.get_resource_name()}")
print_info("")
if self.aws_region:
print_info(f"Region: {self.aws_region}")
if self.aws_profile:
print_info(f"Profile: {self.aws_profile}")
if self.get_aws_region():
print_info(f"Region: {self.get_aws_region()}")
if self.get_aws_profile():
print_info(f"Profile: {self.get_aws_profile()}")
print_info(f"Total {num_resources_to_delete} resources")
confirm = confirm_yes_no("\nConfirm delete")
if not confirm:
Expand Down Expand Up @@ -516,7 +507,7 @@ def update_resources(
for app in apps_to_update:
app.set_workspace_settings(workspace_settings=self.workspace_settings)
app_resources = app.get_resources(
build_context=AwsBuildContext(aws_region=self.aws_region, aws_profile=self.aws_profile)
build_context=AwsBuildContext(aws_region=self.get_aws_region(), aws_profile=self.get_aws_profile())
)
if len(app_resources) > 0:
for app_resource in app_resources:
Expand Down Expand Up @@ -568,10 +559,10 @@ def update_resources(
for resource in final_aws_resources:
print_info(f" -+-> {resource.get_resource_type()}: {resource.get_resource_name()}")
print_info("")
if self.aws_region:
print_info(f"Region: {self.aws_region}")
if self.aws_profile:
print_info(f"Profile: {self.aws_profile}")
if self.get_aws_region():
print_info(f"Region: {self.get_aws_region()}")
if self.get_aws_profile():
print_info(f"Profile: {self.get_aws_profile()}")
print_info(f"Total {num_resources_to_update} resources")
return 0, 0

Expand All @@ -581,10 +572,10 @@ def update_resources(
for resource in final_aws_resources:
print_info(f" -+-> {resource.get_resource_type()}: {resource.get_resource_name()}")
print_info("")
if self.aws_region:
print_info(f"Region: {self.aws_region}")
if self.aws_profile:
print_info(f"Profile: {self.aws_profile}")
if self.get_aws_region():
print_info(f"Region: {self.get_aws_region()}")
if self.get_aws_profile():
print_info(f"Profile: {self.get_aws_profile()}")
print_info(f"Total {num_resources_to_update} resources")
confirm = confirm_yes_no("\nConfirm patch")
if not confirm:
Expand Down
18 changes: 7 additions & 11 deletions phi/k8s/api_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import Optional, TYPE_CHECKING
from typing import Optional

if TYPE_CHECKING:
try:
import kubernetes
except ImportError:
raise ImportError(
"The `kubernetes` package is not installed. "
"Install using `pip install kubernetes` or `pip install phidata[k8s]`."
)

from phi.utils.log import logger

Expand All @@ -10,15 +15,6 @@ class K8sApiClient:
def __init__(self, context: Optional[str] = None, kubeconfig_path: Optional[str] = None):
super().__init__()

try:
import kubernetes
except ImportError:
logger.error(
"The `kubernetes` package is not installed. "
"Install using `pip install kubernetes` or `pip install phidata[k8s]`."
)
exit(0)

self.context: Optional[str] = context
self.kubeconfig_path: Optional[str] = kubeconfig_path
self.configuration: Optional[kubernetes.client.Configuration] = None
Expand Down
20 changes: 14 additions & 6 deletions phi/k8s/app/airflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,30 @@ class AirflowBase(K8sApp):
load_examples: bool = False

def get_db_user(self) -> Optional[str]:
return self.db_user or self.get_secret_from_file("DB_USER")
return self.db_user or self.get_secret_from_file("DATABASE_USER") or self.get_secret_from_file("DB_USER")

def get_db_password(self) -> Optional[str]:
return self.db_password or self.get_secret_from_file("DB_PASSWORD")
return (
self.db_password
or self.get_secret_from_file("DATABASE_PASSWORD")
or self.get_secret_from_file("DB_PASSWORD")
)

def get_db_database(self) -> Optional[str]:
return self.db_database or self.get_secret_from_file("DB_DATABASE")
return self.db_database or self.get_secret_from_file("DATABASE_DB") or self.get_secret_from_file("DB_DATABASE")

def get_db_driver(self) -> Optional[str]:
return self.db_driver or self.get_secret_from_file("DB_DRIVER")
return self.db_driver or self.get_secret_from_file("DATABASE_DRIVER") or self.get_secret_from_file("DB_DRIVER")

def get_db_host(self) -> Optional[str]:
return self.db_host or self.get_secret_from_file("DB_HOST")
return self.db_host or self.get_secret_from_file("DATABASE_HOST") or self.get_secret_from_file("DB_HOST")

def get_db_port(self) -> Optional[int]:
return self.db_port or str_to_int(self.get_secret_from_file("DB_PORT"))
return (
self.db_port
or str_to_int(self.get_secret_from_file("DATABASE_PORT"))
or str_to_int(self.get_secret_from_file("DB_PORT"))
)

def get_redis_password(self) -> Optional[str]:
return self.redis_password or self.get_secret_from_file("REDIS_PASSWORD")
Expand Down
Loading

0 comments on commit aefc309

Please sign in to comment.