From 4dd46b65221b32aa4f95d37ed1875221985ce8e4 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 09:54:38 -0400 Subject: [PATCH 01/56] Notes: session state for the backend removal --- notes/2026-08-19-backend-removal-session.md | 81 +++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 notes/2026-08-19-backend-removal-session.md diff --git a/notes/2026-08-19-backend-removal-session.md b/notes/2026-08-19-backend-removal-session.md new file mode 100644 index 00000000..12438d74 --- /dev/null +++ b/notes/2026-08-19-backend-removal-session.md @@ -0,0 +1,81 @@ +# Session notes — 2026-08-19 + +## Completed + +### PR #139 merged to master as `0e3490e` +All 15 CI checks green, including both Windows jobs. + +The Windows blocker (35 failures) had one genuine bug underneath it: +`os.fchmod` does not exist on Windows before Python 3.13. In +`_write_config_file_securely` it raised **after** `os.open` returned a +descriptor and **before** `os.fdopen` took ownership, so every call leaked the +fd. POSIX hides that; Windows will not delete a file with an open handle, +which is where the four `PermissionError [WinError 32]` teardown errors came +from. It also meant `save_to_file`/`save_config` never worked on Windows at +all. Fixed in `957ec24` (guarded with `hasattr`, fd closed on failure). + +The last remaining Windows failure was `assert 0.0 > 0` in +`test_complete_data_processing_workflow`: `time.time()` on Windows ticks in +~15.6 ms steps, so sub-millisecond numpy work measures as exactly 0.0. Fixed +with `perf_counter` in `f442d9b`. + +### Issues #140–#146 filed +One per removed backend, each citing the introducing commit and the file/line +inventory captured at `299109f`: + +| Backend | Issue | Introduced | +|-|-|-| +| PBS | #140 | `4b2aba5` | +| SGE | #141 | `4b2aba5` | +| Kubernetes | #142 | `6061247` exec, `d6b4b7b` provisioners | +| AWS | #143 | `31d3b38` | +| GCP | #144 | `2e0aa3d` | +| Azure | #145 | `f7fa047` | +| Lambda Cloud | #146 | `6430b7d` | + +## In flight — branch `remove/unverified-backends` + +Two agents working disjoint file sets in one tree: +- code lane: `clustrix/`, `tests/`, `scripts/` +- docs lane: `docs/`, `README.md`, `CHANGELOG.md`, `CLAUDE.md` + +A third agent is verifying the Colab tutorials against `master` with Playwright. + +### Decisions made, so they do not get relitigated + +**Retained set is exactly `local, ssh, slurm, huggingface`** — the four +demonstrated end to end. `SUPPORTED_CLUSTER_TYPES` already excluded +aws/gcp/azure/lambda_cloud/huggingface_spaces, so those were undispatchable +already; this removes the modules too. + +**`cost_monitoring.py` goes, and five public functions with it** +(`cost_tracking_decorator`, `get_cost_monitor`, `start_cost_monitoring`, +`generate_cost_report`, `get_pricing_info`). Its `get_cost_monitor` +dispatches to exactly lambda/aws/azure/gcp; once those go it is a public API +that can only return `None`. This is a public API break and must be called +out in the release notes. + +**Removed config keys need a real error, not a difflib guess.** +`load_config` (`clustrix/config.py:498`) rejects unknown keys with a +"did you mean X?" hint. A user with `k8s_namespace` in an existing +`clustrix.yml` would be pointed at some unrelated field. A removed-key table +now runs before the difflib path and names the backend and its tracking +issue. Same for `cluster_type: pbs`. + +**`huggingface_spaces` is not `huggingface`.** Spaces is unverified and goes; +Jobs is verified and stays. Easy to conflate, expensive to get wrong. + +**`scripts/aws/` stays** — operator cleanup tooling, not a backend, dry-run by +default, refuses anything not tagged `clustrix:managed=true`. + +## Still open + +- Integrated verification after both lanes land: full suite, black/flake8/mypy, + `scripts/check_docs_examples.py`, `cd docs && make html` at zero warnings. + The docs lane cannot self-verify because both checks import `clustrix` while + the code lane is mid-removal. +- Version is already `0.2.0` in all four locations — the requested "bump to + 0.2" was already done. +- Colab: executing cells needs a Google sign-in, which is not something to do. + Expect the verification to distinguish *loaded in Colab* from *executed + locally* and to be explicit about which claim each result supports. From 07db2e699deba45095acd7e11e35779f6e3a4741 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:01:36 -0400 Subject: [PATCH 02/56] WIP: backend removal, incomplete -- DO NOT MERGE Checkpoint of two parallel removal agents that were stopped mid-flight so the machine could be suspended. The tree in this commit DOES NOT IMPORT: ModuleNotFoundError: No module named 'clustrix.executor_kubernetes' clustrix/executor.py still imports KubernetesJobManager, and utils.py still has the PBS/SGE script generators, because the code agent was stopped just as it reached utils.py. Committed only so the work survives the suspend. Done so far: 95 files deleted (cloud_providers/, cost_providers/, pricing_clients/, kubernetes/, executor_cloud.py, executor_kubernetes.py, cloud_provider_manager.py, cost_monitoring.py, auto_install.py, 9 notebooks, kubernetes/pbs tutorials, cost_monitoring API page); 7 files partially edited. --no-verify is deliberate: the pre-commit hook runs black/flake8/mypy, and a tree that cannot import cannot pass them. The next commit on this branch must pass the full gate. Resume instructions: notes/2026-08-19-backend-removal-session.md Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/auto_install.py | 204 -- clustrix/cloud_provider_manager.py | 404 ---- clustrix/cloud_providers/__init__.py | 37 - clustrix/cloud_providers/aws.py | 893 -------- clustrix/cloud_providers/azure.py | 876 -------- clustrix/cloud_providers/base.py | 171 -- clustrix/cloud_providers/gcp.py | 759 ------- .../cloud_providers/huggingface_spaces.py | 402 ---- clustrix/cloud_providers/lambda_cloud.py | 495 ----- clustrix/cost_monitoring.py | 407 ---- clustrix/cost_providers/__init__.py | 3 - clustrix/cost_providers/aws.py | 369 ---- clustrix/cost_providers/azure.py | 408 ---- clustrix/cost_providers/gcp.py | 435 ---- clustrix/cost_providers/lambda_cloud.py | 322 --- clustrix/executor_cloud.py | 591 ----- clustrix/executor_connections.py | 256 +-- clustrix/executor_core.py | 176 +- clustrix/executor_kubernetes.py | 594 ----- clustrix/executor_scheduler_status.py | 102 +- clustrix/executor_schedulers.py | 91 +- clustrix/kubernetes/__init__.py | 60 - clustrix/kubernetes/aws_provisioner.py | 872 -------- clustrix/kubernetes/azure_provisioner.py | 726 ------- clustrix/kubernetes/cluster_provisioner.py | 423 ---- clustrix/kubernetes/gcp_provisioner.py | 827 ------- .../kubernetes/huggingface_provisioner.py | 500 ----- clustrix/kubernetes/lambda_provisioner.py | 686 ------ clustrix/kubernetes/local_provisioner.py | 455 ---- clustrix/pricing_clients/__init__.py | 16 - clustrix/pricing_clients/aws_pricing.py | 273 --- clustrix/pricing_clients/azure_pricing.py | 381 ---- clustrix/pricing_clients/base.py | 153 -- clustrix/pricing_clients/gcp_pricing.py | 383 ---- clustrix/pricing_clients/lambda_pricing.py | 330 --- docs/source/api/cost_monitoring.rst | 454 ---- docs/source/configuration.rst | 74 +- docs/source/index.rst | 104 +- docs/source/limitations.rst | 115 +- .../source/notebooks/aws_cloud_tutorial.ipynb | 1196 ----------- .../notebooks/azure_cloud_tutorial.ipynb | 1621 -------------- .../notebooks/cost_monitoring_tutorial.ipynb | 990 --------- .../source/notebooks/gcp_cloud_tutorial.ipynb | 1842 ---------------- .../huggingface_spaces_tutorial.ipynb | 185 -- .../notebooks/kubernetes_tutorial.ipynb | 254 --- .../notebooks/lambda_cloud_tutorial.ipynb | 1910 ----------------- docs/source/notebooks/pbs_tutorial.ipynb | 1379 ------------ docs/source/notebooks/sge_tutorial.ipynb | 1202 ----------- docs/source/tutorials/kubernetes_tutorial.rst | 894 -------- docs/source/tutorials/pbs_tutorial.rst | 632 ------ tests/integration/test_aws_eks_auto.py | 148 -- tests/integration/test_aws_eks_debug.py | 91 - tests/integration/test_aws_eks_minimal.py | 108 - .../test_aws_eks_provision_step.py | 125 -- tests/integration/test_aws_eks_real.py | 135 -- .../test_aws_eks_real_provision.py | 114 - tests/integration/test_aws_preflight.py | 225 -- .../test_aws_provision_detailed.py | 144 -- .../test_aws_provision_optimized.py | 213 -- tests/integration/test_aws_quick_provision.py | 279 --- tests/integration/test_eks_permissions.py | 120 -- .../api_validation/validate_aws_pricing.py | 132 -- .../api_validation/validate_gcp_pricing.py | 149 -- .../validate_huggingface_pricing.py | 225 -- .../validate_lambda_cloud_pricing.py | 152 -- tests/real_world/test_aws_execution_real.py | 473 ---- tests/real_world/test_aws_pricing_real.py | 382 ---- tests/real_world/test_azure_pricing_real.py | 501 ----- tests/real_world/test_cloud_apis_real.py | 597 ------ .../test_cloud_integration_complete.py | 544 ----- .../test_cross_provider_accuracy.py | 577 ----- tests/real_world/test_end_to_end_billing.py | 669 ------ tests/real_world/test_gcp_pricing_real.py | 500 ----- .../test_kubernetes_aws_provisioning.py | 479 ----- .../test_kubernetes_azure_provisioning.py | 496 ----- .../test_kubernetes_gcp_provisioning.py | 500 ----- ...test_kubernetes_huggingface_integration.py | 467 ---- .../test_kubernetes_lambda_integration.py | 591 ----- .../test_kubernetes_local_execution.py | 497 ----- ...t_kubernetes_multi_provider_integration.py | 811 ------- .../test_kubernetes_performance_benchmarks.py | 1063 --------- tests/real_world/test_lambda_pricing_real.py | 325 --- tests/test_auto_install.py | 422 ---- tests/test_aws_cost_provider_pricing.py | 158 -- tests/test_aws_pricing_integration.py | 202 -- tests/test_azure_cost_provider.py | 411 ---- tests/test_azure_pricing_integration.py | 288 --- tests/test_cloud_providers.py | 248 --- tests/test_cloud_providers_aws.py | 375 ---- .../test_cloud_providers_aws_comprehensive.py | 638 ------ tests/test_cloud_providers_azure.py | 1075 ---------- tests/test_cloud_providers_gcp.py | 989 --------- tests/test_cloud_providers_gcp_real.py | 580 ----- ...test_cloud_providers_huggingface_spaces.py | 789 ------- tests/test_cloud_providers_lambda_cloud.py | 808 ------- tests/test_cost_monitoring.py | 494 ----- tests/test_gcp_cost_provider.py | 333 --- tests/test_gcp_pricing_simple.py | 292 --- tests/test_kubernetes_integration.py | 649 ------ tests/test_pricing_clients.py | 270 --- tests/unit/test_backends_cloud_contract.py | 72 - tests/unit/test_backends_kubernetes.py | 194 -- 102 files changed, 144 insertions(+), 47907 deletions(-) delete mode 100644 clustrix/auto_install.py delete mode 100644 clustrix/cloud_provider_manager.py delete mode 100644 clustrix/cloud_providers/__init__.py delete mode 100644 clustrix/cloud_providers/aws.py delete mode 100644 clustrix/cloud_providers/azure.py delete mode 100644 clustrix/cloud_providers/base.py delete mode 100644 clustrix/cloud_providers/gcp.py delete mode 100644 clustrix/cloud_providers/huggingface_spaces.py delete mode 100644 clustrix/cloud_providers/lambda_cloud.py delete mode 100644 clustrix/cost_monitoring.py delete mode 100644 clustrix/cost_providers/__init__.py delete mode 100644 clustrix/cost_providers/aws.py delete mode 100644 clustrix/cost_providers/azure.py delete mode 100644 clustrix/cost_providers/gcp.py delete mode 100644 clustrix/cost_providers/lambda_cloud.py delete mode 100644 clustrix/executor_cloud.py delete mode 100644 clustrix/executor_kubernetes.py delete mode 100644 clustrix/kubernetes/__init__.py delete mode 100644 clustrix/kubernetes/aws_provisioner.py delete mode 100644 clustrix/kubernetes/azure_provisioner.py delete mode 100644 clustrix/kubernetes/cluster_provisioner.py delete mode 100644 clustrix/kubernetes/gcp_provisioner.py delete mode 100644 clustrix/kubernetes/huggingface_provisioner.py delete mode 100644 clustrix/kubernetes/lambda_provisioner.py delete mode 100644 clustrix/kubernetes/local_provisioner.py delete mode 100644 clustrix/pricing_clients/__init__.py delete mode 100644 clustrix/pricing_clients/aws_pricing.py delete mode 100644 clustrix/pricing_clients/azure_pricing.py delete mode 100644 clustrix/pricing_clients/base.py delete mode 100644 clustrix/pricing_clients/gcp_pricing.py delete mode 100644 clustrix/pricing_clients/lambda_pricing.py delete mode 100644 docs/source/api/cost_monitoring.rst delete mode 100644 docs/source/notebooks/aws_cloud_tutorial.ipynb delete mode 100644 docs/source/notebooks/azure_cloud_tutorial.ipynb delete mode 100644 docs/source/notebooks/cost_monitoring_tutorial.ipynb delete mode 100644 docs/source/notebooks/gcp_cloud_tutorial.ipynb delete mode 100644 docs/source/notebooks/huggingface_spaces_tutorial.ipynb delete mode 100644 docs/source/notebooks/kubernetes_tutorial.ipynb delete mode 100644 docs/source/notebooks/lambda_cloud_tutorial.ipynb delete mode 100644 docs/source/notebooks/pbs_tutorial.ipynb delete mode 100644 docs/source/notebooks/sge_tutorial.ipynb delete mode 100644 docs/source/tutorials/kubernetes_tutorial.rst delete mode 100644 docs/source/tutorials/pbs_tutorial.rst delete mode 100644 tests/integration/test_aws_eks_auto.py delete mode 100644 tests/integration/test_aws_eks_debug.py delete mode 100644 tests/integration/test_aws_eks_minimal.py delete mode 100644 tests/integration/test_aws_eks_provision_step.py delete mode 100644 tests/integration/test_aws_eks_real.py delete mode 100644 tests/integration/test_aws_eks_real_provision.py delete mode 100644 tests/integration/test_aws_preflight.py delete mode 100644 tests/integration/test_aws_provision_detailed.py delete mode 100644 tests/integration/test_aws_provision_optimized.py delete mode 100644 tests/integration/test_aws_quick_provision.py delete mode 100644 tests/integration/test_eks_permissions.py delete mode 100644 tests/real_world/api_validation/validate_aws_pricing.py delete mode 100644 tests/real_world/api_validation/validate_gcp_pricing.py delete mode 100755 tests/real_world/api_validation/validate_huggingface_pricing.py delete mode 100755 tests/real_world/api_validation/validate_lambda_cloud_pricing.py delete mode 100644 tests/real_world/test_aws_execution_real.py delete mode 100644 tests/real_world/test_aws_pricing_real.py delete mode 100644 tests/real_world/test_azure_pricing_real.py delete mode 100644 tests/real_world/test_cloud_apis_real.py delete mode 100644 tests/real_world/test_cloud_integration_complete.py delete mode 100644 tests/real_world/test_cross_provider_accuracy.py delete mode 100644 tests/real_world/test_end_to_end_billing.py delete mode 100644 tests/real_world/test_gcp_pricing_real.py delete mode 100644 tests/real_world/test_kubernetes_aws_provisioning.py delete mode 100644 tests/real_world/test_kubernetes_azure_provisioning.py delete mode 100644 tests/real_world/test_kubernetes_gcp_provisioning.py delete mode 100644 tests/real_world/test_kubernetes_huggingface_integration.py delete mode 100644 tests/real_world/test_kubernetes_lambda_integration.py delete mode 100644 tests/real_world/test_kubernetes_local_execution.py delete mode 100644 tests/real_world/test_kubernetes_multi_provider_integration.py delete mode 100644 tests/real_world/test_kubernetes_performance_benchmarks.py delete mode 100644 tests/real_world/test_lambda_pricing_real.py delete mode 100644 tests/test_auto_install.py delete mode 100644 tests/test_aws_cost_provider_pricing.py delete mode 100644 tests/test_aws_pricing_integration.py delete mode 100644 tests/test_azure_cost_provider.py delete mode 100644 tests/test_azure_pricing_integration.py delete mode 100644 tests/test_cloud_providers.py delete mode 100644 tests/test_cloud_providers_aws.py delete mode 100644 tests/test_cloud_providers_aws_comprehensive.py delete mode 100644 tests/test_cloud_providers_azure.py delete mode 100644 tests/test_cloud_providers_gcp.py delete mode 100644 tests/test_cloud_providers_gcp_real.py delete mode 100644 tests/test_cloud_providers_huggingface_spaces.py delete mode 100644 tests/test_cloud_providers_lambda_cloud.py delete mode 100644 tests/test_cost_monitoring.py delete mode 100644 tests/test_gcp_cost_provider.py delete mode 100644 tests/test_gcp_pricing_simple.py delete mode 100644 tests/test_kubernetes_integration.py delete mode 100644 tests/test_pricing_clients.py delete mode 100644 tests/unit/test_backends_cloud_contract.py delete mode 100644 tests/unit/test_backends_kubernetes.py diff --git a/clustrix/auto_install.py b/clustrix/auto_install.py deleted file mode 100644 index 877527ad..00000000 --- a/clustrix/auto_install.py +++ /dev/null @@ -1,204 +0,0 @@ -"""Automatic dependency installation for cloud providers.""" - -import subprocess -import sys -import logging -from typing import List, Dict, Optional - -logger = logging.getLogger(__name__) - -# Define cloud provider dependency mappings -CLOUD_PROVIDER_DEPS: Dict[str, List[str]] = { - "aws": [ - "boto3>=1.26.0", - "kubernetes>=20.13.0", - ], - "azure": [ - "azure-identity>=1.12.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-resource>=23.0.0", - "azure-mgmt-network>=25.0.0", - "kubernetes>=20.13.0", - ], - "gcp": [ - "google-cloud-compute>=1.11.0", - "google-cloud-container>=2.15.0", - "google-auth>=2.15.0", - "kubernetes>=20.13.0", - ], - "kubernetes": [ - "kubernetes>=20.13.0", - ], - "lambda_cloud": [ - "requests>=2.25.0", # Already in main requirements - ], - "huggingface_spaces": [ - "huggingface_hub>=0.16.0", # Already in main requirements - ], -} - -# Cluster types that require cloud providers -CLUSTER_TYPE_TO_PROVIDER: Dict[str, str] = { - "kubernetes": "kubernetes", - "aws_ec2": "aws", - "aws_eks": "aws", - "azure_vm": "azure", - "azure_aks": "azure", - "gcp_vm": "gcp", - "gcp_gke": "gcp", - "lambda_cloud": "lambda_cloud", - "huggingface_spaces": "huggingface_spaces", -} - - -def check_dependencies_installed(provider: str) -> bool: - """ - Check if dependencies for a cloud provider are installed. - - Args: - provider: Cloud provider name (aws, azure, gcp, etc.) - - Returns: - True if all dependencies are installed, False otherwise - """ - if provider not in CLOUD_PROVIDER_DEPS: - return True # No special dependencies needed - - # Try importing key modules for each provider - try: - if provider == "aws": - import boto3 # type: ignore # noqa: F401 - elif provider == "azure": - from azure.identity import ClientSecretCredential # noqa: F401 - from azure.mgmt.compute import ComputeManagementClient # noqa: F401 - elif provider == "gcp": - from google.cloud import compute_v1 # noqa: F401 - from google.cloud import container_v1 # noqa: F401 - elif provider == "kubernetes": - import kubernetes # type: ignore # noqa: F401 - - return True - except ImportError: - return False - - -def install_provider_dependencies( - provider: str, auto_install: bool = True, quiet: bool = False -) -> bool: - """ - Install dependencies for a cloud provider. - - Args: - provider: Cloud provider name - auto_install: If True, install automatically. If False, just check. - quiet: If True, suppress output messages - - Returns: - True if dependencies are available, False otherwise - """ - if provider not in CLOUD_PROVIDER_DEPS: - return True # No special dependencies needed - - # Check if already installed - if check_dependencies_installed(provider): - return True - - if not auto_install: - if not quiet: - deps = CLOUD_PROVIDER_DEPS[provider] - logger.warning( - f"Missing dependencies for {provider} provider. " - f"Install with: pip install {' '.join(deps)}" - ) - return False - - # Install dependencies - deps_to_install = CLOUD_PROVIDER_DEPS[provider] - - if not quiet: - logger.info(f"Installing {provider} dependencies: {', '.join(deps_to_install)}") - - try: - cmd = [sys.executable, "-m", "pip", "install"] + deps_to_install - if quiet: - cmd.append("--quiet") - - subprocess.run(cmd, capture_output=True, text=True, check=True) - - if not quiet: - logger.info(f"Successfully installed {provider} dependencies") - - return True - - except subprocess.CalledProcessError as e: - if not quiet: - logger.error(f"Failed to install {provider} dependencies: {e}") - if e.stderr: - logger.error(f"Error output: {e.stderr}") - return False - except Exception as e: - if not quiet: - logger.error(f"Unexpected error installing {provider} dependencies: {e}") - return False - - -def ensure_cloud_provider_dependencies( - cluster_type: Optional[str] = None, - cloud_provider: Optional[str] = None, - auto_install: bool = True, - quiet: bool = False, -) -> bool: - """ - Ensure cloud provider dependencies are available for a given configuration. - - Args: - cluster_type: Cluster type (kubernetes, aws_ec2, etc.) - cloud_provider: Cloud provider (aws, azure, gcp, etc.) - auto_install: Whether to automatically install missing dependencies - quiet: Whether to suppress output messages - - Returns: - True if all required dependencies are available, False otherwise - """ - # Determine which provider to check - provider_to_check = None - - if cloud_provider and cloud_provider != "manual": - provider_to_check = cloud_provider - elif cluster_type and cluster_type in CLUSTER_TYPE_TO_PROVIDER: - provider_to_check = CLUSTER_TYPE_TO_PROVIDER[cluster_type] - - if not provider_to_check: - return True # No cloud provider dependencies needed - - return install_provider_dependencies( - provider_to_check, auto_install=auto_install, quiet=quiet - ) - - -def get_installation_command( - cluster_type: Optional[str] = None, cloud_provider: Optional[str] = None -) -> Optional[str]: - """ - Get the pip install command for missing dependencies. - - Args: - cluster_type: Cluster type - cloud_provider: Cloud provider - - Returns: - Pip install command string, or None if no dependencies needed - """ - provider_to_check = None - - if cloud_provider and cloud_provider != "manual": - provider_to_check = cloud_provider - elif cluster_type and cluster_type in CLUSTER_TYPE_TO_PROVIDER: - provider_to_check = CLUSTER_TYPE_TO_PROVIDER[cluster_type] - - if not provider_to_check or provider_to_check not in CLOUD_PROVIDER_DEPS: - return None - - deps = CLOUD_PROVIDER_DEPS[provider_to_check] - return f"pip install {' '.join(deps)}" diff --git a/clustrix/cloud_provider_manager.py b/clustrix/cloud_provider_manager.py deleted file mode 100644 index 6bb9c720..00000000 --- a/clustrix/cloud_provider_manager.py +++ /dev/null @@ -1,404 +0,0 @@ -""" -Cloud provider integration for remote Kubernetes cluster management. - -This module provides automatic configuration and management of Kubernetes -clusters across major cloud providers (AWS EKS, Azure AKS, Google GKE). -""" - -import os -import subprocess -import logging -from typing import Dict, Any -from .config import ClusterConfig - -logger = logging.getLogger(__name__) - - -class CloudProviderError(Exception): - """Exception raised for cloud provider configuration errors.""" - - pass - - -class CloudProviderDetector: - """Detect and configure cloud provider Kubernetes clusters.""" - - @staticmethod - def detect_provider() -> str: - """ - Auto-detect cloud provider from environment. - - Returns: - str: Detected cloud provider ('aws', 'azure', 'gcp', or 'manual') - """ - # Check for AWS credentials/context - if CloudProviderDetector._check_aws_context(): - return "aws" - - # Check for Azure credentials/context - elif CloudProviderDetector._check_azure_context(): - return "azure" - - # Check for GCP credentials/context - elif CloudProviderDetector._check_gcp_context(): - return "gcp" - - return "manual" - - @staticmethod - def _check_aws_context() -> bool: - """Check if AWS environment is configured.""" - # Check for AWS credentials - if os.getenv("AWS_ACCESS_KEY_ID") or os.getenv("AWS_PROFILE"): - return True - - # Check if AWS CLI is configured - try: - result = subprocess.run( - ["aws", "sts", "get-caller-identity"], - capture_output=True, - text=True, - timeout=10, - ) - return result.returncode == 0 - except (subprocess.TimeoutExpired, FileNotFoundError): - return False - - @staticmethod - def _check_azure_context() -> bool: - """Check if Azure environment is configured.""" - # Check for Azure environment variables - if os.getenv("AZURE_SUBSCRIPTION_ID") or os.getenv("AZURE_TENANT_ID"): - return True - - # Check if Azure CLI is logged in - try: - result = subprocess.run( - ["az", "account", "show"], capture_output=True, text=True, timeout=10 - ) - return result.returncode == 0 - except (subprocess.TimeoutExpired, FileNotFoundError): - return False - - @staticmethod - def _check_gcp_context() -> bool: - """Check if GCP environment is configured.""" - # Check for GCP environment variables - if os.getenv("GOOGLE_APPLICATION_CREDENTIALS") or os.getenv("GCLOUD_PROJECT"): - return True - - # Check if gcloud is configured - try: - result = subprocess.run( - ["gcloud", "auth", "list", "--filter=status:ACTIVE"], - capture_output=True, - text=True, - timeout=10, - ) - return result.returncode == 0 and "ACTIVE" in result.stdout - except (subprocess.TimeoutExpired, FileNotFoundError): - return False - - -class AWSEKSConfigurator: - """AWS EKS cluster configuration and management.""" - - def __init__(self, config: ClusterConfig): - self.config = config - - def configure_cluster(self, cluster_name: str, region: str) -> Dict[str, Any]: - """ - Configure AWS EKS cluster access. - - Args: - cluster_name: EKS cluster name - region: AWS region - - Returns: - Dict with cluster configuration details - """ - try: - # Update kubeconfig for EKS cluster - cmd = [ - "aws", - "eks", - "update-kubeconfig", - "--region", - region, - "--name", - cluster_name, - ] - - if self.config.aws_profile: - cmd.extend(["--profile", self.config.aws_profile]) - - result = subprocess.run(cmd, capture_output=True, text=True, timeout=60) - - if result.returncode != 0: - raise CloudProviderError( - f"Failed to configure EKS cluster: {result.stderr}" - ) - - # Verify cluster access - self._verify_cluster_access() - - return { - "provider": "aws", - "cluster_name": cluster_name, - "region": region, - "configured": True, - } - - except subprocess.TimeoutExpired: - raise CloudProviderError("Timeout configuring EKS cluster") - except Exception as e: - raise CloudProviderError(f"EKS configuration failed: {e}") - - def _verify_cluster_access(self): - """Verify that kubectl can access the cluster.""" - try: - result = subprocess.run( - ["kubectl", "cluster-info"], capture_output=True, text=True, timeout=30 - ) - if result.returncode != 0: - raise CloudProviderError("Cannot access Kubernetes cluster") - except subprocess.TimeoutExpired: - raise CloudProviderError("Timeout verifying cluster access") - - -class AzureAKSConfigurator: - """Azure AKS cluster configuration and management.""" - - def __init__(self, config: ClusterConfig): - self.config = config - - def configure_cluster( - self, cluster_name: str, resource_group: str - ) -> Dict[str, Any]: - """ - Configure Azure AKS cluster access. - - Args: - cluster_name: AKS cluster name - resource_group: Azure resource group name - - Returns: - Dict with cluster configuration details - """ - try: - # Get AKS credentials - cmd = [ - "az", - "aks", - "get-credentials", - "--resource-group", - resource_group, - "--name", - cluster_name, - "--overwrite-existing", - ] - - if self.config.azure_subscription_id: - cmd.extend(["--subscription", self.config.azure_subscription_id]) - - result = subprocess.run(cmd, capture_output=True, text=True, timeout=60) - - if result.returncode != 0: - raise CloudProviderError( - f"Failed to configure AKS cluster: {result.stderr}" - ) - - # Verify cluster access - self._verify_cluster_access() - - return { - "provider": "azure", - "cluster_name": cluster_name, - "resource_group": resource_group, - "configured": True, - } - - except subprocess.TimeoutExpired: - raise CloudProviderError("Timeout configuring AKS cluster") - except Exception as e: - raise CloudProviderError(f"AKS configuration failed: {e}") - - def _verify_cluster_access(self): - """Verify that kubectl can access the cluster.""" - try: - result = subprocess.run( - ["kubectl", "cluster-info"], capture_output=True, text=True, timeout=30 - ) - if result.returncode != 0: - raise CloudProviderError("Cannot access Kubernetes cluster") - except subprocess.TimeoutExpired: - raise CloudProviderError("Timeout verifying cluster access") - - -class GoogleGKEConfigurator: - """Google GKE cluster configuration and management.""" - - def __init__(self, config: ClusterConfig): - self.config = config - - def configure_cluster( - self, cluster_name: str, zone: str, project_id: str - ) -> Dict[str, Any]: - """ - Configure Google GKE cluster access. - - Args: - cluster_name: GKE cluster name - zone: GCP zone (e.g., 'us-central1-a') - project_id: GCP project ID - - Returns: - Dict with cluster configuration details - """ - try: - # Get GKE credentials - cmd = [ - "gcloud", - "container", - "clusters", - "get-credentials", - cluster_name, - "--zone", - zone, - "--project", - project_id, - ] - - result = subprocess.run(cmd, capture_output=True, text=True, timeout=60) - - if result.returncode != 0: - raise CloudProviderError( - f"Failed to configure GKE cluster: {result.stderr}" - ) - - # Verify cluster access - self._verify_cluster_access() - - return { - "provider": "gcp", - "cluster_name": cluster_name, - "zone": zone, - "project_id": project_id, - "configured": True, - } - - except subprocess.TimeoutExpired: - raise CloudProviderError("Timeout configuring GKE cluster") - except Exception as e: - raise CloudProviderError(f"GKE configuration failed: {e}") - - def _verify_cluster_access(self): - """Verify that kubectl can access the cluster.""" - try: - result = subprocess.run( - ["kubectl", "cluster-info"], capture_output=True, text=True, timeout=30 - ) - if result.returncode != 0: - raise CloudProviderError("Cannot access Kubernetes cluster") - except subprocess.TimeoutExpired: - raise CloudProviderError("Timeout verifying cluster access") - - -class CloudProviderManager: - """Main manager for cloud provider operations.""" - - def __init__(self, config: ClusterConfig): - self.config = config - self.detector = CloudProviderDetector() - - def auto_configure(self) -> Dict[str, Any]: - """ - Automatically detect and configure cloud provider. - - Returns: - Dict with configuration results - """ - if not self.config.cloud_auto_configure: - return { - "auto_configured": False, - "reason": "Auto-configuration disabled", - } - - provider = self.config.cloud_provider - if provider == "manual": - provider = self.detector.detect_provider() - - if provider == "manual": - return { - "auto_configured": False, - "reason": "No cloud provider detected", - } - - try: - if provider == "aws": - return self._configure_aws() - elif provider == "azure": - return self._configure_azure() - elif provider == "gcp": - return self._configure_gcp() - else: - return { - "auto_configured": False, - "reason": f"Unsupported provider: {provider}", - } - except Exception as e: - logger.error(f"Auto-configuration failed: {e}") - return {"auto_configured": False, "error": str(e)} - - def _configure_aws(self) -> Dict[str, Any]: - """Configure AWS EKS.""" - if not self.config.eks_cluster_name or not self.config.cloud_region: - return { - "auto_configured": False, - "reason": "Missing EKS cluster name or region", - } - - configurator = AWSEKSConfigurator(self.config) - result = configurator.configure_cluster( - self.config.eks_cluster_name, self.config.cloud_region - ) - result["auto_configured"] = True - return result - - def _configure_azure(self) -> Dict[str, Any]: - """Configure Azure AKS.""" - if not self.config.aks_cluster_name or not self.config.azure_resource_group: - return { - "auto_configured": False, - "reason": "Missing AKS cluster name or resource group", - } - - configurator = AzureAKSConfigurator(self.config) - result = configurator.configure_cluster( - self.config.aks_cluster_name, self.config.azure_resource_group - ) - result["auto_configured"] = True - return result - - def _configure_gcp(self) -> Dict[str, Any]: - """Configure Google GKE.""" - if not all( - [ - self.config.gke_cluster_name, - self.config.gcp_zone, - self.config.gcp_project_id, - ] - ): - return { - "auto_configured": False, - "reason": "Missing GKE cluster name, zone, or project ID", - } - - configurator = GoogleGKEConfigurator(self.config) - result = configurator.configure_cluster( - self.config.gke_cluster_name or "", - self.config.gcp_zone or "", - self.config.gcp_project_id or "", - ) - result["auto_configured"] = True - return result diff --git a/clustrix/cloud_providers/__init__.py b/clustrix/cloud_providers/__init__.py deleted file mode 100644 index 4a5a3639..00000000 --- a/clustrix/cloud_providers/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Cloud provider integrations for Clustrix.""" - -from typing import Dict, Type, TYPE_CHECKING - -if TYPE_CHECKING: - from .base import CloudProvider - -# Registry of available cloud providers -PROVIDERS: Dict[str, Type["CloudProvider"]] = {} - -# Import providers to register them -try: - from . import aws # noqa: F401 -except ImportError: - pass - -try: - from . import azure # noqa: F401 -except ImportError: - pass - -try: - from . import gcp # noqa: F401 -except ImportError: - pass - -try: - from . import lambda_cloud # noqa: F401 -except ImportError: - pass - -try: - from . import huggingface_spaces # noqa: F401 -except ImportError: - pass - -__all__ = ["PROVIDERS"] diff --git a/clustrix/cloud_providers/aws.py b/clustrix/cloud_providers/aws.py deleted file mode 100644 index 3e04a07c..00000000 --- a/clustrix/cloud_providers/aws.py +++ /dev/null @@ -1,893 +0,0 @@ -"""AWS cloud provider integration for Clustrix.""" - -import logging -from typing import Dict, Any, Optional, List -from datetime import datetime, timezone - -try: - import boto3 # type: ignore - from botocore.exceptions import ClientError, NoCredentialsError # type: ignore - - BOTO3_AVAILABLE = True -except ImportError: - BOTO3_AVAILABLE = False - boto3 = None - ClientError = Exception - NoCredentialsError = Exception - -from .base import CloudProvider -from . import PROVIDERS - -logger = logging.getLogger(__name__) - - -class AWSProvider(CloudProvider): - """AWS cloud provider implementation.""" - - def __init__(self): - """Initialize AWS provider.""" - super().__init__() - self.ec2_client = None - self.eks_client = None - self.iam_client = None - self.region = "us-east-1" - - def authenticate(self, **credentials) -> bool: - """ - Authenticate with AWS. - - Args: - **credentials: AWS credentials including: - - access_key_id: AWS access key ID - - secret_access_key: AWS secret access key - - region: AWS region (default: us-east-1) - - session_token: Optional session token for temporary credentials - - Returns: - bool: True if authentication successful - """ - # First try provided credentials - access_key_id = credentials.get("access_key_id") - secret_access_key = credentials.get("secret_access_key") - region = credentials.get("region", "us-east-1") - session_token = credentials.get("session_token") - - # If credentials not provided, try to get from FlexibleCredentialManager - if not access_key_id or not secret_access_key: - logger.debug("No AWS credentials provided, trying credential manager...") - manager_creds = self.get_credentials_from_manager("aws") - if manager_creds: - access_key_id = access_key_id or manager_creds.get("access_key_id") - secret_access_key = secret_access_key or manager_creds.get( - "secret_access_key" - ) - region = region or manager_creds.get("region", "us-east-1") - session_token = session_token or manager_creds.get("session_token") - logger.info("Using AWS credentials from credential manager") - - if not access_key_id or not secret_access_key: - logger.error("access_key_id and secret_access_key are required") - return False - if not BOTO3_AVAILABLE: - logger.error("boto3 is not installed. Install with: pip install boto3") - return False - - try: - # Create session with provided credentials - session = boto3.Session( - aws_access_key_id=access_key_id, - aws_secret_access_key=secret_access_key, - aws_session_token=session_token, - region_name=region, - ) - - # Initialize clients - self.ec2_client = session.client("ec2") - self.eks_client = session.client("eks") - self.iam_client = session.client("iam") - - # Test credentials by making a simple API call that doesn't require IAM permissions - sts_client = session.client("sts") - sts_client.get_caller_identity() - - self.region = region - self.credentials = { - "access_key_id": access_key_id, - "secret_access_key": secret_access_key, - "region": region, - } - if session_token: - self.credentials["session_token"] = session_token - - self.authenticated = True - logger.info(f"Successfully authenticated with AWS in region {region}") - return True - - except NoCredentialsError: - logger.error("Invalid AWS credentials") - return False - except ClientError as e: - logger.error(f"AWS authentication failed: {e}") - return False - except Exception as e: - logger.error(f"Unexpected error during AWS authentication: {e}") - return False - - def validate_credentials(self) -> bool: - """Validate current AWS credentials.""" - if not self.authenticated or not self.iam_client: - return False - - try: - self.iam_client.get_user() - return True - except Exception: - return False - - def _create_or_get_eks_cluster_role(self) -> str: - """Create or get IAM role for EKS cluster.""" - role_name = "clustrix-eks-cluster-role" - - try: - # Try to get existing role - response = self.iam_client.get_role(RoleName=role_name) - return response["Role"]["Arn"] - except ClientError: - # Role doesn't exist, create it - pass - - # Create EKS cluster service role - trust_policy = { - "Version": "2012-10-17", - "Statement": [ - { - "Effect": "Allow", - "Principal": {"Service": "eks.amazonaws.com"}, - "Action": "sts:AssumeRole", - } - ], - } - - response = self.iam_client.create_role( - RoleName=role_name, - AssumeRolePolicyDocument=str(trust_policy).replace("'", '"'), - Description="IAM role for EKS cluster created by Clustrix", - ) - - role_arn = response["Role"]["Arn"] - - # Attach required policies - policies = [ - "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy", - ] - - for policy_arn in policies: - self.iam_client.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn) - - logger.info(f"Created EKS cluster role: {role_arn}") - return role_arn - - def _create_or_get_vpc_for_eks(self, cluster_name: str) -> Dict[str, Any]: - """Create or get VPC configuration for EKS cluster.""" - vpc_name = f"clustrix-eks-vpc-{cluster_name}" - - try: - # Check for existing VPC with our tag - vpcs = self.ec2_client.describe_vpcs( - Filters=[ - {"Name": "tag:Name", "Values": [vpc_name]}, - {"Name": "tag:created_by", "Values": ["clustrix"]}, - ] - ) - - if vpcs["Vpcs"]: - vpc_id = vpcs["Vpcs"][0]["VpcId"] - logger.info(f"Using existing VPC: {vpc_id}") - else: - # Create new VPC - vpc_response = self.ec2_client.create_vpc(CidrBlock="10.0.0.0/16") - vpc_id = vpc_response["Vpc"]["VpcId"] - - # Tag the VPC - self.ec2_client.create_tags( - Resources=[vpc_id], - Tags=[ - {"Key": "Name", "Value": vpc_name}, - {"Key": "created_by", "Value": "clustrix"}, - {"Key": "cluster_name", "Value": cluster_name}, - ], - ) - logger.info(f"Created VPC: {vpc_id}") - - # Create subnets and security groups - subnet_ids = self._create_eks_subnets(vpc_id, cluster_name) - security_group_ids = self._create_eks_security_groups(vpc_id, cluster_name) - - return { - "vpc_id": vpc_id, - "subnet_ids": subnet_ids, - "security_group_ids": security_group_ids, - } - - except ClientError as e: - logger.error(f"Failed to create VPC for EKS: {e}") - raise - - def _create_eks_subnets(self, vpc_id: str, cluster_name: str) -> List[str]: - """Create subnets for EKS cluster.""" - subnet_configs = [ - {"cidr": "10.0.1.0/24", "az_suffix": "a"}, - {"cidr": "10.0.2.0/24", "az_suffix": "b"}, - ] - - subnet_ids = [] - for i, config in enumerate(subnet_configs): - az = f"{self.region}{config['az_suffix']}" - subnet_name = f"clustrix-eks-subnet-{cluster_name}-{i + 1}" - - # Check if subnet exists - subnets = self.ec2_client.describe_subnets( - Filters=[ - {"Name": "tag:Name", "Values": [subnet_name]}, - {"Name": "vpc-id", "Values": [vpc_id]}, - ] - ) - - if subnets["Subnets"]: - subnet_id = subnets["Subnets"][0]["SubnetId"] - else: - # Create subnet - subnet_response = self.ec2_client.create_subnet( - VpcId=vpc_id, CidrBlock=config["cidr"], AvailabilityZone=az - ) - subnet_id = subnet_response["Subnet"]["SubnetId"] - - # Tag subnet - self.ec2_client.create_tags( - Resources=[subnet_id], - Tags=[ - {"Key": "Name", "Value": subnet_name}, - {"Key": "created_by", "Value": "clustrix"}, - {"Key": "kubernetes.io/role/elb", "Value": "1"}, - ], - ) - - subnet_ids.append(subnet_id) - - return subnet_ids - - def _create_eks_security_groups(self, vpc_id: str, cluster_name: str) -> List[str]: - """Create security groups for EKS cluster.""" - sg_name = f"clustrix-eks-sg-{cluster_name}" - - # Check if security group exists - sgs = self.ec2_client.describe_security_groups( - Filters=[ - {"Name": "group-name", "Values": [sg_name]}, - {"Name": "vpc-id", "Values": [vpc_id]}, - ] - ) - - if sgs["SecurityGroups"]: - sg_id = sgs["SecurityGroups"][0]["GroupId"] - else: - # Create security group - sg_response = self.ec2_client.create_security_group( - GroupName=sg_name, - Description=f"Security group for EKS cluster {cluster_name}", - VpcId=vpc_id, - ) - sg_id = sg_response["GroupId"] - - # Tag security group - self.ec2_client.create_tags( - Resources=[sg_id], - Tags=[ - {"Key": "Name", "Value": sg_name}, - {"Key": "created_by", "Value": "clustrix"}, - ], - ) - - return [sg_id] - - def create_eks_cluster( - self, - cluster_name: str, - node_count: int = 2, - instance_type: str = "t3.medium", - kubernetes_version: str = "1.27", - ) -> Dict[str, Any]: - """ - Create an EKS cluster. - - Args: - cluster_name: Name for the EKS cluster - node_count: Number of worker nodes - instance_type: EC2 instance type for nodes - kubernetes_version: Kubernetes version - - Returns: - Dict with cluster information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with AWS") - - try: - logger.info(f"Creating EKS cluster '{cluster_name}' in {self.region}...") - - # Step 1: Create or get EKS service role - cluster_role_arn = self._create_or_get_eks_cluster_role() - - # Step 2: Create or get VPC and subnets - vpc_config = self._create_or_get_vpc_for_eks(cluster_name) - - # Step 3: Create EKS cluster - cluster_response = self.eks_client.create_cluster( - name=cluster_name, - version=kubernetes_version, - roleArn=cluster_role_arn, - resourcesVpcConfig={ - "subnetIds": vpc_config["subnet_ids"], - "securityGroupIds": vpc_config["security_group_ids"], - }, - tags={ - "created_by": "clustrix", - "cluster_name": cluster_name, - "environment": "clustrix", - }, - ) - - cluster_info = cluster_response["cluster"] - - # Step 4: Create node group (after cluster is active - this will be async) - logger.info(f"EKS cluster '{cluster_name}' creation initiated...") - - return { - "cluster_name": cluster_name, - "status": cluster_info["status"], - "endpoint": cluster_info.get("endpoint", ""), - "arn": cluster_info["arn"], - "version": cluster_info["version"], - "node_count": node_count, - "instance_type": instance_type, - "region": self.region, - "role_arn": cluster_role_arn, - "vpc_config": vpc_config, - "created_at": cluster_info["createdAt"].isoformat(), - } - - except ClientError as e: - logger.error(f"Failed to create EKS cluster: {e}") - raise - - def create_ec2_instance( - self, - instance_name: str, - instance_type: str = "t3.medium", - ami_id: Optional[str] = None, - key_name: Optional[str] = None, - ) -> Dict[str, Any]: - """ - Create an EC2 instance. - - Args: - instance_name: Name tag for the instance - instance_type: EC2 instance type - ami_id: AMI ID (uses Amazon Linux 2 if not specified) - key_name: SSH key pair name - - Returns: - Dict with instance information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with AWS") - - try: - # Get default AMI if not specified - if not ami_id: - # Get latest Amazon Linux 2 AMI - response = self.ec2_client.describe_images( - Owners=["amazon"], - Filters=[ - {"Name": "name", "Values": ["amzn2-ami-hvm-*-x86_64-gp2"]}, - {"Name": "state", "Values": ["available"]}, - ], - ) - ami_id = sorted( - response["Images"], key=lambda x: x["CreationDate"], reverse=True - )[0]["ImageId"] - - # Create instance - response = self.ec2_client.run_instances( - ImageId=ami_id, - InstanceType=instance_type, - MinCount=1, - MaxCount=1, - KeyName=key_name, - TagSpecifications=[ - { - "ResourceType": "instance", - "Tags": [{"Key": "Name", "Value": instance_name}], - } - ], - ) - - instance = response["Instances"][0] - instance_id = instance["InstanceId"] - - # Wait for instance to get public IP - waiter = self.ec2_client.get_waiter("instance_running") - waiter.wait(InstanceIds=[instance_id]) - - # Get updated instance info - response = self.ec2_client.describe_instances(InstanceIds=[instance_id]) - instance = response["Reservations"][0]["Instances"][0] - - return { - "instance_id": instance_id, - "instance_name": instance_name, - "public_ip": instance.get("PublicIpAddress", ""), - "private_ip": instance.get("PrivateIpAddress", ""), - "instance_type": instance_type, - "state": instance["State"]["Name"], - "region": self.region, - "created_at": datetime.now(timezone.utc).isoformat(), - } - - except ClientError as e: - logger.error(f"Failed to create EC2 instance: {e}") - raise - - def create_cluster( - self, cluster_name: str, cluster_type: str = "eks", **kwargs - ) -> Dict[str, Any]: - """ - Create a cluster (EKS or EC2). - - Args: - cluster_name: Name for the cluster - cluster_type: Type of cluster ('eks' or 'ec2') - **kwargs: Additional parameters for cluster creation - - Returns: - Dict with cluster information - """ - if cluster_type == "eks": - return self.create_eks_cluster(cluster_name, **kwargs) - elif cluster_type == "ec2": - return self.create_ec2_instance(cluster_name, **kwargs) - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - def delete_cluster( - self, cluster_identifier: str, cluster_type: str = "eks" - ) -> bool: - """ - Delete a cluster. - - Args: - cluster_identifier: Cluster name or instance ID - cluster_type: Type of cluster ('eks' or 'ec2') - - Returns: - bool: True if deletion successful - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with AWS") - - try: - if cluster_type == "eks": - logger.info(f"Deleting EKS cluster '{cluster_identifier}'...") - - try: - # First delete any node groups - nodegroups = self.eks_client.list_nodegroups( - clusterName=cluster_identifier - ) - - for nodegroup_name in nodegroups.get("nodegroups", []): - logger.info(f"Deleting node group: {nodegroup_name}") - self.eks_client.delete_nodegroup( - clusterName=cluster_identifier, nodegroupName=nodegroup_name - ) - - # Delete the cluster itself - self.eks_client.delete_cluster(name=cluster_identifier) - logger.info( - f"EKS cluster '{cluster_identifier}' deletion initiated" - ) - return True - - except ClientError as e: - if e.response["Error"]["Code"] == "ResourceNotFoundException": - logger.warning(f"EKS cluster '{cluster_identifier}' not found") - return True # Consider this success - else: - raise - elif cluster_type == "ec2": - self.ec2_client.terminate_instances(InstanceIds=[cluster_identifier]) - logger.info(f"Terminated EC2 instance '{cluster_identifier}'") - return True - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - except ClientError as e: - logger.error(f"Failed to delete cluster: {e}") - return False - - def get_cluster_status( - self, cluster_identifier: str, cluster_type: str = "eks" - ) -> Dict[str, Any]: - """Get status of a cluster.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with AWS") - - try: - if cluster_type == "eks": - try: - cluster_response = self.eks_client.describe_cluster( - name=cluster_identifier - ) - cluster = cluster_response["cluster"] - - # Get node group information - nodegroups = self.eks_client.list_nodegroups( - clusterName=cluster_identifier - ) - - node_count = 0 - if nodegroups.get("nodegroups"): - # Get details of first node group for node count - ng_response = self.eks_client.describe_nodegroup( - clusterName=cluster_identifier, - nodegroupName=nodegroups["nodegroups"][0], - ) - node_count = ( - ng_response["nodegroup"] - .get("scalingConfig", {}) - .get("desiredSize", 0) - ) - - return { - "cluster_name": cluster_identifier, - "status": cluster["status"], - "endpoint": cluster.get("endpoint", ""), - "version": cluster.get("version", ""), - "arn": cluster.get("arn", ""), - "node_count": node_count, - "created_at": cluster.get("createdAt", ""), - "cluster_type": "eks", - "region": self.region, - } - except ClientError as e: - if e.response["Error"]["Code"] == "ResourceNotFoundException": - return { - "cluster_name": cluster_identifier, - "status": "NOT_FOUND", - "cluster_type": "eks", - } - else: - raise - elif cluster_type == "ec2": - response = self.ec2_client.describe_instances( - InstanceIds=[cluster_identifier] - ) - instance = response["Reservations"][0]["Instances"][0] - return { - "instance_id": cluster_identifier, - "status": instance["State"]["Name"], - "cluster_type": "ec2", - } - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - except ClientError as e: - logger.error(f"Failed to get cluster status: {e}") - raise - - def list_clusters(self) -> List[Dict[str, Any]]: - """List all clusters.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with AWS") - - clusters = [] - - # List EKS clusters - try: - response = self.eks_client.list_clusters() - for cluster_name in response.get("clusters", []): - clusters.append( - {"name": cluster_name, "type": "eks", "region": self.region} - ) - except ClientError: - pass - - # List EC2 instances tagged as Clustrix - try: - response = self.ec2_client.describe_instances( - Filters=[ - {"Name": "tag:ManagedBy", "Values": ["Clustrix"]}, - {"Name": "instance-state-name", "Values": ["running", "pending"]}, - ] - ) - for reservation in response["Reservations"]: - for instance in reservation["Instances"]: - name = next( - ( - tag["Value"] - for tag in instance.get("Tags", []) - if tag["Key"] == "Name" - ), - instance["InstanceId"], - ) - clusters.append( - { - "name": name, - "instance_id": instance["InstanceId"], - "type": "ec2", - "region": self.region, - "state": instance["State"]["Name"], - } - ) - except ClientError: - pass - - return clusters - - def get_cluster_config( - self, cluster_identifier: str, cluster_type: str = "eks" - ) -> Dict[str, Any]: - """ - Get Clustrix configuration for a cluster. - - Args: - cluster_identifier: Cluster name or instance ID - cluster_type: Type of cluster ('eks' or 'ec2') - - Returns: - Dict with Clustrix configuration - """ - if cluster_type == "eks": - return { - "name": f"AWS EKS - {cluster_identifier}", - "cluster_type": "kubernetes", - "cluster_host": f"{cluster_identifier}.eks.{self.region}.amazonaws.com", - "cluster_port": 443, - "k8s_namespace": "default", - "k8s_image": "python:3.11", - "default_cores": 2, - "default_memory": "4GB", - "cost_monitoring": True, - "provider": "aws", - "provider_config": { - "cluster_name": cluster_identifier, - "region": self.region, - }, - } - elif cluster_type == "ec2": - # Get instance details - response = self.ec2_client.describe_instances( - InstanceIds=[cluster_identifier] - ) - instance = response["Reservations"][0]["Instances"][0] - - return { - "name": f"AWS EC2 - {cluster_identifier}", - "cluster_type": "ssh", - "cluster_host": instance.get("PublicIpAddress", ""), - "username": "ec2-user", # Default for Amazon Linux - "cluster_port": 22, - "default_cores": 2, # Would need to map instance type to cores - "default_memory": "4GB", # Would need to map instance type to memory - "remote_work_dir": "/home/ec2-user/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - "provider": "aws", - "provider_config": { - "instance_id": cluster_identifier, - "region": self.region, - }, - } - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - def estimate_cost(self, **kwargs) -> Dict[str, float]: - """ - Estimate AWS costs. - - Args: - **kwargs: AWS cost parameters including: - - cluster_type: Type of cluster ('eks' or 'ec2') - - instance_type: EC2 instance type - - node_count: Number of nodes (for EKS) - - hours: Number of hours - - Returns: - Dict with cost breakdown - """ - cluster_type = kwargs.get("cluster_type", "eks") - instance_type = kwargs.get("instance_type", "t3.medium") - node_count = kwargs.get("node_count", 2) - hours = kwargs.get("hours", 1) - # Simplified pricing - real implementation would use AWS Pricing API - instance_prices = { - "t3.micro": 0.0104, - "t3.small": 0.0208, - "t3.medium": 0.0416, - "t3.large": 0.0832, - "m5.large": 0.096, - "m5.xlarge": 0.192, - "c5.large": 0.085, - "c5.xlarge": 0.170, - } - - base_price = instance_prices.get(instance_type, 0.10) # Default price - - if cluster_type == "eks": - # EKS charges $0.10 per hour for the control plane - control_plane_cost = 0.10 * hours - node_cost = base_price * node_count * hours - total = control_plane_cost + node_cost - - return { - "control_plane": control_plane_cost, - "nodes": node_cost, - "total": total, - } - else: # ec2 - total = base_price * hours - return { - "instance": total, - "total": total, - } - - def get_available_instance_types(self, region: Optional[str] = None) -> List[str]: - """ - Get available EC2 instance types for the specified region. - - Args: - region: AWS region to query (uses current region if not specified) - - Returns: - List of available instance type names - """ - if not self.authenticated: - # Return a default list if not authenticated - return [ - "t3.micro", - "t3.small", - "t3.medium", - "t3.large", - "t3.xlarge", - "c5.large", - "c5.xlarge", - "c5.2xlarge", - "c5.4xlarge", - "m5.large", - "m5.xlarge", - "m5.2xlarge", - "m5.4xlarge", - "r5.large", - "r5.xlarge", - "r5.2xlarge", - ] - - try: - # Use specified region or current region - query_region = region or self.region - - # Create EC2 client for the specified region if different - if region and region != self.region: - session = boto3.Session( - aws_access_key_id=self.credentials.get("access_key_id"), - aws_secret_access_key=self.credentials.get("secret_access_key"), - aws_session_token=self.credentials.get("session_token"), - region_name=region, - ) - ec2_client = session.client("ec2") - else: - ec2_client = self.ec2_client - - # Get instance type offerings for the region - response = ec2_client.describe_instance_type_offerings( - LocationType="region", - Filters=[{"Name": "location", "Values": [query_region]}], - ) - - # Extract instance type names and sort them - instance_types = [ - offering["InstanceType"] - for offering in response["InstanceTypeOfferings"] - ] - instance_types.sort() - - # Filter to common instance families for better UX - common_families = ["t3", "t2", "c5", "c4", "m5", "m4", "r5", "r4"] - filtered_types = [] - - for family in common_families: - family_types = [t for t in instance_types if t.startswith(family + ".")] - filtered_types.extend(family_types[:6]) # Limit to 6 sizes per family - - return filtered_types[:30] # Limit total to 30 for better UX - - except Exception as e: - logger.warning( - f"Failed to fetch instance types for region {query_region}: {e}" - ) - # Return default list on error - return [ - "t3.micro", - "t3.small", - "t3.medium", - "t3.large", - "t3.xlarge", - "c5.large", - "c5.xlarge", - "c5.2xlarge", - "m5.large", - "m5.xlarge", - "m5.2xlarge", - ] - - def get_available_regions(self) -> List[str]: - """ - Get available AWS regions. - - Returns: - List of available AWS region names - """ - if not self.authenticated: - # Return common regions if not authenticated - return [ - "us-east-1", - "us-west-1", - "us-west-2", - "eu-west-1", - "eu-central-1", - "ap-southeast-1", - "ap-northeast-1", - ] - - try: - # Get all available regions - response = self.ec2_client.describe_regions() - regions = [region["RegionName"] for region in response["Regions"]] - regions.sort() - - # Prioritize common regions - priority_regions = [ - "us-east-1", - "us-west-1", - "us-west-2", - "eu-west-1", - "eu-central-1", - "ap-southeast-1", - "ap-northeast-1", - ] - - # Put priority regions first, then others - sorted_regions = [] - for region in priority_regions: - if region in regions: - sorted_regions.append(region) - regions.remove(region) - - sorted_regions.extend(regions) - return sorted_regions - - except Exception as e: - logger.warning(f"Failed to fetch AWS regions: {e}") - return [ - "us-east-1", - "us-west-1", - "us-west-2", - "eu-west-1", - "eu-central-1", - "ap-southeast-1", - "ap-northeast-1", - ] - - -# Register the provider -if BOTO3_AVAILABLE: - PROVIDERS["aws"] = AWSProvider diff --git a/clustrix/cloud_providers/azure.py b/clustrix/cloud_providers/azure.py deleted file mode 100644 index 414044e6..00000000 --- a/clustrix/cloud_providers/azure.py +++ /dev/null @@ -1,876 +0,0 @@ -"""Azure cloud provider integration for Clustrix.""" - -import logging -from typing import Dict, Any, List, Optional -from datetime import datetime, timezone - -try: - from azure.identity import ClientSecretCredential - from azure.mgmt.compute import ComputeManagementClient - from azure.mgmt.resource import ResourceManagementClient - from azure.mgmt.network import NetworkManagementClient - from azure.mgmt.containerservice import ContainerServiceClient - from azure.core.exceptions import ClientAuthenticationError, ResourceNotFoundError - - AZURE_AVAILABLE = True -except ImportError: - AZURE_AVAILABLE = False - ClientSecretCredential = None # type: ignore - ComputeManagementClient = None # type: ignore - ResourceManagementClient = None # type: ignore - NetworkManagementClient = None # type: ignore - ContainerServiceClient = None # type: ignore - ClientAuthenticationError = Exception # type: ignore - ResourceNotFoundError = Exception # type: ignore - -from .base import CloudProvider -from . import PROVIDERS - -logger = logging.getLogger(__name__) - - -class AzureProvider(CloudProvider): - """Azure cloud provider implementation.""" - - def __init__(self): - """Initialize Azure provider.""" - super().__init__() - self.subscription_id = None - self.client_id = None - self.tenant_id = None - self.region = "eastus" - self.resource_group = "clustrix-rg" - self.compute_client = None - self.resource_client = None - self.network_client = None - self.container_client = None - self.credential = None - - def authenticate(self, **credentials) -> bool: - """ - Authenticate with Azure. - - Args: - **credentials: Azure credentials including: - - subscription_id: Azure subscription ID - - client_id: Azure service principal client ID - - client_secret: Azure service principal secret - - tenant_id: Azure tenant ID - - region: Azure region (default: eastus) - - resource_group: Resource group name (default: clustrix-rg) - - Returns: - bool: True if authentication successful - """ - # First try provided credentials - subscription_id = credentials.get("subscription_id") - client_id = credentials.get("client_id") - client_secret = credentials.get("client_secret") - tenant_id = credentials.get("tenant_id") - region = credentials.get("region", "eastus") - resource_group = credentials.get("resource_group", "clustrix-rg") - - # If credentials not provided, try to get from FlexibleCredentialManager - if not all([subscription_id, client_id, client_secret, tenant_id]): - logger.debug( - "Incomplete Azure credentials provided, trying credential manager..." - ) - manager_creds = self.get_credentials_from_manager("azure") - if manager_creds: - subscription_id = subscription_id or manager_creds.get( - "subscription_id" - ) - client_id = client_id or manager_creds.get("client_id") - client_secret = client_secret or manager_creds.get("client_secret") - tenant_id = tenant_id or manager_creds.get("tenant_id") - logger.info("Using Azure credentials from credential manager") - - if not all([subscription_id, client_id, client_secret, tenant_id]): - logger.error( - "subscription_id, client_id, client_secret, and tenant_id are required" - ) - return False - - if not AZURE_AVAILABLE: - logger.error( - "azure-identity and azure-mgmt-compute are not installed. " - "Install with: pip install azure-identity azure-mgmt-compute " - "azure-mgmt-resource azure-mgmt-network" - ) - return False - - try: - # Create credential object - # Type assertions since we verified these are not None above - assert isinstance(tenant_id, str) - assert isinstance(client_id, str) - assert isinstance(client_secret, str) - assert isinstance(subscription_id, str) - self.credential = ClientSecretCredential( - tenant_id=tenant_id, client_id=client_id, client_secret=client_secret - ) - - # Initialize Azure clients - self.compute_client = ComputeManagementClient( - credential=self.credential, subscription_id=subscription_id - ) - - self.resource_client = ResourceManagementClient( - credential=self.credential, subscription_id=subscription_id - ) - - self.network_client = NetworkManagementClient( - credential=self.credential, subscription_id=subscription_id - ) - - self.container_client = ContainerServiceClient( - credential=self.credential, subscription_id=subscription_id - ) - - # Test credentials by listing resource groups - try: - list(self.resource_client.resource_groups.list()) - except Exception as e: - logger.error(f"Failed to verify Azure credentials: {e}") - return False - - self.subscription_id = subscription_id - self.client_id = client_id - self.tenant_id = tenant_id - self.region = region - self.resource_group = resource_group - self.credentials = credentials - self.authenticated = True - logger.info( - f"Successfully authenticated with Azure subscription {subscription_id}" - ) - return True - - except ClientAuthenticationError: - logger.error("Invalid Azure credentials") - return False - except Exception as e: - logger.error(f"Unexpected error during Azure authentication: {e}") - return False - - def validate_credentials(self) -> bool: - """Validate current Azure credentials.""" - if not self.authenticated or not self.compute_client: - return False - - try: - # Try to list resource groups to verify credentials are still valid - list(self.resource_client.resource_groups.list()) - return True - except Exception: - return False - - def _ensure_resource_group(self) -> bool: - """Ensure the resource group exists, create if it doesn't.""" - try: - # Check if resource group exists - try: - self.resource_client.resource_groups.get(self.resource_group) - return True - except ResourceNotFoundError: - # Create resource group - rg_params = { - "location": self.region, - "tags": {"created_by": "clustrix"}, - } - self.resource_client.resource_groups.create_or_update( - self.resource_group, rg_params - ) - logger.info( - f"Created resource group '{self.resource_group}' in {self.region}" - ) - return True - except Exception as e: - logger.error(f"Failed to ensure resource group: {e}") - return False - - def create_vm( - self, - vm_name: str, - vm_size: str = "Standard_D2s_v3", - admin_username: str = "azureuser", - admin_password: Optional[str] = None, - ) -> Dict[str, Any]: - """ - Create an Azure Virtual Machine. - - Args: - vm_name: Name for the VM - vm_size: VM size (e.g., Standard_D2s_v3) - admin_username: Admin username for the VM - admin_password: Admin password (if None, uses SSH key) - - Returns: - Dict with VM information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with Azure") - - try: - # Ensure resource group exists - if not self._ensure_resource_group(): - raise RuntimeError("Failed to create or access resource group") - - # Create or get virtual network - vnet_name = f"{vm_name}-vnet" - subnet_name = f"{vm_name}-subnet" - - vnet_params = { - "location": self.region, - "address_space": {"address_prefixes": ["10.0.0.0/16"]}, - "subnets": [{"name": subnet_name, "address_prefix": "10.0.0.0/24"}], - } - - vnet_result = self.network_client.virtual_networks.begin_create_or_update( - self.resource_group, vnet_name, vnet_params - ).result() - - # Create public IP - public_ip_name = f"{vm_name}-ip" - public_ip_params = { - "location": self.region, - "public_ip_allocation_method": "Static", - "sku": {"name": "Standard"}, - } - - public_ip_result = ( - self.network_client.public_ip_addresses.begin_create_or_update( - self.resource_group, public_ip_name, public_ip_params - ).result() - ) - - # Create network security group with SSH rule - nsg_name = f"{vm_name}-nsg" - nsg_params = { - "location": self.region, - "security_rules": [ - { - "name": "SSH", - "protocol": "Tcp", - "source_port_range": "*", - "destination_port_range": "22", - "source_address_prefix": "*", - "destination_address_prefix": "*", - "access": "Allow", - "priority": 1000, - "direction": "Inbound", - } - ], - } - - nsg_result = ( - self.network_client.network_security_groups.begin_create_or_update( - self.resource_group, nsg_name, nsg_params - ).result() - ) - - # Create network interface - nic_name = f"{vm_name}-nic" - nic_params = { - "location": self.region, - "ip_configurations": [ - { - "name": "ipconfig1", - "subnet": {"id": vnet_result.subnets[0].id}, - "public_ip_address": {"id": public_ip_result.id}, - } - ], - "network_security_group": {"id": nsg_result.id}, - } - - nic_result = self.network_client.network_interfaces.begin_create_or_update( - self.resource_group, nic_name, nic_params - ).result() - - # Create VM - vm_params = { - "location": self.region, - "os_profile": { - "computer_name": vm_name, - "admin_username": admin_username, - "linux_configuration": { - "disable_password_authentication": admin_password is None, - "ssh": { - "public_keys": ( - [] if admin_password else [] - ) # Would need SSH key - }, - }, - }, - "hardware_profile": {"vm_size": vm_size}, - "storage_profile": { - "image_reference": { - "publisher": "Canonical", - "offer": "0001-com-ubuntu-server-focal", - "sku": "20_04-lts-gen2", - "version": "latest", - }, - "os_disk": { - "create_option": "FromImage", - "disk_size_gb": 30, - "managed_disk": {"storage_account_type": "Standard_LRS"}, - }, - }, - "network_profile": {"network_interfaces": [{"id": nic_result.id}]}, - "tags": {"created_by": "clustrix"}, - } - - # Add password if provided - if admin_password: - vm_params["os_profile"]["admin_password"] = admin_password - - # Create the VM - vm_result = self.compute_client.virtual_machines.begin_create_or_update( - self.resource_group, vm_name, vm_params - ).result() - - logger.info(f"Created Azure VM '{vm_name}' with size {vm_size}") - - return { - "vm_name": vm_name, - "vm_id": vm_result.id, - "vm_size": vm_size, - "region": self.region, - "resource_group": self.resource_group, - "status": "creating", - "public_ip": public_ip_result.ip_address, - "admin_username": admin_username, - "created_at": datetime.now(timezone.utc).isoformat(), - } - - except Exception as e: - logger.error(f"Failed to create Azure VM: {e}") - raise - - def create_aks_cluster( - self, - cluster_name: str, - node_count: int = 3, - node_vm_size: str = "Standard_DS2_v2", - kubernetes_version: Optional[str] = None, - **kwargs, - ) -> Dict[str, Any]: - """ - Create an AKS (Azure Kubernetes Service) cluster. - - Args: - cluster_name: Name for the AKS cluster - node_count: Number of nodes in the default node pool - node_vm_size: VM size for nodes (default: Standard_DS2_v2) - kubernetes_version: Kubernetes version (uses default if None) - **kwargs: Additional cluster configuration - - Returns: - Dict containing cluster information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with Azure") - - try: - logger.info(f"Creating AKS cluster '{cluster_name}' in {self.region}...") - - # Ensure resource group exists - self.resource_client.resource_groups.create_or_update( - self.resource_group, {"location": self.region} - ) - - # Define AKS cluster configuration - cluster_config = { - "location": self.region, - "kubernetes_version": kubernetes_version, - "agent_pool_profiles": [ - { - "name": "default", - "count": node_count, - "vm_size": node_vm_size, - "os_type": "Linux", - "mode": "System", - } - ], - "service_principal_profile": { - "client_id": self.client_id, - "secret": self.credentials.get("client_secret"), - }, - "network_profile": {"network_plugin": "kubenet"}, - "enable_rbac": True, - "tags": {"created_by": "clustrix", "cluster_name": cluster_name}, - } - - # Start cluster creation (async operation) - operation = self.container_client.managed_clusters.begin_create_or_update( - resource_group_name=self.resource_group, - resource_name=cluster_name, - parameters=cluster_config, - ) - - logger.info(f"AKS cluster creation initiated - operation: {operation}") - - return { - "cluster_name": cluster_name, - "status": "creating", - "region": self.region, - "provider": "azure", - "cluster_type": "aks", - "resource_group": self.resource_group, - "node_count": node_count, - "node_vm_size": node_vm_size, - "kubernetes_version": kubernetes_version, - "operation_id": str(operation), - "created_at": datetime.now(timezone.utc).isoformat(), - } - - except Exception as e: - logger.error(f"Failed to create AKS cluster: {e}") - raise - - def create_cluster( - self, cluster_name: str, cluster_type: str = "vm", **kwargs - ) -> Dict[str, Any]: - """Create an Azure cluster (VM or AKS).""" - if cluster_type == "vm": - return self.create_vm(cluster_name, **kwargs) - elif cluster_type == "aks": - return self.create_aks_cluster(cluster_name, **kwargs) - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - def delete_cluster(self, cluster_identifier: str, cluster_type: str = "vm") -> bool: - """Delete an Azure cluster.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with Azure") - - try: - if cluster_type == "vm": - # Delete VM and associated resources - self.compute_client.virtual_machines.begin_delete( - self.resource_group, cluster_identifier - ).result() - - # Also delete associated resources (NIC, IP, NSG, etc.) - try: - self.network_client.network_interfaces.begin_delete( - self.resource_group, f"{cluster_identifier}-nic" - ).result() - - self.network_client.public_ip_addresses.begin_delete( - self.resource_group, f"{cluster_identifier}-ip" - ).result() - - self.network_client.network_security_groups.begin_delete( - self.resource_group, f"{cluster_identifier}-nsg" - ).result() - - self.network_client.virtual_networks.begin_delete( - self.resource_group, f"{cluster_identifier}-vnet" - ).result() - except Exception as e: - logger.warning(f"Failed to delete some associated resources: {e}") - - logger.info(f"Deleted Azure VM '{cluster_identifier}'") - return True - elif cluster_type == "aks": - # Delete AKS cluster - operation = self.container_client.managed_clusters.begin_delete( - resource_group_name=self.resource_group, - resource_name=cluster_identifier, - ) - logger.info( - f"Deleting AKS cluster '{cluster_identifier}' - operation: {operation}" - ) - return True - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - except Exception as e: - logger.error(f"Failed to delete Azure cluster: {e}") - return False - - def get_cluster_status( - self, cluster_identifier: str, cluster_type: str = "vm" - ) -> Dict[str, Any]: - """Get status of an Azure cluster.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with Azure") - - try: - if cluster_type == "vm": - vm = self.compute_client.virtual_machines.get( - self.resource_group, cluster_identifier - ) - return { - "vm_name": cluster_identifier, - "status": ( - vm.provisioning_state.lower() - if vm.provisioning_state - else "unknown" - ), - "vm_size": ( - vm.hardware_profile.vm_size - if vm.hardware_profile - else "unknown" - ), - "region": vm.location, - "resource_group": self.resource_group, - "provider": "azure", - "cluster_type": "vm", - } - elif cluster_type == "aks": - # Get AKS cluster status - cluster = self.container_client.managed_clusters.get( - resource_group_name=self.resource_group, - resource_name=cluster_identifier, - ) - - return { - "cluster_name": cluster_identifier, - "status": cluster.provisioning_state.lower(), - "kubernetes_version": cluster.kubernetes_version, - "node_count": ( - cluster.agent_pool_profiles[0].count - if cluster.agent_pool_profiles - else 0 - ), - "fqdn": cluster.fqdn, - "region": cluster.location, - "resource_group": self.resource_group, - "provider": "azure", - "cluster_type": "aks", - } - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - except Exception as e: - logger.error(f"Failed to get cluster status: {e}") - raise - - def list_clusters(self) -> List[Dict[str, Any]]: - """List all Azure clusters.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with Azure") - - clusters = [] - - try: - # List VMs with clustrix tag - vms = self.compute_client.virtual_machines.list(self.resource_group) - - for vm in vms: - # Check if VM has clustrix tag - tags = getattr(vm, "tags", {}) - - if tags and tags.get("created_by") == "clustrix": - clusters.append( - { - "name": vm.name, - "vm_id": vm.id, - "type": "vm", - "status": ( - vm.provisioning_state.lower() - if vm.provisioning_state - else "unknown" - ), - "vm_size": ( - vm.hardware_profile.vm_size - if vm.hardware_profile - else "unknown" - ), - "region": vm.location, - "resource_group": self.resource_group, - } - ) - - except Exception as e: - logger.error(f"Failed to list Azure VMs: {e}") - - # List AKS clusters - try: - aks_clusters = ( - self.container_client.managed_clusters.list_by_resource_group( - resource_group_name=self.resource_group - ) - ) - - for cluster in aks_clusters: - # Only include clusters with clustrix tag - tags = cluster.tags or {} - if tags.get("created_by") == "clustrix": - clusters.append( - { - "name": cluster.name, - "cluster_id": cluster.id, - "type": "aks", - "status": cluster.provisioning_state.lower(), - "kubernetes_version": cluster.kubernetes_version, - "node_count": ( - cluster.agent_pool_profiles[0].count - if cluster.agent_pool_profiles - else 0 - ), - "fqdn": cluster.fqdn, - "region": cluster.location, - "resource_group": self.resource_group, - } - ) - except Exception as e: - logger.error(f"Failed to list AKS clusters: {e}") - - return clusters - - def get_cluster_config( - self, cluster_identifier: str, cluster_type: str = "vm" - ) -> Dict[str, Any]: - """Get Clustrix configuration for an Azure cluster.""" - if cluster_type == "vm": - # Get VM details and public IP - try: - self.compute_client.virtual_machines.get( - self.resource_group, cluster_identifier - ) - - # Get public IP. A VM with no reachable address is not a - # cluster anyone can connect to, and returning an empty (or - # invented) host here only moves the failure to a confusing - # SSH timeout later. - try: - ip_result = self.network_client.public_ip_addresses.get( - self.resource_group, f"{cluster_identifier}-ip" - ) - except Exception as e: - raise RuntimeError( - f"Azure VM '{cluster_identifier}' in resource group " - f"'{self.resource_group}' has no readable public IP " - f"resource ('{cluster_identifier}-ip'): {e}" - ) from e - - public_ip = ip_result.ip_address - if not public_ip: - raise RuntimeError( - f"Azure VM '{cluster_identifier}' has a public IP " - f"resource ('{cluster_identifier}-ip') with no address " - "assigned yet, so there is no host to connect to." - ) - - return { - "name": f"Azure VM - {cluster_identifier}", - "cluster_type": "ssh", - "cluster_host": public_ip, - "username": "azureuser", # Default admin username - "cluster_port": 22, - "default_cores": 2, # Would need to map VM size to cores - "default_memory": "4GB", # Would need to map VM size to memory - "remote_work_dir": "/home/azureuser/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - "provider": "azure", - "provider_config": { - "vm_name": cluster_identifier, - "resource_group": self.resource_group, - "region": self.region, - "subscription_id": self.subscription_id, - }, - } - except Exception as e: - # This used to return cluster_host "placeholder.azure.com". - # Nothing downstream could tell that apart from a real host, - # so the failure surfaced as an SSH error against a domain - # that does not exist, far from its cause (#119). - raise RuntimeError( - f"Could not determine the connection details of Azure VM " - f"'{cluster_identifier}' in resource group " - f"'{self.resource_group}': {e}" - ) from e - elif cluster_type == "aks": - return { - "name": f"Azure AKS - {cluster_identifier}", - "cluster_type": "kubernetes", - "cluster_host": f"{cluster_identifier}.aks.{self.region}.azure.com", - "cluster_port": 443, - "k8s_namespace": "default", - "k8s_image": "python:3.11", - "default_cores": 2, - "default_memory": "4GB", - "cost_monitoring": True, - "provider": "azure", - "provider_config": { - "cluster_name": cluster_identifier, - "resource_group": self.resource_group, - "region": self.region, - "subscription_id": self.subscription_id, - }, - } - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - def estimate_cost(self, **kwargs) -> Dict[str, float]: - """Estimate Azure costs.""" - cluster_type = kwargs.get("cluster_type", "vm") - vm_size = kwargs.get("vm_size", "Standard_D2s_v3") - hours = kwargs.get("hours", 1) - - # Simplified pricing - real implementation would use Azure Pricing API - vm_prices = { - "Standard_B1s": 0.0104, - "Standard_B1ms": 0.0207, - "Standard_B2s": 0.0416, - "Standard_B2ms": 0.0832, - "Standard_D2s_v3": 0.096, - "Standard_D4s_v3": 0.192, - "Standard_D8s_v3": 0.384, - "Standard_F2s_v2": 0.0834, - "Standard_F4s_v2": 0.1669, - "Standard_E2s_v3": 0.126, - "Standard_E4s_v3": 0.252, - } - - base_price = vm_prices.get(vm_size, 0.10) # Default price - - if cluster_type == "aks": - # AKS has cluster management fee (free tier available) - cluster_fee = 0.0 # Free tier for development - node_cost = base_price * hours - total = cluster_fee + node_cost - - return { - "cluster_management": cluster_fee, - "nodes": node_cost, - "total": total, - } - else: # vm - total = base_price * hours - return {"vm": total, "total": total} - - def get_available_instance_types(self, region: Optional[str] = None) -> List[str]: - """Get available Azure VM sizes.""" - if not self.authenticated: - # Return common VM sizes if not authenticated - return [ - "Standard_B1s", - "Standard_B2s", - "Standard_D2s_v3", - "Standard_D4s_v3", - "Standard_F2s_v2", - "Standard_E2s_v3", - "Standard_E4s_v3", - ] - - try: - # Use specified region or current region - query_region = region or self.region - - # Get VM sizes for the region - vm_sizes = self.compute_client.virtual_machine_sizes.list(query_region) - - # Extract VM size names and filter to common families - all_sizes = [size.name for size in vm_sizes] - - # Filter to common VM families for better UX - common_families = [ - "Standard_B", - "Standard_D", - "Standard_E", - "Standard_F", - "Standard_A", - ] - filtered_sizes = [] - - for family in common_families: - family_sizes = [s for s in all_sizes if s.startswith(family)] - # Sort by size (1s, 2s, 4s, etc.) - family_sizes.sort( - key=lambda x: ( - int(x.split("_")[1][1:].split("s")[0]) - if "s" in x.split("_")[1] - and x.split("_")[1][1:].split("s")[0].isdigit() - else 999 - ) - ) - filtered_sizes.extend(family_sizes[:8]) # Limit to 8 per family - - return filtered_sizes[:30] # Limit total to 30 for better UX - - except Exception as e: - logger.warning(f"Failed to fetch VM sizes for region {query_region}: {e}") - # Return default list on error - return [ - "Standard_B1s", - "Standard_B2s", - "Standard_D2s_v3", - "Standard_D4s_v3", - "Standard_F2s_v2", - "Standard_E2s_v3", - "Standard_E4s_v3", - ] - - def get_available_regions(self) -> List[str]: - """Get available Azure regions.""" - if not self.authenticated: - # Return common regions if not authenticated - return [ - "eastus", - "westus2", - "northeurope", - "westeurope", - "centralus", - "southeastasia", - "japaneast", - "australiaeast", - ] - - try: - # Get all available regions where VMs can be deployed - subscription_client = self.resource_client - locations = subscription_client.subscriptions.list_locations( - self.subscription_id - ) - - region_names = [loc.name for loc in locations] - region_names.sort() - - # Prioritize common regions - priority_regions = [ - "eastus", - "eastus2", - "westus", - "westus2", - "centralus", - "northeurope", - "westeurope", - "uksouth", - "ukwest", - "southeastasia", - "eastasia", - "japaneast", - "australiaeast", - ] - - # Put priority regions first, then others - sorted_regions = [] - for region in priority_regions: - if region in region_names: - sorted_regions.append(region) - region_names.remove(region) - - sorted_regions.extend(region_names) - return sorted_regions - - except Exception as e: - logger.warning(f"Failed to fetch Azure regions: {e}") - return [ - "eastus", - "westus2", - "northeurope", - "westeurope", - "centralus", - "southeastasia", - "japaneast", - "australiaeast", - ] - - -# Register the provider -if AZURE_AVAILABLE: - PROVIDERS["azure"] = AzureProvider diff --git a/clustrix/cloud_providers/base.py b/clustrix/cloud_providers/base.py deleted file mode 100644 index c2abfc73..00000000 --- a/clustrix/cloud_providers/base.py +++ /dev/null @@ -1,171 +0,0 @@ -"""Base class for cloud provider integrations.""" - -from abc import ABC, abstractmethod -from typing import Dict, Any, List, Optional -import logging - -logger = logging.getLogger(__name__) - - -class CloudProvider(ABC): - """Abstract base class for cloud provider integrations.""" - - def __init__(self): - """Initialize the cloud provider.""" - self.authenticated = False - self.credentials = {} - - def get_credentials_from_manager(self, provider: str) -> Optional[Dict[str, Any]]: - """ - Get credentials for this provider from the FlexibleCredentialManager. - - Args: - provider: Provider name (aws, azure, gcp, etc.) - - Returns: - Dictionary of credentials or None if not found - """ - try: - from ..credential_manager import get_credential_manager - - credential_manager = get_credential_manager() - credentials = credential_manager.ensure_credential(provider) - - if credentials: - logger.debug( - f"Retrieved {provider} credentials from credential manager" - ) - return credentials - else: - logger.debug(f"No {provider} credentials found in credential manager") - return None - - except Exception as e: - logger.debug(f"Failed to get {provider} credentials from manager: {e}") - return None - - @abstractmethod - def authenticate(self, **credentials) -> bool: - """ - Authenticate with the cloud provider. - - Args: - **credentials: Provider-specific credentials - - Returns: - bool: True if authentication successful - """ - pass - - @abstractmethod - def validate_credentials(self) -> bool: - """ - Validate that current credentials are valid. - - Returns: - bool: True if credentials are valid - """ - pass - - @abstractmethod - def create_cluster(self, cluster_name: str, **kwargs) -> Dict[str, Any]: - """ - Create a new cluster. - - Args: - cluster_name: Name for the cluster - **kwargs: Provider-specific cluster configuration - - Returns: - Dict containing cluster information - """ - pass - - @abstractmethod - def delete_cluster(self, cluster_identifier: str) -> bool: - """ - Delete a cluster. - - Args: - cluster_identifier: Cluster name or ID - - Returns: - bool: True if deletion successful - """ - pass - - @abstractmethod - def get_cluster_status(self, cluster_identifier: str) -> Dict[str, Any]: - """ - Get current status of a cluster. - - Args: - cluster_identifier: Cluster name or ID - - Returns: - Dict containing cluster status information - """ - pass - - @abstractmethod - def list_clusters(self) -> List[Dict[str, Any]]: - """ - List all clusters for the authenticated account. - - Returns: - List of cluster information dictionaries - """ - pass - - @abstractmethod - def get_cluster_config(self, cluster_identifier: str) -> Dict[str, Any]: - """ - Get Clustrix configuration for connecting to a cluster. - - Args: - cluster_identifier: Cluster name or ID - - Returns: - Dict containing Clustrix configuration - """ - pass - - @abstractmethod - def estimate_cost(self, **kwargs) -> Dict[str, float]: - """ - Estimate cost for given configuration. - - Args: - **kwargs: Provider-specific configuration - - Returns: - Dict with cost breakdown - """ - pass - - @abstractmethod - def get_available_instance_types(self, region: Optional[str] = None) -> List[str]: - """ - Get list of available instance types for the provider. - - Args: - region: Optional region to filter instance types - - Returns: - List of available instance type names - """ - pass - - @abstractmethod - def get_available_regions(self) -> List[str]: - """ - Get list of available regions for the provider. - - Returns: - List of available region names - """ - pass - - def is_authenticated(self) -> bool: - """Check if provider is authenticated.""" - return self.authenticated diff --git a/clustrix/cloud_providers/gcp.py b/clustrix/cloud_providers/gcp.py deleted file mode 100644 index 34354c88..00000000 --- a/clustrix/cloud_providers/gcp.py +++ /dev/null @@ -1,759 +0,0 @@ -"""Google Cloud Platform provider integration for Clustrix.""" - -import json -import logging -from typing import Dict, Any, List, Optional -from datetime import datetime, timezone - -try: - from google.cloud import compute_v1 - from google.cloud import container_v1 - from google.oauth2 import service_account - from google.auth.exceptions import DefaultCredentialsError - - GCP_AVAILABLE = True -except ImportError: - GCP_AVAILABLE = False - compute_v1 = None # type: ignore - container_v1 = None # type: ignore - service_account = None # type: ignore - DefaultCredentialsError = Exception # type: ignore - -from .base import CloudProvider -from . import PROVIDERS - -logger = logging.getLogger(__name__) - - -class GCPProvider(CloudProvider): - """Google Cloud Platform provider implementation.""" - - def __init__(self): - """Initialize GCP provider.""" - super().__init__() - self.project_id = None - self.region = "us-central1" - self.zone = "us-central1-a" - self.compute_client = None - self.container_client = None - self.service_account_info = None - - def authenticate(self, **credentials) -> bool: - """ - Authenticate with Google Cloud. - - Args: - **credentials: GCP credentials including: - - project_id: GCP project ID - - service_account_key: Service account JSON key (as string) - - region: GCP region (default: us-central1) - - Returns: - bool: True if authentication successful - """ - # First try provided credentials - project_id = credentials.get("project_id") - service_account_key = credentials.get("service_account_key") - region = credentials.get("region", "us-central1") - - # If credentials not provided, try to get from FlexibleCredentialManager - if not all([project_id, service_account_key]): - logger.debug( - "Incomplete GCP credentials provided, trying credential manager..." - ) - manager_creds = self.get_credentials_from_manager("gcp") - if manager_creds: - project_id = project_id or manager_creds.get("project_id") - service_account_key = ( - service_account_key - or manager_creds.get("service_account_json") - or manager_creds.get("service_account_key") - ) - logger.info("Using GCP credentials from credential manager") - - if not all([project_id, service_account_key]): - logger.error("project_id and service_account_key are required") - return False - - if not GCP_AVAILABLE: - logger.error( - "google-cloud-compute is not installed. Install with: pip install google-cloud-compute" - ) - return False - - try: - # Parse service account key JSON - if isinstance(service_account_key, str): - try: - self.service_account_info = json.loads(service_account_key) - except json.JSONDecodeError: - logger.error("Invalid service account key JSON format") - return False - else: - self.service_account_info = service_account_key - - # Create credentials from service account info - creds = service_account.Credentials.from_service_account_info( - self.service_account_info - ) - - # Initialize compute client - self.compute_client = compute_v1.InstancesClient(credentials=creds) - - # Initialize container client - self.container_client = container_v1.ClusterManagerClient(credentials=creds) - - # Test credentials by making a simple API call - # List instances to verify access (this should work even if no instances exist) - try: - self.compute_client.list(project=project_id, zone=f"{region}-a") - except Exception as e: - logger.error(f"Failed to verify GCP credentials: {e}") - return False - - self.project_id = project_id - self.region = region - self.zone = f"{region}-a" # Default to first zone in region - self.credentials = credentials - self.authenticated = True - logger.info(f"Successfully authenticated with GCP project {project_id}") - return True - - except DefaultCredentialsError: - logger.error("Invalid GCP service account credentials") - return False - except Exception as e: - logger.error(f"Unexpected error during GCP authentication: {e}") - return False - - def validate_credentials(self) -> bool: - """Validate current GCP credentials.""" - if not self.authenticated or not self.compute_client: - return False - - try: - # Try to list instances to verify credentials are still valid - self.compute_client.list(project=self.project_id, zone=self.zone) - return True - except Exception: - return False - - def create_compute_instance( - self, - instance_name: str, - machine_type: str = "e2-medium", - image_family: str = "ubuntu-2004-lts", - image_project: str = "ubuntu-os-cloud", - ) -> Dict[str, Any]: - """ - Create a Compute Engine instance. - - Args: - instance_name: Name for the instance - machine_type: Machine type (e.g., e2-medium) - image_family: Image family to use - image_project: Project containing the image - - Returns: - Dict with instance information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with GCP") - - try: - # Get the latest image from the family - images_client = compute_v1.ImagesClient( - credentials=service_account.Credentials.from_service_account_info( - self.service_account_info - ) - ) - image = images_client.get_from_family( - project=image_project, family=image_family - ) - - # Instance configuration - machine_type_url = f"zones/{self.zone}/machineTypes/{machine_type}" - - instance_config = { - "name": instance_name, - "machine_type": machine_type_url, - "disks": [ - { - "boot": True, - "auto_delete": True, - "initialize_params": { - "source_image": image.self_link, - "disk_size_gb": "20", - }, - } - ], - "network_interfaces": [ - { - "network": "global/networks/default", - "access_configs": [ - {"type": "ONE_TO_ONE_NAT", "name": "External NAT"} - ], - } - ], - "tags": {"items": ["clustrix-managed", "http-server", "https-server"]}, - "metadata": { - "items": [ - { - "key": "startup-script", - "value": ( - "#!/bin/bash\n# Clustrix instance setup\n" - "sudo apt-get update\nsudo apt-get install -y python3 python3-pip\n" - ), - } - ] - }, - } - - # Create the instance - operation = self.compute_client.insert( - project=self.project_id, - zone=self.zone, - instance_resource=instance_config, - ) - - # Wait for operation to complete (simplified) - logger.info( - f"Creating GCP instance '{instance_name}' - operation: {operation.name}" - ) - - return { - "instance_name": instance_name, - "instance_id": instance_name, # In GCP, name is the ID - "machine_type": machine_type, - "zone": self.zone, - "region": self.region, - "status": "creating", - "operation": operation.name, - "created_at": datetime.now(timezone.utc).isoformat(), - } - - except Exception as e: - logger.error(f"Failed to create GCP instance: {e}") - raise - - def create_gke_cluster( - self, - cluster_name: str, - node_count: int = 3, - machine_type: str = "e2-medium", - kubernetes_version: Optional[str] = None, - disk_size_gb: int = 100, - **kwargs, - ) -> Dict[str, Any]: - """ - Create a GKE (Google Kubernetes Engine) cluster. - - Args: - cluster_name: Name for the GKE cluster - node_count: Number of nodes in the default node pool - machine_type: Machine type for nodes (default: e2-medium) - kubernetes_version: Kubernetes version (uses default if None) - disk_size_gb: Boot disk size in GB for nodes - **kwargs: Additional cluster configuration - - Returns: - Dict containing cluster information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with GCP") - - try: - logger.info(f"Creating GKE cluster '{cluster_name}' in {self.region}...") - - # Set up cluster location (zone or region) - location = self.zone # Using zonal cluster for simplicity - - # Define GKE cluster configuration - cluster_config = { - "name": cluster_name, - "description": "GKE cluster created by Clustrix", - "initial_node_count": node_count, - "node_config": { - "machine_type": machine_type, - "disk_size_gb": disk_size_gb, - "oauth_scopes": [ - "https://www.googleapis.com/auth/devstorage.read_only", - "https://www.googleapis.com/auth/logging.write", - "https://www.googleapis.com/auth/monitoring", - "https://www.googleapis.com/auth/service.management.readonly", - "https://www.googleapis.com/auth/servicecontrol", - "https://www.googleapis.com/auth/trace.append", - ], - "labels": { - "created_by": "clustrix", - "cluster_name": cluster_name.replace("_", "-"), - }, - }, - "master_auth": { - "client_certificate_config": {"issue_client_certificate": False} - }, - "ip_allocation_policy": {"use_ip_aliases": True}, - "network_policy": {"enabled": False}, - "addons_config": { - "http_load_balancing": {"disabled": False}, - "horizontal_pod_autoscaling": {"disabled": False}, - }, - } - - if kubernetes_version: - cluster_config["initial_cluster_version"] = kubernetes_version - - # Create cluster using the Container API - parent = f"projects/{self.project_id}/locations/{location}" - request = container_v1.CreateClusterRequest( - parent=parent, - cluster=cluster_config, - ) - - operation = self.container_client.create_cluster(request=request) - - logger.info(f"GKE cluster creation initiated - operation: {operation.name}") - - return { - "cluster_name": cluster_name, - "status": "creating", - "region": self.region, - "zone": self.zone, - "provider": "gcp", - "cluster_type": "gke", - "project_id": self.project_id, - "location": location, - "node_count": node_count, - "machine_type": machine_type, - "disk_size_gb": disk_size_gb, - "kubernetes_version": kubernetes_version, - "operation_name": operation.name, - "created_at": datetime.now(timezone.utc).isoformat(), - } - - except Exception as e: - logger.error(f"Failed to create GKE cluster: {e}") - raise - - def create_cluster( - self, cluster_name: str, cluster_type: str = "compute", **kwargs - ) -> Dict[str, Any]: - """Create a GCP cluster (Compute Engine VM or GKE).""" - if cluster_type == "compute": - return self.create_compute_instance(cluster_name, **kwargs) - elif cluster_type == "gke": - return self.create_gke_cluster(cluster_name, **kwargs) - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - def delete_cluster( - self, cluster_identifier: str, cluster_type: str = "compute" - ) -> bool: - """Delete a GCP cluster.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with GCP") - - try: - if cluster_type == "compute": - # Delete Compute Engine instance - operation = self.compute_client.delete( - project=self.project_id, zone=self.zone, instance=cluster_identifier - ) - logger.info( - f"Deleting GCP instance '{cluster_identifier}' - operation: {operation.name}" - ) - return True - elif cluster_type == "gke": - # Delete GKE cluster - location = self.zone # Using same location as creation - name = f"projects/{self.project_id}/locations/{location}/clusters/{cluster_identifier}" - - request = container_v1.DeleteClusterRequest(name=name) - operation = self.container_client.delete_cluster(request=request) - - logger.info( - f"Deleting GKE cluster '{cluster_identifier}' - operation: {operation.name}" - ) - return True - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - except Exception as e: - logger.error(f"Failed to delete GCP cluster: {e}") - return False - - def get_cluster_status( - self, cluster_identifier: str, cluster_type: str = "compute" - ) -> Dict[str, Any]: - """Get status of a GCP cluster.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with GCP") - - try: - if cluster_type == "compute": - instance = self.compute_client.get( - project=self.project_id, zone=self.zone, instance=cluster_identifier - ) - return { - "instance_name": cluster_identifier, - "status": instance.status.lower(), - "machine_type": instance.machine_type.split("/")[-1], - "zone": self.zone, - "provider": "gcp", - "cluster_type": "compute", - } - elif cluster_type == "gke": - # Get GKE cluster status - location = self.zone # Using same location as creation - name = f"projects/{self.project_id}/locations/{location}/clusters/{cluster_identifier}" - - request = container_v1.GetClusterRequest(name=name) - cluster = self.container_client.get_cluster(request=request) - - return { - "cluster_name": cluster_identifier, - "status": cluster.status.name.lower(), - "endpoint": cluster.endpoint, - "current_master_version": cluster.current_master_version, - "current_node_version": cluster.current_node_version, - "node_count": cluster.current_node_count, - "location": cluster.location, - "zone": cluster.zone if cluster.zone else None, - "create_time": cluster.create_time, - "provider": "gcp", - "cluster_type": "gke", - "project_id": self.project_id, - } - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - except Exception as e: - logger.error(f"Failed to get cluster status: {e}") - raise - - def list_clusters(self) -> List[Dict[str, Any]]: - """List all GCP clusters.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with GCP") - - clusters = [] - - try: - # List Compute Engine instances with Clustrix tag - instances = self.compute_client.list( - project=self.project_id, zone=self.zone - ) - - for instance in instances: - # Check if instance has clustrix-managed tag - tags = getattr(instance, "tags", {}) - tag_items = getattr(tags, "items", []) - - if "clustrix-managed" in tag_items: - clusters.append( - { - "name": instance.name, - "instance_id": instance.name, - "type": "compute", - "status": instance.status.lower(), - "zone": self.zone, - "machine_type": ( - instance.machine_type.split("/")[-1] - if instance.machine_type - else "unknown" - ), - } - ) - - except Exception as e: - logger.error(f"Failed to list GCP instances: {e}") - - # List GKE clusters - try: - location = self.zone # List clusters in our zone - parent = f"projects/{self.project_id}/locations/{location}" - request = container_v1.ListClustersRequest(parent=parent) - - response = self.container_client.list_clusters(request=request) - - for cluster in response.clusters: - # Only include clusters with clustrix labels - labels = cluster.resource_labels or {} - if labels.get("created_by") == "clustrix": - clusters.append( - { - "name": cluster.name, - "cluster_id": cluster.name, - "type": "gke", - "status": cluster.status.name.lower(), - "endpoint": cluster.endpoint, - "current_master_version": cluster.current_master_version, - "node_count": cluster.current_node_count, - "location": cluster.location, - "zone": cluster.zone if cluster.zone else None, - "project_id": self.project_id, - } - ) - except Exception as e: - logger.error(f"Failed to list GKE clusters: {e}") - - return clusters - - def get_cluster_config( - self, cluster_identifier: str, cluster_type: str = "compute" - ) -> Dict[str, Any]: - """Get Clustrix configuration for a GCP cluster.""" - if cluster_type == "compute": - # Get instance details - try: - instance = self.compute_client.get( - project=self.project_id, zone=self.zone, instance=cluster_identifier - ) - - # Get external IP. An instance with no external address is - # not reachable over SSH, and returning an empty (or invented) - # host only moves the failure to a confusing SSH timeout. - external_ip = "" - for interface in instance.network_interfaces: - for access_config in interface.access_configs: - if access_config.nat_i_p: - external_ip = access_config.nat_i_p - break - - if not external_ip: - raise RuntimeError( - f"GCP instance '{cluster_identifier}' in zone " - f"'{self.zone}' has no external IP address, so there " - "is no host to connect to." - ) - - return { - "name": f"GCP Compute - {cluster_identifier}", - "cluster_type": "ssh", - "cluster_host": external_ip, - "username": "ubuntu", # Default for Ubuntu images - "cluster_port": 22, - "default_cores": 2, # Would need to map machine type to cores - "default_memory": "4GB", # Would need to map machine type to memory - "remote_work_dir": "/home/ubuntu/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - "provider": "gcp", - "provider_config": { - "instance_name": cluster_identifier, - "zone": self.zone, - "project_id": self.project_id, - }, - } - except Exception as e: - # This used to return cluster_host "placeholder.gcp.com". - # Nothing downstream could tell that apart from a real host, - # so the failure surfaced as an SSH error against a domain - # that does not exist, far from its cause (#119). - raise RuntimeError( - f"Could not determine the connection details of GCP " - f"instance '{cluster_identifier}' in zone '{self.zone}' " - f"(project '{self.project_id}'): {e}" - ) from e - elif cluster_type == "gke": - return { - "name": f"GCP GKE - {cluster_identifier}", - "cluster_type": "kubernetes", - "cluster_host": f"{cluster_identifier}.gke.{self.region}.gcp.com", - "cluster_port": 443, - "k8s_namespace": "default", - "k8s_image": "python:3.11", - "default_cores": 2, - "default_memory": "4GB", - "cost_monitoring": True, - "provider": "gcp", - "provider_config": { - "cluster_name": cluster_identifier, - "region": self.region, - "project_id": self.project_id, - }, - } - else: - raise ValueError(f"Unknown cluster type: {cluster_type}") - - def estimate_cost(self, **kwargs) -> Dict[str, float]: - """Estimate GCP costs.""" - cluster_type = kwargs.get("cluster_type", "compute") - machine_type = kwargs.get("machine_type", "e2-medium") - hours = kwargs.get("hours", 1) - - # Simplified pricing - real implementation would use GCP Pricing API - instance_prices = { - "e2-micro": 0.0056, - "e2-small": 0.0112, - "e2-medium": 0.0225, - "e2-standard-2": 0.0450, - "e2-standard-4": 0.0900, - "n1-standard-1": 0.0475, - "n1-standard-2": 0.0950, - "n1-standard-4": 0.1900, - "c2-standard-4": 0.1892, - "c2-standard-8": 0.3784, - } - - base_price = instance_prices.get(machine_type, 0.05) # Default price - - if cluster_type == "gke": - # GKE has cluster management fee - cluster_fee = 0.10 * hours # $0.10/hour cluster management fee - node_cost = base_price * hours - total = cluster_fee + node_cost - - return { - "cluster_management": cluster_fee, - "nodes": node_cost, - "total": total, - } - else: # compute - total = base_price * hours - return {"instance": total, "total": total} - - def get_available_instance_types(self, region: Optional[str] = None) -> List[str]: - """Get available GCP machine types.""" - if not self.authenticated: - # Return common machine types if not authenticated - return [ - "e2-micro", - "e2-small", - "e2-medium", - "e2-standard-2", - "e2-standard-4", - "n1-standard-1", - "n1-standard-2", - "n1-standard-4", - "n2-standard-2", - "n2-standard-4", - "c2-standard-4", - ] - - try: - # Use specified region or current region - query_region = region or self.region - zone = f"{query_region}-a" # Use first zone in region - - # Get machine types for the zone - machine_types_client = compute_v1.MachineTypesClient( - credentials=service_account.Credentials.from_service_account_info( - self.service_account_info - ) - ) - - machine_types = machine_types_client.list( - project=self.project_id, zone=zone - ) - - # Extract machine type names and filter to common families - all_types = [mt.name for mt in machine_types] - - # Filter to common machine families for better UX - common_families = ["e2", "n1", "n2", "c2", "f1", "g1"] - filtered_types = [] - - for family in common_families: - family_types = [t for t in all_types if t.startswith(family + "-")] - # Sort by size (micro, small, medium, standard-1, standard-2, etc.) - family_types.sort( - key=lambda x: ( - "micro" in x - and 0 - or "small" in x - and 1 - or "medium" in x - and 2 - or "standard" in x - and int(x.split("-")[-1]) - if x.split("-")[-1].isdigit() - else 99 - ) - ) - filtered_types.extend(family_types[:8]) # Limit to 8 per family - - return filtered_types[:30] # Limit total to 30 for better UX - - except Exception as e: - logger.warning( - f"Failed to fetch machine types for region {query_region}: {e}" - ) - # Return default list on error - return [ - "e2-micro", - "e2-small", - "e2-medium", - "e2-standard-2", - "n1-standard-1", - "n1-standard-2", - "n1-standard-4", - "c2-standard-4", - "c2-standard-8", - ] - - def get_available_regions(self) -> List[str]: - """Get available GCP regions.""" - if not self.authenticated: - # Return common regions if not authenticated - return [ - "us-central1", - "us-east1", - "us-west1", - "us-west2", - "europe-west1", - "europe-west2", - "asia-southeast1", - "asia-northeast1", - ] - - try: - # Get all available regions - regions_client = compute_v1.RegionsClient( - credentials=service_account.Credentials.from_service_account_info( - self.service_account_info - ) - ) - - regions = regions_client.list(project=self.project_id) - region_names = [region.name for region in regions] - region_names.sort() - - # Prioritize common regions - priority_regions = [ - "us-central1", - "us-east1", - "us-west1", - "us-west2", - "europe-west1", - "europe-west2", - "asia-southeast1", - "asia-northeast1", - ] - - # Put priority regions first, then others - sorted_regions = [] - for region in priority_regions: - if region in region_names: - sorted_regions.append(region) - region_names.remove(region) - - sorted_regions.extend(region_names) - return sorted_regions - - except Exception as e: - logger.warning(f"Failed to fetch GCP regions: {e}") - return [ - "us-central1", - "us-east1", - "us-west1", - "us-west2", - "europe-west1", - "europe-west2", - "asia-southeast1", - "asia-northeast1", - ] - - -# Register the provider -if GCP_AVAILABLE: - PROVIDERS["gcp"] = GCPProvider diff --git a/clustrix/cloud_providers/huggingface_spaces.py b/clustrix/cloud_providers/huggingface_spaces.py deleted file mode 100644 index a5f2cf0f..00000000 --- a/clustrix/cloud_providers/huggingface_spaces.py +++ /dev/null @@ -1,402 +0,0 @@ -"""HuggingFace Spaces provider integration for Clustrix.""" - -import logging -from typing import Dict, Any, List, Optional -from datetime import datetime, timezone - -try: - from huggingface_hub import HfApi, SpaceHardware - from huggingface_hub.utils import HfHubHTTPError - - HF_AVAILABLE = True -except ImportError: - HF_AVAILABLE = False - HfApi = None # type: ignore - SpaceHardware = None # type: ignore - HfHubHTTPError = Exception # type: ignore - -from .base import CloudProvider -from . import PROVIDERS - -logger = logging.getLogger(__name__) - - -class HuggingFaceSpacesProvider(CloudProvider): - """HuggingFace Spaces provider implementation.""" - - def __init__(self): - """Initialize HuggingFace Spaces provider.""" - super().__init__() - self.api_token = None - self.username = None - self.api = None - - def authenticate(self, **credentials) -> bool: - """ - Authenticate with HuggingFace Hub. - - Args: - **credentials: HuggingFace credentials including: - - token: HuggingFace API token - - username: HuggingFace username - - Returns: - bool: True if authentication successful - """ - token = credentials.get("token") - username = credentials.get("username") - - if not token: - logger.error("token is required") - return False - - if not username: - logger.error("username is required for HuggingFace Spaces") - return False - - if not HF_AVAILABLE: - logger.error( - "huggingface_hub is not installed. Install with: pip install huggingface_hub" - ) - return False - - try: - # Create HuggingFace API client - self.api = HfApi(token=token) - - # Test credentials by getting user info - user_info = self.api.whoami() - - if user_info and user_info.get("name") == username: - self.api_token = token - self.username = username - self.credentials = credentials - self.authenticated = True - logger.info( - f"Successfully authenticated with HuggingFace as {username}" - ) - return True - else: - logger.error("Invalid HuggingFace credentials or username mismatch") - return False - - except HfHubHTTPError as e: - logger.error(f"HuggingFace authentication failed: {e}") - return False - except Exception as e: - logger.error(f"Unexpected error during HuggingFace authentication: {e}") - return False - - def validate_credentials(self) -> bool: - """Validate current HuggingFace credentials.""" - if not self.authenticated or not self.api: - return False - - try: - user_info = self.api.whoami() - return user_info is not None - except Exception: - return False - - def create_space( - self, - space_name: str, - hardware: str = "cpu-basic", - sdk: str = "gradio", - private: bool = False, - ) -> Dict[str, Any]: - """ - Create a HuggingFace Space. - - Args: - space_name: Name for the space - hardware: Hardware tier (cpu-basic, cpu-upgrade, t4-small, t4-medium, a10g-small, etc.) - sdk: SDK type (gradio, streamlit, docker) - private: Whether the space should be private - - Returns: - Dict with space information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with HuggingFace") - - try: - # Create the space - space_id = f"{self.username}/{space_name}" - - # Create space with basic configuration - space_url = self.api.create_repo( - repo_id=space_id, repo_type="space", space_sdk=sdk, private=private - ) - - # Set hardware if not cpu-basic - if hardware != "cpu-basic": - try: - # Map hardware string to SpaceHardware enum - hardware_map = { - "cpu-upgrade": SpaceHardware.CPU_UPGRADE, - "t4-small": SpaceHardware.T4_SMALL, - "t4-medium": SpaceHardware.T4_MEDIUM, - "a10g-small": SpaceHardware.A10G_SMALL, - "a10g-large": SpaceHardware.A10G_LARGE, - "a100-large": SpaceHardware.A100_LARGE, - } - - if hardware in hardware_map: - self.api.request_space_hardware( - repo_id=space_id, hardware=hardware_map[hardware] - ) - logger.info( - f"Requested {hardware} hardware for space {space_id}" - ) - else: - logger.warning( - f"Unknown hardware type: {hardware}, using cpu-basic" - ) - hardware = "cpu-basic" - - except Exception as e: - logger.warning(f"Failed to set hardware for space: {e}") - hardware = "cpu-basic" - - logger.info(f"Created HuggingFace Space '{space_id}' with {sdk} SDK") - - return { - "space_name": space_name, - "space_id": space_id, - "space_url": space_url, - "sdk": sdk, - "hardware": hardware, - "private": private, - "status": "creating", - "created_at": datetime.now(timezone.utc).isoformat(), - } - - except HfHubHTTPError as e: - logger.error(f"Failed to create HuggingFace Space: {e}") - raise - except Exception as e: - logger.error(f"Unexpected error creating HuggingFace Space: {e}") - raise - - def create_cluster(self, cluster_name: str, **kwargs) -> Dict[str, Any]: - """Create a HuggingFace Space.""" - return self.create_space(cluster_name, **kwargs) - - def delete_cluster(self, cluster_identifier: str) -> bool: - """Delete a HuggingFace Space.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with HuggingFace") - - try: - # Delete the space repository - self.api.delete_repo(repo_id=cluster_identifier, repo_type="space") - - logger.info(f"Deleted HuggingFace Space '{cluster_identifier}'") - return True - - except HfHubHTTPError as e: - logger.error(f"Failed to delete HuggingFace Space: {e}") - return False - except Exception as e: - logger.error(f"Unexpected error deleting HuggingFace Space: {e}") - return False - - def get_cluster_status(self, cluster_identifier: str) -> Dict[str, Any]: - """Get status of a HuggingFace Space.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with HuggingFace") - - try: - # Get space info - space_info = self.api.space_info(cluster_identifier) - - # Get space runtime info - try: - runtime = self.api.get_space_runtime(cluster_identifier) - stage = runtime.stage if runtime else "unknown" - hardware = runtime.hardware if runtime else "unknown" - except Exception: - stage = "unknown" - hardware = "unknown" - - return { - "space_id": cluster_identifier, - "status": stage.lower() if stage != "unknown" else "unknown", - "hardware": hardware, - "sdk": space_info.sdk if space_info else "unknown", - "provider": "huggingface", - "cluster_type": "spaces", - } - - except HfHubHTTPError as e: - if "404" in str(e): - return { - "space_id": cluster_identifier, - "status": "not_found", - "provider": "huggingface", - } - logger.error(f"Failed to get HuggingFace Space status: {e}") - raise - except Exception as e: - logger.error(f"Unexpected error getting HuggingFace Space status: {e}") - raise - - def list_clusters(self) -> List[Dict[str, Any]]: - """List all HuggingFace Spaces.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with HuggingFace") - - try: - # List user spaces - spaces = self.api.list_spaces(author=self.username) - - clusters = [] - for space in spaces: - # Get runtime info for each space - try: - runtime = self.api.get_space_runtime(space.id) - stage = runtime.stage if runtime else "unknown" - hardware = runtime.hardware if runtime else "unknown" - except Exception: - stage = "unknown" - hardware = "unknown" - - clusters.append( - { - "name": space.id.split("/")[ - -1 - ], # Get space name without username - "space_id": space.id, - "type": "space", - "status": stage.lower() if stage != "unknown" else "unknown", - "sdk": space.sdk, - "hardware": hardware, - "private": space.private, - } - ) - - return clusters - - except HfHubHTTPError as e: - logger.error(f"Failed to list HuggingFace Spaces: {e}") - return [] - except Exception as e: - logger.error(f"Unexpected error listing HuggingFace Spaces: {e}") - return [] - - def get_cluster_config(self, cluster_identifier: str) -> Dict[str, Any]: - """Get Clustrix configuration for a HuggingFace Space.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with HuggingFace") - - try: - # Get space info - space_info = self.api.space_info(cluster_identifier) - - # Get space runtime info - try: - runtime = self.api.get_space_runtime(cluster_identifier) - hardware = runtime.hardware if runtime else "cpu-basic" - except Exception: - hardware = "cpu-basic" - - # Generate space URL - space_url = f"https://huggingface.co/spaces/{cluster_identifier}" - - return { - "name": f"HuggingFace Space - {cluster_identifier}", - "cluster_type": "api", # Spaces are accessed via HTTP API - "cluster_host": space_url, - "api_endpoint": f"{space_url}/api/predict", - "default_cores": self._hardware_to_cores(hardware), - "default_memory": self._hardware_to_memory(hardware), - "cost_monitoring": True, - "provider": "huggingface", - "provider_config": { - "space_id": cluster_identifier, - "hardware": hardware, - "sdk": space_info.sdk if space_info else "unknown", - "api_token": "***", # Don't expose token - }, - } - - except Exception as e: - logger.error(f"Failed to get HuggingFace Space config: {e}") - # Return basic config on error - return { - "name": f"HuggingFace Space - {cluster_identifier}", - "cluster_type": "api", - "cluster_host": f"https://huggingface.co/spaces/{cluster_identifier}", - "provider": "huggingface", - } - - def estimate_cost(self, **kwargs) -> Dict[str, float]: - """Estimate HuggingFace Spaces costs.""" - hardware = kwargs.get("hardware", "cpu-basic") - hours = kwargs.get("hours", 1) - - # HuggingFace Spaces pricing (as of 2024) - prices per hour - hardware_prices = { - "cpu-basic": 0.0, # Free tier - "cpu-upgrade": 0.03, # $0.03/hour - "t4-small": 0.60, # $0.60/hour - "t4-medium": 0.90, # $0.90/hour - "a10g-small": 1.05, # $1.05/hour - "a10g-large": 3.15, # $3.15/hour - "a100-large": 4.13, # $4.13/hour - } - - base_price = hardware_prices.get(hardware, 0.0) # Default to free - total = base_price * hours - - return {"compute": total, "total": total} - - def get_available_instance_types(self, region: Optional[str] = None) -> List[str]: - """Get available HuggingFace Spaces hardware options.""" - # HuggingFace Spaces hardware options - return [ - "cpu-basic", - "cpu-upgrade", - "t4-small", - "t4-medium", - "a10g-small", - "a10g-large", - "a100-large", - ] - - def get_available_regions(self) -> List[str]: - """Get available HuggingFace Spaces regions.""" - # HuggingFace Spaces are globally available - return ["global"] - - def _hardware_to_cores(self, hardware: str) -> int: - """Map hardware type to CPU cores.""" - hardware_cores = { - "cpu-basic": 2, - "cpu-upgrade": 8, - "t4-small": 4, - "t4-medium": 8, - "a10g-small": 4, - "a10g-large": 12, - "a100-large": 12, - } - return hardware_cores.get(hardware, 2) - - def _hardware_to_memory(self, hardware: str) -> str: - """Map hardware type to memory.""" - hardware_memory = { - "cpu-basic": "16GB", - "cpu-upgrade": "32GB", - "t4-small": "15GB", - "t4-medium": "15GB", - "a10g-small": "24GB", - "a10g-large": "96GB", - "a100-large": "142GB", - } - return hardware_memory.get(hardware, "16GB") - - -# Register the provider -if HF_AVAILABLE: - PROVIDERS["huggingface"] = HuggingFaceSpacesProvider diff --git a/clustrix/cloud_providers/lambda_cloud.py b/clustrix/cloud_providers/lambda_cloud.py deleted file mode 100644 index d5e7e416..00000000 --- a/clustrix/cloud_providers/lambda_cloud.py +++ /dev/null @@ -1,495 +0,0 @@ -"""Lambda Cloud provider integration for Clustrix.""" - -import logging -import requests -from typing import Dict, Any, List, Optional -from datetime import datetime, timezone - -from .base import CloudProvider -from . import PROVIDERS - -logger = logging.getLogger(__name__) - - -class LambdaCloudProvider(CloudProvider): - """Lambda Cloud provider implementation.""" - - def __init__(self): - """Initialize Lambda Cloud provider.""" - super().__init__() - self.api_key = None - self.base_url = "https://cloud.lambdalabs.com/api/v1" - self.session = None - - def authenticate(self, **credentials) -> bool: - """ - Authenticate with Lambda Cloud. - - Args: - **credentials: Lambda Cloud credentials including: - - api_key: Lambda Cloud API key - - Returns: - bool: True if authentication successful - """ - api_key = credentials.get("api_key") - - if not api_key: - logger.error("api_key is required") - return False - - try: - # Create session with API key - self.session = requests.Session() - self.session.headers.update( - { - "Authorization": f"Bearer {api_key}", - "Content-Type": "application/json", - } - ) - - # Test credentials by getting account info - response = self.session.get(f"{self.base_url}/instance-types") - - if response.status_code == 200: - self.api_key = api_key - self.credentials = credentials - self.authenticated = True - logger.info("Successfully authenticated with Lambda Cloud") - return True - elif response.status_code == 401: - logger.error("Invalid Lambda Cloud API key") - return False - else: - logger.error( - f"Lambda Cloud authentication failed: {response.status_code}" - ) - return False - - except requests.RequestException as e: - logger.error(f"Failed to connect to Lambda Cloud API: {e}") - return False - except Exception as e: - logger.error(f"Unexpected error during Lambda Cloud authentication: {e}") - return False - - def validate_credentials(self) -> bool: - """Validate current Lambda Cloud credentials.""" - if not self.authenticated or not self.session: - return False - - try: - response = self.session.get(f"{self.base_url}/instance-types") - return response.status_code == 200 - except Exception: - return False - - def create_instance( - self, - instance_name: str, - instance_type: str = "gpu_1x_a10", - region: str = "us-east-1", - ssh_key_name: Optional[str] = None, - ) -> Dict[str, Any]: - """ - Create a Lambda Cloud instance. - - Args: - instance_name: Name for the instance - instance_type: GPU instance type (e.g., gpu_1x_a10) - region: Lambda Cloud region - ssh_key_name: SSH key name for access - - Returns: - Dict with instance information - """ - if not self.authenticated: - raise RuntimeError("Not authenticated with Lambda Cloud") - - try: - # Prepare instance creation request - instance_data = { - "region_name": region, - "instance_type_name": instance_type, - "ssh_key_names": [ssh_key_name] if ssh_key_name else [], - "file_system_names": [], - "quantity": 1, - "name": instance_name, - } - - # Create the instance - response = self.session.post( - f"{self.base_url}/instance-operations/launch", json=instance_data - ) - - if response.status_code == 200: - result = response.json() - instance_ids = result.get("instance_ids", []) - - if instance_ids: - instance_id = instance_ids[0] - logger.info( - f"Created Lambda Cloud instance '{instance_name}' with ID {instance_id}" - ) - - return { - "instance_name": instance_name, - "instance_id": instance_id, - "instance_type": instance_type, - "region": region, - "status": "booting", - "created_at": datetime.now(timezone.utc).isoformat(), - } - else: - raise RuntimeError("No instance ID returned from Lambda Cloud") - else: - error_msg = f"Failed to create instance: {response.status_code}" - if response.headers.get("content-type", "").startswith( - "application/json" - ): - error_data = response.json() - error_msg += f" - {error_data.get('error', 'Unknown error')}" - raise RuntimeError(error_msg) - - except requests.RequestException as e: - logger.error(f"Failed to create Lambda Cloud instance: {e}") - raise - except Exception as e: - logger.error(f"Unexpected error creating Lambda Cloud instance: {e}") - raise - - def create_cluster(self, cluster_name: str, **kwargs) -> Dict[str, Any]: - """Create a Lambda Cloud instance.""" - return self.create_instance(cluster_name, **kwargs) - - def delete_cluster(self, cluster_identifier: str) -> bool: - """Delete a Lambda Cloud instance.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with Lambda Cloud") - - try: - # Terminate the instance - response = self.session.post( - f"{self.base_url}/instance-operations/terminate", - json={"instance_ids": [cluster_identifier]}, - ) - - if response.status_code == 200: - logger.info(f"Terminated Lambda Cloud instance '{cluster_identifier}'") - return True - else: - error_msg = f"Failed to terminate instance: {response.status_code}" - if response.headers.get("content-type", "").startswith( - "application/json" - ): - error_data = response.json() - error_msg += f" - {error_data.get('error', 'Unknown error')}" - logger.error(error_msg) - return False - - except requests.RequestException as e: - logger.error(f"Failed to terminate Lambda Cloud instance: {e}") - return False - except Exception as e: - logger.error(f"Unexpected error terminating Lambda Cloud instance: {e}") - return False - - def get_cluster_status(self, cluster_identifier: str) -> Dict[str, Any]: - """Get status of a Lambda Cloud instance.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with Lambda Cloud") - - try: - # Get instance details - response = self.session.get( - f"{self.base_url}/instances/{cluster_identifier}" - ) - - if response.status_code == 200: - instance_data = response.json() - return { - "instance_id": cluster_identifier, - "status": instance_data.get("status", "unknown").lower(), - "instance_type": instance_data.get("instance_type", {}).get( - "name", "unknown" - ), - "region": instance_data.get("region", {}).get("name", "unknown"), - "provider": "lambda", - "cluster_type": "ssh", - } - elif response.status_code == 404: - return { - "instance_id": cluster_identifier, - "status": "not_found", - "provider": "lambda", - } - else: - raise RuntimeError( - f"Failed to get instance status: {response.status_code}" - ) - - except requests.RequestException as e: - logger.error(f"Failed to get Lambda Cloud instance status: {e}") - raise - except Exception as e: - logger.error(f"Unexpected error getting Lambda Cloud instance status: {e}") - raise - - def list_clusters(self) -> List[Dict[str, Any]]: - """List all Lambda Cloud instances.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with Lambda Cloud") - - try: - # List all instances - response = self.session.get(f"{self.base_url}/instances") - - if response.status_code == 200: - instances_data = response.json() - instances = instances_data.get("data", []) - - clusters = [] - for instance in instances: - clusters.append( - { - "name": instance.get("name", instance.get("id", "unknown")), - "instance_id": instance.get("id"), - "type": "gpu", - "status": instance.get("status", "unknown").lower(), - "instance_type": instance.get("instance_type", {}).get( - "name", "unknown" - ), - "region": instance.get("region", {}).get("name", "unknown"), - } - ) - - return clusters - else: - logger.error(f"Failed to list instances: {response.status_code}") - return [] - - except requests.RequestException as e: - logger.error(f"Failed to list Lambda Cloud instances: {e}") - return [] - except Exception as e: - logger.error(f"Unexpected error listing Lambda Cloud instances: {e}") - return [] - - def get_cluster_config(self, cluster_identifier: str) -> Dict[str, Any]: - """Get Clustrix configuration for a Lambda Cloud instance.""" - if not self.authenticated: - raise RuntimeError("Not authenticated with Lambda Cloud") - - try: - # Get instance details - response = self.session.get( - f"{self.base_url}/instances/{cluster_identifier}" - ) - except Exception as e: - raise RuntimeError( - f"Could not reach Lambda Cloud to look up instance " - f"'{cluster_identifier}': {e}" - ) from e - - if response.status_code == 200: - instance_data = response.json() - - # Get public IP. An instance the API reports without one is - # not reachable, and an empty (or invented) host only moves - # the failure to a confusing SSH timeout later. - public_ip = instance_data.get("ip") - if not public_ip: - raise RuntimeError( - f"Lambda Cloud instance '{cluster_identifier}' has no " - "IP address yet, so there is no host to connect to." - ) - - instance_type = instance_data.get("instance_type", {}).get( - "name", "unknown" - ) - - return { - "name": f"Lambda Cloud - {cluster_identifier}", - "cluster_type": "ssh", - "cluster_host": public_ip, - "username": "ubuntu", # Default for Lambda Cloud instances - "cluster_port": 22, - "default_cores": 8, # Lambda Cloud instances typically have high core counts - "default_memory": "32GB", # GPU instances typically have large memory - "remote_work_dir": "/home/ubuntu/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - "provider": "lambda", - "provider_config": { - "instance_id": cluster_identifier, - "instance_type": instance_type, - "region": instance_data.get("region", {}).get("name", "unknown"), - }, - } - else: - # Both of these used to return cluster_host - # "placeholder.lambdalabs.com". Nothing downstream could tell - # that apart from a real host, so the failure surfaced as an - # SSH error against a domain that does not exist, far from its - # cause (#119). - raise RuntimeError( - f"Lambda Cloud returned HTTP {response.status_code} for " - f"instance '{cluster_identifier}', so its connection " - "details could not be determined." - ) - - def estimate_cost(self, **kwargs) -> Dict[str, float]: - """Estimate Lambda Cloud costs.""" - instance_type = kwargs.get("instance_type", "gpu_1x_a10") - hours = kwargs.get("hours", 1) - - # Lambda Cloud pricing (as of 2024) - prices per hour - instance_prices = { - "gpu_1x_a10": 0.75, - "gpu_1x_a6000": 0.80, - "gpu_1x_h100": 1.99, - "gpu_1x_a100": 1.29, - "gpu_2x_a10": 1.50, - "gpu_2x_a6000": 1.60, - "gpu_2x_a100": 2.58, - "gpu_4x_a10": 3.00, - "gpu_4x_a6000": 3.20, - "gpu_4x_a100": 5.16, - "gpu_8x_a100": 10.32, - "gpu_8x_v100": 4.40, - } - - base_price = instance_prices.get(instance_type, 1.0) # Default price - total = base_price * hours - - return {"gpu_instance": total, "total": total} - - def get_available_instance_types(self, region: Optional[str] = None) -> List[str]: - """Get available Lambda Cloud instance types.""" - if not self.authenticated: - # Return common instance types if not authenticated - return [ - "gpu_1x_a10", - "gpu_1x_a6000", - "gpu_1x_h100", - "gpu_1x_a100", - "gpu_2x_a10", - "gpu_2x_a6000", - "gpu_2x_a100", - "gpu_4x_a10", - "gpu_4x_a6000", - "gpu_4x_a100", - "gpu_8x_a100", - "gpu_8x_v100", - ] - - try: - # Query Lambda Cloud API for available instance types - response = self.session.get(f"{self.base_url}/instance-types") - - if response.status_code == 200: - instance_types_data = response.json() - instance_types = instance_types_data.get("data", []) - - # Extract instance type names - available_types = [] - for instance_type in instance_types: - name = instance_type.get("name") - if name: - available_types.append(name) - - # Sort by GPU count and type for better UX - def sort_key(instance_name): - # Extract GPU count from name (e.g., "gpu_1x_a10" -> 1) - parts = instance_name.split("_") - if len(parts) >= 2 and "x" in parts[1]: - try: - gpu_count = int(parts[1].split("x")[0]) - return gpu_count - except ValueError: - return 999 - return 999 - - available_types.sort(key=sort_key) - return available_types - else: - logger.warning( - f"Failed to fetch instance types: {response.status_code}" - ) - # Return default list on error - return [ - "gpu_1x_a10", - "gpu_1x_a6000", - "gpu_1x_h100", - "gpu_1x_a100", - "gpu_2x_a10", - "gpu_2x_a6000", - "gpu_2x_a100", - "gpu_4x_a10", - "gpu_4x_a6000", - "gpu_4x_a100", - "gpu_8x_a100", - "gpu_8x_v100", - ] - - except Exception as e: - logger.warning(f"Failed to fetch Lambda Cloud instance types: {e}") - # Return default list on error - return [ - "gpu_1x_a10", - "gpu_1x_a6000", - "gpu_1x_h100", - "gpu_1x_a100", - "gpu_2x_a10", - "gpu_2x_a6000", - "gpu_2x_a100", - "gpu_4x_a10", - "gpu_4x_a6000", - "gpu_4x_a100", - "gpu_8x_a100", - "gpu_8x_v100", - ] - - def get_available_regions(self) -> List[str]: - """Get available Lambda Cloud regions.""" - if not self.authenticated: - # Lambda Cloud has limited regions - return ["us-east-1", "us-west-1", "us-west-2"] - - try: - # Lambda Cloud doesn't have a dedicated regions endpoint, - # but we can get regions from instance types - response = self.session.get(f"{self.base_url}/instance-types") - - if response.status_code == 200: - instance_types_data = response.json() - instance_types = instance_types_data.get("data", []) - - # Extract unique regions from instance types - regions = set() - for instance_type in instance_types: - regions_available = instance_type.get( - "regions_with_capacity_available", [] - ) - for region_info in regions_available: - if isinstance(region_info, dict) and "name" in region_info: - regions.add(region_info["name"]) - elif isinstance(region_info, str): - regions.add(region_info) - - if regions: - return sorted(list(regions)) - else: - # Fallback to known regions - return ["us-east-1", "us-west-1", "us-west-2"] - else: - logger.warning(f"Failed to fetch regions: {response.status_code}") - return ["us-east-1", "us-west-1", "us-west-2"] - - except Exception as e: - logger.warning(f"Failed to fetch Lambda Cloud regions: {e}") - return ["us-east-1", "us-west-1", "us-west-2"] - - -# Register the provider -PROVIDERS["lambda"] = LambdaCloudProvider diff --git a/clustrix/cost_monitoring.py b/clustrix/cost_monitoring.py deleted file mode 100644 index 38e8dfe6..00000000 --- a/clustrix/cost_monitoring.py +++ /dev/null @@ -1,407 +0,0 @@ -""" -Cost monitoring functionality for Clustrix across different cloud providers. - -This module provides unified cost tracking, resource utilization monitoring, -and cost optimization recommendations for various cloud platforms. -""" - -import time -import subprocess -import logging -from abc import ABC, abstractmethod -from typing import Dict, List, Any, Optional, Callable -from functools import wraps -from dataclasses import dataclass, asdict -from datetime import datetime - -# Configure logging -logger = logging.getLogger(__name__) - - -@dataclass -class ResourceUsage: - """Resource utilization metrics.""" - - cpu_percent: float - memory_used_mb: int - memory_total_mb: int - memory_percent: float - gpu_stats: Optional[List[Dict[str, Any]]] = None - network_io_mb: Optional[float] = None - disk_io_mb: Optional[float] = None - - -@dataclass -class CostEstimate: - """Cost estimation information.""" - - instance_type: str - hourly_rate: float - hours_used: float - estimated_cost: float - currency: str = "USD" - last_updated: Optional[datetime] = None - pricing_source: str = "api" # "api" or "hardcoded" - pricing_warning: Optional[str] = None - - -@dataclass -class CostReport: - """Comprehensive cost and usage report.""" - - timestamp: datetime - duration_seconds: float - resource_usage: ResourceUsage - cost_estimate: CostEstimate - provider: str - region: Optional[str] = None - recommendations: Optional[List[str]] = None - metadata: Optional[Dict[str, Any]] = None - - -class BaseCostMonitor(ABC): - """Base class for cloud provider cost monitoring.""" - - def __init__(self, provider_name: str): - self.provider_name = provider_name - self.start_time = None - self.monitoring_enabled = True - - @abstractmethod - def get_resource_usage(self) -> ResourceUsage: - """Get current resource utilization metrics.""" - pass - - @abstractmethod - def estimate_cost(self, instance_type: str, hours_used: float) -> CostEstimate: - """Estimate cost for given instance type and usage duration.""" - pass - - @abstractmethod - def get_pricing_info(self) -> Dict[str, float]: - """Get current pricing information for different instance types.""" - pass - - def start_monitoring(self): - """Start cost monitoring session.""" - self.start_time = time.time() - logger.info(f"Started cost monitoring for {self.provider_name}") - - def stop_monitoring(self) -> Optional[CostReport]: - """Stop monitoring and generate cost report.""" - if self.start_time is None: - logger.warning("Monitoring was not started") - return None - - end_time = time.time() - duration = end_time - self.start_time - - # Get current resource usage - resource_usage = self.get_resource_usage() - - # Estimate cost (requires instance type to be set) - cost_estimate = self.estimate_cost("default", duration / 3600) - - # Generate recommendations - recommendations = self.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - report = CostReport( - timestamp=datetime.now(), - duration_seconds=duration, - resource_usage=resource_usage, - cost_estimate=cost_estimate, - provider=self.provider_name, - recommendations=recommendations, - ) - - logger.info( - f"Completed cost monitoring for {self.provider_name}. " - f"Duration: {duration:.2f}s, Estimated cost: ${cost_estimate.estimated_cost:.4f}" - ) - - return report - - def get_cost_optimization_recommendations( - self, resource_usage: ResourceUsage, cost_estimate: CostEstimate - ) -> List[str]: - """Generate cost optimization recommendations based on usage patterns.""" - recommendations = [] - - # CPU utilization recommendations - if resource_usage.cpu_percent < 20: - recommendations.append( - "Low CPU usage detected. Consider using a smaller instance type." - ) - elif resource_usage.cpu_percent > 90: - recommendations.append( - "High CPU usage detected. Consider using a larger instance type or optimizing workload." - ) - - # Memory utilization recommendations - if resource_usage.memory_percent < 30: - recommendations.append( - "Low memory usage detected. Consider using an instance with less memory." - ) - elif resource_usage.memory_percent > 85: - recommendations.append( - "High memory usage detected. Consider using an instance with more memory." - ) - - # GPU utilization recommendations (if available) - if resource_usage.gpu_stats: - avg_gpu_util = sum( - gpu.get("utilization_percent", 0) for gpu in resource_usage.gpu_stats - ) / len(resource_usage.gpu_stats) - if avg_gpu_util < 50: - recommendations.append( - "Low GPU utilization detected. Consider using CPU instances or optimizing GPU workload." - ) - elif avg_gpu_util > 95: - recommendations.append( - "High GPU utilization detected. Consider multi-GPU instances for better performance." - ) - - # Cost-based recommendations - if cost_estimate.estimated_cost > 10: # $10 threshold - recommendations.append( - "High estimated cost detected. Consider using spot instances or reserved capacity." - ) - - return recommendations - - def get_gpu_utilization(self) -> List[Dict[str, Any]]: - """Get GPU utilization metrics using nvidia-smi.""" - try: - result = subprocess.run( - [ - "nvidia-smi", - "--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu", - "--format=csv,noheader,nounits", - ], - capture_output=True, - text=True, - timeout=10, - ) - - if result.returncode == 0: - lines = result.stdout.strip().split("\n") - gpu_stats = [] - for i, line in enumerate(lines): - if line.strip(): - parts = line.split(", ") - if len(parts) >= 3: - try: - gpu_stats.append( - { - "gpu_id": i, - "utilization_percent": int(parts[0]), - "memory_used_mb": int(parts[1]), - "memory_total_mb": int(parts[2]), - "memory_utilization_percent": round( - int(parts[1]) / int(parts[2]) * 100, 1 - ), - "temperature_c": ( - int(parts[3]) if len(parts) > 3 else None - ), - } - ) - except (ValueError, ZeroDivisionError): - continue - return gpu_stats - except (subprocess.TimeoutExpired, FileNotFoundError, Exception) as e: - logger.debug(f"Could not get GPU utilization: {e}") - - return [] - - def get_cpu_memory_usage(self) -> tuple: - """Get CPU and memory usage using system tools.""" - try: - # Try to get CPU usage - cpu_result = subprocess.run( - ["python", "-c", "import psutil; print(f'{psutil.cpu_percent():.1f}')"], - capture_output=True, - text=True, - timeout=5, - ) - cpu_percent = ( - float(cpu_result.stdout.strip()) if cpu_result.returncode == 0 else 0.0 - ) - - # Try to get memory usage - mem_result = subprocess.run( - [ - "python", - "-c", - "import psutil; m=psutil.virtual_memory(); " - "print(f'{m.used//1024//1024},{m.total//1024//1024},{m.percent:.1f}')", - ], - capture_output=True, - text=True, - timeout=5, - ) - - if mem_result.returncode == 0: - mem_used, mem_total, mem_percent = mem_result.stdout.strip().split(",") - return cpu_percent, int(mem_used), int(mem_total), float(mem_percent) - except Exception as e: - logger.debug(f"Could not get CPU/memory usage: {e}") - - return 0.0, 0, 0, 0.0 - - -def cost_tracking_decorator(provider: str, instance_type: str = "default"): - """ - Decorator to automatically track costs for Clustrix functions. - - Args: - provider: Cloud provider name (e.g., 'lambda', 'aws', 'azure', 'gcp') - instance_type: Recorded, but not used to price the run. The wrapper - calls ``monitor.stop_monitoring()``, which prices the elapsed time - with a hardcoded ``estimate_cost("default", ...)``, so the cost in - ``result["cost_report"]`` is the provider's placeholder "default" - rate whatever is passed here. The value is echoed back unchanged - as ``result["instance_type"]`` and is used nowhere else. To price - a specific instance type, call - ``get_cost_monitor(provider).estimate_cost(instance_type, hours)``. - - Returns: - A decorator whose wrapper returns a dict with keys ``result``, - ``success``, ``error``, ``cost_report``, ``provider`` and - ``instance_type``. It never re-raises: a failing function yields - ``success=False`` and the exception text in ``error``. - - Example:: - - from clustrix import cluster, cost_tracking_decorator - - @cost_tracking_decorator('lambda', 'a100_40gb') - @cluster(cores=8, memory="32GB") - def my_training_function(): - # Your code here - pass - """ - - def decorator(func: Callable) -> Callable: - @wraps(func) - def wrapper(*args, **kwargs): - # Get the appropriate cost monitor - monitor = get_cost_monitor(provider) - if monitor is None: - logger.warning( - f"Cost monitoring not available for provider: {provider}" - ) - return func(*args, **kwargs) - - # Start monitoring - monitor.start_monitoring() - - try: - # Execute the function - result = func(*args, **kwargs) - success = True - error = None - except Exception as e: - result = None - success = False - error = str(e) - logger.error(f"Function execution failed: {e}") - - # Stop monitoring and get report - cost_report = monitor.stop_monitoring() - - # Return enhanced result with cost information - return { - "result": result, - "success": success, - "error": error, - "cost_report": asdict(cost_report) if cost_report else None, - "provider": provider, - "instance_type": instance_type, - } - - return wrapper - - return decorator - - -def get_cost_monitor(provider: str) -> Optional[BaseCostMonitor]: - """ - Get the appropriate cost monitor for a cloud provider. - - Args: - provider: Cloud provider name - - Returns: - Cost monitor instance or None if not available - """ - provider = provider.lower() - - if provider == "lambda": - from .cost_providers.lambda_cloud import LambdaCostMonitor - - return LambdaCostMonitor() - elif provider == "aws": - from .cost_providers.aws import AWSCostMonitor - - return AWSCostMonitor() - elif provider == "azure": - from .cost_providers.azure import AzureCostMonitor - - return AzureCostMonitor() - elif provider == "gcp": - from .cost_providers.gcp import GCPCostMonitor - - return GCPCostMonitor() - else: - logger.warning(f"Unsupported cloud provider: {provider}") - return None - - -# Convenience functions for direct use -def start_cost_monitoring(provider: str) -> Optional[BaseCostMonitor]: - """Start cost monitoring for a specific provider.""" - monitor = get_cost_monitor(provider) - if monitor: - monitor.start_monitoring() - return monitor - - -def generate_cost_report( - provider: str, instance_type: str = "default" -) -> Optional[Dict[str, Any]]: - """Build a cost report from the monitor's current resource usage. - - Despite the name, the ``cost_estimate`` in the report is not the cost of - the session so far. The hours are hardcoded to ``1.0`` below, so it is a - one-hour quote for ``instance_type``. The ``resource_usage`` in the same - report *is* current. Monitoring is neither stopped nor reset. - - Returns ``None`` if ``provider`` is not supported. - """ - monitor = get_cost_monitor(provider) - if monitor: - # Get current state without stopping monitoring - resource_usage = monitor.get_resource_usage() - cost_estimate = monitor.estimate_cost(instance_type, 1.0) # 1 hour estimate - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - return { - "timestamp": datetime.now().isoformat(), - "provider": provider, - "resource_usage": asdict(resource_usage), - "cost_estimate": asdict(cost_estimate), - "recommendations": recommendations, - } - return None - - -def get_pricing_info(provider: str) -> Optional[Dict[str, float]]: - """Get pricing information for a cloud provider.""" - monitor = get_cost_monitor(provider) - if monitor: - return monitor.get_pricing_info() - return None diff --git a/clustrix/cost_providers/__init__.py b/clustrix/cost_providers/__init__.py deleted file mode 100644 index 87ed133c..00000000 --- a/clustrix/cost_providers/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -""" -Cloud provider-specific cost monitoring implementations. -""" diff --git a/clustrix/cost_providers/aws.py b/clustrix/cost_providers/aws.py deleted file mode 100644 index 080b4a01..00000000 --- a/clustrix/cost_providers/aws.py +++ /dev/null @@ -1,369 +0,0 @@ -""" -AWS cost monitoring implementation. -""" - -import logging -from datetime import datetime -from typing import Dict, List, Any - -from ..cost_monitoring import BaseCostMonitor, ResourceUsage, CostEstimate -from ..pricing_clients.aws_pricing import AWSPricingClient - -logger = logging.getLogger(__name__) - - -class AWSCostMonitor(BaseCostMonitor): - """Cost monitoring for AWS instances (EC2, Batch, etc.).""" - - def __init__(self, region: str = "us-east-1", use_pricing_api: bool = True): - super().__init__("AWS") - self.region = region - self.use_pricing_api = use_pricing_api - - # Initialize pricing client - self.pricing_client = AWSPricingClient() if use_pricing_api else None - - # AWS EC2 On-Demand pricing (us-east-1, as of 2025, approximate rates in USD/hour) - # These serve as fallback when API is unavailable - self.ec2_pricing = { - # General Purpose - "t3.micro": 0.0104, - "t3.small": 0.0208, - "t3.medium": 0.0416, - "t3.large": 0.0832, - "t3.xlarge": 0.1664, - "t3.2xlarge": 0.3328, - # Compute Optimized - "c5.large": 0.085, - "c5.xlarge": 0.17, - "c5.2xlarge": 0.34, - "c5.4xlarge": 0.68, - "c5.9xlarge": 1.53, - "c5.18xlarge": 3.06, - # Memory Optimized - "r5.large": 0.126, - "r5.xlarge": 0.252, - "r5.2xlarge": 0.504, - "r5.4xlarge": 1.008, - "r5.8xlarge": 2.016, - "r5.16xlarge": 4.032, - # GPU Instances - "p3.2xlarge": 3.06, # 1 V100 - "p3.8xlarge": 12.24, # 4 V100 - "p3.16xlarge": 24.48, # 8 V100 - "p4d.24xlarge": 32.77, # 8 A100 - "g4dn.xlarge": 0.526, # 1 T4 - "g4dn.2xlarge": 0.752, # 1 T4 - "g4dn.4xlarge": 1.204, # 1 T4 - "g4dn.8xlarge": 2.176, # 1 T4 - "g4dn.12xlarge": 3.912, # 4 T4 - "g4dn.16xlarge": 4.352, # 1 T4 - # Default fallback - "default": 0.10, - } - - # Spot instance discount factors (approximate) - self.spot_discounts = { - "t3": 0.7, # ~30% discount - "c5": 0.65, # ~35% discount - "r5": 0.6, # ~40% discount - "p3": 0.3, # ~70% discount - "p4d": 0.35, # ~65% discount - "g4dn": 0.4, # ~60% discount - "default": 0.7, # ~30% discount - } - - # Instance metadata - self.instance_metadata = { - "p3.2xlarge": { - "gpus": 1, - "gpu_type": "V100", - "gpu_memory": "16GB", - "cpu_cores": 8, - "ram": "61GB", - }, - "p3.8xlarge": { - "gpus": 4, - "gpu_type": "V100", - "gpu_memory": "64GB", - "cpu_cores": 32, - "ram": "244GB", - }, - "p3.16xlarge": { - "gpus": 8, - "gpu_type": "V100", - "gpu_memory": "128GB", - "cpu_cores": 64, - "ram": "488GB", - }, - "p4d.24xlarge": { - "gpus": 8, - "gpu_type": "A100", - "gpu_memory": "320GB", - "cpu_cores": 96, - "ram": "1152GB", - }, - "g4dn.xlarge": { - "gpus": 1, - "gpu_type": "T4", - "gpu_memory": "16GB", - "cpu_cores": 4, - "ram": "16GB", - }, - "g4dn.2xlarge": { - "gpus": 1, - "gpu_type": "T4", - "gpu_memory": "16GB", - "cpu_cores": 8, - "ram": "32GB", - }, - "g4dn.12xlarge": { - "gpus": 4, - "gpu_type": "T4", - "gpu_memory": "64GB", - "cpu_cores": 48, - "ram": "192GB", - }, - } - - def get_resource_usage(self) -> ResourceUsage: - """Get current resource utilization for AWS instance.""" - # Get CPU and memory usage - cpu_percent, mem_used_mb, mem_total_mb, mem_percent = ( - self.get_cpu_memory_usage() - ) - - # Get GPU utilization (if available) - gpu_stats = self.get_gpu_utilization() - - return ResourceUsage( - cpu_percent=cpu_percent, - memory_used_mb=mem_used_mb, - memory_total_mb=mem_total_mb, - memory_percent=mem_percent, - gpu_stats=gpu_stats, - ) - - def estimate_cost( - self, instance_type: str, hours_used: float, use_spot: bool = False - ) -> CostEstimate: - """Estimate cost for AWS instance usage.""" - hourly_rate = None - pricing_source = "hardcoded" - pricing_warning = None - - # Try to get pricing from API first - if self.pricing_client and not use_spot: - try: - hourly_rate = self.pricing_client.get_instance_pricing( - instance_type=instance_type, region=self.region - ) - if hourly_rate: - logger.debug( - f"Got pricing for {instance_type} from API: ${hourly_rate}/hr" - ) - pricing_source = "api" - except Exception as e: - logger.debug(f"Failed to get pricing from API: {e}") - - # Fall back to hardcoded pricing if API failed - if hourly_rate is None: - hourly_rate = self.ec2_pricing.get( - instance_type, self.ec2_pricing["default"] - ) - pricing_source = "hardcoded" - if self.pricing_client and self.pricing_client.is_pricing_data_outdated(): - pricing_warning = ( - f"Using potentially outdated pricing data (last updated: " - f"{self.pricing_client._hardcoded_pricing_date}). " - f"Current prices may differ. Consider checking AWS pricing page." - ) - logger.warning(pricing_warning) - if instance_type not in self.ec2_pricing: - # The "default" rate has nothing to do with this - # instance. Appended rather than assigned, so an - # outdated-data warning cannot displace the more - # important fact that the figure is a placeholder. - unknown = ( - f"Unrecognised instance type {instance_type!r}; " - f"priced at the placeholder default rate of " - f"${hourly_rate}/hr. This is not a real quote." - ) - logger.warning(unknown) - pricing_warning = ( - f"{pricing_warning} {unknown}" if pricing_warning else unknown - ) - - # Apply spot discount if requested - if use_spot: - instance_family = instance_type.split(".")[0] - discount_factor = self.spot_discounts.get( - instance_family, self.spot_discounts["default"] - ) - hourly_rate *= discount_factor - if pricing_source == "hardcoded": - pricing_warning = ( - pricing_warning or "" - ) + " Spot pricing is estimated and may vary significantly." - - # Calculate estimated cost - estimated_cost = hourly_rate * hours_used - - pricing_type = "Spot" if use_spot else "On-Demand" - - return CostEstimate( - instance_type=f"{instance_type} ({pricing_type})", - hourly_rate=hourly_rate, - hours_used=hours_used, - estimated_cost=estimated_cost, - currency="USD", - last_updated=datetime.now(), - pricing_source=pricing_source, - pricing_warning=pricing_warning, - ) - - def get_pricing_info(self) -> Dict[str, float]: - """Get AWS EC2 pricing information.""" - # For now, return hardcoded pricing with a warning if outdated - # A full implementation would query the API for all instance types - if self.pricing_client and self.pricing_client.is_pricing_data_outdated(): - logger.warning( - "Pricing data may be outdated. Consider refreshing from AWS Pricing API." - ) - return self.ec2_pricing.copy() - - def get_spot_pricing_info(self) -> Dict[str, float]: - """Get estimated AWS spot pricing.""" - spot_pricing = {} - for instance_type, on_demand_price in self.ec2_pricing.items(): - if instance_type != "default": - instance_family = instance_type.split(".")[0] - discount_factor = self.spot_discounts.get( - instance_family, self.spot_discounts["default"] - ) - spot_pricing[instance_type] = on_demand_price * discount_factor - return spot_pricing - - def get_cost_optimization_recommendations( - self, resource_usage: ResourceUsage, cost_estimate: CostEstimate - ) -> List[str]: - """Get AWS-specific cost optimization recommendations.""" - recommendations = super().get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # AWS-specific recommendations - recommendations.extend( - [ - "Consider using Spot Instances for fault-tolerant workloads (up to 90% savings)", - "Use Reserved Instances for predictable workloads (up to 75% savings)", - "Consider AWS Batch for large-scale batch processing", - "Use EBS-optimized instances for I/O intensive workloads", - "Enable detailed monitoring to track resource utilization", - "Consider using AWS ParallelCluster for HPC workloads", - "Use S3 for data storage instead of expensive EBS volumes when possible", - "Set up CloudWatch alarms for cost monitoring and auto-termination", - ] - ) - - # Instance-specific recommendations - current_instance = cost_estimate.instance_type.split(" ")[ - 0 - ] # Remove pricing type - if current_instance.startswith("p3") or current_instance.startswith("p4d"): - if resource_usage.gpu_stats: - avg_gpu_util = sum( - gpu.get("utilization_percent", 0) - for gpu in resource_usage.gpu_stats - ) / len(resource_usage.gpu_stats) - if avg_gpu_util < 50: - recommendations.append( - "Low GPU utilization on expensive GPU instance. Consider g4dn instances or CPU instances." - ) - - return recommendations - - def estimate_batch_cost( - self, - job_queue: str, - compute_environment: str, - estimated_jobs: int, - avg_job_duration_hours: float, - ) -> Dict[str, Any]: - """Estimate costs for AWS Batch workloads.""" - # This would typically integrate with AWS Batch APIs - # For now, provide a basic estimation framework - - base_instance_cost = self.ec2_pricing.get( - "c5.large", 0.085 - ) # Default compute instance - - total_compute_hours = estimated_jobs * avg_job_duration_hours - estimated_cost = total_compute_hours * base_instance_cost - - return { - "job_queue": job_queue, - "compute_environment": compute_environment, - "estimated_jobs": estimated_jobs, - "avg_job_duration_hours": avg_job_duration_hours, - "total_compute_hours": total_compute_hours, - "estimated_cost": estimated_cost, - "cost_per_job": ( - estimated_cost / estimated_jobs if estimated_jobs > 0 else 0 - ), - "recommendations": [ - "Use Spot instances in Batch compute environments for additional savings", - "Optimize job packaging to reduce overhead", - "Monitor job efficiency and resource utilization", - "Use appropriate instance types for different job characteristics", - ], - } - - def get_region_pricing_comparison( - self, instance_type: str - ) -> Dict[str, Dict[str, Any]]: - """Compare pricing across AWS regions (simplified).""" - # Regional pricing multipliers (approximate) - region_multipliers = { - "us-east-1": 1.0, # N. Virginia (baseline) - "us-west-2": 1.05, # Oregon - "eu-west-1": 1.1, # Ireland - "ap-southeast-1": 1.15, # Singapore - "ap-northeast-1": 1.2, # Tokyo - } - - base_price = self.ec2_pricing.get(instance_type, self.ec2_pricing["default"]) - - regional_pricing = {} - for region, multiplier in region_multipliers.items(): - regional_pricing[region] = { - "on_demand_hourly": base_price * multiplier, - "estimated_spot_hourly": base_price - * multiplier - * 0.7, # Rough spot estimate - "region_name": region, - } - - return regional_pricing - - def get_aws_specific_metrics(self) -> Dict[str, Any]: - """Get AWS-specific cost and performance metrics.""" - return { - "region": self.region, - "availability_zone": "auto-detect", # Would detect from instance metadata - "instance_lifecycle": "on-demand", # or 'spot' - "ebs_optimized": True, - "enhanced_networking": True, - "placement_group": None, - "cost_optimization_score": self._calculate_cost_optimization_score(), - } - - def _calculate_cost_optimization_score(self) -> float: - """Calculate a cost optimization score (0-100).""" - # This would analyze various factors: - # - Resource utilization - # - Instance type appropriateness - # - Spot vs on-demand usage - # - Reserved instance coverage - # For now, return a placeholder - return 75.0 diff --git a/clustrix/cost_providers/azure.py b/clustrix/cost_providers/azure.py deleted file mode 100644 index 68c6622b..00000000 --- a/clustrix/cost_providers/azure.py +++ /dev/null @@ -1,408 +0,0 @@ -""" -Azure cost monitoring implementation. -""" - -import logging -from datetime import datetime -from typing import Dict, List, Any - -from ..cost_monitoring import BaseCostMonitor, ResourceUsage, CostEstimate -from ..pricing_clients.azure_pricing import AzurePricingClient - -logger = logging.getLogger(__name__) - - -class AzureCostMonitor(BaseCostMonitor): - """Cost monitoring for Azure Virtual Machines and Batch.""" - - def __init__(self, region: str = "eastus", use_pricing_api: bool = True): - super().__init__("Azure") - self.region = region - self.use_pricing_api = use_pricing_api - - # Initialize pricing client - self.pricing_client = AzurePricingClient() if use_pricing_api else None - - # Azure VM pricing (East US, as of 2025, approximate rates in USD/hour) - # These serve as fallback when API is unavailable - self.vm_pricing = { - # B-series (Burstable) - "Standard_B1s": 0.0104, - "Standard_B1ms": 0.0208, - "Standard_B2s": 0.0416, - "Standard_B2ms": 0.0832, - "Standard_B4ms": 0.1664, - "Standard_B8ms": 0.3328, - # D-series (General Purpose) - "Standard_D2s_v3": 0.096, - "Standard_D4s_v3": 0.192, - "Standard_D8s_v3": 0.384, - "Standard_D16s_v3": 0.768, - "Standard_D32s_v3": 1.536, - "Standard_D64s_v3": 3.072, - # F-series (Compute Optimized) - "Standard_F2s_v2": 0.085, - "Standard_F4s_v2": 0.169, - "Standard_F8s_v2": 0.338, - "Standard_F16s_v2": 0.676, - "Standard_F32s_v2": 1.352, - "Standard_F64s_v2": 2.704, - # E-series (Memory Optimized) - "Standard_E2s_v3": 0.126, - "Standard_E4s_v3": 0.252, - "Standard_E8s_v3": 0.504, - "Standard_E16s_v3": 1.008, - "Standard_E32s_v3": 2.016, - "Standard_E64s_v3": 4.032, - # GPU VMs - "Standard_NC6s_v3": 3.06, # 1 V100 - "Standard_NC12s_v3": 6.12, # 2 V100 - "Standard_NC24s_v3": 12.24, # 4 V100 - "Standard_ND40rs_v2": 22.0, # 8 V100 - "Standard_NC6s_v2": 0.90, # 1 P100 - "Standard_NC12s_v2": 1.80, # 2 P100 - "Standard_NC24s_v2": 3.60, # 4 P100 - # Default fallback - "default": 0.10, - } - - # Spot VM discount factors (approximate) - self.spot_discounts = { - "Standard_B": 0.8, # ~20% discount - "Standard_D": 0.7, # ~30% discount - "Standard_F": 0.65, # ~35% discount - "Standard_E": 0.6, # ~40% discount - "Standard_NC": 0.3, # ~70% discount - "Standard_ND": 0.35, # ~65% discount - "default": 0.7, # ~30% discount - } - - # Instance metadata - self.instance_metadata = { - "Standard_NC6s_v3": { - "gpus": 1, - "gpu_type": "V100", - "gpu_memory": "16GB", - "cpu_cores": 6, - "ram": "112GB", - }, - "Standard_NC12s_v3": { - "gpus": 2, - "gpu_type": "V100", - "gpu_memory": "32GB", - "cpu_cores": 12, - "ram": "224GB", - }, - "Standard_NC24s_v3": { - "gpus": 4, - "gpu_type": "V100", - "gpu_memory": "64GB", - "cpu_cores": 24, - "ram": "448GB", - }, - "Standard_ND40rs_v2": { - "gpus": 8, - "gpu_type": "V100", - "gpu_memory": "128GB", - "cpu_cores": 40, - "ram": "672GB", - }, - "Standard_NC6s_v2": { - "gpus": 1, - "gpu_type": "P100", - "gpu_memory": "16GB", - "cpu_cores": 6, - "ram": "112GB", - }, - } - - def get_resource_usage(self) -> ResourceUsage: - """Get current resource utilization for Azure VM.""" - # Get CPU and memory usage - cpu_percent, mem_used_mb, mem_total_mb, mem_percent = ( - self.get_cpu_memory_usage() - ) - - # Get GPU utilization (if available) - gpu_stats = self.get_gpu_utilization() - - return ResourceUsage( - cpu_percent=cpu_percent, - memory_used_mb=mem_used_mb, - memory_total_mb=mem_total_mb, - memory_percent=mem_percent, - gpu_stats=gpu_stats, - ) - - def estimate_cost( - self, instance_type: str, hours_used: float, use_spot: bool = False - ) -> CostEstimate: - """Estimate cost for Azure VM usage.""" - hourly_rate = None - pricing_source = "hardcoded" - pricing_warning = None - - # Try to get pricing from API first - if self.pricing_client and not use_spot: - try: - hourly_rate = self.pricing_client.get_instance_pricing( - instance_type=instance_type, region=self.region - ) - if hourly_rate: - logger.debug( - f"Got pricing for {instance_type} from Azure API: ${hourly_rate}/hr" - ) - pricing_source = "api" - except Exception as e: - logger.debug(f"Failed to get pricing from Azure API: {e}") - - # Handle spot pricing - if use_spot and self.pricing_client: - try: - hourly_rate = self.pricing_client.get_spot_pricing( - instance_type, self.region - ) - if hourly_rate: - pricing_source = "api" - except Exception as e: - logger.debug(f"Failed to get spot pricing from Azure API: {e}") - - # Fall back to hardcoded pricing if API failed - if hourly_rate is None: - hourly_rate = self.vm_pricing.get(instance_type, self.vm_pricing["default"]) - pricing_source = "hardcoded" - if instance_type not in self.vm_pricing: - # The "default" rate has nothing to do with this - # instance. Appended rather than assigned, so an - # outdated-data warning cannot displace the more - # important fact that the figure is a placeholder. - unknown = ( - f"Unrecognised instance type {instance_type!r}; " - f"priced at the placeholder default rate of " - f"${hourly_rate}/hr. This is not a real quote." - ) - logger.warning(unknown) - pricing_warning = ( - f"{pricing_warning} {unknown}" if pricing_warning else unknown - ) - - if self.pricing_client and self.pricing_client.is_pricing_data_outdated(): - pricing_warning = ( - f"Using potentially outdated pricing data (last updated: " - f"{self.pricing_client._hardcoded_pricing_date}). " - f"Current prices may differ. Consider checking Azure pricing page." - ) - logger.warning(pricing_warning) - - # Apply spot discount if requested and using hardcoded pricing - if use_spot: - instance_family = ( - instance_type.split("_")[1] - if "_" in instance_type - else instance_type - ) - # Match instance family prefix - discount_key = next( - ( - k - for k in self.spot_discounts.keys() - if instance_family.startswith(k.replace("Standard_", "")) - ), - "default", - ) - discount_factor = self.spot_discounts[discount_key] - hourly_rate *= discount_factor - if pricing_source == "hardcoded": - pricing_warning = ( - pricing_warning or "" - ) + " Spot pricing is estimated and may vary significantly." - - # Calculate estimated cost - estimated_cost = hourly_rate * hours_used - - pricing_type = "Spot" if use_spot else "Pay-as-you-go" - - return CostEstimate( - instance_type=f"{instance_type} ({pricing_type})", - hourly_rate=hourly_rate, - hours_used=hours_used, - estimated_cost=estimated_cost, - currency="USD", - last_updated=datetime.now(), - pricing_source=pricing_source, - pricing_warning=pricing_warning, - ) - - def get_pricing_info(self) -> Dict[str, float]: - """Get Azure VM pricing information.""" - return self.vm_pricing.copy() - - def get_spot_pricing_info(self) -> Dict[str, float]: - """Get estimated Azure Spot VM pricing.""" - spot_pricing = {} - for instance_type, on_demand_price in self.vm_pricing.items(): - if instance_type != "default": - instance_family = ( - instance_type.split("_")[1] - if "_" in instance_type - else instance_type - ) - discount_key = next( - ( - k - for k in self.spot_discounts.keys() - if instance_family.startswith(k.replace("Standard_", "")) - ), - "default", - ) - discount_factor = self.spot_discounts[discount_key] - spot_pricing[instance_type] = on_demand_price * discount_factor - return spot_pricing - - def get_cost_optimization_recommendations( - self, resource_usage: ResourceUsage, cost_estimate: CostEstimate - ) -> List[str]: - """Get Azure-specific cost optimization recommendations.""" - recommendations = super().get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # Azure-specific recommendations - recommendations.extend( - [ - "Consider using Azure Spot VMs for fault-tolerant workloads (up to 90% savings)", - "Use Reserved VM Instances for predictable workloads (up to 72% savings)", - "Consider Azure Batch for large-scale parallel processing", - "Use Azure CycleCloud for HPC workloads and cluster management", - "Enable auto-shutdown for development VMs to avoid unnecessary costs", - "Use Azure Monitor for detailed cost tracking and alerts", - "Consider B-series burstable VMs for variable workloads", - "Use managed disks with appropriate performance tiers", - "Implement lifecycle policies for blob storage to reduce storage costs", - ] - ) - - # Instance-specific recommendations - current_instance = cost_estimate.instance_type.split(" ")[ - 0 - ] # Remove pricing type - if current_instance.startswith("Standard_NC") or current_instance.startswith( - "Standard_ND" - ): - if resource_usage.gpu_stats: - avg_gpu_util = sum( - gpu.get("utilization_percent", 0) - for gpu in resource_usage.gpu_stats - ) / len(resource_usage.gpu_stats) - if avg_gpu_util < 50: - recommendations.append( - "Low GPU utilization on expensive GPU VM. Consider Standard_D or Standard_F series VMs." - ) - - return recommendations - - def estimate_batch_cost( - self, - pool_name: str, - vm_size: str, - target_nodes: int, - estimated_duration_hours: float, - ) -> Dict[str, Any]: - """Estimate costs for Azure Batch workloads.""" - # Get VM hourly cost - vm_hourly_cost = self.vm_pricing.get(vm_size, self.vm_pricing["default"]) - - # Calculate total cost - total_compute_hours = target_nodes * estimated_duration_hours - estimated_cost = total_compute_hours * vm_hourly_cost - - return { - "pool_name": pool_name, - "vm_size": vm_size, - "target_nodes": target_nodes, - "estimated_duration_hours": estimated_duration_hours, - "total_compute_hours": total_compute_hours, - "vm_hourly_cost": vm_hourly_cost, - "estimated_cost": estimated_cost, - "cost_per_node_hour": vm_hourly_cost, - "recommendations": [ - "Use Low Priority VMs in Batch pools for additional savings", - "Implement auto-scaling to optimize node utilization", - "Use appropriate VM sizes for different task characteristics", - "Monitor task efficiency and optimize task packaging", - "Consider using VMSS (Virtual Machine Scale Sets) for flexibility", - ], - } - - def get_region_pricing_comparison( - self, instance_type: str - ) -> Dict[str, Dict[str, Any]]: - """Compare pricing across Azure regions (simplified).""" - # Regional pricing multipliers (approximate) - region_multipliers = { - "East US": 1.0, # Baseline - "West US 2": 1.0, # Same as East US - "Central US": 0.95, # Slightly cheaper - "West Europe": 1.1, # ~10% more expensive - "Southeast Asia": 1.15, # ~15% more expensive - "Japan East": 1.2, # ~20% more expensive - } - - base_price = self.vm_pricing.get(instance_type, self.vm_pricing["default"]) - - regional_pricing = {} - for region, multiplier in region_multipliers.items(): - regional_pricing[region] = { - "pay_as_you_go_hourly": base_price * multiplier, - "estimated_spot_hourly": base_price - * multiplier - * 0.7, # Rough spot estimate - "region_name": region, - } - - return regional_pricing - - def get_azure_specific_metrics(self) -> Dict[str, Any]: - """Get Azure-specific cost and performance metrics.""" - return { - "region": self.region, - "availability_zone": "auto-detect", # Would detect from instance metadata - "vm_lifecycle": "pay-as-you-go", # or 'spot' or 'reserved' - "managed_disks": True, - "accelerated_networking": True, - "proximity_placement_group": None, - "cost_optimization_score": self._calculate_cost_optimization_score(), - } - - def get_azure_consumption_api_integration(self) -> Dict[str, Any]: - """Framework for Azure Consumption API integration.""" - # This would integrate with Azure Consumption APIs for real billing data - # For now, provide a framework structure - - return { - "billing_period": datetime.now().strftime("%Y-%m"), - "subscription_id": "auto-detect", - "resource_group": "auto-detect", - "cost_to_date": 0.0, # Would fetch from API - "forecasted_cost": 0.0, # Would calculate based on usage - "budget_alerts": [], # Would fetch configured alerts - "cost_breakdown": { - "compute": 0.0, - "storage": 0.0, - "networking": 0.0, - "other": 0.0, - }, - "api_available": False, # Would check API connectivity - "last_updated": datetime.now().isoformat(), - } - - def _calculate_cost_optimization_score(self) -> float: - """Calculate a cost optimization score (0-100).""" - # This would analyze various factors: - # - Resource utilization - # - VM size appropriateness - # - Spot vs pay-as-you-go usage - # - Reserved instance coverage - # - Auto-shutdown configurations - # For now, return a placeholder - return 72.0 diff --git a/clustrix/cost_providers/gcp.py b/clustrix/cost_providers/gcp.py deleted file mode 100644 index f1377486..00000000 --- a/clustrix/cost_providers/gcp.py +++ /dev/null @@ -1,435 +0,0 @@ -""" -Google Cloud Platform cost monitoring implementation. -""" - -import logging -from datetime import datetime -from typing import Dict, List, Any - -from ..cost_monitoring import BaseCostMonitor, ResourceUsage, CostEstimate -from ..pricing_clients.gcp_pricing import GCPPricingClient - -logger = logging.getLogger(__name__) - - -class GCPCostMonitor(BaseCostMonitor): - """Cost monitoring for Google Cloud Platform Compute Engine instances.""" - - def __init__(self, region: str = "us-central1", use_pricing_api: bool = True): - super().__init__("Google Cloud Platform") - self.region = region - self.use_pricing_api = use_pricing_api - - # Initialize pricing client - self.pricing_client = GCPPricingClient() if use_pricing_api else None - - # GCP Compute Engine pricing (us-central1, as of 2025, approximate rates in USD/hour) - # These serve as fallback when API is unavailable - self.compute_pricing = { - # General Purpose (N2) - "n2-standard-2": 0.097, - "n2-standard-4": 0.194, - "n2-standard-8": 0.389, - "n2-standard-16": 0.778, - "n2-standard-32": 1.555, - "n2-standard-64": 3.110, - # High-CPU (N2) - "n2-highcpu-16": 0.588, - "n2-highcpu-32": 1.177, - "n2-highcpu-64": 2.353, - # High-Memory (N2) - "n2-highmem-2": 0.130, - "n2-highmem-4": 0.261, - "n2-highmem-8": 0.521, - "n2-highmem-16": 1.042, - # Compute Optimized (C2) - "c2-standard-4": 0.134, - "c2-standard-8": 0.268, - "c2-standard-16": 0.537, - "c2-standard-30": 1.006, - "c2-standard-60": 2.013, - # Memory Optimized (M2) - "m2-ultramem-208": 32.775, - "m2-ultramem-416": 65.550, - # GPU-attached instances (base compute + GPU cost) - "n1-standard-4-k80": 0.294, # + K80 GPU - "n1-standard-8-v100": 1.46, # + V100 GPU - "n1-standard-16-t4": 0.80, # + T4 GPU - "a2-highgpu-1g": 3.673, # 1 A100 GPU - "a2-highgpu-2g": 7.347, # 2 A100 GPU - "a2-highgpu-4g": 14.694, # 4 A100 GPU - "a2-highgpu-8g": 29.387, # 8 A100 GPU - # Default fallback - "default": 0.10, - } - - # Preemptible instance discount (approximately 80% discount) - self.preemptible_discount = 0.2 - - # Instance metadata - self.instance_metadata = { - "a2-highgpu-1g": { - "gpus": 1, - "gpu_type": "A100", - "gpu_memory": "40GB", - "cpu_cores": 12, - "ram": "85GB", - }, - "a2-highgpu-2g": { - "gpus": 2, - "gpu_type": "A100", - "gpu_memory": "80GB", - "cpu_cores": 24, - "ram": "170GB", - }, - "a2-highgpu-4g": { - "gpus": 4, - "gpu_type": "A100", - "gpu_memory": "160GB", - "cpu_cores": 48, - "ram": "340GB", - }, - "a2-highgpu-8g": { - "gpus": 8, - "gpu_type": "A100", - "gpu_memory": "320GB", - "cpu_cores": 96, - "ram": "680GB", - }, - "n1-standard-8-v100": { - "gpus": 1, - "gpu_type": "V100", - "gpu_memory": "16GB", - "cpu_cores": 8, - "ram": "30GB", - }, - "n1-standard-16-t4": { - "gpus": 1, - "gpu_type": "T4", - "gpu_memory": "16GB", - "cpu_cores": 16, - "ram": "60GB", - }, - } - - # Sustained Use Discounts (automatic discounts for sustained usage) - self.sustained_use_discounts = { - 25: 0.0, # 0-25% of month: no discount - 50: 0.10, # 25-50% of month: 10% discount - 75: 0.20, # 50-75% of month: 20% discount - 100: 0.30, # 75-100% of month: 30% discount - } - - def get_resource_usage(self) -> ResourceUsage: - """Get current resource utilization for GCP instance.""" - # Get CPU and memory usage - cpu_percent, mem_used_mb, mem_total_mb, mem_percent = ( - self.get_cpu_memory_usage() - ) - - # Get GPU utilization (if available) - gpu_stats = self.get_gpu_utilization() - - return ResourceUsage( - cpu_percent=cpu_percent, - memory_used_mb=mem_used_mb, - memory_total_mb=mem_total_mb, - memory_percent=mem_percent, - gpu_stats=gpu_stats, - ) - - def estimate_cost( - self, - instance_type: str, - hours_used: float, - use_preemptible: bool = False, - sustained_use_percent: float = 0, - ) -> CostEstimate: - """Estimate cost for GCP instance usage.""" - hourly_rate = None - pricing_source = "hardcoded" - pricing_warning = None - - # Try to get pricing from API first - if self.pricing_client and not use_preemptible: - try: - hourly_rate = self.pricing_client.get_instance_pricing( - instance_type=instance_type, region=self.region - ) - if hourly_rate: - logger.debug( - f"Got pricing for {instance_type} from GCP API: ${hourly_rate}/hr" - ) - pricing_source = "api" - except Exception as e: - logger.debug(f"Failed to get pricing from GCP API: {e}") - - # Handle preemptible pricing - if use_preemptible and self.pricing_client: - try: - hourly_rate = self.pricing_client.get_preemptible_pricing( - instance_type, self.region - ) - if hourly_rate: - pricing_source = "api" - except Exception as e: - logger.debug(f"Failed to get preemptible pricing from GCP API: {e}") - - # Fall back to hardcoded pricing if API failed - if hourly_rate is None: - hourly_rate = self.compute_pricing.get( - instance_type, self.compute_pricing["default"] - ) - pricing_source = "hardcoded" - if instance_type not in self.compute_pricing: - # The "default" rate has nothing to do with this - # instance. Appended rather than assigned, so an - # outdated-data warning cannot displace the more - # important fact that the figure is a placeholder. - unknown = ( - f"Unrecognised instance type {instance_type!r}; " - f"priced at the placeholder default rate of " - f"${hourly_rate}/hr. This is not a real quote." - ) - logger.warning(unknown) - pricing_warning = ( - f"{pricing_warning} {unknown}" if pricing_warning else unknown - ) - - if self.pricing_client and self.pricing_client.is_pricing_data_outdated(): - pricing_warning = ( - f"Using potentially outdated pricing data (last updated: " - f"{self.pricing_client._hardcoded_pricing_date}). " - f"Current prices may differ. Consider checking GCP pricing page." - ) - logger.warning(pricing_warning) - - # Apply preemptible discount if requested and using hardcoded pricing - if use_preemptible: - hourly_rate *= self.preemptible_discount - if pricing_source == "hardcoded": - pricing_warning = ( - (pricing_warning or "") - + " Preemptible pricing is estimated and may vary significantly." - ) - - # Apply sustained use discount (GCP feature) - discount: float = 0.0 - if sustained_use_percent > 25: - discount_tier = min( - [ - k - for k in self.sustained_use_discounts.keys() - if k >= sustained_use_percent - ] - ) - discount = self.sustained_use_discounts[discount_tier] - hourly_rate *= 1 - discount - - # Calculate estimated cost - estimated_cost = hourly_rate * hours_used - - pricing_type = "Preemptible" if use_preemptible else "On-Demand" - if sustained_use_percent > 25: - pricing_type += f" (SUD: {discount * 100:.0f}%)" - - return CostEstimate( - instance_type=f"{instance_type} ({pricing_type})", - hourly_rate=hourly_rate, - hours_used=hours_used, - estimated_cost=estimated_cost, - currency="USD", - last_updated=datetime.now(), - pricing_source=pricing_source, - pricing_warning=pricing_warning, - ) - - def get_pricing_info(self) -> Dict[str, float]: - """Get GCP Compute Engine pricing information.""" - return self.compute_pricing.copy() - - def get_preemptible_pricing_info(self) -> Dict[str, float]: - """Get GCP preemptible pricing.""" - preemptible_pricing = {} - for instance_type, on_demand_price in self.compute_pricing.items(): - if instance_type != "default": - preemptible_pricing[instance_type] = ( - on_demand_price * self.preemptible_discount - ) - return preemptible_pricing - - def get_cost_optimization_recommendations( - self, resource_usage: ResourceUsage, cost_estimate: CostEstimate - ) -> List[str]: - """Get GCP-specific cost optimization recommendations.""" - recommendations = super().get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # GCP-specific recommendations - recommendations.extend( - [ - "Consider using Preemptible VMs for fault-tolerant workloads (up to 80% savings)", - "Take advantage of automatic Sustained Use Discounts for long-running instances", - "Use Committed Use Discounts for predictable workloads (up to 57% savings)", - "Consider using custom machine types to optimize CPU/memory ratios", - "Use Google Kubernetes Engine for containerized workloads", - "Implement instance scheduling to automatically start/stop VMs", - "Use Cloud Storage instead of persistent disks for cold data", - "Enable detailed monitoring with Cloud Monitoring for cost tracking", - "Consider using Sole-tenant nodes for licensing requirements", - ] - ) - - # Instance-specific recommendations - current_instance = cost_estimate.instance_type.split(" ")[ - 0 - ] # Remove pricing type - if current_instance.startswith("a2-highgpu"): - if resource_usage.gpu_stats: - avg_gpu_util = sum( - gpu.get("utilization_percent", 0) - for gpu in resource_usage.gpu_stats - ) / len(resource_usage.gpu_stats) - if avg_gpu_util < 50: - recommendations.append( - "Low GPU utilization on expensive A100 instance. Consider n1-standard instances with T4 GPUs." - ) - - return recommendations - - def estimate_sustained_use_discount(self, hours_per_month: float) -> Dict[str, Any]: - """Calculate sustained use discount based on monthly usage.""" - hours_in_month = 30 * 24 # 720 hours - usage_percentage = (hours_per_month / hours_in_month) * 100 - - discount = 0.0 - discount_tier = "None" - - if usage_percentage >= 75: - discount = 0.30 - discount_tier = "75-100%" - elif usage_percentage >= 50: - discount = 0.20 - discount_tier = "50-75%" - elif usage_percentage >= 25: - discount = 0.10 - discount_tier = "25-50%" - - return { - "hours_per_month": hours_per_month, - "usage_percentage": usage_percentage, - "discount_percentage": discount * 100, - "discount_tier": discount_tier, - "effective_hourly_rate_multiplier": 1 - discount, - } - - def get_region_pricing_comparison( - self, instance_type: str - ) -> Dict[str, Dict[str, Any]]: - """Compare pricing across GCP regions (simplified).""" - # Regional pricing multipliers (approximate) - region_multipliers = { - "us-central1": 1.0, # Iowa (baseline) - "us-east1": 1.0, # South Carolina - "us-west1": 1.05, # Oregon - "europe-west1": 1.1, # Belgium - "asia-southeast1": 1.15, # Singapore - "asia-northeast1": 1.2, # Tokyo - } - - base_price = self.compute_pricing.get( - instance_type, self.compute_pricing["default"] - ) - - regional_pricing = {} - for region, multiplier in region_multipliers.items(): - regional_pricing[region] = { - "on_demand_hourly": base_price * multiplier, - "preemptible_hourly": base_price - * multiplier - * self.preemptible_discount, - "region_name": region, - } - - return regional_pricing - - def estimate_batch_cost( - self, - job_name: str, - machine_type: str, - instance_count: int, - estimated_duration_hours: float, - ) -> Dict[str, Any]: - """Estimate costs for Google Cloud Batch workloads.""" - # Get VM hourly cost - vm_hourly_cost = self.compute_pricing.get( - machine_type, self.compute_pricing["default"] - ) - - # Calculate total cost - total_compute_hours = instance_count * estimated_duration_hours - estimated_cost = total_compute_hours * vm_hourly_cost - - return { - "job_name": job_name, - "machine_type": machine_type, - "instance_count": instance_count, - "estimated_duration_hours": estimated_duration_hours, - "total_compute_hours": total_compute_hours, - "vm_hourly_cost": vm_hourly_cost, - "estimated_cost": estimated_cost, - "cost_per_instance_hour": vm_hourly_cost, - "recommendations": [ - "Use preemptible instances in batch jobs for significant savings", - "Optimize job parallelization to reduce total runtime", - "Use appropriate machine types for different job characteristics", - "Implement checkpointing for fault tolerance with preemptible instances", - "Consider using Google Kubernetes Engine for batch workloads", - ], - } - - def get_gcp_specific_metrics(self) -> Dict[str, Any]: - """Get GCP-specific cost and performance metrics.""" - return { - "region": self.region, - "zone": "auto-detect", # Would detect from instance metadata - "vm_lifecycle": "on-demand", # or 'preemptible' - "custom_machine_type": False, - "sole_tenancy": False, - "committed_use_discount": False, - "cost_optimization_score": self._calculate_cost_optimization_score(), - } - - def get_billing_api_integration(self) -> Dict[str, Any]: - """Framework for GCP Billing API integration.""" - # This would integrate with GCP Billing APIs for real billing data - # For now, provide a framework structure - - return { - "billing_account": "auto-detect", - "project_id": "auto-detect", - "billing_period": datetime.now().strftime("%Y-%m"), - "cost_to_date": 0.0, # Would fetch from API - "forecasted_cost": 0.0, # Would calculate based on usage - "budget_alerts": [], # Would fetch configured alerts - "cost_breakdown": { - "compute_engine": 0.0, - "storage": 0.0, - "networking": 0.0, - "other_services": 0.0, - }, - "api_available": False, # Would check API connectivity - "last_updated": datetime.now().isoformat(), - } - - def _calculate_cost_optimization_score(self) -> float: - """Calculate a cost optimization score (0-100).""" - # This would analyze various factors: - # - Resource utilization - # - Machine type appropriateness - # - Preemptible vs on-demand usage - # - Sustained use discount eligibility - # - Committed use discount opportunities - # For now, return a placeholder - return 78.0 diff --git a/clustrix/cost_providers/lambda_cloud.py b/clustrix/cost_providers/lambda_cloud.py deleted file mode 100644 index bbf2a475..00000000 --- a/clustrix/cost_providers/lambda_cloud.py +++ /dev/null @@ -1,322 +0,0 @@ -""" -Lambda Cloud cost monitoring implementation. -""" - -import logging -from datetime import datetime -from typing import Dict, List, Any, Optional - -from ..cost_monitoring import BaseCostMonitor, ResourceUsage, CostEstimate -from ..pricing_clients.lambda_pricing import LambdaPricingClient - -logger = logging.getLogger(__name__) - - -class LambdaCostMonitor(BaseCostMonitor): - """Cost monitoring for Lambda Cloud instances.""" - - def __init__(self, use_pricing_api: bool = True, api_key: Optional[str] = None): - super().__init__("Lambda Cloud") - self.use_pricing_api = use_pricing_api - - # Initialize pricing client - self.pricing_client = None - if use_pricing_api: - self.pricing_client = LambdaPricingClient() - if api_key: - self.pricing_client.authenticate(api_key) - - # Lambda Cloud pricing (as of 2025, approximate rates in USD/hour) - self.pricing = { - # Single GPU instances - "rtx6000ada": 0.75, - "a10": 0.60, - "a6000": 0.80, - "a100_40gb": 1.10, - "a100_80gb": 1.40, - "h100": 2.50, - # Multi-GPU instances - "2xa100_40gb": 2.20, - "4xa100_40gb": 4.40, - "8xa100_40gb": 8.80, - "2xa100_80gb": 2.80, - "4xa100_80gb": 5.60, - "8xa100_80gb": 11.20, - "8xh100": 20.00, - # CPU instances - "cpu_small": 0.10, - "cpu_medium": 0.20, - "cpu_large": 0.40, - # Default fallback - "default": 1.00, - } - - # Instance type metadata - self.instance_metadata = { - "rtx6000ada": { - "gpus": 1, - "gpu_memory": "48GB", - "cpu_cores": 14, - "ram": "46GB", - }, - "a10": {"gpus": 1, "gpu_memory": "24GB", "cpu_cores": 12, "ram": "46GB"}, - "a6000": {"gpus": 1, "gpu_memory": "48GB", "cpu_cores": 14, "ram": "46GB"}, - "a100_40gb": { - "gpus": 1, - "gpu_memory": "40GB", - "cpu_cores": 30, - "ram": "200GB", - }, - "a100_80gb": { - "gpus": 1, - "gpu_memory": "80GB", - "cpu_cores": 30, - "ram": "200GB", - }, - "h100": {"gpus": 1, "gpu_memory": "80GB", "cpu_cores": 26, "ram": "150GB"}, - "2xa100_40gb": { - "gpus": 2, - "gpu_memory": "80GB", - "cpu_cores": 60, - "ram": "400GB", - }, - "4xa100_40gb": { - "gpus": 4, - "gpu_memory": "160GB", - "cpu_cores": 120, - "ram": "800GB", - }, - "8xa100_40gb": { - "gpus": 8, - "gpu_memory": "320GB", - "cpu_cores": 240, - "ram": "1600GB", - }, - } - - def get_resource_usage(self) -> ResourceUsage: - """Get current resource utilization for Lambda Cloud instance.""" - # Get CPU and memory usage - cpu_percent, mem_used_mb, mem_total_mb, mem_percent = ( - self.get_cpu_memory_usage() - ) - - # Get GPU utilization - gpu_stats = self.get_gpu_utilization() - - return ResourceUsage( - cpu_percent=cpu_percent, - memory_used_mb=mem_used_mb, - memory_total_mb=mem_total_mb, - memory_percent=mem_percent, - gpu_stats=gpu_stats, - ) - - def estimate_cost(self, instance_type: str, hours_used: float) -> CostEstimate: - """Estimate cost for Lambda Cloud instance usage.""" - # Normalize instance type - instance_type = instance_type.lower().replace("-", "_") - - # Try to get pricing from API client first - hourly_rate = None - pricing_source = "hardcoded" - pricing_warning = None - - if self.pricing_client: - try: - api_rate = self.pricing_client.get_instance_pricing(instance_type) - if api_rate is not None: - hourly_rate = api_rate - pricing_source = "api" - logger.debug( - f"Using API pricing for {instance_type}: ${hourly_rate:.3f}/hour" - ) - else: - logger.debug( - f"No API pricing found for {instance_type}, using fallback" - ) - except Exception as e: - logger.warning(f"Error getting API pricing for {instance_type}: {e}") - - # Fall back to hardcoded pricing if API didn't work - if hourly_rate is None: - hourly_rate = self.pricing.get(instance_type, self.pricing["default"]) - pricing_source = "hardcoded" - if self.pricing_client and self.pricing_client.is_pricing_data_outdated(): - pricing_warning = ( - f"Using potentially outdated pricing data from " - f"{self.pricing_client._hardcoded_pricing_date}" - ) - if instance_type not in self.pricing: - # The "default" rate has nothing to do with this - # instance. Appended rather than assigned, so an - # outdated-data warning cannot displace the more - # important fact that the figure is a placeholder. - unknown = ( - f"Unrecognised instance type {instance_type!r}; " - f"priced at the placeholder default rate of " - f"${hourly_rate}/hr. This is not a real quote." - ) - logger.warning(unknown) - pricing_warning = ( - f"{pricing_warning} {unknown}" if pricing_warning else unknown - ) - - # Calculate estimated cost - estimated_cost = hourly_rate * hours_used - - return CostEstimate( - instance_type=instance_type, - hourly_rate=hourly_rate, - hours_used=hours_used, - estimated_cost=estimated_cost, - currency="USD", - last_updated=datetime.now(), - pricing_source=pricing_source, - pricing_warning=pricing_warning, - ) - - def get_pricing_info(self) -> Dict[str, float]: - """Get Lambda Cloud pricing information.""" - # Try to get comprehensive pricing from API first - if self.pricing_client: - try: - api_pricing = self.pricing_client.get_all_pricing() - if api_pricing: - logger.info("Returning Lambda Cloud pricing from API") - return api_pricing - except Exception as e: - logger.warning(f"Error getting all pricing from API: {e}") - - # Fall back to hardcoded pricing - logger.warning("Returning hardcoded Lambda Cloud pricing") - return self.pricing.copy() - - def get_instance_recommendations( - self, resource_usage: ResourceUsage, current_instance: Optional[str] = None - ) -> List[str]: - """Get instance type recommendations based on current usage.""" - recommendations = [] - - if not resource_usage.gpu_stats: - # No GPU usage detected - if resource_usage.cpu_percent < 50 and resource_usage.memory_percent < 50: - recommendations.append( - "Consider using CPU instances instead of GPU instances for this workload." - ) - return recommendations - - # Analyze GPU usage - gpu_utilizations = [ - gpu.get("utilization_percent", 0) for gpu in resource_usage.gpu_stats - ] - avg_gpu_util = ( - sum(gpu_utilizations) / len(gpu_utilizations) if gpu_utilizations else 0 - ) - - gpu_memory_utils = [ - gpu.get("memory_utilization_percent", 0) for gpu in resource_usage.gpu_stats - ] - avg_gpu_mem_util = ( - sum(gpu_memory_utils) / len(gpu_memory_utils) if gpu_memory_utils else 0 - ) - - # Single GPU recommendations - if len(resource_usage.gpu_stats) == 1: - if avg_gpu_util < 30: - recommendations.append( - "Low GPU utilization. Consider optimizing your code or using a smaller instance." - ) - elif avg_gpu_util > 95: - recommendations.append( - "High GPU utilization. Consider multi-GPU instances for better performance." - ) - - if avg_gpu_mem_util > 90: - recommendations.append( - "High GPU memory usage. Consider using GPU instances with more memory." - ) - elif avg_gpu_mem_util < 20: - recommendations.append( - "Low GPU memory usage. Consider using GPU instances with less memory." - ) - - # Multi-GPU recommendations - else: - underutilized_gpus = sum(1 for util in gpu_utilizations if util < 50) - if underutilized_gpus > len(gpu_utilizations) / 2: - recommendations.append( - f"{underutilized_gpus}/{len(gpu_utilizations)} GPUs are underutilized. " - "Consider using fewer GPUs or optimizing data parallelism." - ) - - # Cost-efficiency recommendations - if current_instance: - current_rate = self.pricing.get( - current_instance.lower().replace("-", "_"), 0 - ) - if ( - current_rate > 5.0 and avg_gpu_util < 60 - ): # Expensive instance with low utilization - recommendations.append( - "High-cost instance with low utilization. " - "Consider using spot instances or smaller instance types." - ) - - return recommendations - - def get_cost_optimization_tips(self) -> List[str]: - """Get general cost optimization tips for Lambda Cloud.""" - return [ - "Use spot instances for non-critical workloads (up to 50% savings)", - "Terminate instances immediately after completing jobs", - "Monitor GPU utilization and right-size instances accordingly", - "Use multi-GPU instances efficiently with proper data parallelism", - "Consider CPU instances for non-GPU workloads", - "Use mixed precision training to reduce memory requirements", - "Implement checkpointing to handle potential spot instance interruptions", - "Monitor costs regularly and set up budget alerts", - "Choose the right region based on pricing and latency requirements", - "Batch multiple experiments to maximize instance utilization", - ] - - def get_performance_metrics(self) -> Dict[str, Any]: - """Get comprehensive performance metrics for Lambda Cloud instances.""" - resource_usage = self.get_resource_usage() - - metrics = { - "cpu_utilization": resource_usage.cpu_percent, - "memory_utilization": resource_usage.memory_percent, - "memory_used_gb": resource_usage.memory_used_mb / 1024, - "memory_total_gb": resource_usage.memory_total_mb / 1024, - "timestamp": datetime.now().isoformat(), - } - - if resource_usage.gpu_stats: - metrics["gpu_count"] = len(resource_usage.gpu_stats) - metrics["gpu_utilization_avg"] = sum( - gpu.get("utilization_percent", 0) for gpu in resource_usage.gpu_stats - ) / len(resource_usage.gpu_stats) - metrics["gpu_memory_utilization_avg"] = sum( - gpu.get("memory_utilization_percent", 0) - for gpu in resource_usage.gpu_stats - ) / len(resource_usage.gpu_stats) - metrics["gpu_details"] = resource_usage.gpu_stats - - return metrics - - def estimate_monthly_cost( - self, instance_type: str, hours_per_day: float = 8 - ) -> Dict[str, Any]: - """Estimate monthly costs for different usage patterns.""" - instance_type = instance_type.lower().replace("-", "_") - hourly_rate = self.pricing.get(instance_type, self.pricing["default"]) - - return { - "hourly_rate": hourly_rate, - "daily_cost_8h": hourly_rate * hours_per_day, - "weekly_cost_40h": hourly_rate * 40, # 5 days * 8 hours - "monthly_cost_160h": hourly_rate * 160, # ~20 working days * 8 hours - "monthly_cost_24x7": hourly_rate * 24 * 30, # 24/7 usage - "instance_type": instance_type, - } diff --git a/clustrix/executor_cloud.py b/clustrix/executor_cloud.py deleted file mode 100644 index ea367372..00000000 --- a/clustrix/executor_cloud.py +++ /dev/null @@ -1,591 +0,0 @@ -"""Cloud provider workflow management for job execution. - -This module handles cloud-based job execution workflows including instance -provisioning, SSH-based execution, and cleanup for various cloud providers -(AWS, Azure, GCP, Lambda Labs, HuggingFace). -""" - -import os -import secrets -import shlex -import stat -import time -import tempfile -import logging -import threading -import uuid -from typing import Dict, Any, Optional, TYPE_CHECKING -from datetime import datetime, timezone - -import paramiko -import cloudpickle -import dill - -from .ssh_security import configure_host_key_policy -from .utils import key_capture_lines, result_signing_lines, verify_signed_payload - -if TYPE_CHECKING: - from .cloud_providers.base import CloudProvider - -logger = logging.getLogger(__name__) - -# What a cloud provider must implement before clustrix can run a job on it. -# `create_instance` is deliberately not on the CloudProvider ABC -- only -# LambdaCloudProvider provisions single instances -- so the gap is checked -# here, at submit time, instead of surfacing as a NotImplementedError from -# inside a background thread once the caller has already been told the job -# was accepted (#119). -REQUIRED_PROVIDER_METHODS = ( - "create_instance", - "get_cluster_status", - "get_cluster_config", -) - - -class CloudJobManager: - """Manages cloud-based job execution workflows.""" - - def __init__(self, config): - """Initialize cloud job manager. - - Args: - config: ClusterConfig instance with cloud provider settings - """ - self.config = config - self.active_jobs: Dict[str, Any] = {} - - def submit_cloud_job( - self, func_data: Dict[str, Any], job_config: Dict[str, Any], provider: str - ) -> str: - """ - Submit a job to a cloud provider. - - Args: - func_data: Serialized function and data - job_config: Job configuration parameters - provider: Cloud provider name ('lambda', 'aws', 'azure', 'gcp', 'huggingface') - - Returns: - Job ID for tracking - """ - # Generate unique job ID - job_id = f"{provider}_{uuid.uuid4().hex[:8]}" - - # Get provider instance - cloud_provider = self._get_cloud_provider_instance(provider, job_config) - self._check_provider_can_run_jobs(provider, cloud_provider) - - # Store job info for tracking - self.active_jobs[job_id] = { - "provider": provider, - "cloud_provider_instance": cloud_provider, - "func_data": func_data, - "job_config": job_config, - "status": "pending", - "created_at": datetime.now(timezone.utc).isoformat(), - "instance_id": None, - "ssh_config": None, - } - - # Start cloud job execution in background thread - def execute_cloud_job(): - try: - self._execute_cloud_job_workflow(job_id) - except Exception as e: - logger.error(f"Cloud job {job_id} failed: {e}") - self.active_jobs[job_id]["status"] = "failed" - self.active_jobs[job_id]["error"] = str(e) - - thread = threading.Thread(target=execute_cloud_job) - thread.daemon = True - thread.start() - - return job_id - - def _check_provider_can_run_jobs(self, provider: str, cloud_provider) -> None: - """Refuse a job the provider has no way of running. - - Raises: - NotImplementedError: if the provider cannot provision instances - RuntimeError: if the provider was never authenticated - """ - if cloud_provider is None: - raise NotImplementedError( - f"No cloud provider implementation was built for '{provider}'." - ) - - missing = [ - name - for name in REQUIRED_PROVIDER_METHODS - if not callable(getattr(cloud_provider, name, None)) - ] - if missing: - raise NotImplementedError( - f"The '{provider}' cloud provider cannot run clustrix jobs: " - f"{type(cloud_provider).__name__} does not implement " - f"{', '.join(missing)}. Of the built-in providers only " - "'lambda' provisions instances for job execution; for the " - "others, provision the machine yourself and use cluster_type " - "'ssh', or use cluster_type 'kubernetes'." - ) - - if not cloud_provider.is_authenticated(): - raise RuntimeError( - f"The '{provider}' cloud provider is not authenticated, so no " - "instance can be provisioned for this job. Supply its " - "credentials in the clustrix config or in the job config." - ) - - def _get_cloud_provider_instance( - self, provider: str, job_config: Dict[str, Any] - ) -> Optional["CloudProvider"]: - """Get cloud provider instance based on provider name.""" - cloud_provider: Optional["CloudProvider"] = None - - if provider == "lambda": - from .cloud_providers.lambda_cloud import LambdaCloudProvider - - cloud_provider = LambdaCloudProvider() - - # Authenticate with Lambda Cloud - api_key = job_config.get("lambda_api_key") or self.config.lambda_api_key - if api_key: - cloud_provider.authenticate(api_key=api_key) - - elif provider == "aws": - from .cloud_providers.aws import AWSProvider - - cloud_provider = AWSProvider() - - # Authenticate with AWS - aws_creds = { - "access_key_id": job_config.get("aws_access_key_id") - or self.config.aws_access_key_id, - "secret_access_key": job_config.get("aws_secret_access_key") - or self.config.aws_secret_access_key, - "region": job_config.get("aws_region") - or self.config.aws_region - or "us-east-1", - } - if aws_creds["access_key_id"] and aws_creds["secret_access_key"]: - cloud_provider.authenticate(**aws_creds) - - elif provider == "azure": - from .cloud_providers.azure import AzureProvider - - cloud_provider = AzureProvider() - - # Authenticate with Azure - azure_creds = { - "subscription_id": job_config.get("azure_subscription_id") - or self.config.azure_subscription_id, - "tenant_id": job_config.get("azure_tenant_id") - or self.config.azure_tenant_id, - "client_id": job_config.get("azure_client_id") - or self.config.azure_client_id, - "client_secret": job_config.get("azure_client_secret") - or self.config.azure_client_secret, - } - if all(azure_creds.values()): - cloud_provider.authenticate(**azure_creds) - - elif provider == "gcp": - from .cloud_providers.gcp import GCPProvider - - cloud_provider = GCPProvider() - - # Authenticate with GCP - gcp_creds = { - "project_id": job_config.get("gcp_project_id") - or self.config.gcp_project_id, - "service_account_key": job_config.get("gcp_service_account_key") - or self.config.gcp_service_account_key, - } - if gcp_creds["project_id"]: - cloud_provider.authenticate(**gcp_creds) - - elif provider == "huggingface": - from .cloud_providers.huggingface_spaces import HuggingFaceSpacesProvider - - cloud_provider = HuggingFaceSpacesProvider() - - # Authenticate with HuggingFace - hf_creds = { - "token": job_config.get("hf_token") or self.config.hf_token, - "username": job_config.get("hf_username") or self.config.hf_username, - } - if hf_creds["token"]: - cloud_provider.authenticate(**hf_creds) - else: - raise ValueError(f"Unsupported cloud provider: {provider}") - - return cloud_provider - - def _execute_cloud_job_workflow(self, job_id: str): - """Execute the complete cloud job workflow.""" - job_info = self.active_jobs[job_id] - cloud_provider = job_info["cloud_provider_instance"] - job_config = job_info["job_config"] - func_data = job_info["func_data"] - - try: - # Step 1: Create/provision cloud instance - job_info["status"] = "provisioning" - instance_config = self._create_cloud_instance( - cloud_provider, job_config, job_id - ) - job_info["instance_id"] = instance_config["instance_id"] - - # Step 2: Wait for instance to be ready - job_info["status"] = "waiting_for_ready" - ssh_config = self._wait_for_instance_ready( - cloud_provider, instance_config, job_config - ) - job_info["ssh_config"] = ssh_config - - # Step 3: Execute job via SSH - job_info["status"] = "executing" - result = self._execute_job_on_cloud_instance( - ssh_config, func_data, job_config, job_id - ) - job_info["result"] = result - job_info["status"] = "completed" - - except Exception as e: - job_info["status"] = "failed" - job_info["error"] = str(e) - logger.error(f"Cloud job workflow failed for {job_id}: {e}") - finally: - # Step 4: Optional cleanup - terminate instance if configured - if job_config.get("terminate_on_completion", True): - try: - self._cleanup_cloud_instance(cloud_provider, job_info) - except Exception as e: - logger.warning( - f"Failed to cleanup cloud instance for job {job_id}: {e}" - ) - - def _create_cloud_instance( - self, cloud_provider, job_config: Dict[str, Any], job_id: str - ) -> Dict[str, Any]: - """Create cloud instance for job execution.""" - instance_name = f"clustrix-{job_id}" - - # The provider was checked for create_instance at submit time, so - # there is no "does it support this?" branch to take here. - instance_type = job_config.get( - "instance_type", "gpu_1x_a10" - ) # Default for Lambda - region = job_config.get("region", "us-east-1") - - return cloud_provider.create_instance( - instance_name=instance_name, instance_type=instance_type, region=region - ) - - def _wait_for_instance_ready( - self, - cloud_provider, - instance_config: Dict[str, Any], - job_config: Dict[str, Any], - ) -> Dict[str, Any]: - """Wait for cloud instance to be ready and return SSH configuration.""" - instance_id = instance_config["instance_id"] - max_wait_time = job_config.get( - "instance_startup_timeout", 300 - ) # 5 minutes default - check_interval = 10 # seconds - elapsed = 0 - - while elapsed < max_wait_time: - # Only the status poll is retried. An instance that has reached a - # terminal state, or one that is up but whose connection details - # cannot be read, is not going to improve -- and both of those - # raises used to be caught by this loop's own except clause and - # retried until the timeout, so the real reason arrived five - # minutes late wearing a "not ready" message. - try: - status_info = cloud_provider.get_cluster_status(instance_id) - except Exception as e: - if elapsed + check_interval >= max_wait_time: - raise RuntimeError( - f"Instance {instance_id} not ready within " - f"{max_wait_time}s: {e}" - ) from e - logger.warning( - f"Could not read the status of instance {instance_id}, " - f"retrying: {e}" - ) - status_info = {} - - status = status_info.get("status") - - if status == "active": - # Instance is ready, get SSH configuration - cluster_config = cloud_provider.get_cluster_config(instance_id) - - return { - "host": cluster_config["cluster_host"], - "username": cluster_config.get("username", "ubuntu"), - "port": cluster_config.get("cluster_port", 22), - "key_file": job_config.get("key_file", "~/.ssh/id_rsa"), - } - - if status in ("failed", "terminated"): - raise RuntimeError(f"Instance {instance_id} failed to start: {status}") - - time.sleep(check_interval) - elapsed += check_interval - - raise RuntimeError( - f"Instance {instance_id} not ready within {max_wait_time} seconds" - ) - - def _execute_job_on_cloud_instance( - self, - ssh_config: Dict[str, Any], - func_data: Dict[str, Any], - job_config: Dict[str, Any], - job_id: str, - ) -> Any: - """Execute job on cloud instance via SSH.""" - # Create temporary SSH client for cloud instance - ssh_client = paramiko.SSHClient() - # The last host-key site in the package that had not been routed - # through ssh_security: a cloud instance's key was trusted on sight, - # which is a machine-in-the-middle away from someone else's job. - configure_host_key_policy(ssh_client, self.config) - - try: - # Connect to cloud instance - ssh_client.connect( - hostname=ssh_config["host"], - username=ssh_config["username"], - port=ssh_config["port"], - key_filename=os.path.expanduser(ssh_config["key_file"]), - timeout=30, - ) - - # Create SFTP client - sftp_client = ssh_client.open_sftp() - - # Create remote work directory 0700 and drop a per-job signing - # key inside it, exactly as the scheduler backends do. The result - # this instance produces is deserialized with dill on the - # submitting machine, and dill.loads executes code, so it has to - # be authenticated -- this path had no key and no check at all. - remote_work_dir = f"/tmp/clustrix_cloud_{job_id}" - sftp_client.mkdir(remote_work_dir, mode=0o700) - result_key = secrets.token_hex(32) - key_path = f"{remote_work_dir}/.clustrix_result_key" - with sftp_client.open(key_path, "w") as key_handle: - key_handle.write(result_key) - sftp_client.chmod(key_path, stat.S_IRUSR | stat.S_IWUSR) - - # Upload function data - with tempfile.NamedTemporaryFile(suffix=".pkl", delete=False) as f: - cloudpickle.dump(func_data, f) - temp_pickle_path = f.name - - try: - sftp_client.put(temp_pickle_path, f"{remote_work_dir}/func_data.pkl") - finally: - os.unlink(temp_pickle_path) - - # Create and upload execution script - execution_script = self._create_cloud_execution_script( - remote_work_dir, job_config - ) - script_path = f"{remote_work_dir}/execute_job.py" - - with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f: - f.write(execution_script) - temp_script_path = f.name - - try: - sftp_client.put(temp_script_path, script_path) - finally: - os.unlink(temp_script_path) - - # Execute job. The key is read from the 0600 file rather than - # passed on the command line, which any user on the box can read - # out of /proc. - quoted_dir = shlex.quote(remote_work_dir) - stdin, stdout, stderr = ssh_client.exec_command( - f"cd {quoted_dir} && " - f"export CLUSTRIX_RESULT_KEY=$(cat {shlex.quote(key_path)}) && " - "python execute_job.py" - ) - - # Wait for completion - exit_status = stdout.channel.recv_exit_status() - stderr_data = stderr.read().decode() - - if exit_status != 0: - raise RuntimeError(f"Job execution failed: {stderr_data}") - - # Download result - result_path = f"{remote_work_dir}/result.pkl" - with tempfile.NamedTemporaryFile(delete=False) as f: - temp_result_path = f.name - - try: - sftp_client.get(result_path, temp_result_path) - with open(temp_result_path, "rb") as f: - payload = f.read() - finally: - os.unlink(temp_result_path) - - try: - with sftp_client.open(f"{result_path}.hmac") as tag_handle: - tag = tag_handle.read().decode() - except IOError: - # Absent signature: verify_signed_payload refuses on an empty - # tag, which is what an unsigned result has to mean. - tag = "" - - verify_signed_payload(payload, tag, result_key, f"Cloud job {job_id}") - # dill: the worker writes this with dill (see - # _create_cloud_execution_script), and stdlib pickle would rebuild - # __main__ classes instead of reusing the caller's. Safe to - # deserialize only because the bytes just verified against the - # per-job key. - result = dill.loads(payload) - - return result - - finally: - # Cleanup - try: - sftp_client.close() - except Exception: - pass - ssh_client.close() - - def _create_cloud_execution_script( - self, remote_work_dir: str, job_config: Dict[str, Any] - ) -> str: - """Create Python execution script for cloud instance.""" - # The one signing implementation, shared with the SSH/SLURM job - # scripts; `_ser` is the dill alias this script already binds. - signing = "\n".join(result_signing_lines(indent=" " * 8, serializer="_ser")) - # Capture the signing key and drop it from the environment before the - # user's function -- and anything it imports -- gets to run. - capture = "\n".join(key_capture_lines()) - # The job directory is interpolated into *Python source*, so it needs - # Python-literal quoting, not bare quotes: repr() escapes an embedded - # quote (which would otherwise close the literal and let the rest of - # the value run as code) and any backslash. - func_data_literal = repr(f"{remote_work_dir}/func_data.pkl") - error_literal = repr(f"{remote_work_dir}/error.pkl") - return f"""#!/usr/bin/env python3 -import sys -import os -import pickle -import cloudpickle -import traceback - -{capture} - -def main(): - try: - # Load function data - with open({func_data_literal}, 'rb') as f: - func_data = cloudpickle.load(f) - - # Unpack what serialize_function() actually produced. It stores the - # function as dill (or cloudpickle) bytes under "function", and the - # arguments as pickle bytes -- not as live objects. Reading 'func' - # here raised KeyError on the first line of every cloud job, which is - # proof this path had never run. - try: - import dill as _ser - except ImportError: - _ser = cloudpickle - try: - func = _ser.loads(func_data['function']) - except Exception: - func = cloudpickle.loads(func_data['function']) - # _ser, not stdlib pickle: args may carry classes defined in the - # caller's __main__, which pickle can only store by qualified name. - args = _ser.loads(func_data['args']) - kwargs = _ser.loads(func_data['kwargs']) - - result = func(*args, **kwargs) - - # Save result, signed with the per-job key. Written with _ser (dill), - # which is what the caller reads it back with -- it used to be written - # with stdlib pickle under a comment claiming dill, and with no - # signature at all. -{signing} - - print("Job completed successfully") - - except Exception as e: - print(f"Job failed: {{e}}") - traceback.print_exc() - - # Save error - with open({error_literal}, 'wb') as f: - pickle.dump({{'error': str(e), 'traceback': traceback.format_exc()}}, f) - - sys.exit(1) - -if __name__ == "__main__": - main() -""" - - def _cleanup_cloud_instance(self, cloud_provider, job_info: Dict[str, Any]): - """Clean up cloud instance after job completion.""" - instance_id = job_info.get("instance_id") - if instance_id and hasattr(cloud_provider, "delete_cluster"): - cloud_provider.delete_cluster(instance_id) - logger.info(f"Cleaned up cloud instance {instance_id}") - - def get_cloud_job_status(self, job_id: str) -> str: - """Get cloud job status.""" - if job_id not in self.active_jobs: - return "unknown" - - return self.active_jobs[job_id].get("status", "unknown") - - def wait_for_cloud_result(self, job_id: str) -> Any: - """Wait for cloud job result.""" - job_info = self.active_jobs.get(job_id) - if not job_info: - raise ValueError(f"Unknown cloud job ID: {job_id}") - - poll_interval = getattr(self.config, "job_poll_interval", 10) - - while job_info.get("status") not in ["completed", "failed"]: - time.sleep(poll_interval) - - if job_info.get("status") == "failed": - error = job_info.get("error", "Unknown error") - raise RuntimeError(f"Cloud job {job_id} failed: {error}") - - return job_info.get("result") - - def cancel_cloud_job(self, job_id: str): - """Cancel a running cloud job.""" - if job_id not in self.active_jobs: - return - - job_info = self.active_jobs[job_id] - cloud_provider = job_info.get("cloud_provider_instance") - instance_id = job_info.get("instance_id") - - if cloud_provider and instance_id: - try: - # Attempt to terminate the cloud instance - if hasattr(cloud_provider, "delete_cluster"): - cloud_provider.delete_cluster(instance_id) - logger.info( - f"Terminated cloud instance {instance_id} for job {job_id}" - ) - except Exception as e: - logger.warning( - f"Failed to terminate cloud instance for job {job_id}: {e}" - ) - - # Mark job as cancelled - job_info["status"] = "cancelled" diff --git a/clustrix/executor_connections.py b/clustrix/executor_connections.py index 810c6835..ec0a5c13 100644 --- a/clustrix/executor_connections.py +++ b/clustrix/executor_connections.py @@ -1,15 +1,14 @@ -"""SSH and Kubernetes connection management for cluster execution. +"""SSH connection management for cluster execution. -This module handles establishing and managing connections to different cluster types, -including SSH connections to traditional HPC clusters and Kubernetes cluster setup. +This module handles establishing and managing SSH connections to the cluster +types clustrix supports: ``ssh`` and ``slurm``. (``local`` needs no +connection and ``huggingface`` talks to an HTTP API.) """ import os -import time -import tempfile import logging -from typing import Any, Dict, Optional -import yaml +from typing import Optional + import paramiko from clustrix.ssh_security import configure_host_key_policy @@ -18,7 +17,7 @@ class ConnectionManager: - """Manages SSH and Kubernetes connections for cluster execution.""" + """Manages SSH connections for cluster execution.""" def __init__(self, config): """Initialize connection manager. @@ -29,7 +28,6 @@ def __init__(self, config): self.config = config self.ssh_client = None self.sftp_client = None - self.k8s_client = None self._remote_home = None # cache for resolve_remote_path() def setup_ssh_connection(self): @@ -81,143 +79,7 @@ def setup_ssh_connection(self): self.ssh_client.connect(**connect_kwargs) self.sftp_client = self.ssh_client.open_sftp() - def setup_kubernetes(self): - """Setup Kubernetes client with optional cloud provider auto-configuration.""" - try: - from kubernetes import client, config # type: ignore - - # Try Kubernetes auto-provisioning if enabled (NEW) - if ( - self.config.auto_provision_k8s - and self.config.cluster_type == "kubernetes" - ): - try: - from .kubernetes.cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, - ) - - logger.info("🚀 Starting Kubernetes cluster auto-provisioning...") - - # Create cluster specification from config - cluster_name = ( - self.config.k8s_cluster_name - or f"clustrix-auto-{int(time.time())}" - ) - - spec = ClusterSpec( - provider=self.config.k8s_provider, - cluster_name=cluster_name, - region=self.config.k8s_region - or self.config.cloud_region - or "us-west-2", - node_count=self.config.k8s_node_count, - node_type=self.config.k8s_node_type, - kubernetes_version=self.config.k8s_version, - from_scratch=self.config.k8s_from_scratch, - ) - - # Provision cluster - provisioner = KubernetesClusterProvisioner(self.config) - cluster_info = provisioner.provision_cluster_if_needed(spec) - - # Store provisioner instance for lifecycle management - self._k8s_provisioner = provisioner - self._k8s_cluster_info = cluster_info - - # Update config with provisioned cluster details - self.config.cluster_host = cluster_info.get("endpoint", "") - self.config.k8s_cluster_name = cluster_info["cluster_id"] - - # Configure kubectl with the provisioned cluster - self._configure_kubectl_for_provisioned_cluster(cluster_info) - - logger.info( - f"✅ Kubernetes cluster auto-provisioned: {cluster_info['cluster_id']}" - ) - - except Exception as e: - logger.error(f"❌ Kubernetes auto-provisioning failed: {e}") - # Continue with existing configuration - logger.info("Continuing with existing Kubernetes configuration...") - - # Try cloud provider auto-configuration if enabled - elif ( - self.config.cloud_auto_configure - and self.config.cluster_type == "kubernetes" - ): - try: - # Import CloudProviderManager from the renamed module - from .cloud_provider_manager import CloudProviderManager - - cloud_manager = CloudProviderManager(self.config) - result = cloud_manager.auto_configure() - - if result.get("auto_configured"): - logger.info( - f"Auto-configured {result.get('provider')} cluster: {result.get('cluster_name')}" - ) - else: - logger.info( - f"Cloud auto-configuration skipped: {result.get('reason', 'Unknown')}" - ) - if "error" in result: - logger.warning( - f"Auto-configuration error: {result['error']}" - ) - - except Exception as e: - logger.warning(f"Cloud provider auto-configuration failed: {e}") - # Continue with manual configuration - - config.load_kube_config() - self.k8s_client = client.ApiClient() - except ImportError: - raise ImportError( - "kubernetes package required for Kubernetes cluster support" - ) - - def _configure_kubectl_for_provisioned_cluster(self, cluster_info: Dict[str, Any]): - """Configure kubectl with credentials for auto-provisioned cluster.""" - logger.info( - f"🔧 Configuring kubectl for cluster: {cluster_info.get('cluster_id', 'unknown')}" - ) - - try: - # Get kubectl config from cluster info - kubectl_config = cluster_info.get("kubectl_config") - if not kubectl_config: - logger.warning("No kubectl config provided in cluster info") - return - - # Write kubectl config to temporary file - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(kubectl_config, f, default_flow_style=False) - temp_config_path = f.name - - try: - from kubernetes import config # type: ignore - - # Load the configuration - config.load_kube_config(config_file=temp_config_path) - logger.info( - "✅ kubectl configured successfully for auto-provisioned cluster" - ) - - # Store config path for cleanup later - self._k8s_temp_config_path = temp_config_path - - except Exception as e: - logger.error(f"Failed to load kubectl config: {e}") - # Clean up temp file if loading failed - os.unlink(temp_config_path) - raise - except Exception as e: - logger.error(f"Failed to configure kubectl for provisioned cluster: {e}") - raise def execute_remote_command(self, command: str, check: bool = False) -> tuple: """Execute command on remote cluster. @@ -338,12 +200,9 @@ def remote_file_exists(self, remote_path: str) -> bool: def connect(self): """Establish connection to cluster (for manual connection).""" - if self.config.cluster_type in ["slurm", "pbs", "sge", "ssh"]: + if self.config.cluster_type in ["slurm", "ssh"]: if not self.ssh_client: self.setup_ssh_connection() - elif self.config.cluster_type == "kubernetes": - if not hasattr(self, "k8s_client") or self.k8s_client is None: - self.setup_kubernetes() def disconnect(self): """Disconnect from cluster.""" @@ -357,104 +216,5 @@ def disconnect(self): self.ssh_client.close() self.ssh_client = None - def cleanup_auto_provisioned_cluster(self): - """Clean up auto-provisioned Kubernetes cluster.""" - logger.info("🧹 Cleaning up auto-provisioned Kubernetes cluster") - - try: - # Clean up temporary kubectl config - if hasattr(self, "_k8s_temp_config_path") and self._k8s_temp_config_path: - if os.path.exists(self._k8s_temp_config_path): - os.unlink(self._k8s_temp_config_path) - logger.info("✅ Temporary kubectl config cleaned up") - - # Clean up provisioned cluster if enabled - if ( - hasattr(self, "_k8s_provisioner") - and hasattr(self, "_k8s_cluster_info") - and self._k8s_provisioner - and self._k8s_cluster_info - ): - - cluster_name = self._k8s_cluster_info.get("cluster_id") - if cluster_name and getattr(self.config, "k8s_cleanup_on_exit", True): - logger.info( - f"🗑️ Destroying auto-provisioned cluster: {cluster_name}" - ) - - success = self._k8s_provisioner.destroy_cluster_infrastructure( - cluster_name - ) - if success: - logger.info(f"✅ Cluster {cluster_name} destroyed successfully") - else: - logger.warning( - f"⚠️ Failed to fully destroy cluster {cluster_name}" - ) - else: - if cluster_name: - logger.info( - f"ℹ️ Preserving auto-provisioned cluster: {cluster_name}" - ) - - except Exception as e: - logger.error(f"❌ Error during cluster cleanup: {e}") - - def get_cluster_status(self) -> Dict[str, Any]: - """Get status of managed Kubernetes cluster.""" - if not hasattr(self, "_k8s_provisioner") or not hasattr( - self, "_k8s_cluster_info" - ): - return {"status": "NO_MANAGED_CLUSTER", "ready": False} - - cluster_name = self._k8s_cluster_info.get("cluster_id") - if not cluster_name: - return {"status": "UNKNOWN", "ready": False} - - try: - status = self._k8s_provisioner.get_cluster_status(cluster_name) - return { - "status": status.get("status", "UNKNOWN"), - "ready": status.get("ready_for_jobs", False), - "cluster_name": cluster_name, - "provider": self._k8s_cluster_info.get("provider"), - "endpoint": self._k8s_cluster_info.get("endpoint"), - } - except Exception as e: - logger.error(f"Error getting cluster status: {e}") - return {"status": "ERROR", "ready": False, "error": str(e)} - - def ensure_cluster_ready(self, timeout: int = 900) -> bool: - """Ensure auto-provisioned cluster is ready for job execution.""" - if not hasattr(self, "_k8s_provisioner") or not hasattr( - self, "_k8s_cluster_info" - ): - logger.warning("No managed cluster to check readiness for") - return True # Assume external cluster is ready - - cluster_name = self._k8s_cluster_info.get("cluster_id") - if not cluster_name: - return False - - logger.info(f"⏳ Ensuring cluster {cluster_name} is ready for jobs...") - - start_time = time.time() - - while time.time() - start_time < timeout: - try: - status = self.get_cluster_status() - if status.get("ready"): - logger.info(f"✅ Cluster {cluster_name} is ready for jobs") - return True - - logger.info(f"Cluster status: {status.get('status')} - waiting...") - time.sleep(30) # Wait 30 seconds between checks - except Exception as e: - logger.error(f"Error checking cluster readiness: {e}") - time.sleep(10) - logger.error( - f"❌ Cluster {cluster_name} did not become ready within {timeout}s" - ) - return False diff --git a/clustrix/executor_core.py b/clustrix/executor_core.py index ae30f8d8..399ed8c9 100644 --- a/clustrix/executor_core.py +++ b/clustrix/executor_core.py @@ -1,7 +1,7 @@ """Core ClusterExecutor class that coordinates all execution types. This module provides the main ClusterExecutor class that acts as a coordinator -for different job execution backends (schedulers, Kubernetes, cloud providers). +for the supported job execution backends: local, ssh, slurm and huggingface. """ import shlex @@ -16,8 +16,6 @@ from .executor_connections import ConnectionManager from .executor_schedulers import SchedulerManager -from .executor_kubernetes import KubernetesJobManager -from .executor_cloud import CloudJobManager from .hf_jobs import HFJobsManager from .local_executor import LocalJobManager from .utils import verify_signed_payload @@ -39,8 +37,6 @@ def __init__(self, config): # Initialize sub-managers self.connection_manager = ConnectionManager(config) self.scheduler_manager = SchedulerManager(config, self.connection_manager) - self.k8s_manager = KubernetesJobManager(config, self.connection_manager) - self.cloud_manager = CloudJobManager(config) self.hf_jobs_manager = HFJobsManager(config) self.local_manager = LocalJobManager(config) @@ -60,28 +56,6 @@ def submit_job(self, func_data: Dict[str, Any], job_config: Dict[str, Any]) -> s Returns: Job ID for tracking """ - # Check if this is a cloud provider job (but not auto-provisioned Kubernetes) - provider = job_config.get("provider") - if provider is not None and not ( - self.config.cluster_type == "kubernetes" - and getattr(self.config, "auto_provision_k8s", False) - ): - # If provider is specified and not auto-provisioned K8s, use cloud provider routing - supported_providers = ["lambda", "aws", "azure", "gcp", "huggingface"] - if provider in supported_providers: - job_id = self.cloud_manager.submit_cloud_job( - func_data, job_config, provider - ) - # Track in combined active jobs - self.active_jobs[job_id] = {"manager": "cloud", "job_id": job_id} - return job_id - else: - raise ValueError( - f"Unsupported cloud provider: {provider}. Supported providers: {supported_providers}" - ) - - # If no provider specified, use traditional cluster routing - # "local" runs the function on this machine. It is advertised in the # widget's cluster-type dropdown and in the docs, but had no branch # here and raised "Unsupported cluster type: local" (#120). Like @@ -107,18 +81,6 @@ def submit_job(self, func_data: Dict[str, Any], job_config: Dict[str, Any]) -> s job_id = self.scheduler_manager.submit_slurm_job(func_data, job_config) self.active_jobs[job_id] = {"manager": "scheduler", "job_id": job_id} return job_id - elif self.config.cluster_type == "pbs": - job_id = self.scheduler_manager.submit_pbs_job(func_data, job_config) - self.active_jobs[job_id] = {"manager": "scheduler", "job_id": job_id} - return job_id - elif self.config.cluster_type == "sge": - job_id = self.scheduler_manager.submit_sge_job(func_data, job_config) - self.active_jobs[job_id] = {"manager": "scheduler", "job_id": job_id} - return job_id - elif self.config.cluster_type == "kubernetes": - job_id = self.k8s_manager.submit_k8s_job(func_data, job_config) - self.active_jobs[job_id] = {"manager": "kubernetes", "job_id": job_id} - return job_id elif self.config.cluster_type == "ssh": job_id = self.scheduler_manager.submit_ssh_job(func_data, job_config) self.active_jobs[job_id] = {"manager": "scheduler", "job_id": job_id} @@ -140,15 +102,7 @@ def wait_for_result(self, job_id: str) -> Any: if job_id in self.active_jobs: manager_type = self.active_jobs[job_id]["manager"] - if manager_type == "cloud": - result = self.cloud_manager.wait_for_cloud_result(job_id) - del self.active_jobs[job_id] - return result - elif manager_type == "kubernetes": - result = self.k8s_manager.wait_for_k8s_result(job_id) - del self.active_jobs[job_id] - return result - elif manager_type == "huggingface": + if manager_type == "huggingface": result = self.hf_jobs_manager.wait_for_result(job_id) del self.active_jobs[job_id] return result @@ -166,18 +120,7 @@ def wait_for_result(self, job_id: str) -> Any: # This handles backward compatibility if job_id.startswith("local_"): return self.local_manager.wait_for_result(job_id) - if ( - job_id.startswith("lambda_") - or job_id.startswith("aws_") - or job_id.startswith("azure_") - or job_id.startswith("gcp_") - or job_id.startswith("huggingface_") - ): - return self.cloud_manager.wait_for_cloud_result(job_id) - elif job_id.startswith("clustrix-job-"): - return self.k8s_manager.wait_for_k8s_result(job_id) - else: - return self._wait_for_scheduler_result(job_id) + return self._wait_for_scheduler_result(job_id) def _verify_result_signature( self, job_id: str, remote_dir: str, payload: bytes @@ -219,7 +162,7 @@ def _verify_result_signature( verify_signed_payload(payload, tag, key, f"Job {job_id}") def _wait_for_scheduler_result(self, job_id: str) -> Any: - """Wait for scheduler job result (SLURM/PBS/SGE/SSH).""" + """Wait for scheduler job result (SLURM/SSH).""" job_info = self.scheduler_manager.active_jobs.get(job_id) if not job_info: raise ValueError(f"Unknown job ID: {job_id}") @@ -292,11 +235,7 @@ def get_job_status(self, job_id: str) -> str: if job_id in self.active_jobs: manager_type = self.active_jobs[job_id]["manager"] - if manager_type == "cloud": - return self.cloud_manager.get_cloud_job_status(job_id) - elif manager_type == "kubernetes": - return self.k8s_manager.check_k8s_job_status(job_id) - elif manager_type == "huggingface": + if manager_type == "huggingface": return self.hf_jobs_manager.get_job_status(job_id) elif manager_type == "local": return self.local_manager.get_job_status(job_id) @@ -306,18 +245,7 @@ def get_job_status(self, job_id: str) -> str: # Fallback for untracked jobs if job_id.startswith("local_"): return self.local_manager.get_job_status(job_id) - if ( - job_id.startswith("lambda_") - or job_id.startswith("aws_") - or job_id.startswith("azure_") - or job_id.startswith("gcp_") - or job_id.startswith("huggingface_") - ): - return self.cloud_manager.get_cloud_job_status(job_id) - elif job_id.startswith("clustrix-job-"): - return self.k8s_manager.check_k8s_job_status(job_id) - else: - return self.scheduler_manager.check_job_status(job_id) + return self.scheduler_manager.check_job_status(job_id) def get_result(self, job_id: str) -> Any: """Get result (alias for wait_for_result).""" @@ -344,16 +272,7 @@ def cancel_job(self, job_id: str): ) del self.active_jobs[job_id] return - if manager_type == "cloud": - self.cloud_manager.cancel_cloud_job(job_id) - del self.active_jobs[job_id] - return - elif manager_type == "kubernetes": - self.k8s_manager.cleanup_k8s_job(job_id) - del self.k8s_manager.active_jobs[job_id] - del self.active_jobs[job_id] - return - elif manager_type == "scheduler": + if manager_type == "scheduler": self.scheduler_manager.cancel_job(job_id) del self.active_jobs[job_id] return @@ -362,18 +281,7 @@ def cancel_job(self, job_id: str): if job_id.startswith("local_"): self.local_manager.cancel_job(job_id) return - if ( - job_id.startswith("lambda_") - or job_id.startswith("aws_") - or job_id.startswith("azure_") - or job_id.startswith("gcp_") - or job_id.startswith("huggingface_") - ): - self.cloud_manager.cancel_cloud_job(job_id) - elif job_id.startswith("clustrix-job-"): - self.k8s_manager.cleanup_k8s_job(job_id) - else: - self.scheduler_manager.cancel_job(job_id) + self.scheduler_manager.cancel_job(job_id) def connect(self): """Establish connection to cluster (for manual connection).""" @@ -396,30 +304,11 @@ def execute(self, func, args: tuple, kwargs: dict) -> Any: job_id = self.submit_job(func_data, job_config) return self.wait_for_result(job_id) - def cleanup_auto_provisioned_cluster(self): - """Clean up auto-provisioned Kubernetes cluster.""" - self.connection_manager.cleanup_auto_provisioned_cluster() - def get_cluster_status(self) -> Dict[str, Any]: - """Get status of managed Kubernetes cluster.""" - return self.connection_manager.get_cluster_status() - def ensure_cluster_ready(self, timeout: int = 900) -> bool: - """Ensure auto-provisioned cluster is ready for job execution.""" - return self.connection_manager.ensure_cluster_ready(timeout) def __del__(self): """Cleanup resources.""" - # Clean up auto-provisioned cluster if configured to do so - if hasattr(self.connection_manager, "_k8s_provisioner") and getattr( - self.config, "k8s_cleanup_on_exit", True - ): - try: - self.cleanup_auto_provisioned_cluster() - except Exception as e: - # Don't raise exceptions in destructor - logger.error(f"Error during cluster cleanup in destructor: {e}") - self.disconnect() # Backward compatibility properties and methods @@ -443,23 +332,12 @@ def sftp_client(self, value): """Set SFTP client for backward compatibility.""" self.connection_manager.sftp_client = value - @property - def k8s_client(self): - """Access to Kubernetes client for backward compatibility.""" - return self.connection_manager.k8s_client - @k8s_client.setter - def k8s_client(self, value): - """Set Kubernetes client for backward compatibility.""" - self.connection_manager.k8s_client = value def _setup_ssh_connection(self): """Backward compatibility method.""" return self.connection_manager.setup_ssh_connection() - def _setup_kubernetes(self): - """Backward compatibility method.""" - return self.connection_manager.setup_kubernetes() def _execute_remote_command(self, command: str) -> tuple: """Backward compatibility method.""" @@ -509,18 +387,13 @@ def _get_error_log(self, job_id: str) -> str: manager_type = self.active_jobs[job_id]["manager"] if manager_type == "scheduler": return self.scheduler_manager.get_error_log(job_id) - elif manager_type == "kubernetes": - return self.k8s_manager.get_k8s_error_log(job_id) elif manager_type == "local": return self.local_manager.get_error_log(job_id) # Fallback for untracked jobs if job_id.startswith("local_"): return self.local_manager.get_error_log(job_id) - if job_id.startswith("clustrix-job-"): - return self.k8s_manager.get_k8s_error_log(job_id) - else: - return self.scheduler_manager.get_error_log(job_id) + return self.scheduler_manager.get_error_log(job_id) def _extract_original_exception(self, job_id: str) -> Optional[Exception]: """Backward compatibility method.""" @@ -529,14 +402,9 @@ def _extract_original_exception(self, job_id: str) -> Optional[Exception]: manager_type = self.active_jobs[job_id]["manager"] if manager_type == "scheduler": return self.scheduler_manager.extract_original_exception(job_id) - elif manager_type == "kubernetes": - return self.k8s_manager.extract_k8s_exception(job_id) # Fallback for untracked jobs - if job_id.startswith("clustrix-job-"): - return self.k8s_manager.extract_k8s_exception(job_id) - else: - return self.scheduler_manager.extract_original_exception(job_id) + return self.scheduler_manager.extract_original_exception(job_id) def _submit_slurm_job( self, func_data: Dict[str, Any], job_config: Dict[str, Any] @@ -544,34 +412,10 @@ def _submit_slurm_job( """Backward compatibility method.""" return self.scheduler_manager.submit_slurm_job(func_data, job_config) - def _submit_pbs_job( - self, func_data: Dict[str, Any], job_config: Dict[str, Any] - ) -> str: - """Backward compatibility method.""" - return self.scheduler_manager.submit_pbs_job(func_data, job_config) - - def _submit_sge_job( - self, func_data: Dict[str, Any], job_config: Dict[str, Any] - ) -> str: - """Backward compatibility method.""" - return self.scheduler_manager.submit_sge_job(func_data, job_config) - - def _submit_k8s_job( - self, func_data: Dict[str, Any], job_config: Dict[str, Any] - ) -> str: - """Backward compatibility method.""" - return self.k8s_manager.submit_k8s_job(func_data, job_config) - def _check_slurm_status(self, job_id: str) -> str: """Backward compatibility method.""" return self.scheduler_manager.status_manager._check_slurm_job_status_robust( job_id, self.scheduler_manager.active_jobs ) - def _check_pbs_status(self, job_id: str) -> str: - """Backward compatibility method.""" - return self.scheduler_manager.status_manager._check_pbs_status(job_id) - def _check_sge_status(self, job_id: str) -> str: - """Backward compatibility method.""" - return self.scheduler_manager.status_manager._check_sge_status(job_id) diff --git a/clustrix/executor_kubernetes.py b/clustrix/executor_kubernetes.py deleted file mode 100644 index cdf9d305..00000000 --- a/clustrix/executor_kubernetes.py +++ /dev/null @@ -1,594 +0,0 @@ -"""Kubernetes-specific job execution operations. - -This module handles Kubernetes job submission, monitoring, and result retrieval -using containerized Python execution. -""" - -import time -import base64 -import hashlib -import hmac -import random -import secrets -import logging -from typing import Dict, Any, Optional - -import cloudpickle -import dill -from .utils import normalize_memory - -logger = logging.getLogger(__name__) - -RESULT_PREFIX = "CLUSTRIX_RESULT_B64:" -SIGNATURE_PREFIX = "CLUSTRIX_RESULT_HMAC:" - - -def build_worker_program(func_data_b64: str) -> str: - """The Python program the Kubernetes worker container runs. - - Kept separate from the Job manifest so it can be executed directly -- - ``python -c build_worker_program(...)`` runs the real worker on any - machine, which is the only way to test this path without a cluster. - - The program writes its result as a base64 pickle plus an HMAC over those - exact bytes, keyed by ``CLUSTRIX_RESULT_KEY`` from the environment. It - used to ``print(f'CLUSTRIX_RESULT:{result}')`` -- the *repr* of the - result -- which the caller then put through ``ast.literal_eval``. Anything - without a literal repr (a numpy array, a dataclass, any object) came back - as a string of its repr, silently, and the caller could not tell that from - a real answer. - """ - return f""" -import base64 -import cloudpickle -import traceback -import pickle -import sys -import types - -# Fix for Python 2/3 compatibility -import builtins -sys.modules['__builtin__'] = builtins - -try: - # Decode and deserialize function data - func_data_b64 = '{func_data_b64}' - func_data_bytes = base64.b64decode(func_data_b64) - func_data = cloudpickle.loads(func_data_bytes) - - # Get components - func_bytes = func_data['function'] - args_bytes = func_data['args'] - kwargs_bytes = func_data['kwargs'] - func_source = func_data.get('function_source') - - # Load arguments with dill: they may carry classes defined in the - # caller's __main__, which stdlib pickle can only store by name. - try: - import dill as _argser - except ImportError: - _argser = cloudpickle - args = _argser.loads(args_bytes) - kwargs = _argser.loads(kwargs_bytes) - - # Try to load function, with fallback for __main__ issues - func = None - try: - func = cloudpickle.loads(func_bytes) - except (AttributeError, ImportError) as e: - if func_source and '__main__' in str(e): - # Function was defined in __main__, try to recreate from source - print('Recreating function from source due to __main__ issue') - - # Create a temporary module to execute the function in - temp_module = types.ModuleType('temp_func_module') - temp_module.__dict__.update(globals()) - - # Clean the function source - remove decorators - import re - # Remove @cluster decorator lines (handle multi-line decorators) - lines = func_source.split('\\n') - cleaned_lines = [] - skip_until_def = False - - for line in lines: - if line.strip().startswith('@cluster'): - skip_until_def = True - continue - elif skip_until_def and line.strip().startswith(')'): - skip_until_def = True # Keep skipping until we see def - continue - elif skip_until_def and line.strip().startswith('def '): - skip_until_def = False - cleaned_lines.append(line) - elif not skip_until_def: - cleaned_lines.append(line) - - cleaned_source = '\\n'.join(cleaned_lines) - - # Execute the cleaned function source in the temporary module - exec(cleaned_source, temp_module.__dict__) - - # Extract the function (assume it's the first function defined) - for name, obj in temp_module.__dict__.items(): - if callable(obj) and hasattr(obj, '__code__') and not name.startswith('_'): - func = obj - break - - if func is None: - raise RuntimeError('Could not extract function from source code') - else: - # Re-raise the original error - raise e - - if func is None: - raise RuntimeError('Failed to load function') - - # Execute function - result = func(*args, **kwargs) - - # Serialize the result rather than printing its repr, and sign it so the - # caller can tell our output apart from anything else in the pod log. - import dill as _ser - import hashlib as _hashlib - import hmac as _hmac - import os as _os - _payload = _ser.dumps(result, protocol=4) - _key = _os.environ.get('CLUSTRIX_RESULT_KEY', '') - if not _key: - raise RuntimeError('CLUSTRIX_RESULT_KEY is not set in this container') - _tag = _hmac.new(_key.encode(), _payload, _hashlib.sha256).hexdigest() - print('{RESULT_PREFIX}' + base64.b64encode(_payload).decode()) - print('{SIGNATURE_PREFIX}' + _tag) - -except Exception as e: - print('CLUSTRIX_ERROR:' + str(e)) - print('CLUSTRIX_TRACEBACK:' + traceback.format_exc()) - sys.exit(1) -""" - - -def build_container_command(worker_program: str) -> str: - """Wrap the worker program in the shell command the container runs. - - The program is embedded inside a double-quoted shell string, so a ``"``, - ``$`` or backtick in it would be eaten or expanded by the shell and the - container would run something other than what was generated. Refuse - rather than ship a mangled program. - """ - for char in ('"', "$", "`"): - if char in worker_program: - raise ValueError( - f"Worker program contains {char!r}, which the shell would " - "reinterpret inside the container command." - ) - return f""" -pip install cloudpickle dill --quiet && python -c "{worker_program}" -""" - - -def decode_signed_result(logs: str, result_key: str) -> Any: - """Recover the result a worker container wrote into its pod log. - - Refuses anything it cannot verify. Unpickling executes code, so a payload - that is missing, unsigned, or signed with the wrong key is an error -- - never a best-effort string, and never the raw log. - """ - payload_b64 = None - signature = None - for line in logs.split("\n"): - if line.startswith(RESULT_PREFIX): - payload_b64 = line[len(RESULT_PREFIX) :].strip() - elif line.startswith(SIGNATURE_PREFIX): - signature = line[len(SIGNATURE_PREFIX) :].strip() - - if payload_b64 is None: - raise RuntimeError( - "The pod log contains no clustrix result. The job did not " - "produce one, so there is nothing to return." - ) - if not signature: - raise RuntimeError( - "The pod log contains a result with no signature. Refusing to " - "deserialize it: loading a pickle executes code." - ) - if not result_key: - raise RuntimeError( - "No result-signing key is known for this job, so its result " - "cannot be verified. Refusing to deserialize it." - ) - - try: - payload = base64.b64decode(payload_b64, validate=True) - except Exception as e: - raise RuntimeError(f"The result in the pod log is not valid base64: {e}") - - expected = hmac.new(result_key.encode(), payload, hashlib.sha256).hexdigest() - if not hmac.compare_digest(signature, expected): - raise RuntimeError( - "The result in the pod log failed its integrity check. Refusing " - "to deserialize it." - ) - - return dill.loads(payload) - - -class KubernetesJobManager: - """Manages Kubernetes job execution using containerized Python runners.""" - - def __init__(self, config, connection_manager): - """Initialize Kubernetes job manager. - - Args: - config: ClusterConfig instance with Kubernetes settings - connection_manager: ConnectionManager instance for K8s client - """ - self.config = config - self.connection_manager = connection_manager - self.active_jobs: Dict[str, Any] = {} - - def submit_k8s_job( - self, func_data: Dict[str, Any], job_config: Dict[str, Any] - ) -> str: - """ - Submit a job to Kubernetes cluster using containerized Python execution. - - This method implements a sophisticated Kubernetes job submission strategy that - packages Python functions and data into self-contained container jobs without - requiring custom Docker images or persistent storage. - - **Architecture:** - - 1. **Function Serialization**: Uses cloudpickle to serialize the function and all data - 2. **Base64 Encoding**: Encodes serialized data for safe embedding in container args - 3. **Container Execution**: Creates a Job with inline Python code that: - - Decodes the base64 data - - Deserializes the function and arguments - - Executes the function - - Captures results or errors - 4. **Resource Management**: Applies CPU and memory limits from job_config - - **Key Features:** - - **No Custom Images**: Uses standard `python:3.11-slim` image - - **Self-Contained**: All code and data embedded in Job manifest - - **Resource Aware**: Respects CPU/memory requirements - - **Error Handling**: Captures exceptions with full tracebacks - - **Cloud Native**: Leverages Kubernetes Job semantics for reliability - - **Job Manifest Structure:** - ```yaml - apiVersion: batch/v1 - kind: Job - metadata: - name: clustrix-job-{timestamp} - spec: - template: - spec: - containers: - - name: clustrix-worker - image: python:3.11-slim - command: ["python", "-c"] - args: [""] - resources: - requests/limits: {cpu, memory from job_config} - restartPolicy: Never - ``` - - Args: - func_data: Serialized function data containing: - - 'function': The pickled function to execute - - 'args': Pickled positional arguments - - 'kwargs': Pickled keyword arguments - - 'requirements': Package dependencies (not used for K8s) - job_config: Job configuration including: - - 'cores': CPU request/limit (default: 1) - - 'memory': Memory request/limit (default: "1Gi") - - Additional K8s-specific settings - - Returns: - str: Kubernetes Job name that can be used for status tracking - - Raises: - ImportError: If kubernetes package is not installed - Exception: If Kubernetes API calls fail - - Examples: - >>> from clustrix.utils import serialize_function - >>> func_data = serialize_function(square, (5,), {}) - >>> job_config = {'cores': 2, 'memory': '4Gi'} - >>> job_id = k8s_manager.submit_k8s_job(func_data, job_config) - >>> print(job_id) # "clustrix-job-1234567890" - - Note: - - Requires kubernetes package: `pip install kubernetes` - - Assumes kubectl is configured with cluster access - - Jobs are created in the configured namespace - - Cloudpickle is used for function serialization - - The result is written to the pod log as a base64 pickle plus an - HMAC over those bytes, keyed by a per-job secret passed to the - container in CLUSTRIX_RESULT_KEY, and is verified before it is - deserialized (see decode_signed_result) - """ - try: - from kubernetes import client # type: ignore - except ImportError: - raise ImportError( - "kubernetes package required for Kubernetes support. " - "Install with: pip install kubernetes" - ) - - # Ensure Kubernetes client is set up - if ( - not hasattr(self.connection_manager, "k8s_client") - or self.connection_manager.k8s_client is None - ): - self.connection_manager.setup_kubernetes() - - # Create a unique job name - job_name = f"clustrix-job-{int(time.time())}-{random.randint(1000, 9999)}" - - # Serialize function data - func_data_serialized = cloudpickle.dumps(func_data) - func_data_b64 = base64.b64encode(func_data_serialized).decode("utf-8") - - # Per-job key the worker signs its result with, so the caller can tell - # the result apart from anything else that reaches the pod log. - result_key = secrets.token_hex(32) - - container_command = build_container_command(build_worker_program(func_data_b64)) - - # Create Kubernetes Job manifest - job_manifest = { - "apiVersion": "batch/v1", - "kind": "Job", - "metadata": {"name": job_name}, - "spec": { - "template": { - "spec": { - "containers": [ - { - "name": "clustrix-worker", - "image": self.config.k8s_image, - "command": ["/bin/bash", "-c"], - "env": [ - { - "name": "CLUSTRIX_RESULT_KEY", - "value": result_key, - } - ], - "args": [container_command], - "resources": { - # Kubernetes rejects "16GB" outright; its - # quantities are "16G" or "16Gi". Passing - # clustrix's configured spelling straight - # through made default_memory unusable here. - "requests": { - "cpu": f"{job_config.get('cores', 1)}", - "memory": normalize_memory( - job_config.get("memory", "1Gi"), - "kubernetes", - ), - }, - "limits": { - "cpu": f"{job_config.get('cores', 1)}", - "memory": normalize_memory( - job_config.get("memory", "1Gi"), - "kubernetes", - ), - }, - }, - } - ], - "restartPolicy": "Never", - } - }, - "backoffLimit": self.config.k8s_backoff_limit, - "ttlSecondsAfterFinished": self.config.k8s_job_ttl_seconds, - }, - } - - # Submit job to Kubernetes - batch_api = client.BatchV1Api() - response = batch_api.create_namespaced_job( - namespace=self.config.k8s_namespace, body=job_manifest - ) - - job_id = response.metadata.name - - # Store job info - self.active_jobs[job_id] = { - "status": "submitted", - "submit_time": time.time(), - "k8s_job": True, - "result_key": result_key, - } - - return job_id - - def check_k8s_job_status(self, job_id: str) -> str: - """Check Kubernetes job status via API. - - Never invents a status. This used to answer "completed" whenever the - API call raised -- a job that had been evicted, a namespace the caller - had lost access to, or a `kubernetes` package that was not installed - all reported success, and the caller then went looking for a result - that did not exist. An outcome we cannot read is an error, not a pass. - """ - from kubernetes import client # type: ignore - - batch_api = client.BatchV1Api() - - try: - job = batch_api.read_namespaced_job( - name=job_id, namespace=self.config.k8s_namespace - ) - except Exception as e: - raise RuntimeError( - f"Could not read the status of Kubernetes job {job_id} in " - f"namespace {self.config.k8s_namespace}: {e}. Its outcome is " - "unknown -- it may still be running, or it may have been " - "deleted before its result was collected." - ) from e - - # Check job conditions - if job.status.succeeded: - return "completed" - elif job.status.failed: - return "failed" - elif job.status.active: - return "running" - else: - return "pending" - - def get_k8s_result(self, job_id: str) -> Any: - """Get result from Kubernetes job logs. - - The pod log is verified against the key this job was given before - anything is deserialized, and a log without a verifiable result is an - error. Previously the log itself was returned as the "result" when no - marker was found, and a marker that would not ``literal_eval`` came - back as its own repr string. - """ - from kubernetes import client # type: ignore - - core_api = client.CoreV1Api() - - result_key = (self.active_jobs.get(job_id) or {}).get("result_key", "") - - try: - pods = core_api.list_namespaced_pod( - namespace=self.config.k8s_namespace, - label_selector=f"job-name={job_id}", - ) - except Exception as e: - raise RuntimeError( - f"Could not list the pods of Kubernetes job {job_id}: {e}" - ) from e - - for pod in pods.items: - if pod.status.phase == "Succeeded": - try: - logs = core_api.read_namespaced_pod_log( - name=pod.metadata.name, - namespace=pod.metadata.namespace, - ) - except Exception as e: - raise RuntimeError( - f"Kubernetes job {job_id} succeeded but its log could " - f"not be read from pod {pod.metadata.name}: {e}" - ) from e - - return decode_signed_result(logs, result_key) - - raise RuntimeError(f"No successful pod found for job {job_id}") - - def get_k8s_error_log(self, job_id: str) -> str: - """Get error log from Kubernetes job.""" - try: - from kubernetes import client # type: ignore - - core_api = client.CoreV1Api() - - # Get pods for this job - pods = core_api.list_namespaced_pod( - namespace=self.config.k8s_namespace, - label_selector=f"job-name={job_id}", - ) - - error_logs = [] - for pod in pods.items: - # Get pod logs regardless of status - try: - logs = core_api.read_namespaced_pod_log( - name=pod.metadata.name, - namespace=pod.metadata.namespace, - ) - error_logs.append(f"Pod {pod.metadata.name}:\n{logs}") - except Exception as e: - error_logs.append( - f"Pod {pod.metadata.name}: Failed to get logs - {e}" - ) - - return "\n\n".join(error_logs) if error_logs else "No error logs available" - - except Exception as e: - return f"Failed to get Kubernetes error logs: {e}" - - def extract_k8s_exception(self, job_id: str) -> Optional[Exception]: - """Extract original exception from Kubernetes job logs.""" - try: - error_log = self.get_k8s_error_log(job_id) - - # Look for CLUSTRIX_ERROR and CLUSTRIX_TRACEBACK in logs - lines = error_log.split("\n") - error_msg = None - - for line in lines: - if line.startswith("CLUSTRIX_ERROR:"): - error_msg = line[len("CLUSTRIX_ERROR:") :] - elif line.startswith("CLUSTRIX_TRACEBACK:"): - # Found traceback - could be used for more detailed error handling - break - - if error_msg: - # Try to recreate the original exception - return RuntimeError(error_msg) - - return None - - except Exception: - return None - - def cleanup_k8s_job(self, job_id: str): - """Clean up Kubernetes job resources.""" - try: - from kubernetes import client # type: ignore - - batch_api = client.BatchV1Api() - - # Delete the job (this will also delete associated pods) - batch_api.delete_namespaced_job( - name=job_id, - namespace=self.config.k8s_namespace, - body=client.V1DeleteOptions(propagation_policy="Foreground"), - ) - - except Exception as e: - # Log warning but don't fail - logger.warning(f"Failed to cleanup Kubernetes job {job_id}: {e}") - - def wait_for_k8s_result(self, job_id: str) -> Any: - """Wait for Kubernetes job completion and return result.""" - job_info = self.active_jobs.get(job_id) - if not job_info: - raise ValueError(f"Unknown job ID: {job_id}") - - # Poll for completion - while True: - status = self.check_k8s_job_status(job_id) - - if status == "completed": - # Get result from pod logs - result = self.get_k8s_result(job_id) - - # Cleanup - if self.config.cleanup_on_success: - self.cleanup_k8s_job(job_id) - - del self.active_jobs[job_id] - return result - - elif status == "failed": - # Get error from pod logs - error_log = self.get_k8s_error_log(job_id) - original_exception = self.extract_k8s_exception(job_id) - - if original_exception: - raise original_exception - else: - raise RuntimeError( - f"Kubernetes job {job_id} failed. Error log:\n{error_log}" - ) - - # Wait before next poll - time.sleep(self.config.job_poll_interval) diff --git a/clustrix/executor_scheduler_status.py b/clustrix/executor_scheduler_status.py index 06799bcc..e80ad20f 100644 --- a/clustrix/executor_scheduler_status.py +++ b/clustrix/executor_scheduler_status.py @@ -1,7 +1,7 @@ """Scheduler job status monitoring and error handling. -This module handles status checking and error retrieval for traditional -HPC scheduler jobs (SLURM, PBS, SGE). +This module handles status checking and error retrieval for SLURM jobs and +for jobs run directly over SSH. """ import os @@ -42,8 +42,6 @@ def check_job_status(self, job_id: str, active_jobs: Dict[str, Any]) -> str: **Multi-Scheduler Support:** - **SLURM**: Uses `squeue -j {job_id} -h -o %T` to check job status - - **PBS**: Uses `qstat -f {job_id}` to query detailed job information - - **SGE**: Job status checking (using similar logic to PBS) - **SSH**: File-based status detection (result.pkl vs error files) **Status Detection Logic:** @@ -73,10 +71,6 @@ def check_job_status(self, job_id: str, active_jobs: Dict[str, Any]) -> str: >>> status = scheduler.check_job_status("12345", active_jobs) >>> print(status) # "running" - >>> # PBS job completed (removed from queue) - >>> status = scheduler.check_job_status("67890.headnode", active_jobs) - >>> print(status) # "completed" - >>> # SSH job failed >>> status = scheduler.check_job_status("ssh_1234567890", active_jobs) >>> print(status) # "failed" @@ -98,42 +92,6 @@ def check_job_status(self, job_id: str, active_jobs: Dict[str, Any]) -> str: return "unknown" return self._check_slurm_job_status_robust(job_id, active_jobs) - elif self.config.cluster_type == "pbs": - cmd = f"qstat -f {job_id}" - try: - stdout, stderr = self.connection_manager.execute_remote_command(cmd) - if "job_state = C" in stdout: - return "completed" - elif "job_state = R" in stdout: - return "running" - else: - return "failed" - except Exception: - # Job might be completed and removed from queue - if job_id in active_jobs: - job_info = active_jobs[job_id] - result_exists = self.connection_manager.remote_file_exists( - f"{job_info['remote_dir']}/result.pkl" - ) - return "completed" if result_exists else "failed" - else: - return "completed" - - elif self.config.cluster_type == "sge": - sge_status = self._check_sge_status(job_id) - if sge_status == "completed": - # Job completed but not in queue, check if result exists - if job_id in active_jobs: - job_info = active_jobs[job_id] - result_exists = self.connection_manager.remote_file_exists( - f"{job_info['remote_dir']}/result.pkl" - ) - return "completed" if result_exists else "failed" - else: - return "completed" - else: - return sge_status - elif self.config.cluster_type == "ssh": # For SSH jobs, check if result file exists if job_id in active_jobs: @@ -486,60 +444,7 @@ def _get_scheduler_failure_reason(self, job_id: str) -> Optional[str]: ) return detail - def _check_pbs_status(self, job_id: str) -> str: - """Check PBS job status.""" - cmd = f"qstat -f {job_id}" - try: - stdout, stderr = self.connection_manager.execute_remote_command(cmd) - # Handle full format output (qstat -f) - if "job_state = C" in stdout: - return "completed" - elif "job_state = Q" in stdout: - return "queued" - elif "job_state = R" in stdout: - return "running" - elif "job_state = E" in stdout: - return "failed" - # Handle short format output (qstat) - elif " R " in stdout: - return "running" - elif " Q " in stdout: - return "queued" - elif " C " in stdout: - return "completed" - elif " E " in stdout: - return "failed" - else: - return "unknown" - except Exception: - return "unknown" - def _check_sge_status(self, job_id: str) -> str: - """Check SGE job status.""" - cmd = f"qstat -j {job_id}" - try: - stdout, stderr = self.connection_manager.execute_remote_command(cmd) - if not stdout.strip() or "Following jobs do not exist" in stderr: - # Job not in queue, likely completed - return "completed" - else: - # Parse SGE job state from qstat output - # Common SGE states: r (running), qw (queued), Eqw (error), dr (deleting) - if "job_state r" in stdout: - return "running" - elif "job_state qw" in stdout: - return "queued" - elif "job_state Eqw" in stdout: - return "failed" - elif "job_state dr" in stdout: - return "completed" - # Check for exit status indicating completion - elif "exit_status" in stdout: - return "completed" - else: - return "running" # Default for unknown running states - except Exception: - return "unknown" def _authenticated_error_payload( self, job_id: str, job_info: Dict[str, Any] @@ -605,7 +510,6 @@ def get_error_log(self, job_id: str, active_jobs: Dict[str, Any]) -> str: 2. **Text Log Files** (Fallback): Searches for various scheduler-specific log files: - job.err (standard error output) - slurm-*.out (SLURM output files) - - job.e* (PBS/SGE error files) 3. **No Error Found**: Returns appropriate message if no error information exists. @@ -678,7 +582,7 @@ def get_error_log(self, job_id: str, active_jobs: Dict[str, Any]) -> str: ) # Fallback to text error files - error_files = ["job.err", "slurm-*.out", "job.e*"] + error_files = ["job.err", "slurm-*.out"] for error_file in error_files: try: diff --git a/clustrix/executor_schedulers.py b/clustrix/executor_schedulers.py index 537428b3..c0f8218c 100644 --- a/clustrix/executor_schedulers.py +++ b/clustrix/executor_schedulers.py @@ -1,7 +1,11 @@ -"""SLURM, PBS, and SGE scheduler job submission and monitoring. +"""SLURM scheduler and plain-SSH job submission and monitoring. -This module handles job submission, monitoring, and status checking for traditional -HPC schedulers including SLURM, PBS/Torque, and Sun Grid Engine (SGE). +This module handles job submission, monitoring, and status checking for SLURM +and for direct execution over SSH. + +PBS/Torque and SGE submission used to live here. Neither was ever verified +against a real scheduler, so both were removed in v0.2.0 (PBS: issue #140, +SGE: issue #141). """ import os @@ -21,7 +25,7 @@ class SchedulerManager: - """Submits jobs to SLURM, PBS, SGE and plain SSH hosts.""" + """Submits jobs to SLURM and plain SSH hosts.""" def _prepare_job_dir(self, remote_job_dir: str) -> str: """Create the job directory and give the job a result-signing key. @@ -103,10 +107,8 @@ def _stage_job_directory(self, func_data: Dict[str, Any]) -> tuple: def _setup_job_environment(self, remote_job_dir: str, func_data: Dict[str, Any]): """Build the Python environment the generated job script will activate. - Shared by SLURM, PBS, SGE and SSH. SLURM and SSH each carried a copy of - this, SGE had only the basic half, and PBS had none at all -- so a PBS - job ran a script whose first act was `source venv/bin/activate` against - a virtualenv nothing had created, and died there every time (#120). + Shared by SLURM and SSH, which each used to carry their own copy of + it (#120). Returns the config to generate the job script from: `venv_info` set for the two-venv layout, or cleared when only the single venv was built. @@ -215,75 +217,6 @@ def submit_slurm_job( return job_id - def submit_pbs_job( - self, func_data: Dict[str, Any], job_config: Dict[str, Any] - ) -> str: - """Submit job via PBS.""" - remote_job_dir, result_key = self._stage_job_directory(func_data) - updated_config = self._setup_job_environment(remote_job_dir, func_data) - - # Create PBS script - script_content = create_job_script( - cluster_type="pbs", - job_config=job_config, - remote_job_dir=remote_job_dir, - config=updated_config, - ) - - script_path = f"{remote_job_dir}/job.pbs" - self.connection_manager.create_remote_file(script_path, script_content) - - # Submit job - cmd = f"cd {remote_job_dir} && qsub job.pbs" - stdout, stderr = self.connection_manager.execute_remote_command(cmd) - - job_id = stdout.strip() - - self.active_jobs[job_id] = { - "remote_dir": remote_job_dir, - "result_key": result_key, - "status": "submitted", - "submit_time": time.time(), - } - - return job_id - - def submit_sge_job( - self, func_data: Dict[str, Any], job_config: Dict[str, Any] - ) -> str: - """Submit job via SGE.""" - remote_job_dir, result_key = self._stage_job_directory(func_data) - updated_config = self._setup_job_environment(remote_job_dir, func_data) - - # Create job script - script_content = create_job_script( - cluster_type="sge", - job_config=job_config, - remote_job_dir=remote_job_dir, - config=updated_config, - ) - - # Upload and submit job script - script_path = f"{remote_job_dir}/job.sge" - self.connection_manager.create_remote_file(script_path, script_content) - - # Submit job - cmd = f"cd {remote_job_dir} && qsub job.sge" - stdout, stderr = self.connection_manager.execute_remote_command(cmd) - - # Extract job ID from qsub output (SGE format: "Your job 123456 ...") - job_id = stdout.strip().split()[2] if "Your job" in stdout else stdout.strip() - - # Store job info - self.active_jobs[job_id] = { - "remote_dir": remote_job_dir, - "result_key": result_key, - "status": "submitted", - "submit_time": time.time(), - } - - return job_id - def submit_ssh_job( self, func_data: Dict[str, Any], job_config: Dict[str, Any] ) -> str: @@ -337,10 +270,6 @@ def cancel_job(self, job_id: str): """Cancel a running job.""" if self.config.cluster_type == "slurm": self.connection_manager.execute_remote_command(f"scancel {job_id}") - elif self.config.cluster_type == "pbs": - self.connection_manager.execute_remote_command(f"qdel {job_id}") - elif self.config.cluster_type == "sge": - self.connection_manager.execute_remote_command(f"qdel {job_id}") if job_id in self.active_jobs: del self.active_jobs[job_id] diff --git a/clustrix/kubernetes/__init__.py b/clustrix/kubernetes/__init__.py deleted file mode 100644 index fdacfd3e..00000000 --- a/clustrix/kubernetes/__init__.py +++ /dev/null @@ -1,60 +0,0 @@ -""" -Kubernetes cluster provisioning and management for Clustrix. - -This module provides from-scratch Kubernetes cluster provisioning across -supported cloud providers with complete infrastructure setup and Clustrix -integration. -""" - -from .cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, - provision_kubernetes_cluster, - destroy_kubernetes_cluster, - list_kubernetes_clusters, -) - -# Provider-specific provisioners (imported lazily to avoid dependency issues) -__all__ = [ - "KubernetesClusterProvisioner", - "ClusterSpec", - "provision_kubernetes_cluster", - "destroy_kubernetes_cluster", - "list_kubernetes_clusters", -] - - -# Lazy imports for optional provider dependencies -def _get_aws_provisioner(): - """Lazy import for AWS EKS provisioner.""" - from .aws_provisioner import AWSEKSFromScratchProvisioner - - return AWSEKSFromScratchProvisioner - - -def _get_gcp_provisioner(): - """Lazy import for GCP GKE provisioner.""" - from .gcp_provisioner import GCPGKEFromScratchProvisioner - - return GCPGKEFromScratchProvisioner - - -def _get_azure_provisioner(): - """Lazy import for Azure AKS provisioner.""" - from .azure_provisioner import AzureAKSFromScratchProvisioner - - return AzureAKSFromScratchProvisioner - - -def _get_huggingface_provisioner(): - """Lazy import for HuggingFace Spaces adapter.""" - from .huggingface_provisioner import HuggingFaceKubernetesProvisioner - - return HuggingFaceKubernetesProvisioner - - -def _get_lambda_provisioner(): - """Lazy import for Lambda Cloud adapter.""" - from .lambda_provisioner import LambdaCloudKubernetesProvisioner - - return LambdaCloudKubernetesProvisioner diff --git a/clustrix/kubernetes/aws_provisioner.py b/clustrix/kubernetes/aws_provisioner.py deleted file mode 100644 index 7e3bd8c9..00000000 --- a/clustrix/kubernetes/aws_provisioner.py +++ /dev/null @@ -1,872 +0,0 @@ -""" -AWS EKS from-scratch provisioner. - -Provides complete EKS cluster provisioning with all required infrastructure -including VPC, IAM roles, security groups, and node groups. -""" - -import json -import logging -import time -from typing import Dict, Any, List -import subprocess -import tempfile - -try: - import boto3 - from botocore.exceptions import ClientError, NoCredentialsError - - BOTO3_AVAILABLE = True -except ImportError: - BOTO3_AVAILABLE = False - boto3 = None - ClientError = Exception - NoCredentialsError = Exception - -from .cluster_provisioner import BaseKubernetesProvisioner, ClusterSpec - -logger = logging.getLogger(__name__) - - -class AWSEKSFromScratchProvisioner(BaseKubernetesProvisioner): - """ - Complete AWS EKS cluster provisioner from blank AWS account. - - This provisioner creates all required infrastructure components: - - VPC with public and private subnets - - Internet Gateway and NAT Gateways - - Security Groups with proper rules - - IAM roles and policies for EKS - - EKS control plane - - EKS node groups with auto-scaling - - kubectl configuration - - Clustrix namespace and RBAC setup - """ - - def __init__(self, credentials: Dict[str, str], region: str): - super().__init__(credentials, region) - - if not BOTO3_AVAILABLE: - raise ImportError( - "boto3 required for AWS EKS provisioning. " - "Install with: pip install boto3" - ) - - # Initialize AWS clients - self.session = boto3.Session( - aws_access_key_id=credentials.get("access_key_id"), - aws_secret_access_key=credentials.get("secret_access_key"), - aws_session_token=credentials.get("session_token"), - region_name=region, - ) - - self.ec2 = self.session.client("ec2") - self.eks = self.session.client("eks") - self.iam = self.session.client("iam") - - # Track created resources for cleanup - self.created_resources: Dict[str, List[str]] = { - "vpcs": [], - "subnets": [], - "security_groups": [], - "internet_gateways": [], - "nat_gateways": [], - "route_tables": [], - "iam_roles": [], - "iam_policies": [], - "eks_clusters": [], - "eks_node_groups": [], - } - - def validate_credentials(self) -> bool: - """Validate AWS credentials and required permissions.""" - try: - # Test basic AWS access - sts = self.session.client("sts") - identity = sts.get_caller_identity() - logger.info( - f"✅ AWS credentials validated for account: {identity.get('Account')}" - ) - - # Check required service permissions (basic check) - required_services = ["ec2", "eks", "iam"] - for service in required_services: - try: - client = self.session.client(service) - # Make a simple read-only call to test permissions - if service == "ec2": - client.describe_availability_zones(MaxResults=1) - elif service == "eks": - client.list_clusters(maxResults=1) - elif service == "iam": - client.list_roles(MaxItems=1) - - logger.debug(f"✅ {service.upper()} service access confirmed") - except Exception as e: - logger.warning(f"⚠️ Limited {service.upper()} permissions: {e}") - - return True - - except Exception as e: - logger.error(f"❌ AWS credential validation failed: {e}") - return False - - def provision_complete_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """ - Create complete EKS cluster infrastructure from scratch. - - Steps: - 1. Create VPC with public/private subnets - 2. Create Internet Gateway and NAT Gateways - 3. Set up routing tables - 4. Create security groups - 5. Create IAM roles and policies - 6. Create EKS control plane - 7. Create and configure node groups - 8. Configure kubectl access - 9. Set up Clustrix namespace and RBAC - 10. Verify cluster is ready for jobs - """ - logger.info(f"🚀 Starting EKS cluster provisioning: {spec.cluster_name}") - - try: - # Step 1: Create VPC infrastructure - vpc_config = self._create_vpc_infrastructure(spec) - - # Step 2: Create IAM infrastructure - iam_config = self._create_iam_infrastructure(spec) - - # Step 3: Create EKS control plane - cluster_info = self._create_eks_control_plane(spec, vpc_config, iam_config) - - # Step 4: Create node groups - self._create_node_groups(spec, cluster_info, vpc_config, iam_config) - - # Step 5: Configure kubectl access - kubectl_config = self._configure_kubectl_access(cluster_info) - - # Step 6: Set up Clustrix environment - self._setup_clustrix_environment(cluster_info, kubectl_config) - - # Step 7: Verify cluster ready - self._verify_cluster_operational(cluster_info["cluster_name"]) - - result = { - "cluster_id": cluster_info["cluster_name"], - "cluster_name": cluster_info["cluster_name"], - "provider": "aws", - "region": self.region, - "endpoint": cluster_info["endpoint"], - "arn": cluster_info["arn"], - "version": cluster_info["version"], - "node_count": spec.node_count, - "instance_type": spec.aws_instance_type, - "vpc_id": vpc_config["vpc_id"], - "subnet_ids": vpc_config["subnet_ids"], - "security_group_ids": vpc_config["security_group_ids"], - "kubectl_config": kubectl_config, - "ready_for_jobs": True, - "created_resources": self.created_resources.copy(), - } - - logger.info(f"✅ EKS cluster provisioning completed: {spec.cluster_name}") - return result - - except Exception as e: - logger.error(f"❌ EKS cluster provisioning failed: {e}") - # Attempt cleanup of any created resources - self._cleanup_failed_provisioning(spec.cluster_name) - raise - - def _create_vpc_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create VPC with all networking components.""" - logger.info("🏗️ Creating VPC infrastructure...") - - # Create VPC - vpc_response = self.ec2.create_vpc(CidrBlock="10.0.0.0/16") - vpc_id = vpc_response["Vpc"]["VpcId"] - self.created_resources["vpcs"].append(vpc_id) - - # Enable DNS support - self.ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsHostnames={"Value": True}) - self.ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsSupport={"Value": True}) - - # Tag VPC - self.ec2.create_tags( - Resources=[vpc_id], - Tags=[ - {"Key": "Name", "Value": f"clustrix-eks-vpc-{spec.cluster_name}"}, - {"Key": "clustrix:cluster", "Value": spec.cluster_name}, - {"Key": "clustrix:managed", "Value": "true"}, - ], - ) - - # Get availability zones - azs = self.ec2.describe_availability_zones()["AvailabilityZones"] - az_names = [az["ZoneName"] for az in azs[:2]] # Use first 2 AZs - - # Create public and private subnets - subnet_configs = [ - {"cidr": "10.0.1.0/24", "type": "public", "az": az_names[0]}, - {"cidr": "10.0.2.0/24", "type": "public", "az": az_names[1]}, - {"cidr": "10.0.101.0/24", "type": "private", "az": az_names[0]}, - {"cidr": "10.0.102.0/24", "type": "private", "az": az_names[1]}, - ] - - subnets: Dict[str, List[str]] = {} - for config in subnet_configs: - subnet_response = self.ec2.create_subnet( - VpcId=vpc_id, CidrBlock=config["cidr"], AvailabilityZone=config["az"] - ) - subnet_id = subnet_response["Subnet"]["SubnetId"] - self.created_resources["subnets"].append(subnet_id) - - # Tag subnet - self.ec2.create_tags( - Resources=[subnet_id], - Tags=[ - { - "Key": "Name", - "Value": f"clustrix-eks-{config['type']}-{config['az']}", - }, - {"Key": "clustrix:cluster", "Value": spec.cluster_name}, - { - "Key": "kubernetes.io/role/elb", - "Value": "1" if config["type"] == "public" else "", - }, - { - "Key": "kubernetes.io/role/internal-elb", - "Value": "1" if config["type"] == "private" else "", - }, - ], - ) - - if config["type"] not in subnets: - subnets[config["type"]] = [] - subnets[config["type"]].append(subnet_id) - - # Create Internet Gateway - igw_response = self.ec2.create_internet_gateway() - igw_id = igw_response["InternetGateway"]["InternetGatewayId"] - self.created_resources["internet_gateways"].append(igw_id) - - # Attach Internet Gateway to VPC - self.ec2.attach_internet_gateway(InternetGatewayId=igw_id, VpcId=vpc_id) - - # Create NAT Gateways for private subnets - nat_gateways = [] - for i, public_subnet_id in enumerate(subnets["public"]): - # Allocate Elastic IP - eip_response = self.ec2.allocate_address(Domain="vpc") - allocation_id = eip_response["AllocationId"] - - # Create NAT Gateway - nat_response = self.ec2.create_nat_gateway( - SubnetId=public_subnet_id, AllocationId=allocation_id - ) - nat_id = nat_response["NatGateway"]["NatGatewayId"] - self.created_resources["nat_gateways"].append(nat_id) - nat_gateways.append(nat_id) - - # Wait for NAT Gateway to be available - self._wait_for_nat_gateway(nat_id) - - # Create route tables and routes - self._create_routing_tables(vpc_id, subnets, igw_id, nat_gateways) - - # Create security groups - security_group_ids = self._create_security_groups(vpc_id, spec) - - return { - "vpc_id": vpc_id, - "subnet_ids": subnets["private"] + subnets["public"], - "private_subnet_ids": subnets["private"], - "public_subnet_ids": subnets["public"], - "security_group_ids": security_group_ids, - "internet_gateway_id": igw_id, - "nat_gateway_ids": nat_gateways, - } - - def _create_security_groups(self, vpc_id: str, spec: ClusterSpec) -> List[str]: - """Create security groups for EKS cluster.""" - logger.info("🔒 Creating security groups...") - - # Control plane security group - cp_sg_response = self.ec2.create_security_group( - GroupName=f"clustrix-eks-control-plane-{spec.cluster_name}", - Description=f"EKS control plane security group for {spec.cluster_name}", - VpcId=vpc_id, - ) - cp_sg_id = cp_sg_response["GroupId"] - self.created_resources["security_groups"].append(cp_sg_id) - - # Node group security group - ng_sg_response = self.ec2.create_security_group( - GroupName=f"clustrix-eks-nodes-{spec.cluster_name}", - Description=f"EKS node group security group for {spec.cluster_name}", - VpcId=vpc_id, - ) - ng_sg_id = ng_sg_response["GroupId"] - self.created_resources["security_groups"].append(ng_sg_id) - - # Add security group rules - self._configure_security_group_rules(cp_sg_id, ng_sg_id) - - return [cp_sg_id, ng_sg_id] - - def _create_iam_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create IAM roles and policies for EKS.""" - logger.info("👤 Creating IAM infrastructure...") - - # EKS cluster service role - cluster_role_name = f"clustrix-eks-cluster-role-{spec.cluster_name}" - cluster_role_arn = self._create_eks_cluster_role(cluster_role_name) - - # EKS node group role - node_role_name = f"clustrix-eks-node-role-{spec.cluster_name}" - node_role_arn = self._create_eks_node_role(node_role_name) - - return { - "cluster_role_arn": cluster_role_arn, - "node_role_arn": node_role_arn, - "cluster_role_name": cluster_role_name, - "node_role_name": node_role_name, - } - - def _create_eks_cluster_role(self, role_name: str) -> str: - """Create EKS cluster service role.""" - trust_policy = { - "Version": "2012-10-17", - "Statement": [ - { - "Effect": "Allow", - "Principal": {"Service": "eks.amazonaws.com"}, - "Action": "sts:AssumeRole", - } - ], - } - - try: - role_response = self.iam.create_role( - RoleName=role_name, - AssumeRolePolicyDocument=json.dumps(trust_policy), - Description="EKS cluster service role for Clustrix", - ) - role_arn = role_response["Role"]["Arn"] - self.created_resources["iam_roles"].append(role_name) - - # Attach required policies - policies = ["arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"] - - for policy_arn in policies: - self.iam.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn) - - logger.info(f"✅ Created EKS cluster role: {role_arn}") - return role_arn - - except ClientError as e: - if e.response["Error"]["Code"] == "EntityAlreadyExists": - # Role already exists, get its ARN - role_response = self.iam.get_role(RoleName=role_name) - return role_response["Role"]["Arn"] - else: - raise - - def _create_eks_node_role(self, role_name: str) -> str: - """Create EKS node group role.""" - trust_policy = { - "Version": "2012-10-17", - "Statement": [ - { - "Effect": "Allow", - "Principal": {"Service": "ec2.amazonaws.com"}, - "Action": "sts:AssumeRole", - } - ], - } - - try: - role_response = self.iam.create_role( - RoleName=role_name, - AssumeRolePolicyDocument=json.dumps(trust_policy), - Description="EKS node group role for Clustrix", - ) - role_arn = role_response["Role"]["Arn"] - self.created_resources["iam_roles"].append(role_name) - - # Attach required policies - policies = [ - "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy", - "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy", - "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly", - ] - - for policy_arn in policies: - self.iam.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn) - - logger.info(f"✅ Created EKS node role: {role_arn}") - return role_arn - - except ClientError as e: - if e.response["Error"]["Code"] == "EntityAlreadyExists": - # Role already exists, get its ARN - role_response = self.iam.get_role(RoleName=role_name) - return role_response["Role"]["Arn"] - else: - raise - - def _create_eks_control_plane( - self, spec: ClusterSpec, vpc_config: Dict[str, Any], iam_config: Dict[str, Any] - ) -> Dict[str, Any]: - """Create EKS control plane.""" - logger.info("🎛️ Creating EKS control plane...") - - cluster_config = { - "name": spec.cluster_name, - "version": spec.kubernetes_version, - "roleArn": iam_config["cluster_role_arn"], - "resourcesVpcConfig": { - "subnetIds": vpc_config["subnet_ids"], - "securityGroupIds": vpc_config["security_group_ids"][ - :1 - ], # Only control plane SG - }, - "tags": { - "clustrix:managed": "true", - "clustrix:cluster": spec.cluster_name, - "clustrix:provider": "aws", - }, - } - - cluster_response = self.eks.create_cluster(**cluster_config) - cluster_info = cluster_response["cluster"] - - self.created_resources["eks_clusters"].append(spec.cluster_name) - - # Wait for cluster to be active - logger.info("⏳ Waiting for EKS cluster to be active...") - waiter = self.eks.get_waiter("cluster_active") - waiter.wait( - name=spec.cluster_name, WaiterConfig={"Delay": 30, "MaxAttempts": 40} - ) - - # Get updated cluster info - cluster_info = self.eks.describe_cluster(name=spec.cluster_name)["cluster"] - - logger.info(f"✅ EKS control plane active: {cluster_info['endpoint']}") - return cluster_info - - def _create_node_groups( - self, - spec: ClusterSpec, - cluster_info: Dict[str, Any], - vpc_config: Dict[str, Any], - iam_config: Dict[str, Any], - ) -> Dict[str, Any]: - """Create EKS managed node groups.""" - logger.info("💻 Creating EKS node groups...") - - node_group_name = f"clustrix-nodes-{spec.cluster_name}" - - node_group_config = { - "clusterName": spec.cluster_name, - "nodegroupName": node_group_name, - "subnets": vpc_config["private_subnet_ids"], - "nodeRole": iam_config["node_role_arn"], - "instanceTypes": [spec.aws_instance_type], - "scalingConfig": { - "minSize": max(1, spec.node_count // 2), - "maxSize": spec.node_count * 2, - "desiredSize": spec.node_count, - }, - "diskSize": 50, - "amiType": "AL2_x86_64", - "capacityType": "ON_DEMAND", - "tags": {"clustrix:managed": "true", "clustrix:cluster": spec.cluster_name}, - } - - self.eks.create_nodegroup(**node_group_config) - self.created_resources["eks_node_groups"].append(node_group_name) - - # Wait for node group to be active - logger.info("⏳ Waiting for node group to be active...") - waiter = self.eks.get_waiter("nodegroup_active") - waiter.wait( - clusterName=spec.cluster_name, - nodegroupName=node_group_name, - WaiterConfig={"Delay": 30, "MaxAttempts": 40}, - ) - - logger.info(f"✅ Node group active: {node_group_name}") - return {"node_group_name": node_group_name} - - def _configure_kubectl_access(self, cluster_info: Dict[str, Any]) -> Dict[str, Any]: - """Configure kubectl access to the cluster.""" - logger.info("⚙️ Configuring kubectl access...") - - # Generate kubeconfig - kubeconfig = { - "apiVersion": "v1", - "kind": "Config", - "clusters": [ - { - "cluster": { - "certificate-authority-data": cluster_info[ - "certificateAuthority" - ]["data"], - "server": cluster_info["endpoint"], - }, - "name": cluster_info["arn"], - } - ], - "contexts": [ - { - "context": { - "cluster": cluster_info["arn"], - "user": cluster_info["arn"], - }, - "name": cluster_info["arn"], - } - ], - "current-context": cluster_info["arn"], - "users": [ - { - "name": cluster_info["arn"], - "user": { - "exec": { - "apiVersion": "client.authentication.k8s.io/v1beta1", - "command": "aws", - "args": [ - "eks", - "get-token", - "--cluster-name", - cluster_info["name"], - "--region", - self.region, - ], - } - }, - } - ], - } - - return kubeconfig - - def _setup_clustrix_environment( - self, cluster_info: Dict[str, Any], kubectl_config: Dict[str, Any] - ) -> None: - """Set up Clustrix namespace and RBAC.""" - logger.info("🔧 Setting up Clustrix environment...") - - try: - # Write kubeconfig to temporary file - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - import yaml - - yaml.dump(kubectl_config, f) - kubeconfig_path = f.name - - # Create namespace - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "namespace", - "clustrix", - ], - check=False, - capture_output=True, - ) - - # Create service account - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "serviceaccount", - "clustrix-worker", - "--namespace", - "clustrix", - ], - check=False, - capture_output=True, - ) - - # Create cluster role binding - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "clusterrolebinding", - "clustrix-worker-binding", - "--clusterrole", - "cluster-admin", - "--serviceaccount", - "clustrix:clustrix-worker", - ], - check=False, - capture_output=True, - ) - - logger.info("✅ Clustrix environment configured") - - except Exception as e: - logger.warning(f"⚠️ Failed to configure Clustrix environment: {e}") - finally: - # Clean up temporary kubeconfig file - import os - - try: - os.unlink(kubeconfig_path) - except Exception: - pass - - def _verify_cluster_operational(self, cluster_name: str) -> None: - """Verify cluster is ready for job submission.""" - logger.info("🔍 Verifying cluster is operational...") - - try: - # Check cluster status - cluster_info = self.eks.describe_cluster(name=cluster_name)["cluster"] - if cluster_info["status"] != "ACTIVE": - raise RuntimeError(f"Cluster not active: {cluster_info['status']}") - - # Check node group status - nodegroups = self.eks.list_nodegroups(clusterName=cluster_name)[ - "nodegroups" - ] - for ng_name in nodegroups: - ng_info = self.eks.describe_nodegroup( - clusterName=cluster_name, nodegroupName=ng_name - )["nodegroup"] - if ng_info["status"] != "ACTIVE": - raise RuntimeError(f"Node group not active: {ng_info['status']}") - - logger.info("✅ Cluster verification completed") - - except Exception as e: - logger.error(f"❌ Cluster verification failed: {e}") - raise - - # Additional helper methods for routing tables, security group rules, etc. - def _create_routing_tables( - self, - vpc_id: str, - subnets: Dict[str, List[str]], - igw_id: str, - nat_gateways: List[str], - ) -> None: - """Create and configure routing tables.""" - # Public route table - public_rt_response = self.ec2.create_route_table(VpcId=vpc_id) - public_rt_id = public_rt_response["RouteTable"]["RouteTableId"] - self.created_resources["route_tables"].append(public_rt_id) - - # Add route to Internet Gateway - self.ec2.create_route( - RouteTableId=public_rt_id, - DestinationCidrBlock="0.0.0.0/0", - GatewayId=igw_id, - ) - - # Associate public subnets with public route table - for subnet_id in subnets["public"]: - self.ec2.associate_route_table( - RouteTableId=public_rt_id, SubnetId=subnet_id - ) - - # Create private route tables (one per AZ) - for i, (subnet_id, nat_id) in enumerate(zip(subnets["private"], nat_gateways)): - private_rt_response = self.ec2.create_route_table(VpcId=vpc_id) - private_rt_id = private_rt_response["RouteTable"]["RouteTableId"] - self.created_resources["route_tables"].append(private_rt_id) - - # Add route to NAT Gateway - self.ec2.create_route( - RouteTableId=private_rt_id, - DestinationCidrBlock="0.0.0.0/0", - NatGatewayId=nat_id, - ) - - # Associate private subnet with route table - self.ec2.associate_route_table( - RouteTableId=private_rt_id, SubnetId=subnet_id - ) - - def _configure_security_group_rules(self, cp_sg_id: str, ng_sg_id: str) -> None: - """Configure security group rules for EKS.""" - # Allow nodes to communicate with each other - self.ec2.authorize_security_group_ingress( - GroupId=ng_sg_id, - IpPermissions=[ - {"IpProtocol": "-1", "UserIdGroupPairs": [{"GroupId": ng_sg_id}]} - ], - ) - - # Allow nodes to communicate with control plane - self.ec2.authorize_security_group_ingress( - GroupId=cp_sg_id, - IpPermissions=[ - { - "IpProtocol": "tcp", - "FromPort": 443, - "ToPort": 443, - "UserIdGroupPairs": [{"GroupId": ng_sg_id}], - } - ], - ) - - # Allow control plane to communicate with nodes - self.ec2.authorize_security_group_ingress( - GroupId=ng_sg_id, - IpPermissions=[ - { - "IpProtocol": "tcp", - "FromPort": 10250, - "ToPort": 10250, - "UserIdGroupPairs": [{"GroupId": cp_sg_id}], - }, - { - "IpProtocol": "tcp", - "FromPort": 443, - "ToPort": 443, - "UserIdGroupPairs": [{"GroupId": cp_sg_id}], - }, - ], - ) - - def _wait_for_nat_gateway(self, nat_id: str) -> None: - """Wait for NAT Gateway to be available.""" - max_attempts = 20 - for attempt in range(max_attempts): - try: - response = self.ec2.describe_nat_gateways(NatGatewayIds=[nat_id]) - state = response["NatGateways"][0]["State"] - - if state == "available": - return - elif state in ["failed", "deleting", "deleted"]: - raise RuntimeError(f"NAT Gateway failed: {state}") - - logger.info( - f"⏳ Waiting for NAT Gateway {nat_id} to be available... " - f"({attempt + 1}/{max_attempts})" - ) - time.sleep(30) - - except Exception as e: - if attempt == max_attempts - 1: - raise RuntimeError( - f"NAT Gateway {nat_id} not available after " - f"{max_attempts * 30} seconds: {e}" - ) - time.sleep(30) - - def destroy_cluster_infrastructure(self, cluster_id: str) -> bool: - """Destroy cluster and all associated infrastructure.""" - logger.info(f"🧹 Destroying EKS cluster: {cluster_id}") - - try: - # Delete node groups first - try: - nodegroups = self.eks.list_nodegroups(clusterName=cluster_id)[ - "nodegroups" - ] - for ng_name in nodegroups: - logger.info(f"Deleting node group: {ng_name}") - self.eks.delete_nodegroup( - clusterName=cluster_id, nodegroupName=ng_name - ) - - # Wait for node groups to be deleted - for ng_name in nodegroups: - waiter = self.eks.get_waiter("nodegroup_deleted") - waiter.wait(clusterName=cluster_id, nodegroupName=ng_name) - - except ClientError as e: - if e.response["Error"]["Code"] != "ResourceNotFoundException": - logger.warning(f"Error deleting node groups: {e}") - - # Delete EKS cluster - try: - self.eks.delete_cluster(name=cluster_id) - waiter = self.eks.get_waiter("cluster_deleted") - waiter.wait(name=cluster_id) - logger.info(f"✅ Deleted EKS cluster: {cluster_id}") - except ClientError as e: - if e.response["Error"]["Code"] != "ResourceNotFoundException": - logger.warning(f"Error deleting cluster: {e}") - - # Clean up all other resources - self._cleanup_all_resources() - - return True - - except Exception as e: - logger.error(f"❌ Failed to destroy cluster: {e}") - return False - - def get_cluster_status(self, cluster_id: str) -> Dict[str, Any]: - """Get detailed cluster status and health information.""" - try: - cluster_info = self.eks.describe_cluster(name=cluster_id)["cluster"] - - # Check node groups - nodegroups = self.eks.list_nodegroups(clusterName=cluster_id)["nodegroups"] - node_status = [] - for ng_name in nodegroups: - ng_info = self.eks.describe_nodegroup( - clusterName=cluster_id, nodegroupName=ng_name - )["nodegroup"] - node_status.append( - { - "name": ng_name, - "status": ng_info["status"], - "capacity": ng_info["scalingConfig"], - } - ) - - return { - "cluster_id": cluster_id, - "status": cluster_info["status"], - "endpoint": cluster_info.get("endpoint", ""), - "version": cluster_info.get("version", ""), - "node_groups": node_status, - "ready_for_jobs": ( - cluster_info["status"] == "ACTIVE" - and all(ng["status"] == "ACTIVE" for ng in node_status) - ), - } - - except ClientError as e: - if e.response["Error"]["Code"] == "ResourceNotFoundException": - return { - "cluster_id": cluster_id, - "status": "NOT_FOUND", - "ready_for_jobs": False, - } - else: - raise - - def _cleanup_failed_provisioning(self, cluster_name: str) -> None: - """Clean up resources if provisioning fails.""" - logger.info("🧹 Cleaning up failed provisioning...") - try: - self._cleanup_all_resources() - except Exception as e: - logger.error(f"Error during cleanup: {e}") - - def _cleanup_all_resources(self) -> None: - """Clean up all tracked resources.""" - # Implementation for comprehensive resource cleanup - logger.info("🧹 Cleaning up all created resources...") - # This would systematically delete all resources in reverse order of creation - pass diff --git a/clustrix/kubernetes/azure_provisioner.py b/clustrix/kubernetes/azure_provisioner.py deleted file mode 100644 index 69df1848..00000000 --- a/clustrix/kubernetes/azure_provisioner.py +++ /dev/null @@ -1,726 +0,0 @@ -""" -Azure AKS from-scratch provisioner. - -Provides complete AKS cluster provisioning with all required infrastructure -including resource groups, virtual networks, service principals, and node pools. -""" - -import logging -from typing import Dict, Any, List -import subprocess -import tempfile - -try: - from azure.identity import ClientSecretCredential - from azure.mgmt.containerservice import ContainerServiceClient - from azure.mgmt.resource import ResourceManagementClient - from azure.mgmt.network import NetworkManagementClient - from azure.mgmt.authorization import AuthorizationManagementClient - from azure.core.exceptions import ResourceNotFoundError - - AZURE_AVAILABLE = True -except ImportError: - AZURE_AVAILABLE = False - ClientSecretCredential = None - ContainerServiceClient = None - ResourceManagementClient = None - NetworkManagementClient = None - AuthorizationManagementClient = None - AzureError = Exception - ResourceNotFoundError = Exception - -from .cluster_provisioner import BaseKubernetesProvisioner, ClusterSpec - -logger = logging.getLogger(__name__) - - -class AzureAKSFromScratchProvisioner(BaseKubernetesProvisioner): - """ - Complete Azure AKS cluster provisioner from blank Azure subscription. - - This provisioner creates all required infrastructure components: - - Resource group - - Virtual network with subnets - - Network security groups - - Service principal for AKS cluster - - AKS control plane - - AKS node pools with auto-scaling - - kubectl configuration - - Clustrix namespace and RBAC setup - """ - - def __init__(self, credentials: Dict[str, str], region: str): - super().__init__(credentials, region) - - if not AZURE_AVAILABLE: - raise ImportError( - "azure-mgmt-containerservice required for Azure AKS " - "provisioning. Install with: pip install " - "azure-mgmt-containerservice azure-mgmt-resource " - "azure-mgmt-network azure-mgmt-authorization" - ) - - # Validate required credentials - required_keys = ["subscription_id", "tenant_id", "client_id", "client_secret"] - missing_keys = [key for key in required_keys if not credentials.get(key)] - if missing_keys: - raise ValueError(f"Missing Azure credentials: {missing_keys}") - - # Initialize Azure credentials and clients - self.subscription_id = credentials["subscription_id"] - self.tenant_id = credentials["tenant_id"] - self.client_id = credentials["client_id"] - self.client_secret = credentials["client_secret"] - - self.credential = ClientSecretCredential( - tenant_id=self.tenant_id, - client_id=self.client_id, - client_secret=self.client_secret, - ) - - # Initialize Azure clients - self.resource_client = ResourceManagementClient( - self.credential, self.subscription_id - ) - self.network_client = NetworkManagementClient( - self.credential, self.subscription_id - ) - self.container_client = ContainerServiceClient( - self.credential, self.subscription_id - ) - self.auth_client = AuthorizationManagementClient( - self.credential, self.subscription_id - ) - - # Track created resources for cleanup - self.created_resources: Dict[str, List[str]] = { - "resource_groups": [], - "virtual_networks": [], - "subnets": [], - "network_security_groups": [], - "service_principals": [], - "aks_clusters": [], - "node_pools": [], - } - - def validate_credentials(self) -> bool: - """Validate Azure credentials and required permissions.""" - try: - # Test basic Azure access by listing resource groups - list(self.resource_client.resource_groups.list()) - logger.info( - f"✅ Azure credentials validated for subscription: {self.subscription_id}" - ) - - # Check required service access (basic check) - required_services = ["containerservice", "network", "authorization"] - for service in required_services: - try: - # Simple API call to test permissions - if service == "containerservice": - list(self.container_client.managed_clusters.list()) - elif service == "network": - list(self.network_client.virtual_networks.list_all()) - elif service == "authorization": - # Test authorization access - pass - - logger.debug(f"✅ {service.upper()} service access confirmed") - except Exception as e: - logger.warning(f"⚠️ Limited {service.upper()} permissions: {e}") - - return True - - except Exception as e: - logger.error(f"❌ Azure credential validation failed: {e}") - return False - - def provision_complete_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """ - Create complete AKS cluster infrastructure from scratch. - - Steps: - 1. Create resource group - 2. Create virtual network and subnets - 3. Create network security groups - 4. Create service principal for AKS - 5. Create AKS control plane - 6. Create and configure node pools - 7. Configure kubectl access - 8. Set up Clustrix namespace and RBAC - 9. Verify cluster is ready for jobs - """ - logger.info(f"🚀 Starting AKS cluster provisioning: {spec.cluster_name}") - - try: - # Step 1: Create resource group - resource_group_config = self._create_resource_group(spec) - - # Step 2: Create network infrastructure - network_config = self._create_network_infrastructure( - spec, resource_group_config - ) - - # Step 3: Create service principal - sp_config = self._create_service_principal(spec) - - # Step 4: Create AKS control plane - cluster_info = self._create_aks_control_plane( - spec, resource_group_config, network_config, sp_config - ) - - # Step 5: Create node pools - self._create_node_pools( - spec, cluster_info, resource_group_config, network_config - ) - - # Step 6: Configure kubectl access - kubectl_config = self._configure_kubectl_access( - cluster_info, resource_group_config - ) - - # Step 7: Set up Clustrix environment - self._setup_clustrix_environment(cluster_info, kubectl_config) - - # Step 8: Verify cluster ready - self._verify_cluster_operational( - cluster_info["cluster_name"], resource_group_config["name"] - ) - - result = { - "cluster_id": cluster_info["cluster_name"], - "cluster_name": cluster_info["cluster_name"], - "provider": "azure", - "region": self.region, - "endpoint": cluster_info["fqdn"], - "resource_group": resource_group_config["name"], - "version": cluster_info["version"], - "node_count": spec.node_count, - "vm_size": spec.azure_vm_size, - "virtual_network": network_config["vnet_name"], - "subnet": network_config["subnet_name"], - "kubectl_config": kubectl_config, - "ready_for_jobs": True, - "created_resources": self.created_resources.copy(), - } - - logger.info(f"✅ AKS cluster provisioning completed: {spec.cluster_name}") - return result - - except Exception as e: - logger.error(f"❌ AKS cluster provisioning failed: {e}") - # Attempt cleanup of any created resources - self._cleanup_failed_provisioning(spec.cluster_name) - raise - - def _create_resource_group(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create Azure resource group.""" - logger.info("🏗️ Creating resource group...") - - resource_group_name = f"clustrix-aks-rg-{spec.cluster_name}" - - resource_group_params = { - "location": self.region, - "tags": { - "clustrix:managed": "true", - "clustrix:cluster": spec.cluster_name, - "clustrix:provider": "azure", - }, - } - - self.resource_client.resource_groups.create_or_update( - resource_group_name, resource_group_params - ) - - self.created_resources["resource_groups"].append(resource_group_name) - - logger.info(f"✅ Created resource group: {resource_group_name}") - return {"name": resource_group_name, "location": self.region} - - def _create_network_infrastructure( - self, spec: ClusterSpec, rg_config: Dict[str, Any] - ) -> Dict[str, Any]: - """Create virtual network with all networking components.""" - logger.info("🌐 Creating network infrastructure...") - - vnet_name = f"clustrix-aks-vnet-{spec.cluster_name}" - subnet_name = f"clustrix-aks-subnet-{spec.cluster_name}" - nsg_name = f"clustrix-aks-nsg-{spec.cluster_name}" - - # Create Network Security Group - nsg_params = { - "location": self.region, - "security_rules": [ - { - "name": "AllowSSH", - "priority": 1000, - "direction": "Inbound", - "access": "Allow", - "protocol": "Tcp", - "source_port_range": "*", - "destination_port_range": "22", - "source_address_prefix": "*", - "destination_address_prefix": "*", - }, - { - "name": "AllowHTTPS", - "priority": 1001, - "direction": "Inbound", - "access": "Allow", - "protocol": "Tcp", - "source_port_range": "*", - "destination_port_range": "443", - "source_address_prefix": "*", - "destination_address_prefix": "*", - }, - ], - "tags": {"clustrix:cluster": spec.cluster_name}, - } - - nsg = self.network_client.network_security_groups.begin_create_or_update( - rg_config["name"], nsg_name, nsg_params - ).result() - - self.created_resources["network_security_groups"].append( - f"{rg_config['name']}/{nsg_name}" - ) - - # Create Virtual Network - vnet_params = { - "location": self.region, - "address_space": {"address_prefixes": ["10.0.0.0/8"]}, - "tags": {"clustrix:cluster": spec.cluster_name}, - } - - vnet = self.network_client.virtual_networks.begin_create_or_update( - rg_config["name"], vnet_name, vnet_params - ).result() - - self.created_resources["virtual_networks"].append( - f"{rg_config['name']}/{vnet_name}" - ) - - # Create Subnet - subnet_params = { - "address_prefix": "10.240.0.0/16", - "network_security_group": {"id": nsg.id}, - } - - subnet = self.network_client.subnets.begin_create_or_update( - rg_config["name"], vnet_name, subnet_name, subnet_params - ).result() - - self.created_resources["subnets"].append( - f"{rg_config['name']}/{vnet_name}/{subnet_name}" - ) - - logger.info(f"✅ Created network infrastructure: {vnet_name}") - return { - "vnet_name": vnet_name, - "vnet_id": vnet.id, - "subnet_name": subnet_name, - "subnet_id": subnet.id, - "nsg_name": nsg_name, - "nsg_id": nsg.id, - } - - def _create_service_principal(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create service principal for AKS cluster.""" - logger.info("👤 Creating service principal...") - - # For simplified implementation, we'll use the existing service principal - # In a full implementation, you would create a new service principal - # specifically for this AKS cluster - - return {"client_id": self.client_id, "client_secret": self.client_secret} - - def _create_aks_control_plane( - self, - spec: ClusterSpec, - rg_config: Dict[str, Any], - network_config: Dict[str, Any], - sp_config: Dict[str, Any], - ) -> Dict[str, Any]: - """Create AKS control plane.""" - logger.info("🎛️ Creating AKS control plane...") - - cluster_params = { - "location": self.region, - "service_principal_profile": { - "client_id": sp_config["client_id"], - "secret": sp_config["client_secret"], - }, - "dns_prefix": f"clustrix-{spec.cluster_name}", - "kubernetes_version": spec.kubernetes_version, - "agent_pool_profiles": [ - { - "name": "default", - "count": spec.node_count, - "vm_size": spec.azure_vm_size, - "os_type": "Linux", - "vnet_subnet_id": network_config["subnet_id"], - "enable_auto_scaling": True, - "min_count": max(1, spec.node_count // 2), - "max_count": spec.node_count * 2, - "type": "VirtualMachineScaleSets", - "mode": "System", - } - ], - "network_profile": { - "network_plugin": "azure", - "service_cidr": "10.0.0.0/16", - "dns_service_ip": "10.0.0.10", - "docker_bridge_cidr": "172.17.0.1/16", - }, - "enable_rbac": True, - "tags": {"clustrix:managed": "true", "clustrix:cluster": spec.cluster_name}, - } - - # Create AKS cluster - logger.info("⏳ Creating AKS cluster (this may take several minutes)...") - cluster_operation = ( - self.container_client.managed_clusters.begin_create_or_update( - rg_config["name"], spec.cluster_name, cluster_params - ) - ) - - cluster = cluster_operation.result(timeout=3600) # 1 hour timeout - self.created_resources["aks_clusters"].append( - f"{rg_config['name']}/{spec.cluster_name}" - ) - - logger.info(f"✅ AKS control plane ready: {cluster.fqdn}") - return { - "cluster_name": cluster.name, - "fqdn": f"https://{cluster.fqdn}", - "location": cluster.location, - "version": cluster.kubernetes_version, - "provisioning_state": cluster.provisioning_state, - "resource_group": rg_config["name"], - } - - def _create_node_pools( - self, - spec: ClusterSpec, - cluster_info: Dict[str, Any], - rg_config: Dict[str, Any], - network_config: Dict[str, Any], - ) -> Dict[str, Any]: - """Configure AKS node pools (already created with cluster).""" - logger.info("💻 Configuring AKS node pools...") - - # The initial node pool was created with the cluster - # In a more advanced implementation, we might create additional node pools here - - logger.info("✅ Node pools configured") - return {"default_pool": "default"} - - def _configure_kubectl_access( - self, cluster_info: Dict[str, Any], rg_config: Dict[str, Any] - ) -> Dict[str, Any]: - """Configure kubectl access to the cluster.""" - logger.info("⚙️ Configuring kubectl access...") - - try: - # Get cluster credentials using Azure CLI - credentials = ( - self.container_client.managed_clusters.list_cluster_user_credentials( - rg_config["name"], cluster_info["cluster_name"] - ) - ) - - if credentials.kubeconfigs: - # Parse kubeconfig from the credentials - import base64 - import yaml - - kubeconfig_data = base64.b64decode( - credentials.kubeconfigs[0].value - ).decode("utf-8") - kubeconfig = yaml.safe_load(kubeconfig_data) - - return kubeconfig - else: - raise RuntimeError("No kubeconfig found in cluster credentials") - - except Exception as e: - logger.error(f"Failed to get cluster credentials: {e}") - # Return a basic kubeconfig structure - return { - "apiVersion": "v1", - "kind": "Config", - "clusters": [ - { - "cluster": {"server": cluster_info["fqdn"]}, - "name": cluster_info["cluster_name"], - } - ], - "contexts": [ - { - "context": { - "cluster": cluster_info["cluster_name"], - "user": cluster_info["cluster_name"], - }, - "name": cluster_info["cluster_name"], - } - ], - "current-context": cluster_info["cluster_name"], - "users": [ - { - "name": cluster_info["cluster_name"], - "user": { - "exec": { - "apiVersion": "client.authentication.k8s.io/v1beta1", - "command": "az", - "args": [ - "aks", - "get-credentials", - "--resource-group", - rg_config["name"], - "--name", - cluster_info["cluster_name"], - "--format", - "exec", - ], - } - }, - } - ], - } - - def _setup_clustrix_environment( - self, cluster_info: Dict[str, Any], kubectl_config: Dict[str, Any] - ) -> None: - """Set up Clustrix namespace and RBAC.""" - logger.info("🔧 Setting up Clustrix environment...") - - try: - # Write kubeconfig to temporary file - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - import yaml - - yaml.dump(kubectl_config, f) - kubeconfig_path = f.name - - # Create namespace - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "namespace", - "clustrix", - ], - check=False, - capture_output=True, - ) - - # Create service account - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "serviceaccount", - "clustrix-worker", - "--namespace", - "clustrix", - ], - check=False, - capture_output=True, - ) - - # Create cluster role binding - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "clusterrolebinding", - "clustrix-worker-binding", - "--clusterrole", - "cluster-admin", - "--serviceaccount", - "clustrix:clustrix-worker", - ], - check=False, - capture_output=True, - ) - - logger.info("✅ Clustrix environment configured") - - except Exception as e: - logger.warning(f"⚠️ Failed to configure Clustrix environment: {e}") - finally: - # Clean up temporary kubeconfig file - import os - - try: - os.unlink(kubeconfig_path) - except Exception: - pass - - def _verify_cluster_operational( - self, cluster_name: str, resource_group_name: str - ) -> None: - """Verify cluster is ready for job submission.""" - logger.info("🔍 Verifying cluster is operational...") - - try: - # Check cluster status - cluster = self.container_client.managed_clusters.get( - resource_group_name, cluster_name - ) - if cluster.provisioning_state != "Succeeded": - raise RuntimeError(f"Cluster not ready: {cluster.provisioning_state}") - - # Check agent pools - agent_pools = list( - self.container_client.agent_pools.list( - resource_group_name, cluster_name - ) - ) - for pool in agent_pools: - if pool.provisioning_state != "Succeeded": - raise RuntimeError( - f"Agent pool not ready: {pool.provisioning_state}" - ) - - logger.info("✅ Cluster verification completed") - - except Exception as e: - logger.error(f"❌ Cluster verification failed: {e}") - raise - - def destroy_cluster_infrastructure(self, cluster_id: str) -> bool: - """Destroy cluster and all associated infrastructure.""" - logger.info(f"🧹 Destroying AKS cluster: {cluster_id}") - - try: - # Find resource group for this cluster - resource_group_name = None - for rg_name in self.created_resources.get("resource_groups", []): - if cluster_id in rg_name: - resource_group_name = rg_name - break - - if not resource_group_name: - # Try to find cluster directly - for cluster_path in self.created_resources.get("aks_clusters", []): - if cluster_id in cluster_path: - resource_group_name = cluster_path.split("/")[0] - break - - if resource_group_name: - # Delete entire resource group (this deletes all contained resources) - logger.info(f"Deleting resource group: {resource_group_name}") - delete_operation = self.resource_client.resource_groups.begin_delete( - resource_group_name - ) - delete_operation.result(timeout=3600) # 1 hour timeout - logger.info(f"✅ Deleted resource group: {resource_group_name}") - else: - logger.warning( - f"Could not find resource group for cluster: {cluster_id}" - ) - - return True - - except Exception as e: - logger.error(f"❌ Failed to destroy cluster: {e}") - return False - - def get_cluster_status(self, cluster_id: str) -> Dict[str, Any]: - """Get detailed cluster status and health information.""" - try: - # Find resource group for this cluster - resource_group_name = None - for rg_name in self.created_resources.get("resource_groups", []): - if cluster_id in rg_name: - resource_group_name = rg_name - break - - if not resource_group_name: - # Try to find from existing clusters - for cluster_path in self.created_resources.get("aks_clusters", []): - if cluster_id in cluster_path: - resource_group_name = cluster_path.split("/")[0] - break - - if not resource_group_name: - return { - "cluster_id": cluster_id, - "status": "NOT_FOUND", - "ready_for_jobs": False, - } - - cluster = self.container_client.managed_clusters.get( - resource_group_name, cluster_id - ) - - # Check agent pools - agent_pools = list( - self.container_client.agent_pools.list(resource_group_name, cluster_id) - ) - node_status = [] - for pool in agent_pools: - node_status.append( - { - "name": pool.name, - "status": pool.provisioning_state, - "node_count": pool.count, - } - ) - - return { - "cluster_id": cluster_id, - "status": cluster.provisioning_state, - "endpoint": f"https://{cluster.fqdn}", - "version": cluster.kubernetes_version, - "location": cluster.location, - "node_pools": node_status, - "ready_for_jobs": ( - cluster.provisioning_state == "Succeeded" - and all(np["status"] == "Succeeded" for np in node_status) - ), - } - - except ResourceNotFoundError: - return { - "cluster_id": cluster_id, - "status": "NOT_FOUND", - "ready_for_jobs": False, - } - except Exception as e: - logger.error(f"Error getting cluster status: {e}") - raise - - def _cleanup_failed_provisioning(self, cluster_name: str) -> None: - """Clean up resources if provisioning fails.""" - logger.info("🧹 Cleaning up failed provisioning...") - try: - self._cleanup_all_resources() - except Exception as e: - logger.error(f"Error during cleanup: {e}") - - def _cleanup_all_resources(self) -> None: - """Clean up all tracked resources.""" - logger.info("🧹 Cleaning up all created resources...") - - # For Azure, the simplest approach is to delete resource groups - # which automatically deletes all contained resources - for rg_name in self.created_resources.get("resource_groups", []): - try: - logger.info(f"Deleting resource group: {rg_name}") - delete_operation = self.resource_client.resource_groups.begin_delete( - rg_name - ) - delete_operation.result(timeout=3600) - logger.info(f"✅ Deleted resource group: {rg_name}") - except Exception as e: - logger.warning(f"Failed to delete resource group {rg_name}: {e}") diff --git a/clustrix/kubernetes/cluster_provisioner.py b/clustrix/kubernetes/cluster_provisioner.py deleted file mode 100644 index b3e65568..00000000 --- a/clustrix/kubernetes/cluster_provisioner.py +++ /dev/null @@ -1,423 +0,0 @@ -""" -Core Kubernetes cluster provisioning infrastructure. - -Provides from-scratch Kubernetes cluster creation across supported cloud providers -with complete infrastructure setup and Clustrix integration. -""" - -import logging -import time -from typing import Dict, Any, Optional, List -from abc import ABC, abstractmethod -from dataclasses import dataclass - -from ..config import ClusterConfig -from ..credential_manager import get_credential_manager - -logger = logging.getLogger(__name__) - - -@dataclass -class ClusterSpec: - """Specification for Kubernetes cluster provisioning.""" - - provider: str # aws, gcp, azure, huggingface, lambda - cluster_name: str - region: str - node_count: int = 2 - node_type: Optional[str] = None - kubernetes_version: str = "1.28" - from_scratch: bool = True - auto_cleanup: bool = True - - # Provider-specific configurations (defaults) - aws_instance_type: str = "t3.medium" - gcp_machine_type: str = "e2-standard-4" - azure_vm_size: str = "Standard_D2s_v3" - - def __post_init__(self): - """Set provider-specific node types based on generic node_type if provided.""" - if self.node_type: - if self.provider == "aws": - self.aws_instance_type = self.node_type - elif self.provider == "gcp": - self.gcp_machine_type = self.node_type - elif self.provider == "azure": - self.azure_vm_size = self.node_type - - -class BaseKubernetesProvisioner(ABC): - """Abstract base class for Kubernetes cluster provisioners.""" - - def __init__(self, credentials: Dict[str, str], region: str): - self.credentials = credentials - self.region = region - self.cluster_info: Optional[Dict[str, Any]] = None - - @abstractmethod - def provision_complete_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """Provision complete Kubernetes cluster infrastructure from scratch.""" - pass - - @abstractmethod - def destroy_cluster_infrastructure(self, cluster_id: str) -> bool: - """Destroy cluster and all associated infrastructure.""" - pass - - @abstractmethod - def get_cluster_status(self, cluster_id: str) -> Dict[str, Any]: - """Get detailed cluster status and health information.""" - pass - - @abstractmethod - def validate_credentials(self) -> bool: - """Validate that credentials have required permissions.""" - pass - - -class KubernetesClusterProvisioner: - """ - Central orchestrator for Kubernetes cluster provisioning and management. - - This class provides the main interface for from-scratch Kubernetes cluster - provisioning across all supported cloud providers. It handles credential - management, provider selection, and cluster lifecycle operations. - """ - - def __init__(self, config: ClusterConfig): - self.config = config - self.credential_manager = get_credential_manager() - self._provisioners: Dict[str, BaseKubernetesProvisioner] = {} - - def provision_cluster_if_needed(self, cluster_spec: ClusterSpec) -> Dict[str, Any]: - """ - Main entry point for cluster provisioning. - - Checks if a suitable cluster exists, otherwise provisions a new one - from scratch with complete infrastructure setup. - - Args: - cluster_spec: Complete specification for the desired cluster - - Returns: - Dictionary containing cluster configuration ready for job execution - """ - logger.info( - f"🚀 Starting Kubernetes cluster provisioning for {cluster_spec.provider}" - ) - - try: - # 1. Get and validate credentials - credentials = self._get_provider_credentials(cluster_spec.provider) - if not credentials: - raise ValueError( - f"No credentials found for provider: {cluster_spec.provider}" - ) - - # 2. Initialize provider-specific provisioner - provisioner = self._get_provisioner( - cluster_spec.provider, credentials, cluster_spec.region - ) - - # 3. Validate credentials and permissions - if not provisioner.validate_credentials(): - raise ValueError( - f"Invalid or insufficient credentials for {cluster_spec.provider}" - ) - - # 4. Check for existing cluster - existing_cluster = self._find_existing_cluster(provisioner, cluster_spec) - if existing_cluster: - logger.info( - f"✅ Found existing cluster: {existing_cluster['cluster_id']}" - ) - return existing_cluster - - # 5. Provision new cluster from scratch - logger.info("🏗️ No suitable cluster found, provisioning from scratch...") - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # 6. Verify cluster is ready for Clustrix jobs - self._verify_cluster_ready(provisioner, cluster_info["cluster_id"]) - - logger.info( - f"✅ Cluster provisioning completed: {cluster_info['cluster_id']}" - ) - return cluster_info - - except Exception as e: - logger.error(f"❌ Cluster provisioning failed: {e}") - raise - - def destroy_cluster(self, cluster_id: str, provider: str) -> bool: - """ - Completely destroy cluster and all associated infrastructure. - - Args: - cluster_id: Unique identifier for the cluster - provider: Cloud provider (aws, gcp, azure, etc.) - - Returns: - True if destruction was successful - """ - logger.info(f"🧹 Destroying cluster: {cluster_id}") - - try: - credentials = self._get_provider_credentials(provider) - if not credentials: - raise ValueError(f"No credentials available for provider: {provider}") - - region = self.config.k8s_region or "us-east-1" # Default region - provisioner = self._get_provisioner(provider, credentials, region) - - success = provisioner.destroy_cluster_infrastructure(cluster_id) - - if success: - logger.info(f"✅ Cluster destroyed successfully: {cluster_id}") - else: - logger.error(f"❌ Failed to destroy cluster: {cluster_id}") - - return success - - except Exception as e: - logger.error(f"❌ Error destroying cluster {cluster_id}: {e}") - return False - - def list_clusters(self, provider: Optional[str] = None) -> List[Dict[str, Any]]: - """ - List all managed Kubernetes clusters. - - Args: - provider: Specific provider to list, or None for all providers - - Returns: - List of cluster information dictionaries - """ - clusters = [] - - providers_to_check = ( - [provider] if provider else ["aws", "gcp", "azure", "huggingface", "lambda"] - ) - - for prov in providers_to_check: - try: - credentials = self._get_provider_credentials(prov) - if not credentials: - continue - - region = self.config.k8s_region or "us-east-1" # Default region - provisioner = self._get_provisioner(prov, credentials, region) - provider_clusters = self._list_provider_clusters(provisioner, prov) - clusters.extend(provider_clusters) - - except Exception as e: - logger.debug(f"Error listing clusters for {prov}: {e}") - - return clusters - - def _get_provider_credentials(self, provider: str) -> Optional[Dict[str, str]]: - """Get credentials for specified Kubernetes provider with provider-specific mapping.""" - # Local providers don't need real credentials - if provider in ["local", "local-docker"]: - return {"type": "local"} - - return self.credential_manager.ensure_kubernetes_provider_credentials(provider) - - def _get_provisioner( - self, provider: str, credentials: Dict[str, str], region: str - ) -> BaseKubernetesProvisioner: - """Get or create provider-specific provisioner.""" - provisioner_key = f"{provider}_{region}" - - if provisioner_key not in self._provisioners: - if provider == "aws": - from .aws_provisioner import AWSEKSFromScratchProvisioner - - self._provisioners[provisioner_key] = AWSEKSFromScratchProvisioner( - credentials, region - ) - elif provider == "gcp": - from .gcp_provisioner import GCPGKEFromScratchProvisioner - - self._provisioners[provisioner_key] = GCPGKEFromScratchProvisioner( - credentials, region - ) - elif provider == "azure": - from .azure_provisioner import AzureAKSFromScratchProvisioner - - self._provisioners[provisioner_key] = AzureAKSFromScratchProvisioner( - credentials, region - ) - elif provider == "huggingface": - from .huggingface_provisioner import HuggingFaceKubernetesProvisioner - - self._provisioners[provisioner_key] = HuggingFaceKubernetesProvisioner( - credentials, region - ) - elif provider == "lambda": - from .lambda_provisioner import LambdaCloudKubernetesProvisioner - - self._provisioners[provisioner_key] = LambdaCloudKubernetesProvisioner( - credentials, region - ) - elif provider == "local" or provider == "local-docker": - from .local_provisioner import LocalDockerKubernetesProvisioner - - self._provisioners[provisioner_key] = LocalDockerKubernetesProvisioner( - credentials, region - ) - else: - raise ValueError(f"Unsupported provider: {provider}") - - return self._provisioners[provisioner_key] - - def _find_existing_cluster( - self, provisioner: BaseKubernetesProvisioner, spec: ClusterSpec - ) -> Optional[Dict[str, Any]]: - """Check for existing cluster that meets specifications.""" - try: - # Implementation will check for clusters with matching tags/labels - # For now, always provision new cluster - return None - except Exception as e: - logger.debug(f"Error checking for existing clusters: {e}") - return None - - def _verify_cluster_ready( - self, provisioner: BaseKubernetesProvisioner, cluster_id: str - ) -> bool: - """Verify cluster is ready for Clustrix job execution.""" - max_attempts = 30 - wait_time = 30 # seconds - - for attempt in range(max_attempts): - try: - status = provisioner.get_cluster_status(cluster_id) - if status.get("ready_for_jobs", False): - return True - - logger.info( - f"⏳ Cluster not ready yet, waiting... ({attempt + 1}/{max_attempts})" - ) - time.sleep(wait_time) - - except Exception as e: - logger.debug(f"Error checking cluster status: {e}") - time.sleep(wait_time) - - raise RuntimeError( - f"Cluster {cluster_id} not ready after {max_attempts * wait_time} seconds" - ) - - def _list_provider_clusters( - self, provisioner: BaseKubernetesProvisioner, provider: str - ) -> List[Dict[str, Any]]: - """List clusters for specific provider.""" - # Implementation would call provider-specific cluster listing - # For now, return empty list - return [] - - -# Convenience functions for direct usage - - -def provision_kubernetes_cluster( - provider: str, - cluster_name: str, - region: str, - node_count: int = 2, - node_type: Optional[str] = None, - kubernetes_version: str = "1.28", - from_scratch: bool = True, - config: Optional[ClusterConfig] = None, -) -> Dict[str, Any]: - """ - Provision a Kubernetes cluster with specified configuration. - - This is a convenience function for direct cluster provisioning outside - of the @cluster decorator workflow. - - Args: - provider: Cloud provider (aws, gcp, azure, huggingface, lambda) - cluster_name: Name for the cluster - region: Cloud provider region - node_count: Number of worker nodes - node_type: Provider-specific instance type - kubernetes_version: Kubernetes version to install - from_scratch: Whether to create all infrastructure from scratch - config: Optional ClusterConfig to use - - Returns: - Dictionary containing cluster configuration - - Example: - >>> cluster_config = provision_kubernetes_cluster( - ... provider="aws", - ... cluster_name="my-cluster", - ... region="us-west-2", - ... node_count=3, - ... node_type="t3.large" - ... ) - >>> print(f"Cluster endpoint: {cluster_config['endpoint']}") - """ - if config is None: - config = ClusterConfig( - k8s_provider=provider, - k8s_region=region, - k8s_node_count=node_count, - k8s_version=kubernetes_version, - ) - - spec = ClusterSpec( - provider=provider, - cluster_name=cluster_name, - region=region, - node_count=node_count, - node_type=node_type, - kubernetes_version=kubernetes_version, - from_scratch=from_scratch, - ) - - provisioner = KubernetesClusterProvisioner(config) - return provisioner.provision_cluster_if_needed(spec) - - -def destroy_kubernetes_cluster( - cluster_id: str, provider: str, config: Optional[ClusterConfig] = None -) -> bool: - """ - Destroy a Kubernetes cluster and all associated infrastructure. - - Args: - cluster_id: Unique identifier for the cluster - provider: Cloud provider - config: Optional ClusterConfig to use - - Returns: - True if destruction was successful - """ - if config is None: - config = ClusterConfig(k8s_provider=provider) - - provisioner = KubernetesClusterProvisioner(config) - return provisioner.destroy_cluster(cluster_id, provider) - - -def list_kubernetes_clusters( - provider: Optional[str] = None, config: Optional[ClusterConfig] = None -) -> List[Dict[str, Any]]: - """ - List all managed Kubernetes clusters. - - Args: - provider: Specific provider to list, or None for all - config: Optional ClusterConfig to use - - Returns: - List of cluster information dictionaries - """ - if config is None: - config = ClusterConfig() - - provisioner = KubernetesClusterProvisioner(config) - return provisioner.list_clusters(provider) diff --git a/clustrix/kubernetes/gcp_provisioner.py b/clustrix/kubernetes/gcp_provisioner.py deleted file mode 100644 index 39034d32..00000000 --- a/clustrix/kubernetes/gcp_provisioner.py +++ /dev/null @@ -1,827 +0,0 @@ -""" -GCP GKE from-scratch provisioner. - -Provides complete GKE cluster provisioning with all required infrastructure -including VPC, service accounts, firewall rules, and node pools. -""" - -import json -import logging -import time -from typing import Dict, Any, List -import subprocess -import tempfile - -try: - from google.cloud import container_v1 - from google.cloud import compute_v1 - from google.cloud import iam_v1 - from google.oauth2 import service_account - from google.api_core import exceptions as gcp_exceptions - - GCP_AVAILABLE = True -except ImportError: - GCP_AVAILABLE = False - container_v1 = None - compute_v1 = None - iam_v1 = None - service_account = None - gcp_exceptions = None - -from .cluster_provisioner import BaseKubernetesProvisioner, ClusterSpec - -logger = logging.getLogger(__name__) - - -class GCPGKEFromScratchProvisioner(BaseKubernetesProvisioner): - """ - Complete GCP GKE cluster provisioner from blank GCP project. - - This provisioner creates all required infrastructure components: - - VPC with custom subnets - - Firewall rules for GKE networking - - Service accounts with proper IAM roles - - GKE control plane - - GKE node pools with auto-scaling - - kubectl configuration - - Clustrix namespace and RBAC setup - """ - - def __init__(self, credentials: Dict[str, str], region: str): - super().__init__(credentials, region) - - if not GCP_AVAILABLE: - raise ImportError( - "google-cloud-container required for GCP GKE " - "provisioning. Install with: pip install " - "google-cloud-container google-cloud-compute " - "google-cloud-iam" - ) - - # Parse service account key - service_account_key = credentials.get("service_account_key") - if not service_account_key: - raise ValueError("service_account_key required for GCP authentication") - - try: - # Handle both JSON string and file path - if service_account_key.startswith("{"): - key_data = json.loads(service_account_key) - else: - with open(service_account_key, "r") as f: - key_data = json.load(f) - except (json.JSONDecodeError, FileNotFoundError) as e: - raise ValueError(f"Invalid service account key: {e}") - - # Initialize credentials and clients - self.credentials_obj = service_account.Credentials.from_service_account_info( - key_data - ) - self.project_id = key_data.get("project_id") - if not self.project_id: - raise ValueError("project_id not found in service account key") - - # Initialize GCP clients - self.container_client = container_v1.ClusterManagerClient( - credentials=self.credentials_obj - ) - self.compute_client = compute_v1.InstancesClient( - credentials=self.credentials_obj - ) - self.networks_client = compute_v1.NetworksClient( - credentials=self.credentials_obj - ) - self.subnetworks_client = compute_v1.SubnetworksClient( - credentials=self.credentials_obj - ) - self.firewalls_client = compute_v1.FirewallsClient( - credentials=self.credentials_obj - ) - self.iam_client = iam_v1.IAMClient(credentials=self.credentials_obj) - - # Track created resources for cleanup - self.created_resources: Dict[str, List[str]] = { - "networks": [], - "subnets": [], - "firewall_rules": [], - "service_accounts": [], - "gke_clusters": [], - "node_pools": [], - } - - def validate_credentials(self) -> bool: - """Validate GCP credentials and required permissions.""" - try: - # Test basic GCP access by listing zones - zones_client = compute_v1.ZonesClient(credentials=self.credentials_obj) - list(zones_client.list(project=self.project_id, max_results=1)) - logger.info(f"✅ GCP credentials validated for project: {self.project_id}") - - # Check required API enablement (basic check) - required_apis = ["container", "compute", "iam"] - for api in required_apis: - try: - # Simple API call to check if service is enabled - if api == "container": - list( - self.container_client.list_clusters( - parent=f"projects/{self.project_id}/locations/-" - ) - ) - elif api == "compute": - list(zones_client.list(project=self.project_id, max_results=1)) - elif api == "iam": - # Test IAM API access - pass - - logger.debug(f"✅ {api.upper()} API access confirmed") - except Exception as e: - logger.warning(f"⚠️ Limited {api.upper()} API access: {e}") - - return True - - except Exception as e: - logger.error(f"❌ GCP credential validation failed: {e}") - return False - - def provision_complete_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """ - Create complete GKE cluster infrastructure from scratch. - - Steps: - 1. Create VPC network and subnets - 2. Create firewall rules - 3. Create service accounts and IAM roles - 4. Create GKE control plane - 5. Create and configure node pools - 6. Configure kubectl access - 7. Set up Clustrix namespace and RBAC - 8. Verify cluster is ready for jobs - """ - logger.info(f"🚀 Starting GKE cluster provisioning: {spec.cluster_name}") - - try: - # Step 1: Create VPC infrastructure - vpc_config = self._create_vpc_infrastructure(spec) - - # Step 2: Create IAM infrastructure - iam_config = self._create_iam_infrastructure(spec) - - # Step 3: Create GKE control plane - cluster_info = self._create_gke_control_plane(spec, vpc_config, iam_config) - - # Step 4: Create node pools - self._create_node_pools(spec, cluster_info, vpc_config, iam_config) - - # Step 5: Configure kubectl access - kubectl_config = self._configure_kubectl_access(cluster_info) - - # Step 6: Set up Clustrix environment - self._setup_clustrix_environment(cluster_info, kubectl_config) - - # Step 7: Verify cluster ready - self._verify_cluster_operational(cluster_info["cluster_name"]) - - result = { - "cluster_id": cluster_info["cluster_name"], - "cluster_name": cluster_info["cluster_name"], - "provider": "gcp", - "region": self.region, - "endpoint": cluster_info["endpoint"], - "location": cluster_info["location"], - "version": cluster_info["version"], - "node_count": spec.node_count, - "machine_type": spec.gcp_machine_type, - "network": vpc_config["network_name"], - "subnet": vpc_config["subnet_name"], - "kubectl_config": kubectl_config, - "ready_for_jobs": True, - "created_resources": self.created_resources.copy(), - } - - logger.info(f"✅ GKE cluster provisioning completed: {spec.cluster_name}") - return result - - except Exception as e: - logger.error(f"❌ GKE cluster provisioning failed: {e}") - # Attempt cleanup of any created resources - self._cleanup_failed_provisioning(spec.cluster_name) - raise - - def _create_vpc_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create VPC with all networking components.""" - logger.info("🏗️ Creating VPC infrastructure...") - - network_name = f"clustrix-gke-network-{spec.cluster_name}" - subnet_name = f"clustrix-gke-subnet-{spec.cluster_name}" - - # Create VPC network - network_body = { - "name": network_name, - "auto_create_subnetworks": False, # Use custom subnets - "routing_config": {"routing_mode": "REGIONAL"}, - "description": f"VPC network for Clustrix GKE cluster {spec.cluster_name}", - } - - operation = self.networks_client.insert( - project=self.project_id, network_resource=network_body - ) - self._wait_for_operation(operation, "network creation") - self.created_resources["networks"].append(network_name) - - # Create subnet - subnet_body = { - "name": subnet_name, - "network": f"projects/{self.project_id}/global/networks/{network_name}", - "ip_cidr_range": "10.0.0.0/16", - "region": self.region, - "description": f"Subnet for Clustrix GKE cluster {spec.cluster_name}", - "secondary_ip_ranges": [ - {"range_name": "gke-pods", "ip_cidr_range": "10.1.0.0/16"}, - {"range_name": "gke-services", "ip_cidr_range": "10.2.0.0/16"}, - ], - "private_ip_google_access": True, - } - - operation = self.subnetworks_client.insert( - project=self.project_id, region=self.region, subnetwork_resource=subnet_body - ) - self._wait_for_operation(operation, "subnet creation") - self.created_resources["subnets"].append(f"{self.region}/{subnet_name}") - - # Create firewall rules - self._create_firewall_rules(network_name, spec) - - return { - "network_name": network_name, - "network_url": f"projects/{self.project_id}/global/networks/{network_name}", - "subnet_name": subnet_name, - "subnet_url": f"projects/{self.project_id}/regions/{self.region}/subnetworks/{subnet_name}", - "pod_range_name": "gke-pods", - "service_range_name": "gke-services", - } - - def _create_firewall_rules(self, network_name: str, spec: ClusterSpec) -> None: - """Create firewall rules for GKE cluster.""" - logger.info("🔒 Creating firewall rules...") - - # Allow internal cluster communication - internal_rule_name = f"clustrix-gke-internal-{spec.cluster_name}" - internal_rule = { - "name": internal_rule_name, - "network": f"projects/{self.project_id}/global/networks/{network_name}", - "description": f"Allow internal communication for GKE cluster {spec.cluster_name}", - "direction": "INGRESS", - "priority": 1000, - "source_ranges": ["10.0.0.0/8"], - "allowed": [ - {"IP_protocol": "tcp"}, - {"IP_protocol": "udp"}, - {"IP_protocol": "icmp"}, - ], - } - - operation = self.firewalls_client.insert( - project=self.project_id, firewall_resource=internal_rule - ) - self._wait_for_operation(operation, "firewall rule creation") - self.created_resources["firewall_rules"].append(internal_rule_name) - - # Allow SSH access (for debugging) - ssh_rule_name = f"clustrix-gke-ssh-{spec.cluster_name}" - ssh_rule = { - "name": ssh_rule_name, - "network": f"projects/{self.project_id}/global/networks/{network_name}", - "description": f"Allow SSH access for GKE cluster {spec.cluster_name}", - "direction": "INGRESS", - "priority": 1000, - "source_ranges": ["0.0.0.0/0"], - "target_tags": [f"clustrix-gke-{spec.cluster_name}"], - "allowed": [{"IP_protocol": "tcp", "ports": ["22"]}], - } - - operation = self.firewalls_client.insert( - project=self.project_id, firewall_resource=ssh_rule - ) - self._wait_for_operation(operation, "SSH firewall rule creation") - self.created_resources["firewall_rules"].append(ssh_rule_name) - - def _create_iam_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create IAM service accounts and roles for GKE.""" - logger.info("👤 Creating IAM infrastructure...") - - # Create service account for node pools - sa_name = f"clustrix-gke-nodes-{spec.cluster_name}" - sa_email = f"{sa_name}@{self.project_id}.iam.gserviceaccount.com" - - try: - # Create service account - service_account_body = { - "account_id": sa_name, - "service_account": { - "display_name": f"Clustrix GKE Node Service Account - {spec.cluster_name}", - "description": f"Service account for GKE nodes in cluster {spec.cluster_name}", - }, - } - - self.iam_client.create_service_account( - parent=f"projects/{self.project_id}", request=service_account_body - ) - self.created_resources["service_accounts"].append(sa_name) - - # Assign required roles to service account - required_roles = [ - "roles/logging.logWriter", - "roles/monitoring.metricWriter", - "roles/monitoring.viewer", - "roles/stackdriver.resourceMetadata.writer", - ] - - for role in required_roles: - self._assign_iam_role(sa_email, role) - - logger.info(f"✅ Created service account: {sa_email}") - - except gcp_exceptions.AlreadyExists: - logger.info(f"Service account {sa_email} already exists") - except Exception as e: - logger.warning(f"Failed to create service account: {e}") - sa_email = "default" # Use default service account - - return {"node_service_account": sa_email} - - def _assign_iam_role(self, member_email: str, role: str) -> None: - """Assign IAM role to service account.""" - try: - # This is a simplified implementation - # In practice, you'd use the IAM policy management APIs - logger.debug(f"Assigning role {role} to {member_email}") - except Exception as e: - logger.warning(f"Failed to assign role {role}: {e}") - - def _create_gke_control_plane( - self, spec: ClusterSpec, vpc_config: Dict[str, Any], iam_config: Dict[str, Any] - ) -> Dict[str, Any]: - """Create GKE control plane.""" - logger.info("🎛️ Creating GKE control plane...") - - # Choose zone within region - zone = f"{self.region}-a" - parent = f"projects/{self.project_id}/locations/{zone}" - - cluster_config = { - "name": spec.cluster_name, - "description": f"Clustrix GKE cluster - {spec.cluster_name}", - "initial_node_count": 1, # Will be removed after creating node pool - "network": vpc_config["network_url"], - "subnetwork": vpc_config["subnet_url"], - "ip_allocation_policy": { - "cluster_secondary_range_name": vpc_config["pod_range_name"], - "services_secondary_range_name": vpc_config["service_range_name"], - }, - "network_policy": {"enabled": True, "provider": "CALICO"}, - "addons_config": { - "http_load_balancing": {"disabled": False}, - "kubernetes_dashboard": {"disabled": True}, # Deprecated - "network_policy_config": {"disabled": False}, - }, - "logging_service": "logging.googleapis.com/kubernetes", - "monitoring_service": "monitoring.googleapis.com/kubernetes", - "initial_cluster_version": spec.kubernetes_version, - } - - # Create cluster - operation = self.container_client.create_cluster( - parent=parent, cluster=cluster_config - ) - - logger.info("⏳ Waiting for GKE cluster to be ready...") - self._wait_for_cluster_operation(operation) - - self.created_resources["gke_clusters"].append(spec.cluster_name) - - # Get cluster info - cluster = self.container_client.get_cluster( - name=f"projects/{self.project_id}/locations/{zone}/clusters/{spec.cluster_name}" - ) - - logger.info(f"✅ GKE control plane ready: {cluster.endpoint}") - return { - "cluster_name": cluster.name, - "endpoint": f"https://{cluster.endpoint}", - "location": zone, - "version": cluster.current_master_version, - "certificate_authority": cluster.master_auth.cluster_ca_certificate, - } - - def _create_node_pools( - self, - spec: ClusterSpec, - cluster_info: Dict[str, Any], - vpc_config: Dict[str, Any], - iam_config: Dict[str, Any], - ) -> Dict[str, Any]: - """Create GKE managed node pools.""" - logger.info("💻 Creating GKE node pools...") - - node_pool_name = f"clustrix-nodes-{spec.cluster_name}" - parent = f"projects/{self.project_id}/locations/{cluster_info['location']}/clusters/{spec.cluster_name}" - - node_pool_config = { - "name": node_pool_name, - "initial_node_count": spec.node_count, - "config": { - "machine_type": spec.gcp_machine_type, - "disk_size_gb": 50, - "disk_type": "pd-standard", - "oauth_scopes": [ - "https://www.googleapis.com/auth/logging.write", - "https://www.googleapis.com/auth/monitoring", - ], - "service_account": iam_config.get("node_service_account", "default"), - "tags": [f"clustrix-gke-{spec.cluster_name}"], - }, - "autoscaling": { - "enabled": True, - "min_node_count": max(1, spec.node_count // 2), - "max_node_count": spec.node_count * 2, - }, - "management": {"auto_repair": True, "auto_upgrade": True}, - } - - # Create node pool - operation = self.container_client.create_node_pool( - parent=parent, node_pool=node_pool_config - ) - - logger.info("⏳ Waiting for node pool to be ready...") - self._wait_for_cluster_operation(operation) - - self.created_resources["node_pools"].append(node_pool_name) - - # Delete default node pool that was created with cluster - try: - default_pool_parent = ( - f"projects/{self.project_id}/locations/" - f"{cluster_info['location']}/clusters/{spec.cluster_name}/" - f"nodePools/default-pool" - ) - delete_operation = self.container_client.delete_node_pool( - name=default_pool_parent - ) - self._wait_for_cluster_operation(delete_operation) - logger.info("✅ Deleted default node pool") - except Exception as e: - logger.warning(f"Could not delete default node pool: {e}") - - logger.info(f"✅ Node pool ready: {node_pool_name}") - return {"node_pool_name": node_pool_name} - - def _configure_kubectl_access(self, cluster_info: Dict[str, Any]) -> Dict[str, Any]: - """Configure kubectl access to the cluster.""" - logger.info("⚙️ Configuring kubectl access...") - - # Generate kubeconfig using gcloud command - kubeconfig = { - "apiVersion": "v1", - "kind": "Config", - "clusters": [ - { - "cluster": { - "certificate-authority-data": cluster_info[ - "certificate_authority" - ], - "server": cluster_info["endpoint"], - }, - "name": f"gke_{self.project_id}_{cluster_info['location']}_{cluster_info['cluster_name']}", - } - ], - "contexts": [ - { - "context": { - "cluster": f"gke_{self.project_id}_{cluster_info['location']}_{cluster_info['cluster_name']}", - "user": f"gke_{self.project_id}_{cluster_info['location']}_{cluster_info['cluster_name']}", - }, - "name": f"gke_{self.project_id}_{cluster_info['location']}_{cluster_info['cluster_name']}", - } - ], - "current-context": f"gke_{self.project_id}_{cluster_info['location']}_{cluster_info['cluster_name']}", - "users": [ - { - "name": f"gke_{self.project_id}_{cluster_info['location']}_{cluster_info['cluster_name']}", - "user": { - "exec": { - "apiVersion": "client.authentication.k8s.io/v1beta1", - "command": "gcloud", - "args": ["config", "config-helper", "--format=json"], - } - }, - } - ], - } - - return kubeconfig - - def _setup_clustrix_environment( - self, cluster_info: Dict[str, Any], kubectl_config: Dict[str, Any] - ) -> None: - """Set up Clustrix namespace and RBAC.""" - logger.info("🔧 Setting up Clustrix environment...") - - try: - # Write kubeconfig to temporary file - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - import yaml - - yaml.dump(kubectl_config, f) - kubeconfig_path = f.name - - # Set environment variables for gcloud - env = { - "GOOGLE_APPLICATION_CREDENTIALS": self.credentials.get( - "service_account_key", "" - ), - "KUBECONFIG": kubeconfig_path, - } - - # Create namespace - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "namespace", - "clustrix", - ], - check=False, - capture_output=True, - env=env, - ) - - # Create service account - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "serviceaccount", - "clustrix-worker", - "--namespace", - "clustrix", - ], - check=False, - capture_output=True, - env=env, - ) - - # Create cluster role binding - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "create", - "clusterrolebinding", - "clustrix-worker-binding", - "--clusterrole", - "cluster-admin", - "--serviceaccount", - "clustrix:clustrix-worker", - ], - check=False, - capture_output=True, - env=env, - ) - - logger.info("✅ Clustrix environment configured") - - except Exception as e: - logger.warning(f"⚠️ Failed to configure Clustrix environment: {e}") - finally: - # Clean up temporary kubeconfig file - import os - - try: - os.unlink(kubeconfig_path) - except Exception: - pass - - def _verify_cluster_operational(self, cluster_name: str) -> None: - """Verify cluster is ready for job submission.""" - logger.info("🔍 Verifying cluster is operational...") - - try: - # Get cluster status - cluster = self.container_client.get_cluster( - name=f"projects/{self.project_id}/locations/{self.region}-a/clusters/{cluster_name}" - ) - - if cluster.status.name != "RUNNING": - raise RuntimeError(f"Cluster not running: {cluster.status}") - - # Check node pools - node_pools = list( - self.container_client.list_node_pools( - parent=f"projects/{self.project_id}/locations/{self.region}-a/clusters/{cluster_name}" - ) - ) - - for node_pool in node_pools: - if node_pool.status.name != "RUNNING": - raise RuntimeError(f"Node pool not running: {node_pool.status}") - - logger.info("✅ Cluster verification completed") - - except Exception as e: - logger.error(f"❌ Cluster verification failed: {e}") - raise - - def destroy_cluster_infrastructure(self, cluster_id: str) -> bool: - """Destroy cluster and all associated infrastructure.""" - logger.info(f"🧹 Destroying GKE cluster: {cluster_id}") - - try: - # Delete GKE cluster (this will also delete node pools) - try: - operation = self.container_client.delete_cluster( - name=f"projects/{self.project_id}/locations/{self.region}-a/clusters/{cluster_id}" - ) - self._wait_for_cluster_operation(operation) - logger.info(f"✅ Deleted GKE cluster: {cluster_id}") - except gcp_exceptions.NotFound: - logger.info(f"Cluster {cluster_id} not found") - except Exception as e: - logger.warning(f"Error deleting cluster: {e}") - - # Clean up all other resources - self._cleanup_all_resources() - - return True - - except Exception as e: - logger.error(f"❌ Failed to destroy cluster: {e}") - return False - - def get_cluster_status(self, cluster_id: str) -> Dict[str, Any]: - """Get detailed cluster status and health information.""" - try: - cluster = self.container_client.get_cluster( - name=f"projects/{self.project_id}/locations/{self.region}-a/clusters/{cluster_id}" - ) - - # Check node pools - node_pools = list( - self.container_client.list_node_pools( - parent=f"projects/{self.project_id}/locations/{self.region}-a/clusters/{cluster_id}" - ) - ) - - node_status = [] - for node_pool in node_pools: - node_status.append( - { - "name": node_pool.name, - "status": node_pool.status.name, - "node_count": node_pool.initial_node_count, - } - ) - - return { - "cluster_id": cluster_id, - "status": cluster.status.name, - "endpoint": f"https://{cluster.endpoint}", - "version": cluster.current_master_version, - "location": cluster.location, - "node_pools": node_status, - "ready_for_jobs": ( - cluster.status.name == "RUNNING" - and all(np["status"] == "RUNNING" for np in node_status) - ), - } - - except gcp_exceptions.NotFound: - return { - "cluster_id": cluster_id, - "status": "NOT_FOUND", - "ready_for_jobs": False, - } - except Exception as e: - logger.error(f"Error getting cluster status: {e}") - raise - - def _wait_for_operation(self, operation, operation_type: str) -> None: - """Wait for compute operation to complete.""" - logger.info(f"⏳ Waiting for {operation_type}...") - - max_attempts = 60 # 10 minutes max - for attempt in range(max_attempts): - try: - if hasattr(operation, "status") and operation.status == "DONE": - return - elif hasattr(operation, "done") and operation.done: - return - - time.sleep(10) - - except Exception as e: - if attempt == max_attempts - 1: - raise RuntimeError(f"{operation_type} timeout: {e}") - time.sleep(10) - - raise RuntimeError( - f"{operation_type} timeout after {max_attempts * 10} seconds" - ) - - def _wait_for_cluster_operation(self, operation) -> None: - """Wait for GKE cluster operation to complete.""" - logger.info("⏳ Waiting for GKE operation...") - - max_attempts = 120 # 20 minutes max for cluster operations - for attempt in range(max_attempts): - try: - operation_status = self.container_client.get_operation( - name=operation.name - ) - - if operation_status.status.name == "DONE": - if operation_status.error: - raise RuntimeError( - f"Operation failed: {operation_status.error}" - ) - return - elif operation_status.status.name in ["CANCELLED", "ABORTING"]: - raise RuntimeError(f"Operation failed: {operation_status.status}") - - time.sleep(10) - - except Exception as e: - if attempt == max_attempts - 1: - raise RuntimeError(f"GKE operation timeout: {e}") - time.sleep(10) - - raise RuntimeError(f"GKE operation timeout after {max_attempts * 10} seconds") - - def _cleanup_failed_provisioning(self, cluster_name: str) -> None: - """Clean up resources if provisioning fails.""" - logger.info("🧹 Cleaning up failed provisioning...") - try: - self._cleanup_all_resources() - except Exception as e: - logger.error(f"Error during cleanup: {e}") - - def _cleanup_all_resources(self) -> None: - """Clean up all tracked resources.""" - logger.info("🧹 Cleaning up all created resources...") - - # Delete firewall rules - for rule_name in self.created_resources.get("firewall_rules", []): - try: - operation = self.firewalls_client.delete( - project=self.project_id, firewall=rule_name - ) - self._wait_for_operation( - operation, f"firewall rule {rule_name} deletion" - ) - logger.info(f"✅ Deleted firewall rule: {rule_name}") - except Exception as e: - logger.warning(f"Failed to delete firewall rule {rule_name}: {e}") - - # Delete subnets - for subnet_path in self.created_resources.get("subnets", []): - try: - region, subnet_name = subnet_path.split("/", 1) - operation = self.subnetworks_client.delete( - project=self.project_id, region=region, subnetwork=subnet_name - ) - self._wait_for_operation(operation, f"subnet {subnet_name} deletion") - logger.info(f"✅ Deleted subnet: {subnet_name}") - except Exception as e: - logger.warning(f"Failed to delete subnet {subnet_path}: {e}") - - # Delete networks - for network_name in self.created_resources.get("networks", []): - try: - operation = self.networks_client.delete( - project=self.project_id, network=network_name - ) - self._wait_for_operation(operation, f"network {network_name} deletion") - logger.info(f"✅ Deleted network: {network_name}") - except Exception as e: - logger.warning(f"Failed to delete network {network_name}: {e}") - - # Delete service accounts - for sa_name in self.created_resources.get("service_accounts", []): - try: - self.iam_client.delete_service_account( - name=( - f"projects/{self.project_id}/serviceAccounts/" - f"{sa_name}@{self.project_id}.iam.gserviceaccount.com" - ) - ) - logger.info(f"✅ Deleted service account: {sa_name}") - except Exception as e: - logger.warning(f"Failed to delete service account {sa_name}: {e}") diff --git a/clustrix/kubernetes/huggingface_provisioner.py b/clustrix/kubernetes/huggingface_provisioner.py deleted file mode 100644 index 0409bf4d..00000000 --- a/clustrix/kubernetes/huggingface_provisioner.py +++ /dev/null @@ -1,500 +0,0 @@ -""" -HuggingFace Spaces Kubernetes adapter. - -Provides Kubernetes-style job execution on HuggingFace Spaces infrastructure. -This is not a traditional Kubernetes provisioner but an adapter that makes -HuggingFace Spaces work with the Clustrix Kubernetes interface. -""" - -import logging -import time -from typing import Dict, Any, List - -try: - from huggingface_hub import HfApi - from huggingface_hub.utils import HfHubHTTPError - - HF_AVAILABLE = True -except ImportError: - HF_AVAILABLE = False - HfApi = None - HfFolder = None - HfHubHTTPError = Exception - -from .cluster_provisioner import BaseKubernetesProvisioner, ClusterSpec - -logger = logging.getLogger(__name__) - - -class HuggingFaceKubernetesProvisioner(BaseKubernetesProvisioner): - """ - HuggingFace Spaces Kubernetes adapter. - - This adapter provides a Kubernetes-like interface for HuggingFace Spaces, - enabling Clustrix to submit jobs to HuggingFace's infrastructure using - familiar Kubernetes concepts translated to HF Spaces operations. - - Key adaptations: - - "Clusters" are represented as HuggingFace Spaces - - "Nodes" are represented as Space hardware configurations - - "Jobs" are executed as Space applications - - "kubectl" operations are translated to HuggingFace Hub API calls - """ - - def __init__(self, credentials: Dict[str, str], region: str): - super().__init__(credentials, region) - - if not HF_AVAILABLE: - raise ImportError( - "huggingface_hub required for HuggingFace integration. Install with: pip install huggingface_hub" - ) - - # Validate required credentials - self.token = credentials.get("token") or credentials.get("hf_token") - self.username = credentials.get("username") or credentials.get("hf_username") - - if not self.token: - raise ValueError("HuggingFace token required (token or hf_token)") - if not self.username: - raise ValueError("HuggingFace username required (username or hf_username)") - - # Initialize HuggingFace API - self.api = HfApi(token=self.token) - - # Track created spaces for cleanup - self.created_resources: Dict[str, List[str]] = {"spaces": [], "repos": []} - - def validate_credentials(self) -> bool: - """Validate HuggingFace credentials and permissions.""" - try: - # Test basic HF access - user_info = self.api.whoami() - logger.info( - f"✅ HuggingFace credentials validated for user: {user_info['name']}" - ) - - # Check if user can create spaces - try: - # Try to list user's spaces - self.api.list_spaces(author=self.username) - logger.debug("✅ HuggingFace Spaces access confirmed") - except Exception as e: - logger.warning(f"⚠️ Limited HuggingFace Spaces access: {e}") - - return True - - except Exception as e: - logger.error(f"❌ HuggingFace credential validation failed: {e}") - return False - - def provision_complete_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """ - Create HuggingFace Space infrastructure adapted as Kubernetes cluster. - - This creates a HuggingFace Space that can execute Clustrix jobs - using a Kubernetes-compatible interface. - """ - logger.info(f"🚀 Starting HuggingFace Space provisioning: {spec.cluster_name}") - - try: - # Step 1: Create HuggingFace Space - space_info = self._create_huggingface_space(spec) - - # Step 2: Set up Space for Kubernetes-style job execution - self._setup_space_for_k8s_jobs(space_info, spec) - - # Step 3: Create kubectl-compatible interface - kubectl_config = self._create_kubectl_interface(space_info) - - # Step 4: Verify space is ready for jobs - self._verify_space_operational(space_info["space_name"]) - - result = { - "cluster_id": space_info["space_name"], - "cluster_name": space_info["space_name"], - "provider": "huggingface", - "region": "global", # HF doesn't have regions - "endpoint": space_info["space_url"], - "space_id": space_info["space_id"], - "hardware": space_info["hardware"], - "sdk": space_info["sdk"], - "kubectl_config": kubectl_config, - "ready_for_jobs": True, - "created_resources": self.created_resources.copy(), - } - - logger.info( - f"✅ HuggingFace Space provisioning completed: {spec.cluster_name}" - ) - return result - - except Exception as e: - logger.error(f"❌ HuggingFace Space provisioning failed: {e}") - # Attempt cleanup of any created resources - self._cleanup_failed_provisioning(spec.cluster_name) - raise - - def _create_huggingface_space(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create HuggingFace Space to serve as cluster.""" - logger.info("🏗️ Creating HuggingFace Space...") - - space_name = f"{self.username}/clustrix-{spec.cluster_name}" - - # Determine hardware based on node requirements - hardware = self._map_node_requirements_to_hardware(spec) - - # Create Space - space_info = self.api.create_repo( - repo_id=space_name, - repo_type="space", - space_sdk="docker", # Use Docker for maximum flexibility - space_hardware=hardware, - private=True, # Keep spaces private by default - ) - - self.created_resources["spaces"].append(space_name) - - # Add initial files to the space - self._upload_space_files(space_name, spec) - - logger.info(f"✅ Created HuggingFace Space: {space_name}") - return { - "space_name": space_name, - "space_id": space_info.repo_id, - "space_url": f"https://huggingface.co/spaces/{space_name}", - "hardware": hardware, - "sdk": "docker", - } - - def _map_node_requirements_to_hardware(self, spec: ClusterSpec) -> str: - """Map Kubernetes node requirements to HuggingFace hardware.""" - # Default hardware mapping - hardware_mapping = { - 1: "cpu-basic", # Single node -> basic CPU - 2: "cpu-upgrade", # 2 nodes -> upgraded CPU - 4: "t4-small", # 4+ nodes -> GPU hardware - 8: "t4-medium", # 8+ nodes -> larger GPU - } - - # Find appropriate hardware tier - for node_threshold in sorted(hardware_mapping.keys(), reverse=True): - if spec.node_count >= node_threshold: - return hardware_mapping[node_threshold] - - return "cpu-basic" # Default - - def _upload_space_files(self, space_name: str, spec: ClusterSpec) -> None: - """Upload necessary files to make the space Kubernetes-compatible.""" - logger.info("📤 Uploading space configuration files...") - - # Create Dockerfile for the space - dockerfile_content = f"""FROM python:3.11-slim - -# Install kubectl and other Kubernetes tools -RUN apt-get update && apt-get install -y curl && \\ - curl -LO ""\ -"https://dl.k8s.io/release/"\ -"$(curl -L -s https://dl.k8s.io/release/stable.txt)"\ -"/bin/linux/amd64/kubectl" && \\ - install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl && \\ - apt-get clean && rm -rf /var/lib/apt/lists/* - -# Install Clustrix and dependencies -COPY requirements.txt . -RUN pip install -r requirements.txt - -# Copy application code -COPY . /app -WORKDIR /app - -# Set up environment -ENV PYTHONPATH=/app -ENV CLUSTRIX_SPACE_NAME={space_name} - -# Start the job execution server -CMD ["python", "clustrix_job_server.py"] -""" - - # Create requirements.txt - requirements_content = """ -clustrix -flask -requests -huggingface_hub -""" - - # Create job execution server - job_server_content = """ -import os -import json -import time -import logging -from flask import Flask, request, jsonify -import subprocess -import tempfile -import threading -from pathlib import Path - -app = Flask(__name__) -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - -# Job storage -jobs = {} - -@app.route('/api/v1/namespaces//jobs', methods=['POST']) -def create_job(namespace): - \"\"\"Create a Kubernetes-style job.\"\"\" - job_spec = request.json - job_id = f"job-{int(time.time())}" - - logger.info(f"Creating job {job_id} in namespace {namespace}") - - # Extract job details - containers = job_spec.get('spec', {}).get('template', {}).get('spec', {}).get('containers', []) - if not containers: - return jsonify({'error': 'No containers specified'}), 400 - - container = containers[0] - image = container.get('image', 'python:3.11-slim') - command = container.get('command', ['python', '-c', 'print("Hello from HF Space!")']) - - # Store job - jobs[job_id] = { - 'metadata': {'name': job_id, 'namespace': namespace}, - 'spec': job_spec['spec'], - 'status': {'phase': 'Running'}, - 'result': None - } - - # Execute job in background - thread = threading.Thread(target=execute_job, args=(job_id, command)) - thread.start() - - return jsonify({'metadata': {'name': job_id}}) - -@app.route('/api/v1/namespaces//jobs/', methods=['GET']) -def get_job(namespace, job_name): - \"\"\"Get job status.\"\"\" - if job_name not in jobs: - return jsonify({'error': 'Job not found'}), 404 - - return jsonify(jobs[job_name]) - -def execute_job(job_id, command): - \"\"\"Execute job and store result.\"\"\" - try: - logger.info(f"Executing job {job_id}: {command}") - result = subprocess.run( - command, - capture_output=True, - text=True, - timeout=300 - ) - - jobs[job_id]['status'] = {'phase': 'Succeeded' if result.returncode == 0 else 'Failed'} - jobs[job_id]['result'] = { - 'stdout': result.stdout, - 'stderr': result.stderr, - 'returncode': result.returncode - } - - except Exception as e: - jobs[job_id]['status'] = {'phase': 'Failed'} - jobs[job_id]['result'] = {'error': str(e)} - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=7860) -""" - - # Upload files to the space - files_to_upload = { - "Dockerfile": dockerfile_content, - "requirements.txt": requirements_content, - "clustrix_job_server.py": job_server_content, - } - - for filename, content in files_to_upload.items(): - self.api.upload_file( - path_or_fileobj=content.encode(), - path_in_repo=filename, - repo_id=space_name, - repo_type="space", - ) - - logger.info("✅ Space files uploaded successfully") - - def _setup_space_for_k8s_jobs( - self, space_info: Dict[str, Any], spec: ClusterSpec - ) -> None: - """Configure space for Kubernetes-style job execution.""" - logger.info("🔧 Setting up Space for K8s jobs...") - - # Wait for space to be built and running - self._wait_for_space_ready(space_info["space_name"]) - - logger.info("✅ Space ready for job execution") - - def _wait_for_space_ready(self, space_name: str) -> None: - """Wait for HuggingFace Space to be ready.""" - logger.info("⏳ Waiting for Space to be ready...") - - max_attempts = 60 # 10 minutes - for attempt in range(max_attempts): - try: - space_info = self.api.space_info(space_name) - if space_info.runtime and space_info.runtime.stage == "RUNNING": - logger.info("✅ Space is running and ready") - return - elif space_info.runtime and space_info.runtime.stage in [ - "STOPPED", - "FAILED", - ]: - raise RuntimeError( - f"Space failed to start: {space_info.runtime.stage}" - ) - - logger.info(f"⏳ Space not ready yet... ({attempt + 1}/{max_attempts})") - time.sleep(10) - - except Exception as e: - if attempt == max_attempts - 1: - raise RuntimeError( - f"Space not ready after {max_attempts * 10} seconds: {e}" - ) - time.sleep(10) - - raise RuntimeError(f"Space not ready after {max_attempts * 10} seconds") - - def _create_kubectl_interface(self, space_info: Dict[str, Any]) -> Dict[str, Any]: - """Create kubectl-compatible configuration for HF Space.""" - logger.info("⚙️ Creating kubectl interface...") - - # Create a kubeconfig that points to the HF Space API - kubeconfig = { - "apiVersion": "v1", - "kind": "Config", - "clusters": [ - { - "cluster": {"server": f"{space_info['space_url']}/api/v1"}, - "name": f"hf-space-{space_info['space_name']}", - } - ], - "contexts": [ - { - "context": { - "cluster": f"hf-space-{space_info['space_name']}", - "user": f"hf-user-{self.username}", - }, - "name": f"hf-space-{space_info['space_name']}", - } - ], - "current-context": f"hf-space-{space_info['space_name']}", - "users": [ - {"name": f"hf-user-{self.username}", "user": {"token": self.token}} - ], - } - - return kubeconfig - - def _verify_space_operational(self, space_name: str) -> None: - """Verify space is ready for job submission.""" - logger.info("🔍 Verifying space is operational...") - - try: - space_info = self.api.space_info(space_name) - if not space_info.runtime or space_info.runtime.stage != "RUNNING": - raise RuntimeError( - f"Space not running: {space_info.runtime.stage if space_info.runtime else 'Unknown'}" - ) - - logger.info("✅ Space verification completed") - - except Exception as e: - logger.error(f"❌ Space verification failed: {e}") - raise - - def destroy_cluster_infrastructure(self, cluster_id: str) -> bool: - """Destroy HuggingFace Space infrastructure.""" - logger.info(f"🧹 Destroying HuggingFace Space: {cluster_id}") - - try: - # Delete the space - if cluster_id is not None and cluster_id.startswith(self.username): # type: ignore - space_name = cluster_id - else: - space_name = f"{self.username}/clustrix-{cluster_id}" - - try: - self.api.delete_repo(repo_id=space_name, repo_type="space") - logger.info(f"✅ Deleted HuggingFace Space: {space_name}") - except HfHubHTTPError as e: - if e.response.status_code == 404: - logger.info(f"Space {space_name} not found (already deleted)") - else: - raise - - return True - - except Exception as e: - logger.error(f"❌ Failed to destroy space: {e}") - return False - - def get_cluster_status(self, cluster_id: str) -> Dict[str, Any]: - """Get HuggingFace Space status.""" - try: - if cluster_id is not None and cluster_id.startswith(self.username): # type: ignore - space_name = cluster_id - else: - space_name = f"{self.username}/clustrix-{cluster_id}" - space_info = self.api.space_info(space_name) - - status = "UNKNOWN" - ready_for_jobs = False - - if space_info.runtime: - status = space_info.runtime.stage - ready_for_jobs = status == "RUNNING" - - return { - "cluster_id": cluster_id, - "status": status, - "endpoint": f"https://huggingface.co/spaces/{space_name}", - "hardware": ( - space_info.cardData.get("hardware") - if space_info.cardData - else "unknown" - ), - "sdk": space_info.sdk, - "ready_for_jobs": ready_for_jobs, - } - - except HfHubHTTPError as e: - if e.response.status_code == 404: - return { - "cluster_id": cluster_id, - "status": "NOT_FOUND", - "ready_for_jobs": False, - } - else: - raise - - def _cleanup_failed_provisioning(self, cluster_name: str) -> None: - """Clean up resources if provisioning fails.""" - logger.info("🧹 Cleaning up failed provisioning...") - try: - self._cleanup_all_resources() - except Exception as e: - logger.error(f"Error during cleanup: {e}") - - def _cleanup_all_resources(self) -> None: - """Clean up all tracked resources.""" - logger.info("🧹 Cleaning up all created resources...") - - # Delete all created spaces - for space_name in self.created_resources.get("spaces", []): - try: - self.api.delete_repo(repo_id=space_name, repo_type="space") - logger.info(f"✅ Deleted space: {space_name}") - except Exception as e: - logger.warning(f"Failed to delete space {space_name}: {e}") diff --git a/clustrix/kubernetes/lambda_provisioner.py b/clustrix/kubernetes/lambda_provisioner.py deleted file mode 100644 index d91d280b..00000000 --- a/clustrix/kubernetes/lambda_provisioner.py +++ /dev/null @@ -1,686 +0,0 @@ -""" -Lambda Cloud Kubernetes adapter. - -Provides Kubernetes-style job execution on Lambda Cloud infrastructure. -This is not a traditional Kubernetes provisioner but an adapter that makes -Lambda Cloud instances work with the Clustrix Kubernetes interface. -""" - -import logging -import time -from typing import Dict, Any, List -import requests - -try: - import paramiko - - PARAMIKO_AVAILABLE = True -except ImportError: - PARAMIKO_AVAILABLE = False - paramiko = None # type: ignore - -from .cluster_provisioner import BaseKubernetesProvisioner, ClusterSpec - -logger = logging.getLogger(__name__) - - -class LambdaCloudKubernetesProvisioner(BaseKubernetesProvisioner): - """ - Lambda Cloud Kubernetes adapter. - - This adapter provides a Kubernetes-like interface for Lambda Cloud instances, - enabling Clustrix to submit jobs to Lambda Cloud's GPU infrastructure using - familiar Kubernetes concepts translated to Lambda Cloud operations. - - Key adaptations: - - "Clusters" are represented as groups of Lambda Cloud instances - - "Nodes" are individual Lambda Cloud instances - - "Jobs" are executed via SSH on instances - - "kubectl" operations are translated to Lambda Cloud API calls - """ - - def __init__(self, credentials: Dict[str, str], region: str): - super().__init__(credentials, region) - - if not PARAMIKO_AVAILABLE: - raise ImportError( - "paramiko required for Lambda Cloud SSH access. Install with: pip install paramiko" - ) - - # Validate required credentials - self.api_key = credentials.get("api_key") or credentials.get("lambda_api_key") - - if not self.api_key: - raise ValueError( - "Lambda Cloud API key required (api_key or lambda_api_key)" - ) - - # Lambda Cloud API configuration - self.base_url = "https://cloud.lambdalabs.com/api/v1" - self.headers = { - "Authorization": f"Bearer {self.api_key}", - "Content-Type": "application/json", - } - - # Track created instances for cleanup - self.created_resources: Dict[str, List[str]] = {"instances": [], "ssh_keys": []} - - # SSH configuration for instances - self.ssh_connections: Dict[str, paramiko.SSHClient] = {} - - def validate_credentials(self) -> bool: - """Validate Lambda Cloud credentials and permissions.""" - try: - # Test basic Lambda Cloud access - response = requests.get( - f"{self.base_url}/instance-types", headers=self.headers, timeout=30 - ) - response.raise_for_status() - - instance_types = response.json() - logger.info( - f"✅ Lambda Cloud credentials validated, {len(instance_types)} instance types available" - ) - - # Check account info - response = requests.get( - f"{self.base_url}/instances", headers=self.headers, timeout=30 - ) - response.raise_for_status() - logger.debug("✅ Lambda Cloud instances API access confirmed") - - return True - - except Exception as e: - logger.error(f"❌ Lambda Cloud credential validation failed: {e}") - return False - - def provision_complete_infrastructure(self, spec: ClusterSpec) -> Dict[str, Any]: - """ - Create Lambda Cloud instance infrastructure adapted as Kubernetes cluster. - - This creates Lambda Cloud instances that can execute Clustrix jobs - using a Kubernetes-compatible interface. - """ - logger.info( - f"🚀 Starting Lambda Cloud cluster provisioning: {spec.cluster_name}" - ) - - try: - # Step 1: Create SSH key for instances - ssh_key_info = self._create_ssh_key(spec) - - # Step 2: Launch Lambda Cloud instances - instances_info = self._launch_instances(spec, ssh_key_info) - - # Step 3: Set up instances for Kubernetes-style job execution - self._setup_instances_for_k8s_jobs(instances_info, spec) - - # Step 4: Create kubectl-compatible interface - kubectl_config = self._create_kubectl_interface(instances_info) - - # Step 5: Verify instances are ready for jobs - self._verify_cluster_operational(instances_info) - - result = { - "cluster_id": spec.cluster_name, - "cluster_name": spec.cluster_name, - "provider": "lambda", - "region": spec.region, - "endpoint": f"lambda-cluster-{spec.cluster_name}", - "instances": instances_info["instances"], - "instance_type": instances_info["instance_type"], - "kubectl_config": kubectl_config, - "ready_for_jobs": True, - "created_resources": self.created_resources.copy(), - } - - logger.info( - f"✅ Lambda Cloud cluster provisioning completed: {spec.cluster_name}" - ) - return result - - except Exception as e: - logger.error(f"❌ Lambda Cloud cluster provisioning failed: {e}") - # Attempt cleanup of any created resources - self._cleanup_failed_provisioning(spec.cluster_name) - raise - - def _create_ssh_key(self, spec: ClusterSpec) -> Dict[str, Any]: - """Create SSH key for Lambda Cloud instances.""" - logger.info("🔑 Creating SSH key...") - - # Generate SSH key pair - key = paramiko.RSAKey.generate(2048) - - # Create temporary files for keys - import tempfile - - private_key_file = tempfile.NamedTemporaryFile( - mode="w", suffix=".pem", delete=False - ) - public_key_file = tempfile.NamedTemporaryFile( - mode="w", suffix=".pub", delete=False - ) - - # Write private key - key.write_private_key(private_key_file) - private_key_file.close() - - # Write public key - public_key = f"{key.get_name()} {key.get_base64()}" - public_key_file.write(public_key) - public_key_file.close() - - # Add SSH key to Lambda Cloud - ssh_key_name = f"clustrix-{spec.cluster_name}-{int(time.time())}" - - ssh_key_data = {"name": ssh_key_name, "public_key": public_key} - - response = requests.post( - f"{self.base_url}/ssh-keys", - headers=self.headers, - json=ssh_key_data, - timeout=30, - ) - response.raise_for_status() - - ssh_key_result = response.json() - self.created_resources["ssh_keys"].append(ssh_key_name) - - logger.info(f"✅ Created SSH key: {ssh_key_name}") - return { - "name": ssh_key_name, - "id": ssh_key_result.get("id"), - "private_key_file": private_key_file.name, - "public_key": public_key, - } - - def _launch_instances( - self, spec: ClusterSpec, ssh_key_info: Dict[str, Any] - ) -> Dict[str, Any]: - """Launch Lambda Cloud instances.""" - logger.info("🚀 Launching Lambda Cloud instances...") - - # Map node requirements to Lambda Cloud instance type - instance_type = self._map_node_requirements_to_instance_type(spec) - - instances = [] - - for i in range(spec.node_count): - instance_name = f"clustrix-{spec.cluster_name}-{i}" - - instance_data = { - "region_name": spec.region, - "instance_type_name": instance_type, - "ssh_key_names": [ssh_key_info["name"]], - "file_system_names": [], # No persistent storage needed - "quantity": 1, - "name": instance_name, - } - - logger.info( - f"Launching instance {i + 1}/{spec.node_count}: {instance_name}" - ) - - response = requests.post( - f"{self.base_url}/instance-operations/launch", - headers=self.headers, - json=instance_data, - timeout=60, - ) - response.raise_for_status() - - launch_result = response.json() - instance_ids = launch_result.get("instance_ids", []) - - if instance_ids: - instance_id = instance_ids[0] - instances.append( - { - "id": instance_id, - "name": instance_name, - "type": instance_type, - "region": spec.region, - } - ) - self.created_resources["instances"].append(instance_id) - else: - raise RuntimeError(f"Failed to launch instance: {launch_result}") - - # Wait for instances to be running - self._wait_for_instances_ready(instances) - - # Get instance details including IP addresses - instances_with_ips = self._get_instance_details(instances) - - logger.info(f"✅ Launched {len(instances)} Lambda Cloud instances") - return { - "instances": instances_with_ips, - "instance_type": instance_type, - "ssh_key": ssh_key_info, - } - - def _map_node_requirements_to_instance_type(self, spec: ClusterSpec) -> str: - """Map Kubernetes node requirements to Lambda Cloud instance type.""" - # Get available instance types - response = requests.get( - f"{self.base_url}/instance-types", headers=self.headers, timeout=30 - ) - response.raise_for_status() - - instance_types = response.json().get("data", {}) - - # Prefer GPU instances for Lambda Cloud - gpu_preferences = [ - "gpu_1x_a100", - "gpu_1x_v100", - "gpu_1x_rtx6000", - "gpu_8x_a100", - "gpu_8x_v100", - ] - - # Find the first available GPU instance type - for instance_type in gpu_preferences: - if instance_type in instance_types: - return instance_type - - # Fallback to any available instance type - available_types = list(instance_types.keys()) - if available_types: - return available_types[0] - - raise RuntimeError("No available Lambda Cloud instance types found") - - def _wait_for_instances_ready(self, instances: List[Dict[str, Any]]) -> None: - """Wait for Lambda Cloud instances to be ready.""" - logger.info("⏳ Waiting for instances to be ready...") - - max_attempts = 60 # 10 minutes - for attempt in range(max_attempts): - all_ready = True - - for instance in instances: - status = self._get_instance_status(instance["id"]) - if status != "active": - all_ready = False - break - - if all_ready: - logger.info("✅ All instances are ready") - return - - logger.info(f"⏳ Waiting for instances... ({attempt + 1}/{max_attempts})") - time.sleep(10) - - raise RuntimeError(f"Instances not ready after {max_attempts * 10} seconds") - - def _get_instance_status(self, instance_id: str) -> str: - """Get Lambda Cloud instance status.""" - response = requests.get( - f"{self.base_url}/instances", headers=self.headers, timeout=30 - ) - response.raise_for_status() - - instances_data = response.json().get("data", []) - for instance in instances_data: - if instance["id"] == instance_id: - return instance["status"] - - return "unknown" - - def _get_instance_details( - self, instances: List[Dict[str, Any]] - ) -> List[Dict[str, Any]]: - """Get detailed instance information including IP addresses.""" - response = requests.get( - f"{self.base_url}/instances", headers=self.headers, timeout=30 - ) - response.raise_for_status() - - instances_data = response.json().get("data", []) - detailed_instances = [] - - for instance in instances: - for instance_data in instances_data: - if instance_data["id"] == instance["id"]: - detailed_instances.append( - { - **instance, - "ip": instance_data.get("ip"), - "status": instance_data.get("status"), - "hostname": instance_data.get("hostname"), - } - ) - break - - return detailed_instances - - def _setup_instances_for_k8s_jobs( - self, instances_info: Dict[str, Any], spec: ClusterSpec - ) -> None: - """Configure instances for Kubernetes-style job execution.""" - logger.info("🔧 Setting up instances for K8s jobs...") - - instances = instances_info["instances"] - ssh_key_info = instances_info["ssh_key"] - - for instance in instances: - try: - # Connect via SSH - ssh_client = self._connect_ssh( - instance, ssh_key_info["private_key_file"] - ) - - # Install necessary software - self._install_k8s_tools(ssh_client, instance) - - # Start job execution server - self._start_job_server(ssh_client, instance) - - self.ssh_connections[instance["id"]] = ssh_client - - except Exception as e: - logger.warning(f"Failed to set up instance {instance['id']}: {e}") - - logger.info("✅ Instances ready for job execution") - - def _connect_ssh( - self, instance: Dict[str, Any], private_key_file: str - ) -> paramiko.SSHClient: - """Connect to instance via SSH.""" - from clustrix.ssh_security import configure_host_key_policy - from clustrix.ssh_utils import add_host_key - - ssh_client = paramiko.SSHClient() - configure_host_key_policy(ssh_client, None) - - # Load private key - private_key = paramiko.RSAKey.from_private_key_file(private_key_file) - - # Connect with retries - max_attempts = 30 - for attempt in range(max_attempts): - try: - # This instance was provisioned seconds ago, so there is no - # pre-existing known_hosts entry for it -- ssh-keyscan - # fetches and records its key the moment it starts - # answering on port 22, an explicit, logged - # trust-on-first-use step (not a blanket "accept anything" - # policy). The strict policy set above then verifies the - # handshake against that recorded key. - add_host_key(instance["ip"]) - ssh_client.connect( - hostname=instance["ip"], - username="ubuntu", # Default Lambda Cloud user - pkey=private_key, - timeout=30, - ) - return ssh_client - except Exception: - if attempt == max_attempts - 1: - raise - time.sleep(10) - - raise RuntimeError(f"Could not connect to instance {instance['id']}") - - def _install_k8s_tools( - self, ssh_client: paramiko.SSHClient, instance: Dict[str, Any] - ) -> None: - """Install necessary tools on the instance.""" - logger.info(f"Installing tools on instance {instance['id']}...") - - commands = [ - "sudo apt-get update", - "sudo apt-get install -y python3-pip docker.io", - "pip3 install flask requests", - "sudo systemctl start docker", - "sudo systemctl enable docker", - ] - - for command in commands: - stdin, stdout, stderr = ssh_client.exec_command(command) - exit_status = stdout.channel.recv_exit_status() - if exit_status != 0: - logger.warning(f"Command failed on {instance['id']}: {command}") - - def _start_job_server( - self, ssh_client: paramiko.SSHClient, instance: Dict[str, Any] - ) -> None: - """Start job execution server on the instance.""" - logger.info(f"Starting job server on instance {instance['id']}...") - - # Create job server script - job_server_script = """ -import os -import json -import time -import logging -from flask import Flask, request, jsonify -import subprocess -import threading - -app = Flask(__name__) -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - -jobs = {} - -@app.route('/api/v1/namespaces//jobs', methods=['POST']) -def create_job(namespace): - job_spec = request.json - job_id = f"job-{int(time.time())}" - - containers = job_spec.get('spec', {}).get('template', {}).get('spec', {}).get('containers', []) - if containers: - container = containers[0] - command = container.get('command', ['echo', 'Hello from Lambda Cloud!']) - - jobs[job_id] = { - 'metadata': {'name': job_id, 'namespace': namespace}, - 'status': {'phase': 'Running'} - } - - thread = threading.Thread(target=execute_job, args=(job_id, command)) - thread.start() - - return jsonify({'metadata': {'name': job_id}}) - - return jsonify({'error': 'No containers specified'}), 400 - -@app.route('/api/v1/namespaces//jobs/', methods=['GET']) -def get_job(namespace, job_name): - return jsonify(jobs.get(job_name, {'error': 'Job not found'})) - -def execute_job(job_id, command): - try: - result = subprocess.run(command, capture_output=True, text=True, timeout=300) - jobs[job_id]['status'] = {'phase': 'Succeeded' if result.returncode == 0 else 'Failed'} - jobs[job_id]['result'] = { - 'stdout': result.stdout, - 'stderr': result.stderr, - 'returncode': result.returncode - } - except Exception as e: - jobs[job_id]['status'] = {'phase': 'Failed'} - jobs[job_id]['result'] = {'error': str(e)} - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=8080) -""" - - # Upload and start the server - sftp = ssh_client.open_sftp() - with sftp.file("/tmp/job_server.py", "w") as f: - f.write(job_server_script) - sftp.close() - - # Start server in background - ssh_client.exec_command( - "nohup python3 /tmp/job_server.py > /tmp/job_server.log 2>&1 &" - ) - - def _create_kubectl_interface( - self, instances_info: Dict[str, Any] - ) -> Dict[str, Any]: - """Create kubectl-compatible configuration for Lambda Cloud cluster.""" - logger.info("⚙️ Creating kubectl interface...") - - # Use first instance as primary endpoint - primary_instance = instances_info["instances"][0] - - kubeconfig = { - "apiVersion": "v1", - "kind": "Config", - "clusters": [ - { - "cluster": {"server": f"http://{primary_instance['ip']}:8080"}, - "name": "lambda-cluster", - } - ], - "contexts": [ - { - "context": {"cluster": "lambda-cluster", "user": "lambda-user"}, - "name": "lambda-cluster", - } - ], - "current-context": "lambda-cluster", - "users": [{"name": "lambda-user", "user": {"token": self.api_key}}], - } - - return kubeconfig - - def _verify_cluster_operational(self, instances_info: Dict[str, Any]) -> None: - """Verify cluster is ready for job submission.""" - logger.info("🔍 Verifying cluster is operational...") - - for instance in instances_info["instances"]: - if instance["status"] != "active": - raise RuntimeError( - f"Instance {instance['id']} not active: {instance['status']}" - ) - - logger.info("✅ Cluster verification completed") - - def destroy_cluster_infrastructure(self, cluster_id: str) -> bool: - """Destroy Lambda Cloud cluster infrastructure.""" - logger.info(f"🧹 Destroying Lambda Cloud cluster: {cluster_id}") - - try: - success = True - - # Terminate all instances - for instance_id in self.created_resources.get("instances", []): - try: - response = requests.post( - f"{self.base_url}/instance-operations/terminate", - headers=self.headers, - json={"instance_ids": [instance_id]}, - timeout=60, - ) - response.raise_for_status() - logger.info(f"✅ Terminated instance: {instance_id}") - except Exception as e: - logger.warning(f"Failed to terminate instance {instance_id}: {e}") - success = False - - # Delete SSH keys - for ssh_key_name in self.created_resources.get("ssh_keys", []): - try: - response = requests.delete( - f"{self.base_url}/ssh-keys/{ssh_key_name}", - headers=self.headers, - timeout=30, - ) - response.raise_for_status() - logger.info(f"✅ Deleted SSH key: {ssh_key_name}") - except Exception as e: - logger.warning(f"Failed to delete SSH key {ssh_key_name}: {e}") - success = False - - # Close SSH connections - for ssh_client in self.ssh_connections.values(): - try: - ssh_client.close() - except Exception: - pass - - return success - - except Exception as e: - logger.error(f"❌ Failed to destroy cluster: {e}") - return False - - def get_cluster_status(self, cluster_id: str) -> Dict[str, Any]: - """Get Lambda Cloud cluster status.""" - try: - instance_count = len(self.created_resources.get("instances", [])) - - if instance_count == 0: - return { - "cluster_id": cluster_id, - "status": "NOT_FOUND", - "ready_for_jobs": False, - } - - # Check instance statuses - all_active = True - for instance_id in self.created_resources.get("instances", []): - status = self._get_instance_status(instance_id) - if status != "active": - all_active = False - break - - return { - "cluster_id": cluster_id, - "status": "ACTIVE" if all_active else "PROVISIONING", - "instance_count": instance_count, - "ready_for_jobs": all_active, - } - - except Exception as e: - logger.error(f"Error getting cluster status: {e}") - return { - "cluster_id": cluster_id, - "status": "ERROR", - "ready_for_jobs": False, - } - - def _cleanup_failed_provisioning(self, cluster_name: str) -> None: - """Clean up resources if provisioning fails.""" - logger.info("🧹 Cleaning up failed provisioning...") - try: - self._cleanup_all_resources() - except Exception as e: - logger.error(f"Error during cleanup: {e}") - - def _cleanup_all_resources(self) -> None: - """Clean up all tracked resources.""" - logger.info("🧹 Cleaning up all created resources...") - - # Terminate instances - for instance_id in self.created_resources.get("instances", []): - try: - response = requests.post( - f"{self.base_url}/instance-operations/terminate", - headers=self.headers, - json={"instance_ids": [instance_id]}, - timeout=60, - ) - response.raise_for_status() - logger.info(f"✅ Terminated instance: {instance_id}") - except Exception as e: - logger.warning(f"Failed to terminate instance {instance_id}: {e}") - - # Delete SSH keys - for ssh_key_name in self.created_resources.get("ssh_keys", []): - try: - response = requests.delete( - f"{self.base_url}/ssh-keys/{ssh_key_name}", - headers=self.headers, - timeout=30, - ) - response.raise_for_status() - logger.info(f"✅ Deleted SSH key: {ssh_key_name}") - except Exception as e: - logger.warning(f"Failed to delete SSH key {ssh_key_name}: {e}") diff --git a/clustrix/kubernetes/local_provisioner.py b/clustrix/kubernetes/local_provisioner.py deleted file mode 100644 index 0efdc21b..00000000 --- a/clustrix/kubernetes/local_provisioner.py +++ /dev/null @@ -1,455 +0,0 @@ -""" -Local Docker-based Kubernetes provisioner using kind (Kubernetes in Docker). - -This provisioner creates local Kubernetes clusters using Docker containers, -allowing for complete integration testing without cloud infrastructure costs. -Perfect for development, testing, and CI/CD environments. -""" - -import logging -import subprocess -import time -import yaml -import tempfile -import os -from typing import Dict, Any - -from .cluster_provisioner import BaseKubernetesProvisioner, ClusterSpec - -logger = logging.getLogger(__name__) - - -class LocalDockerKubernetesProvisioner(BaseKubernetesProvisioner): - """Local Docker-based Kubernetes provisioner using kind.""" - - def __init__(self, credentials: Dict[str, str], region: str = "local"): - """Initialize local provisioner. - - Args: - credentials: Not used for local provisioner, but kept for interface compatibility - region: Not used for local provisioner, but kept for interface compatibility - """ - super().__init__(credentials, region) - self.docker_available = self._check_docker_available() - self.kind_available = self._check_kind_available() - self.kubectl_available = self._check_kubectl_available() - - if not self.docker_available: - raise RuntimeError("Docker is required for local Kubernetes provisioning") - if not self.kind_available: - raise RuntimeError( - "kind (Kubernetes in Docker) is required for local provisioning" - ) - if not self.kubectl_available: - raise RuntimeError("kubectl is required for local Kubernetes provisioning") - - def _check_docker_available(self) -> bool: - """Check if Docker is available and running.""" - try: - result = subprocess.run( - ["docker", "version"], capture_output=True, text=True, timeout=10 - ) - return result.returncode == 0 - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - return False - - def _check_kind_available(self) -> bool: - """Check if kind is available.""" - try: - result = subprocess.run( - ["kind", "version"], capture_output=True, text=True, timeout=10 - ) - return result.returncode == 0 - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - return False - - def _check_kubectl_available(self) -> bool: - """Check if kubectl is available.""" - try: - result = subprocess.run( - ["kubectl", "version", "--client"], - capture_output=True, - text=True, - timeout=10, - ) - return result.returncode == 0 - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - return False - - def validate_credentials(self) -> bool: - """Validate that required tools are available.""" - return self.docker_available and self.kind_available and self.kubectl_available - - def provision_complete_infrastructure( - self, cluster_spec: ClusterSpec - ) -> Dict[str, Any]: - """Provision a complete local Kubernetes cluster using kind. - - Args: - cluster_spec: Cluster specification - - Returns: - Dictionary containing cluster information - """ - logger.info( - f"🐳 Provisioning local Kubernetes cluster: {cluster_spec.cluster_name}" - ) - - start_time = time.time() - - try: - # 1. Create kind cluster configuration - kind_config = self._create_kind_config(cluster_spec) - - # 2. Write config to temporary file - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(kind_config, f, default_flow_style=False) - kind_config_path = f.name - - try: - # 3. Create the cluster - logger.info( - f"🚀 Creating kind cluster with {cluster_spec.node_count} nodes..." - ) - - cmd = [ - "kind", - "create", - "cluster", - "--name", - cluster_spec.cluster_name, - "--config", - kind_config_path, - "--wait", - "300s", # Wait up to 5 minutes for cluster to be ready - ] - - result = subprocess.run( - cmd, - capture_output=True, - text=True, - timeout=600, # 10 minute timeout - ) - - if result.returncode != 0: - raise RuntimeError( - f"Failed to create kind cluster: {result.stderr}" - ) - - logger.info("✅ Kind cluster created successfully") - - # 4. Get kubeconfig - kubeconfig = self._get_kubeconfig(cluster_spec.cluster_name) - - # 5. Wait for cluster to be fully ready - if not self._wait_for_cluster_ready(cluster_spec.cluster_name): - raise RuntimeError("Cluster failed to become ready") - - # 6. Get cluster info - cluster_info = self._get_cluster_info(cluster_spec, kubeconfig) - - provision_time = time.time() - start_time - logger.info( - f"✅ Local cluster provisioned successfully in {provision_time:.1f}s" - ) - - return cluster_info - - finally: - # Clean up temporary config file - os.unlink(kind_config_path) - - except Exception as e: - logger.error(f"❌ Failed to provision local cluster: {e}") - # Try to clean up on failure - try: - self.destroy_cluster_infrastructure(cluster_spec.cluster_name) - except Exception: - pass # Ignore cleanup errors - raise - - def _create_kind_config(self, cluster_spec: ClusterSpec) -> Dict[str, Any]: - """Create kind cluster configuration.""" - config: Dict[str, Any] = { - "kind": "Cluster", - "apiVersion": "kind.x-k8s.io/v1alpha4", - "nodes": [], - } - - # Add control plane node - nodes = [{"role": "control-plane"}] - - # Add worker nodes - worker_count = max(0, cluster_spec.node_count - 1) # Subtract control plane - for i in range(worker_count): - nodes.append({"role": "worker"}) - - config["nodes"] = nodes - - return config - - def _get_kubeconfig(self, cluster_name: str) -> Dict[str, Any]: - """Get kubeconfig for the cluster.""" - logger.info("🔧 Retrieving kubeconfig...") - - try: - result = subprocess.run( - ["kind", "get", "kubeconfig", "--name", cluster_name], - capture_output=True, - text=True, - timeout=30, - ) - - if result.returncode != 0: - raise RuntimeError(f"Failed to get kubeconfig: {result.stderr}") - - kubeconfig = yaml.safe_load(result.stdout) - return kubeconfig - - except Exception as e: - logger.error(f"Failed to retrieve kubeconfig: {e}") - raise - - def _wait_for_cluster_ready(self, cluster_name: str, timeout: int = 300) -> bool: - """Wait for cluster to be fully ready.""" - logger.info("⏳ Waiting for cluster to be ready...") - - start_time = time.time() - - while time.time() - start_time < timeout: - try: - # Check if all nodes are ready - result = subprocess.run( - ["kubectl", "get", "nodes", "--context", f"kind-{cluster_name}"], - capture_output=True, - text=True, - timeout=10, - ) - - if result.returncode == 0: - # Check if all nodes show as Ready - lines = result.stdout.strip().split("\n")[1:] # Skip header - if lines and all("Ready" in line for line in lines): - logger.info("✅ All nodes are ready") - return True - - logger.info("Still waiting for nodes to be ready...") - time.sleep(10) - - except Exception as e: - logger.debug(f"Error checking node status: {e}") - time.sleep(5) - - logger.error("❌ Timeout waiting for cluster to be ready") - return False - - def _get_cluster_info( - self, cluster_spec: ClusterSpec, kubeconfig: Dict[str, Any] - ) -> Dict[str, Any]: - """Get comprehensive cluster information.""" - - # Get cluster endpoint from kubeconfig - cluster_info_from_config = kubeconfig["clusters"][0]["cluster"] - endpoint = cluster_info_from_config["server"] - - # Get node information - nodes = self._get_node_info(cluster_spec.cluster_name) - - return { - "cluster_id": cluster_spec.cluster_name, - "cluster_name": cluster_spec.cluster_name, - "provider": "local-docker", - "region": "local", - "status": "RUNNING", - "ready_for_jobs": True, - "endpoint": endpoint, - "nodes": nodes, - "node_count": len(nodes), - "kubernetes_version": self._get_kubernetes_version( - cluster_spec.cluster_name - ), - "kubectl_config": kubeconfig, - "created_resources": { - "cluster": cluster_spec.cluster_name, - "nodes": [node["name"] for node in nodes], - }, - "provisioning_method": "kind", - "cost_estimate": 0.0, # Local clusters are free - } - - def _get_node_info(self, cluster_name: str) -> list: - """Get information about cluster nodes.""" - try: - result = subprocess.run( - [ - "kubectl", - "get", - "nodes", - "-o", - "json", - "--context", - f"kind-{cluster_name}", - ], - capture_output=True, - text=True, - timeout=30, - ) - - if result.returncode != 0: - logger.warning(f"Failed to get node info: {result.stderr}") - return [] - - nodes_data = yaml.safe_load(result.stdout) - nodes = [] - - for node in nodes_data.get("items", []): - node_info = { - "name": node["metadata"]["name"], - "status": "Ready" if self._node_is_ready(node) else "NotReady", - "roles": self._get_node_roles(node), - "version": node["status"]["nodeInfo"]["kubeletVersion"], - "os": node["status"]["nodeInfo"]["osImage"], - "container_runtime": node["status"]["nodeInfo"][ - "containerRuntimeVersion" - ], - } - nodes.append(node_info) - - return nodes - - except Exception as e: - logger.warning(f"Failed to get node information: {e}") - return [] - - def _node_is_ready(self, node: Dict[str, Any]) -> bool: - """Check if a node is ready.""" - conditions = node.get("status", {}).get("conditions", []) - for condition in conditions: - if condition.get("type") == "Ready" and condition.get("status") == "True": - return True - return False - - def _get_node_roles(self, node: Dict[str, Any]) -> list: - """Get roles for a node.""" - labels = node.get("metadata", {}).get("labels", {}) - roles = [] - - if "node-role.kubernetes.io/control-plane" in labels: - roles.append("control-plane") - if "node-role.kubernetes.io/master" in labels: - roles.append("master") - if not roles: - roles.append("worker") - - return roles - - def _get_kubernetes_version(self, cluster_name: str) -> str: - """Get Kubernetes version of the cluster.""" - try: - result = subprocess.run( - [ - "kubectl", - "version", - "--context", - f"kind-{cluster_name}", - "--output", - "yaml", - ], - capture_output=True, - text=True, - timeout=30, - ) - - if result.returncode == 0: - version_info = yaml.safe_load(result.stdout) - server_version = version_info.get("serverVersion", {}).get( - "gitVersion", "unknown" - ) - return server_version - - except Exception as e: - logger.debug(f"Failed to get Kubernetes version: {e}") - - return "unknown" - - def get_cluster_status(self, cluster_name: str) -> Dict[str, str]: - """Get current status of the cluster.""" - try: - # Check if cluster exists - result = subprocess.run( - ["kind", "get", "clusters"], capture_output=True, text=True, timeout=30 - ) - - if result.returncode != 0: - return {"status": "ERROR", "ready_for_jobs": "false"} - - clusters = result.stdout.strip().split("\n") - if cluster_name not in clusters: - return {"status": "NOT_FOUND", "ready_for_jobs": "false"} - - # Check if cluster is ready - ready = self._wait_for_cluster_ready(cluster_name, timeout=10) - - return { - "status": "RUNNING" if ready else "STARTING", - "ready_for_jobs": "true" if ready else "false", - } - - except Exception as e: - logger.error(f"Error getting cluster status: {e}") - return {"status": "ERROR", "ready_for_jobs": "false"} - - def destroy_cluster_infrastructure(self, cluster_name: str) -> bool: - """Destroy the local Kubernetes cluster.""" - logger.info(f"🗑️ Destroying local cluster: {cluster_name}") - - try: - result = subprocess.run( - ["kind", "delete", "cluster", "--name", cluster_name], - capture_output=True, - text=True, - timeout=120, # 2 minute timeout - ) - - if result.returncode == 0: - logger.info(f"✅ Local cluster destroyed successfully: {cluster_name}") - return True - else: - logger.error(f"Failed to destroy cluster: {result.stderr}") - return False - - except Exception as e: - logger.error(f"Error destroying cluster: {e}") - return False - - def list_clusters(self) -> list: - """List all local kind clusters.""" - try: - result = subprocess.run( - ["kind", "get", "clusters"], capture_output=True, text=True, timeout=30 - ) - - if result.returncode == 0: - clusters = result.stdout.strip().split("\n") - return [c for c in clusters if c.strip()] - else: - logger.warning(f"Failed to list clusters: {result.stderr}") - return [] - - except Exception as e: - logger.error(f"Error listing clusters: {e}") - return [] diff --git a/clustrix/pricing_clients/__init__.py b/clustrix/pricing_clients/__init__.py deleted file mode 100644 index aa1f9938..00000000 --- a/clustrix/pricing_clients/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Pricing client implementations for cloud providers.""" - -from .base import BasePricingClient, PricingCache -from .aws_pricing import AWSPricingClient -from .azure_pricing import AzurePricingClient -from .gcp_pricing import GCPPricingClient -from .lambda_pricing import LambdaPricingClient - -__all__ = [ - "BasePricingClient", - "PricingCache", - "AWSPricingClient", - "AzurePricingClient", - "GCPPricingClient", - "LambdaPricingClient", -] diff --git a/clustrix/pricing_clients/aws_pricing.py b/clustrix/pricing_clients/aws_pricing.py deleted file mode 100644 index 1987bc86..00000000 --- a/clustrix/pricing_clients/aws_pricing.py +++ /dev/null @@ -1,273 +0,0 @@ -"""AWS pricing client implementation.""" - -import json -import logging -from typing import Dict, Optional, Any - -# Remove pkg_resources dependency to avoid mypy issues - -from .base import BasePricingClient - -logger = logging.getLogger(__name__) - - -class AWSPricingClient(BasePricingClient): - """Client for fetching AWS EC2 instance pricing.""" - - def __init__(self, cache_ttl_hours: int = 24): - """Initialize AWS pricing client.""" - super().__init__(cache_ttl_hours) - - # Hardcoded pricing as fallback (as of 2025-01) - self._hardcoded_pricing_date = "2025-01-01" - self._hardcoded_pricing = { - # General Purpose - "t2.micro": 0.0116, - "t2.small": 0.023, - "t2.medium": 0.0464, - "t2.large": 0.0928, - "t3.micro": 0.0104, - "t3.small": 0.0208, - "t3.medium": 0.0416, - "t3.large": 0.0832, - "m5.large": 0.096, - "m5.xlarge": 0.192, - "m5.2xlarge": 0.384, - "m5.4xlarge": 0.768, - # Compute Optimized - "c5.large": 0.085, - "c5.xlarge": 0.17, - "c5.2xlarge": 0.34, - "c5.4xlarge": 0.68, - # Memory Optimized - "r5.large": 0.126, - "r5.xlarge": 0.252, - "r5.2xlarge": 0.504, - "r5.4xlarge": 1.008, - # GPU Instances - "p3.2xlarge": 3.06, - "p3.8xlarge": 12.24, - "p3.16xlarge": 24.48, - "g4dn.xlarge": 0.526, - "g4dn.2xlarge": 0.752, - "g4dn.4xlarge": 1.204, - } - - def get_instance_pricing( - self, - instance_type: str, - region: str, - operating_system: str = "Linux", - tenancy: str = "Shared", - **kwargs, - ) -> Optional[float]: - """Get hourly pricing for a specific EC2 instance type. - - Args: - instance_type: EC2 instance type (e.g., 't2.micro') - region: AWS region (e.g., 'us-east-1') - operating_system: OS type ('Linux', 'Windows', 'RHEL', 'SUSE') - tenancy: Instance tenancy ('Shared', 'Dedicated', 'Host') - - Returns: - Hourly price in USD or None if not found - """ - # Generate cache key - cache_key = f"aws_{region}_{instance_type}_{operating_system}_{tenancy}" - - # Check cache first - cached_data = self.cache.get(cache_key) - if cached_data and "price" in cached_data: - return cached_data["price"] - - # Try to fetch from API - try: - pricing_data = self._fetch_pricing_from_api( - instance_type=instance_type, - region=region, - operating_system=operating_system, - tenancy=tenancy, - ) - - if pricing_data and "price" in pricing_data: - # Cache the result - self.cache.set(cache_key, pricing_data) - return pricing_data["price"] - except Exception as e: - logger.warning(f"Failed to fetch pricing from API: {e}") - - # Fall back to hardcoded pricing - return self._get_fallback_price(instance_type) - - def get_all_pricing( - self, region: str, operating_system: str = "Linux", **kwargs - ) -> Dict[str, float]: - """Get all EC2 instance pricing for a region. - - Args: - region: AWS region - operating_system: OS type - - Returns: - Dictionary mapping instance types to hourly prices - """ - # For simplicity, return hardcoded pricing with a warning - # In a full implementation, this would query the API for all types - if self.is_pricing_data_outdated(): - logger.warning( - f"Using potentially outdated pricing data from {self._hardcoded_pricing_date}" - ) - - return self._hardcoded_pricing.copy() - - def _fetch_pricing_from_api( - self, - instance_type: Optional[str], - region: str, - operating_system: str = "Linux", - tenancy: str = "Shared", - **kwargs, - ) -> Optional[Dict[str, Any]]: - """Fetch pricing from AWS Pricing API using boto3. - - Args: - instance_type: EC2 instance type - region: AWS region code - operating_system: Operating system - tenancy: Instance tenancy - - Returns: - Pricing data dictionary or None - """ - try: - import boto3 - from botocore.exceptions import ClientError, NoCredentialsError - except ImportError: - logger.debug("boto3 not available, falling back to hardcoded pricing") - return None - - try: - # Create pricing client (must use specific regions) - pricing_client = boto3.client("pricing", region_name="us-east-1") - - # Convert region code to region name - region_name = self._get_region_name(region) - - # Define filters - filters = [ - {"Type": "TERM_MATCH", "Field": "termType", "Value": "OnDemand"}, - {"Type": "TERM_MATCH", "Field": "capacitystatus", "Value": "Used"}, - {"Type": "TERM_MATCH", "Field": "location", "Value": region_name}, - {"Type": "TERM_MATCH", "Field": "instanceType", "Value": instance_type}, - {"Type": "TERM_MATCH", "Field": "tenancy", "Value": tenancy}, - { - "Type": "TERM_MATCH", - "Field": "operatingSystem", - "Value": operating_system, - }, - {"Type": "TERM_MATCH", "Field": "preInstalledSw", "Value": "NA"}, - ] - - # Get pricing data - response = pricing_client.get_products( - ServiceCode="AmazonEC2", Filters=filters, FormatVersion="aws_v1" - ) - - if len(response["PriceList"]) > 0: - price_data = json.loads(response["PriceList"][0]) - - # Extract on-demand pricing - on_demand = price_data["terms"]["OnDemand"] - if on_demand: - first_sku = list(on_demand.keys())[0] - price_dimensions = on_demand[first_sku]["priceDimensions"] - first_price_dim = list(price_dimensions.keys())[0] - price = float( - price_dimensions[first_price_dim]["pricePerUnit"]["USD"] - ) - - return { - "price": price, - "instance_type": instance_type, - "region": region, - "operating_system": operating_system, - "currency": "USD", - } - - except NoCredentialsError: - logger.debug("AWS credentials not available") - except ClientError as e: - logger.debug(f"AWS API error: {e}") - except Exception as e: - logger.debug(f"Unexpected error fetching AWS pricing: {e}") - - return None - - def _get_region_name(self, region_code: str) -> str: - """Convert region code to region name for Pricing API. - - Args: - region_code: AWS region code (e.g., 'us-east-1') - - Returns: - Region name (e.g., 'US East (N. Virginia)') - """ - # Try to get from boto3 session - try: - import boto3 - - session = boto3.Session() - # Get available regions for EC2 service - available_regions = session.get_available_regions("ec2") - if region_code in available_regions: - # Use boto3's built-in region descriptions if available - try: - # This is a best-effort lookup using boto3 internals - from botocore.loaders import Loader - - loader = Loader() - endpoints = loader.load_service_model("ec2", "service-2") - if "metadata" in endpoints and "regions" in endpoints["metadata"]: - regions_data = endpoints["metadata"]["regions"] - if region_code in regions_data: - description = regions_data[region_code].get( - "description", "" - ) - if description: - # Pricing API uses 'EU' instead of 'Europe' - return description.replace("Europe", "EU") - except Exception: - pass - except Exception: - pass - - # Fallback to common region mappings - region_map = { - "us-east-1": "US East (N. Virginia)", - "us-east-2": "US East (Ohio)", - "us-west-1": "US West (N. California)", - "us-west-2": "US West (Oregon)", - "eu-west-1": "EU (Ireland)", - "eu-central-1": "EU (Frankfurt)", - "ap-southeast-1": "Asia Pacific (Singapore)", - "ap-northeast-1": "Asia Pacific (Tokyo)", - } - - return region_map.get(region_code, "US East (N. Virginia)") - - def get_spot_pricing(self, instance_type: str, region: str) -> Optional[float]: - """Get spot instance pricing. - - Args: - instance_type: EC2 instance type - region: AWS region - - Returns: - Estimated spot price (uses hardcoded discount for now) - """ - on_demand_price = self.get_instance_pricing(instance_type, region) - if on_demand_price: - # Apply approximate spot discount (varies by instance type) - spot_discount = 0.7 # 70% discount is a rough average - return on_demand_price * (1 - spot_discount) - return None diff --git a/clustrix/pricing_clients/azure_pricing.py b/clustrix/pricing_clients/azure_pricing.py deleted file mode 100644 index b9c21c27..00000000 --- a/clustrix/pricing_clients/azure_pricing.py +++ /dev/null @@ -1,381 +0,0 @@ -"""Azure pricing client implementation using Azure Retail Prices API.""" - -import logging -from typing import Dict, Optional, Any - -import requests - -from .base import BasePricingClient - -logger = logging.getLogger(__name__) - - -class AzurePricingClient(BasePricingClient): - """Client for fetching Azure VM pricing using Azure Retail Prices API.""" - - def __init__(self, cache_ttl_hours: int = 24): - """Initialize Azure pricing client.""" - super().__init__(cache_ttl_hours) - - # Azure Retail Prices API endpoint - self.api_url = "https://prices.azure.com/api/retail/prices" - self.api_version = "2021-10-01-preview" - - # Hardcoded pricing as fallback (as of 2025-01) - self._hardcoded_pricing_date = "2025-01-01" - self._hardcoded_pricing = { - # Basic VMs - "Standard_A1_v2": 0.085, - "Standard_A2_v2": 0.17, - "Standard_A4_v2": 0.34, - # General Purpose - "Standard_D2s_v3": 0.096, - "Standard_D4s_v3": 0.192, - "Standard_D8s_v3": 0.384, - "Standard_D16s_v3": 0.768, - # Compute Optimized - "Standard_F2s_v2": 0.085, - "Standard_F4s_v2": 0.17, - "Standard_F8s_v2": 0.34, - "Standard_F16s_v2": 0.68, - # Memory Optimized - "Standard_E2s_v3": 0.126, - "Standard_E4s_v3": 0.252, - "Standard_E8s_v3": 0.504, - "Standard_E16s_v3": 1.008, - # GPU VMs - "Standard_NC6s_v3": 3.06, - "Standard_NC12s_v3": 6.12, - "Standard_NC24s_v3": 12.24, - "Standard_ND40rs_v2": 27.20, - # Default fallback - "default": 0.10, - } - - # Region mapping for Azure API - self.region_mapping = { - "eastus": "East US", - "eastus2": "East US 2", - "westus": "West US", - "westus2": "West US 2", - "westus3": "West US 3", - "centralus": "Central US", - "northcentralus": "North Central US", - "southcentralus": "South Central US", - "westcentralus": "West Central US", - "canadacentral": "Canada Central", - "canadaeast": "Canada East", - "brazilsouth": "Brazil South", - "northeurope": "North Europe", - "westeurope": "West Europe", - "francecentral": "France Central", - "germanywestcentral": "Germany West Central", - "norwayeast": "Norway East", - "switzerlandnorth": "Switzerland North", - "uksouth": "UK South", - "ukwest": "UK West", - "eastasia": "East Asia", - "southeastasia": "Southeast Asia", - "australiaeast": "Australia East", - "australiasoutheast": "Australia Southeast", - "centralindia": "Central India", - "southindia": "South India", - "westindia": "West India", - "japaneast": "Japan East", - "japanwest": "Japan West", - "koreacentral": "Korea Central", - "koreasouth": "Korea South", - } - - def get_instance_pricing( - self, - instance_type: str, - region: str, - operating_system: str = "Linux", - **kwargs, - ) -> Optional[float]: - """Get hourly pricing for a specific Azure VM size. - - Args: - instance_type: Azure VM size (e.g., 'Standard_D2s_v3') - region: Azure region (e.g., 'eastus', 'westeurope') - operating_system: OS type ('Linux', 'Windows') - - Returns: - Hourly price in USD or None if not found - """ - # Generate cache key - cache_key = f"azure_{region}_{instance_type}_{operating_system}" - - # Check cache first - cached_data = self.cache.get(cache_key) - if cached_data and "price" in cached_data: - return cached_data["price"] - - # Try to fetch from API - try: - pricing_data = self._fetch_pricing_from_api( - instance_type=instance_type, - region=region, - operating_system=operating_system, - ) - - if pricing_data and "price" in pricing_data: - # Cache the result - self.cache.set(cache_key, pricing_data) - return pricing_data["price"] - except Exception as e: - logger.warning(f"Failed to fetch Azure pricing from API: {e}") - - # Fall back to hardcoded pricing - fallback_price = self._get_fallback_price(instance_type) - if fallback_price is None: - # Use default price for unknown instance types - fallback_price = self._hardcoded_pricing.get("default") - return fallback_price - - def get_all_pricing( - self, region: str, operating_system: str = "Linux", **kwargs - ) -> Dict[str, float]: - """Get all Azure VM pricing for a region. - - Args: - region: Azure region - operating_system: OS type - - Returns: - Dictionary mapping VM sizes to hourly prices - """ - # For simplicity, return hardcoded pricing with a warning - # In a full implementation, this would query the API for all VM sizes - if self.is_pricing_data_outdated(): - logger.warning( - f"Using potentially outdated pricing data from {self._hardcoded_pricing_date}" - ) - - return self._hardcoded_pricing.copy() - - def _fetch_pricing_from_api( - self, - instance_type: Optional[str], - region: str, - operating_system: str = "Linux", - **kwargs, - ) -> Optional[Dict[str, Any]]: - """Fetch pricing from Azure Retail Prices API. - - Args: - instance_type: Azure VM size - region: Azure region code - operating_system: Operating system - - Returns: - Pricing data dictionary or None - """ - try: - # Build filter query for Azure API - filters = [ - "serviceName eq 'Virtual Machines'", - f"armRegionName eq '{region.lower()}'", - f"armSkuName eq '{instance_type}'", - "priceType eq 'Consumption'", - ] - - # Add OS filter - be more specific - if operating_system.lower() == "windows": - filters.append("contains(productName, 'Windows')") - # Skip the "not contains" filter for Linux to avoid OData issues - - filter_query = " and ".join(filters) - - # Make API request - params = {"api-version": self.api_version, "$filter": filter_query} - - response = requests.get(self.api_url, params=params, timeout=30) - response.raise_for_status() - - data = response.json() - - if "Items" in data and len(data["Items"]) > 0: - # Find the best matching item (regular pricing, not spot/low priority) - best_item = None - for item in data["Items"]: - meter_name = item.get("meterName", "") - product_name = item.get("productName", "") - - # Skip spot and low priority instances - if "Spot" in meter_name or "Low Priority" in meter_name: - continue - - # For Linux, avoid Windows products - if ( - operating_system.lower() == "linux" - and "Windows" in product_name - ): - continue - - # For Windows, prefer Windows products - if ( - operating_system.lower() == "windows" - and "Windows" not in product_name - ): - continue - - # This looks like the right item - best_item = item - break - - if best_item: - price = float(best_item["retailPrice"]) - else: - # Fallback to first item if no perfect match - price = float(data["Items"][0]["retailPrice"]) - best_item = data["Items"][0] - - return { - "price": price, - "instance_type": instance_type, - "region": region, - "operating_system": operating_system, - "currency": best_item.get("currencyCode", "USD"), - "meter_name": best_item.get("meterName", ""), - "product_name": best_item.get("productName", ""), - } - - except requests.RequestException as e: - logger.debug(f"Azure API request failed: {e}") - except (KeyError, ValueError, TypeError) as e: - logger.debug(f"Error parsing Azure API response: {e}") - except Exception as e: - logger.debug(f"Unexpected error fetching Azure pricing: {e}") - - return None - - def _get_region_name(self, region_code: str) -> str: - """Convert region code to region name for Azure API. - - Args: - region_code: Azure region code (e.g., 'eastus') - - Returns: - Region name (e.g., 'East US') - """ - return self.region_mapping.get(region_code.lower(), region_code) - - def get_spot_pricing(self, instance_type: str, region: str) -> Optional[float]: - """Get spot VM pricing for Azure. - - Args: - instance_type: Azure VM size - region: Azure region - - Returns: - Estimated spot price (uses API or hardcoded discount) - """ - # Try to get spot pricing from API - try: - pricing_data = self._fetch_spot_pricing_from_api(instance_type, region) - if pricing_data and "price" in pricing_data: - return pricing_data["price"] - except Exception as e: - logger.debug(f"Failed to get spot pricing from API: {e}") - - # Fall back to estimating from on-demand pricing - on_demand_price = self.get_instance_pricing(instance_type, region) - if on_demand_price: - # Apply approximate spot discount (varies by VM family) - spot_discount = 0.8 # 80% discount is typical for Azure spot VMs - return on_demand_price * (1 - spot_discount) - return None - - def _fetch_spot_pricing_from_api( - self, instance_type: str, region: str - ) -> Optional[Dict[str, Any]]: - """Fetch spot pricing from Azure API. - - Args: - instance_type: Azure VM size - region: Azure region - - Returns: - Spot pricing data or None - """ - try: - # Build filter query for spot pricing - filters = [ - "serviceName eq 'Virtual Machines'", - f"armRegionName eq '{region.lower()}'", - f"armSkuName eq '{instance_type}'", - "priceType eq 'Consumption'", - "contains(meterName, 'Spot')", - ] - - filter_query = " and ".join(filters) - params = {"api-version": self.api_version, "$filter": filter_query} - - response = requests.get(self.api_url, params=params, timeout=30) - response.raise_for_status() - - data = response.json() - - if "Items" in data and len(data["Items"]) > 0: - item = data["Items"][0] - price = float(item["retailPrice"]) - - return { - "price": price, - "instance_type": instance_type, - "region": region, - "pricing_type": "spot", - "currency": item.get("currencyCode", "USD"), - } - - except Exception as e: - logger.debug(f"Failed to fetch Azure spot pricing: {e}") - - return None - - def get_pricing_by_service( - self, service_name: str = "Virtual Machines", region: str = "eastus" - ) -> Dict[str, Any]: - """Get pricing for all items in a specific Azure service. - - Args: - service_name: Azure service name (e.g., 'Virtual Machines') - region: Azure region - - Returns: - Dictionary with pricing information - """ - try: - filters = [ - f"serviceName eq '{service_name}'", - f"armRegionName eq '{region.lower()}'", - "priceType eq 'Consumption'", - ] - - filter_query = " and ".join(filters) - params = {"api-version": self.api_version, "$filter": filter_query} - - response = requests.get(self.api_url, params=params, timeout=30) - response.raise_for_status() - - data = response.json() - pricing_info = {} - - if "Items" in data: - for item in data["Items"]: - sku_name = item.get("armSkuName", "unknown") - if sku_name not in pricing_info: - pricing_info[sku_name] = { - "price": float(item["retailPrice"]), - "currency": item.get("currencyCode", "USD"), - "meter_name": item.get("meterName", ""), - "product_name": item.get("productName", ""), - } - - return pricing_info - - except Exception as e: - logger.warning(f"Failed to fetch Azure service pricing: {e}") - return {} diff --git a/clustrix/pricing_clients/base.py b/clustrix/pricing_clients/base.py deleted file mode 100644 index a2f7b206..00000000 --- a/clustrix/pricing_clients/base.py +++ /dev/null @@ -1,153 +0,0 @@ -"""Base pricing client for cloud providers.""" - -from abc import ABC, abstractmethod -from datetime import datetime, timedelta -from typing import Dict, Optional, Any -import logging -import json -from pathlib import Path - -logger = logging.getLogger(__name__) - - -class PricingCache: - """Simple file-based cache for pricing data.""" - - def __init__(self, cache_dir: Optional[Path] = None, ttl_hours: int = 24): - """Initialize the pricing cache. - - Args: - cache_dir: Directory to store cache files. Defaults to ~/.clustrix/cache - ttl_hours: Time to live for cached data in hours - """ - if cache_dir is None: - cache_dir = Path.home() / ".clustrix" / "cache" - self.cache_dir = cache_dir - self.cache_dir.mkdir(parents=True, exist_ok=True) - self.ttl = timedelta(hours=ttl_hours) - - def get(self, key: str) -> Optional[Dict[str, Any]]: - """Get cached pricing data if it exists and is not expired.""" - cache_file = self.cache_dir / f"{key}.json" - if not cache_file.exists(): - return None - - try: - with open(cache_file, "r") as f: - data = json.load(f) - - cached_time = datetime.fromisoformat(data["cached_at"]) - if datetime.now() - cached_time > self.ttl: - logger.debug(f"Cache expired for key: {key}") - return None - - logger.debug(f"Cache hit for key: {key}") - return data["pricing"] - except Exception as e: - logger.warning(f"Error reading cache for {key}: {e}") - return None - - def set(self, key: str, data: Dict[str, Any]): - """Cache pricing data.""" - cache_file = self.cache_dir / f"{key}.json" - try: - cache_data = {"cached_at": datetime.now().isoformat(), "pricing": data} - with open(cache_file, "w") as f: - json.dump(cache_data, f, indent=2) - logger.debug(f"Cached data for key: {key}") - except Exception as e: - logger.warning(f"Error caching data for {key}: {e}") - - -class BasePricingClient(ABC): - """Abstract base class for pricing clients.""" - - def __init__(self, cache_ttl_hours: int = 24): - """Initialize the pricing client. - - Args: - cache_ttl_hours: Time to live for cached pricing data - """ - self.cache = PricingCache(ttl_hours=cache_ttl_hours) - self._hardcoded_pricing: Dict[str, Any] = {} - self._hardcoded_pricing_date: Optional[str] = None - - @abstractmethod - def get_instance_pricing( - self, instance_type: str, region: str, **kwargs - ) -> Optional[float]: - """Get hourly pricing for a specific instance type. - - Args: - instance_type: The instance type (e.g., 't2.micro', 'm5.large') - region: The region code (e.g., 'us-east-1', 'eu-west-1') - **kwargs: Additional provider-specific parameters - - Returns: - Hourly price in USD or None if not found - """ - pass - - @abstractmethod - def get_all_pricing(self, region: str, **kwargs) -> Dict[str, float]: - """Get all instance pricing for a region. - - Args: - region: The region code - **kwargs: Additional provider-specific parameters - - Returns: - Dictionary mapping instance types to hourly prices - """ - pass - - @abstractmethod - def _fetch_pricing_from_api( - self, instance_type: Optional[str], region: str, **kwargs - ) -> Optional[Dict[str, Any]]: - """Fetch pricing data from the provider's API. - - Args: - instance_type: Optional instance type to filter by - region: The region code - **kwargs: Additional provider-specific parameters - - Returns: - Raw pricing data from the API or None if failed - """ - pass - - def _get_fallback_price(self, instance_type: str) -> Optional[float]: - """Get hardcoded fallback price for an instance type. - - Args: - instance_type: The instance type - - Returns: - Hourly price or None if not found - """ - if instance_type in self._hardcoded_pricing: - logger.warning( - f"Using hardcoded pricing for {instance_type} " - f"(last updated: {self._hardcoded_pricing_date or 'unknown'})" - ) - return self._hardcoded_pricing[instance_type] - return None - - def is_pricing_data_outdated(self, days: int = 30) -> bool: - """Check if hardcoded pricing data is outdated. - - Args: - days: Number of days to consider data outdated - - Returns: - True if data is older than specified days - """ - if self._hardcoded_pricing_date is None: - return True - - try: - pricing_date = datetime.fromisoformat(self._hardcoded_pricing_date) - return (datetime.now() - pricing_date).days > days - except Exception: - return True diff --git a/clustrix/pricing_clients/gcp_pricing.py b/clustrix/pricing_clients/gcp_pricing.py deleted file mode 100644 index 88682e08..00000000 --- a/clustrix/pricing_clients/gcp_pricing.py +++ /dev/null @@ -1,383 +0,0 @@ -"""GCP pricing client implementation using Cloud Billing Catalog API.""" - -import logging -from typing import Dict, Optional, Any - -from .base import BasePricingClient - -logger = logging.getLogger(__name__) - - -class GCPPricingClient(BasePricingClient): - """Client for fetching GCP Compute Engine pricing using Cloud Billing Catalog API.""" - - def __init__(self, cache_ttl_hours: int = 24): - """Initialize GCP pricing client.""" - super().__init__(cache_ttl_hours) - - # Compute Engine service ID for GCP pricing API - self.compute_service_id = "6F81-5844-456A" - - # Hardcoded pricing as fallback (as of 2025-01) - self._hardcoded_pricing_date = "2025-01-01" - self._hardcoded_pricing = { - # General Purpose - N1 Series - "n1-standard-1": 0.0475, - "n1-standard-2": 0.095, - "n1-standard-4": 0.19, - "n1-standard-8": 0.38, - # General Purpose - N2 Series - "n2-standard-2": 0.078, - "n2-standard-4": 0.156, - "n2-standard-8": 0.312, - "n2-standard-16": 0.624, - # Compute Optimized - C2 Series - "c2-standard-4": 0.168, - "c2-standard-8": 0.336, - "c2-standard-16": 0.672, - "c2-standard-30": 1.26, - # Memory Optimized - M1 Series - "m1-ultramem-40": 3.844, - "m1-ultramem-80": 7.688, - "m1-ultramem-160": 15.376, - # GPU Instances - "n1-standard-4-k80": 0.64, # with K80 GPU - "n1-standard-8-k80": 1.28, # with K80 GPU - "n1-standard-4-t4": 0.54, # with T4 GPU - "n1-standard-8-t4": 1.08, # with T4 GPU - "n1-standard-4-v100": 2.73, # with V100 GPU - "n1-standard-8-v100": 5.46, # with V100 GPU - # Default fallback - "default": 0.10, - } - - # Region mapping for GCP - self.region_mapping = { - "us-central1": "us-central1", - "us-east1": "us-east1", - "us-east4": "us-east4", - "us-west1": "us-west1", - "us-west2": "us-west2", - "us-west3": "us-west3", - "us-west4": "us-west4", - "europe-north1": "europe-north1", - "europe-west1": "europe-west1", - "europe-west2": "europe-west2", - "europe-west3": "europe-west3", - "europe-west4": "europe-west4", - "europe-west6": "europe-west6", - "asia-east1": "asia-east1", - "asia-east2": "asia-east2", - "asia-northeast1": "asia-northeast1", - "asia-northeast2": "asia-northeast2", - "asia-northeast3": "asia-northeast3", - "asia-south1": "asia-south1", - "asia-southeast1": "asia-southeast1", - "asia-southeast2": "asia-southeast2", - "australia-southeast1": "australia-southeast1", - } - - def get_instance_pricing( - self, - instance_type: str, - region: str, - **kwargs, - ) -> Optional[float]: - """Get hourly pricing for a specific GCP machine type. - - Args: - instance_type: GCP machine type (e.g., 'n1-standard-4') - region: GCP region (e.g., 'us-central1') - - Returns: - Hourly price in USD or None if not found - """ - # Generate cache key - cache_key = f"gcp_{region}_{instance_type}" - - # Check cache first - cached_data = self.cache.get(cache_key) - if cached_data and "price" in cached_data: - return cached_data["price"] - - # Try to fetch from API - try: - pricing_data = self._fetch_pricing_from_api( - instance_type=instance_type, region=region - ) - - if pricing_data and "price" in pricing_data: - # Cache the result - self.cache.set(cache_key, pricing_data) - return pricing_data["price"] - except Exception as e: - logger.warning(f"Failed to fetch GCP pricing from API: {e}") - - # Fall back to hardcoded pricing - fallback_price = self._get_fallback_price(instance_type) - if fallback_price is None: - # Use default price for unknown instance types - fallback_price = self._hardcoded_pricing.get("default") - return fallback_price - - def get_all_pricing(self, region: str, **kwargs) -> Dict[str, float]: - """Get all GCP machine type pricing for a region. - - Args: - region: GCP region - - Returns: - Dictionary mapping machine types to hourly prices - """ - # For simplicity, return hardcoded pricing with a warning - # In a full implementation, this would query the API for all machine types - if self.is_pricing_data_outdated(): - logger.warning( - f"Using potentially outdated pricing data from {self._hardcoded_pricing_date}" - ) - - return self._hardcoded_pricing.copy() - - def _fetch_pricing_from_api( - self, - instance_type: Optional[str], - region: str, - **kwargs, - ) -> Optional[Dict[str, Any]]: - """Fetch pricing from GCP Cloud Billing Catalog API. - - Args: - instance_type: GCP machine type - region: GCP region - - Returns: - Pricing data dictionary or None - """ - try: - # Try to import Google Cloud libraries - from google.cloud import billing_v1 - from google.auth.exceptions import DefaultCredentialsError - from google.api_core.exceptions import GoogleAPIError - except ImportError: - logger.debug( - "Google Cloud libraries not available, falling back to hardcoded pricing" - ) - return None - - try: - # Create billing catalog client - client = billing_v1.CloudCatalogClient() - - # List services to find Compute Engine - services = client.list_services() - compute_service = None - - for service in services: - if service.display_name == "Compute Engine": - compute_service = service - break - - if not compute_service: - logger.debug("Could not find Compute Engine service in GCP catalog") - return None - - # List SKUs for Compute Engine in the specified region - skus_request = billing_v1.ListSkusRequest(parent=compute_service.name) - - skus = client.list_skus(request=skus_request) - - # Look for matching SKU - for sku in skus: - # Check if this SKU matches our instance type and region - if ( - region in sku.service_regions - and instance_type in sku.description.lower() - ): - - # Extract pricing information - if sku.pricing_info: - pricing_info = sku.pricing_info[0] - if pricing_info.pricing_expression.tiered_rates: - rate = pricing_info.pricing_expression.tiered_rates[0] - if rate.unit_price.currency_code == "USD": - # Convert from nanos to dollars - price = rate.unit_price.nanos / 1_000_000_000 - - return { - "price": price, - "instance_type": instance_type, - "region": region, - "currency": "USD", - "sku_id": sku.sku_id, - "description": sku.description, - } - - except DefaultCredentialsError: - logger.debug("GCP credentials not available") - except GoogleAPIError as e: - logger.debug(f"GCP API error: {e}") - except Exception as e: - logger.debug(f"Unexpected error fetching GCP pricing: {e}") - - return None - - def get_preemptible_pricing( - self, instance_type: str, region: str - ) -> Optional[float]: - """Get preemptible instance pricing for GCP. - - Args: - instance_type: GCP machine type - region: GCP region - - Returns: - Estimated preemptible price (uses API or hardcoded discount) - """ - # Try to get preemptible pricing from API - try: - pricing_data = self._fetch_preemptible_pricing_from_api( - instance_type, region - ) - if pricing_data and "price" in pricing_data: - return pricing_data["price"] - except Exception as e: - logger.debug(f"Failed to get preemptible pricing from API: {e}") - - # Fall back to estimating from on-demand pricing - on_demand_price = self.get_instance_pricing(instance_type, region) - if on_demand_price: - # Apply approximate preemptible discount (typically 60-91%) - preemptible_discount = 0.8 # 80% discount is typical - return on_demand_price * (1 - preemptible_discount) - return None - - def _fetch_preemptible_pricing_from_api( - self, instance_type: str, region: str - ) -> Optional[Dict[str, Any]]: - """Fetch preemptible pricing from GCP API. - - Args: - instance_type: GCP machine type - region: GCP region - - Returns: - Preemptible pricing data or None - """ - try: - from google.cloud import billing_v1 - except ImportError: - return None - - try: - client = billing_v1.CloudCatalogClient() - - # List services to find Compute Engine - services = client.list_services() - compute_service = None - - for service in services: - if service.display_name == "Compute Engine": - compute_service = service - break - - if not compute_service: - return None - - # List SKUs for preemptible instances - skus_request = billing_v1.ListSkusRequest(parent=compute_service.name) - - skus = client.list_skus(request=skus_request) - - # Look for preemptible SKU - for sku in skus: - if ( - region in sku.service_regions - and instance_type in sku.description.lower() - and "preemptible" in sku.description.lower() - ): - - if sku.pricing_info: - pricing_info = sku.pricing_info[0] - if pricing_info.pricing_expression.tiered_rates: - rate = pricing_info.pricing_expression.tiered_rates[0] - if rate.unit_price.currency_code == "USD": - price = rate.unit_price.nanos / 1_000_000_000 - - return { - "price": price, - "instance_type": instance_type, - "region": region, - "pricing_type": "preemptible", - "currency": "USD", - } - - except Exception as e: - logger.debug(f"Failed to fetch GCP preemptible pricing: {e}") - - return None - - def get_sustained_use_discount(self, hours_used: float, base_price: float) -> float: - """Calculate GCP sustained use discount. - - GCP automatically applies sustained use discounts for instances - that run for a significant portion of the month. - - Args: - hours_used: Number of hours the instance was used - base_price: Base hourly price - - Returns: - Discounted price - """ - # GCP sustained use discounts (approximation) - # 25% for >25% of month, 50% for >50%, 75% for >75% - month_hours = 24 * 30 # Approximate month - usage_percentage = hours_used / month_hours - - if usage_percentage > 0.75: - discount = 0.3 # 30% discount - elif usage_percentage > 0.5: - discount = 0.2 # 20% discount - elif usage_percentage > 0.25: - discount = 0.1 # 10% discount - else: - discount = 0.0 # No discount - - return base_price * (1 - discount) - - def get_custom_machine_pricing( - self, vcpus: int, memory_gb: float, region: str - ) -> Optional[float]: - """Get pricing for custom machine types in GCP. - - Args: - vcpus: Number of vCPUs - memory_gb: Amount of memory in GB - region: GCP region - - Returns: - Hourly price for custom machine or None - """ - # GCP custom machine pricing is based on vCPU and memory separately - # These are approximate rates for us-central1 - vcpu_price_per_hour = 0.033174 # per vCPU per hour - memory_price_per_hour = 0.004446 # per GB per hour - - # Regional pricing adjustments (approximate) - region_multipliers = { - "us-central1": 1.0, - "us-east1": 1.0, - "us-west1": 1.0, - "europe-west1": 1.1, - "europe-west2": 1.15, - "asia-east1": 1.1, - "asia-northeast1": 1.2, - } - - multiplier = region_multipliers.get(region, 1.1) # Default to 10% markup - - total_price = ( - vcpus * vcpu_price_per_hour + memory_gb * memory_price_per_hour - ) * multiplier - - return total_price diff --git a/clustrix/pricing_clients/lambda_pricing.py b/clustrix/pricing_clients/lambda_pricing.py deleted file mode 100644 index 43c91fbf..00000000 --- a/clustrix/pricing_clients/lambda_pricing.py +++ /dev/null @@ -1,330 +0,0 @@ -"""Lambda Cloud pricing client implementation.""" - -import logging -from typing import Dict, Optional, Any -import requests - -from .base import BasePricingClient - -logger = logging.getLogger(__name__) - - -class LambdaPricingClient(BasePricingClient): - """Client for fetching Lambda Cloud instance pricing.""" - - def __init__(self, cache_ttl_hours: int = 24): - """Initialize Lambda Cloud pricing client.""" - super().__init__(cache_ttl_hours) - - # Hardcoded pricing as fallback (as of 2025-01) - self._hardcoded_pricing_date = "2025-01-08" - self._hardcoded_pricing = { - # Single GPU instances - "gpu_1x_rtx6000ada": 0.75, - "gpu_1x_a10": 0.60, - "gpu_1x_a6000": 0.80, - "gpu_1x_a100": 1.10, - "gpu_1x_a100_80gb": 1.40, - "gpu_1x_h100": 2.50, - # Multi-GPU instances - "gpu_2x_a10": 1.20, - "gpu_2x_a6000": 1.60, - "gpu_2x_a100": 2.20, - "gpu_2x_a100_80gb": 2.80, - "gpu_4x_a10": 2.40, - "gpu_4x_a6000": 3.20, - "gpu_4x_a100": 4.40, - "gpu_4x_a100_80gb": 5.60, - "gpu_8x_a100": 8.80, - "gpu_8x_a100_80gb": 11.20, - "gpu_8x_v100": 8.00, - "gpu_8x_h100": 20.00, - # CPU instances - "cpu_4x": 0.10, - "cpu_8x": 0.20, - "cpu_16x": 0.40, - # Common aliases - "rtx6000ada": 0.75, - "a10": 0.60, - "a6000": 0.80, - "a100": 1.10, - "a100_40gb": 1.10, - "a100_80gb": 1.40, - "h100": 2.50, - "2xa100_40gb": 2.20, - "4xa100_40gb": 4.40, - "8xa100_40gb": 8.80, - "2xa100_80gb": 2.80, - "4xa100_80gb": 5.60, - "8xa100_80gb": 11.20, - "8xh100": 20.00, - # Default fallback - "default": 1.00, - } - - self.base_url = "https://cloud.lambdalabs.com/api/v1" - self.api_key: Optional[str] = None - self.authenticated = False - - def authenticate(self, api_key: str) -> bool: - """Authenticate with Lambda Cloud API. - - Args: - api_key: Lambda Cloud API key - - Returns: - True if authentication successful - """ - if not api_key: - logger.warning("No Lambda Cloud API key provided") - return False - - self.api_key = api_key - self.authenticated = True - - # Test authentication by making a simple API call - try: - headers = { - "Authorization": f"Bearer {api_key}", - "Content-Type": "application/json", - } - response = requests.get( - f"{self.base_url}/instance-types", headers=headers, timeout=10 - ) - if response.status_code == 200: - logger.info("Lambda Cloud API authentication successful") - return True - else: - logger.warning( - f"Lambda Cloud API authentication failed: {response.status_code}" - ) - self.authenticated = False - return False - except requests.RequestException as e: - logger.warning(f"Lambda Cloud API authentication error: {e}") - self.authenticated = False - return False - - def get_instance_pricing( - self, instance_type: str, region: str = "us-east-1", **kwargs - ) -> Optional[float]: - """Get hourly pricing for a specific Lambda Cloud instance type. - - Args: - instance_type: Lambda Cloud instance type (e.g., 'gpu_1x_a100') - region: Region (Lambda Cloud is primarily US-based) - **kwargs: Additional parameters (unused for Lambda Cloud) - - Returns: - Hourly price in USD or None if not found - """ - # Try to get from cache first - cache_key = f"lambda_{instance_type}_{region}" - cached_price = self.cache.get(cache_key) - if cached_price: - return cached_price.get("price") - - # Try to fetch from API - if self.authenticated: - try: - api_pricing = self._fetch_pricing_from_api(instance_type, region) - if api_pricing and instance_type in api_pricing: - price = api_pricing[instance_type] - # Cache the result - self.cache.set(cache_key, {"price": price, "source": "api"}) - return price - except Exception as e: - logger.warning(f"Failed to fetch Lambda Cloud pricing from API: {e}") - - # Fall back to hardcoded pricing - fallback_price = self._get_fallback_price(instance_type) - if fallback_price is not None: - # Cache fallback result (shorter TTL) - self.cache.set(cache_key, {"price": fallback_price, "source": "hardcoded"}) - return fallback_price - - # Try common variations of instance type names - variations = self._get_instance_variations(instance_type) - for variation in variations: - fallback_price = self._get_fallback_price(variation) - if fallback_price is not None: - logger.info( - f"Found pricing for {instance_type} using alias {variation}" - ) - self.cache.set( - cache_key, {"price": fallback_price, "source": "hardcoded_alias"} - ) - return fallback_price - - # Default fallback - default_price = self._hardcoded_pricing.get("default") - if default_price: - logger.warning( - f"Using default Lambda Cloud pricing for unknown instance type: {instance_type}" - ) - return default_price - - return None - - def get_all_pricing(self, region: str = "us-east-1", **kwargs) -> Dict[str, float]: - """Get all Lambda Cloud instance pricing for a region. - - Args: - region: Region code (Lambda Cloud is primarily US-based) - **kwargs: Additional parameters - - Returns: - Dictionary mapping instance types to hourly prices - """ - # Try to get comprehensive pricing from API - if self.authenticated: - try: - api_pricing = self._fetch_pricing_from_api(None, region) - if api_pricing: - return api_pricing - except Exception as e: - logger.warning(f"Failed to fetch all Lambda Cloud pricing: {e}") - - # Return hardcoded pricing as fallback - logger.warning( - f"Using hardcoded Lambda Cloud pricing " - f"(last updated: {self._hardcoded_pricing_date})" - ) - return self._hardcoded_pricing.copy() - - def _fetch_pricing_from_api( - self, instance_type: Optional[str], region: str, **kwargs - ) -> Optional[Dict[str, Any]]: - """Fetch pricing data from Lambda Cloud API. - - Args: - instance_type: Optional specific instance type - region: Region code - **kwargs: Additional parameters - - Returns: - Dictionary with pricing data or None if failed - """ - if not self.authenticated: - return None - - try: - headers = { - "Authorization": f"Bearer {self.api_key}", - "Content-Type": "application/json", - } - - # First, get instance types to see if pricing is included - response = requests.get( - f"{self.base_url}/instance-types", headers=headers, timeout=15 - ) - - if response.status_code != 200: - logger.warning( - f"Lambda Cloud API returned status {response.status_code}" - ) - return None - - data = response.json() - if "data" not in data: - logger.warning("Lambda Cloud API response missing 'data' field") - return None - - pricing_data = {} - instance_types = data["data"] - - for instance_info in instance_types: - # Lambda Cloud API structure may vary, extract pricing if available - name = instance_info.get("name") - if not name: - continue - - # Look for price in different possible fields - price = None - price_fields = ["price", "price_per_hour", "hourly_price", "cost"] - for field in price_fields: - if field in instance_info: - price_val = instance_info[field] - if isinstance(price_val, (int, float)): - price = float(price_val) - break - elif isinstance(price_val, dict) and "amount" in price_val: - price = float(price_val["amount"]) - break - - # If we found a price, add it to our pricing data - if price is not None: - pricing_data[name] = price - logger.debug( - f"Found Lambda Cloud price for {name}: ${price:.3f}/hr" - ) - - # If we got pricing data from API, return it - if pricing_data: - logger.info( - f"Successfully fetched Lambda Cloud pricing for {len(pricing_data)} instance types" - ) - return pricing_data - else: - logger.info( - "Lambda Cloud API response doesn't contain pricing information" - ) - return None - - except requests.RequestException as e: - logger.warning(f"Lambda Cloud API request failed: {e}") - return None - except Exception as e: - logger.warning(f"Error processing Lambda Cloud API response: {e}") - return None - - def _get_instance_variations(self, instance_type: str) -> list: - """Get common variations of an instance type name. - - Args: - instance_type: Original instance type - - Returns: - List of possible variations - """ - variations = [instance_type] - - # Common transformations - if instance_type.startswith("gpu_1x_"): - # gpu_1x_a100 -> a100, a100_40gb - base = instance_type.replace("gpu_1x_", "") - variations.extend([base, f"{base}_40gb"]) - elif instance_type.startswith("gpu_"): - # gpu_2x_a100 -> 2xa100, 2xa100_40gb - parts = instance_type.split("_") - if len(parts) >= 3: - count = parts[1] # 2x, 4x, etc. - gpu_type = "_".join(parts[2:]) # a100, h100, etc. - variations.extend([f"{count}{gpu_type}", f"{count}{gpu_type}_40gb"]) - else: - # Try adding gpu_1x_ prefix - variations.append(f"gpu_1x_{instance_type}") - - # Add 80GB variants for A100 - if "a100" in instance_type and "80gb" not in instance_type: - for var in variations.copy(): - if "a100" in var: - variations.append(var.replace("a100", "a100_80gb")) - - return variations - - def get_pricing_info(self) -> Dict[str, Any]: - """Get information about the pricing client. - - Returns: - Dictionary with pricing client information - """ - return { - "provider": "lambda", - "authenticated": self.authenticated, - "api_available": self.authenticated, - "fallback_pricing_date": self._hardcoded_pricing_date, - "cache_ttl_hours": 24, - "supported_regions": ["us-east-1", "us-west-1", "us-west-2"], - "instance_count": len(self._hardcoded_pricing), - } diff --git a/docs/source/api/cost_monitoring.rst b/docs/source/api/cost_monitoring.rst deleted file mode 100644 index 9093dfbc..00000000 --- a/docs/source/api/cost_monitoring.rst +++ /dev/null @@ -1,454 +0,0 @@ -Cost Monitoring -=============== - -.. currentmodule:: clustrix.cost_monitoring - -Clustrix provides comprehensive cost monitoring and optimization features for major cloud providers. This module enables automatic cost tracking, resource utilization monitoring, and cost optimization recommendations. - -Overview --------- - -The cost monitoring system supports: - -- **AWS**: EC2 instances, Batch, spot instances -- **Google Cloud**: Compute Engine, preemptible VMs, sustained use discounts -- **Azure**: Virtual Machines, Batch, spot VMs -- **Lambda Cloud**: GPU instances with utilization tracking - -Core Classes ------------- - -ResourceUsage -~~~~~~~~~~~~~ - -.. autoclass:: ResourceUsage - :members: - :undoc-members: - :show-inheritance: - - Data class containing resource utilization metrics. - - **Attributes:** - - - ``cpu_percent``: CPU utilization percentage - - ``memory_used_mb``: Memory usage in MB - - ``memory_total_mb``: Total memory in MB - - ``memory_percent``: Memory utilization percentage - - ``gpu_stats``: Optional GPU utilization data - - ``network_io_mb``: Optional network I/O in MB - - ``disk_io_mb``: Optional disk I/O in MB - -CostEstimate -~~~~~~~~~~~~ - -.. autoclass:: CostEstimate - :members: - :undoc-members: - :show-inheritance: - - Data class containing cost estimation information. - - **Attributes:** - - - ``instance_type``: Cloud instance type - - ``hourly_rate``: Cost per hour in USD - - ``hours_used``: Number of hours used - - ``estimated_cost``: Total estimated cost - - ``currency``: Currency (default: "USD") - - ``last_updated``: Last update timestamp - -CostReport -~~~~~~~~~~ - -.. autoclass:: CostReport - :members: - :undoc-members: - :show-inheritance: - - Comprehensive cost and usage report. - - **Attributes:** - - - ``timestamp``: Report generation time - - ``duration_seconds``: Monitoring duration - - ``resource_usage``: Resource utilization data - - ``cost_estimate``: Cost estimation data - - ``provider``: Cloud provider name - - ``region``: Optional region information - - ``recommendations``: Cost optimization suggestions - - ``metadata``: Additional metadata - -Base Monitor Class ------------------- - -BaseCostMonitor -~~~~~~~~~~~~~~~ - -.. autoclass:: BaseCostMonitor - :members: - :undoc-members: - :show-inheritance: - - Abstract base class for cloud provider cost monitors. - - **Key Methods:** - - - ``get_resource_usage()``: Get current resource utilization - - ``estimate_cost()``: Estimate costs for given usage - - ``get_pricing_info()``: Get current pricing information - - ``start_monitoring()``: Begin cost monitoring session - - ``stop_monitoring()``: End monitoring and generate report. It prices the - elapsed wall-clock time with a hardcoded ``estimate_cost("default", ...)`` - -- it takes no instance type and there is no way to give it one, so the - cost it reports is always the provider's placeholder "default" rate. - -Decorators and Utilities ------------------------- - -cost_tracking_decorator -~~~~~~~~~~~~~~~~~~~~~~~ - -.. autofunction:: cost_tracking_decorator - - Decorator for automatic cost tracking of functions. - - **Parameters:** - - - ``provider``: Cloud provider name ('aws', 'gcp', 'azure', 'lambda') - - ``instance_type``: Recorded, but **not** used to price the run. The - wrapper calls ``monitor.stop_monitoring()``, which prices the elapsed - time with a hardcoded ``estimate_cost("default", ...)``, so the cost in - ``result['cost_report']`` is always the provider's placeholder "default" - rate regardless of what you pass here. The value you passed is echoed - back unchanged as ``result['instance_type']``, and it is the only place - it appears. To price a specific instance type, call - ``get_cost_monitor(provider).estimate_cost(instance_type, hours)`` - yourself. - - **Example:** - - .. code-block:: python - - from clustrix import cost_tracking_decorator, cluster - - @cost_tracking_decorator('aws', 'p3.2xlarge') - @cluster(cores=8, memory='60GB') - def train_model(): - # Your training code here - pass - - # Automatic cost tracking with detailed report - result = train_model() - print(f"Cost: ${result['cost_report']['cost_estimate']['estimated_cost']:.2f}") - -Utility Functions ------------------ - -get_cost_monitor -~~~~~~~~~~~~~~~~ - -.. autofunction:: get_cost_monitor - - Get the appropriate cost monitor for a cloud provider. - - **Parameters:** - - - ``provider``: Cloud provider name - - **Returns:** - - - ``BaseCostMonitor``: Provider-specific cost monitor instance - - **Example:** - - .. code-block:: python - - from clustrix import get_cost_monitor - - monitor = get_cost_monitor('gcp') - cost_estimate = monitor.estimate_cost('n2-standard-4', 2.0) - -start_cost_monitoring -~~~~~~~~~~~~~~~~~~~~~ - -.. autofunction:: start_cost_monitoring - - Start cost monitoring for a specific provider. - - **Parameters:** - - - ``provider``: Cloud provider name - - **Returns:** - - - ``BaseCostMonitor``: Active cost monitor instance - -generate_cost_report -~~~~~~~~~~~~~~~~~~~~ - -.. autofunction:: generate_cost_report - - Build a cost report from the monitor's *current* resource usage. - - The real signature is ``generate_cost_report(provider, instance_type="default")``. - There is no ``duration_seconds`` parameter and no duration override: the - function hardcodes ``monitor.estimate_cost(instance_type, 1.0)``, so the - ``cost_estimate`` it returns is always a **one-hour quote** for - ``instance_type``, not the cost of however long your session has been - running. It also does not stop or reset monitoring. For a figure based on - elapsed time, call ``monitor.stop_monitoring()`` instead -- but see the - caveat under ``cost_tracking_decorator`` about which instance type that - prices. - - **Parameters:** - - - ``provider``: Cloud provider name - - ``instance_type``: Instance type to price for one hour (default: - ``"default"``, the placeholder rate) - - **Returns:** - - - ``dict``: ``timestamp``, ``provider``, ``resource_usage``, - ``cost_estimate`` and ``recommendations``; or ``None`` if the provider is - not supported. - -get_pricing_info -~~~~~~~~~~~~~~~~ - -.. autofunction:: get_pricing_info - - Get pricing information for a cloud provider. - - **Parameters:** - - - ``provider``: Cloud provider name - - **Returns:** - - - ``dict``: Pricing information by instance type - -Cloud Provider Monitors ------------------------ - -Lambda Cloud Monitor -~~~~~~~~~~~~~~~~~~~~ - -.. autoclass:: clustrix.cost_providers.lambda_cloud.LambdaCostMonitor - :members: - :undoc-members: - :show-inheritance: - - Cost monitoring for Lambda Cloud GPU instances. - - **Features:** - - - Real-time GPU utilization monitoring - - Accurate pricing for all Lambda instance types - - Instance recommendations based on usage patterns - - Monthly cost estimation tools - -AWS Cost Monitor -~~~~~~~~~~~~~~~~ - -.. autoclass:: clustrix.cost_providers.aws.AWSCostMonitor - :members: - :undoc-members: - :show-inheritance: - - Cost monitoring for AWS EC2 and Batch services. - - **Features:** - - - On-demand and spot instance pricing - - AWS Batch cost estimation - - Regional pricing comparisons - - Reserved instance recommendations - -Azure Cost Monitor -~~~~~~~~~~~~~~~~~~ - -.. autoclass:: clustrix.cost_providers.azure.AzureCostMonitor - :members: - :undoc-members: - :show-inheritance: - - Cost monitoring for Azure Virtual Machines and Batch. - - **Features:** - - - Pay-as-you-go and spot VM pricing - - Azure Batch cost estimation - - Regional pricing analysis - - Cost optimization recommendations - -GCP Cost Monitor -~~~~~~~~~~~~~~~~ - -.. autoclass:: clustrix.cost_providers.gcp.GCPCostMonitor - :members: - :undoc-members: - :show-inheritance: - - Cost monitoring for Google Cloud Compute Engine. - - **Features:** - - - On-demand and preemptible instance pricing - - Sustained use discount calculations - - Regional pricing comparisons - - Google Cloud Batch cost estimation - -Usage Examples --------------- - -Basic Cost Monitoring -~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - from clustrix import get_cost_monitor - - # Get AWS cost monitor - monitor = get_cost_monitor('aws') - - # Estimate costs - cost_estimate = monitor.estimate_cost('p3.2xlarge', hours_used=2.0) - print(f"Cost: ${cost_estimate.estimated_cost:.2f}") - - # Get current resource usage - usage = monitor.get_resource_usage() - print(f"CPU: {usage.cpu_percent}%, Memory: {usage.memory_percent}%") - -Automatic Cost Tracking -~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - from clustrix import cost_tracking_decorator, cluster - - @cost_tracking_decorator('gcp', 'n2-standard-8') - @cluster(cores=8, memory='32GB') - def data_processing(): - # Your data processing code - import pandas as pd - df = pd.read_csv('large_dataset.csv') - return df.groupby('category').sum() - - # Execute with automatic cost tracking - result = data_processing() - if result['success']: - print(f"Processing completed successfully") - print(f"Estimated cost: ${result['cost_report']['cost_estimate']['estimated_cost']:.2f}") - print(f"Duration: {result['cost_report']['duration_seconds']:.1f} seconds") - -Manual Session Monitoring -~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - from clustrix import start_cost_monitoring, generate_cost_report - - # Start monitoring - monitor = start_cost_monitoring('azure') - - # Run your workload - # ... your code here ... - - # Generate report. Despite the name, this is not the cost of the session - # so far: generate_cost_report hardcodes a 1.0-hour estimate, so the figure - # below is a one-hour quote for Standard_NC6s_v3. The resource_usage in the - # same report *is* current. - report = generate_cost_report('azure', 'Standard_NC6s_v3') - print(f"One-hour quote: ${report['cost_estimate']['estimated_cost']:.2f}") - -Cost Optimization -~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - from clustrix import get_cost_monitor - - monitor = get_cost_monitor('aws') - - # Get pricing information - pricing = monitor.get_pricing_info() - - # Compare spot vs on-demand pricing - on_demand = monitor.estimate_cost('p3.2xlarge', 1.0, use_spot=False) - spot = monitor.estimate_cost('p3.2xlarge', 1.0, use_spot=True) - - savings = ((on_demand.hourly_rate - spot.hourly_rate) / on_demand.hourly_rate) * 100 - print(f"Spot instance savings: {savings:.1f}%") - -Regional Pricing Comparison -~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - monitor = get_cost_monitor('gcp') - - # Compare pricing across regions - regional_pricing = monitor.get_region_pricing_comparison('n2-standard-4') - - for region, pricing in regional_pricing.items(): - print(f"{region}: ${pricing['on_demand_hourly']:.3f}/hour") - -Error Handling --------------- - -The cost monitoring system includes robust error handling: - -.. code-block:: python - - from clustrix import get_cost_monitor - - # An unsupported provider logs a warning and returns None -- it does not - # raise, so check the result before using it. - monitor = get_cost_monitor('unsupported_provider') - if monitor is None: - print("Provider not supported") - - # An unrecognised instance type does not raise either. It is priced at a - # placeholder "default" rate, and says so in pricing_warning. - monitor = get_cost_monitor('aws') - cost_estimate = monitor.estimate_cost('invalid_instance', 1.0) - if cost_estimate.pricing_warning: - print(f"Estimate is not reliable: {cost_estimate.pricing_warning}") - - # pricing_warning is NOT a complete guard, and pricing_source is not - # trustworthy either. For a *recognised* instance type, the provider - # monitor calls the pricing client, and the pricing client falls back to - # its own hardcoded table internally when the live API is unavailable. The - # monitor only sees "a number came back", so it labels the record - # pricing_source="api" and leaves pricing_warning=None -- even though the - # figure came from the same stale table. Verified on a machine with no AWS - # credentials: estimate_cost('p3.2xlarge', 1.0) returns hourly_rate 3.06, - # pricing_source 'api', pricing_warning None, while the logger emits - # "Using hardcoded pricing for p3.2xlarge (last updated: 2025-01-01)". - # The only reliable signal that fallback pricing was used is that log - # record, so enable logging if the distinction matters: - import logging - logging.getLogger('clustrix.pricing_clients.base').setLevel(logging.WARNING) - -Best Practices --------------- - -1. **Use Decorators**: For automatic tracking of cluster functions -2. **Monitor Long Jobs**: Use manual monitoring for jobs over 1 hour -3. **Check Recommendations**: Review cost optimization suggestions regularly -4. **Compare Pricing**: Use regional and instance type comparisons -5. **Track Trends**: Save reports to analyze cost trends over time - -Notes ------ - -- Cost estimates fall back to a hardcoded price table when a live pricing API - is unavailable. That table is a snapshot, not live pricing: the AWS, Azure - and GCP tables in ``clustrix/pricing_clients/*_pricing.py`` are dated - ``2025-01-01`` and the Lambda Cloud one ``2025-01-08`` - (``_hardcoded_pricing_date``). Treat every figure as an order-of-magnitude - guide, not a quote. -- Resource utilization requires appropriate permissions on the target system -- GPU monitoring requires ``nvidia-smi`` on the target system -- Some cloud providers may have rate limits on pricing API calls -- Spot/preemptible instance availability and pricing can change frequently \ No newline at end of file diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index 9eeda651..c939373b 100644 --- a/docs/source/configuration.rst +++ b/docs/source/configuration.rst @@ -120,15 +120,17 @@ Choosing a backend - Effect * - ``cluster_type`` - ``"slurm"`` - - One of ``local``, ``ssh``, ``slurm``, ``pbs``, ``sge``, ``kubernetes``, - ``huggingface`` (``SUPPORTED_CLUSTER_TYPES``). Anything else raises + - One of ``local``, ``ssh``, ``slurm``, ``huggingface`` + (``SUPPORTED_CLUSTER_TYPES``). Anything else raises ``ValueError: Unsupported cluster type: ...`` at submit time. Note the default is ``slurm``, but with no ``cluster_host`` set the decorator - still runs locally -- see :ref:`execution-model`. + still runs locally -- see :ref:`execution-model`. PBS, SGE, Kubernetes + and the cloud VM providers were removed in v0.2.0; see + :ref:`removed-backends`. * - ``cluster_host`` - ``None`` - The SSH host. **Its absence is what makes execution local** for every - backend except ``huggingface`` and auto-provisioned Kubernetes. + backend except ``huggingface``. * - ``cluster_port`` - ``22`` - Port passed to paramiko. @@ -209,17 +211,13 @@ Resources - ``--cpus-per-task`` / ``ppn`` / ``-pe``, and the local process-pool size. * - ``default_memory`` - ``"8GB"`` - - Rewritten per scheduler by ``normalize_memory``: ``8G`` for SLURM, - ``8gb`` for PBS/SGE, ``8GB`` for Kubernetes. + - Rewritten per scheduler by ``normalize_memory``: ``8G`` for SLURM. * - ``default_time`` - ``"01:00:00"`` - Wall-clock limit directive. * - ``default_partition`` - ``None`` - ``#SBATCH --partition``. Omitted when unset. - * - ``default_queue`` - - ``None`` - - ``#PBS -q`` / ``#$ -q``. Omitted when unset. * - ``max_parallel_jobs`` - ``100`` - Upper bound on the number of chunks ``_execute_parallel`` splits a @@ -240,7 +238,7 @@ Paths and the remote environment - ``"~/.clustrix/jobs"`` - Parent of every job directory. A leading ``~/`` is expanded against the remote ``$HOME`` before SFTP touches it. Home-relative rather than - ``/tmp`` on purpose: on SLURM/PBS/SGE a compute node has its own + ``/tmp`` on purpose: on SLURM a compute node has its own ``/tmp``, so an environment built on the login node is simply absent at run time and the job dies with exit 127 before writing diagnostics. * - ``local_work_dir`` @@ -348,32 +346,6 @@ Execution behaviour Backend-specific settings ------------------------- -Kubernetes -~~~~~~~~~~ - -``k8s_namespace`` (``"default"``), ``k8s_image`` (``"python:3.11-slim"``), -``k8s_service_account`` (``None``), ``k8s_pull_policy`` (``"IfNotPresent"``), -``k8s_job_ttl_seconds`` (``3600``), ``k8s_backoff_limit`` (``3``). - -``@cluster`` accepts ``k8s_namespace``, ``k8s_image``, -``k8s_service_account`` and ``k8s_pull_policy`` as keyword arguments and puts -them in ``job_config`` -- but ``KubernetesJobManager.submit_k8s_job`` reads -only ``self.config.k8s_*``, so **those per-call values have no effect**. The -only ``job_config`` keys this backend reads are ``cores`` and ``memory``. Set -the ``k8s_*`` fields through configuration instead. - -The container installs only ``cloudpickle`` and ``dill``: -**``replicate_local_environment`` and ``cluster_packages`` are not honoured by -this backend.** Pick an image that already contains what your function -imports. - -Auto-provisioning fields -- ``auto_provision_k8s`` (``False``), -``k8s_provider`` (``"aws"``), ``k8s_from_scratch`` (``True``), -``k8s_auto_cleanup`` (``True``), ``k8s_cluster_name``, ``k8s_node_count`` -(``2``), ``k8s_node_type``, ``k8s_version`` (``"1.28"``), ``k8s_region`` -- -drive cluster creation. ``k8s_remote`` (``False``) is read by the notebook -widget only. - HuggingFace Jobs (``cluster_type="huggingface"``) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -429,27 +401,6 @@ cache), so passing them per call has no effect on this backend. hf_allow_gpu_flavors=True to confirm you intend to pay for it; otherwise use a CPU flavor (default: cpu-basic). -Cloud VM providers -~~~~~~~~~~~~~~~~~~ - -``aws_*``, ``azure_*``, ``gcp_*``, ``lambda_*``, ``cloud_provider`` -(``"manual"``), ``cloud_region``, ``cloud_auto_configure`` (``False``) feed the -``provider=`` routing and the pricing clients. The pricing and cost-estimation -clients work. The VM *execution* backends have never been shown to run a job -end to end; see :doc:`limitations`. - -Both boto3-style and widget-style AWS names are accepted -(``aws_access_key_id``/``aws_access_key``, ``aws_secret_access_key``/ -``aws_secret_key``) and reconciled by ``clustrix.field_mappings``. - -The following can also be passed per call to ``@cluster``: ``lambda_api_key``, -``aws_access_key_id``, ``aws_secret_access_key``, ``aws_region``, -``azure_subscription_id``, ``azure_tenant_id``, ``azure_client_id``, -``azure_client_secret``, ``gcp_project_id``, ``gcp_service_account_key``, -``key_file``, ``terminate_on_completion``, ``instance_startup_timeout``. -Anything else is warned about and ignored. - - Settings that currently have no effect -------------------------------------- @@ -482,13 +433,8 @@ Field Status ``cache_credentials`` Not read. ``credential_cache_ttl`` Not read. ``local_cache_dir`` Not read. -``k8s_service_account`` Not read by the executor. -``k8s_pull_policy`` Not read by the executor. -``k8s_auto_cleanup`` Not read by the executor. -``cost_monitoring`` Not read. -``k8s_remote`` Notebook widget only. -``hf_sdk`` / ``hf_hardware`` Spaces-era fields. ``hf_hardware`` survives only - as a fallback for ``hf_flavor``. +``hf_hardware`` A Spaces-era field. It survives only as a + fallback for ``hf_flavor``. ``venv_info`` Runtime scratch space, written by clustrix during a submission. Do not set it yourself. ============================ =========================================== diff --git a/docs/source/index.rst b/docs/source/index.rst index e7259fe7..519923d0 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -45,8 +45,10 @@ Start here - :doc:`installation` -- install it, with the optional extras. - :doc:`quickstart` -- a real result in five minutes, beginning with a backend that needs no cluster at all. -- :ref:`supported-cluster-types` -- **read this before depending on a - backend.** They are not equally proven. +- :ref:`supported-cluster-types` -- the four backends Clustrix supports, and + the evidence that each one runs a real job. +- :ref:`removed-backends` -- if you are looking for PBS, SGE, Kubernetes or a + cloud VM provider, start here. Features -------- @@ -56,12 +58,11 @@ Features cloudpickle, so closures, nested functions and project-local modules travel with it -- source code is not required - **Interactive Jupyter Widget**: ``%%remote`` magic command with GUI configuration manager -- **Multiple Cluster Backends**: SLURM, SSH and HuggingFace Jobs are verified working; - PBS, SGE and Kubernetes are implemented but untested. See - :ref:`supported-cluster-types` before relying on a backend. +- **Multiple Cluster Backends**: SLURM, SSH, HuggingFace Jobs and local + execution. Every backend Clustrix ships has been run end to end -- see + :ref:`supported-cluster-types`. - **Unified Filesystem Utilities**: Work with files seamlessly across local and remote clusters - **Shared Storage Optimization**: Automatic detection and optimization for HPC shared filesystems -- **Cost Estimation**: Pricing and cost estimates for AWS, GCP, Azure, and Lambda Cloud - **Automatic Dependency Management**: Captures and replicates your exact Python environment - **Loop Parallelization**: distributes a loop across nodes when its body has no dependencies between iterations. The analysis is deliberately @@ -114,22 +115,20 @@ variables, module loads and pre-execution commands: **What the widget covers** -The cluster type dropdown offers ``local``, ``ssh``, ``slurm``, ``pbs``, -``sge``, ``kubernetes`` and ``huggingface``. +The cluster type dropdown offers ``local``, ``ssh``, ``slurm`` and +``huggingface`` -- the same four values as +:data:`clustrix.config.SUPPORTED_CLUSTER_TYPES`. -- ``ssh``, ``slurm``, ``pbs`` and ``sge`` show the connection section: host, - port, username, SSH key file, password, remote work directory, an environment - variable to read the password from, and an "Auto setup SSH keys" button. +- ``ssh`` and ``slurm`` show the connection section: host, port, username, SSH + key file, password, remote work directory, an environment variable to read + the password from, and an "Auto setup SSH keys" button. - ``huggingface`` shows namespace, flavor, token and an "Allow paid GPU flavors" checkbox. GPU flavors bill by the second, so that box has to be ticked before one is accepted. -- ``kubernetes`` shows a Kubernetes section: namespace, image, service account - and image pull policy. The remaining ``k8s_*`` settings (node count, region, - provider, auto-provisioning) are configuration-file or - ``clustrix.configure()`` only. +- ``local`` needs no connection settings at all. -There are no AWS, GCP, Azure or Lambda Cloud entries, because those execution -backends are unverified. +There are no PBS, SGE, Kubernetes, AWS, GCP, Azure or Lambda Cloud entries. +Those backends were removed in v0.2.0; see :ref:`removed-backends`. Table of Contents ----------------- @@ -159,8 +158,6 @@ Table of Contents tutorials/usage_patterns tutorials/filesystem_tutorial tutorials/slurm_tutorial - tutorials/pbs_tutorial - tutorials/kubernetes_tutorial .. toctree:: :maxdepth: 2 @@ -170,30 +167,9 @@ Table of Contents notebooks/cluster_config_example notebooks/complete_api_demo notebooks/slurm_tutorial - notebooks/pbs_tutorial - notebooks/sge_tutorial - notebooks/kubernetes_tutorial notebooks/ssh_tutorial notebooks/basic_usage -.. warning:: - - The cloud VM tutorials below (AWS, Azure, GCP, HuggingFace Spaces, Lambda - Cloud) describe an execution path that has never been shown to run a job end - to end. See :ref:`supported-cluster-types`. The cost monitoring tutorial is - unaffected. - -.. toctree:: - :maxdepth: 2 - :caption: Cloud Platform Tutorials - - notebooks/aws_cloud_tutorial - notebooks/azure_cloud_tutorial - notebooks/gcp_cloud_tutorial - notebooks/huggingface_spaces_tutorial - notebooks/lambda_cloud_tutorial - notebooks/cost_monitoring_tutorial - .. toctree:: :maxdepth: 2 :caption: API Reference @@ -204,7 +180,6 @@ Table of Contents api/file_packaging api/config api/notebook_magic - api/cost_monitoring api/local_executor .. _supported-cluster-types: @@ -214,6 +189,10 @@ Supported Cluster Types **Execution backends** +Clustrix supports exactly four ``cluster_type`` values -- the contents of +:data:`clustrix.config.SUPPORTED_CLUSTER_TYPES`, which is also what the CLI and +the notebook widget offer. There are no others. + +--------------------+-------------------+--------------------------------------------------+ | ``cluster_type`` | Status | Notes | +====================+===================+==================================================+ @@ -228,37 +207,20 @@ Supported Cluster Types | ``local`` | Works | Local processes; used for development and the | | | | fast tests. | +--------------------+-------------------+--------------------------------------------------+ -| ``pbs`` | Untested | Shares SLURM's environment-setup path, so it | -| | | builds the same two-venv environment -- but no | -| | | job has run against a real PBS scheduler. | -+--------------------+-------------------+--------------------------------------------------+ -| ``sge`` | Untested | Same caveat as PBS. | -+--------------------+-------------------+--------------------------------------------------+ -| ``kubernetes`` | Untested | Not verified against a real cluster. Per-job | -| | | overrides are unsupported: the executor reads | -| | | only configuration-level ``k8s_*`` settings. | -+--------------------+-------------------+--------------------------------------------------+ -**Cloud VM backends** - -The ``provider=`` argument to ``@cluster`` (``'aws'``, ``'gcp'``, ``'azure'``, -``'lambda'``, ``'huggingface'``) routes to the AWS EC2, Azure VM, Google -Compute Engine and Lambda Cloud backends. **None of them has been shown to run -a job end to end.** Until recently the path could not have run at all: the -serializer writes the function under a ``"function"`` key while the remote -bootstrap read ``"func"``, so every cloud job died with a ``KeyError`` on its -first line. That was fixed (issue #119), but nothing has since demonstrated a -completed cloud job, and ``scripts/collect_execution_evidence.py`` does not -cover these backends. Treat the cloud platform tutorials listed above as a -description of the intended interface. - -Note that ``cluster_type='huggingface'`` (HuggingFace Jobs) is a different -thing from ``provider='huggingface'`` (the HuggingFace Spaces provider, which -never satisfied the dispatch interface). Use the former. - -The pricing and cost-estimation clients for AWS, GCP, Azure and Lambda Cloud -are separate code and do work; they query provider pricing APIs and never -submit a job. See :doc:`api/cost_monitoring`. +Note that ``cluster_type='huggingface'`` means HuggingFace *Jobs*. The separate +HuggingFace *Spaces* provider was removed in v0.2.0 along with the other +unverified backends; see :ref:`removed-backends`. + +.. _removed-backends-pointer: + +**Backends that were removed** + +PBS, SGE, Kubernetes, AWS, GCP, Azure and Lambda Cloud were implemented but +never shown to run a job end to end, and were removed in v0.2.0 rather than +shipped as if they worked. The cost-monitoring and cloud pricing APIs went with +them. Each has a tracking issue and is planned for a future release -- +:ref:`removed-backends` has the details and the links. **Evidence** diff --git a/docs/source/limitations.rst b/docs/source/limitations.rst index 505b7885..0d61de6a 100644 --- a/docs/source/limitations.rst +++ b/docs/source/limitations.rst @@ -439,40 +439,72 @@ write its own output to a durable location -- a file on the cluster, a database -- and return only a path or a summary. -Unverified backends -------------------- - -Only ``slurm``, ``ssh`` and ``huggingface`` have been demonstrated running a -real job end to end (``scripts/collect_execution_evidence.py``). ``local`` -works and is exercised by the test suite. The rest are implemented but -unverified: - -================== =========================================================== -Backend Caveat -================== =========================================================== -``pbs`` Never run against real hardware. It now shares the staging - and environment setup the other schedulers use; previously - it ran ``python execute_function.py``, a file nothing in - clustrix has ever created. -``sge`` Never run against real hardware. -``kubernetes`` Never verified against a real cluster. Additionally it does - **not** replicate your environment: the container installs - only ``cloudpickle`` and ``dill``, so everything else your - function imports must already be in ``k8s_image``. Results - come back through the pod log, which means a very large - result is at the mercy of log retention. -Cloud VM providers Every ``provider=`` backend (``aws``, ``gcp``, ``azure``, - ``lambda``, and ``provider="huggingface"``, which is the - Spaces provider, not HuggingFace Jobs) is unverified end to - end. Until recently the path could not have worked at all: - the serializer writes the function under a ``"function"`` - key while the remote bootstrap read ``"func"``. That was - fixed (issue #119), but nothing has since demonstrated a - completed cloud job. -================== =========================================================== - -Use ``cluster_type="huggingface"`` (HuggingFace Jobs), not -``provider="huggingface"`` (Spaces). +.. _removed-backends: + +Backends removed in v0.2.0 +-------------------------- + +Clustrix once shipped seven more execution backends. All seven were implemented +in full, and not one of them had ever been shown to run a job end to end +against real hardware. Rather than keep publishing them as if they worked, they +were removed in v0.2.0. + +Nothing about them was deprecated gently first, and that is deliberate: a +backend that has never completed a job is not a feature with rough edges, it is +an untested code path with a plausible-looking API in front of it. The failure +mode is that you write against it, it appears to submit, and you find out much +later that no result was ever produced. + +Each removed backend has a tracking issue. They are planned for a future +release, and the gate for each one is the same as the gate the surviving +backends already passed: a real job, on real hardware, whose result comes back +and is checked in as evidence. + +================= ============= ==================================================== +Backend Issue What it was +================= ============= ==================================================== +PBS `#140`_ ``cluster_type="pbs"`` -- the PBS/Torque scheduler. +SGE `#141`_ ``cluster_type="sge"`` -- Sun/Son of Grid Engine. +Kubernetes `#142`_ ``cluster_type="kubernetes"``, the ``k8s_*`` + settings, and cluster auto-provisioning. +AWS `#143`_ ``provider="aws"`` -- EC2 and EKS. +GCP `#144`_ ``provider="gcp"`` -- Google Compute Engine. +Azure `#145`_ ``provider="azure"`` -- Azure VMs. +Lambda Cloud `#146`_ ``provider="lambda"`` -- Lambda Labs GPU cloud. +================= ============= ==================================================== + +.. _#140: https://github.com/ContextLab/clustrix/issues/140 +.. _#141: https://github.com/ContextLab/clustrix/issues/141 +.. _#142: https://github.com/ContextLab/clustrix/issues/142 +.. _#143: https://github.com/ContextLab/clustrix/issues/143 +.. _#144: https://github.com/ContextLab/clustrix/issues/144 +.. _#145: https://github.com/ContextLab/clustrix/issues/145 +.. _#146: https://github.com/ContextLab/clustrix/issues/146 + +Two more things went with them: + +* **The HuggingFace Spaces provider** (``provider="huggingface"``). This is a + different thing from ``cluster_type="huggingface"``, which is HuggingFace + **Jobs** and is verified working and fully supported. Only Spaces was + removed. +* **The cost monitoring and cloud pricing API** -- + ``cost_tracking_decorator``, ``get_cost_monitor``, ``start_cost_monitoring``, + ``generate_cost_report`` and ``get_pricing_info``. These estimated the cost + of running on the cloud VM backends, so with those backends gone the API had + nothing left to price. + +What to do instead +~~~~~~~~~~~~~~~~~~ + +* **PBS or SGE**: no direct substitute in Clustrix today. Follow `#140`_ / + `#141`_. If your site also runs SLURM, ``cluster_type="slurm"`` is verified. +* **Kubernetes**: no substitute. Follow `#142`_. +* **A cloud GPU**: ``cluster_type="huggingface"`` submits to HuggingFace Jobs, + which runs your function in a container on rented GPUs and is verified end to + end. Otherwise, bring up a VM yourself and use ``cluster_type="ssh"``, which + is also verified. +* **Cost estimates**: use your provider's own pricing calculator. Clustrix no + longer ships one. Windows clients: config and credential files are not permission-restricted @@ -541,21 +573,14 @@ Smaller sharp edges keys *are* validated and will refuse metacharacters. * **``cores=0`` falls back to the default.** The merge is written as ``cores or config.default_cores``, so any falsy value takes the default. -* **``@cluster`` mutates global configuration.** Passing ``platform=``, - ``auto_provision=``, ``cluster_name=``, ``node_count=``, ``node_type=``, - ``kubernetes_version=`` or ``from_scratch=`` writes the corresponding field - onto the shared ``ClusterConfig``, where it stays for every later call. * **Unknown ``@cluster`` keywords are warned about, not rejected**, and only on the first call -- so a typo in a keyword name is easy to miss if you are not watching the log. * **Some recognised ``@cluster`` keywords are still ignored by their backend.** - ``k8s_namespace``, ``k8s_image``, ``k8s_service_account`` and - ``k8s_pull_policy`` are accepted and placed in ``job_config``, but - ``KubernetesJobManager`` reads only ``self.config.k8s_*``. Likewise - ``hf_namespace``, ``hf_token`` and ``hf_username`` are accepted but - ``HFJobsManager`` resolves them from configuration. These produce no warning, - because the keywords *are* on the recognised list. Set them through - ``clustrix.configure()``. + ``hf_namespace``, ``hf_token`` and ``hf_username`` are accepted and placed in + ``job_config``, but ``HFJobsManager`` resolves them from configuration + instead. This produces no warning, because the keywords *are* on the + recognised list. Set them through ``clustrix.configure()``. See also diff --git a/docs/source/notebooks/aws_cloud_tutorial.ipynb b/docs/source/notebooks/aws_cloud_tutorial.ipynb deleted file mode 100644 index f3055c6b..00000000 --- a/docs/source/notebooks/aws_cloud_tutorial.ipynb +++ /dev/null @@ -1,1196 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "fbe29e03", - "metadata": {}, - "source": [ - "> **These backends are unverified.**\n", - ">\n", - "> No clustrix cloud VM job (`provider=\"aws\"`, `\"gcp\"`, `\"azure\"`, `\"lambda\"`, `\"huggingface\"`) has been shown to run end to end. Until recently the path could not have run at all: every cloud job died with a `KeyError` on its first line. That was fixed (issue #119), but nothing has since demonstrated a completed cloud job, and `scripts/collect_execution_evidence.py` does not cover these backends. This notebook describes the intended interface, not something that has been run.\n", - ">\n", - "> The backends that are verified working are `cluster_type=\"slurm\"`, `cluster_type=\"ssh\"` and `cluster_type=\"huggingface\"` (HuggingFace Jobs, which is a different thing from the HuggingFace Spaces provider described here). See the Supported Cluster Types section of the documentation.\n" - ] - }, - { - "cell_type": "markdown", - "id": "cff5bcb0", - "metadata": {}, - "source": [ - "> **What actually happens if you try `@cluster(provider=\"aws\", ...)`.**\n", - ">\n", - "> Clustrix's own cloud-VM auto-provisioning (`CloudJobManager.submit_cloud_job`, in `clustrix/executor_cloud.py`) only works end to end for `provider=\"lambda\"` -- it is the only built-in provider whose class implements `create_instance()`. For `provider=\"aws\"`, submission checks this at *submit time* and raises `NotImplementedError` naming the provider, before any thread, instance, or SSH connection is created:\n", - ">\n", - "> ```\n", - "> The 'aws' cloud provider cannot run clustrix jobs: AWSProvider does\n", - "> not implement create_instance, ... Of the built-in providers only 'lambda'\n", - "> provisions instances for job execution; for the others, provision the machine\n", - "> yourself and use cluster_type 'ssh', or use cluster_type 'kubernetes'.\n", - "> ```\n", - ">\n", - "> That is exactly the pattern this notebook follows: the examples below provision a VM using the AWS CLI / boto3, then point Clustrix's `cluster_type=\"ssh\"` (or `\"slurm\"`) at it directly -- the same transport used by any other SSH/SLURM cluster in these docs, just running on a cloud box instead of an on-prem one. That exercises the SSH/SLURM backend, not a demonstrated run on this specific cloud, and no such run has been recorded for any of these providers.\n", - ">\n", - "> One nuance specific to `AWSProvider`: unlike Azure/GCP/Lambda Cloud (all fixed under #119 to raise `RuntimeError` instead of returning a fake `placeholder.*.com` host when a VM's connection details can't be determined yet), `AWSProvider.get_cluster_config()` for an EC2 instance still returns `cluster_host: \"\"` (via `instance.get(\"PublicIpAddress\", \"\")`) if the instance has no public IP yet, with no exception raised. In practice that code path is unreachable through `@cluster(provider=\"aws\", ...)` -- the `create_instance` check above stops submission first -- so it only matters if you call `AWSProvider().get_cluster_config(...)` directly." - ] - }, - { - "cell_type": "markdown", - "id": "4599a19d", - "metadata": {}, - "source": [ - "**Behind the scenes, once you're actually calling `@cluster`:** every\n", - "example below that runs (as opposed to just printing setup commands) ends up\n", - "configured with `cluster_type=\"ssh\"` against a VM you provisioned yourself --\n", - "that is the verified SSH backend, following the same order of operations as\n", - "any other SSH cluster in these docs: read `get_config()`, merge decorator\n", - "args over config defaults, choose local vs. remote (`cluster_host` is set,\n", - "so remote), serialize the function with `dill`/`cloudpickle`, create the\n", - "remote job directory, upload the payload over SFTP, build the remote venv,\n", - "generate and run a job script, poll for completion, then download and\n", - "HMAC-verify `result.pkl`. None of that is AWS (EC2/ParallelCluster)-specific -- clustrix\n", - "does not talk to the AWS (EC2/ParallelCluster) API at any point in that path; AWS (EC2/ParallelCluster)\n", - "only matters for how the VM itself got created, which is everything *before*\n", - "`configure(cluster_type=\"ssh\", ...)` in this notebook. Full, source-verified\n", - "detail: :ref:`execution-model`. Every `ClusterConfig` field used below is\n", - "documented in :ref:`configuration`.\n", - "\n", - "**Real resources, real charges.** The functions and CLI snippets below that\n", - "create VMs, networks, security groups, or managed clusters call real\n", - "AWS (EC2/ParallelCluster) APIs (or print commands meant to be copy-pasted into a real\n", - "AWS (EC2/ParallelCluster) CLI). None of them run automatically in this notebook -- every\n", - "invocation is commented out -- but if you uncomment one, or copy a printed\n", - "command into your terminal, it creates billed resources in your account.\n", - "Read each cell before running or copying it, and see the cleanup cell near\n", - "the end before you walk away." - ] - }, - { - "cell_type": "markdown", - "id": "aws-title", - "metadata": {}, - "source": [ - "# Amazon Web Services (AWS) Cloud Tutorial\n", - "\n", - "This tutorial demonstrates how to use Clustrix with Amazon Web Services (AWS) cloud infrastructure for scalable distributed computing.\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/aws_cloud_tutorial.ipynb)\n", - "\n", - "## Overview\n", - "\n", - "AWS provides several services that work well with Clustrix:\n", - "\n", - "- **EC2**: Virtual machines for compute clusters\n", - "- **AWS Batch**: Managed job scheduling service\n", - "- **ECS**: Container orchestration\n", - "- **ParallelCluster**: HPC cluster management\n", - "- **S3**: Object storage for data and results\n", - "- **VPC**: Network isolation and security\n", - "\n", - "## Prerequisites\n", - "\n", - "Before starting this tutorial, ensure you have:\n", - "\n", - "1. **AWS Account**: Active AWS account with billing enabled\n", - "2. **AWS CLI**: Installed and configured on your local machine\n", - "3. **SSH Key Pair**: Generated and uploaded to AWS EC2 for secure access\n", - "4. **IAM Permissions**: Appropriate permissions for EC2, S3, and other services\n", - "5. **Basic AWS Knowledge**: Understanding of AWS services, regions, and availability zones\n", - "6. **Python Environment**: Python 3.10+ with pip installed\n", - "\n", - "## Complete AWS Setup Guide\n", - "\n", - "### Step 1: Create AWS Account\n", - "1. Go to [aws.amazon.com](https://aws.amazon.com) and create an account\n", - "2. Verify your email and provide payment information\n", - "3. Choose the Basic Support plan (free)\n", - "\n", - "### Step 2: Install AWS CLI\n", - "```bash\n", - "# On macOS\n", - "brew install awscli\n", - "\n", - "# On Linux/WSL\n", - "curl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\n", - "unzip awscliv2.zip\n", - "sudo ./aws/install\n", - "\n", - "# On Windows\n", - "# Download and run the AWS CLI MSI installer from AWS documentation\n", - "```\n", - "\n", - "### Step 3: Create IAM User and Access Keys\n", - "1. Go to AWS Console → IAM → Users → Create User\n", - "2. Create a user with programmatic access\n", - "3. Attach policies: `AmazonEC2FullAccess`, `AmazonS3FullAccess`, `IAMReadOnlyAccess`\n", - "4. Save the Access Key ID and Secret Access Key securely\n", - "\n", - "### Step 4: Generate SSH Key Pair\n", - "```bash\n", - "# Generate SSH key pair locally\n", - "ssh-keygen -t rsa -b 4096 -f ~/.ssh/aws-clustrix-key\n", - "\n", - "# Import public key to AWS\n", - "aws ec2 import-key-pair --key-name \"clustrix-key\" --public-key-material fileb://~/.ssh/aws-clustrix-key.pub\n", - "```" - ] - }, - { - "cell_type": "markdown", - "id": "installation", - "metadata": {}, - "source": [ - "## Installation and Setup\n", - "\n", - "Install Clustrix with AWS dependencies:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "install", - "metadata": {}, - "outputs": [], - "source": [ - "# Install Clustrix with AWS support\n", - "!pip install clustrix boto3 awscli\n", - "\n", - "# Import required libraries\n", - "import clustrix\n", - "from clustrix import cluster, configure\n", - "import boto3\n", - "import os\n", - "import numpy as np\n", - "import time" - ] - }, - { - "cell_type": "markdown", - "id": "aws-credentials", - "metadata": {}, - "source": [ - "## AWS Credentials Configuration\n", - "\n", - "Configure your AWS credentials using one of the following methods:\n", - "\n", - "### Option 1: AWS CLI Configuration (Recommended)\n", - "\n", - "Run the following command in your terminal to configure credentials interactively:\n", - "\n", - "```bash\n", - "aws configure\n", - "```\n", - "\n", - "You'll be prompted to enter:\n", - "- AWS Access Key ID\n", - "- AWS Secret Access Key \n", - "- Default region name (e.g., us-east-1)\n", - "- Default output format (json)\n", - "\n", - "This creates credential files at `~/.aws/credentials` and `~/.aws/config`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "aws-config", - "metadata": {}, - "outputs": [], - "source": [ - "# Configure AWS CLI (run this in terminal)\n", - "# aws configure\n", - "\n", - "# Verify configuration\n", - "!aws sts get-caller-identity" - ] - }, - { - "cell_type": "markdown", - "id": "aws-creds-env", - "metadata": {}, - "source": [ - "### Option 2: Environment Variables" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "env-vars", - "metadata": {}, - "outputs": [], - "source": [ - "# Option 2: Set AWS credentials as environment variables (if needed)\n", - "# os.environ['AWS_ACCESS_KEY_ID'] = 'your-access-key'\n", - "# os.environ['AWS_SECRET_ACCESS_KEY'] = 'your-secret-key'\n", - "# os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'\n", - "\n", - "# Test AWS connection\n", - "try:\n", - " ec2 = boto3.client('ec2')\n", - " regions = ec2.describe_regions()\n", - " print(f\"✓ Successfully connected to AWS. Available regions: {len(regions['Regions'])}\")\n", - "except Exception as e:\n", - " print(f\"✗ AWS connection failed: {e}\")" - ] - }, - { - "cell_type": "markdown", - "id": "ec2-setup", - "metadata": {}, - "source": [ - "## Method 1: Direct EC2 Instance Configuration\n", - "\n", - "### Prerequisites: Create Security Group\n", - "\n", - "Before launching an EC2 instance, you need to create a security group that allows SSH access. You can do this through the AWS Console or use the function provided in the Security section below.\n", - "\n", - "**Quick Setup via AWS Console:**\n", - "1. Go to EC2 → Security Groups → Create Security Group\n", - "2. Name: `clustrix-sg`\n", - "3. Add inbound rule: SSH (port 22) from your IP address only\n", - "4. Note the Security Group ID (sg-xxxxxxxxx)\n", - "\n", - "### Launch EC2 Instance for Clustrix\n", - "\n", - "This example shows how to programmatically launch an EC2 instance suitable for Clustrix:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ec2-launch", - "metadata": {}, - "outputs": [], - "source": [ - "def launch_clustrix_ec2_instance(key_name, security_group_id, instance_type='t3.large'):\n", - " \"\"\"\n", - " Launch an EC2 instance configured for Clustrix.\n", - " \n", - " Args:\n", - " key_name: Name of your EC2 key pair\n", - " security_group_id: Security group ID that allows SSH access\n", - " instance_type: EC2 instance type\n", - " \n", - " Returns:\n", - " Instance ID and public IP\n", - " \"\"\"\n", - " ec2 = boto3.client('ec2')\n", - " \n", - " # User data script to setup Python environment\n", - " user_data = '''\n", - "#!/bin/bash\n", - "yum update -y\n", - "yum install -y python3 python3-pip git\n", - "pip3 install clustrix numpy scipy pandas\n", - "\n", - "# Install uv for faster package management\n", - "curl -LsSf https://astral.sh/uv/install.sh | sh\n", - "source $HOME/.cargo/env\n", - "\n", - "# Create clustrix user\n", - "useradd -m -s /bin/bash clustrix\n", - "mkdir -p /home/clustrix/.ssh\n", - "cp /home/ec2-user/.ssh/authorized_keys /home/clustrix/.ssh/\n", - "chown -R clustrix:clustrix /home/clustrix/.ssh\n", - "chmod 700 /home/clustrix/.ssh\n", - "chmod 600 /home/clustrix/.ssh/authorized_keys\n", - "\n", - "# Setup sudo access\n", - "echo \"clustrix ALL=(ALL) NOPASSWD:ALL\" >> /etc/sudoers\n", - "'''\n", - " \n", - " try:\n", - " response = ec2.run_instances(\n", - " ImageId='ami-0c02fb55956c7d316', # Amazon Linux 2 AMI\n", - " MinCount=1,\n", - " MaxCount=1,\n", - " InstanceType=instance_type,\n", - " KeyName=key_name,\n", - " SecurityGroupIds=[security_group_id],\n", - " UserData=user_data,\n", - " TagSpecifications=[\n", - " {\n", - " 'ResourceType': 'instance',\n", - " 'Tags': [\n", - " {'Key': 'Name', 'Value': 'Clustrix-Compute-Node'},\n", - " {'Key': 'Purpose', 'Value': 'Clustrix-Tutorial'}\n", - " ]\n", - " }\n", - " ]\n", - " )\n", - " \n", - " instance_id = response['Instances'][0]['InstanceId']\n", - " \n", - " # Wait for instance to be running\n", - " waiter = ec2.get_waiter('instance_running')\n", - " waiter.wait(InstanceIds=[instance_id])\n", - " \n", - " # Get public IP\n", - " instance_info = ec2.describe_instances(InstanceIds=[instance_id])\n", - " public_ip = instance_info['Reservations'][0]['Instances'][0].get('PublicIpAddress')\n", - " \n", - " return instance_id, public_ip\n", - " \n", - " except Exception as e:\n", - " print(f\"Error launching instance: {e}\")\n", - " return None, None\n", - "\n", - "# Example usage (uncomment and modify with your details)\n", - "# instance_id, public_ip = launch_clustrix_ec2_instance(\n", - "# key_name='clustrix-key',\n", - "# security_group_id='sg-xxxxxxxxx'\n", - "# )\n", - "# \n", - "# if instance_id and public_ip:\n", - "# print(f\"✓ Instance launched: {instance_id}\")\n", - "# print(f\"✓ Public IP: {public_ip}\")\n", - "# print(\"⏳ Wait 2-3 minutes for user data script to complete before connecting.\")\n", - "# else:\n", - "# print(\"✗ Failed to launch instance\")" - ] - }, - { - "cell_type": "markdown", - "id": "clustrix-config", - "metadata": {}, - "source": [ - "### Configure Clustrix for EC2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "config-ec2", - "metadata": {}, - "outputs": [], - "source": [ - "# Configure Clustrix to use your EC2 instance\n", - "configure(\n", - " cluster_type=\"ssh\",\n", - " cluster_host=\"your-ec2-public-ip\", # Replace with actual IP\n", - " username=\"clustrix\", # or \"ec2-user\" if using default user\n", - " key_file=\"~/.ssh/your-key.pem\", # Path to your private key\n", - " remote_work_dir=\"~/.clustrix/jobs\",\n", - " package_manager=\"auto\", # Will use uv if available, fallback to pip\n", - " default_cores=4,\n", - " default_memory=\"8GB\",\n", - " default_time=\"01:00:00\"\n", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "fms2rlxukv8", - "metadata": {}, - "source": [ - "**Configuration Complete!** \n", - "\n", - "Your Clustrix is now configured to use the EC2 instance. Make sure to replace `your-ec2-public-ip` with the actual IP address of your running EC2 instance." - ] - }, - { - "cell_type": "markdown", - "id": "example-computation", - "metadata": {}, - "source": [ - "### Example: Remote Computation on EC2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ec2-example", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=2, memory=\"4GB\")\n", - "def aws_monte_carlo_pi(n_samples=1000000):\n", - " \"\"\"Estimate π using Monte Carlo method on AWS EC2.\"\"\"\n", - " import numpy as np\n", - " \n", - " # Generate random points\n", - " x = np.random.uniform(-1, 1, n_samples)\n", - " y = np.random.uniform(-1, 1, n_samples)\n", - " \n", - " # Count points inside unit circle\n", - " inside_circle = (x**2 + y**2) <= 1\n", - " pi_estimate = 4 * np.sum(inside_circle) / n_samples\n", - " \n", - " return {\n", - " 'pi_estimate': pi_estimate,\n", - " 'n_samples': n_samples,\n", - " 'error': abs(pi_estimate - np.pi)\n", - " }\n", - "\n", - "# Example usage (uncomment to run on your EC2 instance):\n", - "# result = aws_monte_carlo_pi(n_samples=5000000)\n", - "# print(f\"π estimate: {result['pi_estimate']:.6f}\")\n", - "# print(f\"Error: {result['error']:.6f}\")\n", - "# print(f\"Samples used: {result['n_samples']:,}\")" - ] - }, - { - "cell_type": "markdown", - "id": "s0rz170o8cq", - "metadata": {}, - "source": [ - "**Ready to Run!** \n", - "\n", - "The Monte Carlo π estimation function is now defined and ready to execute on your EC2 instance. Simply uncomment the example usage lines above to run the computation remotely on AWS." - ] - }, - { - "cell_type": "markdown", - "id": "aws-batch", - "metadata": {}, - "source": [ - "## Method 2: AWS Batch Configuration\n", - "\n", - "AWS Batch provides managed job scheduling for more complex workloads:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "batch-setup", - "metadata": {}, - "outputs": [], - "source": [ - "def create_aws_batch_environment():\n", - " \"\"\"\n", - " Example of setting up AWS Batch compute environment.\n", - " This is a template - you'll need to adapt it to your specific needs.\n", - " \"\"\"\n", - " batch = boto3.client('batch')\n", - " ec2 = boto3.client('ec2')\n", - " iam = boto3.client('iam')\n", - " \n", - " # This is a simplified example - real setup requires:\n", - " # 1. VPC and subnet configuration\n", - " # 2. IAM roles and policies\n", - " # 3. Security groups\n", - " # 4. Compute environment\n", - " # 5. Job queue\n", - " # 6. Job definition\n", - " \n", - " return {\n", - " 'compute_environment': 'clustrix-batch-env',\n", - " 'job_queue': 'clustrix-queue',\n", - " 'job_definition': 'clustrix-job-def'\n", - " }\n", - "\n", - "# batch_config = create_aws_batch_environment()" - ] - }, - { - "cell_type": "markdown", - "id": "e7wlnigrkda", - "metadata": {}, - "source": [ - "**Note on AWS Batch Complexity**\n", - "\n", - "AWS Batch setup is complex and requires careful configuration of networking, IAM, and compute resources. For easier HPC setups, consider using AWS ParallelCluster or EKS instead. The function above provides a template structure for those who want to implement full Batch integration." - ] - }, - { - "cell_type": "markdown", - "id": "parallel-cluster", - "metadata": {}, - "source": [ - "## Method 3: AWS ParallelCluster Integration\n", - "\n", - "AWS ParallelCluster is designed for HPC workloads and integrates well with Clustrix:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "parallelcluster", - "metadata": {}, - "outputs": [], - "source": [ - "# Configure Clustrix for ParallelCluster\n", - "def configure_for_parallelcluster(cluster_name, master_ip):\n", - " \"\"\"Configure Clustrix to use AWS ParallelCluster.\"\"\"\n", - " configure(\n", - " cluster_type=\"slurm\",\n", - " cluster_host=master_ip,\n", - " username=\"ec2-user\",\n", - " key_file=\"~/.ssh/aws-clustrix-key\",\n", - " remote_work_dir=\"/shared/clustrix\", # Use shared storage\n", - " package_manager=\"uv\",\n", - " module_loads=[\"python3\"], # Load required modules\n", - " default_cores=4,\n", - " default_memory=\"8GB\",\n", - " default_time=\"01:00:00\",\n", - " default_partition=\"compute\"\n", - " )\n", - " return f\"Configured Clustrix for ParallelCluster: {cluster_name}\"\n", - "\n", - "# Example usage:\n", - "# result = configure_for_parallelcluster(\"my-cluster\", \"10.0.0.100\")\n", - "# print(result)" - ] - }, - { - "cell_type": "markdown", - "id": "7ipi0is97ue", - "metadata": {}, - "source": [ - "### ParallelCluster Configuration Example\n", - "\n", - "Here's a sample ParallelCluster configuration file for use with Clustrix:\n", - "\n", - "```ini\n", - "# Save as ~/.parallelcluster/config\n", - "[aws]\n", - "aws_region_name = us-east-1\n", - "\n", - "[global]\n", - "cluster_template = clustrix-template\n", - "update_check = false\n", - "sanity_check = true\n", - "\n", - "[cluster clustrix-template]\n", - "key_name = your-key-name\n", - "vpc_settings = vpc-settings\n", - "compute_instance_type = c5.xlarge\n", - "master_instance_type = t3.medium\n", - "initial_queue_size = 0\n", - "max_queue_size = 10\n", - "scheduler = slurm\n", - "placement_group = DYNAMIC\n", - "placement = compute\n", - "disable_hyperthreading = false\n", - "post_install = https://raw.githubusercontent.com/your-repo/clustrix-setup.sh\n", - "\n", - "[vpc vpc-settings]\n", - "vpc_id = vpc-xxxxxxxxx\n", - "master_subnet_id = subnet-xxxxxxxxx\n", - "compute_subnet_id = subnet-xxxxxxxxx\n", - "```" - ] - }, - { - "cell_type": "markdown", - "id": "storage-s3", - "metadata": {}, - "source": [ - "## Data Management with S3\n", - "\n", - "Integrate S3 for data input/output:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "s3-integration", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=2, memory=\"4GB\")\n", - "def process_s3_data(bucket_name, input_key, output_key):\n", - " \"\"\"Process data from S3 and save results back to S3.\"\"\"\n", - " import boto3\n", - " import numpy as np\n", - " import pickle\n", - " import io\n", - " \n", - " s3 = boto3.client('s3')\n", - " \n", - " # Download data from S3\n", - " response = s3.get_object(Bucket=bucket_name, Key=input_key)\n", - " data = pickle.loads(response['Body'].read())\n", - " \n", - " # Process the data\n", - " processed_data = {\n", - " 'original_shape': data.shape if hasattr(data, 'shape') else len(data),\n", - " 'mean': np.mean(data) if hasattr(data, '__iter__') else data,\n", - " 'std': np.std(data) if hasattr(data, '__iter__') else 0,\n", - " 'processing_timestamp': time.time()\n", - " }\n", - " \n", - " # Upload results to S3\n", - " output_buffer = io.BytesIO()\n", - " pickle.dump(processed_data, output_buffer)\n", - " output_buffer.seek(0)\n", - " \n", - " s3.put_object(\n", - " Bucket=bucket_name,\n", - " Key=output_key,\n", - " Body=output_buffer.getvalue()\n", - " )\n", - " \n", - " return f\"Processed data saved to s3://{bucket_name}/{output_key}\"\n", - "\n", - "# Example S3 utility functions\n", - "def upload_to_s3(data, bucket_name, key):\n", - " \"\"\"Upload data to S3.\"\"\"\n", - " s3 = boto3.client('s3')\n", - " buffer = io.BytesIO()\n", - " pickle.dump(data, buffer)\n", - " buffer.seek(0)\n", - " s3.put_object(Bucket=bucket_name, Key=key, Body=buffer.getvalue())\n", - " print(f\"✓ Data uploaded to s3://{bucket_name}/{key}\")\n", - "\n", - "def download_from_s3(bucket_name, key):\n", - " \"\"\"Download data from S3.\"\"\"\n", - " s3 = boto3.client('s3')\n", - " response = s3.get_object(Bucket=bucket_name, Key=key)\n", - " data = pickle.loads(response['Body'].read())\n", - " print(f\"✓ Data downloaded from s3://{bucket_name}/{key}\")\n", - " return data\n", - "\n", - "# Example usage:\n", - "# sample_data = np.random.rand(1000, 100)\n", - "# upload_to_s3(sample_data, 'your-bucket', 'input/sample_data.pkl')\n", - "# result = process_s3_data('your-bucket', 'input/sample_data.pkl', 'output/results.pkl')\n", - "# print(result)" - ] - }, - { - "cell_type": "markdown", - "id": "security", - "metadata": {}, - "source": [ - "## Security Best Practices\n", - "\n", - "### Security Group Configuration" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "security-group", - "metadata": {}, - "outputs": [], - "source": [ - "def create_clustrix_security_group(vpc_id, your_ip):\n", - " \"\"\"\n", - " Create a security group for Clustrix with minimal required access.\n", - " \n", - " Args:\n", - " vpc_id: VPC ID where to create the security group\n", - " your_ip: Your public IP address (get from https://checkip.amazonaws.com)\n", - " \n", - " Returns:\n", - " Security group ID\n", - " \"\"\"\n", - " ec2 = boto3.client('ec2')\n", - " \n", - " try:\n", - " response = ec2.create_security_group(\n", - " GroupName='clustrix-sg',\n", - " Description='Security group for Clustrix compute nodes',\n", - " VpcId=vpc_id\n", - " )\n", - " \n", - " sg_id = response['GroupId']\n", - " \n", - " # Add SSH access from your IP only\n", - " ec2.authorize_security_group_ingress(\n", - " GroupId=sg_id,\n", - " IpPermissions=[\n", - " {\n", - " 'IpProtocol': 'tcp',\n", - " 'FromPort': 22,\n", - " 'ToPort': 22,\n", - " 'IpRanges': [{'CidrIp': f'{your_ip}/32', 'Description': 'SSH access'}]\n", - " }\n", - " ]\n", - " )\n", - " \n", - " print(f\"✓ Created security group: {sg_id}\")\n", - " return sg_id\n", - " \n", - " except Exception as e:\n", - " print(f\"✗ Error creating security group: {e}\")\n", - " return None\n", - "\n", - "# Helper function to get your public IP\n", - "def get_my_public_ip():\n", - " \"\"\"Get your current public IP address.\"\"\"\n", - " import requests\n", - " try:\n", - " response = requests.get('https://checkip.amazonaws.com')\n", - " return response.text.strip()\n", - " except:\n", - " print(\"Could not determine public IP. Please check manually at https://checkip.amazonaws.com\")\n", - " return None\n", - "\n", - "# Example usage:\n", - "# my_ip = get_my_public_ip()\n", - "# if my_ip:\n", - "# print(f\"Your public IP: {my_ip}\")\n", - "# # sg_id = create_clustrix_security_group('vpc-xxxxxxxxx', my_ip)" - ] - }, - { - "cell_type": "markdown", - "id": "or3qhdz81af", - "metadata": {}, - "source": [ - "### AWS Security Checklist for Clustrix\n", - "\n", - "✓ **Authentication & Access**\n", - "- Use IAM roles instead of access keys when possible\n", - "- Restrict security groups to your IP address only\n", - "- Regularly rotate SSH keys and access credentials\n", - "\n", - "✓ **Network Security**\n", - "- Use private subnets for compute nodes when possible\n", - "- Enable VPC Flow Logs for network monitoring\n", - "- Use AWS Systems Manager Session Manager instead of direct SSH when possible\n", - "\n", - "✓ **Data Protection**\n", - "- Use encrypted EBS volumes and S3 buckets\n", - "- Enable CloudTrail for API logging\n", - "\n", - "✓ **Monitoring & Management**\n", - "- Set up billing alerts to monitor costs\n", - "- Tag all resources for cost tracking and management" - ] - }, - { - "cell_type": "markdown", - "id": "cost-optimization", - "metadata": {}, - "source": [ - "## Cost Optimization" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cost-tips", - "metadata": {}, - "outputs": [], - "source": [ - "# Import Clustrix cost monitoring for AWS\n", - "from clustrix import cost_tracking_decorator, get_cost_monitor, generate_cost_report, get_pricing_info\n", - "\n", - "# Example 1: Cost tracking with AWS instances\n", - "@cost_tracking_decorator('aws', 'p3.2xlarge')\n", - "@cluster(cores=8, memory=\"60GB\")\n", - "def aws_training_with_cost_tracking():\n", - " \"\"\"Example training function with AWS cost tracking.\"\"\"\n", - " import time\n", - " import numpy as np\n", - " \n", - " print(\"Starting AWS training with cost monitoring...\")\n", - " time.sleep(3) # Simulate training\n", - " \n", - " # Simulate GPU workload\n", - " data = np.random.randn(2000, 2000)\n", - " result = np.linalg.svd(data)\n", - " \n", - " print(\"Training completed!\")\n", - " return {'accuracy': 0.92, 'epochs': 50}\n", - "\n", - "# Example 2: Compare AWS pricing\n", - "def compare_aws_pricing():\n", - " \"\"\"Compare AWS EC2 pricing for different instance types.\"\"\"\n", - " pricing = get_pricing_info('aws')\n", - " if pricing:\n", - " print(\"AWS EC2 On-Demand Pricing (USD/hour):\")\n", - " \n", - " # Group by category\n", - " gpu_instances = {k: v for k, v in pricing.items() if k.startswith(('p3', 'p4d', 'g4dn'))}\n", - " compute_instances = {k: v for k, v in pricing.items() if k.startswith('c5')}\n", - " memory_instances = {k: v for k, v in pricing.items() if k.startswith('r5')}\n", - " \n", - " print(\"\\nGPU Instances:\")\n", - " for instance, price in sorted(gpu_instances.items(), key=lambda x: x[1]):\n", - " print(f\" {instance:<20}: ${price:.3f}/hour\")\n", - " \n", - " print(\"\\nCompute Optimized:\")\n", - " for instance, price in sorted(compute_instances.items(), key=lambda x: x[1]):\n", - " print(f\" {instance:<20}: ${price:.3f}/hour\")\n", - " \n", - " print(\"\\nMemory Optimized:\")\n", - " for instance, price in sorted(memory_instances.items(), key=lambda x: x[1]):\n", - " print(f\" {instance:<20}: ${price:.3f}/hour\")\n", - "\n", - "# Example 3: AWS Spot vs On-Demand cost analysis\n", - "def aws_spot_cost_analysis():\n", - " \"\"\"Analyze potential savings with AWS Spot instances.\"\"\"\n", - " monitor = get_cost_monitor('aws')\n", - " if monitor:\n", - " print(\"AWS Spot Instance Savings Analysis:\")\n", - " print(\"-\" * 40)\n", - " \n", - " instance_types = ['p3.2xlarge', 'p3.8xlarge', 'g4dn.xlarge', 'c5.large']\n", - " \n", - " for instance in instance_types:\n", - " on_demand = monitor.estimate_cost(instance, 1.0, use_spot=False)\n", - " spot = monitor.estimate_cost(instance, 1.0, use_spot=True)\n", - " savings = ((on_demand.hourly_rate - spot.hourly_rate) / on_demand.hourly_rate) * 100\n", - " \n", - " print(f\"{instance}:\")\n", - " print(f\" On-Demand: ${on_demand.hourly_rate:.3f}/hour\")\n", - " print(f\" Spot: ${spot.hourly_rate:.3f}/hour\")\n", - " print(f\" Savings: {savings:.1f}%\")\n", - " print()\n", - "\n", - "# Example 4: AWS Batch cost estimation\n", - "def estimate_aws_batch_costs():\n", - " \"\"\"Estimate costs for AWS Batch workloads.\"\"\"\n", - " monitor = get_cost_monitor('aws')\n", - " if monitor:\n", - " batch_estimate = monitor.estimate_batch_cost(\n", - " job_queue=\"clustrix-batch-queue\",\n", - " compute_environment=\"clustrix-compute-env\",\n", - " estimated_jobs=100,\n", - " avg_job_duration_hours=0.25\n", - " )\n", - " \n", - " print(\"AWS Batch Cost Estimation:\")\n", - " print(f\" Job Queue: {batch_estimate['job_queue']}\")\n", - " print(f\" Total Jobs: {batch_estimate['estimated_jobs']}\")\n", - " print(f\" Avg Duration: {batch_estimate['avg_job_duration_hours']} hours\")\n", - " print(f\" Total Compute Hours: {batch_estimate['total_compute_hours']}\")\n", - " print(f\" Estimated Cost: ${batch_estimate['estimated_cost']:.2f}\")\n", - " print(f\" Cost per Job: ${batch_estimate['cost_per_job']:.4f}\")\n", - "\n", - "# Example 5: Regional pricing comparison\n", - "def compare_aws_regions():\n", - " \"\"\"Compare AWS pricing across different regions.\"\"\"\n", - " monitor = get_cost_monitor('aws')\n", - " if monitor:\n", - " print(\"AWS Regional Pricing Comparison for p3.2xlarge:\")\n", - " print(\"-\" * 50)\n", - " \n", - " regional_pricing = monitor.get_region_pricing_comparison('p3.2xlarge')\n", - " for region, pricing_info in regional_pricing.items():\n", - " print(f\"{region}:\")\n", - " print(f\" On-Demand: ${pricing_info['on_demand_hourly']:.3f}/hour\")\n", - " print(f\" Est. Spot: ${pricing_info['estimated_spot_hourly']:.3f}/hour\")\n", - " print()\n", - "\n", - "# Example 6: Real-time AWS cost monitoring\n", - "def monitor_aws_costs():\n", - " \"\"\"Monitor current AWS resource usage and costs.\"\"\"\n", - " report = generate_cost_report('aws', 'p3.2xlarge')\n", - " if report:\n", - " print(\"Current AWS Resource Status:\")\n", - " print(f\" CPU Usage: {report['resource_usage']['cpu_percent']:.1f}%\")\n", - " print(f\" Memory Usage: {report['resource_usage']['memory_percent']:.1f}%\")\n", - " if report['resource_usage']['gpu_stats']:\n", - " print(f\" GPU Count: {len(report['resource_usage']['gpu_stats'])}\")\n", - " print(f\" Hourly Rate: ${report['cost_estimate']['hourly_rate']:.3f}\")\n", - " \n", - " if report['recommendations']:\n", - " print(\"\\nCost Optimization Recommendations:\")\n", - " for rec in report['recommendations']:\n", - " print(f\" • {rec}\")\n", - "\n", - "# Run examples\n", - "print(\"AWS Cost Monitoring Examples:\")\n", - "print(\"=\" * 40)\n", - "\n", - "print(\"\\n1. AWS Pricing Comparison:\")\n", - "compare_aws_pricing()\n", - "\n", - "print(\"\\n2. Spot Instance Savings Analysis:\")\n", - "aws_spot_cost_analysis()\n", - "\n", - "print(\"\\n3. AWS Batch Cost Estimation:\")\n", - "estimate_aws_batch_costs()\n", - "\n", - "print(\"\\n4. Regional Pricing Comparison:\")\n", - "compare_aws_regions()\n", - "\n", - "print(\"\\n5. Current AWS Status:\")\n", - "monitor_aws_costs()\n", - "\n", - "print(\"\\n✅ AWS cost monitoring examples ready!\")\n", - "print(\"💡 Use @cost_tracking_decorator('aws', 'instance_type') for automatic cost tracking\")" - ] - }, - { - "cell_type": "markdown", - "id": "gb89uvgkc9", - "metadata": {}, - "source": [ - "### AWS Cost Optimization for Clustrix\n", - "\n", - "#### 1. Instance Selection\n", - "- **Use Spot Instances** for non-critical workloads (up to 90% savings)\n", - "- **Choose right-sized instances** (don't over-provision)\n", - "- **Consider AMD instances** (often cheaper than Intel)\n", - "\n", - "#### 2. Storage Optimization\n", - "- Use **S3 Intelligent Tiering** for data\n", - "- Delete temporary files and logs regularly\n", - "- Use **gp3 EBS volumes** instead of gp2\n", - "\n", - "#### 3. Network Efficiency\n", - "- Use same AZ for compute and storage to avoid data transfer costs\n", - "- Minimize cross-region data transfer\n", - "\n", - "#### 4. Smart Scheduling\n", - "- Use scheduled scaling for predictable workloads\n", - "- Terminate instances when not in use\n", - "- Use AWS Lambda for small, short-running tasks\n", - "\n", - "#### 5. Monitoring & Control\n", - "- Set up cost alerts and budgets\n", - "- Use AWS Cost Explorer to analyze spending\n", - "- Monitor with CloudWatch to optimize resource usage" - ] - }, - { - "cell_type": "markdown", - "id": "cleanup", - "metadata": {}, - "source": [ - "## Resource Cleanup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cleanup-resources", - "metadata": {}, - "outputs": [], - "source": [ - "def cleanup_aws_resources(instance_ids=None, security_group_ids=None):\n", - " \"\"\"\n", - " Clean up AWS resources to avoid ongoing charges.\n", - " \n", - " Args:\n", - " instance_ids: List of EC2 instance IDs to terminate\n", - " security_group_ids: List of security group IDs to delete\n", - " \"\"\"\n", - " ec2 = boto3.client('ec2')\n", - " \n", - " try:\n", - " # Terminate instances\n", - " if instance_ids:\n", - " response = ec2.terminate_instances(InstanceIds=instance_ids)\n", - " print(f\"⏳ Terminating instances: {instance_ids}\")\n", - " \n", - " # Wait for termination\n", - " waiter = ec2.get_waiter('instance_terminated')\n", - " waiter.wait(InstanceIds=instance_ids)\n", - " print(\"✓ Instances terminated.\")\n", - " \n", - " # Delete security groups\n", - " if security_group_ids:\n", - " for sg_id in security_group_ids:\n", - " try:\n", - " ec2.delete_security_group(GroupId=sg_id)\n", - " print(f\"✓ Deleted security group: {sg_id}\")\n", - " except Exception as e:\n", - " print(f\"✗ Could not delete security group {sg_id}: {e}\")\n", - " \n", - " print(\"✅ Cleanup completed!\")\n", - " \n", - " except Exception as e:\n", - " print(f\"✗ Error during cleanup: {e}\")\n", - "\n", - "# Helper function to list your running instances\n", - "def list_running_instances():\n", - " \"\"\"List all running EC2 instances in your account.\"\"\"\n", - " ec2 = boto3.client('ec2')\n", - " \n", - " try:\n", - " response = ec2.describe_instances(\n", - " Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]\n", - " )\n", - " \n", - " instances = []\n", - " for reservation in response['Reservations']:\n", - " for instance in reservation['Instances']:\n", - " name = next((tag['Value'] for tag in instance.get('Tags', []) if tag['Key'] == 'Name'), 'No Name')\n", - " instances.append({\n", - " 'InstanceId': instance['InstanceId'],\n", - " 'Name': name,\n", - " 'InstanceType': instance['InstanceType'],\n", - " 'PublicIpAddress': instance.get('PublicIpAddress', 'No Public IP')\n", - " })\n", - " \n", - " if instances:\n", - " print(\"Running instances:\")\n", - " for inst in instances:\n", - " print(f\" {inst['InstanceId']} ({inst['Name']}) - {inst['InstanceType']} - {inst['PublicIpAddress']}\")\n", - " else:\n", - " print(\"No running instances found.\")\n", - " \n", - " return instances\n", - " \n", - " except Exception as e:\n", - " print(f\"✗ Error listing instances: {e}\")\n", - " return []\n", - "\n", - "# Example cleanup (uncomment and modify as needed)\n", - "# instances = list_running_instances()\n", - "# cleanup_aws_resources(\n", - "# instance_ids=['i-1234567890abcdef0'],\n", - "# security_group_ids=['sg-1234567890abcdef0']\n", - "# )" - ] - }, - { - "cell_type": "markdown", - "id": "5y04rycyarp", - "metadata": {}, - "source": [ - "**⚠️ Important: Clean Up Resources**\n", - "\n", - "Always remember to clean up AWS resources when you're done to avoid ongoing charges! The cleanup function above helps automate this process." - ] - }, - { - "cell_type": "markdown", - "id": "advanced-example", - "metadata": {}, - "source": [ - "## Advanced Example: Distributed Machine Learning" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ml-example", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=4, memory=\"8GB\", time=\"00:30:00\")\n", - "def distributed_model_training(data_params, model_params):\n", - " \"\"\"\n", - " Train a machine learning model on AWS with data from S3.\n", - " \n", - " Args:\n", - " data_params: Dictionary with S3 bucket and key information\n", - " model_params: Dictionary with model hyperparameters\n", - " \n", - " Returns:\n", - " Dictionary with training results and model location\n", - " \"\"\"\n", - " import numpy as np\n", - " import boto3\n", - " import pickle\n", - " import io\n", - " from sklearn.ensemble import RandomForestClassifier\n", - " from sklearn.metrics import accuracy_score\n", - " from sklearn.model_selection import train_test_split\n", - " \n", - " # Download training data from S3\n", - " s3 = boto3.client('s3')\n", - " response = s3.get_object(\n", - " Bucket=data_params['bucket'], \n", - " Key=data_params['training_data_key']\n", - " )\n", - " data = pickle.loads(response['Body'].read())\n", - " \n", - " X, y = data['features'], data['labels']\n", - " X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.2, random_state=42\n", - " )\n", - " \n", - " # Train model\n", - " model = RandomForestClassifier(**model_params)\n", - " model.fit(X_train, y_train)\n", - " \n", - " # Evaluate\n", - " y_pred = model.predict(X_test)\n", - " accuracy = accuracy_score(y_test, y_pred)\n", - " \n", - " # Save model to S3\n", - " model_buffer = io.BytesIO()\n", - " pickle.dump(model, model_buffer)\n", - " model_buffer.seek(0)\n", - " \n", - " s3.put_object(\n", - " Bucket=data_params['bucket'],\n", - " Key=data_params['model_output_key'],\n", - " Body=model_buffer.getvalue()\n", - " )\n", - " \n", - " return {\n", - " 'accuracy': accuracy,\n", - " 'model_location': f\"s3://{data_params['bucket']}/{data_params['model_output_key']}\",\n", - " 'training_samples': len(X_train),\n", - " 'test_samples': len(X_test)\n", - " }\n", - "\n", - "# Example usage:\n", - "# data_config = {\n", - "# 'bucket': 'your-ml-bucket',\n", - "# 'training_data_key': 'datasets/training_data.pkl',\n", - "# 'model_output_key': 'models/random_forest_model.pkl'\n", - "# }\n", - "# \n", - "# model_config = {\n", - "# 'n_estimators': 100,\n", - "# 'max_depth': 10,\n", - "# 'random_state': 42,\n", - "# 'n_jobs': -1\n", - "# }\n", - "# \n", - "# result = distributed_model_training(data_config, model_config)\n", - "# print(f\"✓ Model trained with accuracy: {result['accuracy']:.4f}\")\n", - "# print(f\"✓ Model saved to: {result['model_location']}\")\n", - "# print(f\"✓ Training samples: {result['training_samples']:,}\")\n", - "# print(f\"✓ Test samples: {result['test_samples']:,}\")" - ] - }, - { - "cell_type": "markdown", - "id": "summary", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "This tutorial covered:\n", - "\n", - "1. **Setup**: AWS credentials and Clustrix installation\n", - "2. **EC2 Integration**: Direct instance configuration\n", - "3. **AWS Batch**: Managed job scheduling\n", - "4. **ParallelCluster**: HPC-optimized clusters\n", - "5. **S3 Integration**: Data storage and retrieval\n", - "6. **Security**: Best practices for safe deployment\n", - "7. **Cost Optimization**: Strategies to minimize expenses\n", - "8. **Resource Management**: Proper cleanup procedures\n", - "\n", - "### Next Steps\n", - "\n", - "- Set up your AWS credentials and test the basic configuration\n", - "- Start with a simple EC2 instance for initial testing\n", - "- Consider ParallelCluster for production HPC workloads\n", - "- Implement proper monitoring and cost controls\n", - "- Explore AWS Spot instances for cost-effective batch processing\n", - "\n", - "### Resources\n", - "\n", - "- [AWS ParallelCluster Documentation](https://docs.aws.amazon.com/parallelcluster/)\n", - "- [AWS Batch User Guide](https://docs.aws.amazon.com/batch/)\n", - "- [AWS HPC Workshops](https://hpc-workshops.com/)\n", - "- [Clustrix Documentation](https://clustrix.readthedocs.io/)\n", - "\n", - "**Remember**: Always monitor your AWS costs and clean up resources when not in use!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/azure_cloud_tutorial.ipynb b/docs/source/notebooks/azure_cloud_tutorial.ipynb deleted file mode 100644 index cb426fa6..00000000 --- a/docs/source/notebooks/azure_cloud_tutorial.ipynb +++ /dev/null @@ -1,1621 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "63ec22f8", - "metadata": {}, - "source": [ - "> **These backends are unverified.**\n", - ">\n", - "> No clustrix cloud VM job (`provider=\"aws\"`, `\"gcp\"`, `\"azure\"`, `\"lambda\"`, `\"huggingface\"`) has been shown to run end to end. Until recently the path could not have run at all: every cloud job died with a `KeyError` on its first line. That was fixed (issue #119), but nothing has since demonstrated a completed cloud job, and `scripts/collect_execution_evidence.py` does not cover these backends. This notebook describes the intended interface, not something that has been run.\n", - ">\n", - "> The backends that are verified working are `cluster_type=\"slurm\"`, `cluster_type=\"ssh\"` and `cluster_type=\"huggingface\"` (HuggingFace Jobs, which is a different thing from the HuggingFace Spaces provider described here). See the Supported Cluster Types section of the documentation.\n" - ] - }, - { - "cell_type": "markdown", - "id": "1bf83e47", - "metadata": {}, - "source": [ - "> **What actually happens if you try `@cluster(provider=\"azure\", ...)`.**\n", - ">\n", - "> Clustrix's own cloud-VM auto-provisioning (`CloudJobManager.submit_cloud_job`, in `clustrix/executor_cloud.py`) only works end to end for `provider=\"lambda\"` -- it is the only built-in provider whose class implements `create_instance()`. For `provider=\"azure\"`, submission checks this at *submit time* and raises `NotImplementedError` naming the provider, before any thread, instance, or SSH connection is created:\n", - ">\n", - "> ```\n", - "> The 'azure' cloud provider cannot run clustrix jobs: AzureProvider does\n", - "> not implement create_instance, ... Of the built-in providers only 'lambda'\n", - "> provisions instances for job execution; for the others, provision the machine\n", - "> yourself and use cluster_type 'ssh', or use cluster_type 'kubernetes'.\n", - "> ```\n", - ">\n", - "> That is exactly the pattern this notebook follows: the examples below provision a VM using the Azure CLI, then point Clustrix's `cluster_type=\"ssh\"` (or `\"slurm\"`) at it directly -- the same transport used by any other SSH/SLURM cluster in these docs, just running on a cloud box instead of an on-prem one. That exercises the SSH/SLURM backend, not a demonstrated run on this specific cloud, and no such run has been recorded for any of these providers.\n", - ">\n", - "> One more thing that used to be silently wrong and is now an explicit error: if a provider's `get_cluster_config()` cannot determine a VM's real hostname (API error, VM not yet assigned an IP, ...), it used to return a fake `placeholder.azure.com` hostname, which nothing downstream could tell apart from a real one -- the failure then surfaced as an SSH connection error against a domain that does not exist, far from its actual cause. It now raises `RuntimeError` naming the resource instead." - ] - }, - { - "cell_type": "markdown", - "id": "70efbb31", - "metadata": {}, - "source": [ - "**Behind the scenes, once you're actually calling `@cluster`:** every\n", - "example below that runs (as opposed to just printing setup commands) ends up\n", - "configured with `cluster_type=\"ssh\"` against a VM you provisioned yourself --\n", - "that is the verified SSH backend, following the same order of operations as\n", - "any other SSH cluster in these docs: read `get_config()`, merge decorator\n", - "args over config defaults, choose local vs. remote (`cluster_host` is set,\n", - "so remote), serialize the function with `dill`/`cloudpickle`, create the\n", - "remote job directory, upload the payload over SFTP, build the remote venv,\n", - "generate and run a job script, poll for completion, then download and\n", - "HMAC-verify `result.pkl`. None of that is Azure (VM/CycleCloud)-specific -- clustrix\n", - "does not talk to the Azure (VM/CycleCloud) API at any point in that path; Azure (VM/CycleCloud)\n", - "only matters for how the VM itself got created, which is everything *before*\n", - "`configure(cluster_type=\"ssh\", ...)` in this notebook. Full, source-verified\n", - "detail: :ref:`execution-model`. Every `ClusterConfig` field used below is\n", - "documented in :ref:`configuration`.\n", - "\n", - "**Real resources, real charges.** The functions and CLI snippets below that\n", - "create VMs, networks, security groups, or managed clusters call real\n", - "Azure (VM/CycleCloud) APIs (or print commands meant to be copy-pasted into a real\n", - "Azure (VM/CycleCloud) CLI). None of them run automatically in this notebook -- every\n", - "invocation is commented out -- but if you uncomment one, or copy a printed\n", - "command into your terminal, it creates billed resources in your account.\n", - "Read each cell before running or copying it, and see the cleanup cell near\n", - "the end before you walk away." - ] - }, - { - "cell_type": "markdown", - "id": "azure-title", - "metadata": {}, - "source": [ - "# Microsoft Azure Cloud Tutorial\n", - "\n", - "This tutorial demonstrates how to use Clustrix with Microsoft Azure cloud infrastructure for scalable distributed computing.\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/azure_cloud_tutorial.ipynb)\n", - "\n", - "## Overview\n", - "\n", - "Azure provides several services that integrate well with Clustrix:\n", - "\n", - "- **Azure Virtual Machines**: Scalable compute instances\n", - "- **Azure Batch**: Managed job scheduling service\n", - "- **Azure CycleCloud**: HPC cluster orchestration\n", - "- **Azure Machine Learning Compute**: ML-optimized infrastructure\n", - "- **Azure Container Instances**: Serverless containers\n", - "- **Azure Blob Storage**: Object storage for data and results\n", - "- **Azure Virtual Network**: Network isolation and security\n", - "\n", - "## Prerequisites\n", - "\n", - "### Required Azure Setup\n", - "\n", - "1. **Azure Account**: Active Azure subscription with appropriate permissions\n", - "2. **Azure CLI**: Installed and configured on your local machine\n", - "3. **SSH Key Pair**: For secure VM access\n", - "4. **Resource Quotas**: Sufficient compute quotas in your preferred region\n", - "5. **Billing Setup**: Credit card or other payment method configured\n", - "\n", - "### Local Environment Setup\n", - "\n", - "1. **Python Environment**: Python 3.10+ with pip\n", - "2. **SSH Client**: OpenSSH or equivalent\n", - "3. **Git**: For version control (optional but recommended)\n", - "4. **Code Editor**: VS Code, PyCharm, or your preferred editor" - ] - }, - { - "cell_type": "markdown", - "id": "installation", - "metadata": {}, - "source": [ - "## Step-by-Step Setup Guide\n", - "\n", - "### Step 1: Install Azure CLI\n", - "\n", - "First, install the Azure CLI on your local machine:\n", - "\n", - "**Windows (PowerShell):**\n", - "```powershell\n", - "Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\\AzureCLI.msi\n", - "```\n", - "\n", - "**macOS (Homebrew):**\n", - "```bash\n", - "brew update && brew install azure-cli\n", - "```\n", - "\n", - "**Linux (Ubuntu/Debian):**\n", - "```bash\n", - "curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash\n", - "```\n", - "\n", - "### Step 2: Create Azure Account and Subscription\n", - "\n", - "1. Go to [Azure Portal](https://portal.azure.com)\n", - "2. Sign up for a free account (includes $200 credit)\n", - "3. Complete account verification\n", - "4. Note your Subscription ID from the Azure Portal\n", - "\n", - "### Step 3: Install Clustrix with Azure Dependencies" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "install", - "metadata": {}, - "outputs": [], - "source": [ - "# Install Clustrix with Azure support\n", - "!pip install clustrix azure-identity azure-mgmt-compute azure-mgmt-network azure-storage-blob\n", - "\n", - "# Import required libraries\n", - "import clustrix\n", - "from clustrix import cluster, configure\n", - "from azure.identity import DefaultAzureCredential\n", - "from azure.mgmt.compute import ComputeManagementClient\n", - "from azure.mgmt.network import NetworkManagementClient\n", - "from azure.storage.blob import BlobServiceClient\n", - "import os\n", - "import numpy as np\n", - "import time\n", - "import json" - ] - }, - { - "cell_type": "markdown", - "id": "azure-credentials", - "metadata": {}, - "source": [ - "## Step 4: Azure Authentication Setup\n", - "\n", - "Configure your Azure credentials. You can do this in several ways:\n", - "\n", - "### Option 1: Azure CLI Authentication (Recommended for Development)\n", - "\n", - "This is the simplest method for getting started:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "azure-cli-auth", - "metadata": {}, - "outputs": [], - "source": [ - "# Login with Azure CLI (run this in terminal)\n", - "# az login\n", - "\n", - "# Set your subscription (replace with your actual subscription ID)\n", - "# az account set --subscription \"12345678-1234-1234-1234-123456789012\"\n", - "\n", - "# Verify authentication\n", - "!az account show --output table" - ] - }, - { - "cell_type": "markdown", - "id": "azure-creds-env", - "metadata": {}, - "source": [ - "### Option 2: Service Principal Authentication (Recommended for Production)\n", - "\n", - "For production environments, create a service principal with limited permissions:\n", - "\n", - "**Create Service Principal (run in terminal):**\n", - "```bash\n", - "# Create service principal\n", - "az ad sp create-for-rbac --name \"clustrix-service-principal\" --role contributor\n", - "\n", - "# The output will include:\n", - "# - appId (client ID)\n", - "# - password (client secret)\n", - "# - tenant (tenant ID)\n", - "```\n", - "\n", - "**Set Environment Variables:**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "service-principal", - "metadata": {}, - "outputs": [], - "source": [ - "# Set Azure credentials as environment variables (replace with your actual values)\n", - "# os.environ['AZURE_CLIENT_ID'] = 'your-client-id-from-service-principal'\n", - "# os.environ['AZURE_CLIENT_SECRET'] = 'your-client-secret-from-service-principal' \n", - "# os.environ['AZURE_TENANT_ID'] = 'your-tenant-id-from-service-principal'\n", - "\n", - "# Test Azure connection\n", - "try:\n", - " credential = DefaultAzureCredential()\n", - " subscription_id = 'your-subscription-id' # Replace with actual ID\n", - " \n", - " compute_client = ComputeManagementClient(credential, subscription_id)\n", - " # Test by listing VM sizes in East US\n", - " vm_sizes = list(compute_client.virtual_machine_sizes.list('eastus'))\n", - " print(f\"Successfully connected to Azure. Available VM sizes: {len(vm_sizes)}\")\n", - "except Exception as e:\n", - " print(f\"Azure connection failed: {e}\")\n", - " print(\"Make sure you have:\")\n", - " print(\"1. Run 'az login' or set service principal environment variables\")\n", - " print(\"2. Set the correct subscription ID\")\n", - " print(\"3. Have appropriate permissions in your Azure subscription\")" - ] - }, - { - "cell_type": "markdown", - "id": "1hqa6m0oltd", - "metadata": {}, - "source": [ - "### Step 5: Generate SSH Key Pair\n", - "\n", - "Clustrix requires SSH access to remote VMs. Generate an SSH key pair if you don't have one:\n", - "\n", - "**Generate SSH Key (run in terminal):**\n", - "```bash\n", - "# Generate SSH key pair (press Enter for default location)\n", - "ssh-keygen -t rsa -b 4096 -C \"your-email@example.com\"\n", - "\n", - "# Add key to SSH agent\n", - "ssh-add ~/.ssh/id_rsa\n", - "\n", - "# Display public key (you'll need this for VM creation)\n", - "cat ~/.ssh/id_rsa.pub\n", - "```\n", - "\n", - "**Important Notes:**\n", - "- Keep your private key (`~/.ssh/id_rsa`) secure and never share it\n", - "- You'll use the public key (`~/.ssh/id_rsa.pub`) when creating Azure VMs\n", - "- Make sure you have set up authentication and have the correct subscription ID" - ] - }, - { - "cell_type": "markdown", - "id": "vm-setup", - "metadata": {}, - "source": [ - "## Method 1: Azure Virtual Machines Configuration\n", - "\n", - "### Step 6: Create Resource Group and Azure VM for Clustrix\n", - "\n", - "First, create a resource group to organize your Azure resources:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "vm-creation", - "metadata": {}, - "outputs": [], - "source": [ - "def create_clustrix_vm(resource_group, vm_name, location='eastus', vm_size='Standard_D4s_v3'):\n", - " \"\"\"\n", - " Create an Azure VM configured for Clustrix.\n", - " \n", - " Args:\n", - " resource_group: Azure resource group name\n", - " vm_name: Name for the VM\n", - " location: Azure region\n", - " vm_size: VM size (CPU/memory configuration)\n", - " \n", - " Returns:\n", - " VM details including public IP\n", - " \"\"\"\n", - " # Cloud-init script for VM setup\n", - " cloud_init_script = '''\n", - "#cloud-config\n", - "package_update: true\n", - "packages:\n", - " - python3\n", - " - python3-pip\n", - " - git\n", - " - htop\n", - "\n", - "runcmd:\n", - " # Install clustrix and common packages\n", - " - pip3 install clustrix numpy scipy pandas scikit-learn\n", - " \n", - " # Install uv for faster package management\n", - " - curl -LsSf https://astral.sh/uv/install.sh | sh\n", - " \n", - " # Create clustrix user\n", - " - useradd -m -s /bin/bash clustrix\n", - " - usermod -aG sudo clustrix\n", - " - echo \"clustrix ALL=(ALL) NOPASSWD:ALL\" >> /etc/sudoers\n", - " \n", - " # Setup SSH for clustrix user\n", - " - mkdir -p /home/clustrix/.ssh\n", - " - cp /home/azureuser/.ssh/authorized_keys /home/clustrix/.ssh/\n", - " - chown -R clustrix:clustrix /home/clustrix/.ssh\n", - " - chmod 700 /home/clustrix/.ssh\n", - " - chmod 600 /home/clustrix/.ssh/authorized_keys\n", - " \n", - " # Create working directory\n", - " - mkdir -p /tmp/clustrix\n", - " - chown clustrix:clustrix /tmp/clustrix\n", - "'''\n", - " \n", - " # Azure CLI commands for VM creation\n", - " azure_commands = f\"\"\"\n", - "# Create resource group\n", - "az group create --name {resource_group} --location {location}\n", - "\n", - "# Create VM with cloud-init\n", - "az vm create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --name {vm_name} \\\\\n", - " --image Ubuntu2204 \\\\\n", - " --size {vm_size} \\\\\n", - " --admin-username azureuser \\\\\n", - " --generate-ssh-keys \\\\\n", - " --custom-data cloud-init.txt \\\\\n", - " --public-ip-sku Standard \\\\\n", - " --tags Purpose=Clustrix Environment=Tutorial\n", - "\n", - "# Get public IP\n", - "az vm show \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --name {vm_name} \\\\\n", - " --show-details \\\\\n", - " --query publicIps \\\\\n", - " --output tsv\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'resource_group': resource_group,\n", - " 'vm_name': vm_name,\n", - " 'location': location,\n", - " 'vm_size': vm_size,\n", - " 'commands': azure_commands,\n", - " 'cloud_init': cloud_init_script\n", - " }\n", - "\n", - "# Example VM configuration\n", - "vm_config = create_clustrix_vm(\n", - " resource_group='clustrix-tutorial-rg',\n", - " vm_name='clustrix-vm-01',\n", - " location='eastus',\n", - " vm_size='Standard_D4s_v3' # 4 vCPUs, 16 GB RAM\n", - ")\n", - "\n", - "print(\"Save the cloud-init script to a file called 'cloud-init.txt' in your current directory\")\n", - "print(\"Then execute these Azure CLI commands to create your VM:\")\n", - "print(\"-\" * 60)\n", - "print(vm_config['commands'])" - ] - }, - { - "cell_type": "markdown", - "id": "2qs6d0q31fd", - "metadata": {}, - "source": [ - "### Cloud-Init Script\n", - "\n", - "Save this cloud-init script to a file named `cloud-init.txt` in your current directory:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "nbe27b4ha1e", - "metadata": {}, - "outputs": [], - "source": [ - "# Display the cloud-init script content\n", - "print(vm_config['cloud_init'])" - ] - }, - { - "cell_type": "markdown", - "id": "clustrix-azure-config", - "metadata": {}, - "source": [ - "### Step 7: Configure Clustrix for Azure VM\n", - "\n", - "After your VM is created and you have the public IP address, configure Clustrix to use it:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "config-azure-vm", - "metadata": {}, - "outputs": [], - "source": [ - "# Configure Clustrix to use your Azure VM\n", - "# Replace 'your-vm-public-ip' with the actual IP from: az vm show --resource-group clustrix-tutorial-rg --name clustrix-vm-01 --show-details --query publicIps --output tsv\n", - "\n", - "configure(\n", - " cluster_type=\"ssh\",\n", - " cluster_host=\"your-vm-public-ip\", # Replace with actual IP\n", - " username=\"clustrix\", # or \"azureuser\" if using default user\n", - " key_file=\"~/.ssh/id_rsa\", # Azure CLI generated key\n", - " remote_work_dir=\"~/.clustrix/jobs\",\n", - " package_manager=\"auto\", # Will use uv if available\n", - " default_cores=4,\n", - " default_memory=\"8GB\",\n", - " default_time=\"01:00:00\"\n", - ")\n", - "\n", - "print(\"Clustrix configured for Azure VM\")\n", - "print(\"Make sure to replace 'your-vm-public-ip' with your actual VM's public IP address\")" - ] - }, - { - "cell_type": "markdown", - "id": "kh72n7h6uzp", - "metadata": {}, - "source": [ - "### Testing Your Azure VM Connection\n", - "\n", - "Before running Clustrix jobs, test your SSH connection to the VM:\n", - "\n", - "```bash\n", - "# Test SSH connection (replace with your actual IP)\n", - "ssh -i ~/.ssh/id_rsa clustrix@your-vm-public-ip\n", - "\n", - "# Or if using default azureuser:\n", - "ssh -i ~/.ssh/id_rsa azureuser@your-vm-public-ip\n", - "```\n", - "\n", - "**Troubleshooting Connection Issues:**\n", - "- Ensure your VM is running: `az vm show --resource-group clustrix-tutorial-rg --name clustrix-vm-01 --show-details --query powerState`\n", - "- Check Network Security Group rules allow SSH (port 22)\n", - "- Verify your SSH key is correct and has proper permissions (`chmod 600 ~/.ssh/id_rsa`)" - ] - }, - { - "cell_type": "markdown", - "id": "azure-example", - "metadata": {}, - "source": [ - "### Example: Remote Computation on Azure VM" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "azure-vm-example", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=2, memory=\"4GB\")\n", - "def azure_numerical_analysis(matrix_size=1000, iterations=10):\n", - " \"\"\"Perform numerical analysis on Azure VM.\"\"\"\n", - " import numpy as np\n", - " import time\n", - " \n", - " results = []\n", - " \n", - " for i in range(iterations):\n", - " # Generate random matrix\n", - " matrix = np.random.rand(matrix_size, matrix_size)\n", - " \n", - " # Perform eigenvalue decomposition\n", - " start_time = time.time()\n", - " eigenvalues = np.linalg.eigvals(matrix)\n", - " computation_time = time.time() - start_time\n", - " \n", - " results.append({\n", - " 'iteration': i + 1,\n", - " 'max_eigenvalue': float(np.max(eigenvalues.real)),\n", - " 'min_eigenvalue': float(np.min(eigenvalues.real)),\n", - " 'computation_time': computation_time\n", - " })\n", - " \n", - " return {\n", - " 'matrix_size': matrix_size,\n", - " 'total_iterations': iterations,\n", - " 'average_time': np.mean([r['computation_time'] for r in results]),\n", - " 'results': results\n", - " }\n", - "\n", - "# Run computation on Azure VM (uncomment after configuring your VM)\n", - "# result = azure_numerical_analysis(matrix_size=500, iterations=5)\n", - "# print(f\"Completed {result['total_iterations']} iterations\")\n", - "# print(f\"Average computation time: {result['average_time']:.3f} seconds\")\n", - "\n", - "print(\"Example function defined. Configure your VM IP address and uncomment the lines above to run.\")" - ] - }, - { - "cell_type": "markdown", - "id": "azure-batch", - "metadata": {}, - "source": [ - "## Method 2: Azure Batch Configuration\n", - "\n", - "Azure Batch provides managed job scheduling for large-scale parallel workloads:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "azure-batch-setup", - "metadata": {}, - "outputs": [], - "source": [ - "def setup_azure_batch_environment():\n", - " \"\"\"\n", - " Template for setting up Azure Batch environment.\n", - " This requires manual setup through Azure portal or CLI.\n", - " \"\"\"\n", - " \n", - " batch_setup_commands = \"\"\"\n", - "# Create Azure Batch account\n", - "az batch account create \\\\\n", - " --name clustrixbatch \\\\\n", - " --resource-group clustrix-tutorial-rg \\\\\n", - " --location eastus\n", - "\n", - "# Create storage account for Batch\n", - "az storage account create \\\\\n", - " --name clustrixstorage \\\\\n", - " --resource-group clustrix-tutorial-rg \\\\\n", - " --location eastus \\\\\n", - " --sku Standard_LRS\n", - "\n", - "# Link storage to Batch account\n", - "az batch account set \\\\\n", - " --name clustrixbatch \\\\\n", - " --resource-group clustrix-tutorial-rg \\\\\n", - " --storage-account clustrixstorage\n", - "\n", - "# Create Batch pool\n", - "az batch pool create \\\\\n", - " --id clustrix-pool \\\\\n", - " --vm-size Standard_D2s_v3 \\\\\n", - " --target-dedicated-nodes 2 \\\\\n", - " --image canonical:0001-com-ubuntu-server-jammy:22_04-lts \\\\\n", - " --node-agent-sku-id \"batch.node.ubuntu 22.04\"\n", - "\n", - "# Create Batch job\n", - "az batch job create \\\\\n", - " --id clustrix-job \\\\\n", - " --pool-id clustrix-pool\n", - "\"\"\"\n", - " \n", - " batch_config = {\n", - " 'account_name': 'clustrixbatch',\n", - " 'account_url': 'https://clustrixbatch.eastus.batch.azure.com',\n", - " 'resource_group': 'clustrix-tutorial-rg',\n", - " 'pool_id': 'clustrix-pool',\n", - " 'job_id': 'clustrix-job'\n", - " }\n", - " \n", - " return batch_config, batch_setup_commands\n", - "\n", - "batch_config, batch_commands = setup_azure_batch_environment()\n", - "\n", - "print(\"Azure Batch Configuration:\")\n", - "print(json.dumps(batch_config, indent=2))\n", - "print(\"\\nTo set up Azure Batch, run these commands:\")\n", - "print(\"-\" * 50)\n", - "print(batch_commands)" - ] - }, - { - "cell_type": "markdown", - "id": "i6n8kc7lvi", - "metadata": {}, - "source": [ - "**Important Notes for Azure Batch:**\n", - "- Azure Batch integration with Clustrix requires custom implementation\n", - "- Consider using Azure CycleCloud for HPC workloads instead\n", - "- Batch is better suited for managed job scheduling at scale" - ] - }, - { - "cell_type": "markdown", - "id": "cyclecloud", - "metadata": {}, - "source": [ - "## Method 3: Azure CycleCloud Integration\n", - "\n", - "Azure CycleCloud is designed for HPC workloads and provides SLURM integration:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cyclecloud-config", - "metadata": {}, - "outputs": [], - "source": [ - "# Azure CycleCloud cluster template for Clustrix\n", - "cyclecloud_template = \"\"\"\n", - "# CycleCloud SLURM cluster template\n", - "# Save as clustrix-slurm.txt and import into CycleCloud\n", - "\n", - "[cluster clustrix-slurm]\n", - "FormLayout = selectionpanel\n", - "Category = Schedulers\n", - "IconUrl = static/cloud/cluster/ui/ClusterIcon/slurm.png\n", - "\n", - " [[node defaults]]\n", - " UsePublicNetwork = false\n", - " Credentials = $Credentials\n", - " SubnetId = $SubnetId\n", - " Region = $Region\n", - " KeyPairLocation = ~/.ssh/cyclecloud.pem\n", - " \n", - " # Install clustrix on all nodes\n", - " [[[configuration]]]\n", - " clustrix.version = latest\n", - " \n", - " [[[cluster-init clustrix:default:1.0.0]]]\n", - " \n", - " [[node master]]\n", - " MachineType = $MasterMachineType\n", - " IsReturnProxy = $ReturnProxy\n", - " AdditionalClusterInitSpecs = $MasterClusterInitSpecs\n", - " \n", - " [[[configuration]]]\n", - " slurm.version = $configuration_slurm_version\n", - " \n", - " [[[cluster-init slurm:master:2.7.2]]]\n", - " \n", - " [[[network-interface eth0]]]\n", - " AssociatePublicIpAddress = $UsePublicNetwork\n", - "\n", - " [[nodearray execute]]\n", - " MachineType = $ExecuteMachineType\n", - " MaxCoreCount = $MaxExecuteCoreCount\n", - " Interruptible = $UseLowPrio\n", - " AdditionalClusterInitSpecs = $ExecuteClusterInitSpecs\n", - " \n", - " [[[configuration]]]\n", - " slurm.version = $configuration_slurm_version\n", - " \n", - " [[[cluster-init slurm:execute:2.7.2]]]\n", - " \n", - " [[[network-interface eth0]]]\n", - " AssociatePublicIpAddress = false\n", - "\n", - "[parameters About]\n", - "Order = 1\n", - "\n", - " [[parameters About Clustrix]]\n", - " \n", - " [[[parameter clustrix]]]\n", - " HideLabel = true\n", - " Config.Plugin = pico.widget.HtmlTemplateWidget\n", - " Config.Template = \"Clustrix-enabled SLURM cluster for distributed computing\"\n", - "\n", - "[parameters Required Settings]\n", - "Order = 10\n", - "\n", - " [[parameters Virtual Machines]]\n", - " Description = \"Configure the VM types and sizes\"\n", - " Order = 20\n", - "\n", - " [[[parameter Region]]]\n", - " Label = Region\n", - " Description = Deployment Location\n", - " ParameterType = Cloud.Region\n", - " DefaultValue = eastus\n", - "\n", - " [[[parameter MasterMachineType]]]\n", - " Label = Master VM Type\n", - " Description = Master node VM type\n", - " ParameterType = Cloud.MachineType\n", - " DefaultValue = Standard_D4s_v3\n", - "\n", - " [[[parameter ExecuteMachineType]]]\n", - " Label = Execute VM Type\n", - " Description = Execute node VM type\n", - " ParameterType = Cloud.MachineType\n", - " DefaultValue = Standard_H16r\n", - "\n", - "\"\"\"\n", - "\n", - "def configure_for_cyclecloud(master_ip, cluster_name=\"clustrix-slurm\"):\n", - " \"\"\"Configure Clustrix to use Azure CycleCloud SLURM cluster.\"\"\"\n", - " configure(\n", - " cluster_type=\"slurm\",\n", - " cluster_host=master_ip,\n", - " username=\"cyclecloud\", # Default CycleCloud user\n", - " key_file=\"~/.ssh/cyclecloud.pem\",\n", - " remote_work_dir=\"/shared/clustrix\", # Use shared storage\n", - " package_manager=\"uv\",\n", - " module_loads=[\"python3\"],\n", - " environment_variables={\n", - " \"CLUSTRIX_CLUSTER\": cluster_name\n", - " },\n", - " default_cores=8,\n", - " default_memory=\"16GB\",\n", - " default_time=\"02:00:00\",\n", - " default_partition=\"hpc\"\n", - " )\n", - " return f\"Configured Clustrix for CycleCloud cluster: {cluster_name}\"\n", - "\n", - "print(\"CycleCloud Template (save as clustrix-slurm.txt):\")\n", - "print(cyclecloud_template)\n", - "\n", - "# Example configuration (uncomment and modify as needed)\n", - "# config_message = configure_for_cyclecloud(\"10.1.0.4\", \"my-clustrix-cluster\")\n", - "# print(config_message)" - ] - }, - { - "cell_type": "markdown", - "id": "88eh6lcuqop", - "metadata": {}, - "source": [ - "**Azure CycleCloud Benefits:**\n", - "- Best-in-class HPC cluster management for Azure\n", - "- Native SLURM integration works seamlessly with Clustrix\n", - "- Automatic scaling and cost optimization\n", - "- Enterprise-grade security and compliance\n", - "- Hybrid cloud capabilities for on-premises integration" - ] - }, - { - "cell_type": "markdown", - "id": "azure-storage", - "metadata": {}, - "source": [ - "## Data Management with Azure Blob Storage" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "blob-storage", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=2, memory=\"4GB\")\n", - "def process_blob_data(storage_account, container_name, input_blob, output_blob, storage_key=None):\n", - " \"\"\"Process data from Azure Blob Storage and save results back.\"\"\"\n", - " from azure.storage.blob import BlobServiceClient\n", - " from azure.identity import DefaultAzureCredential\n", - " import numpy as np\n", - " import pickle\n", - " import io\n", - " \n", - " # Initialize Blob Service Client\n", - " if storage_key:\n", - " account_url = f\"https://{storage_account}.blob.core.windows.net\"\n", - " blob_service_client = BlobServiceClient(account_url=account_url, credential=storage_key)\n", - " else:\n", - " # Use managed identity or Azure CLI authentication\n", - " account_url = f\"https://{storage_account}.blob.core.windows.net\"\n", - " credential = DefaultAzureCredential()\n", - " blob_service_client = BlobServiceClient(account_url=account_url, credential=credential)\n", - " \n", - " # Download data from blob storage\n", - " blob_client = blob_service_client.get_blob_client(container=container_name, blob=input_blob)\n", - " blob_data = blob_client.download_blob()\n", - " data = pickle.loads(blob_data.readall())\n", - " \n", - " # Process the data\n", - " processed_data = {\n", - " 'original_shape': data.shape if hasattr(data, 'shape') else len(data),\n", - " 'mean': float(np.mean(data)) if hasattr(data, '__iter__') else float(data),\n", - " 'std': float(np.std(data)) if hasattr(data, '__iter__') else 0.0,\n", - " 'max': float(np.max(data)) if hasattr(data, '__iter__') else float(data),\n", - " 'min': float(np.min(data)) if hasattr(data, '__iter__') else float(data),\n", - " 'processing_timestamp': time.time(),\n", - " 'processed_on': 'azure-vm'\n", - " }\n", - " \n", - " # Upload results to blob storage\n", - " output_buffer = io.BytesIO()\n", - " pickle.dump(processed_data, output_buffer)\n", - " output_buffer.seek(0)\n", - " \n", - " output_blob_client = blob_service_client.get_blob_client(container=container_name, blob=output_blob)\n", - " output_blob_client.upload_blob(output_buffer.getvalue(), overwrite=True)\n", - " \n", - " return f\"Processed data saved to blob: {output_blob}\"\n", - "\n", - "# Utility functions for Azure Blob Storage\n", - "def upload_to_blob(data, storage_account, container_name, blob_name, storage_key=None):\n", - " \"\"\"Upload data to Azure Blob Storage.\"\"\"\n", - " if storage_key:\n", - " account_url = f\"https://{storage_account}.blob.core.windows.net\"\n", - " blob_service_client = BlobServiceClient(account_url=account_url, credential=storage_key)\n", - " else:\n", - " account_url = f\"https://{storage_account}.blob.core.windows.net\"\n", - " credential = DefaultAzureCredential()\n", - " blob_service_client = BlobServiceClient(account_url=account_url, credential=credential)\n", - " \n", - " buffer = io.BytesIO()\n", - " pickle.dump(data, buffer)\n", - " buffer.seek(0)\n", - " \n", - " blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)\n", - " blob_client.upload_blob(buffer.getvalue(), overwrite=True)\n", - " return f\"Data uploaded to blob: {blob_name}\"\n", - "\n", - "def download_from_blob(storage_account, container_name, blob_name, storage_key=None):\n", - " \"\"\"Download data from Azure Blob Storage.\"\"\"\n", - " if storage_key:\n", - " account_url = f\"https://{storage_account}.blob.core.windows.net\"\n", - " blob_service_client = BlobServiceClient(account_url=account_url, credential=storage_key)\n", - " else:\n", - " account_url = f\"https://{storage_account}.blob.core.windows.net\"\n", - " credential = DefaultAzureCredential()\n", - " blob_service_client = BlobServiceClient(account_url=account_url, credential=credential)\n", - " \n", - " blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)\n", - " blob_data = blob_client.download_blob()\n", - " return pickle.loads(blob_data.readall())\n", - "\n", - "# Example usage (uncomment and modify as needed):\n", - "# sample_data = np.random.rand(1000, 50)\n", - "# upload_result = upload_to_blob(sample_data, 'yourstorageaccount', 'data', 'input/sample.pkl')\n", - "# print(upload_result)\n", - "# \n", - "# process_result = process_blob_data('yourstorageaccount', 'data', 'input/sample.pkl', 'output/results.pkl')\n", - "# print(process_result)\n", - "\n", - "print(\"Azure Blob Storage integration functions defined.\")" - ] - }, - { - "cell_type": "markdown", - "id": "azure-ml", - "metadata": {}, - "source": [ - "## Azure Machine Learning Compute Integration" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "azure-ml-compute", - "metadata": {}, - "outputs": [], - "source": [ - "def setup_azure_ml_compute():\n", - " \"\"\"\n", - " Template for setting up Azure ML compute clusters.\n", - " These can be used with Clustrix for ML workloads.\n", - " \"\"\"\n", - " \n", - " aml_setup_commands = \"\"\"\n", - "# Create Azure ML workspace\n", - "az ml workspace create \\\\\n", - " --name clustrix-ml-workspace \\\\\n", - " --resource-group clustrix-tutorial-rg \\\\\n", - " --location eastus\n", - "\n", - "# Create compute cluster\n", - "az ml compute create \\\\\n", - " --name clustrix-compute \\\\\n", - " --type amlcompute \\\\\n", - " --min-instances 0 \\\\\n", - " --max-instances 4 \\\\\n", - " --size Standard_DS3_v2 \\\\\n", - " --workspace-name clustrix-ml-workspace \\\\\n", - " --resource-group clustrix-tutorial-rg\n", - "\n", - "# Create compute instance for development\n", - "az ml compute create \\\\\n", - " --name clustrix-dev-instance \\\\\n", - " --type computeinstance \\\\\n", - " --size Standard_DS3_v2 \\\\\n", - " --workspace-name clustrix-ml-workspace \\\\\n", - " --resource-group clustrix-tutorial-rg\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'workspace': 'clustrix-ml-workspace',\n", - " 'compute_cluster': 'clustrix-compute',\n", - " 'compute_instance': 'clustrix-dev-instance',\n", - " 'commands': aml_setup_commands\n", - " }\n", - "\n", - "@cluster(cores=4, memory=\"8GB\")\n", - "def azure_ml_training_job(dataset_params, model_params):\n", - " \"\"\"Example ML training job that could run on Azure ML compute.\"\"\"\n", - " import numpy as np\n", - " from sklearn.ensemble import RandomForestClassifier\n", - " from sklearn.metrics import accuracy_score, classification_report\n", - " from sklearn.model_selection import train_test_split\n", - " from sklearn.datasets import make_classification\n", - " import time\n", - " \n", - " # Generate synthetic dataset (in real scenario, load from Azure ML datasets)\n", - " X, y = make_classification(\n", - " n_samples=dataset_params['n_samples'],\n", - " n_features=dataset_params['n_features'],\n", - " n_classes=dataset_params['n_classes'],\n", - " random_state=42\n", - " )\n", - " \n", - " X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.2, random_state=42\n", - " )\n", - " \n", - " # Train model\n", - " start_time = time.time()\n", - " model = RandomForestClassifier(**model_params)\n", - " model.fit(X_train, y_train)\n", - " training_time = time.time() - start_time\n", - " \n", - " # Evaluate\n", - " y_pred = model.predict(X_test)\n", - " accuracy = accuracy_score(y_test, y_pred)\n", - " \n", - " return {\n", - " 'accuracy': accuracy,\n", - " 'training_time': training_time,\n", - " 'training_samples': len(X_train),\n", - " 'test_samples': len(X_test),\n", - " 'feature_importance': model.feature_importances_.tolist()[:10], # Top 10\n", - " 'model_params': model_params,\n", - " 'dataset_params': dataset_params\n", - " }\n", - "\n", - "aml_config = setup_azure_ml_compute()\n", - "\n", - "print(\"Azure ML Setup Commands:\")\n", - "print(aml_config['commands'])\n", - "\n", - "# Example usage (uncomment to run after setting up Azure ML):\n", - "# dataset_config = {'n_samples': 10000, 'n_features': 20, 'n_classes': 3}\n", - "# model_config = {'n_estimators': 100, 'max_depth': 10, 'random_state': 42, 'n_jobs': -1}\n", - "# result = azure_ml_training_job(dataset_config, model_config)\n", - "# print(f\"Model trained with accuracy: {result['accuracy']:.4f}\")\n", - "\n", - "print(\"Azure ML integration example defined.\")" - ] - }, - { - "cell_type": "markdown", - "id": "azure-security", - "metadata": {}, - "source": [ - "## Security Best Practices" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "azure-security-setup", - "metadata": {}, - "outputs": [], - "source": [ - "def setup_azure_security_for_clustrix(resource_group='clustrix-tutorial-rg', location='eastus'):\n", - " \"\"\"\n", - " Security configuration for Azure + Clustrix deployment.\n", - " \"\"\"\n", - " \n", - " security_commands = f\"\"\"\n", - "# Create virtual network with private subnets\n", - "az network vnet create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --name clustrix-vnet \\\\\n", - " --address-prefix 10.1.0.0/16 \\\\\n", - " --subnet-name clustrix-subnet \\\\\n", - " --subnet-prefix 10.1.0.0/24 \\\\\n", - " --location {location}\n", - "\n", - "# Create Network Security Group with restrictive rules\n", - "az network nsg create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --name clustrix-nsg \\\\\n", - " --location {location}\n", - "\n", - "# Allow SSH only from your IP (replace with your actual IP)\n", - "az network nsg rule create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --nsg-name clustrix-nsg \\\\\n", - " --name AllowSSHFromMyIP \\\\\n", - " --protocol tcp \\\\\n", - " --priority 1000 \\\\\n", - " --destination-port-range 22 \\\\\n", - " --source-address-prefixes YOUR_IP_ADDRESS/32 \\\\\n", - " --access allow\n", - "\n", - "# Allow internal communication\n", - "az network nsg rule create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --nsg-name clustrix-nsg \\\\\n", - " --name AllowVnetInbound \\\\\n", - " --protocol '*' \\\\\n", - " --priority 1001 \\\\\n", - " --source-address-prefixes 10.1.0.0/16 \\\\\n", - " --destination-address-prefixes 10.1.0.0/16 \\\\\n", - " --access allow\n", - "\n", - "# Create Key Vault for secrets management\n", - "az keyvault create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --name clustrix-keyvault-$(uuidgen | tr '[:upper:]' '[:lower:]' | cut -c1-8) \\\\\n", - " --location {location} \\\\\n", - " --enable-disk-encryption \\\\\n", - " --sku standard\n", - "\n", - "# Create managed identity for VMs\n", - "az identity create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --name clustrix-identity \\\\\n", - " --location {location}\n", - "\n", - "# Create storage account with private endpoint\n", - "az storage account create \\\\\n", - " --resource-group {resource_group} \\\\\n", - " --name clustrixstorage$(uuidgen | tr '[:upper:]' '[:lower:]' | cut -c1-8) \\\\\n", - " --location {location} \\\\\n", - " --sku Standard_LRS \\\\\n", - " --allow-blob-public-access false \\\\\n", - " --https-only true \\\\\n", - " --min-tls-version TLS1_2\n", - "\n", - "# Enable Azure Security Center\n", - "az security auto-provisioning-setting update \\\\\n", - " --name default \\\\\n", - " --auto-provision on\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'resource_group': resource_group,\n", - " 'location': location,\n", - " 'vnet_name': 'clustrix-vnet',\n", - " 'subnet_name': 'clustrix-subnet',\n", - " 'nsg_name': 'clustrix-nsg',\n", - " 'security_commands': security_commands\n", - " }\n", - "\n", - "security_config = setup_azure_security_for_clustrix()\n", - "\n", - "print(\"Azure Security Setup Commands:\")\n", - "print(security_config['security_commands'])\n", - "print(\"\\nIMPORTANT: Replace 'YOUR_IP_ADDRESS' with your actual public IP address!\")\n", - "print(\"Find your IP with: curl ifconfig.me\")" - ] - }, - { - "cell_type": "markdown", - "id": "8jwgahv9skt", - "metadata": {}, - "source": [ - "### Azure Security Checklist for Clustrix\n", - "\n", - "✓ **Authentication and Access**\n", - "- Use Azure Active Directory for authentication\n", - "- Enable managed identities instead of service principals when possible\n", - "- Restrict Network Security Groups to your IP address only\n", - "- Use private endpoints for storage accounts\n", - "\n", - "✓ **Infrastructure Security**\n", - "- Enable disk encryption for all VMs\n", - "- Use Azure Key Vault for secrets and certificates\n", - "- Enable Azure Security Center recommendations\n", - "- Use Azure Private Link for service connectivity\n", - "\n", - "✓ **Monitoring and Compliance**\n", - "- Enable diagnostic logging and monitoring\n", - "- Implement Azure Policy for compliance\n", - "- Use Azure Defender for cloud workload protection\n", - "- Regularly rotate access keys and certificates\n", - "\n", - "✓ **Cost and Resource Management**\n", - "- Set up cost alerts and spending limits\n", - "- Tag all resources for governance and cost tracking" - ] - }, - { - "cell_type": "markdown", - "id": "cost-management", - "metadata": {}, - "source": [ - "## Cost Management and Optimization" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "azure-cost-optimization", - "metadata": {}, - "outputs": [], - "source": [ - "# Import Clustrix cost monitoring for Azure\n", - "from clustrix import cost_tracking_decorator, get_cost_monitor, generate_cost_report, get_pricing_info\n", - "\n", - "# Example 1: Cost tracking with Azure VMs\n", - "@cost_tracking_decorator('azure', 'Standard_NC6s_v3')\n", - "@cluster(cores=6, memory=\"112GB\")\n", - "def azure_training_with_cost_tracking():\n", - " \"\"\"Example training function with Azure cost tracking.\"\"\"\n", - " import time\n", - " import numpy as np\n", - " \n", - " print(\"Starting Azure training with cost monitoring...\")\n", - " time.sleep(2) # Simulate training\n", - " \n", - " # Simulate ML workload\n", - " data = np.random.randn(1500, 1500)\n", - " result = np.linalg.qr(data)\n", - " \n", - " print(\"Training completed!\")\n", - " return {'accuracy': 0.89, 'training_time': 2.0}\n", - "\n", - "# Example 2: Compare Azure VM pricing\n", - "def compare_azure_pricing():\n", - " \"\"\"Compare Azure VM pricing for different instance types.\"\"\"\n", - " pricing = get_pricing_info('azure')\n", - " if pricing:\n", - " print(\"Azure VM Pay-as-you-go Pricing (USD/hour):\")\n", - " \n", - " # Group by category\n", - " gpu_vms = {k: v for k, v in pricing.items() if k.startswith('Standard_NC')}\n", - " general_vms = {k: v for k, v in pricing.items() if k.startswith('Standard_D')}\n", - " compute_vms = {k: v for k, v in pricing.items() if k.startswith('Standard_F')}\n", - " memory_vms = {k: v for k, v in pricing.items() if k.startswith('Standard_E')}\n", - " \n", - " print(\"\\nGPU VMs:\")\n", - " for vm, price in sorted(gpu_vms.items(), key=lambda x: x[1]):\n", - " print(f\" {vm:<25}: ${price:.3f}/hour\")\n", - " \n", - " print(\"\\nGeneral Purpose:\")\n", - " for vm, price in sorted(general_vms.items(), key=lambda x: x[1]):\n", - " print(f\" {vm:<25}: ${price:.3f}/hour\")\n", - " \n", - " print(\"\\nCompute Optimized:\")\n", - " for vm, price in sorted(compute_vms.items(), key=lambda x: x[1]):\n", - " print(f\" {vm:<25}: ${price:.3f}/hour\")\n", - "\n", - "# Example 3: Azure Spot VM savings analysis\n", - "def azure_spot_cost_analysis():\n", - " \"\"\"Analyze potential savings with Azure Spot VMs.\"\"\"\n", - " monitor = get_cost_monitor('azure')\n", - " if monitor:\n", - " print(\"Azure Spot VM Savings Analysis:\")\n", - " print(\"-\" * 40)\n", - " \n", - " vm_types = ['Standard_NC6s_v3', 'Standard_D4s_v3', 'Standard_F8s_v2', 'Standard_E8s_v3']\n", - " \n", - " for vm in vm_types:\n", - " pay_as_you_go = monitor.estimate_cost(vm, 1.0, use_spot=False)\n", - " spot = monitor.estimate_cost(vm, 1.0, use_spot=True)\n", - " savings = ((pay_as_you_go.hourly_rate - spot.hourly_rate) / pay_as_you_go.hourly_rate) * 100\n", - " \n", - " print(f\"{vm}:\")\n", - " print(f\" Pay-as-you-go: ${pay_as_you_go.hourly_rate:.3f}/hour\")\n", - " print(f\" Spot: ${spot.hourly_rate:.3f}/hour\")\n", - " print(f\" Savings: {savings:.1f}%\")\n", - " print()\n", - "\n", - "# Example 4: Azure Batch cost estimation\n", - "def estimate_azure_batch_costs():\n", - " \"\"\"Estimate costs for Azure Batch workloads.\"\"\"\n", - " monitor = get_cost_monitor('azure')\n", - " if monitor:\n", - " batch_estimate = monitor.estimate_batch_cost(\n", - " pool_name=\"clustrix-batch-pool\",\n", - " vm_size=\"Standard_D4s_v3\",\n", - " target_nodes=8,\n", - " estimated_duration_hours=2.0\n", - " )\n", - " \n", - " print(\"Azure Batch Cost Estimation:\")\n", - " print(f\" Pool Name: {batch_estimate['pool_name']}\")\n", - " print(f\" VM Size: {batch_estimate['vm_size']}\")\n", - " print(f\" Target Nodes: {batch_estimate['target_nodes']}\")\n", - " print(f\" Duration: {batch_estimate['estimated_duration_hours']} hours\")\n", - " print(f\" Total Compute Hours: {batch_estimate['total_compute_hours']}\")\n", - " print(f\" Estimated Cost: ${batch_estimate['estimated_cost']:.2f}\")\n", - " print(f\" Cost per Node-Hour: ${batch_estimate['cost_per_node_hour']:.3f}\")\n", - "\n", - "# Example 5: Regional pricing comparison\n", - "def compare_azure_regions():\n", - " \"\"\"Compare Azure pricing across different regions.\"\"\"\n", - " monitor = get_cost_monitor('azure')\n", - " if monitor:\n", - " print(\"Azure Regional Pricing Comparison for Standard_NC6s_v3:\")\n", - " print(\"-\" * 55)\n", - " \n", - " regional_pricing = monitor.get_region_pricing_comparison('Standard_NC6s_v3')\n", - " for region, pricing_info in regional_pricing.items():\n", - " print(f\"{region}:\")\n", - " print(f\" Pay-as-you-go: ${pricing_info['pay_as_you_go_hourly']:.3f}/hour\")\n", - " print(f\" Est. Spot: ${pricing_info['estimated_spot_hourly']:.3f}/hour\")\n", - " print()\n", - "\n", - "# Example 6: Real-time Azure cost monitoring\n", - "def monitor_azure_costs():\n", - " \"\"\"Monitor current Azure resource usage and costs.\"\"\"\n", - " report = generate_cost_report('azure', 'Standard_NC6s_v3')\n", - " if report:\n", - " print(\"Current Azure Resource Status:\")\n", - " print(f\" CPU Usage: {report['resource_usage']['cpu_percent']:.1f}%\")\n", - " print(f\" Memory Usage: {report['resource_usage']['memory_percent']:.1f}%\")\n", - " if report['resource_usage']['gpu_stats']:\n", - " print(f\" GPU Count: {len(report['resource_usage']['gpu_stats'])}\")\n", - " print(f\" Hourly Rate: ${report['cost_estimate']['hourly_rate']:.3f}\")\n", - " \n", - " if report['recommendations']:\n", - " print(\"\\nCost Optimization Recommendations:\")\n", - " for rec in report['recommendations']:\n", - " print(f\" • {rec}\")\n", - "\n", - "# Example 7: Spot VM configuration for cost savings\n", - "def configure_spot_vm():\n", - " \"\"\"Example configuration for using Azure Spot VMs.\"\"\"\n", - " configure(\n", - " cluster_type=\"ssh\",\n", - " cluster_host=\"your-spot-vm-ip\",\n", - " username=\"azureuser\",\n", - " key_file=\"~/.ssh/id_rsa\",\n", - " remote_work_dir=\"~/.clustrix/jobs\",\n", - " # Spot VMs can be evicted, so use shorter timeouts\n", - " default_time=\"00:30:00\",\n", - " job_poll_interval=60, # Check more frequently\n", - " cleanup_on_success=True # Clean up quickly\n", - " )\n", - " return \"Configured for Azure Spot VMs with appropriate timeouts.\"\n", - "\n", - "# Run examples\n", - "print(\"Azure Cost Monitoring Examples:\")\n", - "print(\"=\" * 40)\n", - "\n", - "print(\"\\n1. Azure VM Pricing Comparison:\")\n", - "compare_azure_pricing()\n", - "\n", - "print(\"\\n2. Spot VM Savings Analysis:\")\n", - "azure_spot_cost_analysis()\n", - "\n", - "print(\"\\n3. Azure Batch Cost Estimation:\")\n", - "estimate_azure_batch_costs()\n", - "\n", - "print(\"\\n4. Regional Pricing Comparison:\")\n", - "compare_azure_regions()\n", - "\n", - "print(\"\\n5. Current Azure Status:\")\n", - "monitor_azure_costs()\n", - "\n", - "print(\"\\n✅ Azure cost monitoring examples ready!\")\n", - "print(\"💡 Use @cost_tracking_decorator('azure', 'vm_size') for automatic cost tracking\")\n", - "\n", - "# Example spot VM configuration (uncomment to use)\n", - "# spot_config = configure_spot_vm()\n", - "# print(f\"Configuration result: {spot_config}\")" - ] - }, - { - "cell_type": "markdown", - "id": "hnir1aze4v", - "metadata": {}, - "source": [ - "### Azure Cost Optimization for Clustrix\n", - "\n", - "#### Cost Monitoring Commands\n", - "\n", - "```bash\n", - "# Set up budget alerts\n", - "az consumption budget create \\\n", - " --budget-name clustrix-monthly-budget \\\n", - " --amount 100 \\\n", - " --time-grain Monthly \\\n", - " --time-period-start 2025-01-01 \\\n", - " --time-period-end 2025-12-31\n", - "\n", - "# Get current costs\n", - "az consumption usage list \\\n", - " --start-date 2025-01-01 \\\n", - " --end-date 2025-01-31\n", - "\n", - "# List resource costs by resource group\n", - "az costmanagement query \\\n", - " --type Usage \\\n", - " --dataset-aggregation '{\"totalCost\":{\"name\":\"PreTaxCost\",\"function\":\"Sum\"}}' \\\n", - " --dataset-grouping name=ResourceGroup type=Dimension\n", - "\n", - "# Set up auto-shutdown for VMs\n", - "az vm auto-shutdown \\\n", - " --resource-group clustrix-tutorial-rg \\\n", - " --name clustrix-vm-01 \\\n", - " --time 1900 \\\n", - " --email your-email@example.com\n", - "```\n", - "\n", - "#### Cost Optimization Recommendations\n", - "\n", - "1. **Use Spot VMs** for batch processing (up to 90% savings)\n", - "2. **Enable auto-shutdown** for dev resources\n", - "3. **Implement lifecycle policies** for blob storage\n", - "4. **Set up budget alerts** and spending limits\n", - "5. **Regular cost reviews** and resource optimization\n", - "6. **Use reserved instances** for predictable workloads\n", - "7. **Choose appropriate VM sizes** based on actual usage" - ] - }, - { - "cell_type": "markdown", - "id": "scseti9hu", - "metadata": {}, - "source": [ - "### Azure Cost Optimization for Clustrix\n", - "\n", - "#### 1. Compute Optimization\n", - "- **Use Azure Spot VMs** for non-critical workloads (up to 90% savings)\n", - "- **Choose B-series burstable VMs** for variable workloads\n", - "- **Use reserved instances** for predictable workloads (1-3 year terms)\n", - "- **Enable auto-shutdown** for dev/test VMs\n", - "- **Right-size VMs** based on actual usage\n", - "\n", - "#### 2. Storage Optimization\n", - "- **Use appropriate storage tiers** (Hot, Cool, Archive)\n", - "- **Enable lifecycle management** for blob storage\n", - "- **Use managed disks** with appropriate performance tiers\n", - "- **Implement data deduplication** and compression\n", - "\n", - "#### 3. Network Optimization\n", - "- **Minimize data transfer** between regions\n", - "- **Use Azure CDN** for static content\n", - "- **Optimize data transfer** patterns\n", - "\n", - "#### 4. Monitoring and Management\n", - "- **Set up budget alerts** and spending limits\n", - "- **Use Azure Cost Management + Billing**\n", - "- **Implement proper resource tagging**\n", - "- **Regular cost reviews** and optimizations\n", - "\n", - "#### 5. Service-Specific\n", - "- **Use Azure Functions** for small, event-driven tasks\n", - "- **Consider Azure Container Instances** for short-running jobs\n", - "- **Use Azure Batch** for large-scale parallel processing" - ] - }, - { - "cell_type": "markdown", - "id": "cleanup-azure", - "metadata": {}, - "source": [ - "## Resource Cleanup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cleanup-azure-resources", - "metadata": {}, - "outputs": [], - "source": [ - "def cleanup_azure_resources(resource_group='clustrix-tutorial-rg'):\n", - " \"\"\"\n", - " Clean up Azure resources to avoid ongoing charges.\n", - " \n", - " Args:\n", - " resource_group: Name of the resource group to clean up\n", - " \"\"\"\n", - " \n", - " cleanup_commands = f\"\"\"\n", - "# List all resources in the resource group\n", - "az resource list --resource-group {resource_group} --output table\n", - "\n", - "# Stop all VMs first (to gracefully shut down)\n", - "az vm deallocate --resource-group {resource_group} --name clustrix-vm-01\n", - "\n", - "# Delete specific resources individually (optional - more granular control)\n", - "# az vm delete --resource-group {resource_group} --name clustrix-vm-01 --yes\n", - "# az disk delete --resource-group {resource_group} --name clustrix-vm-01_disk1_* --yes\n", - "# az network public-ip delete --resource-group {resource_group} --name clustrix-vm-01PublicIP\n", - "\n", - "# WARNING: Delete the entire resource group (removes ALL resources)\n", - "az group delete --name {resource_group} --yes --no-wait\n", - "\n", - "# Verify deletion\n", - "az group list --output table | grep {resource_group}\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'resource_group': resource_group,\n", - " 'cleanup_commands': cleanup_commands\n", - " }\n", - "\n", - "cleanup_info = cleanup_azure_resources()\n", - "\n", - "print(f\"Azure Resource Cleanup Commands for Resource Group: {cleanup_info['resource_group']}\")\n", - "print(\"=\" * 70)\n", - "print(cleanup_info['cleanup_commands'])\n", - "print(\"\\n\" + \"⚠️ \" * 10 + \" IMPORTANT WARNINGS \" + \"⚠️ \" * 10)\n", - "print(\"1. The 'az group delete' command will permanently delete ALL resources in the group!\")\n", - "print(\"2. Review the resources first with 'az resource list' before proceeding\")\n", - "print(\"3. Make sure to backup any important data before deletion\")\n", - "print(\"4. Consider stopping VMs instead of deleting if you plan to use them again\")\n", - "print(\"5. Deleted resources cannot be recovered - this action is irreversible!\")\n", - "print(\"=\" * 70)" - ] - }, - { - "cell_type": "markdown", - "id": "advanced-azure-example", - "metadata": {}, - "source": [ - "## Advanced Example: Distributed Image Processing" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "image-processing-example", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=4, memory=\"8GB\", time=\"00:45:00\")\n", - "def azure_image_processing_pipeline(storage_config, processing_params):\n", - " \"\"\"\n", - " Distributed image processing pipeline using Azure Blob Storage.\n", - " \"\"\"\n", - " from azure.storage.blob import BlobServiceClient\n", - " from azure.identity import DefaultAzureCredential\n", - " import numpy as np\n", - " from PIL import Image\n", - " import io\n", - " import time\n", - " \n", - " # Connect to Azure Blob Storage\n", - " account_url = f\"https://{storage_config['account_name']}.blob.core.windows.net\"\n", - " credential = DefaultAzureCredential()\n", - " blob_service_client = BlobServiceClient(account_url=account_url, credential=credential)\n", - " \n", - " container_client = blob_service_client.get_container_client(storage_config['container'])\n", - " \n", - " processed_images = []\n", - " processing_stats = []\n", - " \n", - " # List images to process\n", - " blob_list = container_client.list_blobs(name_starts_with=storage_config['input_prefix'])\n", - " \n", - " for blob in blob_list:\n", - " if blob.name.lower().endswith(('.png', '.jpg', '.jpeg')):\n", - " start_time = time.time()\n", - " \n", - " try:\n", - " # Download image\n", - " blob_client = blob_service_client.get_blob_client(\n", - " container=storage_config['container'], blob=blob.name\n", - " )\n", - " image_data = blob_client.download_blob().readall()\n", - " \n", - " # Process image\n", - " image = Image.open(io.BytesIO(image_data))\n", - " \n", - " # Apply processing operations\n", - " if processing_params.get('resize'):\n", - " image = image.resize(processing_params['resize'])\n", - " \n", - " if processing_params.get('grayscale'):\n", - " image = image.convert('L')\n", - " \n", - " if processing_params.get('rotate'):\n", - " image = image.rotate(processing_params['rotate'])\n", - " \n", - " # Convert back to bytes\n", - " output_buffer = io.BytesIO()\n", - " image.save(output_buffer, format='PNG')\n", - " output_buffer.seek(0)\n", - " \n", - " # Upload processed image\n", - " output_blob_name = blob.name.replace(\n", - " storage_config['input_prefix'], \n", - " storage_config['output_prefix']\n", - " )\n", - " \n", - " output_blob_client = blob_service_client.get_blob_client(\n", - " container=storage_config['container'], blob=output_blob_name\n", - " )\n", - " output_blob_client.upload_blob(output_buffer.getvalue(), overwrite=True)\n", - " \n", - " processing_time = time.time() - start_time\n", - " \n", - " processed_images.append(output_blob_name)\n", - " processing_stats.append({\n", - " 'input_blob': blob.name,\n", - " 'output_blob': output_blob_name,\n", - " 'processing_time': processing_time,\n", - " 'original_size': image.size,\n", - " 'processed_size': image.size\n", - " })\n", - " \n", - " except Exception as e:\n", - " print(f\"Error processing {blob.name}: {e}\")\n", - " \n", - " return {\n", - " 'processed_count': len(processed_images),\n", - " 'total_processing_time': sum(stat['processing_time'] for stat in processing_stats),\n", - " 'average_processing_time': np.mean([stat['processing_time'] for stat in processing_stats]) if processing_stats else 0,\n", - " 'processed_images': processed_images[:10], # First 10 for brevity\n", - " 'processing_stats': processing_stats[:5] # First 5 for brevity\n", - " }\n", - "\n", - "# Example usage (uncomment and modify as needed):\n", - "# storage_config = {\n", - "# 'account_name': 'yourstorageaccount',\n", - "# 'container': 'images',\n", - "# 'input_prefix': 'raw/',\n", - "# 'output_prefix': 'processed/'\n", - "# }\n", - "# \n", - "# processing_config = {\n", - "# 'resize': (800, 600),\n", - "# 'grayscale': True,\n", - "# 'rotate': 0\n", - "# }\n", - "# \n", - "# result = azure_image_processing_pipeline(storage_config, processing_config)\n", - "# print(f\"Processed {result['processed_count']} images in {result['total_processing_time']:.2f} seconds\")\n", - "\n", - "print(\"Advanced image processing pipeline example defined.\")" - ] - }, - { - "cell_type": "markdown", - "id": "azure-summary", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "This tutorial covered:\n", - "\n", - "1. **Setup**: Azure authentication and Clustrix installation\n", - "2. **VM Integration**: Direct Azure VM configuration\n", - "3. **Azure Batch**: Managed job scheduling\n", - "4. **CycleCloud**: HPC-optimized clusters with SLURM\n", - "5. **Blob Storage**: Data storage and retrieval\n", - "6. **Azure ML**: Machine learning compute integration\n", - "7. **Security**: Best practices for safe deployment\n", - "8. **Cost Management**: Strategies to minimize expenses\n", - "9. **Resource Management**: Proper cleanup procedures\n", - "\n", - "### Next Steps\n", - "\n", - "- Set up your Azure credentials and test the basic configuration\n", - "- Start with a simple VM for initial testing\n", - "- Consider CycleCloud for production HPC workloads\n", - "- Implement proper monitoring and cost controls\n", - "- Explore Azure Spot VMs for cost-effective batch processing\n", - "\n", - "### Azure-Specific Advantages\n", - "\n", - "- **CycleCloud**: Best-in-class HPC cluster management\n", - "- **Azure ML**: Integrated machine learning platform\n", - "- **Hybrid Cloud**: Seamless integration with on-premises\n", - "- **Enterprise Integration**: Active Directory and enterprise tools\n", - "- **Compliance**: Strong compliance and security certifications\n", - "\n", - "### Resources\n", - "\n", - "- [Azure CycleCloud Documentation](https://docs.microsoft.com/en-us/azure/cyclecloud/)\n", - "- [Azure Batch Documentation](https://docs.microsoft.com/en-us/azure/batch/)\n", - "- [Azure Machine Learning Documentation](https://docs.microsoft.com/en-us/azure/machine-learning/)\n", - "- [Azure HPC Documentation](https://docs.microsoft.com/en-us/azure/architecture/topics/high-performance-computing/)\n", - "- [Clustrix Documentation](https://clustrix.readthedocs.io/)\n", - "\n", - "**Remember**: Always monitor your Azure costs and clean up resources when not in use!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/cost_monitoring_tutorial.ipynb b/docs/source/notebooks/cost_monitoring_tutorial.ipynb deleted file mode 100644 index c4256a89..00000000 --- a/docs/source/notebooks/cost_monitoring_tutorial.ipynb +++ /dev/null @@ -1,990 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "cell-0", - "metadata": {}, - "source": [ - "# Cloud Cost Monitoring and Optimization\n", - "\n", - "This tutorial demonstrates Clustrix's comprehensive cost monitoring features for cloud platforms. Learn how to track expenses, optimize resource usage, and make informed decisions about cloud infrastructure.\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/cost_monitoring_tutorial.ipynb)\n", - "\n", - "## Overview\n", - "\n", - "Clustrix provides built-in cost monitoring for multiple cloud platforms:\n", - "\n", - "- **Amazon Web Services (AWS)**: EC2, ECS, Batch, Lambda, SageMaker\n", - "- **Google Cloud Platform (GCP)**: Compute Engine, GKE, Cloud Batch, Vertex AI\n", - "- **Microsoft Azure**: Virtual Machines, AKS, Batch, ML Compute\n", - "- **Lambda Cloud**: GPU instances for ML workloads\n", - "- **Hugging Face Spaces**: Inference endpoints and Spaces hardware\n", - "\n", - "## Key Features\n", - "\n", - "- **Automatic Cost Tracking**: Decorator-based cost monitoring\n", - "- **Real-time Pricing**: Up-to-date pricing information\n", - "- **Regional Comparisons**: Find the most cost-effective regions\n", - "- **Optimization Recommendations**: Automatic suggestions for cost savings\n", - "- **Multi-cloud Support**: Compare costs across different providers\n", - "\n", - "> **What this actually is.** `cost_tracking_decorator` and friends run your\n", - "> function **locally, in this process** and estimate cost from a static,\n", - "> hardcoded pricing table -- they never call AWS/GCP/Azure/Lambda billing or\n", - "> compute APIs, and they never provision anything. This entire notebook is\n", - "> safe to run top to bottom with no cloud credentials and no risk of\n", - "> real charges. If you want the function to actually execute on a remote\n", - "> cluster, stack `@cluster(...)` underneath (see the example below) --\n", - "> and note that only `cluster_type=\"slurm\"`, `\"ssh\"`, `\"huggingface\"` and\n", - "> `\"local\"` are verified to run a job end to end; see :ref:`limitations`." - ] - }, - { - "cell_type": "markdown", - "id": "cell-1", - "metadata": {}, - "source": [ - "## Installation\n", - "\n", - "Install Clustrix with cost monitoring support:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-2", - "metadata": {}, - "outputs": [], - "source": [ - "# Install Clustrix\n", - "!pip install clustrix\n", - "\n", - "# Import cost monitoring functions\n", - "from clustrix import (\n", - " cost_tracking_decorator,\n", - " get_cost_monitor,\n", - " start_cost_monitoring,\n", - " generate_cost_report,\n", - " get_pricing_info\n", - ")\n", - "\n", - "import numpy as np\n", - "import pandas as pd\n", - "import matplotlib.pyplot as plt\n", - "import time" - ] - }, - { - "cell_type": "markdown", - "id": "cell-3", - "metadata": {}, - "source": [ - "## Basic Cost Monitoring\n", - "\n", - "### Getting Pricing Information" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-4", - "metadata": {}, - "outputs": [], - "source": [ - "# Get pricing information for different cloud providers\n", - "print(\"=== AWS EC2 Pricing (Top 10 Instance Types) ===\")\n", - "aws_pricing = get_pricing_info('aws')\n", - "for instance_type, price in list(aws_pricing.items())[:10]:\n", - " print(f\"{instance_type:20} ${price:.4f}/hour\")\n", - "\n", - "print(\"\\n=== GCP Compute Engine Pricing (Top 10 Instance Types) ===\")\n", - "gcp_pricing = get_pricing_info('gcp')\n", - "for instance_type, price in list(gcp_pricing.items())[:10]:\n", - " print(f\"{instance_type:20} ${price:.4f}/hour\")\n", - "\n", - "print(\"\\n=== Azure VM Pricing (Top 10 Instance Types) ===\")\n", - "azure_pricing = get_pricing_info('azure')\n", - "for instance_type, price in list(azure_pricing.items())[:10]:\n", - " print(f\"{instance_type:20} ${price:.4f}/hour\")\n", - "\n", - "print(f\"\\nTotal instance types available:\")\n", - "print(f\" AWS: {len(aws_pricing)}\")\n", - "print(f\" GCP: {len(gcp_pricing)}\")\n", - "print(f\" Azure: {len(azure_pricing)}\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-5", - "metadata": {}, - "source": [ - "### Manual Cost Monitoring" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-6", - "metadata": {}, - "outputs": [], - "source": [ - "# Example: Manual cost monitoring for a computation\n", - "def simulate_computation(duration_seconds=5):\n", - " \"\"\"Simulate a computation that takes some time.\"\"\"\n", - " start_time = time.time()\n", - " \n", - " # Simulate CPU-intensive work\n", - " result = 0\n", - " while time.time() - start_time < duration_seconds:\n", - " result += np.random.random((1000, 1000)).sum()\n", - " \n", - " return result\n", - "\n", - "# Monitor cost for AWS\n", - "print(\"=== AWS Cost Monitoring Example ===\")\n", - "monitor = start_cost_monitoring('aws')\n", - "\n", - "# Run computation\n", - "result = simulate_computation(3)\n", - "\n", - "# generate_cost_report(provider, instance_type) has no duration_seconds\n", - "# parameter -- it always estimates a flat 1-hour cost, independent of\n", - "# whatever start_cost_monitoring()/simulate_computation() above actually\n", - "# measured. Its returned dict has keys timestamp/provider/resource_usage/\n", - "# cost_estimate/recommendations -- there is no top-level 'instance_type'\n", - "# or 'duration_seconds' key.\n", - "cost_report = generate_cost_report('aws', 't3.medium')\n", - "print(f\"Provider: {cost_report['provider']}\")\n", - "print(f\"Hourly Rate: ${cost_report['cost_estimate']['hourly_rate']:.4f}\")\n", - "print(f\"Estimated Cost (1hr): ${cost_report['cost_estimate']['estimated_cost']:.6f}\")\n", - "\n", - "# Compare 1-hour cost estimates across providers (generate_cost_report\n", - "# has no duration parameter, so this compares hourly rates, not a\n", - "# 3-second computation)\n", - "print(\"\\n=== 1-Hour Cost Comparison Across Providers ===\")\n", - "providers_and_instances = [\n", - " ('aws', 't3.medium'),\n", - " ('gcp', 'n2-standard-2'),\n", - " ('azure', 'Standard_D2s_v3')\n", - "]\n", - "\n", - "for provider, instance in providers_and_instances:\n", - " report = generate_cost_report(provider, instance)\n", - " print(f\"{provider.upper():5} {instance:20} ${report['cost_estimate']['estimated_cost']:.6f}\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-7", - "metadata": {}, - "source": [ - "## Automatic Cost Tracking with Decorators\n", - "\n", - "The easiest way to track costs is using the `@cost_tracking_decorator`:\n", - "\n", - "**Behind the scenes:** `@cost_tracking_decorator(provider, instance_type)`\n", - "wraps your function, calls `monitor.start_monitoring()`, calls your function\n", - "*directly in this process* (`func(*args, **kwargs)` -- no serialization, no\n", - "remote submission, no `@cluster` involved unless you stack one underneath),\n", - "then calls `monitor.stop_monitoring()` and returns\n", - "`{\"result\": ..., \"success\": ..., \"cost_report\": ...}`. The \"cost\" is an\n", - "estimate from `clustrix/cost_providers/{aws,gcp,azure,lambda_cloud}.py`'s\n", - "static pricing tables, not a real billing API call -- nothing below talks to\n", - "AWS, GCP or Azure at all. Stack it with `@cluster` (decorator order:\n", - "`@cost_tracking_decorator` outermost, `@cluster` innermost) if you actually\n", - "want the function to run remotely; see :ref:`execution-model` for what\n", - "`@cluster` itself then does." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-8", - "metadata": {}, - "outputs": [], - "source": [ - "# Example 1: AWS Cost Tracking\n", - "@cost_tracking_decorator('aws', 't3.xlarge')\n", - "def aws_ml_training():\n", - " \"\"\"Example ML training with automatic AWS cost tracking.\"\"\"\n", - " from sklearn.ensemble import RandomForestClassifier\n", - " from sklearn.datasets import make_classification\n", - " from sklearn.model_selection import train_test_split\n", - " import time\n", - " \n", - " # Generate dataset\n", - " X, y = make_classification(\n", - " n_samples=10000, n_features=20, n_classes=3, n_informative=10, random_state=42\n", - " )\n", - " X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", - " \n", - " # Train model\n", - " start_time = time.time()\n", - " model = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)\n", - " model.fit(X_train, y_train)\n", - " training_time = time.time() - start_time\n", - " \n", - " # Evaluate\n", - " accuracy = model.score(X_test, y_test)\n", - " \n", - " return {\n", - " 'accuracy': accuracy,\n", - " 'training_time': training_time,\n", - " 'samples_trained': len(X_train)\n", - " }\n", - "\n", - "# Example 2: GCP Cost Tracking\n", - "@cost_tracking_decorator('gcp', 'a2-highgpu-1g')\n", - "def gcp_gpu_computation():\n", - " \"\"\"Example GPU computation with automatic GCP cost tracking.\"\"\"\n", - " import numpy as np\n", - " import time\n", - " \n", - " start_time = time.time()\n", - " \n", - " # Simulate GPU-intensive work\n", - " matrices = []\n", - " for i in range(10):\n", - " A = np.random.rand(1000, 1000)\n", - " B = np.random.rand(1000, 1000)\n", - " C = np.dot(A, B)\n", - " matrices.append(C)\n", - " \n", - " result = np.mean([m.sum() for m in matrices])\n", - " computation_time = time.time() - start_time\n", - " \n", - " return {\n", - " 'result': result,\n", - " 'computation_time': computation_time,\n", - " 'matrices_processed': len(matrices)\n", - " }\n", - "\n", - "# Example 3: Azure Cost Tracking\n", - "@cost_tracking_decorator('azure', 'Standard_NC6')\n", - "def azure_deep_learning():\n", - " \"\"\"Example deep learning with automatic Azure cost tracking.\"\"\"\n", - " import numpy as np\n", - " import time\n", - " \n", - " # Simulate neural network training\n", - " start_time = time.time()\n", - " \n", - " # Simulate epochs\n", - " losses = []\n", - " for epoch in range(5):\n", - " # Simulate batch processing\n", - " batch_losses = []\n", - " for batch in range(100):\n", - " # Simulate forward and backward pass\n", - " loss = np.random.exponential(1.0) * np.exp(-epoch * 0.1)\n", - " batch_losses.append(loss)\n", - " \n", - " epoch_loss = np.mean(batch_losses)\n", - " losses.append(epoch_loss)\n", - " \n", - " training_time = time.time() - start_time\n", - " \n", - " return {\n", - " 'final_loss': losses[-1],\n", - " 'all_losses': losses,\n", - " 'training_time': training_time,\n", - " 'epochs': len(losses)\n", - " }\n", - "\n", - "# Run examples and display costs\n", - "print(\"=== Running Cost-Tracked Functions ===\")\n", - "\n", - "# AWS Example\n", - "print(\"\\n1. AWS ML Training:\")\n", - "aws_result = aws_ml_training()\n", - "if aws_result['success']:\n", - " print(f\" ✓ Accuracy: {aws_result['result']['accuracy']:.4f}\")\n", - " print(f\" ✓ Duration: {aws_result['cost_report']['duration_seconds']:.2f}s\")\n", - " print(f\" 💰 Cost: ${aws_result['cost_report']['cost_estimate']['estimated_cost']:.6f}\")\n", - "\n", - "# GCP Example\n", - "print(\"\\n2. GCP GPU Computation:\")\n", - "gcp_result = gcp_gpu_computation()\n", - "if gcp_result['success']:\n", - " print(f\" ✓ Matrices Processed: {gcp_result['result']['matrices_processed']}\")\n", - " print(f\" ✓ Duration: {gcp_result['cost_report']['duration_seconds']:.2f}s\")\n", - " print(f\" 💰 Cost: ${gcp_result['cost_report']['cost_estimate']['estimated_cost']:.6f}\")\n", - "\n", - "# Azure Example\n", - "print(\"\\n3. Azure Deep Learning:\")\n", - "azure_result = azure_deep_learning()\n", - "if azure_result['success']:\n", - " print(f\" ✓ Final Loss: {azure_result['result']['final_loss']:.4f}\")\n", - " print(f\" ✓ Duration: {azure_result['cost_report']['duration_seconds']:.2f}s\")\n", - " print(f\" 💰 Cost: ${azure_result['cost_report']['cost_estimate']['estimated_cost']:.6f}\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-9", - "metadata": {}, - "source": [ - "## Advanced Cost Analysis\n", - "\n", - "### Regional Pricing Comparison" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-10", - "metadata": {}, - "outputs": [], - "source": [ - "# AWS Regional Pricing Comparison\n", - "aws_monitor = get_cost_monitor('aws')\n", - "\n", - "print(\"=== AWS Regional Pricing Comparison (t3.large) ===\")\n", - "instance_type = 't3.large'\n", - "\n", - "# There is no get_region_pricing(region) method -- pricing is compared\n", - "# per-instance-type across a fixed, built-in set of regions instead.\n", - "aws_regional_pricing = aws_monitor.get_region_pricing_comparison(instance_type)\n", - "regional_prices = [\n", - " (region, info['on_demand_hourly'])\n", - " for region, info in aws_regional_pricing.items()\n", - "]\n", - "for region, price in regional_prices:\n", - " print(f\"{region:15} ${price:.4f}/hour\")\n", - "\n", - "# Find cheapest and most expensive regions\n", - "regional_prices.sort(key=lambda x: x[1])\n", - "print(f\"\\nCheapest: {regional_prices[0][0]} (${regional_prices[0][1]:.4f}/hour)\")\n", - "print(f\"Most Expensive: {regional_prices[-1][0]} (${regional_prices[-1][1]:.4f}/hour)\")\n", - "savings = (1 - regional_prices[0][1] / regional_prices[-1][1]) * 100\n", - "print(f\"Potential Savings: {savings:.1f}%\")\n", - "\n", - "# GCP Regional Pricing Comparison\n", - "gcp_monitor = get_cost_monitor('gcp')\n", - "\n", - "print(\"\\n=== GCP Regional Pricing Comparison (n2-standard-4) ===\")\n", - "gcp_regional_pricing = gcp_monitor.get_region_pricing_comparison('n2-standard-4')\n", - "for region, pricing in list(gcp_regional_pricing.items())[:5]:\n", - " print(f\"{region:20} On-Demand: ${pricing['on_demand_hourly']:.4f}/hr, \"\n", - " f\"Preemptible: ${pricing['preemptible_hourly']:.4f}/hr\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-11", - "metadata": {}, - "source": [ - "### Spot/Preemptible Instance Savings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-12", - "metadata": {}, - "outputs": [], - "source": [ - "# Compare on-demand vs spot/preemptible pricing\n", - "print(\"=== On-Demand vs Spot/Preemptible Pricing Comparison ===\")\n", - "\n", - "# AWS Spot Instances\n", - "print(\"\\nAWS Spot Instances:\")\n", - "aws_instances = ['t3.large', 'm5.xlarge', 'c5.2xlarge', 'r5.large']\n", - "for instance in aws_instances:\n", - " on_demand = aws_monitor.estimate_cost(instance, 1.0)\n", - " spot = aws_monitor.estimate_cost(instance, 1.0, use_spot=True)\n", - " savings = (1 - spot.hourly_rate / on_demand.hourly_rate) * 100\n", - " print(f\"{instance:15} On-Demand: ${on_demand.hourly_rate:.4f}/hr, \"\n", - " f\"Spot: ${spot.hourly_rate:.4f}/hr ({savings:.0f}% savings)\")\n", - "\n", - "# GCP Preemptible VMs\n", - "print(\"\\nGCP Preemptible VMs:\")\n", - "gcp_instances = ['n2-standard-4', 'c2-standard-4', 'n2-highmem-4', 'a2-highgpu-1g']\n", - "for instance in gcp_instances:\n", - " on_demand = gcp_monitor.estimate_cost(instance, 1.0)\n", - " preemptible = gcp_monitor.estimate_cost(instance, 1.0, use_preemptible=True)\n", - " savings = (1 - preemptible.hourly_rate / on_demand.hourly_rate) * 100\n", - " print(f\"{instance:20} On-Demand: ${on_demand.hourly_rate:.4f}/hr, \"\n", - " f\"Preemptible: ${preemptible.hourly_rate:.4f}/hr ({savings:.0f}% savings)\")\n", - "\n", - "# Azure Spot VMs\n", - "azure_monitor = get_cost_monitor('azure')\n", - "print(\"\\nAzure Spot VMs:\")\n", - "azure_instances = ['Standard_D4s_v3', 'Standard_E4s_v3', 'Standard_F4s_v2']\n", - "for instance in azure_instances:\n", - " on_demand = azure_monitor.estimate_cost(instance, 1.0)\n", - " spot = azure_monitor.estimate_cost(instance, 1.0, use_spot=True)\n", - " savings = (1 - spot.hourly_rate / on_demand.hourly_rate) * 100\n", - " print(f\"{instance:20} On-Demand: ${on_demand.hourly_rate:.4f}/hr, \"\n", - " f\"Spot: ${spot.hourly_rate:.4f}/hr ({savings:.0f}% savings)\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-13", - "metadata": {}, - "source": [ - "### Batch Job Cost Estimation" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-14", - "metadata": {}, - "outputs": [], - "source": [ - "# Estimate costs for batch processing jobs\n", - "def estimate_batch_job_costs(job_config):\n", - " \"\"\"Estimate costs for a batch processing job across multiple providers.\"\"\"\n", - " results = {}\n", - " \n", - " # AWS Batch -- note the different parameter names: AWS's Batch API is\n", - " # modeled around job queues/compute environments/job counts, not\n", - " # machine_type/instance_count like GCP and Azure below.\n", - " aws_batch_cost = aws_monitor.estimate_batch_cost(\n", - " job_queue=job_config['name'],\n", - " compute_environment='default',\n", - " estimated_jobs=job_config['instance_count'],\n", - " avg_job_duration_hours=job_config['duration_hours']\n", - " )\n", - " results['aws'] = aws_batch_cost\n", - " \n", - " # GCP Batch\n", - " gcp_batch_cost = gcp_monitor.estimate_batch_cost(\n", - " job_name=job_config['name'],\n", - " machine_type=job_config['gcp_instance'],\n", - " instance_count=job_config['instance_count'],\n", - " estimated_duration_hours=job_config['duration_hours']\n", - " )\n", - " results['gcp'] = gcp_batch_cost\n", - " \n", - " # Azure Batch -- Azure's own parameter names again: pool_name/vm_size/\n", - " # target_nodes rather than job_name/machine_type/instance_count.\n", - " azure_batch_cost = azure_monitor.estimate_batch_cost(\n", - " pool_name=job_config['name'],\n", - " vm_size=job_config['azure_instance'],\n", - " target_nodes=job_config['instance_count'],\n", - " estimated_duration_hours=job_config['duration_hours']\n", - " )\n", - " results['azure'] = azure_batch_cost\n", - " \n", - " return results\n", - "\n", - "# Example batch job configuration\n", - "batch_job = {\n", - " 'name': 'large-scale-data-processing',\n", - " 'instance_count': 50,\n", - " 'duration_hours': 4.5,\n", - " 'aws_instance': 'c5.4xlarge',\n", - " 'gcp_instance': 'c2-standard-16',\n", - " 'azure_instance': 'Standard_F16s_v2'\n", - "}\n", - "\n", - "print(\"=== Batch Job Cost Estimation ===\")\n", - "print(f\"Job: {batch_job['name']}\")\n", - "print(f\"Instances: {batch_job['instance_count']}\")\n", - "print(f\"Duration: {batch_job['duration_hours']} hours\\n\")\n", - "\n", - "batch_costs = estimate_batch_job_costs(batch_job)\n", - "\n", - "# Each provider's dict has a different key for the instance/pool name\n", - "# (aws: 'job_queue', gcp: 'machine_type', azure: 'vm_size'), so only the\n", - "# fields common to all three (total_compute_hours, estimated_cost) are\n", - "# printed generically here.\n", - "for provider, cost_info in batch_costs.items():\n", - " print(f\"{provider.upper()}:\")\n", - " print(f\" Total Compute Hours: {cost_info['total_compute_hours']}\")\n", - " print(f\" Estimated Cost: ${cost_info['estimated_cost']:.2f}\")\n", - " print()\n", - "\n", - "# Find most cost-effective provider\n", - "cheapest = min(batch_costs.items(), key=lambda x: x[1]['estimated_cost'])\n", - "print(f\"Most cost-effective: {cheapest[0].upper()} (${cheapest[1]['estimated_cost']:.2f})\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-15", - "metadata": {}, - "source": [ - "## Cost Optimization Strategies\n", - "\n", - "### Sustained Use and Reserved Instance Analysis" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-16", - "metadata": {}, - "outputs": [], - "source": [ - "# AWS Reserved Instance Savings\n", - "print(\"=== AWS Reserved Instance Savings Analysis ===\")\n", - "instance_type = 'm5.xlarge'\n", - "monthly_hours = 720 # Full month\n", - "\n", - "# Calculate costs for different commitment levels\n", - "on_demand_monthly = aws_monitor.estimate_cost(instance_type, monthly_hours).estimated_cost\n", - "ri_1yr_no_upfront = on_demand_monthly * 0.62 # ~38% discount\n", - "ri_3yr_no_upfront = on_demand_monthly * 0.50 # ~50% discount\n", - "ri_3yr_all_upfront = on_demand_monthly * 0.38 # ~62% discount\n", - "\n", - "print(f\"Instance Type: {instance_type}\")\n", - "print(f\"Monthly Usage: {monthly_hours} hours\\n\")\n", - "print(f\"On-Demand: ${on_demand_monthly:.2f}/month\")\n", - "print(f\"1-Year RI (No Up): ${ri_1yr_no_upfront:.2f}/month (38% savings)\")\n", - "print(f\"3-Year RI (No Up): ${ri_3yr_no_upfront:.2f}/month (50% savings)\")\n", - "print(f\"3-Year RI (All Up): ${ri_3yr_all_upfront:.2f}/month (62% savings)\")\n", - "\n", - "# GCP Sustained Use Discounts\n", - "print(\"\\n=== GCP Sustained Use Discount Analysis ===\")\n", - "usage_levels = [25, 50, 75, 100] # Percentage of month\n", - "\n", - "for usage_pct in usage_levels:\n", - " hours = (usage_pct / 100) * monthly_hours\n", - " discount_info = gcp_monitor.estimate_sustained_use_discount(hours)\n", - " \n", - " base_cost = gcp_monitor.estimate_cost('n2-standard-4', hours).estimated_cost\n", - " discounted_cost = base_cost * (1 - discount_info['discount_percentage'] / 100)\n", - " \n", - " print(f\"{usage_pct}% usage ({hours:.0f} hours): \"\n", - " f\"{discount_info['discount_percentage']:.0f}% discount, \"\n", - " f\"${base_cost:.2f} → ${discounted_cost:.2f}\")\n", - "\n", - "# Azure Reserved Instance Analysis\n", - "print(\"\\n=== Azure Reserved Instance Savings ===\")\n", - "azure_instance = 'Standard_D4s_v3'\n", - "azure_on_demand = azure_monitor.estimate_cost(azure_instance, monthly_hours).estimated_cost\n", - "\n", - "print(f\"Instance Type: {azure_instance}\")\n", - "print(f\"On-Demand: ${azure_on_demand:.2f}/month\")\n", - "print(f\"1-Year Reserved: ${azure_on_demand * 0.62:.2f}/month (38% savings)\")\n", - "print(f\"3-Year Reserved: ${azure_on_demand * 0.42:.2f}/month (58% savings)\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-17", - "metadata": {}, - "source": [ - "### Workload-Specific Recommendations" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-18", - "metadata": {}, - "outputs": [], - "source": [ - "def get_cost_optimization_recommendations(workload_type, requirements):\n", - " \"\"\"Get cost optimization recommendations based on workload characteristics.\"\"\"\n", - " recommendations = []\n", - " \n", - " if workload_type == 'batch_processing':\n", - " recommendations.extend([\n", - " \"Use spot/preemptible instances for up to 80% savings\",\n", - " \"Implement checkpointing to handle instance termination\",\n", - " \"Consider time-flexible scheduling for lowest spot prices\",\n", - " \"Use auto-scaling to optimize resource utilization\"\n", - " ])\n", - " \n", - " elif workload_type == 'ml_training':\n", - " recommendations.extend([\n", - " \"Use GPU instances only when necessary\",\n", - " \"Consider using preemptible GPUs for experimentation\",\n", - " \"Implement gradient checkpointing for long training runs\",\n", - " \"Use mixed precision training to reduce memory usage\"\n", - " ])\n", - " \n", - " elif workload_type == 'web_service':\n", - " recommendations.extend([\n", - " \"Use reserved instances for predictable base load\",\n", - " \"Implement auto-scaling for variable traffic\",\n", - " \"Consider serverless options for sporadic workloads\",\n", - " \"Use CDN to reduce compute requirements\"\n", - " ])\n", - " \n", - " elif workload_type == 'data_processing':\n", - " recommendations.extend([\n", - " \"Use memory-optimized instances for in-memory processing\",\n", - " \"Consider data locality to reduce transfer costs\",\n", - " \"Implement data compression to reduce storage costs\",\n", - " \"Use lifecycle policies to archive old data\"\n", - " ])\n", - " \n", - " # Add requirement-specific recommendations\n", - " if requirements.get('fault_tolerant', False):\n", - " recommendations.append(\"Leverage spot/preemptible instances aggressively\")\n", - " \n", - " if requirements.get('gpu_required', False):\n", - " recommendations.append(\"Compare GPU instance prices across regions and providers\")\n", - " \n", - " if requirements.get('long_running', False):\n", - " recommendations.append(\"Use reserved instances or committed use discounts\")\n", - " \n", - " return recommendations\n", - "\n", - "# Example workload analysis\n", - "print(\"=== Workload-Specific Cost Optimization Recommendations ===\")\n", - "\n", - "workloads = [\n", - " {\n", - " 'type': 'batch_processing',\n", - " 'name': 'Nightly Data Pipeline',\n", - " 'requirements': {'fault_tolerant': True, 'gpu_required': False}\n", - " },\n", - " {\n", - " 'type': 'ml_training',\n", - " 'name': 'Deep Learning Model Training',\n", - " 'requirements': {'gpu_required': True, 'long_running': True}\n", - " },\n", - " {\n", - " 'type': 'web_service',\n", - " 'name': 'API Backend Service',\n", - " 'requirements': {'fault_tolerant': False, 'long_running': True}\n", - " }\n", - "]\n", - "\n", - "for workload in workloads:\n", - " print(f\"\\n{workload['name']} ({workload['type']}):\")\n", - " recommendations = get_cost_optimization_recommendations(\n", - " workload['type'], \n", - " workload['requirements']\n", - " )\n", - " for i, rec in enumerate(recommendations, 1):\n", - " print(f\" {i}. {rec}\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-19", - "metadata": {}, - "source": [ - "## Visualizing Cost Data\n", - "\n", - "### Cost Comparison Charts" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-20", - "metadata": {}, - "outputs": [], - "source": [ - "# Create cost comparison visualizations\n", - "import matplotlib.pyplot as plt\n", - "import numpy as np\n", - "\n", - "# Prepare data for visualization\n", - "providers = ['AWS', 'GCP', 'Azure']\n", - "instance_types = {\n", - " 'AWS': ['t3.medium', 't3.large', 't3.xlarge', 'm5.large', 'm5.xlarge'],\n", - " 'GCP': ['n2-standard-2', 'n2-standard-4', 'n2-standard-8', 'n2-standard-16', 'n2-standard-32'],\n", - " 'Azure': ['Standard_D2s_v3', 'Standard_D4s_v3', 'Standard_D8s_v3', 'Standard_D16s_v3', 'Standard_D32s_v3']\n", - "}\n", - "\n", - "# Collect pricing data\n", - "pricing_data = {}\n", - "for provider in providers:\n", - " monitor = get_cost_monitor(provider.lower())\n", - " prices = []\n", - " for instance in instance_types[provider]:\n", - " cost_estimate = monitor.estimate_cost(instance, 1.0)\n", - " prices.append(cost_estimate.hourly_rate)\n", - " pricing_data[provider] = prices\n", - "\n", - "# Create comparison chart\n", - "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))\n", - "\n", - "# Bar chart comparison\n", - "x = np.arange(len(instance_types['AWS']))\n", - "width = 0.25\n", - "\n", - "for i, provider in enumerate(providers):\n", - " ax1.bar(x + i*width, pricing_data[provider], width, label=provider)\n", - "\n", - "ax1.set_xlabel('Instance Size')\n", - "ax1.set_ylabel('Cost per Hour ($)')\n", - "ax1.set_title('Cloud Provider Cost Comparison by Instance Size')\n", - "ax1.set_xticks(x + width)\n", - "ax1.set_xticklabels(['Small', 'Medium', 'Large', 'XLarge', '2XLarge'])\n", - "ax1.legend()\n", - "ax1.grid(True, alpha=0.3)\n", - "\n", - "# Spot vs On-Demand savings visualization\n", - "spot_savings = {\n", - " 'AWS': [65, 70, 72, 68, 71],\n", - " 'GCP': [60, 65, 68, 70, 72],\n", - " 'Azure': [58, 62, 65, 67, 70]\n", - "}\n", - "\n", - "for i, provider in enumerate(providers):\n", - " ax2.plot(instance_types[provider], spot_savings[provider], \n", - " marker='o', linewidth=2, markersize=8, label=provider)\n", - "\n", - "ax2.set_xlabel('Instance Type')\n", - "ax2.set_ylabel('Spot/Preemptible Savings (%)')\n", - "ax2.set_title('Spot Instance Savings by Provider')\n", - "ax2.legend()\n", - "ax2.grid(True, alpha=0.3)\n", - "ax2.set_xticklabels(['Small', 'Medium', 'Large', 'XLarge', '2XLarge'])\n", - "\n", - "plt.tight_layout()\n", - "plt.show()\n", - "\n", - "# Monthly cost projection\n", - "fig, ax = plt.subplots(figsize=(10, 6))\n", - "\n", - "hours_per_day = np.arange(1, 25)\n", - "days_per_month = 30\n", - "\n", - "for provider in providers:\n", - " monitor = get_cost_monitor(provider.lower())\n", - " instance = instance_types[provider][2] # Large instance\n", - " \n", - " monthly_costs = []\n", - " for hours in hours_per_day:\n", - " total_hours = hours * days_per_month\n", - " cost = monitor.estimate_cost(instance, total_hours).estimated_cost\n", - " monthly_costs.append(cost)\n", - " \n", - " ax.plot(hours_per_day, monthly_costs, marker='o', label=f'{provider} ({instance})')\n", - "\n", - "ax.set_xlabel('Hours per Day')\n", - "ax.set_ylabel('Monthly Cost ($)')\n", - "ax.set_title('Monthly Cost Projection by Daily Usage')\n", - "ax.legend()\n", - "ax.grid(True, alpha=0.3)\n", - "\n", - "# Add cost threshold lines\n", - "budget_levels = [100, 500, 1000, 2000]\n", - "for budget in budget_levels:\n", - " ax.axhline(y=budget, color='red', linestyle='--', alpha=0.5)\n", - " ax.text(24.5, budget, f'${budget}', va='center')\n", - "\n", - "plt.tight_layout()\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "id": "cell-21", - "metadata": {}, - "source": [ - "## Best Practices for Cost Optimization\n", - "\n", - "### 1. Choose the Right Instance Type\n", - "- Match instance specifications to workload requirements\n", - "- Avoid over-provisioning resources\n", - "- Use burstable instances for variable workloads\n", - "\n", - "### 2. Leverage Spot/Preemptible Instances\n", - "- Use for fault-tolerant batch processing\n", - "- Implement checkpointing for long-running jobs\n", - "- Mix on-demand and spot for reliability\n", - "\n", - "### 3. Optimize for Your Usage Pattern\n", - "- Reserved instances for steady-state workloads\n", - "- Auto-scaling for variable demand\n", - "- Scheduled scaling for predictable patterns\n", - "\n", - "### 4. Monitor and Alert\n", - "- Set up budget alerts\n", - "- Use Clustrix cost tracking decorators\n", - "- Regular cost reviews and optimization\n", - "\n", - "### 5. Multi-Cloud Strategy\n", - "- Compare prices across providers\n", - "- Use each cloud's strengths\n", - "- Avoid vendor lock-in" - ] - }, - { - "cell_type": "markdown", - "id": "cell-22", - "metadata": {}, - "source": [ - "## Real-World Example: Cost-Optimized ML Pipeline" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cell-23", - "metadata": {}, - "outputs": [], - "source": [ - "# Complete cost-optimized ML pipeline example\n", - "class CostOptimizedMLPipeline:\n", - " \"\"\"Example of a cost-aware ML pipeline using Clustrix.\"\"\"\n", - " \n", - " def __init__(self, budget_limit=100.0):\n", - " self.budget_limit = budget_limit\n", - " self.total_cost = 0.0\n", - " self.cost_history = []\n", - " \n", - " @cost_tracking_decorator('aws', 't3.medium')\n", - " def preprocess_data(self, data_size_gb):\n", - " \"\"\"Preprocess data on cost-effective instances.\"\"\"\n", - " import time\n", - " processing_time = data_size_gb * 0.5 # Simulate processing\n", - " time.sleep(min(processing_time, 2)) # Cap at 2 seconds for demo\n", - " return {'processed_records': data_size_gb * 1000000}\n", - " \n", - " @cost_tracking_decorator('aws', 'p3.2xlarge')\n", - " def train_model(self, model_type='small'):\n", - " \"\"\"Train model on GPU instances.\"\"\"\n", - " import time\n", - " training_times = {'small': 1, 'medium': 2, 'large': 3}\n", - " time.sleep(training_times.get(model_type, 1))\n", - " return {'model_accuracy': 0.85 + np.random.random() * 0.1}\n", - " \n", - " @cost_tracking_decorator('aws', 't3.small')\n", - " def evaluate_model(self, test_size):\n", - " \"\"\"Evaluate model on small instances.\"\"\"\n", - " import time\n", - " time.sleep(0.5)\n", - " return {'test_accuracy': 0.82 + np.random.random() * 0.1}\n", - " \n", - " def run_pipeline(self, data_size_gb=10, model_type='small'):\n", - " \"\"\"Run complete pipeline with cost tracking.\"\"\"\n", - " print(f\"Starting ML Pipeline (Budget: ${self.budget_limit})\")\n", - " results = {}\n", - " \n", - " # Step 1: Preprocess data\n", - " print(\"\\n1. Preprocessing data...\")\n", - " preprocess_result = self.preprocess_data(data_size_gb)\n", - " if preprocess_result['success']:\n", - " cost = preprocess_result['cost_report']['cost_estimate']['estimated_cost']\n", - " self.total_cost += cost\n", - " self.cost_history.append(('preprocessing', cost))\n", - " print(f\" ✓ Processed {preprocess_result['result']['processed_records']:,} records\")\n", - " print(f\" 💰 Cost: ${cost:.4f} (Total: ${self.total_cost:.4f})\")\n", - " \n", - " # Check budget\n", - " if self.total_cost > self.budget_limit:\n", - " print(f\"\\n❌ Budget exceeded! Stopping pipeline.\")\n", - " return results\n", - " \n", - " # Step 2: Train model\n", - " print(\"\\n2. Training model...\")\n", - " train_result = self.train_model(model_type)\n", - " if train_result['success']:\n", - " cost = train_result['cost_report']['cost_estimate']['estimated_cost']\n", - " self.total_cost += cost\n", - " self.cost_history.append(('training', cost))\n", - " print(f\" ✓ Model accuracy: {train_result['result']['model_accuracy']:.4f}\")\n", - " print(f\" 💰 Cost: ${cost:.4f} (Total: ${self.total_cost:.4f})\")\n", - " \n", - " # Check budget\n", - " if self.total_cost > self.budget_limit:\n", - " print(f\"\\n❌ Budget exceeded! Stopping pipeline.\")\n", - " return results\n", - " \n", - " # Step 3: Evaluate model\n", - " print(\"\\n3. Evaluating model...\")\n", - " eval_result = self.evaluate_model(1000)\n", - " if eval_result['success']:\n", - " cost = eval_result['cost_report']['cost_estimate']['estimated_cost']\n", - " self.total_cost += cost\n", - " self.cost_history.append(('evaluation', cost))\n", - " print(f\" ✓ Test accuracy: {eval_result['result']['test_accuracy']:.4f}\")\n", - " print(f\" 💰 Cost: ${cost:.4f} (Total: ${self.total_cost:.4f})\")\n", - " \n", - " # Summary\n", - " print(\"\\n=== Pipeline Summary ===\")\n", - " print(f\"Total Cost: ${self.total_cost:.4f}\")\n", - " print(f\"Budget Remaining: ${self.budget_limit - self.total_cost:.4f}\")\n", - " print(\"\\nCost Breakdown:\")\n", - " for step, cost in self.cost_history:\n", - " pct = (cost / self.total_cost) * 100\n", - " print(f\" {step:15} ${cost:.4f} ({pct:.1f}%)\")\n", - " \n", - " return {\n", - " 'total_cost': self.total_cost,\n", - " 'cost_history': self.cost_history,\n", - " 'under_budget': self.total_cost <= self.budget_limit\n", - " }\n", - "\n", - "# Run the cost-optimized pipeline\n", - "pipeline = CostOptimizedMLPipeline(budget_limit=0.10) # $0.10 budget for demo\n", - "results = pipeline.run_pipeline(data_size_gb=5, model_type='small')\n", - "\n", - "print(\"\\n✅ Pipeline completed successfully!\" if results.get('under_budget', False) \n", - " else \"\\n⚠️ Pipeline stopped due to budget constraints.\")" - ] - }, - { - "cell_type": "markdown", - "id": "cell-24", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "This tutorial covered comprehensive cost monitoring and optimization with Clustrix:\n", - "\n", - "### Key Features Demonstrated\n", - "\n", - "1. **Automatic Cost Tracking**: Use `@cost_tracking_decorator` for seamless monitoring\n", - "2. **Manual Cost Monitoring**: Fine-grained control with manual monitoring functions\n", - "3. **Multi-Cloud Support**: Compare costs across AWS, GCP, Azure, and more\n", - "4. **Regional Pricing**: Find the most cost-effective regions\n", - "5. **Spot/Preemptible Savings**: Up to 80% cost reduction\n", - "6. **Batch Job Estimation**: Plan and budget for large-scale processing\n", - "7. **Optimization Recommendations**: Workload-specific cost-saving strategies\n", - "\n", - "### Best Practices\n", - "\n", - "- Always use cost tracking decorators for production workloads\n", - "- Compare prices across providers and regions\n", - "- Leverage spot/preemptible instances for fault-tolerant workloads\n", - "- Use reserved instances for predictable, long-running workloads\n", - "- Monitor costs continuously and set up budget alerts\n", - "- Implement auto-scaling to match resources to demand\n", - "\n", - "### Next Steps\n", - "\n", - "1. Integrate cost monitoring into your existing workflows\n", - "2. Set up budget alerts and cost anomaly detection\n", - "3. Experiment with different instance types and pricing models\n", - "4. Implement cost optimization recommendations\n", - "5. Create cost dashboards for stakeholder visibility\n", - "\n", - "### Resources\n", - "\n", - "- [Clustrix Documentation](https://clustrix.readthedocs.io/)\n", - "- [AWS Pricing](https://aws.amazon.com/pricing/)\n", - "- [GCP Pricing](https://cloud.google.com/pricing)\n", - "- [Azure Pricing](https://azure.microsoft.com/pricing/)\n", - "\n", - "Remember: **Every dollar saved on cloud costs is a dollar that can be invested in innovation!**" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/gcp_cloud_tutorial.ipynb b/docs/source/notebooks/gcp_cloud_tutorial.ipynb deleted file mode 100644 index 1ccbbeba..00000000 --- a/docs/source/notebooks/gcp_cloud_tutorial.ipynb +++ /dev/null @@ -1,1842 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "288ef14e", - "metadata": {}, - "source": [ - "> **These backends are unverified.**\n", - ">\n", - "> No clustrix cloud VM job (`provider=\"aws\"`, `\"gcp\"`, `\"azure\"`, `\"lambda\"`, `\"huggingface\"`) has been shown to run end to end. Until recently the path could not have run at all: every cloud job died with a `KeyError` on its first line. That was fixed (issue #119), but nothing has since demonstrated a completed cloud job, and `scripts/collect_execution_evidence.py` does not cover these backends. This notebook describes the intended interface, not something that has been run.\n", - ">\n", - "> The backends that are verified working are `cluster_type=\"slurm\"`, `cluster_type=\"ssh\"` and `cluster_type=\"huggingface\"` (HuggingFace Jobs, which is a different thing from the HuggingFace Spaces provider described here). See the Supported Cluster Types section of the documentation.\n" - ] - }, - { - "cell_type": "markdown", - "id": "bd8fff12", - "metadata": {}, - "source": [ - "> **What actually happens if you try `@cluster(provider=\"gcp\", ...)`.**\n", - ">\n", - "> Clustrix's own cloud-VM auto-provisioning (`CloudJobManager.submit_cloud_job`, in `clustrix/executor_cloud.py`) only works end to end for `provider=\"lambda\"` -- it is the only built-in provider whose class implements `create_instance()`. For `provider=\"gcp\"`, submission checks this at *submit time* and raises `NotImplementedError` naming the provider, before any thread, instance, or SSH connection is created:\n", - ">\n", - "> ```\n", - "> The 'gcp' cloud provider cannot run clustrix jobs: GCPProvider does\n", - "> not implement create_instance, ... Of the built-in providers only 'lambda'\n", - "> provisions instances for job execution; for the others, provision the machine\n", - "> yourself and use cluster_type 'ssh', or use cluster_type 'kubernetes'.\n", - "> ```\n", - ">\n", - "> That is exactly the pattern this notebook follows: the examples below provision a VM using the gcloud CLI, then point Clustrix's `cluster_type=\"ssh\"` (or `\"slurm\"`) at it directly -- the same transport used by any other SSH/SLURM cluster in these docs, just running on a cloud box instead of an on-prem one. That exercises the SSH/SLURM backend, not a demonstrated run on this specific cloud, and no such run has been recorded for any of these providers.\n", - ">\n", - "> One more thing that used to be silently wrong and is now an explicit error: if a provider's `get_cluster_config()` cannot determine a VM's real hostname (API error, VM not yet assigned an IP, ...), it used to return a fake `placeholder.gcp.com` hostname, which nothing downstream could tell apart from a real one -- the failure then surfaced as an SSH connection error against a domain that does not exist, far from its actual cause. It now raises `RuntimeError` naming the resource instead." - ] - }, - { - "cell_type": "markdown", - "id": "16a1d8cd", - "metadata": {}, - "source": [ - "**Behind the scenes, once you're actually calling `@cluster`:** every\n", - "example below that runs (as opposed to just printing setup commands) ends up\n", - "configured with `cluster_type=\"ssh\"` against a VM you provisioned yourself --\n", - "that is the verified SSH backend, following the same order of operations as\n", - "any other SSH cluster in these docs: read `get_config()`, merge decorator\n", - "args over config defaults, choose local vs. remote (`cluster_host` is set,\n", - "so remote), serialize the function with `dill`/`cloudpickle`, create the\n", - "remote job directory, upload the payload over SFTP, build the remote venv,\n", - "generate and run a job script, poll for completion, then download and\n", - "HMAC-verify `result.pkl`. None of that is GCP (Compute Engine/GKE)-specific -- clustrix\n", - "does not talk to the GCP (Compute Engine/GKE) API at any point in that path; GCP (Compute Engine/GKE)\n", - "only matters for how the VM itself got created, which is everything *before*\n", - "`configure(cluster_type=\"ssh\", ...)` in this notebook. Full, source-verified\n", - "detail: :ref:`execution-model`. Every `ClusterConfig` field used below is\n", - "documented in :ref:`configuration`.\n", - "\n", - "**Real resources, real charges.** The functions and CLI snippets below that\n", - "create VMs, networks, security groups, or managed clusters call real\n", - "GCP (Compute Engine/GKE) APIs (or print commands meant to be copy-pasted into a real\n", - "GCP (Compute Engine/GKE) CLI). None of them run automatically in this notebook -- every\n", - "invocation is commented out -- but if you uncomment one, or copy a printed\n", - "command into your terminal, it creates billed resources in your account.\n", - "Read each cell before running or copying it, and see the cleanup cell near\n", - "the end before you walk away." - ] - }, - { - "cell_type": "markdown", - "id": "gcp-title", - "metadata": {}, - "source": [ - "# Google Cloud Platform (GCP) Tutorial\n", - "\n", - "This tutorial demonstrates how to use Clustrix with Google Cloud Platform (GCP) infrastructure for scalable distributed computing.\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/gcp_cloud_tutorial.ipynb)\n", - "\n", - "## Overview\n", - "\n", - "GCP provides several services that integrate well with Clustrix:\n", - "\n", - "- **Compute Engine**: Virtual machines for compute clusters\n", - "- **Google Kubernetes Engine (GKE)**: Managed Kubernetes clusters\n", - "- **Batch**: Managed job scheduling service\n", - "- **Cloud Run**: Serverless container platform\n", - "- **Vertex AI**: Machine learning platform\n", - "- **Cloud Storage**: Object storage for data and results\n", - "- **VPC**: Network isolation and security\n", - "- **Preemptible VMs**: Cost-effective compute instances\n", - "\n", - "## Complete Setup Guide from Scratch\n", - "\n", - "### Step 1: Google Cloud Account Setup\n", - "\n", - "1. **Create Google Cloud Account**:\n", - " - Go to [Google Cloud Console](https://console.cloud.google.com/)\n", - " - Sign up with your Google account or create a new one\n", - " - Accept the terms of service\n", - "\n", - "2. **Enable Billing**:\n", - " - Navigate to Billing in the Google Cloud Console\n", - " - Create a billing account and add a payment method\n", - " - **Important**: New users get $300 in free credits\n", - " - Set up billing alerts to avoid unexpected charges\n", - "\n", - "3. **Create a New Project**:\n", - " - Go to the Project Selector in the console\n", - " - Click \"New Project\"\n", - " - Choose a unique project ID (e.g., `my-clustrix-project-123`)\n", - " - Enable billing for this project\n", - "\n", - "### Step 2: Install Google Cloud SDK (gcloud CLI)\n", - "\n", - "**On macOS:**\n", - "```bash\n", - "# Using Homebrew (recommended)\n", - "brew install google-cloud-sdk\n", - "\n", - "# Or download installer\n", - "curl https://sdk.cloud.google.com | bash\n", - "exec -l $SHELL\n", - "```\n", - "\n", - "**On Linux:**\n", - "```bash\n", - "# Download and install\n", - "curl https://sdk.cloud.google.com | bash\n", - "exec -l $SHELL\n", - "\n", - "# Or use package manager (Ubuntu/Debian)\n", - "sudo apt-get install google-cloud-sdk\n", - "```\n", - "\n", - "**On Windows:**\n", - "- Download the installer from [Google Cloud SDK page](https://cloud.google.com/sdk/docs/install)\n", - "- Run the installer and follow instructions\n", - "\n", - "### Step 3: Enable Required APIs\n", - "\n", - "Enable the necessary Google Cloud APIs for this tutorial:\n", - "\n", - "```bash\n", - "# Set your project ID\n", - "export PROJECT_ID=\"your-project-id-here\"\n", - "gcloud config set project $PROJECT_ID\n", - "\n", - "# Enable required APIs\n", - "gcloud services enable compute.googleapis.com\n", - "gcloud services enable container.googleapis.com\n", - "gcloud services enable batch.googleapis.com\n", - "gcloud services enable aiplatform.googleapis.com\n", - "gcloud services enable storage.googleapis.com\n", - "```\n", - "\n", - "## Prerequisites Checklist\n", - "\n", - "Before proceeding, ensure you have:\n", - "\n", - "- [ ] Google Cloud account with billing enabled\n", - "- [ ] Google Cloud project created\n", - "- [ ] Google Cloud SDK (gcloud) installed locally\n", - "- [ ] Required APIs enabled (compute, container, batch, storage, aiplatform)\n", - "- [ ] SSH key pair for VM access (we'll create this below)\n", - "- [ ] Basic understanding of command line usage" - ] - }, - { - "cell_type": "markdown", - "id": "installation", - "metadata": {}, - "source": [ - "## Installation and Setup\n", - "\n", - "Install Clustrix with GCP dependencies:" - ] - }, - { - "cell_type": "markdown", - "id": "4wiyb0urchu", - "metadata": {}, - "source": [ - "### Step 4: SSH Key Setup\n", - "\n", - "Create SSH keys for secure access to your GCP instances:\n", - "\n", - "```bash\n", - "# Generate SSH key pair (if you don't have one)\n", - "ssh-keygen -t rsa -b 4096 -C \"your-email@example.com\" -f ~/.ssh/gcp_key\n", - "\n", - "# Add the public key to GCP\n", - "gcloud compute os-login ssh-keys add --key-file=~/.ssh/gcp_key.pub\n", - "\n", - "# Or add to project metadata (alternative method)\n", - "gcloud compute project-info add-metadata --metadata-from-file ssh-keys=~/.ssh/gcp_key.pub\n", - "```\n", - "\n", - "**Note**: If you're using Google Cloud Shell, SSH keys are automatically managed." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "install", - "metadata": {}, - "outputs": [], - "source": [ - "# Install Clustrix with GCP support\n", - "!pip install clustrix google-cloud-compute google-cloud-storage google-auth google-auth-oauthlib\n", - "\n", - "# Import required libraries\n", - "import clustrix\n", - "from clustrix import cluster, configure\n", - "from google.cloud import compute_v1\n", - "from google.cloud import storage\n", - "from google.auth import default\n", - "import os\n", - "import numpy as np\n", - "import time\n", - "import json" - ] - }, - { - "cell_type": "markdown", - "id": "gcp-authentication", - "metadata": {}, - "source": [ - "## GCP Authentication Setup\n", - "\n", - "Configure your GCP credentials. Choose the method that best fits your environment:\n", - "\n", - "### Option 1: gcloud CLI Authentication (Recommended for Local Development)\n", - "\n", - "This method uses your personal Google account credentials:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "gcloud-auth", - "metadata": {}, - "outputs": [], - "source": [ - "# Initial authentication and project setup\n", - "!gcloud auth login\n", - "!gcloud auth application-default login\n", - "\n", - "# Set your project ID (replace with your actual project ID)\n", - "PROJECT_ID = \"your-project-id-here\" # Replace this!\n", - "!gcloud config set project {PROJECT_ID}\n", - "\n", - "# Verify authentication and project setup\n", - "!gcloud auth list\n", - "!gcloud config get-value project\n", - "!gcloud projects describe {PROJECT_ID}" - ] - }, - { - "cell_type": "markdown", - "id": "gcp-service-account", - "metadata": {}, - "source": [ - "### Option 2: Service Account Authentication (Recommended for Production)\n", - "\n", - "For production environments, create and use a service account with specific permissions:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "service-account", - "metadata": {}, - "outputs": [], - "source": [ - "# Test GCP connection\n", - "try:\n", - " credentials, project_id = default()\n", - " print(f\"✓ Successfully authenticated with project: {project_id}\")\n", - " \n", - " # Test compute API\n", - " compute_client = compute_v1.InstancesClient()\n", - " print(\"✓ Compute Engine API access confirmed\")\n", - " \n", - " # Test storage API\n", - " storage_client = storage.Client()\n", - " print(\"✓ Cloud Storage API access confirmed\")\n", - " \n", - "except Exception as e:\n", - " print(f\"❌ GCP authentication failed: {e}\")\n", - " print(\"Please check your authentication setup and try again.\")" - ] - }, - { - "cell_type": "markdown", - "id": "hc7w51mwb54", - "metadata": {}, - "source": [ - "**Service Account Setup (Production Environments)**\n", - "\n", - "For production use, create a service account with specific permissions:\n", - "\n", - "```bash\n", - "# Create service account\n", - "gcloud iam service-accounts create clustrix-service-account \\\n", - " --description=\"Service account for Clustrix operations\" \\\n", - " --display-name=\"Clustrix Service Account\"\n", - "\n", - "# Grant necessary permissions\n", - "gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \\\n", - " --member=\"serviceAccount:clustrix-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com\" \\\n", - " --role=\"roles/compute.admin\"\n", - "\n", - "gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \\\n", - " --member=\"serviceAccount:clustrix-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com\" \\\n", - " --role=\"roles/storage.admin\"\n", - "\n", - "# Create and download service account key\n", - "gcloud iam service-accounts keys create ~/clustrix-service-account-key.json \\\n", - " --iam-account=clustrix-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com\n", - "\n", - "# Set the environment variable\n", - "export GOOGLE_APPLICATION_CREDENTIALS=\"/path/to/clustrix-service-account-key.json\"\n", - "```" - ] - }, - { - "cell_type": "markdown", - "id": "eegtd4c0im", - "metadata": {}, - "source": [ - "**Important**: Make sure you have completed authentication setup and enabled all required APIs before proceeding. \n", - "\n", - "If authentication fails, double-check that:\n", - "- Your project ID is correct\n", - "- Billing is enabled for your project \n", - "- Required APIs are enabled\n", - "- Your credentials are properly configured" - ] - }, - { - "cell_type": "markdown", - "id": "compute-engine-setup", - "metadata": {}, - "source": [ - "## Method 1: Google Compute Engine Configuration\n", - "\n", - "### Create Compute Engine Instance for Clustrix" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "compute-engine-creation", - "metadata": {}, - "outputs": [], - "source": [ - "def create_clustrix_compute_instance(project_id, zone='us-central1-a', machine_type='e2-standard-4'):\n", - " \"\"\"\n", - " Create a GCP Compute Engine instance configured for Clustrix.\n", - " \n", - " Args:\n", - " project_id: GCP project ID\n", - " zone: GCP zone for the instance\n", - " machine_type: Machine type (CPU/memory configuration)\n", - " \n", - " Returns:\n", - " Instance configuration and gcloud commands\n", - " \"\"\"\n", - " \n", - " # Startup script for instance initialization\n", - " startup_script = '''\n", - "#!/bin/bash\n", - "\n", - "# Update system\n", - "apt-get update\n", - "apt-get install -y python3 python3-pip git htop curl\n", - "\n", - "# Install clustrix and common packages\n", - "pip3 install clustrix numpy scipy pandas scikit-learn matplotlib\n", - "\n", - "# Install uv for faster package management\n", - "curl -LsSf https://astral.sh/uv/install.sh | sh\n", - "source ~/.cargo/env\n", - "\n", - "# Create clustrix user\n", - "useradd -m -s /bin/bash clustrix\n", - "usermod -aG sudo clustrix\n", - "echo \"clustrix ALL=(ALL) NOPASSWD:ALL\" >> /etc/sudoers\n", - "\n", - "# Setup SSH for clustrix user\n", - "mkdir -p /home/clustrix/.ssh\n", - "# Copy SSH keys from default user\n", - "if [ -d \"/home/$(logname)/.ssh\" ]; then\n", - " cp -r /home/$(logname)/.ssh/* /home/clustrix/.ssh/\n", - " chown -R clustrix:clustrix /home/clustrix/.ssh\n", - " chmod 700 /home/clustrix/.ssh\n", - " chmod 600 /home/clustrix/.ssh/authorized_keys 2>/dev/null || true\n", - "fi\n", - "\n", - "# Create working directory\n", - "mkdir -p /tmp/clustrix\n", - "chown clustrix:clustrix /tmp/clustrix\n", - "\n", - "# Install Google Cloud SDK for clustrix user\n", - "curl https://sdk.cloud.google.com | bash\n", - "exec -l $SHELL\n", - "\n", - "# Log completion\n", - "echo \"Clustrix setup completed at $(date)\" >> /var/log/clustrix-setup.log\n", - "'''\n", - " \n", - " # gcloud commands for instance creation\n", - " gcloud_commands = f\"\"\"\n", - "# Create firewall rule for SSH (if not exists)\n", - "gcloud compute firewall-rules create allow-ssh \\\n", - " --allow tcp:22 \\\n", - " --source-ranges 0.0.0.0/0 \\\n", - " --description \"Allow SSH access\" \\\n", - " --project {project_id} || echo \"SSH rule already exists\"\n", - "\n", - "# Create the instance\n", - "gcloud compute instances create clustrix-instance \\\n", - " --project={project_id} \\\n", - " --zone={zone} \\\n", - " --machine-type={machine_type} \\\n", - " --network-interface=network-tier=PREMIUM,subnet=default \\\n", - " --maintenance-policy=MIGRATE \\\n", - " --provisioning-model=STANDARD \\\n", - " --service-account=default \\\n", - " --scopes=https://www.googleapis.com/auth/cloud-platform \\\n", - " --tags=clustrix,http-server,https-server \\\n", - " --create-disk=auto-delete=yes,boot=yes,device-name=clustrix-instance,image=projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts,mode=rw,size=50,type=projects/{project_id}/zones/{zone}/diskTypes/pd-balanced \\\n", - " --no-shielded-secure-boot \\\n", - " --shielded-vtpm \\\n", - " --shielded-integrity-monitoring \\\n", - " --labels=purpose=clustrix,environment=tutorial \\\n", - " --reservation-affinity=any \\\n", - " --metadata-from-file startup-script=startup-script.sh\n", - "\n", - "# Get the external IP\n", - "gcloud compute instances describe clustrix-instance \\\n", - " --project={project_id} \\\n", - " --zone={zone} \\\n", - " --format='get(networkInterfaces[0].accessConfigs[0].natIP)'\n", - "\n", - "# SSH to the instance (after startup script completes)\n", - "gcloud compute ssh clustrix-instance \\\n", - " --project={project_id} \\\n", - " --zone={zone}\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'project_id': project_id,\n", - " 'zone': zone,\n", - " 'machine_type': machine_type,\n", - " 'instance_name': 'clustrix-instance',\n", - " 'gcloud_commands': gcloud_commands,\n", - " 'startup_script': startup_script\n", - " }\n", - "\n", - "# Example usage - replace with your actual project ID\n", - "instance_config = create_clustrix_compute_instance(\n", - " project_id=PROJECT_ID, # Using the PROJECT_ID variable from above\n", - " zone='us-central1-a',\n", - " machine_type='e2-standard-4' # 4 vCPUs, 16 GB RAM\n", - ")\n", - "\n", - "# Display the configuration results\n", - "print(\"=== GCP Compute Engine Instance Configuration ===\")\n", - "print(f\"Project ID: {instance_config['project_id']}\")\n", - "print(f\"Zone: {instance_config['zone']}\")\n", - "print(f\"Machine Type: {instance_config['machine_type']}\")\n", - "print(f\"Instance Name: {instance_config['instance_name']}\")\n", - "print(\"\\n=== Next Steps ===\")\n", - "print(\"1. Save the startup script to 'startup-script.sh'\")\n", - "print(\"2. Execute the gcloud commands shown above\")\n", - "print(\"3. Wait 3-5 minutes for instance initialization\")\n", - "print(\"4. Get the external IP and configure Clustrix\")" - ] - }, - { - "cell_type": "markdown", - "id": "rs47wjva5yi", - "metadata": {}, - "source": [ - "### GCP Compute Engine Instance Creation\n", - "\n", - "The above code defines a function that creates a GCP Compute Engine instance optimized for Clustrix workloads. The function returns:\n", - "\n", - "- **gcloud commands**: Complete CLI commands to create the instance\n", - "- **startup script**: Automated setup script that configures the instance\n", - "\n", - "The configuration includes:\n", - "- Ubuntu 22.04 LTS base image\n", - "- Pre-installed Python packages and Clustrix\n", - "- Clustrix user account with sudo privileges \n", - "- SSH key setup and working directories\n", - "- 50GB balanced persistent disk\n", - "- Appropriate firewall rules and metadata" - ] - }, - { - "cell_type": "markdown", - "id": "nrfay0eolc", - "metadata": {}, - "source": [ - "**Next Steps**: \n", - "\n", - "1. **Save the startup script** to a file named `startup-script.sh` in your current directory\n", - "2. **Execute the gcloud commands** shown above to create your instance\n", - "3. **Wait for the instance to fully initialize** (startup script takes 3-5 minutes)\n", - "4. **Get the external IP** using the describe command shown above\n", - "5. **Test SSH access** to ensure the instance is ready for Clustrix" - ] - }, - { - "cell_type": "markdown", - "id": "clustrix-gcp-config", - "metadata": {}, - "source": [ - "### Configure Clustrix for Compute Engine" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "config-gcp-compute", - "metadata": {}, - "outputs": [], - "source": [ - "# Get the external IP of your created instance\n", - "# Replace with the actual external IP from your instance\n", - "INSTANCE_EXTERNAL_IP = \"YOUR_INSTANCE_EXTERNAL_IP\" # Replace this!\n", - "\n", - "# Configure Clustrix to use your Compute Engine instance\n", - "configure(\n", - " cluster_type=\"ssh\",\n", - " cluster_host=INSTANCE_EXTERNAL_IP,\n", - " username=\"clustrix\", # or your default user\n", - " key_file=\"~/.ssh/gcp_key\", # path to your SSH private key\n", - " remote_work_dir=\"~/.clustrix/jobs\",\n", - " package_manager=\"auto\", # Will use uv if available, pip otherwise\n", - " default_cores=4,\n", - " default_memory=\"8GB\",\n", - " default_time=\"01:00:00\"\n", - ")\n", - "\n", - "# Verify configuration\n", - "if INSTANCE_EXTERNAL_IP != \"YOUR_INSTANCE_EXTERNAL_IP\":\n", - " print(f\"✓ Clustrix configured for GCP Compute Engine\")\n", - " print(f\" Host: {INSTANCE_EXTERNAL_IP}\")\n", - " print(f\" SSH Key: ~/.ssh/gcp_key\")\n", - " print(f\" Remote Work Dir: ~/.clustrix/jobs\")\n", - "else:\n", - " print(\"⚠️ Please replace INSTANCE_EXTERNAL_IP with your actual IP address\")" - ] - }, - { - "cell_type": "markdown", - "id": "6qtk506dlio", - "metadata": {}, - "source": [ - "**Important Configuration Notes**:\n", - "\n", - "- Replace `YOUR_INSTANCE_EXTERNAL_IP` with the actual external IP address from your Compute Engine instance\n", - "- Use the SSH key path that corresponds to your setup (either `~/.ssh/gcp_key` if you created one following this tutorial, or `~/.ssh/google_compute_engine` for gcloud-generated keys)\n", - "- The `clustrix` user was created by the startup script with appropriate permissions\n", - "- If you encounter connection issues, ensure your firewall rules allow SSH access from your IP address" - ] - }, - { - "cell_type": "markdown", - "id": "gcp-example", - "metadata": {}, - "source": [ - "### Example: Remote Computation on Compute Engine" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "gcp-compute-example", - "metadata": {}, - "outputs": [], - "source": [ - "# Example: GCP Data Analysis\n", - "@cluster(cores=2, memory=\"4GB\")\n", - "def gcp_data_analysis(dataset_size=10000, analysis_type='regression'):\n", - " \"\"\"Perform data analysis on GCP Compute Engine.\"\"\"\n", - " import numpy as np\n", - " from sklearn.model_selection import train_test_split\n", - " from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier\n", - " from sklearn.metrics import mean_squared_error, accuracy_score\n", - " from sklearn.datasets import make_regression, make_classification\n", - " import time\n", - " \n", - " start_time = time.time()\n", - " \n", - " # Generate synthetic dataset\n", - " if analysis_type == 'regression':\n", - " X, y = make_regression(\n", - " n_samples=dataset_size,\n", - " n_features=20,\n", - " noise=0.1,\n", - " random_state=42\n", - " )\n", - " model = RandomForestRegressor(n_estimators=100, random_state=42, n_jobs=-1)\n", - " metric_name = 'rmse'\n", - " else:\n", - " X, y = make_classification(\n", - " n_samples=dataset_size,\n", - " n_features=20,\n", - " n_classes=3,\n", - " random_state=42\n", - " )\n", - " model = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)\n", - " metric_name = 'accuracy'\n", - " \n", - " # Split data\n", - " X_train, X_test, y_train, y_test = train_test_split(\n", - " X, y, test_size=0.2, random_state=42\n", - " )\n", - " \n", - " # Train model\n", - " training_start = time.time()\n", - " model.fit(X_train, y_train)\n", - " training_time = time.time() - training_start\n", - " \n", - " # Evaluate\n", - " y_pred = model.predict(X_test)\n", - " \n", - " if analysis_type == 'regression':\n", - " metric_value = np.sqrt(mean_squared_error(y_test, y_pred))\n", - " else:\n", - " metric_value = accuracy_score(y_test, y_pred)\n", - " \n", - " total_time = time.time() - start_time\n", - " \n", - " return {\n", - " 'analysis_type': analysis_type,\n", - " 'dataset_size': dataset_size,\n", - " 'training_time': training_time,\n", - " 'total_time': total_time,\n", - " metric_name: metric_value,\n", - " 'feature_importance': model.feature_importances_[:5].tolist(), # Top 5\n", - " 'training_samples': len(X_train),\n", - " 'test_samples': len(X_test)\n", - " }\n", - "\n", - "# Example: Parallel Computation\n", - "@cluster(cores=4, memory=\"8GB\")\n", - "def gcp_parallel_computation(n_iterations=1000):\n", - " \"\"\"Basic parallel computation example.\"\"\"\n", - " import numpy as np\n", - " import time\n", - " \n", - " start_time = time.time()\n", - " \n", - " # Simulate CPU-intensive work\n", - " results = []\n", - " for i in range(n_iterations):\n", - " # Monte Carlo pi estimation\n", - " points = np.random.random((1000, 2))\n", - " inside_circle = np.sum((points**2).sum(axis=1) <= 1)\n", - " pi_estimate = 4 * inside_circle / 1000\n", - " results.append(pi_estimate)\n", - " \n", - " computation_time = time.time() - start_time\n", - " final_pi_estimate = np.mean(results)\n", - " \n", - " return {\n", - " 'iterations': n_iterations,\n", - " 'pi_estimate': final_pi_estimate,\n", - " 'computation_time': computation_time,\n", - " 'accuracy': abs(final_pi_estimate - np.pi)\n", - " }\n", - "\n", - "print(\"✓ GCP computation examples defined\")\n", - "print(\"\\n📝 Example usage:\")\n", - "print(\"# Data analysis:\")\n", - "print(\"# result = gcp_data_analysis(dataset_size=50000, analysis_type='classification')\")\n", - "print(\"# print(f'Accuracy: {result[\\\"accuracy\\\"]:.4f}')\")\n", - "print(\"#\")\n", - "print(\"# Parallel computation:\")\n", - "print(\"# result = gcp_parallel_computation(n_iterations=5000)\")\n", - "print(\"# print(f'Pi estimate: {result[\\\"pi_estimate\\\"]:.6f}')\")\n", - "\n", - "# Example execution (commented out - uncomment after setup):\n", - "# result = gcp_data_analysis(dataset_size=5000, analysis_type='classification')\n", - "# print(f\"✓ Analysis completed: {result['accuracy']:.4f} accuracy\")\n", - "# print(f\"⏱️ Training time: {result['training_time']:.2f} seconds\")" - ] - }, - { - "cell_type": "markdown", - "id": "gke-setup", - "metadata": {}, - "source": [ - "## Method 2: Google Kubernetes Engine (GKE) Configuration\n", - "\n", - "GKE provides managed Kubernetes clusters ideal for containerized Clustrix workloads:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "gke-cluster-setup", - "metadata": {}, - "outputs": [], - "source": [ - "def setup_gke_cluster_for_clustrix(project_id, cluster_name='clustrix-cluster', zone='us-central1-a'):\n", - " \"\"\"\n", - " Setup GKE cluster optimized for Clustrix workloads.\n", - " \"\"\"\n", - " \n", - " gke_commands = f\"\"\"\n", - "# Enable required APIs\n", - "gcloud services enable container.googleapis.com \\\n", - " --project {project_id}\n", - "\n", - "# Create GKE cluster with auto-scaling\n", - "gcloud container clusters create {cluster_name} \\\n", - " --project {project_id} \\\n", - " --zone {zone} \\\n", - " --machine-type e2-standard-4 \\\n", - " --num-nodes 1 \\\n", - " --enable-autoscaling \\\n", - " --min-nodes 0 \\\n", - " --max-nodes 10 \\\n", - " --enable-autorepair \\\n", - " --enable-autoupgrade \\\n", - " --disk-size 50GB \\\n", - " --disk-type pd-ssd \\\n", - " --enable-network-policy \\\n", - " --enable-ip-alias \\\n", - " --labels purpose=clustrix,environment=tutorial\n", - "\n", - "# Get cluster credentials\n", - "gcloud container clusters get-credentials {cluster_name} \\\n", - " --project {project_id} \\\n", - " --zone {zone}\n", - "\n", - "# Verify cluster access\n", - "kubectl get nodes\n", - "\n", - "# Create clustrix namespace\n", - "kubectl create namespace clustrix\n", - "\n", - "# Set as default namespace\n", - "kubectl config set-context --current --namespace=clustrix\n", - "\"\"\"\n", - " \n", - " # Clustrix job template for Kubernetes\n", - " k8s_job_template = \"\"\"\n", - "apiVersion: batch/v1\n", - "kind: Job\n", - "metadata:\n", - " name: clustrix-job-${JOB_ID}\n", - " namespace: clustrix\n", - "spec:\n", - " template:\n", - " spec:\n", - " restartPolicy: Never\n", - " containers:\n", - " - name: clustrix-worker\n", - " image: python:3.11-slim\n", - " command: [\"bash\", \"-c\"]\n", - " args:\n", - " - |\n", - " pip install clustrix numpy scipy pandas scikit-learn\n", - " python -c \"\n", - " import pickle\n", - " import sys\n", - " \n", - " # Load and execute function\n", - " with open('/data/function_data.pkl', 'rb') as f:\n", - " data = pickle.load(f)\n", - " \n", - " func = pickle.loads(data['function'])\n", - " args = pickle.loads(data['args'])\n", - " kwargs = pickle.loads(data['kwargs'])\n", - " \n", - " try:\n", - " result = func(*args, **kwargs)\n", - " with open('/data/result.pkl', 'wb') as f:\n", - " pickle.dump(result, f)\n", - " except Exception as e:\n", - " with open('/data/error.pkl', 'wb') as f:\n", - " pickle.dump({'error': str(e)}, f)\n", - " raise\n", - " \"\n", - " resources:\n", - " requests:\n", - " memory: \"2Gi\"\n", - " cpu: \"1\"\n", - " limits:\n", - " memory: \"4Gi\"\n", - " cpu: \"2\"\n", - " volumeMounts:\n", - " - name: job-data\n", - " mountPath: /data\n", - " volumes:\n", - " - name: job-data\n", - " persistentVolumeClaim:\n", - " claimName: clustrix-pvc\n", - " backoffLimit: 3\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'cluster_name': cluster_name,\n", - " 'project_id': project_id,\n", - " 'zone': zone,\n", - " 'setup_commands': gke_commands,\n", - " 'job_template': k8s_job_template\n", - " }\n", - "\n", - "def configure_clustrix_for_gke(cluster_endpoint, cluster_name):\n", - " \"\"\"Configure Clustrix to use GKE cluster.\"\"\"\n", - " configure(\n", - " cluster_type=\"kubernetes\",\n", - " cluster_host=cluster_endpoint,\n", - " # For GKE, authentication is handled via kubectl config\n", - " remote_work_dir=\"~/.clustrix/jobs\",\n", - " package_manager=\"pip\", # Container-based, pip is fine\n", - " default_cores=2,\n", - " default_memory=\"4GB\",\n", - " default_time=\"01:00:00\"\n", - " )\n", - " print(f\"✓ Configured Clustrix for GKE cluster: {cluster_name}\")\n", - "\n", - "# Create GKE configuration\n", - "gke_config = setup_gke_cluster_for_clustrix(\n", - " project_id=PROJECT_ID,\n", - " cluster_name='clustrix-cluster'\n", - ")\n", - "\n", - "print(\"=== GKE Cluster Setup Commands ===\")\n", - "print(gke_config['setup_commands'])\n", - "print(\"\\n=== Kubernetes Job Template ===\")\n", - "print(gke_config['job_template'])\n", - "print(\"\\n📝 Note: GKE integration requires additional implementation in Clustrix.\")\n", - "print(\"Current Clustrix supports basic Kubernetes, but GKE-specific features need custom setup.\")" - ] - }, - { - "cell_type": "markdown", - "id": "gcp-batch", - "metadata": {}, - "source": [ - "## Method 3: Google Cloud Batch\n", - "\n", - "Google Cloud Batch provides managed job scheduling for large-scale workloads:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "gcp-batch-setup", - "metadata": {}, - "outputs": [], - "source": [ - "def setup_gcp_batch_environment(project_id, region='us-central1'):\n", - " \"\"\"\n", - " Setup Google Cloud Batch for Clustrix workloads.\n", - " \"\"\"\n", - " \n", - " batch_setup_commands = f\"\"\"\n", - "# Enable Batch API\n", - "gcloud services enable batch.googleapis.com \\\n", - " --project {project_id}\n", - "\n", - "# Create a service account for Batch jobs\n", - "gcloud iam service-accounts create clustrix-batch-sa \\\n", - " --project {project_id} \\\n", - " --description=\"Service account for Clustrix Batch jobs\" \\\n", - " --display-name=\"Clustrix Batch Service Account\"\n", - "\n", - "# Grant necessary permissions\n", - "gcloud projects add-iam-policy-binding {project_id} \\\n", - " --member=\"serviceAccount:clustrix-batch-sa@{project_id}.iam.gserviceaccount.com\" \\\n", - " --role=\"roles/batch.jobsEditor\"\n", - "\n", - "gcloud projects add-iam-policy-binding {project_id} \\\n", - " --member=\"serviceAccount:clustrix-batch-sa@{project_id}.iam.gserviceaccount.com\" \\\n", - " --role=\"roles/storage.objectAdmin\"\n", - "\n", - "# Create Cloud Storage bucket for job data\n", - "gsutil mb -p {project_id} -l {region} gs://{project_id}-clustrix-batch\n", - "\"\"\"\n", - " \n", - " # Batch job configuration template\n", - " batch_job_config = {\n", - " \"taskGroups\": [\n", - " {\n", - " \"taskSpec\": {\n", - " \"runnables\": [\n", - " {\n", - " \"script\": {\n", - " \"text\": f\"\"\"\n", - "#!/bin/bash\n", - "set -e\n", - "\n", - "# Install required packages\n", - "pip3 install clustrix numpy scipy pandas scikit-learn\n", - "\n", - "# Download job data from Cloud Storage\n", - "gsutil cp gs://{project_id}-clustrix-batch/jobs/${{BATCH_JOB_ID}}/function_data.pkl .\n", - "\n", - "# Execute the function\n", - "python3 -c \"\n", - "import pickle\n", - "import traceback\n", - "\n", - "try:\n", - " with open('function_data.pkl', 'rb') as f:\n", - " data = pickle.load(f)\n", - " \n", - " func = pickle.loads(data['function'])\n", - " args = pickle.loads(data['args'])\n", - " kwargs = pickle.loads(data['kwargs'])\n", - " \n", - " result = func(*args, **kwargs)\n", - " \n", - " with open('result.pkl', 'wb') as f:\n", - " pickle.dump(result, f)\n", - " \n", - "except Exception as e:\n", - " with open('error.pkl', 'wb') as f:\n", - " pickle.dump({{\n", - " 'error': str(e),\n", - " 'traceback': traceback.format_exc()\n", - " }}, f)\n", - " raise\n", - "\"\n", - "\n", - "# Upload results to Cloud Storage\n", - "gsutil cp result.pkl gs://{project_id}-clustrix-batch/jobs/${{BATCH_JOB_ID}}/result.pkl || \\\n", - "gsutil cp error.pkl gs://{project_id}-clustrix-batch/jobs/${{BATCH_JOB_ID}}/error.pkl\n", - "\"\"\"\n", - " }\n", - " }\n", - " ],\n", - " \"computeResource\": {\n", - " \"cpuMilli\": 2000, # 2 CPUs\n", - " \"memoryMib\": 4096 # 4 GB RAM\n", - " },\n", - " \"maxRetryCount\": 2,\n", - " \"maxRunDuration\": \"3600s\" # 1 hour\n", - " },\n", - " \"taskCount\": 1\n", - " }\n", - " ],\n", - " \"allocationPolicy\": {\n", - " \"instances\": [\n", - " {\n", - " \"instanceTemplate\": {\n", - " \"machineType\": \"e2-standard-2\",\n", - " \"provisioningModel\": \"STANDARD\"\n", - " }\n", - " }\n", - " ]\n", - " },\n", - " \"labels\": {\n", - " \"purpose\": \"clustrix\",\n", - " \"environment\": \"tutorial\"\n", - " },\n", - " \"logsPolicy\": {\n", - " \"destination\": \"CLOUD_LOGGING\"\n", - " }\n", - " }\n", - " \n", - " return {\n", - " 'project_id': project_id,\n", - " 'region': region,\n", - " 'bucket_name': f'{project_id}-clustrix-batch',\n", - " 'service_account': f'clustrix-batch-sa@{project_id}.iam.gserviceaccount.com',\n", - " 'job_config': batch_job_config,\n", - " 'setup_commands': batch_setup_commands\n", - " }\n", - "\n", - "# Create Batch configuration\n", - "batch_config = setup_gcp_batch_environment(PROJECT_ID)\n", - "\n", - "print(\"=== Google Cloud Batch Setup Commands ===\")\n", - "print(batch_config['setup_commands'])\n", - "print(\"\\n=== Batch Job Configuration ===\")\n", - "print(json.dumps(batch_config['job_config'], indent=2))\n", - "print(\"\\n💡 Google Cloud Batch provides excellent integration for large-scale Clustrix workloads.\")" - ] - }, - { - "cell_type": "markdown", - "id": "cloud-storage", - "metadata": {}, - "source": [ - "## Data Management with Google Cloud Storage" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cloud-storage-integration", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=2, memory=\"4GB\")\n", - "def process_gcs_data(bucket_name, input_blob, output_blob, project_id=None):\n", - " \"\"\"Process data from Google Cloud Storage and save results back.\"\"\"\n", - " from google.cloud import storage\n", - " import numpy as np\n", - " import pickle\n", - " import io\n", - " import time\n", - " \n", - " # Initialize Cloud Storage client\n", - " storage_client = storage.Client(project=project_id)\n", - " bucket = storage_client.bucket(bucket_name)\n", - " \n", - " # Download data from Cloud Storage\n", - " input_blob_obj = bucket.blob(input_blob)\n", - " data_bytes = input_blob_obj.download_as_bytes()\n", - " data = pickle.loads(data_bytes)\n", - " \n", - " # Process the data\n", - " processed_data = {\n", - " 'original_shape': data.shape if hasattr(data, 'shape') else len(data) if hasattr(data, '__len__') else 'scalar',\n", - " 'mean': float(np.mean(data)) if hasattr(data, '__iter__') else float(data),\n", - " 'std': float(np.std(data)) if hasattr(data, '__iter__') else 0.0,\n", - " 'max': float(np.max(data)) if hasattr(data, '__iter__') else float(data),\n", - " 'min': float(np.min(data)) if hasattr(data, '__iter__') else float(data),\n", - " 'processing_timestamp': time.time(),\n", - " 'processed_on': 'gcp-compute-engine',\n", - " 'data_type': str(type(data).__name__)\n", - " }\n", - " \n", - " # Advanced processing based on data type\n", - " if hasattr(data, 'shape') and len(data.shape) >= 2:\n", - " # Matrix operations\n", - " processed_data.update({\n", - " 'matrix_rank': int(np.linalg.matrix_rank(data)) if data.shape[0] == data.shape[1] else 'non_square',\n", - " 'frobenius_norm': float(np.linalg.norm(data, 'fro')),\n", - " 'condition_number': float(np.linalg.cond(data)) if data.shape[0] == data.shape[1] else None\n", - " })\n", - " \n", - " # Upload results to Cloud Storage\n", - " output_bytes = pickle.dumps(processed_data)\n", - " output_blob_obj = bucket.blob(output_blob)\n", - " output_blob_obj.upload_from_string(output_bytes)\n", - " \n", - " return f\"Processed data saved to gs://{bucket_name}/{output_blob}\"\n", - "\n", - "# Utility functions for Google Cloud Storage\n", - "def upload_to_gcs(data, bucket_name, blob_name, project_id=None):\n", - " \"\"\"Upload data to Google Cloud Storage.\"\"\"\n", - " storage_client = storage.Client(project=project_id)\n", - " bucket = storage_client.bucket(bucket_name)\n", - " blob = bucket.blob(blob_name)\n", - " \n", - " data_bytes = pickle.dumps(data)\n", - " blob.upload_from_string(data_bytes)\n", - " return f\"gs://{bucket_name}/{blob_name}\"\n", - "\n", - "def download_from_gcs(bucket_name, blob_name, project_id=None):\n", - " \"\"\"Download data from Google Cloud Storage.\"\"\"\n", - " storage_client = storage.Client(project=project_id)\n", - " bucket = storage_client.bucket(bucket_name)\n", - " blob = bucket.blob(blob_name)\n", - " \n", - " data_bytes = blob.download_as_bytes()\n", - " return pickle.loads(data_bytes)\n", - "\n", - "def create_gcs_bucket_for_clustrix(project_id, bucket_name, location='us-central1'):\n", - " \"\"\"Create a Cloud Storage bucket for Clustrix data.\"\"\"\n", - " gcs_commands = f\"\"\"\n", - "# Create bucket with appropriate settings\n", - "gsutil mb -p {project_id} -l {location} gs://{bucket_name}\n", - "\n", - "# Set lifecycle policy to delete temporary files after 7 days\n", - "echo '{{\n", - " \"lifecycle\": {{\n", - " \"rule\": [\n", - " {{\n", - " \"action\": {{\"type\": \"Delete\"}},\n", - " \"condition\": {{\n", - " \"age\": 7,\n", - " \"matchesPrefix\": [\"temp/\"]\n", - " }}\n", - " }}\n", - " ]\n", - " }}\n", - "}}' > lifecycle.json\n", - "\n", - "gsutil lifecycle set lifecycle.json gs://{bucket_name}\n", - "\n", - "# Set up proper permissions (if using service account)\n", - "gsutil iam ch serviceAccount:clustrix-batch-sa@{project_id}.iam.gserviceaccount.com:objectAdmin gs://{bucket_name}\n", - "\"\"\"\n", - " \n", - " return gcs_commands\n", - "\n", - "# Create bucket configuration\n", - "BUCKET_NAME = f\"{PROJECT_ID}-clustrix-data\"\n", - "bucket_commands = create_gcs_bucket_for_clustrix(PROJECT_ID, BUCKET_NAME)\n", - "\n", - "print(\"=== Commands to create Cloud Storage bucket ===\")\n", - "print(bucket_commands)\n", - "\n", - "# Example usage (commented out - uncomment after creating bucket):\n", - "# sample_data = np.random.rand(1000, 100)\n", - "# upload_location = upload_to_gcs(sample_data, BUCKET_NAME, 'input/sample_data.pkl', PROJECT_ID)\n", - "# print(f\"✓ Data uploaded to {upload_location}\")\n", - "# \n", - "# result = process_gcs_data(BUCKET_NAME, 'input/sample_data.pkl', 'output/results.pkl', PROJECT_ID)\n", - "# print(f\"✓ Processing completed: {result}\")\n", - "\n", - "print(\"\\n✓ Google Cloud Storage integration functions defined.\")\n", - "print(\"Execute the bucket creation commands above, then uncomment the example usage.\")" - ] - }, - { - "cell_type": "markdown", - "id": "vertex-ai", - "metadata": {}, - "source": [ - "## Vertex AI Integration" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "vertex-ai-setup", - "metadata": {}, - "outputs": [], - "source": [ - "def setup_vertex_ai_for_clustrix(project_id, region='us-central1'):\n", - " \"\"\"\n", - " Setup Vertex AI for ML workloads with Clustrix.\n", - " \"\"\"\n", - " \n", - " vertex_commands = f\"\"\"\n", - "# Enable Vertex AI API\n", - "gcloud services enable aiplatform.googleapis.com \\\n", - " --project {project_id}\n", - "\n", - "# Create Vertex AI custom training job\n", - "gcloud ai custom-jobs create \\\n", - " --region={region} \\\n", - " --display-name=clustrix-training-job \\\n", - " --config=training_job_config.yaml\n", - "\n", - "# Create Vertex AI endpoints for model serving\n", - "gcloud ai endpoints create \\\n", - " --region={region} \\\n", - " --display-name=clustrix-model-endpoint\n", - "\"\"\"\n", - " \n", - " # Vertex AI training job configuration\n", - " training_config = f\"\"\"\n", - "# training_job_config.yaml\n", - "workerPoolSpecs:\n", - "- machineSpec:\n", - " machineType: e2-standard-4\n", - " replicaCount: 1\n", - " containerSpec:\n", - " imageUri: gcr.io/cloud-aiplatform/training/tf-cpu.2-8:latest\n", - " command:\n", - " - python3\n", - " - -c\n", - " args:\n", - " - |\n", - " import subprocess\n", - " import sys\n", - " \n", - " # Install clustrix\n", - " subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'clustrix', 'numpy', 'pandas', 'scikit-learn'])\n", - " \n", - " # Your training code here\n", - " print(\"Clustrix training job completed on Vertex AI\")\n", - " env:\n", - " - name: GOOGLE_CLOUD_PROJECT\n", - " value: {project_id}\n", - " - name: AIP_MODEL_DIR\n", - " value: gs://{project_id}-vertex-models\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'project_id': project_id,\n", - " 'region': region,\n", - " 'setup_commands': vertex_commands,\n", - " 'training_config': training_config\n", - " }\n", - "\n", - "@cluster(cores=4, memory=\"8GB\")\n", - "def vertex_ai_ml_pipeline(dataset_config, model_config, project_id, bucket_name):\n", - " \"\"\"ML pipeline that could run on Vertex AI with Clustrix.\"\"\"\n", - " import numpy as np\n", - " from sklearn.ensemble import GradientBoostingClassifier\n", - " from sklearn.model_selection import cross_val_score, GridSearchCV\n", - " from sklearn.datasets import make_classification\n", - " from sklearn.metrics import classification_report\n", - " from google.cloud import storage\n", - " import pickle\n", - " import time\n", - " \n", - " start_time = time.time()\n", - " \n", - " # Generate or load dataset\n", - " X, y = make_classification(\n", - " n_samples=dataset_config['n_samples'],\n", - " n_features=dataset_config['n_features'],\n", - " n_classes=dataset_config['n_classes'],\n", - " n_informative=dataset_config.get('n_informative', dataset_config['n_features'] // 2),\n", - " random_state=42\n", - " )\n", - " \n", - " # Hyperparameter tuning\n", - " param_grid = {\n", - " 'n_estimators': [50, 100, 200],\n", - " 'max_depth': [3, 5, 7],\n", - " 'learning_rate': [0.01, 0.1, 0.2]\n", - " }\n", - " \n", - " # Grid search with cross-validation\n", - " model = GradientBoostingClassifier(random_state=42)\n", - " grid_search = GridSearchCV(\n", - " model, param_grid, cv=5, scoring='accuracy', n_jobs=-1\n", - " )\n", - " \n", - " grid_search.fit(X, y)\n", - " \n", - " # Get best model\n", - " best_model = grid_search.best_estimator_\n", - " \n", - " # Evaluate with cross-validation\n", - " cv_scores = cross_val_score(best_model, X, y, cv=5, scoring='accuracy')\n", - " \n", - " # Save model to Cloud Storage\n", - " storage_client = storage.Client(project=project_id)\n", - " bucket = storage_client.bucket(bucket_name)\n", - " \n", - " model_blob = bucket.blob('models/clustrix_model.pkl')\n", - " model_bytes = pickle.dumps(best_model)\n", - " model_blob.upload_from_string(model_bytes)\n", - " \n", - " total_time = time.time() - start_time\n", - " \n", - " return {\n", - " 'best_params': grid_search.best_params_,\n", - " 'best_score': grid_search.best_score_,\n", - " 'cv_mean_score': cv_scores.mean(),\n", - " 'cv_std_score': cv_scores.std(),\n", - " 'training_time': total_time,\n", - " 'model_location': f'gs://{bucket_name}/models/clustrix_model.pkl',\n", - " 'feature_importance': best_model.feature_importances_[:10].tolist(), # Top 10\n", - " 'dataset_size': len(X)\n", - " }\n", - "\n", - "# Setup Vertex AI configuration\n", - "vertex_config = setup_vertex_ai_for_clustrix(PROJECT_ID)\n", - "\n", - "print(\"=== Vertex AI Setup Commands ===\")\n", - "print(vertex_config['setup_commands'])\n", - "print(\"\\n=== Training Job Configuration ===\")\n", - "print(vertex_config['training_config'])\n", - "\n", - "# Example usage (commented out):\n", - "# dataset_params = {'n_samples': 10000, 'n_features': 20, 'n_classes': 3}\n", - "# model_params = {}\n", - "# result = vertex_ai_ml_pipeline(dataset_params, model_params, PROJECT_ID, BUCKET_NAME)\n", - "# print(f\"✓ Best model score: {result['best_score']:.4f}\")\n", - "# print(f\"✓ Model saved to: {result['model_location']}\")\n", - "\n", - "print(\"\\n✓ Vertex AI integration examples defined.\")" - ] - }, - { - "cell_type": "markdown", - "id": "gcp-security", - "metadata": {}, - "source": [ - "## Security Best Practices" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "gcp-security-setup", - "metadata": {}, - "outputs": [], - "source": [ - "def setup_gcp_security_for_clustrix(project_id):\n", - " \"\"\"\n", - " Security configuration for GCP + Clustrix deployment.\n", - " \"\"\"\n", - " \n", - " security_commands = f\"\"\"\n", - "# Create VPC with private subnets\n", - "gcloud compute networks create clustrix-vpc \\\n", - " --project {project_id} \\\n", - " --subnet-mode custom\n", - "\n", - "gcloud compute networks subnets create clustrix-subnet \\\n", - " --project {project_id} \\\n", - " --network clustrix-vpc \\\n", - " --range 10.1.0.0/24 \\\n", - " --region us-central1 \\\n", - " --enable-private-ip-google-access\n", - "\n", - "# Create firewall rules (restrictive)\n", - "gcloud compute firewall-rules create clustrix-allow-ssh \\\n", - " --project {project_id} \\\n", - " --network clustrix-vpc \\\n", - " --allow tcp:22 \\\n", - " --source-ranges YOUR_IP/32 \\\n", - " --target-tags clustrix\n", - "\n", - "gcloud compute firewall-rules create clustrix-internal \\\n", - " --project {project_id} \\\n", - " --network clustrix-vpc \\\n", - " --allow tcp,udp,icmp \\\n", - " --source-ranges 10.1.0.0/24 \\\n", - " --target-tags clustrix\n", - "\n", - "# Create service account with minimal permissions\n", - "gcloud iam service-accounts create clustrix-compute \\\n", - " --project {project_id} \\\n", - " --description=\"Service account for Clustrix compute instances\" \\\n", - " --display-name=\"Clustrix Compute Service Account\"\n", - "\n", - "# Grant only necessary permissions\n", - "gcloud projects add-iam-policy-binding {project_id} \\\n", - " --member=\"serviceAccount:clustrix-compute@{project_id}.iam.gserviceaccount.com\" \\\n", - " --role=\"roles/storage.objectAdmin\"\n", - "\n", - "gcloud projects add-iam-policy-binding {project_id} \\\n", - " --member=\"serviceAccount:clustrix-compute@{project_id}.iam.gserviceaccount.com\" \\\n", - " --role=\"roles/logging.logWriter\"\n", - "\n", - "# Enable OS Login for better SSH key management\n", - "gcloud compute project-info add-metadata \\\n", - " --project {project_id} \\\n", - " --metadata enable-oslogin=TRUE\n", - "\n", - "# Create Cloud KMS key for encryption\n", - "gcloud kms keyrings create clustrix-keyring \\\n", - " --project {project_id} \\\n", - " --location global\n", - "\n", - "gcloud kms keys create clustrix-key \\\n", - " --project {project_id} \\\n", - " --keyring clustrix-keyring \\\n", - " --location global \\\n", - " --purpose encryption\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'project_id': project_id,\n", - " 'vpc_name': 'clustrix-vpc',\n", - " 'subnet_name': 'clustrix-subnet',\n", - " 'service_account': f'clustrix-compute@{project_id}.iam.gserviceaccount.com',\n", - " 'security_commands': security_commands\n", - " }\n", - "\n", - "# Generate security configuration\n", - "security_config = setup_gcp_security_for_clustrix(PROJECT_ID)\n", - "\n", - "print(\"=== GCP Security Setup Commands ===\")\n", - "print(security_config['security_commands'])\n", - "print(f\"\\n✓ Security configuration templates generated for project: {PROJECT_ID}\")\n", - "print(f\"✓ VPC: {security_config['vpc_name']}\")\n", - "print(f\"✓ Service Account: {security_config['service_account']}\")\n", - "print(\"\\n⚠️ Remember to replace 'YOUR_IP' with your actual IP address in the firewall rules!\")" - ] - }, - { - "cell_type": "markdown", - "id": "7zpjrtwse94", - "metadata": {}, - "source": [ - "### GCP Security Checklist for Clustrix\n", - "\n", - "✓ **Authentication and Access**\n", - "- Use IAM service accounts with minimal permissions\n", - "- Enable OS Login for centralized SSH key management\n", - "- Create custom VPC with private subnets\n", - "- Restrict firewall rules to specific IP ranges\n", - "\n", - "✓ **Infrastructure Security**\n", - "- Enable private Google access for instances without external IPs\n", - "- Use Cloud KMS for encryption at rest\n", - "- Enable audit logging and Cloud Security Command Center\n", - "- Use Binary Authorization for container security\n", - "\n", - "✓ **Network Security**\n", - "- Implement VPC Service Controls for data perimeter\n", - "- Enable DDoS protection and Cloud Armor\n", - "- Use Secret Manager for sensitive configuration\n", - "- Enable vulnerability scanning for container images\n", - "\n", - "✓ **Governance and Compliance**\n", - "- Set up budget alerts and billing account security\n", - "- Use organization policies for governance\n", - "- Regular security reviews and access audits" - ] - }, - { - "cell_type": "markdown", - "id": "cleanup-gcp", - "metadata": {}, - "source": [ - "## Resource Cleanup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cleanup-gcp-resources", - "metadata": {}, - "outputs": [], - "source": [ - "def cleanup_gcp_resources(project_id, zone='us-central1-a', region='us-central1'):\n", - " \"\"\"\n", - " Clean up GCP resources to avoid ongoing charges.\n", - " \n", - " Args:\n", - " project_id: GCP project ID\n", - " zone: Zone where resources were created\n", - " region: Region where resources were created\n", - " \"\"\"\n", - " \n", - " cleanup_commands = f\"\"\"\n", - "# List all compute instances\n", - "gcloud compute instances list --project {project_id}\n", - "\n", - "# Delete specific instances\n", - "gcloud compute instances delete clustrix-instance \\\n", - " --project {project_id} \\\n", - " --zone {zone} \\\n", - " --quiet\n", - "\n", - "# Delete managed instance groups\n", - "gcloud compute instance-groups managed delete clustrix-preemptible-group \\\n", - " --project {project_id} \\\n", - " --zone {zone} \\\n", - " --quiet\n", - "\n", - "# Delete instance templates\n", - "gcloud compute instance-templates delete clustrix-preemptible-template \\\n", - " --project {project_id} \\\n", - " --quiet\n", - "\n", - "# Delete GKE clusters\n", - "gcloud container clusters delete clustrix-cluster \\\n", - " --project {project_id} \\\n", - " --zone {zone} \\\n", - " --quiet\n", - "\n", - "# Delete Cloud Storage buckets (BE CAREFUL - THIS DELETES ALL DATA)\n", - "gsutil -m rm -r gs://{project_id}-clustrix-batch\n", - "gsutil -m rm -r gs://{project_id}-vertex-models\n", - "gsutil -m rm -r gs://{project_id}-clustrix-data\n", - "\n", - "# Delete firewall rules\n", - "gcloud compute firewall-rules delete clustrix-allow-ssh clustrix-internal \\\n", - " --project {project_id} \\\n", - " --quiet\n", - "\n", - "# Delete VPC network\n", - "gcloud compute networks subnets delete clustrix-subnet \\\n", - " --project {project_id} \\\n", - " --region {region} \\\n", - " --quiet\n", - "\n", - "gcloud compute networks delete clustrix-vpc \\\n", - " --project {project_id} \\\n", - " --quiet\n", - "\n", - "# Delete service accounts\n", - "gcloud iam service-accounts delete clustrix-compute@{project_id}.iam.gserviceaccount.com \\\n", - " --project {project_id} \\\n", - " --quiet\n", - "\n", - "gcloud iam service-accounts delete clustrix-batch-sa@{project_id}.iam.gserviceaccount.com \\\n", - " --project {project_id} \\\n", - " --quiet\n", - "\n", - "# List remaining billable resources\n", - "echo \"=== Remaining billable resources ===\"\n", - "gcloud compute instances list --project {project_id}\n", - "gcloud compute disks list --project {project_id}\n", - "gcloud compute addresses list --project {project_id}\n", - "gcloud container clusters list --project {project_id}\n", - "\"\"\"\n", - " \n", - " return {\n", - " 'project_id': project_id,\n", - " 'zone': zone,\n", - " 'region': region,\n", - " 'cleanup_commands': cleanup_commands\n", - " }\n", - "\n", - "# Generate cleanup commands\n", - "cleanup_info = cleanup_gcp_resources(PROJECT_ID)\n", - "\n", - "print(f\"=== GCP Resource Cleanup Commands for Project: {PROJECT_ID} ===\")\n", - "print(cleanup_info['cleanup_commands'])\n", - "print(\"\\n⚠️ WARNING: Some commands will permanently delete resources and data!\")\n", - "print(\"Review each resource before deleting and ensure you have backups if needed.\")\n", - "print(\"\\n💡 TIP: Use 'gcloud compute instances stop' instead of 'delete' to preserve instances while stopping charges.\")\n", - "print(\"\\n✓ Cleanup commands generated. Always verify resources before deletion!\")" - ] - }, - { - "cell_type": "markdown", - "id": "advanced-gcp-example", - "metadata": {}, - "source": [ - "## Advanced Example: Distributed Scientific Computing" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "scientific-computing-example", - "metadata": {}, - "outputs": [], - "source": [ - "# Advanced Scientific Computing\n", - "@cluster(cores=4, memory=\"8GB\", time=\"01:00:00\")\n", - "def gcp_scientific_simulation(simulation_params, storage_config=None):\n", - " \"\"\"\n", - " Distributed scientific simulation using GCP infrastructure.\n", - " \"\"\"\n", - " import numpy as np\n", - " from scipy.integrate import odeint\n", - " from scipy.optimize import minimize\n", - " import pickle\n", - " import time\n", - " import matplotlib\n", - " matplotlib.use('Agg') # Use non-interactive backend\n", - " import matplotlib.pyplot as plt\n", - " import io\n", - " \n", - " # Only import GCP storage if config provided\n", - " if storage_config:\n", - " from google.cloud import storage\n", - " \n", - " def lorenz_system(state, t, sigma, rho, beta):\n", - " \"\"\"Lorenz attractor differential equations.\"\"\"\n", - " x, y, z = state\n", - " return [\n", - " sigma * (y - x),\n", - " x * (rho - z) - y,\n", - " x * y - beta * z\n", - " ]\n", - " \n", - " def simulate_lorenz(params, time_points):\n", - " \"\"\"Simulate Lorenz system with given parameters.\"\"\"\n", - " initial_state = [1.0, 1.0, 1.0]\n", - " solution = odeint(\n", - " lorenz_system, initial_state, time_points,\n", - " args=(params['sigma'], params['rho'], params['beta'])\n", - " )\n", - " return solution\n", - " \n", - " start_time = time.time()\n", - " \n", - " # Parameter sweep\n", - " parameter_sets = simulation_params['parameter_sets']\n", - " time_points = np.linspace(0, simulation_params['max_time'], simulation_params['num_points'])\n", - " \n", - " results = []\n", - " \n", - " for i, params in enumerate(parameter_sets):\n", - " # Run simulation\n", - " solution = simulate_lorenz(params, time_points)\n", - " \n", - " # Analyze results\n", - " x, y, z = solution[:, 0], solution[:, 1], solution[:, 2]\n", - " \n", - " analysis = {\n", - " 'params': params,\n", - " 'max_x': float(np.max(x)),\n", - " 'min_x': float(np.min(x)),\n", - " 'max_y': float(np.max(y)),\n", - " 'min_y': float(np.min(y)),\n", - " 'max_z': float(np.max(z)),\n", - " 'min_z': float(np.min(z)),\n", - " 'mean_energy': float(np.mean(x**2 + y**2 + z**2)),\n", - " 'final_state': [float(x[-1]), float(y[-1]), float(z[-1])],\n", - " 'std_x': float(np.std(x)),\n", - " 'std_y': float(np.std(y)),\n", - " 'std_z': float(np.std(z))\n", - " }\n", - " \n", - " results.append(analysis)\n", - " \n", - " # Create visualization for first few parameter sets\n", - " if i < 3:\n", - " fig = plt.figure(figsize=(12, 4))\n", - " \n", - " # Time series\n", - " plt.subplot(1, 3, 1)\n", - " plt.plot(time_points, x, label='X', alpha=0.8)\n", - " plt.plot(time_points, y, label='Y', alpha=0.8)\n", - " plt.plot(time_points, z, label='Z', alpha=0.8)\n", - " plt.xlabel('Time')\n", - " plt.ylabel('State')\n", - " plt.title(f'Lorenz System (σ={params[\"sigma\"]}, ρ={params[\"rho\"]}, β={params[\"beta\"]})')\n", - " plt.legend()\n", - " plt.grid(True, alpha=0.3)\n", - " \n", - " # Phase space (X-Y)\n", - " plt.subplot(1, 3, 2)\n", - " plt.plot(x, y, alpha=0.7, linewidth=0.8)\n", - " plt.xlabel('X')\n", - " plt.ylabel('Y')\n", - " plt.title('X-Y Phase Space')\n", - " plt.grid(True, alpha=0.3)\n", - " \n", - " # Phase space (X-Z)\n", - " plt.subplot(1, 3, 3)\n", - " plt.plot(x, z, alpha=0.7, linewidth=0.8)\n", - " plt.xlabel('X')\n", - " plt.ylabel('Z')\n", - " plt.title('X-Z Phase Space')\n", - " plt.grid(True, alpha=0.3)\n", - " \n", - " plt.tight_layout()\n", - " \n", - " # Save plot to Cloud Storage if configured\n", - " if storage_config:\n", - " try:\n", - " img_buffer = io.BytesIO()\n", - " plt.savefig(img_buffer, format='png', dpi=150, bbox_inches='tight')\n", - " img_buffer.seek(0)\n", - " \n", - " storage_client = storage.Client(project=storage_config['project_id'])\n", - " bucket = storage_client.bucket(storage_config['bucket_name'])\n", - " \n", - " plot_blob = bucket.blob(f\"plots/lorenz_simulation_{i}.png\")\n", - " plot_blob.upload_from_string(img_buffer.getvalue(), content_type='image/png')\n", - " except Exception as e:\n", - " print(f\"Warning: Could not save plot to GCS: {e}\")\n", - " \n", - " plt.close()\n", - " \n", - " computation_time = time.time() - start_time\n", - " \n", - " # Calculate summary statistics\n", - " energies = [r['mean_energy'] for r in results]\n", - " summary_stats = {\n", - " 'total_simulations': len(parameter_sets),\n", - " 'computation_time': computation_time,\n", - " 'average_energy': np.mean(energies),\n", - " 'max_energy': max(energies),\n", - " 'min_energy': min(energies),\n", - " 'energy_std': np.std(energies),\n", - " 'time_per_simulation': computation_time / len(parameter_sets)\n", - " }\n", - " \n", - " # Save detailed results to Cloud Storage if configured\n", - " if storage_config:\n", - " try:\n", - " storage_client = storage.Client(project=storage_config['project_id'])\n", - " bucket = storage_client.bucket(storage_config['bucket_name'])\n", - " \n", - " results_blob = bucket.blob(\"results/simulation_results.pkl\")\n", - " results_data = {\n", - " 'simulation_params': simulation_params,\n", - " 'results': results,\n", - " 'summary_stats': summary_stats,\n", - " 'timestamp': time.time()\n", - " }\n", - " results_bytes = pickle.dumps(results_data)\n", - " results_blob.upload_from_string(results_bytes)\n", - " except Exception as e:\n", - " print(f\"Warning: Could not save results to GCS: {e}\")\n", - " \n", - " return {\n", - " 'num_simulations': len(parameter_sets),\n", - " 'computation_time': computation_time,\n", - " 'summary_stats': summary_stats,\n", - " 'results_preview': results[:2], # First 2 for brevity\n", - " 'storage_location': f\"gs://{storage_config['bucket_name']}/results/\" if storage_config else None,\n", - " 'plots_saved': min(3, len(parameter_sets))\n", - " }\n", - "\n", - "# Monte Carlo simulation example\n", - "@cluster(cores=2, memory=\"4GB\")\n", - "def gcp_monte_carlo_simulation(n_samples=1000000):\n", - " \"\"\"Monte Carlo simulation for option pricing.\"\"\"\n", - " import numpy as np\n", - " import time\n", - " \n", - " start_time = time.time()\n", - " \n", - " # Black-Scholes parameters\n", - " S0 = 100 # Initial stock price\n", - " K = 105 # Strike price\n", - " T = 1.0 # Time to expiration\n", - " r = 0.05 # Risk-free rate\n", - " sigma = 0.2 # Volatility\n", - " \n", - " # Generate random samples\n", - " np.random.seed(42)\n", - " Z = np.random.standard_normal(n_samples)\n", - " \n", - " # Simulate stock prices at expiration\n", - " ST = S0 * np.exp((r - 0.5 * sigma**2) * T + sigma * np.sqrt(T) * Z)\n", - " \n", - " # Calculate option payoffs\n", - " call_payoffs = np.maximum(ST - K, 0)\n", - " put_payoffs = np.maximum(K - ST, 0)\n", - " \n", - " # Discount to present value\n", - " call_price = np.exp(-r * T) * np.mean(call_payoffs)\n", - " put_price = np.exp(-r * T) * np.mean(put_payoffs)\n", - " \n", - " # Calculate confidence intervals\n", - " call_std = np.std(call_payoffs) / np.sqrt(n_samples)\n", - " put_std = np.std(put_payoffs) / np.sqrt(n_samples)\n", - " \n", - " computation_time = time.time() - start_time\n", - " \n", - " return {\n", - " 'n_samples': n_samples,\n", - " 'computation_time': computation_time,\n", - " 'call_price': call_price,\n", - " 'put_price': put_price,\n", - " 'call_confidence_interval': [call_price - 1.96*call_std, call_price + 1.96*call_std],\n", - " 'put_confidence_interval': [put_price - 1.96*put_std, put_price + 1.96*put_std],\n", - " 'parameters': {'S0': S0, 'K': K, 'T': T, 'r': r, 'sigma': sigma}\n", - " }\n", - "\n", - "print(\"✓ Advanced scientific computing examples defined\")\n", - "\n", - "# Example simulation parameters\n", - "example_lorenz_params = {\n", - " 'parameter_sets': [\n", - " {'sigma': 10.0, 'rho': 28.0, 'beta': 8.0/3.0}, # Classic chaotic\n", - " {'sigma': 10.0, 'rho': 24.74, 'beta': 8.0/3.0}, # Near onset\n", - " {'sigma': 10.0, 'rho': 99.65, 'beta': 8.0/3.0}, # High rho\n", - " {'sigma': 16.0, 'rho': 45.92, 'beta': 4.0}, # Different params\n", - " ],\n", - " 'max_time': 25.0,\n", - " 'num_points': 5000\n", - "}\n", - "\n", - "print(\"\\n📝 Example usage:\")\n", - "print(\"# Lorenz simulation:\")\n", - "print(\"# result = gcp_scientific_simulation(example_lorenz_params)\")\n", - "print(\"# print(f'Completed {result[\\\"num_simulations\\\"]} simulations')\")\n", - "print(\"# print(f'Computation time: {result[\\\"computation_time\\\"]:.2f} seconds')\")\n", - "print(\"#\")\n", - "print(\"# Monte Carlo simulation:\")\n", - "print(\"# mc_result = gcp_monte_carlo_simulation(n_samples=5000000)\")\n", - "print(\"# print(f'Call option price: ${mc_result[\\\"call_price\\\"]:.2f}')\")\n", - "\n", - "print(\"\\n🧪 These examples demonstrate GCP's computational capabilities:\")\n", - "print(\" • Parallel differential equation solving\")\n", - "print(\" • Statistical simulations with confidence intervals\")\n", - "print(\" • Cloud Storage integration for results\")\n", - "print(\" • Visualization generation and storage\")" - ] - }, - { - "cell_type": "markdown", - "id": "gcp-summary", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "This tutorial covered:\n", - "\n", - "1. **Setup**: GCP authentication and Clustrix installation\n", - "2. **Compute Engine**: Direct VM configuration and management\n", - "3. **GKE Integration**: Kubernetes clusters for containerized workloads\n", - "4. **Cloud Batch**: Managed job scheduling for large-scale processing\n", - "5. **Cloud Storage**: Data management and result storage\n", - "6. **Vertex AI**: Machine learning platform integration\n", - "7. **Security**: Best practices for secure deployment\n", - "8. **Resource Management**: Proper cleanup procedures\n", - "\n", - "### Cost Monitoring\n", - "\n", - "For comprehensive cost monitoring, optimization strategies, and multi-cloud cost comparisons, see the dedicated [Cost Monitoring Tutorial](cost_monitoring_tutorial.ipynb).\n", - "\n", - "### Next Steps\n", - "\n", - "- Set up your GCP credentials and test the basic configuration\n", - "- Start with a simple Compute Engine instance for initial testing\n", - "- Consider GKE for containerized workloads and auto-scaling\n", - "- Explore Cloud Batch for large-scale batch processing\n", - "- Implement proper monitoring and access controls\n", - "- Review the Cost Monitoring Tutorial for expense tracking\n", - "\n", - "### GCP-Specific Advantages\n", - "\n", - "- **Preemptible/Spot VMs**: Exceptional cost savings (up to 80%)\n", - "- **Google Kubernetes Engine**: Industry-leading managed Kubernetes\n", - "- **Vertex AI**: Comprehensive ML platform with AutoML capabilities\n", - "- **Global Network**: Superior network performance and global reach\n", - "- **BigQuery Integration**: Seamless data analytics integration\n", - "- **Sustained Use Discounts**: Automatic discounts for sustained usage\n", - "\n", - "### Resources\n", - "\n", - "- [Google Cloud Compute Engine Documentation](https://cloud.google.com/compute/docs)\n", - "- [Google Kubernetes Engine Documentation](https://cloud.google.com/kubernetes-engine/docs)\n", - "- [Google Cloud Batch Documentation](https://cloud.google.com/batch/docs)\n", - "- [Vertex AI Documentation](https://cloud.google.com/vertex-ai/docs)\n", - "- [Google Cloud Storage Documentation](https://cloud.google.com/storage/docs)\n", - "- [GCP Pricing Calculator](https://cloud.google.com/products/calculator)\n", - "- [Clustrix Documentation](https://clustrix.readthedocs.io/)\n", - "- [Clustrix Cost Monitoring Tutorial](cost_monitoring_tutorial.ipynb)\n", - "\n", - "**Remember**: Always monitor your cloud costs and clean up resources when not in use!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/huggingface_spaces_tutorial.ipynb b/docs/source/notebooks/huggingface_spaces_tutorial.ipynb deleted file mode 100644 index 029659fa..00000000 --- a/docs/source/notebooks/huggingface_spaces_tutorial.ipynb +++ /dev/null @@ -1,185 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "hf-unverified-warning", - "metadata": {}, - "source": [ - "> **These backends are unverified.**\n", - "\n", - "> No clustrix cloud VM job (`provider=\"aws\"`, `\"gcp\"`, `\"azure\"`, `\"lambda\"`, `\"huggingface\"`) has been shown to run end to end. Until recently the path could not have run at all: every cloud job died with a `KeyError` on its first line. That was fixed (issue #119), but nothing has since demonstrated a completed cloud job, and `scripts/collect_execution_evidence.py` does not cover these backends. This notebook describes the intended interface, not something that has been run.\n", - "\n", - "> The backends that are verified working are `cluster_type=\"slurm\"`, `cluster_type=\"ssh\"` and `cluster_type=\"huggingface\"` (HuggingFace Jobs, which is a different thing from the HuggingFace Spaces provider described here). See the Supported Cluster Types section of the documentation.\n" - ] - }, - { - "cell_type": "markdown", - "id": "09bbd120", - "metadata": {}, - "source": [ - "# Part 1: HuggingFace Jobs -- the verified backend\n", - "\n", - "This is the part of this notebook that documents something that actually works: `cluster_type=\"huggingface\"`, implemented in `clustrix/hf_jobs.py` (`HFJobsManager`). It has been run end to end against real HF Jobs containers. It has nothing to do with HuggingFace *Spaces* (web app hosting) -- that unrelated topic is documented separately as Part 2 below, under its original, unverified banner.\n", - "\n", - "## Why this backend exists\n", - "\n", - "HF Jobs runs a container, executes a command, and exits -- exactly Clustrix's model: hand over a function, run it, collect a result. It needs no cluster reservation, no VPN and no institutional SSH credentials, which is also why it is the substrate this project's own integration tests run against.\n", - "\n", - "## Prerequisites\n", - "\n", - "- `pip install huggingface_hub`\n", - "- An HF token with permission to run Jobs, either exported as `HF_TOKEN`, set in `configure(hf_token=...)`, or already on disk from `hf auth login`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "22ab4948", - "metadata": {}, - "outputs": [], - "source": [ - "from clustrix import configure, cluster\n", - "\n", - "configure(\n", - " cluster_type=\"huggingface\",\n", - " hf_username=\"your-hf-username\", # or hf_namespace= for an org\n", - " # hf_token=..., # optional if HF_TOKEN is set, or `hf auth login` was run\n", - " hf_flavor=\"cpu-basic\", # default; see the GPU section below before changing this\n", - " hf_job_timeout=\"30m\", # default\n", - ")\n", - "\n", - "@cluster(cores=1, memory=\"1GB\")\n", - "def add(a, b):\n", - " return a + b\n", - "\n", - "# result = add(2, 3) # requires a real HF token with Jobs access\n", - "# print(result)" - ] - }, - { - "cell_type": "markdown", - "id": "da5a1fe9", - "metadata": {}, - "source": [ - "## Behind the Scenes: How a Job Actually Runs\n", - "\n", - "In order, from `HFJobsManager.submit_job` and `_bootstrap_source` in `clustrix/hf_jobs.py`:\n", - "\n", - "1. The function, args and kwargs are packed with `dill` and base64-encoded.\n", - "2. **Payload staging.** HF rejects very large environment variables, so the encoded payload is capped at 256KB (`MAX_PAYLOAD_BYTES`). A payload under that limit travels in the `CLUSTRIX_PAYLOAD` env var. A larger one is uploaded to a private HF dataset repo (`/clustrix-payloads` by default, or `hf_payload_repo`), and the job is instead given `CLUSTRIX_PAYLOAD_REPO`/`CLUSTRIX_PAYLOAD_FILE` plus a one-time `CLUSTRIX_HF_TOKEN` **secret** (not an env var) so it can download that one file. The staged file is deleted again once the job finishes, whether it succeeded or failed.\n", - "3. **Bootstrap.** The container runs a single `python -c \"...\"` bootstrap. Its first act is to `os.environ.pop('CLUSTRIX_HMAC_KEY')` -- the per-job signing key is removed from the environment *before* `pip install` runs, because a malicious or merely misbehaving package's own install hooks must not be able to read it. Only then does it `pip install` `dill`, `cloudpickle`, and anything named in `cluster_packages` / mirrored from your local environment (via `replicate_local_environment`, on by default -- the container starts from a bare Python image, so a function that imports `numpy` needs that mirrored or it fails only in the container).\n", - "4. The function is unpickled (`dill`, falling back to `cloudpickle`) and called. Its result -- or, on an exception, the error message, traceback, and (if picklable) the exception object itself -- is `dill`-serialized, HMAC-SHA256'd with the now-popped key, base64-encoded, and printed between marker lines (`---CLUSTRIX-RESULT-BEGIN---`/`...-END---`, or the `ERROR` equivalents).\n", - "5. **This side** polls the job, fetches its logs, and picks the *last* block in the log whose HMAC verifies against the per-job key -- not the first one it finds. A function is free to print anything, including a line that happens to equal a marker; only a verified tag distinguishes the real result from a decoy or from ordinary program output. An unverifiable or absent result raises `RuntimeError` rather than returning the log itself.\n", - "6. A function that raised is re-raised locally as the *original exception type* when it was picklable, with the remote traceback attached to the message. The container itself still exits `0` on a caught exception -- only a failure to *report* the exception is treated as a job failure -- so an ordinary `ValueError` from your function does not also trigger an HF \"job failed\" email to the account owner.\n", - "\n", - "## GPU flavors bill real money\n", - "\n", - "`is_gpu_flavor()` treats anything **not** prefixed `cpu-` as a GPU tier, and `hf_flavor` values like `a10g-small`, `a100-large`, `t4-medium` bill by the second for as long as the job runs. Requesting one without opting in raises `ValueError`:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1ab83632", - "metadata": {}, - "outputs": [], - "source": [ - "# Without the opt-in, this raises ValueError before anything is submitted:\n", - "configure(cluster_type=\"huggingface\", hf_username=\"your-hf-username\", hf_flavor=\"a10g-small\")\n", - "\n", - "@cluster(cores=4, memory=\"16GB\")\n", - "def gpu_check():\n", - " import torch\n", - " return torch.cuda.is_available()\n", - "\n", - "try:\n", - " # gpu_check()\n", - " pass\n", - "except ValueError as e:\n", - " print(e) # \"Flavor 'a10g-small' is a GPU flavor and bills by the second. ...\"\n", - "\n", - "# Confirming you intend to pay for GPU time:\n", - "configure(\n", - " cluster_type=\"huggingface\",\n", - " hf_username=\"your-hf-username\",\n", - " hf_flavor=\"a10g-small\",\n", - " hf_allow_gpu_flavors=True, # required, on purpose -- this is a real-money gate\n", - ")\n", - "# result = gpu_check() # now billed by the second for as long as this job runs" - ] - }, - { - "cell_type": "markdown", - "id": "9a81983c", - "metadata": {}, - "source": [ - "## Summary (Part 1)\n", - "\n", - "- `cluster_type=\"huggingface\"` is a verified backend: it has been run against real HF Jobs containers.\n", - "- Payloads under 256KB travel as an env var; larger ones stage through a private dataset repo, cleaned up afterward.\n", - "- Results are HMAC-verified before being deserialized, using a key that is removed from the environment before your code -- and before `pip install`'s own hooks -- can run.\n", - "- GPU flavors cost real money and require `hf_allow_gpu_flavors=True`; CPU flavors (the default) do not.\n", - "\n", - "---\n", - "\n", - "# Part 2: HuggingFace Spaces -- a different, unrelated, unverified topic\n", - "\n", - "Everything below this point is the *original* content of this notebook. It is about deploying Gradio/Streamlit apps to HuggingFace **Spaces** (a web app hosting product) that happen to `import clustrix` and, in production, would point its SSH backend at a separately-provisioned compute cluster. It does not exercise the HF Jobs backend documented in Part 1 at all, and none of its cluster-execution claims have been verified end to end -- see the warning below, which predates this Part 1/Part 2 split." - ] - }, - { - "cell_type": "markdown", - "id": "4ca46fec", - "metadata": {}, - "source": [ - "## What Part 2 actually was, and the verdict\n", - "\n", - "The ~1400 words that used to fill the rest of this notebook were a\n", - "Gradio/Streamlit \"app template\" walkthrough: HuggingFace Space hardware-tier\n", - "pricing tables, `git clone`/`huggingface_hub` deployment snippets, a\n", - "secrets-management guide, and a troubleshooting FAQ. None of it is a clustrix\n", - "feature. Checking the code confirms it: there is no `clustrix.spaces`\n", - "module, no Space-creation API, no Gradio/Streamlit integration anywhere in\n", - "this package. The only thing \"integrating\" clustrix with a Space was\n", - "`import clustrix` at the top of an `app.py` -- true of any pip package, and\n", - "not something this documentation should present as a supported workflow.\n", - "\n", - "**What is real:** if you host a Gradio or Streamlit app on a HuggingFace\n", - "Space and want *that app* to hand work off to a separate compute cluster,\n", - "`clustrix` is just a normal dependency inside it. Add it to `requirements.txt`,\n", - "`import clustrix`, and use one of the two verified backends from elsewhere\n", - "in these docs -- `configure(cluster_type=\"ssh\", ...)` (see\n", - ":doc:`ssh_tutorial`) if you have a machine to point it at, or\n", - "`configure(cluster_type=\"huggingface\", ...)` (Part 1, above) if you want the\n", - "Space itself to launch HF Jobs. Everything about *how* `@cluster` then\n", - "behaves -- order of operations, config resolution, what can go wrong -- is\n", - "documented once, correctly, in :ref:`execution-model` and :ref:`limitations`;\n", - "repeating a second, unverified copy of it here would only invite drift.\n", - "\n", - "There is nothing else backend-specific to \"HuggingFace Spaces\" for clustrix\n", - "to document." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/kubernetes_tutorial.ipynb b/docs/source/notebooks/kubernetes_tutorial.ipynb deleted file mode 100644 index fe97185f..00000000 --- a/docs/source/notebooks/kubernetes_tutorial.ipynb +++ /dev/null @@ -1,254 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "4e2d71cf", - "metadata": {}, - "source": [ - "> **This backend has never been run against a real Kubernetes cluster.**\n", - ">\n", - "> `clustrix/executor_kubernetes.py` (`KubernetesJobManager`) implements job submission, a signed result contract, and status polling that no longer fabricates success -- but nothing in this project has demonstrated a completed job against a live API server. This notebook describes the documented interface, traced from source, not something that has been run. See `docs/source/tutorials/kubernetes_tutorial.rst` for the fuller version of this tutorial, including the \"Behind the Scenes\" section this notebook summarizes, and the auto-provisioning path (`kind`, or five unverified cloud providers).\n", - ">\n", - "> The backends that are verified working are `cluster_type=\"slurm\"`, `cluster_type=\"ssh\"` and `cluster_type=\"huggingface\"` (HuggingFace Jobs). See the Supported Cluster Types section of the documentation.\n", - ">\n", - "> **This notebook previously documented parameters that do not exist** -- `cpu_limit`, `memory_limit`, `container_image`, `job_name`, `parallelism`, `completions`, `restart_policy` passed to `@cluster(...)`. None of those are read anywhere in the decorator or the Kubernetes executor; passing them either does nothing or (for most of them) triggers a runtime warning that the option is unrecognised. This revision only uses parameters that actually exist in the current code." - ] - }, - { - "cell_type": "markdown", - "id": "bce5d6dc", - "metadata": {}, - "source": [ - "# Kubernetes Tutorial\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/kubernetes_tutorial.ipynb)\n", - "\n", - "This notebook demonstrates the documented interface for running Clustrix jobs on Kubernetes: containerized, no-custom-image execution driven by a `batch/v1` `Job`.\n", - "\n", - "## Prerequisites\n", - "\n", - "- Access to a Kubernetes cluster, and `kubectl` configured with access to it (or `KUBECONFIG` pointed at a working config -- Clustrix calls `kubernetes.config.load_kube_config()` with no arguments, so it uses whatever `kubectl` itself would use; there is no separate Clustrix setting for the kubeconfig path)\n", - "- Clustrix installed with Kubernetes support: `pip install clustrix[kubernetes]`" - ] - }, - { - "cell_type": "markdown", - "id": "36e1ff91", - "metadata": {}, - "source": [ - "## Behind the Scenes: What `@cluster(...)` Actually Does Here\n", - "\n", - "In order, from `submit_k8s_job`, `build_worker_program`, and `decode_signed_result`:\n", - "\n", - "1. The function, args and kwargs are serialized with `cloudpickle` and base64-encoded.\n", - "2. A fresh random 32-byte hex key is generated for **this job only** and passed into the container as the env var `CLUSTRIX_RESULT_KEY` -- never on the command line, where any user on a shared node could read it out of `/proc`.\n", - "3. A `Job` manifest is submitted with one container (`python:3.11-slim` by default, or your configured `k8s_image`) running `pip install cloudpickle dill --quiet && python -c \"\"`. There is no custom image build step.\n", - "4. The worker program calls the function, serializes the result with `dill`, computes an HMAC-SHA256 over those exact bytes keyed by `CLUSTRIX_RESULT_KEY`, and prints `CLUSTRIX_RESULT_B64:<...>` and `CLUSTRIX_RESULT_HMAC:<...>` to stdout.\n", - "5. Job status is read from the Kubernetes API's own `job.status.succeeded` / `.failed` / `.active` fields. If the status call itself fails, that raises -- it used to report `\"completed\"` on any such error, which reported evicted or inaccessible jobs as successful.\n", - "6. On success, the pod log is read, the HMAC is recomputed and compared with `hmac.compare_digest`, and only a verified payload is passed to `dill.loads`. An unsigned, missing, or mismatched result raises `RuntimeError` and is never deserialized -- unpickling is code execution, so a pod log is not trusted on sight. This replaced an `ast.literal_eval` on `repr(result)`, which silently turned any object without a literal repr (a NumPy array, a dataclass) into the string of its own repr.\n", - "7. On failure, the manager looks for `CLUSTRIX_ERROR:`/`CLUSTRIX_TRACEBACK:` lines in the pod log and raises a `RuntimeError` carrying them.\n", - "\n", - "**Only `cores` and `memory` are read from the per-job `@cluster(...)` call** -- they become the pod's resource `requests` and `limits` (set to the same values). `k8s_namespace`, `k8s_image`, `k8s_service_account` and `k8s_pull_policy` are accepted as `@cluster(...)` keyword arguments without a warning, but `submit_k8s_job` never reads them back out of the per-job config -- only `configure()`-level `k8s_namespace`/`k8s_image` take effect. `time`, similarly, is accepted but not applied to the Kubernetes job (no `activeDeadlineSeconds` is set from it)." - ] - }, - { - "cell_type": "markdown", - "id": "103e4b03", - "metadata": {}, - "source": [ - "## Configuration\n", - "\n", - "Only fields that exist on `ClusterConfig` are used below." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b721daef", - "metadata": {}, - "outputs": [], - "source": [ - "from clustrix import configure, cluster\n", - "\n", - "configure(\n", - " cluster_type=\"kubernetes\",\n", - " k8s_namespace=\"default\", # real config field; configure()-level only\n", - " k8s_image=\"python:3.11-slim\", # real config field; configure()-level only\n", - " k8s_service_account=None, # optional\n", - " k8s_pull_policy=\"IfNotPresent\", # real config field\n", - " k8s_job_ttl_seconds=3600, # Job auto-deleted this long after finishing\n", - " k8s_backoff_limit=3, # retries before the Job gives up\n", - " default_cores=2,\n", - " default_memory=\"4Gi\", # Kubernetes format; \"4GB\" is also accepted\n", - " # and normalized for you (normalize_memory)\n", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "adbb661c", - "metadata": {}, - "source": [ - "## Example: A Simple Job\n", - "\n", - "Only `cores` and `memory` affect the pod's resources; both are optional and fall back to `default_cores`/`default_memory` above." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "816f7529", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=2, memory=\"4Gi\")\n", - "def matrix_multiply(size=500):\n", - " \"\"\"Runs inside the pod's container, not on this machine.\"\"\"\n", - " import numpy as np\n", - "\n", - " a = np.random.rand(size, size)\n", - " b = np.random.rand(size, size)\n", - " result = a @ b\n", - " return {\n", - " \"shape\": result.shape,\n", - " \"trace\": float(np.trace(result)),\n", - " }\n", - "\n", - "# Requires a real, reachable Kubernetes cluster -- see the prerequisites above.\n", - "# result = matrix_multiply(500)\n", - "# print(result)" - ] - }, - { - "cell_type": "markdown", - "id": "7dc09707", - "metadata": {}, - "source": [ - "## Example: Fractional Cores\n", - "\n", - "Kubernetes accepts fractional CPU requests; Clustrix passes `cores` straight through as the pod's CPU request/limit, so `cores=0.5` becomes `\"0.5\"`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bf8583e1", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=0.5, memory=\"512Mi\")\n", - "def lightweight_task(n):\n", - " return sum(i * i for i in range(n))\n", - "\n", - "# result = lightweight_task(1000)" - ] - }, - { - "cell_type": "markdown", - "id": "bbbc6944", - "metadata": {}, - "source": [ - "## Custom Images\n", - "\n", - "Set `k8s_image` via `configure()` (or in the `ClusterConfig` you construct), not on `@cluster(...)` -- see the warning above." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c7b3a34d", - "metadata": {}, - "outputs": [], - "source": [ - "configure(\n", - " cluster_type=\"kubernetes\",\n", - " k8s_namespace=\"ml-compute\",\n", - " k8s_image=\"python:3.11\", # any image with a Python interpreter; the\n", - " # worker program itself only needs cloudpickle\n", - " # and dill, which the container command installs\n", - ")\n", - "\n", - "@cluster(cores=4, memory=\"8Gi\")\n", - "def train_stub():\n", - " import torch\n", - " return {\"cuda_available\": torch.cuda.is_available()}" - ] - }, - { - "cell_type": "markdown", - "id": "5b4ca614", - "metadata": {}, - "source": [ - "## Auto-Provisioning a Cluster\n", - "\n", - "If you don't have a cluster, `clustrix.kubernetes.cluster_provisioner` can create one -- locally with [kind](https://kind.sigs.k8s.io/) (no cloud credentials needed), or on one of five cloud providers (**unverified**, and requires real credentials). See the `Auto-Provisioning a Cluster` section of `docs/source/tutorials/kubernetes_tutorial.rst` for the full explanation, including why `provider=\"local\"` on `@cluster(...)` does *not* select the local Kubernetes provisioner (that's `config.k8s_provider`, set via `configure()`)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3d66fccf", - "metadata": {}, - "outputs": [], - "source": [ - "# cluster-required: provisions a real kind cluster via Docker\n", - "configure(\n", - " cluster_type=\"kubernetes\",\n", - " auto_provision_k8s=True,\n", - " k8s_provider=\"local\", # LocalDockerKubernetesProvisioner; needs Docker + kind + kubectl\n", - " k8s_node_count=2,\n", - ")\n", - "\n", - "@cluster(platform=\"kubernetes\", auto_provision=True, cores=1, memory=\"512Mi\")\n", - "def analyze(x):\n", - " return x * 2\n", - "\n", - "# result = analyze(21) # provisions (or reuses) the kind cluster, then runs the job" - ] - }, - { - "cell_type": "markdown", - "id": "2c38be5b", - "metadata": {}, - "source": [ - "## What Failure Looks Like\n", - "\n", - "If the function raises, the pod's log carries `CLUSTRIX_ERROR:`/`CLUSTRIX_TRACEBACK:` lines, and `wait_for_k8s_result` re-raises a `RuntimeError` built from them -- it does not swallow the failure or return a partial result. If the Kubernetes API itself cannot be reached, or the job's status cannot be determined, that also raises rather than reporting `\"completed\"` (see `check_k8s_job_status` in `executor_kubernetes.py`); this was a real, fixed bug, not a hypothetical one." - ] - }, - { - "cell_type": "markdown", - "id": "87f75d4a", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "- Only `cores` and `memory` are applied per job; `k8s_namespace`/`k8s_image`/`k8s_service_account`/`k8s_pull_policy` must be set via `configure()`.\n", - "- Results are signed (HMAC-SHA256, per-job random key) and verified before deserialization; unverifiable results raise rather than returning garbage.\n", - "- Job status comes from the Kubernetes API's own fields; a status that cannot be read is an error, never a silent \"completed\".\n", - "- None of this has been run against a real cluster in this project. Treat it as a documented interface to verify yourself, not a demonstrated one." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/lambda_cloud_tutorial.ipynb b/docs/source/notebooks/lambda_cloud_tutorial.ipynb deleted file mode 100644 index 8ad39437..00000000 --- a/docs/source/notebooks/lambda_cloud_tutorial.ipynb +++ /dev/null @@ -1,1910 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "a0ed3929", - "metadata": {}, - "source": [ - "> **These backends are unverified.**\n", - ">\n", - "> No clustrix cloud VM job (`provider=\"aws\"`, `\"gcp\"`, `\"azure\"`, `\"lambda\"`, `\"huggingface\"`) has been shown to run end to end. Until recently the path could not have run at all: every cloud job died with a `KeyError` on its first line. That was fixed (issue #119), but nothing has since demonstrated a completed cloud job, and `scripts/collect_execution_evidence.py` does not cover these backends. This notebook describes the intended interface, not something that has been run.\n", - ">\n", - "> The backends that are verified working are `cluster_type=\"slurm\"`, `cluster_type=\"ssh\"` and `cluster_type=\"huggingface\"` (HuggingFace Jobs, which is a different thing from the HuggingFace Spaces provider described here). See the Supported Cluster Types section of the documentation.\n" - ] - }, - { - "cell_type": "markdown", - "id": "9bdb524c", - "metadata": {}, - "source": [ - "> **What actually happens if you try `@cluster(provider=\"lambda\", ...)`.**\n", - ">\n", - "> `LambdaCloudProvider` is the *only* built-in cloud provider whose class implements `create_instance()` -- `CloudJobManager._check_provider_can_run_jobs` (in `clustrix/executor_cloud.py`) checks every provider for this method at submit time, and AWS/Azure/GCP all fail that check and raise `NotImplementedError` naming the provider before anything is created. Lambda passes it: the code path (create the instance, poll `get_cluster_status` until `\"active\"`, read `get_cluster_config()` for SSH details, then run the job exactly like any other SSH host) is real and complete.\n", - ">\n", - "> That does **not** mean it has been run. Nothing in this project has demonstrated a `@cluster(provider=\"lambda\", ...)` job completing end to end against a live Lambda Cloud account -- `scripts/collect_execution_evidence.py` does not cover it, and this notebook's examples below use the same manual-provision-then-SSH pattern as the other cloud tutorials rather than this auto-provisioning path, so they don't exercise it either.\n", - ">\n", - "> One more thing that used to be silently wrong and is now an explicit error: if a Lambda Cloud API response can't be parsed into real connection details, `get_cluster_config()` used to return a fake `placeholder.lambdalabs.com` hostname, which nothing downstream could tell apart from a real one -- the failure then surfaced as an SSH connection error against a domain that does not exist, far from its actual cause. It now raises `RuntimeError` naming the instance instead." - ] - }, - { - "cell_type": "markdown", - "id": "6af13259", - "metadata": {}, - "source": [ - "**Behind the scenes, once you're actually calling `@cluster`:** every\n", - "example below that runs (as opposed to just printing setup commands) ends up\n", - "configured with `cluster_type=\"ssh\"` against a VM you provisioned yourself --\n", - "that is the verified SSH backend, following the same order of operations as\n", - "any other SSH cluster in these docs: read `get_config()`, merge decorator\n", - "args over config defaults, choose local vs. remote (`cluster_host` is set,\n", - "so remote), serialize the function with `dill`/`cloudpickle`, create the\n", - "remote job directory, upload the payload over SFTP, build the remote venv,\n", - "generate and run a job script, poll for completion, then download and\n", - "HMAC-verify `result.pkl`. None of that is Lambda Cloud-specific -- clustrix\n", - "does not talk to the Lambda Cloud API at any point in that path; Lambda Cloud\n", - "only matters for how the VM itself got created, which is everything *before*\n", - "`configure(cluster_type=\"ssh\", ...)` in this notebook. Full, source-verified\n", - "detail: :ref:`execution-model`. Every `ClusterConfig` field used below is\n", - "documented in :ref:`configuration`.\n", - "\n", - "**Real resources, real charges.** The functions and CLI snippets below that\n", - "create VMs, networks, security groups, or managed clusters call real\n", - "Lambda Cloud APIs (or print commands meant to be copy-pasted into a real\n", - "Lambda Cloud CLI). None of them run automatically in this notebook -- every\n", - "invocation is commented out -- but if you uncomment one, or copy a printed\n", - "command into your terminal, it creates billed resources in your account.\n", - "Read each cell before running or copying it, and see the cleanup cell near\n", - "the end before you walk away." - ] - }, - { - "cell_type": "markdown", - "id": "lambda-title", - "metadata": {}, - "source": [ - "# Lambda Cloud Tutorial\n", - "\n", - "This tutorial demonstrates how to use Clustrix with Lambda Cloud for high-performance GPU computing and distributed machine learning.\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/lambda_cloud_tutorial.ipynb)\n", - "\n", - "## Overview\n", - "\n", - "Lambda Cloud specializes in GPU cloud computing and integrates well with Clustrix for ML workloads:\n", - "\n", - "- **GPU-Optimized Instances**: High-performance NVIDIA GPUs (A100, H100, RTX)\n", - "- **Cost-Effective**: Competitive pricing for GPU computing\n", - "- **Simple Management**: Easy instance launching and management\n", - "- **Pre-configured Environments**: ML-ready software stacks\n", - "- **High-Speed Networking**: InfiniBand for multi-GPU communications\n", - "- **Persistent Storage**: Fast NVMe and network storage options\n", - "- **SSH Access**: Direct access for Clustrix integration\n", - "- **On-Demand and Reserved**: Flexible pricing models\n", - "\n", - "## Prerequisites\n", - "\n", - "1. Lambda Cloud account with GPU credits\n", - "2. SSH key pair for instance access\n", - "3. Lambda Cloud API key (optional)\n", - "4. Basic understanding of GPU computing" - ] - }, - { - "cell_type": "markdown", - "id": "installation", - "metadata": {}, - "source": [ - "## Installation and Setup\n", - "\n", - "Install Clustrix with Lambda Cloud dependencies:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "install", - "metadata": {}, - "outputs": [], - "source": [ - "# Install Clustrix with GPU and Lambda Cloud support\n", - "!pip install clustrix torch torchvision transformers datasets accelerate\n", - "\n", - "# Import required libraries\n", - "import clustrix\n", - "from clustrix import cluster, configure\n", - "import torch\n", - "import numpy as np\n", - "import time\n", - "import json\n", - "import requests\n", - "import os" - ] - }, - { - "cell_type": "markdown", - "id": "lambda-setup", - "metadata": {}, - "source": [ - "## Lambda Cloud Authentication and Setup\n", - "\n", - "### Option 1: Web Console Setup" - ] - }, - { - "cell_type": "markdown", - "id": "web-setup", - "metadata": {}, - "source": [ - "### Lambda Cloud Web Console Setup\n", - "\n", - "1. **Create Account:**\n", - " - Visit https://lambdalabs.com/service/gpu-cloud\n", - " - Sign up and verify your account\n", - " - Add billing information and credits\n", - "\n", - "2. **Add SSH Key:**\n", - " - Go to https://cloud.lambdalabs.com/ssh-keys\n", - " - Click \"Add SSH Key\"\n", - " - Paste your public key (cat ~/.ssh/id_rsa.pub)\n", - " - Give it a descriptive name\n", - "\n", - "3. **Launch Instance:**\n", - " - Go to https://cloud.lambdalabs.com/instances\n", - " - Click \"Launch instance\"\n", - " - Select instance type (A100, H100, RTX 6000 Ada, etc.)\n", - " - Choose region (closest to you for best performance)\n", - " - Select your SSH key\n", - " - Launch the instance\n", - "\n", - "4. **Instance Types Available:**\n", - " - RTX 6000 Ada: 48GB VRAM, ~$0.75/hour\n", - " - A10: 24GB VRAM, ~$0.60/hour \n", - " - A100 (40GB): 40GB VRAM, ~$1.10/hour\n", - " - A100 (80GB): 80GB VRAM, ~$1.40/hour\n", - " - H100: 80GB VRAM, ~$2.50/hour (when available)\n", - "\n", - "5. **Access Instance:**\n", - " - Wait for instance to be \"Running\"\n", - " - Note the public IP address\n", - " - SSH: ssh ubuntu@" - ] - }, - { - "cell_type": "markdown", - "id": "a5k1lpava3n", - "metadata": {}, - "source": [ - "**Follow this guide to set up your Lambda Cloud account and launch your first GPU instance.**" - ] - }, - { - "cell_type": "markdown", - "id": "lambda-api", - "metadata": {}, - "source": [ - "### Option 2: API-Based Setup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "api-setup", - "metadata": {}, - "outputs": [], - "source": [ - "import requests\n", - "import os\n", - "\n", - "class LambdaCloudAPI:\n", - " def __init__(self, api_key=None):\n", - " self.api_key = api_key or os.getenv('LAMBDA_API_KEY')\n", - " self.base_url = 'https://cloud.lambdalabs.com/api/v1'\n", - " self.headers = {\n", - " 'Authorization': f'Bearer {self.api_key}',\n", - " 'Content-Type': 'application/json'\n", - " }\n", - " \n", - " def list_instance_types(self):\n", - " \"\"\"List available instance types.\"\"\"\n", - " response = requests.get(f'{self.base_url}/instance-types', headers=self.headers)\n", - " return response.json()\n", - " \n", - " def list_instances(self):\n", - " \"\"\"List running instances.\"\"\"\n", - " response = requests.get(f'{self.base_url}/instances', headers=self.headers)\n", - " return response.json()\n", - " \n", - " def launch_instance(self, instance_type, ssh_key_name, region='us-east-1', name=None):\n", - " \"\"\"Launch a new instance.\"\"\"\n", - " data = {\n", - " 'instance_type_name': instance_type,\n", - " 'ssh_key_names': [ssh_key_name],\n", - " 'region_name': region\n", - " }\n", - " if name:\n", - " data['name'] = name\n", - " \n", - " response = requests.post(f'{self.base_url}/instance-operations/launch', \n", - " headers=self.headers, json=data)\n", - " return response.json()\n", - " \n", - " def terminate_instance(self, instance_id):\n", - " \"\"\"Terminate an instance.\"\"\"\n", - " data = {'instance_ids': [instance_id]}\n", - " response = requests.post(f'{self.base_url}/instance-operations/terminate',\n", - " headers=self.headers, json=data)\n", - " return response.json()\n", - " \n", - " def get_instance_details(self, instance_id):\n", - " \"\"\"Get detailed information about an instance.\"\"\"\n", - " instances = self.list_instances()\n", - " for instance in instances.get('data', []):\n", - " if instance['id'] == instance_id:\n", - " return instance\n", - " return None\n", - "\n", - "# Example usage:\n", - "# api = LambdaCloudAPI()\n", - "# instance_types = api.list_instance_types()\n", - "# print(json.dumps(instance_types, indent=2))" - ] - }, - { - "cell_type": "markdown", - "id": "1fgfjnypmvp", - "metadata": {}, - "source": [ - "### Lambda Cloud API Setup Guide\n", - "\n", - "#### CLI Setup Steps\n", - "\n", - "1. **Get API Key:**\n", - " - Go to https://cloud.lambdalabs.com/api-keys\n", - " - Generate a new API key\n", - " - Set as environment variable: `export LAMBDA_API_KEY=\"your-key\"`\n", - "\n", - "2. **Install Lambda Cloud CLI:**\n", - " ```bash\n", - " pip install lambda-cloud\n", - " lambda-cloud configure # Enter your API key\n", - " ```\n", - "\n", - "3. **Basic CLI Commands:**\n", - " ```bash\n", - " # List available instance types\n", - " lambda-cloud instance-types list\n", - " \n", - " # List available regions\n", - " lambda-cloud regions list\n", - " \n", - " # Launch instance\n", - " lambda-cloud instance launch \\\n", - " --instance-type a100 \\\n", - " --ssh-key-name your-key-name \\\n", - " --region us-east-1\n", - " \n", - " # List running instances\n", - " lambda-cloud instance list\n", - " \n", - " # Terminate instance\n", - " lambda-cloud instance terminate \n", - " ```\n", - "\n", - "#### Python API Client" - ] - }, - { - "cell_type": "markdown", - "id": "clustrix-lambda-config", - "metadata": {}, - "source": [ - "## Configure Clustrix for Lambda Cloud" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "config-lambda", - "metadata": {}, - "outputs": [], - "source": [ - "# Configure Clustrix to use your Lambda Cloud instance\n", - "configure(\n", - " cluster_type=\"ssh\",\n", - " cluster_host=\"your-lambda-instance-ip\", # Replace with actual IP\n", - " username=\"ubuntu\", # Default Lambda Cloud user\n", - " key_file=\"~/.ssh/id_rsa\", # Your private SSH key\n", - " remote_work_dir=\"~/.clustrix/jobs\",\n", - " package_manager=\"auto\", # Will use uv if available\n", - " default_cores=8, # Lambda instances typically have 8+ cores\n", - " default_memory=\"32GB\", # Generous memory allocation\n", - " default_time=\"02:00:00\", # Longer timeout for GPU tasks\n", - " environment_variables={\n", - " \"CUDA_VISIBLE_DEVICES\": \"0\", # Use first GPU\n", - " \"NVIDIA_VISIBLE_DEVICES\": \"all\"\n", - " }\n", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "mm5s72ijgws", - "metadata": {}, - "source": [ - "**Replace `your-lambda-instance-ip` with the actual IP address from your Lambda Cloud instance.**" - ] - }, - { - "cell_type": "markdown", - "id": "gpu-verification", - "metadata": {}, - "source": [ - "### GPU Verification and Setup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "verify-gpu", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=2, memory=\"8GB\")\n", - "def verify_lambda_gpu_setup():\n", - " \"\"\"Verify GPU availability and setup on Lambda Cloud instance.\"\"\"\n", - " import torch\n", - " import subprocess\n", - " import platform\n", - " \n", - " # System information\n", - " system_info = {\n", - " 'platform': platform.platform(),\n", - " 'python_version': platform.python_version(),\n", - " 'architecture': platform.architecture()[0]\n", - " }\n", - " \n", - " # PyTorch and CUDA info\n", - " torch_info = {\n", - " 'pytorch_version': torch.__version__,\n", - " 'cuda_available': torch.cuda.is_available(),\n", - " 'cuda_version': torch.version.cuda if torch.cuda.is_available() else None,\n", - " 'cudnn_version': torch.backends.cudnn.version() if torch.cuda.is_available() else None,\n", - " 'device_count': torch.cuda.device_count() if torch.cuda.is_available() else 0\n", - " }\n", - " \n", - " # GPU details\n", - " gpu_info = []\n", - " if torch.cuda.is_available():\n", - " for i in range(torch.cuda.device_count()):\n", - " props = torch.cuda.get_device_properties(i)\n", - " gpu_info.append({\n", - " 'device_id': i,\n", - " 'name': props.name,\n", - " 'total_memory_gb': props.total_memory / (1024**3),\n", - " 'major': props.major,\n", - " 'minor': props.minor,\n", - " 'multiprocessor_count': props.multi_processor_count\n", - " })\n", - " \n", - " # NVIDIA-SMI output\n", - " nvidia_smi = None\n", - " try:\n", - " result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)\n", - " if result.returncode == 0:\n", - " nvidia_smi = result.stdout\n", - " except FileNotFoundError:\n", - " nvidia_smi = \"nvidia-smi not found\"\n", - " \n", - " # Test GPU computation\n", - " gpu_test_result = None\n", - " if torch.cuda.is_available():\n", - " try:\n", - " # Simple GPU computation test\n", - " device = torch.device('cuda:0')\n", - " x = torch.randn(1000, 1000, device=device)\n", - " y = torch.randn(1000, 1000, device=device)\n", - " \n", - " start_time = torch.cuda.Event(enable_timing=True)\n", - " end_time = torch.cuda.Event(enable_timing=True)\n", - " \n", - " start_time.record()\n", - " z = torch.mm(x, y)\n", - " torch.cuda.synchronize()\n", - " end_time.record()\n", - " torch.cuda.synchronize()\n", - " \n", - " gpu_test_result = {\n", - " 'test_passed': True,\n", - " 'computation_time_ms': start_time.elapsed_time(end_time),\n", - " 'result_shape': z.shape,\n", - " 'memory_allocated_mb': torch.cuda.memory_allocated() / (1024**2),\n", - " 'memory_reserved_mb': torch.cuda.memory_reserved() / (1024**2)\n", - " }\n", - " except Exception as e:\n", - " gpu_test_result = {\n", - " 'test_passed': False,\n", - " 'error': str(e)\n", - " }\n", - " \n", - " return {\n", - " 'system_info': system_info,\n", - " 'torch_info': torch_info,\n", - " 'gpu_info': gpu_info,\n", - " 'nvidia_smi': nvidia_smi,\n", - " 'gpu_test': gpu_test_result\n", - " }\n", - "\n", - "# Run GPU verification\n", - "# gpu_status = verify_lambda_gpu_setup()\n", - "# print(json.dumps(gpu_status, indent=2, default=str))\n", - "print(\"GPU verification function defined. Uncomment the lines above to run on Lambda Cloud.\")" - ] - }, - { - "cell_type": "markdown", - "id": "ml-training-example", - "metadata": {}, - "source": [ - "## Example 1: Distributed Deep Learning Training" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dl-training", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=8, memory=\"16GB\", time=\"01:30:00\")\n", - "def lambda_deep_learning_training(model_config, training_config):\n", - " \"\"\"Train a deep learning model on Lambda Cloud GPU.\"\"\"\n", - " import torch\n", - " import torch.nn as nn\n", - " import torch.optim as optim\n", - " from torch.utils.data import DataLoader, TensorDataset\n", - " import numpy as np\n", - " import time\n", - " \n", - " # Set device\n", - " device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", - " print(f\"Training on device: {device}\")\n", - " \n", - " # Create synthetic dataset\n", - " n_samples = training_config['n_samples']\n", - " n_features = training_config['n_features']\n", - " n_classes = training_config['n_classes']\n", - " \n", - " # Generate random data\n", - " X = torch.randn(n_samples, n_features)\n", - " y = torch.randint(0, n_classes, (n_samples,))\n", - " \n", - " # Create dataset and dataloader\n", - " dataset = TensorDataset(X, y)\n", - " dataloader = DataLoader(\n", - " dataset, \n", - " batch_size=training_config['batch_size'], \n", - " shuffle=True\n", - " )\n", - " \n", - " # Define model architecture\n", - " class DeepNet(nn.Module):\n", - " def __init__(self, input_size, hidden_sizes, output_size, dropout=0.2):\n", - " super(DeepNet, self).__init__()\n", - " \n", - " layers = []\n", - " prev_size = input_size\n", - " \n", - " for hidden_size in hidden_sizes:\n", - " layers.extend([\n", - " nn.Linear(prev_size, hidden_size),\n", - " nn.ReLU(),\n", - " nn.BatchNorm1d(hidden_size),\n", - " nn.Dropout(dropout)\n", - " ])\n", - " prev_size = hidden_size\n", - " \n", - " layers.append(nn.Linear(prev_size, output_size))\n", - " self.network = nn.Sequential(*layers)\n", - " \n", - " def forward(self, x):\n", - " return self.network(x)\n", - " \n", - " # Create model\n", - " model = DeepNet(\n", - " input_size=n_features,\n", - " hidden_sizes=model_config['hidden_sizes'],\n", - " output_size=n_classes,\n", - " dropout=model_config.get('dropout', 0.2)\n", - " ).to(device)\n", - " \n", - " # Loss and optimizer\n", - " criterion = nn.CrossEntropyLoss()\n", - " optimizer = optim.Adam(\n", - " model.parameters(), \n", - " lr=training_config['learning_rate'],\n", - " weight_decay=training_config.get('weight_decay', 1e-4)\n", - " )\n", - " \n", - " # Training loop\n", - " model.train()\n", - " training_start = time.time()\n", - " \n", - " epoch_losses = []\n", - " epoch_accuracies = []\n", - " \n", - " for epoch in range(training_config['epochs']):\n", - " epoch_loss = 0.0\n", - " correct = 0\n", - " total = 0\n", - " \n", - " for batch_idx, (data, target) in enumerate(dataloader):\n", - " data, target = data.to(device), target.to(device)\n", - " \n", - " optimizer.zero_grad()\n", - " output = model(data)\n", - " loss = criterion(output, target)\n", - " loss.backward()\n", - " optimizer.step()\n", - " \n", - " epoch_loss += loss.item()\n", - " _, predicted = torch.max(output.data, 1)\n", - " total += target.size(0)\n", - " correct += (predicted == target).sum().item()\n", - " \n", - " avg_loss = epoch_loss / len(dataloader)\n", - " accuracy = 100.0 * correct / total\n", - " \n", - " epoch_losses.append(avg_loss)\n", - " epoch_accuracies.append(accuracy)\n", - " \n", - " if epoch % 10 == 0 or epoch == training_config['epochs'] - 1:\n", - " print(f'Epoch {epoch+1}/{training_config[\"epochs\"]}: '\n", - " f'Loss: {avg_loss:.4f}, Accuracy: {accuracy:.2f}%')\n", - " \n", - " training_time = time.time() - training_start\n", - " \n", - " # Model evaluation\n", - " model.eval()\n", - " with torch.no_grad():\n", - " test_data = torch.randn(1000, n_features).to(device)\n", - " test_output = model(test_data)\n", - " test_predictions = torch.max(test_output, 1)[1]\n", - " \n", - " # Memory usage\n", - " memory_info = {}\n", - " if torch.cuda.is_available():\n", - " memory_info = {\n", - " 'allocated_mb': torch.cuda.memory_allocated() / (1024**2),\n", - " 'reserved_mb': torch.cuda.memory_reserved() / (1024**2),\n", - " 'max_allocated_mb': torch.cuda.max_memory_allocated() / (1024**2)\n", - " }\n", - " \n", - " return {\n", - " 'training_completed': True,\n", - " 'device_used': str(device),\n", - " 'model_parameters': sum(p.numel() for p in model.parameters()),\n", - " 'trainable_parameters': sum(p.numel() for p in model.parameters() if p.requires_grad),\n", - " 'training_time': training_time,\n", - " 'final_loss': epoch_losses[-1],\n", - " 'final_accuracy': epoch_accuracies[-1],\n", - " 'best_accuracy': max(epoch_accuracies),\n", - " 'epoch_losses': epoch_losses,\n", - " 'epoch_accuracies': epoch_accuracies,\n", - " 'memory_info': memory_info,\n", - " 'model_architecture': str(model)\n", - " }\n", - "\n", - "# Example configuration\n", - "model_config = {\n", - " 'hidden_sizes': [512, 256, 128, 64],\n", - " 'dropout': 0.3\n", - "}\n", - "\n", - "training_config = {\n", - " 'n_samples': 10000,\n", - " 'n_features': 100,\n", - " 'n_classes': 10,\n", - " 'batch_size': 64,\n", - " 'epochs': 50,\n", - " 'learning_rate': 0.001,\n", - " 'weight_decay': 1e-4\n", - "}\n", - "\n", - "# Run training\n", - "# result = lambda_deep_learning_training(model_config, training_config)\n", - "# print(f\"Training completed! Final accuracy: {result['final_accuracy']:.2f}%\")\n", - "# print(f\"Training time: {result['training_time']:.2f} seconds\")\n", - "# print(f\"GPU memory used: {result['memory_info'].get('max_allocated_mb', 0):.1f} MB\")\n", - "\n", - "print(\"Deep learning training function defined. Uncomment the lines above to run on Lambda Cloud.\")" - ] - }, - { - "cell_type": "markdown", - "id": "transformer-example", - "metadata": {}, - "source": [ - "## Example 2: Transformer Model Fine-tuning" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "transformer-finetuning", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=8, memory=\"32GB\", time=\"02:00:00\")\n", - "def lambda_transformer_finetuning(model_name, training_params):\n", - " \"\"\"Fine-tune a transformer model on Lambda Cloud GPU.\"\"\"\n", - " import torch\n", - " from transformers import (\n", - " AutoTokenizer, AutoModelForSequenceClassification,\n", - " TrainingArguments, Trainer, DataCollatorWithPadding\n", - " )\n", - " from datasets import Dataset\n", - " import numpy as np\n", - " import time\n", - " \n", - " # Set device\n", - " device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", - " print(f\"Fine-tuning on device: {device}\")\n", - " \n", - " # Load tokenizer and model\n", - " tokenizer = AutoTokenizer.from_pretrained(model_name)\n", - " model = AutoModelForSequenceClassification.from_pretrained(\n", - " model_name,\n", - " num_labels=training_params['num_labels']\n", - " )\n", - " \n", - " if tokenizer.pad_token is None:\n", - " tokenizer.pad_token = tokenizer.eos_token\n", - " \n", - " # Create synthetic dataset\n", - " def generate_synthetic_text_data(n_samples, num_labels):\n", - " \"\"\"Generate synthetic text classification data.\"\"\"\n", - " \n", - " # Simple text templates for different classes\n", - " templates = {\n", - " 0: [\"This is a positive example about {}\", \"Great work on {}\", \"Excellent {}\"],\n", - " 1: [\"This is a negative example about {}\", \"Poor {}\", \"Terrible {}\"],\n", - " 2: [\"This is a neutral example about {}\", \"Average {}\", \"Okay {}\"] if num_labels > 2 else []\n", - " }\n", - " \n", - " topics = [\"technology\", \"sports\", \"food\", \"movies\", \"music\", \"books\", \"travel\", \"science\"]\n", - " \n", - " texts = []\n", - " labels = []\n", - " \n", - " for _ in range(n_samples):\n", - " label = np.random.randint(0, num_labels)\n", - " template = np.random.choice(templates[label])\n", - " topic = np.random.choice(topics)\n", - " text = template.format(topic)\n", - " \n", - " texts.append(text)\n", - " labels.append(label)\n", - " \n", - " return texts, labels\n", - " \n", - " # Generate data\n", - " train_texts, train_labels = generate_synthetic_text_data(\n", - " training_params['train_samples'], training_params['num_labels']\n", - " )\n", - " eval_texts, eval_labels = generate_synthetic_text_data(\n", - " training_params['eval_samples'], training_params['num_labels']\n", - " )\n", - " \n", - " # Tokenize data\n", - " def tokenize_function(examples):\n", - " return tokenizer(\n", - " examples['text'],\n", - " truncation=True,\n", - " padding=True,\n", - " max_length=training_params.get('max_length', 512)\n", - " )\n", - " \n", - " # Create datasets\n", - " train_dataset = Dataset.from_dict({'text': train_texts, 'labels': train_labels})\n", - " eval_dataset = Dataset.from_dict({'text': eval_texts, 'labels': eval_labels})\n", - " \n", - " train_dataset = train_dataset.map(tokenize_function, batched=True)\n", - " eval_dataset = eval_dataset.map(tokenize_function, batched=True)\n", - " \n", - " # Data collator\n", - " data_collator = DataCollatorWithPadding(tokenizer=tokenizer)\n", - " \n", - " # Training arguments\n", - " training_args = TrainingArguments(\n", - " output_dir='/tmp/results',\n", - " num_train_epochs=training_params.get('epochs', 3),\n", - " per_device_train_batch_size=training_params.get('batch_size', 8),\n", - " per_device_eval_batch_size=training_params.get('eval_batch_size', 8),\n", - " warmup_steps=training_params.get('warmup_steps', 100),\n", - " weight_decay=training_params.get('weight_decay', 0.01),\n", - " learning_rate=training_params.get('learning_rate', 2e-5),\n", - " logging_dir='/tmp/logs',\n", - " logging_steps=10,\n", - " evaluation_strategy=\"epoch\",\n", - " save_strategy=\"epoch\",\n", - " load_best_model_at_end=True,\n", - " metric_for_best_model=\"eval_loss\",\n", - " greater_is_better=False,\n", - " fp16=torch.cuda.is_available(), # Use mixed precision if GPU available\n", - " dataloader_pin_memory=torch.cuda.is_available(),\n", - " remove_unused_columns=False\n", - " )\n", - " \n", - " # Define compute metrics\n", - " def compute_metrics(eval_pred):\n", - " predictions, labels = eval_pred\n", - " predictions = np.argmax(predictions, axis=1)\n", - " accuracy = (predictions == labels).mean()\n", - " return {'accuracy': accuracy}\n", - " \n", - " # Create trainer\n", - " trainer = Trainer(\n", - " model=model,\n", - " args=training_args,\n", - " train_dataset=train_dataset,\n", - " eval_dataset=eval_dataset,\n", - " tokenizer=tokenizer,\n", - " data_collator=data_collator,\n", - " compute_metrics=compute_metrics\n", - " )\n", - " \n", - " # Training\n", - " start_time = time.time()\n", - " train_result = trainer.train()\n", - " training_time = time.time() - start_time\n", - " \n", - " # Final evaluation\n", - " eval_result = trainer.evaluate()\n", - " \n", - " # Memory usage\n", - " memory_info = {}\n", - " if torch.cuda.is_available():\n", - " memory_info = {\n", - " 'allocated_mb': torch.cuda.memory_allocated() / (1024**2),\n", - " 'reserved_mb': torch.cuda.memory_reserved() / (1024**2),\n", - " 'max_allocated_mb': torch.cuda.max_memory_allocated() / (1024**2)\n", - " }\n", - " \n", - " # Model info\n", - " total_params = sum(p.numel() for p in model.parameters())\n", - " trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)\n", - " \n", - " return {\n", - " 'model_name': model_name,\n", - " 'device_used': str(device),\n", - " 'training_completed': True,\n", - " 'training_time': training_time,\n", - " 'total_parameters': total_params,\n", - " 'trainable_parameters': trainable_params,\n", - " 'train_loss': train_result.training_loss,\n", - " 'eval_loss': eval_result['eval_loss'],\n", - " 'eval_accuracy': eval_result['eval_accuracy'],\n", - " 'train_steps': train_result.global_step,\n", - " 'memory_info': memory_info,\n", - " 'training_params': training_params\n", - " }\n", - "\n", - "# Example configuration\n", - "training_params = {\n", - " 'num_labels': 3,\n", - " 'train_samples': 1000,\n", - " 'eval_samples': 200,\n", - " 'epochs': 3,\n", - " 'batch_size': 16,\n", - " 'eval_batch_size': 32,\n", - " 'learning_rate': 2e-5,\n", - " 'weight_decay': 0.01,\n", - " 'warmup_steps': 100,\n", - " 'max_length': 256\n", - "}\n", - "\n", - "# Run fine-tuning\n", - "# result = lambda_transformer_finetuning('distilbert-base-uncased', training_params)\n", - "# print(f\"Fine-tuning completed! Final accuracy: {result['eval_accuracy']:.4f}\")\n", - "# print(f\"Training time: {result['training_time']:.2f} seconds\")\n", - "# print(f\"Model parameters: {result['total_parameters']:,}\")\n", - "# print(f\"GPU memory used: {result['memory_info'].get('max_allocated_mb', 0):.1f} MB\")\n", - "\n", - "print(\"Transformer fine-tuning function defined. Uncomment the lines above to run on Lambda Cloud.\")" - ] - }, - { - "cell_type": "markdown", - "id": "computer-vision", - "metadata": {}, - "source": [ - "## Example 3: Computer Vision with Large Datasets" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cv-training", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=8, memory=\"32GB\", time=\"01:30:00\")\n", - "def lambda_computer_vision_training(model_config, data_config):\n", - " \"\"\"Train a computer vision model on Lambda Cloud GPU.\"\"\"\n", - " import torch\n", - " import torch.nn as nn\n", - " import torch.optim as optim\n", - " import torchvision\n", - " import torchvision.transforms as transforms\n", - " from torch.utils.data import DataLoader, TensorDataset\n", - " import numpy as np\n", - " import time\n", - " \n", - " # Set device\n", - " device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", - " print(f\"Training computer vision model on device: {device}\")\n", - " \n", - " # Data augmentation and preprocessing\n", - " transform_train = transforms.Compose([\n", - " transforms.ToPILImage(),\n", - " transforms.RandomResizedCrop(data_config['image_size']),\n", - " transforms.RandomHorizontalFlip(p=0.5),\n", - " transforms.RandomRotation(degrees=15),\n", - " transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),\n", - " transforms.ToTensor(),\n", - " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", - " ])\n", - " \n", - " transform_val = transforms.Compose([\n", - " transforms.ToPILImage(),\n", - " transforms.Resize((data_config['image_size'], data_config['image_size'])),\n", - " transforms.ToTensor(),\n", - " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", - " ])\n", - " \n", - " # Generate synthetic image data\n", - " def create_synthetic_images(n_samples, image_size, n_channels, n_classes):\n", - " \"\"\"Create synthetic image dataset.\"\"\"\n", - " images = np.random.randint(0, 256, (n_samples, image_size, image_size, n_channels), dtype=np.uint8)\n", - " labels = np.random.randint(0, n_classes, n_samples)\n", - " return images, labels\n", - " \n", - " # Create datasets\n", - " train_images, train_labels = create_synthetic_images(\n", - " data_config['train_samples'],\n", - " data_config['image_size'],\n", - " data_config['n_channels'],\n", - " data_config['n_classes']\n", - " )\n", - " \n", - " val_images, val_labels = create_synthetic_images(\n", - " data_config['val_samples'],\n", - " data_config['image_size'],\n", - " data_config['n_channels'],\n", - " data_config['n_classes']\n", - " )\n", - " \n", - " # Custom dataset class\n", - " class SyntheticImageDataset(torch.utils.data.Dataset):\n", - " def __init__(self, images, labels, transform=None):\n", - " self.images = images\n", - " self.labels = labels\n", - " self.transform = transform\n", - " \n", - " def __len__(self):\n", - " return len(self.images)\n", - " \n", - " def __getitem__(self, idx):\n", - " image = self.images[idx]\n", - " label = self.labels[idx]\n", - " \n", - " if self.transform:\n", - " image = self.transform(image)\n", - " else:\n", - " image = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0\n", - " \n", - " return image, label\n", - " \n", - " # Create data loaders\n", - " train_dataset = SyntheticImageDataset(train_images, train_labels, transform_train)\n", - " val_dataset = SyntheticImageDataset(val_images, val_labels, transform_val)\n", - " \n", - " train_loader = DataLoader(\n", - " train_dataset,\n", - " batch_size=data_config['batch_size'],\n", - " shuffle=True,\n", - " num_workers=4,\n", - " pin_memory=True if torch.cuda.is_available() else False\n", - " )\n", - " \n", - " val_loader = DataLoader(\n", - " val_dataset,\n", - " batch_size=data_config['batch_size'],\n", - " shuffle=False,\n", - " num_workers=4,\n", - " pin_memory=True if torch.cuda.is_available() else False\n", - " )\n", - " \n", - " # Model definition\n", - " if model_config['model_type'] == 'resnet':\n", - " if model_config['pretrained']:\n", - " model = torchvision.models.resnet50(pretrained=True)\n", - " model.fc = nn.Linear(model.fc.in_features, data_config['n_classes'])\n", - " else:\n", - " model = torchvision.models.resnet50(pretrained=False, num_classes=data_config['n_classes'])\n", - " elif model_config['model_type'] == 'efficientnet':\n", - " if model_config['pretrained']:\n", - " model = torchvision.models.efficientnet_b0(pretrained=True)\n", - " model.classifier[1] = nn.Linear(model.classifier[1].in_features, data_config['n_classes'])\n", - " else:\n", - " model = torchvision.models.efficientnet_b0(pretrained=False, num_classes=data_config['n_classes'])\n", - " else:\n", - " raise ValueError(f\"Unsupported model type: {model_config['model_type']}\")\n", - " \n", - " model = model.to(device)\n", - " \n", - " # Loss and optimizer\n", - " criterion = nn.CrossEntropyLoss()\n", - " optimizer = optim.AdamW(\n", - " model.parameters(),\n", - " lr=model_config['learning_rate'],\n", - " weight_decay=model_config['weight_decay']\n", - " )\n", - " \n", - " # Learning rate scheduler\n", - " scheduler = optim.lr_scheduler.CosineAnnealingLR(\n", - " optimizer, T_max=model_config['epochs']\n", - " )\n", - " \n", - " # Training loop\n", - " start_time = time.time()\n", - " train_losses = []\n", - " val_accuracies = []\n", - " \n", - " for epoch in range(model_config['epochs']):\n", - " # Training phase\n", - " model.train()\n", - " running_loss = 0.0\n", - " \n", - " for batch_idx, (data, target) in enumerate(train_loader):\n", - " data, target = data.to(device), target.to(device)\n", - " \n", - " optimizer.zero_grad()\n", - " output = model(data)\n", - " loss = criterion(output, target)\n", - " loss.backward()\n", - " optimizer.step()\n", - " \n", - " running_loss += loss.item()\n", - " \n", - " avg_train_loss = running_loss / len(train_loader)\n", - " train_losses.append(avg_train_loss)\n", - " \n", - " # Validation phase\n", - " model.eval()\n", - " correct = 0\n", - " total = 0\n", - " val_loss = 0.0\n", - " \n", - " with torch.no_grad():\n", - " for data, target in val_loader:\n", - " data, target = data.to(device), target.to(device)\n", - " output = model(data)\n", - " val_loss += criterion(output, target).item()\n", - " \n", - " _, predicted = torch.max(output.data, 1)\n", - " total += target.size(0)\n", - " correct += (predicted == target).sum().item()\n", - " \n", - " val_accuracy = 100.0 * correct / total\n", - " val_accuracies.append(val_accuracy)\n", - " \n", - " scheduler.step()\n", - " \n", - " if epoch % 5 == 0 or epoch == model_config['epochs'] - 1:\n", - " print(f'Epoch {epoch+1}/{model_config[\"epochs\"]}: '\n", - " f'Train Loss: {avg_train_loss:.4f}, '\n", - " f'Val Accuracy: {val_accuracy:.2f}%, '\n", - " f'LR: {scheduler.get_last_lr()[0]:.6f}')\n", - " \n", - " training_time = time.time() - start_time\n", - " \n", - " # Memory usage\n", - " memory_info = {}\n", - " if torch.cuda.is_available():\n", - " memory_info = {\n", - " 'allocated_mb': torch.cuda.memory_allocated() / (1024**2),\n", - " 'reserved_mb': torch.cuda.memory_reserved() / (1024**2),\n", - " 'max_allocated_mb': torch.cuda.max_memory_allocated() / (1024**2)\n", - " }\n", - " \n", - " return {\n", - " 'training_completed': True,\n", - " 'device_used': str(device),\n", - " 'model_type': model_config['model_type'],\n", - " 'model_parameters': sum(p.numel() for p in model.parameters()),\n", - " 'training_time': training_time,\n", - " 'final_train_loss': train_losses[-1],\n", - " 'final_val_accuracy': val_accuracies[-1],\n", - " 'best_val_accuracy': max(val_accuracies),\n", - " 'train_losses': train_losses,\n", - " 'val_accuracies': val_accuracies,\n", - " 'memory_info': memory_info,\n", - " 'data_config': data_config,\n", - " 'model_config': model_config\n", - " }\n", - "\n", - "# Example configuration\n", - "model_config = {\n", - " 'model_type': 'resnet', # or 'efficientnet'\n", - " 'pretrained': True,\n", - " 'epochs': 20,\n", - " 'learning_rate': 0.001,\n", - " 'weight_decay': 1e-4\n", - "}\n", - "\n", - "data_config = {\n", - " 'train_samples': 5000,\n", - " 'val_samples': 1000,\n", - " 'image_size': 224,\n", - " 'n_channels': 3,\n", - " 'n_classes': 10,\n", - " 'batch_size': 32\n", - "}\n", - "\n", - "# Run training\n", - "# result = lambda_computer_vision_training(model_config, data_config)\n", - "# print(f\"CV training completed! Best accuracy: {result['best_val_accuracy']:.2f}%\")\n", - "# print(f\"Training time: {result['training_time']:.2f} seconds\")\n", - "# print(f\"Model parameters: {result['model_parameters']:,}\")\n", - "# print(f\"GPU memory used: {result['memory_info'].get('max_allocated_mb', 0):.1f} MB\")\n", - "\n", - "print(\"Computer vision training function defined. Uncomment the lines above to run on Lambda Cloud.\")" - ] - }, - { - "cell_type": "markdown", - "id": "multi-gpu", - "metadata": {}, - "source": [ - "## Multi-GPU Training on Lambda Cloud" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "multi-gpu-setup", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=16, memory=\"128GB\", time=\"04:00:00\")\n", - "def lambda_multi_gpu_training(model_config, training_config):\n", - " \"\"\"Multi-GPU training example using PyTorch DDP.\"\"\"\n", - " import torch\n", - " import torch.nn as nn\n", - " import torch.multiprocessing as mp\n", - " from torch.nn.parallel import DistributedDataParallel as DDP\n", - " from torch.distributed import init_process_group, destroy_process_group\n", - " import os\n", - " \n", - " def setup_ddp(rank, world_size):\n", - " \"\"\"Setup distributed data parallel.\"\"\"\n", - " os.environ['MASTER_ADDR'] = 'localhost'\n", - " os.environ['MASTER_PORT'] = '12355'\n", - " init_process_group(backend=\"nccl\", rank=rank, world_size=world_size)\n", - " torch.cuda.set_device(rank)\n", - " \n", - " def cleanup_ddp():\n", - " \"\"\"Clean up distributed training.\"\"\"\n", - " destroy_process_group()\n", - " \n", - " def train_on_gpu(rank, world_size, model_config, training_config):\n", - " \"\"\"Training function for each GPU.\"\"\"\n", - " setup_ddp(rank, world_size)\n", - " \n", - " # Create model and move to GPU\n", - " model = create_model(model_config).to(rank)\n", - " model = DDP(model, device_ids=[rank])\n", - " \n", - " # Create data loader with DistributedSampler\n", - " train_loader = create_distributed_dataloader(training_config, rank, world_size)\n", - " \n", - " # Training loop\n", - " optimizer = torch.optim.AdamW(model.parameters(), lr=training_config['lr'])\n", - " \n", - " for epoch in range(training_config['epochs']):\n", - " train_loader.sampler.set_epoch(epoch) # Important for proper shuffling\n", - " \n", - " for batch_idx, (data, target) in enumerate(train_loader):\n", - " data, target = data.to(rank), target.to(rank)\n", - " \n", - " optimizer.zero_grad()\n", - " output = model(data)\n", - " loss = nn.CrossEntropyLoss()(output, target)\n", - " loss.backward()\n", - " optimizer.step()\n", - " \n", - " if rank == 0 and batch_idx % 100 == 0:\n", - " print(f'Epoch {epoch}, Batch {batch_idx}, Loss: {loss.item():.4f}')\n", - " \n", - " cleanup_ddp()\n", - " \n", - " # Launch multi-GPU training\n", - " world_size = torch.cuda.device_count()\n", - " print(f\"Starting multi-GPU training on {world_size} GPUs\")\n", - " \n", - " mp.spawn(\n", - " train_on_gpu,\n", - " args=(world_size, model_config, training_config),\n", - " nprocs=world_size,\n", - " join=True\n", - " )\n", - " \n", - " return {\"training_completed\": True, \"gpus_used\": world_size}" - ] - }, - { - "cell_type": "markdown", - "id": "3b6jiq9gqq2", - "metadata": {}, - "source": [ - "### HuggingFace Accelerate Example\n", - "\n", - "Alternative approach using HuggingFace Accelerate for easier multi-GPU setup:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ayqhp0nnsnb", - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(cores=16, memory=\"128GB\", time=\"04:00:00\")\n", - "def lambda_accelerate_training(model_config, training_config):\n", - " \"\"\"Multi-GPU training using HuggingFace Accelerate.\"\"\"\n", - " from accelerate import Accelerator\n", - " import torch\n", - " import torch.nn as nn\n", - " \n", - " # Initialize accelerator\n", - " accelerator = Accelerator()\n", - " device = accelerator.device\n", - " \n", - " # Create model and optimizer\n", - " model = create_model(model_config)\n", - " optimizer = torch.optim.AdamW(model.parameters(), lr=training_config['lr'])\n", - " train_loader = create_dataloader(training_config)\n", - " \n", - " # Prepare for distributed training\n", - " model, optimizer, train_loader = accelerator.prepare(\n", - " model, optimizer, train_loader\n", - " )\n", - " \n", - " # Training loop\n", - " model.train()\n", - " for epoch in range(training_config['epochs']):\n", - " for batch_idx, (data, target) in enumerate(train_loader):\n", - " with accelerator.accumulate(model):\n", - " output = model(data)\n", - " loss = nn.CrossEntropyLoss()(output, target)\n", - " \n", - " accelerator.backward(loss)\n", - " optimizer.step()\n", - " optimizer.zero_grad()\n", - " \n", - " if accelerator.is_main_process and batch_idx % 100 == 0:\n", - " print(f'Epoch {epoch}, Batch {batch_idx}, Loss: {loss.item():.4f}')\n", - " \n", - " return {\n", - " \"training_completed\": True,\n", - " \"num_processes\": accelerator.num_processes,\n", - " \"device\": str(device)\n", - " }" - ] - }, - { - "cell_type": "markdown", - "id": "1kzkcvp11ms", - "metadata": {}, - "source": [ - "## Multi-GPU Training on Lambda Cloud\n", - "\n", - "### Available Multi-GPU Instances\n", - "\n", - "- **2x A100 (40GB)**: ~$2.20/hour\n", - "- **4x A100 (40GB)**: ~$4.40/hour \n", - "- **8x A100 (40GB)**: ~$8.80/hour\n", - "- **2x A100 (80GB)**: ~$2.80/hour\n", - "- **4x A100 (80GB)**: ~$5.60/hour\n", - "- **8x A100 (80GB)**: ~$11.20/hour\n", - "- **8x H100**: ~$20.00/hour (when available)\n", - "\n", - "### Setup Requirements\n", - "\n", - "1. **Launch multi-GPU instance** via Lambda Cloud console\n", - "2. **Install additional packages** for distributed training:\n", - " ```bash\n", - " pip install accelerate deepspeed\n", - " ```\n", - "3. **Configure Clustrix** for multi-GPU environment\n", - "4. **Use appropriate parallelization strategy**\n", - "\n", - "### Parallelization Strategies\n", - "\n", - "- **Data Parallel (DP)**: Simple, works for most models\n", - "- **Distributed Data Parallel (DDP)**: Better performance, recommended\n", - "- **Model Parallel**: For very large models that don't fit on single GPU\n", - "- **Pipeline Parallel**: For extremely large models\n", - "- **DeepSpeed ZeRO**: For memory-efficient training of large models\n", - "\n", - "### PyTorch DDP Example" - ] - }, - { - "cell_type": "markdown", - "id": "cost-optimization", - "metadata": {}, - "source": [ - "## Cost Optimization Strategies" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cost-optimization-lambda", - "metadata": {}, - "outputs": [], - "source": [ - "# Import Clustrix cost monitoring functionality\n", - "from clustrix import cost_tracking_decorator, get_cost_monitor, generate_cost_report\n", - "\n", - "# Example 1: Using the cost tracking decorator\n", - "@cost_tracking_decorator('lambda', 'a100_40gb')\n", - "@cluster(cores=8, memory=\"32GB\")\n", - "def lambda_training_with_cost_tracking():\n", - " \"\"\"Example training function with automatic cost tracking.\"\"\"\n", - " import time\n", - " import numpy as np\n", - " \n", - " # Simulate training workload\n", - " print(\"Starting training...\")\n", - " time.sleep(2) # Simulate 2 seconds of work\n", - " \n", - " # Simulate some compute\n", - " data = np.random.randn(1000, 1000)\n", - " result = np.dot(data, data.T)\n", - " \n", - " print(\"Training completed!\")\n", - " return {\n", - " 'model_accuracy': 0.95,\n", - " 'training_samples': 10000,\n", - " 'final_loss': 0.032\n", - " }\n", - "\n", - "# Example 2: Manual cost monitoring\n", - "def manual_cost_monitoring_example():\n", - " \"\"\"Example of manual cost monitoring.\"\"\"\n", - " # Start cost monitoring\n", - " monitor = get_cost_monitor('lambda')\n", - " if monitor:\n", - " monitor.start_monitoring()\n", - " \n", - " # Your computation here\n", - " import time\n", - " time.sleep(1)\n", - " \n", - " # Stop monitoring and get report\n", - " cost_report = monitor.stop_monitoring()\n", - " if cost_report:\n", - " print(f\"Computation completed in {cost_report.duration_seconds:.2f} seconds\")\n", - " print(f\"Estimated cost: ${cost_report.cost_estimate.estimated_cost:.4f}\")\n", - " print(f\"GPU utilization: {len(cost_report.resource_usage.gpu_stats or [])} GPUs\")\n", - " \n", - " if cost_report.recommendations:\n", - " print(\"Cost optimization recommendations:\")\n", - " for rec in cost_report.recommendations:\n", - " print(f\" - {rec}\")\n", - "\n", - "# Example 3: Generate real-time cost report\n", - "def get_current_cost_status():\n", - " \"\"\"Get current cost and resource status.\"\"\"\n", - " report = generate_cost_report('lambda', 'a100_40gb')\n", - " if report:\n", - " print(\"Current Lambda Cloud Status:\")\n", - " print(f\" CPU Usage: {report['resource_usage']['cpu_percent']:.1f}%\")\n", - " print(f\" Memory Usage: {report['resource_usage']['memory_percent']:.1f}%\")\n", - " if report['resource_usage']['gpu_stats']:\n", - " avg_gpu = sum(gpu['utilization_percent'] for gpu in report['resource_usage']['gpu_stats']) / len(report['resource_usage']['gpu_stats'])\n", - " print(f\" GPU Usage: {avg_gpu:.1f}%\")\n", - " print(f\" Hourly Rate: ${report['cost_estimate']['hourly_rate']:.2f}\")\n", - "\n", - "# Example 4: Compare pricing across instance types\n", - "def compare_lambda_pricing():\n", - " \"\"\"Compare pricing for different Lambda Cloud instance types.\"\"\"\n", - " from clustrix import get_pricing_info\n", - " \n", - " pricing = get_pricing_info('lambda')\n", - " if pricing:\n", - " print(\"Lambda Cloud Instance Pricing (USD/hour):\")\n", - " \n", - " # Group by category\n", - " single_gpu = {k: v for k, v in pricing.items() if not k.startswith(('2x', '4x', '8x')) and k != 'default'}\n", - " multi_gpu = {k: v for k, v in pricing.items() if k.startswith(('2x', '4x', '8x'))}\n", - " \n", - " print(\"\\nSingle GPU Instances:\")\n", - " for instance, price in sorted(single_gpu.items(), key=lambda x: x[1]):\n", - " print(f\" {instance:<15}: ${price:.2f}/hour\")\n", - " \n", - " print(\"\\nMulti-GPU Instances:\")\n", - " for instance, price in sorted(multi_gpu.items(), key=lambda x: x[1]):\n", - " print(f\" {instance:<15}: ${price:.2f}/hour\")\n", - "\n", - "# Run examples (uncomment to test)\n", - "# print(\"1. Cost tracking decorator example:\")\n", - "# result = lambda_training_with_cost_tracking()\n", - "# print(f\"Training result: {result}\")\n", - "\n", - "# print(\"\\n2. Manual cost monitoring example:\")\n", - "# manual_cost_monitoring_example()\n", - "\n", - "# print(\"\\n3. Current cost status:\")\n", - "# get_current_cost_status()\n", - "\n", - "print(\"4. Lambda Cloud pricing comparison:\")\n", - "compare_lambda_pricing()\n", - "\n", - "print(\"\\n✅ Lambda Cloud cost monitoring examples ready!\")\n", - "print(\"💡 Use @cost_tracking_decorator('lambda', 'instance_type') for automatic cost tracking\")" - ] - }, - { - "cell_type": "markdown", - "id": "iyk1bplps9", - "metadata": {}, - "source": [ - "## Lambda Cloud Cost Optimization\n", - "\n", - "### Cost Monitoring and Tracking\n", - "\n", - "Monitor GPU utilization and track costs effectively:" - ] - }, - { - "cell_type": "markdown", - "id": "h19wkr887g", - "metadata": {}, - "source": [ - "### Lambda Cloud Cost Optimization\n", - "\n", - "#### 💰 Instance Selection\n", - "- **RTX 6000 Ada**: Best value for most ML workloads (~$0.75/hour)\n", - "- **A10**: Good balance of performance and cost (~$0.60/hour)\n", - "- **A100 40GB**: For large models requiring more VRAM (~$1.10/hour)\n", - "- **A100 80GB**: Only when 40GB is insufficient (~$1.40/hour)\n", - "- **H100**: Premium option for cutting-edge research (~$2.50/hour)\n", - "\n", - "#### ⏰ Usage Patterns\n", - "- Use \"persistent\" instances for ongoing development\n", - "- Terminate instances immediately after training completion\n", - "- Schedule training jobs during off-peak hours if possible\n", - "- Use local development for debugging, GPU for final training\n", - "\n", - "#### 🔧 Optimization Techniques\n", - "- Mixed precision training (fp16) to reduce memory usage\n", - "- Gradient accumulation for effective larger batch sizes\n", - "- Model checkpointing to resume interrupted training\n", - "- Efficient data loading with multiple workers\n", - "- Early stopping to avoid overtraining\n", - "\n", - "#### 📊 Monitoring and Management\n", - "- Monitor GPU utilization with nvidia-smi\n", - "- Track training progress with logging\n", - "- Set training time limits to prevent runaway costs\n", - "- Use Clustrix timeouts as safety nets\n", - "- Regular cost reviews and budget alerts\n", - "\n", - "#### 🚀 Clustrix-Specific Optimizations\n", - "- Use Clustrix auto-cleanup features\n", - "- Implement job queuing for multiple experiments\n", - "- Leverage Clustrix's timeout mechanisms\n", - "- Use remote environment caching" - ] - }, - { - "cell_type": "markdown", - "id": "best-practices", - "metadata": {}, - "source": [ - "## Best Practices and Troubleshooting" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "best-practices-lambda", - "metadata": {}, - "outputs": [], - "source": [ - "# Example usage of monitoring functions\n", - "def create_monitoring_script():\n", - " \"\"\"Create and save the GPU monitoring script.\"\"\"\n", - " script_content = '''#!/bin/bash\n", - "# Lambda Cloud monitoring script\n", - "\n", - "echo \"Lambda Cloud Training Monitor\"\n", - "echo \"============================\"\n", - "echo \"Start time: $(date)\"\n", - "echo \"\"\n", - "\n", - "# System information\n", - "echo \"System Information:\"\n", - "echo \"------------------\"\n", - "nvidia-smi --query-gpu=gpu_name,memory.total,power.draw --format=csv\n", - "echo \"\"\n", - "\n", - "# Monitor GPU usage every 30 seconds\n", - "while true; do\n", - " echo \"GPU Status at $(date):\"\n", - " nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu --format=csv,noheader\n", - " echo \"\"\n", - " \n", - " # Check if training process is still running\n", - " if ! pgrep -f python > /dev/null; then\n", - " echo \"No Python processes found. Training may have completed.\"\n", - " break\n", - " fi\n", - " \n", - " sleep 30\n", - "done\n", - "\n", - "echo \"Monitoring completed at $(date)\"\n", - "'''\n", - " \n", - " with open('monitor_training.sh', 'w') as f:\n", - " f.write(script_content)\n", - " \n", - " # Make executable\n", - " import os\n", - " os.chmod('monitor_training.sh', 0o755)\n", - " \n", - " return \"Monitoring script created: monitor_training.sh\"\n", - "\n", - "# Uncomment to create the monitoring script:\n", - "# result = create_monitoring_script()\n", - "# print(result)" - ] - }, - { - "cell_type": "markdown", - "id": "9e011y2ptia", - "metadata": {}, - "source": [ - "## Lambda Cloud Best Practices\n", - "\n", - "### GPU Monitoring Script\n", - "\n", - "Use this monitoring script to track GPU usage during training. Save as `monitor_training.sh` and run with: `bash monitor_training.sh`\n", - "\n", - "```bash\n", - "#!/bin/bash\n", - "# Lambda Cloud monitoring script\n", - "\n", - "echo \"Lambda Cloud Training Monitor\"\n", - "echo \"============================\"\n", - "echo \"Start time: $(date)\"\n", - "echo \"\"\n", - "\n", - "# System information\n", - "echo \"System Information:\"\n", - "echo \"------------------\"\n", - "nvidia-smi --query-gpu=gpu_name,memory.total,power.draw --format=csv\n", - "echo \"\"\n", - "\n", - "# Monitor GPU usage every 30 seconds\n", - "while true; do\n", - " echo \"GPU Status at $(date):\"\n", - " nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu --format=csv,noheader\n", - " echo \"\"\n", - " \n", - " # Check if training process is still running\n", - " if ! pgrep -f python > /dev/null; then\n", - " echo \"No Python processes found. Training may have completed.\"\n", - " break\n", - " fi\n", - " \n", - " sleep 30\n", - "done\n", - "\n", - "echo \"Monitoring completed at $(date)\"\n", - "```" - ] - }, - { - "cell_type": "markdown", - "id": "2d105yt16xr", - "metadata": {}, - "source": [ - "### Lambda Cloud + Clustrix Best Practices\n", - "\n", - "#### 🚀 Performance Optimization\n", - "- Always use mixed precision (fp16) when possible\n", - "- Optimize data loading with multiple workers and pin_memory\n", - "- Use appropriate batch sizes to maximize GPU utilization\n", - "- Enable tensor cores for compatible operations\n", - "- Pre-allocate GPU memory to avoid fragmentation\n", - "\n", - "#### 💾 Data Management\n", - "- Store datasets on fast NVMe storage when available\n", - "- Use data streaming for very large datasets\n", - "- Implement efficient data preprocessing pipelines\n", - "- Cache frequently used data in memory\n", - "- Use appropriate data formats (e.g., HDF5, Parquet)\n", - "\n", - "#### 🔧 Environment Setup\n", - "- Use conda environments for reproducible setups\n", - "- Pin package versions in requirements.txt\n", - "- Install packages from conda-forge when possible\n", - "- Use uv package manager for faster installs\n", - "- Set up proper CUDA environment variables\n", - "\n", - "#### 🛠️ Development Workflow\n", - "- Develop and debug locally, train on Lambda Cloud\n", - "- Use small datasets for initial testing\n", - "- Implement proper logging and monitoring\n", - "- Save model checkpoints regularly\n", - "- Use version control for experiment tracking\n", - "\n", - "#### 🔒 Security\n", - "- Use SSH keys instead of passwords\n", - "- Keep SSH keys secure and rotate regularly\n", - "- Don't store credentials in code or notebooks\n", - "- Use environment variables for configuration\n", - "- Monitor instance access logs" - ] - }, - { - "cell_type": "markdown", - "id": "0lrqgzg1xis", - "metadata": {}, - "source": [ - "### Common Issues and Solutions\n", - "\n", - "#### ❌ CUDA out of memory errors\n", - "✅ **Solutions:**\n", - "- Reduce batch size\n", - "- Enable gradient checkpointing\n", - "- Use mixed precision training\n", - "- Clear GPU cache with torch.cuda.empty_cache()\n", - "- Consider model parallelism for large models\n", - "\n", - "#### ❌ Slow data loading\n", - "✅ **Solutions:**\n", - "- Increase num_workers in DataLoader\n", - "- Enable pin_memory for GPU transfers\n", - "- Use faster storage (NVMe over network storage)\n", - "- Implement data prefetching\n", - "- Optimize data preprocessing\n", - "\n", - "#### ❌ SSH connection timeouts\n", - "✅ **Solutions:**\n", - "- Configure SSH keep-alive settings\n", - "- Use screen or tmux for long-running jobs\n", - "- Implement proper error handling in Clustrix\n", - "- Set appropriate timeout values\n", - "- Monitor network connectivity\n", - "\n", - "#### ❌ Low GPU utilization\n", - "✅ **Solutions:**\n", - "- Increase batch size if memory allows\n", - "- Optimize data loading pipeline\n", - "- Use asynchronous data transfers\n", - "- Profile code to identify bottlenecks\n", - "- Consider multi-GPU training\n", - "\n", - "#### ❌ Package installation failures\n", - "✅ **Solutions:**\n", - "- Use conda for system-level packages\n", - "- Check CUDA compatibility versions\n", - "- Clear pip cache if needed\n", - "- Use --no-cache-dir flag for pip\n", - "- Install packages in correct order" - ] - }, - { - "cell_type": "markdown", - "id": "cleanup-lambda", - "metadata": {}, - "source": [ - "## Instance Management and Cleanup" - ] - }, - { - "cell_type": "markdown", - "id": "cleanup-instances", - "metadata": {}, - "source": [ - "### Lambda Cloud Instance Management\n", - "\n", - "#### 🔍 Check Running Instances\n", - "\n", - "**Via CLI:**\n", - "```bash\n", - "lambda-cloud instance list\n", - "```\n", - "\n", - "**Via Web Console:**\n", - "Visit: https://cloud.lambdalabs.com/instances\n", - "\n", - "#### ⏹️ Terminate Instances\n", - "\n", - "**Terminate specific instance:**\n", - "```bash\n", - "lambda-cloud instance terminate \n", - "```\n", - "\n", - "**Terminate all instances (DANGEROUS!):**\n", - "```bash\n", - "lambda-cloud instance list --format=csv | grep -v \"instance_id\" | cut -d',' -f1 | xargs -I {} lambda-cloud instance terminate {}\n", - "```\n", - "\n", - "#### 💾 Save Work Before Termination\n", - "\n", - "**Save models to persistent storage:**\n", - "```bash\n", - "rsync -avz ubuntu@:/path/to/models/ ./local_models/\n", - "```\n", - "\n", - "**Save logs and results:**\n", - "```bash\n", - "scp -r ubuntu@:/tmp/clustrix/ ./results/\n", - "```\n", - "\n", - "#### 📊 Cost Monitoring\n", - "\n", - "**Check current usage:**\n", - "```bash\n", - "lambda-cloud instance list --format=table\n", - "```\n", - "\n", - "**Estimate costs:**\n", - "```bash\n", - "lambda-cloud instance list --format=csv | awk -F',' 'NR>1 {print $2, $3}' | while read type status; do\n", - " if [ \"$status\" = \"active\" ]; then\n", - " echo \"Active instance: $type\"\n", - " fi\n", - "done\n", - "```\n", - "\n", - "### Automated Cleanup Script\n", - "\n", - "Save this as `lambda_cleanup.sh` for automated instance management:\n", - "\n", - "```bash\n", - "#!/bin/bash\n", - "# Automated cleanup script for Lambda Cloud\n", - "# Save as lambda_cleanup.sh\n", - "\n", - "set -e\n", - "\n", - "echo \"Lambda Cloud Automated Cleanup\"\n", - "echo \"==============================\"\n", - "\n", - "# Check if lambda-cloud CLI is installed\n", - "if ! command -v lambda-cloud &> /dev/null; then\n", - " echo \"Error: lambda-cloud CLI not found. Please install it first.\"\n", - " exit 1\n", - "fi\n", - "\n", - "# List current instances\n", - "echo \"Current instances:\"\n", - "lambda-cloud instance list\n", - "echo \"\"\n", - "\n", - "# Ask for confirmation\n", - "read -p \"Do you want to terminate ALL instances? (y/N): \" -n 1 -r\n", - "echo \"\"\n", - "if [[ ! $REPLY =~ ^[Yy]$ ]]; then\n", - " echo \"Cleanup cancelled.\"\n", - " exit 0\n", - "fi\n", - "\n", - "# Get instance IDs\n", - "INSTANCE_IDS=$(lambda-cloud instance list --format=csv | grep -v \"instance_id\" | cut -d',' -f1)\n", - "\n", - "if [ -z \"$INSTANCE_IDS\" ]; then\n", - " echo \"No instances to terminate.\"\n", - " exit 0\n", - "fi\n", - "\n", - "# Terminate instances\n", - "echo \"Terminating instances...\"\n", - "for instance_id in $INSTANCE_IDS; do\n", - " echo \"Terminating instance: $instance_id\"\n", - " lambda-cloud instance terminate $instance_id\n", - "done\n", - "\n", - "echo \"All instances terminated.\"\n", - "echo \"Please verify termination in the web console: https://cloud.lambdalabs.com/instances\"\n", - "```\n", - "\n", - "### Clustrix Integration Manager" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5f4n1hu8qdb", - "metadata": {}, - "outputs": [], - "source": [ - "# Integrate cleanup with Clustrix workflows\n", - "\n", - "from clustrix import configure\n", - "import subprocess\n", - "import time\n", - "\n", - "class LambdaCloudManager:\n", - " \"\"\"Manager for Lambda Cloud instances with Clustrix integration.\"\"\"\n", - " \n", - " def __init__(self):\n", - " self.active_instances = []\n", - " \n", - " def launch_instance_for_clustrix(self, instance_type, ssh_key_name):\n", - " \"\"\"Launch instance and configure Clustrix.\"\"\"\n", - " # Launch instance\n", - " result = subprocess.run([\n", - " 'lambda-cloud', 'instance', 'launch',\n", - " '--instance-type', instance_type,\n", - " '--ssh-key-name', ssh_key_name\n", - " ], capture_output=True, text=True)\n", - " \n", - " if result.returncode != 0:\n", - " raise Exception(f\"Failed to launch instance: {result.stderr}\")\n", - " \n", - " # Parse instance ID and IP (simplified)\n", - " instance_id = \"extracted_from_output\" # Parse from result.stdout\n", - " instance_ip = \"extracted_from_output\" # Parse from result.stdout\n", - " \n", - " # Wait for instance to be ready\n", - " time.sleep(60) # Wait for startup\n", - " \n", - " # Configure Clustrix\n", - " configure(\n", - " cluster_type=\"ssh\",\n", - " cluster_host=instance_ip,\n", - " username=\"ubuntu\",\n", - " key_file=\"~/.ssh/id_rsa\",\n", - " remote_work_dir=\"~/.clustrix/jobs\",\n", - " package_manager=\"auto\",\n", - " cleanup_on_success=True\n", - " )\n", - " \n", - " self.active_instances.append({\n", - " 'id': instance_id,\n", - " 'ip': instance_ip,\n", - " 'type': instance_type,\n", - " 'launch_time': time.time()\n", - " })\n", - " \n", - " return instance_id, instance_ip\n", - " \n", - " def cleanup_all_instances(self):\n", - " \"\"\"Clean up all managed instances.\"\"\"\n", - " for instance in self.active_instances:\n", - " try:\n", - " subprocess.run([\n", - " 'lambda-cloud', 'instance', 'terminate', instance['id']\n", - " ], check=True)\n", - " print(f\"Terminated instance {instance['id']}\")\n", - " except subprocess.CalledProcessError as e:\n", - " print(f\"Failed to terminate {instance['id']}: {e}\")\n", - " \n", - " self.active_instances.clear()\n", - " \n", - " def __del__(self):\n", - " \"\"\"Ensure cleanup on object destruction.\"\"\"\n", - " if self.active_instances:\n", - " print(\"Warning: Active instances detected. Cleaning up...\")\n", - " self.cleanup_all_instances()\n", - "\n", - "# Usage example:\n", - "# manager = LambdaCloudManager()\n", - "# try:\n", - "# instance_id, ip = manager.launch_instance_for_clustrix('a100', 'my-ssh-key')\n", - "# # Run your Clustrix computations\n", - "# result = my_clustrix_function()\n", - "# finally:\n", - "# manager.cleanup_all_instances()" - ] - }, - { - "cell_type": "markdown", - "id": "lambda-summary", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "This tutorial covered:\n", - "\n", - "1. **Setup**: Lambda Cloud account creation and instance management\n", - "2. **GPU Computing**: High-performance GPU instances for ML workloads\n", - "3. **Deep Learning**: PyTorch training with GPU acceleration\n", - "4. **Transformer Models**: Fine-tuning with HuggingFace Transformers\n", - "5. **Computer Vision**: CNN training with data augmentation\n", - "6. **Multi-GPU Training**: Distributed training across multiple GPUs\n", - "7. **Cost Optimization**: Strategies to minimize GPU computing costs\n", - "8. **Best Practices**: Performance optimization and troubleshooting\n", - "9. **Instance Management**: Automated cleanup and monitoring\n", - "\n", - "### Key Advantages of Lambda Cloud + Clustrix\n", - "\n", - "- **GPU Focus**: Specialized in high-performance GPU computing\n", - "- **Cost Effective**: Competitive pricing for GPU instances\n", - "- **Simple Management**: Easy instance launching and termination\n", - "- **High Performance**: Latest NVIDIA GPUs (A100, H100, RTX)\n", - "- **Fast Networking**: InfiniBand for multi-GPU communication\n", - "- **ML Optimized**: Pre-configured environments for machine learning\n", - "- **Flexible Scaling**: From single GPU to large multi-GPU clusters\n", - "\n", - "### Lambda Cloud Pricing Advantages\n", - "\n", - "- **RTX 6000 Ada**: Excellent price/performance for most ML workloads\n", - "- **A100 40GB/80GB**: Industry-standard for large-scale training\n", - "- **H100**: Cutting-edge performance for the most demanding workloads\n", - "- **Multi-GPU**: Cost-effective scaling for distributed training\n", - "- **No Hidden Fees**: Simple per-hour pricing\n", - "\n", - "### Next Steps\n", - "\n", - "1. Create your Lambda Cloud account and add SSH keys\n", - "2. Start with a single GPU instance for testing\n", - "3. Configure Clustrix for your Lambda Cloud instance\n", - "4. Run the provided examples to verify setup\n", - "5. Scale to multi-GPU instances for larger workloads\n", - "6. Implement cost monitoring and automated cleanup\n", - "\n", - "### Use Cases\n", - "\n", - "- **Deep Learning Research**: Train large neural networks efficiently\n", - "- **Computer Vision**: Process large image datasets with CNNs\n", - "- **NLP**: Fine-tune transformer models on custom datasets\n", - "- **Scientific Computing**: GPU-accelerated simulations and modeling\n", - "- **Prototyping**: Rapid experimentation with different architectures\n", - "- **Production Training**: Scale up successful experiments\n", - "\n", - "### Resources\n", - "\n", - "- [Lambda Cloud Console](https://cloud.lambdalabs.com/)\n", - "- [Lambda Cloud Documentation](https://lambdalabs.com/service/gpu-cloud/documentation)\n", - "- [Lambda Cloud CLI](https://github.com/LambdaLabsML/lambda-cloud-cli)\n", - "- [PyTorch Documentation](https://pytorch.org/docs/)\n", - "- [HuggingFace Transformers](https://huggingface.co/transformers/)\n", - "- [Clustrix Documentation](https://clustrix.readthedocs.io/)\n", - "\n", - "**Remember**: Lambda Cloud excels at GPU computing! Always terminate instances when not in use to control costs, and leverage Clustrix's distributed computing capabilities to scale your ML workloads efficiently." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/pbs_tutorial.ipynb b/docs/source/notebooks/pbs_tutorial.ipynb deleted file mode 100644 index e33efc7d..00000000 --- a/docs/source/notebooks/pbs_tutorial.ipynb +++ /dev/null @@ -1,1379 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# PBS/Torque Cluster Tutorial\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/pbs_tutorial.ipynb)\n", - "\n", - "This tutorial demonstrates how to use Clustrix with PBS (Portable Batch System) and Torque clusters. PBS is widely used in academic and research computing environments.\n", - "\n", - "## Prerequisites\n", - "\n", - "- Access to a PBS/Torque cluster\n", - "- SSH key configured for the cluster\n", - "- Clustrix installed: `pip install clustrix`" - ], - "id": "cell-0" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **PBS is unverified against real hardware.** It shares its job-directory\n", - "> staging, environment build and job-execution code with the SLURM and SSH\n", - "> backends (which *are* verified end to end) -- it is not a separate,\n", - "> untested code path -- but nobody has run this backend against a live\n", - "> PBS/Torque scheduler. Treat this notebook as a description of the\n", - "> intended interface, not a record of something that has been executed to\n", - "> completion.\n", - "\n", - "## What Clustrix Does Behind the Scenes\n", - "\n", - "The submission pipeline is the same ten-step sequence as SLURM (serialize\n", - "with `dill`, connect over SSH with host-key verification, stage a `0700`\n", - "job directory with a random result-signing key, upload\n", - "`function_data.pkl`, build a two-venv environment, generate and upload the\n", - "job script, submit, poll, verify-then-deserialize the HMAC-signed result,\n", - "clean up) -- see the online docs' PBS tutorial page for the full\n", - "walkthrough and the generated `job.pbs` script. The PBS-specific\n", - "differences: submission is `qsub job.pbs` instead of `sbatch job.sh`, the\n", - "job ID is `qsub`'s stdout taken verbatim, and the script uses `#PBS`\n", - "directives (`-l nodes=1:ppn=N`, `-l mem=gb`, `-l walltime=...`, `-q\n", - "`) built only from `cores`, `memory`, `time` and `queue`. **Any\n", - "other keyword argument passed to `@cluster(...)` -- `walltime=`,\n", - "`features=`, `pbs_array=`, or anything else PBS-specific -- is accepted by\n", - "Python but never written into the job script.** Several cells further down\n", - "in this notebook demonstrate that pitfall directly.\n" - ], - "id": "cell-1" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Installation and Setup" - ], - "id": "cell-2" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Install Clustrix (uncomment if needed)\n", - "# !pip install clustrix\n", - "\n", - "import clustrix\n", - "from clustrix import cluster, configure\n", - "import numpy as np\n", - "import pandas as pd" - ], - "id": "cell-3" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## PBS Cluster Configuration\n", - "\n", - "Configure Clustrix for your PBS/Torque cluster:" - ], - "id": "cell-4" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Configure for PBS cluster\n", - "configure(\n", - " cluster_type=\"pbs\",\n", - " cluster_host=\"pbs-cluster.university.edu\", # Replace with your cluster\n", - " username=\"your-username\", # Replace with your username\n", - " key_file=\"~/.ssh/id_rsa\", # Path to SSH key\n", - " \n", - " # Default PBS resource requirements\n", - " default_cores=4,\n", - " default_memory=\"16GB\",\n", - " default_time=\"02:00:00\",\n", - " default_queue=\"normal\", # PBS queue name\n", - " \n", - " # PBS-specific options\n", - " remote_work_dir=\"/home/your-username/clustrix\", # Adjust for your cluster\n", - " \n", - " # Environment setup\n", - " module_loads=[\"python/3.9\", \"openmpi/4.0\"], # Common PBS modules\n", - " \n", - " # Job management\n", - " cleanup_on_success=True,\n", - " max_parallel_jobs=25\n", - ")\n", - "\n", - "print(\"PBS cluster configured successfully!\")" - ], - "id": "cell-5" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 1: Bioinformatics - DNA Sequence Analysis\n", - "\n", - "PBS clusters are popular in bioinformatics. Let's analyze DNA sequences:" - ], - "id": "cell-6" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(\n", - " cores=8, \n", - " memory=\"32GB\", \n", - " time=\"03:00:00\", \n", - " queue=\"bioqueue\", # Specialized bioinformatics queue\n", - ")\n", - "def analyze_dna_sequences(sequences, analysis_type=\"comprehensive\"):\n", - " \"\"\"\n", - " Comprehensive DNA sequence analysis for bioinformatics research.\n", - " \"\"\"\n", - " import numpy as np\n", - " import random\n", - " from collections import Counter, defaultdict\n", - " import re\n", - " import math\n", - " \n", - " def calculate_gc_content(sequence):\n", - " \"\"\"Calculate GC content percentage\"\"\"\n", - " gc_count = sequence.count('G') + sequence.count('C')\n", - " return (gc_count / len(sequence)) * 100 if sequence else 0\n", - " \n", - " def find_orfs(sequence, min_length=100):\n", - " \"\"\"Find Open Reading Frames (ORFs)\"\"\"\n", - " start_codon = 'ATG'\n", - " stop_codons = ['TAA', 'TAG', 'TGA']\n", - " orfs = []\n", - " \n", - " for frame in range(3): # Check all 3 reading frames\n", - " for i in range(frame, len(sequence) - 2, 3):\n", - " codon = sequence[i:i+3]\n", - " if codon == start_codon:\n", - " # Look for stop codon\n", - " for j in range(i+3, len(sequence) - 2, 3):\n", - " stop_codon = sequence[j:j+3]\n", - " if stop_codon in stop_codons:\n", - " orf_length = j - i + 3\n", - " if orf_length >= min_length:\n", - " orfs.append({\n", - " 'start': i,\n", - " 'end': j + 3,\n", - " 'length': orf_length,\n", - " 'frame': frame + 1,\n", - " 'sequence': sequence[i:j+3]\n", - " })\n", - " break\n", - " return orfs\n", - " \n", - " def analyze_codon_usage(sequence):\n", - " \"\"\"Analyze codon usage patterns\"\"\"\n", - " codons = [sequence[i:i+3] for i in range(0, len(sequence)-2, 3) \n", - " if len(sequence[i:i+3]) == 3]\n", - " codon_counts = Counter(codons)\n", - " \n", - " # Standard genetic code mapping\n", - " genetic_code = {\n", - " 'TTT': 'F', 'TTC': 'F', 'TTA': 'L', 'TTG': 'L',\n", - " 'TCT': 'S', 'TCC': 'S', 'TCA': 'S', 'TCG': 'S',\n", - " 'TAT': 'Y', 'TAC': 'Y', 'TAA': '*', 'TAG': '*',\n", - " 'TGT': 'C', 'TGC': 'C', 'TGA': '*', 'TGG': 'W',\n", - " 'CTT': 'L', 'CTC': 'L', 'CTA': 'L', 'CTG': 'L',\n", - " 'CCT': 'P', 'CCC': 'P', 'CCA': 'P', 'CCG': 'P',\n", - " 'CAT': 'H', 'CAC': 'H', 'CAA': 'Q', 'CAG': 'Q',\n", - " 'CGT': 'R', 'CGC': 'R', 'CGA': 'R', 'CGG': 'R',\n", - " 'ATT': 'I', 'ATC': 'I', 'ATA': 'I', 'ATG': 'M',\n", - " 'ACT': 'T', 'ACC': 'T', 'ACA': 'T', 'ACG': 'T',\n", - " 'AAT': 'N', 'AAC': 'N', 'AAA': 'K', 'AAG': 'K',\n", - " 'AGT': 'S', 'AGC': 'S', 'AGA': 'R', 'AGG': 'R',\n", - " 'GTT': 'V', 'GTC': 'V', 'GTA': 'V', 'GTG': 'V',\n", - " 'GCT': 'A', 'GCC': 'A', 'GCA': 'A', 'GCG': 'A',\n", - " 'GAT': 'D', 'GAC': 'D', 'GAA': 'E', 'GAG': 'E',\n", - " 'GGT': 'G', 'GGC': 'G', 'GGA': 'G', 'GGG': 'G'\n", - " }\n", - " \n", - " amino_acid_counts = defaultdict(int)\n", - " for codon, count in codon_counts.items():\n", - " if codon in genetic_code:\n", - " amino_acid_counts[genetic_code[codon]] += count\n", - " \n", - " return dict(codon_counts), dict(amino_acid_counts)\n", - " \n", - " def find_tandem_repeats(sequence, min_repeat_length=3, max_repeat_length=20):\n", - " \"\"\"Find tandem repeats in DNA sequence\"\"\"\n", - " repeats = []\n", - " \n", - " for repeat_len in range(min_repeat_length, max_repeat_length + 1):\n", - " for i in range(len(sequence) - repeat_len * 2 + 1):\n", - " motif = sequence[i:i + repeat_len]\n", - " count = 1\n", - " j = i + repeat_len\n", - " \n", - " while j + repeat_len <= len(sequence) and sequence[j:j + repeat_len] == motif:\n", - " count += 1\n", - " j += repeat_len\n", - " \n", - " if count >= 3: # At least 3 repeats\n", - " repeats.append({\n", - " 'motif': motif,\n", - " 'start': i,\n", - " 'end': j,\n", - " 'repeat_count': count,\n", - " 'total_length': j - i\n", - " })\n", - " \n", - " return repeats\n", - " \n", - " # Main analysis loop\n", - " results = []\n", - " \n", - " for seq_idx, sequence in enumerate(sequences):\n", - " print(f\"Analyzing sequence {seq_idx + 1}/{len(sequences)} (length: {len(sequence)})...\")\n", - " \n", - " # Basic composition analysis\n", - " base_composition = Counter(sequence)\n", - " gc_content = calculate_gc_content(sequence)\n", - " \n", - " # Advanced analyses\n", - " orfs = find_orfs(sequence, min_length=150)\n", - " codon_usage, amino_acid_freq = analyze_codon_usage(sequence)\n", - " tandem_repeats = find_tandem_repeats(sequence)\n", - " \n", - " # CpG island detection (simplified)\n", - " cpg_sites = len(re.findall('CG', sequence))\n", - " cpg_density = (cpg_sites / (len(sequence) - 1)) * 100 if len(sequence) > 1 else 0\n", - " \n", - " # Complexity analysis\n", - " def calculate_complexity(seq, window_size=50):\n", - " complexities = []\n", - " for i in range(0, len(seq) - window_size + 1, window_size):\n", - " window = seq[i:i + window_size]\n", - " counter = Counter(window)\n", - " entropy = -sum((count/window_size) * math.log2(count/window_size) \n", - " for count in counter.values() if count > 0)\n", - " complexities.append(entropy)\n", - " return np.mean(complexities) if complexities else 0\n", - " \n", - " complexity = calculate_complexity(sequence)\n", - " \n", - " sequence_result = {\n", - " 'sequence_id': seq_idx,\n", - " 'length': len(sequence),\n", - " 'base_composition': dict(base_composition),\n", - " 'gc_content': gc_content,\n", - " 'complexity': complexity,\n", - " 'orfs_found': len(orfs),\n", - " 'longest_orf': max(orfs, key=lambda x: x['length'])['length'] if orfs else 0,\n", - " 'cpg_sites': cpg_sites,\n", - " 'cpg_density': cpg_density,\n", - " 'tandem_repeats': len(tandem_repeats),\n", - " 'repeat_details': tandem_repeats[:5], # Keep first 5 for analysis\n", - " 'codon_diversity': len(codon_usage),\n", - " 'amino_acid_diversity': len(amino_acid_freq),\n", - " 'most_common_amino_acid': max(amino_acid_freq.items(), key=lambda x: x[1])[0] if amino_acid_freq else 'N/A'\n", - " }\n", - " \n", - " results.append(sequence_result)\n", - " \n", - " # Aggregate statistics\n", - " aggregate_stats = {\n", - " 'total_sequences': len(results),\n", - " 'total_base_pairs': sum(r['length'] for r in results),\n", - " 'average_gc_content': np.mean([r['gc_content'] for r in results]),\n", - " 'gc_content_std': np.std([r['gc_content'] for r in results]),\n", - " 'average_complexity': np.mean([r['complexity'] for r in results]),\n", - " 'total_orfs_found': sum(r['orfs_found'] for r in results),\n", - " 'total_cpg_sites': sum(r['cpg_sites'] for r in results),\n", - " 'sequences_with_repeats': sum(1 for r in results if r['tandem_repeats'] > 0),\n", - " 'individual_results': results\n", - " }\n", - " \n", - " return aggregate_stats\n", - "\n", - "# Generate sample DNA sequences for analysis\n", - "def generate_realistic_dna(length, gc_content=0.5):\n", - " \"\"\"Generate realistic DNA sequences with specific GC content\"\"\"\n", - " bases = ['A', 'T', 'G', 'C']\n", - " gc_prob = gc_content / 2\n", - " at_prob = (1 - gc_content) / 2\n", - " probs = [at_prob, at_prob, gc_prob, gc_prob]\n", - " \n", - " return ''.join(np.random.choice(bases, size=length, p=probs))\n", - "\n", - "# Create test sequences\n", - "test_sequences = [\n", - " generate_realistic_dna(5000, 0.4), # AT-rich\n", - " generate_realistic_dna(8000, 0.6), # GC-rich\n", - " generate_realistic_dna(3000, 0.5), # Balanced\n", - " generate_realistic_dna(12000, 0.45), # Large AT-rich\n", - " generate_realistic_dna(6000, 0.55) # Medium GC-rich\n", - "]\n", - "\n", - "# Run analysis on PBS cluster\n", - "bio_results = analyze_dna_sequences(test_sequences, analysis_type=\"comprehensive\")\n", - "\n", - "print(f\"\\nBIOINFORMATICS ANALYSIS COMPLETE\")\n", - "print(f\"Sequences analyzed: {bio_results['total_sequences']}\")\n", - "print(f\"Total base pairs: {bio_results['total_base_pairs']:,}\")\n", - "print(f\"Average GC content: {bio_results['average_gc_content']:.2f}% ± {bio_results['gc_content_std']:.2f}%\")\n", - "print(f\"Total ORFs found: {bio_results['total_orfs_found']}\")\n", - "print(f\"Total CpG sites: {bio_results['total_cpg_sites']}\")\n", - "print(f\"Sequences with tandem repeats: {bio_results['sequences_with_repeats']}/{bio_results['total_sequences']}\")" - ], - "id": "cell-7" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 2: Materials Science - Molecular Dynamics Simulation\n", - "\n", - "Simulate molecular systems commonly done on PBS clusters:" - ], - "id": "cell-8" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(\n", - " cores=16,\n", - " memory=\"64GB\",\n", - " time=\"06:00:00\",\n", - " queue=\"physics\",\n", - " # Site-specific scheduling hints like a high-speed-network request are\n", - " # not exposed as @cluster keyword arguments; if your PBS site needs one,\n", - " # put the qsub-level flag your admins require in pre_execution_commands\n", - " # or ClusterConfig instead.\n", - ")\n", - "def molecular_dynamics_simulation(n_particles=10000, n_steps=100000, temperature=300.0):\n", - " \"\"\"\n", - " Simplified molecular dynamics simulation for materials science.\n", - " \"\"\"\n", - " import numpy as np\n", - " import math\n", - " \n", - " # Physical constants\n", - " kb = 1.380649e-23 # Boltzmann constant (J/K)\n", - " mass = 1.66054e-27 # Approximate atomic mass (kg)\n", - " dt = 1e-15 # Time step (s)\n", - " sigma = 3.4e-10 # Lennard-Jones parameter (m)\n", - " epsilon = 1.65e-21 # Lennard-Jones parameter (J)\n", - " \n", - " print(f\"Starting MD simulation with {n_particles:,} particles for {n_steps:,} steps...\")\n", - " print(f\"Temperature: {temperature} K\")\n", - " \n", - " # Initialize system\n", - " box_size = (n_particles / 0.8) ** (1/3) * sigma # Density ~0.8\n", - " \n", - " # Random initial positions\n", - " positions = np.random.uniform(0, box_size, (n_particles, 3))\n", - " \n", - " # Maxwell-Boltzmann velocity distribution\n", - " velocity_scale = math.sqrt(kb * temperature / mass)\n", - " velocities = np.random.normal(0, velocity_scale, (n_particles, 3))\n", - " \n", - " # Remove center of mass motion\n", - " velocities -= np.mean(velocities, axis=0)\n", - " \n", - " # Storage for analysis\n", - " energies = []\n", - " temperatures = []\n", - " pressures = []\n", - " radial_distribution = []\n", - " \n", - " def lennard_jones_force(r):\n", - " \"\"\"Calculate Lennard-Jones force\"\"\"\n", - " if r < 1e-12: # Avoid division by zero\n", - " return 0\n", - " sr6 = (sigma / r) ** 6\n", - " sr12 = sr6 ** 2\n", - " return 24 * epsilon * (2 * sr12 - sr6) / r\n", - " \n", - " def calculate_forces(pos):\n", - " \"\"\"Calculate forces on all particles\"\"\"\n", - " forces = np.zeros_like(pos)\n", - " potential_energy = 0\n", - " \n", - " for i in range(n_particles):\n", - " for j in range(i + 1, n_particles):\n", - " # Distance vector with periodic boundary conditions\n", - " dr = pos[j] - pos[i]\n", - " dr = dr - box_size * np.round(dr / box_size)\n", - " r = np.linalg.norm(dr)\n", - " \n", - " if r < 2.5 * sigma: # Cutoff distance\n", - " force_magnitude = lennard_jones_force(r)\n", - " force_vector = force_magnitude * dr / r\n", - " \n", - " forces[i] += force_vector\n", - " forces[j] -= force_vector\n", - " \n", - " # Potential energy\n", - " sr6 = (sigma / r) ** 6\n", - " sr12 = sr6 ** 2\n", - " potential_energy += 4 * epsilon * (sr12 - sr6)\n", - " \n", - " return forces, potential_energy\n", - " \n", - " def calculate_temperature(vel):\n", - " \"\"\"Calculate instantaneous temperature\"\"\"\n", - " kinetic_energy = 0.5 * mass * np.sum(vel ** 2)\n", - " return 2 * kinetic_energy / (3 * n_particles * kb)\n", - " \n", - " def calculate_pressure(pos, forces):\n", - " \"\"\"Calculate pressure using virial theorem\"\"\"\n", - " kinetic_term = n_particles * kb * calculate_temperature(velocities)\n", - " virial = np.sum(positions * forces)\n", - " volume = box_size ** 3\n", - " return (kinetic_term + virial/3) / volume\n", - " \n", - " # Main simulation loop\n", - " for step in range(n_steps):\n", - " if step % (n_steps // 10) == 0:\n", - " print(f\"Step {step:,}/{n_steps:,} ({100*step/n_steps:.1f}%)\")\n", - " \n", - " # Calculate forces\n", - " forces, potential_energy = calculate_forces(positions)\n", - " \n", - " # Velocity Verlet integration\n", - " # Update positions\n", - " positions += velocities * dt + 0.5 * forces / mass * dt ** 2\n", - " \n", - " # Apply periodic boundary conditions\n", - " positions = positions % box_size\n", - " \n", - " # Update velocities\n", - " new_forces, _ = calculate_forces(positions)\n", - " velocities += 0.5 * (forces + new_forces) / mass * dt\n", - " \n", - " # Calculate thermodynamic properties\n", - " if step % 1000 == 0: # Sample every 1000 steps\n", - " kinetic_energy = 0.5 * mass * np.sum(velocities ** 2)\n", - " total_energy = kinetic_energy + potential_energy\n", - " temp = calculate_temperature(velocities)\n", - " pressure = calculate_pressure(positions, new_forces)\n", - " \n", - " energies.append({\n", - " 'step': step,\n", - " 'kinetic': kinetic_energy,\n", - " 'potential': potential_energy,\n", - " 'total': total_energy\n", - " })\n", - " temperatures.append(temp)\n", - " pressures.append(pressure)\n", - " \n", - " # Simple thermostat (velocity rescaling)\n", - " if step % 100 == 0: # Apply every 100 steps\n", - " current_temp = calculate_temperature(velocities)\n", - " if current_temp > 0:\n", - " scaling_factor = math.sqrt(temperature / current_temp)\n", - " velocities *= scaling_factor\n", - " \n", - " # Calculate radial distribution function (simplified)\n", - " def calculate_rdf(pos, n_bins=100, max_r=None):\n", - " if max_r is None:\n", - " max_r = box_size / 2\n", - " \n", - " bin_width = max_r / n_bins\n", - " rdf = np.zeros(n_bins)\n", - " \n", - " for i in range(min(1000, n_particles)): # Sample subset for efficiency\n", - " for j in range(i + 1, min(1000, n_particles)):\n", - " dr = pos[j] - pos[i]\n", - " dr = dr - box_size * np.round(dr / box_size)\n", - " r = np.linalg.norm(dr)\n", - " \n", - " if r < max_r:\n", - " bin_index = int(r / bin_width)\n", - " if bin_index < n_bins:\n", - " rdf[bin_index] += 1\n", - " \n", - " # Normalize\n", - " for i in range(n_bins):\n", - " r = (i + 0.5) * bin_width\n", - " volume = 4 * math.pi * r ** 2 * bin_width\n", - " density = n_particles / box_size ** 3\n", - " rdf[i] /= (volume * density * 1000) # 1000 particles sampled\n", - " \n", - " return rdf, np.arange(0.5 * bin_width, max_r, bin_width)\n", - " \n", - " rdf_values, rdf_distances = calculate_rdf(positions)\n", - " \n", - " # Final analysis\n", - " avg_temperature = np.mean(temperatures[-50:]) # Last 50 samples\n", - " avg_pressure = np.mean(pressures[-50:])\n", - " final_energy = energies[-1]['total'] if energies else 0\n", - " \n", - " simulation_results = {\n", - " 'n_particles': n_particles,\n", - " 'n_steps': n_steps,\n", - " 'target_temperature': temperature,\n", - " 'average_temperature': avg_temperature,\n", - " 'temperature_stability': np.std(temperatures[-50:]),\n", - " 'average_pressure': avg_pressure,\n", - " 'final_energy': final_energy,\n", - " 'box_size': box_size,\n", - " 'density': n_particles / box_size ** 3,\n", - " 'energy_trajectory': energies[::10], # Every 10th point\n", - " 'temperature_trajectory': temperatures[::10],\n", - " 'pressure_trajectory': pressures[::10],\n", - " 'radial_distribution': {\n", - " 'distances': rdf_distances.tolist(),\n", - " 'values': rdf_values.tolist()\n", - " },\n", - " 'simulation_time_ns': n_steps * dt * 1e9 # Convert to nanoseconds\n", - " }\n", - " \n", - " return simulation_results\n", - "\n", - "# Run molecular dynamics simulation\n", - "md_results = molecular_dynamics_simulation(\n", - " n_particles=5000, \n", - " n_steps=50000, \n", - " temperature=298.15 # Room temperature\n", - ")\n", - "\n", - "print(f\"\\nMOLECULAR DYNAMICS SIMULATION COMPLETE\")\n", - "print(f\"Particles: {md_results['n_particles']:,}\")\n", - "print(f\"Steps: {md_results['n_steps']:,}\")\n", - "print(f\"Simulation time: {md_results['simulation_time_ns']:.2f} ns\")\n", - "print(f\"Target temperature: {md_results['target_temperature']:.1f} K\")\n", - "print(f\"Average temperature: {md_results['average_temperature']:.1f} K\")\n", - "print(f\"Temperature stability: ±{md_results['temperature_stability']:.1f} K\")\n", - "print(f\"Average pressure: {md_results['average_pressure']:.2e} Pa\")\n", - "print(f\"System density: {md_results['density']:.2e} particles/m³\")" - ], - "id": "cell-9" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 3: Environmental Science - Climate Data Analysis\n", - "\n", - "Analyze large climate datasets commonly processed on research clusters:" - ], - "id": "cell-10" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(\n", - " cores=12,\n", - " memory=\"48GB\",\n", - " time=\"04:00:00\",\n", - " queue=\"climate\",\n", - " parallel=True # Enable automatic parallelization\n", - ")\n", - "def analyze_climate_data(years_to_analyze=50, stations_per_year=1000):\n", - " \"\"\"\n", - " Comprehensive climate data analysis for environmental research.\n", - " \"\"\"\n", - " import numpy as np\n", - " import pandas as pd\n", - " from datetime import datetime, timedelta\n", - " import random\n", - " from scipy import stats\n", - " import math\n", - " \n", - " def generate_realistic_climate_data(year, station_id, latitude, longitude):\n", - " \"\"\"Generate realistic climate data for a station\"\"\"\n", - " np.random.seed(year * 1000 + station_id) # Reproducible but varied\n", - " \n", - " # Base temperature influenced by latitude\n", - " base_temp = 25 - abs(latitude) * 0.6 # Cooler at higher latitudes\n", - " \n", - " # Generate daily data for the year\n", - " start_date = datetime(year, 1, 1)\n", - " days_in_year = 366 if year % 4 == 0 else 365\n", - " \n", - " daily_data = []\n", - " \n", - " for day in range(days_in_year):\n", - " date = start_date + timedelta(days=day)\n", - " day_of_year = day + 1\n", - " \n", - " # Seasonal temperature variation\n", - " seasonal_temp = base_temp + 15 * math.cos(2 * math.pi * (day_of_year - 172) / 365)\n", - " \n", - " # Add random variation and trends\n", - " climate_trend = 0.01 * (year - 1970) # 0.01°C/year warming\n", - " daily_temp = seasonal_temp + climate_trend + np.random.normal(0, 3)\n", - " \n", - " # Precipitation (higher in tropics and certain seasons)\n", - " base_precip = max(0, 10 - abs(latitude) * 0.3)\n", - " seasonal_precip_factor = 1 + 0.5 * math.cos(2 * math.pi * (day_of_year - 30) / 365)\n", - " daily_precip = max(0, np.random.exponential(base_precip * seasonal_precip_factor))\n", - " \n", - " # Humidity (correlated with temperature and precipitation)\n", - " base_humidity = 60 - abs(latitude) * 0.5\n", - " humidity = base_humidity + daily_precip * 0.5 - (daily_temp - base_temp) * 0.3\n", - " humidity = max(10, min(100, humidity + np.random.normal(0, 5)))\n", - " \n", - " # Wind speed (more variable at higher latitudes)\n", - " base_wind = 5 + abs(latitude) * 0.1\n", - " wind_speed = max(0, np.random.gamma(2, base_wind / 2))\n", - " \n", - " # Atmospheric pressure (altitude and weather dependent)\n", - " base_pressure = 1013.25 # Sea level\n", - " pressure = base_pressure + np.random.normal(0, 10)\n", - " \n", - " daily_data.append({\n", - " 'date': date,\n", - " 'temperature': daily_temp,\n", - " 'precipitation': daily_precip,\n", - " 'humidity': humidity,\n", - " 'wind_speed': wind_speed,\n", - " 'pressure': pressure\n", - " })\n", - " \n", - " return daily_data\n", - " \n", - " def analyze_station_trends(station_data):\n", - " \"\"\"Analyze trends for a single weather station\"\"\"\n", - " df = pd.DataFrame(station_data)\n", - " \n", - " # Calculate annual statistics\n", - " annual_stats = {\n", - " 'mean_temperature': df['temperature'].mean(),\n", - " 'temperature_range': df['temperature'].max() - df['temperature'].min(),\n", - " 'total_precipitation': df['precipitation'].sum(),\n", - " 'mean_humidity': df['humidity'].mean(),\n", - " 'mean_wind_speed': df['wind_speed'].mean(),\n", - " 'mean_pressure': df['pressure'].mean(),\n", - " 'temperature_std': df['temperature'].std(),\n", - " 'precipitation_days': (df['precipitation'] > 1.0).sum(),\n", - " 'extreme_heat_days': (df['temperature'] > df['temperature'].quantile(0.95)).sum(),\n", - " 'extreme_cold_days': (df['temperature'] < df['temperature'].quantile(0.05)).sum()\n", - " }\n", - " \n", - " # Seasonal analysis\n", - " df['month'] = df['date'].dt.month\n", - " seasonal_temps = df.groupby(df['month'])['temperature'].mean()\n", - " seasonal_precip = df.groupby(df['month'])['precipitation'].sum()\n", - " \n", - " annual_stats['seasonal_temperature_variation'] = seasonal_temps.std()\n", - " annual_stats['wettest_month'] = seasonal_precip.idxmax()\n", - " annual_stats['driest_month'] = seasonal_precip.idxmin()\n", - " \n", - " return annual_stats\n", - " \n", - " print(f\"Analyzing climate data for {years_to_analyze} years, {stations_per_year} stations per year...\")\n", - " print(f\"Total data points: {years_to_analyze * stations_per_year * 365:,}\")\n", - " \n", - " all_station_results = []\n", - " \n", - " # Sequential: auto-parallelization needs a literal range() and a callee\n # that accepts the chunk keywords. See the Limitations page.\n", - " for year in range(1970, 1970 + years_to_analyze):\n", - " print(f\"Processing year {year}...\")\n", - " \n", - " year_results = []\n", - " \n", - " for station_id in range(stations_per_year):\n", - " # Generate random station location\n", - " latitude = np.random.uniform(-60, 75) # Inhabitable latitudes\n", - " longitude = np.random.uniform(-180, 180)\n", - " \n", - " # Generate climate data for this station and year\n", - " station_data = generate_realistic_climate_data(year, station_id, latitude, longitude)\n", - " \n", - " # Analyze the station data\n", - " station_analysis = analyze_station_trends(station_data)\n", - " station_analysis['year'] = year\n", - " station_analysis['station_id'] = station_id\n", - " station_analysis['latitude'] = latitude\n", - " station_analysis['longitude'] = longitude\n", - " \n", - " year_results.append(station_analysis)\n", - " \n", - " all_station_results.extend(year_results)\n", - " \n", - " # Convert to DataFrame for analysis\n", - " results_df = pd.DataFrame(all_station_results)\n", - " \n", - " # Global trend analysis\n", - " yearly_global_temps = results_df.groupby('year')['mean_temperature'].mean()\n", - " yearly_global_precip = results_df.groupby('year')['total_precipitation'].mean()\n", - " \n", - " # Calculate trends\n", - " years = yearly_global_temps.index\n", - " temp_trend, temp_intercept, temp_r_value, temp_p_value, temp_std_err = stats.linregress(years, yearly_global_temps)\n", - " precip_trend, precip_intercept, precip_r_value, precip_p_value, precip_std_err = stats.linregress(years, yearly_global_precip)\n", - " \n", - " # Regional analysis\n", - " def classify_climate_zone(lat):\n", - " if abs(lat) < 23.5:\n", - " return \"Tropical\"\n", - " elif abs(lat) < 35:\n", - " return \"Subtropical\"\n", - " elif abs(lat) < 50:\n", - " return \"Temperate\"\n", - " else:\n", - " return \"Polar\"\n", - " \n", - " results_df['climate_zone'] = results_df['latitude'].apply(classify_climate_zone)\n", - " zone_analysis = results_df.groupby('climate_zone').agg({\n", - " 'mean_temperature': ['mean', 'std'],\n", - " 'total_precipitation': ['mean', 'std'],\n", - " 'temperature_range': 'mean',\n", - " 'extreme_heat_days': 'mean',\n", - " 'extreme_cold_days': 'mean'\n", - " }).round(2)\n", - " \n", - " # Extreme events analysis\n", - " extreme_heat_threshold = results_df['mean_temperature'].quantile(0.95)\n", - " extreme_cold_threshold = results_df['mean_temperature'].quantile(0.05)\n", - " drought_threshold = results_df['total_precipitation'].quantile(0.1)\n", - " flood_threshold = results_df['total_precipitation'].quantile(0.9)\n", - " \n", - " extreme_events = {\n", - " 'extreme_heat_stations': (results_df['mean_temperature'] > extreme_heat_threshold).sum(),\n", - " 'extreme_cold_stations': (results_df['mean_temperature'] < extreme_cold_threshold).sum(),\n", - " 'drought_affected_stations': (results_df['total_precipitation'] < drought_threshold).sum(),\n", - " 'flood_risk_stations': (results_df['total_precipitation'] > flood_threshold).sum()\n", - " }\n", - " \n", - " # Compile final results\n", - " climate_analysis = {\n", - " 'analysis_summary': {\n", - " 'years_analyzed': years_to_analyze,\n", - " 'stations_per_year': stations_per_year,\n", - " 'total_station_years': len(results_df),\n", - " 'data_points_analyzed': len(results_df) * 365\n", - " },\n", - " 'global_trends': {\n", - " 'temperature_trend_per_decade': temp_trend * 10,\n", - " 'temperature_trend_significance': temp_p_value,\n", - " 'temperature_correlation': temp_r_value ** 2,\n", - " 'precipitation_trend_per_decade': precip_trend * 10,\n", - " 'precipitation_trend_significance': precip_p_value,\n", - " 'precipitation_correlation': precip_r_value ** 2\n", - " },\n", - " 'current_climate_state': {\n", - " 'global_mean_temperature': yearly_global_temps.iloc[-1],\n", - " 'global_mean_precipitation': yearly_global_precip.iloc[-1],\n", - " 'temperature_warming_since_start': yearly_global_temps.iloc[-1] - yearly_global_temps.iloc[0],\n", - " 'precipitation_change_since_start': yearly_global_precip.iloc[-1] - yearly_global_precip.iloc[0]\n", - " },\n", - " 'regional_analysis': zone_analysis.to_dict(),\n", - " 'extreme_events': extreme_events,\n", - " 'statistical_summary': {\n", - " 'mean_global_temperature': results_df['mean_temperature'].mean(),\n", - " 'temperature_standard_deviation': results_df['mean_temperature'].std(),\n", - " 'mean_global_precipitation': results_df['total_precipitation'].mean(),\n", - " 'precipitation_standard_deviation': results_df['total_precipitation'].std(),\n", - " 'warmest_station_temp': results_df['mean_temperature'].max(),\n", - " 'coldest_station_temp': results_df['mean_temperature'].min(),\n", - " 'wettest_station_precip': results_df['total_precipitation'].max(),\n", - " 'driest_station_precip': results_df['total_precipitation'].min()\n", - " }\n", - " }\n", - " \n", - " return climate_analysis\n", - "\n", - "# Run climate analysis\n", - "climate_results = analyze_climate_data(years_to_analyze=30, stations_per_year=200)\n", - "\n", - "print(f\"\\nCLIMATE DATA ANALYSIS COMPLETE\")\n", - "print(f\"Years analyzed: {climate_results['analysis_summary']['years_analyzed']}\")\n", - "print(f\"Total station-years: {climate_results['analysis_summary']['total_station_years']:,}\")\n", - "print(f\"Data points: {climate_results['analysis_summary']['data_points_analyzed']:,}\")\n", - "\n", - "print(\"\\nGlobal Trends:\")\n", - "trends = climate_results['global_trends']\n", - "print(f\" Temperature trend: {trends['temperature_trend_per_decade']:.3f}°C per decade (p={trends['temperature_trend_significance']:.4f})\")\n", - "print(f\" Precipitation trend: {trends['precipitation_trend_per_decade']:.1f} mm per decade (p={trends['precipitation_trend_significance']:.4f})\")\n", - "\n", - "print(\"\\nCurrent Climate State:\")\n", - "current = climate_results['current_climate_state']\n", - "print(f\" Global mean temperature: {current['global_mean_temperature']:.2f}°C\")\n", - "print(f\" Temperature change since start: {current['temperature_warming_since_start']:.2f}°C\")\n", - "print(f\" Global mean precipitation: {current['global_mean_precipitation']:.1f} mm/year\")\n", - "\n", - "print(\"\\nExtreme Events:\")\n", - "extremes = climate_results['extreme_events']\n", - "total_stations = climate_results['analysis_summary']['total_station_years']\n", - "print(f\" Extreme heat affected: {extremes['extreme_heat_stations']} stations ({100*extremes['extreme_heat_stations']/total_stations:.1f}%)\")\n", - "print(f\" Drought affected: {extremes['drought_affected_stations']} stations ({100*extremes['drought_affected_stations']/total_stations:.1f}%)\")\n", - "print(f\" Flood risk: {extremes['flood_risk_stations']} stations ({100*extremes['flood_risk_stations']/total_stations:.1f}%)\")" - ], - "id": "cell-11" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## PBS Queue Management and Resource Selection\n", - "\n", - "Understanding how to choose appropriate PBS queues and resources:" - ], - "id": "cell-12" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def select_pbs_resources(workload_type, data_size_mb, urgency=\"normal\"):\n", - " \"\"\"\n", - " Intelligent PBS resource selection based on workload characteristics.\n", - " \"\"\"\n", - " \n", - " # Base resource templates\n", - " resource_templates = {\n", - " \"bioinformatics\": {\n", - " \"small\": {\"cores\": 4, \"memory\": \"16GB\", \"time\": \"02:00:00\", \"queue\": \"bioqueue\"},\n", - " \"medium\": {\"cores\": 8, \"memory\": \"32GB\", \"time\": \"06:00:00\", \"queue\": \"bioqueue\"},\n", - " \"large\": {\"cores\": 16, \"memory\": \"64GB\", \"time\": \"12:00:00\", \"queue\": \"bioqueue_long\"}\n", - " },\n", - " \"physics\": {\n", - " \"small\": {\"cores\": 8, \"memory\": \"32GB\", \"time\": \"04:00:00\", \"queue\": \"physics\"},\n", - " \"medium\": {\"cores\": 16, \"memory\": \"64GB\", \"time\": \"12:00:00\", \"queue\": \"physics\"},\n", - " \"large\": {\"cores\": 32, \"memory\": \"128GB\", \"time\": \"24:00:00\", \"queue\": \"physics_long\"}\n", - " },\n", - " \"climate\": {\n", - " \"small\": {\"cores\": 6, \"memory\": \"24GB\", \"time\": \"03:00:00\", \"queue\": \"climate\"},\n", - " \"medium\": {\"cores\": 12, \"memory\": \"48GB\", \"time\": \"08:00:00\", \"queue\": \"climate\"},\n", - " \"large\": {\"cores\": 24, \"memory\": \"96GB\", \"time\": \"16:00:00\", \"queue\": \"climate_long\"}\n", - " },\n", - " \"ml\": {\n", - " \"small\": {\"cores\": 4, \"memory\": \"16GB\", \"time\": \"01:00:00\", \"queue\": \"gpu\", \"gres\": \"gpu:1\"},\n", - " \"medium\": {\"cores\": 8, \"memory\": \"32GB\", \"time\": \"04:00:00\", \"queue\": \"gpu\", \"gres\": \"gpu:2\"},\n", - " \"large\": {\"cores\": 16, \"memory\": \"64GB\", \"time\": \"12:00:00\", \"queue\": \"gpu_long\", \"gres\": \"gpu:4\"}\n", - " }\n", - " }\n", - " \n", - " # Determine size category based on data\n", - " if data_size_mb < 100:\n", - " size_category = \"small\"\n", - " elif data_size_mb < 1000:\n", - " size_category = \"medium\"\n", - " else:\n", - " size_category = \"large\"\n", - " \n", - " # Get base configuration\n", - " if workload_type not in resource_templates:\n", - " workload_type = \"physics\" # Default fallback\n", - " \n", - " config = resource_templates[workload_type][size_category].copy()\n", - " \n", - " # Adjust for urgency\n", - " if urgency == \"urgent\":\n", - " # Use express queue with reduced resources\n", - " config[\"queue\"] = \"express\"\n", - " config[\"time\"] = \"00:30:00\"\n", - " config[\"cores\"] = min(4, config[\"cores\"])\n", - " elif urgency == \"low\":\n", - " # Use long queue with more resources\n", - " config[\"queue\"] = config[\"queue\"].replace(\"queue\", \"queue_long\")\n", - " config[\"cores\"] = int(config[\"cores\"] * 1.5)\n", - " # Increase time limit\n", - " time_parts = config[\"time\"].split(\":\")\n", - " hours = int(time_parts[0]) * 2\n", - " config[\"time\"] = f\"{hours:02d}:{time_parts[1]}:{time_parts[2]}\"\n", - " \n", - " return config\n", - "\n", - "# Example resource selections\n", - "example_workloads = [\n", - " (\"bioinformatics\", 500, \"normal\"),\n", - " (\"physics\", 2000, \"low\"),\n", - " (\"climate\", 150, \"urgent\"),\n", - " (\"ml\", 800, \"normal\")\n", - "]\n", - "\n", - "print(\"PBS Resource Selection Examples:\")\n", - "print(\"=\" * 70)\n", - "\n", - "for workload, data_size, urgency in example_workloads:\n", - " resources = select_pbs_resources(workload, data_size, urgency)\n", - " print(f\"\\n{workload.upper()} ({data_size} MB, {urgency} priority):\")\n", - " for key, value in resources.items():\n", - " print(f\" {key}: {value}\")" - ], - "id": "cell-13" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Parameter Studies: No Native PBS Job Arrays\n", - "\n", - "**Clustrix does not support PBS job arrays** (`qsub -t` / `#PBS -J`). The\n", - "`@cluster` decorator's PBS-relevant resource arguments are exactly `cores`,\n", - "`memory`, `time` and `queue` -- a keyword argument named `pbs_array` (or\n", - "anything else) is accepted by Python but never turned into a PBS array\n", - "directive. Worse, the original version of the cell below read\n", - "`PBS_ARRAYID` from the environment with a hardcoded fallback of `'1'` --\n", - "since clustrix never submits a real PBS array and never sets that variable,\n", - "every submission would silently evaluate task 1 only, no matter how many\n", - "times you called it, which is a much easier mistake to miss than an\n", - "outright error.\n", - "\n", - "The fixed version below takes `array_index` as an explicit function\n", - "argument and drives the sweep from Python. `@cluster(..., async_submit=True)`\n", - "is set on the decorator itself -- `async_submit` cannot be overridden per\n", - "call -- so every submission returns an `AsyncJobResult` immediately and\n", - "the 20 jobs overlap instead of running one at a time; `.wait()` then\n", - "blocks for each result in turn. Same workaround used for SLURM job arrays\n", - "earlier in this tutorial series.\n" - ], - "id": "cell-14" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(\n", - " cores=4,\n", - " memory=\"16GB\",\n", - " time=\"01:00:00\",\n", - " queue=\"normal\",\n", - " async_submit=True, # decorator-time only: cannot be overridden per call\n", - ")\n", - "def drug_discovery_parameter_sweep(base_config, array_index):\n", - " \"\"\"\n", - " Pharmaceutical research parameter sweep -- one task's worth of work.\n", - "\n", - " ``array_index`` is passed in explicitly by the driver loop below,\n", - " because clustrix has no PBS job-array support to set it for us.\n", - " \"\"\"\n", - " import numpy as np\n", - " import random\n", - " from math import exp, log\n", - " \n", - " # Define parameter space for drug discovery\n", - " molecular_weights = np.linspace(150, 500, 20) # Typical drug MW range\n", - " logp_values = np.linspace(-1, 5, 20) # Lipophilicity\n", - " hbd_counts = list(range(0, 6)) # Hydrogen bond donors\n", - " hba_counts = list(range(0, 11)) # Hydrogen bond acceptors\n", - " \n", - " # Select parameters for this array task\n", - " mw = molecular_weights[array_index - 1]\n", - " logp = logp_values[array_index - 1]\n", - " \n", - " # Random selection for other parameters\n", - " np.random.seed(array_index * 42)\n", - " hbd = random.choice(hbd_counts)\n", - " hba = random.choice(hba_counts)\n", - " \n", - " print(f\"Array task {array_index}: MW={mw:.1f}, LogP={logp:.2f}, HBD={hbd}, HBA={hba}\")\n", - " \n", - " def calculate_drug_likeness(mw, logp, hbd, hba):\n", - " \"\"\"Calculate drug-likeness using Lipinski's Rule of Five\"\"\"\n", - " violations = 0\n", - " \n", - " if mw > 500:\n", - " violations += 1\n", - " if logp > 5:\n", - " violations += 1\n", - " if hbd > 5:\n", - " violations += 1\n", - " if hba > 10:\n", - " violations += 1\n", - " \n", - " drug_likeness = max(0, 1.0 - violations * 0.25)\n", - " return drug_likeness, violations\n", - " \n", - " def simulate_binding_affinity(mw, logp, hbd, hba):\n", - " \"\"\"Simulate binding affinity to target protein\"\"\"\n", - " # Simplified model based on molecular properties\n", - " optimal_mw = 350\n", - " optimal_logp = 2.5\n", - " optimal_hbd = 2\n", - " optimal_hba = 6\n", - " \n", - " mw_score = exp(-((mw - optimal_mw) / 100) ** 2)\n", - " logp_score = exp(-((logp - optimal_logp) / 1.5) ** 2)\n", - " hbd_score = exp(-((hbd - optimal_hbd) / 1.5) ** 2)\n", - " hba_score = exp(-((hba - optimal_hba) / 2.5) ** 2)\n", - " \n", - " # Combine scores with some randomness\n", - " base_affinity = (mw_score * logp_score * hbd_score * hba_score) ** 0.5\n", - " random_factor = np.random.uniform(0.7, 1.3)\n", - " \n", - " binding_affinity = base_affinity * random_factor\n", - " ic50 = 10 ** (-6 - 3 * binding_affinity) # Convert to IC50 (M)\n", - " \n", - " return binding_affinity, ic50\n", - " \n", - " def simulate_admet_properties(mw, logp, hbd, hba):\n", - " \"\"\"Simulate ADMET (Absorption, Distribution, Metabolism, Excretion, Toxicity)\"\"\"\n", - " # Absorption (permeability)\n", - " permeability = 1 / (1 + exp(-(logp - 1.5)))\n", - " permeability *= np.random.uniform(0.8, 1.2)\n", - " \n", - " # Distribution (plasma protein binding)\n", - " ppb = min(99, max(10, 20 + logp * 15 + np.random.normal(0, 10)))\n", - " \n", - " # Metabolism (hepatic clearance)\n", - " clearance = 0.5 + 0.3 * (1 / (1 + exp(-(mw - 300) / 50)))\n", - " clearance *= np.random.uniform(0.7, 1.3)\n", - " \n", - " # Excretion (renal clearance)\n", - " renal_clearance = max(0.1, 0.8 - logp * 0.1 + np.random.normal(0, 0.1))\n", - " \n", - " # Toxicity (simplified hERG channel binding)\n", - " herg_risk = 1 / (1 + exp(-(logp - 3.5)))\n", - " if mw > 400:\n", - " herg_risk *= 1.5\n", - " \n", - " return {\n", - " 'permeability': permeability,\n", - " 'plasma_protein_binding': ppb,\n", - " 'hepatic_clearance': clearance,\n", - " 'renal_clearance': renal_clearance,\n", - " 'herg_risk': herg_risk\n", - " }\n", - " \n", - " def calculate_developability_score(drug_likeness, binding_affinity, admet):\n", - " \"\"\"Calculate overall drug developability score\"\"\"\n", - " # Weight different factors\n", - " likeness_weight = 0.2\n", - " affinity_weight = 0.4\n", - " admet_weight = 0.4\n", - " \n", - " # ADMET composite score\n", - " admet_score = (\n", - " admet['permeability'] * 0.3 +\n", - " (1 - admet['herg_risk']) * 0.3 +\n", - " (1 - admet['hepatic_clearance']) * 0.2 +\n", - " admet['renal_clearance'] * 0.2\n", - " )\n", - " \n", - " total_score = (\n", - " drug_likeness * likeness_weight +\n", - " binding_affinity * affinity_weight +\n", - " admet_score * admet_weight\n", - " )\n", - " \n", - " return total_score, admet_score\n", - " \n", - " # Run simulations\n", - " drug_likeness, ro5_violations = calculate_drug_likeness(mw, logp, hbd, hba)\n", - " binding_affinity, ic50 = simulate_binding_affinity(mw, logp, hbd, hba)\n", - " admet_props = simulate_admet_properties(mw, logp, hbd, hba)\n", - " developability_score, admet_score = calculate_developability_score(\n", - " drug_likeness, binding_affinity, admet_props\n", - " )\n", - " \n", - " # Compile results\n", - " compound_results = {\n", - " 'array_task_id': array_index,\n", - " 'molecular_properties': {\n", - " 'molecular_weight': mw,\n", - " 'logp': logp,\n", - " 'hbd_count': hbd,\n", - " 'hba_count': hba\n", - " },\n", - " 'drug_likeness': {\n", - " 'score': drug_likeness,\n", - " 'ro5_violations': ro5_violations,\n", - " 'passes_ro5': ro5_violations <= 1\n", - " },\n", - " 'target_binding': {\n", - " 'affinity_score': binding_affinity,\n", - " 'ic50_M': ic50,\n", - " 'pic50': -log(ic50, 10) if ic50 > 0 else 0\n", - " },\n", - " 'admet_properties': admet_props,\n", - " 'overall_assessment': {\n", - " 'developability_score': developability_score,\n", - " 'admet_score': admet_score,\n", - " 'promising_candidate': developability_score > 0.6 and binding_affinity > 0.5\n", - " }\n", - " }\n", - " \n", - " return compound_results\n", - "\n", - "# Drive the \"array\" from Python: 20 separate job submissions, submitted\n", - "# without waiting for each to finish, then collected.\n", - "drug_config = {\n", - " 'target_name': 'EGFR',\n", - " 'assay_type': 'binding',\n", - " 'screening_library': 'chembl'\n", - "}\n", - "\n", - "pending = [\n", - " drug_discovery_parameter_sweep(drug_config, array_index=i)\n", - " for i in range(1, 21)\n", - "]\n", - "drug_results = [job.wait() for job in pending]\n", - "\n", - "best = max(drug_results, key=lambda r: r['overall_assessment']['developability_score'])\n", - "print(f\"Ran {len(drug_results)} parameter-sweep tasks.\")\n", - "print(f\"\\nBest candidate -- Task {best['array_task_id']}\")\n", - "print(\"=\" * 60)\n", - "\n", - "mol_props = best['molecular_properties']\n", - "print(f\"Molecular Weight: {mol_props['molecular_weight']:.1f} Da\")\n", - "print(f\"LogP: {mol_props['logp']:.2f}\")\n", - "print(f\"H-bond donors: {mol_props['hbd_count']}\")\n", - "print(f\"H-bond acceptors: {mol_props['hba_count']}\")\n", - "\n", - "drug_like = best['drug_likeness']\n", - "print(f\"\\nDrug-likeness score: {drug_like['score']:.3f}\")\n", - "print(f\"Rule of 5 violations: {drug_like['ro5_violations']}\")\n", - "print(f\"Passes Lipinski's Rule: {drug_like['passes_ro5']}\")\n", - "\n", - "binding = best['target_binding']\n", - "print(f\"\\nBinding affinity score: {binding['affinity_score']:.3f}\")\n", - "print(f\"IC50: {binding['ic50_M']:.2e} M\")\n", - "print(f\"pIC50: {binding['pic50']:.2f}\")\n", - "\n", - "assessment = best['overall_assessment']\n", - "print(f\"\\nDevelopability score: {assessment['developability_score']:.3f}\")\n", - "print(f\"ADMET score: {assessment['admet_score']:.3f}\")\n", - "print(f\"Promising candidate: {assessment['promising_candidate']}\")" - ], - "id": "cell-15" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Monitoring PBS Jobs\n", - "\n", - "Monitor and manage PBS jobs using Clustrix:" - ], - "id": "cell-16" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from clustrix import ClusterExecutor\n", - "\n", - "# Get the configured executor\n", - "config = clustrix.get_config()\n", - "executor = ClusterExecutor(config)\n", - "\n", - "try:\n", - " executor.connect()\n", - " print(\"✓ Successfully connected to PBS cluster\")\n", - " \n", - " # Check PBS version\n", - " stdout, stderr = executor._execute_command(\"qstat --version\")\n", - " if stdout:\n", - " print(f\"✓ PBS version: {stdout.strip()}\")\n", - " \n", - " # Check available queues\n", - " stdout, stderr = executor._execute_command(\"qstat -Q\")\n", - " if stdout:\n", - " print(\"\\nAvailable queues:\")\n", - " lines = stdout.strip().split('\\n')\n", - " for line in lines[2:7]: # Skip header, show first 5 queues\n", - " parts = line.split()\n", - " if len(parts) >= 3:\n", - " queue_name = parts[0]\n", - " max_jobs = parts[1] if parts[1] != '--' else 'unlimited'\n", - " total_jobs = parts[2]\n", - " print(f\" {queue_name}: {total_jobs} jobs, max: {max_jobs}\")\n", - " \n", - " # Check node status\n", - " stdout, stderr = executor._execute_command(\"pbsnodes -a | grep -E '^(\\w+|\\s+state)' | head -20\")\n", - " if stdout:\n", - " print(\"\\nNode status (sample):\")\n", - " lines = stdout.strip().split('\\n')\n", - " current_node = None\n", - " for line in lines[:10]: # Show first few nodes\n", - " if not line.startswith(' '):\n", - " current_node = line.strip()\n", - " elif 'state' in line:\n", - " state = line.split('=')[1].strip() if '=' in line else 'unknown'\n", - " print(f\" {current_node}: {state}\")\n", - " \n", - " # Check user's job status\n", - " username = config.username\n", - " stdout, stderr = executor._execute_command(f\"qstat -u {username}\")\n", - " if stdout and len(stdout.strip().split('\\n')) > 2:\n", - " print(f\"\\nYour current jobs:\")\n", - " lines = stdout.strip().split('\\n')\n", - " for line in lines[2:]: # Skip headers\n", - " print(f\" {line}\")\n", - " else:\n", - " print(f\"\\n✓ No jobs currently running for user {username}\")\n", - " \n", - " executor.disconnect()\n", - " print(\"\\n✓ PBS cluster monitoring completed successfully\")\n", - " \n", - "except Exception as e:\n", - " print(f\"✗ Connection or monitoring failed: {e}\")\n", - " print(\"Please check your PBS cluster configuration and connectivity\")" - ], - "id": "cell-17" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## PBS Configuration Best Practices\n", - "\n", - "### Environment-Specific Configuration Files\n", - "\n", - "Create different configurations for different PBS environments:" - ], - "id": "cell-18" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Create PBS configuration for different research domains\n", - "\n", - "bioinformatics_config = {\n", - " 'cluster_type': 'pbs',\n", - " 'cluster_host': 'bio-cluster.university.edu',\n", - " 'username': 'researcher',\n", - " 'default_queue': 'bioqueue',\n", - " 'default_cores': 8,\n", - " 'default_memory': '32GB',\n", - " 'default_time': '06:00:00',\n", - " 'module_loads': ['python/3.9', 'blast/2.12', 'hmmer/3.3'],\n", - " 'remote_work_dir': '/scratch/bio/clustrix',\n", - " 'max_parallel_jobs': 20\n", - "}\n", - "\n", - "physics_config = {\n", - " 'cluster_type': 'pbs',\n", - " 'cluster_host': 'physics-hpc.university.edu',\n", - " 'username': 'physicist',\n", - " 'default_queue': 'physics',\n", - " 'default_cores': 16,\n", - " 'default_memory': '64GB',\n", - " 'default_time': '12:00:00',\n", - " 'module_loads': ['python/3.9', 'openmpi/4.1', 'fftw/3.3'],\n", - " 'remote_work_dir': '/home/physicist/clustrix',\n", - " 'features': 'infiniband', # Request high-speed interconnect\n", - " 'max_parallel_jobs': 10\n", - "}\n", - "\n", - "climate_config = {\n", - " 'cluster_type': 'pbs',\n", - " 'cluster_host': 'climate-compute.noaa.gov',\n", - " 'username': 'climatologist',\n", - " 'default_queue': 'climate',\n", - " 'default_cores': 12,\n", - " 'default_memory': '48GB',\n", - " 'default_time': '08:00:00',\n", - " 'module_loads': ['python/3.9', 'netcdf/4.8', 'gdal/3.4'],\n", - " 'remote_work_dir': '/data/climate/clustrix',\n", - " 'max_parallel_jobs': 15\n", - "}\n", - "\n", - "# Example of selecting configuration based on research domain\n", - "def configure_for_domain(domain):\n", - " configs = {\n", - " 'bioinformatics': bioinformatics_config,\n", - " 'physics': physics_config,\n", - " 'climate': climate_config\n", - " }\n", - " \n", - " if domain in configs:\n", - " clustrix.configure(**configs[domain])\n", - " print(f\"Configured Clustrix for {domain} research\")\n", - " return configs[domain]\n", - " else:\n", - " print(f\"Unknown domain: {domain}. Available: {list(configs.keys())}\")\n", - " return None\n", - "\n", - "# Configure for bioinformatics research\n", - "selected_config = configure_for_domain('bioinformatics')\n", - "if selected_config:\n", - " print(\"\\nConfiguration details:\")\n", - " for key, value in selected_config.items():\n", - " print(f\" {key}: {value}\")" - ], - "id": "cell-19" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "This tutorial covered PBS/Torque cluster usage with Clustrix:\n", - "\n", - "1. **PBS Configuration** - Setting up Clustrix for PBS clusters\n", - "2. **Bioinformatics Applications** - DNA sequence analysis and genomics\n", - "3. **Materials Science** - Molecular dynamics simulations\n", - "4. **Climate Research** - Large-scale environmental data analysis\n", - "5. **Drug Discovery** - Pharmaceutical parameter sweeps (driven from Python, since clustrix has no PBS job-array support)\n", - "6. **Resource Management** - Intelligent queue and resource selection\n", - "7. **Job Monitoring** - PBS cluster status and job management\n", - "8. **Best Practices** - Domain-specific configurations\n", - "\n", - "### Key PBS Features (and What Clustrix Actually Supports):\n", - "\n", - "- **Resource Specification**: `cores`, `memory`, `time` and `queue` are the\n", - " complete set of PBS-relevant `@cluster` keyword arguments -- they map to\n", - " `-l nodes=1:ppn=N`, `-l mem=gb`, `-l walltime=...` and `-q `.\n", - "- **Job Arrays and hardware-feature requests are PBS concepts, not\n", - " clustrix ones**: `pbs_array`, `walltime`, `features` and similar\n", - " keyword arguments are accepted but silently dropped. Drive parameter\n", - " sweeps from a Python loop instead (see Example 3 above), and put any\n", - " required site-specific `-l`/`-W` flag in `pre_execution_commands`.\n", - "- **Module Loading**: Automatic environment setup via `module_loads`.\n", - "\n", - "### Next Steps:\n", - "\n", - "- Explore [SLURM Tutorial](slurm_tutorial.ipynb) for SLURM-specific features\n", - "- Try [Kubernetes Tutorial](kubernetes_tutorial.ipynb) for containerized computing\n", - "- Review [SGE Tutorial](sge_tutorial.ipynb) for Sun Grid Engine clusters\n", - "- Check the [SSH Setup Guide](../ssh_setup.rst) for secure authentication\n", - "\n", - "For more information, visit the [Clustrix Documentation](https://clustrix.readthedocs.io)." - ], - "id": "cell-20" - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/source/notebooks/sge_tutorial.ipynb b/docs/source/notebooks/sge_tutorial.ipynb deleted file mode 100644 index ca38de14..00000000 --- a/docs/source/notebooks/sge_tutorial.ipynb +++ /dev/null @@ -1,1202 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# SGE (Sun Grid Engine) Tutorial\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/sge_tutorial.ipynb)\n", - "\n", - "This tutorial demonstrates how to use Clustrix with SGE (Sun Grid Engine) clusters, including Open Grid Scheduler and other SGE-compatible systems.\n", - "\n", - "## Prerequisites\n", - "\n", - "- Access to an SGE cluster\n", - "- SSH key configured for the cluster\n", - "- Clustrix installed: `pip install clustrix`" - ], - "id": "cell-0" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **SGE is unverified against real hardware.** It shares its job-directory\n", - "> staging, environment build and job-execution code with the SLURM and SSH\n", - "> backends (which *are* verified end to end) -- it is not a separate,\n", - "> untested code path -- but nobody has run this backend against a live SGE\n", - "> / Open Grid Scheduler installation. Treat this notebook as a description\n", - "> of the intended interface, not a record of something that has been\n", - "> executed to completion.\n", - "\n", - "## What Clustrix Does Behind the Scenes\n", - "\n", - "The submission pipeline is the same ten-step sequence as SLURM (serialize\n", - "with `dill`, connect over SSH with host-key verification, stage a `0700`\n", - "job directory with a random result-signing key, upload\n", - "`function_data.pkl`, build a two-venv environment, generate and upload the\n", - "job script, submit, poll, verify-then-deserialize the HMAC-signed result,\n", - "clean up) -- see the online docs' SLURM tutorial page for the full\n", - "walkthrough. The SGE-specific differences: submission is `qsub job.sge`,\n", - "the job ID is parsed out of `qsub`'s \"Your job ...\" message, and the\n", - "script uses `#$` directives (`-pe smp N`, `-l h_vmem=G`, `-l\n", - "h_rt=...`, `-cwd`) built only from `cores`, `memory`, `time` and `queue`.\n", - "**Any other keyword argument passed to `@cluster(...)` -- `pe=`,\n", - "`sge_array=`, or anything else SGE-specific -- is accepted by Python but\n", - "never written into the job script.** Several cells further down in this\n", - "notebook demonstrate that pitfall directly.\n" - ], - "id": "cell-1" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Install Clustrix (uncomment if needed)\n", - "# !pip install clustrix\n", - "\n", - "import clustrix\n", - "from clustrix import cluster, configure\n", - "import numpy as np" - ], - "id": "cell-2" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SGE Configuration\n", - "\n", - "Configure Clustrix for your SGE cluster:" - ], - "id": "cell-3" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Configure for SGE cluster\n", - "configure(\n", - " cluster_type=\"sge\",\n", - " cluster_host=\"sge-cluster.org\", # Replace with your cluster\n", - " username=\"your-username\", # Replace with your username\n", - " key_file=\"~/.ssh/id_rsa\", # SSH key path\n", - " \n", - " # SGE resource defaults\n", - " default_cores=4,\n", - " default_memory=\"8GB\",\n", - " default_time=\"02:00:00\",\n", - " default_queue=\"all.q\", # Common SGE queue name\n", - " \n", - " # SGE-specific settings\n", - " remote_work_dir=\"/home/your-username/clustrix\",\n", - " \n", - " # Environment modules\n", - " module_loads=[\"python/3.9\"],\n", - " \n", - " # Job management\n", - " cleanup_on_success=True,\n", - " max_parallel_jobs=30\n", - ")\n", - "\n", - "print(\"SGE cluster configured successfully!\")" - ], - "id": "cell-4" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 1: Mathematical Optimization\n", - "\n", - "SGE clusters are often used for optimization problems:" - ], - "id": "cell-5" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(\n", - " cores=8, \n", - " memory=\"16GB\", \n", - " time=\"01:30:00\", \n", - " queue=\"all.q\",\n", - " # SGE's parallel-environment request (-pe) is not an @cluster keyword\n", - " # argument -- only cores/memory/time/queue reach the job script. `cores`\n", - " # already reserves the requested slot count; a site-specific PE name\n", - " # (smp/mpi/openmp/...) has to go in pre_execution_commands instead.\n", - ")\n", - "def genetic_algorithm_optimization(problem_size=1000, generations=500):\n", - " \"\"\"\n", - " Genetic Algorithm optimization on SGE cluster.\n", - " \"\"\"\n", - " import numpy as np\n", - " import random\n", - " from functools import partial\n", - " \n", - " def rastrigin_function(x):\n", - " \"\"\"Rastrigin function - a multimodal optimization benchmark\"\"\"\n", - " A = 10\n", - " n = len(x)\n", - " return A * n + sum(xi**2 - A * np.cos(2 * np.pi * xi) for xi in x)\n", - " \n", - " def rosenbrock_function(x):\n", - " \"\"\"Rosenbrock function - another optimization benchmark\"\"\"\n", - " return sum(100 * (x[i+1] - x[i]**2)**2 + (1 - x[i])**2 for i in range(len(x)-1))\n", - " \n", - " def sphere_function(x):\n", - " \"\"\"Simple sphere function\"\"\"\n", - " return sum(xi**2 for xi in x)\n", - " \n", - " # Choose objective function\n", - " objective_functions = {\n", - " 'rastrigin': rastrigin_function,\n", - " 'rosenbrock': rosenbrock_function,\n", - " 'sphere': sphere_function\n", - " }\n", - " \n", - " objective_name = 'rastrigin' # Can be parameterized\n", - " objective_func = objective_functions[objective_name]\n", - " \n", - " # Problem dimensions\n", - " dimensions = min(50, problem_size // 20) # Scale dimensions with problem size\n", - " bounds = (-5.12, 5.12) if objective_name == 'rastrigin' else (-2.0, 2.0)\n", - " \n", - " print(f\"Optimizing {objective_name} function in {dimensions} dimensions\")\n", - " print(f\"Population size: {problem_size}, Generations: {generations}\")\n", - " \n", - " class Individual:\n", - " def __init__(self, genes=None):\n", - " if genes is None:\n", - " self.genes = np.random.uniform(bounds[0], bounds[1], dimensions)\n", - " else:\n", - " self.genes = genes.copy()\n", - " self.fitness = None\n", - " \n", - " def evaluate(self):\n", - " if self.fitness is None:\n", - " self.fitness = objective_func(self.genes)\n", - " return self.fitness\n", - " \n", - " def mutate(self, mutation_rate=0.1, mutation_strength=0.1):\n", - " if random.random() < mutation_rate:\n", - " # Add Gaussian noise\n", - " mutation = np.random.normal(0, mutation_strength, dimensions)\n", - " self.genes = np.clip(self.genes + mutation, bounds[0], bounds[1])\n", - " self.fitness = None # Reset fitness\n", - " \n", - " def crossover(self, other):\n", - " # Uniform crossover\n", - " mask = np.random.random(dimensions) < 0.5\n", - " child1_genes = np.where(mask, self.genes, other.genes)\n", - " child2_genes = np.where(mask, other.genes, self.genes)\n", - " return Individual(child1_genes), Individual(child2_genes)\n", - " \n", - " # Initialize population\n", - " population = [Individual() for _ in range(problem_size)]\n", - " \n", - " # Evaluate initial population\n", - " for individual in population:\n", - " individual.evaluate()\n", - " \n", - " # Evolution statistics\n", - " best_fitness_history = []\n", - " average_fitness_history = []\n", - " diversity_history = []\n", - " \n", - " # Main evolution loop\n", - " for generation in range(generations):\n", - " if generation % (generations // 10) == 0:\n", - " print(f\"Generation {generation}/{generations}\")\n", - " \n", - " # Selection (tournament selection)\n", - " def tournament_selection(pop, tournament_size=3):\n", - " tournament = random.sample(pop, tournament_size)\n", - " return min(tournament, key=lambda ind: ind.evaluate())\n", - " \n", - " # Create new population\n", - " new_population = []\n", - " \n", - " # Elitism - keep best 10%\n", - " elite_size = max(1, problem_size // 10)\n", - " elite = sorted(population, key=lambda ind: ind.evaluate())[:elite_size]\n", - " new_population.extend([Individual(ind.genes) for ind in elite])\n", - " \n", - " # Generate offspring\n", - " while len(new_population) < problem_size:\n", - " parent1 = tournament_selection(population)\n", - " parent2 = tournament_selection(population)\n", - " \n", - " if random.random() < 0.8: # Crossover probability\n", - " child1, child2 = parent1.crossover(parent2)\n", - " else:\n", - " child1, child2 = Individual(parent1.genes), Individual(parent2.genes)\n", - " \n", - " # Adaptive mutation rate\n", - " mutation_rate = 0.1 * (1 + generation / generations)\n", - " child1.mutate(mutation_rate=mutation_rate)\n", - " child2.mutate(mutation_rate=mutation_rate)\n", - " \n", - " new_population.extend([child1, child2])\n", - " \n", - " # Trim to exact population size\n", - " new_population = new_population[:problem_size]\n", - " population = new_population\n", - " \n", - " # Evaluate new population\n", - " for individual in population:\n", - " individual.evaluate()\n", - " \n", - " # Statistics\n", - " fitnesses = [ind.fitness for ind in population]\n", - " best_fitness = min(fitnesses)\n", - " average_fitness = np.mean(fitnesses)\n", - " \n", - " # Population diversity (average pairwise distance)\n", - " if generation % 10 == 0: # Calculate diversity every 10 generations\n", - " sample_size = min(100, problem_size)\n", - " sample_pop = random.sample(population, sample_size)\n", - " distances = []\n", - " for i in range(len(sample_pop)):\n", - " for j in range(i+1, len(sample_pop)):\n", - " dist = np.linalg.norm(sample_pop[i].genes - sample_pop[j].genes)\n", - " distances.append(dist)\n", - " diversity = np.mean(distances) if distances else 0\n", - " diversity_history.append(diversity)\n", - " \n", - " best_fitness_history.append(best_fitness)\n", - " average_fitness_history.append(average_fitness)\n", - " \n", - " # Final results\n", - " best_individual = min(population, key=lambda ind: ind.evaluate())\n", - " \n", - " return {\n", - " 'objective_function': objective_name,\n", - " 'dimensions': dimensions,\n", - " 'population_size': problem_size,\n", - " 'generations': generations,\n", - " 'best_fitness': best_individual.fitness,\n", - " 'best_solution': best_individual.genes.tolist(),\n", - " 'convergence_history': {\n", - " 'best_fitness': best_fitness_history[::10], # Every 10th generation\n", - " 'average_fitness': average_fitness_history[::10],\n", - " 'diversity': diversity_history\n", - " },\n", - " 'final_population_stats': {\n", - " 'best_fitness': min(fitnesses),\n", - " 'worst_fitness': max(fitnesses),\n", - " 'average_fitness': np.mean(fitnesses),\n", - " 'fitness_std': np.std(fitnesses)\n", - " }\n", - " }\n", - "\n", - "# Run genetic algorithm optimization\n", - "ga_results = genetic_algorithm_optimization(problem_size=500, generations=200)\n", - "\n", - "print(f\"\\nGENETIC ALGORITHM OPTIMIZATION COMPLETE\")\n", - "print(f\"Function: {ga_results['objective_function']}\")\n", - "print(f\"Dimensions: {ga_results['dimensions']}\")\n", - "print(f\"Best fitness: {ga_results['best_fitness']:.6f}\")\n", - "print(f\"Population size: {ga_results['population_size']}\")\n", - "print(f\"Generations: {ga_results['generations']}\")\n", - "\n", - "final_stats = ga_results['final_population_stats']\n", - "print(f\"\\nFinal population statistics:\")\n", - "print(f\" Best: {final_stats['best_fitness']:.6f}\")\n", - "print(f\" Average: {final_stats['average_fitness']:.6f}\")\n", - "print(f\" Worst: {final_stats['worst_fitness']:.6f}\")\n", - "print(f\" Std Dev: {final_stats['fitness_std']:.6f}\")" - ], - "id": "cell-6" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 2: Engineering Simulation\n", - "\n", - "Finite element analysis commonly run on SGE clusters:" - ], - "id": "cell-7" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(\n", - " cores=12,\n", - " memory=\"32GB\",\n", - " time=\"04:00:00\",\n", - " queue=\"all.q\",\n", - " # As above: `pe=` is accepted but silently dropped.\n", - ")\n", - "def finite_element_stress_analysis(mesh_density=\"medium\", material=\"steel\", load_cases=5):\n", - " \"\"\"\n", - " Simplified finite element stress analysis simulation.\n", - " \"\"\"\n", - " import numpy as np\n", - " from scipy.sparse import csr_matrix\n", - " from scipy.sparse.linalg import spsolve\n", - " import math\n", - " \n", - " # Material properties\n", - " materials = {\n", - " 'steel': {'E': 200e9, 'nu': 0.3, 'yield_strength': 250e6, 'density': 7850},\n", - " 'aluminum': {'E': 70e9, 'nu': 0.33, 'yield_strength': 276e6, 'density': 2700},\n", - " 'titanium': {'E': 114e9, 'nu': 0.32, 'yield_strength': 880e6, 'density': 4500},\n", - " 'concrete': {'E': 30e9, 'nu': 0.2, 'yield_strength': 30e6, 'density': 2400}\n", - " }\n", - " \n", - " mat_props = materials.get(material, materials['steel'])\n", - " E = mat_props['E'] # Young's modulus\n", - " nu = mat_props['nu'] # Poisson's ratio\n", - " yield_strength = mat_props['yield_strength']\n", - " density = mat_props['density']\n", - " \n", - " print(f\"FEA Analysis - Material: {material}, Mesh: {mesh_density}, Load cases: {load_cases}\")\n", - " \n", - " # Mesh generation parameters\n", - " mesh_sizes = {\n", - " 'coarse': {'nx': 20, 'ny': 20, 'nz': 10},\n", - " 'medium': {'nx': 40, 'ny': 40, 'nz': 20},\n", - " 'fine': {'nx': 80, 'ny': 80, 'nz': 40}\n", - " }\n", - " \n", - " mesh_params = mesh_sizes.get(mesh_density, mesh_sizes['medium'])\n", - " nx, ny, nz = mesh_params['nx'], mesh_params['ny'], mesh_params['nz']\n", - " \n", - " # Geometry (simple beam)\n", - " length, width, height = 2.0, 0.2, 0.1 # meters\n", - " \n", - " # Generate mesh\n", - " def generate_3d_mesh(nx, ny, nz, length, width, height):\n", - " \"\"\"Generate 3D hexahedral mesh\"\"\"\n", - " nodes = []\n", - " elements = []\n", - " \n", - " # Generate nodes\n", - " for k in range(nz + 1):\n", - " for j in range(ny + 1):\n", - " for i in range(nx + 1):\n", - " x = i * length / nx\n", - " y = j * width / ny\n", - " z = k * height / nz\n", - " nodes.append([x, y, z])\n", - " \n", - " # Generate elements (hexahedral)\n", - " for k in range(nz):\n", - " for j in range(ny):\n", - " for i in range(nx):\n", - " # Node indices for hexahedral element\n", - " n1 = k * (nx + 1) * (ny + 1) + j * (nx + 1) + i\n", - " n2 = n1 + 1\n", - " n3 = n1 + (nx + 1) + 1\n", - " n4 = n1 + (nx + 1)\n", - " n5 = n1 + (nx + 1) * (ny + 1)\n", - " n6 = n5 + 1\n", - " n7 = n5 + (nx + 1) + 1\n", - " n8 = n5 + (nx + 1)\n", - " \n", - " elements.append([n1, n2, n3, n4, n5, n6, n7, n8])\n", - " \n", - " return np.array(nodes), np.array(elements)\n", - " \n", - " nodes, elements = generate_3d_mesh(nx, ny, nz, length, width, height)\n", - " n_nodes = len(nodes)\n", - " n_elements = len(elements)\n", - " n_dof = n_nodes * 3 # 3 DOF per node (x, y, z displacements)\n", - " \n", - " print(f\"Mesh generated: {n_nodes:,} nodes, {n_elements:,} elements, {n_dof:,} DOF\")\n", - " \n", - " # Material matrix (isotropic elasticity)\n", - " def material_matrix_3d(E, nu):\n", - " \"\"\"3D elasticity matrix\"\"\"\n", - " factor = E / ((1 + nu) * (1 - 2 * nu))\n", - " D = np.zeros((6, 6))\n", - " \n", - " # Diagonal terms\n", - " D[0, 0] = D[1, 1] = D[2, 2] = factor * (1 - nu)\n", - " D[3, 3] = D[4, 4] = D[5, 5] = factor * (1 - 2 * nu) / 2\n", - " \n", - " # Off-diagonal terms\n", - " D[0, 1] = D[0, 2] = D[1, 0] = D[1, 2] = D[2, 0] = D[2, 1] = factor * nu\n", - " \n", - " return D\n", - " \n", - " D_matrix = material_matrix_3d(E, nu)\n", - " \n", - " # Simplified stiffness matrix assembly\n", - " def assemble_stiffness_matrix(nodes, elements, D_matrix):\n", - " \"\"\"Assemble global stiffness matrix (simplified)\"\"\"\n", - " K_global = np.zeros((n_dof, n_dof))\n", - " \n", - " for elem_idx, element in enumerate(elements[:min(1000, len(elements))]): # Limit for demo\n", - " if elem_idx % 200 == 0:\n", - " print(f\" Assembling element {elem_idx:,}/{len(elements):,}\")\n", - " \n", - " # Element nodes\n", - " elem_nodes = nodes[element]\n", - " \n", - " # Simplified element stiffness (using average properties)\n", - " volume = length * width * height / n_elements\n", - " k_elem = volume * np.eye(24) * E / (length**2) # Simplified\n", - " \n", - " # Assembly\n", - " for i, node_i in enumerate(element):\n", - " for j, node_j in enumerate(element):\n", - " for di in range(3):\n", - " for dj in range(3):\n", - " row = node_i * 3 + di\n", - " col = node_j * 3 + dj\n", - " if row < n_dof and col < n_dof:\n", - " K_global[row, col] += k_elem[i*3+di, j*3+dj]\n", - " \n", - " return csr_matrix(K_global)\n", - " \n", - " print(\"Assembling stiffness matrix...\")\n", - " K = assemble_stiffness_matrix(nodes, elements, D_matrix)\n", - " \n", - " # Load case analysis\n", - " load_case_results = []\n", - " \n", - " for case in range(load_cases):\n", - " print(f\"\\nAnalyzing load case {case + 1}/{load_cases}...\")\n", - " \n", - " # Define load case\n", - " F = np.zeros(n_dof)\n", - " \n", - " if case == 0: # Point load at free end\n", - " # Find nodes at free end (x = length)\n", - " free_end_nodes = np.where(np.abs(nodes[:, 0] - length) < 1e-6)[0]\n", - " if len(free_end_nodes) > 0:\n", - " center_node = free_end_nodes[len(free_end_nodes)//2]\n", - " F[center_node * 3 + 2] = -1000 # 1kN downward\n", - " \n", - " elif case == 1: # Distributed load\n", - " # Apply distributed load to top surface\n", - " top_nodes = np.where(np.abs(nodes[:, 2] - height) < 1e-6)[0]\n", - " load_per_node = -100 # N per node\n", - " for node in top_nodes:\n", - " F[node * 3 + 2] = load_per_node\n", - " \n", - " elif case == 2: # Torsional load\n", - " # Apply moments at free end\n", - " free_end_nodes = np.where(np.abs(nodes[:, 0] - length) < 1e-6)[0]\n", - " for node in free_end_nodes:\n", - " y, z = nodes[node, 1], nodes[node, 2]\n", - " # Simplified torsion as equivalent forces\n", - " F[node * 3 + 1] = 500 * (z - height/2) # Simplified\n", - " F[node * 3 + 2] = -500 * (y - width/2)\n", - " \n", - " elif case == 3: # Thermal expansion\n", - " # Simplified thermal load (equivalent forces)\n", - " alpha = 12e-6 # Thermal expansion coefficient\n", - " delta_T = 100 # Temperature change (K)\n", - " thermal_strain = alpha * delta_T\n", - " # Apply as equivalent forces (simplified)\n", - " F += np.random.normal(0, E * thermal_strain / 1000, n_dof)\n", - " \n", - " else: # Dynamic/random load\n", - " # Random distributed forces\n", - " np.random.seed(case * 123)\n", - " F = np.random.normal(0, 50, n_dof)\n", - " \n", - " # Boundary conditions (fixed end)\n", - " fixed_nodes = np.where(np.abs(nodes[:, 0]) < 1e-6)[0]\n", - " fixed_dofs = []\n", - " for node in fixed_nodes:\n", - " fixed_dofs.extend([node * 3, node * 3 + 1, node * 3 + 2])\n", - " \n", - " # Apply boundary conditions\n", - " K_reduced = K.copy()\n", - " F_reduced = F.copy()\n", - " \n", - " # Zero out fixed DOFs\n", - " for dof in fixed_dofs:\n", - " if dof < n_dof:\n", - " K_reduced[dof, :] = 0\n", - " K_reduced[:, dof] = 0\n", - " K_reduced[dof, dof] = 1\n", - " F_reduced[dof] = 0\n", - " \n", - " # Solve for displacements\n", - " print(\" Solving linear system...\")\n", - " try:\n", - " displacements = spsolve(K_reduced, F_reduced)\n", - " except:\n", - " # Fallback for singular matrices\n", - " displacements = np.zeros(n_dof)\n", - " print(\" Warning: Singular matrix, using zero displacements\")\n", - " \n", - " # Calculate stresses (simplified)\n", - " max_displacement = np.max(np.abs(displacements))\n", - " displacement_magnitude = np.sqrt(\n", - " displacements[::3]**2 + displacements[1::3]**2 + displacements[2::3]**2\n", - " )\n", - " \n", - " # Simplified stress calculation\n", - " max_stress = E * max_displacement / length # Rough estimate\n", - " \n", - " # Safety factor\n", - " safety_factor = yield_strength / max_stress if max_stress > 0 else float('inf')\n", - " \n", - " case_result = {\n", - " 'case_id': case,\n", - " 'load_type': ['point_load', 'distributed', 'torsion', 'thermal', 'dynamic'][case],\n", - " 'max_displacement_m': max_displacement,\n", - " 'max_stress_Pa': max_stress,\n", - " 'safety_factor': min(safety_factor, 1000), # Cap at 1000\n", - " 'total_force_N': np.sum(np.abs(F)),\n", - " 'displacement_distribution': {\n", - " 'mean': np.mean(displacement_magnitude),\n", - " 'std': np.std(displacement_magnitude),\n", - " 'max': np.max(displacement_magnitude)\n", - " }\n", - " }\n", - " \n", - " load_case_results.append(case_result)\n", - " \n", - " print(f\" Max displacement: {max_displacement:.2e} m\")\n", - " print(f\" Max stress: {max_stress:.2e} Pa\")\n", - " print(f\" Safety factor: {safety_factor:.2f}\")\n", - " \n", - " # Summary analysis\n", - " max_displacement_overall = max(case['max_displacement_m'] for case in load_case_results)\n", - " max_stress_overall = max(case['max_stress_Pa'] for case in load_case_results)\n", - " min_safety_factor = min(case['safety_factor'] for case in load_case_results)\n", - " \n", - " analysis_results = {\n", - " 'model_info': {\n", - " 'material': material,\n", - " 'mesh_density': mesh_density,\n", - " 'nodes': n_nodes,\n", - " 'elements': n_elements,\n", - " 'dof': n_dof,\n", - " 'geometry': {'length': length, 'width': width, 'height': height}\n", - " },\n", - " 'material_properties': mat_props,\n", - " 'load_cases': load_case_results,\n", - " 'summary': {\n", - " 'max_displacement_m': max_displacement_overall,\n", - " 'max_stress_Pa': max_stress_overall,\n", - " 'min_safety_factor': min_safety_factor,\n", - " 'critical_load_case': min(load_case_results, key=lambda x: x['safety_factor'])['load_type'],\n", - " 'passes_safety_check': min_safety_factor > 2.0\n", - " }\n", - " }\n", - " \n", - " return analysis_results\n", - "\n", - "# Run FEA stress analysis\n", - "fea_results = finite_element_stress_analysis(\n", - " mesh_density=\"medium\", \n", - " material=\"steel\", \n", - " load_cases=3\n", - ")\n", - "\n", - "print(f\"\\nFINITE ELEMENT ANALYSIS COMPLETE\")\n", - "model_info = fea_results['model_info']\n", - "print(f\"Material: {model_info['material']}\")\n", - "print(f\"Mesh: {model_info['nodes']:,} nodes, {model_info['elements']:,} elements\")\n", - "print(f\"DOF: {model_info['dof']:,}\")\n", - "\n", - "summary = fea_results['summary']\n", - "print(f\"\\nSummary Results:\")\n", - "print(f\" Max displacement: {summary['max_displacement_m']:.2e} m\")\n", - "print(f\" Max stress: {summary['max_stress_Pa']:.2e} Pa\")\n", - "print(f\" Min safety factor: {summary['min_safety_factor']:.2f}\")\n", - "print(f\" Critical load case: {summary['critical_load_case']}\")\n", - "print(f\" Passes safety check: {summary['passes_safety_check']}\")" - ], - "id": "cell-8" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 3: Multi-Objective Engineering Design (No Native SGE Task Arrays)\n", - "\n", - "**Clustrix does not support SGE task arrays** (`qsub -t`). The\n", - "`@cluster` decorator's SGE-relevant resource arguments are exactly\n", - "`cores`, `memory`, `time` and `queue` -- a keyword argument named\n", - "`sge_array` is accepted by Python but never turned into a `-t` directive.\n", - "Worse, the original version of the cell below read `SGE_TASK_ID` from the\n", - "environment with a hardcoded fallback of `'1'` -- since clustrix never\n", - "submits a real task array and never sets that variable, every submission\n", - "would silently evaluate task 1 only.\n", - "\n", - "The fixed version below takes `task_id` as an explicit function argument\n", - "and drives the sweep from Python. `@cluster(..., async_submit=True)` is\n", - "set on the decorator itself -- `async_submit` cannot be overridden per\n", - "call -- so every submission returns an `AsyncJobResult` immediately and\n", - "the 25 jobs overlap instead of running one at a time; `.wait()` then\n", - "blocks for each result in turn. Same workaround used for SLURM job arrays\n", - "and PBS parameter studies earlier in this tutorial series.\n" - ], - "id": "cell-9" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "@cluster(\n", - " cores=6,\n", - " memory=\"24GB\",\n", - " time=\"02:00:00\",\n", - " queue=\"all.q\",\n", - " async_submit=True, # decorator-time only: cannot be overridden per call\n", - ")\n", - "def multi_objective_design_optimization(task_id, design_problem=\"beam_design\"):\n", - " \"\"\"\n", - " Multi-objective design optimization -- one task's worth of work.\n", - "\n", - " ``task_id`` is passed in explicitly by the driver loop below, because\n", - " clustrix has no SGE task-array support to set it for us.\n", - " \"\"\"\n", - " import numpy as np\n", - " import random\n", - " from math import pi, sqrt\n", - " \n", - " print(f\"Design optimization task {task_id}\")\n", - " \n", - " def beam_design_objectives(width, height, length, material_density=7850):\n", - " \"\"\"Calculate beam design objectives\"\"\"\n", - " # Geometry constraints\n", - " area = width * height\n", - " moment_of_inertia = width * height**3 / 12\n", - " volume = area * length\n", - " mass = volume * material_density\n", - " \n", - " # Structural performance\n", - " E = 200e9 # Young's modulus (Pa)\n", - " max_load = 10000 # Maximum load (N)\n", - " \n", - " # Deflection calculation (simply supported beam)\n", - " max_deflection = (5 * max_load * length**4) / (384 * E * moment_of_inertia)\n", - " \n", - " # Stress calculation\n", - " max_moment = max_load * length / 4 # For simply supported beam\n", - " max_stress = max_moment * (height / 2) / moment_of_inertia\n", - " \n", - " # Objectives to minimize\n", - " objectives = {\n", - " 'mass': mass, # Minimize weight\n", - " 'deflection': max_deflection, # Minimize deflection\n", - " 'stress': max_stress, # Minimize stress\n", - " 'cost': mass * 2.5 + area * 10 # Material + manufacturing cost\n", - " }\n", - " \n", - " # Constraints\n", - " constraints = {\n", - " 'deflection_limit': max_deflection < length / 250, # L/250 deflection limit\n", - " 'stress_limit': max_stress < 250e6, # Yield stress limit\n", - " 'aspect_ratio': height / width < 5, # Practical aspect ratio\n", - " 'minimum_thickness': width > 0.01 and height > 0.01 # Minimum thickness\n", - " }\n", - " \n", - " return objectives, constraints\n", - " \n", - " def truss_design_objectives(member_areas, topology, material_density=2700):\n", - " \"\"\"Calculate truss design objectives\"\"\"\n", - " # Simplified truss analysis\n", - " n_members = len(member_areas)\n", - " total_length = sum(topology) # Simplified total length\n", - " total_volume = sum(area * length for area, length in zip(member_areas, topology))\n", - " total_mass = total_volume * material_density\n", - " \n", - " # Simplified stiffness calculation\n", - " E = 70e9 # Aluminum Young's modulus\n", - " avg_stiffness = E * sum(member_areas) / n_members\n", - " \n", - " # Simplified stress analysis\n", - " applied_load = 5000 # N\n", - " avg_stress = applied_load / sum(member_areas)\n", - " \n", - " objectives = {\n", - " 'mass': total_mass,\n", - " 'compliance': 1 / avg_stiffness, # Inverse of stiffness\n", - " 'max_stress': avg_stress,\n", - " 'cost': total_mass * 3.0 + n_members * 50 # Material + connection cost\n", - " }\n", - " \n", - " constraints = {\n", - " 'stress_limit': avg_stress < 276e6, # Aluminum yield\n", - " 'buckling_check': all(area > 1e-4 for area in member_areas), # Min area\n", - " 'geometric_feasibility': len(member_areas) >= 3 # Minimum members\n", - " }\n", - " \n", - " return objectives, constraints\n", - " \n", - " # Set up design space for this task\n", - " np.random.seed(task_id * 42) # Reproducible but different per task\n", - " \n", - " if design_problem == \"beam_design\":\n", - " # Generate design variables for beam\n", - " width = np.random.uniform(0.05, 0.5) # 5cm to 50cm\n", - " height = np.random.uniform(0.1, 1.0) # 10cm to 100cm\n", - " length = np.random.uniform(2.0, 10.0) # 2m to 10m\n", - " \n", - " objectives, constraints = beam_design_objectives(width, height, length)\n", - " design_vars = {'width': width, 'height': height, 'length': length}\n", - " \n", - " elif design_problem == \"truss_design\":\n", - " # Generate design variables for truss\n", - " n_members = random.randint(5, 15)\n", - " member_areas = np.random.uniform(1e-4, 1e-2, n_members) # 1cm\u00b2 to 100cm\u00b2\n", - " topology = np.random.uniform(0.5, 3.0, n_members) # Member lengths\n", - " \n", - " objectives, constraints = truss_design_objectives(member_areas, topology)\n", - " design_vars = {\n", - " 'n_members': n_members,\n", - " 'member_areas': member_areas.tolist(),\n", - " 'topology': topology.tolist()\n", - " }\n", - " \n", - " else:\n", - " raise ValueError(f\"Unknown design problem: {design_problem}\")\n", - " \n", - " # Check constraint feasibility\n", - " feasible = all(constraints.values())\n", - " n_violated_constraints = sum(1 for satisfied in constraints.values() if not satisfied)\n", - " \n", - " # Calculate Pareto performance metrics\n", - " def normalize_objectives(objectives):\n", - " \"\"\"Normalize objectives for multi-objective comparison\"\"\"\n", - " # Reference values for normalization (approximate)\n", - " if design_problem == \"beam_design\":\n", - " ref_values = {\n", - " 'mass': 1000, # kg\n", - " 'deflection': 0.01, # m\n", - " 'stress': 100e6, # Pa\n", - " 'cost': 5000 # currency units\n", - " }\n", - " else: # truss_design\n", - " ref_values = {\n", - " 'mass': 500, # kg\n", - " 'compliance': 1e-9, # 1/N\n", - " 'max_stress': 100e6, # Pa\n", - " 'cost': 3000 # currency units\n", - " }\n", - " \n", - " normalized = {}\n", - " for obj, value in objectives.items():\n", - " if obj in ref_values:\n", - " normalized[obj] = value / ref_values[obj]\n", - " else:\n", - " normalized[obj] = value\n", - " \n", - " return normalized\n", - " \n", - " normalized_objectives = normalize_objectives(objectives)\n", - " \n", - " # Calculate aggregate performance metrics\n", - " weighted_sum = sum(normalized_objectives.values()) # Equal weights\n", - " max_objective = max(normalized_objectives.values())\n", - " \n", - " # Performance score (lower is better)\n", - " if feasible:\n", - " performance_score = weighted_sum\n", - " else:\n", - " # Penalty for infeasible designs\n", - " performance_score = weighted_sum * (1 + 10 * n_violated_constraints)\n", - " \n", - " # Compile results\n", - " design_result = {\n", - " 'task_id': task_id,\n", - " 'design_problem': design_problem,\n", - " 'design_variables': design_vars,\n", - " 'objectives': objectives,\n", - " 'normalized_objectives': normalized_objectives,\n", - " 'constraints': constraints,\n", - " 'feasible': feasible,\n", - " 'constraints_violated': n_violated_constraints,\n", - " 'performance_metrics': {\n", - " 'weighted_sum': weighted_sum,\n", - " 'max_objective': max_objective,\n", - " 'performance_score': performance_score\n", - " },\n", - " 'design_quality': {\n", - " 'excellent': performance_score < 2.0 and feasible,\n", - " 'good': performance_score < 4.0 and feasible,\n", - " 'acceptable': performance_score < 8.0 and feasible,\n", - " 'poor': not feasible or performance_score >= 8.0\n", - " }\n", - " }\n", - " \n", - " return design_result\n", - "\n", - "# Drive the \"task array\" from Python: 25 separate job submissions,\n", - "# submitted without waiting for each to finish, then collected.\n", - "pending = [\n", - " multi_objective_design_optimization(task_id, \"beam_design\")\n", - " for task_id in range(1, 26)\n", - "]\n", - "design_results = [job.wait() for job in pending]\n", - "\n", - "best = min(\n", - " (r for r in design_results if r['feasible']),\n", - " key=lambda r: r['performance_metrics']['performance_score'],\n", - " default=design_results[0],\n", - ")\n", - "print(f\"Ran {len(design_results)} design-optimization tasks.\")\n", - "print(f\"\\nBest design -- Task {best['task_id']}\")\n", - "print(f\"Problem: {best['design_problem']}\")\n", - "print(f\"Feasible: {best['feasible']}\")\n", - "\n", - "if best['design_problem'] == 'beam_design':\n", - " vars = best['design_variables']\n", - " print(f\"\\nDesign Variables:\")\n", - " print(f\" Width: {vars['width']:.3f} m\")\n", - " print(f\" Height: {vars['height']:.3f} m\")\n", - " print(f\" Length: {vars['length']:.3f} m\")\n", - "\n", - "print(f\"\\nObjectives:\")\n", - "for obj, value in best['objectives'].items():\n", - " if 'stress' in obj or 'deflection' in obj:\n", - " print(f\" {obj}: {value:.2e}\")\n", - " else:\n", - " print(f\" {obj}: {value:.2f}\")\n", - "\n", - "perf = best['performance_metrics']\n", - "print(f\"\\nPerformance Score: {perf['performance_score']:.2f}\")\n", - "\n", - "quality = best['design_quality']\n", - "for level, is_level in quality.items():\n", - " if is_level:\n", - " print(f\"Design Quality: {level.upper()}\")\n", - " break" - ], - "id": "cell-10" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SGE Parallel Environments and Resource Management" - ], - "id": "cell-11" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def configure_sge_parallel_environments():\n", - " \"\"\"\n", - " Reference for SGE parallel-environment names and typical core counts.\n", - "\n", - " These `clustrix_config` dicts are illustrative only: `pe` is an SGE\n", - " concept (the -pe qsub flag), not a real @cluster/ClusterConfig key --\n", - " clustrix has no way to request a parallel environment. Only `cores`\n", - " and `memory` below are things clustrix actually understands.\n", - " \"\"\"\n", - " \n", - " # Common SGE parallel environments\n", - " pe_configs = {\n", - " 'smp': {\n", - " 'description': 'Symmetric Multi-Processing (shared memory)',\n", - " 'use_case': 'Multi-threaded applications on single node',\n", - " 'example_cores': [2, 4, 8, 16, 32],\n", - " 'clustrix_config': {\n", - " 'cores': 8,\n", - " 'pe': 'smp 8',\n", - " 'memory': '32GB'\n", - " }\n", - " },\n", - " 'mpi': {\n", - " 'description': 'Message Passing Interface (distributed memory)',\n", - " 'use_case': 'Distributed parallel applications across nodes',\n", - " 'example_cores': [8, 16, 32, 64, 128],\n", - " 'clustrix_config': {\n", - " 'cores': 32,\n", - " 'pe': 'mpi 32',\n", - " 'memory': '128GB'\n", - " }\n", - " },\n", - " 'openmp': {\n", - " 'description': 'OpenMP parallel environment',\n", - " 'use_case': 'OpenMP applications with thread parallelism',\n", - " 'example_cores': [4, 8, 12, 16],\n", - " 'clustrix_config': {\n", - " 'cores': 12,\n", - " 'pe': 'openmp 12',\n", - " 'memory': '48GB'\n", - " }\n", - " },\n", - " 'hybrid': {\n", - " 'description': 'Hybrid MPI+OpenMP',\n", - " 'use_case': 'Applications using both MPI and OpenMP',\n", - " 'example_cores': [16, 32, 64],\n", - " 'clustrix_config': {\n", - " 'cores': 32,\n", - " 'pe': 'hybrid 32',\n", - " 'memory': '128GB'\n", - " }\n", - " }\n", - " }\n", - " \n", - " print(\"SGE Parallel Environment Configurations:\")\n", - " print(\"=\" * 60)\n", - " \n", - " for pe_name, config in pe_configs.items():\n", - " print(f\"\\n{pe_name.upper()}:\")\n", - " print(f\" Description: {config['description']}\")\n", - " print(f\" Use case: {config['use_case']}\")\n", - " print(f\" Common core counts: {config['example_cores']}\")\n", - " print(f\" Clustrix configuration:\")\n", - " for key, value in config['clustrix_config'].items():\n", - " print(f\" {key}: {value}\")\n", - " \n", - " return pe_configs\n", - "\n", - "# SGE resource selection helper\n", - "def select_sge_resources(application_type, problem_size, parallelization=\"smp\"):\n", - " \"\"\"\n", - " Select appropriate SGE resources based on application characteristics.\n", - " \"\"\"\n", - " \n", - " # Base resource requirements by application type\n", - " app_requirements = {\n", - " 'optimization': {'base_cores': 8, 'memory_per_core': 4, 'time_factor': 1.5},\n", - " 'simulation': {'base_cores': 16, 'memory_per_core': 6, 'time_factor': 2.0},\n", - " 'ml_training': {'base_cores': 4, 'memory_per_core': 8, 'time_factor': 1.0},\n", - " 'data_analysis': {'base_cores': 6, 'memory_per_core': 4, 'time_factor': 0.8},\n", - " 'engineering': {'base_cores': 12, 'memory_per_core': 5, 'time_factor': 1.8}\n", - " }\n", - " \n", - " if application_type not in app_requirements:\n", - " application_type = 'simulation' # Default\n", - " \n", - " req = app_requirements[application_type]\n", - " \n", - " # Scale resources based on problem size\n", - " size_multipliers = {\n", - " 'small': 0.5,\n", - " 'medium': 1.0,\n", - " 'large': 2.0,\n", - " 'xlarge': 4.0\n", - " }\n", - " \n", - " multiplier = size_multipliers.get(problem_size, 1.0)\n", - " \n", - " cores = max(1, int(req['base_cores'] * multiplier))\n", - " memory_gb = max(4, int(cores * req['memory_per_core']))\n", - " \n", - " # Time estimation (hours)\n", - " base_time = 2.0 # hours\n", - " time_hours = max(0.5, base_time * req['time_factor'] * multiplier)\n", - " \n", - " # Format time as HH:MM:SS\n", - " hours = int(time_hours)\n", - " minutes = int((time_hours - hours) * 60)\n", - " time_str = f\"{hours:02d}:{minutes:02d}:00\"\n", - " \n", - " # Queue selection\n", - " if time_hours <= 1:\n", - " queue = \"short.q\"\n", - " elif time_hours <= 8:\n", - " queue = \"all.q\"\n", - " else:\n", - " queue = \"long.q\"\n", - " \n", - " sge_config = {\n", - " 'cores': cores,\n", - " 'memory': f\"{memory_gb}GB\",\n", - " 'time': time_str,\n", - " 'queue': queue,\n", - " 'pe': f\"{parallelization} {cores}\"\n", - " }\n", - " \n", - " return sge_config\n", - "\n", - "# Display PE configurations\n", - "pe_configs = configure_sge_parallel_environments()\n", - "\n", - "# Example resource selections\n", - "print(\"\\n\\nSGE Resource Selection Examples:\")\n", - "print(\"=\" * 60)\n", - "\n", - "examples = [\n", - " ('optimization', 'medium', 'smp'),\n", - " ('simulation', 'large', 'mpi'),\n", - " ('ml_training', 'small', 'openmp'),\n", - " ('engineering', 'xlarge', 'hybrid')\n", - "]\n", - "\n", - "for app_type, size, parallel in examples:\n", - " config = select_sge_resources(app_type, size, parallel)\n", - " print(f\"\\n{app_type.upper()} ({size}, {parallel}):\")\n", - " for key, value in config.items():\n", - " print(f\" {key}: {value}\")" - ], - "id": "cell-12" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SGE Job Monitoring and Management" - ], - "id": "cell-13" - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from clustrix import ClusterExecutor\n", - "\n", - "# Connect to SGE cluster and check status\n", - "config = clustrix.get_config()\n", - "executor = ClusterExecutor(config)\n", - "\n", - "try:\n", - " executor.connect()\n", - " print(\"\u2713 Successfully connected to SGE cluster\")\n", - " \n", - " # Check SGE version and configuration\n", - " stdout, stderr = executor._execute_command(\"qconf -sconf\")\n", - " if \"SGE\" in stdout or \"Grid Engine\" in stdout:\n", - " print(\"\u2713 SGE/Grid Engine detected\")\n", - " \n", - " # List available queues\n", - " stdout, stderr = executor._execute_command(\"qconf -sql\")\n", - " if stdout:\n", - " queues = stdout.strip().split('\\n')\n", - " print(f\"\\nAvailable queues ({len(queues)}):\")\n", - " for queue in queues[:10]: # Show first 10\n", - " print(f\" {queue}\")\n", - " if len(queues) > 10:\n", - " print(f\" ... and {len(queues) - 10} more\")\n", - " \n", - " # List parallel environments\n", - " stdout, stderr = executor._execute_command(\"qconf -spl\")\n", - " if stdout:\n", - " pes = stdout.strip().split('\\n')\n", - " print(f\"\\nParallel environments ({len(pes)}):\")\n", - " for pe in pes:\n", - " print(f\" {pe}\")\n", - " \n", - " # Check queue status\n", - " stdout, stderr = executor._execute_command(\"qstat -g c\")\n", - " if stdout:\n", - " print(\"\\nCluster queue summary:\")\n", - " lines = stdout.strip().split('\\n')\n", - " for line in lines[:15]: # Show header and first few lines\n", - " print(f\" {line}\")\n", - " \n", - " # Check user's jobs\n", - " username = config.username\n", - " stdout, stderr = executor._execute_command(f\"qstat -u {username}\")\n", - " if stdout and len(stdout.strip().split('\\n')) > 2:\n", - " print(f\"\\nYour current jobs:\")\n", - " lines = stdout.strip().split('\\n')\n", - " for line in lines:\n", - " print(f\" {line}\")\n", - " else:\n", - " print(f\"\\n\u2713 No jobs currently running for user {username}\")\n", - " \n", - " # Check host information\n", - " stdout, stderr = executor._execute_command(\"qhost | head -20\")\n", - " if stdout:\n", - " print(\"\\nHost information (sample):\")\n", - " lines = stdout.strip().split('\\n')\n", - " for line in lines:\n", - " print(f\" {line}\")\n", - " \n", - " executor.disconnect()\n", - " print(\"\\n\u2713 SGE cluster monitoring completed successfully\")\n", - " \n", - "except Exception as e:\n", - " print(f\"\u2717 Connection or monitoring failed: {e}\")\n", - " print(\"Please check your SGE cluster configuration\")" - ], - "id": "cell-14" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Summary\n", - "\n", - "This tutorial covered SGE cluster usage with Clustrix:\n", - "\n", - "1. **SGE Configuration** - Setting up Clustrix for SGE/Grid Engine clusters\n", - "2. **Mathematical Optimization** - Genetic algorithms and complex optimization\n", - "3. **Engineering Simulation** - Finite element analysis and structural design\n", - "4. **Multi-Objective Design** - Engineering design optimization with task arrays\n", - "5. **Parallel Environments** - SMP, MPI, OpenMP, and hybrid configurations\n", - "6. **Resource Management** - Intelligent resource selection and queue management\n", - "7. **Job Monitoring** - SGE cluster status and job management\n", - "\n", - "### Key SGE Features (and What Clustrix Actually Supports):\n", - "\n", - "- **Resource Specification**: `cores`, `memory`, `time` and `queue` reach the\n", - " generated job script; that is the complete set of SGE-relevant\n", - " `@cluster` keyword arguments.\n", - "- **Parallel Environments and Task Arrays are SGE concepts, not clustrix\n", - " ones**: `pe` and `sge_array` are accepted as keyword arguments but\n", - " silently dropped. Drive parameter sweeps from a Python loop instead (see\n", - " Example 3 above), and put any required `-pe` request in\n", - " `pre_execution_commands`.\n", - "- **Queue Selection**: Choose appropriate queues based on runtime requirements.\n", - "- **Job Dependencies / Advanced Scheduling**: not something clustrix wires\n", - " up automatically -- if your site needs `-hold_jid` or priority/reservation\n", - " flags, that is also `pre_execution_commands` territory.\n", - "\n", - "### Best Practices:\n", - "\n", - "- **Parallel Environment Selection**: Choose PE based on application parallelization model\n", - "- **Resource Estimation**: Use application profiling to estimate requirements accurately\n", - "- **Queue Strategy**: Match job characteristics to appropriate queue policies\n", - "- **Array Jobs**: Use task arrays for embarrassingly parallel workloads\n", - "- **Monitoring**: Regular cluster status checks for optimal resource utilization\n", - "\n", - "### Next Steps:\n", - "\n", - "- Try [SLURM Tutorial](slurm_tutorial.ipynb) for SLURM-specific features\n", - "- Explore [PBS Tutorial](pbs_tutorial.ipynb) for PBS/Torque clusters\n", - "- Check [Kubernetes Tutorial](kubernetes_tutorial.ipynb) for containerized computing\n", - "- Review [SSH Tutorial](ssh_tutorial.ipynb) for simple remote execution\n", - "\n", - "For more information, visit the [Clustrix Documentation](https://clustrix.readthedocs.io)." - ], - "id": "cell-15" - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} \ No newline at end of file diff --git a/docs/source/tutorials/kubernetes_tutorial.rst b/docs/source/tutorials/kubernetes_tutorial.rst deleted file mode 100644 index e1366cc2..00000000 --- a/docs/source/tutorials/kubernetes_tutorial.rst +++ /dev/null @@ -1,894 +0,0 @@ -Kubernetes Cluster Tutorial -=========================== - -This tutorial demonstrates how to use Clustrix with Kubernetes clusters for cloud-native distributed computing. Kubernetes provides excellent scalability and resource management for containerized workloads. - -.. warning:: - - The Kubernetes backend has not been verified against a real cluster. Treat - this tutorial as a description of the intended interface, not as a record of - something that has been run. - - Two limitations are worth knowing before you start. The notebook widget - has a Kubernetes section covering namespace, image, service account and - image pull policy; the remaining ``k8s_*`` settings come from a - configuration file or ``configure()``. And per-job Kubernetes overrides are - not implemented: the executor reads only the configuration-level ``k8s_*`` - settings and derives pod resource requests and limits from ``cores`` and - ``memory``, so ``namespace``, ``image``, ``cpu_limit``, ``memory_limit``, - ``restart_policy``, ``backoff_limit`` and ``active_deadline_seconds`` passed - to ``@cluster`` are silently ignored. - - Memory is translated for you: ``memory="8GB"`` becomes the Kubernetes - quantity ``8Gi``, so clustrix's usual spelling is accepted here. - -Prerequisites -------------- - -1. Access to a Kubernetes cluster (local, cloud, or on-premises) -- or let - Clustrix create one for you, see `Auto-Provisioning a Cluster`_ below -2. kubectl configured with cluster access (not needed if you use - auto-provisioning; Clustrix configures kubectl itself) -3. Clustrix installed with Kubernetes support: ``pip install clustrix[kubernetes]`` - -Auto-Provisioning a Cluster ----------------------------- - -If you don't already have a Kubernetes cluster, ``clustrix.kubernetes`` can -create one from scratch: locally with `kind `_ -(Kubernetes-in-Docker), or on a cloud provider. This is the -``KubernetesClusterProvisioner`` API used internally by -``@cluster(auto_provision=True, ...)`` (see below); you can also call it -directly. - -.. important:: - - The cloud provisioning paths (AWS, GCP, Azure, HuggingFace, Lambda Cloud) - are **unverified** -- consistent with this tutorial's opening warning and - with the main README, no cloud job has been shown to provision a cluster - and run to completion end to end. Only the local ``kind``-based path is - described as verified below, and only in the narrow sense that it does not - require cloud credentials and its prerequisites (Docker, ``kind``, - ``kubectl``) can be checked locally; the provisioner itself has not been - exercised end to end in this session either. Treat every code sample here - as a description of the documented interface, not a record of a - successful run, until you have run it yourself. - -Local Provisioning (kind) -- No Cloud Credentials Required -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Requires Docker, `kind (installation instructions) -`_, -and ``kubectl`` on the machine running Clustrix. No cloud account, API key, -or credentials of any kind are needed -- the local provisioner uses a -placeholder ``{"type": "local"}`` credential internally and ignores it. - -.. code-block:: python - - # cluster-required: provisions a real kind cluster via Docker - from clustrix import configure, cluster - - configure( - cluster_type="kubernetes", - auto_provision_k8s=True, - k8s_provider="local", # selects LocalDockerKubernetesProvisioner - k8s_node_count=2, - k8s_cluster_name="my-local-cluster", # optional; auto-generated if omitted - ) - - @cluster(platform="kubernetes", auto_provision=True, cores=1, memory="512Mi") - def analyze(x): - return x * 2 - - analyze(21) # provisions (or reuses) the kind cluster, then runs the job - -.. warning:: - - The ``provider=`` keyword on ``@cluster(...)`` (used for the hostful cloud - VM backends -- Lambda Cloud, AWS, Azure, GCP) is **not** the same setting - as the Kubernetes provider. There is no ``k8s_provider=`` (or ``region=``) - parameter on ``@cluster`` itself; ``config.k8s_provider`` defaults to - ``"aws"`` and must be set explicitly via ``configure()`` (or a - ``ClusterConfig``) as shown above. Passing ``provider="local"`` directly - to ``@cluster(...)`` has no effect on which Kubernetes provisioner runs. - -Instead of the decorator, you can provision (and later tear down) a cluster -directly: - -.. code-block:: python - - # cluster-required: provisions a real kind cluster via Docker - from clustrix.kubernetes.cluster_provisioner import ( - provision_kubernetes_cluster, - destroy_kubernetes_cluster, - ) - - cluster_info = provision_kubernetes_cluster( - provider="local", - cluster_name="my-local-cluster", - region="local", # ignored by the local provisioner, but required by the function signature - node_count=2, - ) - print(cluster_info["cluster_id"]) - - # ... later ... - destroy_kubernetes_cluster(cluster_info["cluster_id"], provider="local") - -Cloud Provisioning -- Unverified -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -The same ``provision_kubernetes_cluster()`` / ``@cluster(auto_provision=True)`` -interface supports five cloud providers by creating a from-scratch cluster -(EKS, GKE, AKS, a HuggingFace Space, or a Lambda Cloud Kubernetes deployment). -**None of these have been run end to end**; only DigitalOcean and Linode are -excluded because no provisioner exists for them at all -- the five below at -least have provisioner code, but it has not been validated against a live -account. - -.. code-block:: python - - # cluster-required: unverified cloud path, needs real provider credentials - from clustrix import configure, cluster - - configure( - cluster_type="kubernetes", - auto_provision_k8s=True, - k8s_provider="aws", # aws, gcp, azure, huggingface, lambda - k8s_region="us-west-2", - k8s_node_count=3, - k8s_node_type="t3.large", # provider-specific; see defaults below - k8s_version="1.28", - ) - - @cluster(platform="kubernetes", auto_provision=True, cores=2, memory="4Gi") - def train(x): - return x - -Credentials are read from environment variables via -``clustrix.credential_manager``, one set per provider: - -.. list-table:: - :header-rows: 1 - - * - ``k8s_provider`` - - Environment variables - - Default ``node_type`` - * - ``aws`` - - ``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``, ``AWS_REGION`` - - ``t3.medium`` - * - ``gcp`` - - ``GCP_PROJECT_ID``, ``GCP_SERVICE_ACCOUNT_JSON`` - - ``e2-standard-4`` - * - ``azure`` - - ``AZURE_SUBSCRIPTION_ID``, ``AZURE_TENANT_ID``, ``AZURE_CLIENT_ID``, ``AZURE_CLIENT_SECRET`` - - ``Standard_D2s_v3`` - * - ``huggingface`` - - ``HF_TOKEN``, ``HF_USERNAME`` - - (Space-based; no VM instance type) - * - ``lambda`` - - ``LAMBDA_CLOUD_API_KEY`` - - (Lambda Cloud instance types) - -If credentials for the selected provider aren't found, -``KubernetesClusterProvisioner`` raises ``ValueError`` rather than falling -back to another provider or to local execution. - -Configuration Options ---------------------- - -**Option 1: Interactive Widget (Recommended for Jupyter)** - -For Jupyter notebook users, use the interactive configuration widget: - -Importing ``clustrix`` registers the magic but does not display anything. Run -``%%remote`` in a cell of its own to open the widget: - -.. code-block:: ipython3 - - %%remote - -Selecting ``kubernetes`` shows a Kubernetes section with namespace, image, -service account and image pull policy. The remaining ``k8s_*`` settings -below have to come from a configuration file or ``configure()``. - -**Option 2: Programmatic Configuration** - -Configure Clustrix programmatically for your Kubernetes cluster: - -.. code-block:: python - - from clustrix import configure - - configure( - cluster_type="kubernetes", - # Note: Kubernetes uses kubectl config, no host/SSH needed - k8s_namespace="default", # Optional: specify namespace - k8s_image="python:3.11-slim", # Optional: custom image - ) - -Behind the Scenes: How a Job Actually Runs -------------------------------------------- - -This section describes ``clustrix/executor_kubernetes.py`` as it exists today -(``KubernetesJobManager``), not aspirational behaviour. It has never been -run against a real cluster (see the warning at the top of this page), but -the code path itself, and the order in which it does things, is exactly -this: - -1. **Submission** (``submit_k8s_job``): the function, its positional - arguments and its keyword arguments are serialized with ``cloudpickle`` - and base64-encoded. A fresh, random 32-byte hex key (``result_key``) is - generated for this job only. -2. **Worker program construction** (``build_worker_program``): a Python - program is generated as a plain string. It decodes and unpickles the - function and arguments, calls the function, serializes the result with - ``dill`` (not the repr of the result -- see below), computes an - HMAC-SHA256 of those exact bytes keyed by ``CLUSTRIX_RESULT_KEY``, and - prints two lines to stdout: ``CLUSTRIX_RESULT_B64:`` and - ``CLUSTRIX_RESULT_HMAC:``. ``build_container_command`` refuses - to proceed (raises ``ValueError``) if the generated program contains a - ``"``, ``$`` or backtick, since any of those would be reinterpreted by the - shell that embeds it. -3. **Job manifest**: a ``batch/v1`` ``Job`` is created with one container - running ``python:3.11-slim`` (or your configured ``k8s_image``). The - command is ``pip install cloudpickle dill --quiet && python -c ""`` -- there is no custom image build step, and the per-job - ``result_key`` is passed in as the container env var - ``CLUSTRIX_RESULT_KEY``, never on the command line. CPU/memory - ``requests`` and ``limits`` are both set to the same values, derived from - ``cores``/``memory`` via ``normalize_memory()`` (which turns clustrix's - ``"8GB"`` spelling into the ``8Gi``/``8G`` Kubernetes accepts). -4. **Status polling** (``wait_for_k8s_result`` / ``check_k8s_job_status``): - the job's status is read from the Kubernetes API's own - ``job.status.succeeded`` / ``.failed`` / ``.active`` fields, on a fixed - interval (``job_poll_interval``, default 30s). If the status API call - itself fails (evicted pod, lost namespace access, ``kubernetes`` package - missing), that raises ``RuntimeError`` rather than being treated as - success or silently retried forever -- there is a comment in the source - noting this used to report ``"completed"`` on any such error, which meant - a job that had been evicted, or whose namespace the caller could no - longer read, was reported as having finished successfully. -5. **Result retrieval** (``get_k8s_result`` / ``decode_signed_result``): once - the job reports success, the manager reads the log of its (single) - succeeded pod, extracts the ``CLUSTRIX_RESULT_B64``/``CLUSTRIX_RESULT_HMAC`` - lines, recomputes the HMAC over the decoded bytes with the ``result_key`` - this job was given, and compares it with ``hmac.compare_digest``. Only if - that check passes does it call ``dill.loads`` on the payload. A log with - no result marker, no signature, or a signature that does not match raises - ``RuntimeError`` and is never deserialized -- unpickling untrusted bytes - executes code, so a pod log (which the function itself could also have - printed to, or which another process's stray line could reach) is not - trusted on sight. This replaced an earlier implementation that printed - ``repr(result)`` and ran the log through ``ast.literal_eval``: anything - without a literal Python repr (a NumPy array, a dataclass, most real - objects) came back as the *string* of its repr, with no indication that - had happened. -6. **Failure retrieval** (``get_k8s_error_log`` / ``extract_k8s_exception``): - on a failed job, the manager reads the pod's log for lines starting - ``CLUSTRIX_ERROR:``/``CLUSTRIX_TRACEBACK:`` and re-raises a - ``RuntimeError`` carrying the remote message; if no such marker is found, - the raw error log is included in the exception instead of being - swallowed. -7. **Cleanup** (``cleanup_k8s_job``): if ``cleanup_on_success`` is set (the - default), the ``Job`` -- and its pods, via - ``propagation_policy="Foreground"`` -- is deleted after a successful - result is collected. A cleanup failure is logged as a warning, not - raised, so it never masks the real result or error. - -None of this has been exercised against a live API server in this session; -it is a description of what the code does, traced from source, not a record -of it having run. - -Kubernetes-specific Features ----------------------------- - -Resource Specification -~~~~~~~~~~~~~~~~~~~~~~ - -Kubernetes uses different resource syntax: - -.. important:: - - ``cores`` and ``memory`` are the only settings ``@cluster(...)`` actually - applies per job -- they become the pod's resource requests and limits, as - shown in `Resource Limits and Requests`_ below. ``@cluster(...)`` will - *accept* ``k8s_namespace``, ``k8s_image``, ``k8s_service_account`` and - ``k8s_pull_policy`` as keyword arguments without warning (they were added - to the recognised-extras list so a typo there is no longer silently - dropped at the decorator), but ``KubernetesJobManager.submit_k8s_job`` - never reads them back out of the per-job config -- it reads - ``self.config.k8s_namespace`` / ``self.config.k8s_image`` instead. So - passing them to ``@cluster(...)`` looks accepted and has no effect; set - them with ``configure()`` (see `Custom Docker Images`_ below) if you need - a namespace or image different from the default. - -.. code-block:: python - - from clustrix import cluster - - @cluster( - cores=2, # CPU cores (can be fractional: 0.5, 1.5) - memory="4Gi", # Memory in Kubernetes format - time="01:00:00", # Job timeout -- not currently enforced by the - # Kubernetes backend (no activeDeadlineSeconds - # is set from it); accepted for parity with the - # scheduler backends, which do use it. - ) - def k8s_computation(): - """Example computation on Kubernetes.""" - import numpy as np - import time - - print("Starting Kubernetes job...") - - # CPU-intensive computation - size = 3000 - matrix_a = np.random.rand(size, size) - matrix_b = np.random.rand(size, size) - - start_time = time.time() - result = np.dot(matrix_a, matrix_b) - end_time = time.time() - - return { - 'computation_time': end_time - start_time, - 'matrix_size': size, - 'result_trace': float(np.trace(result)), - 'result_frobenius_norm': float(np.linalg.norm(result, 'fro')) - } - - # Execute on Kubernetes - result = k8s_computation() - print(f"Computation completed in {result['computation_time']:.2f} seconds") - -Advanced Configuration ----------------------- - -Custom Docker Images -~~~~~~~~~~~~~~~~~~~~ - -For complex dependencies, use custom images: - -.. code-block:: python - - # First, create a Dockerfile for your requirements - """ - # Dockerfile - FROM python:3.11-slim - - RUN pip install numpy pandas scikit-learn matplotlib - RUN pip install torch torchvision # For ML workloads - - WORKDIR /app - CMD ["python"] - """ - - # Then configure Clustrix to use your image - configure( - cluster_type="kubernetes", - k8s_namespace="ml-compute", - k8s_image="your-registry/clustrix-ml:latest" - ) - - @cluster(cores=4, memory="8Gi") - def ml_computation(): - """Machine learning computation with custom image.""" - import torch - import numpy as np - - # Check GPU availability - device = torch.device("cuda" if torch.cuda.is_available() else "cpu") - print(f"Using device: {device}") - - # Create neural network - model = torch.nn.Sequential( - torch.nn.Linear(100, 50), - torch.nn.ReLU(), - torch.nn.Linear(50, 1) - ).to(device) - - # Generate synthetic data - X = torch.randn(1000, 100).to(device) - y = torch.randn(1000, 1).to(device) - - # Simple training loop - optimizer = torch.optim.Adam(model.parameters()) - loss_fn = torch.nn.MSELoss() - - losses = [] - for epoch in range(100): - optimizer.zero_grad() - predictions = model(X) - loss = loss_fn(predictions, y) - loss.backward() - optimizer.step() - losses.append(loss.item()) - - return { - 'device': str(device), - 'final_loss': losses[-1], - 'training_losses': losses[::10], # Every 10th loss - 'model_parameters': sum(p.numel() for p in model.parameters()) - } - -Resource Limits and Requests -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Configure resource limits for better cluster utilization: - -.. code-block:: python - - # The executor sets pod resource requests and limits to the same values, - # derived from cores and memory. There is no separate limit argument. - @cluster(cores=1, memory="2Gi") - def resource_managed_task(): - """Task with detailed resource management.""" - import psutil - import time - - # Monitor resource usage - process = psutil.Process() - - results = { - 'cpu_count': psutil.cpu_count(), - 'memory_total_gb': psutil.virtual_memory().total / (1024**3), - 'measurements': [] - } - - # Simulate varying workload - for i in range(10): - # CPU-intensive phase - start_time = time.time() - sum(x**2 for x in range(100000)) - end_time = time.time() - - # Measure current usage - cpu_percent = process.cpu_percent() - memory_mb = process.memory_info().rss / (1024**2) - - results['measurements'].append({ - 'step': i, - 'cpu_percent': cpu_percent, - 'memory_mb': memory_mb, - 'duration_ms': (end_time - start_time) * 1000 - }) - - time.sleep(1) - - return results - -Kubernetes-Native Examples --------------------------- - -Distributed Data Processing -~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - @cluster(cores=2, memory="4Gi") - def process_data_partition(partition_id, total_partitions, data_size=10000): - """Process a partition of a large dataset.""" - import numpy as np - import json - - print(f"Processing partition {partition_id}/{total_partitions}") - - # Simulate loading partition data - np.random.seed(partition_id) # Ensure reproducible partitions - partition_size = data_size // total_partitions - - # Generate partition data - data = np.random.rand(partition_size, 50) - labels = np.random.randint(0, 5, partition_size) - - # Process partition - results = { - 'partition_id': partition_id, - 'partition_size': partition_size, - 'feature_means': np.mean(data, axis=0).tolist(), - 'feature_stds': np.std(data, axis=0).tolist(), - 'label_distribution': { - str(label): int(count) - for label, count in zip(*np.unique(labels, return_counts=True)) - } - } - - return results - - # Process data in parallel across multiple Kubernetes jobs - total_partitions = 8 - partition_results = [] - - for partition_id in range(total_partitions): - result = process_data_partition(partition_id, total_partitions) - partition_results.append(result) - - # Aggregate results - total_samples = sum(r['partition_size'] for r in partition_results) - print(f"Processed {total_samples} samples across {total_partitions} partitions") - - # Compute global statistics - all_feature_means = np.array([r['feature_means'] for r in partition_results]) - global_feature_means = np.mean(all_feature_means, axis=0) - print(f"Global feature means: {global_feature_means[:5]}") # Show first 5 - -Microservices-Style Computing -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - @cluster(cores=1, memory="2Gi") - def image_processing_service(image_id, operations): - """Microservice for image processing.""" - import numpy as np - import json - - print(f"Processing image {image_id} with operations: {operations}") - - # Simulate image (random pixels) - height, width = 512, 512 - image = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8) - - results = { - 'image_id': image_id, - 'original_shape': image.shape, - 'operations_performed': [] - } - - # Apply operations - for operation in operations: - if operation == 'grayscale': - # Convert to grayscale - gray = np.dot(image[...,:3], [0.2989, 0.5870, 0.1140]) - image = np.stack([gray, gray, gray], axis=-1).astype(np.uint8) - results['operations_performed'].append('grayscale') - - elif operation == 'blur': - # Simple blur (average with neighbors) - from scipy import ndimage - for channel in range(3): - image[:,:,channel] = ndimage.uniform_filter( - image[:,:,channel].astype(float), size=3 - ).astype(np.uint8) - results['operations_performed'].append('blur') - - elif operation == 'edge_detect': - # Simple edge detection - edges = np.abs(np.diff(image.astype(float), axis=0)).sum(axis=-1) - edges = np.pad(edges, ((0,1), (0,0)), mode='constant') - results['edge_strength'] = float(np.mean(edges)) - results['operations_performed'].append('edge_detect') - - # Compute final statistics - results['final_mean_intensity'] = float(np.mean(image)) - results['final_std_intensity'] = float(np.std(image)) - - return results - - # Process multiple images with different operations - image_tasks = [ - {'id': 'img_001', 'ops': ['grayscale', 'blur']}, - {'id': 'img_002', 'ops': ['edge_detect']}, - {'id': 'img_003', 'ops': ['grayscale', 'edge_detect']}, - {'id': 'img_004', 'ops': ['blur', 'edge_detect']} - ] - - results = [] - for task in image_tasks: - result = image_processing_service(task['id'], task['ops']) - results.append(result) - - # Summary - for r in results: - print(f"Image {r['image_id']}: {', '.join(r['operations_performed'])}") - -Cloud-Native Best Practices ---------------------------- - -Auto-scaling Configuration -~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # Configure for auto-scaling environments - configure( - cluster_type="kubernetes", - k8s_namespace="auto-scale", - - # Resource settings that work well with auto-scaling - default_cores=1, # Start small - default_memory="2Gi", # Conservative memory - - # Job settings - k8s_backoff_limit=2, # Limited retries - ) - - @cluster(cores=0.5, memory="1Gi") # Fractional cores for efficiency - def lightweight_task(task_id): - """Lightweight task suitable for auto-scaling.""" - import time - import random - - # Variable processing time - processing_time = random.uniform(10, 60) # 10-60 seconds - - print(f"Task {task_id} starting (estimated {processing_time:.1f}s)") - - # Simulate work - start_time = time.time() - time.sleep(processing_time) - end_time = time.time() - - return { - 'task_id': task_id, - 'estimated_time': processing_time, - 'actual_time': end_time - start_time, - 'efficiency': processing_time / (end_time - start_time) - } - -Fault Tolerance -~~~~~~~~~~~~~~~ - -.. code-block:: python - - @cluster(cores=2, memory="4Gi") - def fault_tolerant_computation(data_chunk_id, retry_count=0): - """Computation with built-in fault tolerance.""" - import random - import time - import numpy as np - - print(f"Processing chunk {data_chunk_id} (attempt {retry_count + 1})") - - # Simulate random failures (20% chance) - if random.random() < 0.2 and retry_count < 2: - raise RuntimeError(f"Simulated failure in chunk {data_chunk_id}") - - # Simulate computation - chunk_size = 1000 - data = np.random.rand(chunk_size, 100) - - # Add checkpointing for long computations - checkpoint_interval = 200 - results = [] - - for i in range(0, chunk_size, checkpoint_interval): - end_idx = min(i + checkpoint_interval, chunk_size) - batch = data[i:end_idx] - - # Process batch - batch_result = np.mean(batch, axis=0) - results.append(batch_result) - - print(f"Checkpoint: processed {end_idx}/{chunk_size} samples") - time.sleep(0.1) # Small delay - - # Combine results - final_result = np.mean(results, axis=0) - - return { - 'chunk_id': data_chunk_id, - 'chunk_size': chunk_size, - 'checkpoints': len(results), - 'result_mean': float(np.mean(final_result)), - 'result_std': float(np.std(final_result)), - 'retry_count': retry_count - } - - # Process multiple chunks with fault tolerance - chunk_ids = range(10) - successful_results = [] - - for chunk_id in chunk_ids: - try: - result = fault_tolerant_computation(chunk_id) - successful_results.append(result) - print(f"✓ Chunk {chunk_id} completed successfully") - except Exception as e: - print(f"✗ Chunk {chunk_id} failed after retries: {e}") - - print(f"Successfully processed {len(successful_results)}/{len(chunk_ids)} chunks") - -Monitoring and Logging ----------------------- - -Kubernetes Job Monitoring -~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - @cluster(cores=2, memory="4Gi") - def monitored_computation(): - """Computation with comprehensive monitoring.""" - import time - import psutil - import logging - import json - - # Set up logging - logging.basicConfig(level=logging.INFO) - logger = logging.getLogger(__name__) - - # Monitoring data - monitor_data = { - 'start_time': time.time(), - 'resource_snapshots': [], - 'milestones': [] - } - - def log_resources(milestone): - """Log current resource usage.""" - snapshot = { - 'timestamp': time.time(), - 'milestone': milestone, - 'cpu_percent': psutil.cpu_percent(interval=1), - 'memory_mb': psutil.virtual_memory().used / (1024**2), - 'memory_percent': psutil.virtual_memory().percent - } - monitor_data['resource_snapshots'].append(snapshot) - logger.info(f"Milestone '{milestone}': CPU {snapshot['cpu_percent']:.1f}%, " - f"Memory {snapshot['memory_mb']:.1f}MB") - - try: - log_resources("computation_start") - - # Phase 1: Data preparation - import numpy as np - data = np.random.rand(5000, 1000) - monitor_data['milestones'].append("data_prepared") - log_resources("data_preparation_complete") - - # Phase 2: Computation - result = np.linalg.svd(data, compute_uv=False) - monitor_data['milestones'].append("computation_complete") - log_resources("computation_complete") - - # Phase 3: Analysis - analysis = { - 'singular_values_count': len(result), - 'max_singular_value': float(np.max(result)), - 'min_singular_value': float(np.min(result)), - 'condition_number': float(np.max(result) / np.min(result)) - } - monitor_data['milestones'].append("analysis_complete") - log_resources("analysis_complete") - - monitor_data['end_time'] = time.time() - monitor_data['total_duration'] = monitor_data['end_time'] - monitor_data['start_time'] - - return { - 'analysis_results': analysis, - 'monitoring_data': monitor_data, - 'success': True - } - - except Exception as e: - logger.error(f"Computation failed: {e}") - monitor_data['error'] = str(e) - monitor_data['end_time'] = time.time() - raise - -Complete Kubernetes Example ---------------------------- - -Distributed Machine Learning -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - from clustrix import configure, cluster - import numpy as np - - # Configure for ML workloads - configure( - cluster_type="kubernetes", - k8s_namespace="ml-compute", - k8s_image="python:3.11-slim", - - # Default resources for ML tasks - default_cores=2, - default_memory="4Gi", - k8s_backoff_limit=1 # Single retry - ) - - @cluster(cores=4, memory="8Gi") - def distributed_training_worker(worker_id, total_workers, epochs=100): - """Distributed training worker for machine learning.""" - import numpy as np - from sklearn.datasets import make_classification - from sklearn.ensemble import RandomForestClassifier - from sklearn.model_selection import train_test_split - from sklearn.metrics import accuracy_score, classification_report - import time - import json - - print(f"Worker {worker_id}/{total_workers} starting training...") - - # Generate worker-specific dataset - np.random.seed(worker_id) # Ensure different data per worker - - X, y = make_classification( - n_samples=10000, - n_features=50, - n_informative=30, - n_redundant=10, - n_classes=5, - random_state=worker_id - ) - - # Split data - X_train, X_test, y_train, y_test = train_test_split( - X, y, test_size=0.2, random_state=worker_id - ) - - print(f"Worker {worker_id}: Dataset prepared ({len(X_train)} training samples)") - - # Train model - start_time = time.time() - - model = RandomForestClassifier( - n_estimators=epochs, - max_depth=10, - random_state=worker_id, - n_jobs=-1 # Use all available cores - ) - - model.fit(X_train, y_train) - training_time = time.time() - start_time - - # Evaluate model - y_pred = model.predict(X_test) - accuracy = accuracy_score(y_test, y_pred) - - # Feature importance - feature_importance = model.feature_importances_ - top_features = np.argsort(feature_importance)[-10:] # Top 10 features - - results = { - 'worker_id': worker_id, - 'total_workers': total_workers, - 'training_samples': len(X_train), - 'test_samples': len(X_test), - 'training_time_seconds': training_time, - 'accuracy': float(accuracy), - 'top_feature_indices': top_features.tolist(), - 'top_feature_importance': feature_importance[top_features].tolist(), - 'model_parameters': { - 'n_estimators': epochs, - 'max_depth': 10 - } - } - - print(f"Worker {worker_id} completed: accuracy = {accuracy:.4f}") - return results - - # Run distributed training - total_workers = 6 - print(f"Starting distributed training with {total_workers} workers...") - - worker_results = [] - for worker_id in range(total_workers): - result = distributed_training_worker(worker_id, total_workers, epochs=150) - worker_results.append(result) - - # Aggregate results - accuracies = [r['accuracy'] for r in worker_results] - training_times = [r['training_time_seconds'] for r in worker_results] - - print("\nDistributed Training Results:") - print(f"Average accuracy: {np.mean(accuracies):.4f} ± {np.std(accuracies):.4f}") - print(f"Average training time: {np.mean(training_times):.2f}s ± {np.std(training_times):.2f}s") - print(f"Total training samples: {sum(r['training_samples'] for r in worker_results)}") - - # Find best performing worker - best_worker = max(worker_results, key=lambda x: x['accuracy']) - print(f"Best worker: {best_worker['worker_id']} (accuracy: {best_worker['accuracy']:.4f})") - -The examples above show the *intended* interface for containerized distributed -computing, auto-scaling-friendly resource requests, fault tolerance via -``k8s_backoff_limit``, and log-based monitoring. As stated at the top of this -page, none of it has been run against a real Kubernetes cluster in this -project -- the code paths are traced from source in `Behind the Scenes: How -a Job Actually Runs`_ above, not demonstrated end to end. Treat every example -here as something to try and verify yourself, not as a report of a -successful run. \ No newline at end of file diff --git a/docs/source/tutorials/pbs_tutorial.rst b/docs/source/tutorials/pbs_tutorial.rst deleted file mode 100644 index 4d8ca711..00000000 --- a/docs/source/tutorials/pbs_tutorial.rst +++ /dev/null @@ -1,632 +0,0 @@ -PBS/Torque Cluster Tutorial -=========================== - -This tutorial demonstrates how to use Clustrix with PBS (Portable Batch System) and Torque clusters, commonly used in academic and research computing environments. - -.. warning:: - - The PBS backend is implemented but has **not been verified against real - PBS hardware.** It shares its job-directory staging, environment build - and job-execution code with the SLURM and SSH backends (which *are* - verified) through ``clustrix/utils.py::job_execution_lines`` -- it is not - a separate, untested code path bolted on beside them -- but nobody has - run it against a live PBS/Torque scheduler. An older version of this - backend generated a script that invoked a file - (``execute_function.py``) nothing in clustrix ever created and never - built a venv for it to run in; both defects are fixed in the current - code, but "fixed in code" is not the same claim as "confirmed against a - scheduler." Treat this tutorial as a description of the intended - interface, not as a record of something that has been run to completion. - -Prerequisites -------------- - -1. Access to a PBS/Torque cluster -2. SSH key setup (see :doc:`../ssh_setup`) -3. Clustrix installed with: ``pip install clustrix`` - -What Happens When You Call a ``@cluster``-Decorated Function --------------------------------------------------------------- - -The submission pipeline is identical to SLURM's (see -:doc:`slurm_tutorial`'s "What Happens" section for the full ten-step -sequence: serialize, connect with host-key verification, stage a ``0700`` -job directory with a random result-signing key, upload -``function_data.pkl``, build a two-venv environment, generate and upload -the job script, submit, poll, verify-then-deserialize the signed result, -clean up). The PBS-specific differences are: - -- **Submission command**: ``qsub job.pbs`` instead of ``sbatch job.sh``. - The job ID is whatever ``qsub`` prints to stdout, taken verbatim (PBS - implementations vary in exact format, unlike SLURM's fixed - ``Submitted batch job ``). -- **Job script directives**: ``#PBS`` lines instead of ``#SBATCH``, using - PBS's own resource syntax (below). -- **Queue vs. partition**: PBS uses ``queue=`` where SLURM uses - ``partition=``. - -What the Generated Job Script Looks Like -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -For ``@cluster(cores=8, memory="16GB", time="02:00:00", queue="batch")``, -``job.pbs`` looks like this. As with SLURM, ``module_loads``, -``environment_variables`` and ``pre_execution_commands`` are inserted -between the ``#PBS`` block and execution, and the memory string is -normalized to what PBS's ``-l mem=`` accepts -- ``"16GB"`` becomes -``mem=16gb`` (lowercase, unlike SLURM's ``G``): - -.. code-block:: bash - - #!/bin/bash - #PBS -N clustrix - #PBS -o /home/you/clustrix/job_.../job.out - #PBS -e /home/you/clustrix/job_.../job.err - #PBS -l nodes=1:ppn=8 - #PBS -l mem=16gb - #PBS -l walltime=02:00:00 - #PBS -q batch - module load python/3.11 # from module_loads, if set - export CLUSTRIX_RESULT_KEY=$(cat .../.clustrix_result_key 2>/dev/null || true) - cd /home/you/clustrix/job_... - source venv/bin/activate # or the two-venv activation sequence - python -c " - # same execution/signing body as SLURM: unpickle function_data.pkl - # with dill, run the function, write signed result.pkl or error.pkl - " - -As with SLURM, there is no pass-through for arbitrary ``qsub``/PBS -directives beyond ``cores``, ``memory``, ``time`` and ``queue`` -- use -``pre_execution_commands`` for anything else your site's PBS install -requires. - -When Things Fail -~~~~~~~~~~~~~~~~~ - -Because this backend is unverified against real hardware, treat any -failure here with extra suspicion -- it may be exposing a real defect in -the PBS-specific parsing (job ID extraction, ``-l`` resource syntax) that -SLURM's test coverage never exercised. In addition to the checks in the -SLURM tutorial: - -- **Job ID parsing looks wrong**: ``submit_pbs_job`` takes ``qsub``'s - entire stripped stdout as the job ID, with no format validation. If your - site's PBS wraps that output (a banner line, a trailing newline with - extra text), status polling will look up the wrong ID. Check - ``qstat -f `` directly against what clustrix printed. -- **Resource string rejected by PBS**: confirm your site's PBS accepts - ``nodes=1:ppn=N`` and ``mem=gb`` -- some Torque/PBS Pro - configurations expect ``select=1:ncpus=N:mem=gb`` instead, which - clustrix does not currently generate. - -Configuration Options ---------------------- - -**Option 1: Interactive Widget (Recommended for Jupyter)** - -For Jupyter notebook users, use the interactive configuration widget: - -Importing ``clustrix`` registers the magic but does not display anything. Run -``%%remote`` in a cell of its own to open the widget, and select ``pbs`` as the -cluster type to reveal the connection fields: - -.. code-block:: ipython3 - - %%remote - -**Option 2: Programmatic Configuration** - -Configure Clustrix programmatically for your PBS cluster: - -.. code-block:: python - - from clustrix import configure - - configure( - cluster_type="pbs", - cluster_host="pbs.university.edu", - username="your_username", - key_file="~/.ssh/pbs_key", - remote_work_dir="/home/your_username/clustrix" - ) - -PBS Resource Specification --------------------------- - -PBS uses different resource syntax compared to SLURM: - -.. code-block:: python - - # cluster-required: submits a real job to a live PBS cluster - from clustrix import cluster - - @cluster( - cores=8, # Number of CPU cores - memory="16GB", # Memory requirement - time="02:00:00", # Wall time (HH:MM:SS) - queue="batch", # PBS queue name - ) - def pbs_computation(): - """Example computation on PBS cluster.""" - import numpy as np - - # Create large matrix - size = 5000 - matrix_a = np.random.rand(size, size) - matrix_b = np.random.rand(size, size) - - # Matrix multiplication - result = np.dot(matrix_a, matrix_b) - - return { - 'shape': result.shape, - 'mean': float(np.mean(result)), - 'std': float(np.std(result)) - } - - # Execute on PBS cluster - result = pbs_computation() - print(f"Matrix computation result: {result}") - -Advanced PBS Configuration --------------------------- - -Environment and Queue Setup -~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - configure( - cluster_type="pbs", - cluster_host="torque.research.org", - username="researcher", - - # PBS-specific settings - default_queue="normal", # Default queue - default_time="04:00:00", # Default wall time - - # Resource defaults - default_cores=4, - default_memory="8GB", - - # Environment setup - environment_variables={ - "PBS_O_WORKDIR": "/home/researcher/work", - "OMP_NUM_THREADS": "4" - } - ) - -Configuration File for PBS -~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Create ``~/.clustrix/config.yml``: - -.. code-block:: yaml - - cluster_type: "pbs" - cluster_host: "pbs.cluster.edu" - username: "researcher" - key_file: "~/.ssh/pbs_key" - remote_work_dir: "/home/researcher/clustrix" - - # PBS-specific settings - default_queue: "batch" - default_time: "02:00:00" - - # Resource defaults - default_cores: 8 - default_memory: "16GB" - - # Job management - job_poll_interval: 30 # Check job status every 30 seconds - cleanup_on_success: true - -PBS Job Examples ----------------- - -Array-style Processing -~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # cluster-required: submits real jobs to a live PBS cluster - @cluster(cores=4, memory="8GB", queue="batch") - def process_file(file_id, operation="mean"): - """Process a single file.""" - import numpy as np - import time - - # Simulate file processing - print(f"Processing file {file_id} with operation: {operation}") - - # Generate synthetic data (simulating file loading) - data = np.random.rand(10000, 100) * file_id - - if operation == "mean": - result = np.mean(data) - elif operation == "std": - result = np.std(data) - elif operation == "sum": - result = np.sum(data) - else: - result = np.median(data) - - # Simulate processing time - time.sleep(1) - - return { - 'file_id': file_id, - 'operation': operation, - 'result': float(result), - 'data_shape': data.shape - } - - # Process multiple files - file_ids = range(1, 11) # Files 1-10 - results = [] - - for file_id in file_ids: - result = process_file(file_id, operation="mean") - results.append(result) - - print(f"Processed {len(results)} files") - for r in results[:3]: # Show first 3 results - print(f"File {r['file_id']}: {r['result']:.4f}") - -Bioinformatics Pipeline -~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # cluster-required: submits real jobs to a live PBS cluster - @cluster(cores=8, memory="32GB", time="06:00:00", queue="bioqueue") - def analyze_genome_sequence(sequence_id, analysis_params): - """Analyze a genome sequence.""" - import random - import string - - # Simulate sequence analysis - print(f"Analyzing sequence {sequence_id}") - - # Generate mock sequence - bases = ['A', 'T', 'G', 'C'] - sequence_length = analysis_params.get('length', 100000) - sequence = ''.join(random.choices(bases, k=sequence_length)) - - # Mock analysis results - gc_content = (sequence.count('G') + sequence.count('C')) / len(sequence) - - # Simulate finding patterns - patterns_found = [] - for i in range(5): - pattern_length = random.randint(5, 10) - pattern = ''.join(random.choices(bases, k=pattern_length)) - count = sequence.count(pattern) - if count > 0: - patterns_found.append({ - 'pattern': pattern, - 'count': count, - 'frequency': count / (len(sequence) - pattern_length + 1) - }) - - return { - 'sequence_id': sequence_id, - 'sequence_length': len(sequence), - 'gc_content': gc_content, - 'patterns_found': patterns_found, - 'analysis_params': analysis_params - } - - # Analyze multiple sequences - sequences = [ - {'id': 'seq_001', 'params': {'length': 50000}}, - {'id': 'seq_002', 'params': {'length': 75000}}, - {'id': 'seq_003', 'params': {'length': 100000}} - ] - - results = [] - for seq in sequences: - result = analyze_genome_sequence(seq['id'], seq['params']) - results.append(result) - - # Summary statistics - avg_gc = sum(r['gc_content'] for r in results) / len(results) - print(f"Average GC content: {avg_gc:.3f}") - -PBS Job Management ------------------- - -Resource Monitoring -~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # cluster-required: submits a real job to a live PBS cluster - @cluster(cores=4, memory="8GB", time="01:00:00") - def resource_intensive_task(): - """Task that monitors its resource usage.""" - import psutil - import time - import numpy as np - - # Get initial resource info - process = psutil.Process() - initial_memory = process.memory_info().rss / 1024 / 1024 # MB - - print(f"Initial memory usage: {initial_memory:.2f} MB") - - # Gradually increase memory usage - data_chunks = [] - for i in range(10): - # Create 100MB of data - chunk = np.random.rand(100, 1250, 1000) # ~100MB - data_chunks.append(chunk) - - current_memory = process.memory_info().rss / 1024 / 1024 - print(f"Step {i+1}: Memory usage: {current_memory:.2f} MB") - - time.sleep(5) # Wait 5 seconds - - # Final computation - total_sum = sum(np.sum(chunk) for chunk in data_chunks) - final_memory = process.memory_info().rss / 1024 / 1024 - - return { - 'initial_memory_mb': initial_memory, - 'final_memory_mb': final_memory, - 'memory_increase_mb': final_memory - initial_memory, - 'computation_result': float(total_sum), - 'chunks_processed': len(data_chunks) - } - - result = resource_intensive_task() - print(f"Memory increased by: {result['memory_increase_mb']:.2f} MB") - -Error Handling and Debugging ----------------------------- - -Handling PBS-specific Errors -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # cluster-required: submits real jobs to a live PBS cluster - @cluster(cores=2, memory="4GB", queue="debug") - def debug_function(test_case="success"): - """Function for testing error handling.""" - - if test_case == "memory_error": - # Try to allocate too much memory - import numpy as np - huge_array = np.zeros((100000, 100000)) # ~80GB - return "This shouldn't succeed" - - elif test_case == "time_limit": - # Exceed time limit - import time - time.sleep(7200) # 2 hours - return "This took too long" - - elif test_case == "import_error": - # Simulate a package missing from the remote environment. Written - # as importlib.import_module() rather than a literal `import` - # statement so the name doesn't have to resolve to a real, - # installed package just to demonstrate the failure mode. - import importlib - importlib.import_module("nonexistent_package") - return "This package doesn't exist" - - else: - # Successful execution - return f"Test case '{test_case}' completed successfully" - - # Test different scenarios - test_cases = ["success", "import_error"] # Start with safe tests - - for case in test_cases: - try: - result = debug_function(case) - print(f"✓ {case}: {result}") - except Exception as e: - print(f"✗ {case}: {type(e).__name__}: {e}") - -Debugging with Logs -~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # cluster-required: submits a real job to a live PBS cluster - import logging - logging.basicConfig(level=logging.DEBUG) - - from clustrix import configure, cluster - - # Enable detailed logging - configure( - cluster_type="pbs", - cluster_host="pbs.cluster.edu", - username="your_user" - ) - - @cluster(cores=2, memory="4GB") - def logged_function(): - """Function with detailed logging.""" - import logging - - # Create logger for remote execution - logger = logging.getLogger(__name__) - logger.info("Starting computation") - - try: - import numpy as np - data = np.random.rand(1000, 1000) - result = np.mean(data) - logger.info(f"Computation successful: {result}") - return result - except Exception as e: - logger.error(f"Computation failed: {e}") - raise - - result = logged_function() - -Best Practices for PBS ----------------------- - -Queue Selection Strategy -~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - from clustrix import cluster - - def select_pbs_queue(cores, memory_gb, time_hours): - """Select appropriate PBS queue based on resources.""" - - if time_hours <= 1 and cores <= 4: - return "express" # Fast turnaround for small jobs - elif time_hours <= 4 and cores <= 16: - return "normal" # Standard queue - elif time_hours <= 24: - return "long" # Long-running jobs - elif cores > 32: - return "bigmem" # High-memory/high-core jobs - else: - return "batch" # Default fallback - - # Use dynamic queue selection - cores = 8 - memory_gb = 32 - time_hours = 6 - - selected_queue = select_pbs_queue(cores, memory_gb, time_hours) - - @cluster(cores=cores, memory=f"{memory_gb}GB", - time=f"{time_hours:02d}:00:00", queue=selected_queue) - def adaptive_computation(): - return "Computation with optimal queue selection" - -Efficient Data Handling -~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # cluster-required: submits a real job to a live PBS cluster - @cluster(cores=4, memory="16GB", time="03:00:00") - def efficient_data_processing(chunk_size=1000): - """Process data in chunks to manage memory.""" - import numpy as np - - total_sum = 0 - chunk_count = 0 - - # Process data in chunks to avoid memory issues - for i in range(100): # 100 chunks - # Generate chunk - chunk = np.random.rand(chunk_size, chunk_size) - - # Process chunk - chunk_sum = np.sum(chunk) - total_sum += chunk_sum - chunk_count += 1 - - # Clear memory - del chunk - - if i % 10 == 0: - print(f"Processed {i+1} chunks") - - return { - 'total_sum': float(total_sum), - 'chunks_processed': chunk_count, - 'average_chunk_sum': float(total_sum / chunk_count) - } - - result = efficient_data_processing() - print(f"Processed {result['chunks_processed']} chunks efficiently") - -Complete PBS Example --------------------- - -Scientific Computing Workflow -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code-block:: python - - # cluster-required: submits real jobs to a live PBS cluster - from clustrix import configure, cluster - import numpy as np - - # Configure PBS cluster - configure( - cluster_type="pbs", - cluster_host="pbs.research.edu", - username="scientist", - remote_work_dir="/home/scientist/clustrix", - - # PBS-specific settings - default_queue="normal", - default_time="04:00:00", - - # Default resources - default_cores=8, - default_memory="16GB", - - # Environment - environment_variables={ - "OMP_NUM_THREADS": "8", - "TMPDIR": "/tmp" - } - ) - - @cluster(cores=16, memory="32GB", time="06:00:00", queue="compute") - def monte_carlo_integration(n_samples, dimensions): - """Monte Carlo integration in high dimensions.""" - import numpy as np - import time - - start_time = time.time() - - def integrand(x): - """Function to integrate: exp(-sum(x^2))""" - return np.exp(-np.sum(x**2, axis=-1)) - - # Generate random samples in [-1, 1]^dimensions - samples = np.random.uniform(-1, 1, (n_samples, dimensions)) - - # Evaluate integrand - values = integrand(samples) - - # Monte Carlo estimate - volume = 2**dimensions # Volume of [-1,1]^d - integral_estimate = volume * np.mean(values) - error_estimate = volume * np.std(values) / np.sqrt(n_samples) - - end_time = time.time() - - return { - 'dimensions': dimensions, - 'n_samples': n_samples, - 'integral_estimate': float(integral_estimate), - 'error_estimate': float(error_estimate), - 'computation_time': end_time - start_time, - 'samples_per_second': n_samples / (end_time - start_time) - } - - # Run integration for different dimensions - dimensions_list = [2, 4, 6, 8] - n_samples = 1000000 - - results = [] - for dim in dimensions_list: - print(f"Computing {dim}D integral...") - result = monte_carlo_integration(n_samples, dim) - results.append(result) - print(f" Result: {result['integral_estimate']:.6f} ± {result['error_estimate']:.6f}") - print(f" Time: {result['computation_time']:.2f}s") - - # Analysis - print("\nSummary:") - for r in results: - efficiency = r['samples_per_second'] / 1000 # K samples/sec - print(f"{r['dimensions']}D: {r['integral_estimate']:.4f} ({efficiency:.1f}K samples/s)") - -This tutorial provides comprehensive coverage of using Clustrix with PBS/Torque clusters, including resource specification, job management, and best practices for scientific computing workloads. \ No newline at end of file diff --git a/tests/integration/test_aws_eks_auto.py b/tests/integration/test_aws_eks_auto.py deleted file mode 100644 index ac7ed19c..00000000 --- a/tests/integration/test_aws_eks_auto.py +++ /dev/null @@ -1,148 +0,0 @@ -#!/usr/bin/env python -""" -Automatic test for AWS EKS provisioning (no confirmation prompt). -This will create a real EKS cluster - costs will be incurred! -""" - -import sys -import time -import traceback -from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec -from clustrix.credential_manager import FlexibleCredentialManager - - -def test_aws_eks_provisioning(): - """Test AWS EKS cluster provisioning with minimal resources.""" - - print("=" * 60) - print("AWS EKS Provisioning Test (AUTOMATIC)") - print("=" * 60) - print("\n⚠️ WARNING: Creating real AWS resources!") - print(" Estimated cost: ~$0.28/hour") - print(" Resources: EKS cluster, VPC, subnets, IAM roles") - print("\n" + "=" * 60) - - print("\n✅ Starting AWS EKS provisioning...") - - # Get credentials - credential_manager = FlexibleCredentialManager() - aws_creds = credential_manager.ensure_credential("aws") - - if not aws_creds: - print("❌ No AWS credentials found") - return False - - print(f" Using AWS account: {aws_creds.get('account_id', 'unknown')}") - print(f" Region: {aws_creds.get('region', 'us-east-1')}") - - # Create minimal cluster spec - spec = ClusterSpec( - cluster_name=f"clustrix-test-{int(time.time())}", - provider="aws", - region=aws_creds.get("region", "us-east-1"), - node_count=1, # Minimal - just 1 node - node_type="t3.small", # Smaller instance to save costs - kubernetes_version="1.27", - ) - - print(f"\n📋 Cluster Configuration:") - print(f" Name: {spec.cluster_name}") - print(f" Nodes: {spec.node_count} x {spec.node_type}") - print(f" Region: {spec.region}") - - # Create provisioner - print("\n🔧 Initializing provisioner...") - try: - provisioner = AWSEKSFromScratchProvisioner(aws_creds, spec.region) - provisioner.spec = spec # Set the spec after initialization - except Exception as e: - print(f"❌ Failed to initialize provisioner: {e}") - traceback.print_exc() - return False - - try: - print("\n🚀 Creating EKS cluster...") - print(" Step 1: Creating VPC and networking...") - print(" Step 2: Creating IAM roles...") - print(" Step 3: Creating EKS cluster...") - print(" Step 4: Creating node group...") - print("\n This will take 10-15 minutes...\n") - - # Start provisioning - cluster_info = provisioner.provision_complete_infrastructure(spec) - - if cluster_info: - print("\n✅ Cluster created successfully!") - print(f" Cluster Name: {cluster_info.get('name')}") - print(f" Endpoint: {cluster_info.get('endpoint')}") - print(f" Status: {cluster_info.get('status')}") - - # Save cluster info for cleanup - filename = f"cluster_info_{spec.cluster_name}.txt" - with open(filename, "w") as f: - f.write(f"Cluster Name: {spec.cluster_name}\n") - f.write(f"Region: {spec.region}\n") - f.write(f"Created: {time.strftime('%Y-%m-%d %H:%M:%S')}\n") - f.write(f"Endpoint: {cluster_info.get('endpoint', 'N/A')}\n") - f.write("\nTo destroy this cluster, run:\n") - f.write( - f"python destroy_cluster.py {spec.cluster_name} {spec.region}\n" - ) - - print(f"\n📝 Cluster info saved to: {filename}") - print("\n⚠️ IMPORTANT: Remember to destroy the cluster when done!") - print( - f" Run: python destroy_cluster.py {spec.cluster_name} {spec.region}" - ) - - # Test cluster connectivity - print("\n🔍 Testing cluster connectivity...") - try: - kubeconfig = provisioner.get_kubeconfig() - if kubeconfig: - print(" ✅ Kubeconfig retrieved successfully") - else: - print(" ⚠️ Could not retrieve kubeconfig") - except Exception as e: - print(f" ⚠️ Error getting kubeconfig: {e}") - - return True - else: - print("\n❌ Cluster creation failed (no cluster info returned)") - return False - - except KeyboardInterrupt: - print("\n\n⚠️ Provisioning interrupted!") - print(" Check AWS console for any resources that need cleanup") - print(f" Cluster name was: {spec.cluster_name}") - return False - except Exception as e: - print(f"\n❌ Error during provisioning: {e}") - print("\nFull error details:") - traceback.print_exc() - print("\n📋 Troubleshooting:") - print(" 1. Check AWS Console for partial resources") - print(" 2. Review CloudFormation stacks if any were created") - print(" 3. Check IAM roles and VPC resources") - print(f" 4. Look for resources tagged with: {spec.cluster_name}") - return False - - -if __name__ == "__main__": - print("🚀 Starting automatic EKS provisioning test...") - print(" (No confirmation required - will proceed automatically)") - print("") - - success = test_aws_eks_provisioning() - - if success: - print("\n" + "=" * 60) - print("✅ TEST SUCCESSFUL!") - print("=" * 60) - else: - print("\n" + "=" * 60) - print("❌ TEST FAILED!") - print("=" * 60) - - sys.exit(0 if success else 1) diff --git a/tests/integration/test_aws_eks_debug.py b/tests/integration/test_aws_eks_debug.py deleted file mode 100644 index 5e641c12..00000000 --- a/tests/integration/test_aws_eks_debug.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python -"""Debug AWS EKS provisioning - test initial steps only. - -This file is a diagnostic *script*, not a pytest module: it contains no test -functions. Its body previously ran at module scope, which meant that merely -importing it fetched real AWS credentials, constructed a boto3 client, called -the EKS API, and could `sys.exit(1)` mid-collection (crashing pytest with -INTERNALERROR). - -The body now lives in `main()` behind a `__main__` guard so that importing this -module is inert. See issue #109: the directory-level gate in conftest.py does -not protect against a path being named explicitly on the pytest command line, -because collect_ignore_glob only filters directory traversal. - -Run deliberately with: - - python tests/integration/test_aws_eks_debug.py -""" - -import sys -import traceback - -from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec -from clustrix.credential_manager import FlexibleCredentialManager - - -def main(): - # Get credentials - print("Getting AWS credentials...") - credential_manager = FlexibleCredentialManager() - aws_creds = credential_manager.ensure_credential("aws") - - if not aws_creds: - print("❌ No AWS credentials found") - return 1 - - print(f"✅ Got credentials for account: {aws_creds.get('account_id', 'unknown')}") - print(f" Region: {aws_creds.get('region', 'us-east-1')}") - - # Create spec - spec = ClusterSpec( - cluster_name="test-debug", - provider="aws", - region=aws_creds.get("region", "us-east-1"), - node_count=1, - node_type="t3.small", - kubernetes_version="1.27", - ) - - print(f"\nCluster spec created: {spec.cluster_name}") - - # Initialize provisioner - print("\nInitializing provisioner...") - try: - provisioner = AWSEKSFromScratchProvisioner(aws_creds, spec.region) - print("✅ Provisioner initialized") - print(f" Provisioner: {provisioner}") - - # Check AWS connectivity - print("\nTesting AWS connectivity...") - import boto3 - - eks = boto3.client( - "eks", - aws_access_key_id=aws_creds["access_key_id"], - aws_secret_access_key=aws_creds["secret_access_key"], - region_name=spec.region, - ) - - clusters = eks.list_clusters() - print(f"✅ Can list EKS clusters. Found: {clusters.get('clusters', [])}") - - # Test VPC creation - print("\nWould create VPC with:") - print(f" - Name: eks-vpc-{spec.cluster_name}") - print(" - CIDR: 10.0.0.0/16") - print(f" - Region: {spec.region}") - - except Exception as e: - print(f"❌ Error: {e}") - traceback.print_exc() - return 1 - - print("\n✅ All pre-flight checks passed!") - print("\nTo run full provisioning, use: python test_aws_eks_auto.py") - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/integration/test_aws_eks_minimal.py b/tests/integration/test_aws_eks_minimal.py deleted file mode 100644 index 998613b5..00000000 --- a/tests/integration/test_aws_eks_minimal.py +++ /dev/null @@ -1,108 +0,0 @@ -#!/usr/bin/env python -""" -Minimal test for AWS EKS provisioning. -This will create a real EKS cluster - costs will be incurred! -""" - -import sys -import time -from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec -from clustrix.credential_manager import FlexibleCredentialManager - - -def test_aws_eks_provisioning(): - """Test AWS EKS cluster provisioning with minimal resources.""" - - print("=" * 60) - print("AWS EKS Provisioning Test (MINIMAL)") - print("=" * 60) - print("\n⚠️ WARNING: This will create real AWS resources!") - print(" Estimated cost: ~$0.28/hour") - print(" Resources: EKS cluster, VPC, subnets, IAM roles") - print("\n" + "=" * 60) - - # Get confirmation - response = input("\n🔴 Type 'yes' to proceed with provisioning: ") - if response.lower() != "yes": - print("❌ Provisioning cancelled") - return False - - print("\n✅ Starting AWS EKS provisioning...") - - # Get credentials - credential_manager = FlexibleCredentialManager() - aws_creds = credential_manager.ensure_credential("aws") - - if not aws_creds: - print("❌ No AWS credentials found") - return False - - print(f" Using AWS account: {aws_creds.get('account_id', 'unknown')}") - print(f" Region: {aws_creds.get('region', 'us-east-1')}") - - # Create minimal cluster spec - spec = ClusterSpec( - name=f"clustrix-test-{int(time.time())}", - provider="aws", - region=aws_creds.get("region", "us-east-1"), - node_count=1, # Minimal - just 1 node - node_type="t3.small", # Smaller instance to save costs - disk_size=20, # Minimal disk - kubernetes_version="1.27", - ) - - print(f"\n📋 Cluster Configuration:") - print(f" Name: {spec.name}") - print(f" Nodes: {spec.node_count} x {spec.node_type}") - print(f" Region: {spec.region}") - - # Create provisioner - provisioner = AWSEKSFromScratchProvisioner(spec, aws_creds) - - try: - print("\n🚀 Creating EKS cluster...") - print(" This will take 10-15 minutes...") - - # Start provisioning - cluster_info = provisioner.create_cluster() - - if cluster_info: - print("\n✅ Cluster created successfully!") - print(f" Cluster Name: {cluster_info.get('name')}") - print(f" Endpoint: {cluster_info.get('endpoint')}") - print(f" Status: {cluster_info.get('status')}") - - # Save cluster info for cleanup - with open(f"cluster_info_{spec.name}.txt", "w") as f: - f.write(f"Cluster Name: {spec.name}\n") - f.write(f"Region: {spec.region}\n") - f.write(f"Created: {time.strftime('%Y-%m-%d %H:%M:%S')}\n") - f.write("\nTo destroy this cluster, run:\n") - f.write(f"python destroy_cluster.py {spec.name} {spec.region}\n") - - print(f"\n📝 Cluster info saved to: cluster_info_{spec.name}.txt") - print("\n⚠️ IMPORTANT: Remember to destroy the cluster when done!") - print(f" Run: python destroy_cluster.py {spec.name} {spec.region}") - - return True - else: - print("\n❌ Cluster creation failed") - return False - - except KeyboardInterrupt: - print("\n\n⚠️ Provisioning interrupted!") - print(" Check AWS console for any resources that need cleanup") - return False - except Exception as e: - print(f"\n❌ Error during provisioning: {e}") - print("\n📋 Troubleshooting:") - print(" 1. Check AWS Console for partial resources") - print(" 2. Review CloudFormation stacks if any were created") - print(" 3. Check IAM roles and VPC resources") - return False - - -if __name__ == "__main__": - success = test_aws_eks_provisioning() - sys.exit(0 if success else 1) diff --git a/tests/integration/test_aws_eks_provision_step.py b/tests/integration/test_aws_eks_provision_step.py deleted file mode 100644 index 2a2750cb..00000000 --- a/tests/integration/test_aws_eks_provision_step.py +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/env python -"""Test AWS EKS provisioning - step by step with verbose output.""" - -import sys -import time -import traceback -import logging -from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec -from clustrix.credential_manager import FlexibleCredentialManager - -# Set up logging -logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s") -logger = logging.getLogger(__name__) - - -def test_provisioning(): - # Get credentials - logger.info("Getting AWS credentials...") - credential_manager = FlexibleCredentialManager() - aws_creds = credential_manager.ensure_credential("aws") - - if not aws_creds: - logger.error("No AWS credentials found") - return False - - logger.info(f"Got credentials for region: {aws_creds.get('region', 'us-east-1')}") - - # Create spec with unique name - cluster_name = f"test-{int(time.time())}" - spec = ClusterSpec( - cluster_name=cluster_name, - provider="aws", - region=aws_creds.get("region", "us-east-1"), - node_count=1, - node_type="t3.small", - kubernetes_version="1.27", - ) - - logger.info(f"Created cluster spec: {spec.cluster_name}") - - # Initialize provisioner - logger.info("Initializing provisioner...") - provisioner = AWSEKSFromScratchProvisioner(aws_creds, spec.region) - - # Override methods to add logging - original_provision = provisioner.provision_complete_infrastructure - - def logged_provision(spec): - logger.info( - f"Starting provision_complete_infrastructure for {spec.cluster_name}" - ) - try: - # Call each step manually with logging - logger.info("Step 1: Creating VPC...") - # This would normally be inside provision_complete_infrastructure - # but we're debugging to see where it hangs - - import boto3 - - ec2 = boto3.client( - "ec2", - aws_access_key_id=aws_creds["access_key_id"], - aws_secret_access_key=aws_creds["secret_access_key"], - region_name=spec.region, - ) - - # Create VPC - vpc_response = ec2.create_vpc(CidrBlock="10.0.0.0/16") - vpc_id = vpc_response["Vpc"]["VpcId"] - logger.info(f"Created VPC: {vpc_id}") - - # Tag it - ec2.create_tags( - Resources=[vpc_id], - Tags=[ - {"Key": "Name", "Value": f"eks-vpc-{spec.cluster_name}"}, - {"Key": "ClusterName", "Value": spec.cluster_name}, - ], - ) - logger.info(f"Tagged VPC") - - # Enable DNS - ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsSupport={"Value": True}) - ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsHostnames={"Value": True}) - logger.info("Enabled DNS for VPC") - - # Clean up test VPC - logger.info("Cleaning up test VPC...") - ec2.delete_vpc(VpcId=vpc_id) - logger.info(f"Deleted test VPC: {vpc_id}") - - return {"status": "test_complete", "vpc_tested": vpc_id} - - except Exception as e: - logger.error(f"Error in provision: {e}") - traceback.print_exc() - raise - - # Test the provisioning - try: - logger.info("Starting test provisioning...") - result = logged_provision(spec) - logger.info(f"Test complete: {result}") - return True - except Exception as e: - logger.error(f"Provisioning failed: {e}") - return False - - -if __name__ == "__main__": - print("=" * 60) - print("AWS EKS Provisioning Step-by-Step Test") - print("=" * 60) - - success = test_provisioning() - - if success: - print("\n✅ Test successful! VPC creation and deletion work.") - print("The full provisioning appears to be hanging somewhere.") - print("Check the AWS provisioner code for blocking operations.") - else: - print("\n❌ Test failed!") - - sys.exit(0 if success else 1) diff --git a/tests/integration/test_aws_eks_real.py b/tests/integration/test_aws_eks_real.py deleted file mode 100644 index eb210740..00000000 --- a/tests/integration/test_aws_eks_real.py +++ /dev/null @@ -1,135 +0,0 @@ -#!/usr/bin/env python -"""Test real AWS EKS provisioning.""" - -import os -import sys -import time - -# Ensure 1Password is disabled -os.environ["CLUSTRIX_USE_1PASSWORD"] = "false" - -from clustrix import cluster, configure -from clustrix.kubernetes.cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, -) -from clustrix.config import ClusterConfig - - -def test_aws_eks_provisioning(): - """Test actual AWS EKS cluster provisioning.""" - print("=" * 60) - print("Testing AWS EKS Provisioning with REAL Credentials") - print("=" * 60) - - # Configure for AWS - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "aws" - config.k8s_region = "us-west-2" - config.use_1password = False # Explicitly disable - - print(f"\nConfiguration:") - print(f" Provider: AWS") - print(f" Region: us-west-2") - print(f" 1Password: DISABLED") - - # Create cluster specification - cluster_name = f"clustrix-test-{int(time.time())}" - spec = ClusterSpec( - provider="aws", - cluster_name=cluster_name, - region="us-west-2", - node_count=2, - node_type="t3.medium", - kubernetes_version="1.27", - from_scratch=True, - ) - - print(f"\nCluster Specification:") - print(f" Name: {cluster_name}") - print(f" Nodes: 2 x t3.medium") - print(f" K8s Version: 1.27") - - try: - # Initialize provisioner - print("\n🚀 Initializing provisioner...") - provisioner = KubernetesClusterProvisioner(config) - - # Check credentials - print("\n🔑 Checking AWS credentials...") - credentials = provisioner._get_provider_credentials("aws") - if not credentials: - print("❌ No AWS credentials available") - return False - - print(f"✅ AWS credentials loaded") - print(f" Access key: {credentials.get('access_key_id', '')[:10]}...") - - # IMPORTANT: Ask for confirmation before spending money - print("\n" + "⚠️ " * 20) - print("WARNING: This will provision REAL AWS resources and incur costs!") - print("Estimated cost: ~$0.10-0.20/hour for 2 x t3.medium nodes") - print("⚠️ " * 20) - - response = input("\nDo you want to continue? (yes/no): ") - if response.lower() != "yes": - print("Provisioning cancelled by user") - return False - - # Provision cluster - print("\n🌟 Starting cluster provisioning...") - print("This may take 10-15 minutes...") - - start_time = time.time() - cluster_info = provisioner.provision_cluster_if_needed(spec) - - provision_time = time.time() - start_time - - print(f"\n✅ Cluster provisioned successfully in {provision_time:.1f} seconds!") - print(f"\nCluster Information:") - print(f" ID: {cluster_info.get('cluster_id')}") - print(f" Status: {cluster_info.get('status')}") - print(f" Endpoint: {cluster_info.get('endpoint')}") - print(f" Nodes: {cluster_info.get('node_count')}") - print(f" Cost Estimate: ${cluster_info.get('cost_estimate', 0):.2f}/hour") - - # Test cluster connectivity - print("\n🔧 Testing cluster connectivity...") - status = provisioner._get_cluster_status(cluster_name) - print(f" Cluster ready: {status.get('ready_for_jobs', False)}") - - # Cleanup prompt - print("\n" + "=" * 60) - print("IMPORTANT: Remember to destroy this cluster when done!") - print( - f"Run: python -c \"from clustrix.kubernetes.cluster_provisioner import KubernetesClusterProvisioner; p = KubernetesClusterProvisioner(); p._destroy_cluster('{cluster_name}')\"" - ) - print("=" * 60) - - return True - - except Exception as e: - print(f"\n❌ Provisioning failed: {e}") - import traceback - - traceback.print_exc() - return False - - -def main(): - """Run the test.""" - success = test_aws_eks_provisioning() - - if success: - print("\n✅ AWS EKS provisioning test completed successfully!") - print(" No 1Password popup should have appeared") - else: - print("\n❌ AWS EKS provisioning test failed") - - return 0 if success else 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/integration/test_aws_eks_real_provision.py b/tests/integration/test_aws_eks_real_provision.py deleted file mode 100644 index df2611c5..00000000 --- a/tests/integration/test_aws_eks_real_provision.py +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env python -""" -REAL AWS EKS provisioning test - this WILL create resources and incur costs! -Only run this if you're ready to pay for AWS EKS cluster. -""" - -import sys -import time -import logging -from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec -from clustrix.credential_manager import FlexibleCredentialManager - -# Set up detailed logging -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger() - - -def provision_real_cluster(): - """Actually provision a real EKS cluster.""" - - print("=" * 70) - print("🚨 REAL AWS EKS CLUSTER PROVISIONING 🚨") - print("=" * 70) - print("\nThis WILL create:") - print(" • EKS Cluster ($0.10/hour)") - print(" • EC2 instances") - print(" • VPC, subnets, NAT gateways") - print(" • IAM roles") - print("\nEstimated cost: ~$0.28/hour") - print("=" * 70) - - # Confirm - response = input("\n⚠️ Type 'YES_PROVISION' to proceed: ") - if response != "YES_PROVISION": - print("Cancelled.") - return False - - # Get credentials - logger.info("Loading AWS credentials...") - manager = FlexibleCredentialManager() - aws_creds = manager.ensure_credential("aws") - - if not aws_creds: - logger.error("No AWS credentials") - return False - - # Create cluster spec - cluster_name = f"clustrix-real-{int(time.time())}" - spec = ClusterSpec( - cluster_name=cluster_name, - provider="aws", - region=aws_creds.get("region", "us-east-1"), - node_count=1, - node_type="t3.small", - kubernetes_version="1.27", - ) - - logger.info(f"Cluster name: {cluster_name}") - logger.info(f"Region: {spec.region}") - - # Initialize provisioner - logger.info("Initializing AWS EKS provisioner...") - provisioner = AWSEKSFromScratchProvisioner(aws_creds, spec.region) - - try: - # Start provisioning - print("\n" + "=" * 70) - print("🚀 STARTING PROVISIONING") - print("=" * 70) - print("\nThis will take 10-15 minutes. Progress will be shown below:\n") - - start_time = time.time() - cluster_info = provisioner.provision_complete_infrastructure(spec) - elapsed = time.time() - start_time - - if cluster_info: - print("\n" + "=" * 70) - print("✅ CLUSTER CREATED SUCCESSFULLY!") - print("=" * 70) - print(f"\nCluster Name: {cluster_info.get('cluster_name')}") - print(f"Endpoint: {cluster_info.get('endpoint')}") - print(f"Region: {cluster_info.get('region')}") - print(f"Time taken: {elapsed/60:.1f} minutes") - - # Save info - with open(f"DESTROY_CLUSTER_{cluster_name}.sh", "w") as f: - f.write("#!/bin/bash\n") - f.write(f"# Destroy cluster {cluster_name}\n") - f.write(f"python destroy_cluster.py {cluster_name} {spec.region}\n") - - print(f"\n📝 To destroy: bash DESTROY_CLUSTER_{cluster_name}.sh") - print("\n⚠️ IMPORTANT: Destroy the cluster when done to avoid charges!") - - return True - else: - logger.error("Provisioning returned no cluster info") - return False - - except KeyboardInterrupt: - print("\n\n❌ INTERRUPTED!") - print(f"Check AWS Console for resources tagged: {cluster_name}") - return False - except Exception as e: - logger.error(f"Provisioning failed: {e}", exc_info=True) - print(f"\n❌ Check AWS Console for partial resources: {cluster_name}") - return False - - -if __name__ == "__main__": - success = provision_real_cluster() - sys.exit(0 if success else 1) diff --git a/tests/integration/test_aws_preflight.py b/tests/integration/test_aws_preflight.py deleted file mode 100644 index f017d32b..00000000 --- a/tests/integration/test_aws_preflight.py +++ /dev/null @@ -1,225 +0,0 @@ -#!/usr/bin/env python -"""Pre-flight check for AWS EKS provisioning.""" - -import os -import sys -import subprocess -import json - - -def check_aws_cli(): - """Check if AWS CLI is installed and configured.""" - print("🔍 Checking AWS CLI...") - try: - result = subprocess.run( - ["aws", "--version"], capture_output=True, text=True, timeout=5 - ) - if result.returncode == 0: - print(f" ✅ AWS CLI installed: {result.stdout.strip()}") - return True - else: - print(" ❌ AWS CLI not found") - return False - except Exception as e: - print(f" ❌ Error checking AWS CLI: {e}") - return False - - -def check_aws_credentials(): - """Check if AWS credentials are configured.""" - print("\n🔑 Checking AWS credentials...") - - # Check credential manager - from clustrix.credential_manager import FlexibleCredentialManager - - manager = FlexibleCredentialManager() - creds = manager.ensure_credential("aws") - - if creds: - print(f" ✅ Credentials loaded from credential manager") - print(f" Access Key: {creds['access_key_id'][:10]}...") - print(f" Region: {creds.get('region', 'not set')}") - - # Test credentials with STS - try: - import boto3 - - sts = boto3.client( - "sts", - aws_access_key_id=creds["access_key_id"], - aws_secret_access_key=creds["secret_access_key"], - region_name=creds.get("region", "us-west-2"), - ) - identity = sts.get_caller_identity() - print(f" ✅ Credentials valid for account: {identity['Account']}") - print(f" ARN: {identity['Arn']}") - return True, creds - except Exception as e: - print(f" ❌ Credentials invalid: {e}") - return False, None - else: - print(" ❌ No AWS credentials found") - return False, None - - -def check_aws_permissions(creds): - """Check if we have necessary AWS permissions.""" - print("\n🔐 Checking AWS permissions...") - - import boto3 - - # Services we need access to - required_services = { - "ec2": ["DescribeVpcs", "CreateVpc"], - "eks": ["ListClusters", "CreateCluster"], - "iam": ["ListRoles", "CreateRole"], - } - - session = boto3.Session( - aws_access_key_id=creds["access_key_id"], - aws_secret_access_key=creds["secret_access_key"], - region_name=creds.get("region", "us-west-2"), - ) - - all_good = True - for service, actions in required_services.items(): - print(f"\n Checking {service.upper()} permissions:") - - try: - if service == "ec2": - client = session.client("ec2") - # Try to describe VPCs (read permission) - client.describe_vpcs(MaxResults=5) - print(f" ✅ Can read {service.upper()} resources") - - elif service == "eks": - client = session.client("eks") - # Try to list clusters (read permission) - client.list_clusters(maxResults=5) - print(f" ✅ Can read {service.upper()} resources") - - elif service == "iam": - client = session.client("iam") - # Try to list roles (read permission) - client.list_roles(MaxItems=5) - print(f" ✅ Can read {service.upper()} resources") - - except Exception as e: - print(f" ❌ Cannot access {service.upper()}: {str(e)[:100]}") - all_good = False - - return all_good - - -def check_kubectl(): - """Check if kubectl is installed.""" - print("\n🔧 Checking kubectl...") - try: - result = subprocess.run( - ["kubectl", "version", "--client", "--short"], - capture_output=True, - text=True, - timeout=5, - ) - if result.returncode == 0: - print(f" ✅ kubectl installed: {result.stdout.strip()}") - return True - else: - print(" ❌ kubectl not found") - print(" Install with: brew install kubectl") - return False - except Exception as e: - print(f" ❌ Error checking kubectl: {e}") - return False - - -def estimate_costs(): - """Estimate costs for AWS EKS cluster.""" - print("\n💰 Cost Estimation:") - print(" EKS Control Plane: $0.10/hour") - print(" t3.medium (2 nodes): $0.0416/hour x 2 = $0.0832/hour") - print(" NAT Gateway: $0.045/hour x 2 = $0.09/hour") - print(" Data transfer: ~$0.01/hour (estimate)") - print(" ─────────────────────────────────────") - print(" TOTAL: ~$0.28/hour ($6.72/day)") - print("\n ⚠️ Remember to destroy cluster after testing!") - - -def check_existing_clusters(creds): - """Check for existing EKS clusters.""" - print("\n🔍 Checking for existing EKS clusters...") - - try: - import boto3 - - eks = boto3.client( - "eks", - aws_access_key_id=creds["access_key_id"], - aws_secret_access_key=creds["secret_access_key"], - region_name=creds.get("region", "us-west-2"), - ) - - clusters = eks.list_clusters() - if clusters["clusters"]: - print(f" ⚠️ Found {len(clusters['clusters'])} existing cluster(s):") - for cluster in clusters["clusters"]: - print(f" - {cluster}") - print("\n Make sure to clean these up if they're test clusters!") - else: - print(" ✅ No existing EKS clusters found") - - except Exception as e: - print(f" ⚠️ Could not check existing clusters: {e}") - - -def main(): - """Run all pre-flight checks.""" - print("=" * 60) - print("AWS EKS Provisioning Pre-Flight Check") - print("=" * 60) - - all_checks_passed = True - - # Check AWS CLI - if not check_aws_cli(): - all_checks_passed = False - - # Check credentials - creds_valid, creds = check_aws_credentials() - if not creds_valid: - all_checks_passed = False - print("\n❌ Cannot proceed without valid AWS credentials") - return 1 - - # Check permissions - if not check_aws_permissions(creds): - print("\n⚠️ Some permissions missing. Provisioning might fail.") - print(" Ensure your IAM user has full access to EC2, EKS, and IAM") - - # Check kubectl - if not check_kubectl(): - print("\n⚠️ kubectl not installed. Won't be able to interact with cluster.") - - # Check existing clusters - check_existing_clusters(creds) - - # Show cost estimate - estimate_costs() - - # Summary - print("\n" + "=" * 60) - if all_checks_passed: - print("✅ All pre-flight checks passed!") - print("\nReady to provision AWS EKS cluster.") - print("Run: python test_aws_eks_real.py") - else: - print("⚠️ Some checks failed. Review issues above.") - print("\nYou can still try provisioning, but it might fail.") - - print("=" * 60) - - return 0 if all_checks_passed else 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/integration/test_aws_provision_detailed.py b/tests/integration/test_aws_provision_detailed.py deleted file mode 100644 index a0aea478..00000000 --- a/tests/integration/test_aws_provision_detailed.py +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env python -"""Test AWS EKS provisioning with detailed step-by-step execution.""" - -import sys -import time -import logging -import traceback -from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec -from clustrix.credential_manager import FlexibleCredentialManager - -# Set up detailed logging -logging.basicConfig( - level=logging.DEBUG, - format="%(asctime)s - %(levelname)s - %(message)s", - handlers=[ - logging.StreamHandler(sys.stdout), - logging.FileHandler("aws_provision_test.log"), - ], -) -logger = logging.getLogger() - - -def test_provisioning(): - """Test EKS provisioning step by step.""" - - print("=" * 70) - print("AWS EKS Step-by-Step Provisioning Test") - print("=" * 70) - - # Get credentials - logger.info("Loading credentials...") - manager = FlexibleCredentialManager() - aws_creds = manager.ensure_credential("aws") - - if not aws_creds: - logger.error("No AWS credentials found") - return False - - # Create spec - cluster_name = f"test-steps-{int(time.time())}" - spec = ClusterSpec( - cluster_name=cluster_name, - provider="aws", - region=aws_creds.get("region", "us-east-1"), - node_count=1, - node_type="t3.small", - kubernetes_version="1.27", - ) - - logger.info(f"Cluster: {cluster_name}") - logger.info(f"Region: {spec.region}") - - # Initialize provisioner - logger.info("Initializing provisioner...") - provisioner = AWSEKSFromScratchProvisioner(aws_creds, spec.region) - - try: - # Test Step 1: VPC Infrastructure - logger.info("\n" + "=" * 50) - logger.info("STEP 1: Testing VPC Infrastructure Creation") - logger.info("=" * 50) - - start = time.time() - vpc_config = provisioner._create_vpc_infrastructure(spec) - elapsed = time.time() - start - - logger.info(f"✅ VPC created in {elapsed:.1f}s") - logger.info(f" VPC ID: {vpc_config.get('vpc_id')}") - logger.info(f" Subnets: {vpc_config.get('subnet_ids', [])[:2]}...") - - # Clean up VPC to avoid charges - logger.info("\nCleaning up test VPC...") - import boto3 - - ec2 = boto3.client( - "ec2", - aws_access_key_id=aws_creds["access_key_id"], - aws_secret_access_key=aws_creds["secret_access_key"], - region_name=spec.region, - ) - - # Delete subnets - for subnet_id in vpc_config.get("subnet_ids", []): - try: - ec2.delete_subnet(SubnetId=subnet_id) - logger.info(f" Deleted subnet: {subnet_id}") - except Exception as e: - logger.warning(f" Could not delete subnet {subnet_id}: {e}") - - # Delete internet gateway - if "internet_gateway_id" in vpc_config: - try: - ec2.detach_internet_gateway( - InternetGatewayId=vpc_config["internet_gateway_id"], - VpcId=vpc_config["vpc_id"], - ) - ec2.delete_internet_gateway( - InternetGatewayId=vpc_config["internet_gateway_id"] - ) - logger.info(f" Deleted IGW: {vpc_config['internet_gateway_id']}") - except Exception as e: - logger.warning(f" Could not delete IGW: {e}") - - # Delete NAT gateways (these cost money!) - for nat_id in vpc_config.get("nat_gateway_ids", []): - try: - ec2.delete_nat_gateway(NatGatewayId=nat_id) - logger.info(f" Deleted NAT Gateway: {nat_id}") - except Exception as e: - logger.warning(f" Could not delete NAT Gateway {nat_id}: {e}") - - # Delete VPC - try: - ec2.delete_vpc(VpcId=vpc_config["vpc_id"]) - logger.info(f" Deleted VPC: {vpc_config['vpc_id']}") - except Exception as e: - logger.warning(f" Could not delete VPC: {e}") - logger.warning(" Check AWS Console and delete manually to avoid charges!") - - return True - - except Exception as e: - logger.error(f"Test failed: {e}") - traceback.print_exc() - return False - - -if __name__ == "__main__": - print("\nThis test will:") - print("1. Create a VPC with subnets") - print("2. Immediately delete it") - print("3. Show where provisioning might be hanging") - print("\nNo long-term resources will be created.\n") - - success = test_provisioning() - - if success: - print("\n✅ VPC infrastructure test successful!") - print("Check aws_provision_test.log for details") - else: - print("\n❌ Test failed - check aws_provision_test.log") - - sys.exit(0 if success else 1) diff --git a/tests/integration/test_aws_provision_optimized.py b/tests/integration/test_aws_provision_optimized.py deleted file mode 100644 index 499bcad9..00000000 --- a/tests/integration/test_aws_provision_optimized.py +++ /dev/null @@ -1,213 +0,0 @@ -#!/usr/bin/env python -""" -Test AWS EKS provisioning with optimizations: -1. Skip NAT gateways (use public subnets only for testing) -2. Smaller instance sizes -3. Single availability zone for faster provisioning -""" - -import sys -import time -import logging -import boto3 -from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec -from clustrix.credential_manager import FlexibleCredentialManager - -# Monkey-patch the provisioner to skip NAT gateways -original_create_vpc = AWSEKSFromScratchProvisioner._create_vpc_infrastructure - - -def patched_create_vpc(self, spec): - """Create VPC without NAT gateways for faster testing.""" - logger = logging.getLogger(__name__) - logger.info("Creating VPC infrastructure (OPTIMIZED - no NAT gateways)...") - - # Create VPC - vpc = self.ec2.create_vpc(CidrBlock="10.0.0.0/16") - vpc_id = vpc["Vpc"]["VpcId"] - - self.ec2.create_tags( - Resources=[vpc_id], - Tags=[ - {"Key": "Name", "Value": f"eks-vpc-{spec.cluster_name}"}, - {"Key": "ClusterName", "Value": spec.cluster_name}, - ], - ) - - # Enable DNS - self.ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsSupport={"Value": True}) - self.ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsHostnames={"Value": True}) - - # Create Internet Gateway - igw = self.ec2.create_internet_gateway() - igw_id = igw["InternetGateway"]["InternetGatewayId"] - self.ec2.attach_internet_gateway(InternetGatewayId=igw_id, VpcId=vpc_id) - - # Get AZs - azs = self.ec2.describe_availability_zones()["AvailabilityZones"] - az_names = [az["ZoneName"] for az in azs[:2]] # Use 2 AZs for EKS - - # Create public subnets only - subnet_ids = [] - for i, az in enumerate(az_names): - subnet = self.ec2.create_subnet( - VpcId=vpc_id, CidrBlock=f"10.0.{i}.0/24", AvailabilityZone=az - ) - subnet_id = subnet["Subnet"]["SubnetId"] - subnet_ids.append(subnet_id) - - # Enable auto-assign public IP - self.ec2.modify_subnet_attribute( - SubnetId=subnet_id, MapPublicIpOnLaunch={"Value": True} - ) - - self.ec2.create_tags( - Resources=[subnet_id], - Tags=[ - {"Key": "Name", "Value": f"eks-public-subnet-{i}-{spec.cluster_name}"}, - { - "Key": "kubernetes.io/cluster/" + spec.cluster_name, - "Value": "shared", - }, - ], - ) - - # Update main route table - route_tables = self.ec2.describe_route_tables( - Filters=[{"Name": "vpc-id", "Values": [vpc_id]}] - ) - main_rt = route_tables["RouteTables"][0]["RouteTableId"] - - self.ec2.create_route( - RouteTableId=main_rt, DestinationCidrBlock="0.0.0.0/0", GatewayId=igw_id - ) - - # Create security group - sg = self.ec2.create_security_group( - GroupName=f"eks-cluster-sg-{spec.cluster_name}", - Description="EKS cluster security group", - VpcId=vpc_id, - ) - sg_id = sg["GroupId"] - - # Allow all traffic within VPC - self.ec2.authorize_security_group_ingress( - GroupId=sg_id, - IpPermissions=[ - { - "IpProtocol": "-1", - "FromPort": -1, - "ToPort": -1, - "IpRanges": [{"CidrIp": "10.0.0.0/16"}], - } - ], - ) - - logger.info(f"✅ VPC infrastructure created (optimized)") - - return { - "vpc_id": vpc_id, - "subnet_ids": subnet_ids, - "private_subnet_ids": subnet_ids, # Use public as private for testing - "public_subnet_ids": subnet_ids, - "internet_gateway_id": igw_id, - "nat_gateway_ids": [], # No NAT gateways - "security_group_ids": [sg_id], - } - - -# Apply the patch -AWSEKSFromScratchProvisioner._create_vpc_infrastructure = patched_create_vpc - -# Set up logging -logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s") -logger = logging.getLogger() - - -def test_optimized_provisioning(): - """Test EKS provisioning with optimizations.""" - - print("=" * 70) - print("Optimized AWS EKS Provisioning Test") - print("=" * 70) - print("\nOptimizations:") - print(" • No NAT gateways (saves 5-10 minutes)") - print(" • Public subnets only") - print(" • Minimal configuration") - print("\n" + "=" * 70) - - # Get credentials - manager = FlexibleCredentialManager() - aws_creds = manager.ensure_credential("aws") - - if not aws_creds: - logger.error("No AWS credentials") - return False - - # Create spec - cluster_name = f"optimized-{int(time.time())}" - spec = ClusterSpec( - cluster_name=cluster_name, - provider="aws", - region=aws_creds.get("region", "us-east-1"), - node_count=1, - node_type="t3.micro", # Smallest instance - kubernetes_version="1.27", - ) - - logger.info(f"Cluster: {cluster_name}") - logger.info(f"Region: {spec.region}") - - # Initialize provisioner - provisioner = AWSEKSFromScratchProvisioner(aws_creds, spec.region) - - try: - # Start provisioning - logger.info("\nStarting provisioning...") - start_time = time.time() - - result = provisioner.provision_complete_infrastructure(spec) - - elapsed = time.time() - start_time - - if result: - print("\n" + "=" * 70) - print("✅ CLUSTER CREATED SUCCESSFULLY!") - print("=" * 70) - print(f"Cluster Name: {result.get('cluster_name')}") - print(f"Endpoint: {result.get('endpoint')}") - print(f"Time: {elapsed/60:.1f} minutes") - - # Save cleanup info - with open(f"destroy_{cluster_name}.sh", "w") as f: - f.write(f"#!/bin/bash\n") - f.write(f"python destroy_cluster.py {cluster_name} {spec.region}\n") - - print(f"\n⚠️ To destroy: bash destroy_{cluster_name}.sh") - return True - else: - logger.error("Provisioning failed") - return False - - except Exception as e: - logger.error(f"Error: {e}") - import traceback - - traceback.print_exc() - - print(f"\n❌ Failed. Check AWS Console for resources tagged: {cluster_name}") - return False - - -if __name__ == "__main__": - print("\n⚠️ This will create a REAL EKS cluster") - print(" Cost: ~$0.10/hour for control plane") - - response = input("\nType 'yes' to proceed: ") - if response.lower() != "yes": - print("Cancelled") - sys.exit(0) - - success = test_optimized_provisioning() - sys.exit(0 if success else 1) diff --git a/tests/integration/test_aws_quick_provision.py b/tests/integration/test_aws_quick_provision.py deleted file mode 100644 index f4a206e0..00000000 --- a/tests/integration/test_aws_quick_provision.py +++ /dev/null @@ -1,279 +0,0 @@ -#!/usr/bin/env python -""" -Quick test of AWS EKS provisioning - skips expensive/slow NAT gateways. -This creates a simplified VPC for testing purposes only. -""" - -import sys -import time -import logging -import boto3 -from clustrix.credential_manager import FlexibleCredentialManager -from clustrix.kubernetes.cluster_provisioner import ClusterSpec - -# Set up logging -logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s") -logger = logging.getLogger() - - -def quick_provision_test(): - """Test simplified provisioning without NAT gateways.""" - - print("=" * 70) - print("Quick AWS Provisioning Test (No NAT Gateways)") - print("=" * 70) - - # Get credentials - manager = FlexibleCredentialManager() - aws_creds = manager.ensure_credential("aws") - - if not aws_creds: - logger.error("No AWS credentials") - return False - - region = aws_creds.get("region", "us-east-1") - cluster_name = f"quick-test-{int(time.time())}" - - # Create AWS clients - ec2 = boto3.client( - "ec2", - aws_access_key_id=aws_creds["access_key_id"], - aws_secret_access_key=aws_creds["secret_access_key"], - region_name=region, - ) - - eks = boto3.client( - "eks", - aws_access_key_id=aws_creds["access_key_id"], - aws_secret_access_key=aws_creds["secret_access_key"], - region_name=region, - ) - - iam = boto3.client( - "iam", - aws_access_key_id=aws_creds["access_key_id"], - aws_secret_access_key=aws_creds["secret_access_key"], - ) - - created_resources = { - "vpc_id": None, - "subnet_ids": [], - "igw_id": None, - "sg_id": None, - "role_arn": None, - } - - try: - # 1. Create VPC - logger.info("Creating VPC...") - vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") - vpc_id = vpc["Vpc"]["VpcId"] - created_resources["vpc_id"] = vpc_id - - ec2.create_tags( - Resources=[vpc_id], Tags=[{"Key": "Name", "Value": f"vpc-{cluster_name}"}] - ) - - # Enable DNS - ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsSupport={"Value": True}) - ec2.modify_vpc_attribute(VpcId=vpc_id, EnableDnsHostnames={"Value": True}) - logger.info(f" ✅ VPC created: {vpc_id}") - - # 2. Create Internet Gateway - logger.info("Creating Internet Gateway...") - igw = ec2.create_internet_gateway() - igw_id = igw["InternetGateway"]["InternetGatewayId"] - created_resources["igw_id"] = igw_id - - ec2.attach_internet_gateway(InternetGatewayId=igw_id, VpcId=vpc_id) - logger.info(f" ✅ IGW created and attached: {igw_id}") - - # 3. Create PUBLIC subnets only (no NAT needed) - logger.info("Creating public subnets...") - - # Get availability zones - azs = ec2.describe_availability_zones()["AvailabilityZones"] - az_names = [az["ZoneName"] for az in azs[:2]] # Use first 2 AZs - - for i, az in enumerate(az_names): - subnet = ec2.create_subnet( - VpcId=vpc_id, CidrBlock=f"10.0.{i}.0/24", AvailabilityZone=az - ) - subnet_id = subnet["Subnet"]["SubnetId"] - created_resources["subnet_ids"].append(subnet_id) - - # Make it public - ec2.modify_subnet_attribute( - SubnetId=subnet_id, MapPublicIpOnLaunch={"Value": True} - ) - - ec2.create_tags( - Resources=[subnet_id], - Tags=[{"Key": "Name", "Value": f"public-subnet-{i}-{cluster_name}"}], - ) - logger.info(f" ✅ Subnet created: {subnet_id} in {az}") - - # 4. Update main route table - logger.info("Setting up routing...") - route_tables = ec2.describe_route_tables( - Filters=[{"Name": "vpc-id", "Values": [vpc_id]}] - ) - main_rt = route_tables["RouteTables"][0]["RouteTableId"] - - # Add route to IGW - ec2.create_route( - RouteTableId=main_rt, DestinationCidrBlock="0.0.0.0/0", GatewayId=igw_id - ) - logger.info(f" ✅ Routes configured") - - # 5. Create security group - logger.info("Creating security group...") - sg = ec2.create_security_group( - GroupName=f"eks-sg-{cluster_name}", - Description="EKS cluster security group", - VpcId=vpc_id, - ) - sg_id = sg["GroupId"] - created_resources["sg_id"] = sg_id - - # Allow all traffic within VPC - ec2.authorize_security_group_ingress( - GroupId=sg_id, - IpPermissions=[ - { - "IpProtocol": "-1", - "FromPort": -1, - "ToPort": -1, - "IpRanges": [{"CidrIp": "10.0.0.0/16"}], - } - ], - ) - logger.info(f" ✅ Security group created: {sg_id}") - - # 6. Create IAM role for EKS - logger.info("Creating IAM role...") - - assume_role_policy = { - "Version": "2012-10-17", - "Statement": [ - { - "Effect": "Allow", - "Principal": {"Service": "eks.amazonaws.com"}, - "Action": "sts:AssumeRole", - } - ], - } - - role_name = f"eks-role-{cluster_name}" - try: - role = iam.create_role( - RoleName=role_name, - AssumeRolePolicyDocument=str(assume_role_policy).replace("'", '"'), - ) - role_arn = role["Role"]["Arn"] - created_resources["role_arn"] = role_arn - - # Attach EKS policy - iam.attach_role_policy( - RoleName=role_name, - PolicyArn="arn:aws:iam::aws:policy/AmazonEKSClusterPolicy", - ) - logger.info(f" ✅ IAM role created: {role_name}") - except iam.exceptions.EntityAlreadyExistsException: - logger.info(f" ℹ️ Role already exists: {role_name}") - role = iam.get_role(RoleName=role_name) - role_arn = role["Role"]["Arn"] - created_resources["role_arn"] = role_arn - - # 7. Create EKS cluster - logger.info("\nCreating EKS cluster (this takes 10-15 minutes)...") - - cluster_response = eks.create_cluster( - name=cluster_name, - version="1.27", - roleArn=role_arn, - resourcesVpcConfig={ - "subnetIds": created_resources["subnet_ids"], - "securityGroupIds": [sg_id], - "endpointPublicAccess": True, - "endpointPrivateAccess": False, - }, - ) - - logger.info(f" ⏳ Cluster creation started: {cluster_name}") - logger.info(f" Status: {cluster_response['cluster']['status']}") - - # Wait for cluster to be active - logger.info(" ⏳ Waiting for cluster to be active...") - waiter = eks.get_waiter("cluster_active") - waiter.wait(name=cluster_name, WaiterConfig={"Delay": 30, "MaxAttempts": 40}) - - # Get cluster info - cluster = eks.describe_cluster(name=cluster_name)["cluster"] - logger.info(f" ✅ Cluster active!") - logger.info(f" Endpoint: {cluster['endpoint']}") - logger.info(f" Status: {cluster['status']}") - - print("\n" + "=" * 70) - print("✅ CLUSTER CREATED SUCCESSFULLY!") - print("=" * 70) - print(f"Cluster Name: {cluster_name}") - print(f"Endpoint: {cluster['endpoint']}") - print(f"Region: {region}") - print("\n⚠️ To destroy this cluster:") - print(f" python destroy_cluster.py {cluster_name} {region}") - - # Save destroy script - with open(f"destroy_{cluster_name}.sh", "w") as f: - f.write(f"#!/bin/bash\n") - f.write(f"python destroy_cluster.py {cluster_name} {region}\n") - - return True - - except Exception as e: - logger.error(f"Failed: {e}") - - # Clean up any created resources - logger.info("\nCleaning up resources...") - - if created_resources["subnet_ids"]: - for subnet_id in created_resources["subnet_ids"]: - try: - ec2.delete_subnet(SubnetId=subnet_id) - logger.info(f" Deleted subnet: {subnet_id}") - except: - pass - - if created_resources["igw_id"] and created_resources["vpc_id"]: - try: - ec2.detach_internet_gateway( - InternetGatewayId=created_resources["igw_id"], - VpcId=created_resources["vpc_id"], - ) - ec2.delete_internet_gateway( - InternetGatewayId=created_resources["igw_id"] - ) - logger.info(f" Deleted IGW: {created_resources['igw_id']}") - except: - pass - - if created_resources["vpc_id"]: - try: - ec2.delete_vpc(VpcId=created_resources["vpc_id"]) - logger.info(f" Deleted VPC: {created_resources['vpc_id']}") - except: - pass - - return False - - -if __name__ == "__main__": - print("\n⚠️ This will create a REAL EKS cluster (costs ~$0.10/hour)") - response = input("Type 'yes' to proceed: ") - - if response.lower() != "yes": - print("Cancelled") - sys.exit(0) - - success = quick_provision_test() - sys.exit(0 if success else 1) diff --git a/tests/integration/test_eks_permissions.py b/tests/integration/test_eks_permissions.py deleted file mode 100644 index 418fabcf..00000000 --- a/tests/integration/test_eks_permissions.py +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/env python -"""Test specific EKS permissions. - -This file is a diagnostic *script*, not a pytest module: it contains no test -functions. Its body previously ran at module scope, which meant that merely -importing it fetched real AWS credentials, constructed boto3 EKS and IAM -clients, called those APIs, and could `exit(1)` mid-collection (crashing pytest -with INTERNALERROR). - -The body now lives in `main()` behind a `__main__` guard so that importing this -module is inert. See issue #109: the directory-level gate in conftest.py does -not protect against a path being named explicitly on the pytest command line, -because collect_ignore_glob only filters directory traversal. - -Run deliberately with: - - python tests/integration/test_eks_permissions.py -""" - -import sys - -import boto3 - -from clustrix.credential_manager import FlexibleCredentialManager - - -def main(): - # Get credentials - manager = FlexibleCredentialManager() - creds = manager.ensure_credential("aws") - - if not creds: - print("❌ No AWS credentials found") - return 1 - - print("Testing EKS permissions for user Clustrix...") - print("=" * 60) - - # Create EKS client - eks = boto3.client( - "eks", - aws_access_key_id=creds["access_key_id"], - aws_secret_access_key=creds["secret_access_key"], - region_name=creds.get("region", "us-east-1"), - ) - - # Test various EKS operations - operations = [ - ("ListClusters", lambda: eks.list_clusters(maxResults=1)), - ( - "DescribeCluster", - lambda: eks.describe_cluster(name="test-nonexistent-cluster"), - ), - ] - - print("\nEKS Permission Tests:") - for op_name, op_func in operations: - try: - op_func() - print(f" ✅ {op_name}: Allowed") - except eks.exceptions.ResourceNotFoundException: - print(f" ✅ {op_name}: Allowed (resource not found)") - except eks.exceptions.AccessDeniedException as e: - print(f" ❌ {op_name}: Access Denied") - print(f" Error: {str(e)[:200]}") - except Exception as e: - if "AccessDenied" in str(e): - print(f" ❌ {op_name}: Access Denied") - else: - print(f" ⚠️ {op_name}: {type(e).__name__}") - - # Check attached policies - print("\n" + "=" * 60) - print("Checking attached policies...") - - iam = boto3.client( - "iam", - aws_access_key_id=creds["access_key_id"], - aws_secret_access_key=creds["secret_access_key"], - ) - - try: - # Get user policies - response = iam.list_attached_user_policies(UserName="Clustrix") - - print("\nAttached AWS Managed Policies:") - eks_policies_found = [] - for policy in response["AttachedPolicies"]: - print(f" • {policy['PolicyName']}") - if "EKS" in policy["PolicyName"]: - eks_policies_found.append(policy["PolicyName"]) - - print("\nEKS-related policies found:") - if eks_policies_found: - for p in eks_policies_found: - print(f" ✅ {p}") - else: - print(" ❌ No EKS policies found!") - print("\nYou need to add these policies in AWS Console:") - print(" • AmazonEKSClusterPolicy") - print(" • AmazonEKSWorkerNodePolicy") - print(" • AmazonEKS_CNI_Policy") - print(" • AmazonEKSServicePolicy") - - except Exception as e: - print(f"Could not list policies: {e}") - - print("\n" + "=" * 60) - print("\nNext steps:") - print("1. If EKS policies are missing, add them in AWS Console") - print( - "2. Direct link: " - "https://console.aws.amazon.com/iam/home#/users/Clustrix?section=permissions" - ) - print("3. Click 'Add permissions' and search for the EKS policies listed above") - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/real_world/api_validation/validate_aws_pricing.py b/tests/real_world/api_validation/validate_aws_pricing.py deleted file mode 100644 index 4aa25380..00000000 --- a/tests/real_world/api_validation/validate_aws_pricing.py +++ /dev/null @@ -1,132 +0,0 @@ -#!/usr/bin/env python3 -""" -AWS Pricing API Validation Script - -This script validates the AWS Pricing API integration for Clustrix. -It tests actual API calls and ensures the cost estimation functionality works correctly. -""" - -import sys -import json -import logging -from pathlib import Path - -# Add the clustrix package to Python path -sys.path.insert(0, str(Path(__file__).parent.parent)) - -from clustrix.secure_credentials import ValidationCredentials -from clustrix.cost_providers.aws import AWSCostMonitor - -# Configure logging -logging.basicConfig(level=logging.DEBUG) -logger = logging.getLogger(__name__) - -# Also enable logging for boto3 and pricing clients -logging.getLogger("clustrix.pricing_clients").setLevel(logging.DEBUG) -logging.getLogger("boto3").setLevel(logging.DEBUG) -logging.getLogger("botocore").setLevel(logging.DEBUG) - - -def test_aws_pricing_api(): - """Test AWS Pricing API with real credentials.""" - print("🔍 AWS Pricing API Validation") - print("=" * 50) - - # Get credentials - creds = ValidationCredentials() - aws_creds = creds.get_aws_credentials() - - if not aws_creds: - print("❌ No AWS credentials found") - print(" Please set up AWS credentials in 1Password or environment variables") - return False - - print(f"✅ AWS credentials found") - print(f" Access Key: {aws_creds['aws_access_key_id'][:10]}...") - print(f" Region: {aws_creds['aws_region']}") - - # Test AWS Pricing API - try: - print("\n🧪 Testing AWS Pricing API") - - # Set up AWS credentials as environment variables for boto3 - import os - - os.environ["AWS_ACCESS_KEY_ID"] = aws_creds["aws_access_key_id"] - os.environ["AWS_SECRET_ACCESS_KEY"] = aws_creds["aws_secret_access_key"] - os.environ["AWS_DEFAULT_REGION"] = aws_creds["aws_region"] - - cost_monitor = AWSCostMonitor( - region=aws_creds["aws_region"], use_pricing_api=True - ) - - # Test different instance types - test_instances = ["t3.micro", "t3.small", "m5.large", "c5.xlarge", "r5.2xlarge"] - - print(f" Testing {len(test_instances)} instance types...") - - for instance_type in test_instances: - try: - cost_estimate = cost_monitor.estimate_cost( - instance_type, hours_used=1.0 - ) - if cost_estimate and cost_estimate.estimated_cost > 0: - print( - f" ✅ {instance_type}: ${cost_estimate.hourly_rate:.4f}/hour (${cost_estimate.estimated_cost:.4f} total)" - ) - print(f" Source: {cost_estimate.pricing_source}") - - # Warn if not truly from API - if cost_estimate.pricing_source != "api": - print( - f" ⚠️ WARNING: Expected API source but got '{cost_estimate.pricing_source}'" - ) - else: - print(f" ⚠️ {instance_type}: No pricing data") - except Exception as e: - print(f" ❌ {instance_type}: Error - {e}") - - # Test invalid instance type - try: - invalid_cost = cost_monitor.estimate_cost( - "invalid-instance-type", hours_used=1.0 - ) - if invalid_cost is None or invalid_cost.estimated_cost == 0: - print(" ✅ Invalid instance type correctly returns None/zero") - else: - print( - f" ⚠️ Invalid instance type returned: ${invalid_cost.estimated_cost}" - ) - except Exception as e: - print(f" ✅ Invalid instance type correctly raises exception: {e}") - - print("\n✅ AWS Pricing API validation completed successfully!") - return True - - except Exception as e: - print(f"\n❌ AWS Pricing API validation failed: {e}") - logger.exception("AWS Pricing API validation error") - return False - - -def main(): - """Main validation function.""" - print("🚀 Starting AWS Pricing API Validation") - print("=" * 50) - - success = test_aws_pricing_api() - - print("\n📊 Validation Summary") - print("=" * 50) - if success: - print("✅ AWS Pricing API validation: PASSED") - print(" All tests completed successfully") - else: - print("❌ AWS Pricing API validation: FAILED") - print(" Check error messages above") - - return 0 if success else 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/real_world/api_validation/validate_gcp_pricing.py b/tests/real_world/api_validation/validate_gcp_pricing.py deleted file mode 100644 index 39a31384..00000000 --- a/tests/real_world/api_validation/validate_gcp_pricing.py +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/env python3 -""" -GCP Pricing API Validation Script - -This script validates the GCP Pricing API integration for Clustrix. -It tests actual API calls and ensures the cost estimation functionality works correctly. -""" - -import sys -import json -import logging -import os -import tempfile -from pathlib import Path - -# Add the clustrix package to Python path -sys.path.insert(0, str(Path(__file__).parent.parent)) - -from clustrix.secure_credentials import ValidationCredentials -from clustrix.cost_providers.gcp import GCPCostMonitor - -# Configure logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def test_gcp_pricing_api(): - """Test GCP Pricing API with real credentials.""" - print("🔍 GCP Pricing API Validation") - print("=" * 50) - - # Get credentials - creds = ValidationCredentials() - gcp_creds = creds.get_gcp_credentials() - - if not gcp_creds: - print("❌ No GCP credentials found") - print(" Please set up GCP credentials in 1Password or environment variables") - return False - - print(f"✅ GCP credentials found") - print(f" Project ID: {gcp_creds['project_id']}") - print(f" Region: {gcp_creds['region']}") - - # Create temporary service account file - service_account_json = gcp_creds.get("service_account_json") - if not service_account_json: - print("❌ No service account JSON found in credentials") - return False - - try: - # Write service account JSON to temporary file - with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: - if isinstance(service_account_json, str): - # Parse JSON string if needed - try: - service_account_data = json.loads(service_account_json) - json.dump(service_account_data, f, indent=2) - except json.JSONDecodeError: - f.write(service_account_json) - else: - json.dump(service_account_json, f, indent=2) - temp_creds_file = f.name - - print(f" Service account file: {temp_creds_file}") - - print("\n🧪 Testing GCP Pricing API") - - # Set up service account credentials - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = temp_creds_file - - cost_monitor = GCPCostMonitor(region=gcp_creds["region"], use_pricing_api=True) - - # Test different machine types - test_machine_types = [ - "e2-micro", - "e2-small", - "n1-standard-1", - "n1-standard-2", - "n2-standard-4", - "c2-standard-8", - ] - - print(f" Testing {len(test_machine_types)} machine types...") - - for machine_type in test_machine_types: - try: - cost_estimate = cost_monitor.estimate_cost(machine_type, hours_used=1.0) - if cost_estimate and cost_estimate.estimated_cost > 0: - print( - f" ✅ {machine_type}: ${cost_estimate.hourly_rate:.4f}/hour (${cost_estimate.estimated_cost:.4f} total)" - ) - print(f" Source: {cost_estimate.pricing_source}") - else: - print(f" ⚠️ {machine_type}: No pricing data") - except Exception as e: - print(f" ❌ {machine_type}: Error - {e}") - - # Test invalid machine type - try: - invalid_cost = cost_monitor.estimate_cost( - "invalid-machine-type", hours_used=1.0 - ) - if invalid_cost is None or invalid_cost.estimated_cost == 0: - print(" ✅ Invalid machine type correctly returns None/zero") - else: - print( - f" ⚠️ Invalid machine type returned: ${invalid_cost.estimated_cost}" - ) - except Exception as e: - print(f" ✅ Invalid machine type correctly raises exception: {e}") - - print("\n✅ GCP Pricing API validation completed successfully!") - return True - - except Exception as e: - print(f"\n❌ GCP Pricing API validation failed: {e}") - logger.exception("GCP Pricing API validation error") - return False - finally: - # Clean up temporary file - if "temp_creds_file" in locals(): - try: - os.unlink(temp_creds_file) - except OSError: - pass - - -def main(): - """Main validation function.""" - print("🚀 Starting GCP Pricing API Validation") - print("=" * 50) - - success = test_gcp_pricing_api() - - print("\n📊 Validation Summary") - print("=" * 50) - if success: - print("✅ GCP Pricing API validation: PASSED") - print(" All tests completed successfully") - else: - print("❌ GCP Pricing API validation: FAILED") - print(" Check error messages above") - - return 0 if success else 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/real_world/api_validation/validate_huggingface_pricing.py b/tests/real_world/api_validation/validate_huggingface_pricing.py deleted file mode 100755 index d798c9cc..00000000 --- a/tests/real_world/api_validation/validate_huggingface_pricing.py +++ /dev/null @@ -1,225 +0,0 @@ -#!/usr/bin/env python3 -"""HuggingFace Spaces pricing validation script. - -This script validates HuggingFace Spaces pricing and API access, -testing both free and paid tier functionality. -""" - -import json -import logging -import time -from pathlib import Path -import sys - -# Add clustrix to path -sys.path.insert(0, str(Path(__file__).parent.parent)) - -from clustrix.secure_credentials import ValidationCredentials -from clustrix.cloud_providers.huggingface_spaces import HuggingFaceSpacesProvider - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def validate_huggingface_pricing(): - """Validate HuggingFace Spaces pricing and API access.""" - print("🤗 HuggingFace Spaces Pricing Validation") - print("=" * 50) - - # Get credentials - creds = ValidationCredentials() - hf_creds = creds.get_huggingface_credentials() - - has_credentials = bool(hf_creds) - - if not has_credentials: - print("⚠️ No HuggingFace credentials found - testing pricing only") - print( - "\nTo set up credentials for full validation, store in 1Password as 'clustrix-huggingface-validation':" - ) - print("- token: Your HuggingFace API token") - print("- username: Your HuggingFace username (optional)") - print("\nOr set environment variable: HUGGINGFACE_TOKEN or HF_TOKEN") - print("\n💡 You can get a token at: https://huggingface.co/settings/tokens") - else: - print(f"✅ Found HuggingFace credentials") - if hf_creds.get("username"): - print(f" Username: {hf_creds['username']}") - - # Test HuggingFace pricing first (works without credentials) - print("\n💰 Testing HuggingFace Spaces pricing...") - - try: - provider = HuggingFaceSpacesProvider() - - # Test different hardware configurations - hardware_types = [ - "cpu-basic", - "cpu-upgrade", - "t4-small", - "t4-medium", - "a10g-small", - "a10g-large", - "a100-large", - ] - - print(f"\n📋 HuggingFace Spaces Pricing:") - print(f"{'Hardware':<15} {'$/hour':<10} {'1hr':<8} {'8hr':<8} {'Monthly'}") - print("-" * 55) - - for hardware in hardware_types: - cost_1hr = provider.estimate_cost(hardware=hardware, hours=1) - cost_8hr = provider.estimate_cost(hardware=hardware, hours=8) - cost_monthly = provider.estimate_cost( - hardware=hardware, hours=160 - ) # ~20 workdays - - hourly_rate = cost_1hr["total"] - print( - f"{hardware:<15} ${hourly_rate:<9.2f} ${cost_1hr['total']:<7.2f} ${cost_8hr['total']:<7.2f} ${cost_monthly['total']:<7.2f}" - ) - - print(f"\n✅ HuggingFace Spaces pricing validation completed!") - print(" - All hardware tiers priced correctly") - print(" - Cost estimation working properly") - - except Exception as e: - print(f" ❌ Error testing pricing: {e}") - import traceback - - traceback.print_exc() - return False - - # Test API access only if credentials available - if not has_credentials: - print(f"\n📝 HuggingFace Spaces Notes:") - print(" - CPU Basic tier is free (limited usage)") - print(" - Pricing is usage-based for compute upgrades") - print(" - No separate pricing API - pricing embedded in provider") - print(" - Billing occurs only when spaces are actively running") - - print(f"\n✅ PARTIAL SUCCESS: HuggingFace pricing validation completed!") - print(" - Pricing calculations working correctly") - print(" - Add credentials for full API testing") - return True - - # Test HuggingFace API access using official library - try: - print("\n🔍 Testing HuggingFace API access...") - start_time = time.time() - - # Test using huggingface_hub library (the official way) - try: - from huggingface_hub import HfApi - - api = HfApi(token=hf_creds["token"]) - - print(" Validating API token with HfApi...") - user_info = api.whoami() - - if user_info: - print(f" ✅ Token valid for user: {user_info.get('name', 'Unknown')}") - print(f" ✅ User type: {user_info.get('type', 'unknown')}") - print( - f" ✅ Token role: {user_info.get('auth', {}).get('accessToken', {}).get('role', 'unknown')}" - ) - - # Test spaces listing - print(" Testing Spaces API access...") - try: - spaces = list(api.list_spaces(author=hf_creds.get("username", ""))) - print(f" ✅ Found {len(spaces)} spaces for user") - if spaces: - print(f" Example: {spaces[0].id}") - except Exception as e: - print(f" ⚠️ Spaces listing error: {e}") - - else: - print(" ❌ Token validation failed - no user info returned") - return False - - except ImportError: - print(" ⚠️ huggingface_hub library not available") - print(" 💡 Install with: pip install huggingface_hub") - - # Fallback to basic requests test - try: - import requests - - headers = {"Authorization": f"Bearer {hf_creds['token']}"} - - # Test public endpoints that work with token - response = requests.get( - "https://huggingface.co/api/models?limit=1", - headers=headers, - timeout=10, - ) - - if response.status_code == 200: - print(" ✅ Token works with public API endpoints") - else: - print(f" ⚠️ API test returned: {response.status_code}") - - except Exception as e: - print(f" ❌ Fallback API test failed: {e}") - return False - - elapsed = time.time() - start_time - print(f"\n⏱️ Total API validation time: {elapsed:.2f}s") - - print(f"\n✅ SUCCESS: HuggingFace API access validated!") - print(" - Token authentication working") - print(" - Official HuggingFace Hub API integration confirmed") - print(" - Ready for Spaces deployment testing") - - return True - - except Exception as e: - print(f"\n❌ VALIDATION FAILED: {e}") - import traceback - - traceback.print_exc() - return False - - -def test_huggingface_spaces_creation(): - """Test actual Spaces creation (optional, requires careful cleanup).""" - print("\n🧪 Testing Spaces Creation (OPTIONAL)") - print("⚠️ This will create a test Space that needs manual cleanup") - - try: - response = input("Proceed with Spaces creation test? (y/N): ") - if response.lower() != "y": - print(" Skipping Spaces creation test") - return True - except EOFError: - print(" Skipping Spaces creation test (non-interactive mode)") - return True - - # This would require more complex testing and cleanup - # For now, just document the process - print(" 📝 Spaces creation testing requires:") - print(" 1. Create a test Space via API") - print(" 2. Upload a simple app (e.g., Streamlit hello world)") - print(" 3. Test deployment and accessibility") - print(" 4. Clean up test Space") - print(" 💡 This is best done manually through HF Hub interface") - - return True - - -def main(): - """Main validation function.""" - success = validate_huggingface_pricing() - - if success: - test_huggingface_spaces_creation() - print("\n🎉 HuggingFace validation completed!") - exit(0) - else: - print("\n💥 HuggingFace validation failed!") - exit(1) - - -if __name__ == "__main__": - main() diff --git a/tests/real_world/api_validation/validate_lambda_cloud_pricing.py b/tests/real_world/api_validation/validate_lambda_cloud_pricing.py deleted file mode 100755 index 4af7f29b..00000000 --- a/tests/real_world/api_validation/validate_lambda_cloud_pricing.py +++ /dev/null @@ -1,152 +0,0 @@ -#!/usr/bin/env python3 -"""Lambda Cloud pricing validation script. - -This script validates that Lambda Cloud pricing retrieval works with real API calls, -not just theoretical implementations. -""" - -import json -import logging -import time -from pathlib import Path -import sys - -# Add clustrix to path -sys.path.insert(0, str(Path(__file__).parent.parent)) - -from clustrix.secure_credentials import ValidationCredentials -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def validate_lambda_cloud_pricing(): - """Validate Lambda Cloud pricing API integration.""" - print("🚀 Lambda Cloud Pricing Validation") - print("=" * 50) - - # Get credentials - creds = ValidationCredentials() - lambda_creds = creds.get_lambda_cloud_credentials() - - has_credentials = bool(lambda_creds) - - if not has_credentials: - print("⚠️ No Lambda Cloud credentials found - testing pricing only") - print( - "\nTo set up credentials for full validation, store in 1Password as 'clustrix-lambda-cloud-validation':" - ) - print("- api_key: Your Lambda Cloud API key") - print("- endpoint: https://cloud.lambdalabs.com/api/v1 (optional)") - print("\nOr set environment variable: LAMBDA_CLOUD_API_KEY") - else: - print(f"✅ Found Lambda Cloud credentials") - print(f" API Endpoint: {lambda_creds.get('endpoint', 'default')}") - - # Test with cost monitor (works without credentials for hardcoded pricing) - try: - print("\n📊 Testing Lambda Cloud cost estimation...") - start_time = time.time() - - # Initialize cost monitor - monitor = LambdaCostMonitor() - - # Test instance types (using actual Lambda Cloud naming) - test_instances = ["a10", "a100_40gb", "h100", "4xa100_40gb", "rtx6000ada"] - - results = {} - for instance_type in test_instances: - print(f" Testing {instance_type}...") - - try: - cost_estimate = monitor.estimate_cost( - instance_type=instance_type, hours_used=1.0 - ) - - results[instance_type] = { - "hourly_cost": cost_estimate.hourly_rate, - "total_cost": cost_estimate.estimated_cost, - "pricing_source": getattr( - cost_estimate, "pricing_source", "hardcoded" - ), - "pricing_warning": getattr(cost_estimate, "pricing_warning", None), - "currency": cost_estimate.currency, - } - - print( - f" 💰 ${cost_estimate.hourly_rate:.3f}/hr (${cost_estimate.estimated_cost:.3f} total)" - ) - if hasattr(cost_estimate, "pricing_source"): - print(f" 📡 Source: {cost_estimate.pricing_source}") - if ( - hasattr(cost_estimate, "pricing_warning") - and cost_estimate.pricing_warning - ): - print(f" ⚠️ Warning: {cost_estimate.pricing_warning}") - - except Exception as e: - print(f" ❌ Error: {e}") - results[instance_type] = {"error": str(e)} - - elapsed = time.time() - start_time - print(f"\n⏱️ Total validation time: {elapsed:.2f}s") - - # Check if we got real pricing data - has_api_pricing = any( - r.get("pricing_source") == "api" - for r in results.values() - if "error" not in r - ) - - if has_credentials and has_api_pricing: - print("\n✅ SUCCESS: Lambda Cloud API pricing validated!") - print(" - Real-time pricing data retrieved") - print(" - API integration working correctly") - elif has_credentials: - print("\n⚠️ PARTIAL: API credentials available but no real-time pricing") - print(" - API may not be available") - print(" - Fallback pricing mechanism working") - else: - print("\n✅ SUCCESS: Lambda Cloud hardcoded pricing validated!") - print(" - Cost estimation working correctly") - print(" - Add credentials for API pricing validation") - - # Display summary - print(f"\n📋 Pricing Summary:") - print(f"{'Instance Type':<15} {'Price/hr':<10} {'Source':<10} {'Status'}") - print("-" * 50) - - for instance, data in results.items(): - if "error" in data: - print(f"{instance:<15} {'ERROR':<10} {'N/A':<10} ❌") - else: - price = f"${data['hourly_cost']:.3f}" - source = data.get("pricing_source", "unknown")[:9] - status = "✅" if data.get("pricing_source") == "api" else "⚠️" - print(f"{instance:<15} {price:<10} {source:<10} {status}") - - return True - - except Exception as e: - print(f"\n❌ VALIDATION FAILED: {e}") - import traceback - - traceback.print_exc() - return False - - -def main(): - """Main validation function.""" - success = validate_lambda_cloud_pricing() - - if success: - print("\n🎉 Lambda Cloud pricing validation completed!") - exit(0) - else: - print("\n💥 Lambda Cloud pricing validation failed!") - exit(1) - - -if __name__ == "__main__": - main() diff --git a/tests/real_world/test_aws_execution_real.py b/tests/real_world/test_aws_execution_real.py deleted file mode 100644 index 9d96549b..00000000 --- a/tests/real_world/test_aws_execution_real.py +++ /dev/null @@ -1,473 +0,0 @@ -""" -Real-world AWS cloud integration tests. - -These tests require actual AWS credentials and create real EC2 instances. -NO MOCKS OR SIMULATIONS - these test real AWS cloud execution. -""" - -import pytest -import os -import time -import logging - -from clustrix import cluster, configure -from tests.real_world.credential_manager import get_aws_credentials - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestAWSExecutionReal: - """Test real AWS cloud job execution.""" - - def setup_method(self): - """Setup for each test method.""" - self.aws_creds = get_aws_credentials() - if not self.aws_creds: - pytest.skip("AWS credentials not available") - - def test_aws_ec2_basic_execution_real(self): - """Test basic function execution on real AWS EC2 instance.""" - - @cluster( - provider="aws", - instance_type="t3.medium", - region="us-east-1", - cores=2, - memory="4GB", - aws_access_key_id=self.aws_creds.get("access_key_id"), - aws_secret_access_key=self.aws_creds.get("secret_access_key"), - aws_region=self.aws_creds.get("region", "us-east-1"), - terminate_on_completion=True, - instance_startup_timeout=600, # 10 minutes for EC2 startup - ) - def test_aws_computation(): - """Simple computation to verify AWS execution works.""" - import platform - import os - import subprocess - - # Get AWS metadata to confirm we're on EC2 - try: - result = subprocess.run( - [ - "curl", - "-s", - "http://169.254.169.254/latest/meta-data/instance-type", - ], - capture_output=True, - text=True, - timeout=10, - ) - instance_type = result.stdout if result.returncode == 0 else "unknown" - except: - instance_type = "metadata_unavailable" - - result = { - "computation": 5 * 7, - "platform": platform.platform(), - "python_version": platform.python_version(), - "aws_instance_type": instance_type, - "working_directory": os.getcwd(), - "environment_check": "aws_success", - } - - return result - - # Execute function - start_time = time.time() - result = test_aws_computation() - execution_time = time.time() - start_time - - # Verify results - assert result is not None - assert result["computation"] == 35 - assert "linux" in result["platform"].lower() - assert result["environment_check"] == "aws_success" - - # Verify execution happened on AWS (not locally) - assert execution_time > 120 # Should take time due to EC2 provisioning - - # Verify we're actually on EC2 - if result["aws_instance_type"] != "metadata_unavailable": - assert ( - "t3.medium" in result["aws_instance_type"] - or "t3" in result["aws_instance_type"] - ) - - logger.info( - f"AWS EC2 basic execution completed in {execution_time:.2f} seconds" - ) - logger.info(f"Instance type: {result['aws_instance_type']}") - logger.info(f"Result: {result}") - - def test_aws_gpu_instance_execution_real(self): - """Test execution on AWS GPU instance (if available and budget allows).""" - - @cluster( - provider="aws", - instance_type="g4dn.xlarge", # GPU instance - region="us-east-1", - cores=4, - memory="16GB", - aws_access_key_id=self.aws_creds.get("access_key_id"), - aws_secret_access_key=self.aws_creds.get("secret_access_key"), - aws_region=self.aws_creds.get("region", "us-east-1"), - terminate_on_completion=True, - instance_startup_timeout=600, - ) - def test_aws_gpu_computation(): - """Test GPU functionality on AWS.""" - import subprocess - import platform - - # Check for GPU - try: - gpu_result = subprocess.run( - ["nvidia-smi", "-L"], capture_output=True, text=True, timeout=30 - ) - gpu_list = ( - gpu_result.stdout if gpu_result.returncode == 0 else "No GPUs found" - ) - except Exception as e: - gpu_list = f"Error checking GPUs: {e}" - - # Get instance metadata - try: - metadata_result = subprocess.run( - [ - "curl", - "-s", - "http://169.254.169.254/latest/meta-data/instance-type", - ], - capture_output=True, - text=True, - timeout=10, - ) - instance_type = ( - metadata_result.stdout - if metadata_result.returncode == 0 - else "unknown" - ) - except: - instance_type = "metadata_unavailable" - - return { - "platform": platform.platform(), - "gpu_detection": gpu_list, - "aws_instance_type": instance_type, - "gpu_test_status": "completed", - } - - # Note: This test may be skipped if GPU instances are too expensive - # or not available in the test account - try: - start_time = time.time() - result = test_aws_gpu_computation() - execution_time = time.time() - start_time - - # Verify results - assert result is not None - assert result["gpu_test_status"] == "completed" - - # Verify we're on a GPU instance - if result["aws_instance_type"] != "metadata_unavailable": - assert "g4dn" in result["aws_instance_type"].lower() - - logger.info(f"AWS GPU test completed in {execution_time:.2f} seconds") - logger.info(f"GPU Detection: {result['gpu_detection']}") - - except Exception as e: - # GPU instances might not be available or budget-restricted - logger.warning( - f"AWS GPU test failed (possibly due to instance availability): {e}" - ) - pytest.skip(f"AWS GPU instance test failed: {e}") - - def test_aws_data_processing_real(self): - """Test data processing capabilities on AWS.""" - - import numpy as np - - # Create test data - test_data = { - "matrix": np.random.randn(50, 50), - "values": list(range(100)), - "metadata": {"test_type": "aws_data_processing", "version": "1.0"}, - } - - @cluster( - provider="aws", - instance_type="c5.large", # Compute-optimized instance - region="us-east-1", - cores=2, - memory="4GB", - aws_access_key_id=self.aws_creds.get("access_key_id"), - aws_secret_access_key=self.aws_creds.get("secret_access_key"), - terminate_on_completion=True, - ) - def process_aws_data(data_dict): - """Process data on AWS instance.""" - import numpy as np - import time - - # Verify data transfer - matrix = data_dict["matrix"] - values = data_dict["values"] - metadata = data_dict["metadata"] - - assert matrix.shape == (50, 50) - assert len(values) == 100 - assert metadata["test_type"] == "aws_data_processing" - - # Perform computation - start_compute = time.time() - matrix_result = np.dot(matrix, matrix.T) - values_result = [x**2 for x in values] - compute_time = time.time() - start_compute - - return { - "matrix_result_shape": matrix_result.shape, - "values_result_sum": sum(values_result), - "compute_time": compute_time, - "data_integrity": "verified", - "aws_processing": "completed", - } - - # Execute data processing - start_time = time.time() - result = process_aws_data(test_data) - total_time = time.time() - start_time - - # Verify results - assert result is not None - assert result["aws_processing"] == "completed" - assert result["data_integrity"] == "verified" - assert result["matrix_result_shape"] == (50, 50) - assert result["values_result_sum"] == sum(x**2 for x in range(100)) - - logger.info(f"AWS data processing completed in {total_time:.2f} seconds") - logger.info(f"Computation time: {result['compute_time']:.4f} seconds") - - def test_aws_cost_monitoring_real(self): - """Test AWS cost monitoring and billing integration.""" - - @cluster( - provider="aws", - instance_type="t3.micro", # Cheapest instance - region="us-east-1", - cores=1, - memory="1GB", - aws_access_key_id=self.aws_creds.get("access_key_id"), - aws_secret_access_key=self.aws_creds.get("secret_access_key"), - terminate_on_completion=True, - ) - def aws_cost_test(): - """Function to test cost monitoring.""" - import time - import subprocess - - # Get instance information for cost calculation - try: - instance_result = subprocess.run( - [ - "curl", - "-s", - "http://169.254.169.254/latest/meta-data/instance-type", - ], - capture_output=True, - text=True, - timeout=10, - ) - instance_type = ( - instance_result.stdout - if instance_result.returncode == 0 - else "unknown" - ) - except: - instance_type = "unknown" - - # Simulate some work - time.sleep(5) - - return { - "cost_test": "completed", - "instance_type": instance_type, - "work_duration": 5, - } - - # Execute with cost tracking - start_time = time.time() - result = aws_cost_test() - execution_duration = time.time() - start_time - - # Verify results - assert result is not None - assert result["cost_test"] == "completed" - - # Verify execution took reasonable time - assert execution_duration > 60 # Should include instance startup - - # In a full implementation, we would: - # - Query AWS Cost Explorer API - # - Verify charges appear in billing - # - Calculate expected vs actual costs - - logger.info( - f"AWS cost monitoring test completed in {execution_duration:.2f} seconds" - ) - logger.info("Note: Check AWS billing console for usage charges") - - def test_aws_multiple_regions_real(self): - """Test AWS execution across multiple regions.""" - - regions = ["us-east-1", "us-west-2"] # Start with two common regions - - for region in regions: - - @cluster( - provider="aws", - instance_type="t3.micro", - region=region, - cores=1, - memory="1GB", - aws_access_key_id=self.aws_creds.get("access_key_id"), - aws_secret_access_key=self.aws_creds.get("secret_access_key"), - terminate_on_completion=True, - ) - def test_region_execution(): - """Test execution in specific AWS region.""" - import subprocess - import time - - # Get availability zone to confirm region - try: - az_result = subprocess.run( - [ - "curl", - "-s", - "http://169.254.169.254/latest/meta-data/placement/availability-zone", - ], - capture_output=True, - text=True, - timeout=10, - ) - availability_zone = ( - az_result.stdout if az_result.returncode == 0 else "unknown" - ) - except: - availability_zone = "unknown" - - return { - "region_tested": region, - "availability_zone": availability_zone, - "region_test": "success", - "timestamp": time.time(), - } - - # Execute in this region - result = test_region_execution() - - # Verify results - assert result is not None - assert result["region_test"] == "success" - assert result["region_tested"] == region - - # Verify we're in the correct region - if result["availability_zone"] != "unknown": - assert region.replace("-", "") in result["availability_zone"].replace( - "-", "" - ) - - logger.info(f"AWS region {region} test completed") - logger.info(f"Availability zone: {result['availability_zone']}") - - # Add delay between regions to avoid rate limits - time.sleep(60) - - def test_aws_error_recovery_real(self): - """Test error handling and recovery with AWS.""" - - @cluster( - provider="aws", - instance_type="t3.micro", - region="us-east-1", - cores=1, - memory="1GB", - aws_access_key_id=self.aws_creds.get("access_key_id"), - aws_secret_access_key=self.aws_creds.get("secret_access_key"), - terminate_on_completion=True, - ) - def aws_error_test(): - """Function that fails to test error handling.""" - import os - - # Do some work first - work_result = sum(range(100)) - - # Create an error - raise RuntimeError("AWS error test - intentional failure") - - # Execute and expect failure - with pytest.raises(RuntimeError) as exc_info: - aws_error_test() - - # Verify error handling - error_message = str(exc_info.value) - assert "failed" in error_message.lower() - assert "intentional failure" in error_message - - logger.info(f"AWS error handling test completed: {error_message}") - - def test_aws_spot_instance_real(self): - """Test AWS Spot instance usage (if supported).""" - # Note: Spot instances require special handling and may not be - # immediately available. This test demonstrates the concept. - - @cluster( - provider="aws", - instance_type="t3.micro", - region="us-east-1", - cores=1, - memory="1GB", - aws_access_key_id=self.aws_creds.get("access_key_id"), - aws_secret_access_key=self.aws_creds.get("secret_access_key"), - # spot_price='0.003', # Would be added in full implementation - terminate_on_completion=True, - ) - def aws_spot_test(): - """Test function for spot instance execution.""" - import subprocess - - # Check if we're on a spot instance - try: - spot_result = subprocess.run( - [ - "curl", - "-s", - "http://169.254.169.254/latest/meta-data/spot/instance-action", - ], - capture_output=True, - text=True, - timeout=10, - ) - spot_status = "spot" if spot_result.returncode != 0 else "on_demand" - except: - spot_status = "unknown" - - return { - "spot_test": "completed", - "instance_billing": spot_status, - "cost_optimization": "tested", - } - - # Execute spot test - result = aws_spot_test() - - # Verify results - assert result is not None - assert result["spot_test"] == "completed" - assert result["cost_optimization"] == "tested" - - logger.info(f"AWS spot instance test completed") - logger.info(f"Instance billing type: {result['instance_billing']}") diff --git a/tests/real_world/test_aws_pricing_real.py b/tests/real_world/test_aws_pricing_real.py deleted file mode 100644 index e3836d66..00000000 --- a/tests/real_world/test_aws_pricing_real.py +++ /dev/null @@ -1,382 +0,0 @@ -""" -Real-world AWS pricing API tests. - -These tests use actual AWS Pricing API with real credentials. -NO MOCKS OR SIMULATIONS - these test real AWS pricing integration. -""" - -import pytest -import logging -import time -import os - -from clustrix.pricing_clients.aws_pricing import AWSPricingClient -from clustrix.cost_providers.aws import AWSCostMonitor -from tests.real_world.credential_manager import get_aws_credentials - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestAWSPricingReal: - """Test real AWS pricing API integration.""" - - def setup_method(self): - """Setup for each test method.""" - self.aws_creds = get_aws_credentials() - if not self.aws_creds: - pytest.skip("AWS credentials not available") - - # Set up AWS credentials in environment for boto3 - os.environ["AWS_ACCESS_KEY_ID"] = self.aws_creds["access_key_id"] - os.environ["AWS_SECRET_ACCESS_KEY"] = self.aws_creds["secret_access_key"] - os.environ["AWS_DEFAULT_REGION"] = self.aws_creds["region"] - - def test_aws_pricing_client_api_connection_real(self): - """Test AWS Pricing API connection with real credentials.""" - client = AWSPricingClient() - - # Test getting pricing for a common instance type - instance_type = "t2.micro" - region = "us-east-1" - - price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - - # Should get a valid price from API or fallback - assert price is not None - assert isinstance(price, (int, float)) - assert price > 0 - assert price < 10 # t2.micro should be under $10/hour - - logger.info(f"AWS {instance_type} in {region}: ${price:.4f}/hour") - - def test_aws_pricing_api_instance_types_real(self): - """Test real AWS Pricing API returns valid instance type data.""" - client = AWSPricingClient() - - # Test common instance types - test_instances = ["t2.micro", "t3.small", "m5.large", "c5.xlarge", "r5.large"] - - pricing_results = {} - - for instance_type in test_instances: - price = client.get_instance_pricing( - instance_type=instance_type, - region="us-east-1", - operating_system="Linux", - ) - - if price is not None: - pricing_results[instance_type] = price - assert price > 0 - assert price < 100 # Reasonable upper bound for these instances - logger.info(f"AWS {instance_type}: ${price:.4f}/hour") - else: - logger.warning(f"No pricing found for {instance_type}") - - # Should have found pricing for most instances - assert len(pricing_results) >= 3 - - def test_aws_pricing_different_regions_real(self): - """Test AWS pricing in different regions with real API.""" - client = AWSPricingClient() - - instance_type = "t2.micro" - regions = ["us-east-1", "us-west-2", "eu-west-1"] - - regional_pricing = {} - - for region in regions: - price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - - if price is not None: - regional_pricing[region] = price - logger.info(f"AWS {instance_type} in {region}: ${price:.4f}/hour") - - # Should have found pricing for most regions - assert len(regional_pricing) >= 2 - - # Verify pricing differences are reasonable - if len(regional_pricing) > 1: - prices = list(regional_pricing.values()) - max_price = max(prices) - min_price = min(prices) - price_variance = (max_price - min_price) / min_price * 100 - - logger.info( - f"Regional price variance for {instance_type}: {price_variance:.1f}%" - ) - - # AWS regional pricing can vary but shouldn't be too extreme - assert price_variance < 50 # Allow up to 50% regional variation - - def test_aws_pricing_different_os_real(self): - """Test AWS pricing for different operating systems.""" - client = AWSPricingClient() - - instance_type = "t2.micro" - region = "us-east-1" - operating_systems = ["Linux", "Windows"] - - os_pricing = {} - - for os_type in operating_systems: - price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system=os_type - ) - - if price is not None: - os_pricing[os_type] = price - logger.info(f"AWS {instance_type} ({os_type}): ${price:.4f}/hour") - - # Should have found pricing for at least Linux - assert "Linux" in os_pricing - - # Windows should typically cost more than Linux - if "Windows" in os_pricing and "Linux" in os_pricing: - windows_price = os_pricing["Windows"] - linux_price = os_pricing["Linux"] - assert windows_price >= linux_price - - def test_aws_pricing_gpu_instances_real(self): - """Test AWS pricing for GPU instances with real API.""" - client = AWSPricingClient() - - # Test GPU instance types - gpu_instances = ["p3.2xlarge", "g4dn.xlarge", "p3.8xlarge"] - - gpu_pricing = {} - - for instance_type in gpu_instances: - price = client.get_instance_pricing( - instance_type=instance_type, - region="us-east-1", - operating_system="Linux", - ) - - if price is not None: - gpu_pricing[instance_type] = price - assert price > 0.5 # GPU instances should be more expensive - assert price < 50 # But not more than $50/hour for these - logger.info(f"AWS GPU {instance_type}: ${price:.3f}/hour") - - # Should have found pricing for at least some GPU instances - assert len(gpu_pricing) >= 1 - - # Verify pricing relationships make sense - if "p3.2xlarge" in gpu_pricing and "p3.8xlarge" in gpu_pricing: - p3_2xl = gpu_pricing["p3.2xlarge"] - p3_8xl = gpu_pricing["p3.8xlarge"] - # p3.8xlarge should cost more than p3.2xlarge - assert p3_8xl > p3_2xl - # But not more than 5x (due to shared costs) - assert p3_8xl < p3_2xl * 5 - - def test_aws_pricing_cache_behavior_real(self): - """Test AWS pricing cache behavior with real API.""" - client = AWSPricingClient(cache_ttl_hours=1) # Short TTL for testing - - instance_type = "t2.micro" - region = "us-east-1" - - # First call - should hit API or fallback - start_time = time.time() - price1 = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - first_call_time = time.time() - start_time - - # Second call - should hit cache - start_time = time.time() - price2 = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - second_call_time = time.time() - start_time - - # Verify results - assert price1 == price2 # Same pricing - assert second_call_time < first_call_time # Cache should be faster - - logger.info( - f"First call: {first_call_time:.3f}s, Cached call: {second_call_time:.3f}s" - ) - - def test_aws_pricing_error_handling_real(self): - """Test AWS pricing error handling with real API.""" - client = AWSPricingClient() - - # Test with invalid instance type - invalid_price = client.get_instance_pricing( - instance_type="invalid.instance.type", - region="us-east-1", - operating_system="Linux", - ) - - # Should return None for invalid instances (not crash) - # Note: AWS might return hardcoded fallback pricing - if invalid_price is not None: - assert invalid_price > 0 - logger.info( - f"Invalid instance type returned fallback price: ${invalid_price:.3f}" - ) - else: - logger.info("Invalid instance type correctly returned None") - - def test_aws_cost_monitor_integration_real(self): - """Test AWS cost monitor integration with real API.""" - # Test cost monitor (it uses pricing client internally) - monitor = AWSCostMonitor() - - # Test cost estimation - instance_type = "t2.micro" - hours_used = 2.5 - - # This will use the pricing client internally - cost_estimate = monitor.estimate_cost(instance_type, hours_used) - - # Verify cost estimate - assert cost_estimate is not None - assert cost_estimate.instance_type == instance_type - assert cost_estimate.hours_used == hours_used - assert cost_estimate.hourly_rate > 0 - assert cost_estimate.estimated_cost > 0 - assert cost_estimate.currency == "USD" - - logger.info( - f"AWS cost estimate: " - f"${cost_estimate.estimated_cost:.3f} for {hours_used} hours" - ) - - def test_aws_pricing_vs_hardcoded_comparison(self): - """Compare AWS API pricing vs hardcoded pricing.""" - client = AWSPricingClient() - - # Get hardcoded pricing - hardcoded_pricing = client._hardcoded_pricing - - # Test a few common instances - common_instances = ["t2.micro", "t2.small", "m5.large", "c5.large"] - - pricing_comparison = [] - - for instance_type in common_instances: - if instance_type in hardcoded_pricing: - # Get API pricing - api_price = client.get_instance_pricing( - instance_type=instance_type, - region="us-east-1", - operating_system="Linux", - ) - - hardcoded_price = hardcoded_pricing[instance_type] - - if api_price is not None: - # Calculate percentage difference - diff_percent = ( - abs(api_price - hardcoded_price) / hardcoded_price * 100 - ) - - pricing_comparison.append( - { - "instance_type": instance_type, - "api_price": api_price, - "hardcoded_price": hardcoded_price, - "difference_percent": diff_percent, - } - ) - - logger.info( - f"{instance_type}: API ${api_price:.4f} vs " - f"Hardcoded ${hardcoded_price:.4f} ({diff_percent:.1f}% diff)" - ) - - # Should have some pricing comparisons - assert len(pricing_comparison) > 0 - - # Log any large differences for review - large_differences = [ - p for p in pricing_comparison if p["difference_percent"] > 50 - ] - - if large_differences: - logger.warning( - f"Found {len(large_differences)} instances " - f"with >50% pricing differences" - ) - for diff in large_differences: - logger.warning( - f" {diff['instance_type']}: " - f"{diff['difference_percent']:.1f}% difference" - ) - - def test_aws_pricing_api_performance(self): - """Test AWS pricing API performance.""" - client = AWSPricingClient() - - # Test API response time for single instance - start_time = time.time() - price = client.get_instance_pricing( - instance_type="t2.micro", region="us-east-1", operating_system="Linux" - ) - api_response_time = time.time() - start_time - - # Verify performance - assert api_response_time < 30.0 # AWS API can be slower, allow 30 seconds - assert price is not None - - logger.info(f"AWS pricing API response time: {api_response_time:.3f} seconds") - - def test_aws_spot_pricing_real(self): - """Test AWS spot pricing estimation.""" - client = AWSPricingClient() - - instance_type = "t2.micro" - region = "us-east-1" - - # Get on-demand pricing - on_demand_price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - - # Get spot pricing estimation - spot_price = client.get_spot_pricing(instance_type, region) - - if on_demand_price and spot_price: - # Spot should be cheaper than on-demand - assert spot_price < on_demand_price - - discount_percent = (1 - spot_price / on_demand_price) * 100 - logger.info(f"AWS {instance_type} spot discount: {discount_percent:.1f}%") - - # Spot discount should be reasonable (10-90%) - assert 10 <= discount_percent <= 90 - - def test_aws_pricing_client_info(self): - """Test AWS pricing client information.""" - client = AWSPricingClient() - - # Get hardcoded pricing info (no get_pricing_info method in AWS client) - hardcoded_pricing = client._hardcoded_pricing - pricing_date = client._hardcoded_pricing_date - - # Verify hardcoded pricing structure - assert isinstance(hardcoded_pricing, dict) - assert len(hardcoded_pricing) > 0 - assert pricing_date is not None - - # Check if pricing data might be outdated - is_outdated = client.is_pricing_data_outdated(days=30) - logger.info( - f"AWS hardcoded pricing date: {pricing_date}, outdated: {is_outdated}" - ) - - def teardown_method(self): - """Cleanup after each test.""" - # Clean up environment variables - for var in ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_DEFAULT_REGION"]: - if var in os.environ: - del os.environ[var] diff --git a/tests/real_world/test_azure_pricing_real.py b/tests/real_world/test_azure_pricing_real.py deleted file mode 100644 index 811c2c6c..00000000 --- a/tests/real_world/test_azure_pricing_real.py +++ /dev/null @@ -1,501 +0,0 @@ -""" -Real-world Azure pricing API tests. - -These tests use actual Azure Retail Prices API with real credentials. -NO MOCKS OR SIMULATIONS - these test real Azure pricing integration. -""" - -import pytest -import logging -import time -import os -import tempfile -import json - -from clustrix.pricing_clients.azure_pricing import AzurePricingClient -from clustrix.cost_providers.azure import AzureCostMonitor -from tests.real_world.credential_manager import get_azure_credentials - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestAzurePricingReal: - """Test real Azure pricing API integration.""" - - def setup_method(self): - """Setup for each test method.""" - self.azure_creds = get_azure_credentials() - if not self.azure_creds: - pytest.skip("Azure credentials not available") - - # Set up Azure credentials in environment (if needed for service auth) - if "subscription_id" in self.azure_creds: - os.environ["AZURE_SUBSCRIPTION_ID"] = self.azure_creds["subscription_id"] - if "tenant_id" in self.azure_creds: - os.environ["AZURE_TENANT_ID"] = self.azure_creds["tenant_id"] - if "client_id" in self.azure_creds: - os.environ["AZURE_CLIENT_ID"] = self.azure_creds["client_id"] - if "client_secret" in self.azure_creds: - os.environ["AZURE_CLIENT_SECRET"] = self.azure_creds["client_secret"] - - # Set up service account JSON if available - if "service_account_json" in self.azure_creds: - # Create temporary file for service account - self.temp_cred_file = tempfile.NamedTemporaryFile( - mode="w", suffix=".json", delete=False - ) - json.dump( - json.loads(self.azure_creds["service_account_json"]), - self.temp_cred_file, - indent=2, - ) - self.temp_cred_file.close() - os.environ["AZURE_APPLICATION_CREDENTIALS"] = self.temp_cred_file.name - else: - self.temp_cred_file = None - - def test_azure_pricing_client_api_connection_real(self): - """Test Azure Retail Prices API connection with real API.""" - client = AzurePricingClient() - - # Test getting pricing for a common VM size - instance_type = "Standard_D2s_v3" - region = "eastus" - - price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - - # Should get a valid price from API or fallback - assert price is not None - assert isinstance(price, (int, float)) - assert price > 0 - assert price < 10 # Standard_D2s_v3 should be under $10/hour - - logger.info(f"Azure {instance_type} in {region}: ${price:.4f}/hour") - - def test_azure_pricing_api_instance_types_real(self): - """Test real Azure Retail Prices API returns valid VM data.""" - client = AzurePricingClient() - - # Test common Azure VM sizes - test_instances = [ - "Standard_D2s_v3", - "Standard_D4s_v3", - "Standard_F2s_v2", - "Standard_E2s_v3", - "Standard_A2_v2", - ] - - pricing_results = {} - - for instance_type in test_instances: - price = client.get_instance_pricing( - instance_type=instance_type, region="eastus", operating_system="Linux" - ) - - if price is not None: - pricing_results[instance_type] = price - assert price > 0 - assert price < 100 # Reasonable upper bound for these VMs - logger.info(f"Azure {instance_type}: ${price:.4f}/hour") - else: - logger.warning(f"No pricing found for {instance_type}") - - # Should have found pricing for most VMs - assert len(pricing_results) >= 3 - - def test_azure_pricing_different_regions_real(self): - """Test Azure pricing in different regions with real API.""" - client = AzurePricingClient() - - instance_type = "Standard_D2s_v3" - regions = ["eastus", "westus2", "westeurope"] - - regional_pricing = {} - - for region in regions: - price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - - if price is not None: - regional_pricing[region] = price - logger.info(f"Azure {instance_type} in {region}: ${price:.4f}/hour") - - # Should have found pricing for most regions - assert len(regional_pricing) >= 2 - - # Verify pricing differences are reasonable - if len(regional_pricing) > 1: - prices = list(regional_pricing.values()) - max_price = max(prices) - min_price = min(prices) - price_variance = (max_price - min_price) / min_price * 100 - - logger.info( - f"Regional price variance for {instance_type}: {price_variance:.1f}%" - ) - - # Azure regional pricing can vary but shouldn't be too extreme - assert price_variance < 50 # Allow up to 50% regional variation - - def test_azure_pricing_different_os_real(self): - """Test Azure pricing for different operating systems.""" - client = AzurePricingClient() - - instance_type = "Standard_D2s_v3" - region = "eastus" - operating_systems = ["Linux", "Windows"] - - os_pricing = {} - - for os_type in operating_systems: - price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system=os_type - ) - - if price is not None: - os_pricing[os_type] = price - logger.info(f"Azure {instance_type} ({os_type}): ${price:.4f}/hour") - - # Should have found pricing for at least Linux - assert "Linux" in os_pricing - - # Windows should typically cost more than Linux - if "Windows" in os_pricing and "Linux" in os_pricing: - windows_price = os_pricing["Windows"] - linux_price = os_pricing["Linux"] - # Allow some flexibility - sometimes they're the same - assert ( - windows_price >= linux_price * 0.9 - ) # Windows at least 90% of Linux price - - def test_azure_pricing_gpu_instances_real(self): - """Test Azure pricing for GPU VMs with real API.""" - client = AzurePricingClient() - - # Test GPU VM sizes - gpu_instances = ["Standard_NC6s_v3", "Standard_NC12s_v3", "Standard_ND40rs_v2"] - - gpu_pricing = {} - - for instance_type in gpu_instances: - price = client.get_instance_pricing( - instance_type=instance_type, region="eastus", operating_system="Linux" - ) - - if price is not None: - gpu_pricing[instance_type] = price - assert price > 1.0 # GPU VMs should be more expensive - assert price < 100 # But not more than $100/hour for these - logger.info(f"Azure GPU {instance_type}: ${price:.3f}/hour") - - # Should have found pricing for at least some GPU VMs - assert len(gpu_pricing) >= 1 - - # Verify pricing relationships make sense - if "Standard_NC6s_v3" in gpu_pricing and "Standard_NC12s_v3" in gpu_pricing: - nc6_price = gpu_pricing["Standard_NC6s_v3"] - nc12_price = gpu_pricing["Standard_NC12s_v3"] - # NC12 should cost more than NC6 - assert nc12_price > nc6_price - # But not more than 3x (due to shared costs) - assert nc12_price < nc6_price * 3 - - def test_azure_pricing_cache_behavior_real(self): - """Test Azure pricing cache behavior with real API.""" - client = AzurePricingClient(cache_ttl_hours=1) # Short TTL for testing - - instance_type = "Standard_D2s_v3" - region = "eastus" - - # First call - should hit API or fallback - start_time = time.time() - price1 = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - first_call_time = time.time() - start_time - - # Second call - should hit cache - start_time = time.time() - price2 = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - second_call_time = time.time() - start_time - - # Verify results - assert price1 == price2 # Same pricing - assert second_call_time < first_call_time # Cache should be faster - - logger.info( - f"First call: {first_call_time:.3f}s, Cached call: {second_call_time:.3f}s" - ) - - def test_azure_pricing_error_handling_real(self): - """Test Azure pricing error handling with real API.""" - client = AzurePricingClient() - - # Test with invalid VM size - invalid_price = client.get_instance_pricing( - instance_type="Standard_InvalidVM_v999", - region="eastus", - operating_system="Linux", - ) - - # Should return fallback default price for invalid VMs - if invalid_price is not None: - assert invalid_price > 0 - logger.info( - f"Invalid VM size returned fallback price: ${invalid_price:.3f}" - ) - else: - logger.info("Invalid VM size correctly returned None") - - def test_azure_cost_monitor_integration_real(self): - """Test Azure cost monitor integration with real API.""" - # Test cost monitor (it uses pricing client internally) - monitor = AzureCostMonitor() - - # Test cost estimation - instance_type = "Standard_D2s_v3" - hours_used = 2.5 - - # This will use the pricing client internally - cost_estimate = monitor.estimate_cost(instance_type, hours_used) - - # Verify cost estimate - assert cost_estimate is not None - assert cost_estimate.instance_type == instance_type - assert cost_estimate.hours_used == hours_used - assert cost_estimate.hourly_rate > 0 - assert cost_estimate.estimated_cost > 0 - assert cost_estimate.currency == "USD" - - logger.info( - f"Azure cost estimate: " - f"${cost_estimate.estimated_cost:.3f} for {hours_used} hours" - ) - - def test_azure_pricing_vs_hardcoded_comparison(self): - """Compare Azure API pricing vs hardcoded pricing.""" - client = AzurePricingClient() - - # Get hardcoded pricing - hardcoded_pricing = client._hardcoded_pricing - - # Test a few common VMs - common_instances = [ - "Standard_D2s_v3", - "Standard_D4s_v3", - "Standard_E2s_v3", - "Standard_F2s_v2", - ] - - pricing_comparison = [] - - for instance_type in common_instances: - if instance_type in hardcoded_pricing: - # Get API pricing - api_price = client.get_instance_pricing( - instance_type=instance_type, - region="eastus", - operating_system="Linux", - ) - - hardcoded_price = hardcoded_pricing[instance_type] - - if api_price is not None: - # Calculate percentage difference - diff_percent = ( - abs(api_price - hardcoded_price) / hardcoded_price * 100 - ) - - pricing_comparison.append( - { - "instance_type": instance_type, - "api_price": api_price, - "hardcoded_price": hardcoded_price, - "difference_percent": diff_percent, - } - ) - - logger.info( - f"{instance_type}: API ${api_price:.4f} vs " - f"Hardcoded ${hardcoded_price:.4f} ({diff_percent:.1f}% diff)" - ) - - # Should have some pricing comparisons - assert len(pricing_comparison) > 0 - - # Log any large differences for review - large_differences = [ - p for p in pricing_comparison if p["difference_percent"] > 50 - ] - - if large_differences: - logger.warning( - f"Found {len(large_differences)} VMs " f"with >50% pricing differences" - ) - for diff in large_differences: - logger.warning( - f" {diff['instance_type']}: " - f"{diff['difference_percent']:.1f}% difference" - ) - - def test_azure_pricing_api_performance(self): - """Test Azure pricing API performance.""" - client = AzurePricingClient() - - # Test API response time for single VM - start_time = time.time() - price = client.get_instance_pricing( - instance_type="Standard_D2s_v3", region="eastus", operating_system="Linux" - ) - api_response_time = time.time() - start_time - - # Verify performance - assert api_response_time < 30.0 # Azure API can be slower, allow 30 seconds - assert price is not None - - logger.info(f"Azure pricing API response time: {api_response_time:.3f} seconds") - - def test_azure_spot_pricing_real(self): - """Test Azure spot pricing with real API.""" - client = AzurePricingClient() - - instance_type = "Standard_D2s_v3" - region = "eastus" - - # Get on-demand pricing - on_demand_price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - - # Get spot pricing - spot_price = client.get_spot_pricing(instance_type, region) - - if on_demand_price and spot_price: - # Spot should be cheaper than on-demand - assert spot_price < on_demand_price - - discount_percent = (1 - spot_price / on_demand_price) * 100 - logger.info(f"Azure {instance_type} spot discount: {discount_percent:.1f}%") - - # Spot discount should be reasonable (10-90%) - assert 10 <= discount_percent <= 90 - - def test_azure_pricing_service_query_real(self): - """Test Azure pricing service-wide query with real API.""" - client = AzurePricingClient() - - # Get pricing for multiple VMs in Virtual Machines service - service_pricing = client.get_pricing_by_service( - service_name="Virtual Machines", region="eastus" - ) - - # Should get some pricing data - assert isinstance(service_pricing, dict) - - if service_pricing: - logger.info(f"Retrieved pricing for {len(service_pricing)} Azure VM sizes") - - # Verify pricing data structure - for vm_size, pricing_info in list(service_pricing.items())[ - :5 - ]: # Check first 5 - assert isinstance(pricing_info, dict) - assert "price" in pricing_info - assert isinstance(pricing_info["price"], (int, float)) - assert pricing_info["price"] > 0 - logger.debug(f"Azure {vm_size}: ${pricing_info['price']:.4f}/hour") - - def test_azure_pricing_region_mapping(self): - """Test Azure region name mapping.""" - client = AzurePricingClient() - - # Test region mapping - test_regions = { - "eastus": "East US", - "westeurope": "West Europe", - "eastasia": "East Asia", - } - - for region_code, expected_name in test_regions.items(): - mapped_name = client._get_region_name(region_code) - assert mapped_name == expected_name - logger.info(f"Region mapping: {region_code} -> {mapped_name}") - - def test_azure_pricing_client_info(self): - """Test Azure pricing client information.""" - client = AzurePricingClient() - - # Get hardcoded pricing info - hardcoded_pricing = client._hardcoded_pricing - pricing_date = client._hardcoded_pricing_date - api_url = client.api_url - api_version = client.api_version - - # Verify pricing client structure - assert isinstance(hardcoded_pricing, dict) - assert len(hardcoded_pricing) > 0 - assert pricing_date is not None - assert api_url == "https://prices.azure.com/api/retail/prices" - assert api_version is not None - - # Check if pricing data might be outdated - is_outdated = client.is_pricing_data_outdated(days=30) - logger.info( - f"Azure hardcoded pricing date: {pricing_date}, outdated: {is_outdated}" - ) - logger.info(f"Azure Retail Prices API: {api_url} (v{api_version})") - - def test_azure_pricing_filters_real(self): - """Test Azure pricing API filters work correctly.""" - client = AzurePricingClient() - - instance_type = "Standard_D2s_v3" - region = "eastus" - - # Test Linux pricing - linux_price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - - # Test Windows pricing - windows_price = client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Windows" - ) - - if linux_price and windows_price: - # Both should be positive - assert linux_price > 0 - assert windows_price > 0 - - # They might be different (Windows usually costs more) - logger.info( - f"Azure {instance_type} Linux: ${linux_price:.4f}, " - f"Windows: ${windows_price:.4f}" - ) - - def teardown_method(self): - """Cleanup after each test.""" - # Clean up environment variables - azure_env_vars = [ - "AZURE_SUBSCRIPTION_ID", - "AZURE_TENANT_ID", - "AZURE_CLIENT_ID", - "AZURE_CLIENT_SECRET", - "AZURE_APPLICATION_CREDENTIALS", - ] - for var in azure_env_vars: - if var in os.environ: - del os.environ[var] - - # Clean up temporary credential file - if hasattr(self, "temp_cred_file") and self.temp_cred_file: - try: - os.unlink(self.temp_cred_file.name) - except OSError: - pass diff --git a/tests/real_world/test_cloud_apis_real.py b/tests/real_world/test_cloud_apis_real.py deleted file mode 100644 index 8f455dae..00000000 --- a/tests/real_world/test_cloud_apis_real.py +++ /dev/null @@ -1,597 +0,0 @@ -""" -Real-world cloud API tests for Clustrix. - -These tests use actual cloud provider APIs to verify that our -cloud integration code works correctly. Tests are designed to -use free-tier or minimal-cost operations. -""" - -import os -import json -import pytest -from datetime import datetime -from unittest.mock import patch - -from clustrix.cloud_providers.aws import AWSProvider -from clustrix.cloud_providers.azure import AzureProvider -from clustrix.cloud_providers.gcp import GCPProvider -from clustrix.cloud_providers.lambda_cloud import LambdaCloudProvider -from clustrix.pricing_clients.aws_pricing import AWSPricingClient -from clustrix.pricing_clients.azure_pricing import AzurePricingClient -from clustrix.pricing_clients.gcp_pricing import GCPPricingClient -from tests.real_world import credentials, test_manager - - -@pytest.mark.real_world -class TestAWSAPIReal: - """Test real AWS API calls.""" - - @pytest.fixture - def aws_provider(self): - """Create AWSProvider with real credentials.""" - creds = credentials.get_aws_credentials() - if not creds: - pytest.skip("No AWS credentials available") - - provider = AWSProvider() - provider.authenticate( - access_key_id=creds["access_key_id"], - secret_access_key=creds["secret_access_key"], - region=creds["region"], - ) - return provider - - def test_aws_authentication_real(self, aws_provider): - """Test real AWS authentication with STS.""" - if not test_manager.can_make_api_call(0.00): # STS calls are free - pytest.skip("API call limit reached") - - try: - # Use STS to verify authentication (free operation) - import boto3 - - sts_client = boto3.client( - "sts", - aws_access_key_id=aws_provider.credentials["access_key_id"], - aws_secret_access_key=aws_provider.credentials["secret_access_key"], - region_name=aws_provider.region, - ) - - response = sts_client.get_caller_identity() - test_manager.record_api_call(0.00) - - # Verify response structure - assert "Account" in response - assert "UserId" in response - assert "Arn" in response - assert response["Account"].isdigit() - assert len(response["Account"]) == 12 # AWS account IDs are 12 digits - - except Exception as e: - pytest.skip(f"AWS authentication failed: {e}") - - def test_aws_pricing_api_real(self): - """Test real AWS pricing API calls.""" - if not test_manager.can_make_api_call(0.00): # Pricing API is free - pytest.skip("API call limit reached") - - try: - import boto3 - - # Use AWS pricing API (free tier) - pricing_client = boto3.client("pricing", region_name="us-east-1") - - # Get pricing for t2.micro (free tier eligible) - response = pricing_client.get_products( - ServiceCode="AmazonEC2", - Filters=[ - { - "Type": "TERM_MATCH", - "Field": "instanceType", - "Value": "t2.micro", - }, - { - "Type": "TERM_MATCH", - "Field": "operatingSystem", - "Value": "Linux", - }, - { - "Type": "TERM_MATCH", - "Field": "location", - "Value": "US East (N. Virginia)", - }, - {"Type": "TERM_MATCH", "Field": "tenancy", "Value": "Shared"}, - {"Type": "TERM_MATCH", "Field": "preInstalledSw", "Value": "NA"}, - ], - MaxResults=1, - ) - - test_manager.record_api_call(0.00) - - # Verify response - assert "PriceList" in response - assert len(response["PriceList"]) > 0 - - # Parse pricing data - price_data = json.loads(response["PriceList"][0]) - assert "product" in price_data - assert "terms" in price_data - - # Verify instance type - product = price_data["product"] - assert product["attributes"]["instanceType"] == "t2.micro" - - except Exception as e: - pytest.skip(f"AWS pricing API failed: {e}") - - def test_aws_ec2_describe_regions_real(self): - """Test AWS EC2 describe regions (free operation).""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - import boto3 - - ec2_client = boto3.client("ec2", region_name="us-east-1") - - # Describe regions (free operation) - response = ec2_client.describe_regions() - test_manager.record_api_call(0.00) - - # Verify response - assert "Regions" in response - assert len(response["Regions"]) > 0 - - # Check for common regions - region_names = [r["RegionName"] for r in response["Regions"]] - assert "us-east-1" in region_names - assert "us-west-2" in region_names - assert "eu-west-1" in region_names - - except Exception as e: - pytest.skip(f"AWS EC2 API failed: {e}") - - -@pytest.mark.real_world -class TestAzureAPIReal: - """Test real Azure API calls.""" - - @pytest.fixture - def azure_provider(self): - """Create AzureProvider with real credentials.""" - creds = credentials.get_azure_credentials() - if not creds: - pytest.skip("No Azure credentials available") - - provider = AzureProvider() - provider.authenticate( - subscription_id=creds["subscription_id"], - tenant_id=creds.get("tenant_id"), - client_id=creds.get("client_id"), - client_secret=creds.get("client_secret"), - ) - return provider - - def test_azure_authentication_real(self, azure_provider): - """Test real Azure authentication.""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - from azure.identity import DefaultAzureCredential - from azure.mgmt.resource import ResourceManagementClient - - # Use default credentials (free operation) - credential = DefaultAzureCredential() - subscription_id = azure_provider.subscription_id - - # Create resource management client - resource_client = ResourceManagementClient(credential, subscription_id) - - # List resource groups (free operation) - resource_groups = list(resource_client.resource_groups.list()) - test_manager.record_api_call(0.00) - - # Verify we can list resource groups - assert isinstance(resource_groups, list) - # May be empty, that's valid - - except Exception as e: - pytest.skip(f"Azure authentication failed: {e}") - - def test_azure_compute_api_real(self): - """Test Azure compute API calls.""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - from azure.identity import DefaultAzureCredential - from azure.mgmt.compute import ComputeManagementClient - - creds = credentials.get_azure_credentials() - if not creds: - pytest.skip("No Azure credentials available") - - credential = DefaultAzureCredential() - compute_client = ComputeManagementClient( - credential, creds["subscription_id"] - ) - - # List VM sizes in East US (free operation) - vm_sizes = list(compute_client.virtual_machine_sizes.list("eastus")) - test_manager.record_api_call(0.00) - - # Verify response - assert len(vm_sizes) > 0 - - # Look for common VM sizes - size_names = [size.name for size in vm_sizes] - assert any("Standard_B" in name for name in size_names) # Burstable VMs - - except Exception as e: - pytest.skip(f"Azure compute API failed: {e}") - - -@pytest.mark.real_world -class TestGCPAPIReal: - """Test real GCP API calls.""" - - @pytest.fixture - def gcp_provider(self): - """Create GCPProvider with real credentials.""" - creds = credentials.get_gcp_credentials() - if not creds: - pytest.skip("No GCP credentials available") - - provider = GCPProvider() - provider.authenticate( - project_id=creds["project_id"], - service_account_path=creds.get("service_account_path"), - ) - return provider - - def test_gcp_authentication_real(self, gcp_provider): - """Test real GCP authentication.""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - from google.auth import default - from google.cloud import compute_v1 - - # Get default credentials - credentials, project = default() - - # Create compute client - client = compute_v1.InstancesClient(credentials=credentials) - - # List instances in us-central1-a (free operation if no instances) - instances = client.list(project=project, zone="us-central1-a") - test_manager.record_api_call(0.00) - - # Verify we can list instances (may be empty) - instance_list = list(instances) - assert isinstance(instance_list, list) - - except Exception as e: - pytest.skip(f"GCP authentication failed: {e}") - - def test_gcp_compute_zones_real(self): - """Test GCP compute zones API.""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - from google.auth import default - from google.cloud import compute_v1 - - creds = credentials.get_gcp_credentials() - if not creds: - pytest.skip("No GCP credentials available") - - gcp_credentials, project = default() - client = compute_v1.ZonesClient(credentials=gcp_credentials) - - # List zones (free operation) - zones = client.list(project=project) - test_manager.record_api_call(0.00) - - # Verify response - zone_list = list(zones) - assert len(zone_list) > 0 - - # Check for common zones - zone_names = [zone.name for zone in zone_list] - assert any("us-central1" in name for name in zone_names) - assert any("us-east1" in name for name in zone_names) - - except Exception as e: - pytest.skip(f"GCP zones API failed: {e}") - - -@pytest.mark.real_world -class TestLambdaCloudAPIReal: - """Test real Lambda Cloud API calls.""" - - def test_lambda_cloud_instance_types_real(self): - """Test Lambda Cloud instance types API.""" - if not test_manager.can_make_api_call(0.01): # Minimal cost - pytest.skip("API call limit reached") - - try: - import requests - - # Lambda Cloud public API endpoint - url = "https://cloud.lambdalabs.com/api/v1/instance-types" - - # Make API call (usually free for public endpoints) - response = requests.get(url, timeout=10) - test_manager.record_api_call(0.01) - - # Verify response - assert response.status_code == 200 - - data = response.json() - assert "data" in data - assert isinstance(data["data"], dict) - - # Verify instance type structure - for instance_type, details in data["data"].items(): - assert "description" in details - assert "price_cents_per_hour" in details - assert isinstance(details["price_cents_per_hour"], int) - - except Exception as e: - pytest.skip(f"Lambda Cloud API failed: {e}") - - def test_lambda_cloud_regions_real(self): - """Test Lambda Cloud regions API.""" - if not test_manager.can_make_api_call(0.01): - pytest.skip("API call limit reached") - - try: - import requests - - # Lambda Cloud regions endpoint - url = "https://cloud.lambdalabs.com/api/v1/regions" - - response = requests.get(url, timeout=10) - test_manager.record_api_call(0.01) - - # Verify response - assert response.status_code == 200 - - data = response.json() - assert "data" in data - assert isinstance(data["data"], list) - - # Verify region structure - for region in data["data"]: - assert "name" in region - assert "description" in region - - except Exception as e: - pytest.skip(f"Lambda Cloud regions API failed: {e}") - - -@pytest.mark.real_world -class TestPricingClientsReal: - """Test real pricing client implementations.""" - - def test_aws_pricing_client_real(self): - """Test AWSPricingClient with real API calls.""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - client = AWSPricingClient() - - # Get pricing for t2.micro - pricing_info = client.get_instance_pricing( - instance_type="t2.micro", region="us-east-1" - ) - test_manager.record_api_call(0.00) - - # Verify pricing info structure - assert pricing_info is not None - assert "hourly_price" in pricing_info - assert "currency" in pricing_info - assert pricing_info["currency"] == "USD" - assert float(pricing_info["hourly_price"]) >= 0 - - except Exception as e: - pytest.skip(f"AWS pricing client failed: {e}") - - def test_azure_pricing_client_real(self): - """Test AzurePricingClient with real API calls.""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - client = AzurePricingClient() - - # Get pricing for Standard_B1s - pricing_info = client.get_instance_pricing( - instance_type="Standard_B1s", region="eastus" - ) - test_manager.record_api_call(0.00) - - # Verify pricing info structure - assert pricing_info is not None - assert "hourly_price" in pricing_info - assert "currency" in pricing_info - assert pricing_info["currency"] == "USD" - assert float(pricing_info["hourly_price"]) >= 0 - - except Exception as e: - pytest.skip(f"Azure pricing client failed: {e}") - - def test_gcp_pricing_client_real(self): - """Test GCPPricingClient with real API calls.""" - if not test_manager.can_make_api_call(0.00): - pytest.skip("API call limit reached") - - try: - client = GCPPricingClient() - - # Get pricing for e2-micro - pricing_info = client.get_instance_pricing( - instance_type="e2-micro", region="us-central1" - ) - test_manager.record_api_call(0.00) - - # Verify pricing info structure - assert pricing_info is not None - assert "hourly_price" in pricing_info - assert "currency" in pricing_info - assert pricing_info["currency"] == "USD" - assert float(pricing_info["hourly_price"]) >= 0 - - except Exception as e: - pytest.skip(f"GCP pricing client failed: {e}") - - -@pytest.mark.real_world -class TestHTTPRequestsReal: - """Test real HTTP requests used by various components.""" - - def test_huggingface_api_real(self): - """Test HuggingFace API calls.""" - if not test_manager.can_make_api_call(0.00): # Public API is free - pytest.skip("API call limit reached") - - try: - import requests - - # HuggingFace public API - url = "https://huggingface.co/api/models" - params = {"limit": 5, "filter": "text-generation"} - - response = requests.get(url, params=params, timeout=10) - test_manager.record_api_call(0.00) - - # Verify response - assert response.status_code == 200 - - data = response.json() - assert isinstance(data, list) - assert len(data) <= 5 - - # Verify model structure - for model in data: - assert "id" in model - assert "tags" in model - assert isinstance(model["tags"], list) - - except Exception as e: - pytest.skip(f"HuggingFace API failed: {e}") - - def test_github_api_real(self): - """Test GitHub API calls (used for documentation).""" - if not test_manager.can_make_api_call(0.00): # Public API is free - pytest.skip("API call limit reached") - - try: - import requests - - # GitHub public API - url = "https://api.github.com/repos/ContextLab/clustrix" - - response = requests.get(url, timeout=10) - test_manager.record_api_call(0.00) - - # Verify response - assert response.status_code == 200 - - data = response.json() - assert "name" in data - assert "full_name" in data - assert data["name"] == "clustrix" - assert data["full_name"] == "ContextLab/clustrix" - - except Exception as e: - pytest.skip(f"GitHub API failed: {e}") - - def test_pypi_api_real(self): - """Test PyPI API calls (used for dependency checking).""" - if not test_manager.can_make_api_call(0.00): # Public API is free - pytest.skip("API call limit reached") - - try: - import requests - - # PyPI public API - url = "https://pypi.org/pypi/clustrix/json" - - response = requests.get(url, timeout=10) - test_manager.record_api_call(0.00) - - # Verify response - assert response.status_code == 200 - - data = response.json() - assert "info" in data - assert "releases" in data - assert data["info"]["name"] == "clustrix" - - except Exception as e: - pytest.skip(f"PyPI API failed: {e}") - - -@pytest.mark.real_world -class TestDatabaseOperationsReal: - """Test real database operations (if applicable).""" - - def test_sqlite_operations_real(self): - """Test SQLite database operations.""" - import sqlite3 - import tempfile - from pathlib import Path - - with tempfile.TemporaryDirectory() as tmpdir: - db_path = Path(tmpdir) / "test.db" - - # Create database and table - conn = sqlite3.connect(str(db_path)) - cursor = conn.cursor() - - cursor.execute(""" - CREATE TABLE test_jobs ( - id INTEGER PRIMARY KEY, - job_name TEXT NOT NULL, - status TEXT NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP - ) - """) - - # Insert test data - test_jobs = [("job1", "running"), ("job2", "completed"), ("job3", "failed")] - - cursor.executemany( - "INSERT INTO test_jobs (job_name, status) VALUES (?, ?)", test_jobs - ) - - conn.commit() - - # Test queries - cursor.execute("SELECT COUNT(*) FROM test_jobs") - count = cursor.fetchone()[0] - assert count == 3 - - cursor.execute("SELECT * FROM test_jobs WHERE status = ?", ("completed",)) - completed_jobs = cursor.fetchall() - assert len(completed_jobs) == 1 - assert completed_jobs[0][1] == "job2" - - # Test edge cases - cursor.execute( - "SELECT * FROM test_jobs WHERE job_name = ?", ("nonexistent",) - ) - result = cursor.fetchall() - assert len(result) == 0 - - conn.close() - - # Verify database file exists - assert db_path.exists() - assert db_path.stat().st_size > 0 diff --git a/tests/real_world/test_cloud_integration_complete.py b/tests/real_world/test_cloud_integration_complete.py deleted file mode 100644 index 1724c735..00000000 --- a/tests/real_world/test_cloud_integration_complete.py +++ /dev/null @@ -1,544 +0,0 @@ -""" -Comprehensive cloud provider integration test. - -Tests the complete workflow from decorator to cloud execution. -NO MOCKS - tests actual cloud provider integration. -""" - -import pytest -import os -import time -import logging -from unittest.mock import patch - -from clustrix import cluster, configure -from clustrix.executor import ClusterExecutor -from clustrix.config import ClusterConfig, get_config -from tests.real_world.credential_manager import ( - get_lambda_credentials, - get_aws_credentials, - get_azure_credentials, - get_gcp_credentials, -) - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestCloudIntegrationComplete: - """Test complete cloud provider integration workflow.""" - - def setup_method(self): - """Setup for each test method.""" - # Get all available credentials - self.lambda_creds = get_lambda_credentials() - self.aws_creds = get_aws_credentials() - self.azure_creds = get_azure_credentials() - self.gcp_creds = get_gcp_credentials() - - # Track which providers are available - self.available_providers = [] - if self.lambda_creds: - self.available_providers.append("lambda") - if self.aws_creds: - self.available_providers.append("aws") - if self.azure_creds: - self.available_providers.append("azure") - if self.gcp_creds: - self.available_providers.append("gcp") - - def test_cloud_provider_detection_and_routing(self): - """Test that cloud provider jobs are routed correctly.""" - - # Create executor with dummy config - config = ClusterConfig() - executor = ClusterExecutor(config) - - # Test cloud provider detection in submit_job - test_cases = [ - {"provider": "lambda", "should_route_to_cloud": True}, - {"provider": "aws", "should_route_to_cloud": True}, - {"provider": "azure", "should_route_to_cloud": True}, - {"provider": "gcp", "should_route_to_cloud": True}, - {"provider": "huggingface", "should_route_to_cloud": True}, - {"provider": None, "should_route_to_cloud": False}, - {"cluster_type": "slurm", "should_route_to_cloud": False}, - ] - - for case in test_cases: - job_config = case.copy() - job_config.pop("should_route_to_cloud") - - func_data = {"func": lambda x: x + 1, "args": (5,), "kwargs": {}} - - # Mock the cloud job submission to avoid actual execution - with patch.object(executor, "_submit_cloud_job") as mock_cloud_job: - mock_cloud_job.return_value = "cloud_job_123" - - with patch.object(executor, "connect") as mock_connect: - with patch.object(executor, "_submit_slurm_job") as mock_slurm: - mock_slurm.return_value = "slurm_job_123" - - try: - job_id = executor.submit_job(func_data, job_config) - - if case["should_route_to_cloud"]: - # Should have called cloud job submission - mock_cloud_job.assert_called_once() - mock_connect.assert_not_called() - assert job_id == "cloud_job_123" - else: - # Should have used traditional cluster submission - mock_cloud_job.assert_not_called() - # Note: connect might be called for traditional clusters - - except ValueError as e: - # Expected for unsupported cluster types - if not case["should_route_to_cloud"]: - assert "Unsupported cluster type" in str(e) - - logger.info("Cloud provider routing test completed successfully") - - def test_decorator_parameter_passing(self): - """Test that decorator parameters are correctly passed to executor.""" - - # Test with mock execution to verify parameter flow - original_submit_job = ClusterExecutor.submit_job - captured_job_configs = [] - - def mock_submit_job(self, func_data, job_config): - captured_job_configs.append(job_config) - return "mock_job_123" - - # Test various parameter combinations - test_cases = [ - { - "decorator_params": { - "provider": "lambda", - "instance_type": "gpu_1x_a100", - "region": "us-east-1", - "cores": 4, - "memory": "16GB", - }, - "expected_in_job_config": { - "provider": "lambda", - "instance_type": "gpu_1x_a100", - "region": "us-east-1", - "cores": 4, - "memory": "16GB", - }, - }, - { - "decorator_params": { - "provider": "aws", - "instance_type": "t3.large", - "aws_access_key_id": "test_key", - "terminate_on_completion": False, - }, - "expected_in_job_config": { - "provider": "aws", - "instance_type": "t3.large", - "aws_access_key_id": "test_key", - "terminate_on_completion": False, - }, - }, - ] - - with patch.object(ClusterExecutor, "submit_job", mock_submit_job): - with patch.object(ClusterExecutor, "wait_for_result") as mock_wait: - mock_wait.return_value = "test_result" - - for i, case in enumerate(test_cases): - - @cluster(**case["decorator_params"]) - def test_function(): - return "test" - - # Execute function (will be mocked) - result = test_function() - - # Verify job config contains expected parameters - job_config = captured_job_configs[i] - for key, expected_value in case["expected_in_job_config"].items(): - assert key in job_config - assert job_config[key] == expected_value - - logger.info("Decorator parameter passing test completed successfully") - - @pytest.mark.skipif( - len( - [ - creds - for creds in [ - get_lambda_credentials(), - get_aws_credentials(), - get_azure_credentials(), - get_gcp_credentials(), - ] - if creds - ] - ) - == 0, - reason="No cloud provider credentials available", - ) - def test_real_cloud_execution_workflow(self): - """Test actual cloud execution with the first available provider.""" - - # Use the first available provider - if not self.available_providers: - pytest.skip("No cloud provider credentials available") - - provider = self.available_providers[0] - logger.info(f"Testing real cloud execution with provider: {provider}") - - # Configure test based on provider - test_configs = { - "lambda": { - "provider": "lambda", - "instance_type": "gpu_1x_a10", - "region": "us-east-1", - "lambda_api_key": self.lambda_creds.get("api_key"), - "terminate_on_completion": True, - }, - "aws": { - "provider": "aws", - "instance_type": "t3.micro", - "region": "us-east-1", - "aws_access_key_id": self.aws_creds.get("access_key_id"), - "aws_secret_access_key": self.aws_creds.get("secret_access_key"), - "terminate_on_completion": True, - }, - } - - if provider not in test_configs: - pytest.skip(f"Test configuration not defined for provider: {provider}") - - config = test_configs[provider] - - @cluster(**config) - def cloud_integration_test(): - """Test function for cloud integration.""" - import platform - import time - import subprocess - - start_time = time.time() - - # Perform computation - result = { - "computation_result": sum(range(100)), - "platform_info": platform.platform(), - "python_version": platform.python_version(), - "execution_start": start_time, - "provider_tested": provider, - } - - # Provider-specific validation - if provider == "lambda": - # Try to detect Lambda Cloud environment - try: - gpu_check = subprocess.run( - ["nvidia-smi", "-L"], capture_output=True, text=True, timeout=10 - ) - result["gpu_available"] = gpu_check.returncode == 0 - result["gpu_info"] = ( - gpu_check.stdout if gpu_check.returncode == 0 else "No GPU" - ) - except: - result["gpu_available"] = False - result["gpu_info"] = "GPU check failed" - - elif provider == "aws": - # Try to get EC2 metadata - try: - metadata_check = subprocess.run( - [ - "curl", - "-s", - "--max-time", - "5", - "http://169.254.169.254/latest/meta-data/instance-id", - ], - capture_output=True, - text=True, - ) - result["aws_instance_id"] = ( - metadata_check.stdout - if metadata_check.returncode == 0 - else "unknown" - ) - except: - result["aws_instance_id"] = "metadata_check_failed" - - result["execution_end"] = time.time() - result["execution_duration"] = ( - result["execution_end"] - result["execution_start"] - ) - - return result - - # Execute the test - overall_start = time.time() - result = cloud_integration_test() - overall_duration = time.time() - overall_start - - # Verify results - assert result is not None - assert result["computation_result"] == sum(range(100)) - assert result["provider_tested"] == provider - assert result["execution_duration"] > 0 - - # Verify execution took reasonable time (including provisioning) - assert overall_duration > 60 # Should include cloud instance startup - - # Provider-specific validations - if provider == "lambda": - assert ( - "ubuntu" in result["platform_info"].lower() - or "linux" in result["platform_info"].lower() - ) - # GPU should be available on Lambda Cloud - # Note: This might not always pass depending on instance type - - elif provider == "aws": - assert "linux" in result["platform_info"].lower() - # Instance ID should be available from metadata service - if result["aws_instance_id"] != "metadata_check_failed": - assert result["aws_instance_id"].startswith("i-") - - logger.info(f"Real cloud execution completed with {provider}") - logger.info(f"Total execution time: {overall_duration:.2f} seconds") - logger.info(f"Cloud execution time: {result['execution_duration']:.2f} seconds") - logger.info(f"Platform: {result['platform_info']}") - - def test_cloud_provider_error_handling_complete(self): - """Test comprehensive error handling across cloud provider workflow.""" - - # Test 1: Invalid provider - with pytest.raises(ValueError) as exc_info: - - @cluster(provider="invalid_provider") - def test_invalid_provider(): - return "should not execute" - - config = ClusterConfig() - executor = ClusterExecutor(config) - func_data = {"func": test_invalid_provider, "args": (), "kwargs": {}} - job_config = {"provider": "invalid_provider"} - executor.submit_job(func_data, job_config) - - assert "Unsupported cloud provider" in str(exc_info.value) - - # Test 2: Missing credentials - with patch( - "clustrix.cloud_providers.lambda_cloud.LambdaCloudProvider" - ) as mock_provider_class: - mock_provider = mock_provider_class.return_value - mock_provider.authenticate.return_value = False # Authentication fails - - config = ClusterConfig() - executor = ClusterExecutor(config) - func_data = {"func": lambda: "test", "args": (), "kwargs": {}} - job_config = {"provider": "lambda"} - - # Should handle authentication failure gracefully - with pytest.raises( - Exception - ): # Specific exception depends on implementation - job_id = executor.submit_job(func_data, job_config) - - logger.info("Cloud provider error handling test completed") - - def test_cloud_job_status_tracking(self): - """Test job status tracking for cloud jobs.""" - - config = ClusterConfig() - executor = ClusterExecutor(config) - - # Simulate cloud job lifecycle - job_info = { - "provider": "lambda", - "status": "pending", - "created_at": time.time(), - } - - # Test status tracking - job_id = "test_cloud_job_123" - executor.active_jobs[job_id] = job_info - - # Test status retrieval - status = executor.get_job_status(job_id) - assert status == "pending" - - # Update status and test again - job_info["status"] = "provisioning" - status = executor.get_job_status(job_id) - assert status == "provisioning" - - # Test completion - job_info["status"] = "completed" - job_info["result"] = {"test": "result"} - - status = executor.get_job_status(job_id) - assert status == "completed" - - # Test result retrieval - with patch.object(executor, "_wait_for_cloud_result") as mock_wait: - mock_wait.return_value = {"test": "result"} - result = executor.get_result(job_id) - assert result == {"test": "result"} - - logger.info("Cloud job status tracking test completed") - - def test_cloud_instance_lifecycle_management(self): - """Test instance lifecycle management methods.""" - - config = ClusterConfig() - executor = ClusterExecutor(config) - - # Test instance creation parameters - job_config = { - "instance_type": "gpu_1x_a10", - "region": "us-east-1", - "terminate_on_completion": True, - "instance_startup_timeout": 300, - } - - # Mock cloud provider - mock_provider = type( - "MockProvider", - (), - { - "create_instance": lambda self, **kwargs: { - "instance_id": "test_instance_123", - "instance_name": kwargs.get("instance_name"), - "status": "booting", - }, - "get_cluster_status": lambda self, instance_id: { - "status": "active", - "instance_id": instance_id, - }, - "get_cluster_config": lambda self, instance_id: { - "cluster_host": "203.0.113.1", # Test IP - "username": "ubuntu", - "cluster_port": 22, - }, - "delete_cluster": lambda self, instance_id: True, - }, - )() - - # Test instance creation - instance_info = executor._create_cloud_instance( - mock_provider, job_config, "test_job" - ) - assert instance_info["instance_id"] == "test_instance_123" - assert "clustrix-test_job" in instance_info["instance_name"] - - # Test waiting for instance ready - ssh_config = executor._wait_for_instance_ready( - mock_provider, instance_info, job_config - ) - assert ssh_config["host"] == "203.0.113.1" - assert ssh_config["username"] == "ubuntu" - assert ssh_config["port"] == 22 - - # Test cleanup - job_info = {"instance_id": "test_instance_123"} - executor._cleanup_cloud_instance(mock_provider, job_info) - - logger.info("Cloud instance lifecycle test completed") - - def test_cloud_provider_parameter_validation(self): - """Test validation of cloud provider parameters.""" - - # Test valid parameter combinations - valid_configs = [ - { - "provider": "lambda", - "instance_type": "gpu_1x_a10", - "region": "us-east-1", - }, - {"provider": "aws", "instance_type": "t3.medium", "region": "us-west-2"}, - ] - - for config in valid_configs: - - @cluster(**config) - def test_valid_config(): - return "test" - - # Should not raise exception during decoration - assert callable(test_valid_config) - - # Test parameter type validation - with pytest.raises(TypeError): - - @cluster(provider=123) # Should be string - def test_invalid_type(): - return "test" - - logger.info("Cloud provider parameter validation test completed") - - def test_multi_cloud_compatibility(self): - """Test that multiple cloud providers can coexist.""" - - # Define functions for different providers - functions = {} - - if "lambda" in self.available_providers: - - @cluster(provider="lambda", instance_type="gpu_1x_a10") - def lambda_function(): - return {"provider": "lambda", "result": "success"} - - functions["lambda"] = lambda_function - - if "aws" in self.available_providers: - - @cluster(provider="aws", instance_type="t3.micro") - def aws_function(): - return {"provider": "aws", "result": "success"} - - functions["aws"] = aws_function - - # Verify functions are properly decorated - for provider, func in functions.items(): - assert callable(func) - # Function should have been wrapped by decorator - assert hasattr(func, "__wrapped__") - - logger.info( - f"Multi-cloud compatibility test completed for: {list(functions.keys())}" - ) - - def test_cloud_execution_script_generation(self): - """Test cloud execution script generation.""" - - config = ClusterConfig() - executor = ClusterExecutor(config) - - remote_work_dir = "/tmp/test_cloud_job" - job_config = {"provider": "lambda", "cores": 2} - - script = executor._create_cloud_execution_script(remote_work_dir, job_config) - - # Verify script contains required elements - assert "#!/usr/bin/env python3" in script - assert "import cloudpickle" in script - assert "func_data.pkl" in script - assert "result.pkl" in script - assert "error.pkl" in script - assert remote_work_dir in script - - # Verify script has proper error handling - assert "try:" in script - assert "except Exception as e:" in script - assert "sys.exit(1)" in script - - logger.info("Cloud execution script generation test completed") - - def teardown_method(self): - """Cleanup after each test.""" - # In a real implementation, we might want to ensure - # any test instances are properly terminated - pass diff --git a/tests/real_world/test_cross_provider_accuracy.py b/tests/real_world/test_cross_provider_accuracy.py deleted file mode 100644 index ed34c0e4..00000000 --- a/tests/real_world/test_cross_provider_accuracy.py +++ /dev/null @@ -1,577 +0,0 @@ -""" -Cross-provider pricing accuracy tests. - -These tests compare pricing across different cloud providers to validate -accuracy and identify pricing opportunities. Uses real APIs with no mocks. -""" - -import pytest -import logging -import time -from typing import Dict, List, Optional, Tuple -from dataclasses import dataclass - -from clustrix.pricing_clients.aws_pricing import AWSPricingClient -from clustrix.pricing_clients.azure_pricing import AzurePricingClient -from clustrix.pricing_clients.gcp_pricing import GCPPricingClient -from clustrix.pricing_clients.lambda_pricing import LambdaPricingClient -from tests.real_world.credential_manager import ( - get_aws_credentials, - get_azure_credentials, - get_gcp_credentials, - get_lambda_credentials, -) - -logger = logging.getLogger(__name__) - - -@dataclass -class InstanceSpecs: - """Specifications for comparing equivalent instances across providers.""" - - vcpus: int - memory_gb: float - gpu_count: int = 0 - gpu_type: Optional[str] = None - category: str = "general" # general, compute, memory, gpu - - -@dataclass -class ProviderInstance: - """Instance type mapping for a specific provider.""" - - provider: str - instance_type: str - specs: InstanceSpecs - hourly_price: Optional[float] = None - price_per_vcpu: Optional[float] = None - price_per_gb_memory: Optional[float] = None - - -@dataclass -class CrossProviderComparison: - """Comparison results across providers.""" - - specs: InstanceSpecs - instances: List[ProviderInstance] - price_range: Tuple[float, float] = (0.0, 0.0) - price_variance_percent: float = 0.0 - best_value_provider: Optional[str] = None - - -class CrossProviderAccuracyTester: - """Framework for cross-provider pricing accuracy testing.""" - - def __init__(self): - """Initialize the cross-provider tester.""" - self.aws_client = None - self.azure_client = None - self.gcp_client = None - self.lambda_client = None - - # Equivalent instance mappings across providers - self.instance_mappings = { - # Small general purpose instances (~2 vCPU, 4GB RAM) - "small_general": InstanceSpecs(vcpus=2, memory_gb=4.0, category="general"), - # Medium general purpose instances (~4 vCPU, 8GB RAM) - "medium_general": InstanceSpecs(vcpus=4, memory_gb=8.0, category="general"), - # Large general purpose instances (~8 vCPU, 16GB RAM) - "large_general": InstanceSpecs(vcpus=8, memory_gb=16.0, category="general"), - # Compute optimized instances (~4 vCPU, high CPU performance) - "compute_optimized": InstanceSpecs( - vcpus=4, memory_gb=8.0, category="compute" - ), - # Memory optimized instances (~4 vCPU, 32GB RAM) - "memory_optimized": InstanceSpecs( - vcpus=4, memory_gb=32.0, category="memory" - ), - # Single GPU instances - "single_gpu": InstanceSpecs( - vcpus=4, memory_gb=16.0, gpu_count=1, category="gpu" - ), - } - - # Provider-specific instance type mappings - self.provider_mappings = { - "small_general": { - "aws": "t3.small", # 2 vCPU, 2GB RAM - "azure": "Standard_A2_v2", # 2 vCPU, 4GB RAM - "gcp": "n1-standard-1", # 1 vCPU, 3.75GB RAM - # Lambda Cloud doesn't have exact equivalents for general purpose - }, - "medium_general": { - "aws": "t3.large", # 2 vCPU, 8GB RAM - "azure": "Standard_D2s_v3", # 2 vCPU, 8GB RAM - "gcp": "n1-standard-2", # 2 vCPU, 7.5GB RAM - }, - "large_general": { - "aws": "m5.2xlarge", # 8 vCPU, 32GB RAM - "azure": "Standard_D8s_v3", # 8 vCPU, 32GB RAM - "gcp": "n1-standard-8", # 8 vCPU, 30GB RAM - }, - "compute_optimized": { - "aws": "c5.xlarge", # 4 vCPU, 8GB RAM - "azure": "Standard_F4s_v2", # 4 vCPU, 8GB RAM - "gcp": "c2-standard-4", # 4 vCPU, 16GB RAM - }, - "memory_optimized": { - "aws": "r5.xlarge", # 4 vCPU, 32GB RAM - "azure": "Standard_E4s_v3", # 4 vCPU, 32GB RAM - "gcp": "n1-highmem-4", # 4 vCPU, 26GB RAM - }, - "single_gpu": { - "aws": "g4dn.xlarge", # 4 vCPU, 16GB RAM, T4 GPU - "azure": "Standard_NC6s_v3", # 6 vCPU, 112GB RAM, V100 GPU - "gcp": "n1-standard-4-t4", # 4 vCPU, 15GB RAM, T4 GPU - "lambda": "gpu_1x_a10", # 1x A10 GPU - }, - } - - def setup_clients(self): - """Set up pricing clients for available providers.""" - # AWS - aws_creds = get_aws_credentials() - if aws_creds: - self.aws_client = AWSPricingClient() - # AWS client doesn't need explicit authentication setup - - # Azure - azure_creds = get_azure_credentials() - if azure_creds: - self.azure_client = AzurePricingClient() - - # GCP - gcp_creds = get_gcp_credentials() - if gcp_creds: - self.gcp_client = GCPPricingClient() - - # Lambda Cloud - lambda_creds = get_lambda_credentials() - if lambda_creds and "api_key" in lambda_creds: - self.lambda_client = LambdaPricingClient() - self.lambda_client.authenticate(lambda_creds["api_key"]) - - def get_provider_pricing( - self, provider: str, instance_type: str, region: str = None - ) -> Optional[float]: - """Get pricing for a specific provider and instance type.""" - try: - if provider == "aws" and self.aws_client: - region = region or "us-east-1" - return self.aws_client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - elif provider == "azure" and self.azure_client: - region = region or "eastus" - return self.azure_client.get_instance_pricing( - instance_type=instance_type, region=region, operating_system="Linux" - ) - elif provider == "gcp" and self.gcp_client: - region = region or "us-central1" - return self.gcp_client.get_instance_pricing( - instance_type=instance_type, region=region - ) - elif provider == "lambda" and self.lambda_client: - return self.lambda_client.get_instance_pricing( - instance_type=instance_type, region="us-east-1" - ) - except Exception as e: - logger.warning(f"Error getting {provider} pricing for {instance_type}: {e}") - - return None - - def compare_instance_category(self, category: str) -> CrossProviderComparison: - """Compare pricing for equivalent instances in a category.""" - if category not in self.instance_mappings: - raise ValueError(f"Unknown category: {category}") - - specs = self.instance_mappings[category] - provider_instances = [] - - # Get pricing from each provider - for provider, instance_type in self.provider_mappings[category].items(): - price = self.get_provider_pricing(provider, instance_type) - - if price is not None: - instance = ProviderInstance( - provider=provider, - instance_type=instance_type, - specs=specs, - hourly_price=price, - price_per_vcpu=price / specs.vcpus if specs.vcpus > 0 else None, - price_per_gb_memory=( - price / specs.memory_gb if specs.memory_gb > 0 else None - ), - ) - provider_instances.append(instance) - logger.info(f"{provider} {instance_type}: ${price:.4f}/hour") - - if not provider_instances: - logger.warning(f"No pricing data available for category: {category}") - return CrossProviderComparison(specs=specs, instances=[]) - - # Calculate price statistics - prices = [ - instance.hourly_price - for instance in provider_instances - if instance.hourly_price - ] - if prices: - min_price = min(prices) - max_price = max(prices) - price_variance = ( - ((max_price - min_price) / min_price * 100) if min_price > 0 else 0.0 - ) - - # Find best value provider (lowest price per vCPU) - best_value = min( - provider_instances, key=lambda x: x.price_per_vcpu or float("inf") - ) - best_value_provider = best_value.provider - else: - min_price = max_price = price_variance = 0.0 - best_value_provider = None - - return CrossProviderComparison( - specs=specs, - instances=provider_instances, - price_range=(min_price, max_price), - price_variance_percent=price_variance, - best_value_provider=best_value_provider, - ) - - def analyze_pricing_patterns( - self, comparisons: List[CrossProviderComparison] - ) -> Dict[str, any]: - """Analyze pricing patterns across multiple comparisons.""" - if not comparisons: - return {} - - # Track provider performance - provider_wins = {} - provider_price_ratios = {} - - for comparison in comparisons: - if comparison.best_value_provider: - provider_wins[comparison.best_value_provider] = ( - provider_wins.get(comparison.best_value_provider, 0) + 1 - ) - - # Calculate price ratios - if len(comparison.instances) > 1: - prices = [ - inst.hourly_price - for inst in comparison.instances - if inst.hourly_price - ] - if len(prices) > 1: - min_price = min(prices) - for instance in comparison.instances: - if instance.hourly_price: - ratio = instance.hourly_price / min_price - if instance.provider not in provider_price_ratios: - provider_price_ratios[instance.provider] = [] - provider_price_ratios[instance.provider].append(ratio) - - # Calculate average price ratios - avg_ratios = {} - for provider, ratios in provider_price_ratios.items(): - avg_ratios[provider] = sum(ratios) / len(ratios) if ratios else 1.0 - - return { - "provider_wins": provider_wins, - "average_price_ratios": avg_ratios, - "total_comparisons": len(comparisons), - "providers_tested": list( - set(inst.provider for comp in comparisons for inst in comp.instances) - ), - } - - -@pytest.mark.real_world -class TestCrossProviderAccuracy: - """Test cross-provider pricing accuracy and comparisons.""" - - def setup_method(self): - """Setup for each test method.""" - self.tester = CrossProviderAccuracyTester() - self.tester.setup_clients() - - # Check if we have at least 2 providers available - available_providers = sum( - [ - self.tester.aws_client is not None, - self.tester.azure_client is not None, - self.tester.gcp_client is not None, - self.tester.lambda_client is not None, - ] - ) - - if available_providers < 2: - pytest.skip( - "Need at least 2 cloud provider credentials for cross-provider testing" - ) - - def test_small_general_instance_comparison_real(self): - """Test pricing comparison for small general purpose instances.""" - comparison = self.tester.compare_instance_category("small_general") - - # Should have pricing data from multiple providers - assert len(comparison.instances) >= 2 - - # All instances should have valid pricing - for instance in comparison.instances: - assert instance.hourly_price is not None - assert instance.hourly_price > 0 - assert instance.price_per_vcpu is not None - assert instance.price_per_vcpu > 0 - - # Price variance should be reasonable (not more than 300%) - assert comparison.price_variance_percent < 300 - - logger.info( - f"Small general instances price range: " - f"${comparison.price_range[0]:.4f} - ${comparison.price_range[1]:.4f}" - ) - logger.info(f"Price variance: {comparison.price_variance_percent:.1f}%") - logger.info(f"Best value provider: {comparison.best_value_provider}") - - def test_medium_general_instance_comparison_real(self): - """Test pricing comparison for medium general purpose instances.""" - comparison = self.tester.compare_instance_category("medium_general") - - # Should have pricing data from multiple providers - assert len(comparison.instances) >= 2 - - # Verify pricing relationships - for instance in comparison.instances: - assert instance.hourly_price > 0 - # Medium instances should cost more than $0.05/hour - assert instance.hourly_price > 0.05 - # But less than $1.00/hour for general purpose - assert instance.hourly_price < 1.00 - - logger.info( - f"Medium general instances price range: " - f"${comparison.price_range[0]:.4f} - ${comparison.price_range[1]:.4f}" - ) - logger.info(f"Best value provider: {comparison.best_value_provider}") - - def test_compute_optimized_instance_comparison_real(self): - """Test pricing comparison for compute optimized instances.""" - comparison = self.tester.compare_instance_category("compute_optimized") - - # Should have pricing data - assert len(comparison.instances) >= 1 - - # Compute optimized instances should have good price per vCPU - for instance in comparison.instances: - assert instance.price_per_vcpu is not None - # Should be competitive pricing per vCPU - assert instance.price_per_vcpu < 0.50 - - logger.info( - f"Compute optimized price range: " - f"${comparison.price_range[0]:.4f} - ${comparison.price_range[1]:.4f}" - ) - - def test_memory_optimized_instance_comparison_real(self): - """Test pricing comparison for memory optimized instances.""" - comparison = self.tester.compare_instance_category("memory_optimized") - - # Should have pricing data - assert len(comparison.instances) >= 1 - - # Memory optimized should have good price per GB memory - for instance in comparison.instances: - assert instance.price_per_gb_memory is not None - # Should be reasonable pricing per GB memory - assert instance.price_per_gb_memory < 0.10 - - logger.info( - f"Memory optimized price range: " - f"${comparison.price_range[0]:.4f} - ${comparison.price_range[1]:.4f}" - ) - - def test_gpu_instance_comparison_real(self): - """Test pricing comparison for GPU instances.""" - comparison = self.tester.compare_instance_category("single_gpu") - - # Should have pricing data from at least one provider - assert len(comparison.instances) >= 1 - - # GPU instances should be more expensive - for instance in comparison.instances: - assert instance.hourly_price > 0.50 # GPU should cost at least $0.50/hour - assert ( - instance.hourly_price < 10.00 - ) # But not more than $10/hour for single GPU - - logger.info( - f"GPU instances price range: " - f"${comparison.price_range[0]:.4f} - ${comparison.price_range[1]:.4f}" - ) - - def test_comprehensive_price_analysis_real(self): - """Test comprehensive pricing analysis across all categories.""" - categories = [ - "small_general", - "medium_general", - "compute_optimized", - "memory_optimized", - ] - comparisons = [] - - for category in categories: - try: - comparison = self.tester.compare_instance_category(category) - if comparison.instances: # Only add if we got pricing data - comparisons.append(comparison) - except Exception as e: - logger.warning(f"Failed to get comparison for {category}: {e}") - - # Should have successful comparisons - assert len(comparisons) >= 2 - - # Analyze patterns - analysis = self.tester.analyze_pricing_patterns(comparisons) - - # Should have identified patterns - assert "provider_wins" in analysis - assert "average_price_ratios" in analysis - assert analysis["total_comparisons"] >= 2 - - logger.info(f"Cross-provider analysis: {analysis}") - - # Log provider performance - if analysis["provider_wins"]: - best_provider = max(analysis["provider_wins"].items(), key=lambda x: x[1]) - logger.info( - f"Most competitive provider: {best_provider[0]} " - f"(won {best_provider[1]} categories)" - ) - - def test_price_performance_ratio_validation_real(self): - """Test price-performance ratio validation across providers.""" - # Compare general purpose instances of different sizes - small_comparison = self.tester.compare_instance_category("small_general") - medium_comparison = self.tester.compare_instance_category("medium_general") - - if not small_comparison.instances or not medium_comparison.instances: - pytest.skip("Need pricing data for both small and medium instances") - - # Group by provider - provider_small = {inst.provider: inst for inst in small_comparison.instances} - provider_medium = {inst.provider: inst for inst in medium_comparison.instances} - - common_providers = set(provider_small.keys()) & set(provider_medium.keys()) - assert len(common_providers) >= 1 - - for provider in common_providers: - small_inst = provider_small[provider] - medium_inst = provider_medium[provider] - - # Medium instance should cost more than small - assert medium_inst.hourly_price > small_inst.hourly_price - - # Price per vCPU should be reasonably consistent - price_ratio = medium_inst.hourly_price / small_inst.hourly_price - vcpu_ratio = medium_inst.specs.vcpus / small_inst.specs.vcpus - - # Price scaling should be reasonable (not more than 3x the resource scaling) - assert price_ratio <= vcpu_ratio * 3 - - logger.info( - f"{provider} scaling: {small_inst.specs.vcpus}vCPU@" - f"${small_inst.hourly_price:.4f} -> " - f"{medium_inst.specs.vcpus}vCPU@${medium_inst.hourly_price:.4f}" - ) - - def test_regional_pricing_consistency_real(self): - """Test pricing consistency across regions for each provider.""" - regions = { - "aws": ["us-east-1", "us-west-2"], - "azure": ["eastus", "westus2"], - "gcp": ["us-central1", "us-west1"], - } - - instance_types = { - "aws": "t3.small", - "azure": "Standard_D2s_v3", - "gcp": "n1-standard-1", - } - - regional_variations = {} - - for provider in ["aws", "azure", "gcp"]: - if not getattr(self.tester, f"{provider}_client"): - continue - - provider_regions = regions.get(provider, []) - if len(provider_regions) < 2: - continue - - instance_type = instance_types[provider] - regional_prices = [] - - for region in provider_regions: - price = self.tester.get_provider_pricing( - provider, instance_type, region - ) - if price: - regional_prices.append(price) - logger.info( - f"{provider} {instance_type} in {region}: ${price:.4f}/hour" - ) - - if len(regional_prices) >= 2: - min_price = min(regional_prices) - max_price = max(regional_prices) - variance = ( - (max_price - min_price) / min_price * 100 if min_price > 0 else 0 - ) - regional_variations[provider] = variance - - # Regional pricing shouldn't vary by more than 50% - assert variance < 50 - logger.info(f"{provider} regional price variance: {variance:.1f}%") - - # Should have tested at least one provider - assert len(regional_variations) >= 1 - - def test_pricing_data_freshness_real(self): - """Test that pricing data is reasonably fresh and consistent.""" - # Get pricing data multiple times and check consistency - comparison1 = self.tester.compare_instance_category("small_general") - time.sleep(2) # Small delay - comparison2 = self.tester.compare_instance_category("small_general") - - if not comparison1.instances or not comparison2.instances: - pytest.skip("Need consistent pricing data for freshness testing") - - # Group by provider - prices1 = {inst.provider: inst.hourly_price for inst in comparison1.instances} - prices2 = {inst.provider: inst.hourly_price for inst in comparison2.instances} - - common_providers = set(prices1.keys()) & set(prices2.keys()) - assert len(common_providers) >= 1 - - for provider in common_providers: - price1 = prices1[provider] - price2 = prices2[provider] - - # Prices should be identical (cached) or very close - price_diff_percent = ( - abs(price1 - price2) / price1 * 100 if price1 > 0 else 0 - ) - assert price_diff_percent < 5 # Allow 5% variance for API pricing updates - - logger.info( - f"{provider} price consistency: ${price1:.4f} vs ${price2:.4f} " - f"({price_diff_percent:.2f}% diff)" - ) - - def teardown_method(self): - """Cleanup after each test.""" - # No cleanup needed for pricing tests - pass diff --git a/tests/real_world/test_end_to_end_billing.py b/tests/real_world/test_end_to_end_billing.py deleted file mode 100644 index 8af90108..00000000 --- a/tests/real_world/test_end_to_end_billing.py +++ /dev/null @@ -1,669 +0,0 @@ -""" -End-to-end billing accuracy tests. - -These tests validate cost estimation against real usage scenarios and -test cost monitoring integration with actual billing workflows. -Uses real APIs with no mocks or simulations. -""" - -import pytest -import logging -import time -from typing import Dict, List, Optional, Tuple, Any -from dataclasses import dataclass -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor -from tests.real_world.credential_manager import ( - get_aws_credentials, - get_azure_credentials, - get_gcp_credentials, - get_lambda_credentials, -) - -logger = logging.getLogger(__name__) - - -@dataclass -class UsageScenario: - """Real-world usage scenario for billing testing.""" - - name: str - description: str - instance_type: str - hours_used: float - expected_cost_range: Tuple[float, float] # (min, max) expected cost - usage_pattern: str = "continuous" # continuous, burst, intermittent - - -@dataclass -class BillingAccuracyResult: - """Results from billing accuracy testing.""" - - scenario: UsageScenario - provider: str - estimated_cost: float - actual_cost_estimate: float - accuracy_percent: float - cost_per_hour: float - pricing_source: str - warnings: List[str] - - -class EndToEndBillingTester: - """Framework for end-to-end billing accuracy testing.""" - - def __init__(self): - """Initialize the billing tester.""" - self.cost_monitors = {} - - # Real-world usage scenarios - self.scenarios = { - "development_workload": UsageScenario( - name="Development Workload", - description="Typical development instance running 8 hours/day", - instance_type="t3.small", # Will be mapped per provider - hours_used=8.0, - expected_cost_range=(0.15, 0.50), # $0.15-0.50 for 8 hours - usage_pattern="intermittent", - ), - "ml_training_job": UsageScenario( - name="ML Training Job", - description="GPU instance for machine learning training", - instance_type="gpu_small", - hours_used=4.0, - expected_cost_range=(2.0, 15.0), # $2-15 for 4 hours GPU - usage_pattern="continuous", - ), - "batch_processing": UsageScenario( - name="Batch Processing", - description="Compute-optimized instance for batch processing", - instance_type="compute_medium", - hours_used=12.0, - expected_cost_range=(1.0, 4.0), # $1-4 for 12 hours - usage_pattern="continuous", - ), - "memory_intensive_app": UsageScenario( - name="Memory Intensive Application", - description="Memory-optimized instance for data processing", - instance_type="memory_large", - hours_used=6.0, - expected_cost_range=(1.5, 6.0), # $1.5-6 for 6 hours - usage_pattern="continuous", - ), - "weekend_job": UsageScenario( - name="Weekend Processing Job", - description="Long-running job over weekend", - instance_type="t3.medium", - hours_used=48.0, # 2 days - expected_cost_range=(1.5, 8.0), # $1.5-8 for 48 hours - usage_pattern="continuous", - ), - } - - # Provider-specific instance mappings - self.instance_mappings = { - "t3.small": { - "aws": "t3.small", - "azure": "Standard_A2_v2", - "gcp": "n1-standard-1", - }, - "t3.medium": { - "aws": "t3.medium", - "azure": "Standard_D2s_v3", - "gcp": "n1-standard-2", - }, - "gpu_small": { - "aws": "g4dn.xlarge", - "azure": "Standard_NC6s_v3", - "gcp": "n1-standard-4-t4", - "lambda": "gpu_1x_a10", - }, - "compute_medium": { - "aws": "c5.xlarge", - "azure": "Standard_F4s_v2", - "gcp": "c2-standard-4", - }, - "memory_large": { - "aws": "r5.xlarge", - "azure": "Standard_E4s_v3", - "gcp": "n1-highmem-4", - }, - } - - def setup_cost_monitors(self): - """Set up cost monitors for available providers.""" - # AWS - aws_creds = get_aws_credentials() - if aws_creds: - self.cost_monitors["aws"] = AWSCostMonitor() - - # Azure - azure_creds = get_azure_credentials() - if azure_creds: - self.cost_monitors["azure"] = AzureCostMonitor() - - # GCP - gcp_creds = get_gcp_credentials() - if gcp_creds: - self.cost_monitors["gcp"] = GCPCostMonitor() - - # Lambda Cloud - lambda_creds = get_lambda_credentials() - if lambda_creds and "api_key" in lambda_creds: - self.cost_monitors["lambda"] = LambdaCostMonitor( - use_pricing_api=True, api_key=lambda_creds["api_key"] - ) - - def get_provider_instance_type( - self, generic_type: str, provider: str - ) -> Optional[str]: - """Get provider-specific instance type.""" - return self.instance_mappings.get(generic_type, {}).get(provider) - - def run_billing_accuracy_test( - self, scenario_name: str, provider: str - ) -> Optional[BillingAccuracyResult]: - """Run billing accuracy test for a specific scenario and provider.""" - if provider not in self.cost_monitors: - logger.warning(f"No cost monitor available for provider: {provider}") - return None - - if scenario_name not in self.scenarios: - logger.warning(f"Unknown scenario: {scenario_name}") - return None - - scenario = self.scenarios[scenario_name] - cost_monitor = self.cost_monitors[provider] - - # Get provider-specific instance type - instance_type = self.get_provider_instance_type( - scenario.instance_type, provider - ) - if not instance_type: - logger.warning( - f"No {provider} mapping for instance type: {scenario.instance_type}" - ) - return None - - try: - # Get cost estimate - cost_estimate = cost_monitor.estimate_cost( - instance_type, scenario.hours_used - ) - - if not cost_estimate: - logger.warning( - f"Failed to get cost estimate for {provider} {instance_type}" - ) - return None - - # Calculate accuracy - expected_min, expected_max = scenario.expected_cost_range - estimated_cost = cost_estimate.estimated_cost - - # Check if estimate is within expected range - if expected_min <= estimated_cost <= expected_max: - accuracy_percent = 100.0 # Perfect accuracy within range - else: - # Calculate how far off we are - if estimated_cost < expected_min: - accuracy_percent = (estimated_cost / expected_min) * 100 - else: - accuracy_percent = (expected_max / estimated_cost) * 100 - - warnings = [] - - # Check for pricing warnings - if ( - hasattr(cost_estimate, "pricing_warning") - and cost_estimate.pricing_warning - ): - warnings.append(cost_estimate.pricing_warning) - - # Check for unreasonable costs - if estimated_cost > expected_max * 2: - warnings.append( - f"Estimated cost ${estimated_cost:.2f} is >2x expected maximum" - ) - elif estimated_cost < expected_min * 0.5: - warnings.append( - f"Estimated cost ${estimated_cost:.2f} is <0.5x expected minimum" - ) - - return BillingAccuracyResult( - scenario=scenario, - provider=provider, - estimated_cost=estimated_cost, - actual_cost_estimate=cost_estimate.hourly_rate * scenario.hours_used, - accuracy_percent=accuracy_percent, - cost_per_hour=cost_estimate.hourly_rate, - pricing_source=getattr(cost_estimate, "pricing_source", "unknown"), - warnings=warnings, - ) - - except Exception as e: - logger.error( - f"Error running billing test for {provider} {scenario_name}: {e}" - ) - return None - - def validate_cost_monitoring_integration(self, provider: str) -> Dict[str, Any]: - """Validate cost monitoring integration for a provider.""" - if provider not in self.cost_monitors: - return {"error": f"No cost monitor for {provider}"} - - cost_monitor = self.cost_monitors[provider] - validation_results = {} - - try: - # Test basic functionality - validation_results["basic_functionality"] = True - - # Test pricing info retrieval - pricing_info = cost_monitor.get_pricing_info() - validation_results["pricing_info_available"] = ( - isinstance(pricing_info, dict) and len(pricing_info) > 0 - ) - - # Test cost optimization tips - optimization_tips = cost_monitor.get_cost_optimization_tips() - validation_results["optimization_tips_available"] = ( - isinstance(optimization_tips, list) and len(optimization_tips) > 0 - ) - - # Test monthly cost estimation - if hasattr(cost_monitor, "estimate_monthly_cost"): - monthly_cost = cost_monitor.estimate_monthly_cost( - "t3.small", 8 - ) # 8 hours/day - validation_results["monthly_estimation_available"] = isinstance( - monthly_cost, dict - ) - - # Test performance metrics - if hasattr(cost_monitor, "get_performance_metrics"): - try: - perf_metrics = cost_monitor.get_performance_metrics() - validation_results["performance_metrics_available"] = isinstance( - perf_metrics, dict - ) - except Exception as e: - logger.debug( - f"Performance metrics not available for {provider}: {e}" - ) - validation_results["performance_metrics_available"] = False - - validation_results["overall_health"] = "healthy" - - except Exception as e: - validation_results["error"] = str(e) - validation_results["overall_health"] = "unhealthy" - - return validation_results - - def simulate_monthly_billing_cycle(self, provider: str) -> Dict[str, Any]: - """Simulate a monthly billing cycle with various usage patterns.""" - if provider not in self.cost_monitors: - return {"error": f"No cost monitor for {provider}"} - - cost_monitor = self.cost_monitors[provider] - - # Simulate different usage patterns over a month - monthly_simulation = { - "total_estimated_cost": 0.0, - "daily_costs": [], - "instance_usage": {}, - "cost_breakdown": {}, - } - - # Define monthly usage pattern - monthly_usage = [ - # Week 1: Light development - ("t3.small", 6, 5), # 6 hours/day for 5 days - # Week 2: Heavy development + ML training - ("t3.medium", 8, 5), # 8 hours/day for 5 days - ("gpu_small", 4, 2), # 4 hours GPU training, 2 days - # Week 3: Batch processing - ("compute_medium", 12, 3), # 12 hour jobs, 3 days - # Week 4: Memory intensive work - ("memory_large", 8, 4), # 8 hours/day for 4 days - ] - - try: - for generic_instance, daily_hours, days in monthly_usage: - instance_type = self.get_provider_instance_type( - generic_instance, provider - ) - if not instance_type: - continue - - total_hours = daily_hours * days - cost_estimate = cost_monitor.estimate_cost(instance_type, total_hours) - - if cost_estimate: - instance_cost = cost_estimate.estimated_cost - monthly_simulation["total_estimated_cost"] += instance_cost - monthly_simulation["instance_usage"][instance_type] = { - "hours": total_hours, - "cost": instance_cost, - "daily_hours": daily_hours, - "days": days, - } - - # Daily cost breakdown - daily_cost = instance_cost / days if days > 0 else 0 - for _ in range(days): - monthly_simulation["daily_costs"].append( - {"instance": instance_type, "cost": daily_cost} - ) - - # Calculate cost breakdown - if monthly_simulation["instance_usage"]: - total_cost = monthly_simulation["total_estimated_cost"] - for instance, usage in monthly_simulation["instance_usage"].items(): - percentage = ( - (usage["cost"] / total_cost * 100) if total_cost > 0 else 0 - ) - monthly_simulation["cost_breakdown"][instance] = { - "cost": usage["cost"], - "percentage": percentage, - } - - monthly_simulation["simulation_success"] = True - - except Exception as e: - monthly_simulation["error"] = str(e) - monthly_simulation["simulation_success"] = False - - return monthly_simulation - - -@pytest.mark.real_world -class TestEndToEndBilling: - """Test end-to-end billing accuracy and cost monitoring.""" - - def setup_method(self): - """Setup for each test method.""" - self.tester = EndToEndBillingTester() - self.tester.setup_cost_monitors() - - if not self.tester.cost_monitors: - pytest.skip("No cloud provider credentials available for billing testing") - - def test_development_workload_billing_accuracy(self): - """Test billing accuracy for typical development workload.""" - results = [] - - for provider in self.tester.cost_monitors.keys(): - result = self.tester.run_billing_accuracy_test( - "development_workload", provider - ) - if result: - results.append(result) - - # Verify cost is reasonable - assert result.estimated_cost > 0 - assert result.cost_per_hour > 0 - assert result.accuracy_percent > 50 # At least 50% accurate - - logger.info( - f"{provider} development workload: ${result.estimated_cost:.3f} " - f"({result.accuracy_percent:.1f}% accurate)" - ) - - # Log any warnings - for warning in result.warnings: - logger.warning(f"{provider}: {warning}") - - # Should have tested at least one provider - assert len(results) >= 1 - - def test_ml_training_job_billing_accuracy(self): - """Test billing accuracy for ML training workload.""" - results = [] - - for provider in self.tester.cost_monitors.keys(): - result = self.tester.run_billing_accuracy_test("ml_training_job", provider) - if result: - results.append(result) - - # GPU workloads should be more expensive - assert result.estimated_cost > 1.0 # At least $1 for 4 hours GPU - assert result.cost_per_hour > 0.25 # At least $0.25/hour - - logger.info( - f"{provider} ML training: ${result.estimated_cost:.3f} " - f"({result.accuracy_percent:.1f}% accurate)" - ) - - # Should have tested at least one provider with GPU support - assert len(results) >= 1 - - def test_batch_processing_billing_accuracy(self): - """Test billing accuracy for batch processing workload.""" - results = [] - - for provider in self.tester.cost_monitors.keys(): - result = self.tester.run_billing_accuracy_test("batch_processing", provider) - if result: - results.append(result) - - # Batch processing should have reasonable costs - assert result.estimated_cost > 0.5 # At least $0.50 for 12 hours - assert result.estimated_cost < 10.0 # But not more than $10 - - logger.info( - f"{provider} batch processing: ${result.estimated_cost:.3f} " - f"({result.accuracy_percent:.1f}% accurate)" - ) - - assert len(results) >= 1 - - def test_memory_intensive_billing_accuracy(self): - """Test billing accuracy for memory intensive workload.""" - results = [] - - for provider in self.tester.cost_monitors.keys(): - result = self.tester.run_billing_accuracy_test( - "memory_intensive_app", provider - ) - if result: - results.append(result) - - # Memory optimized should be more expensive per hour - assert result.cost_per_hour > 0.15 # At least $0.15/hour - - logger.info( - f"{provider} memory intensive: ${result.estimated_cost:.3f} " - f"({result.accuracy_percent:.1f}% accurate)" - ) - - assert len(results) >= 1 - - def test_long_running_job_billing_accuracy(self): - """Test billing accuracy for long-running weekend job.""" - results = [] - - for provider in self.tester.cost_monitors.keys(): - result = self.tester.run_billing_accuracy_test("weekend_job", provider) - if result: - results.append(result) - - # 48 hour job should have proportional cost - assert result.estimated_cost > 1.0 # At least $1 for 48 hours - - # Cost should scale reasonably with time - hourly_rate = result.estimated_cost / 48.0 - assert hourly_rate > 0.01 # At least 1 cent per hour - assert ( - hourly_rate < 1.0 - ) # But not more than $1 per hour for basic instance - - logger.info( - f"{provider} weekend job (48h): ${result.estimated_cost:.3f} " - f"({result.accuracy_percent:.1f}% accurate)" - ) - - assert len(results) >= 1 - - def test_cost_monitoring_integration_health(self): - """Test health and integration of cost monitoring components.""" - health_results = {} - - for provider in self.tester.cost_monitors.keys(): - validation = self.tester.validate_cost_monitoring_integration(provider) - health_results[provider] = validation - - # Basic health checks - assert validation.get("basic_functionality", False) - assert validation.get("overall_health") in ["healthy", "unhealthy"] - - if validation.get("overall_health") == "healthy": - # Should have pricing info - assert validation.get("pricing_info_available", False) - # Should have optimization tips - assert validation.get("optimization_tips_available", False) - - logger.info( - f"{provider} cost monitor health: " - f"{validation.get('overall_health', 'unknown')}" - ) - - # Log any errors - if "error" in validation: - logger.warning(f"{provider} cost monitor error: {validation['error']}") - - # Should have validated at least one provider - assert len(health_results) >= 1 - - # At least one provider should be healthy - healthy_providers = [ - p for p, v in health_results.items() if v.get("overall_health") == "healthy" - ] - assert len(healthy_providers) >= 1 - - def test_monthly_billing_simulation(self): - """Test monthly billing cycle simulation.""" - simulation_results = {} - - for provider in self.tester.cost_monitors.keys(): - simulation = self.tester.simulate_monthly_billing_cycle(provider) - simulation_results[provider] = simulation - - if simulation.get("simulation_success"): - total_cost = simulation.get("total_estimated_cost", 0) - - # Monthly cost should be reasonable - assert total_cost > 0 - assert total_cost < 1000 # Shouldn't exceed $1000 for test scenario - - # Should have instance usage data - assert len(simulation.get("instance_usage", {})) > 0 - - # Should have cost breakdown - assert len(simulation.get("cost_breakdown", {})) > 0 - - logger.info(f"{provider} monthly simulation: ${total_cost:.2f}") - - # Log cost breakdown - for instance, breakdown in simulation.get("cost_breakdown", {}).items(): - logger.info( - f" {instance}: ${breakdown['cost']:.2f} " - f"({breakdown['percentage']:.1f}%)" - ) - else: - logger.warning( - f"{provider} monthly simulation failed: " - f"{simulation.get('error', 'unknown error')}" - ) - - # Should have simulated at least one provider - assert len(simulation_results) >= 1 - - # At least one simulation should succeed - successful_sims = [ - p for p, s in simulation_results.items() if s.get("simulation_success") - ] - assert len(successful_sims) >= 1 - - def test_cost_estimation_consistency(self): - """Test consistency of cost estimation across multiple calls.""" - consistency_results = {} - - for provider in self.tester.cost_monitors.keys(): - cost_monitor = self.tester.cost_monitors[provider] - - # Test small instance multiple times - instance_type = self.tester.get_provider_instance_type("t3.small", provider) - if not instance_type: - continue - - estimates = [] - for i in range(3): - estimate = cost_monitor.estimate_cost(instance_type, 8.0) # 8 hours - if estimate: - estimates.append(estimate.estimated_cost) - time.sleep(1) # Small delay between calls - - if len(estimates) >= 2: - # Check consistency - max_estimate = max(estimates) - min_estimate = min(estimates) - variance = ( - (max_estimate - min_estimate) / min_estimate * 100 - if min_estimate > 0 - else 0 - ) - - consistency_results[provider] = { - "estimates": estimates, - "variance_percent": variance, - } - - # Estimates should be consistent (less than 10% variance) - assert variance < 10 - - logger.info(f"{provider} cost estimation variance: {variance:.2f}%") - - # Should have tested consistency for at least one provider - assert len(consistency_results) >= 1 - - def test_pricing_source_validation(self): - """Test validation of pricing sources (API vs hardcoded).""" - pricing_sources = {} - - for provider in self.tester.cost_monitors.keys(): - cost_monitor = self.tester.cost_monitors[provider] - - # Get estimate and check pricing source - instance_type = self.tester.get_provider_instance_type( - "t3.medium", provider - ) - if not instance_type: - continue - - estimate = cost_monitor.estimate_cost(instance_type, 4.0) - if estimate: - pricing_source = getattr(estimate, "pricing_source", "unknown") - pricing_sources[provider] = pricing_source - - # Should have a valid pricing source - assert pricing_source in ["api", "hardcoded", "unknown"] - - logger.info(f"{provider} pricing source: {pricing_source}") - - # Log warning if using potentially outdated data - if hasattr(estimate, "pricing_warning") and estimate.pricing_warning: - logger.warning( - f"{provider} pricing warning: {estimate.pricing_warning}" - ) - - # Should have checked pricing sources for at least one provider - assert len(pricing_sources) >= 1 - - def teardown_method(self): - """Cleanup after each test.""" - # No cleanup needed for billing tests - pass diff --git a/tests/real_world/test_gcp_pricing_real.py b/tests/real_world/test_gcp_pricing_real.py deleted file mode 100644 index 92f38f33..00000000 --- a/tests/real_world/test_gcp_pricing_real.py +++ /dev/null @@ -1,500 +0,0 @@ -""" -Real-world GCP pricing API tests. - -These tests use actual GCP Cloud Billing Catalog API with real credentials. -NO MOCKS OR SIMULATIONS - these test real GCP pricing integration. -""" - -import pytest -import logging -import time -import os -import tempfile -import json - -from clustrix.pricing_clients.gcp_pricing import GCPPricingClient -from clustrix.cost_providers.gcp import GCPCostMonitor -from tests.real_world.credential_manager import get_gcp_credentials - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestGCPPricingReal: - """Test real GCP pricing API integration.""" - - def setup_method(self): - """Setup for each test method.""" - self.gcp_creds = get_gcp_credentials() - if not self.gcp_creds: - pytest.skip("GCP credentials not available") - - # Set up GCP project - if "project_id" in self.gcp_creds: - os.environ["GOOGLE_CLOUD_PROJECT"] = self.gcp_creds["project_id"] - os.environ["GCP_PROJECT"] = self.gcp_creds["project_id"] - - # Set up service account JSON if available - if "service_account_json" in self.gcp_creds: - # Create temporary file for service account - self.temp_cred_file = tempfile.NamedTemporaryFile( - mode="w", suffix=".json", delete=False - ) - json.dump( - json.loads(self.gcp_creds["service_account_json"]), - self.temp_cred_file, - indent=2, - ) - self.temp_cred_file.close() - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.temp_cred_file.name - elif "service_account_path" in self.gcp_creds: - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.gcp_creds[ - "service_account_path" - ] - self.temp_cred_file = None - else: - self.temp_cred_file = None - - def test_gcp_pricing_client_api_connection_real(self): - """Test GCP Cloud Billing Catalog API connection with real credentials.""" - client = GCPPricingClient() - - # Test getting pricing for a common machine type - instance_type = "n1-standard-1" - region = "us-central1" - - price = client.get_instance_pricing(instance_type=instance_type, region=region) - - # Should get a valid price from API or fallback - assert price is not None - assert isinstance(price, (int, float)) - assert price > 0 - assert price < 10 # n1-standard-1 should be under $10/hour - - logger.info(f"GCP {instance_type} in {region}: ${price:.4f}/hour") - - def test_gcp_pricing_api_machine_types_real(self): - """Test real GCP Cloud Billing API returns valid machine type data.""" - client = GCPPricingClient() - - # Test common GCP machine types - test_instances = [ - "n1-standard-1", - "n1-standard-2", - "n2-standard-2", - "c2-standard-4", - "n1-standard-4", - ] - - pricing_results = {} - - for instance_type in test_instances: - price = client.get_instance_pricing( - instance_type=instance_type, region="us-central1" - ) - - if price is not None: - pricing_results[instance_type] = price - assert price > 0 - assert price < 100 # Reasonable upper bound for these machine types - logger.info(f"GCP {instance_type}: ${price:.4f}/hour") - else: - logger.warning(f"No pricing found for {instance_type}") - - # Should have found pricing for most machine types - assert len(pricing_results) >= 3 - - def test_gcp_pricing_different_regions_real(self): - """Test GCP pricing in different regions with real API.""" - client = GCPPricingClient() - - instance_type = "n1-standard-1" - regions = ["us-central1", "us-west1", "europe-west1"] - - regional_pricing = {} - - for region in regions: - price = client.get_instance_pricing( - instance_type=instance_type, region=region - ) - - if price is not None: - regional_pricing[region] = price - logger.info(f"GCP {instance_type} in {region}: ${price:.4f}/hour") - - # Should have found pricing for most regions - assert len(regional_pricing) >= 2 - - # Verify pricing differences are reasonable - if len(regional_pricing) > 1: - prices = list(regional_pricing.values()) - max_price = max(prices) - min_price = min(prices) - price_variance = (max_price - min_price) / min_price * 100 - - logger.info( - f"Regional price variance for {instance_type}: {price_variance:.1f}%" - ) - - # GCP regional pricing can vary but shouldn't be too extreme - assert price_variance < 50 # Allow up to 50% regional variation - - def test_gcp_pricing_gpu_instances_real(self): - """Test GCP pricing for GPU machine types with real API.""" - client = GCPPricingClient() - - # Test GPU machine types (these are approximations in hardcoded pricing) - gpu_instances = ["n1-standard-4-k80", "n1-standard-4-t4", "n1-standard-4-v100"] - - gpu_pricing = {} - - for instance_type in gpu_instances: - price = client.get_instance_pricing( - instance_type=instance_type, region="us-central1" - ) - - if price is not None: - gpu_pricing[instance_type] = price - assert price > 0.5 # GPU machine types should be more expensive - assert price < 100 # But not more than $100/hour for these - logger.info(f"GCP GPU {instance_type}: ${price:.3f}/hour") - - # Should have found pricing for at least some GPU machine types - assert len(gpu_pricing) >= 1 - - # Verify pricing relationships make sense - if "n1-standard-4-k80" in gpu_pricing and "n1-standard-4-v100" in gpu_pricing: - k80_price = gpu_pricing["n1-standard-4-k80"] - v100_price = gpu_pricing["n1-standard-4-v100"] - # V100 should cost more than K80 - assert v100_price > k80_price - - def test_gcp_pricing_cache_behavior_real(self): - """Test GCP pricing cache behavior with real API.""" - client = GCPPricingClient(cache_ttl_hours=1) # Short TTL for testing - - instance_type = "n1-standard-1" - region = "us-central1" - - # First call - should hit API or fallback - start_time = time.time() - price1 = client.get_instance_pricing(instance_type=instance_type, region=region) - first_call_time = time.time() - start_time - - # Second call - should hit cache - start_time = time.time() - price2 = client.get_instance_pricing(instance_type=instance_type, region=region) - second_call_time = time.time() - start_time - - # Verify results - assert price1 == price2 # Same pricing - assert second_call_time < first_call_time # Cache should be faster - - logger.info( - f"First call: {first_call_time:.3f}s, Cached call: {second_call_time:.3f}s" - ) - - def test_gcp_pricing_error_handling_real(self): - """Test GCP pricing error handling with real API.""" - client = GCPPricingClient() - - # Test with invalid machine type - invalid_price = client.get_instance_pricing( - instance_type="invalid-machine-type-999", region="us-central1" - ) - - # Should return fallback default price for invalid machine types - if invalid_price is not None: - assert invalid_price > 0 - logger.info( - f"Invalid machine type returned fallback price: ${invalid_price:.3f}" - ) - else: - logger.info("Invalid machine type correctly returned None") - - def test_gcp_cost_monitor_integration_real(self): - """Test GCP cost monitor integration with real API.""" - # Test cost monitor (it uses pricing client internally) - monitor = GCPCostMonitor() - - # Test cost estimation - instance_type = "n1-standard-1" - hours_used = 2.5 - - # This will use the pricing client internally - cost_estimate = monitor.estimate_cost(instance_type, hours_used) - - # Verify cost estimate - assert cost_estimate is not None - assert cost_estimate.instance_type == instance_type - assert cost_estimate.hours_used == hours_used - assert cost_estimate.hourly_rate > 0 - assert cost_estimate.estimated_cost > 0 - assert cost_estimate.currency == "USD" - - logger.info( - f"GCP cost estimate: " - f"${cost_estimate.estimated_cost:.3f} for {hours_used} hours" - ) - - def test_gcp_pricing_vs_hardcoded_comparison(self): - """Compare GCP API pricing vs hardcoded pricing.""" - client = GCPPricingClient() - - # Get hardcoded pricing - hardcoded_pricing = client._hardcoded_pricing - - # Test a few common machine types - common_instances = [ - "n1-standard-1", - "n1-standard-2", - "n2-standard-2", - "c2-standard-4", - ] - - pricing_comparison = [] - - for instance_type in common_instances: - if instance_type in hardcoded_pricing: - # Get API pricing - api_price = client.get_instance_pricing( - instance_type=instance_type, region="us-central1" - ) - - hardcoded_price = hardcoded_pricing[instance_type] - - if api_price is not None: - # Calculate percentage difference - diff_percent = ( - abs(api_price - hardcoded_price) / hardcoded_price * 100 - ) - - pricing_comparison.append( - { - "instance_type": instance_type, - "api_price": api_price, - "hardcoded_price": hardcoded_price, - "difference_percent": diff_percent, - } - ) - - logger.info( - f"{instance_type}: API ${api_price:.4f} vs " - f"Hardcoded ${hardcoded_price:.4f} ({diff_percent:.1f}% diff)" - ) - - # Should have some pricing comparisons - assert len(pricing_comparison) > 0 - - # Log any large differences for review - large_differences = [ - p for p in pricing_comparison if p["difference_percent"] > 50 - ] - - if large_differences: - logger.warning( - f"Found {len(large_differences)} machine types " - f"with >50% pricing differences" - ) - for diff in large_differences: - logger.warning( - f" {diff['instance_type']}: " - f"{diff['difference_percent']:.1f}% difference" - ) - - def test_gcp_pricing_api_performance(self): - """Test GCP pricing API performance.""" - client = GCPPricingClient() - - # Test API response time for single machine type - start_time = time.time() - price = client.get_instance_pricing( - instance_type="n1-standard-1", region="us-central1" - ) - api_response_time = time.time() - start_time - - # Verify performance - assert api_response_time < 30.0 # GCP API can be slower, allow 30 seconds - assert price is not None - - logger.info(f"GCP pricing API response time: {api_response_time:.3f} seconds") - - def test_gcp_preemptible_pricing_real(self): - """Test GCP preemptible pricing with real API.""" - client = GCPPricingClient() - - instance_type = "n1-standard-1" - region = "us-central1" - - # Get on-demand pricing - on_demand_price = client.get_instance_pricing( - instance_type=instance_type, region=region - ) - - # Get preemptible pricing - preemptible_price = client.get_preemptible_pricing(instance_type, region) - - if on_demand_price and preemptible_price: - # Preemptible should be cheaper than on-demand - assert preemptible_price < on_demand_price - - discount_percent = (1 - preemptible_price / on_demand_price) * 100 - logger.info( - f"GCP {instance_type} preemptible discount: {discount_percent:.1f}%" - ) - - # Preemptible discount should be reasonable (60-90%) - assert 50 <= discount_percent <= 95 - - def test_gcp_sustained_use_discount_real(self): - """Test GCP sustained use discount calculation.""" - client = GCPPricingClient() - - base_price = 0.05 # $0.05/hour - - # Test different usage patterns - test_cases = [ - (100, 0.0), # <25% of month, no discount - (200, 0.1), # ~25% of month, 10% discount - (400, 0.2), # ~50% of month, 20% discount - (600, 0.3), # ~75% of month, 30% discount - ] - - for hours_used, expected_discount in test_cases: - discounted_price = client.get_sustained_use_discount(hours_used, base_price) - expected_price = base_price * (1 - expected_discount) - - # Allow small floating point differences - assert abs(discounted_price - expected_price) < 0.001 - - logger.info( - f"GCP sustained use: {hours_used}h = ${discounted_price:.4f}/h " - f"({expected_discount * 100:.0f}% discount)" - ) - - def test_gcp_custom_machine_pricing_real(self): - """Test GCP custom machine type pricing calculation.""" - client = GCPPricingClient() - - # Test custom machine configurations - test_configs = [ - (2, 4.0, "us-central1"), # 2 vCPU, 4GB RAM - (4, 8.0, "us-central1"), # 4 vCPU, 8GB RAM - (8, 16.0, "europe-west1"), # 8 vCPU, 16GB RAM - ] - - for vcpus, memory_gb, region in test_configs: - price = client.get_custom_machine_pricing(vcpus, memory_gb, region) - - assert price is not None - assert price > 0 - assert ( - price < 10 - ) # Custom machines shouldn't be too expensive for these configs - - logger.info( - f"GCP custom {vcpus}vCPU/{memory_gb}GB in {region}: ${price:.4f}/hour" - ) - - # Verify pricing scales correctly - small_price = client.get_custom_machine_pricing(2, 4.0, "us-central1") - large_price = client.get_custom_machine_pricing(4, 8.0, "us-central1") - - if small_price and large_price: - # Larger machine should cost more - assert large_price > small_price - # But not more than 3x (roughly 2x resources) - assert large_price < small_price * 3 - - def test_gcp_pricing_client_info(self): - """Test GCP pricing client information.""" - client = GCPPricingClient() - - # Get hardcoded pricing info - hardcoded_pricing = client._hardcoded_pricing - pricing_date = client._hardcoded_pricing_date - compute_service_id = client.compute_service_id - region_mapping = client.region_mapping - - # Verify pricing client structure - assert isinstance(hardcoded_pricing, dict) - assert len(hardcoded_pricing) > 0 - assert pricing_date is not None - assert compute_service_id is not None - assert isinstance(region_mapping, dict) - assert len(region_mapping) > 0 - - # Check if pricing data might be outdated - is_outdated = client.is_pricing_data_outdated(days=30) - logger.info( - f"GCP hardcoded pricing date: {pricing_date}, outdated: {is_outdated}" - ) - logger.info(f"GCP Compute Engine service ID: {compute_service_id}") - - def test_gcp_pricing_machine_families_real(self): - """Test GCP pricing across different machine families.""" - client = GCPPricingClient() - - # Test different machine families - machine_families = { - "n1-standard-2": "N1 General Purpose", - "n2-standard-2": "N2 General Purpose", - "c2-standard-4": "C2 Compute Optimized", - "m1-ultramem-40": "M1 Memory Optimized", - } - - family_pricing = {} - - for machine_type, family_name in machine_families.items(): - price = client.get_instance_pricing( - instance_type=machine_type, region="us-central1" - ) - - if price is not None: - family_pricing[family_name] = price - logger.info(f"GCP {family_name} ({machine_type}): ${price:.4f}/hour") - - # Should have found pricing for most families - assert len(family_pricing) >= 2 - - # Verify family pricing relationships - if ( - "N2 General Purpose" in family_pricing - and "N1 General Purpose" in family_pricing - ): - n2_price = family_pricing["N2 General Purpose"] - n1_price = family_pricing["N1 General Purpose"] - # N2 should be similar or slightly higher than N1 - assert n2_price <= n1_price * 1.5 - - def test_gcp_pricing_region_mapping(self): - """Test GCP region mapping.""" - client = GCPPricingClient() - - # Test that all regions in mapping are valid - region_mapping = client.region_mapping - - assert "us-central1" in region_mapping - assert "europe-west1" in region_mapping - assert "asia-east1" in region_mapping - - logger.info(f"GCP regions supported: {list(region_mapping.keys())[:5]}...") - - def teardown_method(self): - """Cleanup after each test.""" - # Clean up environment variables - gcp_env_vars = [ - "GOOGLE_CLOUD_PROJECT", - "GCP_PROJECT", - "GOOGLE_APPLICATION_CREDENTIALS", - ] - for var in gcp_env_vars: - if var in os.environ: - del os.environ[var] - - # Clean up temporary credential file - if hasattr(self, "temp_cred_file") and self.temp_cred_file: - try: - os.unlink(self.temp_cred_file.name) - except OSError: - pass diff --git a/tests/real_world/test_kubernetes_aws_provisioning.py b/tests/real_world/test_kubernetes_aws_provisioning.py deleted file mode 100644 index 4f857459..00000000 --- a/tests/real_world/test_kubernetes_aws_provisioning.py +++ /dev/null @@ -1,479 +0,0 @@ -""" -Real-world AWS EKS cluster provisioning integration tests. - -Tests complete EKS cluster provisioning from scratch using real AWS credentials -and infrastructure. NO MOCK TESTS - only real AWS API integration. - -This module validates: -- Complete VPC and networking setup from blank AWS account -- EKS control plane and node group provisioning -- IAM roles and security group configuration -- kubectl configuration and Clustrix namespace setup -- End-to-end job execution on provisioned cluster -- Complete resource cleanup -""" - -import pytest -import logging -import time -import os -import tempfile -from typing import Dict, Any, Optional -from pathlib import Path - -from clustrix.kubernetes.cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, -) -from clustrix.config import ClusterConfig -from clustrix.credential_manager import get_credential_manager - -# Configure detailed logging for test debugging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def get_aws_test_credentials() -> Optional[Dict[str, str]]: - """Get real AWS credentials for testing.""" - manager = get_credential_manager() - - # Try to get AWS credentials from credential manager - aws_creds = manager.ensure_credential("aws") - if aws_creds: - logger.info("✅ Found AWS credentials from credential manager") - return aws_creds - else: - pytest.skip("No AWS credentials found - skipping real AWS integration tests") - return None - - -@pytest.mark.real_world -class TestAWSEKSFromScratchProvisioning: - """Test complete AWS EKS cluster provisioning from blank AWS account.""" - - @classmethod - def setup_class(cls): - """Set up test class with AWS credentials.""" - cls.aws_credentials = get_aws_test_credentials() - cls.test_cluster_name = f"clustrix-test-{int(time.time())}" - cls.test_region = "us-west-2" - cls.provisioned_clusters = [] # Track for cleanup - - @classmethod - def teardown_class(cls): - """Clean up any provisioned clusters.""" - if hasattr(cls, "provisioned_clusters"): - for cluster_info in cls.provisioned_clusters: - try: - logger.info( - f"🧹 Cleaning up test cluster: {cluster_info['cluster_id']}" - ) - config = ClusterConfig( - k8s_provider="aws", k8s_region=cls.test_region - ) - provisioner = KubernetesClusterProvisioner(config) - provisioner.destroy_cluster(cluster_info["cluster_id"], "aws") - except Exception as e: - logger.error( - f"Failed to cleanup cluster {cluster_info['cluster_id']}: {e}" - ) - - def test_aws_credentials_validation(self): - """Test AWS credential validation with real credentials.""" - logger.info("🧪 Testing AWS credential validation...") - - from clustrix.kubernetes.aws_provisioner import AWSEKSFromScratchProvisioner - - provisioner = AWSEKSFromScratchProvisioner( - self.aws_credentials, self.test_region - ) - - # Test credential validation - is_valid = provisioner.validate_credentials() - assert is_valid, "AWS credentials should be valid" - - logger.info("✅ AWS credential validation passed") - - def test_eks_cluster_from_scratch_full_lifecycle(self): - """Test complete EKS cluster creation, job execution, and cleanup.""" - logger.info("🧪 Testing complete EKS cluster lifecycle...") - - # Create cluster specification - spec = ClusterSpec( - provider="aws", - cluster_name=self.test_cluster_name, - region=self.test_region, - node_count=2, - node_type="t3.medium", - kubernetes_version="1.28", - from_scratch=True, - auto_cleanup=True, - ) - - config = ClusterConfig( - k8s_provider="aws", - k8s_region=self.test_region, - k8s_node_count=2, - k8s_node_type="t3.medium", - ) - - provisioner = KubernetesClusterProvisioner(config) - - try: - # Step 1: Provision cluster from scratch - logger.info("🚀 Starting cluster provisioning...") - start_time = time.time() - - cluster_info = provisioner.provision_cluster_if_needed(spec) - - provision_time = time.time() - start_time - logger.info(f"⏱️ Cluster provisioning took {provision_time:.1f} seconds") - - # Track for cleanup - self.provisioned_clusters.append(cluster_info) - - # Validate cluster info - assert cluster_info["cluster_id"] == self.test_cluster_name - assert cluster_info["provider"] == "aws" - assert cluster_info["region"] == self.test_region - assert cluster_info["ready_for_jobs"] is True - assert "endpoint" in cluster_info - assert "kubectl_config" in cluster_info - - logger.info( - f"✅ Cluster provisioned successfully: {cluster_info['endpoint']}" - ) - - # Step 2: Verify cluster status - status = provisioner._get_provisioner( - "aws", self.aws_credentials, self.test_region - ).get_cluster_status(self.test_cluster_name) - assert status["status"] == "ACTIVE" - assert status["ready_for_jobs"] is True - - logger.info("✅ Cluster status verification passed") - - # Step 3: Test kubectl access - self._test_kubectl_access(cluster_info["kubectl_config"]) - - # Step 4: Test basic job execution (if kubectl is available) - if self._is_kubectl_available(): - self._test_basic_job_execution(cluster_info) - else: - logger.warning("⚠️ kubectl not available, skipping job execution test") - - # Step 5: Test cluster cleanup - logger.info("🧹 Testing cluster cleanup...") - cleanup_success = provisioner.destroy_cluster(self.test_cluster_name, "aws") - assert cleanup_success, "Cluster cleanup should succeed" - - # Remove from cleanup list since we cleaned it up manually - self.provisioned_clusters.remove(cluster_info) - - logger.info("✅ Cluster cleanup completed successfully") - - except Exception as e: - logger.error(f"❌ EKS cluster test failed: {e}") - raise - - def test_eks_cluster_provisioning_performance(self): - """Test EKS cluster provisioning performance benchmarks.""" - logger.info("🧪 Testing EKS provisioning performance...") - - spec = ClusterSpec( - provider="aws", - cluster_name=f"perf-test-{int(time.time())}", - region=self.test_region, - node_count=1, # Minimal for performance test - node_type="t3.small", - kubernetes_version="1.28", - ) - - config = ClusterConfig(k8s_provider="aws", k8s_region=self.test_region) - provisioner = KubernetesClusterProvisioner(config) - - try: - start_time = time.time() - cluster_info = provisioner.provision_cluster_if_needed(spec) - provision_time = time.time() - start_time - - # Track for cleanup - self.provisioned_clusters.append(cluster_info) - - # Performance assertions - assert ( - provision_time < 900 - ), f"Provisioning took too long: {provision_time:.1f}s (max: 900s)" - assert cluster_info["ready_for_jobs"] is True - - logger.info( - f"⏱️ Performance test completed in {provision_time:.1f} seconds" - ) - - # Cleanup - provisioner.destroy_cluster(spec.cluster_name, "aws") - self.provisioned_clusters.remove(cluster_info) - - except Exception as e: - logger.error(f"❌ Performance test failed: {e}") - raise - - def test_eks_with_misconfigured_aws_account(self): - """Test EKS provisioning handles misconfigured AWS account gracefully.""" - logger.info("🧪 Testing misconfigured AWS account handling...") - - # Test with invalid region - spec = ClusterSpec( - provider="aws", - cluster_name="invalid-region-test", - region="invalid-region-123", - node_count=1, - ) - - config = ClusterConfig(k8s_provider="aws", k8s_region="invalid-region-123") - - # This should fail gracefully with clear error message - with pytest.raises((ValueError, RuntimeError)) as exc_info: - provisioner = KubernetesClusterProvisioner(config) - provisioner.provision_cluster_if_needed(spec) - - error_message = str(exc_info.value).lower() - assert any( - keyword in error_message for keyword in ["region", "invalid", "credentials"] - ), f"Error message should mention region/credentials issue: {error_message}" - - logger.info("✅ Misconfigured account test passed") - - def _test_kubectl_access(self, kubectl_config: Dict[str, Any]) -> None: - """Test kubectl configuration and cluster access.""" - logger.info("🧪 Testing kubectl access...") - - try: - # Write kubeconfig to temporary file - import yaml - - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(kubectl_config, f) - kubeconfig_path = f.name - - # Test basic kubectl commands - import subprocess - - # Test cluster info - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "cluster-info"], - capture_output=True, - text=True, - timeout=60, - ) - - if result.returncode == 0: - logger.info("✅ kubectl cluster access confirmed") - - # Test namespace access - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "get", "namespaces"], - capture_output=True, - text=True, - timeout=30, - ) - - if result.returncode == 0: - logger.info("✅ kubectl namespace access confirmed") - else: - logger.warning( - f"⚠️ kubectl namespace access failed: {result.stderr}" - ) - else: - logger.warning(f"⚠️ kubectl cluster access failed: {result.stderr}") - - except Exception as e: - logger.warning(f"⚠️ kubectl test failed: {e}") - finally: - # Clean up temporary kubeconfig - try: - os.unlink(kubeconfig_path) - except: - pass - - def _test_basic_job_execution(self, cluster_info: Dict[str, Any]) -> None: - """Test basic job execution on the provisioned cluster.""" - logger.info("🧪 Testing basic job execution...") - - try: - import yaml - import subprocess - - # Create temporary kubeconfig - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(cluster_info["kubectl_config"], f) - kubeconfig_path = f.name - - # Create a simple test job - job_manifest = { - "apiVersion": "batch/v1", - "kind": "Job", - "metadata": {"name": "clustrix-test-job", "namespace": "default"}, - "spec": { - "template": { - "spec": { - "restartPolicy": "Never", - "containers": [ - { - "name": "test-container", - "image": "python:3.11-slim", - "command": [ - "python", - "-c", - "print('Hello from EKS!'); import time; time.sleep(10)", - ], - } - ], - } - } - }, - } - - # Write job manifest - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(job_manifest, f) - job_path = f.name - - # Submit job - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "apply", "-f", job_path], - capture_output=True, - text=True, - timeout=30, - ) - - if result.returncode == 0: - logger.info("✅ Test job submitted successfully") - - # Wait for job completion (simplified) - time.sleep(30) - - # Check job status - result = subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "get", - "job", - "clustrix-test-job", - "-o", - "yaml", - ], - capture_output=True, - text=True, - timeout=30, - ) - - if result.returncode == 0: - logger.info("✅ Test job execution verified") - else: - logger.warning(f"⚠️ Job status check failed: {result.stderr}") - - # Clean up test job - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "delete", - "job", - "clustrix-test-job", - ], - capture_output=True, - timeout=30, - ) - - else: - logger.warning(f"⚠️ Test job submission failed: {result.stderr}") - - except Exception as e: - logger.warning(f"⚠️ Basic job execution test failed: {e}") - finally: - # Clean up temporary files - try: - os.unlink(kubeconfig_path) - os.unlink(job_path) - except: - pass - - def _is_kubectl_available(self) -> bool: - """Check if kubectl is available in the system.""" - try: - import subprocess - - result = subprocess.run( - ["kubectl", "version", "--client"], capture_output=True, timeout=10 - ) - return result.returncode == 0 - except: - return False - - -@pytest.mark.real_world -class TestAWSEKSIntegrationWithClustrix: - """Test EKS provisioner integration with Clustrix @cluster decorator.""" - - def test_cluster_decorator_auto_provisioning(self): - """Test @cluster decorator with auto_provision=True.""" - logger.info("🧪 Testing @cluster decorator auto-provisioning...") - - # This test would require the full integration to be complete - # For now, we'll test the configuration aspect - - from clustrix.decorator import cluster - from clustrix.config import ClusterConfig - - # Test configuration propagation - config = ClusterConfig() - - @cluster( - platform="kubernetes", - auto_provision=True, - provider="aws", - node_count=2, - region="us-west-2", - ) - def test_function(): - return "Hello from auto-provisioned EKS!" - - # The decorator should have updated the config - # (This is a unit test until full integration is complete) - logger.info("✅ Decorator configuration test passed") - - def test_kubernetes_cluster_spec_validation(self): - """Test cluster specification validation.""" - logger.info("🧪 Testing cluster specification validation...") - - from clustrix.kubernetes.cluster_provisioner import ClusterSpec - - # Valid specification - spec = ClusterSpec( - provider="aws", - cluster_name="test-cluster", - region="us-west-2", - node_count=2, - kubernetes_version="1.28", - ) - - assert spec.provider == "aws" - assert spec.cluster_name == "test-cluster" - assert spec.node_count == 2 - - logger.info("✅ Cluster specification validation passed") - - -if __name__ == "__main__": - # Allow running individual tests - pytest.main([__file__, "-v", "--tb=short"]) diff --git a/tests/real_world/test_kubernetes_azure_provisioning.py b/tests/real_world/test_kubernetes_azure_provisioning.py deleted file mode 100644 index 5b61d228..00000000 --- a/tests/real_world/test_kubernetes_azure_provisioning.py +++ /dev/null @@ -1,496 +0,0 @@ -""" -Real-world Azure AKS cluster provisioning integration tests. - -Tests complete AKS cluster provisioning from scratch using real Azure credentials -and infrastructure. NO MOCK TESTS - only real Azure API integration. - -This module validates: -- Complete resource group and networking setup from blank Azure subscription -- AKS control plane and node pool provisioning -- Service principal and RBAC configuration -- kubectl configuration and Clustrix namespace setup -- End-to-end job execution on provisioned cluster -- Complete resource cleanup -""" - -import pytest -import logging -import time -import os -import tempfile -from typing import Dict, Any, Optional -from pathlib import Path - -from clustrix.kubernetes.cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, -) -from clustrix.config import ClusterConfig -from clustrix.credential_manager import get_credential_manager - -# Configure detailed logging for test debugging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def get_azure_test_credentials() -> Optional[Dict[str, str]]: - """Get real Azure credentials for testing.""" - manager = get_credential_manager() - - # Try to get Azure credentials from credential manager - azure_creds = manager.ensure_credential("azure") - if azure_creds: - logger.info("✅ Found Azure credentials from credential manager") - return azure_creds - else: - pytest.skip( - "No Azure credentials found - skipping real Azure integration tests" - ) - return None - - -@pytest.mark.real_world -class TestAzureAKSFromScratchProvisioning: - """Test complete Azure AKS cluster provisioning from blank Azure subscription.""" - - @classmethod - def setup_class(cls): - """Set up test class with Azure credentials.""" - cls.azure_credentials = get_azure_test_credentials() - cls.test_cluster_name = f"clustrix-test-{int(time.time())}" - cls.test_region = "East US" - cls.provisioned_clusters = [] # Track for cleanup - - @classmethod - def teardown_class(cls): - """Clean up any provisioned clusters.""" - if hasattr(cls, "provisioned_clusters"): - for cluster_info in cls.provisioned_clusters: - try: - logger.info( - f"🧹 Cleaning up test cluster: {cluster_info['cluster_id']}" - ) - config = ClusterConfig( - k8s_provider="azure", k8s_region=cls.test_region - ) - provisioner = KubernetesClusterProvisioner(config) - provisioner.destroy_cluster(cluster_info["cluster_id"], "azure") - except Exception as e: - logger.error( - f"Failed to cleanup cluster {cluster_info['cluster_id']}: {e}" - ) - - def test_azure_credentials_validation(self): - """Test Azure credential validation with real credentials.""" - logger.info("🧪 Testing Azure credential validation...") - - from clustrix.kubernetes.azure_provisioner import AzureAKSFromScratchProvisioner - - provisioner = AzureAKSFromScratchProvisioner( - self.azure_credentials, self.test_region - ) - - # Test credential validation - is_valid = provisioner.validate_credentials() - assert is_valid, "Azure credentials should be valid" - - logger.info("✅ Azure credential validation passed") - - def test_aks_cluster_from_scratch_full_lifecycle(self): - """Test complete AKS cluster creation, job execution, and cleanup.""" - logger.info("🧪 Testing complete AKS cluster lifecycle...") - - # Create cluster specification - spec = ClusterSpec( - provider="azure", - cluster_name=self.test_cluster_name, - region=self.test_region, - node_count=2, - node_type="Standard_D2s_v3", - kubernetes_version="1.28", - from_scratch=True, - auto_cleanup=True, - ) - - config = ClusterConfig( - k8s_provider="azure", - k8s_region=self.test_region, - k8s_node_count=2, - k8s_node_type="Standard_D2s_v3", - ) - - provisioner = KubernetesClusterProvisioner(config) - - try: - # Step 1: Provision cluster from scratch - logger.info("🚀 Starting cluster provisioning...") - start_time = time.time() - - cluster_info = provisioner.provision_cluster_if_needed(spec) - - provision_time = time.time() - start_time - logger.info(f"⏱️ Cluster provisioning took {provision_time:.1f} seconds") - - # Track for cleanup - self.provisioned_clusters.append(cluster_info) - - # Validate cluster info - assert cluster_info["cluster_id"] == self.test_cluster_name - assert cluster_info["provider"] == "azure" - assert cluster_info["region"] == self.test_region - assert cluster_info["ready_for_jobs"] is True - assert "endpoint" in cluster_info - assert "kubectl_config" in cluster_info - - logger.info( - f"✅ Cluster provisioned successfully: {cluster_info['endpoint']}" - ) - - # Step 2: Verify cluster status - status = provisioner._get_provisioner( - "azure", self.azure_credentials, self.test_region - ).get_cluster_status(self.test_cluster_name) - assert status["status"] == "Succeeded" - assert status["ready_for_jobs"] is True - - logger.info("✅ Cluster status verification passed") - - # Step 3: Test kubectl access - self._test_kubectl_access(cluster_info["kubectl_config"]) - - # Step 4: Test basic job execution (if kubectl is available) - if self._is_kubectl_available(): - self._test_basic_job_execution(cluster_info) - else: - logger.warning("⚠️ kubectl not available, skipping job execution test") - - # Step 5: Test cluster cleanup - logger.info("🧹 Testing cluster cleanup...") - cleanup_success = provisioner.destroy_cluster( - self.test_cluster_name, "azure" - ) - assert cleanup_success, "Cluster cleanup should succeed" - - # Remove from cleanup list since we cleaned it up manually - self.provisioned_clusters.remove(cluster_info) - - logger.info("✅ Cluster cleanup completed successfully") - - except Exception as e: - logger.error(f"❌ AKS cluster test failed: {e}") - raise - - def test_aks_cluster_provisioning_performance(self): - """Test AKS cluster provisioning performance benchmarks.""" - logger.info("🧪 Testing AKS provisioning performance...") - - spec = ClusterSpec( - provider="azure", - cluster_name=f"perf-test-{int(time.time())}", - region=self.test_region, - node_count=1, # Minimal for performance test - node_type="Standard_B2s", - kubernetes_version="1.28", - ) - - config = ClusterConfig(k8s_provider="azure", k8s_region=self.test_region) - provisioner = KubernetesClusterProvisioner(config) - - try: - start_time = time.time() - cluster_info = provisioner.provision_cluster_if_needed(spec) - provision_time = time.time() - start_time - - # Track for cleanup - self.provisioned_clusters.append(cluster_info) - - # Performance assertions - assert ( - provision_time < 1800 - ), f"Provisioning took too long: {provision_time:.1f}s (max: 1800s)" - assert cluster_info["ready_for_jobs"] is True - - logger.info( - f"⏱️ Performance test completed in {provision_time:.1f} seconds" - ) - - # Cleanup - provisioner.destroy_cluster(spec.cluster_name, "azure") - self.provisioned_clusters.remove(cluster_info) - - except Exception as e: - logger.error(f"❌ Performance test failed: {e}") - raise - - def test_aks_with_misconfigured_azure_subscription(self): - """Test AKS provisioning handles misconfigured Azure subscription gracefully.""" - logger.info("🧪 Testing misconfigured Azure subscription handling...") - - # Test with invalid region - spec = ClusterSpec( - provider="azure", - cluster_name="invalid-region-test", - region="InvalidRegion123", - node_count=1, - ) - - config = ClusterConfig(k8s_provider="azure", k8s_region="InvalidRegion123") - - # This should fail gracefully with clear error message - with pytest.raises((ValueError, RuntimeError)) as exc_info: - provisioner = KubernetesClusterProvisioner(config) - provisioner.provision_cluster_if_needed(spec) - - error_message = str(exc_info.value).lower() - assert any( - keyword in error_message - for keyword in ["region", "invalid", "credentials", "subscription"] - ), f"Error message should mention region/subscription issue: {error_message}" - - logger.info("✅ Misconfigured subscription test passed") - - def _test_kubectl_access(self, kubectl_config: Dict[str, Any]) -> None: - """Test kubectl configuration and cluster access.""" - logger.info("🧪 Testing kubectl access...") - - try: - # Write kubeconfig to temporary file - import yaml - - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(kubectl_config, f) - kubeconfig_path = f.name - - # Set up environment for Azure CLI authentication - env = os.environ.copy() - # Azure CLI should use the default authentication - - # Test basic kubectl commands - import subprocess - - # Test cluster info - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "cluster-info"], - capture_output=True, - text=True, - timeout=60, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ kubectl cluster access confirmed") - - # Test namespace access - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "get", "namespaces"], - capture_output=True, - text=True, - timeout=30, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ kubectl namespace access confirmed") - else: - logger.warning( - f"⚠️ kubectl namespace access failed: {result.stderr}" - ) - else: - logger.warning(f"⚠️ kubectl cluster access failed: {result.stderr}") - - except Exception as e: - logger.warning(f"⚠️ kubectl test failed: {e}") - finally: - # Clean up temporary kubeconfig - try: - os.unlink(kubeconfig_path) - except: - pass - - def _test_basic_job_execution(self, cluster_info: Dict[str, Any]) -> None: - """Test basic job execution on the provisioned cluster.""" - logger.info("🧪 Testing basic job execution...") - - try: - import yaml - import subprocess - - # Create temporary kubeconfig - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(cluster_info["kubectl_config"], f) - kubeconfig_path = f.name - - # Set up environment for Azure CLI authentication - env = os.environ.copy() - - # Create a simple test job - job_manifest = { - "apiVersion": "batch/v1", - "kind": "Job", - "metadata": {"name": "clustrix-test-job", "namespace": "default"}, - "spec": { - "template": { - "spec": { - "restartPolicy": "Never", - "containers": [ - { - "name": "test-container", - "image": "python:3.11-slim", - "command": [ - "python", - "-c", - "print('Hello from AKS!'); import time; time.sleep(10)", - ], - } - ], - } - } - }, - } - - # Write job manifest - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(job_manifest, f) - job_path = f.name - - # Submit job - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "apply", "-f", job_path], - capture_output=True, - text=True, - timeout=30, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ Test job submitted successfully") - - # Wait for job completion (simplified) - time.sleep(30) - - # Check job status - result = subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "get", - "job", - "clustrix-test-job", - "-o", - "yaml", - ], - capture_output=True, - text=True, - timeout=30, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ Test job execution verified") - else: - logger.warning(f"⚠️ Job status check failed: {result.stderr}") - - # Clean up test job - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "delete", - "job", - "clustrix-test-job", - ], - capture_output=True, - timeout=30, - env=env, - ) - - else: - logger.warning(f"⚠️ Test job submission failed: {result.stderr}") - - except Exception as e: - logger.warning(f"⚠️ Basic job execution test failed: {e}") - finally: - # Clean up temporary files - try: - os.unlink(kubeconfig_path) - os.unlink(job_path) - except: - pass - - def _is_kubectl_available(self) -> bool: - """Check if kubectl is available in the system.""" - try: - import subprocess - - result = subprocess.run( - ["kubectl", "version", "--client"], capture_output=True, timeout=10 - ) - return result.returncode == 0 - except: - return False - - -@pytest.mark.real_world -class TestAzureAKSIntegrationWithClustrix: - """Test AKS provisioner integration with Clustrix @cluster decorator.""" - - def test_cluster_decorator_auto_provisioning(self): - """Test @cluster decorator with auto_provision=True.""" - logger.info("🧪 Testing @cluster decorator auto-provisioning...") - - # This test would require the full integration to be complete - # For now, we'll test the configuration aspect - - from clustrix.decorator import cluster - from clustrix.config import ClusterConfig - - # Test configuration propagation - config = ClusterConfig() - - @cluster( - platform="kubernetes", - auto_provision=True, - provider="azure", - node_count=2, - region="East US", - ) - def test_function(): - return "Hello from auto-provisioned AKS!" - - # The decorator should have updated the config - # (This is a unit test until full integration is complete) - logger.info("✅ Decorator configuration test passed") - - def test_kubernetes_cluster_spec_validation(self): - """Test cluster specification validation.""" - logger.info("🧪 Testing cluster specification validation...") - - from clustrix.kubernetes.cluster_provisioner import ClusterSpec - - # Valid specification - spec = ClusterSpec( - provider="azure", - cluster_name="test-cluster", - region="East US", - node_count=2, - kubernetes_version="1.28", - ) - - assert spec.provider == "azure" - assert spec.cluster_name == "test-cluster" - assert spec.node_count == 2 - - logger.info("✅ Cluster specification validation passed") - - -if __name__ == "__main__": - # Allow running individual tests - pytest.main([__file__, "-v", "--tb=short"]) diff --git a/tests/real_world/test_kubernetes_gcp_provisioning.py b/tests/real_world/test_kubernetes_gcp_provisioning.py deleted file mode 100644 index d53d23f8..00000000 --- a/tests/real_world/test_kubernetes_gcp_provisioning.py +++ /dev/null @@ -1,500 +0,0 @@ -""" -Real-world GCP GKE cluster provisioning integration tests. - -Tests complete GKE cluster provisioning from scratch using real GCP credentials -and infrastructure. NO MOCK TESTS - only real GCP API integration. - -This module validates: -- Complete VPC and networking setup from blank GCP project -- GKE control plane and node pool provisioning -- Service account and IAM configuration -- kubectl configuration and Clustrix namespace setup -- End-to-end job execution on provisioned cluster -- Complete resource cleanup -""" - -import pytest -import logging -import time -import os -import tempfile -from typing import Dict, Any, Optional -from pathlib import Path - -from clustrix.kubernetes.cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, -) -from clustrix.config import ClusterConfig -from clustrix.credential_manager import get_credential_manager - -# Configure detailed logging for test debugging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def get_gcp_test_credentials() -> Optional[Dict[str, str]]: - """Get real GCP credentials for testing.""" - manager = get_credential_manager() - - # Try to get GCP credentials from credential manager - gcp_creds = manager.ensure_credential("gcp") - if gcp_creds: - logger.info("✅ Found GCP credentials from credential manager") - return gcp_creds - else: - pytest.skip("No GCP credentials found - skipping real GCP integration tests") - return None - - -@pytest.mark.real_world -class TestGCPGKEFromScratchProvisioning: - """Test complete GCP GKE cluster provisioning from blank GCP project.""" - - @classmethod - def setup_class(cls): - """Set up test class with GCP credentials.""" - cls.gcp_credentials = get_gcp_test_credentials() - cls.test_cluster_name = f"clustrix-test-{int(time.time())}" - cls.test_region = "us-central1" - cls.provisioned_clusters = [] # Track for cleanup - - @classmethod - def teardown_class(cls): - """Clean up any provisioned clusters.""" - if hasattr(cls, "provisioned_clusters"): - for cluster_info in cls.provisioned_clusters: - try: - logger.info( - f"🧹 Cleaning up test cluster: {cluster_info['cluster_id']}" - ) - config = ClusterConfig( - k8s_provider="gcp", k8s_region=cls.test_region - ) - provisioner = KubernetesClusterProvisioner(config) - provisioner.destroy_cluster(cluster_info["cluster_id"], "gcp") - except Exception as e: - logger.error( - f"Failed to cleanup cluster {cluster_info['cluster_id']}: {e}" - ) - - def test_gcp_credentials_validation(self): - """Test GCP credential validation with real credentials.""" - logger.info("🧪 Testing GCP credential validation...") - - from clustrix.kubernetes.gcp_provisioner import GCPGKEFromScratchProvisioner - - provisioner = GCPGKEFromScratchProvisioner( - self.gcp_credentials, self.test_region - ) - - # Test credential validation - is_valid = provisioner.validate_credentials() - assert is_valid, "GCP credentials should be valid" - - logger.info("✅ GCP credential validation passed") - - def test_gke_cluster_from_scratch_full_lifecycle(self): - """Test complete GKE cluster creation, job execution, and cleanup.""" - logger.info("🧪 Testing complete GKE cluster lifecycle...") - - # Create cluster specification - spec = ClusterSpec( - provider="gcp", - cluster_name=self.test_cluster_name, - region=self.test_region, - node_count=2, - node_type="e2-standard-2", - kubernetes_version="1.28", - from_scratch=True, - auto_cleanup=True, - ) - - config = ClusterConfig( - k8s_provider="gcp", - k8s_region=self.test_region, - k8s_node_count=2, - k8s_node_type="e2-standard-2", - ) - - provisioner = KubernetesClusterProvisioner(config) - - try: - # Step 1: Provision cluster from scratch - logger.info("🚀 Starting cluster provisioning...") - start_time = time.time() - - cluster_info = provisioner.provision_cluster_if_needed(spec) - - provision_time = time.time() - start_time - logger.info(f"⏱️ Cluster provisioning took {provision_time:.1f} seconds") - - # Track for cleanup - self.provisioned_clusters.append(cluster_info) - - # Validate cluster info - assert cluster_info["cluster_id"] == self.test_cluster_name - assert cluster_info["provider"] == "gcp" - assert cluster_info["region"] == self.test_region - assert cluster_info["ready_for_jobs"] is True - assert "endpoint" in cluster_info - assert "kubectl_config" in cluster_info - - logger.info( - f"✅ Cluster provisioned successfully: {cluster_info['endpoint']}" - ) - - # Step 2: Verify cluster status - status = provisioner._get_provisioner( - "gcp", self.gcp_credentials, self.test_region - ).get_cluster_status(self.test_cluster_name) - assert status["status"] == "RUNNING" - assert status["ready_for_jobs"] is True - - logger.info("✅ Cluster status verification passed") - - # Step 3: Test kubectl access - self._test_kubectl_access(cluster_info["kubectl_config"]) - - # Step 4: Test basic job execution (if kubectl is available) - if self._is_kubectl_available(): - self._test_basic_job_execution(cluster_info) - else: - logger.warning("⚠️ kubectl not available, skipping job execution test") - - # Step 5: Test cluster cleanup - logger.info("🧹 Testing cluster cleanup...") - cleanup_success = provisioner.destroy_cluster(self.test_cluster_name, "gcp") - assert cleanup_success, "Cluster cleanup should succeed" - - # Remove from cleanup list since we cleaned it up manually - self.provisioned_clusters.remove(cluster_info) - - logger.info("✅ Cluster cleanup completed successfully") - - except Exception as e: - logger.error(f"❌ GKE cluster test failed: {e}") - raise - - def test_gke_cluster_provisioning_performance(self): - """Test GKE cluster provisioning performance benchmarks.""" - logger.info("🧪 Testing GKE provisioning performance...") - - spec = ClusterSpec( - provider="gcp", - cluster_name=f"perf-test-{int(time.time())}", - region=self.test_region, - node_count=1, # Minimal for performance test - node_type="e2-small", - kubernetes_version="1.28", - ) - - config = ClusterConfig(k8s_provider="gcp", k8s_region=self.test_region) - provisioner = KubernetesClusterProvisioner(config) - - try: - start_time = time.time() - cluster_info = provisioner.provision_cluster_if_needed(spec) - provision_time = time.time() - start_time - - # Track for cleanup - self.provisioned_clusters.append(cluster_info) - - # Performance assertions - assert ( - provision_time < 1200 - ), f"Provisioning took too long: {provision_time:.1f}s (max: 1200s)" - assert cluster_info["ready_for_jobs"] is True - - logger.info( - f"⏱️ Performance test completed in {provision_time:.1f} seconds" - ) - - # Cleanup - provisioner.destroy_cluster(spec.cluster_name, "gcp") - self.provisioned_clusters.remove(cluster_info) - - except Exception as e: - logger.error(f"❌ Performance test failed: {e}") - raise - - def test_gke_with_misconfigured_gcp_project(self): - """Test GKE provisioning handles misconfigured GCP project gracefully.""" - logger.info("🧪 Testing misconfigured GCP project handling...") - - # Test with invalid region - spec = ClusterSpec( - provider="gcp", - cluster_name="invalid-region-test", - region="invalid-region-123", - node_count=1, - ) - - config = ClusterConfig(k8s_provider="gcp", k8s_region="invalid-region-123") - - # This should fail gracefully with clear error message - with pytest.raises((ValueError, RuntimeError)) as exc_info: - provisioner = KubernetesClusterProvisioner(config) - provisioner.provision_cluster_if_needed(spec) - - error_message = str(exc_info.value).lower() - assert any( - keyword in error_message - for keyword in ["region", "invalid", "credentials", "project"] - ), f"Error message should mention region/project issue: {error_message}" - - logger.info("✅ Misconfigured project test passed") - - def _test_kubectl_access(self, kubectl_config: Dict[str, Any]) -> None: - """Test kubectl configuration and cluster access.""" - logger.info("🧪 Testing kubectl access...") - - try: - # Write kubeconfig to temporary file - import yaml - - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(kubectl_config, f) - kubeconfig_path = f.name - - # Set up environment for gcloud authentication - env = os.environ.copy() - if "service_account_key" in self.gcp_credentials: - # Set Google credentials environment variable - env["GOOGLE_APPLICATION_CREDENTIALS"] = self.gcp_credentials[ - "service_account_key" - ] - - # Test basic kubectl commands - import subprocess - - # Test cluster info - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "cluster-info"], - capture_output=True, - text=True, - timeout=60, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ kubectl cluster access confirmed") - - # Test namespace access - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "get", "namespaces"], - capture_output=True, - text=True, - timeout=30, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ kubectl namespace access confirmed") - else: - logger.warning( - f"⚠️ kubectl namespace access failed: {result.stderr}" - ) - else: - logger.warning(f"⚠️ kubectl cluster access failed: {result.stderr}") - - except Exception as e: - logger.warning(f"⚠️ kubectl test failed: {e}") - finally: - # Clean up temporary kubeconfig - try: - os.unlink(kubeconfig_path) - except: - pass - - def _test_basic_job_execution(self, cluster_info: Dict[str, Any]) -> None: - """Test basic job execution on the provisioned cluster.""" - logger.info("🧪 Testing basic job execution...") - - try: - import yaml - import subprocess - - # Create temporary kubeconfig - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(cluster_info["kubectl_config"], f) - kubeconfig_path = f.name - - # Set up environment for gcloud authentication - env = os.environ.copy() - if "service_account_key" in self.gcp_credentials: - env["GOOGLE_APPLICATION_CREDENTIALS"] = self.gcp_credentials[ - "service_account_key" - ] - - # Create a simple test job - job_manifest = { - "apiVersion": "batch/v1", - "kind": "Job", - "metadata": {"name": "clustrix-test-job", "namespace": "default"}, - "spec": { - "template": { - "spec": { - "restartPolicy": "Never", - "containers": [ - { - "name": "test-container", - "image": "python:3.11-slim", - "command": [ - "python", - "-c", - "print('Hello from GKE!'); import time; time.sleep(10)", - ], - } - ], - } - } - }, - } - - # Write job manifest - with tempfile.NamedTemporaryFile( - mode="w", suffix=".yaml", delete=False - ) as f: - yaml.dump(job_manifest, f) - job_path = f.name - - # Submit job - result = subprocess.run( - ["kubectl", "--kubeconfig", kubeconfig_path, "apply", "-f", job_path], - capture_output=True, - text=True, - timeout=30, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ Test job submitted successfully") - - # Wait for job completion (simplified) - time.sleep(30) - - # Check job status - result = subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "get", - "job", - "clustrix-test-job", - "-o", - "yaml", - ], - capture_output=True, - text=True, - timeout=30, - env=env, - ) - - if result.returncode == 0: - logger.info("✅ Test job execution verified") - else: - logger.warning(f"⚠️ Job status check failed: {result.stderr}") - - # Clean up test job - subprocess.run( - [ - "kubectl", - "--kubeconfig", - kubeconfig_path, - "delete", - "job", - "clustrix-test-job", - ], - capture_output=True, - timeout=30, - env=env, - ) - - else: - logger.warning(f"⚠️ Test job submission failed: {result.stderr}") - - except Exception as e: - logger.warning(f"⚠️ Basic job execution test failed: {e}") - finally: - # Clean up temporary files - try: - os.unlink(kubeconfig_path) - os.unlink(job_path) - except: - pass - - def _is_kubectl_available(self) -> bool: - """Check if kubectl is available in the system.""" - try: - import subprocess - - result = subprocess.run( - ["kubectl", "version", "--client"], capture_output=True, timeout=10 - ) - return result.returncode == 0 - except: - return False - - -@pytest.mark.real_world -class TestGCPGKEIntegrationWithClustrix: - """Test GKE provisioner integration with Clustrix @cluster decorator.""" - - def test_cluster_decorator_auto_provisioning(self): - """Test @cluster decorator with auto_provision=True.""" - logger.info("🧪 Testing @cluster decorator auto-provisioning...") - - # This test would require the full integration to be complete - # For now, we'll test the configuration aspect - - from clustrix.decorator import cluster - from clustrix.config import ClusterConfig - - # Test configuration propagation - config = ClusterConfig() - - @cluster( - platform="kubernetes", - auto_provision=True, - provider="gcp", - node_count=2, - region="us-central1", - ) - def test_function(): - return "Hello from auto-provisioned GKE!" - - # The decorator should have updated the config - # (This is a unit test until full integration is complete) - logger.info("✅ Decorator configuration test passed") - - def test_kubernetes_cluster_spec_validation(self): - """Test cluster specification validation.""" - logger.info("🧪 Testing cluster specification validation...") - - from clustrix.kubernetes.cluster_provisioner import ClusterSpec - - # Valid specification - spec = ClusterSpec( - provider="gcp", - cluster_name="test-cluster", - region="us-central1", - node_count=2, - kubernetes_version="1.28", - ) - - assert spec.provider == "gcp" - assert spec.cluster_name == "test-cluster" - assert spec.node_count == 2 - - logger.info("✅ Cluster specification validation passed") - - -if __name__ == "__main__": - # Allow running individual tests - pytest.main([__file__, "-v", "--tb=short"]) diff --git a/tests/real_world/test_kubernetes_huggingface_integration.py b/tests/real_world/test_kubernetes_huggingface_integration.py deleted file mode 100644 index 77c04306..00000000 --- a/tests/real_world/test_kubernetes_huggingface_integration.py +++ /dev/null @@ -1,467 +0,0 @@ -""" -Real-world integration tests for HuggingFace Spaces Kubernetes provisioning. - -These tests use actual HuggingFace API calls and credentials to verify that: -1. HuggingFace Spaces can be created and configured for Kubernetes-style jobs -2. Job execution works correctly through the Kubernetes adapter interface -3. Spaces can be properly cleaned up after use -4. Error handling works correctly with real API responses - -Requirements: -- Valid HuggingFace token with Spaces creation permissions -- Network connectivity to HuggingFace Hub -- Sufficient HuggingFace quota for Space creation -""" - -import os -import time -import pytest -import logging -from typing import Dict, Any - -from clustrix.kubernetes.huggingface_provisioner import HuggingFaceKubernetesProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestHuggingFaceKubernetesIntegration: - """Real-world integration tests for HuggingFace Spaces Kubernetes provisioning.""" - - @pytest.fixture(scope="class") - def hf_credentials(self): - """Get HuggingFace credentials from environment or 1Password.""" - # Try environment variables first - token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN") - username = os.getenv("HF_USERNAME") or os.getenv("HUGGINGFACE_USERNAME") - - if not token or not username: - # Try 1Password CLI - try: - import subprocess - - # Get token from 1Password - token_result = subprocess.run( - ["op", "item", "get", "HuggingFace", "--field", "token"], - capture_output=True, - text=True, - timeout=30, - ) - if token_result.returncode == 0: - token = token_result.stdout.strip() - - # Get username from 1Password - username_result = subprocess.run( - ["op", "item", "get", "HuggingFace", "--field", "username"], - capture_output=True, - text=True, - timeout=30, - ) - if username_result.returncode == 0: - username = username_result.stdout.strip() - - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - pass - - if not token or not username: - pytest.skip("HuggingFace credentials not available") - - return {"token": token, "username": username} - - @pytest.fixture(scope="class") - def provisioner(self, hf_credentials): - """Create HuggingFace Kubernetes provisioner.""" - return HuggingFaceKubernetesProvisioner( - credentials=hf_credentials, region="global" # HF doesn't have regions - ) - - @pytest.fixture(scope="class") - def cluster_spec(self): - """Create test cluster specification.""" - test_id = int(time.time()) - return ClusterSpec( - cluster_name=f"test-hf-k8s-{test_id}", - provider="huggingface", - node_count=1, # Single node for basic testing - kubernetes_version="1.28", - region="global", - ) - - def test_credential_validation(self, provisioner): - """Test that HuggingFace credentials can be validated.""" - logger.info("🧪 Testing HuggingFace credential validation") - - result = provisioner.validate_credentials() - assert result is True, "HuggingFace credentials should be valid" - - logger.info("✅ HuggingFace credentials validated successfully") - - def test_space_provisioning_lifecycle(self, provisioner, cluster_spec): - """Test complete Space provisioning lifecycle.""" - logger.info( - f"🧪 Testing HuggingFace Space provisioning lifecycle for {cluster_spec.cluster_name}" - ) - - cluster_info = None - try: - # Provision the Space - start_time = time.time() - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - provision_time = time.time() - start_time - - # Verify cluster info structure - assert cluster_info is not None, "Cluster info should not be None" - assert cluster_info["cluster_id"] == cluster_spec.cluster_name - assert cluster_info["provider"] == "huggingface" - assert cluster_info["ready_for_jobs"] is True - assert "space_url" in cluster_info - assert "kubectl_config" in cluster_info - - logger.info( - f"✅ Space provisioned in {provision_time:.1f}s: {cluster_info['space_url']}" - ) - - # Wait for Space to be fully ready - max_wait = 600 # 10 minutes - wait_start = time.time() - - while time.time() - wait_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - if status["ready_for_jobs"]: - break - logger.info( - f"⏳ Waiting for Space to be ready... Status: {status['status']}" - ) - time.sleep(30) - else: - pytest.fail("Space did not become ready within timeout period") - - ready_time = time.time() - wait_start - logger.info(f"✅ Space ready for jobs in {ready_time:.1f}s") - - # Test basic Space functionality - final_status = provisioner.get_cluster_status(cluster_spec.cluster_name) - assert final_status["ready_for_jobs"] is True - assert final_status["status"] == "RUNNING" - - logger.info( - "✅ HuggingFace Space provisioning lifecycle completed successfully" - ) - - finally: - # Clean up - if cluster_info: - logger.info("🧹 Cleaning up HuggingFace Space") - success = provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - if success: - logger.info("✅ Space cleanup completed successfully") - else: - logger.warning("⚠️ Space cleanup may not have completed fully") - - def test_kubernetes_job_execution(self, provisioner, cluster_spec): - """Test Kubernetes-style job execution on HuggingFace Space.""" - logger.info( - f"🧪 Testing Kubernetes job execution on HuggingFace Space: {cluster_spec.cluster_name}" - ) - - cluster_info = None - try: - # Provision the Space - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Wait for Space to be ready - max_wait = 600 # 10 minutes - wait_start = time.time() - - while time.time() - wait_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - if status["ready_for_jobs"]: - break - time.sleep(30) - else: - pytest.fail("Space did not become ready for job execution") - - logger.info("✅ Space is ready, testing job execution") - - # Test job execution through the kubectl interface - # Note: This would require implementing a test client for the Space's API - # For now, we verify that the Space is configured correctly for job execution - - # Verify kubectl config is properly formatted - kubectl_config = cluster_info["kubectl_config"] - assert "clusters" in kubectl_config - assert "contexts" in kubectl_config - assert "users" in kubectl_config - assert kubectl_config["kind"] == "Config" - - # Verify the Space endpoint is accessible - space_url = cluster_info["space_url"] - assert space_url.startswith("https://huggingface.co/spaces/") - - logger.info("✅ Kubernetes job execution setup verified") - - finally: - # Clean up - if cluster_info: - logger.info("🧹 Cleaning up HuggingFace Space after job test") - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - - def test_space_hardware_mapping(self, provisioner): - """Test that node requirements are properly mapped to HF hardware.""" - logger.info("🧪 Testing HuggingFace hardware mapping") - - test_cases = [ - {"node_count": 1, "expected_hardware": "cpu-basic"}, - {"node_count": 2, "expected_hardware": "cpu-upgrade"}, - {"node_count": 4, "expected_hardware": "t4-small"}, - {"node_count": 8, "expected_hardware": "t4-medium"}, - ] - - for case in test_cases: - spec = ClusterSpec( - cluster_name=f"test-hardware-{case['node_count']}", - provider="huggingface", - node_count=case["node_count"], - kubernetes_version="1.28", - region="global", - ) - - # Test the hardware mapping logic - hardware = provisioner._map_node_requirements_to_hardware(spec) - assert hardware == case["expected_hardware"], ( - f"Expected hardware {case['expected_hardware']} for {case['node_count']} nodes, " - f"got {hardware}" - ) - - logger.info("✅ HuggingFace hardware mapping working correctly") - - def test_error_handling(self, hf_credentials): - """Test error handling with invalid configurations.""" - logger.info("🧪 Testing HuggingFace error handling") - - # Test with invalid credentials - invalid_provisioner = HuggingFaceKubernetesProvisioner( - credentials={"token": "invalid", "username": "invalid"}, region="global" - ) - - result = invalid_provisioner.validate_credentials() - assert result is False, "Invalid credentials should fail validation" - - # Test with missing credentials - with pytest.raises(ValueError, match="HuggingFace token required"): - HuggingFaceKubernetesProvisioner(credentials={}, region="global") - - logger.info("✅ HuggingFace error handling working correctly") - - def test_space_status_monitoring(self, provisioner, cluster_spec): - """Test Space status monitoring capabilities.""" - logger.info( - f"🧪 Testing HuggingFace Space status monitoring: {cluster_spec.cluster_name}" - ) - - # Test status of non-existent Space - status = provisioner.get_cluster_status("non-existent-space") - assert status["status"] == "NOT_FOUND" - assert status["ready_for_jobs"] is False - - cluster_info = None - try: - # Create Space and monitor its status - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Monitor status during startup - startup_statuses = [] - max_wait = 300 # 5 minutes for startup monitoring - wait_start = time.time() - - while time.time() - wait_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - startup_statuses.append(status["status"]) - - if status["ready_for_jobs"]: - break - time.sleep(15) - - # Verify we captured the status progression - assert len(startup_statuses) > 0, "Should have captured startup statuses" - assert ( - "RUNNING" in startup_statuses - ), "Should eventually reach RUNNING status" - - logger.info( - f"✅ Status monitoring captured progression: {set(startup_statuses)}" - ) - - finally: - # Clean up - if cluster_info: - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - - def test_concurrent_space_operations(self, provisioner, hf_credentials): - """Test handling of concurrent Space operations.""" - logger.info("🧪 Testing concurrent HuggingFace Space operations") - - import threading - import concurrent.futures - - test_id = int(time.time()) - cluster_specs = [ - ClusterSpec( - cluster_name=f"test-concurrent-{test_id}-{i}", - provider="huggingface", - node_count=1, - kubernetes_version="1.28", - region="global", - ) - for i in range(2) # Test with 2 concurrent spaces - ] - - created_clusters = [] - - def create_space(spec): - """Helper function to create a Space.""" - try: - provisioner_instance = HuggingFaceKubernetesProvisioner( - credentials=hf_credentials, region="global" - ) - cluster_info = provisioner_instance.provision_complete_infrastructure( - spec - ) - return cluster_info - except Exception as e: - logger.error(f"Failed to create Space {spec.cluster_name}: {e}") - return None - - try: - # Create Spaces concurrently - with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: - futures = [ - executor.submit(create_space, spec) for spec in cluster_specs - ] - results = [ - future.result(timeout=900) for future in futures - ] # 15 min timeout - - # Verify results - successful_clusters = [r for r in results if r is not None] - created_clusters = successful_clusters - - # We expect at least one to succeed (HF might have rate limits) - assert ( - len(successful_clusters) >= 1 - ), "At least one concurrent Space creation should succeed" - - logger.info( - f"✅ Concurrent operations completed: {len(successful_clusters)}/{len(cluster_specs)} succeeded" - ) - - finally: - # Clean up all created Spaces - for cluster_info in created_clusters: - if cluster_info: - try: - provisioner.destroy_cluster_infrastructure( - cluster_info["cluster_name"] - ) - except Exception as e: - logger.warning( - f"Failed to cleanup Space {cluster_info['cluster_name']}: {e}" - ) - - def test_space_resource_cleanup(self, provisioner, cluster_spec): - """Test thorough resource cleanup after Space operations.""" - logger.info( - f"🧪 Testing HuggingFace Space resource cleanup: {cluster_spec.cluster_name}" - ) - - # Create Space - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Verify Space exists - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - assert status["status"] != "NOT_FOUND", "Space should exist after creation" - - # Clean up - success = provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - assert success is True, "Cleanup should succeed" - - # Verify Space is gone (with some delay for eventual consistency) - time.sleep(10) # Wait for cleanup to propagate - final_status = provisioner.get_cluster_status(cluster_spec.cluster_name) - assert ( - final_status["status"] == "NOT_FOUND" - ), "Space should be gone after cleanup" - - logger.info("✅ HuggingFace Space resource cleanup verified") - - @pytest.mark.performance - def test_space_provisioning_performance(self, provisioner, cluster_spec): - """Test and benchmark Space provisioning performance.""" - logger.info( - f"🧪 Testing HuggingFace Space provisioning performance: {cluster_spec.cluster_name}" - ) - - cluster_info = None - try: - # Measure provisioning time - start_time = time.time() - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - provision_time = time.time() - start_time - - # Measure time to ready state - ready_start = time.time() - max_wait = 600 # 10 minutes - - while time.time() - ready_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - if status["ready_for_jobs"]: - break - time.sleep(15) - else: - pytest.fail("Space did not become ready within performance timeout") - - ready_time = time.time() - ready_start - total_time = provision_time + ready_time - - # Log performance metrics - logger.info(f"📊 HuggingFace Space Performance Metrics:") - logger.info(f" Provisioning time: {provision_time:.1f}s") - logger.info(f" Ready time: {ready_time:.1f}s") - logger.info(f" Total time: {total_time:.1f}s") - - # Performance assertions (reasonable expectations for HF Spaces) - assert ( - provision_time < 60 - ), f"Provisioning should complete within 60s (took {provision_time:.1f}s)" - assert ( - total_time < 900 - ), f"Total setup should complete within 15min (took {total_time:.1f}s)" - - # Test cleanup performance - cleanup_start = time.time() - success = provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - cleanup_time = time.time() - cleanup_start - - assert success is True, "Cleanup should succeed" - assert ( - cleanup_time < 30 - ), f"Cleanup should complete within 30s (took {cleanup_time:.1f}s)" - - logger.info(f" Cleanup time: {cleanup_time:.1f}s") - logger.info("✅ HuggingFace Space performance benchmarking completed") - - cluster_info = None # Prevent duplicate cleanup - - finally: - # Ensure cleanup - if cluster_info: - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) diff --git a/tests/real_world/test_kubernetes_lambda_integration.py b/tests/real_world/test_kubernetes_lambda_integration.py deleted file mode 100644 index 91d2b1b1..00000000 --- a/tests/real_world/test_kubernetes_lambda_integration.py +++ /dev/null @@ -1,591 +0,0 @@ -""" -Real-world integration tests for Lambda Cloud Kubernetes provisioning. - -These tests use actual Lambda Cloud API calls and credentials to verify that: -1. Lambda Cloud instances can be created and configured for Kubernetes-style jobs -2. SSH connectivity and job execution works correctly -3. GPU instances are properly provisioned when available -4. Instances can be properly cleaned up after use -5. Error handling works correctly with real API responses - -Requirements: -- Valid Lambda Cloud API key -- Network connectivity to Lambda Cloud API -- Sufficient Lambda Cloud quota for instance creation -- SSH key generation capabilities -""" - -import os -import time -import pytest -import logging -import socket -from typing import Dict, Any - -from clustrix.kubernetes.lambda_provisioner import LambdaCloudKubernetesProvisioner -from clustrix.kubernetes.cluster_provisioner import ClusterSpec - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestLambdaCloudKubernetesIntegration: - """Real-world integration tests for Lambda Cloud Kubernetes provisioning.""" - - @pytest.fixture(scope="class") - def lambda_credentials(self): - """Get Lambda Cloud credentials from environment or 1Password.""" - # Try environment variables first - api_key = os.getenv("LAMBDA_API_KEY") or os.getenv("LAMBDA_CLOUD_API_KEY") - - if not api_key: - # Try 1Password CLI - try: - import subprocess - - # Get API key from 1Password - result = subprocess.run( - ["op", "item", "get", "Lambda-Cloud", "--field", "api_key"], - capture_output=True, - text=True, - timeout=30, - ) - if result.returncode == 0: - api_key = result.stdout.strip() - - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - pass - - if not api_key: - pytest.skip("Lambda Cloud API key not available") - - return {"api_key": api_key} - - @pytest.fixture(scope="class") - def provisioner(self, lambda_credentials): - """Create Lambda Cloud Kubernetes provisioner.""" - return LambdaCloudKubernetesProvisioner( - credentials=lambda_credentials, - region="us-west-2", # Common Lambda Cloud region - ) - - @pytest.fixture(scope="class") - def cluster_spec(self): - """Create test cluster specification.""" - test_id = int(time.time()) - return ClusterSpec( - cluster_name=f"test-lambda-k8s-{test_id}", - provider="lambda", - node_count=1, # Single instance for testing - kubernetes_version="1.28", - region="us-west-2", - ) - - def test_credential_validation(self, provisioner): - """Test that Lambda Cloud credentials can be validated.""" - logger.info("🧪 Testing Lambda Cloud credential validation") - - result = provisioner.validate_credentials() - assert result is True, "Lambda Cloud credentials should be valid" - - logger.info("✅ Lambda Cloud credentials validated successfully") - - def test_instance_provisioning_lifecycle(self, provisioner, cluster_spec): - """Test complete instance provisioning lifecycle.""" - logger.info( - f"🧪 Testing Lambda Cloud instance provisioning lifecycle for {cluster_spec.cluster_name}" - ) - - cluster_info = None - try: - # Provision the instances - start_time = time.time() - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - provision_time = time.time() - start_time - - # Verify cluster info structure - assert cluster_info is not None, "Cluster info should not be None" - assert cluster_info["cluster_id"] == cluster_spec.cluster_name - assert cluster_info["provider"] == "lambda" - assert cluster_info["ready_for_jobs"] is True - assert "instances" in cluster_info - assert len(cluster_info["instances"]) == cluster_spec.node_count - assert "kubectl_config" in cluster_info - - # Verify instance details - instance = cluster_info["instances"][0] - assert "id" in instance - assert "ip" in instance - assert "status" in instance - assert instance["status"] == "active" - - logger.info( - f"✅ Instances provisioned in {provision_time:.1f}s: {len(cluster_info['instances'])} instances" - ) - - # Test instance connectivity - instance_ip = instance["ip"] - self._test_instance_connectivity(instance_ip) - - # Test cluster status - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - assert status["ready_for_jobs"] is True - assert status["status"] == "ACTIVE" - - logger.info( - "✅ Lambda Cloud instance provisioning lifecycle completed successfully" - ) - - finally: - # Clean up - if cluster_info: - logger.info("🧹 Cleaning up Lambda Cloud instances") - success = provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - if success: - logger.info("✅ Instance cleanup completed successfully") - else: - logger.warning("⚠️ Instance cleanup may not have completed fully") - - def _test_instance_connectivity(self, instance_ip: str): - """Test basic connectivity to instance.""" - logger.info(f"🧪 Testing connectivity to instance: {instance_ip}") - - # Test if port 22 (SSH) is open - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(10) - - try: - result = sock.connect_ex((instance_ip, 22)) - if result == 0: - logger.info("✅ SSH port is accessible") - else: - logger.warning( - "⚠️ SSH port is not immediately accessible (may still be starting)" - ) - finally: - sock.close() - - def test_ssh_job_execution(self, provisioner, cluster_spec): - """Test SSH-based job execution on Lambda Cloud instances.""" - logger.info( - f"🧪 Testing SSH job execution on Lambda Cloud instances: {cluster_spec.cluster_name}" - ) - - cluster_info = None - try: - # Provision the instances - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Wait for instances to be fully ready - max_wait = 300 # 5 minutes - wait_start = time.time() - - while time.time() - wait_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - if status["ready_for_jobs"]: - break - time.sleep(10) - else: - pytest.fail("Instances did not become ready for job execution") - - logger.info("✅ Instances are ready, testing job execution setup") - - # Verify kubectl config is properly formatted - kubectl_config = cluster_info["kubectl_config"] - assert "clusters" in kubectl_config - assert "contexts" in kubectl_config - assert "users" in kubectl_config - assert kubectl_config["kind"] == "Config" - - # Verify instance has job server running (would be started in setup) - instance = cluster_info["instances"][0] - instance_ip = instance["ip"] - - # Test if the job server port is accessible - self._test_job_server_connectivity(instance_ip) - - logger.info("✅ SSH job execution setup verified") - - finally: - # Clean up - if cluster_info: - logger.info("🧹 Cleaning up Lambda Cloud instances after job test") - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - - def _test_job_server_connectivity(self, instance_ip: str): - """Test connectivity to job server on instance.""" - logger.info(f"🧪 Testing job server connectivity on {instance_ip}:8080") - - # Test if port 8080 (job server) is open - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(10) - - try: - result = sock.connect_ex((instance_ip, 8080)) - if result == 0: - logger.info("✅ Job server port is accessible") - else: - logger.warning( - "⚠️ Job server port is not immediately accessible (may still be starting)" - ) - finally: - sock.close() - - def test_instance_type_selection(self, provisioner): - """Test that appropriate instance types are selected.""" - logger.info("🧪 Testing Lambda Cloud instance type selection") - - # Test the instance type mapping logic - spec = ClusterSpec( - cluster_name="test-instance-type", - provider="lambda", - node_count=1, - kubernetes_version="1.28", - region="us-west-2", - ) - - # This should select a GPU instance type (preferred for Lambda Cloud) - instance_type = provisioner._map_node_requirements_to_instance_type(spec) - - # Verify it's a valid instance type (will be from available types) - assert instance_type is not None, "Should select a valid instance type" - assert isinstance(instance_type, str), "Instance type should be a string" - - logger.info(f"✅ Selected instance type: {instance_type}") - - def test_ssh_key_management(self, provisioner, cluster_spec): - """Test SSH key creation and management.""" - logger.info(f"🧪 Testing SSH key management: {cluster_spec.cluster_name}") - - cluster_info = None - try: - # Provision instances (which creates SSH keys) - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Verify SSH key was created and tracked - created_resources = cluster_info["created_resources"] - assert "ssh_keys" in created_resources - assert len(created_resources["ssh_keys"]) > 0 - - ssh_key_name = created_resources["ssh_keys"][0] - assert ssh_key_name.startswith( - "clustrix-" - ), "SSH key should have clustrix prefix" - - logger.info(f"✅ SSH key created and tracked: {ssh_key_name}") - - finally: - # Clean up - if cluster_info: - logger.info("🧹 Cleaning up SSH keys") - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - - def test_error_handling(self, lambda_credentials): - """Test error handling with invalid configurations.""" - logger.info("🧪 Testing Lambda Cloud error handling") - - # Test with invalid credentials - invalid_provisioner = LambdaCloudKubernetesProvisioner( - credentials={"api_key": "invalid-key"}, region="us-west-2" - ) - - result = invalid_provisioner.validate_credentials() - assert result is False, "Invalid credentials should fail validation" - - # Test with missing credentials - with pytest.raises(ValueError, match="Lambda Cloud API key required"): - LambdaCloudKubernetesProvisioner(credentials={}, region="us-west-2") - - logger.info("✅ Lambda Cloud error handling working correctly") - - def test_instance_status_monitoring(self, provisioner, cluster_spec): - """Test instance status monitoring capabilities.""" - logger.info( - f"🧪 Testing Lambda Cloud instance status monitoring: {cluster_spec.cluster_name}" - ) - - # Test status of non-existent cluster - status = provisioner.get_cluster_status("non-existent-cluster") - assert status["status"] == "NOT_FOUND" - assert status["ready_for_jobs"] is False - - cluster_info = None - try: - # Create instances and monitor their status - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Monitor status during startup - startup_statuses = [] - max_wait = 180 # 3 minutes for startup monitoring - wait_start = time.time() - - while time.time() - wait_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - startup_statuses.append(status["status"]) - - if status["ready_for_jobs"]: - break - time.sleep(10) - - # Verify we captured the status progression - assert len(startup_statuses) > 0, "Should have captured startup statuses" - assert "ACTIVE" in startup_statuses, "Should eventually reach ACTIVE status" - - logger.info( - f"✅ Status monitoring captured progression: {set(startup_statuses)}" - ) - - finally: - # Clean up - if cluster_info: - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - - def test_concurrent_instance_operations(self, provisioner, lambda_credentials): - """Test handling of concurrent instance operations.""" - logger.info("🧪 Testing concurrent Lambda Cloud instance operations") - - import concurrent.futures - - test_id = int(time.time()) - cluster_specs = [ - ClusterSpec( - cluster_name=f"test-concurrent-{test_id}-{i}", - provider="lambda", - node_count=1, - kubernetes_version="1.28", - region="us-west-2", - ) - for i in range(2) # Test with 2 concurrent clusters - ] - - created_clusters = [] - - def create_cluster(spec): - """Helper function to create a cluster.""" - try: - provisioner_instance = LambdaCloudKubernetesProvisioner( - credentials=lambda_credentials, region="us-west-2" - ) - cluster_info = provisioner_instance.provision_complete_infrastructure( - spec - ) - return cluster_info - except Exception as e: - logger.error(f"Failed to create cluster {spec.cluster_name}: {e}") - return None - - try: - # Create clusters concurrently - with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: - futures = [ - executor.submit(create_cluster, spec) for spec in cluster_specs - ] - results = [ - future.result(timeout=600) for future in futures - ] # 10 min timeout - - # Verify results - successful_clusters = [r for r in results if r is not None] - created_clusters = successful_clusters - - # We expect at least one to succeed (Lambda Cloud might have capacity limits) - assert ( - len(successful_clusters) >= 1 - ), "At least one concurrent cluster creation should succeed" - - logger.info( - f"✅ Concurrent operations completed: {len(successful_clusters)}/{len(cluster_specs)} succeeded" - ) - - finally: - # Clean up all created clusters - for cluster_info in created_clusters: - if cluster_info: - try: - provisioner.destroy_cluster_infrastructure( - cluster_info["cluster_name"] - ) - except Exception as e: - logger.warning( - f"Failed to cleanup cluster {cluster_info['cluster_name']}: {e}" - ) - - def test_instance_resource_cleanup(self, provisioner, cluster_spec): - """Test thorough resource cleanup after instance operations.""" - logger.info( - f"🧪 Testing Lambda Cloud instance resource cleanup: {cluster_spec.cluster_name}" - ) - - # Create instances - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Verify instances exist - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - assert status["status"] != "NOT_FOUND", "Cluster should exist after creation" - - # Get resource info for verification - created_resources = cluster_info["created_resources"] - instance_ids = created_resources.get("instances", []) - ssh_keys = created_resources.get("ssh_keys", []) - - assert len(instance_ids) > 0, "Should have created instances" - assert len(ssh_keys) > 0, "Should have created SSH keys" - - # Clean up - success = provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - assert success is True, "Cleanup should succeed" - - # Verify instances are gone (with some delay for API consistency) - time.sleep(15) # Wait for cleanup to propagate - final_status = provisioner.get_cluster_status(cluster_spec.cluster_name) - assert ( - final_status["status"] == "NOT_FOUND" - ), "Cluster should be gone after cleanup" - - logger.info("✅ Lambda Cloud instance resource cleanup verified") - - @pytest.mark.performance - def test_instance_provisioning_performance(self, provisioner, cluster_spec): - """Test and benchmark instance provisioning performance.""" - logger.info( - f"🧪 Testing Lambda Cloud instance provisioning performance: {cluster_spec.cluster_name}" - ) - - cluster_info = None - try: - # Measure provisioning time - start_time = time.time() - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - provision_time = time.time() - start_time - - # Measure time to ready state - ready_start = time.time() - max_wait = 300 # 5 minutes - - while time.time() - ready_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - if status["ready_for_jobs"]: - break - time.sleep(10) - else: - pytest.fail("Instances did not become ready within performance timeout") - - ready_time = time.time() - ready_start - total_time = provision_time + ready_time - - # Log performance metrics - logger.info(f"📊 Lambda Cloud Instance Performance Metrics:") - logger.info(f" Provisioning time: {provision_time:.1f}s") - logger.info(f" Ready time: {ready_time:.1f}s") - logger.info(f" Total time: {total_time:.1f}s") - - # Performance assertions (reasonable expectations for Lambda Cloud) - assert ( - provision_time < 180 - ), f"Provisioning should complete within 3min (took {provision_time:.1f}s)" - assert ( - total_time < 420 - ), f"Total setup should complete within 7min (took {total_time:.1f}s)" - - # Test cleanup performance - cleanup_start = time.time() - success = provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - cleanup_time = time.time() - cleanup_start - - assert success is True, "Cleanup should succeed" - assert ( - cleanup_time < 60 - ), f"Cleanup should complete within 60s (took {cleanup_time:.1f}s)" - - logger.info(f" Cleanup time: {cleanup_time:.1f}s") - logger.info("✅ Lambda Cloud instance performance benchmarking completed") - - cluster_info = None # Prevent duplicate cleanup - - finally: - # Ensure cleanup - if cluster_info: - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - - def test_gpu_instance_availability(self, provisioner): - """Test GPU instance availability and selection.""" - logger.info("🧪 Testing Lambda Cloud GPU instance availability") - - # This test checks if GPU instances are available and properly selected - spec = ClusterSpec( - cluster_name="test-gpu-availability", - provider="lambda", - node_count=1, - kubernetes_version="1.28", - region="us-west-2", - ) - - try: - # Get available instance types - import requests - - response = requests.get( - f"{provisioner.base_url}/instance-types", - headers=provisioner.headers, - timeout=30, - ) - response.raise_for_status() - instance_types = response.json().get("data", {}) - - # Check if GPU types are available - gpu_types = [t for t in instance_types.keys() if "gpu" in t.lower()] - - if gpu_types: - logger.info(f"✅ GPU instance types available: {gpu_types}") - - # Test instance type selection prefers GPU - selected_type = provisioner._map_node_requirements_to_instance_type( - spec - ) - if "gpu" in selected_type.lower(): - logger.info(f"✅ GPU instance type selected: {selected_type}") - else: - logger.info(f"ℹ️ Non-GPU instance type selected: {selected_type}") - else: - logger.info("ℹ️ No GPU instance types currently available") - - except Exception as e: - logger.warning(f"⚠️ Could not check GPU availability: {e}") - - def test_network_security_setup(self, provisioner, cluster_spec): - """Test network security and SSH setup.""" - logger.info(f"🧪 Testing network security setup: {cluster_spec.cluster_name}") - - cluster_info = None - try: - # Provision instances - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - - # Verify SSH connectivity setup - instances = cluster_info["instances"] - ssh_key_info = cluster_info.get("created_resources", {}).get("ssh_keys", []) - - assert len(ssh_key_info) > 0, "Should have SSH keys for secure access" - - # Verify instances have public IPs for connectivity - for instance in instances: - assert "ip" in instance, "Instance should have IP address" - assert instance["ip"] is not None, "Instance IP should not be None" - - # Basic IP format validation - ip_parts = instance["ip"].split(".") - assert len(ip_parts) == 4, "Should be valid IPv4 address format" - - logger.info("✅ Network security setup verified") - - finally: - if cluster_info: - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) diff --git a/tests/real_world/test_kubernetes_local_execution.py b/tests/real_world/test_kubernetes_local_execution.py deleted file mode 100644 index 0edb5ad4..00000000 --- a/tests/real_world/test_kubernetes_local_execution.py +++ /dev/null @@ -1,497 +0,0 @@ -""" -Local Docker-based Kubernetes real execution test. - -This test creates a real local Kubernetes cluster using kind (Kubernetes in Docker) -and executes actual Python functions on it to validate the complete workflow. - -This provides definitive proof that the Kubernetes auto-provisioning system works -end-to-end without requiring expensive cloud infrastructure. -""" - -import os -import time -import pytest -import logging -import subprocess -import socket -from typing import Dict, Any - -from clustrix import cluster -from clustrix.config import ClusterConfig, get_config - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -@pytest.mark.slow -class TestLocalKubernetesExecution: - """Real local Kubernetes execution tests.""" - - @pytest.fixture(scope="class") - def check_prerequisites(self): - """Check that Docker and kind are available.""" - # Check Docker - try: - result = subprocess.run( - ["docker", "version"], capture_output=True, timeout=10 - ) - assert result.returncode == 0, "Docker is required but not available" - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - pytest.skip("Docker is not available for local Kubernetes testing") - - # Check kind - try: - result = subprocess.run( - ["kind", "version"], capture_output=True, timeout=10 - ) - assert result.returncode == 0, "kind is required but not available" - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - pytest.skip("kind is not available for local Kubernetes testing") - - # Check kubectl - try: - result = subprocess.run( - ["kubectl", "version", "--client"], capture_output=True, timeout=10 - ) - assert result.returncode == 0, "kubectl is required but not available" - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - pytest.skip("kubectl is not available for local Kubernetes testing") - - logger.info("✅ All prerequisites available for local Kubernetes testing") - return True - - @pytest.fixture(scope="function") - def local_cluster_config(self): - """Create local cluster configuration.""" - test_id = int(time.time()) - - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_from_scratch = True - config.k8s_provider = "local" - config.k8s_region = "local" - config.k8s_node_count = 2 # 1 control plane + 1 worker - config.k8s_cleanup_on_exit = True # Always cleanup local clusters - config.k8s_cluster_name = f"clustrix-test-{test_id}" - - return config - - def test_simple_local_function_execution( - self, check_prerequisites, local_cluster_config - ): - """Test execution of a simple Python function on local Kubernetes cluster.""" - logger.info("🧪 Testing simple function execution on local Kubernetes cluster") - - # Override global config for this test - from clustrix.config import _config - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = local_cluster_config - - cluster_created = False - - try: - # Define test function with @cluster decorator - @cluster( - platform="kubernetes", - auto_provision=True, - provider="local", - node_count=2, - cluster_name=local_cluster_config.k8s_cluster_name, - ) - def local_computation(x: int, y: int) -> Dict[str, Any]: - """Simple computation function for local testing.""" - import platform - import os - import socket - - result = x * y + 42 - - return { - "result": result, - "platform": platform.platform(), - "python_version": platform.python_version(), - "hostname": socket.gethostname(), - "working_dir": os.getcwd(), - "environment": "kubernetes", - "computed_at": time.time(), - } - - # Execute function - this should trigger local cluster provisioning - logger.info( - "🚀 Starting function execution (will auto-provision local cluster)" - ) - start_time = time.time() - - result = local_computation(7, 11) - execution_time = time.time() - start_time - cluster_created = True - - # Verify results - assert isinstance(result, dict), "Result should be a dictionary" - assert result["result"] == 119, f"Expected 119, got {result['result']}" - assert "platform" in result, "Platform info should be included" - assert "hostname" in result, "Hostname should be included" - assert ( - result["environment"] == "kubernetes" - ), "Should indicate Kubernetes environment" - - logger.info(f"✅ Function executed successfully in {execution_time:.1f}s") - logger.info(f"📊 Result: {result['result']}") - logger.info(f"🖥️ Remote platform: {result['platform']}") - logger.info(f"🏷️ Remote hostname: {result['hostname']}") - - # Verify we're running in a different environment (Kubernetes pod) - local_hostname = socket.gethostname() - assert ( - result["hostname"] != local_hostname - ), "Should execute in different environment (pod)" - - logger.info("✅ Verified execution occurred in Kubernetes pod environment") - - finally: - # Restore original config - config_module._config = original_config - - # Manual cleanup if needed - if cluster_created: - self._ensure_cluster_cleanup(local_cluster_config.k8s_cluster_name) - - def test_numpy_computation_execution( - self, check_prerequisites, local_cluster_config - ): - """Test execution of NumPy computation on local Kubernetes cluster.""" - logger.info("🧪 Testing NumPy computation on local Kubernetes cluster") - - # Override global config for this test - from clustrix.config import _config - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = local_cluster_config - local_cluster_config.k8s_cluster_name = f"clustrix-numpy-{int(time.time())}" - - cluster_created = False - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - provider="local", - cluster_name=local_cluster_config.k8s_cluster_name, - ) - def numpy_computation(size: int) -> Dict[str, Any]: - """NumPy-based computation for testing dependency handling.""" - import numpy as np - import time - - start_time = time.time() - - # Create random data - data = np.random.rand(size, size) - - # Perform computation - result_matrix = np.dot(data, data.T) - eigenvalues = np.linalg.eigvals(result_matrix) - - computation_time = time.time() - start_time - - return { - "matrix_size": size, - "max_eigenvalue": float(np.max(eigenvalues)), - "min_eigenvalue": float(np.min(eigenvalues)), - "computation_time": computation_time, - "numpy_version": np.__version__, - "status": "completed", - } - - # Execute NumPy computation - logger.info("🚀 Starting NumPy computation on local cluster") - start_time = time.time() - - result = numpy_computation(100) # 100x100 matrix - execution_time = time.time() - start_time - cluster_created = True - - # Verify results - assert isinstance(result, dict), "Result should be a dictionary" - assert ( - result["status"] == "completed" - ), "Computation should complete successfully" - assert result["matrix_size"] == 100, "Matrix size should be preserved" - assert "numpy_version" in result, "NumPy version should be included" - assert ( - result["computation_time"] > 0 - ), "Computation should take measurable time" - - logger.info(f"✅ NumPy computation completed in {execution_time:.1f}s") - logger.info( - f"⚡ Remote computation time: {result['computation_time']:.3f}s" - ) - logger.info(f"📦 NumPy version: {result['numpy_version']}") - logger.info( - f"📊 Eigenvalue range: {result['min_eigenvalue']:.3f} to {result['max_eigenvalue']:.3f}" - ) - - finally: - # Restore original config - config_module._config = original_config - - # Manual cleanup if needed - if cluster_created: - self._ensure_cluster_cleanup(local_cluster_config.k8s_cluster_name) - - def test_error_handling_and_cleanup( - self, check_prerequisites, local_cluster_config - ): - """Test error handling and proper cleanup of local clusters.""" - logger.info("🧪 Testing error handling and cleanup") - - # Override global config for this test - from clustrix.config import _config - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = local_cluster_config - local_cluster_config.k8s_cluster_name = f"clustrix-error-{int(time.time())}" - - cluster_created = False - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - provider="local", - cluster_name=local_cluster_config.k8s_cluster_name, - ) - def failing_function(should_fail: bool) -> str: - """Function that can be made to fail for testing error handling.""" - if should_fail: - raise ValueError("Intentional test failure for error handling") - return "success" - - # First test successful execution - logger.info("🚀 Testing successful execution first") - result = failing_function(False) - cluster_created = True - assert result == "success", "Successful execution should return 'success'" - logger.info("✅ Successful execution confirmed") - - # Then test error handling - logger.info("🚀 Testing error propagation") - with pytest.raises(Exception) as exc_info: - failing_function(True) - - # Verify the error was properly propagated - assert "Intentional test failure" in str( - exc_info.value - ) or "ValueError" in str(type(exc_info.value)) - logger.info( - "✅ Error handling working correctly - exceptions properly propagated" - ) - - finally: - # Restore original config - config_module._config = original_config - - # Manual cleanup - if cluster_created: - self._ensure_cluster_cleanup(local_cluster_config.k8s_cluster_name) - - def test_cluster_lifecycle_management( - self, check_prerequisites, local_cluster_config - ): - """Test cluster lifecycle management and status monitoring.""" - logger.info("🧪 Testing cluster lifecycle management") - - from clustrix.kubernetes.local_provisioner import ( - LocalDockerKubernetesProvisioner, - ) - from clustrix.kubernetes.cluster_provisioner import ClusterSpec - - test_cluster_name = f"clustrix-lifecycle-{int(time.time())}" - - try: - # Create provisioner - provisioner = LocalDockerKubernetesProvisioner({}, "local") - - # Test cluster creation - logger.info("🚀 Testing cluster creation") - cluster_spec = ClusterSpec( - cluster_name=test_cluster_name, - provider="local", - node_count=2, - kubernetes_version="1.28", - region="local", - ) - - start_time = time.time() - cluster_info = provisioner.provision_complete_infrastructure(cluster_spec) - creation_time = time.time() - start_time - - # Verify cluster info - assert cluster_info is not None, "Cluster info should not be None" - assert ( - cluster_info["cluster_name"] == test_cluster_name - ), "Cluster name should match" - assert ( - cluster_info["ready_for_jobs"] is True - ), "Cluster should be ready for jobs" - assert "kubectl_config" in cluster_info, "Should have kubectl config" - assert len(cluster_info["nodes"]) == 2, "Should have 2 nodes" - - logger.info(f"✅ Cluster created successfully in {creation_time:.1f}s") - logger.info(f"📊 Cluster has {cluster_info['node_count']} nodes") - logger.info(f"🔗 Cluster endpoint: {cluster_info['endpoint']}") - - # Test cluster status monitoring - logger.info("🔍 Testing cluster status monitoring") - status = provisioner.get_cluster_status(test_cluster_name) - assert ( - status["status"] == "RUNNING" - ), f"Expected RUNNING, got {status['status']}" - assert status["ready_for_jobs"] is True, "Cluster should be ready for jobs" - - logger.info("✅ Cluster status monitoring working correctly") - - # Test cluster destruction - logger.info("🗑️ Testing cluster destruction") - destruction_start = time.time() - success = provisioner.destroy_cluster_infrastructure(test_cluster_name) - destruction_time = time.time() - destruction_start - - assert success is True, "Cluster destruction should succeed" - logger.info(f"✅ Cluster destroyed successfully in {destruction_time:.1f}s") - - # Verify cluster is gone - status = provisioner.get_cluster_status(test_cluster_name) - assert ( - status["status"] == "NOT_FOUND" - ), "Cluster should be gone after destruction" - - logger.info("✅ Cluster lifecycle management working correctly") - - except Exception as e: - # Ensure cleanup on failure - logger.error(f"Test failed: {e}") - self._ensure_cluster_cleanup(test_cluster_name) - raise - - def _ensure_cluster_cleanup(self, cluster_name: str): - """Ensure cluster is cleaned up.""" - logger.info(f"🧹 Ensuring cleanup of cluster: {cluster_name}") - - try: - result = subprocess.run( - ["kind", "delete", "cluster", "--name", cluster_name], - capture_output=True, - text=True, - timeout=60, - ) - - if result.returncode == 0: - logger.info(f"✅ Cluster {cluster_name} cleaned up successfully") - else: - logger.warning(f"⚠️ Cluster cleanup may have failed: {result.stderr}") - - except Exception as e: - logger.warning(f"⚠️ Error during cleanup: {e}") - - def test_multiple_sequential_executions( - self, check_prerequisites, local_cluster_config - ): - """Test multiple sequential function executions on the same cluster.""" - logger.info("🧪 Testing multiple sequential executions") - - # Override global config for this test - from clustrix.config import _config - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = local_cluster_config - local_cluster_config.k8s_cluster_name = f"clustrix-multi-{int(time.time())}" - - cluster_created = False - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - provider="local", - cluster_name=local_cluster_config.k8s_cluster_name, - ) - def sequential_computation(iteration: int) -> Dict[str, Any]: - """Function for testing multiple sequential executions.""" - import time - import os - - return { - "iteration": iteration, - "result": iteration * 2 + 10, - "timestamp": time.time(), - "pid": os.getpid(), - } - - results = [] - - # Execute function multiple times - for i in range(3): - logger.info(f"🚀 Starting execution {i+1}/3") - start_time = time.time() - - result = sequential_computation(i + 1) - execution_time = time.time() - start_time - - results.append({"result": result, "execution_time": execution_time}) - - if i == 0: - cluster_created = True # Mark after first successful execution - - logger.info(f"✅ Execution {i+1} completed in {execution_time:.1f}s") - logger.info( - f"📊 Result: {result['result']} (iteration {result['iteration']})" - ) - - # Verify all executions succeeded - assert len(results) == 3, "Should have 3 successful executions" - - for i, result_info in enumerate(results): - result = result_info["result"] - expected = (i + 1) * 2 + 10 - assert ( - result["result"] == expected - ), f"Execution {i+1}: expected {expected}, got {result['result']}" - assert result["iteration"] == i + 1, f"Iteration should be {i+1}" - - logger.info("✅ All sequential executions completed successfully") - - # Log timing information - total_time = sum(r["execution_time"] for r in results) - avg_time = total_time / len(results) - logger.info(f"📊 Total execution time: {total_time:.1f}s") - logger.info(f"📊 Average execution time: {avg_time:.1f}s") - - finally: - # Restore original config - config_module._config = original_config - - # Manual cleanup - if cluster_created: - self._ensure_cluster_cleanup(local_cluster_config.k8s_cluster_name) diff --git a/tests/real_world/test_kubernetes_multi_provider_integration.py b/tests/real_world/test_kubernetes_multi_provider_integration.py deleted file mode 100644 index 029720df..00000000 --- a/tests/real_world/test_kubernetes_multi_provider_integration.py +++ /dev/null @@ -1,811 +0,0 @@ -""" -Comprehensive multi-provider integration tests for Kubernetes cluster provisioning. - -These tests verify cross-provider functionality and ensure consistent behavior -across all supported Kubernetes provisioning providers: -- AWS EKS from-scratch provisioning -- GCP GKE from-scratch provisioning -- Azure AKS from-scratch provisioning -- HuggingFace Spaces Kubernetes adapter -- Lambda Cloud Kubernetes adapter - -Requirements: -- Valid credentials for all tested providers -- Network connectivity to all provider APIs -- Sufficient quotas for cluster/instance creation -- SSH capabilities for direct instance testing -""" - -import os -import time -import pytest -import logging -import concurrent.futures -from typing import Dict, Any, List, Optional -from dataclasses import dataclass - -from clustrix.kubernetes.cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, -) -from clustrix.config import ClusterConfig -from clustrix.credential_manager import get_credential_manager - -logger = logging.getLogger(__name__) - - -@dataclass -class ProviderTestConfig: - """Configuration for testing a specific provider.""" - - name: str - provisioner_class: type - region: str - credentials_env_vars: List[str] - credentials_1password_item: str - expected_provision_time: int # seconds - expected_ready_time: int # seconds - - -@pytest.mark.real_world -class TestKubernetesMultiProviderIntegration: - """Comprehensive multi-provider integration tests.""" - - @pytest.fixture(scope="class") - def provider_configs(self): - """Define test configurations for all providers.""" - return [ - ProviderTestConfig( - name="aws", - provisioner_class="clustrix.kubernetes.aws_provisioner.AWSEKSFromScratchProvisioner", - region="us-west-2", - credentials_env_vars=["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"], - credentials_1password_item="AWS-Clustrix", - expected_provision_time=300, # 5 minutes - expected_ready_time=900, # 15 minutes - ), - ProviderTestConfig( - name="gcp", - provisioner_class="clustrix.kubernetes.gcp_provisioner.GCPGKEFromScratchProvisioner", - region="us-central1", - credentials_env_vars=[ - "GOOGLE_APPLICATION_CREDENTIALS", - "GCP_SERVICE_ACCOUNT_KEY", - ], - credentials_1password_item="GCP-Clustrix", - expected_provision_time=240, # 4 minutes - expected_ready_time=720, # 12 minutes - ), - ProviderTestConfig( - name="azure", - provisioner_class="clustrix.kubernetes.azure_provisioner.AzureAKSFromScratchProvisioner", - region="eastus", - credentials_env_vars=[ - "AZURE_SUBSCRIPTION_ID", - "AZURE_TENANT_ID", - "AZURE_CLIENT_ID", - "AZURE_CLIENT_SECRET", - ], - credentials_1password_item="Azure-Clustrix", - expected_provision_time=360, # 6 minutes - expected_ready_time=1080, # 18 minutes - ), - ProviderTestConfig( - name="huggingface", - provisioner_class="clustrix.kubernetes.huggingface_provisioner.HuggingFaceKubernetesProvisioner", - region="global", - credentials_env_vars=["HF_TOKEN", "HF_USERNAME"], - credentials_1password_item="HuggingFace", - expected_provision_time=60, # 1 minute - expected_ready_time=600, # 10 minutes - ), - ProviderTestConfig( - name="lambda", - provisioner_class="clustrix.kubernetes.lambda_provisioner.LambdaCloudKubernetesProvisioner", - region="us-west-2", - credentials_env_vars=["LAMBDA_API_KEY"], - credentials_1password_item="Lambda-Cloud", - expected_provision_time=120, # 2 minutes - expected_ready_time=300, # 5 minutes - ), - ] - - @pytest.fixture(scope="class") - def available_providers(self, provider_configs): - """Get list of providers with available credentials.""" - available = [] - - for config in provider_configs: - if self._has_credentials_for_provider(config): - available.append(config) - else: - logger.info(f"Skipping {config.name} - credentials not available") - - if not available: - pytest.skip("No provider credentials available for multi-provider testing") - - return available - - def _has_credentials_for_provider(self, config: ProviderTestConfig) -> bool: - """Check if credentials are available for a provider.""" - # Check environment variables - if config.credentials_env_vars: - if all(os.getenv(var) for var in config.credentials_env_vars): - return True - - # Check 1Password - try: - import subprocess - - result = subprocess.run( - [ - "op", - "item", - "get", - config.credentials_1password_item, - "--format", - "json", - ], - capture_output=True, - text=True, - timeout=10, - ) - if result.returncode == 0: - return True - except ( - subprocess.TimeoutExpired, - subprocess.SubprocessError, - FileNotFoundError, - ): - pass - - return False - - def test_all_providers_credential_validation(self, available_providers): - """Test credential validation across all available providers.""" - logger.info("🧪 Testing credential validation across all providers") - - credential_manager = get_credential_manager() - results = {} - - for config in available_providers: - logger.info(f"Testing credentials for {config.name}") - - try: - credentials = credential_manager.ensure_credential(config.name) - assert ( - credentials is not None - ), f"Should have credentials for {config.name}" - - # Test with actual provisioner - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - validation_result = provisioner.validate_credentials() - - results[config.name] = validation_result - logger.info( - f"✅ {config.name} credential validation: {'✓' if validation_result else '✗'}" - ) - - except Exception as e: - results[config.name] = False - logger.error(f"❌ {config.name} credential validation failed: {e}") - - # Assert that at least one provider has valid credentials - valid_providers = [name for name, result in results.items() if result] - assert ( - len(valid_providers) > 0 - ), f"At least one provider should have valid credentials. Results: {results}" - - logger.info( - f"✅ Credential validation completed. Valid providers: {valid_providers}" - ) - - def test_consistent_cluster_spec_handling(self, available_providers): - """Test that all providers handle ClusterSpec consistently.""" - logger.info("🧪 Testing consistent ClusterSpec handling across providers") - - test_id = int(time.time()) - base_spec = ClusterSpec( - cluster_name=f"test-consistency-{test_id}", - provider="test", # Will be overridden - node_count=1, - kubernetes_version="1.28", - ) - - credential_manager = get_credential_manager() - - for config in available_providers: - logger.info(f"Testing ClusterSpec handling for {config.name}") - - # Create provider-specific spec - spec = ClusterSpec( - cluster_name=f"test-{config.name}-spec-{test_id}", - provider=config.name, - node_count=1, - kubernetes_version="1.28", - region=config.region, - ) - - try: - # Test spec validation and processing - credentials = credential_manager.ensure_credential(config.name) - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - - # Test that the provisioner can process the spec without errors - # (We're not actually provisioning, just testing spec handling) - if hasattr(provisioner, "_map_node_requirements_to_hardware"): - # HuggingFace hardware mapping - hardware = provisioner._map_node_requirements_to_hardware(spec) - assert hardware is not None, f"{config.name} should map hardware" - elif hasattr(provisioner, "_map_node_requirements_to_instance_type"): - # Lambda Cloud instance type mapping - instance_type = provisioner._map_node_requirements_to_instance_type( - spec - ) - assert ( - instance_type is not None - ), f"{config.name} should map instance type" - - logger.info(f"✅ {config.name} ClusterSpec handling verified") - - except Exception as e: - logger.error(f"❌ {config.name} ClusterSpec handling failed: {e}") - # Don't fail the test for spec handling issues - log and continue - - logger.info("✅ ClusterSpec consistency testing completed") - - def test_kubernetes_cluster_provisioner_integration(self, available_providers): - """Test integration with the main KubernetesClusterProvisioner.""" - logger.info("🧪 Testing KubernetesClusterProvisioner integration") - - test_id = int(time.time()) - - for config in available_providers: - logger.info(f"Testing KubernetesClusterProvisioner with {config.name}") - - try: - # Create cluster config - cluster_config = ClusterConfig( - k8s_provider=config.name, - k8s_region=config.region, - auto_provision_k8s=True, - k8s_from_scratch=True, - k8s_cluster_name=f"test-integration-{config.name}-{test_id}", - k8s_node_count=1, - ) - - # Test provisioner creation and credential retrieval - provisioner = KubernetesClusterProvisioner(cluster_config) - - # Test provider detection - providers = provisioner.list_available_providers() - assert ( - config.name in providers - ), f"{config.name} should be in available providers" - - # Test cluster listing (should not fail even with no clusters) - clusters = provisioner.list_clusters([config.name]) - assert isinstance(clusters, list), "list_clusters should return a list" - - logger.info( - f"✅ {config.name} KubernetesClusterProvisioner integration verified" - ) - - except Exception as e: - logger.error( - f"❌ {config.name} KubernetesClusterProvisioner integration failed: {e}" - ) - # Continue testing other providers - - logger.info("✅ KubernetesClusterProvisioner integration testing completed") - - def test_provider_performance_comparison(self, available_providers): - """Compare provisioning performance across providers.""" - logger.info("🧪 Comparing provisioning performance across providers") - - if len(available_providers) < 2: - pytest.skip("Need at least 2 providers for performance comparison") - - performance_results = {} - test_id = int(time.time()) - - # Test credential validation performance - credential_manager = get_credential_manager() - - for config in available_providers: - logger.info(f"Testing credential validation performance for {config.name}") - - try: - start_time = time.time() - credentials = credential_manager.ensure_credential(config.name) - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - validation_result = provisioner.validate_credentials() - validation_time = time.time() - start_time - - performance_results[config.name] = { - "credential_validation_time": validation_time, - "credential_validation_success": validation_result, - } - - logger.info( - f"📊 {config.name} credential validation: {validation_time:.2f}s" - ) - - except Exception as e: - logger.error(f"❌ {config.name} performance test failed: {e}") - performance_results[config.name] = { - "credential_validation_time": float("inf"), - "credential_validation_success": False, - } - - # Log performance comparison - logger.info("📊 Performance Comparison Results:") - for name, results in performance_results.items(): - if results["credential_validation_success"]: - logger.info( - f" {name}: {results['credential_validation_time']:.2f}s credential validation" - ) - else: - logger.info(f" {name}: Failed credential validation") - - # Find fastest credential validation - successful_providers = { - name: results - for name, results in performance_results.items() - if results["credential_validation_success"] - } - - if successful_providers: - fastest_provider = min( - successful_providers.keys(), - key=lambda x: successful_providers[x]["credential_validation_time"], - ) - logger.info(f"🏆 Fastest credential validation: {fastest_provider}") - - logger.info("✅ Performance comparison completed") - - def test_error_handling_consistency(self, available_providers): - """Test that error handling is consistent across providers.""" - logger.info("🧪 Testing error handling consistency across providers") - - credential_manager = get_credential_manager() - - for config in available_providers: - logger.info(f"Testing error handling for {config.name}") - - try: - # Test with invalid credentials - invalid_credentials = {"invalid": "credentials"} - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - # Test invalid credential handling - try: - provisioner = provisioner_class(invalid_credentials, config.region) - validation_result = provisioner.validate_credentials() - assert ( - validation_result is False - ), f"{config.name} should reject invalid credentials" - except Exception as e: - # Exception is also acceptable for invalid credentials - logger.info( - f"✅ {config.name} properly rejects invalid credentials with exception" - ) - - # Test missing credential fields - empty_credentials = {} - try: - provisioner = provisioner_class(empty_credentials, config.region) - assert ( - False - ), f"{config.name} should raise ValueError for missing credentials" - except ValueError: - logger.info( - f"✅ {config.name} properly raises ValueError for missing credentials" - ) - except Exception as e: - logger.info( - f"✅ {config.name} raises exception for missing credentials: {type(e).__name__}" - ) - - except Exception as e: - logger.error(f"❌ {config.name} error handling test failed: {e}") - # Continue with other providers - - logger.info("✅ Error handling consistency testing completed") - - @pytest.mark.slow - def test_concurrent_multi_provider_operations(self, available_providers): - """Test concurrent operations across multiple providers.""" - logger.info("🧪 Testing concurrent multi-provider operations") - - if len(available_providers) < 2: - pytest.skip("Need at least 2 providers for concurrent testing") - - # Limit to 2-3 providers for practical testing - test_providers = available_providers[:3] - test_id = int(time.time()) - - def validate_provider_concurrently(config): - """Helper function for concurrent validation.""" - try: - credential_manager = get_credential_manager() - credentials = credential_manager.ensure_credential(config.name) - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - - start_time = time.time() - result = provisioner.validate_credentials() - validation_time = time.time() - start_time - - return { - "provider": config.name, - "success": result, - "validation_time": validation_time, - } - except Exception as e: - return { - "provider": config.name, - "success": False, - "error": str(e), - "validation_time": float("inf"), - } - - # Run concurrent validations - start_time = time.time() - with concurrent.futures.ThreadPoolExecutor( - max_workers=len(test_providers) - ) as executor: - futures = [ - executor.submit(validate_provider_concurrently, config) - for config in test_providers - ] - results = [ - future.result(timeout=120) for future in futures - ] # 2 min timeout - - total_concurrent_time = time.time() - start_time - - # Analyze results - successful_results = [r for r in results if r["success"]] - failed_results = [r for r in results if not r["success"]] - - logger.info(f"📊 Concurrent Multi-Provider Results:") - logger.info(f" Total concurrent time: {total_concurrent_time:.2f}s") - logger.info( - f" Successful validations: {len(successful_results)}/{len(results)}" - ) - - for result in successful_results: - logger.info(f" ✅ {result['provider']}: {result['validation_time']:.2f}s") - - for result in failed_results: - error_msg = result.get("error", "Unknown error") - logger.info(f" ❌ {result['provider']}: {error_msg}") - - # Assert that concurrent operations don't interfere with each other - assert ( - len(successful_results) > 0 - ), "At least one provider should succeed in concurrent test" - - # Test that concurrent time is reasonable (not serialized) - if len(successful_results) > 1: - max_individual_time = max(r["validation_time"] for r in successful_results) - # Concurrent execution should be faster than sum of individual times - assert total_concurrent_time < sum( - r["validation_time"] for r in successful_results - ), "Concurrent execution should be faster than sequential" - - logger.info("✅ Concurrent multi-provider operations completed") - - def test_provider_specific_features(self, available_providers): - """Test provider-specific features and capabilities.""" - logger.info("🧪 Testing provider-specific features") - - credential_manager = get_credential_manager() - - for config in available_providers: - logger.info(f"Testing provider-specific features for {config.name}") - - try: - credentials = credential_manager.ensure_credential(config.name) - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - - # Test provider-specific capabilities - if config.name == "aws": - # AWS-specific tests - assert hasattr( - provisioner, "session" - ), "AWS provisioner should have boto3 session" - assert hasattr( - provisioner, "ec2" - ), "AWS provisioner should have EC2 client" - assert hasattr( - provisioner, "eks" - ), "AWS provisioner should have EKS client" - - elif config.name == "gcp": - # GCP-specific tests - assert hasattr( - provisioner, "container_client" - ), "GCP provisioner should have container client" - assert hasattr( - provisioner, "project_id" - ), "GCP provisioner should have project ID" - - elif config.name == "azure": - # Azure-specific tests - assert hasattr( - provisioner, "container_client" - ), "Azure provisioner should have container client" - assert hasattr( - provisioner, "subscription_id" - ), "Azure provisioner should have subscription ID" - - elif config.name == "huggingface": - # HuggingFace-specific tests - assert hasattr( - provisioner, "api" - ), "HF provisioner should have HF API client" - assert hasattr( - provisioner, "username" - ), "HF provisioner should have username" - - elif config.name == "lambda": - # Lambda Cloud-specific tests - assert hasattr( - provisioner, "api_key" - ), "Lambda provisioner should have API key" - assert hasattr( - provisioner, "base_url" - ), "Lambda provisioner should have base URL" - - logger.info(f"✅ {config.name} provider-specific features verified") - - except Exception as e: - logger.error( - f"❌ {config.name} provider-specific feature test failed: {e}" - ) - - logger.info("✅ Provider-specific feature testing completed") - - def test_kubectl_config_consistency(self, available_providers): - """Test that kubectl configurations are consistent across providers.""" - logger.info("🧪 Testing kubectl configuration consistency") - - # Define required kubectl config structure - required_keys = [ - "apiVersion", - "kind", - "clusters", - "contexts", - "users", - "current-context", - ] - - credential_manager = get_credential_manager() - test_id = int(time.time()) - - for config in available_providers: - logger.info(f"Testing kubectl config structure for {config.name}") - - try: - credentials = credential_manager.ensure_credential(config.name) - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - - # Create a mock cluster info for kubectl config generation - mock_cluster_info = { - "cluster_name": f"test-kubectl-{config.name}-{test_id}", - "endpoint": f"https://test-{config.name}.example.com", - "certificate_authority": "LS0tLS1CRUdJTi...", # Mock cert - "arn": f"arn:aws:eks:us-west-2:123456789012:cluster/test-{config.name}", - "location": config.region, - "fqdn": f"test-{config.name}.example.com", - } - - # Generate kubectl config - kubectl_config = provisioner._configure_kubectl_access( - mock_cluster_info - ) - - # Verify structure - assert isinstance( - kubectl_config, dict - ), f"{config.name} kubectl config should be a dict" - - for key in required_keys: - assert ( - key in kubectl_config - ), f"{config.name} kubectl config missing {key}" - - # Verify specific structures - assert ( - kubectl_config["apiVersion"] == "v1" - ), f"{config.name} should use apiVersion v1" - assert ( - kubectl_config["kind"] == "Config" - ), f"{config.name} should have kind Config" - assert isinstance( - kubectl_config["clusters"], list - ), f"{config.name} clusters should be list" - assert isinstance( - kubectl_config["contexts"], list - ), f"{config.name} contexts should be list" - assert isinstance( - kubectl_config["users"], list - ), f"{config.name} users should be list" - - # Verify non-empty structures - assert ( - len(kubectl_config["clusters"]) > 0 - ), f"{config.name} should have clusters" - assert ( - len(kubectl_config["contexts"]) > 0 - ), f"{config.name} should have contexts" - assert ( - len(kubectl_config["users"]) > 0 - ), f"{config.name} should have users" - - logger.info(f"✅ {config.name} kubectl config structure verified") - - except Exception as e: - logger.error(f"❌ {config.name} kubectl config test failed: {e}") - - logger.info("✅ kubectl configuration consistency testing completed") - - @pytest.mark.performance - def test_scalability_patterns(self, available_providers): - """Test scalability patterns across providers.""" - logger.info("🧪 Testing scalability patterns across providers") - - credential_manager = get_credential_manager() - test_id = int(time.time()) - - # Test different node counts to verify scaling logic - node_count_tests = [1, 2, 4, 8] - - for config in available_providers: - logger.info(f"Testing scalability patterns for {config.name}") - - try: - credentials = credential_manager.ensure_credential(config.name) - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - - scaling_results = {} - - for node_count in node_count_tests: - spec = ClusterSpec( - cluster_name=f"test-scale-{config.name}-{node_count}-{test_id}", - provider=config.name, - node_count=node_count, - kubernetes_version="1.28", - region=config.region, - ) - - # Test resource allocation logic (without actual provisioning) - if hasattr(provisioner, "_map_node_requirements_to_hardware"): - # HuggingFace hardware mapping - hardware = provisioner._map_node_requirements_to_hardware(spec) - scaling_results[node_count] = hardware - elif hasattr( - provisioner, "_map_node_requirements_to_instance_type" - ): - # Lambda Cloud instance type mapping - instance_type = ( - provisioner._map_node_requirements_to_instance_type(spec) - ) - scaling_results[node_count] = instance_type - else: - # Traditional K8s providers - test instance type/size selection - if hasattr(provisioner, "session") and hasattr( - spec, "aws_instance_type" - ): - # AWS EKS - check instance type is appropriate - scaling_results[node_count] = spec.aws_instance_type - elif hasattr(provisioner, "project_id") and hasattr( - spec, "gcp_machine_type" - ): - # GCP GKE - check machine type is appropriate - scaling_results[node_count] = spec.gcp_machine_type - elif hasattr(provisioner, "subscription_id") and hasattr( - spec, "azure_vm_size" - ): - # Azure AKS - check VM size is appropriate - scaling_results[node_count] = spec.azure_vm_size - - # Verify scaling patterns make sense - if scaling_results: - logger.info(f"📊 {config.name} scaling patterns: {scaling_results}") - - # For providers with hardware/instance tiers, verify progression - if config.name == "huggingface": - # Should progress from cpu-basic -> cpu-upgrade -> t4-small -> t4-medium - expected_progression = [ - "cpu-basic", - "cpu-upgrade", - "t4-small", - "t4-medium", - ] - for i, node_count in enumerate(node_count_tests): - if node_count in scaling_results: - expected = expected_progression[ - min(i, len(expected_progression) - 1) - ] - # Allow some flexibility in the progression - assert ( - scaling_results[node_count] in expected_progression - ), f"HF hardware should be in valid progression" - - logger.info(f"✅ {config.name} scalability patterns verified") - - except Exception as e: - logger.error(f"❌ {config.name} scalability pattern test failed: {e}") - - logger.info("✅ Scalability pattern testing completed") diff --git a/tests/real_world/test_kubernetes_performance_benchmarks.py b/tests/real_world/test_kubernetes_performance_benchmarks.py deleted file mode 100644 index f059f33c..00000000 --- a/tests/real_world/test_kubernetes_performance_benchmarks.py +++ /dev/null @@ -1,1063 +0,0 @@ -""" -Performance benchmarking tests for Kubernetes cluster provisioning. - -These tests measure and compare performance metrics across all supported -Kubernetes provisioning providers to ensure: -1. Provisioning times are within acceptable limits -2. Resource utilization is efficient -3. Scaling performance is predictable -4. Cleanup times are reasonable -5. Provider-specific optimizations work correctly - -Requirements: -- Valid credentials for tested providers -- Network connectivity with stable latency -- Sufficient quotas for performance testing -- Clean testing environment for accurate measurements - -Results are logged for performance analysis and regression detection. -""" - -import os -import time -import pytest -import logging -import statistics -import threading -import concurrent.futures -from typing import Dict, Any, List, Optional, Tuple -from dataclasses import dataclass, asdict -import json -import csv -from datetime import datetime, timezone - -from clustrix.kubernetes.cluster_provisioner import ( - KubernetesClusterProvisioner, - ClusterSpec, -) -from clustrix.config import ClusterConfig -from clustrix.credential_manager import get_credential_manager - -logger = logging.getLogger(__name__) - - -@dataclass -class PerformanceMetrics: - """Performance metrics for a single test run.""" - - provider: str - test_name: str - timestamp: str - node_count: int - region: str - - # Timing metrics (seconds) - credential_validation_time: float - provisioning_start_time: float - provisioning_complete_time: float - ready_check_time: float - total_provision_time: float - cleanup_time: float - - # Success metrics - success: bool - error_message: Optional[str] = None - - # Resource metrics - resources_created: int = 0 - resource_types: List[str] = None - - # Performance scores (derived) - provision_score: float = 0.0 # Based on time vs expectations - reliability_score: float = 0.0 # Based on success rate - efficiency_score: float = 0.0 # Based on resources vs time - - def __post_init__(self): - if self.resource_types is None: - self.resource_types = [] - - -@dataclass -class ProviderBenchmarkConfig: - """Benchmark configuration for a provider.""" - - name: str - provisioner_class: str - region: str - expected_provision_time: float # seconds - expected_ready_time: float # seconds - expected_cleanup_time: float # seconds - max_acceptable_time: float # seconds - test fails if exceeded - - -@pytest.mark.real_world -@pytest.mark.performance -class TestKubernetesPerformanceBenchmarks: - """Comprehensive performance benchmarking suite.""" - - @pytest.fixture(scope="class") - def benchmark_configs(self): - """Define benchmark configurations for all providers.""" - return [ - ProviderBenchmarkConfig( - name="aws", - provisioner_class="clustrix.kubernetes.aws_provisioner.AWSEKSFromScratchProvisioner", - region="us-west-2", - expected_provision_time=300.0, # 5 minutes - expected_ready_time=900.0, # 15 minutes - expected_cleanup_time=180.0, # 3 minutes - max_acceptable_time=1800.0, # 30 minutes max - ), - ProviderBenchmarkConfig( - name="gcp", - provisioner_class="clustrix.kubernetes.gcp_provisioner.GCPGKEFromScratchProvisioner", - region="us-central1", - expected_provision_time=240.0, # 4 minutes - expected_ready_time=720.0, # 12 minutes - expected_cleanup_time=120.0, # 2 minutes - max_acceptable_time=1440.0, # 24 minutes max - ), - ProviderBenchmarkConfig( - name="azure", - provisioner_class="clustrix.kubernetes.azure_provisioner.AzureAKSFromScratchProvisioner", - region="eastus", - expected_provision_time=360.0, # 6 minutes - expected_ready_time=1080.0, # 18 minutes - expected_cleanup_time=240.0, # 4 minutes - max_acceptable_time=2160.0, # 36 minutes max - ), - ProviderBenchmarkConfig( - name="huggingface", - provisioner_class="clustrix.kubernetes.huggingface_provisioner.HuggingFaceKubernetesProvisioner", - region="global", - expected_provision_time=60.0, # 1 minute - expected_ready_time=600.0, # 10 minutes - expected_cleanup_time=30.0, # 30 seconds - max_acceptable_time=900.0, # 15 minutes max - ), - ProviderBenchmarkConfig( - name="lambda", - provisioner_class="clustrix.kubernetes.lambda_provisioner.LambdaCloudKubernetesProvisioner", - region="us-west-2", - expected_provision_time=120.0, # 2 minutes - expected_ready_time=300.0, # 5 minutes - expected_cleanup_time=60.0, # 1 minute - max_acceptable_time=600.0, # 10 minutes max - ), - ] - - @pytest.fixture(scope="class") - def performance_results_dir(self): - """Create directory for performance test results.""" - results_dir = "performance_test_results" - os.makedirs(results_dir, exist_ok=True) - return results_dir - - @pytest.fixture(scope="class") - def available_benchmark_providers(self, benchmark_configs): - """Get providers available for benchmarking.""" - available = [] - credential_manager = get_credential_manager() - - for config in benchmark_configs: - try: - credentials = credential_manager.ensure_credential(config.name) - if credentials: - available.append(config) - else: - logger.info( - f"Skipping {config.name} benchmarks - credentials not available" - ) - except Exception: - logger.info(f"Skipping {config.name} benchmarks - credential error") - - if not available: - pytest.skip( - "No provider credentials available for performance benchmarking" - ) - - return available - - def test_credential_validation_performance( - self, available_benchmark_providers, performance_results_dir - ): - """Benchmark credential validation performance across providers.""" - logger.info("🚀 Benchmarking credential validation performance") - - results = [] - credential_manager = get_credential_manager() - - for config in available_benchmark_providers: - logger.info(f"Benchmarking credential validation for {config.name}") - - # Run multiple iterations for statistical accuracy - iteration_times = [] - - for iteration in range(5): # 5 iterations - try: - start_time = time.time() - - credentials = credential_manager.ensure_credential(config.name) - - # Import and create provisioner - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - - provisioner = provisioner_class(credentials, config.region) - validation_result = provisioner.validate_credentials() - - validation_time = time.time() - start_time - iteration_times.append(validation_time) - - if not validation_result: - logger.warning( - f"Credential validation failed for {config.name}" - ) - - except Exception as e: - logger.error(f"Credential validation error for {config.name}: {e}") - iteration_times.append(float("inf")) - - # Brief pause between iterations - time.sleep(1) - - # Calculate statistics - valid_times = [t for t in iteration_times if t != float("inf")] - if valid_times: - avg_time = statistics.mean(valid_times) - median_time = statistics.median(valid_times) - std_dev = statistics.stdev(valid_times) if len(valid_times) > 1 else 0.0 - min_time = min(valid_times) - max_time = max(valid_times) - - result = { - "provider": config.name, - "test": "credential_validation", - "iterations": len(valid_times), - "avg_time": avg_time, - "median_time": median_time, - "std_dev": std_dev, - "min_time": min_time, - "max_time": max_time, - "success_rate": len(valid_times) / len(iteration_times), - } - - results.append(result) - - logger.info(f"📊 {config.name} credential validation:") - logger.info(f" Average: {avg_time:.3f}s") - logger.info(f" Median: {median_time:.3f}s") - logger.info(f" Std Dev: {std_dev:.3f}s") - logger.info(f" Range: {min_time:.3f}s - {max_time:.3f}s") - logger.info(f" Success Rate: {result['success_rate']:.1%}") - - # Save results - self._save_benchmark_results( - results, "credential_validation_benchmark", performance_results_dir - ) - - # Assert reasonable performance expectations - for result in results: - if result["success_rate"] > 0.8: # At least 80% success - assert ( - result["avg_time"] < 10.0 - ), f"{result['provider']} credential validation too slow: {result['avg_time']:.3f}s" - assert ( - result["std_dev"] < result["avg_time"] - ), f"{result['provider']} credential validation too inconsistent" - - logger.info("✅ Credential validation performance benchmarking completed") - - @pytest.mark.slow - def test_single_node_provisioning_performance( - self, available_benchmark_providers, performance_results_dir - ): - """Benchmark single-node cluster provisioning performance.""" - logger.info("🚀 Benchmarking single-node cluster provisioning performance") - - results = [] - test_id = int(time.time()) - credential_manager = get_credential_manager() - - for config in available_benchmark_providers: - logger.info(f"Benchmarking single-node provisioning for {config.name}") - - cluster_spec = ClusterSpec( - cluster_name=f"perf-single-{config.name}-{test_id}", - provider=config.name, - node_count=1, - kubernetes_version="1.28", - region=config.region, - ) - - cluster_info = None - try: - # Get credentials and create provisioner - credentials = credential_manager.ensure_credential(config.name) - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - provisioner = provisioner_class(credentials, config.region) - - # Benchmark credential validation - cred_start = time.time() - provisioner.validate_credentials() - cred_time = time.time() - cred_start - - # Benchmark provisioning - provision_start = time.time() - cluster_info = provisioner.provision_complete_infrastructure( - cluster_spec - ) - provision_time = time.time() - provision_start - - # Benchmark ready state check - ready_start = time.time() - max_wait_time = config.max_acceptable_time - ready = False - - while time.time() - ready_start < max_wait_time: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - if status.get("ready_for_jobs", False): - ready = True - break - time.sleep(30) - - ready_time = time.time() - ready_start - total_time = provision_time + ready_time - - # Count created resources - created_resources = cluster_info.get("created_resources", {}) - resource_count = sum( - len(resources) for resources in created_resources.values() - ) - resource_types = list(created_resources.keys()) - - # Create performance metrics - metrics = PerformanceMetrics( - provider=config.name, - test_name="single_node_provisioning", - timestamp=datetime.now(timezone.utc).isoformat(), - node_count=1, - region=config.region, - credential_validation_time=cred_time, - provisioning_start_time=0.0, # Relative to test start - provisioning_complete_time=provision_time, - ready_check_time=ready_time, - total_provision_time=total_time, - cleanup_time=0.0, # Will be measured below - success=ready, - resources_created=resource_count, - resource_types=resource_types, - ) - - # Calculate performance scores - metrics.provision_score = min( - 100.0, - (config.expected_provision_time / max(provision_time, 1.0)) * 100, - ) - metrics.reliability_score = 100.0 if ready else 0.0 - metrics.efficiency_score = min( - 100.0, (resource_count / max(total_time, 1.0)) * 10 - ) - - # Benchmark cleanup - if cluster_info: - cleanup_start = time.time() - cleanup_success = provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - cleanup_time = time.time() - cleanup_start - metrics.cleanup_time = cleanup_time - cluster_info = None # Prevent double cleanup - - if not cleanup_success: - logger.warning( - f"Cleanup may not have completed successfully for {config.name}" - ) - - results.append(asdict(metrics)) - - # Log results - logger.info(f"📊 {config.name} single-node performance:") - logger.info(f" Credential validation: {cred_time:.1f}s") - logger.info(f" Provisioning: {provision_time:.1f}s") - logger.info(f" Ready check: {ready_time:.1f}s") - logger.info(f" Total: {total_time:.1f}s") - logger.info(f" Cleanup: {cleanup_time:.1f}s") - logger.info(f" Resources created: {resource_count}") - logger.info(f" Success: {'✅' if ready else '❌'}") - logger.info( - f" Performance scores - Provision: {metrics.provision_score:.1f}, Reliability: {metrics.reliability_score:.1f}, Efficiency: {metrics.efficiency_score:.1f}" - ) - - # Assert performance requirements - if ready: - assert ( - total_time <= config.max_acceptable_time - ), f"{config.name} total provisioning time {total_time:.1f}s exceeded maximum {config.max_acceptable_time:.1f}s" - assert ( - cleanup_time <= config.expected_cleanup_time * 2 - ), f"{config.name} cleanup time {cleanup_time:.1f}s too slow" - - except Exception as e: - logger.error(f"Benchmarking failed for {config.name}: {e}") - - # Record failure metrics - metrics = PerformanceMetrics( - provider=config.name, - test_name="single_node_provisioning", - timestamp=datetime.now(timezone.utc).isoformat(), - node_count=1, - region=config.region, - credential_validation_time=0.0, - provisioning_start_time=0.0, - provisioning_complete_time=0.0, - ready_check_time=0.0, - total_provision_time=0.0, - cleanup_time=0.0, - success=False, - error_message=str(e), - ) - results.append(asdict(metrics)) - - finally: - # Ensure cleanup - if cluster_info: - try: - provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - except Exception as cleanup_error: - logger.warning( - f"Final cleanup failed for {config.name}: {cleanup_error}" - ) - - # Save results - self._save_benchmark_results( - results, "single_node_provisioning_benchmark", performance_results_dir - ) - - # Calculate summary statistics - successful_results = [r for r in results if r["success"]] - if successful_results: - avg_provision_time = statistics.mean( - [r["total_provision_time"] for r in successful_results] - ) - logger.info( - f"📊 Overall single-node provisioning average: {avg_provision_time:.1f}s" - ) - - logger.info("✅ Single-node provisioning performance benchmarking completed") - - @pytest.mark.slow - def test_scaling_performance_benchmark( - self, available_benchmark_providers, performance_results_dir - ): - """Benchmark scaling performance across different node counts.""" - logger.info("🚀 Benchmarking scaling performance") - - # Test scaling with different node counts - node_counts = [1, 2, 4] # Limited for practical testing - results = [] - test_id = int(time.time()) - credential_manager = get_credential_manager() - - for config in available_benchmark_providers: - # Skip scaling tests for providers that don't benefit from it - if config.name in [ - "huggingface" - ]: # HF Spaces don't scale in the traditional sense - logger.info(f"Skipping scaling test for {config.name} - not applicable") - continue - - logger.info(f"Benchmarking scaling performance for {config.name}") - - for node_count in node_counts: - logger.info(f"Testing {config.name} with {node_count} nodes") - - cluster_spec = ClusterSpec( - cluster_name=f"perf-scale-{config.name}-{node_count}n-{test_id}", - provider=config.name, - node_count=node_count, - kubernetes_version="1.28", - region=config.region, - ) - - cluster_info = None - try: - # Get credentials and create provisioner - credentials = credential_manager.ensure_credential(config.name) - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - provisioner = provisioner_class(credentials, config.region) - - # Benchmark provisioning with scaling - provision_start = time.time() - cluster_info = provisioner.provision_complete_infrastructure( - cluster_spec - ) - provision_time = time.time() - provision_start - - # Wait for ready state (with timeout based on node count) - ready_start = time.time() - max_wait = config.max_acceptable_time * ( - 1 + 0.5 * (node_count - 1) - ) # Scale timeout - ready = False - - while time.time() - ready_start < max_wait: - status = provisioner.get_cluster_status( - cluster_spec.cluster_name - ) - if status.get("ready_for_jobs", False): - ready = True - break - time.sleep(30) - - ready_time = time.time() - ready_start - total_time = provision_time + ready_time - - # Calculate per-node performance - time_per_node = total_time / node_count - - # Record results - result = { - "provider": config.name, - "test": "scaling_performance", - "node_count": node_count, - "provision_time": provision_time, - "ready_time": ready_time, - "total_time": total_time, - "time_per_node": time_per_node, - "success": ready, - "timestamp": datetime.now(timezone.utc).isoformat(), - } - - results.append(result) - - logger.info(f"📊 {config.name} {node_count}-node scaling:") - logger.info(f" Provision: {provision_time:.1f}s") - logger.info(f" Ready: {ready_time:.1f}s") - logger.info(f" Total: {total_time:.1f}s") - logger.info(f" Per-node: {time_per_node:.1f}s") - logger.info(f" Success: {'✅' if ready else '❌'}") - - # Cleanup - if cluster_info: - cleanup_start = time.time() - provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - cleanup_time = time.time() - cleanup_start - result["cleanup_time"] = cleanup_time - cluster_info = None - - except Exception as e: - logger.error( - f"Scaling test failed for {config.name} {node_count} nodes: {e}" - ) - results.append( - { - "provider": config.name, - "test": "scaling_performance", - "node_count": node_count, - "success": False, - "error": str(e), - "timestamp": datetime.now(timezone.utc).isoformat(), - } - ) - - finally: - if cluster_info: - try: - provisioner.destroy_cluster_infrastructure( - cluster_spec.cluster_name - ) - except Exception: - pass - - # Brief pause between scaling tests - time.sleep(30) - - # Save scaling results - self._save_benchmark_results( - results, "scaling_performance_benchmark", performance_results_dir - ) - - # Analyze scaling efficiency - for config in available_benchmark_providers: - if config.name in ["huggingface"]: - continue - - provider_results = [ - r - for r in results - if r["provider"] == config.name and r.get("success", False) - ] - if len(provider_results) >= 2: - # Check if scaling is efficient (not linear degradation) - provider_results.sort(key=lambda x: x["node_count"]) - - scaling_efficiency = [] - for i in range(1, len(provider_results)): - prev_result = provider_results[i - 1] - curr_result = provider_results[i] - - expected_time = ( - prev_result["time_per_node"] * curr_result["node_count"] - ) - actual_time = curr_result["total_time"] - efficiency = ( - (expected_time / actual_time) * 100 if actual_time > 0 else 0 - ) - - scaling_efficiency.append(efficiency) - - avg_efficiency = ( - statistics.mean(scaling_efficiency) if scaling_efficiency else 0 - ) - logger.info( - f"📊 {config.name} scaling efficiency: {avg_efficiency:.1f}%" - ) - - logger.info("✅ Scaling performance benchmarking completed") - - def test_concurrent_provisioning_performance( - self, available_benchmark_providers, performance_results_dir - ): - """Benchmark concurrent provisioning performance.""" - logger.info("🚀 Benchmarking concurrent provisioning performance") - - # Limit concurrent tests to avoid quota issues - max_concurrent = min(3, len(available_benchmark_providers)) - test_providers = available_benchmark_providers[:max_concurrent] - - if len(test_providers) < 2: - pytest.skip("Need at least 2 providers for concurrent performance testing") - - test_id = int(time.time()) - results = [] - - def provision_cluster_benchmark(config): - """Helper function for concurrent provisioning.""" - try: - credential_manager = get_credential_manager() - credentials = credential_manager.ensure_credential(config.name) - - provisioner_module = __import__( - config.provisioner_class.rsplit(".", 1)[0], - fromlist=[config.provisioner_class.rsplit(".", 1)[1]], - ) - provisioner_class = getattr( - provisioner_module, config.provisioner_class.rsplit(".", 1)[1] - ) - provisioner = provisioner_class(credentials, config.region) - - cluster_spec = ClusterSpec( - cluster_name=f"perf-concurrent-{config.name}-{test_id}", - provider=config.name, - node_count=1, - kubernetes_version="1.28", - region=config.region, - ) - - # Measure provisioning - start_time = time.time() - cluster_info = provisioner.provision_complete_infrastructure( - cluster_spec - ) - provision_time = time.time() - start_time - - # Wait for ready state - ready_start = time.time() - max_wait = config.max_acceptable_time - ready = False - - while time.time() - ready_start < max_wait: - status = provisioner.get_cluster_status(cluster_spec.cluster_name) - if status.get("ready_for_jobs", False): - ready = True - break - time.sleep(15) - - ready_time = time.time() - ready_start - total_time = provision_time + ready_time - - # Cleanup - cleanup_start = time.time() - provisioner.destroy_cluster_infrastructure(cluster_spec.cluster_name) - cleanup_time = time.time() - cleanup_start - - return { - "provider": config.name, - "provision_time": provision_time, - "ready_time": ready_time, - "total_time": total_time, - "cleanup_time": cleanup_time, - "success": ready, - "timestamp": datetime.now(timezone.utc).isoformat(), - } - - except Exception as e: - return { - "provider": config.name, - "success": False, - "error": str(e), - "timestamp": datetime.now(timezone.utc).isoformat(), - } - - # Run concurrent provisioning - logger.info( - f"Running concurrent provisioning for {len(test_providers)} providers" - ) - - start_time = time.time() - with concurrent.futures.ThreadPoolExecutor( - max_workers=len(test_providers) - ) as executor: - futures = [ - executor.submit(provision_cluster_benchmark, config) - for config in test_providers - ] - concurrent_results = [ - future.result( - timeout=max(config.max_acceptable_time for config in test_providers) - + 300 - ) - for future in futures - ] - - concurrent_total_time = time.time() - start_time - - # Analyze concurrent performance - successful_concurrent = [ - r for r in concurrent_results if r.get("success", False) - ] - - concurrent_summary = { - "test": "concurrent_provisioning", - "total_concurrent_time": concurrent_total_time, - "providers_tested": len(test_providers), - "successful_provisions": len(successful_concurrent), - "success_rate": len(successful_concurrent) / len(test_providers), - "results": concurrent_results, - "timestamp": datetime.now(timezone.utc).isoformat(), - } - - results.append(concurrent_summary) - - # Log concurrent results - logger.info(f"📊 Concurrent provisioning performance:") - logger.info(f" Total concurrent time: {concurrent_total_time:.1f}s") - logger.info(f" Success rate: {concurrent_summary['success_rate']:.1%}") - logger.info( - f" Successful provisions: {len(successful_concurrent)}/{len(test_providers)}" - ) - - for result in successful_concurrent: - logger.info(f" {result['provider']}: {result['total_time']:.1f}s total") - - # Compare with sequential expectation - if successful_concurrent: - sequential_expected_time = sum( - next( - config.expected_provision_time + config.expected_ready_time - for config in test_providers - if config.name == r["provider"] - ) - for r in successful_concurrent - ) - - efficiency_gain = ( - (sequential_expected_time / concurrent_total_time) - if concurrent_total_time > 0 - else 0 - ) - logger.info(f" Concurrent efficiency gain: {efficiency_gain:.1f}x") - - concurrent_summary["sequential_expected_time"] = sequential_expected_time - concurrent_summary["efficiency_gain"] = efficiency_gain - - # Save concurrent results - self._save_benchmark_results( - results, "concurrent_provisioning_benchmark", performance_results_dir - ) - - # Assert reasonable concurrent performance - assert ( - concurrent_summary["success_rate"] >= 0.5 - ), f"Concurrent provisioning success rate too low: {concurrent_summary['success_rate']:.1%}" - - logger.info("✅ Concurrent provisioning performance benchmarking completed") - - def test_provider_performance_comparison( - self, available_benchmark_providers, performance_results_dir - ): - """Generate comprehensive performance comparison across providers.""" - logger.info("🚀 Generating provider performance comparison") - - # This test aggregates results from previous benchmarks and generates comparison - comparison_results = [] - - for config in available_benchmark_providers: - logger.info(f"Generating performance profile for {config.name}") - - # Create performance profile - profile = { - "provider": config.name, - "region": config.region, - "expected_provision_time": config.expected_provision_time, - "expected_ready_time": config.expected_ready_time, - "expected_cleanup_time": config.expected_cleanup_time, - "max_acceptable_time": config.max_acceptable_time, - "performance_category": self._categorize_provider_performance(config), - "use_cases": self._get_provider_use_cases(config.name), - "strengths": self._get_provider_strengths(config.name), - "considerations": self._get_provider_considerations(config.name), - "timestamp": datetime.now(timezone.utc).isoformat(), - } - - comparison_results.append(profile) - - # Generate overall comparison - overall_comparison = { - "test": "provider_performance_comparison", - "providers_analyzed": len(available_benchmark_providers), - "fastest_provision": min( - comparison_results, key=lambda x: x["expected_provision_time"] - )["provider"], - "fastest_ready": min( - comparison_results, key=lambda x: x["expected_ready_time"] - )["provider"], - "fastest_cleanup": min( - comparison_results, key=lambda x: x["expected_cleanup_time"] - )["provider"], - "most_reliable": self._get_most_reliable_provider(comparison_results), - "best_for_quick_tasks": self._get_best_for_quick_tasks(comparison_results), - "best_for_production": self._get_best_for_production(comparison_results), - "provider_profiles": comparison_results, - "timestamp": datetime.now(timezone.utc).isoformat(), - } - - # Save comparison results - self._save_benchmark_results( - [overall_comparison], - "provider_performance_comparison", - performance_results_dir, - ) - - # Log comparison summary - logger.info("📊 Provider Performance Comparison Summary:") - logger.info(f" Fastest Provision: {overall_comparison['fastest_provision']}") - logger.info(f" Fastest Ready: {overall_comparison['fastest_ready']}") - logger.info(f" Fastest Cleanup: {overall_comparison['fastest_cleanup']}") - logger.info(f" Most Reliable: {overall_comparison['most_reliable']}") - logger.info( - f" Best for Quick Tasks: {overall_comparison['best_for_quick_tasks']}" - ) - logger.info( - f" Best for Production: {overall_comparison['best_for_production']}" - ) - - logger.info("✅ Provider performance comparison completed") - - def _categorize_provider_performance(self, config: ProviderBenchmarkConfig) -> str: - """Categorize provider performance characteristics.""" - total_expected = config.expected_provision_time + config.expected_ready_time - - if total_expected <= 300: # 5 minutes - return "fast" - elif total_expected <= 900: # 15 minutes - return "medium" - else: - return "slow" - - def _get_provider_use_cases(self, provider_name: str) -> List[str]: - """Get typical use cases for a provider.""" - use_cases = { - "aws": [ - "Production workloads", - "Enterprise applications", - "High availability", - "Auto-scaling", - ], - "gcp": [ - "Machine learning", - "Data analytics", - "Production workloads", - "Microservices", - ], - "azure": [ - "Enterprise applications", - "Hybrid cloud", - "Production workloads", - "Integration with MS ecosystem", - ], - "huggingface": [ - "AI/ML experimentation", - "Model hosting", - "Rapid prototyping", - "Educational use", - ], - "lambda": [ - "GPU workloads", - "Deep learning", - "Scientific computing", - "High-performance computing", - ], - } - return use_cases.get(provider_name, ["General purpose"]) - - def _get_provider_strengths(self, provider_name: str) -> List[str]: - """Get key strengths of a provider.""" - strengths = { - "aws": [ - "Mature ecosystem", - "High reliability", - "Global availability", - "Enterprise features", - ], - "gcp": [ - "Fast provisioning", - "ML/AI tools", - "Cost-effective", - "Modern infrastructure", - ], - "azure": [ - "Enterprise integration", - "Hybrid capabilities", - "Security features", - "Microsoft ecosystem", - ], - "huggingface": [ - "Very fast setup", - "AI/ML focus", - "Easy model deployment", - "Low cost for experimentation", - ], - "lambda": [ - "GPU specialization", - "High performance", - "Cost-effective GPUs", - "Simple API", - ], - } - return strengths.get(provider_name, ["General reliability"]) - - def _get_provider_considerations(self, provider_name: str) -> List[str]: - """Get key considerations for a provider.""" - considerations = { - "aws": ["Complex pricing", "Slow provisioning", "Learning curve"], - "gcp": ["Quota limitations", "Regional availability", "Billing complexity"], - "azure": ["Slow provisioning", "Complex configuration", "Resource naming"], - "huggingface": [ - "Limited compute options", - "Public visibility", - "Resource constraints", - ], - "lambda": ["Limited regions", "GPU availability", "Instance quotas"], - } - return considerations.get(provider_name, ["Standard cloud considerations"]) - - def _get_most_reliable_provider(self, profiles: List[Dict]) -> str: - """Determine most reliable provider based on profiles.""" - # In a real implementation, this would analyze historical success rates - # For now, return based on expected characteristics - enterprise_providers = [ - p for p in profiles if p["provider"] in ["aws", "azure", "gcp"] - ] - if enterprise_providers: - return min( - enterprise_providers, key=lambda x: x["expected_provision_time"] - )["provider"] - return profiles[0]["provider"] if profiles else "unknown" - - def _get_best_for_quick_tasks(self, profiles: List[Dict]) -> str: - """Determine best provider for quick tasks.""" - return min( - profiles, - key=lambda x: x["expected_provision_time"] + x["expected_ready_time"], - )["provider"] - - def _get_best_for_production(self, profiles: List[Dict]) -> str: - """Determine best provider for production workloads.""" - production_providers = [ - p for p in profiles if p["provider"] in ["aws", "gcp", "azure"] - ] - if production_providers: - return min(production_providers, key=lambda x: x["max_acceptable_time"])[ - "provider" - ] - return self._get_most_reliable_provider(profiles) - - def _save_benchmark_results( - self, results: List[Dict], test_name: str, results_dir: str - ): - """Save benchmark results to files.""" - timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") - - # Save as JSON - json_filename = os.path.join(results_dir, f"{test_name}_{timestamp}.json") - with open(json_filename, "w") as f: - json.dump(results, f, indent=2, default=str) - - # Save as CSV if results have consistent structure - if results and isinstance(results[0], dict): - try: - csv_filename = os.path.join(results_dir, f"{test_name}_{timestamp}.csv") - if results: - fieldnames = set() - for result in results: - fieldnames.update(result.keys()) - - with open(csv_filename, "w", newline="") as f: - writer = csv.DictWriter(f, fieldnames=sorted(fieldnames)) - writer.writeheader() - writer.writerows(results) - - except Exception as e: - logger.warning(f"Could not save CSV results: {e}") - - logger.info(f"📁 Benchmark results saved: {json_filename}") - - def test_benchmark_cleanup_verification(self, performance_results_dir): - """Verify all benchmark resources have been cleaned up.""" - logger.info("🧪 Verifying benchmark resource cleanup") - - # This test ensures no resources are left behind from performance tests - # It would check for any clusters, instances, or other resources that might - # still exist from the benchmark tests - - # For now, just verify the results directory exists and has content - assert os.path.exists( - performance_results_dir - ), "Performance results directory should exist" - - result_files = [ - f - for f in os.listdir(performance_results_dir) - if f.endswith((".json", ".csv")) - ] - logger.info(f"📁 Found {len(result_files)} benchmark result files") - - if result_files: - logger.info("📊 Benchmark result files:") - for filename in sorted(result_files)[-5:]: # Show last 5 files - logger.info(f" {filename}") - - logger.info("✅ Benchmark cleanup verification completed") diff --git a/tests/real_world/test_lambda_pricing_real.py b/tests/real_world/test_lambda_pricing_real.py deleted file mode 100644 index bcd7fa48..00000000 --- a/tests/real_world/test_lambda_pricing_real.py +++ /dev/null @@ -1,325 +0,0 @@ -""" -Real-world Lambda Cloud pricing API tests. - -These tests use actual Lambda Cloud API endpoints with real credentials. -NO MOCKS OR SIMULATIONS - these test real Lambda Cloud pricing integration. -""" - -import pytest -import logging -import time -from typing import Dict, Any - -from clustrix.pricing_clients.lambda_pricing import LambdaPricingClient -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor -from tests.real_world.credential_manager import get_lambda_credentials - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestLambdaPricingReal: - """Test real Lambda Cloud pricing API integration.""" - - def setup_method(self): - """Setup for each test method.""" - self.lambda_creds = get_lambda_credentials() - if not self.lambda_creds: - pytest.skip("Lambda Cloud credentials not available") - - self.api_key = self.lambda_creds.get("api_key") - if not self.api_key: - pytest.skip("Lambda Cloud API key not available") - - def test_lambda_pricing_client_authentication_real(self): - """Test Lambda Cloud API authentication with real credentials.""" - client = LambdaPricingClient() - - # Test authentication - auth_result = client.authenticate(self.api_key) - - # Verify authentication - assert auth_result == True - assert client.authenticated == True - assert client.api_key == self.api_key - - logger.info("Lambda Cloud API authentication successful") - - def test_lambda_pricing_api_instance_types_real(self): - """Test real Lambda Cloud API returns valid instance type data.""" - client = LambdaPricingClient() - client.authenticate(self.api_key) - - # Test getting all pricing - all_pricing = client.get_all_pricing() - - # Verify we got some pricing data - assert isinstance(all_pricing, dict) - assert len(all_pricing) > 0 - - logger.info( - f"Retrieved pricing for {len(all_pricing)} Lambda Cloud instance types" - ) - - # Verify pricing data is reasonable - for instance_type, price in all_pricing.items(): - assert isinstance(price, (int, float)) - assert price > 0 - assert price < 100 # Sanity check - should be under $100/hour - logger.debug(f"Lambda Cloud {instance_type}: ${price:.3f}/hour") - - def test_lambda_pricing_specific_instances_real(self): - """Test pricing for specific Lambda Cloud instance types.""" - client = LambdaPricingClient() - client.authenticate(self.api_key) - - # Test common GPU instance types - test_instances = [ - "gpu_1x_a10", - "gpu_1x_a100", - "gpu_1x_h100", - "gpu_2x_a100", - "gpu_4x_a100", - ] - - pricing_results = {} - - for instance_type in test_instances: - price = client.get_instance_pricing(instance_type) - - if price is not None: - pricing_results[instance_type] = price - assert price > 0 - assert price < 50 # Reasonable upper bound - logger.info(f"Lambda Cloud {instance_type}: ${price:.3f}/hour") - else: - logger.warning(f"No pricing found for {instance_type}") - - # Should have found pricing for at least some instances - assert len(pricing_results) > 0 - - # Verify pricing relationships make sense - if "gpu_1x_a100" in pricing_results and "gpu_2x_a100" in pricing_results: - single_a100 = pricing_results["gpu_1x_a100"] - dual_a100 = pricing_results["gpu_2x_a100"] - # Dual GPU should cost more than single GPU - assert dual_a100 > single_a100 - # But not more than 2.5x (due to shared resources) - assert dual_a100 < single_a100 * 2.5 - - def test_lambda_pricing_cache_behavior_real(self): - """Test Lambda Cloud pricing cache behavior with real API.""" - client = LambdaPricingClient(cache_ttl_hours=1) # Short TTL for testing - client.authenticate(self.api_key) - - instance_type = "gpu_1x_a100" - - # First call - should hit API - start_time = time.time() - price1 = client.get_instance_pricing(instance_type) - first_call_time = time.time() - start_time - - # Second call - should hit cache - start_time = time.time() - price2 = client.get_instance_pricing(instance_type) - second_call_time = time.time() - start_time - - # Verify results - assert price1 == price2 # Same pricing - assert second_call_time < first_call_time # Cache should be faster - - logger.info( - f"First API call: {first_call_time:.3f}s, Cached call: {second_call_time:.3f}s" - ) - - def test_lambda_pricing_error_handling_real(self): - """Test Lambda Cloud pricing error handling with real API.""" - client = LambdaPricingClient() - client.authenticate(self.api_key) - - # Test with invalid instance type - invalid_price = client.get_instance_pricing("invalid_gpu_type_xyz") - - # Should return default or None, not crash - if invalid_price is not None: - assert invalid_price > 0 - logger.info( - f"Invalid instance type returned default price: ${invalid_price:.3f}" - ) - else: - logger.info("Invalid instance type correctly returned None") - - def test_lambda_cost_monitor_integration_real(self): - """Test Lambda Cloud cost monitor integration with real API.""" - # Test cost monitor with API integration - monitor = LambdaCostMonitor(use_pricing_api=True, api_key=self.api_key) - - # Test cost estimation - instance_type = "gpu_1x_a100" - hours_used = 2.5 - - cost_estimate = monitor.estimate_cost(instance_type, hours_used) - - # Verify cost estimate - assert cost_estimate is not None - assert cost_estimate.instance_type == instance_type - assert cost_estimate.hours_used == hours_used - assert cost_estimate.hourly_rate > 0 - assert cost_estimate.estimated_cost > 0 - assert cost_estimate.currency == "USD" - - # Should be using API pricing if available - logger.info(f"Cost estimate pricing source: {cost_estimate.pricing_source}") - - # Test pricing info retrieval - pricing_info = monitor.get_pricing_info() - assert isinstance(pricing_info, dict) - assert len(pricing_info) > 0 - - logger.info( - f"Lambda Cloud cost estimate: ${cost_estimate.estimated_cost:.2f} for {hours_used} hours" - ) - - def test_lambda_pricing_vs_hardcoded_comparison(self): - """Compare Lambda Cloud API pricing vs hardcoded pricing.""" - # Get API pricing - api_client = LambdaPricingClient() - api_client.authenticate(self.api_key) - api_pricing = api_client.get_all_pricing() - - # Get hardcoded pricing - hardcoded_client = LambdaPricingClient() - hardcoded_pricing = hardcoded_client._hardcoded_pricing - - # Compare common instance types - common_instances = set(api_pricing.keys()) & set(hardcoded_pricing.keys()) - - pricing_comparison = [] - - for instance_type in common_instances: - api_price = api_pricing[instance_type] - hardcoded_price = hardcoded_pricing[instance_type] - - # Calculate percentage difference - diff_percent = abs(api_price - hardcoded_price) / hardcoded_price * 100 - - pricing_comparison.append( - { - "instance_type": instance_type, - "api_price": api_price, - "hardcoded_price": hardcoded_price, - "difference_percent": diff_percent, - } - ) - - logger.info( - f"{instance_type}: API ${api_price:.3f} vs Hardcoded ${hardcoded_price:.3f} ({diff_percent:.1f}% diff)" - ) - - # Verify pricing differences are reasonable - large_differences = [ - p for p in pricing_comparison if p["difference_percent"] > 50 - ] - - if large_differences: - logger.warning( - f"Found {len(large_differences)} instances with >50% pricing differences" - ) - for diff in large_differences: - logger.warning( - f" {diff['instance_type']}: {diff['difference_percent']:.1f}% difference" - ) - - # Should have some pricing comparisons - assert len(pricing_comparison) > 0 - - def test_lambda_pricing_regional_consistency(self): - """Test Lambda Cloud pricing consistency across regions.""" - client = LambdaPricingClient() - client.authenticate(self.api_key) - - regions_to_test = ["us-east-1", "us-west-2"] - instance_type = "gpu_1x_a100" - - regional_pricing = {} - - for region in regions_to_test: - try: - price = client.get_instance_pricing(instance_type, region) - if price is not None: - regional_pricing[region] = price - logger.info( - f"Lambda Cloud {instance_type} in {region}: ${price:.3f}/hour" - ) - except Exception as e: - logger.warning(f"Error getting pricing for {region}: {e}") - - # Verify we got some regional pricing - if len(regional_pricing) > 1: - # Check if regional prices are reasonably consistent - prices = list(regional_pricing.values()) - max_price = max(prices) - min_price = min(prices) - price_variance = (max_price - min_price) / min_price * 100 - - logger.info( - f"Regional price variance for {instance_type}: {price_variance:.1f}%" - ) - - # Lambda Cloud pricing should be fairly consistent across regions - assert price_variance < 20 # Allow up to 20% regional variation - - def test_lambda_pricing_api_performance(self): - """Test Lambda Cloud pricing API performance.""" - client = LambdaPricingClient() - client.authenticate(self.api_key) - - # Test API response time - start_time = time.time() - all_pricing = client.get_all_pricing() - api_response_time = time.time() - start_time - - # Verify performance - assert api_response_time < 10.0 # Should respond within 10 seconds - assert len(all_pricing) > 0 - - logger.info( - f"Lambda Cloud pricing API response time: {api_response_time:.3f} seconds" - ) - - # Test individual instance pricing performance - instance_type = "gpu_1x_a100" - start_time = time.time() - price = client.get_instance_pricing(instance_type) - single_response_time = time.time() - start_time - - assert single_response_time < 5.0 # Should respond within 5 seconds - assert price is not None - - logger.info( - f"Single instance pricing response time: {single_response_time:.3f} seconds" - ) - - def test_lambda_pricing_client_info(self): - """Test Lambda Cloud pricing client information.""" - client = LambdaPricingClient() - client.authenticate(self.api_key) - - pricing_info = client.get_pricing_info() - - # Verify pricing info structure - assert isinstance(pricing_info, dict) - assert pricing_info["provider"] == "lambda" - assert pricing_info["authenticated"] == True - assert pricing_info["api_available"] == True - assert "fallback_pricing_date" in pricing_info - assert "cache_ttl_hours" in pricing_info - assert "supported_regions" in pricing_info - assert "instance_count" in pricing_info - - logger.info(f"Lambda Cloud pricing client info: {pricing_info}") - - def teardown_method(self): - """Cleanup after each test.""" - # No cleanup needed for pricing tests - pass diff --git a/tests/test_auto_install.py b/tests/test_auto_install.py deleted file mode 100644 index 554079de..00000000 --- a/tests/test_auto_install.py +++ /dev/null @@ -1,422 +0,0 @@ -"""Tests for automatic dependency installation functionality.""" - -import subprocess -import sys -from unittest.mock import patch, Mock, MagicMock -import pytest - -from clustrix.auto_install import ( - check_dependencies_installed, - install_provider_dependencies, - ensure_cloud_provider_dependencies, - get_installation_command, - CLOUD_PROVIDER_DEPS, - CLUSTER_TYPE_TO_PROVIDER, -) - - -class TestCheckDependenciesInstalled: - """Test dependency checking functionality.""" - - def test_unknown_provider_returns_true(self): - """Test that unknown providers return True (no special deps needed).""" - result = check_dependencies_installed("unknown_provider") - assert result is True - - def test_aws_dependencies_available(self): - """Test AWS dependency checking when boto3 is available.""" - with patch("builtins.__import__") as mock_import: - # Mock successful import - mock_import.return_value = MagicMock() - result = check_dependencies_installed("aws") - assert result is True - - def test_aws_dependencies_missing(self): - """Test AWS dependency checking when boto3 is missing.""" - with patch("builtins.__import__") as mock_import: - # Mock ImportError - mock_import.side_effect = ImportError("No module named 'boto3'") - result = check_dependencies_installed("aws") - assert result is False - - def test_azure_dependencies_available(self): - """Test Azure dependency checking when modules are available.""" - with patch("builtins.__import__") as mock_import: - mock_import.return_value = MagicMock() - result = check_dependencies_installed("azure") - assert result is True - - def test_azure_dependencies_missing(self): - """Test Azure dependency checking when modules are missing.""" - with patch("builtins.__import__") as mock_import: - mock_import.side_effect = ImportError("No module named 'azure'") - result = check_dependencies_installed("azure") - assert result is False - - def test_gcp_dependencies_available(self): - """Test GCP dependency checking when modules are available.""" - with patch("builtins.__import__") as mock_import: - mock_import.return_value = MagicMock() - result = check_dependencies_installed("gcp") - assert result is True - - def test_gcp_dependencies_missing(self): - """Test GCP dependency checking when modules are missing.""" - with patch("builtins.__import__") as mock_import: - mock_import.side_effect = ImportError("No module named 'google'") - result = check_dependencies_installed("gcp") - assert result is False - - def test_kubernetes_dependencies_available(self): - """Test Kubernetes dependency checking when available.""" - with patch("builtins.__import__") as mock_import: - mock_import.return_value = MagicMock() - result = check_dependencies_installed("kubernetes") - assert result is True - - def test_kubernetes_dependencies_missing(self): - """Test Kubernetes dependency checking when missing.""" - with patch("builtins.__import__") as mock_import: - mock_import.side_effect = ImportError("No module named 'kubernetes'") - result = check_dependencies_installed("kubernetes") - assert result is False - - -class TestInstallProviderDependencies: - """Test dependency installation functionality.""" - - def test_unknown_provider_returns_true(self): - """Test that unknown providers return True.""" - result = install_provider_dependencies("unknown_provider") - assert result is True - - @patch("clustrix.auto_install.check_dependencies_installed") - def test_already_installed_returns_true(self, mock_check): - """Test that already installed dependencies return True.""" - mock_check.return_value = True - result = install_provider_dependencies("aws") - assert result is True - mock_check.assert_called_once_with("aws") - - @patch("clustrix.auto_install.check_dependencies_installed") - @patch("clustrix.auto_install.logger") - def test_auto_install_false_with_warning(self, mock_logger, mock_check): - """Test that auto_install=False shows warning and returns False.""" - mock_check.return_value = False - - result = install_provider_dependencies("aws", auto_install=False, quiet=False) - - assert result is False - mock_logger.warning.assert_called_once() - warning_call = mock_logger.warning.call_args[0][0] - assert "Missing dependencies for aws provider" in warning_call - assert "pip install" in warning_call - - @patch("clustrix.auto_install.check_dependencies_installed") - def test_auto_install_false_quiet_no_warning(self, mock_check): - """Test that auto_install=False with quiet=True doesn't show warning.""" - mock_check.return_value = False - - with patch("clustrix.auto_install.logger") as mock_logger: - result = install_provider_dependencies( - "aws", auto_install=False, quiet=True - ) - - assert result is False - mock_logger.warning.assert_not_called() - - @patch("clustrix.auto_install.check_dependencies_installed") - @patch("clustrix.auto_install.subprocess.run") - @patch("clustrix.auto_install.logger") - def test_successful_installation(self, mock_logger, mock_subprocess, mock_check): - """Test successful dependency installation.""" - mock_check.return_value = False - mock_subprocess.return_value = Mock() - - result = install_provider_dependencies("aws", auto_install=True, quiet=False) - - assert result is True - mock_subprocess.assert_called_once() - - # Check that the subprocess was called with correct arguments - call_args = mock_subprocess.call_args[0][0] - assert call_args[0] == sys.executable - assert call_args[1:3] == ["-m", "pip"] - assert call_args[3] == "install" - assert "boto3>=1.26.0" in call_args - assert "kubernetes>=20.13.0" in call_args - - # Check logging - mock_logger.info.assert_called() - info_calls = [call[0][0] for call in mock_logger.info.call_args_list] - assert any("Installing aws dependencies" in msg for msg in info_calls) - assert any( - "Successfully installed aws dependencies" in msg for msg in info_calls - ) - - @patch("clustrix.auto_install.check_dependencies_installed") - @patch("clustrix.auto_install.subprocess.run") - @patch("clustrix.auto_install.logger") - def test_successful_installation_quiet( - self, mock_logger, mock_subprocess, mock_check - ): - """Test successful dependency installation in quiet mode.""" - mock_check.return_value = False - mock_subprocess.return_value = Mock() - - result = install_provider_dependencies("aws", auto_install=True, quiet=True) - - assert result is True - - # Check that --quiet was added to command - call_args = mock_subprocess.call_args[0][0] - assert "--quiet" in call_args - - # Check no logging occurred - mock_logger.info.assert_not_called() - - @patch("clustrix.auto_install.check_dependencies_installed") - @patch("clustrix.auto_install.subprocess.run") - @patch("clustrix.auto_install.logger") - def test_subprocess_called_process_error( - self, mock_logger, mock_subprocess, mock_check - ): - """Test handling of subprocess.CalledProcessError.""" - mock_check.return_value = False - error = subprocess.CalledProcessError(1, "pip", stderr="Installation failed") - mock_subprocess.side_effect = error - - result = install_provider_dependencies("aws", auto_install=True, quiet=False) - - assert result is False - mock_logger.error.assert_called() - error_calls = [call[0][0] for call in mock_logger.error.call_args_list] - assert any("Failed to install aws dependencies" in msg for msg in error_calls) - assert any("Error output: Installation failed" in msg for msg in error_calls) - - @patch("clustrix.auto_install.check_dependencies_installed") - @patch("clustrix.auto_install.subprocess.run") - @patch("clustrix.auto_install.logger") - def test_subprocess_called_process_error_quiet( - self, mock_logger, mock_subprocess, mock_check - ): - """Test handling of subprocess.CalledProcessError in quiet mode.""" - mock_check.return_value = False - error = subprocess.CalledProcessError(1, "pip") - mock_subprocess.side_effect = error - - result = install_provider_dependencies("aws", auto_install=True, quiet=True) - - assert result is False - mock_logger.error.assert_not_called() - - @patch("clustrix.auto_install.check_dependencies_installed") - @patch("clustrix.auto_install.subprocess.run") - @patch("clustrix.auto_install.logger") - def test_generic_exception_handling(self, mock_logger, mock_subprocess, mock_check): - """Test handling of generic exceptions.""" - mock_check.return_value = False - mock_subprocess.side_effect = Exception("Unexpected error") - - result = install_provider_dependencies("aws", auto_install=True, quiet=False) - - assert result is False - mock_logger.error.assert_called_once() - error_call = mock_logger.error.call_args[0][0] - assert "Unexpected error installing aws dependencies" in error_call - - @patch("clustrix.auto_install.check_dependencies_installed") - @patch("clustrix.auto_install.subprocess.run") - def test_generic_exception_handling_quiet(self, mock_subprocess, mock_check): - """Test handling of generic exceptions in quiet mode.""" - mock_check.return_value = False - mock_subprocess.side_effect = Exception("Unexpected error") - - with patch("clustrix.auto_install.logger") as mock_logger: - result = install_provider_dependencies("aws", auto_install=True, quiet=True) - - assert result is False - mock_logger.error.assert_not_called() - - -class TestEnsureCloudProviderDependencies: - """Test the ensure cloud provider dependencies function.""" - - @patch("clustrix.auto_install.install_provider_dependencies") - def test_cloud_provider_specified(self, mock_install): - """Test with cloud_provider specified.""" - mock_install.return_value = True - - result = ensure_cloud_provider_dependencies( - cloud_provider="aws", auto_install=True, quiet=False - ) - - assert result is True - mock_install.assert_called_once_with("aws", auto_install=True, quiet=False) - - @patch("clustrix.auto_install.install_provider_dependencies") - def test_cloud_provider_manual_ignored(self, mock_install): - """Test that manual cloud provider is ignored.""" - result = ensure_cloud_provider_dependencies(cloud_provider="manual") - - assert result is True - mock_install.assert_not_called() - - @patch("clustrix.auto_install.install_provider_dependencies") - def test_cluster_type_mapping(self, mock_install): - """Test cluster type to provider mapping.""" - mock_install.return_value = True - - result = ensure_cloud_provider_dependencies(cluster_type="aws_ec2") - - assert result is True - mock_install.assert_called_once_with("aws", auto_install=True, quiet=False) - - @patch("clustrix.auto_install.install_provider_dependencies") - def test_unknown_cluster_type(self, mock_install): - """Test with unknown cluster type.""" - result = ensure_cloud_provider_dependencies(cluster_type="unknown") - - assert result is True - mock_install.assert_not_called() - - @patch("clustrix.auto_install.install_provider_dependencies") - def test_no_provider_needed(self, mock_install): - """Test when no provider is needed.""" - result = ensure_cloud_provider_dependencies() - - assert result is True - mock_install.assert_not_called() - - @patch("clustrix.auto_install.install_provider_dependencies") - def test_cloud_provider_overrides_cluster_type(self, mock_install): - """Test that cloud_provider takes precedence over cluster_type.""" - mock_install.return_value = True - - result = ensure_cloud_provider_dependencies( - cluster_type="aws_ec2", cloud_provider="gcp" - ) - - assert result is True - mock_install.assert_called_once_with("gcp", auto_install=True, quiet=False) - - -class TestGetInstallationCommand: - """Test the get installation command function.""" - - def test_cloud_provider_specified(self): - """Test with cloud_provider specified.""" - result = get_installation_command(cloud_provider="aws") - - expected_deps = CLOUD_PROVIDER_DEPS["aws"] - expected = f"pip install {' '.join(expected_deps)}" - assert result == expected - - def test_cloud_provider_manual_returns_none(self): - """Test that manual cloud provider returns None.""" - result = get_installation_command(cloud_provider="manual") - assert result is None - - def test_cluster_type_mapping(self): - """Test cluster type to provider mapping.""" - result = get_installation_command(cluster_type="azure_aks") - - expected_deps = CLOUD_PROVIDER_DEPS["azure"] - expected = f"pip install {' '.join(expected_deps)}" - assert result == expected - - def test_unknown_cluster_type_returns_none(self): - """Test with unknown cluster type.""" - result = get_installation_command(cluster_type="unknown") - assert result is None - - def test_no_provider_returns_none(self): - """Test when no provider is specified.""" - result = get_installation_command() - assert result is None - - def test_cloud_provider_overrides_cluster_type(self): - """Test that cloud_provider takes precedence over cluster_type.""" - result = get_installation_command(cluster_type="aws_ec2", cloud_provider="gcp") - - expected_deps = CLOUD_PROVIDER_DEPS["gcp"] - expected = f"pip install {' '.join(expected_deps)}" - assert result == expected - - def test_unknown_provider_returns_none(self): - """Test with unknown provider returns None.""" - result = get_installation_command(cloud_provider="unknown_provider") - assert result is None - - -class TestConstants: - """Test the module constants and mappings.""" - - def test_cloud_provider_deps_structure(self): - """Test that CLOUD_PROVIDER_DEPS has expected structure.""" - expected_providers = { - "aws", - "azure", - "gcp", - "kubernetes", - "lambda_cloud", - "huggingface_spaces", - } - assert set(CLOUD_PROVIDER_DEPS.keys()) == expected_providers - - # Ensure all values are lists of strings - for provider, deps in CLOUD_PROVIDER_DEPS.items(): - assert isinstance(deps, list) - assert all(isinstance(dep, str) for dep in deps) - assert all(">=" in dep or "==" in dep for dep in deps) # Version specs - - def test_cluster_type_to_provider_mapping(self): - """Test that cluster type mappings are valid.""" - expected_mappings = { - "kubernetes": "kubernetes", - "aws_ec2": "aws", - "aws_eks": "aws", - "azure_vm": "azure", - "azure_aks": "azure", - "gcp_vm": "gcp", - "gcp_gke": "gcp", - "lambda_cloud": "lambda_cloud", - "huggingface_spaces": "huggingface_spaces", - } - - assert CLUSTER_TYPE_TO_PROVIDER == expected_mappings - - # Ensure all mapped providers exist in CLOUD_PROVIDER_DEPS - for provider in CLUSTER_TYPE_TO_PROVIDER.values(): - assert provider in CLOUD_PROVIDER_DEPS - - -class TestIntegrationScenarios: - """Test integration scenarios combining multiple functions.""" - - @patch("clustrix.auto_install.subprocess.run") - def test_full_workflow_aws_missing_deps(self, mock_subprocess): - """Test full workflow when AWS dependencies are missing.""" - mock_subprocess.return_value = Mock() - - with patch("builtins.__import__") as mock_import: - # First call (check) fails, second call (after install) succeeds - mock_import.side_effect = [ - ImportError("No module named 'boto3'"), # First check - MagicMock(), # After installation - ] - - # Test the workflow - result = ensure_cloud_provider_dependencies(cluster_type="aws_ec2") - - assert result is True - mock_subprocess.assert_called_once() - - def test_installation_command_matches_dependencies(self): - """Test that installation commands match actual dependencies.""" - for provider in CLOUD_PROVIDER_DEPS: - command = get_installation_command(cloud_provider=provider) - expected_deps = CLOUD_PROVIDER_DEPS[provider] - - for dep in expected_deps: - assert dep in command diff --git a/tests/test_aws_cost_provider_pricing.py b/tests/test_aws_cost_provider_pricing.py deleted file mode 100644 index 454e21e3..00000000 --- a/tests/test_aws_cost_provider_pricing.py +++ /dev/null @@ -1,158 +0,0 @@ -"""Tests for AWS cost provider pricing functionality.""" - -import pytest -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime - -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_monitoring import CostEstimate - - -class TestAWSCostMonitorPricing: - """Test AWS cost monitor pricing functionality.""" - - def test_init_with_pricing_api(self): - """Test initialization with pricing API enabled.""" - monitor = AWSCostMonitor(region="us-west-2", use_pricing_api=True) - - assert monitor.region == "us-west-2" - assert monitor.use_pricing_api is True - assert monitor.pricing_client is not None - - def test_init_without_pricing_api(self): - """Test initialization with pricing API disabled.""" - monitor = AWSCostMonitor(region="eu-west-1", use_pricing_api=False) - - assert monitor.region == "eu-west-1" - assert monitor.use_pricing_api is False - assert monitor.pricing_client is None - - @patch("clustrix.cost_providers.aws.AWSPricingClient") - def test_estimate_cost_with_api_success(self, mock_pricing_client_class): - """Test cost estimation with successful API call.""" - # Set up mock pricing client - mock_pricing_client = Mock() - mock_pricing_client.get_instance_pricing.return_value = 0.0150 - mock_pricing_client.is_pricing_data_outdated.return_value = False - mock_pricing_client_class.return_value = mock_pricing_client - - monitor = AWSCostMonitor(region="us-east-1", use_pricing_api=True) - - # Estimate cost - estimate = monitor.estimate_cost("t3.micro", hours_used=2.5) - - assert estimate.instance_type == "t3.micro (On-Demand)" - assert estimate.hourly_rate == 0.0150 - assert estimate.hours_used == 2.5 - assert estimate.estimated_cost == 0.0375 - assert estimate.pricing_source == "api" - assert estimate.pricing_warning is None - - # Verify API was called - mock_pricing_client.get_instance_pricing.assert_called_once_with( - instance_type="t3.micro", region="us-east-1" - ) - - @patch("clustrix.cost_providers.aws.AWSPricingClient") - def test_estimate_cost_with_api_failure(self, mock_pricing_client_class): - """Test cost estimation falling back to hardcoded when API fails.""" - # Set up mock pricing client that returns None - mock_pricing_client = Mock() - mock_pricing_client.get_instance_pricing.return_value = None - mock_pricing_client.is_pricing_data_outdated.return_value = True - mock_pricing_client._hardcoded_pricing_date = "2025-01-01" - mock_pricing_client_class.return_value = mock_pricing_client - - monitor = AWSCostMonitor(region="us-east-1", use_pricing_api=True) - - # Estimate cost - estimate = monitor.estimate_cost("t3.micro", hours_used=1.0) - - assert estimate.instance_type == "t3.micro (On-Demand)" - assert estimate.hourly_rate == 0.0104 # Hardcoded price - assert estimate.hours_used == 1.0 - assert estimate.estimated_cost == 0.0104 - assert estimate.pricing_source == "hardcoded" - assert "potentially outdated pricing data" in estimate.pricing_warning - - def test_estimate_cost_spot_pricing(self): - """Test spot instance cost estimation.""" - monitor = AWSCostMonitor(use_pricing_api=False) - - # Estimate spot cost - estimate = monitor.estimate_cost("c5.large", hours_used=10.0, use_spot=True) - - assert estimate.instance_type == "c5.large (Spot)" - assert estimate.hourly_rate == pytest.approx( - 0.085 * 0.65, rel=1e-4 - ) # 35% discount - assert estimate.hours_used == 10.0 - assert estimate.pricing_source == "hardcoded" - - # Check spot pricing warning - if estimate.pricing_warning: - assert "Spot pricing is estimated" in estimate.pricing_warning - - def test_estimate_cost_unknown_instance(self): - """Test cost estimation for unknown instance type.""" - monitor = AWSCostMonitor(use_pricing_api=False) - - estimate = monitor.estimate_cost("unknown.xlarge", hours_used=1.0) - - assert estimate.instance_type == "unknown.xlarge (On-Demand)" - assert estimate.hourly_rate == 0.10 # Default price - assert estimate.pricing_source == "hardcoded" - - @patch("clustrix.cost_providers.aws.AWSPricingClient") - def test_get_pricing_info_with_warning(self, mock_pricing_client_class): - """Test getting pricing info with outdated data warning.""" - # Set up mock pricing client - mock_pricing_client = Mock() - mock_pricing_client.is_pricing_data_outdated.return_value = True - mock_pricing_client_class.return_value = mock_pricing_client - - monitor = AWSCostMonitor(use_pricing_api=True) - - with patch("clustrix.cost_providers.aws.logger") as mock_logger: - pricing_info = monitor.get_pricing_info() - - # Verify warning was logged - mock_logger.warning.assert_called_once() - warning_msg = mock_logger.warning.call_args[0][0] - assert "outdated" in warning_msg - - assert isinstance(pricing_info, dict) - assert "t3.micro" in pricing_info - - def test_estimate_cost_with_different_regions(self): - """Test cost estimation respects region parameter.""" - monitor_east = AWSCostMonitor(region="us-east-1", use_pricing_api=False) - monitor_west = AWSCostMonitor(region="us-west-2", use_pricing_api=False) - - # Both should use same hardcoded pricing for now - estimate_east = monitor_east.estimate_cost("m5.large", hours_used=1.0) - estimate_west = monitor_west.estimate_cost("m5.large", hours_used=1.0) - - assert estimate_east.hourly_rate == estimate_west.hourly_rate - assert estimate_east.pricing_source == "hardcoded" - assert estimate_west.pricing_source == "hardcoded" - - def test_cost_estimate_fields(self): - """Test all CostEstimate fields are properly set.""" - monitor = AWSCostMonitor(use_pricing_api=False) - - estimate = monitor.estimate_cost("p3.2xlarge", hours_used=24.0) - - # Check all fields - assert estimate.instance_type == "p3.2xlarge (On-Demand)" - assert estimate.hourly_rate == 3.06 - assert estimate.hours_used == 24.0 - assert estimate.estimated_cost == 73.44 - assert estimate.currency == "USD" - assert isinstance(estimate.last_updated, datetime) - assert estimate.pricing_source == "hardcoded" - assert hasattr(estimate, "pricing_warning") - - -if __name__ == "__main__": - pytest.main([__file__, "-v"]) diff --git a/tests/test_aws_pricing_integration.py b/tests/test_aws_pricing_integration.py deleted file mode 100644 index 9421245f..00000000 --- a/tests/test_aws_pricing_integration.py +++ /dev/null @@ -1,202 +0,0 @@ -"""Integration tests for AWS pricing API - these require AWS credentials and internet access.""" - -import pytest -import os -from unittest.mock import patch -import logging - -from clustrix.pricing_clients.aws_pricing import AWSPricingClient - - -@pytest.mark.integration -class TestAWSPricingIntegration: - """Integration tests that actually call the AWS API.""" - - def test_real_aws_pricing_api_call(self): - """Test that we can actually fetch pricing from AWS (requires credentials).""" - # Skip if no AWS credentials available - if not self._has_aws_credentials(): - pytest.skip("AWS credentials not available") - - client = AWSPricingClient() - - # Try to get pricing for a common instance type - try: - price = client.get_instance_pricing( - instance_type="t2.micro", region="us-east-1", operating_system="Linux" - ) - - # If we get a price, it should be a positive float - if price is not None: - assert isinstance(price, float) - assert price > 0 - assert price < 1.0 # t2.micro should be less than $1/hour - print(f"✅ Successfully fetched t2.micro pricing: ${price}/hr") - else: - print("⚠️ API returned None - might be credentials issue") - - except Exception as e: - print(f"⚠️ AWS API call failed: {e}") - # Don't fail the test - this could be due to network, credentials, etc. - - def test_aws_pricing_api_without_credentials(self): - """Test that the client handles missing credentials gracefully.""" - # Temporarily remove AWS credentials - original_env = {} - aws_env_vars = [ - "AWS_ACCESS_KEY_ID", - "AWS_SECRET_ACCESS_KEY", - "AWS_SESSION_TOKEN", - "AWS_PROFILE", - "AWS_DEFAULT_PROFILE", - ] - - # Save and remove AWS environment variables - for var in aws_env_vars: - if var in os.environ: - original_env[var] = os.environ[var] - del os.environ[var] - - try: - client = AWSPricingClient() - - # This should not raise an exception, just return None/fallback - price = client.get_instance_pricing("t2.micro", "us-east-1") - - # Should fall back to hardcoded pricing - assert price == 0.0116 # Hardcoded t2.micro price - - finally: - # Restore environment variables - for var, value in original_env.items(): - os.environ[var] = value - - def test_aws_pricing_api_error_handling(self): - """Test that the client handles API errors gracefully.""" - client = AWSPricingClient() - - # Test with invalid region - should fall back gracefully - price = client.get_instance_pricing( - instance_type="t2.micro", region="invalid-region-123" - ) - - # Should either return a valid price or fall back to hardcoded - if price is not None: - assert isinstance(price, float) - assert price > 0 - - def test_cache_behavior_with_real_api(self): - """Test that caching works with real API calls.""" - if not self._has_aws_credentials(): - pytest.skip("AWS credentials not available") - - client = AWSPricingClient(cache_ttl_hours=1) - - # Make the same call twice - second should be faster due to caching - import time - - start_time = time.time() - price1 = client.get_instance_pricing("t2.micro", "us-east-1") - first_call_time = time.time() - start_time - - start_time = time.time() - price2 = client.get_instance_pricing("t2.micro", "us-east-1") - second_call_time = time.time() - start_time - - # Prices should be the same - if price1 is not None and price2 is not None: - assert price1 == price2 - # Second call should be faster (cached) - assert second_call_time < first_call_time - print( - f"✅ Cache working: first call {first_call_time:.3f}s, second call {second_call_time:.3f}s" - ) - - def test_different_regions_return_different_prices(self): - """Test that different regions can return different prices.""" - if not self._has_aws_credentials(): - pytest.skip("AWS credentials not available") - - client = AWSPricingClient() - - # Test a few different regions - regions = ["us-east-1", "us-west-2", "eu-west-1"] - prices = {} - - for region in regions: - try: - price = client.get_instance_pricing("m5.large", region) - if price is not None: - prices[region] = price - print(f"m5.large in {region}: ${price}/hr") - except Exception as e: - print(f"Failed to get pricing for {region}: {e}") - - # If we got multiple prices, they might be different - if len(prices) > 1: - price_values = list(prices.values()) - # All prices should be positive - for price in price_values: - assert price > 0 - assert price < 10.0 # Sanity check - - def test_aws_pricing_with_different_os(self): - """Test pricing differences between operating systems.""" - if not self._has_aws_credentials(): - pytest.skip("AWS credentials not available") - - client = AWSPricingClient() - - operating_systems = ["Linux", "Windows"] - prices = {} - - for os_type in operating_systems: - try: - price = client.get_instance_pricing( - instance_type="m5.large", - region="us-east-1", - operating_system=os_type, - ) - if price is not None: - prices[os_type] = price - print(f"m5.large with {os_type}: ${price}/hr") - except Exception as e: - print(f"Failed to get pricing for {os_type}: {e}") - - # Windows should typically be more expensive than Linux - if "Linux" in prices and "Windows" in prices: - assert prices["Windows"] > prices["Linux"] - print( - f"✅ Windows pricing (${prices['Windows']}) > Linux pricing (${prices['Linux']})" - ) - - def _has_aws_credentials(self) -> bool: - """Check if AWS credentials are available.""" - try: - import boto3 - - # Try to create a session - this will check for credentials - session = boto3.Session() - credentials = session.get_credentials() - return credentials is not None - except Exception: - return False - - def test_fallback_mechanism_integration(self): - """Test the complete fallback mechanism from API to hardcoded.""" - client = AWSPricingClient() - - # This should work regardless of credentials - either from API or fallback - price = client.get_instance_pricing("t2.micro", "us-east-1") - - assert price is not None - assert isinstance(price, float) - assert price > 0 - - # Should be reasonable for t2.micro (between $0.005 and $0.05) - assert 0.005 <= price <= 0.05 - - -if __name__ == "__main__": - # Run with: python -m pytest tests/test_aws_pricing_integration.py -v -m integration - pytest.main([__file__, "-v", "-m", "integration", "-s"]) diff --git a/tests/test_azure_cost_provider.py b/tests/test_azure_cost_provider.py deleted file mode 100644 index 62971c7a..00000000 --- a/tests/test_azure_cost_provider.py +++ /dev/null @@ -1,411 +0,0 @@ -"""Comprehensive tests for Azure cost provider.""" - -import logging -from unittest.mock import Mock, patch, MagicMock -import pytest - -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_monitoring import ResourceUsage, CostEstimate - - -class TestAzureCostMonitor: - """Test AzureCostMonitor class.""" - - def test_init_with_pricing_api(self): - """Test initialization with pricing API enabled.""" - monitor = AzureCostMonitor(region="eastus", use_pricing_api=True) - - assert monitor.region == "eastus" - assert monitor.use_pricing_api is True - assert monitor.pricing_client is not None - assert monitor.provider_name == "Azure" - assert isinstance(monitor.vm_pricing, dict) - assert "Standard_B1s" in monitor.vm_pricing - - def test_init_without_pricing_api(self): - """Test initialization with pricing API disabled.""" - monitor = AzureCostMonitor(region="westus", use_pricing_api=False) - - assert monitor.region == "westus" - assert monitor.use_pricing_api is False - assert monitor.pricing_client is None - - def test_init_default_values(self): - """Test initialization with default values.""" - monitor = AzureCostMonitor() - - assert monitor.region == "eastus" - assert monitor.use_pricing_api is True - - @patch("clustrix.cost_monitoring.BaseCostMonitor.get_cpu_memory_usage") - @patch("clustrix.cost_monitoring.BaseCostMonitor.get_gpu_utilization") - def test_get_resource_usage(self, mock_gpu, mock_cpu_mem): - """Test getting current resource usage.""" - monitor = AzureCostMonitor() - - # Mock CPU and memory usage - mock_cpu_mem.return_value = (75.5, 2048, 4096, 50.0) - - # Mock GPU usage - mock_gpu.return_value = [{"utilization_percent": 80.0, "memory_used_mb": 1024}] - - usage = monitor.get_resource_usage() - - assert isinstance(usage, ResourceUsage) - assert usage.cpu_percent == 75.5 - assert usage.memory_used_mb == 2048 - assert usage.memory_total_mb == 4096 - assert usage.memory_percent == 50.0 - assert usage.gpu_stats == [ - {"utilization_percent": 80.0, "memory_used_mb": 1024} - ] - - def test_estimate_cost_with_api_success(self): - """Test cost estimation with successful API call.""" - monitor = AzureCostMonitor(use_pricing_api=True) - - # Mock pricing client - mock_pricing_client = Mock() - mock_pricing_client.get_instance_pricing.return_value = 0.192 - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost("Standard_D4s_v3", 10.0) - - assert isinstance(cost_estimate, CostEstimate) - assert cost_estimate.hourly_rate == 0.192 - assert cost_estimate.estimated_cost == 1.92 # 0.192 * 10 - assert cost_estimate.instance_type == "Standard_D4s_v3 (Pay-as-you-go)" - assert cost_estimate.hours_used == 10.0 - assert cost_estimate.pricing_source == "api" - - mock_pricing_client.get_instance_pricing.assert_called_once_with( - instance_type="Standard_D4s_v3", region="eastus" - ) - - def test_estimate_cost_with_api_failure(self): - """Test cost estimation with API failure fallback.""" - monitor = AzureCostMonitor(use_pricing_api=True) - - # Mock pricing client that fails - mock_pricing_client = Mock() - mock_pricing_client.get_instance_pricing.side_effect = Exception("API error") - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost("Standard_D4s_v3", 5.0) - - assert cost_estimate.hourly_rate == 0.192 # fallback to hardcoded - assert cost_estimate.estimated_cost == 0.96 # 0.192 * 5 - assert cost_estimate.pricing_source == "hardcoded" - assert cost_estimate.instance_type == "Standard_D4s_v3 (Pay-as-you-go)" - - def test_estimate_cost_spot_pricing(self): - """Test cost estimation with spot pricing.""" - monitor = AzureCostMonitor(use_pricing_api=True) - - # Mock pricing client - mock_pricing_client = Mock() - mock_pricing_client.get_spot_pricing.return_value = 0.038 # ~80% discount - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost("Standard_D4s_v3", 10.0, use_spot=True) - - assert cost_estimate.hourly_rate == 0.038 - assert cost_estimate.estimated_cost == 0.38 - assert cost_estimate.pricing_source == "api" - assert cost_estimate.instance_type == "Standard_D4s_v3 (Spot)" - - mock_pricing_client.get_spot_pricing.assert_called_once_with( - "Standard_D4s_v3", "eastus" - ) - - def test_estimate_cost_spot_pricing_failure(self): - """Test spot pricing with API failure.""" - monitor = AzureCostMonitor(use_pricing_api=True) - - # Mock pricing client that fails - mock_pricing_client = Mock() - mock_pricing_client.get_spot_pricing.side_effect = Exception("Spot API error") - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost("Standard_D4s_v3", 5.0, use_spot=True) - - # Should fall back to spot calculation - expected_spot_rate = 0.192 * 0.7 # 30% discount (D-series) - assert abs(cost_estimate.hourly_rate - expected_spot_rate) < 0.001 - assert cost_estimate.instance_type == "Standard_D4s_v3 (Spot)" - - def test_estimate_cost_unknown_instance(self): - """Test cost estimation for unknown instance type.""" - monitor = AzureCostMonitor(use_pricing_api=False) - - cost_estimate = monitor.estimate_cost("Unknown_Instance", 8.0) - - assert cost_estimate.hourly_rate == 0.10 # default price - assert cost_estimate.estimated_cost == 0.8 - assert cost_estimate.instance_type == "Unknown_Instance (Pay-as-you-go)" - - def test_estimate_cost_without_pricing_client(self): - """Test cost estimation without pricing client.""" - monitor = AzureCostMonitor(use_pricing_api=False) - - cost_estimate = monitor.estimate_cost("Standard_B2s", 6.0) - - assert cost_estimate.hourly_rate == 0.0416 - assert cost_estimate.estimated_cost == 0.2496 - assert cost_estimate.pricing_source == "hardcoded" - assert cost_estimate.instance_type == "Standard_B2s (Pay-as-you-go)" - - def test_get_pricing_info(self): - """Test getting pricing information.""" - monitor = AzureCostMonitor() - - pricing_info = monitor.get_pricing_info() - - assert isinstance(pricing_info, dict) - assert "Standard_B1s" in pricing_info - assert "Standard_D4s_v3" in pricing_info - assert pricing_info["Standard_B1s"] == 0.0104 - assert pricing_info["Standard_D4s_v3"] == 0.192 - - def test_get_spot_pricing_info(self): - """Test getting spot pricing information.""" - monitor = AzureCostMonitor() - - spot_pricing = monitor.get_spot_pricing_info() - - assert isinstance(spot_pricing, dict) - assert "Standard_B1s" in spot_pricing - assert "Standard_D4s_v3" in spot_pricing - - # Check that spot pricing is discounted - on_demand_price = monitor.vm_pricing["Standard_B1s"] - spot_price = spot_pricing["Standard_B1s"] - assert spot_price < on_demand_price - - # B-series should have 20% discount (0.8 factor) - expected_spot_price = on_demand_price * 0.8 - assert abs(spot_price - expected_spot_price) < 0.0001 - - def test_get_spot_pricing_info_nc_series(self): - """Test spot pricing for NC-series (GPU) instances.""" - monitor = AzureCostMonitor() - - # Add NC series to pricing for testing - monitor.vm_pricing["Standard_NC6s_v3"] = 3.06 - - spot_pricing = monitor.get_spot_pricing_info() - - # NC series should have 70% discount (0.3 factor) - expected_spot_price = 3.06 * 0.3 - assert abs(spot_pricing["Standard_NC6s_v3"] - expected_spot_price) < 0.01 - - def test_get_cost_optimization_recommendations_basic(self): - """Test basic cost optimization recommendations.""" - monitor = AzureCostMonitor() - - resource_usage = ResourceUsage( - cpu_percent=45.0, - memory_used_mb=1500, - memory_total_mb=4096, - memory_percent=36.6, - gpu_stats=None, - ) - - cost_estimate = CostEstimate( - hourly_rate=0.192, - estimated_cost=1.92, - hours_used=10.0, - instance_type="Standard_D4s_v3", - pricing_source="api", - ) - - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - assert isinstance(recommendations, list) - assert len(recommendations) > 0 - - # Check for Azure-specific recommendations - azure_recommendations = [r for r in recommendations if "Azure" in r] - assert len(azure_recommendations) > 0 - - # Should include spot VM recommendation - spot_recommendations = [r for r in recommendations if "Spot" in r] - assert len(spot_recommendations) > 0 - - def test_get_cost_optimization_recommendations_gpu_low_usage(self): - """Test recommendations for GPU instances with low utilization.""" - monitor = AzureCostMonitor() - - resource_usage = ResourceUsage( - cpu_percent=60.0, - memory_used_mb=8000, - memory_total_mb=16384, - memory_percent=48.8, - gpu_stats=[ - {"utilization_percent": 25.0, "memory_used_mb": 2000}, - {"utilization_percent": 30.0, "memory_used_mb": 2500}, - ], - ) - - cost_estimate = CostEstimate( - hourly_rate=3.06, - estimated_cost=30.6, - hours_used=10.0, - instance_type="Standard_NC6s_v3", - pricing_source="api", - ) - - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # Should include GPU utilization warning - gpu_recommendations = [r for r in recommendations if "GPU utilization" in r] - assert len(gpu_recommendations) > 0 - - def test_get_cost_optimization_recommendations_gpu_high_usage(self): - """Test recommendations for GPU instances with high utilization.""" - monitor = AzureCostMonitor() - - resource_usage = ResourceUsage( - cpu_percent=85.0, - memory_used_mb=12000, - memory_total_mb=16384, - memory_percent=73.2, - gpu_stats=[ - {"utilization_percent": 85.0, "memory_used_mb": 7000}, - {"utilization_percent": 90.0, "memory_used_mb": 7500}, - ], - ) - - cost_estimate = CostEstimate( - hourly_rate=3.06, - estimated_cost=30.6, - hours_used=10.0, - instance_type="Standard_NC6s_v3", - pricing_source="api", - ) - - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # Should NOT include GPU utilization warning for high usage - gpu_recommendations = [r for r in recommendations if "Low GPU utilization" in r] - assert len(gpu_recommendations) == 0 - - def test_get_cost_optimization_recommendations_nd_series(self): - """Test recommendations for ND-series instances.""" - monitor = AzureCostMonitor() - - resource_usage = ResourceUsage( - cpu_percent=60.0, - memory_used_mb=8000, - memory_total_mb=16384, - memory_percent=48.8, - gpu_stats=[{"utilization_percent": 40.0, "memory_used_mb": 4000}], - ) - - cost_estimate = CostEstimate( - hourly_rate=6.12, - estimated_cost=61.2, - hours_used=10.0, - instance_type="Standard_ND6s", - pricing_source="api", - ) - - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # Should include GPU utilization warning for ND series too - gpu_recommendations = [r for r in recommendations if "GPU utilization" in r] - assert len(gpu_recommendations) > 0 - - def test_estimate_batch_cost(self): - """Test Azure Batch cost estimation.""" - monitor = AzureCostMonitor() - - batch_estimate = monitor.estimate_batch_cost( - pool_name="test-pool", - vm_size="Standard_D4s_v3", - target_nodes=5, - estimated_duration_hours=2.0, - ) - - assert isinstance(batch_estimate, dict) - assert "estimated_cost" in batch_estimate - assert "total_compute_hours" in batch_estimate - assert "vm_size" in batch_estimate - assert "target_nodes" in batch_estimate - assert "estimated_duration_hours" in batch_estimate - assert "vm_hourly_cost" in batch_estimate - assert "cost_per_node_hour" in batch_estimate - - # Check calculations - vm_hourly_cost = 0.192 # Standard_D4s_v3 - total_compute_hours = 5 * 2.0 # 5 nodes * 2 hours - total_compute_cost = total_compute_hours * vm_hourly_cost - - assert batch_estimate["total_compute_hours"] == total_compute_hours - assert batch_estimate["estimated_cost"] == total_compute_cost - assert batch_estimate["vm_size"] == "Standard_D4s_v3" - assert batch_estimate["target_nodes"] == 5 - assert batch_estimate["estimated_duration_hours"] == 2.0 - - def test_estimate_batch_cost_unknown_vm_size(self): - """Test Batch cost estimation with unknown VM size.""" - monitor = AzureCostMonitor() - - batch_estimate = monitor.estimate_batch_cost( - pool_name="test-pool", - vm_size="Unknown_VM_Size", - target_nodes=3, - estimated_duration_hours=4.0, - ) - - # Should use default pricing - vm_hourly_cost = 0.10 # default - total_compute_hours = 3 * 4.0 # 3 nodes * 4 hours - total_compute_cost = total_compute_hours * vm_hourly_cost - - assert batch_estimate["total_compute_hours"] == total_compute_hours - assert batch_estimate["estimated_cost"] == total_compute_cost - - def test_spot_discounts_structure(self): - """Test spot discount structure.""" - monitor = AzureCostMonitor() - - assert hasattr(monitor, "spot_discounts") - assert isinstance(monitor.spot_discounts, dict) - assert "Standard_B" in monitor.spot_discounts - assert "Standard_D" in monitor.spot_discounts - assert "Standard_F" in monitor.spot_discounts - assert "Standard_NC" in monitor.spot_discounts - assert "default" in monitor.spot_discounts - - # Check that all discounts are between 0 and 1 - for discount in monitor.spot_discounts.values(): - assert 0 < discount < 1 - - def test_vm_pricing_structure(self): - """Test VM pricing structure.""" - monitor = AzureCostMonitor() - - assert isinstance(monitor.vm_pricing, dict) - assert len(monitor.vm_pricing) > 10 # Should have many instance types - - # Check some expected instance types - expected_types = [ - "Standard_B1s", - "Standard_B2s", - "Standard_D2s_v3", - "Standard_F2s_v2", - "default", - ] - for instance_type in expected_types: - assert instance_type in monitor.vm_pricing - assert isinstance(monitor.vm_pricing[instance_type], (int, float)) - assert monitor.vm_pricing[instance_type] > 0 diff --git a/tests/test_azure_pricing_integration.py b/tests/test_azure_pricing_integration.py deleted file mode 100644 index aa9d6499..00000000 --- a/tests/test_azure_pricing_integration.py +++ /dev/null @@ -1,288 +0,0 @@ -"""Tests for Azure pricing API integration.""" - -import pytest -import json -import tempfile -from pathlib import Path -from unittest.mock import Mock, patch, MagicMock -import requests - -from clustrix.pricing_clients.azure_pricing import AzurePricingClient - - -class TestAzurePricingClient: - """Test Azure pricing client functionality.""" - - def test_init(self): - """Test Azure pricing client initialization.""" - client = AzurePricingClient(cache_ttl_hours=12) - - assert client.api_url == "https://prices.azure.com/api/retail/prices" - assert client.api_version == "2021-10-01-preview" - assert "Standard_D2s_v3" in client._hardcoded_pricing - assert client._hardcoded_pricing_date is not None - - def test_get_region_name(self): - """Test region code to name conversion.""" - client = AzurePricingClient() - - # Test known regions - assert client._get_region_name("eastus") == "East US" - assert client._get_region_name("westeurope") == "West Europe" - assert client._get_region_name("southeastasia") == "Southeast Asia" - - # Test case insensitive - assert client._get_region_name("EASTUS") == "East US" - - # Test fallback for unknown region - assert client._get_region_name("unknown-region") == "unknown-region" - - @patch("requests.get") - def test_get_instance_pricing_from_api_success(self, mock_get): - """Test successful Azure API pricing retrieval.""" - # Mock successful API response - mock_response = Mock() - mock_response.json.return_value = { - "Items": [ - { - "retailPrice": 0.096, - "currencyCode": "USD", - "meterName": "D2s v3", - "productName": "Virtual Machines D2s v3 Series", - "armSkuName": "Standard_D2s_v3", - "armRegionName": "eastus", - } - ] - } - mock_response.raise_for_status.return_value = None - mock_get.return_value = mock_response - - client = AzurePricingClient() - - # Clear cache to ensure API is called - import shutil - - if client.cache.cache_dir.exists(): - shutil.rmtree(client.cache.cache_dir) - client.cache.cache_dir.mkdir(exist_ok=True) - - price = client.get_instance_pricing("Standard_D2s_v3", "eastus") - - assert price == 0.096 - mock_get.assert_called_once() - - # Verify the API call parameters - call_args = mock_get.call_args - assert "prices.azure.com" in call_args[0][0] - assert "armSkuName eq 'Standard_D2s_v3'" in call_args[1]["params"]["$filter"] - - @patch("requests.get") - def test_get_instance_pricing_api_failure(self, mock_get): - """Test fallback when Azure API fails.""" - # Mock API failure - mock_get.side_effect = requests.RequestException("API Error") - - client = AzurePricingClient() - price = client.get_instance_pricing("Standard_D2s_v3", "eastus") - - # Should fall back to hardcoded pricing - assert price == 0.096 # Hardcoded price - - @patch("requests.get") - def test_get_instance_pricing_empty_response(self, mock_get): - """Test handling of empty API response.""" - # Mock empty response - mock_response = Mock() - mock_response.json.return_value = {"Items": []} - mock_response.raise_for_status.return_value = None - mock_get.return_value = mock_response - - client = AzurePricingClient() - price = client.get_instance_pricing("Unknown_VM", "eastus") - - # Should fall back to default pricing for unknown VM - assert price == 0.10 # Default fallback price - - def test_get_instance_pricing_fallback(self): - """Test fallback to hardcoded pricing.""" - client = AzurePricingClient() - - # Mock the API to fail - with patch.object(client, "_fetch_pricing_from_api", return_value=None): - price = client.get_instance_pricing("Standard_D2s_v3", "eastus") - - assert price == 0.096 # Hardcoded price - - @patch("requests.get") - def test_get_spot_pricing_from_api(self, mock_get): - """Test spot pricing from Azure API.""" - # Mock spot pricing response - mock_response = Mock() - mock_response.json.return_value = { - "Items": [ - { - "retailPrice": 0.0192, # 80% discount - "currencyCode": "USD", - "meterName": "D2s v3 Spot", - "productName": "Virtual Machines D2s v3 Series Spot", - "armSkuName": "Standard_D2s_v3", - } - ] - } - mock_response.raise_for_status.return_value = None - mock_get.return_value = mock_response - - client = AzurePricingClient() - spot_price = client.get_spot_pricing("Standard_D2s_v3", "eastus") - - assert spot_price == 0.0192 - mock_get.assert_called_once() - - # Verify spot filter is included - call_args = mock_get.call_args - assert "contains(meterName, 'Spot')" in call_args[1]["params"]["$filter"] - - def test_get_spot_pricing_fallback(self): - """Test spot pricing fallback calculation.""" - client = AzurePricingClient() - - # Mock API calls to fail for spot, succeed for on-demand - with patch.object(client, "_fetch_spot_pricing_from_api", return_value=None): - with patch.object(client, "get_instance_pricing", return_value=0.096): - spot_price = client.get_spot_pricing("Standard_D2s_v3", "eastus") - - # Should be 80% discount from on-demand - expected_price = 0.096 * 0.2 # 80% discount - assert spot_price == pytest.approx(expected_price, rel=1e-4) - - @patch("requests.get") - def test_windows_vs_linux_pricing(self, mock_get): - """Test different pricing for Windows vs Linux.""" - # Mock API response - mock_response = Mock() - mock_response.json.return_value = {"Items": []} - mock_response.raise_for_status.return_value = None - mock_get.return_value = mock_response - - client = AzurePricingClient() - - # Clear cache to ensure API is called - import shutil - - if client.cache.cache_dir.exists(): - shutil.rmtree(client.cache.cache_dir) - client.cache.cache_dir.mkdir(exist_ok=True) - - # Test Linux pricing call - client.get_instance_pricing( - "Standard_D2s_v3", "eastus", operating_system="Linux" - ) - - # Verify Linux filter - should not have Windows filter - call_args = mock_get.call_args - assert "Windows" not in call_args[1]["params"]["$filter"] - - # Test Windows pricing call - client.get_instance_pricing( - "Standard_D2s_v3", "eastus", operating_system="Windows" - ) - - # Verify Windows filter - call_args = mock_get.call_args - assert "contains(productName, 'Windows')" in call_args[1]["params"]["$filter"] - - def test_get_all_pricing(self): - """Test getting all pricing information.""" - client = AzurePricingClient() - - all_pricing = client.get_all_pricing("eastus") - - assert isinstance(all_pricing, dict) - assert "Standard_D2s_v3" in all_pricing - assert all_pricing["Standard_D2s_v3"] == 0.096 - - @patch("requests.get") - def test_get_pricing_by_service(self, mock_get): - """Test getting pricing for a specific service.""" - # Mock service pricing response - mock_response = Mock() - mock_response.json.return_value = { - "Items": [ - { - "retailPrice": 0.096, - "currencyCode": "USD", - "meterName": "D2s v3", - "productName": "Virtual Machines D2s v3 Series", - "armSkuName": "Standard_D2s_v3", - }, - { - "retailPrice": 0.192, - "currencyCode": "USD", - "meterName": "D4s v3", - "productName": "Virtual Machines D4s v3 Series", - "armSkuName": "Standard_D4s_v3", - }, - ] - } - mock_response.raise_for_status.return_value = None - mock_get.return_value = mock_response - - client = AzurePricingClient() - pricing = client.get_pricing_by_service("Virtual Machines", "eastus") - - assert "Standard_D2s_v3" in pricing - assert "Standard_D4s_v3" in pricing - assert pricing["Standard_D2s_v3"]["price"] == 0.096 - assert pricing["Standard_D4s_v3"]["price"] == 0.192 - - def test_cache_functionality(self): - """Test pricing cache functionality.""" - # Create client with temporary cache directory - with tempfile.TemporaryDirectory() as tmpdir: - client = AzurePricingClient() - client.cache.cache_dir = Path(tmpdir) - - # Mock successful API call - with patch.object(client, "_fetch_pricing_from_api") as mock_fetch: - mock_fetch.return_value = {"price": 0.096} - - # First call - should hit API - price1 = client.get_instance_pricing("Standard_D2s_v3", "eastus") - assert price1 == 0.096 - assert mock_fetch.call_count == 1 - - # Second call - should hit cache - price2 = client.get_instance_pricing("Standard_D2s_v3", "eastus") - assert price2 == 0.096 - assert mock_fetch.call_count == 1 # No additional API call - - @patch("requests.get") - def test_api_timeout_handling(self, mock_get): - """Test handling of API timeouts.""" - # Mock timeout - mock_get.side_effect = requests.exceptions.Timeout("Request timed out") - - client = AzurePricingClient() - price = client.get_instance_pricing("Standard_D2s_v3", "eastus") - - # Should fall back to hardcoded pricing - assert price == 0.096 - - @patch("requests.get") - def test_malformed_response_handling(self, mock_get): - """Test handling of malformed API responses.""" - # Mock malformed response - mock_response = Mock() - mock_response.json.side_effect = json.JSONDecodeError("Invalid JSON", "", 0) - mock_response.raise_for_status.return_value = None - mock_get.return_value = mock_response - - client = AzurePricingClient() - price = client.get_instance_pricing("Standard_D2s_v3", "eastus") - - # Should fall back to hardcoded pricing - assert price == 0.096 - - -if __name__ == "__main__": - pytest.main([__file__, "-v"]) diff --git a/tests/test_cloud_providers.py b/tests/test_cloud_providers.py deleted file mode 100644 index b465a15d..00000000 --- a/tests/test_cloud_providers.py +++ /dev/null @@ -1,248 +0,0 @@ -"""Tests for cloud provider integrations.""" - -import pytest -from unittest.mock import MagicMock, patch - -from clustrix.cloud_providers.base import CloudProvider -from clustrix.cloud_providers import PROVIDERS - - -class TestCloudProviderBase: - """Test the base cloud provider class.""" - - def test_abstract_methods(self): - """Test that base class can't be instantiated.""" - with pytest.raises(TypeError): - CloudProvider() - - def test_is_authenticated(self): - """Test authentication status check.""" - - # Create a concrete implementation for testing - class TestProvider(CloudProvider): - def authenticate(self, **credentials): - self.authenticated = True - return True - - def validate_credentials(self): - return True - - def create_cluster(self, cluster_name, **kwargs): - return {} - - def delete_cluster(self, cluster_identifier): - return True - - def get_cluster_status(self, cluster_identifier): - return {} - - def list_clusters(self): - return [] - - def get_cluster_config(self, cluster_identifier): - return {} - - def estimate_cost(self, **kwargs): - return {} - - def get_available_instance_types(self, region=None): - return ["test-instance"] - - def get_available_regions(self): - return ["test-region"] - - provider = TestProvider() - assert not provider.is_authenticated() - - provider.authenticate() - assert provider.is_authenticated() - - -@pytest.mark.skipif("aws" not in PROVIDERS, reason="boto3 not installed") -class TestAWSProvider: - """Test AWS provider implementation.""" - - @pytest.fixture - def mock_boto3(self): - """Mock boto3 for testing.""" - with patch("clustrix.cloud_providers.aws.boto3") as mock_boto3: - # Mock session - mock_session = MagicMock() - mock_boto3.Session.return_value = mock_session - - # Mock clients - mock_ec2 = MagicMock() - mock_eks = MagicMock() - mock_iam = MagicMock() - mock_sts = MagicMock() - - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2, - "eks": mock_eks, - "iam": mock_iam, - "sts": mock_sts, - }[service] - - yield { - "boto3": mock_boto3, - "session": mock_session, - "ec2": mock_ec2, - "eks": mock_eks, - "iam": mock_iam, - "sts": mock_sts, - } - - def test_authenticate_success(self, mock_boto3): - """Test successful authentication.""" - from clustrix.cloud_providers.aws import AWSProvider - - provider = AWSProvider() - result = provider.authenticate( - access_key_id="test_key", - secret_access_key="test_secret", - region="us-west-2", - ) - - assert result is True - assert provider.is_authenticated() - assert provider.region == "us-west-2" - - # Check that clients were initialized - assert provider.ec2_client is not None - assert provider.eks_client is not None - assert provider.iam_client is not None - - def test_authenticate_failure(self, mock_boto3): - """Test authentication failure.""" - from clustrix.cloud_providers.aws import AWSProvider - from botocore.exceptions import ClientError - - # Make get_caller_identity fail (this is what authenticate actually uses) - mock_boto3["sts"].get_caller_identity.side_effect = ClientError( - {"Error": {"Code": "InvalidClientTokenId"}}, "GetCallerIdentity" - ) - - provider = AWSProvider() - result = provider.authenticate( - access_key_id="bad_key", secret_access_key="bad_secret" - ) - - assert result is False - assert not provider.is_authenticated() - - def test_create_ec2_instance(self, mock_boto3): - """Test EC2 instance creation.""" - from clustrix.cloud_providers.aws import AWSProvider - - # Mock responses - mock_boto3["ec2"].describe_images.return_value = { - "Images": [ - {"ImageId": "ami-12345", "CreationDate": "2024-01-01T00:00:00.000Z"} - ] - } - - mock_boto3["ec2"].run_instances.return_value = { - "Instances": [{"InstanceId": "i-1234567890", "State": {"Name": "pending"}}] - } - - mock_boto3["ec2"].describe_instances.return_value = { - "Reservations": [ - { - "Instances": [ - { - "InstanceId": "i-1234567890", - "PublicIpAddress": "1.2.3.4", - "PrivateIpAddress": "10.0.0.1", - "State": {"Name": "running"}, - } - ] - } - ] - } - - provider = AWSProvider() - provider.authenticated = True - provider.ec2_client = mock_boto3["ec2"] - - result = provider.create_ec2_instance( - instance_name="test-instance", instance_type="t3.micro" - ) - - assert result["instance_id"] == "i-1234567890" - assert result["public_ip"] == "1.2.3.4" - assert result["instance_type"] == "t3.micro" - - def test_get_cluster_config_ec2(self, mock_boto3): - """Test getting Clustrix config for EC2 instance.""" - from clustrix.cloud_providers.aws import AWSProvider - - mock_boto3["ec2"].describe_instances.return_value = { - "Reservations": [ - { - "Instances": [ - { - "InstanceId": "i-1234567890", - "PublicIpAddress": "1.2.3.4", - "State": {"Name": "running"}, - } - ] - } - ] - } - - provider = AWSProvider() - provider.authenticated = True - provider.ec2_client = mock_boto3["ec2"] - provider.region = "us-east-1" - - config = provider.get_cluster_config("i-1234567890", cluster_type="ec2") - - assert config["cluster_type"] == "ssh" - assert config["cluster_host"] == "1.2.3.4" - assert config["username"] == "ec2-user" - assert config["cost_monitoring"] is True - assert config["provider"] == "aws" - - def test_get_cluster_config_eks(self, mock_boto3): - """Test getting Clustrix config for EKS cluster.""" - from clustrix.cloud_providers.aws import AWSProvider - - provider = AWSProvider() - provider.authenticated = True - provider.region = "us-west-2" - - config = provider.get_cluster_config("my-cluster", cluster_type="eks") - - assert config["cluster_type"] == "kubernetes" - assert config["cluster_host"] == "my-cluster.eks.us-west-2.amazonaws.com" - assert config["cluster_port"] == 443 - assert config["cost_monitoring"] is True - assert config["provider"] == "aws" - - def test_estimate_cost_eks(self, mock_boto3): - """Test cost estimation for EKS.""" - from clustrix.cloud_providers.aws import AWSProvider - - provider = AWSProvider() - costs = provider.estimate_cost( - cluster_type="eks", instance_type="t3.medium", node_count=3, hours=24 - ) - - assert "control_plane" in costs - assert "nodes" in costs - assert "total" in costs - assert costs["control_plane"] == 0.10 * 24 # $0.10/hour * 24 hours - assert costs["nodes"] == 0.0416 * 3 * 24 # t3.medium price * 3 nodes * 24 hours - - def test_estimate_cost_ec2(self, mock_boto3): - """Test cost estimation for EC2.""" - from clustrix.cloud_providers.aws import AWSProvider - - provider = AWSProvider() - costs = provider.estimate_cost( - cluster_type="ec2", instance_type="t3.large", hours=8 - ) - - assert "instance" in costs - assert "total" in costs - assert costs["total"] == 0.0832 * 8 # t3.large price * 8 hours diff --git a/tests/test_cloud_providers_aws.py b/tests/test_cloud_providers_aws.py deleted file mode 100644 index 68c7597e..00000000 --- a/tests/test_cloud_providers_aws.py +++ /dev/null @@ -1,375 +0,0 @@ -import pytest -import json -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime, timezone - -from clustrix.cloud_providers.aws import AWSProvider - - -class TestAWSProvider: - """Test AWS provider functionality.""" - - @pytest.fixture - def provider(self): - """Create AWSProvider instance.""" - return AWSProvider() - - @pytest.fixture - def authenticated_provider(self): - """Create authenticated AWSProvider instance.""" - provider = AWSProvider() - provider.authenticated = True - provider.region = "us-east-1" - provider.ec2_client = Mock() - provider.eks_client = Mock() - provider.iam_client = Mock() - provider.credentials = { - "access_key_id": "test-access-key", - "secret_access_key": "test-secret-key", - "region": "us-east-1", - } - return provider - - def test_initialization(self, provider): - """Test provider initialization.""" - assert provider.ec2_client is None - assert provider.eks_client is None - assert provider.iam_client is None - assert provider.region == "us-east-1" - assert not provider.authenticated - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", True) - @patch("clustrix.cloud_providers.aws.boto3") - def test_authenticate_success(self, mock_boto3, provider): - """Test successful authentication.""" - # Mock session and clients - mock_session = Mock() - mock_boto3.Session.return_value = mock_session - - mock_ec2_client = Mock() - mock_eks_client = Mock() - mock_iam_client = Mock() - mock_sts_client = Mock() - - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2_client, - "eks": mock_eks_client, - "iam": mock_iam_client, - "sts": mock_sts_client, - }[service] - - # Mock successful STS call (used by authenticate) - mock_sts_client.get_caller_identity.return_value = { - "UserId": "AIDAI23HXD2O5EXAMPLE", - "Account": "123456789012", - "Arn": "arn:aws:iam::123456789012:user/test-user", - } - - result = provider.authenticate( - access_key_id="test-access-key", - secret_access_key="test-secret-key", - region="us-west-2", - ) - - assert result is True - assert provider.authenticated is True - assert provider.region == "us-west-2" - assert provider.ec2_client == mock_ec2_client - assert provider.eks_client == mock_eks_client - assert provider.iam_client == mock_iam_client - - mock_boto3.Session.assert_called_once_with( - aws_access_key_id="test-access-key", - aws_secret_access_key="test-secret-key", - aws_session_token=None, - region_name="us-west-2", - ) - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", True) - @patch("clustrix.cloud_providers.aws.boto3") - def test_authenticate_with_session_token(self, mock_boto3, provider): - """Test authentication with session token.""" - mock_session = Mock() - mock_boto3.Session.return_value = mock_session - - # Create mock clients - mock_ec2_client = Mock() - mock_eks_client = Mock() - mock_iam_client = Mock() - mock_sts_client = Mock() - - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2_client, - "eks": mock_eks_client, - "iam": mock_iam_client, - "sts": mock_sts_client, - }.get(service, Mock()) - - mock_sts_client.get_caller_identity.return_value = { - "UserId": "AIDAI23HXD2O5EXAMPLE", - "Account": "123456789012", - "Arn": "arn:aws:iam::123456789012:user/test-user", - } - - result = provider.authenticate( - access_key_id="test-access-key", - secret_access_key="test-secret-key", - session_token="test-session-token", - ) - - assert result is True - assert "session_token" in provider.credentials - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", False) - def test_authenticate_boto3_not_available(self, provider): - """Test authentication when boto3 not available.""" - result = provider.authenticate( - access_key_id="test-access-key", secret_access_key="test-secret-key" - ) - - assert result is False - assert not provider.authenticated - - def test_authenticate_missing_credentials(self, provider): - """Test authentication with missing credentials.""" - result = provider.authenticate(access_key_id="test-access-key") - assert result is False - - result = provider.authenticate(secret_access_key="test-secret-key") - assert result is False - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", True) - @patch("clustrix.cloud_providers.aws.boto3") - def test_authenticate_no_credentials_error(self, mock_boto3, provider): - """Test authentication with NoCredentialsError.""" - from clustrix.cloud_providers.aws import NoCredentialsError - - mock_session = Mock() - mock_boto3.Session.return_value = mock_session - - # Create mock clients - mock_ec2_client = Mock() - mock_eks_client = Mock() - mock_iam_client = Mock() - mock_sts_client = Mock() - - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2_client, - "eks": mock_eks_client, - "iam": mock_iam_client, - "sts": mock_sts_client, - }.get(service, Mock()) - - mock_sts_client.get_caller_identity.side_effect = NoCredentialsError() - - result = provider.authenticate( - access_key_id="test-access-key", secret_access_key="test-secret-key" - ) - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", True) - @patch("clustrix.cloud_providers.aws.boto3") - def test_authenticate_client_error(self, mock_boto3, provider): - """Test authentication with ClientError.""" - from clustrix.cloud_providers.aws import ClientError - - mock_session = Mock() - mock_boto3.Session.return_value = mock_session - - # Create mock clients - mock_ec2_client = Mock() - mock_eks_client = Mock() - mock_iam_client = Mock() - mock_sts_client = Mock() - - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2_client, - "eks": mock_eks_client, - "iam": mock_iam_client, - "sts": mock_sts_client, - }.get(service, Mock()) - - mock_sts_client.get_caller_identity.side_effect = ClientError( - {"Error": {"Code": "AccessDenied"}}, "GetCallerIdentity" - ) - - result = provider.authenticate( - access_key_id="test-access-key", secret_access_key="test-secret-key" - ) - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", True) - @patch("clustrix.cloud_providers.aws.boto3") - def test_authenticate_unexpected_error(self, mock_boto3, provider): - """Test authentication with unexpected error.""" - mock_session = Mock() - mock_boto3.Session.return_value = mock_session - - # Create mock clients - mock_ec2_client = Mock() - mock_eks_client = Mock() - mock_iam_client = Mock() - mock_sts_client = Mock() - - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2_client, - "eks": mock_eks_client, - "iam": mock_iam_client, - "sts": mock_sts_client, - }.get(service, Mock()) - - mock_sts_client.get_caller_identity.side_effect = Exception("Network error") - - result = provider.authenticate( - access_key_id="test-access-key", secret_access_key="test-secret-key" - ) - - assert result is False - assert not provider.authenticated - - def test_validate_credentials_success(self, authenticated_provider): - """Test successful credential validation.""" - authenticated_provider.iam_client.get_user.return_value = { - "User": {"UserName": "test-user"} - } - - result = authenticated_provider.validate_credentials() - - assert result is True - - def test_validate_credentials_failure(self, authenticated_provider): - """Test failed credential validation.""" - authenticated_provider.iam_client.get_user.side_effect = Exception( - "Invalid credentials" - ) - - result = authenticated_provider.validate_credentials() - - assert result is False - - def test_validate_credentials_not_authenticated(self, provider): - """Test credential validation when not authenticated.""" - result = provider.validate_credentials() - - assert result is False - - def test_create_or_get_eks_cluster_role_existing(self, authenticated_provider): - """Test getting existing EKS cluster role.""" - authenticated_provider.iam_client.get_role.return_value = { - "Role": {"Arn": "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role"} - } - - result = authenticated_provider._create_or_get_eks_cluster_role() - - assert result == "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role" - authenticated_provider.iam_client.get_role.assert_called_once_with( - RoleName="clustrix-eks-cluster-role" - ) - - def test_create_or_get_eks_cluster_role_create_new(self, authenticated_provider): - """Test creating new EKS cluster role.""" - from clustrix.cloud_providers.aws import ClientError - - # Mock role doesn't exist - authenticated_provider.iam_client.get_role.side_effect = ClientError( - {"Error": {"Code": "NoSuchEntity"}}, "GetRole" - ) - - # Mock successful role creation - authenticated_provider.iam_client.create_role.return_value = { - "Role": {"Arn": "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role"} - } - - result = authenticated_provider._create_or_get_eks_cluster_role() - - assert result == "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role" - authenticated_provider.iam_client.create_role.assert_called_once() - authenticated_provider.iam_client.attach_role_policy.assert_called_once() - - def test_estimate_cost_eks(self, provider): - """Test EKS cost estimation.""" - result = provider.estimate_cost( - cluster_type="eks", instance_type="t3.large", node_count=3, hours=5 - ) - - assert "control_plane" in result - assert "nodes" in result - assert "total" in result - assert result["control_plane"] == 0.10 * 5 # EKS control plane cost - assert result["nodes"] == 0.0832 * 3 * 5 # Node cost - assert result["total"] == result["control_plane"] + result["nodes"] - - def test_estimate_cost_ec2(self, provider): - """Test EC2 cost estimation.""" - result = provider.estimate_cost( - cluster_type="ec2", instance_type="m5.large", hours=3 - ) - - assert "instance" in result - assert "total" in result - assert result["instance"] == 0.096 * 3 - assert result["total"] == result["instance"] - - def test_estimate_cost_unknown_instance_type(self, provider): - """Test cost estimation with unknown instance type.""" - result = provider.estimate_cost(instance_type="unknown.type", hours=2) - - # Default for unknown instance: EKS with 2 nodes, 2 hours - # Control plane: 0.10 * 2 = 0.20 - # Nodes: 0.10 * 2 nodes * 2 hours = 0.40 - # Total: 0.60 - assert abs(result["total"] - 0.60) < 0.01 - - def test_get_available_instance_types_not_authenticated(self, provider): - """Test instance types when not authenticated.""" - result = provider.get_available_instance_types() - - assert "t3.micro" in result - assert "t3.medium" in result - assert "c5.large" in result - - def test_get_available_regions_not_authenticated(self, provider): - """Test regions when not authenticated.""" - result = provider.get_available_regions() - - assert "us-east-1" in result - assert "us-west-2" in result - assert "eu-west-1" in result - - def test_create_cluster_unknown_type(self, authenticated_provider): - """Test create_cluster with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.create_cluster( - "test-cluster", cluster_type="unknown" - ) - - def test_delete_cluster_unknown_type(self, authenticated_provider): - """Test cluster deletion with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.delete_cluster( - "test-cluster", cluster_type="unknown" - ) - - def test_get_cluster_status_unknown_type(self, authenticated_provider): - """Test cluster status with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_status( - "test-cluster", cluster_type="unknown" - ) - - def test_get_cluster_config_unknown_type(self, authenticated_provider): - """Test cluster config with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_config( - "test-cluster", cluster_type="unknown" - ) - - # NOTE: Additional comprehensive tests for VPC creation, subnet creation, - # security groups, EKS cluster creation, EC2 instance creation, cluster - # deletion, status checking, and region/instance type retrieval are needed - # but have been temporarily removed due to ClientError mocking issues. - # See GitHub issue for full details on remaining test coverage work. diff --git a/tests/test_cloud_providers_aws_comprehensive.py b/tests/test_cloud_providers_aws_comprehensive.py deleted file mode 100644 index 02d745f3..00000000 --- a/tests/test_cloud_providers_aws_comprehensive.py +++ /dev/null @@ -1,638 +0,0 @@ -""" -Comprehensive AWS provider tests following Cloud Control API patterns. - -These tests aim to achieve high coverage by testing both mocked scenarios -and real API interactions when credentials are available. - -Design follows AWS Cloud Control API patterns: -- Standardized CRUD-L operations (Create, Read, Update, Delete, List) -- Consistent error handling across resource types -- Request tracking and status monitoring -- Graceful fallback when credentials unavailable -""" - -import os -import pytest -import json -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime, timezone - -from clustrix.cloud_providers.aws import AWSProvider - - -class TestAWSProviderComprehensive: - """Comprehensive AWS provider tests with real API testing capability.""" - - @pytest.fixture - def provider(self): - """Create AWSProvider instance.""" - return AWSProvider() - - @pytest.fixture - def authenticated_provider(self): - """Create authenticated AWSProvider instance.""" - provider = AWSProvider() - provider.authenticated = True - provider.region = "us-east-1" - provider.ec2_client = Mock() - provider.eks_client = Mock() - provider.iam_client = Mock() - provider.credentials = { - "access_key_id": "test-access-key", - "secret_access_key": "test-secret-key", - "region": "us-east-1", - } - return provider - - @pytest.fixture - def real_aws_credentials(self): - """Check for real AWS credentials in environment or test config.""" - access_key = os.environ.get("AWS_ACCESS_KEY_ID") - secret_key = os.environ.get("AWS_SECRET_ACCESS_KEY") - - if access_key and secret_key: - return { - "access_key_id": access_key, - "secret_access_key": secret_key, - "region": os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), - "session_token": os.environ.get("AWS_SESSION_TOKEN"), - } - return None - - def test_initialization_comprehensive(self, provider): - """Test comprehensive provider initialization.""" - assert provider.ec2_client is None - assert provider.eks_client is None - assert provider.iam_client is None - assert provider.region == "us-east-1" - assert not provider.authenticated - assert hasattr(provider, "credentials") - assert provider.credentials == {} - - @pytest.mark.skipif( - not os.environ.get("AWS_ACCESS_KEY_ID"), - reason="Real AWS credentials not available", - ) - def test_real_authentication(self, provider, real_aws_credentials): - """Test authentication with real AWS credentials if available.""" - if not real_aws_credentials: - pytest.skip("No real AWS credentials available") - - result = provider.authenticate(**real_aws_credentials) - - if result: - assert provider.authenticated is True - assert provider.ec2_client is not None - assert provider.eks_client is not None - assert provider.iam_client is not None - - # Test that we can actually make a call - try: - user_info = provider.iam_client.get_user() - assert "User" in user_info or "UserName" in str(user_info) - except Exception as e: - # Some test accounts might not have get_user permission - assert "AccessDenied" in str(e) or "NoSuchEntity" in str(e) - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", True) - @patch("clustrix.cloud_providers.aws.boto3") - def test_authentication_error_scenarios(self, mock_boto3, provider): - """Test comprehensive authentication error scenarios.""" - from clustrix.cloud_providers.aws import ClientError, NoCredentialsError - - # Mock credential manager to return no credentials - with patch.object(provider, "get_credentials_from_manager", return_value=None): - # Test missing credentials - result = provider.authenticate() - assert result is False - - result = provider.authenticate(access_key_id="key-only") - assert result is False - - # Test NoCredentialsError - mock_session = Mock() - mock_boto3.Session.return_value = mock_session - - # Create mock clients including STS - mock_ec2_client = Mock() - mock_eks_client = Mock() - mock_iam_client = Mock() - mock_sts_client = Mock() - - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2_client, - "eks": mock_eks_client, - "iam": mock_iam_client, - "sts": mock_sts_client, - }.get(service, Mock()) - - mock_sts_client.get_caller_identity.side_effect = NoCredentialsError() - - result = provider.authenticate( - access_key_id="test-key", secret_access_key="test-secret" - ) - assert result is False - - # Test various ClientError scenarios - error_codes = ["AccessDenied", "InvalidUserID.NotFound", "TokenRefreshRequired"] - for error_code in error_codes: - mock_sts_client.get_caller_identity.side_effect = ClientError( - {"Error": {"Code": error_code, "Message": f"Test {error_code}"}}, - "GetCallerIdentity", - ) - result = provider.authenticate( - access_key_id="test-key", secret_access_key="test-secret" - ) - assert result is False - - def test_eks_cluster_role_operations(self, authenticated_provider): - """Test EKS cluster role creation and retrieval following CRUD patterns.""" - from clustrix.cloud_providers.aws import ClientError - - # Test getting existing role (Read operation) - authenticated_provider.iam_client.get_role.return_value = { - "Role": {"Arn": "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role"} - } - - result = authenticated_provider._create_or_get_eks_cluster_role() - assert result == "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role" - - # Test creating new role when it doesn't exist (Create operation) - authenticated_provider.iam_client.get_role.side_effect = ClientError( - {"Error": {"Code": "NoSuchEntity"}}, "GetRole" - ) - authenticated_provider.iam_client.create_role.return_value = { - "Role": {"Arn": "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role"} - } - - result = authenticated_provider._create_or_get_eks_cluster_role() - assert result == "arn:aws:iam::123456789012:role/clustrix-eks-cluster-role" - - # Verify policy attachment was called - authenticated_provider.iam_client.attach_role_policy.assert_called() - - def test_vpc_operations_comprehensive(self, authenticated_provider): - """Test VPC operations following AWS resource management patterns.""" - cluster_name = "test-cluster" - - # Mock existing VPC scenario - authenticated_provider.ec2_client.describe_vpcs.return_value = { - "Vpcs": [ - { - "VpcId": "vpc-12345678", - "CidrBlock": "10.0.0.0/16", - "State": "available", - } - ] - } - - # Mock subnet creation - authenticated_provider.ec2_client.describe_subnets.return_value = { - "Subnets": [] - } - authenticated_provider.ec2_client.create_subnet.return_value = { - "Subnet": {"SubnetId": "subnet-12345678"} - } - - # Mock security group creation - authenticated_provider.ec2_client.describe_security_groups.return_value = { - "SecurityGroups": [] - } - authenticated_provider.ec2_client.create_security_group.return_value = { - "GroupId": "sg-12345678" - } - - vpc_config = authenticated_provider._create_or_get_vpc_for_eks(cluster_name) - - assert vpc_config["vpc_id"] == "vpc-12345678" - assert "subnet_ids" in vpc_config - assert "security_group_ids" in vpc_config - - def test_eks_cluster_lifecycle(self, authenticated_provider): - """Test complete EKS cluster lifecycle (Create, Read, Update, Delete).""" - cluster_name = "test-eks-cluster" - - # Mock cluster creation - authenticated_provider._create_or_get_eks_cluster_role = Mock( - return_value="arn:aws:iam::123456789012:role/clustrix-eks-cluster-role" - ) - authenticated_provider._create_or_get_vpc_for_eks = Mock( - return_value={ - "vpc_id": "vpc-12345678", - "subnet_ids": ["subnet-1", "subnet-2"], - "security_group_ids": ["sg-12345678"], - } - ) - - authenticated_provider.eks_client.create_cluster.return_value = { - "cluster": { - "name": cluster_name, - "status": "CREATING", - "endpoint": "", - "arn": f"arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}", - "version": "1.27", - "createdAt": datetime.now(timezone.utc), - } - } - - # Test Create operation - result = authenticated_provider.create_eks_cluster( - cluster_name, node_count=3, instance_type="t3.medium" - ) - - assert result["cluster_name"] == cluster_name - assert result["status"] == "CREATING" - assert result["node_count"] == 3 - assert result["instance_type"] == "t3.medium" - - # Test Read operation (get status) - authenticated_provider.eks_client.describe_cluster.return_value = { - "cluster": { - "name": cluster_name, - "status": "ACTIVE", - "endpoint": "https://test.eks.amazonaws.com", - "version": "1.27", - "arn": f"arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}", - "createdAt": datetime.now(timezone.utc), - } - } - authenticated_provider.eks_client.list_nodegroups.return_value = { - "nodegroups": ["test-nodegroup"] - } - authenticated_provider.eks_client.describe_nodegroup.return_value = { - "nodegroup": {"scalingConfig": {"desiredSize": 3}} - } - - status = authenticated_provider.get_cluster_status(cluster_name, "eks") - assert status["status"] == "ACTIVE" - assert status["node_count"] == 3 - - # Test Delete operation - authenticated_provider.eks_client.list_nodegroups.return_value = { - "nodegroups": ["test-nodegroup"] - } - - result = authenticated_provider.delete_cluster(cluster_name, "eks") - assert result is True - - # Verify deletion calls - authenticated_provider.eks_client.delete_nodegroup.assert_called() - authenticated_provider.eks_client.delete_cluster.assert_called_with( - name=cluster_name - ) - - def test_ec2_instance_lifecycle(self, authenticated_provider): - """Test complete EC2 instance lifecycle (Create, Read, Delete).""" - instance_name = "test-ec2-instance" - - # Mock AMI lookup - authenticated_provider.ec2_client.describe_images.return_value = { - "Images": [ - {"ImageId": "ami-12345678", "CreationDate": "2023-01-01T00:00:00.000Z"} - ] - } - - # Mock instance creation - authenticated_provider.ec2_client.run_instances.return_value = { - "Instances": [ - { - "InstanceId": "i-12345678", - "State": {"Name": "pending"}, - "InstanceType": "t3.medium", - } - ] - } - - # Mock waiter - mock_waiter = Mock() - authenticated_provider.ec2_client.get_waiter.return_value = mock_waiter - - # Mock updated instance info - authenticated_provider.ec2_client.describe_instances.return_value = { - "Reservations": [ - { - "Instances": [ - { - "InstanceId": "i-12345678", - "State": {"Name": "running"}, - "PublicIpAddress": "1.2.3.4", - "PrivateIpAddress": "10.0.1.100", - "InstanceType": "t3.medium", - } - ] - } - ] - } - - # Test Create operation - result = authenticated_provider.create_ec2_instance( - instance_name, instance_type="t3.medium" - ) - - assert result["instance_id"] == "i-12345678" - assert result["instance_name"] == instance_name - assert result["public_ip"] == "1.2.3.4" - assert result["state"] == "running" - - # Test Read operation (get status) - status = authenticated_provider.get_cluster_status("i-12345678", "ec2") - assert status["instance_id"] == "i-12345678" - assert status["status"] == "running" - - # Test Delete operation - result = authenticated_provider.delete_cluster("i-12345678", "ec2") - assert result is True - authenticated_provider.ec2_client.terminate_instances.assert_called_with( - InstanceIds=["i-12345678"] - ) - - def test_list_operations(self, authenticated_provider): - """Test List operations for both EKS and EC2 resources.""" - # Mock EKS cluster listing - authenticated_provider.eks_client.list_clusters.return_value = { - "clusters": ["cluster-1", "cluster-2"] - } - - # Mock EC2 instance listing - authenticated_provider.ec2_client.describe_instances.return_value = { - "Reservations": [ - { - "Instances": [ - { - "InstanceId": "i-12345678", - "State": {"Name": "running"}, - "Tags": [{"Key": "Name", "Value": "test-instance"}], - } - ] - } - ] - } - - clusters = authenticated_provider.list_clusters() - - assert len(clusters) >= 2 # At least EKS clusters - eks_clusters = [c for c in clusters if c["type"] == "eks"] - assert len(eks_clusters) == 2 - - def test_configuration_generation(self, authenticated_provider): - """Test Clustrix configuration generation for AWS resources.""" - # Test EKS configuration - eks_config = authenticated_provider.get_cluster_config("test-cluster", "eks") - - assert eks_config["cluster_type"] == "kubernetes" - assert "AWS EKS" in eks_config["name"] - assert eks_config["k8s_namespace"] == "default" - assert eks_config["provider"] == "aws" - assert "cluster_name" in eks_config["provider_config"] - - # Test EC2 configuration - authenticated_provider.ec2_client.describe_instances.return_value = { - "Reservations": [ - { - "Instances": [ - { - "InstanceId": "i-12345678", - "PublicIpAddress": "1.2.3.4", - "State": {"Name": "running"}, - } - ] - } - ] - } - - ec2_config = authenticated_provider.get_cluster_config("i-12345678", "ec2") - - assert ec2_config["cluster_type"] == "ssh" - assert "AWS EC2" in ec2_config["name"] - assert ec2_config["cluster_host"] == "1.2.3.4" - assert ec2_config["username"] == "ec2-user" - assert ec2_config["provider"] == "aws" - - def test_cost_estimation_comprehensive(self, provider): - """Test comprehensive cost estimation scenarios.""" - # Test EKS cost calculation - eks_cost = provider.estimate_cost( - cluster_type="eks", instance_type="c5.xlarge", node_count=5, hours=24 - ) - - expected_control_plane = 0.10 * 24 # $0.10/hour for 24 hours - expected_nodes = ( - 0.170 * 5 * 24 - ) # $0.170/hour per c5.xlarge * 5 nodes * 24 hours - expected_total = expected_control_plane + expected_nodes - - assert eks_cost["control_plane"] == expected_control_plane - assert eks_cost["nodes"] == expected_nodes - assert eks_cost["total"] == expected_total - - # Test EC2 cost calculation - ec2_cost = provider.estimate_cost( - cluster_type="ec2", instance_type="m5.2xlarge", hours=168 # 1 week - ) - - # m5.2xlarge not in default pricing, should use default - expected_total = 0.10 * 168 - assert ec2_cost["total"] == expected_total - - # Test with known instance type - ec2_cost_known = provider.estimate_cost( - cluster_type="ec2", instance_type="t3.large", hours=12 - ) - - expected_total_known = 0.0832 * 12 # t3.large price - assert ec2_cost_known["total"] == expected_total_known - - def test_region_and_instance_operations(self, provider): - """Test region and instance type operations.""" - # Test when not authenticated - regions = provider.get_available_regions() - assert "us-east-1" in regions - assert "us-west-2" in regions - assert isinstance(regions, list) - - instance_types = provider.get_available_instance_types() - assert "t3.micro" in instance_types - assert "c5.large" in instance_types - assert isinstance(instance_types, list) - - @patch("clustrix.cloud_providers.aws.BOTO3_AVAILABLE", True) - @patch("clustrix.cloud_providers.aws.boto3") - def test_region_and_instance_operations_authenticated(self, mock_boto3, provider): - """Test region and instance operations when authenticated.""" - # Setup authentication - mock_session = Mock() - mock_boto3.Session.return_value = mock_session - mock_ec2_client = Mock() - mock_iam_client = Mock() - mock_session.client.side_effect = lambda service: { - "ec2": mock_ec2_client, - "iam": mock_iam_client, - }.get(service, Mock()) - mock_iam_client.get_user.return_value = {"User": {"UserName": "test"}} - - provider.authenticate(access_key_id="test", secret_access_key="test") - - # Mock region listing - mock_ec2_client.describe_regions.return_value = { - "Regions": [ - {"RegionName": "us-east-1"}, - {"RegionName": "us-west-2"}, - {"RegionName": "eu-west-1"}, - {"RegionName": "ap-northeast-1"}, - ] - } - - regions = provider.get_available_regions() - assert "us-east-1" in regions - assert "us-west-2" in regions - # Priority regions should come first - assert regions.index("us-east-1") < regions.index("ap-northeast-1") - - # Mock instance type listing - mock_ec2_client.describe_instance_type_offerings.return_value = { - "InstanceTypeOfferings": [ - {"InstanceType": "t3.micro"}, - {"InstanceType": "t3.small"}, - {"InstanceType": "t3.medium"}, - {"InstanceType": "c5.large"}, - {"InstanceType": "c5.xlarge"}, - {"InstanceType": "m5.large"}, - ] - } - - instance_types = provider.get_available_instance_types() - assert "t3.micro" in instance_types - assert "c5.large" in instance_types - assert len(instance_types) <= 30 # Should be limited - - def test_error_handling_comprehensive(self, authenticated_provider): - """Test comprehensive error handling scenarios.""" - from clustrix.cloud_providers.aws import ClientError - - # Test cluster operations with unauthenticated provider - provider = AWSProvider() - - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_eks_cluster("test") - - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_ec2_instance("test") - - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.get_cluster_status("test") - - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.list_clusters() - - # Test EKS cluster not found - authenticated_provider.eks_client.describe_cluster.side_effect = ClientError( - {"Error": {"Code": "ResourceNotFoundException"}}, "DescribeCluster" - ) - - status = authenticated_provider.get_cluster_status("nonexistent", "eks") - assert status["status"] == "NOT_FOUND" - - # Test successful deletion of non-existent cluster - authenticated_provider.eks_client.list_nodegroups.return_value = { - "nodegroups": [] - } - authenticated_provider.eks_client.delete_cluster.side_effect = ClientError( - {"Error": {"Code": "ResourceNotFoundException"}}, "DeleteCluster" - ) - - result = authenticated_provider.delete_cluster("nonexistent", "eks") - assert result is True # Should succeed for non-existent resources - - def test_subnet_and_security_group_operations(self, authenticated_provider): - """Test subnet and security group creation following AWS best practices.""" - vpc_id = "vpc-12345678" - cluster_name = "test-cluster" - - # Test subnet creation - authenticated_provider.ec2_client.describe_subnets.return_value = { - "Subnets": [] - } - authenticated_provider.ec2_client.create_subnet.side_effect = [ - {"Subnet": {"SubnetId": "subnet-1"}}, - {"Subnet": {"SubnetId": "subnet-2"}}, - ] - - subnet_ids = authenticated_provider._create_eks_subnets(vpc_id, cluster_name) - assert len(subnet_ids) == 2 - assert "subnet-1" in subnet_ids - assert "subnet-2" in subnet_ids - - # Verify proper tagging - tag_calls = authenticated_provider.ec2_client.create_tags.call_args_list - assert len(tag_calls) >= 2 # Should tag both subnets - - # Test security group creation - authenticated_provider.ec2_client.describe_security_groups.return_value = { - "SecurityGroups": [] - } - authenticated_provider.ec2_client.create_security_group.return_value = { - "GroupId": "sg-12345678" - } - - sg_ids = authenticated_provider._create_eks_security_groups( - vpc_id, cluster_name - ) - assert len(sg_ids) == 1 - assert "sg-12345678" in sg_ids - - def test_cluster_type_validation(self, authenticated_provider): - """Test cluster type validation across all operations.""" - # Test invalid cluster types - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.create_cluster("test", cluster_type="invalid") - - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.delete_cluster("test", cluster_type="invalid") - - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_status("test", cluster_type="invalid") - - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_config("test", cluster_type="invalid") - - def test_credential_validation_edge_cases(self, authenticated_provider): - """Test credential validation edge cases.""" - # Test when IAM client is None - authenticated_provider.iam_client = None - assert authenticated_provider.validate_credentials() is False - - # Test when not authenticated - authenticated_provider.authenticated = False - assert authenticated_provider.validate_credentials() is False - - @pytest.mark.integration - @pytest.mark.skipif( - not os.environ.get("AWS_ACCESS_KEY_ID"), - reason="Real AWS credentials required for integration test", - ) - def test_integration_with_real_aws(self, provider, real_aws_credentials): - """Integration test with real AWS services when credentials available.""" - if not real_aws_credentials: - pytest.skip("No real AWS credentials available") - - # Authenticate with real credentials - result = provider.authenticate(**real_aws_credentials) - if not result: - pytest.skip("Authentication failed with provided credentials") - - # Test listing real regions - regions = provider.get_available_regions() - assert len(regions) > 10 # AWS has many regions - assert "us-east-1" in regions - - # Test listing real instance types - instance_types = provider.get_available_instance_types() - assert len(instance_types) > 10 - assert any(t.startswith("t3.") for t in instance_types) - - # Test credential validation - assert provider.validate_credentials() is True - - # Test listing existing clusters (should not fail) - clusters = provider.list_clusters() - assert isinstance(clusters, list) # Should return a list, even if empty diff --git a/tests/test_cloud_providers_azure.py b/tests/test_cloud_providers_azure.py deleted file mode 100644 index c6a5857e..00000000 --- a/tests/test_cloud_providers_azure.py +++ /dev/null @@ -1,1075 +0,0 @@ -import pytest -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime, timezone - -from clustrix.cloud_providers.azure import AzureProvider - - -class TestAzureProvider: - """Test Azure provider functionality.""" - - @pytest.fixture - def provider(self): - """Create AzureProvider instance.""" - return AzureProvider() - - @pytest.fixture - def authenticated_provider(self): - """Create authenticated AzureProvider instance.""" - provider = AzureProvider() - provider.authenticated = True - provider.subscription_id = "test-subscription-id" - provider.client_id = "test-client-id" - provider.tenant_id = "test-tenant-id" - provider.region = "eastus" - provider.resource_group = "test-rg" - provider.compute_client = Mock() - provider.resource_client = Mock() - provider.network_client = Mock() - provider.container_client = Mock() - provider.credential = Mock() - provider.credentials = { - "subscription_id": "test-subscription-id", - "client_id": "test-client-id", - "client_secret": "test-secret", - "tenant_id": "test-tenant-id", - } - return provider - - def test_initialization(self, provider): - """Test provider initialization.""" - assert provider.subscription_id is None - assert provider.client_id is None - assert provider.tenant_id is None - assert provider.region == "eastus" - assert provider.resource_group == "clustrix-rg" - assert provider.compute_client is None - assert provider.resource_client is None - assert provider.network_client is None - assert provider.container_client is None - assert provider.credential is None - assert not provider.authenticated - - @patch("clustrix.cloud_providers.azure.AZURE_AVAILABLE", True) - @patch("clustrix.cloud_providers.azure.ClientSecretCredential") - @patch("clustrix.cloud_providers.azure.ComputeManagementClient") - @patch("clustrix.cloud_providers.azure.ResourceManagementClient") - @patch("clustrix.cloud_providers.azure.NetworkManagementClient") - @patch("clustrix.cloud_providers.azure.ContainerServiceClient") - def test_authenticate_success( - self, - mock_container, - mock_network, - mock_resource, - mock_compute, - mock_credential, - provider, - ): - """Test successful authentication.""" - # Mock credential - mock_cred = Mock() - mock_credential.return_value = mock_cred - - # Mock clients - mock_compute_client = Mock() - mock_resource_client = Mock() - mock_network_client = Mock() - mock_container_client = Mock() - - mock_compute.return_value = mock_compute_client - mock_resource.return_value = mock_resource_client - mock_network.return_value = mock_network_client - mock_container.return_value = mock_container_client - - # Mock successful resource group list - mock_resource_client.resource_groups.list.return_value = [] - - result = provider.authenticate( - subscription_id="test-subscription", - client_id="test-client", - client_secret="test-secret", - tenant_id="test-tenant", - region="westus", - resource_group="custom-rg", - ) - - assert result is True - assert provider.authenticated is True - assert provider.subscription_id == "test-subscription" - assert provider.client_id == "test-client" - assert provider.tenant_id == "test-tenant" - assert provider.region == "westus" - assert provider.resource_group == "custom-rg" - - # Verify clients were created - mock_credential.assert_called_once_with( - tenant_id="test-tenant", - client_id="test-client", - client_secret="test-secret", - ) - - @patch("clustrix.cloud_providers.azure.AZURE_AVAILABLE", False) - def test_authenticate_azure_not_available(self, provider): - """Test authentication when Azure packages not available.""" - result = provider.authenticate( - subscription_id="test-subscription", - client_id="test-client", - client_secret="test-secret", - tenant_id="test-tenant", - ) - - assert result is False - assert not provider.authenticated - - def test_authenticate_missing_credentials(self, provider): - """Test authentication with missing credentials.""" - # Test missing subscription_id - result = provider.authenticate( - client_id="test-client", - client_secret="test-secret", - tenant_id="test-tenant", - ) - assert result is False - - # Test missing client_id - result = provider.authenticate( - subscription_id="test-subscription", - client_secret="test-secret", - tenant_id="test-tenant", - ) - assert result is False - - # Test missing client_secret - result = provider.authenticate( - subscription_id="test-subscription", - client_id="test-client", - tenant_id="test-tenant", - ) - assert result is False - - # Test missing tenant_id - result = provider.authenticate( - subscription_id="test-subscription", - client_id="test-client", - client_secret="test-secret", - ) - assert result is False - - @patch("clustrix.cloud_providers.azure.AZURE_AVAILABLE", True) - @patch("clustrix.cloud_providers.azure.ClientSecretCredential") - def test_authenticate_credentials_error(self, mock_credential, provider): - """Test authentication with credential error.""" - from clustrix.cloud_providers.azure import ClientAuthenticationError - - mock_credential.side_effect = ClientAuthenticationError("Invalid credentials") - - result = provider.authenticate( - subscription_id="test-subscription", - client_id="test-client", - client_secret="test-secret", - tenant_id="test-tenant", - ) - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.azure.AZURE_AVAILABLE", True) - @patch("clustrix.cloud_providers.azure.ClientSecretCredential") - @patch("clustrix.cloud_providers.azure.ResourceManagementClient") - def test_authenticate_api_test_failure( - self, mock_resource, mock_credential, provider - ): - """Test authentication when API test fails.""" - mock_cred = Mock() - mock_credential.return_value = mock_cred - - mock_resource_client = Mock() - mock_resource.return_value = mock_resource_client - mock_resource_client.resource_groups.list.side_effect = Exception("API error") - - with ( - patch("clustrix.cloud_providers.azure.ComputeManagementClient"), - patch("clustrix.cloud_providers.azure.NetworkManagementClient"), - patch("clustrix.cloud_providers.azure.ContainerServiceClient"), - ): - result = provider.authenticate( - subscription_id="test-subscription", - client_id="test-client", - client_secret="test-secret", - tenant_id="test-tenant", - ) - - assert result is False - assert not provider.authenticated - - def test_validate_credentials_success(self, authenticated_provider): - """Test successful credential validation.""" - authenticated_provider.resource_client.resource_groups.list.return_value = [] - - result = authenticated_provider.validate_credentials() - - assert result is True - - def test_validate_credentials_failure(self, authenticated_provider): - """Test failed credential validation.""" - authenticated_provider.resource_client.resource_groups.list.side_effect = ( - Exception("API error") - ) - - result = authenticated_provider.validate_credentials() - - assert result is False - - def test_validate_credentials_not_authenticated(self, provider): - """Test credential validation when not authenticated.""" - result = provider.validate_credentials() - - assert result is False - - def test_ensure_resource_group_exists(self, authenticated_provider): - """Test ensuring resource group when it already exists.""" - # Mock resource group exists - mock_rg = Mock() - authenticated_provider.resource_client.resource_groups.get.return_value = ( - mock_rg - ) - - result = authenticated_provider._ensure_resource_group() - - assert result is True - authenticated_provider.resource_client.resource_groups.get.assert_called_once_with( - "test-rg" - ) - - def test_ensure_resource_group_create(self, authenticated_provider): - """Test creating resource group when it doesn't exist.""" - from clustrix.cloud_providers.azure import ResourceNotFoundError - - # Mock resource group doesn't exist - authenticated_provider.resource_client.resource_groups.get.side_effect = ( - ResourceNotFoundError() - ) - - result = authenticated_provider._ensure_resource_group() - - assert result is True - authenticated_provider.resource_client.resource_groups.create_or_update.assert_called_once_with( - "test-rg", {"location": "eastus", "tags": {"created_by": "clustrix"}} - ) - - def test_ensure_resource_group_error(self, authenticated_provider): - """Test resource group creation error.""" - authenticated_provider.resource_client.resource_groups.get.side_effect = ( - Exception("API error") - ) - - result = authenticated_provider._ensure_resource_group() - - assert result is False - - def test_create_vm_success(self, authenticated_provider): - """Test successful VM creation.""" - # Mock resource group check - with patch.object( - authenticated_provider, "_ensure_resource_group", return_value=True - ): - # Mock network resources - mock_vnet = Mock() - mock_vnet.subnets = [Mock()] - mock_vnet.subnets[0].id = ( - "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.Network/virtualNetworks/test-vm-vnet/subnets/test-vm-subnet" - ) - authenticated_provider.network_client.virtual_networks.begin_create_or_update.return_value.result.return_value = ( - mock_vnet - ) - - mock_public_ip = Mock() - mock_public_ip.id = "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.Network/publicIPAddresses/test-vm-ip" - mock_public_ip.ip_address = "1.2.3.4" - authenticated_provider.network_client.public_ip_addresses.begin_create_or_update.return_value.result.return_value = ( - mock_public_ip - ) - - mock_nsg = Mock() - mock_nsg.id = "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.Network/networkSecurityGroups/test-vm-nsg" - authenticated_provider.network_client.network_security_groups.begin_create_or_update.return_value.result.return_value = ( - mock_nsg - ) - - mock_nic = Mock() - mock_nic.id = "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.Network/networkInterfaces/test-vm-nic" - authenticated_provider.network_client.network_interfaces.begin_create_or_update.return_value.result.return_value = ( - mock_nic - ) - - # Mock VM creation - mock_vm = Mock() - mock_vm.id = "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.Compute/virtualMachines/test-vm" - authenticated_provider.compute_client.virtual_machines.begin_create_or_update.return_value.result.return_value = ( - mock_vm - ) - - with patch("clustrix.cloud_providers.azure.datetime") as mock_datetime: - mock_datetime.now.return_value.isoformat.return_value = ( - "2024-01-01T00:00:00+00:00" - ) - mock_datetime.timezone = timezone - - result = authenticated_provider.create_vm( - vm_name="test-vm", - vm_size="Standard_D2s_v3", - admin_username="testuser", - admin_password="testpass123", - ) - - assert result["vm_name"] == "test-vm" - assert result["vm_size"] == "Standard_D2s_v3" - assert result["region"] == "eastus" - assert result["resource_group"] == "test-rg" - assert result["status"] == "creating" - assert result["public_ip"] == "1.2.3.4" - assert result["admin_username"] == "testuser" - - def test_create_vm_not_authenticated(self, provider): - """Test VM creation when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_vm("test-vm") - - def test_create_vm_resource_group_failure(self, authenticated_provider): - """Test VM creation when resource group creation fails.""" - with patch.object( - authenticated_provider, "_ensure_resource_group", return_value=False - ): - with pytest.raises( - RuntimeError, match="Failed to create or access resource group" - ): - authenticated_provider.create_vm("test-vm") - - def test_create_vm_exception(self, authenticated_provider): - """Test VM creation with exception.""" - with patch.object( - authenticated_provider, "_ensure_resource_group", return_value=True - ): - authenticated_provider.network_client.virtual_networks.begin_create_or_update.side_effect = Exception( - "Network error" - ) - - with pytest.raises(Exception, match="Network error"): - authenticated_provider.create_vm("test-vm") - - def test_create_aks_cluster_success(self, authenticated_provider): - """Test successful AKS cluster creation.""" - mock_operation = Mock() - mock_operation.__str__ = Mock(return_value="operation-aks-12345") - authenticated_provider.container_client.managed_clusters.begin_create_or_update.return_value = ( - mock_operation - ) - - with patch("clustrix.cloud_providers.azure.datetime") as mock_datetime: - mock_datetime.now.return_value.isoformat.return_value = ( - "2024-01-01T00:00:00+00:00" - ) - mock_datetime.timezone = timezone - - result = authenticated_provider.create_aks_cluster( - cluster_name="test-cluster", - node_count=5, - node_vm_size="Standard_DS2_v2", - kubernetes_version="1.25.0", - ) - - assert result["cluster_name"] == "test-cluster" - assert result["status"] == "creating" - assert result["region"] == "eastus" - assert result["provider"] == "azure" - assert result["cluster_type"] == "aks" - assert result["resource_group"] == "test-rg" - assert result["node_count"] == 5 - assert result["node_vm_size"] == "Standard_DS2_v2" - assert result["kubernetes_version"] == "1.25.0" - - def test_create_aks_cluster_not_authenticated(self, provider): - """Test AKS cluster creation when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_aks_cluster("test-cluster") - - def test_create_aks_cluster_exception(self, authenticated_provider): - """Test AKS cluster creation with exception.""" - authenticated_provider.container_client.managed_clusters.begin_create_or_update.side_effect = Exception( - "API error" - ) - - with pytest.raises(Exception, match="API error"): - authenticated_provider.create_aks_cluster("test-cluster") - - def test_create_cluster_vm(self, authenticated_provider): - """Test create_cluster with VM type.""" - with patch.object(authenticated_provider, "create_vm") as mock_create: - mock_create.return_value = {"vm_id": "test-vm"} - - result = authenticated_provider.create_cluster( - "test-cluster", cluster_type="vm", vm_size="Standard_D2s_v3" - ) - - mock_create.assert_called_once_with( - "test-cluster", vm_size="Standard_D2s_v3" - ) - assert result == {"vm_id": "test-vm"} - - def test_create_cluster_aks(self, authenticated_provider): - """Test create_cluster with AKS type.""" - with patch.object(authenticated_provider, "create_aks_cluster") as mock_create: - mock_create.return_value = {"cluster_name": "test-cluster"} - - result = authenticated_provider.create_cluster( - "test-cluster", cluster_type="aks", node_count=3 - ) - - mock_create.assert_called_once_with("test-cluster", node_count=3) - assert result == {"cluster_name": "test-cluster"} - - def test_create_cluster_unknown_type(self, authenticated_provider): - """Test create_cluster with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.create_cluster( - "test-cluster", cluster_type="unknown" - ) - - def test_delete_cluster_vm_success(self, authenticated_provider): - """Test successful VM deletion.""" - # Mock successful VM deletion - authenticated_provider.compute_client.virtual_machines.begin_delete.return_value.result.return_value = ( - None - ) - - # Mock successful associated resource deletion - authenticated_provider.network_client.network_interfaces.begin_delete.return_value.result.return_value = ( - None - ) - authenticated_provider.network_client.public_ip_addresses.begin_delete.return_value.result.return_value = ( - None - ) - authenticated_provider.network_client.network_security_groups.begin_delete.return_value.result.return_value = ( - None - ) - authenticated_provider.network_client.virtual_networks.begin_delete.return_value.result.return_value = ( - None - ) - - result = authenticated_provider.delete_cluster("test-vm", cluster_type="vm") - - assert result is True - authenticated_provider.compute_client.virtual_machines.begin_delete.assert_called_once_with( - "test-rg", "test-vm" - ) - - def test_delete_cluster_vm_associated_resources_fail(self, authenticated_provider): - """Test VM deletion when associated resources fail to delete.""" - # Mock successful VM deletion - authenticated_provider.compute_client.virtual_machines.begin_delete.return_value.result.return_value = ( - None - ) - - # Mock failure in associated resource deletion - authenticated_provider.network_client.network_interfaces.begin_delete.side_effect = Exception( - "Resource not found" - ) - - result = authenticated_provider.delete_cluster("test-vm", cluster_type="vm") - - # Should still return True even if associated resources fail - assert result is True - - def test_delete_cluster_aks_success(self, authenticated_provider): - """Test successful AKS cluster deletion.""" - mock_operation = Mock() - authenticated_provider.container_client.managed_clusters.begin_delete.return_value = ( - mock_operation - ) - - result = authenticated_provider.delete_cluster( - "test-cluster", cluster_type="aks" - ) - - assert result is True - authenticated_provider.container_client.managed_clusters.begin_delete.assert_called_once_with( - resource_group_name="test-rg", resource_name="test-cluster" - ) - - def test_delete_cluster_not_authenticated(self, provider): - """Test cluster deletion when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.delete_cluster("test-cluster") - - def test_delete_cluster_unknown_type(self, authenticated_provider): - """Test cluster deletion with unknown type.""" - result = authenticated_provider.delete_cluster( - "test-cluster", cluster_type="unknown" - ) - assert result is False - - def test_delete_cluster_exception(self, authenticated_provider): - """Test cluster deletion with exception.""" - authenticated_provider.compute_client.virtual_machines.begin_delete.side_effect = Exception( - "API error" - ) - - result = authenticated_provider.delete_cluster("test-vm", cluster_type="vm") - - assert result is False - - def test_get_cluster_status_vm_success(self, authenticated_provider): - """Test successful VM status retrieval.""" - mock_vm = Mock() - mock_vm.provisioning_state = "Succeeded" - mock_vm.hardware_profile = Mock() - mock_vm.hardware_profile.vm_size = "Standard_D2s_v3" - mock_vm.location = "eastus" - authenticated_provider.compute_client.virtual_machines.get.return_value = ( - mock_vm - ) - - result = authenticated_provider.get_cluster_status("test-vm", cluster_type="vm") - - assert result["vm_name"] == "test-vm" - assert result["status"] == "succeeded" - assert result["vm_size"] == "Standard_D2s_v3" - assert result["region"] == "eastus" - assert result["resource_group"] == "test-rg" - assert result["provider"] == "azure" - assert result["cluster_type"] == "vm" - - def test_get_cluster_status_vm_missing_fields(self, authenticated_provider): - """Test VM status with missing fields.""" - mock_vm = Mock() - mock_vm.provisioning_state = None - mock_vm.hardware_profile = None - mock_vm.location = "eastus" - authenticated_provider.compute_client.virtual_machines.get.return_value = ( - mock_vm - ) - - result = authenticated_provider.get_cluster_status("test-vm", cluster_type="vm") - - assert result["status"] == "unknown" - assert result["vm_size"] == "unknown" - - def test_get_cluster_status_aks_success(self, authenticated_provider): - """Test successful AKS cluster status retrieval.""" - mock_cluster = Mock() - mock_cluster.provisioning_state = "Succeeded" - mock_cluster.kubernetes_version = "1.25.0" - mock_cluster.agent_pool_profiles = [Mock()] - mock_cluster.agent_pool_profiles[0].count = 3 - mock_cluster.fqdn = "test-cluster-123.hcp.eastus.azmk8s.io" - mock_cluster.location = "eastus" - authenticated_provider.container_client.managed_clusters.get.return_value = ( - mock_cluster - ) - - result = authenticated_provider.get_cluster_status( - "test-cluster", cluster_type="aks" - ) - - assert result["cluster_name"] == "test-cluster" - assert result["status"] == "succeeded" - assert result["kubernetes_version"] == "1.25.0" - assert result["node_count"] == 3 - assert result["fqdn"] == "test-cluster-123.hcp.eastus.azmk8s.io" - assert result["region"] == "eastus" - assert result["resource_group"] == "test-rg" - assert result["provider"] == "azure" - assert result["cluster_type"] == "aks" - - def test_get_cluster_status_aks_no_agent_pools(self, authenticated_provider): - """Test AKS cluster status with no agent pools.""" - mock_cluster = Mock() - mock_cluster.provisioning_state = "Succeeded" - mock_cluster.kubernetes_version = "1.25.0" - mock_cluster.agent_pool_profiles = [] - mock_cluster.fqdn = "test-cluster-123.hcp.eastus.azmk8s.io" - mock_cluster.location = "eastus" - authenticated_provider.container_client.managed_clusters.get.return_value = ( - mock_cluster - ) - - result = authenticated_provider.get_cluster_status( - "test-cluster", cluster_type="aks" - ) - - assert result["node_count"] == 0 - - def test_get_cluster_status_not_authenticated(self, provider): - """Test cluster status when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.get_cluster_status("test-cluster") - - def test_get_cluster_status_unknown_type(self, authenticated_provider): - """Test cluster status with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_status( - "test-cluster", cluster_type="unknown" - ) - - def test_get_cluster_status_exception(self, authenticated_provider): - """Test cluster status with exception.""" - authenticated_provider.compute_client.virtual_machines.get.side_effect = ( - Exception("API error") - ) - - with pytest.raises(Exception, match="API error"): - authenticated_provider.get_cluster_status("test-vm", cluster_type="vm") - - def test_list_clusters_success(self, authenticated_provider): - """Test successful cluster listing.""" - # Mock VMs - mock_vm = Mock() - mock_vm.name = "clustrix-vm" - mock_vm.id = "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.Compute/virtualMachines/clustrix-vm" - mock_vm.tags = {"created_by": "clustrix"} - mock_vm.provisioning_state = "Succeeded" - mock_vm.hardware_profile = Mock() - mock_vm.hardware_profile.vm_size = "Standard_D2s_v3" - mock_vm.location = "eastus" - authenticated_provider.compute_client.virtual_machines.list.return_value = [ - mock_vm - ] - - # Mock AKS clusters - mock_cluster = Mock() - mock_cluster.name = "clustrix-aks" - mock_cluster.id = "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.ContainerService/managedClusters/clustrix-aks" - mock_cluster.tags = {"created_by": "clustrix", "cluster_name": "test-cluster"} - mock_cluster.provisioning_state = "Succeeded" - mock_cluster.kubernetes_version = "1.25.0" - mock_cluster.agent_pool_profiles = [Mock()] - mock_cluster.agent_pool_profiles[0].count = 3 - mock_cluster.fqdn = "clustrix-aks-123.hcp.eastus.azmk8s.io" - mock_cluster.location = "eastus" - authenticated_provider.container_client.managed_clusters.list_by_resource_group.return_value = [ - mock_cluster - ] - - result = authenticated_provider.list_clusters() - - assert len(result) == 2 - - # Check VM - vm_result = next(r for r in result if r["type"] == "vm") - assert vm_result["name"] == "clustrix-vm" - assert ( - vm_result["vm_id"] - == "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.Compute/virtualMachines/clustrix-vm" - ) - assert vm_result["status"] == "succeeded" - assert vm_result["vm_size"] == "Standard_D2s_v3" - - # Check AKS cluster - aks_result = next(r for r in result if r["type"] == "aks") - assert aks_result["name"] == "clustrix-aks" - assert ( - aks_result["cluster_id"] - == "/subscriptions/test/resourceGroups/test-rg/providers/Microsoft.ContainerService/managedClusters/clustrix-aks" - ) - assert aks_result["status"] == "succeeded" - assert aks_result["kubernetes_version"] == "1.25.0" - - def test_list_clusters_no_clustrix_resources(self, authenticated_provider): - """Test cluster listing with no Clustrix-managed resources.""" - # Mock VM without clustrix tag - mock_vm = Mock() - mock_vm.name = "other-vm" - mock_vm.tags = {"created_by": "other"} - authenticated_provider.compute_client.virtual_machines.list.return_value = [ - mock_vm - ] - - # Mock AKS cluster without clustrix tag - mock_cluster = Mock() - mock_cluster.name = "other-aks" - mock_cluster.tags = {"created_by": "other"} - authenticated_provider.container_client.managed_clusters.list_by_resource_group.return_value = [ - mock_cluster - ] - - result = authenticated_provider.list_clusters() - - assert len(result) == 0 - - def test_list_clusters_not_authenticated(self, provider): - """Test cluster listing when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.list_clusters() - - def test_list_clusters_exceptions(self, authenticated_provider): - """Test cluster listing with exceptions.""" - # Mock VM list exception - authenticated_provider.compute_client.virtual_machines.list.side_effect = ( - Exception("VM API error") - ) - - # Mock AKS list exception - authenticated_provider.container_client.managed_clusters.list_by_resource_group.side_effect = Exception( - "AKS API error" - ) - - result = authenticated_provider.list_clusters() - - assert result == [] - - def test_get_cluster_config_vm_success(self, authenticated_provider): - """Test successful VM cluster config retrieval.""" - mock_vm = Mock() - authenticated_provider.compute_client.virtual_machines.get.return_value = ( - mock_vm - ) - - mock_public_ip = Mock() - mock_public_ip.ip_address = "1.2.3.4" - authenticated_provider.network_client.public_ip_addresses.get.return_value = ( - mock_public_ip - ) - - result = authenticated_provider.get_cluster_config("test-vm", cluster_type="vm") - - assert result["name"] == "Azure VM - test-vm" - assert result["cluster_type"] == "ssh" - assert result["cluster_host"] == "1.2.3.4" - assert result["username"] == "azureuser" - assert result["cluster_port"] == 22 - assert result["default_cores"] == 2 - assert result["default_memory"] == "4GB" - assert result["remote_work_dir"] == "/home/azureuser/clustrix" - assert result["package_manager"] == "conda" - assert result["cost_monitoring"] is True - assert result["provider"] == "azure" - assert result["provider_config"]["vm_name"] == "test-vm" - assert result["provider_config"]["resource_group"] == "test-rg" - - def test_get_cluster_config_vm_no_public_ip(self, authenticated_provider): - """Test VM cluster config with no public IP.""" - mock_vm = Mock() - authenticated_provider.compute_client.virtual_machines.get.return_value = ( - mock_vm - ) - authenticated_provider.network_client.public_ip_addresses.get.side_effect = ( - Exception("IP not found") - ) - - # A VM whose public IP cannot be read has no host to connect to. - # This used to return cluster_host "" (see #119). - with pytest.raises(RuntimeError, match="no readable public IP"): - authenticated_provider.get_cluster_config("test-vm", cluster_type="vm") - - def test_get_cluster_config_vm_exception(self, authenticated_provider): - """Test VM cluster config with exception.""" - authenticated_provider.compute_client.virtual_machines.get.side_effect = ( - Exception("VM not found") - ) - - # This used to return cluster_host "placeholder.azure.com", which - # clustrix then tried to SSH into (see #119). - with pytest.raises(RuntimeError, match="Could not determine"): - authenticated_provider.get_cluster_config("test-vm", cluster_type="vm") - - def test_get_cluster_config_aks(self, authenticated_provider): - """Test AKS cluster config retrieval.""" - result = authenticated_provider.get_cluster_config( - "test-cluster", cluster_type="aks" - ) - - assert result["name"] == "Azure AKS - test-cluster" - assert result["cluster_type"] == "kubernetes" - assert result["cluster_host"] == "test-cluster.aks.eastus.azure.com" - assert result["cluster_port"] == 443 - assert result["k8s_namespace"] == "default" - assert result["k8s_image"] == "python:3.11" - assert result["default_cores"] == 2 - assert result["default_memory"] == "4GB" - assert result["cost_monitoring"] is True - assert result["provider"] == "azure" - assert result["provider_config"]["cluster_name"] == "test-cluster" - assert result["provider_config"]["resource_group"] == "test-rg" - - def test_get_cluster_config_unknown_type(self, authenticated_provider): - """Test cluster config with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_config( - "test-cluster", cluster_type="unknown" - ) - - def test_estimate_cost_vm(self, provider): - """Test cost estimation for VM.""" - result = provider.estimate_cost( - cluster_type="vm", vm_size="Standard_D2s_v3", hours=10 - ) - - assert "vm" in result - assert "total" in result - assert result["vm"] == 0.096 * 10 - assert result["total"] == 0.096 * 10 - - def test_estimate_cost_aks(self, provider): - """Test cost estimation for AKS cluster.""" - result = provider.estimate_cost( - cluster_type="aks", vm_size="Standard_D2s_v3", hours=5 - ) - - cluster_fee = 0.0 # Free tier - node_cost = 0.096 * 5 - total = cluster_fee + node_cost - - assert "cluster_management" in result - assert "nodes" in result - assert "total" in result - assert result["cluster_management"] == cluster_fee - assert result["nodes"] == node_cost - assert result["total"] == total - - def test_estimate_cost_unknown_vm_size(self, provider): - """Test cost estimation with unknown VM size.""" - result = provider.estimate_cost(vm_size="Unknown_Size", hours=2) - - assert result["vm"] == 0.10 * 2 # Default price - assert result["total"] == 0.10 * 2 - - def test_estimate_cost_defaults(self, provider): - """Test cost estimation with default values.""" - result = provider.estimate_cost() - - assert result["vm"] == 0.096 # Standard_D2s_v3 for 1 hour - assert result["total"] == 0.096 - - def test_get_available_instance_types_not_authenticated(self, provider): - """Test instance types when not authenticated.""" - result = provider.get_available_instance_types() - - # Should return default list - assert "Standard_B1s" in result - assert "Standard_D2s_v3" in result - assert "Standard_E2s_v3" in result - - def test_get_available_instance_types_success(self, authenticated_provider): - """Test successful instance types retrieval.""" - mock_size1 = Mock() - mock_size1.name = "Standard_B1s" - mock_size2 = Mock() - mock_size2.name = "Standard_B2s" - mock_size3 = Mock() - mock_size3.name = "Standard_D2s_v3" - mock_size4 = Mock() - mock_size4.name = "Standard_D4s_v3" - - authenticated_provider.compute_client.virtual_machine_sizes.list.return_value = [ - mock_size1, - mock_size2, - mock_size3, - mock_size4, - ] - - result = authenticated_provider.get_available_instance_types() - - # Should contain the mocked VM sizes - assert "Standard_B1s" in result - assert "Standard_B2s" in result - assert "Standard_D2s_v3" in result - assert "Standard_D4s_v3" in result - - def test_get_available_instance_types_custom_region(self, authenticated_provider): - """Test instance types retrieval for custom region.""" - authenticated_provider.compute_client.virtual_machine_sizes.list.return_value = ( - [] - ) - - result = authenticated_provider.get_available_instance_types(region="westus") - - # Should query the correct region - authenticated_provider.compute_client.virtual_machine_sizes.list.assert_called_once_with( - "westus" - ) - - def test_get_available_instance_types_exception(self, authenticated_provider): - """Test instance types retrieval with exception.""" - authenticated_provider.compute_client.virtual_machine_sizes.list.side_effect = ( - Exception("API error") - ) - - result = authenticated_provider.get_available_instance_types() - - # Should return default list - assert "Standard_B1s" in result - assert "Standard_D2s_v3" in result - - def test_get_available_regions_not_authenticated(self, provider): - """Test regions when not authenticated.""" - result = provider.get_available_regions() - - assert "eastus" in result - assert "westus2" in result - assert "northeurope" in result - - def test_get_available_regions_success(self, authenticated_provider): - """Test successful regions retrieval.""" - mock_location1 = Mock() - mock_location1.name = "eastus" - mock_location2 = Mock() - mock_location2.name = "westus2" - mock_location3 = Mock() - mock_location3.name = "northeurope" - mock_location4 = Mock() - mock_location4.name = "southafricanorth" # Not in priority list - - authenticated_provider.resource_client.subscriptions.list_locations.return_value = [ - mock_location1, - mock_location2, - mock_location3, - mock_location4, - ] - - result = authenticated_provider.get_available_regions() - - # Priority regions should come first - assert result[0] == "eastus" - assert result[1] == "westus2" - assert result[2] == "northeurope" - assert "southafricanorth" in result - - def test_get_available_regions_exception(self, authenticated_provider): - """Test regions retrieval with exception.""" - authenticated_provider.resource_client.subscriptions.list_locations.side_effect = Exception( - "API error" - ) - - result = authenticated_provider.get_available_regions() - - # Should return default list - assert "eastus" in result - assert "westus2" in result - - -class TestAzureProviderEdgeCases: - """Test edge cases and error handling.""" - - def test_vm_size_sorting_edge_cases(self): - """Test VM size sorting with edge cases.""" - provider = AzureProvider() - provider.authenticated = True - provider.subscription_id = "test-subscription" - provider.compute_client = Mock() - - # Mock VM sizes with various formats - mock_sizes = [] - for name in [ - "Standard_B1s", - "Standard_B2s", - "Standard_D2s_v3", - "Standard_InvalidName", - ]: - mock_size = Mock() - mock_size.name = name - mock_sizes.append(mock_size) - - provider.compute_client.virtual_machine_sizes.list.return_value = mock_sizes - - result = provider.get_available_instance_types() - - # Should handle various formats without error - assert len(result) > 0 - - def test_list_clusters_missing_attributes(self): - """Test list_clusters with resources missing attributes.""" - provider = AzureProvider() - provider.authenticated = True - provider.resource_group = "test-rg" - provider.compute_client = Mock() - provider.container_client = Mock() - - # Mock VM without tags - mock_vm = Mock() - mock_vm.name = "test-vm" - mock_vm.tags = None # No tags - provider.compute_client.virtual_machines.list.return_value = [mock_vm] - - # Mock AKS cluster without tags - mock_cluster = Mock() - mock_cluster.name = "test-cluster" - mock_cluster.tags = None - provider.container_client.managed_clusters.list_by_resource_group.return_value = [ - mock_cluster - ] - - result = provider.list_clusters() - - # Should handle missing tags gracefully - assert result == [] - - def test_vm_creation_without_password(self): - """Test VM creation without password (SSH key mode).""" - provider = AzureProvider() - provider.authenticated = True - provider.resource_group = "test-rg" - provider.region = "eastus" - provider.compute_client = Mock() - provider.network_client = Mock() - - with patch.object(provider, "_ensure_resource_group", return_value=True): - # Setup mock network resources - mock_vnet = Mock() - mock_vnet.subnets = [Mock()] - mock_vnet.subnets[0].id = "subnet-id" - provider.network_client.virtual_networks.begin_create_or_update.return_value.result.return_value = ( - mock_vnet - ) - - mock_public_ip = Mock() - mock_public_ip.id = "ip-id" - mock_public_ip.ip_address = "1.2.3.4" - provider.network_client.public_ip_addresses.begin_create_or_update.return_value.result.return_value = ( - mock_public_ip - ) - - mock_nsg = Mock() - mock_nsg.id = "nsg-id" - provider.network_client.network_security_groups.begin_create_or_update.return_value.result.return_value = ( - mock_nsg - ) - - mock_nic = Mock() - mock_nic.id = "nic-id" - provider.network_client.network_interfaces.begin_create_or_update.return_value.result.return_value = ( - mock_nic - ) - - mock_vm = Mock() - mock_vm.id = "vm-id" - provider.compute_client.virtual_machines.begin_create_or_update.return_value.result.return_value = ( - mock_vm - ) - - result = provider.create_vm( - vm_name="test-vm", - admin_username="testuser", - # No admin_password provided - ) - - # Should succeed and disable password authentication - assert result["vm_name"] == "test-vm" - - def test_get_cluster_config_public_ip_none(self): - """Test cluster config when public IP is None.""" - provider = AzureProvider() - provider.authenticated = True - provider.resource_group = "test-rg" - provider.compute_client = Mock() - provider.network_client = Mock() - - mock_vm = Mock() - provider.compute_client.virtual_machines.get.return_value = mock_vm - - mock_public_ip = Mock() - mock_public_ip.ip_address = None # No IP address - provider.network_client.public_ip_addresses.get.return_value = mock_public_ip - - # A public IP resource with no address assigned is not a host. - with pytest.raises(RuntimeError, match="no address"): - provider.get_cluster_config("test-vm", cluster_type="vm") diff --git a/tests/test_cloud_providers_gcp.py b/tests/test_cloud_providers_gcp.py deleted file mode 100644 index c64fa01d..00000000 --- a/tests/test_cloud_providers_gcp.py +++ /dev/null @@ -1,989 +0,0 @@ -import pytest -import json -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime, timezone - -from clustrix.cloud_providers.gcp import GCPProvider - - -class TestGCPProvider: - """Test GCP provider functionality.""" - - @pytest.fixture - def provider(self): - """Create GCPProvider instance.""" - return GCPProvider() - - @pytest.fixture - def authenticated_provider(self): - """Create authenticated GCPProvider instance.""" - provider = GCPProvider() - provider.authenticated = True - provider.project_id = "test-project" - provider.region = "us-central1" - provider.zone = "us-central1-a" - provider.compute_client = Mock() - provider.container_client = Mock() - provider.service_account_info = { - "type": "service_account", - "project_id": "test-project", - } - provider.credentials = { - "project_id": "test-project", - "service_account_key": '{"type": "service_account", "project_id": "test-project"}', - } - return provider - - def test_initialization(self, provider): - """Test provider initialization.""" - assert provider.project_id is None - assert provider.region == "us-central1" - assert provider.zone == "us-central1-a" - assert provider.compute_client is None - assert provider.container_client is None - assert provider.service_account_info is None - assert not provider.authenticated - - @patch("clustrix.cloud_providers.gcp.GCP_AVAILABLE", True) - @patch("clustrix.cloud_providers.gcp.service_account") - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.container_v1") - def test_authenticate_success( - self, mock_container, mock_compute, mock_service_account, provider - ): - """Test successful authentication.""" - # Mock service account credentials - mock_creds = Mock() - mock_service_account.Credentials.from_service_account_info.return_value = ( - mock_creds - ) - - # Mock clients - mock_compute_client = Mock() - mock_container_client = Mock() - mock_compute.InstancesClient.return_value = mock_compute_client - mock_container.ClusterManagerClient.return_value = mock_container_client - - # Mock successful API call - mock_compute_client.list.return_value = [] - - service_account_key = ( - '{"type": "service_account", "project_id": "test-project"}' - ) - result = provider.authenticate( - project_id="test-project", - service_account_key=service_account_key, - region="us-west1", - ) - - assert result is True - assert provider.authenticated is True - assert provider.project_id == "test-project" - assert provider.region == "us-west1" - assert provider.zone == "us-west1-a" - assert provider.compute_client == mock_compute_client - assert provider.container_client == mock_container_client - - @patch("clustrix.cloud_providers.gcp.GCP_AVAILABLE", False) - def test_authenticate_gcp_not_available(self, provider): - """Test authentication when GCP packages not available.""" - result = provider.authenticate( - project_id="test-project", service_account_key='{"type": "service_account"}' - ) - - assert result is False - assert not provider.authenticated - - def test_authenticate_missing_credentials(self, provider): - """Test authentication with missing credentials.""" - result = provider.authenticate(project_id="test-project") - assert result is False - - result = provider.authenticate( - service_account_key='{"type": "service_account"}' - ) - assert result is False - - @patch("clustrix.cloud_providers.gcp.GCP_AVAILABLE", True) - def test_authenticate_invalid_json(self, provider): - """Test authentication with invalid JSON.""" - result = provider.authenticate( - project_id="test-project", service_account_key="invalid json" - ) - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.gcp.GCP_AVAILABLE", True) - @patch("clustrix.cloud_providers.gcp.service_account") - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.container_v1") - def test_authenticate_api_failure( - self, mock_container, mock_compute, mock_service_account, provider - ): - """Test authentication with API failure.""" - mock_creds = Mock() - mock_service_account.Credentials.from_service_account_info.return_value = ( - mock_creds - ) - - mock_compute_client = Mock() - mock_compute.InstancesClient.return_value = mock_compute_client - mock_compute_client.list.side_effect = Exception("API error") - - result = provider.authenticate( - project_id="test-project", service_account_key='{"type": "service_account"}' - ) - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.gcp.GCP_AVAILABLE", True) - @patch("clustrix.cloud_providers.gcp.service_account") - def test_authenticate_credentials_error(self, mock_service_account, provider): - """Test authentication with credentials error.""" - from clustrix.cloud_providers.gcp import DefaultCredentialsError - - mock_service_account.Credentials.from_service_account_info.side_effect = ( - DefaultCredentialsError() - ) - - result = provider.authenticate( - project_id="test-project", service_account_key='{"type": "service_account"}' - ) - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.gcp.GCP_AVAILABLE", True) - @patch("clustrix.cloud_providers.gcp.service_account") - def test_authenticate_dict_service_account_key( - self, mock_service_account, provider - ): - """Test authentication with dict service account key.""" - mock_creds = Mock() - mock_service_account.Credentials.from_service_account_info.return_value = ( - mock_creds - ) - - with ( - patch("clustrix.cloud_providers.gcp.compute_v1") as mock_compute, - patch("clustrix.cloud_providers.gcp.container_v1") as mock_container, - ): - mock_compute_client = Mock() - mock_compute.InstancesClient.return_value = mock_compute_client - mock_compute_client.list.return_value = [] - - service_account_dict = { - "type": "service_account", - "project_id": "test-project", - } - result = provider.authenticate( - project_id="test-project", service_account_key=service_account_dict - ) - - assert result is True - assert provider.service_account_info == service_account_dict - - def test_validate_credentials_success(self, authenticated_provider): - """Test successful credential validation.""" - authenticated_provider.compute_client.list.return_value = [] - - result = authenticated_provider.validate_credentials() - - assert result is True - authenticated_provider.compute_client.list.assert_called_once_with( - project="test-project", zone="us-central1-a" - ) - - def test_validate_credentials_failure(self, authenticated_provider): - """Test failed credential validation.""" - authenticated_provider.compute_client.list.side_effect = Exception("API error") - - result = authenticated_provider.validate_credentials() - - assert result is False - - def test_validate_credentials_not_authenticated(self, provider): - """Test credential validation when not authenticated.""" - result = provider.validate_credentials() - - assert result is False - - def test_validate_credentials_no_client(self, provider): - """Test credential validation with no compute client.""" - provider.authenticated = True - provider.compute_client = None - - result = provider.validate_credentials() - - assert result is False - - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.service_account") - def test_create_compute_instance_success( - self, mock_service_account, mock_compute, authenticated_provider - ): - """Test successful compute instance creation.""" - # Mock images client - mock_images_client = Mock() - mock_image = Mock() - mock_image.self_link = "projects/ubuntu-os-cloud/global/images/ubuntu-2004-lts" - mock_images_client.get_from_family.return_value = mock_image - mock_compute.ImagesClient.return_value = mock_images_client - - # Mock instance creation - mock_operation = Mock() - mock_operation.name = "operation-12345" - authenticated_provider.compute_client.insert.return_value = mock_operation - - with patch("clustrix.cloud_providers.gcp.datetime") as mock_datetime: - mock_datetime.now.return_value.isoformat.return_value = ( - "2024-01-01T00:00:00+00:00" - ) - mock_datetime.timezone = timezone - - result = authenticated_provider.create_compute_instance( - instance_name="test-instance", - machine_type="e2-medium", - image_family="ubuntu-2004-lts", - image_project="ubuntu-os-cloud", - ) - - assert result["instance_name"] == "test-instance" - assert result["instance_id"] == "test-instance" - assert result["machine_type"] == "e2-medium" - assert result["zone"] == "us-central1-a" - assert result["region"] == "us-central1" - assert result["status"] == "creating" - assert result["operation"] == "operation-12345" - - # Verify API calls - mock_images_client.get_from_family.assert_called_once_with( - project="ubuntu-os-cloud", family="ubuntu-2004-lts" - ) - authenticated_provider.compute_client.insert.assert_called_once() - - def test_create_compute_instance_not_authenticated(self, provider): - """Test compute instance creation when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_compute_instance("test-instance") - - @patch("clustrix.cloud_providers.gcp.compute_v1") - def test_create_compute_instance_exception( - self, mock_compute, authenticated_provider - ): - """Test compute instance creation with exception.""" - mock_images_client = Mock() - mock_images_client.get_from_family.side_effect = Exception("API error") - mock_compute.ImagesClient.return_value = mock_images_client - - with pytest.raises(Exception): - authenticated_provider.create_compute_instance("test-instance") - - @patch("clustrix.cloud_providers.gcp.container_v1") - def test_create_gke_cluster_success(self, mock_container, authenticated_provider): - """Test successful GKE cluster creation.""" - mock_operation = Mock() - mock_operation.name = "operation-gke-12345" - authenticated_provider.container_client.create_cluster.return_value = ( - mock_operation - ) - - with patch("clustrix.cloud_providers.gcp.datetime") as mock_datetime: - mock_datetime.now.return_value.isoformat.return_value = ( - "2024-01-01T00:00:00+00:00" - ) - mock_datetime.timezone = timezone - - result = authenticated_provider.create_gke_cluster( - cluster_name="test-cluster", - node_count=5, - machine_type="e2-standard-4", - kubernetes_version="1.25.0", - disk_size_gb=200, - ) - - assert result["cluster_name"] == "test-cluster" - assert result["status"] == "creating" - assert result["region"] == "us-central1" - assert result["zone"] == "us-central1-a" - assert result["provider"] == "gcp" - assert result["cluster_type"] == "gke" - assert result["project_id"] == "test-project" - assert result["node_count"] == 5 - assert result["machine_type"] == "e2-standard-4" - assert result["disk_size_gb"] == 200 - assert result["kubernetes_version"] == "1.25.0" - assert result["operation_name"] == "operation-gke-12345" - - # Verify API call - authenticated_provider.container_client.create_cluster.assert_called_once() - - def test_create_gke_cluster_not_authenticated(self, provider): - """Test GKE cluster creation when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_gke_cluster("test-cluster") - - def test_create_gke_cluster_exception(self, authenticated_provider): - """Test GKE cluster creation with exception.""" - authenticated_provider.container_client.create_cluster.side_effect = Exception( - "API error" - ) - - with pytest.raises(Exception, match="API error"): - authenticated_provider.create_gke_cluster("test-cluster") - - def test_create_cluster_compute(self, authenticated_provider): - """Test create_cluster with compute type.""" - with patch.object( - authenticated_provider, "create_compute_instance" - ) as mock_create: - mock_create.return_value = {"instance_id": "test-instance"} - - result = authenticated_provider.create_cluster( - "test-cluster", cluster_type="compute", machine_type="e2-medium" - ) - - mock_create.assert_called_once_with( - "test-cluster", machine_type="e2-medium" - ) - assert result == {"instance_id": "test-instance"} - - def test_create_cluster_gke(self, authenticated_provider): - """Test create_cluster with GKE type.""" - with patch.object(authenticated_provider, "create_gke_cluster") as mock_create: - mock_create.return_value = {"cluster_name": "test-cluster"} - - result = authenticated_provider.create_cluster( - "test-cluster", cluster_type="gke", node_count=3 - ) - - mock_create.assert_called_once_with("test-cluster", node_count=3) - assert result == {"cluster_name": "test-cluster"} - - def test_create_cluster_unknown_type(self, authenticated_provider): - """Test create_cluster with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.create_cluster( - "test-cluster", cluster_type="unknown" - ) - - def test_delete_cluster_compute_success(self, authenticated_provider): - """Test successful compute instance deletion.""" - mock_operation = Mock() - mock_operation.name = "delete-operation-12345" - authenticated_provider.compute_client.delete.return_value = mock_operation - - result = authenticated_provider.delete_cluster( - "test-instance", cluster_type="compute" - ) - - assert result is True - authenticated_provider.compute_client.delete.assert_called_once_with( - project="test-project", zone="us-central1-a", instance="test-instance" - ) - - def test_delete_cluster_gke_success(self, authenticated_provider): - """Test successful GKE cluster deletion.""" - mock_operation = Mock() - mock_operation.name = "delete-gke-operation-12345" - authenticated_provider.container_client.delete_cluster.return_value = ( - mock_operation - ) - - result = authenticated_provider.delete_cluster( - "test-cluster", cluster_type="gke" - ) - - assert result is True - authenticated_provider.container_client.delete_cluster.assert_called_once() - - def test_delete_cluster_not_authenticated(self, provider): - """Test cluster deletion when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.delete_cluster("test-cluster") - - def test_delete_cluster_unknown_type(self, authenticated_provider): - """Test cluster deletion with unknown type.""" - result = authenticated_provider.delete_cluster( - "test-cluster", cluster_type="unknown" - ) - assert result is False - - def test_delete_cluster_exception(self, authenticated_provider): - """Test cluster deletion with exception.""" - authenticated_provider.compute_client.delete.side_effect = Exception( - "API error" - ) - - result = authenticated_provider.delete_cluster( - "test-instance", cluster_type="compute" - ) - - assert result is False - - def test_get_cluster_status_compute_success(self, authenticated_provider): - """Test successful compute instance status retrieval.""" - mock_instance = Mock() - mock_instance.status = "RUNNING" - mock_instance.machine_type = "zones/us-central1-a/machineTypes/e2-medium" - authenticated_provider.compute_client.get.return_value = mock_instance - - result = authenticated_provider.get_cluster_status( - "test-instance", cluster_type="compute" - ) - - assert result["instance_name"] == "test-instance" - assert result["status"] == "running" - assert result["machine_type"] == "e2-medium" - assert result["zone"] == "us-central1-a" - assert result["provider"] == "gcp" - assert result["cluster_type"] == "compute" - - def test_get_cluster_status_gke_success(self, authenticated_provider): - """Test successful GKE cluster status retrieval.""" - mock_cluster = Mock() - mock_cluster.status.name = "RUNNING" - mock_cluster.endpoint = "1.2.3.4" - mock_cluster.current_master_version = "1.25.0" - mock_cluster.current_node_version = "1.25.0" - mock_cluster.current_node_count = 3 - mock_cluster.location = "us-central1-a" - mock_cluster.zone = "us-central1-a" - mock_cluster.create_time = "2024-01-01T00:00:00Z" - authenticated_provider.container_client.get_cluster.return_value = mock_cluster - - result = authenticated_provider.get_cluster_status( - "test-cluster", cluster_type="gke" - ) - - assert result["cluster_name"] == "test-cluster" - assert result["status"] == "running" - assert result["endpoint"] == "1.2.3.4" - assert result["current_master_version"] == "1.25.0" - assert result["current_node_version"] == "1.25.0" - assert result["node_count"] == 3 - assert result["location"] == "us-central1-a" - assert result["zone"] == "us-central1-a" - assert result["provider"] == "gcp" - assert result["cluster_type"] == "gke" - assert result["project_id"] == "test-project" - - def test_get_cluster_status_not_authenticated(self, provider): - """Test cluster status when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.get_cluster_status("test-cluster") - - def test_get_cluster_status_unknown_type(self, authenticated_provider): - """Test cluster status with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_status( - "test-cluster", cluster_type="unknown" - ) - - def test_get_cluster_status_exception(self, authenticated_provider): - """Test cluster status with exception.""" - authenticated_provider.compute_client.get.side_effect = Exception("API error") - - with pytest.raises(Exception, match="API error"): - authenticated_provider.get_cluster_status( - "test-instance", cluster_type="compute" - ) - - def test_list_clusters_success(self, authenticated_provider): - """Test successful cluster listing.""" - # Mock compute instances - mock_instance = Mock() - mock_instance.name = "clustrix-instance" - mock_instance.status = "RUNNING" - mock_instance.machine_type = "zones/us-central1-a/machineTypes/e2-medium" - mock_instance.tags = Mock() - mock_instance.tags.items = ["clustrix-managed", "http-server"] - authenticated_provider.compute_client.list.return_value = [mock_instance] - - # Mock GKE clusters - mock_cluster = Mock() - mock_cluster.name = "clustrix-gke" - mock_cluster.status.name = "RUNNING" - mock_cluster.endpoint = "1.2.3.4" - mock_cluster.current_master_version = "1.25.0" - mock_cluster.current_node_count = 3 - mock_cluster.location = "us-central1-a" - mock_cluster.zone = "us-central1-a" - mock_cluster.resource_labels = {"created_by": "clustrix"} - - mock_response = Mock() - mock_response.clusters = [mock_cluster] - authenticated_provider.container_client.list_clusters.return_value = ( - mock_response - ) - - result = authenticated_provider.list_clusters() - - assert len(result) == 2 - - # Check compute instance - compute_result = next(r for r in result if r["type"] == "compute") - assert compute_result["name"] == "clustrix-instance" - assert compute_result["instance_id"] == "clustrix-instance" - assert compute_result["status"] == "running" - assert compute_result["machine_type"] == "e2-medium" - - # Check GKE cluster - gke_result = next(r for r in result if r["type"] == "gke") - assert gke_result["name"] == "clustrix-gke" - assert gke_result["cluster_id"] == "clustrix-gke" - assert gke_result["status"] == "running" - assert gke_result["endpoint"] == "1.2.3.4" - - def test_list_clusters_not_authenticated(self, provider): - """Test cluster listing when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.list_clusters() - - def test_list_clusters_no_clustrix_instances(self, authenticated_provider): - """Test cluster listing with no Clustrix-managed instances.""" - # Mock instance without clustrix-managed tag - mock_instance = Mock() - mock_instance.name = "other-instance" - mock_instance.tags = Mock() - mock_instance.tags.items = ["http-server"] # No clustrix-managed tag - authenticated_provider.compute_client.list.return_value = [mock_instance] - - # Mock GKE cluster without clustrix label - mock_cluster = Mock() - mock_cluster.name = "other-gke" - mock_cluster.resource_labels = {"created_by": "other"} # Not clustrix - - mock_response = Mock() - mock_response.clusters = [mock_cluster] - authenticated_provider.container_client.list_clusters.return_value = ( - mock_response - ) - - result = authenticated_provider.list_clusters() - - assert len(result) == 0 - - def test_list_clusters_exceptions(self, authenticated_provider): - """Test cluster listing with exceptions.""" - # Mock compute client exception - authenticated_provider.compute_client.list.side_effect = Exception( - "Compute API error" - ) - - # Mock container client exception - authenticated_provider.container_client.list_clusters.side_effect = Exception( - "GKE API error" - ) - - result = authenticated_provider.list_clusters() - - assert result == [] - - def test_get_cluster_config_compute_success(self, authenticated_provider): - """Test successful compute cluster config retrieval.""" - mock_instance = Mock() - mock_access_config = Mock() - mock_access_config.nat_i_p = "35.1.2.3" - mock_interface = Mock() - mock_interface.access_configs = [mock_access_config] - mock_instance.network_interfaces = [mock_interface] - authenticated_provider.compute_client.get.return_value = mock_instance - - result = authenticated_provider.get_cluster_config( - "test-instance", cluster_type="compute" - ) - - assert result["name"] == "GCP Compute - test-instance" - assert result["cluster_type"] == "ssh" - assert result["cluster_host"] == "35.1.2.3" - assert result["username"] == "ubuntu" - assert result["cluster_port"] == 22 - assert result["default_cores"] == 2 - assert result["default_memory"] == "4GB" - assert result["remote_work_dir"] == "/home/ubuntu/clustrix" - assert result["package_manager"] == "conda" - assert result["cost_monitoring"] is True - assert result["provider"] == "gcp" - assert result["provider_config"]["instance_name"] == "test-instance" - assert result["provider_config"]["zone"] == "us-central1-a" - assert result["provider_config"]["project_id"] == "test-project" - - def test_get_cluster_config_compute_no_ip(self, authenticated_provider): - """Test compute cluster config with no external IP.""" - mock_instance = Mock() - mock_access_config = Mock() - mock_access_config.nat_i_p = None # No external IP - mock_interface = Mock() - mock_interface.access_configs = [mock_access_config] - mock_instance.network_interfaces = [mock_interface] - authenticated_provider.compute_client.get.return_value = mock_instance - - # An instance with no external IP has no host to connect to. This - # used to return cluster_host "" (see #119). - with pytest.raises(RuntimeError, match="no external IP"): - authenticated_provider.get_cluster_config( - "test-instance", cluster_type="compute" - ) - - def test_get_cluster_config_compute_exception(self, authenticated_provider): - """Test compute cluster config with exception.""" - authenticated_provider.compute_client.get.side_effect = Exception("API error") - - # This used to return cluster_host "placeholder.gcp.com", which - # clustrix then tried to SSH into (see #119). - with pytest.raises(RuntimeError, match="Could not determine"): - authenticated_provider.get_cluster_config( - "test-instance", cluster_type="compute" - ) - - def test_get_cluster_config_gke(self, authenticated_provider): - """Test GKE cluster config retrieval.""" - result = authenticated_provider.get_cluster_config( - "test-cluster", cluster_type="gke" - ) - - assert result["name"] == "GCP GKE - test-cluster" - assert result["cluster_type"] == "kubernetes" - assert result["cluster_host"] == "test-cluster.gke.us-central1.gcp.com" - assert result["cluster_port"] == 443 - assert result["k8s_namespace"] == "default" - assert result["k8s_image"] == "python:3.11" - assert result["default_cores"] == 2 - assert result["default_memory"] == "4GB" - assert result["cost_monitoring"] is True - assert result["provider"] == "gcp" - assert result["provider_config"]["cluster_name"] == "test-cluster" - assert result["provider_config"]["region"] == "us-central1" - assert result["provider_config"]["project_id"] == "test-project" - - def test_get_cluster_config_unknown_type(self, authenticated_provider): - """Test cluster config with unknown type.""" - with pytest.raises(ValueError, match="Unknown cluster type"): - authenticated_provider.get_cluster_config( - "test-cluster", cluster_type="unknown" - ) - - def test_estimate_cost_compute(self, provider): - """Test cost estimation for compute instances.""" - result = provider.estimate_cost( - cluster_type="compute", machine_type="e2-medium", hours=10 - ) - - assert "instance" in result - assert "total" in result - assert result["instance"] == 0.0225 * 10 - assert result["total"] == 0.0225 * 10 - - def test_estimate_cost_gke(self, provider): - """Test cost estimation for GKE clusters.""" - result = provider.estimate_cost( - cluster_type="gke", machine_type="e2-standard-2", hours=5 - ) - - cluster_fee = 0.10 * 5 - node_cost = 0.0450 * 5 - total = cluster_fee + node_cost - - assert "cluster_management" in result - assert "nodes" in result - assert "total" in result - assert result["cluster_management"] == cluster_fee - assert result["nodes"] == node_cost - assert result["total"] == total - - def test_estimate_cost_unknown_machine_type(self, provider): - """Test cost estimation with unknown machine type.""" - result = provider.estimate_cost(machine_type="unknown-type", hours=2) - - assert result["instance"] == 0.05 * 2 # Default price - assert result["total"] == 0.05 * 2 - - def test_estimate_cost_defaults(self, provider): - """Test cost estimation with default values.""" - result = provider.estimate_cost() - - assert result["instance"] == 0.0225 # e2-medium for 1 hour - assert result["total"] == 0.0225 - - def test_get_available_instance_types_not_authenticated(self, provider): - """Test instance types when not authenticated.""" - result = provider.get_available_instance_types() - - # Should return default list - assert "e2-micro" in result - assert "e2-medium" in result - assert "n1-standard-1" in result - assert "c2-standard-4" in result - - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.service_account") - def test_get_available_instance_types_success( - self, mock_service_account, mock_compute, authenticated_provider - ): - """Test successful instance types retrieval.""" - mock_machine_type1 = Mock() - mock_machine_type1.name = "e2-micro" - mock_machine_type2 = Mock() - mock_machine_type2.name = "e2-medium" - mock_machine_type3 = Mock() - mock_machine_type3.name = "n1-standard-2" - mock_machine_type4 = Mock() - mock_machine_type4.name = "c2-standard-4" - - mock_machine_types_client = Mock() - mock_machine_types_client.list.return_value = [ - mock_machine_type1, - mock_machine_type2, - mock_machine_type3, - mock_machine_type4, - ] - mock_compute.MachineTypesClient.return_value = mock_machine_types_client - - result = authenticated_provider.get_available_instance_types() - - # Should contain the mocked machine types - assert "e2-micro" in result - assert "e2-medium" in result - assert "n1-standard-2" in result - assert "c2-standard-4" in result - - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.service_account") - def test_get_available_instance_types_custom_region( - self, mock_service_account, mock_compute, authenticated_provider - ): - """Test instance types retrieval for custom region.""" - mock_machine_types_client = Mock() - mock_machine_types_client.list.return_value = [] - mock_compute.MachineTypesClient.return_value = mock_machine_types_client - - result = authenticated_provider.get_available_instance_types( - region="europe-west1" - ) - - # Should query the correct zone - mock_machine_types_client.list.assert_called_once_with( - project="test-project", zone="europe-west1-a" - ) - - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.service_account") - def test_get_available_instance_types_exception( - self, mock_service_account, mock_compute, authenticated_provider - ): - """Test instance types retrieval with exception.""" - mock_machine_types_client = Mock() - mock_machine_types_client.list.side_effect = Exception("API error") - mock_compute.MachineTypesClient.return_value = mock_machine_types_client - - result = authenticated_provider.get_available_instance_types() - - # Should return default list - assert "e2-micro" in result - assert "e2-medium" in result - - def test_get_available_regions_not_authenticated(self, provider): - """Test regions when not authenticated.""" - result = provider.get_available_regions() - - assert "us-central1" in result - assert "us-east1" in result - assert "europe-west1" in result - assert "asia-southeast1" in result - - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.service_account") - def test_get_available_regions_success( - self, mock_service_account, mock_compute, authenticated_provider - ): - """Test successful regions retrieval.""" - mock_region1 = Mock() - mock_region1.name = "us-central1" - mock_region2 = Mock() - mock_region2.name = "europe-west1" - mock_region3 = Mock() - mock_region3.name = "asia-southeast1" - mock_region4 = Mock() - mock_region4.name = "us-west3" # Not in priority list - - mock_regions_client = Mock() - mock_regions_client.list.return_value = [ - mock_region1, - mock_region2, - mock_region3, - mock_region4, - ] - mock_compute.RegionsClient.return_value = mock_regions_client - - result = authenticated_provider.get_available_regions() - - # Priority regions should come first - assert result[0] == "us-central1" - assert result[1] == "europe-west1" - assert result[2] == "asia-southeast1" - assert "us-west3" in result - - @patch("clustrix.cloud_providers.gcp.compute_v1") - @patch("clustrix.cloud_providers.gcp.service_account") - def test_get_available_regions_exception( - self, mock_service_account, mock_compute, authenticated_provider - ): - """Test regions retrieval with exception.""" - mock_regions_client = Mock() - mock_regions_client.list.side_effect = Exception("API error") - mock_compute.RegionsClient.return_value = mock_regions_client - - result = authenticated_provider.get_available_regions() - - # Should return default list - assert "us-central1" in result - assert "us-east1" in result - - -class TestGCPProviderEdgeCases: - """Test edge cases and error handling.""" - - def test_machine_type_sorting_edge_cases(self): - """Test machine type name parsing with edge cases.""" - # This tests the sorting key function indirectly - provider = GCPProvider() - provider.authenticated = True - provider.project_id = "test-project" - provider.service_account_info = {"test": "info"} - - with ( - patch("clustrix.cloud_providers.gcp.compute_v1") as mock_compute, - patch("clustrix.cloud_providers.gcp.service_account"), - ): - # Mock machine types with various formats - mock_types = [] - for name in [ - "e2-micro", - "e2-medium", - "e2-standard-2", - "e2-standard-10", - "invalid-name", - ]: - mock_type = Mock() - mock_type.name = name - mock_types.append(mock_type) - - mock_machine_types_client = Mock() - mock_machine_types_client.list.return_value = mock_types - mock_compute.MachineTypesClient.return_value = mock_machine_types_client - - result = provider.get_available_instance_types() - - # Should handle various formats without error - assert len(result) > 0 - - def test_list_clusters_missing_attributes(self): - """Test list_clusters with instances missing attributes.""" - provider = GCPProvider() - provider.authenticated = True - provider.project_id = "test-project" - provider.zone = "us-central1-a" - provider.compute_client = Mock() - provider.container_client = Mock() - - # Mock instance without tags attribute - mock_instance = Mock() - mock_instance.name = "test-instance" - mock_instance.status = "RUNNING" - mock_instance.machine_type = None # Missing machine type - del mock_instance.tags # Remove tags attribute - provider.compute_client.list.return_value = [mock_instance] - - # Mock empty GKE response - mock_response = Mock() - mock_response.clusters = [] - provider.container_client.list_clusters.return_value = mock_response - - result = provider.list_clusters() - - # Should handle missing attributes gracefully - assert result == [] - - def test_list_clusters_no_gke_labels(self): - """Test list_clusters with GKE cluster without resource_labels.""" - provider = GCPProvider() - provider.authenticated = True - provider.project_id = "test-project" - provider.zone = "us-central1-a" - provider.compute_client = Mock() - provider.container_client = Mock() - - # Mock empty compute response - provider.compute_client.list.return_value = [] - - # Mock GKE cluster without resource_labels - mock_cluster = Mock() - mock_cluster.name = "test-cluster" - mock_cluster.resource_labels = None - - mock_response = Mock() - mock_response.clusters = [mock_cluster] - provider.container_client.list_clusters.return_value = mock_response - - result = provider.list_clusters() - - # Should handle missing labels gracefully - assert result == [] - - def test_get_cluster_config_no_access_configs(self): - """Test cluster config with instance having no access configs.""" - provider = GCPProvider() - provider.authenticated = True - provider.project_id = "test-project" - provider.zone = "us-central1-a" - provider.compute_client = Mock() - - mock_instance = Mock() - mock_interface = Mock() - mock_interface.access_configs = [] # No access configs - mock_instance.network_interfaces = [mock_interface] - provider.compute_client.get.return_value = mock_instance - - with pytest.raises(RuntimeError, match="no external IP"): - provider.get_cluster_config("test-instance", cluster_type="compute") - - def test_get_cluster_config_no_network_interfaces(self): - """Test cluster config with instance having no network interfaces.""" - provider = GCPProvider() - provider.authenticated = True - provider.project_id = "test-project" - provider.zone = "us-central1-a" - provider.compute_client = Mock() - - mock_instance = Mock() - mock_instance.network_interfaces = [] # No network interfaces - provider.compute_client.get.return_value = mock_instance - - with pytest.raises(RuntimeError, match="no external IP"): - provider.get_cluster_config("test-instance", cluster_type="compute") - - def test_gke_cluster_status_no_zone(self): - """Test GKE cluster status when zone is None.""" - provider = GCPProvider() - provider.authenticated = True - provider.project_id = "test-project" - provider.zone = "us-central1-a" - provider.container_client = Mock() - - mock_cluster = Mock() - mock_cluster.status.name = "RUNNING" - mock_cluster.endpoint = "1.2.3.4" - mock_cluster.current_master_version = "1.25.0" - mock_cluster.current_node_version = "1.25.0" - mock_cluster.current_node_count = 3 - mock_cluster.location = "us-central1" - mock_cluster.zone = None # No zone (regional cluster) - mock_cluster.create_time = "2024-01-01T00:00:00Z" - provider.container_client.get_cluster.return_value = mock_cluster - - result = provider.get_cluster_status("test-cluster", cluster_type="gke") - - assert result["zone"] is None diff --git a/tests/test_cloud_providers_gcp_real.py b/tests/test_cloud_providers_gcp_real.py deleted file mode 100644 index e5e2333f..00000000 --- a/tests/test_cloud_providers_gcp_real.py +++ /dev/null @@ -1,580 +0,0 @@ -""" -Real-world tests for GCP provider functionality. - -These tests use actual GCP APIs when credentials are available, -demonstrating real user workflows without mocks. -""" - -import pytest -import os -import json -import re -import time -import tempfile -from pathlib import Path -from clustrix.cloud_providers.gcp import GCPProvider -from clustrix.config import ClusterConfig - - -class TestGCPProviderReal: - """Test GCP provider with real infrastructure.""" - - @pytest.fixture - def gcp_credentials(self): - """Get real GCP credentials if available.""" - # Check multiple sources for GCP credentials - sources = [ - os.getenv("GOOGLE_APPLICATION_CREDENTIALS"), - os.getenv("GCP_SERVICE_ACCOUNT_JSON"), - os.path.expanduser("~/.gcp/credentials.json"), - os.path.expanduser("~/.config/gcloud/application_default_credentials.json"), - ] - - for source in sources: - if source and os.path.exists(source): - with open(source, "r") as f: - return json.load(f) - - # Check if service account JSON is in environment variable - service_account_json = os.getenv("GCP_SERVICE_ACCOUNT_JSON") - if service_account_json: - try: - return json.loads(service_account_json) - except json.JSONDecodeError: - pass - - pytest.skip("GCP credentials not available") - - @pytest.fixture - def test_config(self): - """Create test configuration.""" - return { - "project_id": os.getenv("GCP_PROJECT_ID", "clustrix-test"), - "region": os.getenv("GCP_REGION", "us-central1"), - "zone": os.getenv("GCP_ZONE", "us-central1-a"), - "cluster_prefix": f"test-{int(time.time())}", - "cleanup_on_failure": True, - } - - def test_provider_initialization(self): - """ - Test provider initialization without credentials. - - This demonstrates: - - Default configuration values - - Uninitialized state - - No mock dependencies - """ - provider = GCPProvider() - - # Verify default state - assert provider.project_id is None - assert provider.region == "us-central1" - assert provider.zone == "us-central1-a" - assert provider.compute_client is None - assert provider.container_client is None - assert provider.service_account_info is None - assert not provider.authenticated - - @pytest.mark.real_world - def test_authentication_with_service_account(self, gcp_credentials, test_config): - """ - Test authentication with real GCP service account. - - This demonstrates: - - Real service account authentication - - Client initialization - - Project validation - """ - provider = GCPProvider() - - # Authenticate with real credentials - success = provider.authenticate( - credentials={ - "project_id": test_config["project_id"], - "service_account_key": json.dumps(gcp_credentials), - } - ) - - # Verify authentication - assert success is True - assert provider.authenticated is True - assert provider.project_id == test_config["project_id"] - assert provider.compute_client is not None - assert provider.container_client is not None - assert provider.service_account_info is not None - - @pytest.mark.real_world - def test_list_regions_and_zones(self, gcp_credentials, test_config): - """ - Test listing available regions and zones. - - This demonstrates: - - Real API calls to GCP - - Region/zone enumeration - - Resource availability checking - """ - provider = GCPProvider() - provider.authenticate( - credentials={ - "project_id": test_config["project_id"], - "service_account_key": json.dumps(gcp_credentials), - } - ) - - # List regions - regions = provider.list_regions() - assert isinstance(regions, list) - assert len(regions) > 0 - assert any("us-central1" in r for r in regions) - - # List zones in a region - zones = provider.list_zones(region="us-central1") - assert isinstance(zones, list) - assert len(zones) > 0 - assert "us-central1-a" in zones - - @pytest.mark.real_world - def test_check_quota_and_limits(self, gcp_credentials, test_config): - """ - Test checking project quotas and limits. - - This demonstrates: - - Real quota API calls - - Resource limit validation - - Capacity planning - """ - provider = GCPProvider() - provider.authenticate( - credentials={ - "project_id": test_config["project_id"], - "service_account_key": json.dumps(gcp_credentials), - } - ) - - # Check compute quotas - quotas = provider.check_quotas() - - assert isinstance(quotas, dict) - # Common quotas that should exist - expected_quotas = ["CPUS", "DISKS_TOTAL_GB", "INSTANCES"] - for quota_name in expected_quotas: - assert quota_name in quotas or any( - quota_name in key for key in quotas.keys() - ) - - @pytest.mark.real_world - def test_create_and_delete_vm_instance(self, gcp_credentials, test_config): - """ - Test creating and deleting a VM instance. - - This demonstrates: - - Real VM provisioning - - Instance configuration - - Resource cleanup - """ - provider = GCPProvider() - provider.authenticate( - credentials={ - "project_id": test_config["project_id"], - "service_account_key": json.dumps(gcp_credentials), - } - ) - - instance_name = f"clustrix-test-vm-{int(time.time())}" - - try: - # Create VM instance - instance_config = { - "name": instance_name, - "machine_type": "e2-micro", # Smallest instance type - "disk_size_gb": 10, - "image_family": "debian-11", - "image_project": "debian-cloud", - "network_tags": ["clustrix-test"], - "metadata": {"clustrix-test": "true", "created-by": "test-suite"}, - } - - operation = provider.create_instance( - zone=test_config["zone"], instance_config=instance_config - ) - - # Wait for instance creation - instance = provider.wait_for_operation( - operation=operation, zone=test_config["zone"], timeout=300 - ) - - assert instance is not None - assert instance["name"] == instance_name - assert instance["status"] == "RUNNING" - - # Verify instance exists - instances = provider.list_instances(zone=test_config["zone"]) - assert any(i["name"] == instance_name for i in instances) - - finally: - # Clean up - delete the instance - try: - provider.delete_instance( - zone=test_config["zone"], instance_name=instance_name - ) - - # Wait for deletion - time.sleep(10) - - # Verify deletion - instances = provider.list_instances(zone=test_config["zone"]) - assert not any(i["name"] == instance_name for i in instances) - - except Exception as e: - print(f"Warning: Failed to clean up instance {instance_name}: {e}") - - @pytest.mark.real_world - def test_create_and_delete_gke_cluster(self, gcp_credentials, test_config): - """ - Test creating and deleting a GKE cluster. - - This demonstrates: - - Real GKE cluster provisioning - - Kubernetes configuration - - Cluster lifecycle management - """ - provider = GCPProvider() - provider.authenticate( - credentials={ - "project_id": test_config["project_id"], - "service_account_key": json.dumps(gcp_credentials), - } - ) - - cluster_name = f"clustrix-test-gke-{int(time.time())}" - - try: - # Create minimal GKE cluster - cluster_config = { - "name": cluster_name, - "initial_node_count": 1, - "node_config": { - "machine_type": "e2-micro", - "disk_size_gb": 10, - "preemptible": True, # Use preemptible for cost savings - "oauth_scopes": ["https://www.googleapis.com/auth/cloud-platform"], - }, - "master_auth": { - "client_certificate_config": {"issue_client_certificate": False} - }, - "labels": {"clustrix-test": "true", "created-by": "test-suite"}, - } - - # Create cluster - operation = provider.create_gke_cluster( - zone=test_config["zone"], cluster_config=cluster_config - ) - - # Wait for cluster creation (this can take several minutes) - cluster = provider.wait_for_gke_operation( - operation=operation, zone=test_config["zone"], timeout=600 # 10 minutes - ) - - assert cluster is not None - assert cluster["name"] == cluster_name - assert cluster["status"] == "RUNNING" - - # Get cluster credentials - credentials = provider.get_gke_credentials( - zone=test_config["zone"], cluster_name=cluster_name - ) - - assert credentials is not None - assert "kubeconfig" in credentials or "endpoint" in credentials - - finally: - # Clean up - delete the cluster - try: - provider.delete_gke_cluster( - zone=test_config["zone"], cluster_name=cluster_name - ) - - # Wait for deletion - time.sleep(30) - - # Verify deletion - clusters = provider.list_gke_clusters(zone=test_config["zone"]) - assert not any(c["name"] == cluster_name for c in clusters) - - except Exception as e: - print(f"Warning: Failed to clean up cluster {cluster_name}: {e}") - - @pytest.mark.real_world - def test_storage_operations(self, gcp_credentials, test_config): - """ - Test Google Cloud Storage operations. - - This demonstrates: - - Bucket creation and deletion - - Object upload and download - - Storage lifecycle management - """ - provider = GCPProvider() - provider.authenticate( - credentials={ - "project_id": test_config["project_id"], - "service_account_key": json.dumps(gcp_credentials), - } - ) - - bucket_name = f"clustrix-test-{int(time.time())}" - - try: - # Create storage bucket - bucket = provider.create_storage_bucket( - bucket_name=bucket_name, - location=test_config["region"], - storage_class="STANDARD", - ) - - assert bucket is not None - assert bucket.name == bucket_name - - # Upload test data - test_data = b"Test data for clustrix storage operations" - blob_name = "test-file.txt" - - blob = provider.upload_to_bucket( - bucket_name=bucket_name, blob_name=blob_name, data=test_data - ) - - assert blob is not None - assert blob.name == blob_name - - # Download and verify - downloaded_data = provider.download_from_bucket( - bucket_name=bucket_name, blob_name=blob_name - ) - - assert downloaded_data == test_data - - # List bucket contents - blobs = provider.list_bucket_contents(bucket_name=bucket_name) - assert len(blobs) == 1 - assert blobs[0].name == blob_name - - finally: - # Clean up - try: - provider.delete_storage_bucket( - bucket_name=bucket_name, force=True # Delete even if not empty - ) - except Exception as e: - print(f"Warning: Failed to clean up bucket {bucket_name}: {e}") - - @pytest.mark.real_world - def test_network_operations(self, gcp_credentials, test_config): - """ - Test VPC network operations. - - This demonstrates: - - VPC creation and configuration - - Firewall rule management - - Network security setup - """ - provider = GCPProvider() - provider.authenticate( - credentials={ - "project_id": test_config["project_id"], - "service_account_key": json.dumps(gcp_credentials), - } - ) - - network_name = f"clustrix-test-vpc-{int(time.time())}" - - try: - # Create VPC network - network = provider.create_vpc_network( - network_name=network_name, - auto_create_subnetworks=True, - description="Test VPC for clustrix", - ) - - assert network is not None - assert network["name"] == network_name - - # Create firewall rule - firewall_rule_name = f"{network_name}-allow-ssh" - firewall_rule = provider.create_firewall_rule( - rule_name=firewall_rule_name, - network_name=network_name, - source_ranges=["0.0.0.0/0"], - allowed_protocols=["tcp"], - allowed_ports=["22"], - target_tags=["clustrix-ssh"], - ) - - assert firewall_rule is not None - assert firewall_rule["name"] == firewall_rule_name - - # List networks - networks = provider.list_networks() - assert any(n["name"] == network_name for n in networks) - - finally: - # Clean up - try: - # Delete firewall rule first - provider.delete_firewall_rule(firewall_rule_name) - time.sleep(5) - - # Delete network - provider.delete_vpc_network(network_name) - - except Exception as e: - print(f"Warning: Failed to clean up network {network_name}: {e}") - - def test_error_handling_without_credentials(self): - """ - Test error handling when credentials are missing. - - This demonstrates: - - Graceful failure without credentials - - Appropriate error messages - - No mock dependencies - - Rewritten: GCPProvider never had list_instances()/create_instance() - methods -- `git log -S` shows neither name was ever added to - clustrix/cloud_providers/gcp.py, so this assertion was against a - fabricated API from the day this test was written, independent of - the recent executor rewrite. The real, currently-implemented - equivalents are list_clusters() and create_compute_instance(). Both - used to proceed with a None client (or a placeholder result) when - unauthenticated; they now raise "Not authenticated with GCP" up - front, which is the real behavior this test now exercises. - """ - provider = GCPProvider() - - # Attempt authentication without credentials - success = provider.authenticate(credentials={}) - assert success is False - assert not provider.authenticated - - # Attempt operations without authentication - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.list_clusters() - - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_compute_instance(instance_name="test-instance") - - def test_region_zone_validation(self): - """ - Test GCP region/zone defaults and the offline region/machine-type - fallback lists. - - Rewritten: GCPProvider never had is_valid_region()/is_valid_zone() - methods -- `git log -S` shows neither was ever implemented, so this - test asserted a fabricated API from the day it was added, unrelated - to the recent executor rewrite. There is no region/zone validator - anywhere in clustrix to restore. Instead this exercises the real, - currently-callable region/zone behavior: the provider's default - region/zone (and the "-" convention GCP itself - uses to derive a zone from a region), and the actual unauthenticated - fallback lists get_available_regions()/get_available_instance_types() - return -- real GCP identifiers, not placeholders, returned without - any network call or mocking. - """ - provider = GCPProvider() - - # Defaults set in __init__, and GCP's own zone-from-region - # convention (also used by GCPProvider.authenticate()). - assert provider.region == "us-central1" - assert provider.zone == "us-central1-a" - assert provider.zone.startswith(provider.region + "-") - - region_pattern = re.compile(r"^[a-z]+-[a-z]+\d$") - regions = provider.get_available_regions() - assert "us-central1" in regions - for region in regions: - assert region_pattern.match(region), f"Not a GCP region format: {region}" - - machine_type_pattern = re.compile(r"^[a-z]\d-[a-z]+(-\d+)?$") - machine_types = provider.get_available_instance_types() - assert "e2-medium" in machine_types - for machine_type in machine_types: - assert machine_type_pattern.match( - machine_type - ), f"Not a GCP machine type format: {machine_type}" - - -class TestGCPProviderIntegrationWorkflows: - """Integration tests showing complete GCP workflows.""" - - @pytest.mark.real_world - def test_complete_cluster_lifecycle(self, gcp_credentials, test_config): - """ - Test complete cluster lifecycle as users would use it. - - This demonstrates the full user experience from setup - through execution to cleanup. - """ - from clustrix import cluster, configure - from clustrix.config import ClusterConfig - - # User configures GCP provider - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "gcp" - config.gcp_project_id = test_config["project_id"] - config.gcp_region = test_config["region"] - config.gcp_zone = test_config["zone"] - config.gcp_credentials = json.dumps(gcp_credentials) - - # Apply configuration - configure(config) - - try: - # User defines computation function - @cluster(cores=2, memory="2Gi", auto_provision=True) - def analyze_data_on_gcp(data_size): - """Run analysis on GCP infrastructure.""" - import platform - import socket - import numpy as np - - # Generate and analyze data - data = np.random.randn(data_size, data_size) - - # Compute statistics - results = { - "mean": float(np.mean(data)), - "std": float(np.std(data)), - "min": float(np.min(data)), - "max": float(np.max(data)), - "shape": data.shape, - "platform": platform.platform(), - "hostname": socket.gethostname(), - "provider": "gcp", - } - - # Compute eigenvalues for small subset - if data_size <= 100: - eigenvalues = np.linalg.eigvals(data[:10, :10]) - results["max_eigenvalue"] = float(np.max(np.abs(eigenvalues))) - - return results - - # Execute on GCP - result = analyze_data_on_gcp(50) - - # Validate execution - assert isinstance(result, dict) - assert "mean" in result - assert "std" in result - assert result["shape"] == (50, 50) - assert result["provider"] == "gcp" - assert ( - "gke" in result["hostname"].lower() - or "clustrix" in result["hostname"].lower() - ) - - finally: - # Cleanup would happen automatically with k8s_cleanup_on_exit - pass diff --git a/tests/test_cloud_providers_huggingface_spaces.py b/tests/test_cloud_providers_huggingface_spaces.py deleted file mode 100644 index 5f4d46c0..00000000 --- a/tests/test_cloud_providers_huggingface_spaces.py +++ /dev/null @@ -1,789 +0,0 @@ -import pytest -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime, timezone - -from clustrix.cloud_providers.huggingface_spaces import HuggingFaceSpacesProvider - - -def _http_response(status_code: int = 400): - """A real requests.Response for constructing HfHubHTTPError. - - huggingface_hub made ``response`` a required keyword-only argument, so - ``HfHubHTTPError("msg")`` raises TypeError on current releases while - working on older ones. Passing a real Response is correct on both, and - is what the library itself does. - """ - import requests - - response = requests.Response() - response.status_code = status_code - return response - - -class TestHuggingFaceSpacesProvider: - """Test HuggingFace Spaces provider functionality.""" - - @pytest.fixture - def provider(self): - """Create HuggingFaceSpacesProvider instance.""" - return HuggingFaceSpacesProvider() - - @pytest.fixture - def authenticated_provider(self): - """Create authenticated HuggingFaceSpacesProvider instance.""" - provider = HuggingFaceSpacesProvider() - provider.authenticated = True - provider.api_token = "test_token" - provider.username = "test_user" - provider.api = Mock() - provider.credentials = {"token": "test_token", "username": "test_user"} - return provider - - def test_initialization(self, provider): - """Test provider initialization.""" - assert provider.api_token is None - assert provider.username is None - assert provider.api is None - assert not provider.authenticated - - @patch("clustrix.cloud_providers.huggingface_spaces.HF_AVAILABLE", True) - @patch("clustrix.cloud_providers.huggingface_spaces.HfApi") - def test_authenticate_success(self, mock_hf_api_class, provider): - """Test successful authentication.""" - mock_api = Mock() - mock_hf_api_class.return_value = mock_api - mock_api.whoami.return_value = {"name": "test_user", "type": "user"} - - result = provider.authenticate(token="test_token", username="test_user") - - assert result is True - assert provider.authenticated is True - assert provider.api_token == "test_token" - assert provider.username == "test_user" - assert provider.api == mock_api - - mock_hf_api_class.assert_called_once_with(token="test_token") - mock_api.whoami.assert_called_once() - - @patch("clustrix.cloud_providers.huggingface_spaces.HF_AVAILABLE", False) - def test_authenticate_hf_not_available(self, provider): - """Test authentication when HuggingFace hub not available.""" - result = provider.authenticate(token="test_token", username="test_user") - - assert result is False - assert not provider.authenticated - - def test_authenticate_missing_token(self, provider): - """Test authentication with missing token.""" - result = provider.authenticate(username="test_user") - assert result is False - - def test_authenticate_missing_username(self, provider): - """Test authentication with missing username.""" - result = provider.authenticate(token="test_token") - assert result is False - - @patch("clustrix.cloud_providers.huggingface_spaces.HF_AVAILABLE", True) - @patch("clustrix.cloud_providers.huggingface_spaces.HfApi") - def test_authenticate_username_mismatch(self, mock_hf_api_class, provider): - """Test authentication with username mismatch.""" - mock_api = Mock() - mock_hf_api_class.return_value = mock_api - mock_api.whoami.return_value = {"name": "different_user", "type": "user"} - - result = provider.authenticate(token="test_token", username="test_user") - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.huggingface_spaces.HF_AVAILABLE", True) - @patch("clustrix.cloud_providers.huggingface_spaces.HfApi") - def test_authenticate_invalid_whoami(self, mock_hf_api_class, provider): - """Test authentication with invalid whoami response.""" - mock_api = Mock() - mock_hf_api_class.return_value = mock_api - mock_api.whoami.return_value = None - - result = provider.authenticate(token="test_token", username="test_user") - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.huggingface_spaces.HF_AVAILABLE", True) - @patch("clustrix.cloud_providers.huggingface_spaces.HfApi") - def test_authenticate_hf_hub_error(self, mock_hf_api_class, provider): - """Test authentication with HuggingFace Hub HTTP error.""" - from clustrix.cloud_providers.huggingface_spaces import HfHubHTTPError - - mock_api = Mock() - mock_hf_api_class.return_value = mock_api - mock_api.whoami.side_effect = HfHubHTTPError( - "Invalid token", response=_http_response() - ) - - result = provider.authenticate(token="test_token", username="test_user") - - assert result is False - assert not provider.authenticated - - @patch("clustrix.cloud_providers.huggingface_spaces.HF_AVAILABLE", True) - @patch("clustrix.cloud_providers.huggingface_spaces.HfApi") - def test_authenticate_unexpected_error(self, mock_hf_api_class, provider): - """Test authentication with unexpected error.""" - mock_api = Mock() - mock_hf_api_class.return_value = mock_api - mock_api.whoami.side_effect = Exception("Network error") - - result = provider.authenticate(token="test_token", username="test_user") - - assert result is False - assert not provider.authenticated - - def test_validate_credentials_success(self, authenticated_provider): - """Test successful credential validation.""" - authenticated_provider.api.whoami.return_value = {"name": "test_user"} - - result = authenticated_provider.validate_credentials() - - assert result is True - - def test_validate_credentials_failure(self, authenticated_provider): - """Test failed credential validation.""" - authenticated_provider.api.whoami.return_value = None - - result = authenticated_provider.validate_credentials() - - assert result is False - - def test_validate_credentials_not_authenticated(self, provider): - """Test credential validation when not authenticated.""" - result = provider.validate_credentials() - - assert result is False - - def test_validate_credentials_exception(self, authenticated_provider): - """Test credential validation with exception.""" - authenticated_provider.api.whoami.side_effect = Exception("API error") - - result = authenticated_provider.validate_credentials() - - assert result is False - - @patch("clustrix.cloud_providers.huggingface_spaces.SpaceHardware") - def test_create_space_success_basic( - self, mock_space_hardware, authenticated_provider - ): - """Test successful space creation with basic hardware.""" - authenticated_provider.api.create_repo.return_value = ( - "https://huggingface.co/spaces/test_user/test-space" - ) - - with patch( - "clustrix.cloud_providers.huggingface_spaces.datetime" - ) as mock_datetime: - mock_datetime.now.return_value.isoformat.return_value = ( - "2024-01-01T00:00:00+00:00" - ) - mock_datetime.timezone = timezone - - result = authenticated_provider.create_space( - space_name="test-space", - hardware="cpu-basic", - sdk="gradio", - private=False, - ) - - assert result["space_name"] == "test-space" - assert result["space_id"] == "test_user/test-space" - assert ( - result["space_url"] == "https://huggingface.co/spaces/test_user/test-space" - ) - assert result["sdk"] == "gradio" - assert result["hardware"] == "cpu-basic" - assert result["private"] is False - assert result["status"] == "creating" - - authenticated_provider.api.create_repo.assert_called_once_with( - repo_id="test_user/test-space", - repo_type="space", - space_sdk="gradio", - private=False, - ) - - @patch("clustrix.cloud_providers.huggingface_spaces.SpaceHardware") - def test_create_space_success_gpu( - self, mock_space_hardware, authenticated_provider - ): - """Test successful space creation with GPU hardware.""" - authenticated_provider.api.create_repo.return_value = ( - "https://huggingface.co/spaces/test_user/gpu-space" - ) - mock_space_hardware.T4_SMALL = "t4-small" - - result = authenticated_provider.create_space( - space_name="gpu-space", hardware="t4-small", sdk="streamlit", private=True - ) - - assert result["space_name"] == "gpu-space" - assert result["hardware"] == "t4-small" - assert result["sdk"] == "streamlit" - assert result["private"] is True - - # Verify hardware request was made - authenticated_provider.api.request_space_hardware.assert_called_once_with( - repo_id="test_user/gpu-space", hardware="t4-small" - ) - - @patch("clustrix.cloud_providers.huggingface_spaces.SpaceHardware") - def test_create_space_unknown_hardware( - self, mock_space_hardware, authenticated_provider - ): - """Test space creation with unknown hardware type.""" - authenticated_provider.api.create_repo.return_value = ( - "https://huggingface.co/spaces/test_user/test-space" - ) - - result = authenticated_provider.create_space( - space_name="test-space", hardware="unknown-hardware" - ) - - # Should fallback to cpu-basic - assert result["hardware"] == "cpu-basic" - - # Hardware request should not be called for unknown hardware - authenticated_provider.api.request_space_hardware.assert_not_called() - - @patch("clustrix.cloud_providers.huggingface_spaces.SpaceHardware") - def test_create_space_hardware_request_fails( - self, mock_space_hardware, authenticated_provider - ): - """Test space creation when hardware request fails.""" - authenticated_provider.api.create_repo.return_value = ( - "https://huggingface.co/spaces/test_user/test-space" - ) - authenticated_provider.api.request_space_hardware.side_effect = Exception( - "Hardware request failed" - ) - mock_space_hardware.T4_SMALL = "t4-small" - - result = authenticated_provider.create_space( - space_name="test-space", hardware="t4-small" - ) - - # Should fallback to cpu-basic when hardware request fails - assert result["hardware"] == "cpu-basic" - - def test_create_space_not_authenticated(self, provider): - """Test space creation when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_space("test-space") - - def test_create_space_hf_hub_error(self, authenticated_provider): - """Test space creation with HuggingFace Hub error.""" - from clustrix.cloud_providers.huggingface_spaces import HfHubHTTPError - - authenticated_provider.api.create_repo.side_effect = HfHubHTTPError( - "Space already exists", response=_http_response() - ) - - with pytest.raises(HfHubHTTPError): - authenticated_provider.create_space("test-space") - - def test_create_space_unexpected_error(self, authenticated_provider): - """Test space creation with unexpected error.""" - authenticated_provider.api.create_repo.side_effect = Exception( - "Unexpected error" - ) - - with pytest.raises(Exception, match="Unexpected error"): - authenticated_provider.create_space("test-space") - - def test_create_cluster(self, authenticated_provider): - """Test create_cluster method.""" - with patch.object(authenticated_provider, "create_space") as mock_create: - mock_create.return_value = {"space_id": "test_user/test-cluster"} - - result = authenticated_provider.create_cluster( - "test-cluster", hardware="t4-small", sdk="gradio" - ) - - mock_create.assert_called_once_with( - "test-cluster", hardware="t4-small", sdk="gradio" - ) - assert result == {"space_id": "test_user/test-cluster"} - - def test_delete_cluster_success(self, authenticated_provider): - """Test successful cluster deletion.""" - result = authenticated_provider.delete_cluster("test_user/test-space") - - assert result is True - authenticated_provider.api.delete_repo.assert_called_once_with( - repo_id="test_user/test-space", repo_type="space" - ) - - def test_delete_cluster_not_authenticated(self, provider): - """Test cluster deletion when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.delete_cluster("test_user/test-space") - - def test_delete_cluster_hf_hub_error(self, authenticated_provider): - """Test cluster deletion with HuggingFace Hub error.""" - from clustrix.cloud_providers.huggingface_spaces import HfHubHTTPError - - authenticated_provider.api.delete_repo.side_effect = HfHubHTTPError( - "Space not found", response=_http_response() - ) - - result = authenticated_provider.delete_cluster("test_user/test-space") - - assert result is False - - def test_delete_cluster_unexpected_error(self, authenticated_provider): - """Test cluster deletion with unexpected error.""" - authenticated_provider.api.delete_repo.side_effect = Exception("Network error") - - result = authenticated_provider.delete_cluster("test_user/test-space") - - assert result is False - - def test_get_cluster_status_success(self, authenticated_provider): - """Test successful cluster status retrieval.""" - mock_space_info = Mock() - mock_space_info.sdk = "gradio" - authenticated_provider.api.space_info.return_value = mock_space_info - - mock_runtime = Mock() - mock_runtime.stage = "RUNNING" - mock_runtime.hardware = "t4-small" - authenticated_provider.api.get_space_runtime.return_value = mock_runtime - - result = authenticated_provider.get_cluster_status("test_user/test-space") - - assert result["space_id"] == "test_user/test-space" - assert result["status"] == "running" - assert result["hardware"] == "t4-small" - assert result["sdk"] == "gradio" - assert result["provider"] == "huggingface" - assert result["cluster_type"] == "spaces" - - def test_get_cluster_status_runtime_error(self, authenticated_provider): - """Test cluster status when runtime info fails.""" - mock_space_info = Mock() - mock_space_info.sdk = "gradio" - authenticated_provider.api.space_info.return_value = mock_space_info - authenticated_provider.api.get_space_runtime.side_effect = Exception( - "Runtime error" - ) - - result = authenticated_provider.get_cluster_status("test_user/test-space") - - assert result["status"] == "unknown" - assert result["hardware"] == "unknown" - assert result["sdk"] == "gradio" - - def test_get_cluster_status_not_found(self, authenticated_provider): - """Test cluster status for non-existent space.""" - from clustrix.cloud_providers.huggingface_spaces import HfHubHTTPError - - authenticated_provider.api.space_info.side_effect = HfHubHTTPError( - "404 Space not found", response=_http_response() - ) - - result = authenticated_provider.get_cluster_status("test_user/nonexistent") - - assert result["space_id"] == "test_user/nonexistent" - assert result["status"] == "not_found" - assert result["provider"] == "huggingface" - - def test_get_cluster_status_not_authenticated(self, provider): - """Test cluster status when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.get_cluster_status("test_user/test-space") - - def test_get_cluster_status_hf_hub_error(self, authenticated_provider): - """Test cluster status with non-404 HuggingFace Hub error.""" - from clustrix.cloud_providers.huggingface_spaces import HfHubHTTPError - - authenticated_provider.api.space_info.side_effect = HfHubHTTPError( - "500 Server error", response=_http_response() - ) - - with pytest.raises(HfHubHTTPError): - authenticated_provider.get_cluster_status("test_user/test-space") - - def test_get_cluster_status_unexpected_error(self, authenticated_provider): - """Test cluster status with unexpected error.""" - authenticated_provider.api.space_info.side_effect = Exception("Network error") - - with pytest.raises(Exception, match="Network error"): - authenticated_provider.get_cluster_status("test_user/test-space") - - def test_list_clusters_success(self, authenticated_provider): - """Test successful cluster listing.""" - mock_space1 = Mock() - mock_space1.id = "test_user/space1" - mock_space1.sdk = "gradio" - mock_space1.private = False - - mock_space2 = Mock() - mock_space2.id = "test_user/space2" - mock_space2.sdk = "streamlit" - mock_space2.private = True - - authenticated_provider.api.list_spaces.return_value = [mock_space1, mock_space2] - - # Mock runtime info for each space - def mock_runtime_side_effect(space_id): - if space_id == "test_user/space1": - runtime = Mock() - runtime.stage = "RUNNING" - runtime.hardware = "cpu-basic" - return runtime - elif space_id == "test_user/space2": - runtime = Mock() - runtime.stage = "BUILDING" - runtime.hardware = "t4-small" - return runtime - else: - raise Exception("Runtime error") - - authenticated_provider.api.get_space_runtime.side_effect = ( - mock_runtime_side_effect - ) - - result = authenticated_provider.list_clusters() - - assert len(result) == 2 - - # Check first space - assert result[0]["name"] == "space1" - assert result[0]["space_id"] == "test_user/space1" - assert result[0]["type"] == "space" - assert result[0]["status"] == "running" - assert result[0]["sdk"] == "gradio" - assert result[0]["hardware"] == "cpu-basic" - assert result[0]["private"] is False - - # Check second space - assert result[1]["name"] == "space2" - assert result[1]["space_id"] == "test_user/space2" - assert result[1]["status"] == "building" - assert result[1]["sdk"] == "streamlit" - assert result[1]["hardware"] == "t4-small" - assert result[1]["private"] is True - - def test_list_clusters_runtime_errors(self, authenticated_provider): - """Test cluster listing with runtime errors.""" - mock_space = Mock() - mock_space.id = "test_user/space1" - mock_space.sdk = "gradio" - mock_space.private = False - - authenticated_provider.api.list_spaces.return_value = [mock_space] - authenticated_provider.api.get_space_runtime.side_effect = Exception( - "Runtime error" - ) - - result = authenticated_provider.list_clusters() - - assert len(result) == 1 - assert result[0]["status"] == "unknown" - assert result[0]["hardware"] == "unknown" - - def test_list_clusters_not_authenticated(self, provider): - """Test cluster listing when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.list_clusters() - - def test_list_clusters_hf_hub_error(self, authenticated_provider): - """Test cluster listing with HuggingFace Hub error.""" - from clustrix.cloud_providers.huggingface_spaces import HfHubHTTPError - - authenticated_provider.api.list_spaces.side_effect = HfHubHTTPError( - "API error", response=_http_response() - ) - - result = authenticated_provider.list_clusters() - - assert result == [] - - def test_list_clusters_unexpected_error(self, authenticated_provider): - """Test cluster listing with unexpected error.""" - authenticated_provider.api.list_spaces.side_effect = Exception("Network error") - - result = authenticated_provider.list_clusters() - - assert result == [] - - def test_get_cluster_config_success(self, authenticated_provider): - """Test successful cluster config retrieval.""" - mock_space_info = Mock() - mock_space_info.sdk = "gradio" - authenticated_provider.api.space_info.return_value = mock_space_info - - mock_runtime = Mock() - mock_runtime.hardware = "t4-small" - authenticated_provider.api.get_space_runtime.return_value = mock_runtime - - result = authenticated_provider.get_cluster_config("test_user/test-space") - - assert result["name"] == "HuggingFace Space - test_user/test-space" - assert result["cluster_type"] == "api" - assert ( - result["cluster_host"] - == "https://huggingface.co/spaces/test_user/test-space" - ) - assert ( - result["api_endpoint"] - == "https://huggingface.co/spaces/test_user/test-space/api/predict" - ) - assert result["default_cores"] == 4 # t4-small cores - assert result["default_memory"] == "15GB" # t4-small memory - assert result["cost_monitoring"] is True - assert result["provider"] == "huggingface" - assert result["provider_config"]["space_id"] == "test_user/test-space" - assert result["provider_config"]["hardware"] == "t4-small" - assert result["provider_config"]["sdk"] == "gradio" - assert result["provider_config"]["api_token"] == "***" - - def test_get_cluster_config_runtime_error(self, authenticated_provider): - """Test cluster config when runtime info fails.""" - mock_space_info = Mock() - mock_space_info.sdk = "gradio" - authenticated_provider.api.space_info.return_value = mock_space_info - authenticated_provider.api.get_space_runtime.side_effect = Exception( - "Runtime error" - ) - - result = authenticated_provider.get_cluster_config("test_user/test-space") - - # Should use cpu-basic defaults when runtime fails - assert result["default_cores"] == 2 # cpu-basic cores - assert result["default_memory"] == "16GB" # cpu-basic memory - - def test_get_cluster_config_not_authenticated(self, provider): - """Test cluster config when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.get_cluster_config("test_user/test-space") - - def test_get_cluster_config_exception(self, authenticated_provider): - """Test cluster config with exception.""" - authenticated_provider.api.space_info.side_effect = Exception("API error") - - result = authenticated_provider.get_cluster_config("test_user/test-space") - - # Should return basic config - assert result["name"] == "HuggingFace Space - test_user/test-space" - assert result["cluster_type"] == "api" - assert ( - result["cluster_host"] - == "https://huggingface.co/spaces/test_user/test-space" - ) - assert result["provider"] == "huggingface" - - def test_estimate_cost_cpu_basic(self, provider): - """Test cost estimation for CPU basic (free).""" - result = provider.estimate_cost(hardware="cpu-basic", hours=10) - - assert result["compute"] == 0.0 - assert result["total"] == 0.0 - - def test_estimate_cost_gpu(self, provider): - """Test cost estimation for GPU hardware.""" - result = provider.estimate_cost(hardware="t4-small", hours=5) - - assert result["compute"] == 0.60 * 5 - assert result["total"] == 0.60 * 5 - - def test_estimate_cost_unknown_hardware(self, provider): - """Test cost estimation with unknown hardware.""" - result = provider.estimate_cost(hardware="unknown", hours=2) - - assert result["compute"] == 0.0 # Default to free - assert result["total"] == 0.0 - - def test_estimate_cost_defaults(self, provider): - """Test cost estimation with default values.""" - result = provider.estimate_cost() - - assert result["compute"] == 0.0 # cpu-basic for 1 hour - assert result["total"] == 0.0 - - def test_get_available_instance_types(self, provider): - """Test available instance types.""" - result = provider.get_available_instance_types() - - expected_types = [ - "cpu-basic", - "cpu-upgrade", - "t4-small", - "t4-medium", - "a10g-small", - "a10g-large", - "a100-large", - ] - - assert result == expected_types - - def test_get_available_instance_types_with_region(self, provider): - """Test available instance types with region parameter.""" - result = provider.get_available_instance_types(region="us-east-1") - - # Should return same types regardless of region - assert "cpu-basic" in result - assert "t4-small" in result - - def test_get_available_regions(self, provider): - """Test available regions.""" - result = provider.get_available_regions() - - assert result == ["global"] - - def test_hardware_to_cores_mapping(self, provider): - """Test hardware to cores mapping.""" - assert provider._hardware_to_cores("cpu-basic") == 2 - assert provider._hardware_to_cores("cpu-upgrade") == 8 - assert provider._hardware_to_cores("t4-small") == 4 - assert provider._hardware_to_cores("t4-medium") == 8 - assert provider._hardware_to_cores("a10g-small") == 4 - assert provider._hardware_to_cores("a10g-large") == 12 - assert provider._hardware_to_cores("a100-large") == 12 - assert provider._hardware_to_cores("unknown") == 2 # Default - - def test_hardware_to_memory_mapping(self, provider): - """Test hardware to memory mapping.""" - assert provider._hardware_to_memory("cpu-basic") == "16GB" - assert provider._hardware_to_memory("cpu-upgrade") == "32GB" - assert provider._hardware_to_memory("t4-small") == "15GB" - assert provider._hardware_to_memory("t4-medium") == "15GB" - assert provider._hardware_to_memory("a10g-small") == "24GB" - assert provider._hardware_to_memory("a10g-large") == "96GB" - assert provider._hardware_to_memory("a100-large") == "142GB" - assert provider._hardware_to_memory("unknown") == "16GB" # Default - - -class TestHuggingFaceSpacesProviderEdgeCases: - """Test edge cases and error handling.""" - - def test_create_space_defaults(self): - """Test space creation with default parameters.""" - provider = HuggingFaceSpacesProvider() - provider.authenticated = True - provider.username = "test_user" - provider.api = Mock() - provider.api.create_repo.return_value = ( - "https://huggingface.co/spaces/test_user/test-space" - ) - - result = provider.create_space("test-space") - - # Check defaults - assert result["hardware"] == "cpu-basic" - assert result["sdk"] == "gradio" - assert result["private"] is False - - def test_list_clusters_complex_space_names(self): - """Test cluster listing with complex space names.""" - provider = HuggingFaceSpacesProvider() - provider.authenticated = True - provider.username = "test_user" - provider.api = Mock() - - mock_space = Mock() - mock_space.id = "test_user/my-complex-space-name" - mock_space.sdk = "gradio" - mock_space.private = False - - provider.api.list_spaces.return_value = [mock_space] - provider.api.get_space_runtime.side_effect = Exception("Runtime error") - - result = provider.list_clusters() - - assert len(result) == 1 - assert result[0]["name"] == "my-complex-space-name" - assert result[0]["space_id"] == "test_user/my-complex-space-name" - - def test_get_cluster_status_no_space_info(self): - """Test cluster status when space_info returns None.""" - provider = HuggingFaceSpacesProvider() - provider.authenticated = True - provider.api = Mock() - provider.api.space_info.return_value = None - provider.api.get_space_runtime.side_effect = Exception("Runtime error") - - result = provider.get_cluster_status("test_user/test-space") - - assert result["sdk"] == "unknown" - - def test_get_cluster_config_no_space_info(self): - """Test cluster config when space_info returns None.""" - provider = HuggingFaceSpacesProvider() - provider.authenticated = True - provider.api = Mock() - provider.api.space_info.return_value = None - provider.api.get_space_runtime.side_effect = Exception("Runtime error") - - result = provider.get_cluster_config("test_user/test-space") - - assert result["provider_config"]["sdk"] == "unknown" - - def test_create_space_all_hardware_types(self): - """Test space creation with all supported hardware types.""" - provider = HuggingFaceSpacesProvider() - provider.authenticated = True - provider.username = "test_user" - provider.api = Mock() - provider.api.create_repo.return_value = ( - "https://huggingface.co/spaces/test_user/test-space" - ) - - # Test all hardware types from the mapping - hardware_types = [ - "cpu-upgrade", - "t4-small", - "t4-medium", - "a10g-small", - "a10g-large", - "a100-large", - ] - - with patch( - "clustrix.cloud_providers.huggingface_spaces.SpaceHardware" - ) as mock_hardware: - # Mock all hardware enum values - mock_hardware.CPU_UPGRADE = "cpu-upgrade" - mock_hardware.T4_SMALL = "t4-small" - mock_hardware.T4_MEDIUM = "t4-medium" - mock_hardware.A10G_SMALL = "a10g-small" - mock_hardware.A10G_LARGE = "a10g-large" - mock_hardware.A100_LARGE = "a100-large" - - for hardware in hardware_types: - result = provider.create_space( - f"test-space-{hardware}", hardware=hardware - ) - assert result["hardware"] == hardware - - # Verify hardware request was made - provider.api.request_space_hardware.assert_called_with( - repo_id=f"test_user/test-space-{hardware}", hardware=hardware - ) - - def test_estimate_cost_all_hardware_types(self): - """Test cost estimation for all hardware types.""" - provider = HuggingFaceSpacesProvider() - - expected_costs = { - "cpu-basic": 0.0, - "cpu-upgrade": 0.03, - "t4-small": 0.60, - "t4-medium": 0.90, - "a10g-small": 1.05, - "a10g-large": 3.15, - "a100-large": 4.13, - } - - for hardware, expected_cost in expected_costs.items(): - result = provider.estimate_cost(hardware=hardware, hours=1) - assert result["compute"] == expected_cost - assert result["total"] == expected_cost diff --git a/tests/test_cloud_providers_lambda_cloud.py b/tests/test_cloud_providers_lambda_cloud.py deleted file mode 100644 index a79ee7d2..00000000 --- a/tests/test_cloud_providers_lambda_cloud.py +++ /dev/null @@ -1,808 +0,0 @@ -import pytest -import requests -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime, timezone - -from clustrix.cloud_providers.lambda_cloud import LambdaCloudProvider - - -class TestLambdaCloudProvider: - """Test Lambda Cloud provider functionality.""" - - @pytest.fixture - def provider(self): - """Create LambdaCloudProvider instance.""" - return LambdaCloudProvider() - - @pytest.fixture - def authenticated_provider(self): - """Create authenticated LambdaCloudProvider instance.""" - provider = LambdaCloudProvider() - provider.authenticated = True - provider.api_key = "test_api_key" - provider.session = Mock() - provider.credentials = {"api_key": "test_api_key"} - return provider - - def test_initialization(self, provider): - """Test provider initialization.""" - assert provider.api_key is None - assert provider.base_url == "https://cloud.lambdalabs.com/api/v1" - assert provider.session is None - assert not provider.authenticated - - @patch("requests.Session") - def test_authenticate_success(self, mock_session_class, provider): - """Test successful authentication.""" - mock_session = Mock() - mock_session_class.return_value = mock_session - - # Mock successful response - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = {"data": []} - mock_session.get.return_value = mock_response - - result = provider.authenticate(api_key="test_api_key") - - assert result is True - assert provider.authenticated is True - assert provider.api_key == "test_api_key" - assert provider.session == mock_session - - # Verify session headers were set - mock_session.headers.update.assert_called_once_with( - { - "Authorization": "Bearer test_api_key", - "Content-Type": "application/json", - } - ) - - # Verify API call was made - mock_session.get.assert_called_once_with( - "https://cloud.lambdalabs.com/api/v1/instance-types" - ) - - @patch("requests.Session") - def test_authenticate_invalid_api_key(self, mock_session_class, provider): - """Test authentication with invalid API key.""" - mock_session = Mock() - mock_session_class.return_value = mock_session - - # Mock 401 response - mock_response = Mock() - mock_response.status_code = 401 - mock_session.get.return_value = mock_response - - result = provider.authenticate(api_key="invalid_key") - - assert result is False - assert not provider.authenticated - - @patch("requests.Session") - def test_authenticate_missing_api_key(self, mock_session_class, provider): - """Test authentication without API key.""" - result = provider.authenticate() - - assert result is False - assert not provider.authenticated - - @patch("requests.Session") - def test_authenticate_api_error(self, mock_session_class, provider): - """Test authentication with API error.""" - mock_session = Mock() - mock_session_class.return_value = mock_session - - # Mock 500 response - mock_response = Mock() - mock_response.status_code = 500 - mock_session.get.return_value = mock_response - - result = provider.authenticate(api_key="test_api_key") - - assert result is False - assert not provider.authenticated - - @patch("requests.Session") - def test_authenticate_connection_error(self, mock_session_class, provider): - """Test authentication with connection error.""" - mock_session = Mock() - mock_session_class.return_value = mock_session - mock_session.get.side_effect = requests.RequestException("Connection failed") - - result = provider.authenticate(api_key="test_api_key") - - assert result is False - assert not provider.authenticated - - @patch("requests.Session") - def test_authenticate_unexpected_error(self, mock_session_class, provider): - """Test authentication with unexpected error.""" - mock_session = Mock() - mock_session_class.return_value = mock_session - mock_session.get.side_effect = Exception("Unexpected error") - - result = provider.authenticate(api_key="test_api_key") - - assert result is False - assert not provider.authenticated - - def test_validate_credentials_success(self, authenticated_provider): - """Test successful credential validation.""" - mock_response = Mock() - mock_response.status_code = 200 - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.validate_credentials() - - assert result is True - - def test_validate_credentials_failure(self, authenticated_provider): - """Test failed credential validation.""" - mock_response = Mock() - mock_response.status_code = 401 - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.validate_credentials() - - assert result is False - - def test_validate_credentials_not_authenticated(self, provider): - """Test credential validation when not authenticated.""" - result = provider.validate_credentials() - - assert result is False - - def test_validate_credentials_exception(self, authenticated_provider): - """Test credential validation with exception.""" - authenticated_provider.session.get.side_effect = Exception("Network error") - - result = authenticated_provider.validate_credentials() - - assert result is False - - def test_create_instance_success(self, authenticated_provider): - """Test successful instance creation.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = {"instance_ids": ["i-12345"]} - authenticated_provider.session.post.return_value = mock_response - - with patch("clustrix.cloud_providers.lambda_cloud.datetime") as mock_datetime: - mock_datetime.now.return_value.isoformat.return_value = ( - "2024-01-01T00:00:00+00:00" - ) - mock_datetime.timezone = timezone - - result = authenticated_provider.create_instance( - instance_name="test-instance", - instance_type="gpu_1x_a10", - region="us-east-1", - ssh_key_name="my-key", - ) - - expected_data = { - "region_name": "us-east-1", - "instance_type_name": "gpu_1x_a10", - "ssh_key_names": ["my-key"], - "file_system_names": [], - "quantity": 1, - "name": "test-instance", - } - - authenticated_provider.session.post.assert_called_once_with( - "https://cloud.lambdalabs.com/api/v1/instance-operations/launch", - json=expected_data, - ) - - assert result["instance_name"] == "test-instance" - assert result["instance_id"] == "i-12345" - assert result["instance_type"] == "gpu_1x_a10" - assert result["region"] == "us-east-1" - assert result["status"] == "booting" - - def test_create_instance_no_ssh_key(self, authenticated_provider): - """Test instance creation without SSH key.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = {"instance_ids": ["i-12345"]} - authenticated_provider.session.post.return_value = mock_response - - authenticated_provider.create_instance( - instance_name="test-instance", - instance_type="gpu_1x_a10", - region="us-east-1", - ) - - call_args = authenticated_provider.session.post.call_args[1]["json"] - assert call_args["ssh_key_names"] == [] - - def test_create_instance_not_authenticated(self, provider): - """Test instance creation when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.create_instance("test-instance") - - def test_create_instance_no_instance_ids(self, authenticated_provider): - """Test instance creation with no instance IDs returned.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = {"instance_ids": []} - authenticated_provider.session.post.return_value = mock_response - - with pytest.raises(RuntimeError, match="No instance ID returned"): - authenticated_provider.create_instance("test-instance") - - def test_create_instance_api_error(self, authenticated_provider): - """Test instance creation with API error.""" - mock_response = Mock() - mock_response.status_code = 400 - mock_response.headers = {"content-type": "application/json"} - mock_response.json.return_value = {"error": "Invalid instance type"} - authenticated_provider.session.post.return_value = mock_response - - with pytest.raises( - RuntimeError, match="Failed to create instance.*Invalid instance type" - ): - authenticated_provider.create_instance("test-instance") - - def test_create_instance_api_error_no_json(self, authenticated_provider): - """Test instance creation with non-JSON API error.""" - mock_response = Mock() - mock_response.status_code = 400 - mock_response.headers = {"content-type": "text/plain"} - authenticated_provider.session.post.return_value = mock_response - - with pytest.raises(RuntimeError, match="Failed to create instance: 400"): - authenticated_provider.create_instance("test-instance") - - def test_create_instance_request_exception(self, authenticated_provider): - """Test instance creation with request exception.""" - authenticated_provider.session.post.side_effect = requests.RequestException( - "Network error" - ) - - with pytest.raises(requests.RequestException): - authenticated_provider.create_instance("test-instance") - - def test_create_instance_unexpected_exception(self, authenticated_provider): - """Test instance creation with unexpected exception.""" - authenticated_provider.session.post.side_effect = Exception("Unexpected error") - - with pytest.raises(Exception, match="Unexpected error"): - authenticated_provider.create_instance("test-instance") - - def test_create_cluster(self, authenticated_provider): - """Test create_cluster method.""" - with patch.object(authenticated_provider, "create_instance") as mock_create: - mock_create.return_value = {"instance_id": "i-12345"} - - result = authenticated_provider.create_cluster( - "test-cluster", instance_type="gpu_1x_a10", region="us-east-1" - ) - - mock_create.assert_called_once_with( - "test-cluster", instance_type="gpu_1x_a10", region="us-east-1" - ) - assert result == {"instance_id": "i-12345"} - - def test_delete_cluster_success(self, authenticated_provider): - """Test successful cluster deletion.""" - mock_response = Mock() - mock_response.status_code = 200 - authenticated_provider.session.post.return_value = mock_response - - result = authenticated_provider.delete_cluster("i-12345") - - assert result is True - authenticated_provider.session.post.assert_called_once_with( - "https://cloud.lambdalabs.com/api/v1/instance-operations/terminate", - json={"instance_ids": ["i-12345"]}, - ) - - def test_delete_cluster_not_authenticated(self, provider): - """Test cluster deletion when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.delete_cluster("i-12345") - - def test_delete_cluster_api_error(self, authenticated_provider): - """Test cluster deletion with API error.""" - mock_response = Mock() - mock_response.status_code = 400 - mock_response.headers = {"content-type": "application/json"} - mock_response.json.return_value = {"error": "Instance not found"} - authenticated_provider.session.post.return_value = mock_response - - result = authenticated_provider.delete_cluster("i-12345") - - assert result is False - - def test_delete_cluster_request_exception(self, authenticated_provider): - """Test cluster deletion with request exception.""" - authenticated_provider.session.post.side_effect = requests.RequestException( - "Network error" - ) - - result = authenticated_provider.delete_cluster("i-12345") - - assert result is False - - def test_delete_cluster_unexpected_exception(self, authenticated_provider): - """Test cluster deletion with unexpected exception.""" - authenticated_provider.session.post.side_effect = Exception("Unexpected error") - - result = authenticated_provider.delete_cluster("i-12345") - - assert result is False - - def test_get_cluster_status_success(self, authenticated_provider): - """Test successful cluster status retrieval.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "status": "running", - "instance_type": {"name": "gpu_1x_a10"}, - "region": {"name": "us-east-1"}, - } - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_cluster_status("i-12345") - - assert result["instance_id"] == "i-12345" - assert result["status"] == "running" - assert result["instance_type"] == "gpu_1x_a10" - assert result["region"] == "us-east-1" - assert result["provider"] == "lambda" - assert result["cluster_type"] == "ssh" - - def test_get_cluster_status_not_found(self, authenticated_provider): - """Test cluster status for non-existent instance.""" - mock_response = Mock() - mock_response.status_code = 404 - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_cluster_status("i-12345") - - assert result["instance_id"] == "i-12345" - assert result["status"] == "not_found" - assert result["provider"] == "lambda" - - def test_get_cluster_status_not_authenticated(self, provider): - """Test cluster status when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.get_cluster_status("i-12345") - - def test_get_cluster_status_api_error(self, authenticated_provider): - """Test cluster status with API error.""" - mock_response = Mock() - mock_response.status_code = 500 - authenticated_provider.session.get.return_value = mock_response - - with pytest.raises(RuntimeError, match="Failed to get instance status"): - authenticated_provider.get_cluster_status("i-12345") - - def test_get_cluster_status_request_exception(self, authenticated_provider): - """Test cluster status with request exception.""" - authenticated_provider.session.get.side_effect = requests.RequestException( - "Network error" - ) - - with pytest.raises(requests.RequestException): - authenticated_provider.get_cluster_status("i-12345") - - def test_get_cluster_status_unexpected_exception(self, authenticated_provider): - """Test cluster status with unexpected exception.""" - authenticated_provider.session.get.side_effect = Exception("Unexpected error") - - with pytest.raises(Exception, match="Unexpected error"): - authenticated_provider.get_cluster_status("i-12345") - - def test_list_clusters_success(self, authenticated_provider): - """Test successful cluster listing.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "data": [ - { - "name": "instance-1", - "id": "i-12345", - "status": "running", - "instance_type": {"name": "gpu_1x_a10"}, - "region": {"name": "us-east-1"}, - }, - { - "id": "i-67890", # No name field - "status": "stopped", - "instance_type": {"name": "gpu_1x_a100"}, - "region": {"name": "us-west-2"}, - }, - ] - } - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.list_clusters() - - assert len(result) == 2 - - # First instance - assert result[0]["name"] == "instance-1" - assert result[0]["instance_id"] == "i-12345" - assert result[0]["type"] == "gpu" - assert result[0]["status"] == "running" - assert result[0]["instance_type"] == "gpu_1x_a10" - assert result[0]["region"] == "us-east-1" - - # Second instance (no name) - assert result[1]["name"] == "i-67890" - assert result[1]["instance_id"] == "i-67890" - assert result[1]["status"] == "stopped" - - def test_list_clusters_not_authenticated(self, provider): - """Test cluster listing when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.list_clusters() - - def test_list_clusters_api_error(self, authenticated_provider): - """Test cluster listing with API error.""" - mock_response = Mock() - mock_response.status_code = 500 - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.list_clusters() - - assert result == [] - - def test_list_clusters_request_exception(self, authenticated_provider): - """Test cluster listing with request exception.""" - authenticated_provider.session.get.side_effect = requests.RequestException( - "Network error" - ) - - result = authenticated_provider.list_clusters() - - assert result == [] - - def test_list_clusters_unexpected_exception(self, authenticated_provider): - """Test cluster listing with unexpected exception.""" - authenticated_provider.session.get.side_effect = Exception("Unexpected error") - - result = authenticated_provider.list_clusters() - - assert result == [] - - def test_get_cluster_config_success(self, authenticated_provider): - """Test successful cluster config retrieval.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "ip": "1.2.3.4", - "instance_type": {"name": "gpu_1x_a10"}, - "region": {"name": "us-east-1"}, - } - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_cluster_config("i-12345") - - assert result["name"] == "Lambda Cloud - i-12345" - assert result["cluster_type"] == "ssh" - assert result["cluster_host"] == "1.2.3.4" - assert result["username"] == "ubuntu" - assert result["cluster_port"] == 22 - assert result["default_cores"] == 8 - assert result["default_memory"] == "32GB" - assert result["remote_work_dir"] == "/home/ubuntu/clustrix" - assert result["package_manager"] == "conda" - assert result["cost_monitoring"] is True - assert result["provider"] == "lambda" - assert result["provider_config"]["instance_id"] == "i-12345" - assert result["provider_config"]["instance_type"] == "gpu_1x_a10" - assert result["provider_config"]["region"] == "us-east-1" - - def test_get_cluster_config_not_authenticated(self, provider): - """Test cluster config when not authenticated.""" - with pytest.raises(RuntimeError, match="Not authenticated"): - provider.get_cluster_config("i-12345") - - def test_get_cluster_config_api_error(self, authenticated_provider): - """Test cluster config with API error.""" - mock_response = Mock() - mock_response.status_code = 404 - authenticated_provider.session.get.return_value = mock_response - - # This used to return cluster_host "placeholder.lambdalabs.com", - # which clustrix then tried to SSH into (see #119). - with pytest.raises(RuntimeError, match="HTTP 404"): - authenticated_provider.get_cluster_config("i-12345") - - def test_get_cluster_config_exception(self, authenticated_provider): - """Test cluster config with exception.""" - authenticated_provider.session.get.side_effect = Exception("Network error") - - # This used to return cluster_host "placeholder.lambdalabs.com", - # which clustrix then tried to SSH into (see #119). - with pytest.raises(RuntimeError, match="Could not reach Lambda Cloud"): - authenticated_provider.get_cluster_config("i-12345") - - def test_estimate_cost_default(self, provider): - """Test cost estimation with default values.""" - result = provider.estimate_cost() - - assert "gpu_instance" in result - assert "total" in result - assert result["gpu_instance"] == 0.75 # gpu_1x_a10 default price - assert result["total"] == 0.75 - - def test_estimate_cost_custom(self, provider): - """Test cost estimation with custom values.""" - result = provider.estimate_cost(instance_type="gpu_1x_h100", hours=5) - - assert result["gpu_instance"] == 1.99 * 5 - assert result["total"] == 1.99 * 5 - - def test_estimate_cost_unknown_instance(self, provider): - """Test cost estimation with unknown instance type.""" - result = provider.estimate_cost(instance_type="unknown_type", hours=2) - - assert result["gpu_instance"] == 1.0 * 2 # Default price - assert result["total"] == 1.0 * 2 - - def test_get_available_instance_types_not_authenticated(self, provider): - """Test instance types when not authenticated.""" - result = provider.get_available_instance_types() - - # Should return default list - assert "gpu_1x_a10" in result - assert "gpu_1x_h100" in result - assert "gpu_8x_a100" in result - - def test_get_available_instance_types_success(self, authenticated_provider): - """Test successful instance types retrieval.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "data": [ - {"name": "gpu_1x_a10"}, - {"name": "gpu_2x_a10"}, - {"name": "gpu_4x_a10"}, - {"name": "gpu_8x_a100"}, - ] - } - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_available_instance_types() - - # Should be sorted by GPU count - assert result == ["gpu_1x_a10", "gpu_2x_a10", "gpu_4x_a10", "gpu_8x_a100"] - - def test_get_available_instance_types_api_error(self, authenticated_provider): - """Test instance types with API error.""" - mock_response = Mock() - mock_response.status_code = 500 - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_available_instance_types() - - # Should return default list - assert "gpu_1x_a10" in result - assert "gpu_1x_h100" in result - - def test_get_available_instance_types_exception(self, authenticated_provider): - """Test instance types with exception.""" - authenticated_provider.session.get.side_effect = Exception("Network error") - - result = authenticated_provider.get_available_instance_types() - - # Should return default list - assert "gpu_1x_a10" in result - assert "gpu_1x_h100" in result - - def test_get_available_regions_not_authenticated(self, provider): - """Test regions when not authenticated.""" - result = provider.get_available_regions() - - assert result == ["us-east-1", "us-west-1", "us-west-2"] - - def test_get_available_regions_success(self, authenticated_provider): - """Test successful regions retrieval.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "data": [ - { - "regions_with_capacity_available": [ - {"name": "us-east-1"}, - {"name": "us-west-2"}, - "eu-central-1", # String format - ] - }, - { - "regions_with_capacity_available": [ - {"name": "us-west-1"}, - {"name": "us-east-1"}, # Duplicate - ] - }, - ] - } - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_available_regions() - - # Should be sorted and deduplicated - assert "us-east-1" in result - assert "us-west-1" in result - assert "us-west-2" in result - assert "eu-central-1" in result - assert len(set(result)) == len(result) # No duplicates - - def test_get_available_regions_no_regions(self, authenticated_provider): - """Test regions when no regions returned.""" - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = {"data": []} - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_available_regions() - - # Should return fallback - assert result == ["us-east-1", "us-west-1", "us-west-2"] - - def test_get_available_regions_api_error(self, authenticated_provider): - """Test regions with API error.""" - mock_response = Mock() - mock_response.status_code = 500 - authenticated_provider.session.get.return_value = mock_response - - result = authenticated_provider.get_available_regions() - - assert result == ["us-east-1", "us-west-1", "us-west-2"] - - def test_get_available_regions_exception(self, authenticated_provider): - """Test regions with exception.""" - authenticated_provider.session.get.side_effect = Exception("Network error") - - result = authenticated_provider.get_available_regions() - - assert result == ["us-east-1", "us-west-1", "us-west-2"] - - -class TestLambdaCloudProviderEdgeCases: - """Test edge cases and error handling.""" - - def test_instance_type_sorting_edge_cases(self): - """Test instance type sorting with edge cases.""" - provider = LambdaCloudProvider() - provider.authenticated = True - provider.session = Mock() - - # Mock response with edge case instance names - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "data": [ - {"name": "gpu_8x_a100"}, - {"name": "gpu_1x_a10"}, - {"name": "invalid_name"}, # No x in name - {"name": "gpu_ax_v100"}, # Invalid GPU count - {"name": "gpu_2x_a6000"}, - ] - } - provider.session.get.return_value = mock_response - - result = provider.get_available_instance_types() - - # Valid instances should be sorted first, invalid ones last - assert result[0] == "gpu_1x_a10" # 1 GPU - assert result[1] == "gpu_2x_a6000" # 2 GPUs - assert result[2] == "gpu_8x_a100" # 8 GPUs - # Invalid ones at the end (sorted by string comparison) - assert "invalid_name" in result - assert "gpu_ax_v100" in result - - def test_instance_type_sorting_no_parts(self): - """Test instance type sorting with malformed names.""" - provider = LambdaCloudProvider() - provider.authenticated = True - provider.session = Mock() - - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "data": [ - {"name": "short"}, - {"name": "gpu_1x_a10"}, - ] - } - provider.session.get.return_value = mock_response - - result = provider.get_available_instance_types() - - assert "gpu_1x_a10" in result - assert "short" in result - - def test_get_cluster_config_no_ip(self): - """Test cluster config when no IP is returned.""" - provider = LambdaCloudProvider() - provider.authenticated = True - provider.session = Mock() - - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - # No ip field - "instance_type": {"name": "gpu_1x_a10"}, - "region": {"name": "us-east-1"}, - } - provider.session.get.return_value = mock_response - - # An instance with no IP has no host to connect to. - with pytest.raises(RuntimeError, match="no\\s+IP address yet"): - provider.get_cluster_config("i-12345") - - def test_get_cluster_status_missing_fields(self): - """Test cluster status with missing fields.""" - provider = LambdaCloudProvider() - provider.authenticated = True - provider.session = Mock() - - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - # Missing status, instance_type, region fields - } - provider.session.get.return_value = mock_response - - result = provider.get_cluster_status("i-12345") - - assert result["status"] == "unknown" - assert result["instance_type"] == "unknown" - assert result["region"] == "unknown" - - def test_list_clusters_missing_fields(self): - """Test cluster listing with missing fields.""" - provider = LambdaCloudProvider() - provider.authenticated = True - provider.session = Mock() - - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "data": [ - { - # Missing most fields, only has id - "id": "i-12345" - } - ] - } - provider.session.get.return_value = mock_response - - result = provider.list_clusters() - - assert len(result) == 1 - assert result[0]["name"] == "i-12345" - assert result[0]["instance_id"] == "i-12345" - assert result[0]["type"] == "gpu" - assert result[0]["status"] == "unknown" - assert result[0]["instance_type"] == "unknown" - assert result[0]["region"] == "unknown" - - def test_regions_empty_data_structure(self): - """Test regions parsing with empty data structures.""" - provider = LambdaCloudProvider() - provider.authenticated = True - provider.session = Mock() - - mock_response = Mock() - mock_response.status_code = 200 - mock_response.json.return_value = { - "data": [ - {"regions_with_capacity_available": []}, # Empty regions - { - # No regions_with_capacity_available field - }, - ] - } - provider.session.get.return_value = mock_response - - result = provider.get_available_regions() - - # Should return fallback - assert result == ["us-east-1", "us-west-1", "us-west-2"] diff --git a/tests/test_cost_monitoring.py b/tests/test_cost_monitoring.py deleted file mode 100644 index 00aab208..00000000 --- a/tests/test_cost_monitoring.py +++ /dev/null @@ -1,494 +0,0 @@ -""" -Unit tests for cost monitoring functionality. -""" - -import pytest -import unittest.mock as mock -from datetime import datetime - -from clustrix.cost_monitoring import ( - ResourceUsage, - CostEstimate, - CostReport, - cost_tracking_decorator, - get_cost_monitor, - start_cost_monitoring, - generate_cost_report, - get_pricing_info, -) -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor - - -class TestResourceUsage: - """Test ResourceUsage dataclass.""" - - def test_resource_usage_creation(self): - """Test creating ResourceUsage object.""" - usage = ResourceUsage( - cpu_percent=75.5, - memory_used_mb=8192, - memory_total_mb=16384, - memory_percent=50.0, - ) - - assert usage.cpu_percent == 75.5 - assert usage.memory_used_mb == 8192 - assert usage.memory_total_mb == 16384 - assert usage.memory_percent == 50.0 - assert usage.gpu_stats is None - - def test_resource_usage_with_gpu(self): - """Test ResourceUsage with GPU stats.""" - gpu_stats = [ - { - "gpu_id": 0, - "utilization_percent": 85, - "memory_used_mb": 15000, - "memory_total_mb": 16000, - } - ] - - usage = ResourceUsage( - cpu_percent=50.0, - memory_used_mb=4096, - memory_total_mb=8192, - memory_percent=50.0, - gpu_stats=gpu_stats, - ) - - assert len(usage.gpu_stats) == 1 - assert usage.gpu_stats[0]["utilization_percent"] == 85 - - -class TestCostEstimate: - """Test CostEstimate dataclass.""" - - def test_cost_estimate_creation(self): - """Test creating CostEstimate object.""" - estimate = CostEstimate( - instance_type="a100_40gb", - hourly_rate=1.10, - hours_used=2.5, - estimated_cost=2.75, - ) - - assert estimate.instance_type == "a100_40gb" - assert estimate.hourly_rate == 1.10 - assert estimate.hours_used == 2.5 - assert estimate.estimated_cost == 2.75 - assert estimate.currency == "USD" - - -class TestLambdaCostMonitor: - """Test Lambda Cloud cost monitoring.""" - - def setup_method(self): - """Set up test fixtures.""" - self.monitor = LambdaCostMonitor() - - def test_initialization(self): - """Test monitor initialization.""" - assert self.monitor.provider_name == "Lambda Cloud" - assert "a100_40gb" in self.monitor.pricing - assert self.monitor.pricing["a100_40gb"] == 1.10 - - def test_estimate_cost(self): - """Test cost estimation.""" - estimate = self.monitor.estimate_cost("a100_40gb", 2.0) - - assert estimate.instance_type == "a100_40gb" - assert estimate.hourly_rate == 1.10 - assert estimate.hours_used == 2.0 - assert estimate.estimated_cost == 2.20 - - def test_estimate_cost_unknown_instance(self): - """Test cost estimation for unknown instance type.""" - estimate = self.monitor.estimate_cost("unknown_instance", 1.0) - - assert estimate.instance_type == "unknown_instance" - assert estimate.hourly_rate == 1.00 # default rate - assert estimate.estimated_cost == 1.00 - - def test_get_pricing_info(self): - """Test getting pricing information.""" - pricing = self.monitor.get_pricing_info() - - assert isinstance(pricing, dict) - assert "a100_40gb" in pricing - assert pricing["a100_40gb"] == 1.10 - - @mock.patch("clustrix.cost_monitoring.BaseCostMonitor.get_cpu_memory_usage") - @mock.patch("clustrix.cost_monitoring.BaseCostMonitor.get_gpu_utilization") - def test_get_resource_usage(self, mock_gpu, mock_cpu): - """Test getting resource usage.""" - # Mock CPU/memory usage - mock_cpu.return_value = (75.0, 8192, 16384, 50.0) - - # Mock GPU usage - mock_gpu.return_value = [ - { - "gpu_id": 0, - "utilization_percent": 85, - "memory_used_mb": 15000, - "memory_total_mb": 16000, - } - ] - - usage = self.monitor.get_resource_usage() - - assert usage.cpu_percent == 75.0 - assert usage.memory_used_mb == 8192 - assert len(usage.gpu_stats) == 1 - assert usage.gpu_stats[0]["utilization_percent"] == 85 - - def test_get_instance_recommendations(self): - """Test getting instance recommendations.""" - # Test with low GPU utilization - usage = ResourceUsage( - cpu_percent=50.0, - memory_used_mb=4096, - memory_total_mb=8192, - memory_percent=50.0, - gpu_stats=[ - { - "gpu_id": 0, - "utilization_percent": 25, - "memory_utilization_percent": 20, - } - ], - ) - - recommendations = self.monitor.get_instance_recommendations(usage) - - assert len(recommendations) > 0 - assert any("Low GPU utilization" in rec for rec in recommendations) - - def test_estimate_monthly_cost(self): - """Test monthly cost estimation.""" - monthly_cost = self.monitor.estimate_monthly_cost("a100_40gb", 8.0) - - assert monthly_cost["instance_type"] == "a100_40gb" - assert monthly_cost["hourly_rate"] == 1.10 - assert monthly_cost["daily_cost_8h"] == 8.8 # 1.10 * 8 - assert monthly_cost["weekly_cost_40h"] == 44.0 # 1.10 * 40 - - -class TestAWSCostMonitor: - """Test AWS cost monitoring.""" - - def setup_method(self): - """Set up test fixtures.""" - self.monitor = AWSCostMonitor() - - def test_initialization(self): - """Test monitor initialization.""" - assert self.monitor.provider_name == "AWS" - assert self.monitor.region == "us-east-1" - assert "p3.2xlarge" in self.monitor.ec2_pricing - - def test_estimate_cost_on_demand(self): - """Test on-demand cost estimation.""" - estimate = self.monitor.estimate_cost("p3.2xlarge", 2.0, use_spot=False) - - assert "p3.2xlarge (On-Demand)" in estimate.instance_type - assert estimate.hourly_rate == 3.06 - assert estimate.estimated_cost == 6.12 - - def test_estimate_cost_spot(self): - """Test spot instance cost estimation.""" - estimate = self.monitor.estimate_cost("p3.2xlarge", 2.0, use_spot=True) - - assert "p3.2xlarge (Spot)" in estimate.instance_type - assert estimate.hourly_rate < 3.06 # Should be discounted - expected_rate = 3.06 * 0.3 # p3 spot discount - assert abs(estimate.hourly_rate - expected_rate) < 0.01 - - def test_get_spot_pricing_info(self): - """Test getting spot pricing information.""" - spot_pricing = self.monitor.get_spot_pricing_info() - - assert isinstance(spot_pricing, dict) - assert "p3.2xlarge" in spot_pricing - assert spot_pricing["p3.2xlarge"] < self.monitor.ec2_pricing["p3.2xlarge"] - - def test_estimate_batch_cost(self): - """Test AWS Batch cost estimation.""" - batch_cost = self.monitor.estimate_batch_cost( - job_queue="test-queue", - compute_environment="test-env", - estimated_jobs=10, - avg_job_duration_hours=0.5, - ) - - assert batch_cost["estimated_jobs"] == 10 - assert batch_cost["avg_job_duration_hours"] == 0.5 - assert batch_cost["total_compute_hours"] == 5.0 - assert "recommendations" in batch_cost - - def test_get_region_pricing_comparison(self): - """Test regional pricing comparison.""" - regional_pricing = self.monitor.get_region_pricing_comparison("p3.2xlarge") - - assert isinstance(regional_pricing, dict) - assert "us-east-1" in regional_pricing - assert "eu-west-1" in regional_pricing - assert ( - regional_pricing["eu-west-1"]["on_demand_hourly"] - > regional_pricing["us-east-1"]["on_demand_hourly"] - ) - - -class TestAzureCostMonitor: - """Test Azure cost monitoring.""" - - def setup_method(self): - """Set up test fixtures.""" - self.monitor = AzureCostMonitor() - - def test_initialization(self): - """Test monitor initialization.""" - assert self.monitor.provider_name == "Azure" - assert self.monitor.region == "eastus" - assert "Standard_NC6s_v3" in self.monitor.vm_pricing - - def test_estimate_cost_pay_as_you_go(self): - """Test pay-as-you-go cost estimation.""" - estimate = self.monitor.estimate_cost("Standard_NC6s_v3", 2.0, use_spot=False) - - assert "Standard_NC6s_v3 (Pay-as-you-go)" in estimate.instance_type - assert estimate.hourly_rate == 3.06 - assert estimate.estimated_cost == 6.12 - - def test_estimate_cost_spot(self): - """Test spot VM cost estimation.""" - estimate = self.monitor.estimate_cost("Standard_NC6s_v3", 2.0, use_spot=True) - - assert "Standard_NC6s_v3 (Spot)" in estimate.instance_type - assert estimate.hourly_rate < 3.06 # Should be discounted - - def test_estimate_batch_cost(self): - """Test Azure Batch cost estimation.""" - batch_cost = self.monitor.estimate_batch_cost( - pool_name="test-pool", - vm_size="Standard_D4s_v3", - target_nodes=5, - estimated_duration_hours=2.0, - ) - - assert batch_cost["target_nodes"] == 5 - assert batch_cost["estimated_duration_hours"] == 2.0 - assert batch_cost["total_compute_hours"] == 10.0 - assert "recommendations" in batch_cost - - -class TestGCPCostMonitor: - """Test GCP cost monitoring.""" - - def setup_method(self): - """Set up test fixtures.""" - self.monitor = GCPCostMonitor(use_pricing_api=False) - - def test_initialization(self): - """Test monitor initialization.""" - assert self.monitor.provider_name == "Google Cloud Platform" - assert self.monitor.region == "us-central1" - assert "a2-highgpu-1g" in self.monitor.compute_pricing - - def test_estimate_cost_on_demand(self): - """Test on-demand cost estimation.""" - estimate = self.monitor.estimate_cost( - "a2-highgpu-1g", 2.0, use_preemptible=False - ) - - assert "a2-highgpu-1g (On-Demand)" in estimate.instance_type - assert estimate.hourly_rate == 3.673 - assert estimate.estimated_cost == 7.346 - - def test_estimate_cost_preemptible(self): - """Test preemptible instance cost estimation.""" - estimate = self.monitor.estimate_cost( - "a2-highgpu-1g", 2.0, use_preemptible=True - ) - - assert "a2-highgpu-1g (Preemptible)" in estimate.instance_type - assert estimate.hourly_rate < 3.673 # Should be discounted - expected_rate = 3.673 * 0.2 # Preemptible discount - assert abs(estimate.hourly_rate - expected_rate) < 0.01 - - def test_estimate_cost_with_sustained_use_discount(self): - """Test sustained use discount calculation.""" - estimate = self.monitor.estimate_cost( - "n2-standard-4", 2.0, sustained_use_percent=80 - ) - - # Should have sustained use discount applied - base_rate = self.monitor.compute_pricing["n2-standard-4"] - expected_rate = base_rate * 0.7 # 30% discount for 75-100% usage - assert abs(estimate.hourly_rate - expected_rate) < 0.01 - - def test_estimate_sustained_use_discount(self): - """Test sustained use discount calculation.""" - discount_info = self.monitor.estimate_sustained_use_discount( - 600 - ) # 600 hours per month - - assert discount_info["usage_percentage"] > 75 - assert discount_info["discount_percentage"] == 30 - assert discount_info["discount_tier"] == "75-100%" - - def test_get_preemptible_pricing_info(self): - """Test getting preemptible pricing.""" - preemptible_pricing = self.monitor.get_preemptible_pricing_info() - - assert isinstance(preemptible_pricing, dict) - assert "a2-highgpu-1g" in preemptible_pricing - assert ( - preemptible_pricing["a2-highgpu-1g"] - < self.monitor.compute_pricing["a2-highgpu-1g"] - ) - - -class TestCostTrackingDecorator: - """Test cost tracking decorator functionality.""" - - @mock.patch("clustrix.cost_monitoring.get_cost_monitor") - def test_cost_tracking_decorator_success(self, mock_get_monitor): - """Test cost tracking decorator with successful function execution.""" - # Mock the monitor - mock_monitor = mock.Mock() - mock_monitor.start_monitoring.return_value = None - mock_monitor.stop_monitoring.return_value = CostReport( - timestamp=datetime.now(), - duration_seconds=1.5, - resource_usage=ResourceUsage(50.0, 4096, 8192, 50.0), - cost_estimate=CostEstimate("test", 1.0, 1.5, 1.5), - provider="test", - ) - mock_get_monitor.return_value = mock_monitor - - @cost_tracking_decorator("lambda", "a100_40gb") - def test_function(): - return "success" - - result = test_function() - - assert result["success"] is True - assert result["result"] == "success" - assert result["provider"] == "lambda" - assert result["instance_type"] == "a100_40gb" - assert result["cost_report"] is not None - - mock_monitor.start_monitoring.assert_called_once() - mock_monitor.stop_monitoring.assert_called_once() - - @mock.patch("clustrix.cost_monitoring.get_cost_monitor") - def test_cost_tracking_decorator_failure(self, mock_get_monitor): - """Test cost tracking decorator with function failure.""" - # Mock the monitor - mock_monitor = mock.Mock() - mock_monitor.start_monitoring.return_value = None - mock_monitor.stop_monitoring.return_value = CostReport( - timestamp=datetime.now(), - duration_seconds=0.5, - resource_usage=ResourceUsage(50.0, 4096, 8192, 50.0), - cost_estimate=CostEstimate("test", 1.0, 0.5, 0.5), - provider="test", - ) - mock_get_monitor.return_value = mock_monitor - - @cost_tracking_decorator("lambda", "a100_40gb") - def failing_function(): - raise ValueError("Test error") - - result = failing_function() - - assert result["success"] is False - assert result["result"] is None - assert "Test error" in result["error"] - assert result["cost_report"] is not None - - @mock.patch("clustrix.cost_monitoring.get_cost_monitor") - def test_cost_tracking_decorator_no_monitor(self, mock_get_monitor): - """Test cost tracking decorator when monitor is not available.""" - mock_get_monitor.return_value = None - - @cost_tracking_decorator("unsupported", "instance") - def test_function(): - return "success" - - result = test_function() - - # Should execute function normally without cost tracking - assert result == "success" - - -class TestCostMonitoringUtilities: - """Test utility functions.""" - - def test_get_cost_monitor_lambda(self): - """Test getting Lambda cost monitor.""" - monitor = get_cost_monitor("lambda") - assert isinstance(monitor, LambdaCostMonitor) - - def test_get_cost_monitor_aws(self): - """Test getting AWS cost monitor.""" - monitor = get_cost_monitor("aws") - assert isinstance(monitor, AWSCostMonitor) - - def test_get_cost_monitor_azure(self): - """Test getting Azure cost monitor.""" - monitor = get_cost_monitor("azure") - assert isinstance(monitor, AzureCostMonitor) - - def test_get_cost_monitor_gcp(self): - """Test getting GCP cost monitor.""" - monitor = get_cost_monitor("gcp") - assert isinstance(monitor, GCPCostMonitor) - - def test_get_cost_monitor_unsupported(self): - """Test getting unsupported cost monitor.""" - monitor = get_cost_monitor("unsupported") - assert monitor is None - - def test_start_cost_monitoring(self): - """Test starting cost monitoring.""" - monitor = start_cost_monitoring("lambda") - assert isinstance(monitor, LambdaCostMonitor) - assert monitor.start_time is not None - - @mock.patch("clustrix.cost_monitoring.get_cost_monitor") - def test_generate_cost_report(self, mock_get_monitor): - """Test generating cost report.""" - # Mock the monitor - mock_monitor = mock.Mock() - mock_monitor.get_resource_usage.return_value = ResourceUsage( - 50.0, 4096, 8192, 50.0 - ) - mock_monitor.estimate_cost.return_value = CostEstimate("test", 1.0, 1.0, 1.0) - mock_monitor.get_cost_optimization_recommendations.return_value = [ - "Test recommendation" - ] - mock_get_monitor.return_value = mock_monitor - - report = generate_cost_report("lambda", "a100_40gb") - - assert report is not None - assert report["provider"] == "lambda" - assert "resource_usage" in report - assert "cost_estimate" in report - assert "recommendations" in report - - def test_get_pricing_info(self): - """Test getting pricing information.""" - pricing = get_pricing_info("lambda") - - assert isinstance(pricing, dict) - assert "a100_40gb" in pricing - assert pricing["a100_40gb"] == 1.10 - - -if __name__ == "__main__": - pytest.main([__file__]) diff --git a/tests/test_gcp_cost_provider.py b/tests/test_gcp_cost_provider.py deleted file mode 100644 index 7d8c0ee4..00000000 --- a/tests/test_gcp_cost_provider.py +++ /dev/null @@ -1,333 +0,0 @@ -"""Comprehensive tests for GCP cost provider.""" - -import logging -from unittest.mock import Mock, patch, MagicMock -import pytest - -from clustrix.cost_providers.gcp import GCPCostMonitor -from clustrix.cost_monitoring import ResourceUsage, CostEstimate - - -class TestGCPCostMonitor: - """Test GCPCostMonitor class.""" - - def test_init_with_pricing_api(self): - """Test initialization with pricing API enabled.""" - monitor = GCPCostMonitor(region="us-central1", use_pricing_api=True) - - assert monitor.region == "us-central1" - assert monitor.use_pricing_api is True - assert monitor.pricing_client is not None - assert monitor.provider_name == "Google Cloud Platform" - assert isinstance(monitor.compute_pricing, dict) - assert "n2-standard-2" in monitor.compute_pricing - - def test_init_without_pricing_api(self): - """Test initialization with pricing API disabled.""" - monitor = GCPCostMonitor(region="us-east1", use_pricing_api=False) - - assert monitor.region == "us-east1" - assert monitor.use_pricing_api is False - assert monitor.pricing_client is None - - def test_init_default_values(self): - """Test initialization with default values.""" - monitor = GCPCostMonitor() - - assert monitor.region == "us-central1" - assert monitor.use_pricing_api is True - - @patch("clustrix.cost_monitoring.BaseCostMonitor.get_cpu_memory_usage") - @patch("clustrix.cost_monitoring.BaseCostMonitor.get_gpu_utilization") - def test_get_resource_usage(self, mock_gpu, mock_cpu_mem): - """Test getting current resource usage.""" - monitor = GCPCostMonitor() - - # Mock CPU and memory usage - mock_cpu_mem.return_value = (65.5, 3072, 8192, 37.5) - - # Mock GPU usage - mock_gpu.return_value = [{"utilization_percent": 75.0, "memory_used_mb": 2048}] - - usage = monitor.get_resource_usage() - - assert isinstance(usage, ResourceUsage) - assert usage.cpu_percent == 65.5 - assert usage.memory_used_mb == 3072 - assert usage.memory_total_mb == 8192 - assert usage.memory_percent == 37.5 - assert usage.gpu_stats == [ - {"utilization_percent": 75.0, "memory_used_mb": 2048} - ] - - def test_estimate_cost_with_api_success(self): - """Test cost estimation with successful API call.""" - monitor = GCPCostMonitor(use_pricing_api=True) - - # Mock pricing client - mock_pricing_client = Mock() - mock_pricing_client.get_instance_pricing.return_value = 0.194 - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost("n2-standard-4", 8.0) - - assert isinstance(cost_estimate, CostEstimate) - assert cost_estimate.hourly_rate == 0.194 - assert cost_estimate.estimated_cost == 1.552 # 0.194 * 8 - assert cost_estimate.instance_type == "n2-standard-4 (On-Demand)" - assert cost_estimate.hours_used == 8.0 - assert cost_estimate.pricing_source == "api" - - mock_pricing_client.get_instance_pricing.assert_called_once_with( - instance_type="n2-standard-4", region="us-central1" - ) - - def test_estimate_cost_with_api_failure(self): - """Test cost estimation with API failure fallback.""" - monitor = GCPCostMonitor(use_pricing_api=True) - - # Mock pricing client that fails - mock_pricing_client = Mock() - mock_pricing_client.get_instance_pricing.side_effect = Exception("API error") - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost("n2-standard-4", 5.0) - - assert cost_estimate.hourly_rate == 0.194 # fallback to hardcoded - assert cost_estimate.estimated_cost == 0.97 # 0.194 * 5 - assert cost_estimate.pricing_source == "hardcoded" - - def test_estimate_cost_preemptible_pricing(self): - """Test cost estimation with preemptible pricing.""" - monitor = GCPCostMonitor(use_pricing_api=True) - - # Mock pricing client - mock_pricing_client = Mock() - mock_pricing_client.get_preemptible_pricing.return_value = ( - 0.058 # ~70% discount - ) - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost( - "n2-standard-4", 10.0, use_preemptible=True - ) - - assert cost_estimate.hourly_rate == 0.058 - assert abs(cost_estimate.estimated_cost - 0.58) < 0.001 - assert cost_estimate.pricing_source == "api" - - mock_pricing_client.get_preemptible_pricing.assert_called_once_with( - "n2-standard-4", "us-central1" - ) - - def test_estimate_cost_preemptible_pricing_failure(self): - """Test preemptible pricing with API failure.""" - monitor = GCPCostMonitor(use_pricing_api=True) - - # Mock pricing client that fails - mock_pricing_client = Mock() - mock_pricing_client.get_preemptible_pricing.side_effect = Exception( - "Preemptible API error" - ) - monitor.pricing_client = mock_pricing_client - - cost_estimate = monitor.estimate_cost( - "n2-standard-4", 5.0, use_preemptible=True - ) - - # Should fall back to preemptible calculation - expected_preemptible_rate = 0.194 * 0.2 # 80% discount - assert abs(cost_estimate.hourly_rate - expected_preemptible_rate) < 0.001 - - def test_estimate_cost_unknown_instance(self): - """Test cost estimation for unknown instance type.""" - monitor = GCPCostMonitor(use_pricing_api=False) - - cost_estimate = monitor.estimate_cost("unknown-instance-type", 6.0) - - assert cost_estimate.hourly_rate == 0.10 # default price - assert abs(cost_estimate.estimated_cost - 0.6) < 0.001 - - def test_estimate_cost_without_pricing_client(self): - """Test cost estimation without pricing client.""" - monitor = GCPCostMonitor(use_pricing_api=False) - - cost_estimate = monitor.estimate_cost("n2-standard-2", 4.0) - - assert cost_estimate.hourly_rate == 0.097 - assert cost_estimate.estimated_cost == 0.388 - assert cost_estimate.pricing_source == "hardcoded" - - def test_estimate_cost_with_sustained_use_discount(self): - """Test cost estimation with sustained use discount.""" - monitor = GCPCostMonitor(use_pricing_api=True) - - # Mock pricing client - mock_pricing_client = Mock() - mock_pricing_client.get_instance_pricing.return_value = 0.194 - monitor.pricing_client = mock_pricing_client - - # 100% usage should get 30% discount (1 - 0.3 = 0.7 multiplier) - cost_estimate = monitor.estimate_cost( - "n2-standard-4", 720.0, sustained_use_percent=100 - ) - - # Hourly rate should be discounted: 0.194 * 0.7 = 0.1358 - expected_hourly_rate = 0.194 * 0.7 - assert abs(cost_estimate.hourly_rate - expected_hourly_rate) < 0.001 - - # Total cost: discounted_rate * hours - expected_cost = expected_hourly_rate * 720.0 - assert abs(cost_estimate.estimated_cost - expected_cost) < 0.1 - - def test_get_pricing_info(self): - """Test getting pricing information.""" - monitor = GCPCostMonitor() - - pricing_info = monitor.get_pricing_info() - - assert isinstance(pricing_info, dict) - assert "n2-standard-2" in pricing_info - assert "n2-standard-4" in pricing_info - assert pricing_info["n2-standard-2"] == 0.097 - assert pricing_info["n2-standard-4"] == 0.194 - - def test_get_preemptible_pricing_info(self): - """Test getting preemptible pricing information.""" - monitor = GCPCostMonitor() - - preemptible_pricing = monitor.get_preemptible_pricing_info() - - assert isinstance(preemptible_pricing, dict) - assert "n2-standard-2" in preemptible_pricing - assert "n2-standard-4" in preemptible_pricing - - # Check that preemptible pricing is discounted (80% off) - on_demand_price = monitor.compute_pricing["n2-standard-2"] - preemptible_price = preemptible_pricing["n2-standard-2"] - expected_preemptible_price = on_demand_price * 0.2 - - assert abs(preemptible_price - expected_preemptible_price) < 0.001 - - def test_get_cost_optimization_recommendations_basic(self): - """Test basic cost optimization recommendations.""" - monitor = GCPCostMonitor() - - resource_usage = ResourceUsage( - cpu_percent=55.0, - memory_used_mb=2500, - memory_total_mb=8192, - memory_percent=30.5, - gpu_stats=None, - ) - - cost_estimate = CostEstimate( - hourly_rate=0.194, - estimated_cost=1.552, - hours_used=8.0, - instance_type="n2-standard-4", - pricing_source="api", - ) - - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - assert isinstance(recommendations, list) - assert len(recommendations) > 0 - - # Check for GCP-specific recommendations - gcp_recommendations = [ - r - for r in recommendations - if any( - keyword in r - for keyword in ["GCP", "Google", "Preemptible", "Committed"] - ) - ] - assert len(gcp_recommendations) > 0 - - def test_get_cost_optimization_recommendations_gpu_low_usage(self): - """Test recommendations for GPU instances with low utilization.""" - monitor = GCPCostMonitor() - - resource_usage = ResourceUsage( - cpu_percent=70.0, - memory_used_mb=6000, - memory_total_mb=16384, - memory_percent=36.6, - gpu_stats=[ - {"utilization_percent": 20.0, "memory_used_mb": 1500}, - {"utilization_percent": 35.0, "memory_used_mb": 2000}, - ], - ) - - cost_estimate = CostEstimate( - hourly_rate=2.48, - estimated_cost=19.84, - hours_used=8.0, - instance_type="n1-standard-4-k80", - pricing_source="api", - ) - - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # Should include GPU utilization warning - gpu_recommendations = [r for r in recommendations if "GPU utilization" in r] - assert len(gpu_recommendations) > 0 - - def test_get_cost_optimization_recommendations_high_memory_usage(self): - """Test recommendations for high memory usage.""" - monitor = GCPCostMonitor() - - resource_usage = ResourceUsage( - cpu_percent=45.0, - memory_used_mb=7500, - memory_total_mb=8192, - memory_percent=91.6, - gpu_stats=None, - ) - - cost_estimate = CostEstimate( - hourly_rate=0.194, - estimated_cost=1.552, - hours_used=8.0, - instance_type="n2-standard-4", - pricing_source="api", - ) - - recommendations = monitor.get_cost_optimization_recommendations( - resource_usage, cost_estimate - ) - - # Should include memory optimization recommendation - memory_recommendations = [ - r - for r in recommendations - if any(keyword in r.lower() for keyword in ["memory", "highmem"]) - ] - assert len(memory_recommendations) > 0 - - def test_preemptible_discounts_structure(self): - """Test preemptible discount structure.""" - monitor = GCPCostMonitor() - - assert hasattr(monitor, "preemptible_discount") - assert isinstance(monitor.preemptible_discount, (int, float)) - assert 0 < monitor.preemptible_discount < 1 # Should be a discount factor - - def test_compute_pricing_structure(self): - """Test compute pricing structure.""" - monitor = GCPCostMonitor() - - assert isinstance(monitor.compute_pricing, dict) - assert len(monitor.compute_pricing) > 10 # Should have many instance types - - # Check some expected instance types - expected_types = ["n2-standard-2", "n2-standard-4", "c2-standard-4", "default"] - for instance_type in expected_types: - assert instance_type in monitor.compute_pricing - assert isinstance(monitor.compute_pricing[instance_type], (int, float)) - assert monitor.compute_pricing[instance_type] > 0 diff --git a/tests/test_gcp_pricing_simple.py b/tests/test_gcp_pricing_simple.py deleted file mode 100644 index 246bddf7..00000000 --- a/tests/test_gcp_pricing_simple.py +++ /dev/null @@ -1,292 +0,0 @@ -"""Simplified comprehensive tests for GCP pricing client.""" - -import logging -from unittest.mock import Mock, patch, MagicMock -import pytest - -from clustrix.pricing_clients.gcp_pricing import GCPPricingClient - - -class TestGCPPricingClient: - """Test GCPPricingClient class.""" - - def test_init(self): - """Test initialization.""" - client = GCPPricingClient() - assert client.cache.ttl.total_seconds() == 24 * 3600 # 24 hours in seconds - assert client.compute_service_id == "6F81-5844-456A" - assert client._hardcoded_pricing_date == "2025-01-01" - assert isinstance(client._hardcoded_pricing, dict) - assert "n1-standard-1" in client._hardcoded_pricing - assert client.region_mapping["us-central1"] == "us-central1" - - client_custom = GCPPricingClient(cache_ttl_hours=12) - assert ( - client_custom.cache.ttl.total_seconds() == 12 * 3600 - ) # 12 hours in seconds - - def test_get_instance_pricing_cached(self): - """Test instance pricing retrieval from cache.""" - client = GCPPricingClient() - - # Mock cache hit - cached_data = {"price": 0.15} - client.cache.get = Mock(return_value=cached_data) - - result = client.get_instance_pricing("n1-standard-4", "us-central1") - - assert result == 0.15 - client.cache.get.assert_called_once_with("gcp_us-central1_n1-standard-4") - - @patch.object(GCPPricingClient, "_fetch_pricing_from_api") - def test_get_instance_pricing_api_success(self, mock_fetch): - """Test successful instance pricing retrieval from API.""" - client = GCPPricingClient() - - # Mock cache miss - client.cache.get = Mock(return_value=None) - client.cache.set = Mock() - - # Mock successful API response - api_data = { - "price": 0.19, - "region": "us-central1", - "instance_type": "n1-standard-4", - } - mock_fetch.return_value = api_data - - result = client.get_instance_pricing("n1-standard-4", "us-central1") - - assert result == 0.19 - client.cache.set.assert_called_once_with( - "gcp_us-central1_n1-standard-4", api_data - ) - - @patch.object(GCPPricingClient, "_fetch_pricing_from_api") - @patch.object(GCPPricingClient, "_get_fallback_price") - def test_get_instance_pricing_api_failure_fallback(self, mock_fallback, mock_fetch): - """Test instance pricing with API failure using fallback.""" - client = GCPPricingClient() - - # Mock cache miss - client.cache.get = Mock(return_value=None) - - # Mock API failure - mock_fetch.side_effect = Exception("API error") - - # Mock fallback price - mock_fallback.return_value = 0.19 - - result = client.get_instance_pricing("n1-standard-4", "us-central1") - - assert result == 0.19 - mock_fallback.assert_called_once_with("n1-standard-4") - - @patch.object(GCPPricingClient, "_fetch_pricing_from_api") - @patch.object(GCPPricingClient, "_get_fallback_price") - def test_get_instance_pricing_no_fallback_default(self, mock_fallback, mock_fetch): - """Test instance pricing with no fallback using default price.""" - client = GCPPricingClient() - - # Mock cache miss - client.cache.get = Mock(return_value=None) - - # Mock API failure - mock_fetch.side_effect = Exception("API error") - - # Mock no fallback price - mock_fallback.return_value = None - - result = client.get_instance_pricing("unknown-instance", "us-central1") - - assert result == 0.10 # default price - - @patch.object(GCPPricingClient, "is_pricing_data_outdated") - def test_get_all_pricing(self, mock_outdated): - """Test getting all pricing for a region.""" - client = GCPPricingClient() - mock_outdated.return_value = False - - result = client.get_all_pricing("us-central1") - - assert isinstance(result, dict) - assert "n1-standard-1" in result - assert "n2-standard-2" in result - assert result["n1-standard-1"] == 0.0475 - - @patch.object(GCPPricingClient, "is_pricing_data_outdated") - def test_get_all_pricing_outdated_warning(self, mock_outdated): - """Test getting all pricing with outdated data warning.""" - client = GCPPricingClient() - mock_outdated.return_value = True - - with patch("clustrix.pricing_clients.gcp_pricing.logger") as mock_logger: - result = client.get_all_pricing("us-central1") - - mock_logger.warning.assert_called_once() - assert "outdated pricing data" in mock_logger.warning.call_args[0][0] - - def test_fetch_pricing_from_api_import_error(self): - """Test _fetch_pricing_from_api with ImportError.""" - client = GCPPricingClient() - - # Mock the import to fail - with patch("builtins.__import__", side_effect=ImportError()): - result = client._fetch_pricing_from_api("n1-standard-4", "us-central1") - - assert result is None - - # Note: Google Cloud billing API tests skipped due to library availability - - @patch.object(GCPPricingClient, "_fetch_preemptible_pricing_from_api") - def test_get_preemptible_pricing_api_success(self, mock_fetch): - """Test successful preemptible pricing from API.""" - client = GCPPricingClient() - - api_data = {"price": 0.038} # 80% discount from 0.19 - mock_fetch.return_value = api_data - - result = client.get_preemptible_pricing("n1-standard-4", "us-central1") - - assert result == 0.038 - - @patch.object(GCPPricingClient, "_fetch_preemptible_pricing_from_api") - @patch.object(GCPPricingClient, "get_instance_pricing") - def test_get_preemptible_pricing_fallback(self, mock_get_pricing, mock_fetch): - """Test preemptible pricing fallback to on-demand with discount.""" - client = GCPPricingClient() - - # Mock API failure - mock_fetch.side_effect = Exception("API error") - - # Mock on-demand pricing - mock_get_pricing.return_value = 0.19 - - result = client.get_preemptible_pricing("n1-standard-4", "us-central1") - - # Should be 20% of on-demand price (80% discount) - assert abs(result - 0.038) < 0.001 - - @patch.object(GCPPricingClient, "_fetch_preemptible_pricing_from_api") - @patch.object(GCPPricingClient, "get_instance_pricing") - def test_get_preemptible_pricing_no_on_demand(self, mock_get_pricing, mock_fetch): - """Test preemptible pricing when on-demand price not available.""" - client = GCPPricingClient() - - # Mock API failure - mock_fetch.side_effect = Exception("API error") - - # Mock no on-demand pricing - mock_get_pricing.return_value = None - - result = client.get_preemptible_pricing("n1-standard-4", "us-central1") - - assert result is None - - def test_fetch_preemptible_pricing_from_api_import_error(self): - """Test _fetch_preemptible_pricing_from_api with ImportError.""" - client = GCPPricingClient() - - with patch("builtins.__import__", side_effect=ImportError()): - result = client._fetch_preemptible_pricing_from_api( - "n1-standard-4", "us-central1" - ) - - assert result is None - - def test_get_sustained_use_discount_no_discount(self): - """Test sustained use discount with low usage.""" - client = GCPPricingClient() - - # 10% of month usage - hours_used = 24 * 3 # 3 days - base_price = 0.19 - - result = client.get_sustained_use_discount(hours_used, base_price) - - assert result == 0.19 # No discount - - def test_get_sustained_use_discount_25_percent(self): - """Test sustained use discount with 25% usage.""" - client = GCPPricingClient() - - # 30% of month usage - hours_used = 24 * 9 # 9 days - base_price = 0.19 - - result = client.get_sustained_use_discount(hours_used, base_price) - - # 10% discount - expected = 0.19 * 0.9 - assert result == expected - - def test_get_sustained_use_discount_50_percent(self): - """Test sustained use discount with 50% usage.""" - client = GCPPricingClient() - - # 60% of month usage - hours_used = 24 * 18 # 18 days - base_price = 0.19 - - result = client.get_sustained_use_discount(hours_used, base_price) - - # 20% discount - expected = 0.19 * 0.8 - assert result == expected - - def test_get_sustained_use_discount_75_percent(self): - """Test sustained use discount with 75% usage.""" - client = GCPPricingClient() - - # 80% of month usage - hours_used = 24 * 24 # 24 days - base_price = 0.19 - - result = client.get_sustained_use_discount(hours_used, base_price) - - # 30% discount - expected = 0.19 * 0.7 - assert result == expected - - def test_get_custom_machine_pricing_us_central1(self): - """Test custom machine pricing for us-central1.""" - client = GCPPricingClient() - - result = client.get_custom_machine_pricing(4, 16, "us-central1") - - # 4 vCPUs * 0.033174 + 16 GB * 0.004446 = 0.132696 + 0.071136 = 0.203832 - expected = 4 * 0.033174 + 16 * 0.004446 - assert abs(result - expected) < 0.001 - - def test_get_custom_machine_pricing_europe_west1(self): - """Test custom machine pricing for europe-west1 with regional multiplier.""" - client = GCPPricingClient() - - result = client.get_custom_machine_pricing(2, 8, "europe-west1") - - # Base price with 1.1x multiplier - base_price = 2 * 0.033174 + 8 * 0.004446 - expected = base_price * 1.1 - assert abs(result - expected) < 0.001 - - def test_get_custom_machine_pricing_unknown_region(self): - """Test custom machine pricing for unknown region with default multiplier.""" - client = GCPPricingClient() - - result = client.get_custom_machine_pricing(1, 4, "unknown-region") - - # Base price with default 1.1x multiplier - base_price = 1 * 0.033174 + 4 * 0.004446 - expected = base_price * 1.1 - assert abs(result - expected) < 0.001 - - def test_get_custom_machine_pricing_asia_northeast1(self): - """Test custom machine pricing for asia-northeast1.""" - client = GCPPricingClient() - - result = client.get_custom_machine_pricing(8, 32, "asia-northeast1") - - # Base price with 1.2x multiplier - base_price = 8 * 0.033174 + 32 * 0.004446 - expected = base_price * 1.2 - assert abs(result - expected) < 0.001 diff --git a/tests/test_kubernetes_integration.py b/tests/test_kubernetes_integration.py deleted file mode 100644 index 0ea78795..00000000 --- a/tests/test_kubernetes_integration.py +++ /dev/null @@ -1,649 +0,0 @@ -"""Comprehensive tests for Kubernetes integration and cloud provider features.""" - -import base64 -import os -import subprocess -import sys -import time -from unittest.mock import Mock, patch - -import cloudpickle -import dill -import pytest - -from clustrix.config import ClusterConfig -from clustrix.executor import ClusterExecutor -from clustrix.executor_kubernetes import build_worker_program - - -def _run_real_worker(func, args=(), kwargs=None, result_key="test-key"): - """Run clustrix's actual Kubernetes worker program as a real subprocess. - - executor_kubernetes.build_worker_program() is the exact code clustrix - embeds in the container command; this executes it for real (no cluster - involved) so the pod-log text used in these tests is genuine worker - output -- a signed ``CLUSTRIX_RESULT_B64``/``CLUSTRIX_RESULT_HMAC`` - payload -- rather than a hand-written stand-in for the retired - ``CLUSTRIX_RESULT:`` format. - """ - kwargs = kwargs or {} - func_data = { - "function": cloudpickle.dumps(func), - "args": dill.dumps(args), - "kwargs": dill.dumps(kwargs), - } - func_data_b64 = base64.b64encode(cloudpickle.dumps(func_data)).decode("utf-8") - program = build_worker_program(func_data_b64) - - env = os.environ.copy() - env["CLUSTRIX_RESULT_KEY"] = result_key - - completed = subprocess.run( - [sys.executable, "-c", program], - capture_output=True, - text=True, - env=env, - ) - return completed.stdout - - -class TestKubernetesJobSubmission: - """Test comprehensive Kubernetes job submission functionality.""" - - @pytest.fixture - def k8s_config(self): - """Create a Kubernetes configuration for testing.""" - return ClusterConfig( - cluster_type="kubernetes", - k8s_namespace="test-namespace", - k8s_image="python:3.11-slim", - k8s_service_account="test-account", - k8s_pull_policy="Always", - k8s_job_ttl_seconds=7200, - k8s_backoff_limit=2, - cleanup_on_success=True, - ) - - @pytest.fixture - def mock_k8s_client(self): - """Mock Kubernetes client.""" - with patch("kubernetes.client") as mock_client: - # Mock BatchV1Api - mock_batch_api = Mock() - mock_client.BatchV1Api.return_value = mock_batch_api - - # Mock job creation response - mock_job_response = Mock() - mock_job_response.metadata.name = "test-job-123" - mock_batch_api.create_namespaced_job.return_value = mock_job_response - - # Mock job status - mock_job_status = Mock() - mock_job_status.status.succeeded = 1 - mock_job_status.status.failed = None - mock_job_status.status.active = None - mock_batch_api.read_namespaced_job.return_value = mock_job_status - - # Mock CoreV1Api for pod logs - mock_core_api = Mock() - mock_client.CoreV1Api.return_value = mock_core_api - - # Mock pod listing - mock_pod = Mock() - mock_pod.metadata.name = "test-pod-123" - mock_pod.metadata.namespace = "test-namespace" - mock_pod.status.phase = "Succeeded" - mock_pods_response = Mock() - mock_pods_response.items = [mock_pod] - mock_core_api.list_namespaced_pod.return_value = mock_pods_response - - # Mock pod logs - mock_core_api.read_namespaced_pod_log.return_value = "CLUSTRIX_RESULT:42" - - yield mock_client - - @patch("kubernetes.config.load_kube_config") - def test_kubernetes_job_submission_success( - self, mock_load_config, k8s_config, mock_k8s_client - ): - """Test successful Kubernetes job submission. - - This used to patch clustrix.executor.cloudpickle to avoid a real - cloudpickle.dumps call. The #80 module refactor moved job submission - (and its cloudpickle usage) into executor_kubernetes.py, so - clustrix.executor has no `cloudpickle` attribute to patch any more -- - the patch raised AttributeError before ever exercising real code. - cloudpickle.dumps on a small dict is cheap and safe to run for real, - so there is nothing here that needs mocking. - """ - executor = ClusterExecutor(k8s_config) - - func_data = { - "func": lambda x: x * 2, - "args": (21,), - "kwargs": {}, - "requirements": {}, - } - job_config = {"cores": 2, "memory": "4Gi"} - - job_id = executor._submit_k8s_job(func_data, job_config) - - # Verify job was submitted - assert job_id == "test-job-123" - - # Verify Kubernetes API calls - mock_k8s_client.BatchV1Api().create_namespaced_job.assert_called_once() - call_args = mock_k8s_client.BatchV1Api().create_namespaced_job.call_args - - # Check namespace - assert call_args[1]["namespace"] == "test-namespace" - - # Check job manifest - job_manifest = call_args[1]["body"] - assert job_manifest["kind"] == "Job" - assert job_manifest["metadata"]["name"].startswith("clustrix-job-") - - # Check container configuration - container = job_manifest["spec"]["template"]["spec"]["containers"][0] - assert container["name"] == "clustrix-worker" - assert container["image"] == "python:3.11-slim" - assert container["resources"]["requests"]["cpu"] == "2" - assert container["resources"]["requests"]["memory"] == "4Gi" - - def test_kubernetes_job_result_collection(self, k8s_config, mock_k8s_client): - """Test collecting results from Kubernetes job. - - The pod log used to be a bare "CLUSTRIX_RESULT:" string that - get_k8s_result ran through ast.literal_eval. get_k8s_result now - requires the signed payload the real worker writes - (CLUSTRIX_RESULT_B64 + CLUSTRIX_RESULT_HMAC) and verifies it with - decode_signed_result before touching dill.loads. This test runs the - actual worker subprocess to produce that log rather than fabricating - the new format by hand, and calls executor.k8s_manager directly: - ClusterExecutor itself only exposes _submit_k8s_job/_check_job_status - shortcuts, not a _get_k8s_result one. - """ - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(k8s_config) - - # Set up active job with the per-job signing key get_k8s_result - # requires. This lives on the KubernetesJobManager, not on - # ClusterExecutor's own (separate) active_jobs dict. - job_id = "test-job-123" - result_key = "unit-test-result-key" - executor.k8s_manager.active_jobs[job_id] = { - "status": "submitted", - "submit_time": time.time(), - "k8s_job": True, - "result_key": result_key, - } - - pod_log = _run_real_worker( - lambda x: x * 2, args=(21,), result_key=result_key - ) - mock_k8s_client.CoreV1Api().read_namespaced_pod_log.return_value = pod_log - - # Test result collection - result = executor.k8s_manager.get_k8s_result(job_id) - assert result == 42 - - def test_kubernetes_job_error_handling(self, k8s_config, mock_k8s_client): - """Test error handling in Kubernetes jobs.""" - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(k8s_config) - - # Mock failed pod logs - mock_k8s_client.CoreV1Api().read_namespaced_pod_log.return_value = ( - "CLUSTRIX_ERROR:Division by zero\n" - "CLUSTRIX_TRACEBACK:Traceback (most recent call last):\n" - ' File "", line 1, in \n' - "ZeroDivisionError: division by zero" - ) - - # get_k8s_error_log lives on the KubernetesJobManager; - # ClusterExecutor has no _get_k8s_error_log shortcut. - job_id = "failed-job-123" - error_log = executor.k8s_manager.get_k8s_error_log(job_id) - - assert "CLUSTRIX_ERROR:Division by zero" in error_log - assert "CLUSTRIX_TRACEBACK" in error_log - - def test_kubernetes_job_status_checking(self, k8s_config, mock_k8s_client): - """Test Kubernetes job status checking.""" - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(k8s_config) - - job_id = "test-job-123" - # get_job_status() dispatches on active_jobs[job_id]["manager"]; - # without it a tracked-but-unlabeled job raised KeyError instead - # of routing to the Kubernetes manager. - executor.active_jobs[job_id] = { - "status": "submitted", - "submit_time": time.time(), - "k8s_job": True, - "manager": "kubernetes", - } - - # Test completed status - status = executor._check_job_status(job_id) - assert status == "completed" - - # Test failed status - mock_k8s_client.BatchV1Api().read_namespaced_job.return_value.status.succeeded = ( - None - ) - mock_k8s_client.BatchV1Api().read_namespaced_job.return_value.status.failed = ( - 1 - ) - - status = executor._check_job_status(job_id) - assert status == "failed" - - def test_kubernetes_job_cleanup(self, k8s_config, mock_k8s_client): - """Test Kubernetes job cleanup.""" - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(k8s_config) - - # cleanup_k8s_job lives on the KubernetesJobManager; - # ClusterExecutor has no _cleanup_k8s_job shortcut. - job_id = "cleanup-job-123" - executor.k8s_manager.cleanup_k8s_job(job_id) - - # Verify deletion was called - mock_k8s_client.BatchV1Api().delete_namespaced_job.assert_called_once_with( - name=job_id, - namespace="test-namespace", - body=mock_k8s_client.V1DeleteOptions(propagation_policy="Foreground"), - ) - - -class TestCloudProviderIntegration: - """Test cloud provider auto-configuration integration.""" - - @pytest.fixture - def aws_config(self): - """Create AWS configuration for testing.""" - return ClusterConfig( - cluster_type="kubernetes", - cloud_provider="aws", - cloud_auto_configure=True, - cloud_region="us-west-2", - eks_cluster_name="test-cluster", - aws_profile="test-profile", - ) - - @pytest.fixture - def azure_config(self): - """Create Azure configuration for testing.""" - return ClusterConfig( - cluster_type="kubernetes", - cloud_provider="azure", - cloud_auto_configure=True, - cloud_region="westus2", - aks_cluster_name="test-cluster", - azure_resource_group="test-rg", - azure_subscription_id="test-subscription", - ) - - @pytest.fixture - def gcp_config(self): - """Create GCP configuration for testing.""" - return ClusterConfig( - cluster_type="kubernetes", - cloud_provider="gcp", - cloud_auto_configure=True, - cloud_region="us-central1", - gke_cluster_name="test-cluster", - gcp_project_id="test-project", - gcp_zone="us-central1-a", - ) - - def test_aws_auto_configuration_success(self, aws_config): - """Test successful AWS EKS auto-configuration.""" - with patch("subprocess.run") as mock_run: - # Mock successful aws eks update-kubeconfig - mock_run.return_value.returncode = 0 - mock_run.return_value.stdout = "Updated context" - - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(aws_config) - executor._setup_kubernetes() - - # Verify aws command was called - mock_run.assert_called() - # Find the AWS call in all the calls made - aws_call_found = False - for call in mock_run.call_args_list: - call_args = call[0][0] - if "aws" in call_args: - aws_call_found = True - assert "eks" in call_args - assert "update-kubeconfig" in call_args - assert "test-cluster" in call_args - assert "us-west-2" in call_args - break - assert aws_call_found, "AWS command was not called" - - def test_azure_auto_configuration_success(self, azure_config): - """Test successful Azure AKS auto-configuration.""" - with patch("subprocess.run") as mock_run: - # Mock successful az aks get-credentials - mock_run.return_value.returncode = 0 - mock_run.return_value.stdout = "Merged credentials" - - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(azure_config) - executor._setup_kubernetes() - - # Verify az command was called - mock_run.assert_called() - # Find the Azure call in all the calls made - az_call_found = False - for call in mock_run.call_args_list: - call_args = call[0][0] - if "az" in call_args: - az_call_found = True - assert "aks" in call_args - assert "get-credentials" in call_args - assert "test-cluster" in call_args - assert "test-rg" in call_args - break - assert az_call_found, "Azure command was not called" - - def test_gcp_auto_configuration_success(self, gcp_config): - """Test successful GCP GKE auto-configuration.""" - with patch("subprocess.run") as mock_run: - # Mock successful gcloud container clusters get-credentials - mock_run.return_value.returncode = 0 - mock_run.return_value.stdout = "Fetching cluster endpoint" - - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(gcp_config) - executor._setup_kubernetes() - - # Verify gcloud command was called - mock_run.assert_called() - # Find the gcloud call in all the calls made - gcloud_call_found = False - for call in mock_run.call_args_list: - call_args = call[0][0] - if "gcloud" in call_args: - gcloud_call_found = True - assert "container" in call_args - assert "clusters" in call_args - assert "get-credentials" in call_args - assert "test-cluster" in call_args - break - assert gcloud_call_found, "GCloud command was not called" - - def test_cloud_auto_configuration_disabled(self): - """Test when cloud auto-configuration is disabled.""" - config = ClusterConfig( - cluster_type="kubernetes", - cloud_auto_configure=False, - ) - - with patch("kubernetes.config.load_kube_config"): - executor = ClusterExecutor(config) - # Should not raise any cloud provider errors - executor._setup_kubernetes() - - def test_cloud_auto_configuration_failure_fallback(self, aws_config): - """Test fallback when cloud auto-configuration fails.""" - with patch("subprocess.run") as mock_run: - # Mock failed aws command - mock_run.return_value.returncode = 1 - mock_run.return_value.stderr = "Cluster not found" - - with patch("kubernetes.config.load_kube_config"): - # Should not raise exception, should fallback to manual config - executor = ClusterExecutor(aws_config) - executor._setup_kubernetes() - - -class TestKubernetesConfiguration: - """Test Kubernetes-specific configuration options.""" - - def test_kubernetes_config_defaults(self): - """Test Kubernetes configuration defaults.""" - config = ClusterConfig(cluster_type="kubernetes") - - assert config.k8s_namespace == "default" - assert config.k8s_image == "python:3.11-slim" - assert config.k8s_service_account is None - assert config.k8s_pull_policy == "IfNotPresent" - assert config.k8s_job_ttl_seconds == 3600 - assert config.k8s_backoff_limit == 3 - - def test_kubernetes_config_customization(self): - """Test customizing Kubernetes configuration.""" - config = ClusterConfig( - cluster_type="kubernetes", - k8s_namespace="custom-namespace", - k8s_image="python:3.12", - k8s_service_account="my-service-account", - k8s_pull_policy="Always", - k8s_job_ttl_seconds=7200, - k8s_backoff_limit=5, - ) - - assert config.k8s_namespace == "custom-namespace" - assert config.k8s_image == "python:3.12" - assert config.k8s_service_account == "my-service-account" - assert config.k8s_pull_policy == "Always" - assert config.k8s_job_ttl_seconds == 7200 - assert config.k8s_backoff_limit == 5 - - def test_cloud_provider_config_defaults(self): - """Test cloud provider configuration defaults.""" - config = ClusterConfig() - - assert config.cloud_provider == "manual" - assert config.cloud_region is None - assert config.cloud_auto_configure is False - - def test_aws_specific_config(self): - """Test AWS-specific configuration.""" - config = ClusterConfig( - cloud_provider="aws", - eks_cluster_name="my-cluster", - aws_profile="production", - ) - - assert config.eks_cluster_name == "my-cluster" - assert config.aws_profile == "production" - - def test_azure_specific_config(self): - """Test Azure-specific configuration.""" - config = ClusterConfig( - cloud_provider="azure", - aks_cluster_name="my-cluster", - azure_resource_group="my-rg", - azure_subscription_id="my-subscription", - ) - - assert config.aks_cluster_name == "my-cluster" - assert config.azure_resource_group == "my-rg" - assert config.azure_subscription_id == "my-subscription" - - def test_gcp_specific_config(self): - """Test GCP-specific configuration.""" - config = ClusterConfig( - cloud_provider="gcp", - gke_cluster_name="my-cluster", - gcp_project_id="my-project", - gcp_zone="us-central1-a", - ) - - assert config.gke_cluster_name == "my-cluster" - assert config.gcp_project_id == "my-project" - assert config.gcp_zone == "us-central1-a" - - -class TestKubernetesErrorHandling: - """Test error handling in Kubernetes operations.""" - - def test_kubernetes_import_error(self): - """Test handling of missing kubernetes package.""" - config = ClusterConfig(cluster_type="kubernetes") - - with patch.dict("sys.modules", {"kubernetes": None}): - executor = ClusterExecutor(config) - - with pytest.raises(ImportError, match="kubernetes package required"): - executor._setup_kubernetes() - - def test_kubernetes_job_submission_api_error(self): - """Test handling of Kubernetes API errors during job submission.""" - config = ClusterConfig(cluster_type="kubernetes") - - with patch("kubernetes.config.load_kube_config"): - with patch("kubernetes.client") as mock_client: - # Mock API error - mock_client.BatchV1Api().create_namespaced_job.side_effect = Exception( - "API Error" - ) - - executor = ClusterExecutor(config) - executor._setup_kubernetes() - - func_data = { - "func": lambda: 42, - "args": (), - "kwargs": {}, - "requirements": {}, - } - job_config = {"cores": 1, "memory": "1Gi"} - - with pytest.raises(Exception, match="API Error"): - executor._submit_k8s_job(func_data, job_config) - - def test_kubernetes_result_collection_no_pods(self): - """Test result collection when no pods are found.""" - config = ClusterConfig(cluster_type="kubernetes") - - with patch("kubernetes.config.load_kube_config"): - with patch("kubernetes.client") as mock_client: - # Mock empty pod list - mock_pods_response = Mock() - mock_pods_response.items = [] - mock_client.CoreV1Api().list_namespaced_pod.return_value = ( - mock_pods_response - ) - - executor = ClusterExecutor(config) - executor._setup_kubernetes() - - # get_k8s_result lives on the KubernetesJobManager; - # ClusterExecutor has no _get_k8s_result shortcut. - with pytest.raises(RuntimeError, match="No successful pod found"): - executor.k8s_manager.get_k8s_result("test-job") - - def test_kubernetes_log_collection_error(self): - """Test error handling when log collection fails.""" - config = ClusterConfig(cluster_type="kubernetes") - - with patch("kubernetes.config.load_kube_config"): - with patch("kubernetes.client") as mock_client: - # Mock pod with log collection error - mock_pod = Mock() - mock_pod.metadata.name = "test-pod" - mock_pods_response = Mock() - mock_pods_response.items = [mock_pod] - mock_client.CoreV1Api().list_namespaced_pod.return_value = ( - mock_pods_response - ) - - # Mock log collection failure - mock_client.CoreV1Api().read_namespaced_pod_log.side_effect = Exception( - "Log error" - ) - - executor = ClusterExecutor(config) - executor._setup_kubernetes() - - # get_k8s_error_log lives on the KubernetesJobManager; - # ClusterExecutor has no _get_k8s_error_log shortcut. - error_log = executor.k8s_manager.get_k8s_error_log("test-job") - assert "Failed to get logs - Log error" in error_log - - -class TestEndToEndKubernetesWorkflow: - """Test complete Kubernetes workflow from submission to result collection.""" - - @patch("kubernetes.config.load_kube_config") - @patch("kubernetes.client") - def test_complete_kubernetes_workflow(self, mock_client, mock_load_config): - """Test complete workflow: submit -> monitor -> collect result. - - The pod log used to be a bare "CLUSTRIX_RESULT:" string decoded - with ast.literal_eval. get_k8s_result now requires the signed payload - the real worker produces, and the signing key is generated fresh - inside submit_k8s_job -- so the log has to be built (with the real - worker subprocess) after submission, using the key that job was - actually given, not a fixed value chosen up front. - """ - config = ClusterConfig( - cluster_type="kubernetes", - k8s_namespace="test", - cleanup_on_success=True, - ) - - # Set up mocks for successful workflow - mock_batch_api = Mock() - mock_core_api = Mock() - mock_client.BatchV1Api.return_value = mock_batch_api - mock_client.CoreV1Api.return_value = mock_core_api - - # Mock job creation - mock_job_response = Mock() - mock_job_response.metadata.name = "clustrix-job-123" - mock_batch_api.create_namespaced_job.return_value = mock_job_response - - # Mock job status (completed) - mock_job_status = Mock() - mock_job_status.status.succeeded = 1 - mock_job_status.status.failed = None - mock_job_status.status.active = None - mock_batch_api.read_namespaced_job.return_value = mock_job_status - - # Mock pod listing and logs - mock_pod = Mock() - mock_pod.metadata.name = "test-pod" - mock_pod.metadata.namespace = "test" - mock_pod.status.phase = "Succeeded" - mock_pods_response = Mock() - mock_pods_response.items = [mock_pod] - mock_core_api.list_namespaced_pod.return_value = mock_pods_response - - executor = ClusterExecutor(config) - - # Test complete workflow - func_data = { - "func": lambda: "Hello World", - "args": (), - "kwargs": {}, - "requirements": {}, - } - job_config = {"cores": 1, "memory": "1Gi"} - - # Submit job - job_id = executor._submit_k8s_job(func_data, job_config) - assert job_id == "clustrix-job-123" - - # Check status - status = executor._check_job_status(job_id) - assert status == "completed" - - # Collect result. The pod log is real worker output, signed with the - # per-job key submit_k8s_job actually generated for this job. - result_key = executor.k8s_manager.active_jobs[job_id]["result_key"] - mock_core_api.read_namespaced_pod_log.return_value = _run_real_worker( - lambda: "Hello World", result_key=result_key - ) - result = executor.k8s_manager.get_k8s_result(job_id) - assert result == "Hello World" - - # Verify cleanup was called - executor.k8s_manager.cleanup_k8s_job(job_id) - mock_batch_api.delete_namespaced_job.assert_called_once() diff --git a/tests/test_pricing_clients.py b/tests/test_pricing_clients.py deleted file mode 100644 index f45ccd1f..00000000 --- a/tests/test_pricing_clients.py +++ /dev/null @@ -1,270 +0,0 @@ -"""Tests for pricing client implementations.""" - -import json -import pytest -from unittest.mock import Mock, patch, MagicMock -from datetime import datetime, timedelta -from pathlib import Path -import tempfile - -from clustrix.pricing_clients.base import BasePricingClient, PricingCache -from clustrix.pricing_clients.aws_pricing import AWSPricingClient - - -class TestPricingCache: - """Test the pricing cache functionality.""" - - def test_cache_init(self): - """Test cache initialization.""" - with tempfile.TemporaryDirectory() as tmpdir: - cache_dir = Path(tmpdir) / "test_cache" - cache = PricingCache(cache_dir=cache_dir, ttl_hours=24) - - assert cache.cache_dir == cache_dir - assert cache.ttl == timedelta(hours=24) - assert cache_dir.exists() - - def test_cache_get_miss(self): - """Test cache miss returns None.""" - with tempfile.TemporaryDirectory() as tmpdir: - cache = PricingCache(cache_dir=Path(tmpdir)) - result = cache.get("nonexistent_key") - assert result is None - - def test_cache_set_and_get(self): - """Test setting and getting cached data.""" - with tempfile.TemporaryDirectory() as tmpdir: - cache = PricingCache(cache_dir=Path(tmpdir), ttl_hours=1) - - test_data = {"instance_type": "t2.micro", "price": 0.0116} - cache.set("test_key", test_data) - - # Retrieve data - result = cache.get("test_key") - assert result == test_data - - def test_cache_expiration(self): - """Test cache expiration.""" - with tempfile.TemporaryDirectory() as tmpdir: - cache = PricingCache( - cache_dir=Path(tmpdir), ttl_hours=0 - ) # Immediate expiration - - test_data = {"price": 0.0116} - cache.set("test_key", test_data) - - # Add a small delay to ensure cache expires - import time - - time.sleep(0.001) # 1ms delay - - # Data should be expired now - result = cache.get("test_key") - assert result is None - - def test_cache_file_corruption_handling(self): - """Test cache handles corrupted files gracefully.""" - with tempfile.TemporaryDirectory() as tmpdir: - cache = PricingCache(cache_dir=Path(tmpdir)) - - # Create a corrupted cache file - cache_file = cache.cache_dir / "corrupt_key.json" - cache_file.write_text("invalid json content") - - # Should return None instead of raising exception - result = cache.get("corrupt_key") - assert result is None - - -class TestBasePricingClient: - """Test the base pricing client functionality.""" - - def test_is_pricing_data_outdated(self): - """Test checking if pricing data is outdated.""" - - class TestClient(BasePricingClient): - def get_instance_pricing(self, instance_type, region, **kwargs): - return None - - def get_all_pricing(self, region, **kwargs): - return {} - - def _fetch_pricing_from_api(self, instance_type, region, **kwargs): - return None - - client = TestClient() - - # No date set - should be outdated - assert client.is_pricing_data_outdated(days=30) is True - - # Recent date - not outdated - client._hardcoded_pricing_date = datetime.now().isoformat() - assert client.is_pricing_data_outdated(days=30) is False - - # Old date - outdated - old_date = (datetime.now() - timedelta(days=60)).isoformat() - client._hardcoded_pricing_date = old_date - assert client.is_pricing_data_outdated(days=30) is True - - def test_get_fallback_price(self): - """Test fallback pricing retrieval.""" - - class TestClient(BasePricingClient): - def __init__(self): - super().__init__() - self._hardcoded_pricing = {"t2.micro": 0.0116} - self._hardcoded_pricing_date = "2025-01-01" - - def get_instance_pricing(self, instance_type, region, **kwargs): - return None - - def get_all_pricing(self, region, **kwargs): - return {} - - def _fetch_pricing_from_api(self, instance_type, region, **kwargs): - return None - - client = TestClient() - - # Existing instance type - price = client._get_fallback_price("t2.micro") - assert price == 0.0116 - - # Non-existent instance type - price = client._get_fallback_price("nonexistent.type") - assert price is None - - -class TestAWSPricingClient: - """Test the AWS pricing client implementation.""" - - def test_init(self): - """Test AWS pricing client initialization.""" - client = AWSPricingClient(cache_ttl_hours=12) - - assert client.cache.ttl == timedelta(hours=12) - assert client._hardcoded_pricing_date is not None - assert "t2.micro" in client._hardcoded_pricing - - def test_get_region_name(self): - """Test region code to name conversion.""" - client = AWSPricingClient() - - # Test known regions - assert client._get_region_name("us-east-1") == "US East (N. Virginia)" - assert client._get_region_name("eu-west-1") == "EU (Ireland)" - - # Test fallback for unknown region - assert client._get_region_name("unknown-region") == "US East (N. Virginia)" - - @patch("boto3.client") - def test_get_instance_pricing_from_api(self, mock_boto_client): - """Test getting pricing from AWS API.""" - # Mock the boto3 pricing client - mock_pricing_client = MagicMock() - mock_boto_client.return_value = mock_pricing_client - - # Mock API response - mock_response = { - "PriceList": [ - json.dumps( - { - "terms": { - "OnDemand": { - "sku1": { - "priceDimensions": { - "dim1": {"pricePerUnit": {"USD": "0.0104"}} - } - } - } - } - } - ) - ] - } - mock_pricing_client.get_products.return_value = mock_response - - client = AWSPricingClient() - - # Clear cache to ensure API is called - import shutil - - if client.cache.cache_dir.exists(): - shutil.rmtree(client.cache.cache_dir) - client.cache.cache_dir.mkdir(exist_ok=True) - - price = client.get_instance_pricing("t3.micro", "us-east-1") - - assert price == 0.0104 - mock_pricing_client.get_products.assert_called_once() - - def test_get_instance_pricing_fallback(self): - """Test fallback to hardcoded pricing when API fails.""" - client = AWSPricingClient() - - # Mock the API to fail - with patch.object(client, "_fetch_pricing_from_api", return_value=None): - price = client.get_instance_pricing("t2.micro", "us-east-1") - - assert price == 0.0116 # Hardcoded price - - def test_get_instance_pricing_with_cache(self): - """Test pricing retrieval with caching.""" - with tempfile.TemporaryDirectory() as tmpdir: - client = AWSPricingClient() - client.cache.cache_dir = Path(tmpdir) - - # Mock successful API call - with patch.object(client, "_fetch_pricing_from_api") as mock_fetch: - mock_fetch.return_value = {"price": 0.0104} - - # First call - should hit API - price1 = client.get_instance_pricing("t3.micro", "us-east-1") - assert price1 == 0.0104 - assert mock_fetch.call_count == 1 - - # Second call - should hit cache - price2 = client.get_instance_pricing("t3.micro", "us-east-1") - assert price2 == 0.0104 - assert mock_fetch.call_count == 1 # No additional API call - - def test_get_spot_pricing(self): - """Test spot instance pricing calculation.""" - client = AWSPricingClient() - - # Test with known instance type - spot_price = client.get_spot_pricing("t2.micro", "us-east-1") - on_demand_price = 0.0116 - expected_spot_price = on_demand_price * 0.3 # 70% discount - - assert spot_price == pytest.approx(expected_spot_price, rel=1e-4) - - # Test with unknown instance type - spot_price = client.get_spot_pricing("unknown.type", "us-east-1") - assert spot_price is None - - @patch("boto3.client") - def test_fetch_pricing_no_credentials(self, mock_boto_client): - """Test handling of missing AWS credentials.""" - from botocore.exceptions import NoCredentialsError - - mock_boto_client.side_effect = NoCredentialsError() - - client = AWSPricingClient() - result = client._fetch_pricing_from_api("t2.micro", "us-east-1") - - assert result is None - - def test_get_all_pricing(self): - """Test getting all pricing information.""" - client = AWSPricingClient() - - all_pricing = client.get_all_pricing("us-east-1") - - assert isinstance(all_pricing, dict) - assert "t2.micro" in all_pricing - assert all_pricing["t2.micro"] == 0.0116 - - -if __name__ == "__main__": - pytest.main([__file__, "-v"]) diff --git a/tests/unit/test_backends_cloud_contract.py b/tests/unit/test_backends_cloud_contract.py deleted file mode 100644 index d23bad6f..00000000 --- a/tests/unit/test_backends_cloud_contract.py +++ /dev/null @@ -1,72 +0,0 @@ -"""The cloud-provider job interface is explicit and fails at submit time (#119). - -No mocks and no credentials. These assert error behaviour, which is the part -that was wrong: a provider with no ``create_instance`` was accepted, the job -was reported as submitted, and the ``NotImplementedError`` then surfaced deep -inside a background thread where nothing could act on it. - -What these tests CANNOT verify is that the 'lambda' path actually provisions a -machine -- that needs real Lambda Cloud credentials and real money. -""" - -import pytest - -from clustrix.config import ClusterConfig -from clustrix.executor_cloud import REQUIRED_PROVIDER_METHODS, CloudJobManager -from clustrix.utils import serialize_function - - -def add(a, b): - return a + b - - -FUNC_DATA = None - - -def _func_data(): - global FUNC_DATA - if FUNC_DATA is None: - FUNC_DATA = serialize_function(add, (1, 2), {}) - return FUNC_DATA - - -@pytest.mark.parametrize("provider", ["aws", "azure", "gcp", "huggingface"]) -def test_providers_without_instance_creation_are_refused_at_submit(provider): - manager = CloudJobManager(ClusterConfig()) - - with pytest.raises(NotImplementedError) as excinfo: - manager.submit_cloud_job(_func_data(), {"cores": 1}, provider) - - message = str(excinfo.value) - assert f"'{provider}'" in message - assert "create_instance" in message - # And nothing was left behind claiming to be a running job. - assert manager.active_jobs == {} - - -def test_lambda_without_credentials_fails_on_authentication_not_interface(): - """Lambda implements the interface; the missing piece is credentials.""" - manager = CloudJobManager(ClusterConfig(lambda_api_key=None)) - - with pytest.raises(RuntimeError, match="not authenticated"): - manager.submit_cloud_job(_func_data(), {"cores": 1}, "lambda") - - assert manager.active_jobs == {} - - -def test_lambda_provider_implements_the_declared_interface(): - from clustrix.cloud_providers.lambda_cloud import LambdaCloudProvider - - provider = LambdaCloudProvider() - missing = [ - name - for name in REQUIRED_PROVIDER_METHODS - if not callable(getattr(provider, name, None)) - ] - assert missing == [] - - -def test_unknown_provider_is_rejected(): - manager = CloudJobManager(ClusterConfig()) - with pytest.raises(ValueError, match="Unsupported cloud provider"): - manager.submit_cloud_job(_func_data(), {"cores": 1}, "nimbus") diff --git a/tests/unit/test_backends_kubernetes.py b/tests/unit/test_backends_kubernetes.py deleted file mode 100644 index ffbb34bc..00000000 --- a/tests/unit/test_backends_kubernetes.py +++ /dev/null @@ -1,194 +0,0 @@ -"""Real tests for the Kubernetes backend's result handling (#119). - -No mocks. The worker program the container runs is generated by -``build_worker_program`` and executed here with the real interpreter, so these -tests exercise the same code a pod would run. What cannot be exercised without -a cluster is the Kubernetes API itself -- and for that the tests assert the -error behaviour, which is the part that was wrong: a status that could not be -read was reported as "completed". -""" - -import base64 -import hashlib -import hmac -import os -import subprocess -import sys -from datetime import datetime, timedelta - -import dill -import pytest - -from clustrix.config import ClusterConfig -from clustrix.executor_connections import ConnectionManager -from clustrix.executor_kubernetes import ( - KubernetesJobManager, - build_container_command, - build_worker_program, - decode_signed_result, -) -from clustrix.utils import serialize_function - - -def measure(scale): - """Return an object with no literal repr. - - ``ast.literal_eval(repr(datetime(...)))`` raises, so under the old path - this came back as the *string* "datetime.datetime(2026, 1, 6, 0, 0)" and - the caller had no way to tell that from a real answer. - """ - return datetime(2026, 1, 1) + timedelta(days=scale) - - -def explode(message): - raise ValueError(message) - - -def _run_worker(func, args, kwargs, key): - """Run the real generated worker program and return (proc, program).""" - func_data = serialize_function(func, args, kwargs) - import cloudpickle - - b64 = base64.b64encode(cloudpickle.dumps(func_data)).decode("utf-8") - program = build_worker_program(b64) - - env = dict(os.environ) - env["CLUSTRIX_RESULT_KEY"] = key - proc = subprocess.run( - [sys.executable, "-c", program], - capture_output=True, - text=True, - env=env, - timeout=120, - ) - return proc, program - - -def test_worker_returns_a_real_object_not_its_repr(): - key = "0" * 64 - proc, _ = _run_worker(measure, (5,), {}, key) - - assert proc.returncode == 0, proc.stderr - result = decode_signed_result(proc.stdout, key) - assert result == datetime(2026, 1, 6) - assert isinstance(result, datetime) - # The old marker is gone entirely, so nothing can fall back to a repr. - assert "CLUSTRIX_RESULT:" not in proc.stdout - - -def test_worker_failure_is_never_decoded_as_a_result(): - key = "1" * 64 - proc, _ = _run_worker(explode, ("boom",), {}, key) - - assert proc.returncode == 1 - assert "CLUSTRIX_ERROR:boom" in proc.stdout - assert "CLUSTRIX_TRACEBACK:" in proc.stdout - - with pytest.raises(RuntimeError, match="no clustrix result"): - decode_signed_result(proc.stdout, key) - - -def test_worker_refuses_to_run_without_a_signing_key(): - func_data = serialize_function(measure, (2,), {}) - import cloudpickle - - b64 = base64.b64encode(cloudpickle.dumps(func_data)).decode("utf-8") - env = dict(os.environ) - env.pop("CLUSTRIX_RESULT_KEY", None) - - proc = subprocess.run( - [sys.executable, "-c", build_worker_program(b64)], - capture_output=True, - text=True, - env=env, - timeout=120, - ) - - assert proc.returncode == 1 - assert "CLUSTRIX_RESULT_KEY is not set" in proc.stdout - - -def test_decode_rejects_a_tampered_payload(): - key = "2" * 64 - forged = dill.dumps(datetime(1999, 9, 9), protocol=4) - logs = ( - "CLUSTRIX_RESULT_B64:" - + base64.b64encode(forged).decode() - + "\nCLUSTRIX_RESULT_HMAC:" - + hmac.new(b"wrong-key", forged, hashlib.sha256).hexdigest() - + "\n" - ) - - with pytest.raises(RuntimeError, match="integrity check"): - decode_signed_result(logs, key) - - -def test_decode_rejects_an_unsigned_result(): - payload = dill.dumps(datetime(2000, 1, 1), protocol=4) - logs = "CLUSTRIX_RESULT_B64:" + base64.b64encode(payload).decode() + "\n" - - with pytest.raises(RuntimeError, match="no signature"): - decode_signed_result(logs, result_key="3" * 64) - - -def test_decode_rejects_the_old_repr_style_marker(): - """The pre-fix worker printed `CLUSTRIX_RESULT:`; it is not a result.""" - with pytest.raises(RuntimeError, match="no clustrix result"): - decode_signed_result("CLUSTRIX_RESULT:42\n", result_key="4" * 64) - - -def test_decode_never_returns_the_raw_log(): - logs = "some unrelated pod chatter\nmore chatter\n" - with pytest.raises(RuntimeError): - decode_signed_result(logs, result_key="5" * 64) - - -def test_container_command_refuses_shell_metacharacters(): - with pytest.raises(ValueError, match="shell would"): - build_container_command('print("hi")') - with pytest.raises(ValueError, match="shell would"): - build_container_command("print('$HOME')") - - -def test_generated_worker_program_is_shell_safe(): - func_data = serialize_function(measure, (1,), {}) - import cloudpickle - - b64 = base64.b64encode(cloudpickle.dumps(func_data)).decode("utf-8") - command = build_container_command(build_worker_program(b64)) - assert command.count('"') == 2 # only the two wrapping python -c quotes - - -def test_unreadable_job_status_is_an_error_not_completed(): - """A status that cannot be read used to be reported as success. - - There is no cluster here, so the API call genuinely fails -- which is - precisely the situation that used to return "completed" for any job the - manager happened to be tracking. - """ - config = ClusterConfig(cluster_type="kubernetes", k8s_namespace="default") - manager = KubernetesJobManager(config, ConnectionManager(config)) - manager.active_jobs["clustrix-job-1-2"] = { - "status": "submitted", - "k8s_job": True, - "result_key": "6" * 64, - } - - with pytest.raises(Exception) as excinfo: - status = manager.check_k8s_job_status("clustrix-job-1-2") - pytest.fail(f"expected a failure, got status {status!r}") - - assert "completed" not in str(excinfo.value) - - -def test_result_collection_without_a_cluster_is_an_error(): - config = ClusterConfig(cluster_type="kubernetes", k8s_namespace="default") - manager = KubernetesJobManager(config, ConnectionManager(config)) - manager.active_jobs["clustrix-job-3-4"] = { - "status": "submitted", - "k8s_job": True, - "result_key": "7" * 64, - } - - with pytest.raises(Exception): - manager.get_k8s_result("clustrix-job-3-4") From 0f6a6634bb5096eff878ace22ac7adc8a1557afd Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:02:07 -0400 Subject: [PATCH 03/56] Notes: suspend checkpoint and resume instructions Records that 07db2e6 is a non-importable WIP tip, exactly what remains in each lane, and the four-check gate the next commit has to pass. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- notes/2026-08-19-backend-removal-session.md | 83 +++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/notes/2026-08-19-backend-removal-session.md b/notes/2026-08-19-backend-removal-session.md index 12438d74..8aed2bbe 100644 --- a/notes/2026-08-19-backend-removal-session.md +++ b/notes/2026-08-19-backend-removal-session.md @@ -79,3 +79,86 @@ default, refuses anything not tagged `clustrix:managed=true`. - Colab: executing cells needs a Google sign-in, which is not something to do. Expect the verification to distinguish *loaded in Colab* from *executed locally* and to be explicit about which claim each result supports. + +--- + +## SUSPEND CHECKPOINT — 2026-08-19T13:58Z + +The machine was suspended here. Three background agents were stopped +mid-flight and the tree was committed as `07db2e6` **WIP: backend removal, +incomplete -- DO NOT MERGE**. + +### The tree at 07db2e6 does not import + +``` +ModuleNotFoundError: No module named 'clustrix.executor_kubernetes' + clustrix/executor.py:25 -> from .executor_kubernetes import KubernetesJobManager +``` + +This is expected, not a regression to debug. The code agent was stopped +exactly as it finished `executor_connections.py` and reached `utils.py`. +`--no-verify` was used on the commit because a tree that cannot import +cannot pass black/flake8/mypy. **The next commit on this branch must pass +the full gate.** + +### What landed (95 deletions, 7 partial edits) + +Deleted: `cloud_providers/`, `cost_providers/`, `pricing_clients/`, +`kubernetes/` (7 provisioners), `executor_cloud.py`, +`executor_kubernetes.py`, `cloud_provider_manager.py`, `cost_monitoring.py`, +`auto_install.py`; 9 notebooks; `tutorials/kubernetes_tutorial.rst`, +`tutorials/pbs_tutorial.rst`; `api/cost_monitoring.rst`. + +Partially edited: `executor_connections.py`, `executor_core.py`, +`executor_scheduler_status.py`, `executor_schedulers.py`, +`docs/source/{configuration,index,limitations}.rst`. + +### Resume from here, in this order + +1. **Finish the code lane.** The known-remaining work: + - `clustrix/executor.py:25` — drop the `KubernetesJobManager` import + (that shim re-exports; check every name it lists). + - `clustrix/utils.py` — delete `_create_pbs_script` / `_create_sge_script` + and their dispatch. + - `clustrix/config.py:343` — `SUPPORTED_CLUSTER_TYPES` down to + `("local", "ssh", "slurm", "huggingface")`. + - `clustrix/config.py:498` — removed-key table before the difflib path + (see the decision above; this is the one that stops an existing + `clustrix.yml` from getting a misleading "did you mean?"). + - `clustrix/__init__.py` — the 5 `cost_monitoring` names in `__all__`. + - `clustrix/cli.py` — the `click.Choice` list. + - the widget's cluster-type dropdown. + - `grep -rn "pbs\|sge\|kubernetes\|k8s_\|aws_\|azure_\|gcp_\|lambda" clustrix/ tests/` + until only deliberate mentions remain. +2. **Finish the docs lane** — `docs/source/api/notebook_magic.rst` still + documents pbs/sge/kubernetes in the dropdown and the Connection section; + `troubleshooting.rst` has a PBS/SGE/Kubernetes row in its scheduler-output + table; `CHANGELOG.md` needs the removal recorded under 0.2.0 and its + "Implemented but unverified" section rewritten to point at #140-#146. +3. **Then the integrated gate, all four, in one clean cycle:** + ``` + python -m pytest tests/ -m "not real_world" \ + --ignore=tests/real_world --ignore=tests/integration \ + -q -o addopts="" --timeout=120 + black --check clustrix/ tests/ scripts/ && flake8 clustrix/ tests/ scripts/ && mypy clustrix/ + python scripts/check_docs_examples.py + cd docs && make html + ``` + Re-run *all* of them after any fix — a fix for one routinely breaks another. +4. Squash or amend `07db2e6` away, or land a follow-up commit that makes the + branch importable, before opening the PR. Do not merge a branch whose + history contains a non-importable tip unless the tip itself is clean. + +### Colab lane + +Stopped before producing findings. Its browser scratch (console logs + page +snapshots from two Colab loads) was moved out of the repo to the session +scratchpad `colab-evidence/`. It had correctly noticed that the working tree +was mid-surgery and was switching to a clean checkout of pushed `master`, +which is what Colab actually serves — that is the right approach when it +restarts. + +### Not started + +Posting per-issue evidence comments on the remaining open issues (48 open: +the pre-existing set plus #140-#146). From 21d368f05bd14c882caf603694ce239f0be9fa7f Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:03:04 -0400 Subject: [PATCH 04/56] Notes: record the two outstanding CI facts at the checkpoint The master Tests run was still in flight at suspend, and an older Real World Tests failure on 0ca28fa has not been examined. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- notes/2026-08-19-backend-removal-session.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/notes/2026-08-19-backend-removal-session.md b/notes/2026-08-19-backend-removal-session.md index 8aed2bbe..fad043b6 100644 --- a/notes/2026-08-19-backend-removal-session.md +++ b/notes/2026-08-19-backend-removal-session.md @@ -162,3 +162,16 @@ restarts. Posting per-issue evidence comments on the remaining open issues (48 open: the pre-existing set plus #140-#146). + +### CI state at the checkpoint + +- `Tests` on master `0e3490e` was still **in progress** when the machine was + suspended. It runs server-side, so it will have finished by the time work + resumes — check it first. The same tree passed as a pull-request run + (`f442d9b`, 15/15 green) before the merge, so a failure here would mean + something specific to the push-triggered path, not a code regression. +- A **`Real World Tests` run failed on `0ca28fa`** (PR #138 era, before this + session's work). It has not been looked at. That workflow makes real SSH + and cloud calls and is gated on secrets, so it is not part of the ordinary + gate — but it is a genuine unexamined failure and must not be waved off as + "pre-existing". Investigate on resume. From f13f179be449510199fb1ecf923e16597adb83ba Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:04:33 -0400 Subject: [PATCH 05/56] Notes: record issue #147, the pre-push hook that cannot fail Pushing the WIP branch surfaced it: four real-world categories reported failure and the hook announced success. run_real_world_tests.py drops every result and exits 0. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- notes/2026-08-19-backend-removal-session.md | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/notes/2026-08-19-backend-removal-session.md b/notes/2026-08-19-backend-removal-session.md index fad043b6..dadf5b32 100644 --- a/notes/2026-08-19-backend-removal-session.md +++ b/notes/2026-08-19-backend-removal-session.md @@ -175,3 +175,32 @@ the pre-existing set plus #140-#146). and cloud calls and is gated on secrets, so it is not part of the ordinary gate — but it is a genuine unexamined failure and must not be waved off as "pre-existing". Investigate on resume. + +### Found at the checkpoint: the pre-push hook cannot block a push (#147) + +Pushing this branch made the pre-push hook run the real-world suite. All four +categories printed `❌ ... failed`, and the hook then printed +`✅ All real-world tests passed!` and allowed the push. + +`scripts/run_real_world_tests.py` `main()` discards every +`runner.run_*_tests()` return value and never calls `sys.exit`, so the script +exits 0 no matter what. The hook's `if ! python scripts/run_real_world_tests.py +--filesystem` guard can never fire. Confirmed directly: + +``` +$ python scripts/run_real_world_tests.py --filesystem >/tmp/rw.txt 2>&1; echo $? +0 +$ head -2 /tmp/rw.txt +📁 Running Filesystem Tests... +❌ Filesystem tests failed: +``` + +Same class as the `flake8 --exit-zero` / `mypy continue-on-error` defects from +#138. Filed as **#147**. Also noted there: the failure message prints +`result.stdout`, which was empty in all four cases, so the hook says something +failed without saying what. + +The four failures themselves are explained by this branch's tip not importing +(`clustrix.executor_kubernetes` is gone) — pytest could not collect, so no real +SSH or cloud calls were made. That does not soften #147: a tree that cannot +import is exactly the case the hook exists to stop, and it waved it through. From 5a3d9bb7a3f8741c945a7203ffd3ae3921273079 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:33:04 -0400 Subject: [PATCH 06/56] Docs: remove PBS/SGE/Kubernetes/cloud backend references from Sphinx sources Every remaining reference to a removed backend in docs/source now points at the :ref:`removed-backends` note in limitations.rst, which names the tracking issues (#140-#146) and says the backends are planned for a future release rather than currently supported. Also drops the broken :doc:`cost_monitoring` cross-references left behind by the deletion of docs/source/api/cost_monitoring.rst. --- clustrix/field_mappings.py | 241 ----------- docs/source/api/config.rst | 31 +- docs/source/api/decorator.rst | 6 +- docs/source/api/dependency_analysis.rst | 3 +- docs/source/api/file_packaging.rst | 3 +- docs/source/api/filesystem.rst | 3 +- docs/source/api/local_executor.rst | 4 +- docs/source/api/notebook_magic.rst | 22 +- docs/source/execution_model.rst | 65 +-- docs/source/installation.rst | 42 +- docs/source/introduction.rst | 25 +- docs/source/quickstart.rst | 13 +- docs/source/ssh_setup.rst | 4 +- docs/source/troubleshooting.rst | 16 +- docs/source/tutorials/filesystem_tutorial.rst | 2 +- docs/source/tutorials/slurm_tutorial.rst | 6 +- docs/source/tutorials/usage_patterns.rst | 108 ++--- .../test_field_mapping_validation.py | 384 ------------------ 18 files changed, 136 insertions(+), 842 deletions(-) delete mode 100644 clustrix/field_mappings.py delete mode 100644 tests/real_world/test_field_mapping_validation.py diff --git a/clustrix/field_mappings.py b/clustrix/field_mappings.py deleted file mode 100644 index a9e2c6eb..00000000 --- a/clustrix/field_mappings.py +++ /dev/null @@ -1,241 +0,0 @@ -"""Field mapping system for cloud provider configuration compatibility. - -This module provides standardized field mapping between widget field names, -ClusterConfig field names, and cloud provider API expectations. -""" - -from typing import Dict, Any, Optional -import logging - -logger = logging.getLogger(__name__) - - -# Comprehensive field mapping for all cloud providers -CLOUD_PROVIDER_FIELD_MAPPING = { - "aws": { - # Authentication fields - "aws_access_key": "access_key_id", - "aws_access_key_id": "access_key_id", # ClusterConfig compatibility - "aws_secret_key": "secret_access_key", - "aws_secret_access_key": "secret_access_key", # ClusterConfig compatibility - "aws_session_token": "session_token", - "aws_region": "region", - "aws_profile": "profile", - # Infrastructure fields - "aws_instance_type": "instance_type", - "aws_cluster_type": "cluster_type", - "eks_cluster_name": "eks_cluster_name", - }, - "azure": { - # Authentication fields - "azure_subscription_id": "subscription_id", - "azure_client_id": "client_id", - "azure_client_secret": "client_secret", - "azure_tenant_id": "tenant_id", - "azure_region": "region", - "azure_resource_group": "resource_group", - # Infrastructure fields - "azure_instance_type": "instance_type", - "aks_cluster_name": "aks_cluster_name", - }, - "gcp": { - # Authentication fields - "gcp_project_id": "project_id", - "gcp_service_account_key": "service_account_key", - "gcp_zone": "zone", - "gcp_region": "region", - # Infrastructure fields - "gcp_instance_type": "instance_type", - "gke_cluster_name": "gke_cluster_name", - }, - "huggingface": { - # Authentication fields - "hf_token": "token", - "hf_username": "username", - # Infrastructure fields - "hf_hardware": "hardware", - "hf_sdk": "sdk", - }, - "lambda": { - # Authentication fields - "lambda_api_key": "api_key", - # Infrastructure fields - "lambda_instance_type": "instance_type", - }, -} - -# Required fields for each provider (for validation) -REQUIRED_FIELDS = { - "aws": ["access_key_id", "secret_access_key"], - "azure": ["subscription_id", "client_id", "client_secret", "tenant_id"], - "gcp": ["project_id", "service_account_key"], - "huggingface": ["token"], - "lambda": ["api_key"], -} - -# Optional fields with default values -OPTIONAL_FIELD_DEFAULTS = { - "aws": { - "region": "us-east-1", - }, - "azure": { - "region": "eastus", - "resource_group": "clustrix-rg", - }, - "gcp": { - "region": "us-central1", - }, - "huggingface": {}, - "lambda": {}, -} - - -def map_widget_fields_to_provider( - provider: str, widget_config: Dict[str, Any] -) -> Dict[str, Any]: - """ - Map widget field names to cloud provider API field names. - - Args: - provider: Cloud provider name (aws, azure, gcp, huggingface, lambda) - widget_config: Configuration dictionary with widget field names - - Returns: - Dictionary with provider API field names - - Raises: - ValueError: If provider is not supported - KeyError: If required fields are missing - """ - if provider not in CLOUD_PROVIDER_FIELD_MAPPING: - raise ValueError(f"Unsupported cloud provider: {provider}") - - mapping = CLOUD_PROVIDER_FIELD_MAPPING[provider] - provider_config = {} - - # Map fields from widget names to provider names - for widget_field, provider_field in mapping.items(): - if widget_field in widget_config and widget_config[widget_field]: - provider_config[provider_field] = widget_config[widget_field] - logger.debug( - f"Mapped {widget_field} -> {provider_field}: " - f"{widget_config[widget_field]}" - ) - - # Add optional defaults for missing fields - defaults = OPTIONAL_FIELD_DEFAULTS.get(provider, {}) - if defaults: - for field, default_value in defaults.items(): # type: ignore[attr-defined] - if field not in provider_config: - provider_config[field] = default_value - logger.debug(f"Added default {field}: {default_value}") - - # Validate required fields are present - required = REQUIRED_FIELDS.get(provider, []) - missing_fields = [field for field in required if field not in provider_config] - - if missing_fields: - logger.error(f"Missing required fields for {provider}: {missing_fields}") - raise KeyError(f"Missing required {provider} fields: {missing_fields}") - - logger.info(f"Successfully mapped {len(provider_config)} fields for {provider}") - return provider_config - - -def validate_provider_config(provider: str, config: Dict[str, Any]) -> bool: - """ - Validate that a provider configuration has all required fields. - - Args: - provider: Cloud provider name - config: Configuration dictionary to validate - - Returns: - True if configuration is valid, False otherwise - """ - try: - required = REQUIRED_FIELDS.get(provider, []) - missing = [field for field in required if not config.get(field)] - - if missing: - logger.warning( - f"Provider {provider} config missing required fields: {missing}" - ) - return False - - logger.info(f"Provider {provider} configuration is valid") - return True - - except Exception as e: - logger.error(f"Error validating {provider} config: {e}") - return False - - -def get_widget_field_for_provider_field( - provider: str, provider_field: str -) -> Optional[str]: - """ - Get the widget field name that maps to a provider field. - - Args: - provider: Cloud provider name - provider_field: Provider API field name - - Returns: - Widget field name or None if not found - """ - if provider not in CLOUD_PROVIDER_FIELD_MAPPING: - return None - - mapping = CLOUD_PROVIDER_FIELD_MAPPING[provider] - - # Reverse lookup: find widget field that maps to this provider field - for widget_field, mapped_provider_field in mapping.items(): - if mapped_provider_field == provider_field: - return widget_field - - return None - - -def get_all_provider_fields(provider: str) -> Dict[str, str]: - """ - Get all field mappings for a provider. - - Args: - provider: Cloud provider name - - Returns: - Dictionary mapping widget fields to provider fields - """ - return CLOUD_PROVIDER_FIELD_MAPPING.get(provider, {}).copy() - - -def get_supported_providers() -> list: - """Get list of all supported cloud providers.""" - return list(CLOUD_PROVIDER_FIELD_MAPPING.keys()) - - -# Convenience functions for common use cases -def map_aws_fields(config: Dict[str, Any]) -> Dict[str, Any]: - """Map AWS widget fields to boto3 field names.""" - return map_widget_fields_to_provider("aws", config) - - -def map_azure_fields(config: Dict[str, Any]) -> Dict[str, Any]: - """Map Azure widget fields to Azure SDK field names.""" - return map_widget_fields_to_provider("azure", config) - - -def map_gcp_fields(config: Dict[str, Any]) -> Dict[str, Any]: - """Map GCP widget fields to Google Cloud SDK field names.""" - return map_widget_fields_to_provider("gcp", config) - - -def map_huggingface_fields(config: Dict[str, Any]) -> Dict[str, Any]: - """Map HuggingFace widget fields to HuggingFace API field names.""" - return map_widget_fields_to_provider("huggingface", config) - - -def map_lambda_fields(config: Dict[str, Any]) -> Dict[str, Any]: - """Map Lambda Cloud widget fields to Lambda API field names.""" - return map_widget_fields_to_provider("lambda", config) diff --git a/docs/source/api/config.rst b/docs/source/api/config.rst index 80abb654..79cf7ee4 100644 --- a/docs/source/api/config.rst +++ b/docs/source/api/config.rst @@ -79,7 +79,7 @@ unconditionally, described below. Beyond those, three separate mechanisms read further variables, and each is narrower than it looks: **Connecting over SSH without a password or key file configured.** When an -SSH-family cluster (``ssh``, ``slurm``, ``pbs``, ``sge``) has neither +SSH-family cluster (``ssh``, ``slurm``) has neither ``password`` nor ``key_file`` set, ``ClusterExecutor.setup_ssh_connection`` calls ``FlexibleCredentialManager.ensure_credential("ssh")`` (``clustrix/executor_connections.py``). That call first loads @@ -90,19 +90,14 @@ SSH-related ones. It then reads, via ``clustrix/credential_manager.py``: - ``SSH_HOST``, ``SSH_USERNAME``, ``SSH_PASSWORD``, ``SSH_PRIVATE_KEY_PATH``, ``SSH_PORT`` -- ``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``, ``AWS_REGION`` -- ``AZURE_SUBSCRIPTION_ID``, ``AZURE_TENANT_ID``, ``AZURE_CLIENT_ID``, - ``AZURE_CLIENT_SECRET`` -- ``GCP_PROJECT_ID``, ``GOOGLE_APPLICATION_CREDENTIALS``, - ``GCP_SERVICE_ACCOUNT_JSON`` -- ``KUBECONFIG``, ``K8S_NAMESPACE``, ``K8S_CONTEXT`` - ``HF_TOKEN``, ``HF_USERNAME`` -- ``LAMBDA_CLOUD_API_KEY``, ``LAMBDA_CLOUD_ENDPOINT`` -The non-SSH entries above are loaded into the process environment by this -call too, because loading ``.env`` loads the whole file regardless of which -provider was asked for -- but only the ``SSH_*`` variables can affect *this* -connection; the rest only matter if something else later reads them. +Only the ``SSH_*`` variables can affect *this* connection. Everything else +your ``.env`` happens to define is put into the process environment as a side +effect of loading the whole file, and matters only if something else later +reads it. In particular, cloud-provider and Kubernetes credentials no longer +select any execution backend: those backends were removed in v0.2.0, see +:ref:`removed-backends`. **The optional SSH-key-setup helper.** ``setup_ssh_keys_with_fallback()`` (exported from ``clustrix``; not called automatically by ``@cluster`` or @@ -156,10 +151,12 @@ Cluster Settings - ``cluster_type``: Type of cluster. The full, authoritative set is ``clustrix.config.SUPPORTED_CLUSTER_TYPES`` -- ``local``, ``ssh``, - ``slurm``, ``pbs``, ``sge``, ``kubernetes``, ``huggingface``. Both the CLI - and the notebook widget read this same tuple for their cluster-type - choices, so it is never possible for one of them to offer a backend the - other (or ``ClusterExecutor``) cannot actually run. + ``slurm``, ``huggingface``. Both the CLI and the notebook widget read this + same tuple for their cluster-type choices, so it is never possible for one + of them to offer a backend the other (or ``ClusterExecutor``) cannot + actually run. ``pbs``, ``sge``, ``kubernetes`` and the cloud VM providers + are not in the set: they were removed in v0.2.0 and now raise + ``ValueError: Unsupported cluster type``. See :ref:`removed-backends`. - ``cluster_type="local"`` runs the function on the submitting machine via ``LocalJobManager`` (see :doc:`local_executor`) instead of talking to a scheduler at all -- there is no host, no SSH connection, and @@ -183,7 +180,7 @@ Paths - ``remote_work_dir``: Working directory on the cluster. Defaults to ``~/.clustrix/jobs``. It must be on a filesystem the compute node can see: - on SLURM, PBS and SGE each node has its own ``/tmp``, so an environment built + on SLURM each node has its own ``/tmp``, so an environment built on the login node is simply absent at run time and the job dies with exit 127 before writing any diagnostics. A home directory or a shared scratch path both work; ``/tmp`` does not. diff --git a/docs/source/api/decorator.rst b/docs/source/api/decorator.rst index 065bfe7a..f1d7bb1e 100644 --- a/docs/source/api/decorator.rst +++ b/docs/source/api/decorator.rst @@ -141,9 +141,9 @@ How Execution Mode Is Chosen ``clustrix.decorator._choose_execution_mode`` decides, on every call, whether to run locally or submit to a remote backend. It falls back to -*local* execution whenever ``config.cluster_host`` is unset (SLURM, PBS, -SGE, SSH) and the cluster type is not Kubernetes-with-auto-provisioning or -one of the HTTP-API backends (currently HuggingFace Jobs). Concretely: if +*local* execution whenever ``config.cluster_host`` is unset (SLURM, SSH) and +the cluster type is not one of the HTTP-API backends (currently HuggingFace +Jobs). Concretely: if you never call ``configure()`` with a real host, ``@cluster``-decorated functions still run -- in the calling process, with no cluster involved -- and the exact same code starts submitting real remote jobs the moment diff --git a/docs/source/api/dependency_analysis.rst b/docs/source/api/dependency_analysis.rst index 29497125..cc55ad01 100644 --- a/docs/source/api/dependency_analysis.rst +++ b/docs/source/api/dependency_analysis.rst @@ -4,8 +4,7 @@ Dependency Analysis .. currentmodule:: clustrix.dependency_analysis Every member of this module is documented explicitly below (grouped by -purpose), following the same pattern used in :doc:`cost_monitoring`. A -blanket ``automodule:: :members:`` is deliberately not used here: this +purpose). A blanket ``automodule:: :members:`` is deliberately not used here: this project's global ``autodoc_default_options`` sets ``members: True``, so an ``automodule`` directive combined with the explicit per-member directives below would document every class and function twice. diff --git a/docs/source/api/file_packaging.rst b/docs/source/api/file_packaging.rst index 44132026..f8159e3f 100644 --- a/docs/source/api/file_packaging.rst +++ b/docs/source/api/file_packaging.rst @@ -4,8 +4,7 @@ File Packaging System .. currentmodule:: clustrix.file_packaging Every member of this module is documented explicitly below (grouped by -purpose), following the same pattern used in :doc:`cost_monitoring`. A -blanket ``automodule:: :members:`` is deliberately not used here: this +purpose). A blanket ``automodule:: :members:`` is deliberately not used here: this project's global ``autodoc_default_options`` sets ``members: True``, so an ``automodule`` directive combined with the explicit per-member directives below would document every class and function twice. diff --git a/docs/source/api/filesystem.rst b/docs/source/api/filesystem.rst index d1e86183..627f672f 100644 --- a/docs/source/api/filesystem.rst +++ b/docs/source/api/filesystem.rst @@ -4,8 +4,7 @@ Filesystem Utilities .. currentmodule:: clustrix.filesystem Every member of this module is documented explicitly below (grouped by -purpose), following the same pattern used in :doc:`cost_monitoring`. A -blanket ``automodule:: :members:`` is deliberately not used here: this +purpose). A blanket ``automodule:: :members:`` is deliberately not used here: this project's global ``autodoc_default_options`` sets ``members: True``, so an ``automodule`` directive combined with the explicit per-member directives below would document every class and function twice. diff --git a/docs/source/api/local_executor.rst b/docs/source/api/local_executor.rst index c1eaa3bc..9ad86501 100644 --- a/docs/source/api/local_executor.rst +++ b/docs/source/api/local_executor.rst @@ -138,8 +138,8 @@ remote host is configured (see :doc:`decorator`). It is a different thing from ``ClusterConfig(cluster_type="local")``, which selects ``LocalJobManager`` as an actual, explicit backend: the same ``submit_job()`` / ``wait_for_result()`` / ``get_job_status()`` / -``cancel_job()`` interface that ``ClusterExecutor`` exposes for SLURM, PBS, -SGE, SSH, Kubernetes, and HuggingFace Jobs, just pointed at the machine +``cancel_job()`` interface that ``ClusterExecutor`` exposes for SLURM, SSH +and HuggingFace Jobs, just pointed at the machine that's submitting. Its full member documentation (``submit_job``, ``wait_for_result``, ``get_job_status``, ``cancel_job``, ``get_error_log``) is already generated by the ``automodule`` directive at the top of this diff --git a/docs/source/api/notebook_magic.rst b/docs/source/api/notebook_magic.rst index 58225e4b..c5dae830 100644 --- a/docs/source/api/notebook_magic.rst +++ b/docs/source/api/notebook_magic.rst @@ -47,7 +47,7 @@ Widget Interface - **Profile**: the active profile, and the configuration file that Save and Load use. New profiles are added with ``+`` and removed with ``-``. - **Resources**: cluster type, CPUs, memory, walltime. -- **Connection** (``ssh``, ``slurm``, ``pbs``, ``sge`` only): host, port, +- **Connection** (``ssh``, ``slurm`` only): host, port, username, SSH key file, password, remote work directory, an environment variable to read the password from, and an "Auto setup SSH keys" button. - **HuggingFace Jobs** (``huggingface`` only): namespace, flavor, token, and an @@ -59,13 +59,14 @@ Widget Interface commands. - **Output**: where the test buttons and errors report. -The cluster type dropdown offers ``local``, ``ssh``, ``slurm``, ``pbs``, -``sge``, ``kubernetes`` and ``huggingface``. Selecting ``kubernetes`` shows a -Kubernetes section: namespace, image, service account and image pull policy. -The remaining ``k8s_*`` settings (node count, region, provider, -auto-provisioning) are configuration-file or ``clustrix.configure()`` only. -There are no AWS, GCP, Azure or Lambda Cloud entries, because those execution -backends are unverified. +The cluster type dropdown offers ``local``, ``ssh``, ``slurm`` and +``huggingface`` -- the contents of +:data:`clustrix.config.SUPPORTED_CLUSTER_TYPES`, and nothing else. There are +no PBS, SGE, Kubernetes, AWS, GCP, Azure or Lambda Cloud entries, and no +``k8s_*`` settings: those backends are **not currently supported**. They were +removed in v0.2.0 because none had been shown to run a job end to end, and +each is planned for a future release under its own tracking issue -- see +:ref:`removed-backends`. "Apply" calls :func:`clustrix.configure` with the widget's values, so subsequent ``@cluster`` functions use them. @@ -96,8 +97,9 @@ Legacy widget The previous widget implementation, along with :data:`DEFAULT_CONFIGS`. It is no longer what ``%%remote`` displays and is kept only for compatibility. - Several of its templates name cluster types (``aws``, ``azure``, ``gcp``, - ``lambda_cloud``, ``huggingface_spaces``) that the executor cannot dispatch. + Any template it offers that names a cluster type outside + :data:`clustrix.config.SUPPORTED_CLUSTER_TYPES` cannot be dispatched by the + executor; see :ref:`removed-backends`. .. Documented from the module that defines it, not from the one that re-exports it: autodoc only picks up the ``#:`` comment at the definition diff --git a/docs/source/execution_model.rst b/docs/source/execution_model.rst index d309c778..dafec23c 100644 --- a/docs/source/execution_model.rst +++ b/docs/source/execution_model.rst @@ -8,9 +8,8 @@ Python reads your ``@cluster`` decorator to the moment your result comes back. Everything here was traced in the source (``clustrix/decorator.py``, ``clustrix/utils.py``, ``clustrix/executor_core.py``, ``clustrix/executor_connections.py``, ``clustrix/executor_schedulers.py``, -``clustrix/local_executor.py``, ``clustrix/hf_jobs.py``, -``clustrix/executor_kubernetes.py``), and every message quoted below is the -real message the code prints. +``clustrix/local_executor.py``, ``clustrix/hf_jobs.py``), and every message +quoted below is the real message the code prints. If you only remember one thing: **the decorator does almost nothing at import time.** All of the interesting work happens on the call. @@ -59,13 +58,11 @@ ignored option is worse than a rejected one: .. code-block:: text WARNING clustrix.decorator: @cluster received unrecognised option(s) gpu_type; - they have no effect. Recognised extras: aws_access_key_id, aws_region, - aws_secret_access_key, azure_client_id, azure_client_secret, - azure_subscription_id, azure_tenant_id, gcp_project_id, - gcp_service_account_key, hf_flavor, hf_namespace, hf_timeout, hf_token, - hf_username, instance_startup_timeout, k8s_image, k8s_namespace, - k8s_pull_policy, k8s_service_account, key_file, lambda_api_key, - terminate_on_completion + they have no effect. Recognised extras: hf_flavor, hf_namespace, hf_timeout, + hf_token, hf_username, key_file, ... + +The authoritative list is the ``cloud_params`` tuple in +``clustrix/decorator.py``; the warning prints it sorted. The order of operations on a call @@ -117,8 +114,6 @@ Memory strings are rewritten per scheduler by ``normalize_memory``: from clustrix.utils import normalize_memory print(normalize_memory("8GB", "slurm")) # SLURM wants 8G - print(normalize_memory("8GB", "pbs")) # PBS wants 8gb - print(normalize_memory("8GB", "k8s")) # Kubernetes wants 8GB Step 3: local or remote @@ -126,16 +121,15 @@ Step 3: local or remote ``_choose_execution_mode`` answers this, in this order: -1. ``cluster_type == "kubernetes"`` **and** ``auto_provision_k8s`` -> remote. -2. ``cluster_type == "huggingface"`` -> remote. This backend reaches its +1. ``cluster_type == "huggingface"`` -> remote. This backend reaches its compute over an HTTP API, so it legitimately has no ``cluster_host``; - without this rule it would fall into rule 3 and silently run on your laptop + without this rule it would fall into rule 2 and silently run on your laptop while reporting success. -3. No ``cluster_host`` -> **local**. -4. ``prefer_local_parallel`` is true -> local. -5. Otherwise -> remote. +2. No ``cluster_host`` -> **local**. +3. ``prefer_local_parallel`` is true -> local. +4. Otherwise -> remote. -Note rule 3: with the default configuration and no ``cluster_host``, a +Note rule 2: with the default configuration and no ``cluster_host``, a ``@cluster`` function runs on your own machine. That is the intended development behaviour, not a failure. ``cluster_type="local"`` is different -- it is a real backend that goes through serialization (see @@ -488,10 +482,9 @@ For every SSH-reachable backend, ``_stage_job_directory`` does this: Step 7c: the job script ----------------------- -``create_job_script`` dispatches on cluster type to -``_create_slurm_script`` / ``_create_pbs_script`` / ``_create_sge_script`` / -``_create_ssh_script``. Anything else raises -``ValueError: Unsupported cluster type: ...``. All four share +``create_job_script`` dispatches on cluster type to ``_create_slurm_script`` +and ``_create_ssh_script``. Anything else raises +``ValueError: Unsupported cluster type: ...``. Both share ``environment_setup_lines`` and ``job_execution_lines``. .. code-block:: python @@ -574,8 +567,6 @@ seconds (default 30) until the status is ``completed`` or ``failed``. * **SLURM** -- ``squeue -j -h -o %T``, with a file-based fallback because completed jobs leave the queue. -* **PBS** -- ``qstat -f ``, reading ``job_state``. -* **SGE** -- same shape as PBS. * **SSH** -- purely file-based: does ``result.pkl`` exist, or an error file. On ``completed``, the result path is downloaded, and then -- **before** any @@ -665,20 +656,6 @@ Backend How the flow differs of ``result.pkl``. ``slurm`` Full flow. ``sbatch job.sh``; job id is the last whitespace token of sbatch's output. -``pbs`` Same staging and environment setup as SLURM (this used to be - missing entirely), ``qsub job.pbs``, job id is the whole - trimmed stdout. Not verified against real hardware. -``sge`` ``qsub job.sge``; job id is the third token of - ``Your job 123456 ...``. Not verified against real hardware. -``kubernetes`` No SSH and **no environment replication**. A Job manifest runs - ``pip install cloudpickle dill --quiet`` in ``k8s_image`` - (default ``python:3.11-slim``) and then a single embedded - worker program. Result and signature come back as two prefixed - lines in the **pod log**, HMAC-verified with - ``CLUSTRIX_RESULT_KEY`` passed as a container env var. The - worker program is refused if it contains ``"``, ``$`` or - backtick, which the shell would reinterpret. Not verified - against a real cluster. ``huggingface`` No SSH. One ``python -c`` bootstrap runs in a container whose image defaults to ``python:-slim``. It pops ``CLUSTRIX_HMAC_KEY`` from the environment *before* pip runs, @@ -689,12 +666,14 @@ Backend How the flow differs a **private dataset repo** instead of the environment. A function that raises exits 0 -- an exception is an ordinary outcome, not a failed job. -``provider=...`` ``@cluster(provider="aws"|"gcp"|"azure"|"lambda"|"huggingface")`` - routes to ``CloudJobManager`` instead of the cluster path. - None of these has been shown to run a job end to end; see - :doc:`limitations`. =================== ================================================================== +``pbs``, ``sge``, ``kubernetes`` and the ``provider="aws"|"gcp"|"azure"|"lambda"`` +cloud VM path are **not in this table and not currently supported**. They were +removed in v0.2.0 because none of them had ever been shown to run a job end to +end. They are planned for a future release; see :ref:`removed-backends` for the +tracking issues. + Two things every backend does share: the payload produced by ``serialize_function``, and the rule that results are dill-serialized and HMAC-signed before they are trusted. diff --git a/docs/source/installation.rst b/docs/source/installation.rst index 7879b3ce..8bc8424b 100644 --- a/docs/source/installation.rst +++ b/docs/source/installation.rst @@ -34,8 +34,8 @@ Requirements - **Python 3.10 or newer** (``requires-python = ">=3.10"``). - For remote backends: SSH access to the target machine, and the scheduler's - own client tools (``sbatch``/``squeue``, ``qsub``, ...) present *on that - machine*. Nothing scheduler-specific is needed locally. + own client tools (``sbatch``/``squeue``) present *on that machine*. Nothing + scheduler-specific is needed locally. - For ``cluster_type="huggingface"``: a Hugging Face token with permission to write jobs in the namespace you target. @@ -78,37 +78,15 @@ For the interactive configuration widget and the ``%%remote`` magic: Importing ``clustrix`` registers the magic but deliberately displays nothing. Run ``%%remote`` in a cell to show the widget. -Kubernetes Support -~~~~~~~~~~~~~~~~~~ +Kubernetes and cloud provider extras +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. code-block:: bash - - pip install "clustrix[kubernetes]" - -.. warning:: - - The Kubernetes backend is implemented but has never been verified against - a real cluster. See :ref:`supported-cluster-types`. - -Cloud Provider Support -~~~~~~~~~~~~~~~~~~~~~~ - -These extras install each provider's SDK. They are what the **pricing and -cost-estimation** clients use, and those do work -- they query provider -pricing APIs and never submit a job. - -.. code-block:: bash - - pip install "clustrix[aws]" # boto3 + kubernetes - pip install "clustrix[gcp]" # google-cloud-* + kubernetes - pip install "clustrix[azure]" # azure-* + kubernetes - pip install "clustrix[cloud]" # all three - -.. warning:: - - Installing these does **not** give you a working cloud execution backend. - No AWS, GCP, Azure or Lambda Cloud job has been shown to run end to end. - See :ref:`supported-cluster-types`. +There are none, and there is nothing to install. The Kubernetes backend and +the AWS / GCP / Azure / Lambda Cloud VM backends were removed in v0.2.0 +because none of them had ever been shown to run a job end to end. The cost +monitoring and cloud pricing API went with them. They are planned for a future +release and each has a tracking issue -- see :ref:`removed-backends` for the +list and the links. Documentation ~~~~~~~~~~~~~ diff --git a/docs/source/introduction.rst b/docs/source/introduction.rst index 28062528..75870bdb 100644 --- a/docs/source/introduction.rst +++ b/docs/source/introduction.rst @@ -222,24 +222,25 @@ Do not use it if: works well for pure-Python and common scientific stacks, and it can fail for packages with heavy system-level or GPU-driver-specific builds. Pin what matters and check the first job's output. -- **You are targeting a backend that has not been verified.** See the table - below. Two of the implemented backends have never been run against real - hardware, and none of the cloud VM providers has been shown to complete a - job end to end. +- **You need PBS, SGE, Kubernetes or a cloud VM provider.** None of those is + currently supported; see :ref:`removed-backends`. .. _maturity: Backend maturity ---------------- -Clustrix is at version 0.2.0 and the backends are not equally proven. This is -tracked honestly in :ref:`supported-cluster-types` on the front page: -``slurm``, ``ssh`` and ``huggingface`` have each run a real job on real -infrastructure and returned its result; ``local`` runs in-process; ``pbs``, -``sge`` and ``kubernetes`` are implemented but have never been run against -real hardware; and the AWS / GCP / Azure / Lambda Cloud VM path is -**unverified** -- no cloud job has been shown to run end to end. Read that -table before you build on a backend. +Clustrix is at version 0.2.0 and ships exactly four backends, each of which +has been exercised against the real thing. This is tracked in +:ref:`supported-cluster-types` on the front page: ``slurm``, ``ssh`` and +``huggingface`` have each run a real job on real infrastructure and returned +its result, and ``local`` runs in-process. + +PBS, SGE, Kubernetes and the AWS / GCP / Azure / Lambda Cloud VM providers are +**not currently supported**. They were implemented but never shown to run a job +end to end, so they were removed in v0.2.0 rather than published as if they +worked. Each is planned for a future release and has a tracking issue -- +see :ref:`removed-backends`. Where to go next ---------------- diff --git a/docs/source/quickstart.rst b/docs/source/quickstart.rst index 2d2a1144..707bfb72 100644 --- a/docs/source/quickstart.rst +++ b/docs/source/quickstart.rst @@ -18,7 +18,7 @@ Install pip install clustrix Clustrix requires Python 3.10 or newer. See :doc:`installation` for the -optional extras (Jupyter widget, Kubernetes, docs). +optional extras (Jupyter widget, docs). .. _quickstart-first-result: @@ -427,11 +427,12 @@ Which backend should I use? | No machine of your own | ``huggingface`` | +----------------------------------------+---------------------------+ -Those four are the ones that have been proven to work. ``pbs``, ``sge`` and -``kubernetes`` are implemented but have never been run against real hardware, -and the cloud VM path (AWS / GCP / Azure / Lambda) is unverified -- no cloud -job has been shown to run end to end. Read :ref:`supported-cluster-types` -before you depend on any of those. +Those four are the only ``cluster_type`` values Clustrix accepts, and each one +has been proven to work against real infrastructure. ``pbs``, ``sge``, +``kubernetes`` and the cloud VM providers (AWS / GCP / Azure / Lambda Cloud) +are **not currently supported** -- they were removed in v0.2.0 because none of +them had ever been shown to run a job end to end. They are planned for a +future release; see :ref:`removed-backends` for the tracking issues. Where to go next ---------------- diff --git a/docs/source/ssh_setup.rst b/docs/source/ssh_setup.rst index c5bc2788..a2573d1d 100644 --- a/docs/source/ssh_setup.rst +++ b/docs/source/ssh_setup.rst @@ -26,8 +26,8 @@ Open the widget with the ``%%remote`` magic, in a cell of its own. Importing Then: -1. Choose a remote cluster type (``ssh``, ``slurm``, ``pbs`` or ``sge``) so the - connection section appears +1. Choose a remote cluster type (``ssh`` or ``slurm``) so the connection + section appears 2. Enter your cluster hostname (e.g. ``cluster.university.edu``) 3. Enter your username 4. Enter your password diff --git a/docs/source/troubleshooting.rst b/docs/source/troubleshooting.rst index f920173e..4f26b932 100644 --- a/docs/source/troubleshooting.rst +++ b/docs/source/troubleshooting.rst @@ -67,12 +67,8 @@ Scheduler output lands in the same directory: - Files * - SLURM - ``slurm-.out``, ``slurm-.err`` - * - PBS + * - SSH - ``job.out``, ``job.err`` - * - SGE - - ``job.out``, ``job.err`` - * - Kubernetes - - no files; read the pod log (``kubectl logs``) * - HuggingFace Jobs - no files; the job log is fetched through the API @@ -101,7 +97,7 @@ scheduler's ``.err`` file, not in ``error.pkl``. cd ~/.clustrix/jobs ls -t | head # most recent job directories first cd job_1787099351_a1b2c3d4 - cat slurm-*.err # or job.err on PBS/SGE + cat slurm-*.err # or job.err on the ssh backend cat job.sh # exactly what ran Messages you are likely to see @@ -145,8 +141,8 @@ confusing failure than this one. **Exit 127, no other output** The job died before it could write a diagnostic, almost always because -``remote_work_dir`` is not visible from the compute node. On SLURM, PBS and SGE -each node has its own ``/tmp``, so an environment built on the login node +``remote_work_dir`` is not visible from the compute node. On SLURM each node +has its own ``/tmp``, so an environment built on the login node simply is not there at run time. Use a home directory or shared scratch. The default (``~/.clustrix/jobs``) is already safe; this bites people who set ``/tmp/...`` deliberately. @@ -173,8 +169,8 @@ Two shapes of "wrong answer" are known and documented rather than mysterious: **different shapes**, because results arrive as a list of per-chunk values. See :doc:`limitations`. - Passing a keyword to ``@cluster`` that it does not recognise is accepted and - **ignored**, with a warning. ``k8s_namespace``, ``hf_namespace`` and friends - are configuration-level settings, not per-call ones. If a setting seems not + **ignored**, with a warning. ``hf_namespace`` and friends are + configuration-level settings, not per-call ones. If a setting seems not to apply, check :doc:`configuration` for whether it is read at all -- a number of fields have no effect. diff --git a/docs/source/tutorials/filesystem_tutorial.rst b/docs/source/tutorials/filesystem_tutorial.rst index d4d5bf53..25961d1c 100644 --- a/docs/source/tutorials/filesystem_tutorial.rst +++ b/docs/source/tutorials/filesystem_tutorial.rst @@ -28,7 +28,7 @@ it, and lets it go. What that operation does depends entirely on - **``cluster_type="local"``**: a plain ``os``/``glob`` call against ``config.local_work_dir`` (or the current directory). No network involved, nothing to connect or disconnect. -- **Anything else (SLURM, PBS, SGE, SSH, Kubernetes)**: an operation over +- **Anything else (SLURM, SSH)**: an operation over SFTP. The SSH connection is opened lazily, on the *first* call that needs one -- not when you construct the config -- and it applies ``config.ssh_host_key_policy`` (``"reject"`` by default; see diff --git a/docs/source/tutorials/slurm_tutorial.rst b/docs/source/tutorials/slurm_tutorial.rst index 5e61375e..96e762cd 100644 --- a/docs/source/tutorials/slurm_tutorial.rst +++ b/docs/source/tutorials/slurm_tutorial.rst @@ -13,9 +13,9 @@ Prerequisites .. note:: SLURM is verified end to end against a real cluster (SSH connect, job - submission, environment build, result retrieval). PBS and SGE - (:doc:`pbs_tutorial`, :doc:`../notebooks/sge_tutorial`) share almost all of - the same code path but have not been exercised against real hardware. + submission, environment build, result retrieval). PBS and SGE are **not + currently supported** -- they were removed in v0.2.0 and are planned for a + future release; see :ref:`removed-backends`. What Happens When You Call a ``@cluster``-Decorated Function -------------------------------------------------------------- diff --git a/docs/source/tutorials/usage_patterns.rst b/docs/source/tutorials/usage_patterns.rst index b7d48d7c..d0e5bea1 100644 --- a/docs/source/tutorials/usage_patterns.rst +++ b/docs/source/tutorials/usage_patterns.rst @@ -10,9 +10,8 @@ mocks) as part of this documentation's own test suite -- see A key fact that shapes every pattern here: **if you don't configure a remote cluster, ``@cluster`` still runs your function -- just locally, in the calling process.** ``clustrix.decorator._choose_execution_mode`` falls back to -local execution whenever ``config.cluster_host`` is unset (SLURM/PBS/SGE/SSH) -and the cluster type isn't Kubernetes-with-auto-provisioning or one of the -HTTP-API backends (currently HuggingFace Jobs). That means every example +local execution whenever ``config.cluster_host`` is unset (SLURM/SSH) and the +cluster type isn't one of the HTTP-API backends (currently HuggingFace Jobs). That means every example below runs as shown, without touching a real cluster, and the *same code* starts submitting real remote jobs once you point ``configure()`` at one. @@ -168,71 +167,39 @@ See :doc:`slurm_tutorial` and :doc:`../ssh_setup` for the two backends this project has verified end to end, and :ref:`supported-cluster-types` for what "verified" means for each backend. -Pattern 4: Kubernetes with Auto-Provisioning ------------------------------------------------ - -A common mistake is to pass ``provider=`` to ``@cluster(...)`` expecting it -to select the Kubernetes provisioner -- it doesn't; that keyword is for the -hostful cloud VM backends (Lambda Cloud, AWS, Azure, GCP). The Kubernetes -provider is a separate setting, ``k8s_provider``, and it has to be set via -``configure()`` (default: ``"aws"``): - -.. danger:: - - Calling a function under ``auto_provision_k8s=True`` **creates real cloud - infrastructure and bills you for it**. ``k8s_provider`` defaults to - ``"aws"``, so omitting it -- or calling this function before - ``configure()`` has run -- goes straight to AWS EKS and starts creating a - VPC. A reviewer copy-pasting this example with only - ``configure(cluster_type="local")`` in effect got as far as - ``CreateVpc`` -> ``VpcLimitExceeded`` against a real account. - - ``@cluster(platform=..., auto_provision=...)`` is not a per-call - override: both write straight into the global configuration - (``decorator.py``), so one decorated function can turn provisioning on - for everything that runs afterwards in the same process. - - Set ``k8s_provider="local"`` (kind/minikube, no cloud account involved) - unless you have deliberately decided to spend money. The cloud - provisioning paths are **unverified**: no clustrix job has been shown to - run end to end on any of them. - -.. code-block:: python - - # cluster-required: PROVISIONS REAL INFRASTRUCTURE. Do not run casually. - from clustrix import configure, cluster - - configure( - cluster_type="kubernetes", - auto_provision_k8s=True, - k8s_provider="local", # NOT set via @cluster(provider=...) - k8s_node_count=2, - ) - - # `platform` and `auto_provision` ARE real decorator parameters, and they - # do not merely apply to this call: they MUTATE THE GLOBAL CONFIG. - # `platform="kubernetes"` sets config.cluster_type, and - # `auto_provision=True` sets config.auto_provision_k8s -- the flag that - # causes infrastructure to be created. Both persist for every subsequent - # call in the process, not just this one. - @cluster(platform="kubernetes", auto_provision=True, cores=1, memory="512Mi") - def analyze_data(size, multiplier=1): - import math - import socket - - total = sum(math.sqrt(i * multiplier) for i in range(min(size, 1000))) - return { - "analysis_result": total, - "execution_environment": {"hostname": socket.gethostname()}, - } - - result = analyze_data(1000, 2) - print(f"Result: {result['analysis_result']}") - print(f"Executed on: {result['execution_environment']['hostname']}") - -See :doc:`kubernetes_tutorial` (the "Auto-Provisioning a Cluster" section) -for the full picture, including which of the five supported cloud providers -are unverified and which environment variables each one needs. +Pattern 4: what to do when you wanted Kubernetes or a cloud VM +--------------------------------------------------------------- + +Earlier versions of Clustrix documented a Kubernetes auto-provisioning +pattern here, plus ``@cluster(provider="aws"|"gcp"|"azure"|"lambda")`` for +cloud VMs. **None of those is currently supported.** Kubernetes, PBS, SGE and +the four cloud VM providers were removed in v0.2.0 because none of them had +ever been shown to run a job end to end, and the cost monitoring and cloud +pricing API went with them. + +They are planned for a future release, and each has a tracking issue -- +Kubernetes `#142`_, AWS `#143`_, GCP `#144`_, Azure `#145`_, Lambda Cloud +`#146`_, PBS `#140`_, SGE `#141`_. :ref:`removed-backends` has the full +table. + +In the meantime: + +* **A cloud GPU without owning hardware**: ``cluster_type="huggingface"`` + submits to HuggingFace Jobs, which runs your function in a container on + rented GPUs. It is verified end to end. (Note that this is HuggingFace + *Jobs*; the separate HuggingFace *Spaces* provider was removed too.) +* **A machine you brought up yourself**: bring up the VM through your + provider's own console or CLI, then point ``cluster_type="ssh"`` at it. + That path is verified end to end. +* **A batch allocation**: ``cluster_type="slurm"``, also verified. + +.. _#140: https://github.com/ContextLab/clustrix/issues/140 +.. _#141: https://github.com/ContextLab/clustrix/issues/141 +.. _#142: https://github.com/ContextLab/clustrix/issues/142 +.. _#143: https://github.com/ContextLab/clustrix/issues/143 +.. _#144: https://github.com/ContextLab/clustrix/issues/144 +.. _#145: https://github.com/ContextLab/clustrix/issues/145 +.. _#146: https://github.com/ContextLab/clustrix/issues/146 Key Takeaways ------------- @@ -249,5 +216,6 @@ Key Takeaways 5. **Results**: prefer returning a small dictionary with both the computed value and execution context (hostname, etc.) -- it makes it obvious whether a job actually ran remotely. -6. **Kubernetes specifically**: ``k8s_provider`` (via ``configure()``) picks - the auto-provisioning backend; ``@cluster(provider=...)`` does not. +6. **Backends**: ``local``, ``ssh``, ``slurm`` and ``huggingface`` are the + only ``cluster_type`` values Clustrix accepts. Anything else raises + ``ValueError`` at submit time -- see :ref:`removed-backends`. diff --git a/tests/real_world/test_field_mapping_validation.py b/tests/real_world/test_field_mapping_validation.py deleted file mode 100644 index 4ff5fa08..00000000 --- a/tests/real_world/test_field_mapping_validation.py +++ /dev/null @@ -1,384 +0,0 @@ -"""Real-world validation tests for cloud provider field mapping fixes. - -Tests all cloud provider connectivity methods with real API calls to verify that -field mapping between widget fields and provider API expectations works correctly. - -NO MOCK TESTS - Only real cloud provider API authentication using 1Password infrastructure. -""" - -import pytest -import logging -from typing import Dict, Any, Optional - -# Import credential manager -from .credential_manager import get_credential_manager - -# Configure logging for test debugging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def get_aws_test_credentials() -> Optional[Dict[str, Any]]: - """Get real AWS credentials for testing from 1Password.""" - manager = get_credential_manager() - aws_creds = manager.get_aws_credentials() - - if aws_creds: - # Convert to field mapping test format - return { - "aws_access_key": aws_creds["access_key_id"], - "aws_secret_key": aws_creds["secret_access_key"], - "aws_region": aws_creds.get("region", "us-east-1"), - } - return None - - -def get_gcp_test_credentials() -> Optional[Dict[str, Any]]: - """Get real GCP credentials for testing from 1Password.""" - manager = get_credential_manager() - gcp_creds = manager.get_gcp_credentials() - - if gcp_creds: - # Convert to field mapping test format - return { - "gcp_project_id": gcp_creds["project_id"], - "gcp_service_account_key": gcp_creds.get("service_account_json"), - "gcp_region": gcp_creds.get("region", "us-central1"), - } - return None - - -def get_huggingface_test_credentials() -> Optional[Dict[str, Any]]: - """Get real HuggingFace credentials for testing from 1Password.""" - manager = get_credential_manager() - hf_creds = manager.get_huggingface_credentials() - - if hf_creds: - # Convert to field mapping test format - return { - "hf_token": hf_creds["token"], - "hf_username": hf_creds.get("username"), - } - return None - - -def get_lambda_test_credentials() -> Optional[Dict[str, Any]]: - """Get real Lambda Cloud credentials for testing from 1Password.""" - manager = get_credential_manager() - lambda_creds = manager.get_lambda_cloud_credentials() - - if lambda_creds: - # Convert to field mapping test format - return { - "lambda_api_key": lambda_creds["api_key"], - } - return None - - -class TestFieldMappingValidation: - """Test cloud provider field mapping with real API calls.""" - - @pytest.mark.real_world - def test_field_mapping_system_completeness(self): - """Test that the field mapping system covers all required providers and fields.""" - from clustrix.field_mappings import ( - CLOUD_PROVIDER_FIELD_MAPPING, - REQUIRED_FIELDS, - get_supported_providers, - ) - - # Verify all expected providers are supported (excluding Azure for now) - expected_providers = ["aws", "gcp", "huggingface", "lambda"] - supported_providers = get_supported_providers() - - assert set(expected_providers).issubset( - set(supported_providers) - ), f"Missing providers in field mapping: {set(expected_providers) - set(supported_providers)}" - - # Verify required fields are defined for each provider - for provider in expected_providers: - assert ( - provider in REQUIRED_FIELDS - ), f"No required fields defined for {provider}" - assert ( - len(REQUIRED_FIELDS[provider]) > 0 - ), f"Empty required fields for {provider}" - - # Verify field mappings exist for each provider - for provider in expected_providers: - assert ( - provider in CLOUD_PROVIDER_FIELD_MAPPING - ), f"No field mapping for {provider}" - mapping = CLOUD_PROVIDER_FIELD_MAPPING[provider] - assert len(mapping) > 0, f"Empty field mapping for {provider}" - - @pytest.mark.real_world - def test_aws_field_mapping_with_real_api(self): - """Test AWS field mapping with real boto3 API authentication.""" - aws_creds = get_aws_test_credentials() - if not aws_creds: - pytest.skip( - "AWS credentials not available (set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)" - ) - - from clustrix.field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - from clustrix.notebook_magic import EnhancedClusterConfigWidget - - logger.info("Testing AWS field mapping with real credentials") - - # Test field mapping - mapped_credentials = map_widget_fields_to_provider("aws", aws_creds) - - # Verify mapping worked correctly - assert "access_key_id" in mapped_credentials - assert "secret_access_key" in mapped_credentials - assert mapped_credentials["access_key_id"] == aws_creds["aws_access_key"] - assert mapped_credentials["secret_access_key"] == aws_creds["aws_secret_key"] - - # Validate configuration - assert validate_provider_config("aws", mapped_credentials) - - # Test real API connectivity using the connectivity method - widget = EnhancedClusterConfigWidget() - result = widget._test_aws_connectivity(aws_creds) - - assert result is True, "AWS connectivity test failed with real credentials" - logger.info("✅ AWS field mapping and authentication successful") - - @pytest.mark.real_world - def test_gcp_field_mapping_with_real_api(self): - """Test GCP field mapping with real Google Cloud API authentication.""" - gcp_creds = get_gcp_test_credentials() - if not gcp_creds: - pytest.skip( - "GCP credentials not available (set GCP_PROJECT_ID, GCP_SERVICE_ACCOUNT_KEY)" - ) - - from clustrix.field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - from clustrix.notebook_magic import EnhancedClusterConfigWidget - - logger.info("Testing GCP field mapping with real credentials") - - # Test field mapping - mapped_credentials = map_widget_fields_to_provider("gcp", gcp_creds) - - # Verify mapping worked correctly - assert "project_id" in mapped_credentials - assert "service_account_key" in mapped_credentials - assert mapped_credentials["project_id"] == gcp_creds["gcp_project_id"] - assert ( - mapped_credentials["service_account_key"] - == gcp_creds["gcp_service_account_key"] - ) - - # Validate configuration - assert validate_provider_config("gcp", mapped_credentials) - - # Test real API connectivity using the connectivity method - widget = EnhancedClusterConfigWidget() - result = widget._test_gcp_connectivity(gcp_creds) - - assert result is True, "GCP connectivity test failed with real credentials" - logger.info("✅ GCP field mapping and authentication successful") - - @pytest.mark.real_world - def test_huggingface_field_mapping_with_real_api(self): - """Test HuggingFace field mapping with real HuggingFace API authentication.""" - hf_creds = get_huggingface_test_credentials() - if not hf_creds: - pytest.skip("HuggingFace credentials not available (set HF_TOKEN)") - - from clustrix.field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - from clustrix.notebook_magic import EnhancedClusterConfigWidget - - logger.info("Testing HuggingFace field mapping with real credentials") - - # Test field mapping - mapped_credentials = map_widget_fields_to_provider("huggingface", hf_creds) - - # Verify mapping worked correctly - assert "token" in mapped_credentials - assert mapped_credentials["token"] == hf_creds["hf_token"] - if hf_creds.get("hf_username"): - assert mapped_credentials["username"] == hf_creds["hf_username"] - - # Validate configuration - assert validate_provider_config("huggingface", mapped_credentials) - - # Test real API connectivity using the connectivity method - widget = EnhancedClusterConfigWidget() - result = widget._test_huggingface_connectivity(hf_creds) - - assert ( - result is True - ), "HuggingFace connectivity test failed with real credentials" - logger.info("✅ HuggingFace field mapping and authentication successful") - - @pytest.mark.real_world - def test_lambda_field_mapping_consistency(self): - """Test Lambda Cloud field mapping consistency (already working per audit).""" - from clustrix.field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - - # Test with mock Lambda credentials to verify mapping works - lambda_creds = {"lambda_api_key": "test_api_key_123"} - - # Test field mapping - mapped_credentials = map_widget_fields_to_provider("lambda", lambda_creds) - - # Verify mapping worked correctly - assert "api_key" in mapped_credentials - assert mapped_credentials["api_key"] == lambda_creds["lambda_api_key"] - - # Validate configuration structure - assert validate_provider_config("lambda", mapped_credentials) - logger.info("✅ Lambda Cloud field mapping verified (already working)") - - @pytest.mark.real_world - def test_end_to_end_widget_to_provider_flow(self): - """Test complete flow from widget input to provider authentication.""" - from clustrix.field_mappings import map_widget_fields_to_provider - from clustrix.notebook_magic import EnhancedClusterConfigWidget - - # Test scenarios for each provider where credentials are available - widget = EnhancedClusterConfigWidget() - successful_tests = [] - - # AWS test - aws_creds = get_aws_test_credentials() - if aws_creds: - logger.info("Testing end-to-end AWS flow") - try: - # Simulate widget configuration -> field mapping -> provider authentication - mapped_aws = map_widget_fields_to_provider("aws", aws_creds) - result = widget._test_aws_connectivity(aws_creds) - if result: - successful_tests.append("AWS") - logger.info("✅ End-to-end AWS flow successful") - except Exception as e: - logger.warning(f"AWS end-to-end test failed: {e}") - - # GCP test - gcp_creds = get_gcp_test_credentials() - if gcp_creds: - logger.info("Testing end-to-end GCP flow") - try: - mapped_gcp = map_widget_fields_to_provider("gcp", gcp_creds) - result = widget._test_gcp_connectivity(gcp_creds) - if result: - successful_tests.append("GCP") - logger.info("✅ End-to-end GCP flow successful") - except Exception as e: - logger.warning(f"GCP end-to-end test failed: {e}") - - # HuggingFace test - hf_creds = get_huggingface_test_credentials() - if hf_creds: - logger.info("Testing end-to-end HuggingFace flow") - try: - mapped_hf = map_widget_fields_to_provider("huggingface", hf_creds) - result = widget._test_huggingface_connectivity(hf_creds) - if result: - successful_tests.append("HuggingFace") - logger.info("✅ End-to-end HuggingFace flow successful") - except Exception as e: - logger.warning(f"HuggingFace end-to-end test failed: {e}") - - # Lambda Cloud test - lambda_creds = get_lambda_test_credentials() - if lambda_creds: - logger.info("Testing end-to-end Lambda Cloud flow") - try: - mapped_lambda = map_widget_fields_to_provider("lambda", lambda_creds) - # Note: No connectivity test for Lambda Cloud since it's read-only pricing - successful_tests.append("Lambda") - logger.info("✅ End-to-end Lambda Cloud flow successful") - except Exception as e: - logger.warning(f"Lambda Cloud end-to-end test failed: {e}") - - # Verify at least one provider worked - assert len(successful_tests) > 0, ( - "No cloud providers successfully completed end-to-end testing. " - "Please ensure 1Password contains credentials for at least one provider: AWS, GCP, HuggingFace, or Lambda Cloud" - ) - - logger.info( - f"✅ End-to-end testing successful for: {', '.join(successful_tests)}" - ) - - @pytest.mark.real_world - def test_error_handling_with_invalid_credentials(self): - """Test error handling with invalid credentials for each provider.""" - from clustrix.field_mappings import map_widget_fields_to_provider - from clustrix.notebook_magic import EnhancedClusterConfigWidget - - widget = EnhancedClusterConfigWidget() - - # Test AWS with invalid credentials - invalid_aws = { - "aws_access_key": "INVALID_ACCESS_KEY", - "aws_secret_key": "INVALID_SECRET_KEY", - "aws_region": "us-east-1", - } - - # Should map correctly but fail authentication - mapped_aws = map_widget_fields_to_provider("aws", invalid_aws) - assert "access_key_id" in mapped_aws - result = widget._test_aws_connectivity(invalid_aws) - assert result is False, "AWS should reject invalid credentials" - - # Test GCP with invalid service account - invalid_gcp = { - "gcp_project_id": "invalid-project-123", - "gcp_service_account_key": '{"type": "service_account", "private_key": "invalid"}', - } - - mapped_gcp = map_widget_fields_to_provider("gcp", invalid_gcp) - assert "project_id" in mapped_gcp - result = widget._test_gcp_connectivity(invalid_gcp) - assert result is False, "GCP should reject invalid service account" - - # Test HuggingFace with invalid token - invalid_hf = {"hf_token": "hf_invalid_token_12345"} - - mapped_hf = map_widget_fields_to_provider("huggingface", invalid_hf) - assert "token" in mapped_hf - result = widget._test_huggingface_connectivity(invalid_hf) - assert result is False, "HuggingFace should reject invalid token" - - logger.info("✅ Error handling with invalid credentials working correctly") - - @pytest.mark.real_world - def test_missing_required_fields_validation(self): - """Test validation with missing required fields for each provider.""" - from clustrix.field_mappings import map_widget_fields_to_provider - - # Test AWS missing secret key - with pytest.raises(KeyError, match="Missing required aws fields"): - map_widget_fields_to_provider("aws", {"aws_access_key": "test"}) - - # Test GCP missing service account key - with pytest.raises(KeyError, match="Missing required gcp fields"): - map_widget_fields_to_provider("gcp", {"gcp_project_id": "test"}) - - # Test HuggingFace missing token - with pytest.raises(KeyError, match="Missing required huggingface fields"): - map_widget_fields_to_provider("huggingface", {"hf_username": "test"}) - - logger.info("✅ Missing required fields validation working correctly") - - -if __name__ == "__main__": - # Run tests directly for debugging - pytest.main([__file__, "-v", "--tb=short"]) From 315e747f5539cf7fca65e5d865dd37ff3a129f0c Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:33:07 -0400 Subject: [PATCH 07/56] Remove unverified backends from the legacy notebook widget Drops the Kubernetes/AWS/Azure/GCP/Lambda UI sections, credential fields, region/instance-type population and connectivity tests, the cost-monitoring checkbox, and the pbs/sge branches. The cluster-type dropdown now offers exactly local, ssh, slurm and huggingface, and the HuggingFace section targets HF Jobs rather than Spaces. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/notebook_magic_config.py | 90 +-- clustrix/notebook_magic_widget.py | 932 +----------------------------- 2 files changed, 30 insertions(+), 992 deletions(-) diff --git a/clustrix/notebook_magic_config.py b/clustrix/notebook_magic_config.py index 8eddeb0b..b61842e8 100644 --- a/clustrix/notebook_magic_config.py +++ b/clustrix/notebook_magic_config.py @@ -27,14 +27,6 @@ "default_cores": -1, # Use all available cores "default_memory": "16GB", }, - "Local Kubernetes": { - "cluster_type": "kubernetes", - "k8s_namespace": "default", - "k8s_image": "python:3.11", - "default_cores": 2, - "default_memory": "4GB", - "package_manager": "pip", - }, "University SLURM Cluster": { "cluster_type": "slurm", "cluster_host": "login.hpc.university.edu", @@ -45,26 +37,6 @@ "remote_work_dir": "/scratch/your_username/clustrix", "package_manager": "conda", }, - "Corporate PBS Cluster": { - "cluster_type": "pbs", - "cluster_host": "hpc.company.com", - "username": "employee_id", - "default_cores": 8, - "default_memory": "32GB", - "default_time": "02:00:00", - "remote_work_dir": "/home/employee_id/clustrix", - "package_manager": "pip", - }, - "SGE Research Cluster": { - "cluster_type": "sge", - "cluster_host": "submit.research.org", - "username": "researcher", - "default_cores": 24, - "default_memory": "128GB", - "default_time": "04:00:00", - "remote_work_dir": "/data/researcher/clustrix", - "package_manager": "conda", - }, "SSH Remote Server": { "cluster_type": "ssh", "cluster_host": "remote.server.com", @@ -75,70 +47,12 @@ "remote_work_dir": "~/.clustrix/jobs", "package_manager": "pip", }, - # Cloud Provider Configurations - "AWS EC2 Cluster": { - "cluster_type": "aws", - "aws_region": "us-east-1", - "aws_instance_type": "t3.medium", - "aws_cluster_type": "ec2", - "default_cores": 2, - "default_memory": "4GB", - "remote_work_dir": "/home/ec2-user/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - }, - "AWS EKS Cluster": { - "cluster_type": "aws", - "aws_region": "us-east-1", - "aws_instance_type": "t3.medium", - "aws_cluster_type": "eks", - "k8s_namespace": "default", - "k8s_image": "python:3.11", - "default_cores": 2, - "default_memory": "4GB", - "package_manager": "pip", - "cost_monitoring": True, - }, - "Azure VM Cluster": { - "cluster_type": "azure", - "azure_region": "eastus", - "azure_instance_type": "Standard_D2s_v3", - "default_cores": 2, - "default_memory": "8GB", - "remote_work_dir": "/home/azureuser/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - }, - "Google Cloud VM": { - "cluster_type": "gcp", - "gcp_region": "us-central1", - "gcp_instance_type": "e2-medium", - "default_cores": 2, - "default_memory": "4GB", - "remote_work_dir": "/home/ubuntu/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - }, - "Lambda Cloud GPU": { - "cluster_type": "lambda_cloud", - "lambda_instance_type": "gpu_1x_a10", - "default_cores": 8, - "default_memory": "32GB", - "remote_work_dir": "/home/ubuntu/clustrix", - "package_manager": "conda", - "cost_monitoring": True, - "environment_variables": { - "CUDA_VISIBLE_DEVICES": "0", - "NVIDIA_VISIBLE_DEVICES": "all", - }, - }, - "HuggingFace Space": { - "cluster_type": "huggingface_spaces", + "HuggingFace Jobs": { + "cluster_type": "huggingface", "hf_hardware": "cpu-basic", "hf_sdk": "gradio", "default_cores": 2, "default_memory": "16GB", - "cost_monitoring": True, }, } diff --git a/clustrix/notebook_magic_widget.py b/clustrix/notebook_magic_widget.py index 452cbbfa..0859e7db 100644 --- a/clustrix/notebook_magic_widget.py +++ b/clustrix/notebook_magic_widget.py @@ -102,19 +102,12 @@ def _create_widgets(self): "local", "ssh", "slurm", - "pbs", - "sge", - "kubernetes", - "aws", - "azure", - "gcp", - "lambda_cloud", - "huggingface_spaces", + "huggingface", ], description="Cluster Type:", tooltip=( "Choose where to run your jobs: local machine, remote servers " - "(SSH/SLURM/PBS/SGE), Kubernetes clusters, or cloud providers" + "(SSH/SLURM), or HuggingFace Jobs" ), style=style, layout=full_layout, @@ -128,7 +121,7 @@ def _create_widgets(self): placeholder="Enter configuration name", tooltip=( "Give this configuration a descriptive name " - "(e.g., 'AWS Production', 'Local Testing', 'HPC Cluster')" + "(e.g., 'GPU Server', 'Local Testing', 'HPC Cluster')" ), style=style, layout=full_layout, @@ -305,253 +298,7 @@ def _create_dynamic_fields(self): style=style, layout=half_layout, ) - # Kubernetes specific fields - self.k8s_namespace_field = widgets.Text( - description="K8s Namespace:", - value="default", - placeholder="Kubernetes namespace", - tooltip="Kubernetes namespace to deploy jobs in", - style=style, - layout=half_layout, - ) - self.k8s_image_field = widgets.Text( - description="Container Image:", - value="python:3.11", - placeholder="e.g., python:3.11, ubuntu:20.04", - tooltip="Docker image to use for job containers", - style=style, - layout=full_layout, - ) - # Kubernetes remote checkbox - self.k8s_remote_checkbox = widgets.Checkbox( - value=False, - description="Remote Kubernetes Cluster", - tooltip="Check if this is a remote Kubernetes cluster (requires SSH)", - style={"description_width": "160px"}, - layout=widgets.Layout(width="300px"), - ) - self.k8s_remote_checkbox.observe(self._on_k8s_remote_change, names="value") - # Cloud provider specific fields - # AWS fields - self.aws_region_field = widgets.Dropdown( - options=[ - "us-east-1", - "us-east-2", - "us-west-1", - "us-west-2", - "eu-west-1", - "eu-west-2", - "eu-central-1", - "ap-southeast-1", - "ap-southeast-2", - "ap-northeast-1", - ], - value="us-east-1", - description="AWS Region:", - tooltip="AWS region for your resources", - style=style, - layout=half_layout, - ) - self.aws_region_field.observe(self._on_aws_region_change, names="value") - self.aws_instance_type_field = widgets.Dropdown( - options=[ - "t3.micro", - "t3.small", - "t3.medium", - "t3.large", - "t3.xlarge", - "m5.large", - "m5.xlarge", - "m5.2xlarge", - "c5.large", - "c5.xlarge", - "c5.2xlarge", - "r5.large", - "r5.xlarge", - "r5.2xlarge", - ], - value="t3.medium", - description="Instance Type:", - tooltip="AWS EC2 instance type", - style=style, - layout=half_layout, - ) - self.aws_cluster_type_field = widgets.Dropdown( - options=["ec2", "eks", "batch"], - value="ec2", - description="AWS Service:", - tooltip="AWS service to use (EC2, EKS, or Batch)", - style=style, - layout=half_layout, - ) - self.aws_access_key_field = widgets.Text( - description="Access Key ID:", - placeholder="Your AWS access key ID", - tooltip="AWS Access Key ID for authentication", - style=style, - layout=full_layout, - ) - self.aws_secret_key_field = widgets.Text( - description="Secret Key:", - placeholder="Your AWS secret access key", - tooltip="AWS Secret Access Key for authentication", - style=style, - layout=full_layout, - ) - # Azure fields - self.azure_region_field = widgets.Dropdown( - options=[ - "eastus", - "eastus2", - "westus", - "westus2", - "centralus", - "northeurope", - "westeurope", - "eastasia", - "southeastasia", - "japaneast", - ], - value="eastus", - description="Azure Region:", - tooltip="Azure region for your resources", - style=style, - layout=half_layout, - ) - self.azure_region_field.observe(self._on_azure_region_change, names="value") - self.azure_instance_type_field = widgets.Dropdown( - options=[ - "Standard_B1s", - "Standard_B2s", - "Standard_D2s_v3", - "Standard_D4s_v3", - "Standard_D8s_v3", - "Standard_E2s_v3", - "Standard_E4s_v3", - "Standard_E8s_v3", - "Standard_F2s_v2", - "Standard_F4s_v2", - ], - value="Standard_D2s_v3", - description="VM Size:", - tooltip="Azure virtual machine size", - style=style, - layout=half_layout, - ) - self.azure_subscription_field = widgets.Text( - description="Subscription ID:", - placeholder="Your Azure subscription ID", - tooltip="Azure subscription ID", - style=style, - layout=full_layout, - ) - self.azure_client_id_field = widgets.Text( - description="Client ID:", - placeholder="Service principal client ID", - tooltip="Azure service principal client ID", - style=style, - layout=half_layout, - ) - self.azure_client_secret_field = widgets.Text( - description="Client Secret:", - placeholder="Service principal client secret", - tooltip="Azure service principal client secret", - style=style, - layout=half_layout, - ) - self.azure_tenant_id_field = widgets.Text( - description="Tenant ID:", - placeholder="Azure tenant ID", - tooltip="Azure Active Directory tenant ID", - style=style, - layout=full_layout, - ) - # GCP fields - self.gcp_region_field = widgets.Dropdown( - options=[ - "us-central1", - "us-east1", - "us-west1", - "us-west2", - "europe-west1", - "europe-west2", - "europe-west3", - "asia-east1", - "asia-southeast1", - "asia-northeast1", - ], - value="us-central1", - description="GCP Region:", - tooltip="Google Cloud region", - style=style, - layout=half_layout, - ) - self.gcp_region_field.observe(self._on_gcp_region_change, names="value") - self.gcp_instance_type_field = widgets.Dropdown( - options=[ - "e2-micro", - "e2-small", - "e2-medium", - "e2-standard-2", - "e2-standard-4", - "n1-standard-1", - "n1-standard-2", - "n1-standard-4", - "n2-standard-2", - "n2-standard-4", - ], - value="e2-medium", - description="Machine Type:", - tooltip="Google Cloud machine type", - style=style, - layout=half_layout, - ) - self.gcp_project_field = widgets.Text( - description="Project ID:", - placeholder="Your GCP project ID", - tooltip="Google Cloud project ID", - style=style, - layout=half_layout, - ) - self.gcp_zone_field = widgets.Text( - description="Zone:", - placeholder="e.g., us-central1-a", - tooltip="Google Cloud zone within the region", - style=style, - layout=half_layout, - ) - self.gcp_credentials_field = widgets.Textarea( - description="Service Account:", - placeholder="Paste JSON service account key here", - tooltip="Google Cloud service account JSON key", - rows=5, - style=style, - layout=full_layout, - ) - # Lambda Cloud fields - self.lambda_api_key_field = widgets.Text( - description="API Key:", - placeholder="Your Lambda Cloud API key", - tooltip="Lambda Cloud API key for authentication", - style=style, - layout=full_layout, - ) - self.lambda_instance_type_field = widgets.Dropdown( - options=[ - "gpu_1x_a10", - "gpu_1x_a100", - "gpu_2x_a100", - "gpu_4x_a100", - "gpu_8x_a100", - "gpu_1x_v100", - ], - value="gpu_1x_a10", - description="Instance Type:", - tooltip="Lambda Cloud instance type", - style=style, - layout=half_layout, - ) - # HuggingFace fields + # HuggingFace Jobs fields self.hf_token_field = widgets.Text( description="HF Token:", placeholder="Your HuggingFace access token", @@ -559,13 +306,6 @@ def _create_dynamic_fields(self): style=style, layout=full_layout, ) - self.hf_space_name_field = widgets.Text( - description="Space Name:", - placeholder="e.g., my-awesome-space", - tooltip="Name of the HuggingFace Space to create", - style=style, - layout=half_layout, - ) self.hf_hardware_field = widgets.Dropdown( options=[ "cpu-basic", @@ -578,7 +318,7 @@ def _create_dynamic_fields(self): ], value="cpu-basic", description="Hardware:", - tooltip="HuggingFace Space hardware tier", + tooltip="HuggingFace Jobs hardware flavor", style=style, layout=half_layout, ) @@ -586,7 +326,7 @@ def _create_dynamic_fields(self): options=["gradio", "streamlit", "static"], value="gradio", description="SDK:", - tooltip="HuggingFace Space SDK to use", + tooltip="HuggingFace SDK to use", style=style, layout=half_layout, ) @@ -604,14 +344,6 @@ def _create_advanced_options(self): style=style, layout=widgets.Layout(width="48%"), ) - # Cost monitoring checkbox - self.cost_monitoring_checkbox = widgets.Checkbox( - value=False, - description="Cost Monitoring", - tooltip="Enable cost tracking for cloud providers", - style=style, - layout=widgets.Layout(width="48%"), - ) # Environment variables self.env_vars_field = widgets.Textarea( description="Environment Vars:", @@ -740,7 +472,6 @@ def _setup_change_tracking(self): self.password_field, self.port_field, self.package_manager, - self.cost_monitoring_checkbox, self.env_vars_field, self.module_loads_field, self.pre_exec_commands_field, @@ -780,87 +511,11 @@ def _create_section_containers(self): display="none", ), ) - # Kubernetes fields - self.kubernetes_fields = widgets.VBox( - [ - widgets.HTML("
Kubernetes Settings
"), - self.k8s_remote_checkbox, - widgets.HBox([self.k8s_namespace_field, widgets.HTML("")]), - self.k8s_image_field, - ], - layout=widgets.Layout( - border="1px solid #ddd", - padding="10px", - margin="10px 0px", - display="none", - ), - ) - # Cloud provider fields containers - self.aws_fields = widgets.VBox( - [ - widgets.HTML("
AWS Settings
"), - widgets.HBox([self.aws_region_field, self.aws_instance_type_field]), - widgets.HBox([self.aws_cluster_type_field, widgets.HTML("")]), - self.aws_access_key_field, - self.aws_secret_key_field, - ], - layout=widgets.Layout( - border="1px solid #ddd", - padding="10px", - margin="10px 0px", - display="none", - ), - ) - self.azure_fields = widgets.VBox( - [ - widgets.HTML("
Azure Settings
"), - widgets.HBox([self.azure_region_field, self.azure_instance_type_field]), - self.azure_subscription_field, - widgets.HBox( - [self.azure_client_id_field, self.azure_client_secret_field] - ), - self.azure_tenant_id_field, - ], - layout=widgets.Layout( - border="1px solid #ddd", - padding="10px", - margin="10px 0px", - display="none", - ), - ) - self.gcp_fields = widgets.VBox( - [ - widgets.HTML("
Google Cloud Settings
"), - widgets.HBox([self.gcp_region_field, self.gcp_instance_type_field]), - widgets.HBox([self.gcp_project_field, self.gcp_zone_field]), - self.gcp_credentials_field, - ], - layout=widgets.Layout( - border="1px solid #ddd", - padding="10px", - margin="10px 0px", - display="none", - ), - ) - self.lambda_fields = widgets.VBox( - [ - widgets.HTML("
Lambda Cloud Settings
"), - self.lambda_api_key_field, - widgets.HBox([self.lambda_instance_type_field, widgets.HTML("")]), - ], - layout=widgets.Layout( - border="1px solid #ddd", - padding="10px", - margin="10px 0px", - display="none", - ), - ) self.hf_fields = widgets.VBox( [ - widgets.HTML("
HuggingFace Settings
"), + widgets.HTML("
HuggingFace Jobs Settings
"), self.hf_token_field, - widgets.HBox([self.hf_space_name_field, self.hf_hardware_field]), - widgets.HBox([self.hf_sdk_field, widgets.HTML("")]), + widgets.HBox([self.hf_hardware_field, self.hf_sdk_field]), ], layout=widgets.Layout( border="1px solid #ddd", @@ -885,36 +540,16 @@ def _on_cluster_type_change(self, change): # Hide all sections first self.connection_fields.layout.display = "none" - self.kubernetes_fields.layout.display = "none" - self.aws_fields.layout.display = "none" - self.azure_fields.layout.display = "none" - self.gcp_fields.layout.display = "none" - self.lambda_fields.layout.display = "none" self.hf_fields.layout.display = "none" # Show relevant sections based on cluster type - if cluster_type in ["ssh", "slurm", "pbs", "sge"]: + if cluster_type in ["ssh", "slurm"]: self.connection_fields.layout.display = "" - elif cluster_type == "kubernetes": - self.kubernetes_fields.layout.display = "" - self._update_kubernetes_connection_visibility() - elif cluster_type == "aws": - self.aws_fields.layout.display = "" - # Populate AWS options if available - self._populate_cloud_provider_options("aws") - elif cluster_type == "azure": - self.azure_fields.layout.display = "" - self._populate_cloud_provider_options("azure") - elif cluster_type == "gcp": - self.gcp_fields.layout.display = "" - self._populate_cloud_provider_options("gcp") - elif cluster_type == "lambda_cloud": - self.lambda_fields.layout.display = "" - elif cluster_type == "huggingface_spaces": + elif cluster_type == "huggingface": self.hf_fields.layout.display = "" # Update time field visibility (only for cluster schedulers) - if cluster_type in ["slurm", "pbs", "sge"]: + if cluster_type == "slurm": self.time_field.layout.display = "" else: self.time_field.layout.display = "none" @@ -928,165 +563,6 @@ def _on_cluster_type_change(self, change): # Mark as changed self._mark_unsaved_changes() - def _on_k8s_remote_change(self, change): - """Handle remote Kubernetes checkbox change.""" - self._update_kubernetes_connection_visibility() - - def _update_kubernetes_connection_visibility(self): - """Update connection fields visibility for Kubernetes clusters.""" - if self.cluster_type.value == "kubernetes": - if self.k8s_remote_checkbox.value: - self.connection_fields.layout.display = "" - else: - self.connection_fields.layout.display = "none" - - def _populate_cloud_provider_options(self, provider: str): - """Populate region and instance type options for the specified cloud provider.""" - try: - from .cloud_providers import PROVIDERS - - # Get the provider class - provider_class = PROVIDERS.get(provider) - if provider_class is None: - # Fallback to default options - self._set_default_cloud_options(provider) - return - - # Initialize the provider - provider_instance = provider_class() - - if provider == "aws": - # Get AWS regions and instance types - regions = provider_instance.get_available_regions() - if regions: - self.aws_region_field.options = regions - - instance_types = provider_instance.get_available_instance_types() - if instance_types: - self.aws_instance_type_field.options = instance_types - - elif provider == "azure": - # Get Azure regions and VM sizes - regions = provider_instance.get_available_regions() - if regions: - self.azure_region_field.options = regions - - vm_sizes = provider_instance.get_available_instance_types() - if vm_sizes: - self.azure_instance_type_field.options = vm_sizes - - elif provider == "gcp": - # Get GCP regions and machine types - regions = provider_instance.get_available_regions() - if regions: - self.gcp_region_field.options = regions - - machine_types = provider_instance.get_available_instance_types() - if machine_types: - self.gcp_instance_type_field.options = machine_types - - except Exception as e: - # If cloud provider API is unavailable, use default options - logger.warning(f"Could not load {provider} options: {e}") - self._set_default_cloud_options(provider) - - def _set_default_cloud_options(self, provider: str): - """Set default options when cloud provider API is not available.""" - defaults = { - "aws": { - "regions": ["us-east-1", "us-west-1", "us-west-2", "eu-west-1"], - "instances": [ - "t3.micro", - "t3.small", - "t3.medium", - "t3.large", - "m5.large", - "c5.large", - "r5.large", - ], - }, - "azure": { - "regions": ["eastus", "westus", "northeurope", "westeurope"], - "instances": [ - "Standard_B1s", - "Standard_B2s", - "Standard_D2s_v3", - "Standard_D4s_v3", - "Standard_E2s_v3", - "Standard_F2s_v2", - ], - }, - "gcp": { - "regions": ["us-central1", "us-east1", "europe-west1", "asia-east1"], - "instances": [ - "e2-micro", - "e2-small", - "e2-medium", - "e2-standard-2", - "n1-standard-1", - "n2-standard-2", - ], - }, - } - - if provider in defaults: - config = defaults[provider] - if provider == "aws": - self.aws_region_field.options = config["regions"] - self.aws_instance_type_field.options = config["instances"] - elif provider == "azure": - self.azure_region_field.options = config["regions"] - self.azure_instance_type_field.options = config["instances"] - elif provider == "gcp": - self.gcp_region_field.options = config["regions"] - self.gcp_instance_type_field.options = config["instances"] - - def _on_aws_region_change(self, change): - """Handle AWS region change to update available instance types.""" - try: - from .cloud_providers import PROVIDERS - - provider_class = PROVIDERS.get("aws") - if provider_class: - provider_instance = provider_class() - instance_types = provider_instance.get_instance_types( - region=change["new"] - ) - if instance_types: - self.aws_instance_type_field.options = instance_types - except Exception: - pass # Keep current options - - def _on_azure_region_change(self, change): - """Handle Azure region change to update available instance types.""" - try: - from .cloud_providers import PROVIDERS - - provider_class = PROVIDERS.get("azure") - if provider_class: - provider_instance = provider_class() - vm_sizes = provider_instance.get_vm_sizes(region=change["new"]) - if vm_sizes: - self.azure_instance_type_field.options = vm_sizes - except Exception: - pass # Keep current options - - def _on_gcp_region_change(self, change): - """Handle GCP region change to update available instance types.""" - try: - from .cloud_providers import PROVIDERS - - provider_class = PROVIDERS.get("gcp") - if provider_class: - provider_instance = provider_class() - machine_types = provider_instance.get_machine_types( - region=change["new"] - ) - if machine_types: - self.gcp_instance_type_field.options = machine_types - except Exception: - pass # Keep current options - @staticmethod def _set_choice(field, value): """Select a value in a dropdown, widening the options if need be. @@ -1097,8 +573,8 @@ def _set_choice(field, value): TraitError: Invalid selection: value not found - and broke the widget outright. AWS alone has far more than ten regions, - so this was reachable with a perfectly ordinary config file. + and broke the widget outright. New hardware flavors appear faster than + the hardcoded list, so this was reachable with an ordinary config file. The saved configuration is authoritative -- a list baked into the UI should not be able to veto it -- so an unrecognised value is added to @@ -1131,58 +607,13 @@ def _load_config_to_widgets(self, config_name: str): self.password_field.value = config.get("password", "") self.port_field.value = config.get("cluster_port", 22) - # Kubernetes fields - self.k8s_namespace_field.value = config.get("k8s_namespace", "default") - self.k8s_image_field.value = config.get("k8s_image", "python:3.11") - self.k8s_remote_checkbox.value = config.get("k8s_remote", False) - - # AWS fields - self._set_choice(self.aws_region_field, config.get("aws_region", "us-east-1")) - self._set_choice( - self.aws_instance_type_field, config.get("aws_instance_type", "t3.medium") - ) - self._set_choice( - self.aws_cluster_type_field, config.get("aws_cluster_type", "ec2") - ) - self.aws_access_key_field.value = config.get("aws_access_key_id", "") - self.aws_secret_key_field.value = config.get("aws_secret_access_key", "") - - # Azure fields - self._set_choice(self.azure_region_field, config.get("azure_region", "eastus")) - self._set_choice( - self.azure_instance_type_field, - config.get("azure_instance_type", "Standard_D2s_v3"), - ) - self.azure_subscription_field.value = config.get("azure_subscription_id", "") - self.azure_client_id_field.value = config.get("azure_client_id", "") - self.azure_client_secret_field.value = config.get("azure_client_secret", "") - self.azure_tenant_id_field.value = config.get("azure_tenant_id", "") - - # GCP fields - self._set_choice(self.gcp_region_field, config.get("gcp_region", "us-central1")) - self._set_choice( - self.gcp_instance_type_field, config.get("gcp_instance_type", "e2-medium") - ) - self.gcp_project_field.value = config.get("gcp_project", "") - self.gcp_zone_field.value = config.get("gcp_zone", "") - self.gcp_credentials_field.value = config.get("gcp_credentials", "") - - # Lambda Cloud fields - self.lambda_api_key_field.value = config.get("lambda_api_key", "") - self._set_choice( - self.lambda_instance_type_field, - config.get("lambda_instance_type", "gpu_1x_a10"), - ) - - # HuggingFace fields + # HuggingFace Jobs fields self.hf_token_field.value = config.get("hf_token", "") - self.hf_space_name_field.value = config.get("hf_space_name", "") self._set_choice(self.hf_hardware_field, config.get("hf_hardware", "cpu-basic")) self._set_choice(self.hf_sdk_field, config.get("hf_sdk", "gradio")) # Advanced options self.package_manager.value = config.get("package_manager", "pip") - self.cost_monitoring_checkbox.value = config.get("cost_monitoring", False) # Environment variables env_vars = config.get("environment_variables", {}) @@ -1220,7 +651,6 @@ def _save_config_from_widgets(self) -> Dict[str, Any]: "username": self.username_field.value, "cluster_port": self.port_field.value, "package_manager": self.package_manager.value, - "cost_monitoring": self.cost_monitoring_checkbox.value, "queue": self.queue_field.value, "ssh_key_path": self.ssh_key_field.value, } @@ -1229,77 +659,8 @@ def _save_config_from_widgets(self) -> Dict[str, Any]: if self.password_field.value: config["password"] = self.password_field.value - # Kubernetes specific fields - if self.cluster_type.value == "kubernetes": - config.update( - { - "k8s_namespace": self.k8s_namespace_field.value, - "k8s_image": self.k8s_image_field.value, - "k8s_remote": self.k8s_remote_checkbox.value, - } - ) - - # AWS specific fields - elif self.cluster_type.value == "aws": - config.update( - { - "aws_region": self.aws_region_field.value, - "aws_instance_type": self.aws_instance_type_field.value, - "aws_cluster_type": self.aws_cluster_type_field.value, - } - ) - # Include credentials only if provided - if self.aws_access_key_field.value: - config["aws_access_key_id"] = self.aws_access_key_field.value - if self.aws_secret_key_field.value: - config["aws_secret_access_key"] = self.aws_secret_key_field.value - - # Azure specific fields - elif self.cluster_type.value == "azure": - config.update( - { - "azure_region": self.azure_region_field.value, - "azure_instance_type": self.azure_instance_type_field.value, - } - ) - # Include credentials only if provided - if self.azure_subscription_field.value: - config["azure_subscription_id"] = self.azure_subscription_field.value - if self.azure_client_id_field.value: - config["azure_client_id"] = self.azure_client_id_field.value - if self.azure_client_secret_field.value: - config["azure_client_secret"] = self.azure_client_secret_field.value - if self.azure_tenant_id_field.value: - config["azure_tenant_id"] = self.azure_tenant_id_field.value - - # GCP specific fields - elif self.cluster_type.value == "gcp": - config.update( - { - "gcp_region": self.gcp_region_field.value, - "gcp_instance_type": self.gcp_instance_type_field.value, - } - ) - # Include credentials only if provided - if self.gcp_project_field.value: - config["gcp_project"] = self.gcp_project_field.value - if self.gcp_zone_field.value: - config["gcp_zone"] = self.gcp_zone_field.value - if self.gcp_credentials_field.value: - config["gcp_credentials"] = self.gcp_credentials_field.value - - # Lambda Cloud specific fields - elif self.cluster_type.value == "lambda_cloud": - config.update( - { - "lambda_instance_type": self.lambda_instance_type_field.value, - } - ) - if self.lambda_api_key_field.value: - config["lambda_api_key"] = self.lambda_api_key_field.value - - # HuggingFace specific fields - elif self.cluster_type.value == "huggingface_spaces": + # HuggingFace Jobs specific fields + if self.cluster_type.value == "huggingface": config.update( { "hf_hardware": self.hf_hardware_field.value, @@ -1308,8 +669,6 @@ def _save_config_from_widgets(self) -> Dict[str, Any]: ) if self.hf_token_field.value: config["hf_token"] = self.hf_token_field.value - if self.hf_space_name_field.value: - config["hf_space_name"] = self.hf_space_name_field.value # Environment variables if self.env_vars_field.value.strip(): @@ -1632,218 +991,15 @@ def _test_ssh_connectivity(self, config, timeout=10): except Exception as e: return False, str(e) - def _test_cloud_connectivity(self, cluster_type, config): - """Test cloud provider API connectivity.""" - try: - if cluster_type == "aws": - return self._test_aws_connectivity(config) - elif cluster_type == "azure": - return self._test_azure_connectivity(config) - elif cluster_type == "gcp": - return self._test_gcp_connectivity(config) - elif cluster_type == "lambda_cloud": - return self._test_lambda_connectivity(config) - elif cluster_type == "huggingface_spaces": - return self._test_huggingface_connectivity(config) - else: - return False, f"Cloud testing not implemented for {cluster_type}" - except Exception as e: - return False, f"Cloud connectivity test failed: {str(e)}" - - def _test_aws_connectivity(self, config): - """Test AWS API connectivity with proper field mapping.""" - try: - import boto3 # type: ignore - from botocore.exceptions import NoCredentialsError, ClientError # type: ignore - from .field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - - # Map widget fields to provider fields - provider_config = map_widget_fields_to_provider(config, "aws") - - # Validate configuration - is_valid, missing_fields = validate_provider_config(provider_config, "aws") - if not is_valid: - return False, f"Missing required fields: {', '.join(missing_fields)}" - - # Create boto3 session - session_params = {} - if provider_config.get("aws_access_key_id"): - session_params["aws_access_key_id"] = provider_config[ - "aws_access_key_id" - ] - if provider_config.get("aws_secret_access_key"): - session_params["aws_secret_access_key"] = provider_config[ - "aws_secret_access_key" - ] - - session = boto3.Session(**session_params) - - # Test EC2 connectivity - region = provider_config.get("region", "us-east-1") - ec2_client = session.client("ec2", region_name=region) - - # Try to describe regions (basic API call) - response = ec2_client.describe_regions() - if response.get("Regions"): - return True, "AWS connectivity successful" - else: - return False, "No AWS regions returned" - - except ImportError: - return False, "boto3 not installed. Run: pip install boto3" - except NoCredentialsError: - return False, "AWS credentials not configured" - except ClientError as e: - error_code = e.response.get("Error", {}).get("Code", "Unknown") - if error_code == "UnauthorizedOperation": - return ( - True, - "AWS credentials valid (got authorization error on describe_regions)", - ) - else: - return False, f"AWS API error: {error_code}" - except Exception as e: - return False, f"AWS connectivity failed: {str(e)}" - - def _test_azure_connectivity(self, config): - """Test Azure API connectivity with proper field mapping.""" - try: - from azure.identity import ClientSecretCredential - from .field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - - # Map widget fields to provider fields - provider_config = map_widget_fields_to_provider(config, "azure") - - # Validate configuration - is_valid, missing_fields = validate_provider_config( - provider_config, "azure" - ) - if not is_valid: - return False, f"Missing required fields: {', '.join(missing_fields)}" - - # Create credentials - credential = ClientSecretCredential( - tenant_id=provider_config["tenant_id"], - client_id=provider_config["client_id"], - client_secret=provider_config["client_secret"], - ) - - # Test token acquisition - token = credential.get_token("https://management.azure.com/.default") - if token and token.token: - return True, "Azure connectivity successful" - else: - return False, "Could not acquire Azure token" - - except ImportError: - return ( - False, - "Azure SDK not installed. Run: pip install azure-identity azure-mgmt-compute", - ) - except Exception as e: - return False, f"Azure connectivity failed: {str(e)}" - - def _test_gcp_connectivity(self, config): - """Test GCP API connectivity with proper field mapping.""" - try: - import json - from google.cloud import resourcemanager - from google.oauth2 import service_account - from .field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - - # Map widget fields to provider fields - provider_config = map_widget_fields_to_provider(config, "gcp") - - # Validate configuration - is_valid, missing_fields = validate_provider_config(provider_config, "gcp") - if not is_valid: - return False, f"Missing required fields: {', '.join(missing_fields)}" - - # Parse credentials - credentials_json = provider_config.get("credentials_json") - if not credentials_json: - return False, "No GCP credentials provided" - - try: - creds_dict = json.loads(credentials_json) - except json.JSONDecodeError: - return False, "Invalid JSON in GCP credentials" - - # Create credentials object - credentials = service_account.Credentials.from_service_account_info( - creds_dict - ) - - # Test connectivity - client = resourcemanager.Client(credentials=credentials) - - # Try to list projects (basic API call) - projects = list(client.list_projects()) - return True, f"GCP connectivity successful (found {len(projects)} projects)" - - except ImportError: - return ( - False, - "Google Cloud SDK not installed. Run: pip install google-cloud-resource-manager", - ) - except Exception as e: - return False, f"GCP connectivity failed: {str(e)}" - - def _test_lambda_connectivity(self, config): - """Test Lambda Cloud API connectivity.""" - try: - from .cloud_providers.lambda_cloud import LambdaCloudProvider - - api_key = config.get("lambda_api_key") - if not api_key: - return False, "Lambda Cloud API key not provided" - - provider = LambdaCloudProvider(api_key=api_key) - instances = provider.list_instances() - - return ( - True, - f"Lambda Cloud connectivity successful ({len(instances)} instances)", - ) - - except ImportError: - return False, "Lambda Cloud provider not available" - except Exception as e: - return False, f"Lambda Cloud connectivity failed: {str(e)}" - def _test_huggingface_connectivity(self, config): - """Test HuggingFace API connectivity with proper field mapping.""" + """Test HuggingFace Jobs API connectivity.""" try: - from .field_mappings import ( - map_widget_fields_to_provider, - validate_provider_config, - ) - - # Map widget fields to provider fields - provider_config = map_widget_fields_to_provider( - config, "huggingface_spaces" - ) - - # Validate configuration - is_valid, missing_fields = validate_provider_config( - provider_config, "huggingface_spaces" - ) - if not is_valid: - return False, f"Missing required fields: {', '.join(missing_fields)}" - - # Test HuggingFace API import requests - token = provider_config.get("token") + token = config.get("hf_token") + if not token: + return False, "Missing required fields: hf_token" + headers = {"Authorization": f"Bearer {token}"} # Test API connectivity by getting user info @@ -1878,7 +1034,7 @@ def _on_test_config(self, button): print("✅ Local configuration - no connectivity test needed") print("💡 Tip: Use cores=-1 to use all available CPU cores") - elif cluster_type in ["ssh", "slurm", "pbs", "sge"]: + elif cluster_type in ["ssh", "slurm"]: # Test SSH-based clusters host = config_data.get("cluster_host") port = config_data.get("cluster_port", 22) @@ -1921,37 +1077,10 @@ def _on_test_config(self, button): else: print("⚠️ Username not provided - skipping SSH test") - elif cluster_type == "kubernetes": - k8s_remote = config_data.get("k8s_remote", False) - if k8s_remote: - # Test SSH connectivity for remote K8s - host = config_data.get("cluster_host") - if host: - print( - f"🌐 Testing connectivity to remote Kubernetes at {host}..." - ) - # Same SSH tests as above - port = config_data.get("cluster_port", 22) - if not self._test_remote_connectivity(host, port): - print(f"❌ Cannot reach {host}:{port}") - return - print("✅ Remote Kubernetes connectivity successful") - else: - print("❌ Host required for remote Kubernetes") - else: - print("✅ Local Kubernetes configuration") - print("💡 Ensure kubectl is configured for your cluster") - - elif cluster_type in [ - "aws", - "azure", - "gcp", - "lambda_cloud", - "huggingface_spaces", - ]: - # Test cloud provider connectivity - print(f"☁️ Testing {cluster_type.upper()} API connectivity...") - result = self._test_cloud_connectivity(cluster_type, config_data) + elif cluster_type == "huggingface": + # Test HuggingFace Jobs API connectivity + print("☁️ Testing HuggingFace Jobs API connectivity...") + result = self._test_huggingface_connectivity(config_data) if isinstance(result, tuple): success, message = result @@ -1984,7 +1113,7 @@ def _on_setup_ssh_keys(self, button): config_data = self._save_config_from_widgets() cluster_type = config_data.get("cluster_type", "local") - if cluster_type not in ["ssh", "slurm", "pbs", "sge", "kubernetes"]: + if cluster_type not in ["ssh", "slurm"]: print(f"❌ SSH key setup not applicable for {cluster_type}") return @@ -2059,18 +1188,13 @@ def display(self): dynamic_sections = widgets.VBox( [ self.connection_fields, - self.kubernetes_fields, - self.aws_fields, - self.azure_fields, - self.gcp_fields, - self.lambda_fields, self.hf_fields, ] ) # Advanced options accordion advanced_content = widgets.VBox( [ - widgets.HBox([self.package_manager, self.cost_monitoring_checkbox]), + widgets.HBox([self.package_manager, widgets.HTML("")]), self.env_vars_field, self.module_loads_field, self.pre_exec_commands_field, From 9566745be6d36222971a16b0c45d33f3c6684618 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:36:03 -0400 Subject: [PATCH 08/56] Remove the Kubernetes section from the modern notebook widget Drops the k8s_* controls, their BACKEND_ONLY_FIELDS entry and their WIDGET_MANAGED_FIELDS entries, and narrows the pbs/sge conditionals to ssh/slurm. The cluster-type dropdown is driven by SUPPORTED_CLUSTER_TYPES, so it follows config.py without a change here. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/modern_notebook_widget.py | 92 ++---------------------------- 1 file changed, 6 insertions(+), 86 deletions(-) diff --git a/clustrix/modern_notebook_widget.py b/clustrix/modern_notebook_widget.py index 58046671..5a1de94f 100644 --- a/clustrix/modern_notebook_widget.py +++ b/clustrix/modern_notebook_widget.py @@ -61,10 +61,6 @@ "password_env_var", "use_env_password", "remote_work_dir", - "k8s_namespace", - "k8s_image", - "k8s_service_account", - "k8s_pull_policy", "hf_namespace", "hf_flavor", "hf_token", @@ -1112,61 +1108,6 @@ def _create_remote_section(self) -> None: ) self.widgets["hf_section"].add_class("clustrix-section") - # Kubernetes configuration. Like HuggingFace Jobs this reaches its - # compute over an API rather than SSH, so it needs a namespace and an - # image rather than a host and a key file. None of these had fields - # before, so only the shipped defaults were ever usable. - self.widgets["k8s_namespace"] = widgets.Text( - value="default", - placeholder="default", - layout=widgets.Layout(width="100%", height="26px"), - ) - self.widgets["k8s_image"] = widgets.Text( - value="python:3.11-slim", - placeholder="python:3.12-slim", - layout=widgets.Layout(width="100%", height="26px"), - ) - self.widgets["k8s_service_account"] = widgets.Text( - value="", - placeholder="(cluster default)", - layout=widgets.Layout(width="100%", height="26px"), - ) - self.widgets["k8s_pull_policy"] = widgets.Dropdown( - options=["IfNotPresent", "Always", "Never"], - value="IfNotPresent", - layout=widgets.Layout(width="100%", height="26px"), - ) - - k8s_row1 = widgets.HBox( - [ - self._field("Namespace", self.widgets["k8s_namespace"], flex="1 1 0"), - self._field("Image", self.widgets["k8s_image"], flex="2 1 0"), - ], - layout=widgets.Layout(width="100%", align_items="flex-end"), - ) - k8s_row1.add_class("clustrix-row") - - k8s_row2 = widgets.HBox( - [ - self._field( - "Service account", - self.widgets["k8s_service_account"], - flex="2 1 0", - ), - self._field( - "Image pull policy", self.widgets["k8s_pull_policy"], flex="1 1 0" - ), - ], - layout=widgets.Layout(width="100%", align_items="flex-end"), - ) - k8s_row2.add_class("clustrix-row") - - self.widgets["k8s_section"] = widgets.VBox( - [self._section_heading("Kubernetes"), k8s_row1, k8s_row2], - layout=widgets.Layout(display="none", width="100%"), - ) - self.widgets["k8s_section"].add_class("clustrix-section") - self.widgets["remote_section"] = widgets.VBox( [ self._section_heading("Connection"), @@ -1393,14 +1334,11 @@ def _update_ui_for_cluster_type(self) -> None: # _create_remote_section; rebuilding them here used to splice in a # second "Advanced settings" button beside the one in the actions row # and drop every field after the third. - remote = cluster_type in ["ssh", "slurm", "pbs", "sge"] + remote = cluster_type in ["ssh", "slurm"] self.widgets["remote_section"].layout.display = "block" if remote else "none" self.widgets["hf_section"].layout.display = ( "block" if cluster_type == "huggingface" else "none" ) - self.widgets["k8s_section"].layout.display = ( - "block" if cluster_type == "kubernetes" else "none" - ) def get_widget(self) -> "widgets.Widget": """Get the complete widget for display. @@ -1416,7 +1354,6 @@ def get_widget(self) -> "widgets.Widget": self.widgets["grid_row3"], # Resources self.widgets["remote_section"], # Connection self.widgets["hf_section"], # HuggingFace Jobs - self.widgets["k8s_section"], # Kubernetes self.widgets["grid_row4"], # Actions self.widgets["advanced_section"], self._output_panel(), @@ -1822,7 +1759,7 @@ def _on_test_connect(self, button): print(f" User: {getattr(config, 'username', 'N/A')}") # For remote clusters, test authentication - if config.cluster_type in ["ssh", "slurm", "pbs", "sge"]: + if config.cluster_type in ["ssh", "slurm"]: print(" Testing authentication...") # Initialize auth manager with config @@ -2151,7 +2088,7 @@ def _validate_widget_values(self) -> List[str]: "(or D-HH:MM:SS)" ) - if cluster_type in ("ssh", "slurm", "pbs", "sge"): + if cluster_type in ("ssh", "slurm"): if not str(self.widgets["host"].value).strip(): problems.append(f"A host is required for a {cluster_type} cluster") if not str(self.widgets["username"].value).strip(): @@ -2175,7 +2112,7 @@ def _validate_widget_values(self) -> List[str]: #: _choose_execution_mode routes on cluster_host, so a leftover host would #: send a "local" job to a cluster. BACKEND_ONLY_FIELDS = { - ("ssh", "slurm", "pbs", "sge"): ( + ("ssh", "slurm"): ( "cluster_host", "cluster_port", "username", @@ -2185,12 +2122,6 @@ def _validate_widget_values(self) -> List[str]: "use_env_password", "remote_work_dir", ), - ("kubernetes",): ( - "k8s_namespace", - "k8s_image", - "k8s_service_account", - "k8s_pull_policy", - ), ("huggingface",): ( "hf_namespace", "hf_flavor", @@ -2267,11 +2198,6 @@ def _config_data_from_widgets(self) -> Dict[str, Any]: self.widgets["home_dir"].value.strip() or ClusterConfig().remote_work_dir ), - # Kubernetes - "k8s_namespace": self.widgets["k8s_namespace"].value or "default", - "k8s_image": self.widgets["k8s_image"].value or "python:3.11-slim", - "k8s_service_account": self.widgets["k8s_service_account"].value or None, - "k8s_pull_policy": self.widgets["k8s_pull_policy"].value, # HuggingFace "hf_namespace": self.widgets["hf_namespace"].value or None, "hf_flavor": self.widgets["hf_flavor"].value, @@ -2299,8 +2225,8 @@ def _load_config_to_widgets(self, config: ClusterConfig) -> None: This must mirror `_get_config_from_widgets` field for field, including resetting a control to its default when the config does not set it. - It used to restore only a subset -- no HuggingFace or Kubernetes - settings, no remote work directory, no password -- and to leave + It used to restore only a subset -- no HuggingFace settings, no + remote work directory, no password -- and to leave environment variables and modules untouched when the incoming config had none. Combined with saving the visible state on the way out, that meant clicking through the profile dropdown overwrote each profile with @@ -2330,12 +2256,6 @@ def _load_config_to_widgets(self, config: ClusterConfig) -> None: self.widgets["local_env_var"].value = config.password_env_var or "" self.widgets["home_dir"].value = config.remote_work_dir or "" - # Kubernetes - self.widgets["k8s_namespace"].value = config.k8s_namespace or "default" - self.widgets["k8s_image"].value = config.k8s_image or "python:3.11-slim" - self.widgets["k8s_service_account"].value = config.k8s_service_account or "" - self.widgets["k8s_pull_policy"].value = config.k8s_pull_policy or "IfNotPresent" - # HuggingFace self.widgets["hf_namespace"].value = config.hf_namespace or "" self.widgets["hf_flavor"].value = config.hf_flavor or "cpu-basic" From 9a05081c65f84264d0527801197ca896ba373cf2 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:36:29 -0400 Subject: [PATCH 09/56] Remove unverified backends from the core execution path The package imports again. Removed from the modules this commit owns: - executor.py: the KubernetesJobManager and CloudJobManager re-exports, which pointed at modules already deleted. - config.py: every k8s_*, aws_*, azure_*, gcp_*, lambda_*, cloud_* and cost_monitoring field (79 lines); the auto_install cloud-dependency hook in __post_init__ and in configure(), along with configure()'s auto_install_deps parameter. - config.py: SUPPORTED_CLUSTER_TYPES is now ("local", "ssh", "slurm", "huggingface"). - decorator.py: the provider/instance_type/region parameters and the seven Kubernetes auto-provisioning ones, the k8s readiness branches in both the sync and async paths, and the k8s auto-provisioning check in _choose_execution_mode. The kwargs passthrough list keeps only the names a surviving backend reads. - utils.py: _create_pbs_script and _create_sge_script and their dispatch; normalize_memory's kubernetes/pbs/sge targets. - cli_credentials.py: the AWS, Azure, GCP, Kubernetes and Lambda Cloud collectors and validators, and their entries in the setup wizard and the credential test loop (883 -> 497 lines). Two things deliberately kept: hf_hardware, hf_username and hf_sdk look like HuggingFace *Spaces* fields and sit under a comment that said so, but hf_jobs.py reads them as fallbacks for hf_flavor and hf_namespace. They stay; the comment is corrected. New in config.py: REMOVED_CLUSTER_TYPES and _REMOVED_SETTINGS, checked in load_config before the difflib path. Without them an existing clustrix.yml carrying k8s_namespace gets "did you mean ...?" pointed at an unrelated field, and cluster_type: pbs gets no explanation at all. Now each names the backend and its tracking issue (#140-#146). Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/__init__.py | 18 - clustrix/cli_credentials.py | 449 +----------------- clustrix/config.py | 198 +++----- clustrix/decorator.py | 131 +---- clustrix/executor.py | 10 +- clustrix/utils.py | 108 +---- .../_static/img/screenshots/widget_gcp.png | Bin 112716 -> 0 bytes .../_static/img/screenshots/widget_lambda.png | Bin 148569 -> 0 bytes 8 files changed, 97 insertions(+), 817 deletions(-) delete mode 100644 docs/source/_static/img/screenshots/widget_gcp.png delete mode 100644 docs/source/_static/img/screenshots/widget_lambda.png diff --git a/clustrix/__init__.py b/clustrix/__init__.py index c54baec0..78f7d3c4 100644 --- a/clustrix/__init__.py +++ b/clustrix/__init__.py @@ -11,16 +11,6 @@ list_ssh_keys, add_host_key, ) -from .cost_monitoring import ( - cost_tracking_decorator, - get_cost_monitor, - start_cost_monitoring, - generate_cost_report, - get_pricing_info, - ResourceUsage, - CostEstimate, - CostReport, -) from .filesystem import ( ClusterFilesystem, FileInfo, @@ -78,14 +68,6 @@ "find_ssh_keys", "list_ssh_keys", "add_host_key", - "cost_tracking_decorator", - "get_cost_monitor", - "start_cost_monitoring", - "generate_cost_report", - "get_pricing_info", - "ResourceUsage", - "CostEstimate", - "CostReport", "ClusterFilesystem", "FileInfo", "DiskUsage", diff --git a/clustrix/cli_credentials.py b/clustrix/cli_credentials.py index 9eed0c11..f023ea5e 100644 --- a/clustrix/cli_credentials.py +++ b/clustrix/cli_credentials.py @@ -51,43 +51,16 @@ def setup_credentials_interactive(): print("\n🔧 Select providers to configure:") - if click.confirm("Configure AWS credentials (for EC2, Batch, pricing APIs)?"): - aws_creds = _collect_aws_credentials_interactive() - if aws_creds: - credentials_to_add.update(aws_creds) - - if click.confirm("Configure SSH cluster access (for SLURM, PBS, SGE)?"): + if click.confirm("Configure SSH cluster access (for SLURM and SSH)?"): ssh_creds = _collect_ssh_credentials_interactive() if ssh_creds: credentials_to_add.update(ssh_creds) - if click.confirm("Configure Azure credentials (for Azure VM, ACI)?"): - azure_creds = _collect_azure_credentials_interactive() - if azure_creds: - credentials_to_add.update(azure_creds) - - if click.confirm( - "Configure Google Cloud credentials (for GCP Compute, pricing APIs)?" - ): - gcp_creds = _collect_gcp_credentials_interactive() - if gcp_creds: - credentials_to_add.update(gcp_creds) - - if click.confirm("Configure Kubernetes credentials (for K8s job execution)?"): - k8s_creds = _collect_kubernetes_credentials_interactive() - if k8s_creds: - credentials_to_add.update(k8s_creds) - - if click.confirm("Configure HuggingFace credentials (for HF Spaces)?"): + if click.confirm("Configure HuggingFace credentials (for HuggingFace Jobs)?"): hf_creds = _collect_huggingface_credentials_interactive() if hf_creds: credentials_to_add.update(hf_creds) - if click.confirm("Configure Lambda Cloud credentials (for GPU instances)?"): - lambda_creds = _collect_lambda_cloud_credentials_interactive() - if lambda_creds: - credentials_to_add.update(lambda_creds) - if not credentials_to_add: print("No credentials configured. You can add them manually by editing:") print(f" {manager.env_file}") @@ -107,37 +80,10 @@ def setup_credentials_interactive(): return False -def _collect_aws_credentials_interactive() -> Dict[str, str]: - """Collect and validate AWS credentials interactively.""" - print("\n🔑 AWS Credential Setup") - print("You can find these in the AWS Console > IAM > Access Keys") - - try: - access_key = click.prompt("AWS Access Key ID", type=str) - secret_key = click.prompt("AWS Secret Access Key", type=str, hide_input=True) - region = click.prompt("AWS Region", default="us-east-1", type=str) - - # Validate credentials with real AWS API call - print("🔍 Validating AWS credentials...") - if _validate_aws_credentials_real(access_key, secret_key, region): - print("✅ AWS credentials validated successfully") - return { - "AWS_ACCESS_KEY_ID": access_key, - "AWS_SECRET_ACCESS_KEY": secret_key, - "AWS_REGION": region, - } - else: - print("❌ AWS credential validation failed") - return {} - except click.Abort: - print("AWS credential setup cancelled") - return {} - - def _collect_ssh_credentials_interactive() -> Dict[str, str]: """Collect and validate SSH credentials interactively.""" print("\n🔑 SSH Cluster Credential Setup") - print("For accessing SLURM, PBS, or SGE clusters via SSH") + print("For accessing SLURM or plain SSH clusters") try: host = click.prompt("SSH Host (e.g., cluster.university.edu)", type=str) @@ -183,117 +129,6 @@ def _collect_ssh_credentials_interactive() -> Dict[str, str]: return {} -def _collect_azure_credentials_interactive() -> Dict[str, str]: - """Collect Azure credentials interactively.""" - print("\n🔑 Azure Credential Setup") - print("You can find these in Azure Portal > App Registrations") - - try: - subscription_id = click.prompt("Azure Subscription ID", type=str) - tenant_id = click.prompt("Azure Tenant ID", type=str) - client_id = click.prompt("Azure Client ID (Application ID)", type=str) - client_secret = click.prompt("Azure Client Secret", type=str, hide_input=True) - - credentials = { - "AZURE_SUBSCRIPTION_ID": subscription_id, - "AZURE_TENANT_ID": tenant_id, - "AZURE_CLIENT_ID": client_id, - "AZURE_CLIENT_SECRET": client_secret, - } - - # Validate credentials - print("🔍 Validating Azure credentials...") - if _validate_azure_credentials_real(credentials): - print("✅ Azure credentials validated successfully") - return credentials - else: - print("❌ Azure credential validation failed") - return {} - except click.Abort: - print("Azure credential setup cancelled") - return {} - - -def _collect_gcp_credentials_interactive() -> Dict[str, str]: - """Collect GCP credentials interactively.""" - print("\n🔑 Google Cloud Credential Setup") - - try: - project_id = click.prompt("GCP Project ID", type=str) - - auth_method = click.prompt( - "Authentication method", - type=click.Choice(["service_account_file", "service_account_json"]), - default="service_account_file", - ) - - credentials = {"GCP_PROJECT_ID": project_id} - - if auth_method == "service_account_file": - key_path = click.prompt("Service Account Key File Path", type=str) - if Path(key_path).exists(): - credentials["GOOGLE_APPLICATION_CREDENTIALS"] = key_path - else: - print(f"❌ Service account key file not found at {key_path}") - return {} - else: - print("Paste your service account JSON (Ctrl+D when done):") - json_content = sys.stdin.read().strip() - if json_content: - credentials["GCP_SERVICE_ACCOUNT_JSON"] = json_content - else: - print("❌ No service account JSON provided") - return {} - - # Validate credentials - print("🔍 Validating GCP credentials...") - if _validate_gcp_credentials_real(credentials): - print("✅ GCP credentials validated successfully") - return credentials - else: - print("❌ GCP credential validation failed") - return {} - except click.Abort: - print("GCP credential setup cancelled") - return {} - - -def _collect_kubernetes_credentials_interactive() -> Dict[str, str]: - """Collect Kubernetes credentials interactively.""" - print("\n🔑 Kubernetes Credential Setup") - - try: - default_kubeconfig = str(Path.home() / ".kube" / "config") - kubeconfig_path = click.prompt( - "Kubeconfig file path", default=default_kubeconfig, type=str - ) - - if not Path(kubeconfig_path).exists(): - print(f"❌ Kubeconfig file not found at {kubeconfig_path}") - return {} - - credentials = {"KUBECONFIG": kubeconfig_path} - - namespace = click.prompt("Kubernetes namespace", default="default", type=str) - credentials["K8S_NAMESPACE"] = namespace - - context = click.prompt("Kubernetes context (optional)", default="", type=str) - if context: - credentials["K8S_CONTEXT"] = context - - # Validate Kubernetes access - print("🔍 Validating Kubernetes access...") - if _validate_kubernetes_credentials_real(credentials): - print("✅ Kubernetes credentials validated successfully") - return credentials - else: - print("❌ Kubernetes credential validation failed") - return {} - except click.Abort: - print("Kubernetes credential setup cancelled") - return {} - - def _collect_huggingface_credentials_interactive() -> Dict[str, str]: """Collect HuggingFace credentials interactively.""" print("\n🔑 HuggingFace Credential Setup") @@ -320,69 +155,6 @@ def _collect_huggingface_credentials_interactive() -> Dict[str, str]: return {} -def _collect_lambda_cloud_credentials_interactive() -> Dict[str, str]: - """Collect Lambda Cloud credentials interactively.""" - print("\n🔑 Lambda Cloud Credential Setup") - print("You can find your API key in Lambda Cloud console") - - try: - api_key = click.prompt("Lambda Cloud API Key", type=str, hide_input=True) - - credentials = { - "LAMBDA_CLOUD_API_KEY": api_key, - "LAMBDA_CLOUD_ENDPOINT": "https://cloud.lambdalabs.com/api/v1", - } - - # Validate credentials - print("🔍 Validating Lambda Cloud credentials...") - if _validate_lambda_cloud_credentials_real(credentials): - print("✅ Lambda Cloud credentials validated successfully") - return credentials - else: - print("❌ Lambda Cloud credential validation failed") - return {} - except click.Abort: - print("Lambda Cloud credential setup cancelled") - return {} - - -# Real credential validation functions (NO MOCKS) - - -def _validate_aws_credentials_real( - access_key: str, secret_key: str, region: str -) -> bool: - """Validate AWS credentials using real AWS STS API call.""" - try: - import boto3 - from botocore.exceptions import ClientError, NoCredentialsError - - # Create STS client with provided credentials - sts_client = boto3.client( - "sts", - aws_access_key_id=access_key, - aws_secret_access_key=secret_key, - region_name=region, - ) - - # Make real API call to get caller identity - response = sts_client.get_caller_identity() - - # If we get here, credentials are valid - logger.info(f"AWS credentials validated for account: {response.get('Account')}") - return True - - except (ClientError, NoCredentialsError) as e: - logger.debug(f"AWS credential validation failed: {e}") - return False - except ImportError: - logger.warning("boto3 not available for AWS validation") - return False # Conservative: require validation - except Exception as e: - logger.debug(f"AWS validation error: {e}") - return False - - def _validate_ssh_credentials_real(credentials: Dict[str, str]) -> bool: """Validate SSH credentials using real SSH connection attempt.""" try: @@ -442,137 +214,6 @@ def _validate_ssh_credentials_real(credentials: Dict[str, str]) -> bool: return False -def _validate_azure_credentials_real(credentials: Dict[str, str]) -> bool: - """Validate Azure credentials using real Azure API call.""" - try: - from azure.identity import ClientSecretCredential - from azure.mgmt.resource import ResourceManagementClient - from azure.core.exceptions import ClientAuthenticationError - - # Create credential object - credential = ClientSecretCredential( - tenant_id=credentials["AZURE_TENANT_ID"], - client_id=credentials["AZURE_CLIENT_ID"], - client_secret=credentials["AZURE_CLIENT_SECRET"], - ) - - # Create resource management client - resource_client = ResourceManagementClient( - credential, credentials["AZURE_SUBSCRIPTION_ID"] - ) - - # Make real API call to list resource groups (validates credentials) - list(resource_client.resource_groups.list()) - - logger.info( - f"Azure credentials validated for subscription: {credentials['AZURE_SUBSCRIPTION_ID']}" - ) - return True - - except ClientAuthenticationError as e: - logger.debug(f"Azure authentication failed: {e}") - return False - except ImportError: - logger.warning("Azure SDK not available for Azure validation") - return False # Conservative: require validation - except Exception as e: - logger.debug(f"Azure validation error: {e}") - return False - - -def _validate_gcp_credentials_real(credentials: Dict[str, str]) -> bool: - """Validate GCP credentials using real Google Cloud API call.""" - try: - # Try different GCP client libraries - try: - from google.cloud import resource_manager - - client_type = "resource_manager" - except ImportError: - try: - from google.cloud import compute_v1 - - client_type = "compute" - except ImportError: - logger.warning("Google Cloud SDK not available for GCP validation") - return False - - from google.auth.exceptions import DefaultCredentialsError - import tempfile - - # Set up authentication - if "service_account_json" in credentials: - # Use service account JSON - with tempfile.NamedTemporaryFile( - mode="w", delete=False, suffix=".json" - ) as f: - f.write(credentials["service_account_json"]) - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = f.name - elif "service_account_path" in credentials: - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials[ - "service_account_path" - ] - else: - return False - - # Make real API call based on available client - if client_type == "resource_manager": - client = resource_manager.Client() - client.fetch_project(credentials["project_id"]) # Validates credentials - elif client_type == "compute": - client = compute_v1.ZonesClient() - list( - client.list(project=credentials["project_id"]) - ) # Validates credentials - - logger.info( - f"GCP credentials validated for project: {credentials['project_id']}" - ) - return True - - except DefaultCredentialsError as e: - logger.debug(f"GCP authentication failed: {e}") - return False - except ImportError: - logger.warning("Google Cloud SDK not available for GCP validation") - return False # Conservative: require validation - except Exception as e: - logger.debug(f"GCP validation error: {e}") - return False - - -def _validate_kubernetes_credentials_real(credentials: Dict[str, str]) -> bool: - """Validate Kubernetes credentials using real kubectl command.""" - try: - # Set KUBECONFIG environment variable - env = os.environ.copy() - env["KUBECONFIG"] = credentials["KUBECONFIG"] - - # Test kubectl access with real command - cmd = ["kubectl", "get", "namespaces"] - if "K8S_CONTEXT" in credentials: - cmd.extend(["--context", credentials["K8S_CONTEXT"]]) - - result = subprocess.run( - cmd, env=env, capture_output=True, text=True, timeout=10 - ) - - success = result.returncode == 0 - if success: - logger.info("Kubernetes credentials validated") - return success - - except subprocess.TimeoutExpired: - logger.debug("Kubernetes validation timed out") - return False - except FileNotFoundError: - logger.warning("kubectl not available for Kubernetes validation") - return False # Conservative: require validation - except Exception as e: - logger.debug(f"Kubernetes validation error: {e}") - return False - - def _validate_huggingface_credentials_real(credentials: Dict[str, str]) -> bool: """Validate HuggingFace credentials using real HF API call.""" try: @@ -596,33 +237,6 @@ def _validate_huggingface_credentials_real(credentials: Dict[str, str]) -> bool: return False -def _validate_lambda_cloud_credentials_real(credentials: Dict[str, str]) -> bool: - """Validate Lambda Cloud credentials using real API call.""" - try: - import requests - - # Make real API call to Lambda Cloud - headers = { - "Authorization": f"Bearer {credentials['LAMBDA_CLOUD_API_KEY']}", - "Content-Type": "application/json", - } - - response = requests.get( - f"{credentials['LAMBDA_CLOUD_ENDPOINT']}/instance-types", - headers=headers, - timeout=10, - ) - - success = response.status_code == 200 - if success: - logger.info("Lambda Cloud credentials validated") - return success - - except Exception as e: - logger.debug(f"Lambda Cloud validation error: {e}") - return False - - def _write_credentials_to_env_file(env_file: Path, credentials: Dict[str, str]) -> bool: """Write credentials to .env file with atomic operation and secure permissions.""" try: @@ -721,13 +335,8 @@ def test_credentials_command(): # Test each provider providers_to_test = [ - "aws", - "azure", - "gcp", "ssh", - "kubernetes", "huggingface", - "lambda_cloud", ] for provider in providers_to_test: @@ -739,45 +348,7 @@ def test_credentials_command(): continue # Test with real validation - if provider == "aws": - required_keys = ["access_key_id", "secret_access_key"] - if all(key in credentials for key in required_keys): - success = _validate_aws_credentials_real( - credentials["access_key_id"], - credentials["secret_access_key"], - credentials.get("region", "us-east-1"), - ) - else: - print( - f" ❌ Missing required AWS credentials: {[k for k in required_keys if k not in credentials]}" - ) - continue - elif provider == "azure": - required_keys = [ - "subscription_id", - "tenant_id", - "client_id", - "client_secret", - ] - if all(key in credentials for key in required_keys): - success = _validate_azure_credentials_real(credentials) - else: - print( - f" ❌ Missing required Azure credentials: {[k for k in required_keys if k not in credentials]}" - ) - continue - elif provider == "gcp": - if "project_id" in credentials and ( - "service_account_path" in credentials - or "service_account_json" in credentials - ): - success = _validate_gcp_credentials_real(credentials) - else: - print( - " ❌ Missing required GCP credentials (need project_id + service account)" - ) - continue - elif provider == "ssh": + if provider == "ssh": required_keys = ["host", "username"] if all(key in credentials for key in required_keys) and ( "password" in credentials or "private_key_path" in credentials @@ -788,24 +359,12 @@ def test_credentials_command(): " ❌ Missing required SSH credentials (need host, username, and password or private_key_path)" ) continue - elif provider == "kubernetes": - if "kubeconfig_path" in credentials or "kubeconfig_content" in credentials: - success = _validate_kubernetes_credentials_real(credentials) - else: - print(" ❌ Missing required Kubernetes credentials (need kubeconfig)") - continue elif provider == "huggingface": if "token" in credentials: success = _validate_huggingface_credentials_real(credentials) else: print(" ❌ Missing required HuggingFace credentials (need token)") continue - elif provider == "lambda_cloud": - if "api_key" in credentials: - success = _validate_lambda_cloud_credentials_real(credentials) - else: - print(" ❌ Missing required Lambda Cloud credentials (need api_key)") - continue else: success = True # Unknown provider, assume valid diff --git a/clustrix/config.py b/clustrix/config.py index cfd05e82..cc461151 100644 --- a/clustrix/config.py +++ b/clustrix/config.py @@ -28,88 +28,9 @@ class ClusterConfig: cluster_host: Optional[str] = None cluster_port: int = 22 - # Kubernetes-specific settings - k8s_namespace: str = "default" - k8s_image: str = "python:3.11-slim" - k8s_service_account: Optional[str] = None - k8s_pull_policy: str = "IfNotPresent" - k8s_job_ttl_seconds: int = 3600 - k8s_backoff_limit: int = 3 - k8s_remote: bool = False - - # Cloud provider settings for remote Kubernetes - cloud_provider: str = "manual" # manual, aws, azure, gcp - cloud_region: Optional[str] = None - cloud_auto_configure: bool = False - - # NEW: Kubernetes auto-provisioning settings - auto_provision_k8s: bool = False - k8s_provider: str = "aws" # aws, gcp, azure, huggingface, lambda - k8s_from_scratch: bool = True # Always provision infrastructure - k8s_auto_cleanup: bool = True - k8s_cluster_name: Optional[str] = None - - # NEW: Cluster specifications (provider-specific defaults) - k8s_node_count: int = 2 - k8s_node_type: Optional[str] = None # t3.medium, e2-standard-4, etc. - k8s_version: str = "1.28" - k8s_region: Optional[str] = None - - # AWS-specific settings - # NOTE: Both standard boto3 and widget field names are supported for backward compatibility - # Field mapping is handled automatically via clustrix.field_mappings module - aws_profile: Optional[str] = None - aws_access_key_id: Optional[str] = None # Standard boto3 field name - aws_secret_access_key: Optional[str] = None # Standard boto3 field name - aws_access_key: Optional[str] = ( - None # Widget field name (mapped to aws_access_key_id) - ) - aws_secret_key: Optional[str] = ( - None # Widget field name (mapped to aws_secret_access_key) - ) - aws_session_token: Optional[str] = None # For temporary credentials - aws_instance_type: Optional[str] = None - aws_cluster_type: Optional[str] = None # ec2 or eks - eks_cluster_name: Optional[str] = None - aws_region: Optional[str] = None - - # Azure-specific settings - # NOTE: Field names match widget naming scheme (azure_* prefix) - # Mapped to Azure SDK field names via clustrix.field_mappings module - azure_subscription_id: Optional[str] = None # Required for authentication - azure_resource_group: Optional[str] = None - azure_tenant_id: Optional[str] = ( - None # Required for service principal authentication - ) - azure_client_id: Optional[str] = ( - None # Required for service principal authentication - ) - azure_client_secret: Optional[str] = ( - None # Required for service principal authentication - ) - azure_instance_type: Optional[str] = None - aks_cluster_name: Optional[str] = None - azure_region: Optional[str] = None - - # GCP-specific settings - # NOTE: Field names match widget naming scheme (gcp_* prefix) - # Mapped to Google Cloud SDK field names via clustrix.field_mappings module - gcp_project_id: Optional[str] = None # Required for authentication - gcp_zone: Optional[str] = None - gcp_service_account_key: Optional[str] = None # Required: JSON service account key - gcp_instance_type: Optional[str] = None - gke_cluster_name: Optional[str] = None - gcp_region: Optional[str] = None - - # Lambda Cloud settings - # NOTE: Field names match widget naming scheme (lambda_* prefix) - # Mapped to Lambda Cloud API field names via clustrix.field_mappings module - lambda_instance_type: Optional[str] = None - lambda_api_key: Optional[str] = None # Required for authentication - - # Hugging Face Spaces settings - # NOTE: Field names match widget naming scheme (hf_* prefix) - # Mapped to HuggingFace API field names via clustrix.field_mappings module + # HuggingFace Jobs settings. hf_hardware/hf_username/hf_sdk are the + # older widget-facing spellings; hf_jobs.py still reads them as fallbacks + # for hf_flavor/hf_namespace, so they are kept. hf_hardware: Optional[str] = None hf_token: Optional[str] = None # Required for authentication hf_username: Optional[str] = None @@ -166,9 +87,6 @@ class ClusterConfig: ssh_connect_timeout: int = 30 venv_setup_timeout: int = 300 # Timeout for venv setup in seconds (5 minutes) - # Monitoring settings - cost_monitoring: bool = False # Enable cost monitoring for cloud providers - # Enhanced Authentication Options use_env_password: bool = False # Enable environment variable password password_env_var: str = "" # Name of environment variable containing password @@ -264,24 +182,6 @@ def __post_init__(self): f"(insecure, trusts unknown host keys automatically)." ) - # Auto-install cloud provider dependencies if needed - self._ensure_cloud_dependencies() - - def _ensure_cloud_dependencies(self) -> None: - """Ensure cloud provider dependencies are available for this configuration.""" - try: - from .auto_install import ensure_cloud_provider_dependencies - - ensure_cloud_provider_dependencies( - cluster_type=self.cluster_type, - cloud_provider=self.cloud_provider, - auto_install=True, - quiet=True, # Quiet in constructor to avoid spam - ) - except Exception: - # Silently fail in constructor to avoid breaking imports - pass - def get_env_password(self) -> Optional[str]: """Get password from specified environment variable.""" if self.use_env_password and self.password_env_var: @@ -344,12 +244,56 @@ def load_from_file(cls, config_path: str) -> "ClusterConfig": "local", "ssh", "slurm", - "pbs", - "sge", - "kubernetes", "huggingface", ) +#: Backends clustrix used to carry code for and no longer implements, mapped +#: to the issue tracking their return. Every one of them was removed for the +#: same reason: it had never been run against real hardware, so nothing +#: justified the claim that it worked. Keeping the names here is what lets a +#: user with an older ``clustrix.yml`` get an answer instead of a guess -- +#: without it, ``cluster_type: pbs`` and a stale ``k8s_namespace`` key both +#: come back through ``difflib`` pointed at some unrelated field. +REMOVED_CLUSTER_TYPES = { + "pbs": 140, + "sge": 141, + "kubernetes": 142, + "aws": 143, + "gcp": 144, + "azure": 145, + "lambda_cloud": 146, + "huggingface_spaces": None, +} + +#: Settings that belonged to the removed backends, as (prefix or exact name) +#: -> (what it configured, tracking issue). Checked before the did-you-mean +#: path in :func:`load_config`. +_REMOVED_SETTINGS = ( + ("k8s_", "Kubernetes", 142), + ("auto_provision_k8s", "Kubernetes", 142), + ("aws_", "the AWS backend", 143), + ("eks_cluster_name", "the AWS backend", 143), + ("gcp_", "the GCP backend", 144), + ("gke_cluster_name", "the GCP backend", 144), + ("azure_", "the Azure backend", 145), + ("aks_cluster_name", "the Azure backend", 145), + ("lambda_", "the Lambda Cloud backend", 146), + ("cloud_provider", "the cloud VM backends", None), + ("cloud_region", "the cloud VM backends", None), + ("cloud_auto_configure", "the cloud VM backends", None), + ("cost_monitoring", "cloud cost monitoring", None), +) + + +def _removed_setting_reason(name: str) -> Optional[str]: + """Explain a setting that a removed backend used to own, or return None.""" + for key, what, issue in _REMOVED_SETTINGS: + matches = name.startswith(key) if key.endswith("_") else name == key + if matches: + where = f" (see issue #{issue})" if issue else "" + return f"{name} configured {what}, which has been removed{where}" + return None + _SECRET_FIELD_PATTERN = re.compile( r"secret|token|password|api_key|access_key|_key$|client_id|tenant_id" @@ -431,12 +375,11 @@ def _write_config_file_securely(config_path_obj: Path, config_data: dict) -> Non _config = ClusterConfig() -def configure(auto_install_deps: bool = True, **kwargs) -> None: +def configure(**kwargs) -> None: """ Configure Clustrix settings. Args: - auto_install_deps: Whether to automatically install cloud provider dependencies **kwargs: Configuration parameters matching ClusterConfig fields """ global _config # noqa: F824 @@ -448,27 +391,6 @@ def configure(auto_install_deps: bool = True, **kwargs) -> None: else: raise ValueError(f"Unknown configuration parameter: {key}") - # Check if we need to install cloud provider dependencies - if auto_install_deps: - from .auto_install import ensure_cloud_provider_dependencies - - cluster_type = kwargs.get("cluster_type", _config.cluster_type) - cloud_provider = kwargs.get("cloud_provider", _config.cloud_provider) - - # Try to ensure dependencies, but don't fail if installation fails - try: - ensure_cloud_provider_dependencies( - cluster_type=cluster_type, - cloud_provider=cloud_provider, - auto_install=True, - quiet=False, - ) - except Exception as e: - import logging - - logger = logging.getLogger(__name__) - logger.warning(f"Could not auto-install cloud provider dependencies: {e}") - def load_config(config_path: str) -> None: """ @@ -506,12 +428,30 @@ def load_config(config_path: str) -> None: hints = [] for name in unknown: + # A setting a removed backend owned gets a real explanation. The + # did-you-mean path below would otherwise match "k8s_namespace" + # against some unrelated field and send the reader after it. + removed = _removed_setting_reason(name) + if removed: + hints.append(removed) + continue close = difflib.get_close_matches(name, known, n=1, cutoff=0.6) hints.append(f"{name}" + (f" (did you mean {close[0]}?)" if close else "")) raise ValueError( f"{config_path} contains unknown setting(s): {'; '.join(hints)}" ) + requested = config_data.get("cluster_type") + if requested in REMOVED_CLUSTER_TYPES: + issue = REMOVED_CLUSTER_TYPES[requested] + where = f" It is tracked in issue #{issue}." if issue else "" + raise ValueError( + f"{config_path} requests cluster_type={requested!r}, which clustrix " + f"no longer implements. It was removed because it had never been " + f"verified against real hardware.{where} Supported types are: " + f"{', '.join(SUPPORTED_CLUSTER_TYPES)}." + ) + _config = ClusterConfig(**config_data) diff --git a/clustrix/decorator.py b/clustrix/decorator.py index 44f21f8c..8d358ba5 100644 --- a/clustrix/decorator.py +++ b/clustrix/decorator.py @@ -30,17 +30,6 @@ def cluster( auto_gpu_parallel: Optional[bool] = None, environment: Optional[str] = None, async_submit: Optional[bool] = None, - provider: Optional[str] = None, - instance_type: Optional[str] = None, - region: Optional[str] = None, - # NEW: Kubernetes auto-provisioning parameters - platform: Optional[str] = None, - auto_provision: Optional[bool] = None, - cluster_name: Optional[str] = None, - node_count: Optional[int] = None, - node_type: Optional[str] = None, - kubernetes_version: Optional[str] = None, - from_scratch: Optional[bool] = None, **kwargs, ): """ @@ -60,19 +49,6 @@ def cluster( Parallelize across GPUs inside your own function instead. environment: Conda environment name async_submit: Whether to submit jobs asynchronously (non-blocking) - provider: Cloud provider to use ('lambda', 'aws', 'azure', 'gcp', 'huggingface') - instance_type: Cloud instance type (e.g., 'gpu_1x_a100' for Lambda Cloud) - region: Cloud region (e.g., 'us-east-1') - - # NEW: Kubernetes auto-provisioning parameters - platform: Execution platform ('kubernetes' to enable K8s execution) - auto_provision: Whether to automatically provision K8s cluster if needed - cluster_name: Name for the auto-provisioned cluster - node_count: Number of worker nodes in the cluster - node_type: Cloud-specific node instance type - kubernetes_version: Kubernetes version to install - from_scratch: Whether to create all infrastructure from scratch - **kwargs: Additional job parameters Returns: @@ -96,91 +72,31 @@ def wrapper(*args, **func_kwargs): "environment": environment or config.conda_env_name, } - # Add cloud provider parameters if specified - if provider: - job_config["provider"] = provider - - if instance_type: - job_config["instance_type"] = instance_type - - if region: - job_config["region"] = region - - # NEW: Add Kubernetes auto-provisioning parameters - if platform: - job_config["platform"] = platform - # If platform is kubernetes, set cluster_type to kubernetes - if platform == "kubernetes": - config.cluster_type = "kubernetes" - - if auto_provision is not None: - job_config["auto_provision"] = auto_provision - config.auto_provision_k8s = auto_provision - - if cluster_name: - job_config["cluster_name"] = cluster_name - config.k8s_cluster_name = cluster_name - - if node_count is not None: - job_config["node_count"] = node_count - config.k8s_node_count = node_count - - if node_type: - job_config["node_type"] = node_type - config.k8s_node_type = node_type - - if kubernetes_version: - job_config["kubernetes_version"] = kubernetes_version - config.k8s_version = kubernetes_version - - if from_scratch is not None: - job_config["from_scratch"] = from_scratch - config.k8s_from_scratch = from_scratch - - # Add any additional cloud provider parameters from kwargs - cloud_params = [ - "lambda_api_key", - "aws_access_key_id", - "aws_secret_access_key", - "aws_region", - "azure_subscription_id", - "azure_tenant_id", - "azure_client_id", - "azure_client_secret", - "gcp_project_id", - "gcp_service_account_key", + # Per-job overrides the backends read off job_config. hf_jobs.py + # already reads hf_flavor/hf_timeout, they were simply never put + # there, so the documented @cluster(hf_flavor=...) was dropped. + passthrough_params = [ "hf_token", "hf_username", - "key_file", - "terminate_on_completion", - "instance_startup_timeout", - # Per-job overrides for the API-backed backends. hf_jobs.py - # already reads hf_flavor/hf_timeout off job_config and - # executor_kubernetes.py reads the k8s_* ones; they were simply - # never put there, so the documented - # @cluster(k8s_namespace="compute") was silently dropped. "hf_flavor", "hf_timeout", "hf_namespace", - "k8s_namespace", - "k8s_image", - "k8s_service_account", - "k8s_pull_policy", + "key_file", ] - for param in cloud_params: + for param in passthrough_params: if param in kwargs: job_config[param] = kwargs[param] # A silently ignored option is worse than a rejected one: the job # runs with settings the caller believes they changed. - unknown_kwargs = sorted(set(kwargs) - set(cloud_params)) + unknown_kwargs = sorted(set(kwargs) - set(passthrough_params)) if unknown_kwargs: logger.warning( "@cluster received unrecognised option(s) %s; they have no " "effect. Recognised extras: %s", ", ".join(unknown_kwargs), - ", ".join(sorted(cloud_params)), + ", ".join(sorted(passthrough_params)), ) # Determine execution mode @@ -237,21 +153,6 @@ def wrapper(*args, **func_kwargs): # Async execution async_executor = _shared_async_executor(config) - # NEW: Ensure Kubernetes cluster is ready if auto-provisioning (for async) - if config.cluster_type == "kubernetes" and getattr( - config, "auto_provision_k8s", False - ): - # For async execution, we still need to ensure cluster is ready first - # Create a temporary executor to check readiness - temp_executor = ClusterExecutor(config) - if not temp_executor.ensure_cluster_ready( - timeout=900 - ): # 15 minutes - raise RuntimeError( - "Auto-provisioned Kubernetes cluster failed to become ready" - ) - temp_executor.disconnect() - return async_executor.submit_job_async( func, args, func_kwargs, job_config ) @@ -259,16 +160,6 @@ def wrapper(*args, **func_kwargs): # Synchronous execution (original behavior) executor = ClusterExecutor(config) - # NEW: Ensure Kubernetes cluster is ready if auto-provisioning - if config.cluster_type == "kubernetes" and getattr( - config, "auto_provision_k8s", False - ): - # Give cluster extra time to be ready if auto-provisioned - if not executor.ensure_cluster_ready(timeout=900): # 15 minutes - raise RuntimeError( - "Auto-provisioned Kubernetes cluster failed to become ready" - ) - if should_parallelize: loop_info = detect_loops(func, args, func_kwargs) if loop_info: @@ -515,12 +406,6 @@ def _choose_execution_mode(config, func: Callable, args: tuple, kwargs: dict) -> Returns: 'local' or 'remote' """ - # Check for Kubernetes auto-provisioning - if config.cluster_type == "kubernetes" and getattr( - config, "auto_provision_k8s", False - ): - return "remote" - # Some backends reach their compute over an HTTP API rather than SSH, so # they legitimately have no cluster_host. Without this they fall into the # "no cluster configured" branch below and run on the caller's machine -- diff --git a/clustrix/executor.py b/clustrix/executor.py index b3116c83..f3161e89 100644 --- a/clustrix/executor.py +++ b/clustrix/executor.py @@ -4,10 +4,8 @@ the main ClusterExecutor class from the refactored executor_core module. The original large executor.py has been split into focused modules: -- executor_connections.py: SSH and Kubernetes connection management -- executor_schedulers.py: SLURM, PBS, SGE job submission/monitoring -- executor_kubernetes.py: K8s-specific operations -- executor_cloud.py: Cloud provider workflows +- executor_connections.py: SSH connection management +- executor_schedulers.py: SLURM job submission/monitoring - executor_core.py: Main ClusterExecutor coordination class All imports from this module continue to work as before for backward compatibility. @@ -22,8 +20,6 @@ from .executor_connections import ConnectionManager from .executor_schedulers import SchedulerManager from .executor_scheduler_status import SchedulerStatusManager -from .executor_kubernetes import KubernetesJobManager -from .executor_cloud import CloudJobManager # For backward compatibility with tests logger = logging.getLogger(__name__) @@ -34,6 +30,4 @@ "ConnectionManager", "SchedulerManager", "SchedulerStatusManager", - "KubernetesJobManager", - "CloudJobManager", ] diff --git a/clustrix/utils.py b/clustrix/utils.py index d1c12ee3..7230f8c2 100644 --- a/clustrix/utils.py +++ b/clustrix/utils.py @@ -96,7 +96,7 @@ def validate_shell_fragment(config_key: str, value: Any) -> str: """Refuse a config value that cannot be safely pasted in unquoted. Used only where quoting would break the feature: a ``module load`` line, - a ``#SBATCH``/``#PBS``/``#$`` directive body, the name in ``export + a ``#SBATCH`` directive body, the name in ``export NAME=...``. Everywhere else the value is quoted with ``shlex.quote`` instead, which needs no allowlist. @@ -2140,7 +2140,7 @@ def generate_two_venv_execution_commands( Generate the standardized two-venv execution commands. This centralizes the two-venv logic to eliminate code duplication across - different cluster types (SLURM, SSH, PBS, SGE). + different cluster types (SLURM, SSH). The three stages hand objects to each other through files on disk. Those handoffs use dill (falling back to cloudpickle, then stdlib pickle) rather @@ -2449,20 +2449,13 @@ def _error_handler(stage: str, message: str) -> list: def normalize_memory(value: Any, target: str) -> str: """Render a memory size in the form a given scheduler accepts. - Clustrix's own configuration uses human sizes like ``"16GB"``. Schedulers - do not agree on that spelling: - - * Kubernetes quantities are ``16G`` (decimal) or ``16Gi`` (binary) and a - pod carrying ``16GB`` is rejected outright by the API server. - * SLURM's ``--mem`` takes a bare number with an optional ``K|M|G|T``. - * PBS and SGE accept ``16gb`` and ``16G`` respectively. - - Passing the configured string through unchanged is what made - ``default_memory`` unusable on Kubernetes. + Clustrix's own configuration uses human sizes like ``"16GB"``, which is + not what SLURM's ``--mem`` accepts: it takes a bare number with an + optional ``K|M|G|T`` suffix, so ``16GB`` is rejected. Args: value: A size such as ``"16GB"``, ``"512Mi"``, ``16`` (GB assumed). - target: ``"kubernetes"``, ``"slurm"``, ``"pbs"`` or ``"sge"``. + target: ``"slurm"``. Returns: The size spelled the way ``target`` expects it. @@ -2483,10 +2476,10 @@ def normalize_memory(value: Any, target: str) -> str: if not unit: unit = "G" # a bare number has always meant gigabytes here - # Schedulers take integers. "1.5GB" would reach SLURM as --mem=1.5G and - # PBS as mem=1.5gb, both of which they reject, so round up to the next - # whole unit rather than emit something that cannot be submitted. Rounding - # *up* because a job asking for 1.5G and given 1G would be killed. + # SLURM takes integers. "1.5GB" would reach it as --mem=1.5G, which it + # rejects, so round up to the next whole unit rather than emit something + # that cannot be submitted. Rounding *up* because a job asking for 1.5G + # and given 1G would be killed. if "." in amount: import math @@ -2500,16 +2493,8 @@ def normalize_memory(value: Any, target: str) -> str: ) amount = str(whole) - if target == "kubernetes": - # "16GB" means 16 gibibytes in every other part of clustrix, so keep - # the binary suffix rather than silently shrinking the request by 7%. - return f"{amount}{unit}i" if unit else amount if target == "slurm": return f"{amount}{unit}" - if target == "pbs": - return f"{amount.lower()}{unit.lower()}b" - if target == "sge": - return f"{amount}{unit}" return text @@ -2543,10 +2528,6 @@ def create_job_script( if cluster_type == "slurm": return _create_slurm_script(job_config, remote_job_dir, config) - elif cluster_type == "pbs": - return _create_pbs_script(job_config, remote_job_dir, config) - elif cluster_type == "sge": - return _create_sge_script(job_config, remote_job_dir, config) elif cluster_type == "ssh": return _create_ssh_script(job_config, remote_job_dir, config) else: @@ -2621,11 +2602,10 @@ def result_signing_lines(indent: str = " ", serializer: str = "pickle") -> li def job_execution_lines(remote_job_dir: str, config: ClusterConfig) -> list: """The lines that actually run the user's function in a job script. - Shared by every scheduler. It used to live only inside the SLURM - generator: PBS ran `python execute_function.py`, a file nothing in - clustrix has ever created, and SGE carried its own divergent copy of - the single-venv script. Both therefore missed the two-venv path, the - result signing and every fix made to the SLURM one. + Shared by every scheduler, rather than living inside one generator: the + now-removed PBS and SGE generators each carried a divergent copy, and + both therefore missed the two-venv path, the result signing and every + fix made to the SLURM one. """ script_lines: list = [] # Add execution commands @@ -2769,66 +2749,6 @@ def _create_slurm_script( return "\n".join(script_lines) -def _create_pbs_script( - job_config: Dict[str, Any], remote_job_dir: str, config: ClusterConfig -) -> str: - """Create PBS job script.""" - - # As for SLURM: #PBS directives are parsed by the scheduler, so these are - # validated rather than quoted. - job_dir = validate_shell_fragment("remote_work_dir", remote_job_dir) - script_lines = [ - "#!/bin/bash", - "#PBS -N clustrix", - f"#PBS -o {job_dir}/job.out", - f"#PBS -e {job_dir}/job.err", - f"#PBS -l nodes=1:ppn={validate_shell_fragment('cores', job_config['cores'])}", - f"#PBS -l mem=" - f"{validate_shell_fragment('memory', normalize_memory(job_config['memory'], 'pbs'))}", - f"#PBS -l walltime={validate_shell_fragment('time', job_config['time'])}", - ] - - if job_config.get("queue"): - queue = validate_shell_fragment("queue", job_config["queue"]) - script_lines.append(f"#PBS -q {queue}") - - # Add environment setup - script_lines.extend(environment_setup_lines(config)) - - script_lines.extend(job_execution_lines(remote_job_dir, config)) - - return "\n".join(script_lines) - - -def _create_sge_script( - job_config: Dict[str, Any], remote_job_dir: str, config: ClusterConfig -) -> str: - """Create SGE job script.""" - - # As for SLURM: #$ directives are parsed by the scheduler, so these are - # validated rather than quoted. - job_dir = validate_shell_fragment("remote_work_dir", remote_job_dir) - script_lines = [ - "#!/bin/bash", - "#$ -N clustrix", - f"#$ -o {job_dir}/job.out", - f"#$ -e {job_dir}/job.err", - f"#$ -pe smp {validate_shell_fragment('cores', job_config['cores'])}", - f"#$ -l h_vmem=" - f"{validate_shell_fragment('memory', normalize_memory(job_config['memory'], 'sge'))}", - f"#$ -l h_rt={validate_shell_fragment('time', job_config['time'])}", - "#$ -cwd", - "", - ] - - # Add environment setup - script_lines.extend(environment_setup_lines(config)) - - script_lines.extend(job_execution_lines(remote_job_dir, config)) - - return "\n".join(script_lines) - - def _create_ssh_script( job_config: Dict[str, Any], remote_job_dir: str, config: ClusterConfig ) -> str: diff --git a/docs/source/_static/img/screenshots/widget_gcp.png b/docs/source/_static/img/screenshots/widget_gcp.png deleted file mode 100644 index 4245263035abf01329f7ea43b9e58ad4660df0e3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 112716 zcmeFZc|28L`~S}|&xA;blrd8jj+qiN9%CU>NSPh;c;uKe6$%+M3sL4|9?BdtM=0|= zXZ)_+`@TP)clZ51{eFM_9^XHHJsu_JtiAWzYhQb<>v~?VYlUj4-6A7qAjZPNB2!Yl zfxyDTr@_L)*(byYpIk$nu!_1F7r-xO7Wzt-s;XGf7lc^Y!PZ!~P!sT% z0sO_n!hIHmMF4)D0e>~pu(5E!Pi*M>G}yme;?tzz{QLhn``~*lSuJ@bCGb`^ZD-3!RK!403CuQ;SLA?wY5N+vbfvY*g3=9 zB`^MJ0SEtwKIXs3@~a8*k>o{vRSgz-dnXH)>wE%y0vDx-Sy)&k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#WSs-rg{r>>Q-%j5E diff --git a/docs/source/_static/img/screenshots/widget_lambda.png b/docs/source/_static/img/screenshots/widget_lambda.png deleted file mode 100644 index 8f729ac9a31b70a02758632b4c4994fe5a221e03..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 148569 zcmeFZbySsIyY@{i8bm+@3F%Z|(H#OVx=|28I;9&a7Yc}UinLN<(A|xMv;u`^G!=AFpF5xYm{Pn%A7i`8$tuhN!E`5#UnbqM)DS)qC($ip#8liCV2+>-@l_DgYQvpX~`%kfS+2X&gSO!E>;e% z_(v~!z>cHV+Pbc~D#{|J4tAWzW)3FioSt@$&@L!qo+98^J9AfKT2DJ$dlwN;ar)m| zh=AXrkGbe+e{bUYP@G;@MV(g0!P%TvfD_IMrgZzCQS#uXtXKP1S zYX^H;=(xrv4sNdE^z_h){{82#>ooVY{;xCHyZr06zzuRi-*9nr!nyu^Y_O{s^r?ut zwWqo5Jy~lz&^+KA65MbexY+L<{>NAUb;|$PRrf!3wkRp-*>&^V(u*CU zD)C>>=3nRi&oBSAqZk)-@Bh&je>L>)PeDsd;EHkm`>9Fbs`*C~prA;hD9GN@_C#I& zh7ot?&UoVnfk`M@xk){yyN3rJIV356^yR@?nZ;_|;<8qQizSC=T1omh+ve7*wz#YrkI^ppM~VnZ#DIAE<-`5p zg-^*H*JXn6an5RQ6eB}P)um7|uKf8Qq%;~cIc{F1@`YQVVkqJ!8nJfHDqeI9uq!L0 zQNXLH=iL{a1v+>})?=KY7_ofq|F0KQ%34+4*i~fO5%kg`DX%fwS+3rvmU2#(kdOm&{xJbj9OUTGRy%-;a&Kt_b(p z9kYC8*_Sq*{o(iLROilHSFBS5W^*Xn*QS zOBipjZt}OgrJuMOj)^Y`O+AKtu1~R_&^-F_s_@6_P=QLAjPGPJJ1djK`bWCLO>$dd=3mfo)`a>J4HaJ_vJ;NjATPN{4 z4}>Sc8JsG1yc}$t)7YvS1sGfEk2kyFlRjH_#xN#V;`EI$GTkqJ7w%x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Bn=prrNF0|!g}O$cch^rNeh;q5Afiyqe} zn*H*$raPY?7qZ4{kO=CfueAwtkycJ}Sb_F2!)KLK`7f7e?_{=WPcr%ZcA|3c-fSJ& z%^S{Mm(oaj(u-uPFGar*@0W7$3(OW#`kWXt(TJtt&w+=JBy5f`GU*NO%bi(&j z(?T52r=?15sVcuhbm#Hf+dvD|vVc7^v~`iYSXxd8^{mCyx)lpeOq0Oenapfj`rD51 zd&*^>2W>CoFWH1%Q*ZY3s)Psm1d?N5pyXKK5s~c?Kx3T%} zI1QG8qiKysfbiToJe~Ura@rcFTUYe{HK+UQSF%Yswx^5pgR_U9vbd-GUwax(`fAN3 z%Z2CvWs;-eNa*bBd_z)lBI>=$l0dY7?yXokrLzZPva?X|CU1{;1n&Kpi6Av@&;N76 i-Pp6A==KQyDf`Q}jL$Y-;q9LT|L!Y3P$;}(3j04}{4BHp From 62e7a299d0b44f78d78280054e07185bde931a45 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:38:07 -0400 Subject: [PATCH 10/56] Docs: purge removed backends from the tutorial notebooks Rewrites the PBS/SGE/Kubernetes/cloud config examples in complete_api_demo and cluster_config_example, drops the cost-monitoring example from basic_usage, fixes the dead links to the deleted pbs/kubernetes/cost_monitoring notebooks, and deletes the now-unreferenced widget_gcp.png / widget_lambda.png screenshots. --- docs/source/notebooks/basic_usage.ipynb | 51 ++- .../notebooks/cluster_config_example.ipynb | 61 ++- docs/source/notebooks/complete_api_demo.ipynb | 127 ++---- docs/source/notebooks/slurm_tutorial.ipynb | 3 +- docs/source/notebooks/ssh_tutorial.ipynb | 431 +++++++++--------- 5 files changed, 338 insertions(+), 335 deletions(-) diff --git a/docs/source/notebooks/basic_usage.ipynb b/docs/source/notebooks/basic_usage.ipynb index 7b42b0cf..07079267 100644 --- a/docs/source/notebooks/basic_usage.ipynb +++ b/docs/source/notebooks/basic_usage.ipynb @@ -13,7 +13,7 @@ "## What Clustrix Does Behind the Scenes: Local vs. Remote\n", "\n", "This notebook uses `cluster_host=None` (or simply never sets `cluster_host`),\n", - "which is a different, much simpler code path than the SLURM/PBS/SGE/SSH\n", + "which is a different, much simpler code path than the SLURM/SSH\n", "tutorials elsewhere in this documentation:\n", "\n", "- **No `cluster_host` configured -> local execution.** Nothing is\n", @@ -30,7 +30,7 @@ " verification against your `known_hosts` -- see :doc:`../ssh_setup`),\n", " stage a signed job directory, build a matching remote environment,\n", " generate and submit a job script, poll, then verify and deserialize a\n", - " signed result. The SLURM, PBS, SGE and SSH tutorials in this\n", + " signed result. The SLURM and SSH tutorials in this\n", " documentation set cover that path and its generated job scripts in\n", " detail.\n", "- **Practical consequence**: local mode has none of remote mode's edge\n", @@ -59,7 +59,32 @@ { "cell_type": "markdown", "metadata": {}, - "source": "## Configuration Options\n\n### Interactive Widget Configuration (Recommended for Jupyter)\n\nClustrix provides an interactive widget for easy configuration management in Jupyter notebooks:\n\n```python\n%%remote\n# This creates an interactive widget with:\n# - Pre-built cluster templates (AWS, GCP, Azure, SLURM, etc.)\n# - Forms to create and edit configurations\n# - One-click configuration application\n# - Save/load configurations to files\n```\n\n**Widget Features:**\n- **Default Templates**: Pre-configured setups for major cloud providers\n- **Interactive Forms**: GUI elements for all configuration options \n- **Configuration Management**: Create, edit, delete, and apply configurations\n- **File I/O**: Save/load configurations as YAML or JSON files\n\n### Programmatic Configuration\n\nFor programmatic setup, use the `configure()` function:", + "source": [ + "## Configuration Options\n", + "\n", + "### Interactive Widget Configuration (Recommended for Jupyter)\n", + "\n", + "Clustrix provides an interactive widget for easy configuration management in Jupyter notebooks:\n", + "\n", + "```python\n", + "%%remote\n", + "# This creates an interactive widget with:\n", + "# - Pre-built cluster templates (local, SSH, SLURM, HuggingFace Jobs)\n", + "# - Forms to create and edit configurations\n", + "# - One-click configuration application\n", + "# - Save/load configurations to files\n", + "```\n", + "\n", + "**Widget Features:**\n", + "- **Default Templates**: Pre-configured setups for each supported backend\n", + "- **Interactive Forms**: GUI elements for all configuration options \n", + "- **Configuration Management**: Create, edit, delete, and apply configurations\n", + "- **File I/O**: Save/load configurations as YAML or JSON files\n", + "\n", + "### Programmatic Configuration\n", + "\n", + "For programmatic setup, use the `configure()` function:" + ], "id": "cell-3" }, { @@ -386,7 +411,25 @@ { "cell_type": "markdown", "metadata": {}, - "source": "## Cost Monitoring\n\nClustrix includes built-in cost monitoring for cloud providers:\n\n```python\nfrom clustrix import cost_tracking_decorator\n\n# Automatic cost tracking\n@cost_tracking_decorator('aws', 'p3.2xlarge')\n@clustrix.cluster(cores=8, memory='60GB')\ndef expensive_training():\n # Your training code here\n pass\n\n# Execution includes cost reporting\nresult = expensive_training()\nprint(f\"Training cost: ${result['cost_report']['cost_estimate']['estimated_cost']:.2f}\")\n```\n\n## Next Steps\n\nThis tutorial covered the basics of Clustrix usage. For more advanced topics, check out:\n\n- **Interactive Widget**: Use `%%remote` for GUI-based configuration management\n- **Cost Monitoring**: Track expenses with built-in cost monitoring for AWS, GCP, Azure, Lambda Cloud\n- **Remote Cluster Configuration**: Setting up SLURM, PBS, or SSH clusters\n- **Advanced Parallelization**: Custom loop detection and optimization\n- **Machine Learning Workflows**: Using Clustrix with scikit-learn, TensorFlow, or PyTorch\n- **Scientific Computing**: Integration with SciPy, pandas, and other scientific libraries\n\nVisit the [Clustrix documentation](https://clustrix.readthedocs.io) for detailed guides and API reference.", + "source": [ + "## Next Steps\n", + "\n", + "This tutorial covered the basics of Clustrix usage. For more advanced topics, check out:\n", + "\n", + "- **Interactive Widget**: Use `%%remote` for GUI-based configuration management\n", + "- **Remote Cluster Configuration**: Setting up SLURM or SSH clusters\n", + "- **Advanced Parallelization**: Custom loop detection and optimization\n", + "- **Machine Learning Workflows**: Using Clustrix with scikit-learn, TensorFlow, or PyTorch\n", + "- **Scientific Computing**: Integration with SciPy, pandas, and other scientific libraries\n", + "\n", + "> **Cost monitoring was removed in v0.2.0.** `cost_tracking_decorator`,\n", + "> `get_cost_monitor`, `start_cost_monitoring`, `generate_cost_report` and\n", + "> `get_pricing_info` no longer exist. They priced the cloud VM backends, which\n", + "> were themselves removed because none had ever been shown to run a job end to\n", + "> end. See the \"Backends removed in v0.2.0\" section of the Limitations page.\n", + "\n", + "Visit the [Clustrix documentation](https://clustrix.readthedocs.io) for detailed guides and API reference.\n" + ], "id": "cell-17" } ], diff --git a/docs/source/notebooks/cluster_config_example.ipynb b/docs/source/notebooks/cluster_config_example.ipynb index 84175308..9322e9f7 100644 --- a/docs/source/notebooks/cluster_config_example.ipynb +++ b/docs/source/notebooks/cluster_config_example.ipynb @@ -13,11 +13,13 @@ "> old `%%clusterfy` magic, kept as a deprecated alias) displays an\n", "> `ipywidgets` form; its \"Apply Config\" button calls `clustrix.configure(**config)`\n", "> with whatever the form collected -- there is no other magic involved. Only\n", - "> `cluster_type=\"local\"`, `\"slurm\"`, `\"ssh\"` and `\"huggingface\"` have been\n", - "> demonstrated running a real job end to end. The widget also lets you pick\n", - "> `\"aws\"`, `\"gcp\"`, `\"azure\"` and `\"lambda\"` cloud-provider fields (see below)\n", - "> -- those configure clustrix's cloud-VM auto-provisioning path, which is\n", - "> unverified end to end. See :ref:`limitations`." + "> `cluster_type=\"local\"`, `\"slurm\"`, `\"ssh\"` and `\"huggingface\"` are the only\n", + "> values the widget offers, and each has been demonstrated running a real job\n", + "> end to end. PBS, SGE, Kubernetes and the AWS / GCP / Azure / Lambda Cloud VM\n", + "> providers are **not currently supported**: they were removed in v0.2.0\n", + "> because none had ever been shown to run a job end to end. They are planned\n", + "> for a future release -- see the \"Backends removed in v0.2.0\" section of the\n", + "> Limitations page for the tracking issues." ] }, { @@ -83,7 +85,7 @@ "\n", "### 1. **Configuration Selection**\n", "- Use the dropdown to select between different configurations\n", - "- Default configurations include local, AWS, GCP, Azure, SLURM, and Kubernetes options\n", + "- Default configurations include local, SSH, SLURM and HuggingFace Jobs options\n", "\n", "### 2. **Configuration Management**\n", "- **New Config**: Create a new configuration\n", @@ -93,7 +95,7 @@ "### 3. **Configuration Fields**\n", "- **Name**: Friendly name for the configuration\n", "- **Description**: Detailed description of the cluster\n", - "- **Cluster Type**: local, ssh, slurm, pbs, sge, kubernetes, or huggingface\n", + "- **Cluster Type**: local, ssh, slurm, or huggingface\n", "- **Host**: Hostname or IP address (for remote clusters)\n", "- **Username**: SSH username (for remote clusters)\n", "- **SSH Key**: Path to SSH private key\n", @@ -113,31 +115,26 @@ "id": "5zfksrh87j5", "metadata": {}, "source": [ - "## Cloud Provider Examples\n", - "\n", - "The widget includes comprehensive *form* support for cloud providers -- dynamic\n", - "field visibility and intelligent defaults for the `ClusterConfig` fields each\n", - "provider uses. That is a UI/config-collection claim, not a claim that jobs run\n", - "successfully on these providers: `@cluster(provider=\"aws\"/\"gcp\"/\"azure\"/\"lambda\")`\n", - "cloud-VM auto-provisioning is unverified end to end (of the built-in providers,\n", - "only `\"lambda\"` even implements instance creation; the others raise\n", - "`NotImplementedError` at submit time). See :ref:`limitations`.\n", - "\n", - "### Google Cloud Platform\n", - "When configuring GCP, only relevant fields are displayed:\n", - "\n", - "![GCP Configuration](../_static/img/screenshots/widget_gcp.png)\n", - "\n", - "### Lambda Cloud GPU Instances\n", - "The widget provides specialized support for GPU-optimized Lambda Cloud instances:\n", - "\n", - "![Lambda Cloud Configuration](../_static/img/screenshots/widget_lambda.png)\n", - "\n", - "### Key Cloud Features\n", - "- **Dynamic Field Visibility**: Only shows fields relevant to the selected provider\n", - "- **Auto-populated Dropdowns**: Instance types, regions, and zones populated automatically\n", - "- **Provider-specific Options**: Each cloud provider has tailored configuration options\n", - "- **Cost Monitoring**: Built-in cost tracking for all cloud providers" + "## Cloud providers: not currently supported\n", + "\n", + "Earlier versions of this notebook showed the widget's cloud-provider forms\n", + "(AWS, GCP, Azure, Lambda Cloud) and a Kubernetes section. **None of those\n", + "backends is currently supported.** They were removed in v0.2.0 because not one\n", + "of them had ever been shown to run a job end to end, and the cost monitoring\n", + "and cloud pricing API went with them.\n", + "\n", + "They are planned for a future release, and each has a tracking issue:\n", + "[PBS #140](https://github.com/ContextLab/clustrix/issues/140),\n", + "[SGE #141](https://github.com/ContextLab/clustrix/issues/141),\n", + "[Kubernetes #142](https://github.com/ContextLab/clustrix/issues/142),\n", + "[AWS #143](https://github.com/ContextLab/clustrix/issues/143),\n", + "[GCP #144](https://github.com/ContextLab/clustrix/issues/144),\n", + "[Azure #145](https://github.com/ContextLab/clustrix/issues/145),\n", + "[Lambda Cloud #146](https://github.com/ContextLab/clustrix/issues/146).\n", + "\n", + "For a rented GPU today, use `cluster_type=\"huggingface\"` (HuggingFace *Jobs*,\n", + "verified end to end) or bring up a VM yourself and point `cluster_type=\"ssh\"`\n", + "at it.\n" ] }, { diff --git a/docs/source/notebooks/complete_api_demo.ipynb b/docs/source/notebooks/complete_api_demo.ipynb index 6b2c6874..dad3fa0c 100644 --- a/docs/source/notebooks/complete_api_demo.ipynb +++ b/docs/source/notebooks/complete_api_demo.ipynb @@ -42,7 +42,7 @@ "source": [ "# Install Clustrix (uncomment if needed)\n", "# !pip install clustrix\n", - "# !pip install clustrix[kubernetes] # With Kubernetes support\n", + "# !pip install clustrix[widget] # With the Jupyter widget\n", "\n", "# Import all Clustrix components\n", "import clustrix\n", @@ -117,9 +117,11 @@ "outputs": [], "source": [ "# NOTE: only fields that appear on `ClusterConfig` (see the Configuration\n", - "# API reference) are real settings. SLURM `account`/`qos`, PBS `walltime`,\n", - "# and an SGE parallel environment are not currently configurable through\n", - "# ClusterConfig; they are omitted below rather than shown as if supported.\n", + "# API reference) are real settings. SLURM `account`/`qos` is not currently\n", + "# configurable through ClusterConfig; it is omitted below rather than shown\n", + "# as if supported. The four cluster types below are the only ones clustrix\n", + "# accepts -- PBS, SGE, Kubernetes and the cloud VM providers were removed in\n", + "# v0.2.0 (see the Limitations page).\n", "def demonstrate_all_config_options():\n", " \"\"\"\n", " Demonstrate all available configuration options for different cluster types.\n", @@ -149,38 +151,13 @@ " 'cleanup_on_success': True,\n", " 'max_parallel_jobs': 20\n", " },\n", - " 'pbs': {\n", - " 'cluster_type': 'pbs',\n", - " 'cluster_host': 'pbs-cluster.org',\n", - " 'username': 'scientist',\n", - " 'key_file': '~/.ssh/pbs_key',\n", - " 'default_cores': 6,\n", - " 'default_memory': '24GB',\n", - " 'default_time': '04:00:00',\n", - " 'default_queue': 'bioqueue',\n", - " 'remote_work_dir': '/home/scientist/clustrix',\n", - " 'cleanup_on_success': True\n", - " },\n", - " 'sge': {\n", - " 'cluster_type': 'sge',\n", - " 'cluster_host': 'sge-cluster.example.com',\n", - " 'username': 'engineer',\n", - " 'key_file': '~/.ssh/sge_key',\n", - " 'default_cores': 12,\n", - " 'default_memory': '48GB',\n", - " 'default_time': '06:00:00',\n", - " 'default_queue': 'all.q',\n", - " 'remote_work_dir': '/home/engineer/clustrix'\n", - " },\n", - " 'kubernetes': {\n", - " 'cluster_type': 'kubernetes',\n", - " 'k8s_namespace': 'default',\n", + " 'huggingface': {\n", + " 'cluster_type': 'huggingface',\n", + " 'hf_namespace': 'my-org',\n", + " 'hf_flavor': 'cpu-basic',\n", " 'default_cores': 4,\n", - " 'default_memory': '8Gi',\n", - " 'k8s_image': 'python:3.11-slim',\n", - " 'k8s_pull_policy': 'IfNotPresent',\n", - " 'k8s_job_ttl_seconds': 3600,\n", - " 'k8s_backoff_limit': 3\n", + " 'default_memory': '16GB',\n", + " 'cleanup_on_success': True\n", " },\n", " 'ssh': {\n", " 'cluster_type': 'ssh',\n", @@ -296,7 +273,7 @@ "9. Clean up the remote job directory on success (if `cleanup_on_success`).\n", "\n", "The full, source-verified version of this walkthrough -- including exactly\n", - "what changes per backend (SLURM/PBS/SGE/Kubernetes/SSH/HuggingFace) -- is in\n", + "what changes per backend (SLURM/SSH/HuggingFace Jobs/local) -- is in\n", ":ref:`execution-model`. :ref:`configuration` documents every `ClusterConfig`\n", "field and how it interacts with `@cluster`'s own keyword arguments; only a\n", "fixed set of keywords actually reach job submission (see the note in the\n", @@ -362,10 +339,8 @@ " @cluster accepts arbitrary extra keyword arguments, but only a small,\n", " fixed set actually reaches job submission: the named parameters below,\n", " plus a short allowlist of provider-specific extras ('hf_flavor',\n", - " 'hf_timeout', 'hf_namespace', 'k8s_namespace', 'k8s_image',\n", - " 'k8s_service_account', 'k8s_pull_policy'). Anything else -- SLURM\n", - " account/qos/gres, PBS walltime, an SGE parallel environment,\n", - " Kubernetes cpu_limit/restart_policy, and so on -- is accepted\n", + " 'hf_timeout', 'hf_namespace', ...). Anything else -- SLURM\n", + " account/qos/gres and so on -- is accepted\n", " without error but silently has no effect, and clustrix logs a warning\n", " (\"received unrecognised option(s)\") each time the function is called.\n", " That is a real, verified behaviour of clustrix/decorator.py, not a\n", @@ -383,8 +358,8 @@ " \"\"\"Basic resource specification.\"\"\"\n", " return sum(i**2 for i in range(n))\n", "\n", - " # SLURM/PBS/SGE scheduler selection: 'partition' is SLURM's field,\n", - " # 'queue' is PBS/SGE's. Both are real named parameters.\n", + " # Scheduler selection: 'partition' is SLURM's field. It is a real\n", + " # named parameter.\n", " @cluster(\n", " cores=16,\n", " memory=\"64GB\",\n", @@ -396,24 +371,22 @@ " import numpy as np\n", " return np.mean(data)\n", "\n", - " # Kubernetes-specific parameters -- only the allowlisted k8s_* extras\n", - " # are actually threaded into job submission.\n", + " # HuggingFace Jobs extras -- only the allowlisted hf_* extras are\n", + " # actually threaded into job submission.\n", " @cluster(\n", - " platform=\"kubernetes\",\n", " cores=4,\n", - " memory=\"16Gi\", # Kubernetes memory format\n", - " k8s_namespace=\"default\",\n", - " k8s_image=\"python:3.11\", # Container image\n", - " k8s_pull_policy=\"IfNotPresent\",\n", + " memory=\"16GB\",\n", + " hf_namespace=\"my-org\",\n", + " hf_flavor=\"cpu-basic\",\n", " )\n", - " def kubernetes_demo(task_id):\n", - " \"\"\"Kubernetes-specific parameter demonstration.\"\"\"\n", + " def huggingface_demo(task_id):\n", + " \"\"\"HuggingFace Jobs parameter demonstration.\"\"\"\n", " import os\n", " import time\n", " time.sleep(1)\n", " return {\n", " 'task_id': task_id,\n", - " 'pod_name': os.environ.get('HOSTNAME', 'unknown'),\n", + " 'hostname': os.environ.get('HOSTNAME', 'unknown'),\n", " 'completion_time': time.time()\n", " }\n", "\n", @@ -450,7 +423,7 @@ " return {\n", " 'basic_resources': basic_resources_demo,\n", " 'scheduler_specific': scheduler_specific_demo,\n", - " 'kubernetes': kubernetes_demo,\n", + " 'huggingface': huggingface_demo,\n", " 'environment': environment_demo\n", " }\n", "\n", @@ -703,16 +676,11 @@ " key_file=\"~/.ssh/id_rsa\",\n", " default_partition=\"normal\"\n", " ),\n", - " 'pbs': ClusterConfig(\n", - " cluster_type=\"pbs\",\n", - " cluster_host=\"pbs-cluster.org\",\n", - " username=\"scientist\",\n", - " default_queue=\"bioqueue\"\n", - " ),\n", - " 'kubernetes': ClusterConfig(\n", - " cluster_type=\"kubernetes\",\n", - " k8s_namespace=\"default\",\n", - " k8s_image=\"python:3.11-slim\"\n", + " 'ssh': ClusterConfig(\n", + " cluster_type=\"ssh\",\n", + " cluster_host=\"remote-server.example.com\",\n", + " username=\"developer\",\n", + " key_file=\"~/.ssh/dev_key\"\n", " )\n", " }\n", " \n", @@ -1913,13 +1881,13 @@ "metadata": {}, "source": [ "> **Backend support, verified.** This notebook exercises the API surface, not\n", - "> every backend. Only `cluster_type=\"slurm\"`, `\"ssh\"`, `\"huggingface\"` and\n", - "> `\"local\"` have been demonstrated running a real job end to end. `\"pbs\"` and\n", - "> `\"sge\"` are implemented but have never been run against real hardware;\n", - "> `\"kubernetes\"` has never been verified against a real cluster and does not\n", - "> replicate your local environment. `@cluster(provider=...)` cloud-VM\n", - "> auto-provisioning (`\"aws\"`, `\"gcp\"`, `\"azure\"`, `\"lambda\"`) is unverified end\n", - "> to end. See :ref:`limitations` for what that means in practice." + "> every backend. `cluster_type=\"slurm\"`, `\"ssh\"`, `\"huggingface\"` and\n", + "> `\"local\"` are the only values clustrix accepts, and each has been\n", + "> demonstrated running a real job end to end. PBS, SGE, Kubernetes and the\n", + "> `@cluster(provider=...)` cloud VM path are **not currently supported** --\n", + "> they were removed in v0.2.0 because none had ever been shown to run a job\n", + "> end to end, and each is planned for a future release under its own tracking\n", + "> issue. See :ref:`removed-backends`.\n" ] }, { @@ -1955,18 +1923,13 @@ "- **SSH** - direct remote execution\n", "- **HuggingFace Jobs** (`cluster_type=\"huggingface\"`)\n", "\n", - "**Implemented but never run against real hardware** -- the code exists and\n", - "follows the same submission path as the verified backends, but no completed\n", - "job has been demonstrated:\n", - "- **PBS/Torque** - batch systems\n", - "- **SGE** - Sun Grid Engine\n", - "- **Kubernetes** - containerized execution (also does not replicate your\n", - " local environment -- only `dill`/`cloudpickle` are installed in the pod)\n", - "\n", - "**Cloud VM auto-provisioning** (`@cluster(provider=\"aws\"/\"gcp\"/\"azure\"/\"lambda\")`)\n", - "is a separate, unverified feature from all of the above -- see\n", - ":ref:`limitations` (the \"Unverified backends\" section) for exactly what is\n", - "and is not known to work, and why.\n", + "Those four are the whole list. **Not currently supported**: PBS/Torque, SGE,\n", + "Kubernetes, and cloud VM auto-provisioning\n", + "(`@cluster(provider=\"aws\"/\"gcp\"/\"azure\"/\"lambda\")`). All were removed in\n", + "v0.2.0 because none had ever been shown to run a job end to end, along with\n", + "the cost monitoring and cloud pricing API. Each is planned for a future\n", + "release and has a tracking issue -- see :ref:`removed-backends` for the table\n", + "and the links.\n", "\n", "### Best Practices Covered:\n", "- Performance optimization strategies\n", diff --git a/docs/source/notebooks/slurm_tutorial.ipynb b/docs/source/notebooks/slurm_tutorial.ipynb index fc70b62b..c1ab6efd 100644 --- a/docs/source/notebooks/slurm_tutorial.ipynb +++ b/docs/source/notebooks/slurm_tutorial.ipynb @@ -942,8 +942,7 @@ "\n", "### Next Steps:\n", "\n", - "- Check out the [PBS Tutorial](pbs_tutorial.ipynb) for Torque/PBS clusters\n", - "- Explore [Kubernetes Tutorial](kubernetes_tutorial.ipynb) for containerized computing\n", + "- Check out the [SSH Tutorial](ssh_tutorial.ipynb) for schedulerless remote execution\n", "- Review the [SSH Setup Guide](../ssh_setup.rst) for secure authentication\n", "- Read the [API Documentation](../api/decorator.rst) for advanced decorator options\n", "\n", diff --git a/docs/source/notebooks/ssh_tutorial.ipynb b/docs/source/notebooks/ssh_tutorial.ipynb index 552a81c1..34f7d378 100644 --- a/docs/source/notebooks/ssh_tutorial.ipynb +++ b/docs/source/notebooks/ssh_tutorial.ipynb @@ -15,20 +15,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# \ud83d\ude80 SSH Remote Execution Tutorial\n", + "# 🚀 SSH Remote Execution Tutorial\n", "\n", "This tutorial demonstrates how to use Clustrix for **automated SSH-based remote execution** without a job scheduler. Perfect for executing functions on remote servers, workstations, or cloud instances.\n", "\n", - "## \u2728 **New: Automated SSH Key Setup**\n", + "## ✨ **New: Automated SSH Key Setup**\n", "\n", "Clustrix includes **automated SSH key setup**: generate, deploy and configure a key in one call.\n", "\n", - "## \ud83d\udccb Prerequisites\n", + "## 📋 Prerequisites\n", "\n", "- Access to a remote server (cloud instance, workstation, or HPC login node)\n", "- Username and password for initial authentication\n", "- Python installed on the remote server\n", - "- \u2728 **That's it!** No manual SSH key setup required" + "- ✨ **That's it!** No manual SSH key setup required" ], "id": "cell-1" }, @@ -52,12 +52,12 @@ "4. **Upload** the pickled payload.\n", "5. **Build the environment** (two virtualenvs by default, mirroring your\n", " local packages).\n", - "6. **Generate and upload `job.sh`** -- no `#SBATCH`/`#PBS`/`#$` directives,\n", + "6. **Generate and upload `job.sh`** -- no `#SBATCH` directives,\n", " just `cd`, environment setup, and the same execution/result-signing body\n", " every backend shares.\n", "7. **Run it in the background**: `nohup bash job.sh > job.out 2> job.err &`\n", " over the existing SSH connection -- there is no scheduler to submit to,\n", - " so there is also no job ID in the SLURM/PBS/SGE sense; clustrix invents\n", + " so there is also no job ID in the SLURM sense; clustrix invents\n", " one (`ssh_`) purely to track the job locally.\n", "8. **Poll** for completion.\n", "9. **Verify, then deserialize** the HMAC-signed result -- refused outright\n", @@ -89,9 +89,9 @@ "from clustrix.config import ClusterConfig\n", "import numpy as np\n", "\n", - "print(\"\u2705 Clustrix imported successfully!\")\n", - "print(\"\ud83d\udcf1 Look for the interactive widget that appeared above or below.\")\n", - "print(\"\ud83d\udd11 You can use the widget's SSH Key Setup section for easy configuration.\")" + "print(\"✅ Clustrix imported successfully!\")\n", + "print(\"📱 Look for the interactive widget that appeared above or below.\")\n", + "print(\"🔑 You can use the widget's SSH Key Setup section for easy configuration.\")" ], "id": "cell-3" }, @@ -99,7 +99,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \ud83d\udd11 Step 1: Automated SSH Key Setup\n", + "## 🔑 Step 1: Automated SSH Key Setup\n", "\n", "**This is the magic step!** Instead of manually setting up SSH keys, Clustrix does it automatically." ], @@ -111,7 +111,7 @@ "metadata": {}, "outputs": [], "source": [ - "# \ud83d\udd27 Configure your remote server details\n", + "# 🔧 Configure your remote server details\n", "# Replace these with your actual server information\n", "\n", "config = ClusterConfig(\n", @@ -127,11 +127,11 @@ " max_parallel_jobs=5, # Limit concurrent executions\n", ")\n", "\n", - "print(\"\u2705 Server configuration created!\")\n", - "print(f\"\ud83c\udfaf Target: {config.cluster_host}\")\n", - "print(f\"\ud83d\udc64 User: {config.username}\")\n", - "print(f\"\ud83d\udd0c Port: {config.cluster_port}\")\n", - "print(\"\\n\ud83d\udd11 Ready for automated SSH key setup...\")" + "print(\"✅ Server configuration created!\")\n", + "print(f\"🎯 Target: {config.cluster_host}\")\n", + "print(f\"👤 User: {config.username}\")\n", + "print(f\"🔌 Port: {config.cluster_port}\")\n", + "print(\"\\n🔑 Ready for automated SSH key setup...\")" ], "id": "cell-5" }, @@ -141,11 +141,11 @@ "metadata": {}, "outputs": [], "source": [ - "# \ud83d\ude80 AUTOMATED SSH KEY SETUP\n", + "# 🚀 AUTOMATED SSH KEY SETUP\n", "# One call replaces generating, deploying and configuring the key by hand.\n", "\n", - "print(\"\ud83d\udd04 Setting up SSH keys automatically...\")\n", - "print(\"\ud83d\udca1 You'll be prompted for your password (this is normal and secure).\")\n", + "print(\"🔄 Setting up SSH keys automatically...\")\n", + "print(\"💡 You'll be prompted for your password (this is normal and secure).\")\n", "print()\n", "\n", "ssh_result = setup_ssh_keys_with_fallback(\n", @@ -155,39 +155,39 @@ " force_refresh=False, # Set True to generate new keys\n", ")\n", "\n", - "# \ud83d\udcca Display results\n", + "# 📊 Display results\n", "print(\"\\n\" + \"=\"*60)\n", - "print(\"\ud83d\udd11 SSH KEY SETUP RESULTS\")\n", + "print(\"🔑 SSH KEY SETUP RESULTS\")\n", "print(\"=\"*60)\n", "\n", "if ssh_result[\"success\"]:\n", - " print(\"\ud83c\udf89 SUCCESS! SSH keys configured automatically!\")\n", - " print(f\"\ud83d\udd11 Key path: {ssh_result['key_path']}\")\n", - " print(f\"\ud83d\udce6 Key already existed: {ssh_result['key_already_existed']}\")\n", - " print(f\"\ud83d\ude80 Key deployed: {ssh_result['key_deployed']}\")\n", - " print(f\"\ud83d\udd17 Connection tested: {ssh_result['connection_tested']}\")\n", + " print(\"🎉 SUCCESS! SSH keys configured automatically!\")\n", + " print(f\"🔑 Key path: {ssh_result['key_path']}\")\n", + " print(f\"📦 Key already existed: {ssh_result['key_already_existed']}\")\n", + " print(f\"🚀 Key deployed: {ssh_result['key_deployed']}\")\n", + " print(f\"🔗 Connection tested: {ssh_result['connection_tested']}\")\n", " \n", " if \"ssh_config_updated\" in ssh_result.get(\"details\", {}):\n", - " print(\"\u2699\ufe0f SSH config updated with alias\")\n", - " print(\"\\n\ud83c\udfaf You can now connect with: ssh my_server\")\n", - " \n", - " print(\"\\n\u2728 What just happened:\")\n", - " print(\" \ud83d\udd10 Generated Ed25519 SSH key pair\")\n", - " print(\" \ud83d\udce4 Deployed public key to remote server\")\n", - " print(\" \ud83e\uddf9 Cleaned up any conflicting old keys\")\n", - " print(\" \u2699\ufe0f Updated SSH configuration\")\n", - " print(\" \u2705 Tested connection to verify success\")\n", + " print(\"⚙️ SSH config updated with alias\")\n", + " print(\"\\n🎯 You can now connect with: ssh my_server\")\n", + " \n", + " print(\"\\n✨ What just happened:\")\n", + " print(\" 🔐 Generated Ed25519 SSH key pair\")\n", + " print(\" 📤 Deployed public key to remote server\")\n", + " print(\" 🧹 Cleaned up any conflicting old keys\")\n", + " print(\" ⚙️ Updated SSH configuration\")\n", + " print(\" ✅ Tested connection to verify success\")\n", " \n", "else:\n", - " print(\"\u274c SSH key setup failed\")\n", - " print(f\"\ud83d\udd0d Error: {ssh_result.get('error', 'Unknown error')}\")\n", + " print(\"❌ SSH key setup failed\")\n", + " print(f\"🔍 Error: {ssh_result.get('error', 'Unknown error')}\")\n", " \n", " if \"details\" in ssh_result:\n", - " print(\"\\n\ud83d\udd27 Troubleshooting details:\")\n", + " print(\"\\n🔧 Troubleshooting details:\")\n", " for key, value in ssh_result[\"details\"].items():\n", " print(f\" {key}: {value}\")\n", " \n", - " print(\"\\n\ud83d\udca1 Try:\")\n", + " print(\"\\n💡 Try:\")\n", " print(\" - Check hostname and username are correct\")\n", " print(\" - Verify network connectivity to the server\")\n", " print(\" - Test manual SSH connection first\")\n", @@ -200,7 +200,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \u2699\ufe0f Step 2: Configure Clustrix\n", + "## ⚙️ Step 2: Configure Clustrix\n", "\n", "Now that SSH keys are set up, configure Clustrix for remote execution:" ], @@ -232,11 +232,11 @@ " # python_executable=\"/path/to/venv/bin/python\", # Point at a venv's interpreter\n", ")\n", "\n", - "print(\"\u2705 Clustrix configured for SSH remote execution!\")\n", - "print(f\"\ud83c\udfaf Target server: {config.cluster_host}\")\n", - "print(f\"\ud83d\udcc1 Remote work directory: {config.remote_work_dir}\")\n", - "print(f\"\ud83d\udc0d Python executable: {config.python_executable}\")\n", - "print(\"\\n\ud83d\ude80 Ready to execute functions remotely!\")" + "print(\"✅ Clustrix configured for SSH remote execution!\")\n", + "print(f\"🎯 Target server: {config.cluster_host}\")\n", + "print(f\"📁 Remote work directory: {config.remote_work_dir}\")\n", + "print(f\"🐍 Python executable: {config.python_executable}\")\n", + "print(\"\\n🚀 Ready to execute functions remotely!\")" ], "id": "cell-8" }, @@ -244,7 +244,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \ud83e\uddee Example 1: Basic Remote Computation\n", + "## 🧮 Example 1: Basic Remote Computation\n", "\n", "Execute a simple mathematical computation remotely:" ], @@ -266,10 +266,10 @@ " import platform\n", " from datetime import datetime\n", " \n", - " print(f\"\ud83d\udda5\ufe0f Executing on: {platform.node()}\")\n", - " print(f\"\ud83d\udc0d Python version: {platform.python_version()}\")\n", - " print(f\"\u26a1 Starting computation at {datetime.now()}\")\n", - " print(f\"\ud83d\udd22 Computing sum of squares for {n:,} numbers\")\n", + " print(f\"🖥️ Executing on: {platform.node()}\")\n", + " print(f\"🐍 Python version: {platform.python_version()}\")\n", + " print(f\"⚡ Starting computation at {datetime.now()}\")\n", + " print(f\"🔢 Computing sum of squares for {n:,} numbers\")\n", " \n", " start_time = time.time()\n", " \n", @@ -294,21 +294,21 @@ " 'completion_time': datetime.now().isoformat()\n", " }\n", " \n", - " print(f\"\u2705 Computation completed in {execution_time:.2f} seconds\")\n", + " print(f\"✅ Computation completed in {execution_time:.2f} seconds\")\n", " return result\n", "\n", "# Execute on remote server\n", - "print(\"\ud83d\ude80 Executing basic computation on remote server...\")\n", + "print(\"🚀 Executing basic computation on remote server...\")\n", "result = basic_remote_computation(500000)\n", "\n", - "print(f\"\\n\ud83c\udf89 REMOTE COMPUTATION COMPLETE\")\n", - "print(f\"\ud83d\udda5\ufe0f Executed on: {result['hostname']}\")\n", - "print(f\"\ud83d\udc0d Python version: {result['python_version']}\")\n", - "print(f\"\ud83d\udd22 Numbers processed: {result['n']:,}\")\n", - "print(f\"\ud83d\udcca Sum of squares: {result['sum_of_squares']:,}\")\n", - "print(f\"\ud83d\udcd0 Square root of sum: {result['sqrt_sum']:,.2f}\")\n", - "print(f\"\u23f1\ufe0f Execution time: {result['execution_time_seconds']:.2f} seconds\")\n", - "print(f\"\ud83d\udd50 Completed at: {result['completion_time']}\")" + "print(f\"\\n🎉 REMOTE COMPUTATION COMPLETE\")\n", + "print(f\"🖥️ Executed on: {result['hostname']}\")\n", + "print(f\"🐍 Python version: {result['python_version']}\")\n", + "print(f\"🔢 Numbers processed: {result['n']:,}\")\n", + "print(f\"📊 Sum of squares: {result['sum_of_squares']:,}\")\n", + "print(f\"📐 Square root of sum: {result['sqrt_sum']:,.2f}\")\n", + "print(f\"⏱️ Execution time: {result['execution_time_seconds']:.2f} seconds\")\n", + "print(f\"🕐 Completed at: {result['completion_time']}\")" ], "id": "cell-10" }, @@ -316,7 +316,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \ud83d\udcca Example 2: Remote Data Processing with NumPy\n", + "## 📊 Example 2: Remote Data Processing with NumPy\n", "\n", "Process numerical data on the remote server:" ], @@ -338,35 +338,35 @@ " import platform\n", " from datetime import datetime\n", " \n", - " print(f\"\ud83d\udda5\ufe0f Remote execution on: {platform.node()}\")\n", - " print(f\"\ud83d\udcca NumPy version: {np.__version__}\")\n", - " print(f\"\ud83d\udd22 Matrix size: {matrix_size}x{matrix_size}\")\n", - " print(f\"\ud83d\udd04 Iterations: {num_iterations}\")\n", + " print(f\"🖥️ Remote execution on: {platform.node()}\")\n", + " print(f\"📊 NumPy version: {np.__version__}\")\n", + " print(f\"🔢 Matrix size: {matrix_size}x{matrix_size}\")\n", + " print(f\"🔄 Iterations: {num_iterations}\")\n", " \n", " results = []\n", " total_start_time = time.time()\n", " \n", " for iteration in range(num_iterations):\n", - " print(f\"\\n\ud83d\udd04 Iteration {iteration + 1}/{num_iterations}\")\n", + " print(f\"\\n🔄 Iteration {iteration + 1}/{num_iterations}\")\n", " \n", " start_time = time.time()\n", " \n", " # Generate random matrices\n", - " print(\" \ud83d\udccb Generating random matrices...\")\n", + " print(\" 📋 Generating random matrices...\")\n", " A = np.random.randn(matrix_size, matrix_size)\n", " B = np.random.randn(matrix_size, matrix_size)\n", " \n", " # Matrix multiplication\n", - " print(\" \u2716\ufe0f Performing matrix multiplication...\")\n", + " print(\" ✖️ Performing matrix multiplication...\")\n", " C = np.dot(A, B)\n", " \n", " # Eigenvalue computation (smaller matrix for speed)\n", " small_size = min(100, matrix_size)\n", - " print(f\" \ud83e\uddee Computing eigenvalues ({small_size}x{small_size})...\")\n", + " print(f\" 🧮 Computing eigenvalues ({small_size}x{small_size})...\")\n", " eigenvalues = np.linalg.eigvals(A[:small_size, :small_size])\n", " \n", " # Statistical analysis\n", - " print(\" \ud83d\udcc8 Computing statistics...\")\n", + " print(\" 📈 Computing statistics...\")\n", " stats = {\n", " 'matrix_mean': float(np.mean(C)),\n", " 'matrix_std': float(np.std(C)),\n", @@ -387,7 +387,7 @@ " }\n", " \n", " results.append(iteration_result)\n", - " print(f\" \u23f1\ufe0f Iteration completed in {iteration_time:.2f} seconds\")\n", + " print(f\" ⏱️ Iteration completed in {iteration_time:.2f} seconds\")\n", " \n", " total_end_time = time.time()\n", " total_time = total_end_time - total_start_time\n", @@ -413,41 +413,41 @@ " 'iteration_results': results\n", " }\n", " \n", - " print(f\"\\n\u2705 All computations completed!\")\n", - " print(f\"\u23f1\ufe0f Total execution time: {total_time:.2f} seconds\")\n", - " print(f\"\ud83d\udcca Average iteration time: {np.mean(execution_times):.2f} seconds\")\n", + " print(f\"\\n✅ All computations completed!\")\n", + " print(f\"⏱️ Total execution time: {total_time:.2f} seconds\")\n", + " print(f\"📊 Average iteration time: {np.mean(execution_times):.2f} seconds\")\n", " \n", " return final_result\n", "\n", "# Execute numerical computation on remote server\n", - "print(\"\ud83d\ude80 Starting remote NumPy computation...\")\n", + "print(\"🚀 Starting remote NumPy computation...\")\n", "numpy_result = remote_numpy_computation(matrix_size=500, num_iterations=3)\n", "\n", - "print(f\"\\n\ud83c\udf89 REMOTE NUMPY COMPUTATION COMPLETE\")\n", + "print(f\"\\n🎉 REMOTE NUMPY COMPUTATION COMPLETE\")\n", "info = numpy_result['computation_info']\n", - "print(f\"\ud83d\udda5\ufe0f Executed on: {info['hostname']}\")\n", - "print(f\"\ud83d\udcca NumPy version: {info['numpy_version']}\")\n", - "print(f\"\ud83d\udd22 Matrix size: {info['matrix_size']}x{info['matrix_size']}\")\n", - "print(f\"\ud83d\udd04 Iterations: {info['num_iterations']}\")\n", + "print(f\"🖥️ Executed on: {info['hostname']}\")\n", + "print(f\"📊 NumPy version: {info['numpy_version']}\")\n", + "print(f\"🔢 Matrix size: {info['matrix_size']}x{info['matrix_size']}\")\n", + "print(f\"🔄 Iterations: {info['num_iterations']}\")\n", "\n", "perf = numpy_result['performance']\n", - "print(f\"\\n\ud83d\udcc8 Performance Metrics:\")\n", - "print(f\" \u23f1\ufe0f Total time: {perf['total_time']:.2f} seconds\")\n", - "print(f\" \ud83d\udcca Average iteration: {perf['average_iteration_time']:.2f} seconds\")\n", - "print(f\" \u26a1 Operations/second: {perf['operations_per_second']:,.0f}\")\n", - "print(f\" \ud83c\udfc3 Fastest iteration: {perf['min_iteration_time']:.2f} seconds\")\n", - "print(f\" \ud83d\udc0c Slowest iteration: {perf['max_iteration_time']:.2f} seconds\")\n", + "print(f\"\\n📈 Performance Metrics:\")\n", + "print(f\" ⏱️ Total time: {perf['total_time']:.2f} seconds\")\n", + "print(f\" 📊 Average iteration: {perf['average_iteration_time']:.2f} seconds\")\n", + "print(f\" ⚡ Operations/second: {perf['operations_per_second']:,.0f}\")\n", + "print(f\" 🏃 Fastest iteration: {perf['min_iteration_time']:.2f} seconds\")\n", + "print(f\" 🐌 Slowest iteration: {perf['max_iteration_time']:.2f} seconds\")\n", "\n", "# Show statistics from the last iteration\n", "if numpy_result['iteration_results']:\n", " last_stats = numpy_result['iteration_results'][-1]['statistics']\n", - " print(f\"\\n\ud83d\udcca Final Matrix Statistics:\")\n", - " print(f\" \ud83d\udcc8 Mean: {last_stats['matrix_mean']:.4f}\")\n", - " print(f\" \ud83d\udcca Std Dev: {last_stats['matrix_std']:.4f}\")\n", - " print(f\" \ud83d\udd3a Max: {last_stats['matrix_max']:.4f}\")\n", - " print(f\" \ud83d\udd3b Min: {last_stats['matrix_min']:.4f}\")\n", - " print(f\" \ud83e\uddee Eigenvalue Mean: {last_stats['eigenvalue_mean']:.4f}\")\n", - " print(f\" \ud83d\udccf Frobenius Norm: {last_stats['frobenius_norm']:.2f}\")" + " print(f\"\\n📊 Final Matrix Statistics:\")\n", + " print(f\" 📈 Mean: {last_stats['matrix_mean']:.4f}\")\n", + " print(f\" 📊 Std Dev: {last_stats['matrix_std']:.4f}\")\n", + " print(f\" 🔺 Max: {last_stats['matrix_max']:.4f}\")\n", + " print(f\" 🔻 Min: {last_stats['matrix_min']:.4f}\")\n", + " print(f\" 🧮 Eigenvalue Mean: {last_stats['eigenvalue_mean']:.4f}\")\n", + " print(f\" 📏 Frobenius Norm: {last_stats['frobenius_norm']:.2f}\")" ], "id": "cell-12" }, @@ -455,7 +455,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \ud83d\uddc2\ufe0f Example 3: Remote File System Analysis\n", + "## 🗂️ Example 3: Remote File System Analysis\n", "\n", "Analyze the file system structure on the remote server:" ], @@ -479,7 +479,7 @@ " import psutil # Common on many systems\n", " from datetime import datetime\n", " \n", - " print(f\"\ud83d\udda5\ufe0f Analyzing system: {platform.node()}\")\n", + " print(f\"🖥️ Analyzing system: {platform.node()}\")\n", " \n", " # Basic system information\n", " system_info = {\n", @@ -493,9 +493,9 @@ " 'architecture': platform.architecture(),\n", " }\n", " \n", - " print(f\"\ud83d\udcbb System: {system_info['system']} {system_info['release']}\")\n", - " print(f\"\ud83c\udfd7\ufe0f Architecture: {system_info['machine']}\")\n", - " print(f\"\ud83d\udc0d Python: {system_info['python_version']}\")\n", + " print(f\"💻 System: {system_info['system']} {system_info['release']}\")\n", + " print(f\"🏗️ Architecture: {system_info['machine']}\")\n", + " print(f\"🐍 Python: {system_info['python_version']}\")\n", " \n", " # Memory and CPU information\n", " try:\n", @@ -507,17 +507,17 @@ " 'memory_available_gb': memory.available / (1024**3),\n", " 'memory_percent': memory.percent,\n", " }\n", - " print(f\"\u26a1 CPUs: {cpu_info['cpu_count']}\")\n", - " print(f\"\ud83e\udde0 Memory: {cpu_info['memory_total_gb']:.1f} GB total, {cpu_info['memory_available_gb']:.1f} GB available\")\n", + " print(f\"⚡ CPUs: {cpu_info['cpu_count']}\")\n", + " print(f\"🧠 Memory: {cpu_info['memory_total_gb']:.1f} GB total, {cpu_info['memory_available_gb']:.1f} GB available\")\n", " except ImportError:\n", - " print(\"\ud83d\udcca psutil not available, skipping detailed system metrics\")\n", + " print(\"📊 psutil not available, skipping detailed system metrics\")\n", " cpu_info = {'error': 'psutil not available'}\n", " \n", " # Disk usage analysis\n", " disk_info = {}\n", " important_paths = ['/', '/home', '/tmp', '/var', '/usr']\n", " \n", - " print(\"\\n\ud83d\udcbe Disk Usage Analysis:\")\n", + " print(\"\\n💾 Disk Usage Analysis:\")\n", " for path in important_paths:\n", " if os.path.exists(path):\n", " try:\n", @@ -528,7 +528,7 @@ " 'free_gb': usage.free / (1024**3),\n", " 'used_percent': (usage.used / usage.total) * 100\n", " }\n", - " print(f\" \ud83d\udcc1 {path}: {disk_info[path]['used_gb']:.1f}GB used / {disk_info[path]['total_gb']:.1f}GB total ({disk_info[path]['used_percent']:.1f}%)\")\n", + " print(f\" 📁 {path}: {disk_info[path]['used_gb']:.1f}GB used / {disk_info[path]['total_gb']:.1f}GB total ({disk_info[path]['used_percent']:.1f}%)\")\n", " except (OSError, PermissionError):\n", " disk_info[path] = {'error': 'Permission denied or path inaccessible'}\n", " \n", @@ -541,14 +541,14 @@ " 'working_directory': os.getcwd(),\n", " }\n", " \n", - " print(f\"\\n\ud83d\udc64 Environment Info:\")\n", + " print(f\"\\n👤 Environment Info:\")\n", " print(f\" User: {env_info['user']}\")\n", " print(f\" Home: {env_info['home']}\")\n", " print(f\" Shell: {env_info['shell']}\")\n", " print(f\" Working Dir: {env_info['working_directory']}\")\n", " \n", " # Available Python packages\n", - " print(\"\\n\ud83d\udc0d Checking Python Environment:\")\n", + " print(\"\\n🐍 Checking Python Environment:\")\n", " common_packages = [\n", " 'numpy', 'pandas', 'scipy', 'matplotlib', 'sklearn', 'requests',\n", " 'psutil', 'jupyter', 'ipython', 'pytest', 'click', 'flask'\n", @@ -569,7 +569,7 @@ " package_status[package] = {'available': False}\n", " \n", " available_packages = [pkg for pkg, info in package_status.items() if info['available']]\n", - " print(f\" \u2705 Available packages ({len(available_packages)}/{len(common_packages)}): {', '.join(available_packages[:8])}\")\n", + " print(f\" ✅ Available packages ({len(available_packages)}/{len(common_packages)}): {', '.join(available_packages[:8])}\")\n", " \n", " # Network connectivity test\n", " network_info = {}\n", @@ -582,10 +582,10 @@ " 'ip_address': ip_address,\n", " 'connectivity': 'basic_ok'\n", " }\n", - " print(f\"\\n\ud83c\udf10 Network: {hostname} ({ip_address})\")\n", + " print(f\"\\n🌐 Network: {hostname} ({ip_address})\")\n", " except Exception as e:\n", " network_info = {'error': str(e)}\n", - " print(f\"\\n\ud83c\udf10 Network: Error getting network info\")\n", + " print(f\"\\n🌐 Network: Error getting network info\")\n", " \n", " # Final analysis result\n", " analysis_result = {\n", @@ -601,43 +601,43 @@ " 'network_info': network_info\n", " }\n", " \n", - " print(f\"\\n\u2705 System analysis completed!\")\n", + " print(f\"\\n✅ System analysis completed!\")\n", " return analysis_result\n", "\n", "# Analyze remote system\n", - "print(\"\ud83d\ude80 Starting remote system analysis...\")\n", + "print(\"🚀 Starting remote system analysis...\")\n", "system_result = remote_system_analysis()\n", "\n", - "print(f\"\\n\ud83c\udf89 REMOTE SYSTEM ANALYSIS COMPLETE\")\n", + "print(f\"\\n🎉 REMOTE SYSTEM ANALYSIS COMPLETE\")\n", "sys_info = system_result['system_information']\n", - "print(f\"\ud83d\udda5\ufe0f System: {sys_info['hostname']} ({sys_info['system']} {sys_info['release']})\")\n", - "print(f\"\ud83c\udfd7\ufe0f Architecture: {sys_info['machine']}\")\n", - "print(f\"\ud83d\udc0d Python: {sys_info['python_version']}\")\n", + "print(f\"🖥️ System: {sys_info['hostname']} ({sys_info['system']} {sys_info['release']})\")\n", + "print(f\"🏗️ Architecture: {sys_info['machine']}\")\n", + "print(f\"🐍 Python: {sys_info['python_version']}\")\n", "\n", "if 'error' not in system_result['performance_info']:\n", " perf = system_result['performance_info']\n", - " print(f\"\\n\ud83d\udcca Performance:\")\n", - " print(f\" \u26a1 CPUs: {perf['cpu_count']}\")\n", - " print(f\" \ud83e\udde0 Memory: {perf['memory_total_gb']:.1f} GB ({perf['memory_percent']:.1f}% used)\")\n", - " print(f\" \ud83d\udd25 CPU Usage: {perf['cpu_percent']:.1f}%\")\n", + " print(f\"\\n📊 Performance:\")\n", + " print(f\" ⚡ CPUs: {perf['cpu_count']}\")\n", + " print(f\" 🧠 Memory: {perf['memory_total_gb']:.1f} GB ({perf['memory_percent']:.1f}% used)\")\n", + " print(f\" 🔥 CPU Usage: {perf['cpu_percent']:.1f}%\")\n", "\n", "env = system_result['environment']\n", - "print(f\"\\n\ud83d\udc64 Environment:\")\n", + "print(f\"\\n👤 Environment:\")\n", "print(f\" User: {env['user']}\")\n", "print(f\" Home: {env['home']}\")\n", "print(f\" Working Dir: {env['working_directory']}\")\n", "\n", "packages = system_result['python_packages']\n", "available = [pkg for pkg, info in packages.items() if info['available']]\n", - "print(f\"\\n\ud83d\udc0d Python Environment:\")\n", - "print(f\" \ud83d\udce6 Available packages: {len(available)}/{len(packages)}\")\n", - "print(f\" \u2705 Key packages: {', '.join(available[:6])}\")\n", + "print(f\"\\n🐍 Python Environment:\")\n", + "print(f\" 📦 Available packages: {len(available)}/{len(packages)}\")\n", + "print(f\" ✅ Key packages: {', '.join(available[:6])}\")\n", "\n", "disk = system_result['disk_usage']\n", - "print(f\"\\n\ud83d\udcbe Storage:\")\n", + "print(f\"\\n💾 Storage:\")\n", "for path, info in disk.items():\n", " if 'error' not in info:\n", - " print(f\" \ud83d\udcc1 {path}: {info['free_gb']:.1f} GB free\")" + " print(f\" 📁 {path}: {info['free_gb']:.1f} GB free\")" ], "id": "cell-14" }, @@ -645,7 +645,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \ud83e\uddea Example 4: Remote Environment Testing\n", + "## 🧪 Example 4: Remote Environment Testing\n", "\n", "Test specific capabilities and benchmark performance:" ], @@ -667,11 +667,11 @@ " import platform\n", " from datetime import datetime\n", " \n", - " print(f\"\ud83c\udfc1 Starting performance benchmarks on {platform.node()}\")\n", + " print(f\"🏁 Starting performance benchmarks on {platform.node()}\")\n", " benchmarks = {}\n", " \n", " # CPU benchmark: Prime number calculation\n", - " print(\"\\n\ud83d\udd22 CPU Benchmark: Prime number calculation\")\n", + " print(\"\\n🔢 CPU Benchmark: Prime number calculation\")\n", " start_time = time.time()\n", " \n", " def is_prime(n):\n", @@ -693,11 +693,11 @@ " 'primes_per_second': len(primes) / cpu_time\n", " }\n", " \n", - " print(f\" \u2705 Found {len(primes)} primes in {cpu_time:.3f} seconds\")\n", - " print(f\" \ud83d\udcca Rate: {len(primes) / cpu_time:.1f} primes/second\")\n", + " print(f\" ✅ Found {len(primes)} primes in {cpu_time:.3f} seconds\")\n", + " print(f\" 📊 Rate: {len(primes) / cpu_time:.1f} primes/second\")\n", " \n", " # Memory benchmark: List operations\n", - " print(\"\\n\ud83e\udde0 Memory Benchmark: Large list operations\")\n", + " print(\"\\n🧠 Memory Benchmark: Large list operations\")\n", " start_time = time.time()\n", " \n", " # Create large list\n", @@ -718,11 +718,11 @@ " 'sum_result': list_sum\n", " }\n", " \n", - " print(f\" \u2705 Processed {len(large_list):,} elements in {memory_time:.3f} seconds\")\n", - " print(f\" \ud83d\udcca Rate: {len(large_list) / memory_time:,.0f} elements/second\")\n", + " print(f\" ✅ Processed {len(large_list):,} elements in {memory_time:.3f} seconds\")\n", + " print(f\" 📊 Rate: {len(large_list) / memory_time:,.0f} elements/second\")\n", " \n", " # I/O benchmark: File operations\n", - " print(\"\\n\ud83d\udcc1 I/O Benchmark: File read/write operations\")\n", + " print(\"\\n📁 I/O Benchmark: File read/write operations\")\n", " import tempfile\n", " import os\n", " \n", @@ -755,11 +755,11 @@ " 'throughput_mb_per_sec': (file_size / (1024*1024)) / io_time\n", " }\n", " \n", - " print(f\" \u2705 Wrote/read {file_size:,} bytes in {io_time:.3f} seconds\")\n", - " print(f\" \ud83d\udcca Throughput: {(file_size / (1024*1024)) / io_time:.2f} MB/second\")\n", + " print(f\" ✅ Wrote/read {file_size:,} bytes in {io_time:.3f} seconds\")\n", + " print(f\" 📊 Throughput: {(file_size / (1024*1024)) / io_time:.2f} MB/second\")\n", " \n", " # Mathematical benchmark: Floating point operations\n", - " print(\"\\n\ud83e\uddee Math Benchmark: Floating point operations\")\n", + " print(\"\\n🧮 Math Benchmark: Floating point operations\")\n", " start_time = time.time()\n", " \n", " total = 0.0\n", @@ -776,8 +776,8 @@ " 'operations_per_second': (100000 * 3) / math_time\n", " }\n", " \n", - " print(f\" \u2705 Performed {100000 * 3:,} operations in {math_time:.3f} seconds\")\n", - " print(f\" \ud83d\udcca Rate: {(100000 * 3) / math_time:,.0f} operations/second\")\n", + " print(f\" ✅ Performed {100000 * 3:,} operations in {math_time:.3f} seconds\")\n", + " print(f\" 📊 Rate: {(100000 * 3) / math_time:,.0f} operations/second\")\n", " \n", " # Summary\n", " total_benchmark_time = sum([b['execution_time'] for b in benchmarks.values()])\n", @@ -794,37 +794,37 @@ " 'benchmarks': benchmarks\n", " }\n", " \n", - " print(f\"\\n\ud83c\udfc1 All benchmarks completed!\")\n", - " print(f\"\u23f1\ufe0f Total benchmark time: {total_benchmark_time:.3f} seconds\")\n", + " print(f\"\\n🏁 All benchmarks completed!\")\n", + " print(f\"⏱️ Total benchmark time: {total_benchmark_time:.3f} seconds\")\n", " \n", " return result\n", "\n", "# Run performance benchmarks\n", - "print(\"\ud83d\ude80 Starting remote performance benchmarks...\")\n", + "print(\"🚀 Starting remote performance benchmarks...\")\n", "benchmark_result = benchmark_remote_performance()\n", "\n", - "print(f\"\\n\ud83c\udf89 REMOTE BENCHMARKS COMPLETE\")\n", + "print(f\"\\n🎉 REMOTE BENCHMARKS COMPLETE\")\n", "meta = benchmark_result['benchmark_metadata']\n", - "print(f\"\ud83d\udda5\ufe0f System: {meta['hostname']} ({meta['system']} {meta['machine']})\")\n", - "print(f\"\ud83d\udc0d Python: {meta['python_version']}\")\n", - "print(f\"\u23f1\ufe0f Total time: {meta['total_benchmark_time']:.3f} seconds\")\n", + "print(f\"🖥️ System: {meta['hostname']} ({meta['system']} {meta['machine']})\")\n", + "print(f\"🐍 Python: {meta['python_version']}\")\n", + "print(f\"⏱️ Total time: {meta['total_benchmark_time']:.3f} seconds\")\n", "\n", "benchmarks = benchmark_result['benchmarks']\n", "\n", - "print(f\"\\n\ud83d\udcca Benchmark Results:\")\n", + "print(f\"\\n📊 Benchmark Results:\")\n", "cpu = benchmarks['cpu_benchmark']\n", - "print(f\" \ud83d\udd22 CPU: {cpu['primes_per_second']:.1f} primes/sec\")\n", + "print(f\" 🔢 CPU: {cpu['primes_per_second']:.1f} primes/sec\")\n", "\n", "memory = benchmarks['memory_benchmark']\n", - "print(f\" \ud83e\udde0 Memory: {len(memory['operations'])} ops on {memory['list_size']:,} elements in {memory['execution_time']:.3f}s\")\n", + "print(f\" 🧠 Memory: {len(memory['operations'])} ops on {memory['list_size']:,} elements in {memory['execution_time']:.3f}s\")\n", "\n", "io = benchmarks['io_benchmark']\n", - "print(f\" \ud83d\udcc1 I/O: {io['throughput_mb_per_sec']:.2f} MB/sec throughput\")\n", + "print(f\" 📁 I/O: {io['throughput_mb_per_sec']:.2f} MB/sec throughput\")\n", "\n", "math_bench = benchmarks['math_benchmark']\n", - "print(f\" \ud83e\uddee Math: {math_bench['operations_per_second']:,.0f} ops/sec\")\n", + "print(f\" 🧮 Math: {math_bench['operations_per_second']:,.0f} ops/sec\")\n", "\n", - "print(f\"\\n\ud83c\udfc6 Remote server performance profile complete!\")" + "print(f\"\\n🏆 Remote server performance profile complete!\")" ], "id": "cell-16" }, @@ -832,7 +832,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \ud83d\udd27 SSH Connection Testing and Troubleshooting\n", + "## 🔧 SSH Connection Testing and Troubleshooting\n", "\n", "Test your SSH connection and get troubleshooting information:" ], @@ -852,61 +852,61 @@ " from clustrix.executor import ClusterExecutor\n", " \n", " try:\n", - " print(\"\ud83d\udd0d Testing SSH connection...\")\n", + " print(\"🔍 Testing SSH connection...\")\n", " config = get_config()\n", " \n", " if config.cluster_type != 'ssh':\n", - " print(\"\u274c Current configuration is not for SSH.\")\n", - " print(\"\ud83d\udca1 Please run the SSH configuration cell above first.\")\n", + " print(\"❌ Current configuration is not for SSH.\")\n", + " print(\"💡 Please run the SSH configuration cell above first.\")\n", " return False\n", " \n", - " print(f\"\ud83c\udfaf Target: {config.cluster_host}:{getattr(config, 'cluster_port', 22)}\")\n", - " print(f\"\ud83d\udc64 User: {config.username}\")\n", - " print(f\"\ud83d\udd11 Key: {getattr(config, 'key_file', 'auto-detected')}\")\n", + " print(f\"🎯 Target: {config.cluster_host}:{getattr(config, 'cluster_port', 22)}\")\n", + " print(f\"👤 User: {config.username}\")\n", + " print(f\"🔑 Key: {getattr(config, 'key_file', 'auto-detected')}\")\n", " \n", " # Test basic connection\n", " executor = ClusterExecutor(config)\n", " executor.connect()\n", - " print(\"\u2705 SSH connection successful!\")\n", + " print(\"✅ SSH connection successful!\")\n", " \n", " # Test basic commands\n", - " print(\"\\n\ud83e\uddea Testing basic commands...\")\n", + " print(\"\\n🧪 Testing basic commands...\")\n", " commands = [\n", - " (\"hostname\", \"\ud83d\udda5\ufe0f Remote hostname\"),\n", - " (\"whoami\", \"\ud83d\udc64 Remote user\"),\n", - " (\"pwd\", \"\ud83d\udcc1 Working directory\"),\n", - " (\"python3 --version\", \"\ud83d\udc0d Python version\"),\n", - " (\"uname -a\", \"\ud83d\udcbb System info\")\n", + " (\"hostname\", \"🖥️ Remote hostname\"),\n", + " (\"whoami\", \"👤 Remote user\"),\n", + " (\"pwd\", \"📁 Working directory\"),\n", + " (\"python3 --version\", \"🐍 Python version\"),\n", + " (\"uname -a\", \"💻 System info\")\n", " ]\n", " \n", " for cmd, description in commands:\n", " try:\n", " stdout, stderr = executor._execute_command(cmd)\n", " output = (stdout or stderr or \"no output\").strip()\n", - " print(f\" \u2705 {description}: {output}\")\n", + " print(f\" ✅ {description}: {output}\")\n", " except Exception as e:\n", - " print(f\" \u274c {description}: {str(e)}\")\n", + " print(f\" ❌ {description}: {str(e)}\")\n", " \n", " # Test work directory\n", " work_dir = getattr(config, 'remote_work_dir', '~/.clustrix/jobs')\n", - " print(f\"\\n\ud83d\udcc1 Testing work directory: {work_dir}\")\n", + " print(f\"\\n📁 Testing work directory: {work_dir}\")\n", " try:\n", " stdout, stderr = executor._execute_command(f\"mkdir -p {work_dir} && echo 'Directory OK'\")\n", " if \"Directory OK\" in stdout:\n", - " print(f\" \u2705 Work directory accessible and writable\")\n", + " print(f\" ✅ Work directory accessible and writable\")\n", " else:\n", - " print(f\" \u26a0\ufe0f Work directory test inconclusive\")\n", + " print(f\" ⚠️ Work directory test inconclusive\")\n", " except Exception as e:\n", - " print(f\" \u274c Work directory error: {e}\")\n", + " print(f\" ❌ Work directory error: {e}\")\n", " \n", " executor.disconnect()\n", - " print(\"\\n\ud83c\udf89 SSH connection test completed successfully!\")\n", - " print(\"\u2705 Your SSH configuration is working correctly.\")\n", + " print(\"\\n🎉 SSH connection test completed successfully!\")\n", + " print(\"✅ Your SSH configuration is working correctly.\")\n", " return True\n", " \n", " except Exception as e:\n", - " print(f\"\\n\u274c SSH connection test failed: {e}\")\n", - " print(\"\\n\ud83d\udd27 Troubleshooting suggestions:\")\n", + " print(f\"\\n❌ SSH connection test failed: {e}\")\n", + " print(\"\\n🔧 Troubleshooting suggestions:\")\n", " print(\" 1. Check hostname and port are correct\")\n", " print(\" 2. Verify username is correct\")\n", " print(\" 3. Test manual SSH: ssh user@hostname\")\n", @@ -915,14 +915,14 @@ " return False\n", "\n", "# Run connection test\n", - "print(\"\ud83d\udd0d SSH CONNECTION TEST\")\n", + "print(\"🔍 SSH CONNECTION TEST\")\n", "print(\"=\" * 30)\n", "test_success = test_ssh_connection()\n", "\n", "if test_success:\n", - " print(\"\\n\ud83d\ude80 Ready for remote execution!\")\n", + " print(\"\\n🚀 Ready for remote execution!\")\n", "else:\n", - " print(\"\\n\ud83d\udd27 Please fix SSH issues before proceeding.\")" + " print(\"\\n🔧 Please fix SSH issues before proceeding.\")" ], "id": "cell-18" }, @@ -930,28 +930,28 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## \ud83d\udcda Summary and Best Practices\n", + "## 📚 Summary and Best Practices\n", "\n", - "### \ud83c\udf89 What You've Learned\n", + "### 🎉 What You've Learned\n", "\n", - "1. **\ud83d\udd11 Automated SSH Setup**: generate, deploy and configure a key in one call\n", - "2. **\u2699\ufe0f Remote Configuration**: Easy Clustrix setup for SSH execution\n", - "3. **\ud83e\uddee Remote Computing**: Mathematical computations on remote servers\n", - "4. **\ud83d\udcca Data Processing**: NumPy operations and analysis remotely\n", - "5. **\ud83d\uddc2\ufe0f System Analysis**: File system and environment inspection\n", - "6. **\ud83c\udfc1 Performance Testing**: Benchmarking remote server capabilities\n", - "7. **\ud83d\udd27 Troubleshooting**: Connection testing and problem resolution\n", + "1. **🔑 Automated SSH Setup**: generate, deploy and configure a key in one call\n", + "2. **⚙️ Remote Configuration**: Easy Clustrix setup for SSH execution\n", + "3. **🧮 Remote Computing**: Mathematical computations on remote servers\n", + "4. **📊 Data Processing**: NumPy operations and analysis remotely\n", + "5. **🗂️ System Analysis**: File system and environment inspection\n", + "6. **🏁 Performance Testing**: Benchmarking remote server capabilities\n", + "7. **🔧 Troubleshooting**: Connection testing and problem resolution\n", "\n", - "### \ud83d\udd12 Security Best Practices\n", + "### 🔒 Security Best Practices\n", "\n", - "- **\u2705 Use SSH keys**: Automated setup creates secure Ed25519 keys\n", - "- **\u2705 Unique keys**: Different keys for different servers\n", - "- **\u2705 Regular rotation**: Use `force_refresh=True` periodically\n", - "- **\u2705 Secure storage**: Keys stored with proper permissions (600/644)\n", - "- **\u2705 Clean up**: Enable `cleanup_on_success=True`\n", - "- **\u2705 Monitor access**: Check SSH logs on your servers\n", + "- **✅ Use SSH keys**: Automated setup creates secure Ed25519 keys\n", + "- **✅ Unique keys**: Different keys for different servers\n", + "- **✅ Regular rotation**: Use `force_refresh=True` periodically\n", + "- **✅ Secure storage**: Keys stored with proper permissions (600/644)\n", + "- **✅ Clean up**: Enable `cleanup_on_success=True`\n", + "- **✅ Monitor access**: Check SSH logs on your servers\n", "\n", - "### \ud83d\udca1 Performance Tips\n", + "### 💡 Performance Tips\n", "\n", "- **Parallel execution**: Set `max_parallel_jobs` appropriately\n", "- **Work directory**: Use fast storage (e.g., `/tmp` or SSD)\n", @@ -959,56 +959,57 @@ "- **Data transfer**: Minimize large data transfers between local/remote\n", "- **Connection reuse**: Clustrix automatically reuses SSH connections\n", "\n", - "### \ud83c\udfaf When to Use SSH vs Other Cluster Types\n", + "### 🎯 When to Use SSH vs Other Cluster Types\n", "\n", "**Choose SSH when:**\n", "- Working with single servers or workstations\n", "- Need immediate execution (no queuing)\n", "- Prototyping and development\n", - "- Cloud instances (AWS, GCP, Azure)\n", + "- A cloud VM you brought up yourself through your provider's console or CLI\n", "- Personal computing resources\n", "\n", - "**Choose SLURM/PBS/SGE when:**\n", + "**Choose SLURM when:**\n", "- Large HPC clusters with job schedulers\n", "- Need resource management and fair sharing\n", "- Production workloads with resource constraints\n", "- Long-running computations requiring scheduling\n", "\n", - "**Choose Kubernetes when:**\n", - "- Containerized execution environments\n", - "- Auto-scaling and fault tolerance needed\n", - "- Cloud-native applications\n", - "- Microservices architecture\n", + "**Choose HuggingFace Jobs (`cluster_type=\"huggingface\"`) when:**\n", + "- You have no machine of your own and want a rented CPU or GPU container\n", "\n", - "### \ud83d\ude80 Next Steps\n", + "> PBS, SGE, Kubernetes and the `@cluster(provider=...)` cloud VM path are\n", + "> **not currently supported**. They were removed in v0.2.0 because none had\n", + "> ever been shown to run a job end to end; each is planned for a future\n", + "> release under its own tracking issue. See the \"Backends removed in v0.2.0\"\n", + "> section of the Limitations page.\n", + "\n", + "### 🚀 Next Steps\n", "\n", "1. **Try other tutorials**:\n", " - [SLURM Tutorial](slurm_tutorial.ipynb) for HPC clusters\n", - " - [Kubernetes Tutorial](kubernetes_tutorial.ipynb) for container orchestration\n", - " - [Cost Monitoring Tutorial](cost_monitoring_tutorial.ipynb) for cloud costs\n", + " - [Filesystem Tutorial](filesystem_tutorial.ipynb) for remote file operations\n", "\n", "2. **Explore advanced features**:\n", " - Multiple cluster configurations\n", " - Custom environment setup\n", " - Filesystem utilities\n", - " - Cloud provider integrations\n", "\n", "3. **Read documentation**:\n", " - [SSH Setup Guide](../ssh_setup.rst) for detailed configuration\n", " - [API Documentation](../api/decorator.rst) for advanced options\n", " - [Clustrix Documentation](https://clustrix.readthedocs.io) for comprehensive guides\n", "\n", - "### \ud83c\udf8a Congratulations!\n", + "### 🎊 Congratulations!\n", "\n", "You've successfully learned how to use Clustrix's automated SSH setup and remote execution capabilities. You can now:\n", "\n", - "- \u26a1 Set up SSH access in one call instead of three manual steps\n", - "- \ud83d\ude80 Execute Python functions on any SSH-accessible server\n", - "- \ud83d\udcca Perform complex computations remotely\n", - "- \ud83d\udd27 Troubleshoot and optimize your setup\n", - "- \ud83d\udd12 Maintain security best practices\n", + "- ⚡ Set up SSH access in one call instead of three manual steps\n", + "- 🚀 Execute Python functions on any SSH-accessible server\n", + "- 📊 Perform complex computations remotely\n", + "- 🔧 Troubleshoot and optimize your setup\n", + "- 🔒 Maintain security best practices\n", "\n", - "**Happy remote computing!** \ud83c\udf89" + "**Happy remote computing!** 🎉" ], "id": "cell-19" } @@ -1038,4 +1039,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} From 9b90eaa75062be1983fe99f6b7963e4a606d2d42 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:39:34 -0400 Subject: [PATCH 11/56] Issue #147: make the real-world runner able to fail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit main() dropped every runner.run_*_tests() return value and never called sys.exit, so the script exited 0 no matter what. The pre-push hook guards each category with `if ! python scripts/run_real_world_tests.py --`, which therefore could never fire: four categories printed "failed" and the hook still announced "All real-world tests passed!" and allowed the push. Same class as the flake8 --exit-zero and mypy continue-on-error steps fixed in #138 -- a check that reports problems but cannot fail. Also fixes the second half of #147: the failure message printed only result.stdout, which was empty in all four observed failures because a pytest collection error goes to stderr. _report_failure now prints the exit code, stdout, stderr, and says so explicitly when there was no output at all. Verified by appending a deliberately failing test to tests/real_world/test_filesystem_real.py and running the script: EXIT CODE: 1 ❌ Filesystem tests failed (exit 1) assert False, "deliberate failure to verify exit-code propagation" E AssertionError: deliberate failure to verify exit-code propagation and, with that test removed, the same command exits 0. Before this change the failing case also exited 0 with an empty message body. Also drops the removed backends from two scripts: the kubernetes tutorial entry in check_docs_examples' _SECTION_BOUNDS (that page is deleted) and the aws/azure/gcp/lambda_cloud entries in the credential display names. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- scripts/check_docs_examples.py | 7 +--- scripts/run_real_world_tests.py | 62 +++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/scripts/check_docs_examples.py b/scripts/check_docs_examples.py index 5b96c926..fc977dc8 100644 --- a/scripts/check_docs_examples.py +++ b/scripts/check_docs_examples.py @@ -484,12 +484,7 @@ def make_namespace() -> dict: #: Pages that need a narrower window than "the whole file". Keyed by path #: relative to the repository root. -_SECTION_BOUNDS = { - "docs/source/tutorials/kubernetes_tutorial.rst": ( - "Auto-Provisioning a Cluster\n----", - "Configuration Options\n---", - ), -} +_SECTION_BOUNDS: dict = {} #: Directories under docs/ that are build output or vendored, not sources. _SKIP_DIRS = {"build", "_build", "_static", "_templates"} diff --git a/scripts/run_real_world_tests.py b/scripts/run_real_world_tests.py index 1ffc427a..f66748f3 100755 --- a/scripts/run_real_world_tests.py +++ b/scripts/run_real_world_tests.py @@ -13,6 +13,24 @@ from typing import Dict +def _report_failure(label: str, result: subprocess.CompletedProcess) -> None: + """Print everything a failed pytest run produced. + + The four categories the pre-push hook runs all reported failure with an + empty body, because only ``stdout`` was printed and a collection error + goes to ``stderr``. An operator was told something failed and not what + (issue #147). + """ + print(f"\u274c {label} failed (exit {result.returncode})") + if result.stdout: + print(result.stdout) + if result.stderr: + print("--- stderr ---") + print(result.stderr) + if not result.stdout and not result.stderr: + print("(no output captured)") + + class RealWorldTestRunner: """Runner for real-world tests with various configurations.""" @@ -51,13 +69,9 @@ def check_credentials(self) -> Dict[str, bool]: print(f" 1Password: {'✅' if manager.is_1password_available() else '❌'}") service_names = { - "aws": "AWS", - "azure": "Azure", - "gcp": "GCP", "ssh": "SSH", "slurm": "SLURM", "huggingface": "HuggingFace", - "lambda_cloud": "Lambda Cloud", } for service, available in credentials.items(): @@ -110,8 +124,7 @@ def run_unit_tests(self) -> bool: print("✅ Unit tests passed") return True else: - print(f"❌ Unit tests failed: {result.stdout}") - print(f"Error: {result.stderr}") + _report_failure("Unit tests", result) return False except Exception as e: print(f"❌ Error running unit tests: {e}") @@ -137,7 +150,7 @@ def run_filesystem_tests(self) -> bool: print("✅ Filesystem tests passed") return True else: - print(f"❌ Filesystem tests failed: {result.stdout}") + _report_failure("Filesystem tests", result) return False except Exception as e: print(f"❌ Error running filesystem tests: {e}") @@ -163,7 +176,7 @@ def run_ssh_tests(self) -> bool: print("✅ SSH tests passed") return True else: - print(f"❌ SSH tests failed: {result.stdout}") + _report_failure("SSH tests", result) return False except Exception as e: print(f"❌ Error running SSH tests: {e}") @@ -192,7 +205,7 @@ def run_api_tests(self, include_expensive: bool = False) -> bool: print("✅ API tests passed") return True else: - print(f"❌ API tests failed: {result.stdout}") + _report_failure("API tests", result) return False except Exception as e: print(f"❌ Error running API tests: {e}") @@ -220,7 +233,7 @@ def run_visual_tests(self) -> bool: print(f"📸 Check screenshots in: {self.real_world_dir / 'screenshots'}") return True else: - print(f"❌ Visual tests failed: {result.stdout}") + _report_failure("Visual tests", result) return False except Exception as e: print(f"❌ Error running visual tests: {e}") @@ -247,7 +260,7 @@ def run_hybrid_tests(self) -> bool: print("✅ Hybrid tests passed") return True else: - print(f"❌ Hybrid tests failed: {result.stdout}") + _report_failure("Hybrid tests", result) return False except Exception as e: print(f"❌ Error running hybrid tests: {e}") @@ -282,7 +295,7 @@ def run_all_tests( print("✅ All tests passed") return True else: - print(f"❌ Some tests failed: {result.stdout}") + _report_failure("Tests", result) return False except Exception as e: print(f"❌ Error running tests: {e}") @@ -375,26 +388,37 @@ def main(): runner.run_demo() return + # Every return value below is collected. Dropping them is what made the + # pre-push hook incapable of blocking a push (issue #147): each category + # printed "failed" and the script still exited 0, so the hook's + # `if ! python scripts/run_real_world_tests.py --filesystem` never fired. + outcomes = [] + if args.unit: - runner.run_unit_tests() + outcomes.append(runner.run_unit_tests()) if args.filesystem: - runner.run_filesystem_tests() + outcomes.append(runner.run_filesystem_tests()) if args.ssh: - runner.run_ssh_tests() + outcomes.append(runner.run_ssh_tests()) if args.api: - runner.run_api_tests(include_expensive=args.expensive) + outcomes.append(runner.run_api_tests(include_expensive=args.expensive)) if args.visual: - runner.run_visual_tests() + outcomes.append(runner.run_visual_tests()) if args.hybrid: - runner.run_hybrid_tests() + outcomes.append(runner.run_hybrid_tests()) if args.all: - runner.run_all_tests(include_expensive=args.expensive, include_visual=True) + outcomes.append( + runner.run_all_tests(include_expensive=args.expensive, include_visual=True) + ) + + if outcomes and not all(outcomes): + sys.exit(1) if not any( [ From a89e9121984216ee07da31d7b27e4026c4f950ba Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:40:19 -0400 Subject: [PATCH 12/56] Remove tests for deleted unverified backends (kubernetes, pbs, sge, cloud) Deleted test modules that exist solely to exercise backends removed from the package: Kubernetes, PBS, SGE, Lambda Cloud, direct cloud compute and container-registry validators, plus the reference kubernetes workflow. tests/integration lost its eight Kubernetes auto-provisioning scripts. --- tests/integration/test_complete_execution.py | 157 ---- tests/integration/test_direct_execution.py | 217 ------ tests/integration/test_local_simple.py | 79 -- .../integration/test_proper_user_workflow.py | 129 ---- .../integration/test_real_user_experience.py | 169 ----- tests/integration/test_standalone_function.py | 138 ---- tests/integration/test_ultimate_validation.py | 151 ---- .../integration/test_working_user_pattern.py | 188 ----- .../validate_aws_batch_connectivity.py | 258 ------- .../validate_container_registry.py | 425 ----------- .../validate_docker_functionality.py | 431 ----------- .../test_container_registry_comprehensive.py | 674 ------------------ ...test_direct_cloud_compute_comprehensive.py | 628 ---------------- .../test_kubernetes_comprehensive.py | 506 ------------- .../test_kubernetes_end_to_end_execution.py | 445 ------------ .../test_kubernetes_job_submission_real.py | 565 --------------- .../test_lambda_cloud_execution_real.py | 378 ---------- .../test_pbs_job_submission_real.py | 475 ------------ .../test_sge_job_submission_real.py | 504 ------------- tests/reference_workflows/__init__.py | 10 - .../kubernetes_workflows.py | 333 --------- 21 files changed, 6860 deletions(-) delete mode 100644 tests/integration/test_complete_execution.py delete mode 100644 tests/integration/test_direct_execution.py delete mode 100644 tests/integration/test_local_simple.py delete mode 100644 tests/integration/test_proper_user_workflow.py delete mode 100644 tests/integration/test_real_user_experience.py delete mode 100644 tests/integration/test_standalone_function.py delete mode 100644 tests/integration/test_ultimate_validation.py delete mode 100644 tests/integration/test_working_user_pattern.py delete mode 100644 tests/real_world/api_validation/validate_aws_batch_connectivity.py delete mode 100644 tests/real_world/api_validation/validate_container_registry.py delete mode 100644 tests/real_world/api_validation/validate_docker_functionality.py delete mode 100644 tests/real_world/test_container_registry_comprehensive.py delete mode 100644 tests/real_world/test_direct_cloud_compute_comprehensive.py delete mode 100644 tests/real_world/test_kubernetes_comprehensive.py delete mode 100644 tests/real_world/test_kubernetes_end_to_end_execution.py delete mode 100644 tests/real_world/test_kubernetes_job_submission_real.py delete mode 100644 tests/real_world/test_lambda_cloud_execution_real.py delete mode 100644 tests/real_world/test_pbs_job_submission_real.py delete mode 100644 tests/real_world/test_sge_job_submission_real.py delete mode 100644 tests/reference_workflows/kubernetes_workflows.py diff --git a/tests/integration/test_complete_execution.py b/tests/integration/test_complete_execution.py deleted file mode 100644 index b44fef24..00000000 --- a/tests/integration/test_complete_execution.py +++ /dev/null @@ -1,157 +0,0 @@ -#!/usr/bin/env python3 - -""" -Complete execution test - verify actual computation results. -This test will use inline code to avoid any import issues. -""" - -import logging -import time -from clustrix.config import ClusterConfig -from clustrix import cluster - -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger(__name__) - - -def test_complete_execution(): - """Test complete execution with result verification.""" - - print("🧪 COMPLETE EXECUTION TEST: Verify Computation Results") - print("=" * 60) - - # Configuration - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" - config.k8s_from_scratch = True - config.k8s_node_count = 2 - config.k8s_region = "local" - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"complete-test-{int(time.time())}" - - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = config - - try: - print(f"\n📋 Configuration: {config.k8s_cluster_name}") - - # Create the simplest possible function to test - print("\n⚡ Creating simple computation function...") - - # Expected result: 7 * 11 + 42 = 77 + 42 = 119 - expected_result = 7 * 11 + 42 - print(f"Expected computation result: 7 * 11 + 42 = {expected_result}") - - # Use exec to create function in global scope to avoid __main__ issues - function_code = ''' -def simple_math(x, y): - """Simple math function - completely self-contained.""" - result = x * y + 42 - - # Get environment info - import socket - import platform - hostname = socket.gethostname() - - return { - "computation": result, - "input_x": x, - "input_y": y, - "hostname": hostname, - "platform": platform.system(), - "expected": 119, # 7*11+42 - "correct": result == 119 - } -''' - - # Execute the function definition in global scope - exec(function_code, globals()) - - # Get the function from globals - simple_math = globals()["simple_math"] - - # Apply @cluster decorator - cluster_math = cluster( - cores=1, - memory="512Mi", - platform="kubernetes", - auto_provision=True, - provider="local", - node_count=2, - )(simple_math) - - print("✅ Function created and decorated") - - # Execute and verify - print("\n🚀 Executing function on Kubernetes...") - print(" Input: x=7, y=11") - print(" Expected: 7*11+42 = 119") - - start_time = time.time() - result = cluster_math(7, 11) - total_time = time.time() - start_time - - print(f"\n📊 RESULTS (execution time: {total_time:.1f}s):") - print(f" 🔢 Computation result: {result['computation']}") - print(f" 📥 Input x: {result['input_x']}") - print(f" 📥 Input y: {result['input_y']}") - print(f" 🖥️ Executed on: {result['hostname']}") - print(f" 🐧 Platform: {result['platform']}") - print(f" 🎯 Expected: {result['expected']}") - print(f" ✅ Correct: {result['correct']}") - - # Verification - if result["correct"] and result["computation"] == expected_result: - print(f"\n🎉 SUCCESS: Computation is correct!") - print(f" ✅ Expected {expected_result}, got {result['computation']}") - - # Check if it ran on Kubernetes - hostname = result["hostname"] - if any( - k8s_indicator in hostname.lower() - for k8s_indicator in ["worker", "node", "kind", "k8s"] - ): - print(f" ✅ Executed on Kubernetes: {hostname}") - return True - else: - print(f" ⚠️ Hostname unclear, but computation succeeded: {hostname}") - return True - - else: - print(f"\n❌ FAILURE: Computation incorrect!") - print(f" Expected: {expected_result}") - print(f" Got: {result['computation']}") - print(f" Correct flag: {result['correct']}") - return False - - except Exception as e: - print(f"\n💥 ERROR: {e}") - import traceback - - traceback.print_exc() - return False - - finally: - config_module._config = original_config - - -if __name__ == "__main__": - success = test_complete_execution() - - print("\n" + "=" * 60) - if success: - print("🏆 COMPLETE SUCCESS!") - print(" ✅ Kubernetes cluster auto-provisioned") - print(" ✅ Function executed on correct container") - print(" ✅ Computation returned correct result") - print(" ✅ End-to-end system verified!") - else: - print("❌ SYSTEM NEEDS MORE DEBUGGING") - - exit(0 if success else 1) diff --git a/tests/integration/test_direct_execution.py b/tests/integration/test_direct_execution.py deleted file mode 100644 index f539c08a..00000000 --- a/tests/integration/test_direct_execution.py +++ /dev/null @@ -1,217 +0,0 @@ -#!/usr/bin/env python3 - -""" -Direct execution test - bypass function serialization completely. -Instead of serializing functions, send the function code as text. -""" - -import logging -import time -from clustrix.config import ClusterConfig - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def test_direct_kubernetes_execution(): - """Test direct Kubernetes job execution with inline function code.""" - - print("🧪 DIRECT KUBERNETES EXECUTION TEST") - print("=" * 60) - - # Setup - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" - config.k8s_from_scratch = True - config.k8s_node_count = 2 - config.k8s_region = "local" - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"direct-{int(time.time())}" - - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = config - - try: - from clustrix.executor import ClusterExecutor - - print(f"📋 Cluster: {config.k8s_cluster_name}") - - # Create executor and provision cluster - executor = ClusterExecutor(config) - - print("🚀 Auto-provisioning Kubernetes cluster...") - if not executor.ensure_cluster_ready(timeout=900): - raise RuntimeError("Cluster provisioning failed") - - # Create direct job with inline Python code - print("⚡ Creating direct Kubernetes job with computation...") - - function_code = """ -import socket -import platform -import json - -# Input values -x = 7 -y = 11 - -# Computation -result = x * y + 42 - -# Environment info -hostname = socket.gethostname() -system = platform.system() - -# Output -output = { - "computation": result, - "inputs": {"x": x, "y": y}, - "environment": {"hostname": hostname, "system": system}, - "verification": {"expected": 119, "correct": result == 119}, - "message": "Direct execution successful!" -} - -print(f"CLUSTRIX_RESULT:{json.dumps(output)}") -""" - - # Submit job directly to Kubernetes - from kubernetes import client - from kubernetes.client.rest import ApiException - import base64 - import json - - batch_api = client.BatchV1Api() - - job_name = f"direct-test-{int(time.time())}" - - job = client.V1Job( - api_version="batch/v1", - kind="Job", - metadata=client.V1ObjectMeta(name=job_name), - spec=client.V1JobSpec( - template=client.V1PodTemplateSpec( - spec=client.V1PodSpec( - containers=[ - client.V1Container( - name="direct-worker", - image="python:3.11-slim", - command=["/bin/bash", "-c"], - args=[f'python -c "{function_code}"'], - resources=client.V1ResourceRequirements( - requests={"cpu": "1", "memory": "512Mi"}, - limits={"cpu": "1", "memory": "512Mi"}, - ), - ) - ], - restart_policy="Never", - ) - ), - backoff_limit=3, - ttl_seconds_after_finished=600, - ), - ) - - print(f"📤 Submitting job: {job_name}") - batch_api.create_namespaced_job(body=job, namespace="default") - - # Wait for completion - print("⏳ Waiting for job completion...") - max_wait = 120 # 2 minutes - start_wait = time.time() - - while time.time() - start_wait < max_wait: - try: - job_status = batch_api.read_namespaced_job( - name=job_name, namespace="default" - ) - if job_status.status.succeeded: - print("✅ Job completed successfully!") - break - elif job_status.status.failed: - print("❌ Job failed!") - raise RuntimeError("Job execution failed") - - time.sleep(5) - - except Exception as e: - print(f"Error checking job status: {e}") - time.sleep(5) - else: - raise TimeoutError("Job did not complete in time") - - # Get logs - print("📄 Retrieving job logs...") - core_api = client.CoreV1Api() - - pods = core_api.list_namespaced_pod( - namespace="default", label_selector=f"job-name={job_name}" - ) - - if not pods.items: - raise RuntimeError("No pods found for job") - - pod_name = pods.items[0].metadata.name - logs = core_api.read_namespaced_pod_log(name=pod_name, namespace="default") - - print(f"📋 Raw logs:") - print(logs) - - # Parse result - result_lines = [line for line in logs.split("\n") if "CLUSTRIX_RESULT:" in line] - if not result_lines: - raise RuntimeError("No result found in logs") - - result_json = result_lines[0].split("CLUSTRIX_RESULT:", 1)[1] - result = json.loads(result_json) - - # Display results - print(f"\n📊 COMPUTATION RESULTS:") - print(f" 🔢 Result: {result['computation']}") - print(f" 📥 Inputs: x={result['inputs']['x']}, y={result['inputs']['y']}") - print(f" 🖥️ Host: {result['environment']['hostname']}") - print(f" 🐧 System: {result['environment']['system']}") - print(f" 🎯 Expected: {result['verification']['expected']}") - print(f" ✅ Correct: {result['verification']['correct']}") - print(f" 💬 Message: {result['message']}") - - # Final verification - if result["verification"]["correct"]: - hostname = result["environment"]["hostname"] - print(f"\n🎉 COMPLETE SUCCESS!") - print(f" ✅ Computation correct: {result['computation']} == 119") - print(f" ✅ Kubernetes execution: {hostname}") - return True - else: - print(f"\n❌ Computation incorrect!") - return False - - except Exception as e: - print(f"\n💥 ERROR: {e}") - import traceback - - traceback.print_exc() - return False - - finally: - config_module._config = original_config - - -if __name__ == "__main__": - success = test_direct_kubernetes_execution() - - print("\n" + "=" * 60) - if success: - print("🏆 ULTIMATE SUCCESS!") - print(" ✅ Kubernetes cluster auto-provisioned") - print(" ✅ Job executed directly on Kubernetes") - print(" ✅ Computation returned correct result") - print(" ✅ Verified execution on correct container") - print(" ✅ End-to-end system completely validated!") - else: - print("❌ System needs more work") - - exit(0 if success else 1) diff --git a/tests/integration/test_local_simple.py b/tests/integration/test_local_simple.py deleted file mode 100644 index 56103137..00000000 --- a/tests/integration/test_local_simple.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env python3 - -""" -Simple test of local Kubernetes execution without pytest. -""" - -import logging -import os -import sys -import time - -from clustrix import cluster -from clustrix.config import ClusterConfig -import clustrix.config as config_module - -# Import test functions from unit tests -sys.path.append(os.path.join(os.path.dirname(__file__), "..", "unit")) -from test_functions import simple_computation as base_simple_computation # noqa: E402 - -# Set up logging -logging.basicConfig(level=logging.DEBUG) -logger = logging.getLogger(__name__) - - -def test_local_execution(): - """Test local Kubernetes execution.""" - - # Set up local config - local_config = ClusterConfig() - local_config.cluster_type = "kubernetes" - local_config.auto_provision_k8s = True - local_config.k8s_from_scratch = True - local_config.k8s_provider = "local" - local_config.k8s_region = "local" - local_config.k8s_node_count = 2 - local_config.k8s_cleanup_on_exit = True - local_config.k8s_cluster_name = f"test-simple-{int(time.time())}" - - # Override global config - original_config = config_module._config - config_module._config = local_config - - try: - logger.info("🧪 Testing simple function execution on local Kubernetes cluster") - - # Create decorated function using imported base function - simple_computation = cluster( - platform="kubernetes", - auto_provision=True, - provider="local", - node_count=2, - cores=1, # Explicitly set cores - memory="512Mi", # Explicitly set memory in K8s format - cluster_name=local_config.k8s_cluster_name, - )(base_simple_computation) - - # Execute function - logger.info( - "🚀 Starting function execution (will auto-provision local cluster)" - ) - start_time = time.time() - - result = simple_computation(7, 11) - execution_time = time.time() - start_time - - # Verify results - logger.info(f"✅ Function executed successfully in {execution_time:.1f}s") - logger.info(f"📊 Result: {result}") - - return True - - finally: - # Restore original config - config_module._config = original_config - - -if __name__ == "__main__": - success = test_local_execution() - print(f"Test {'PASSED' if success else 'FAILED'}") diff --git a/tests/integration/test_proper_user_workflow.py b/tests/integration/test_proper_user_workflow.py deleted file mode 100644 index dd00333e..00000000 --- a/tests/integration/test_proper_user_workflow.py +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env python3 - -""" -Proper user workflow test for Kubernetes auto-provisioning. -This demonstrates the correct way users would structure their projects. -""" - -import logging -import time -from clustrix.config import ClusterConfig - -# Set up logging to see what's happening -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger(__name__) - - -def test_proper_user_workflow(): - """Test the proper user workflow with separate modules (real-world usage).""" - - print("🧪 Testing Proper User Workflow: Kubernetes Auto-Provisioning") - print("=" * 70) - - # Step 1: User creates a custom configuration - print("\n📋 Step 1: Creating custom cluster configuration...") - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" # Use local Docker-based Kubernetes - config.k8s_from_scratch = True - config.k8s_node_count = 2 - config.k8s_region = "local" - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"proper-test-{int(time.time())}" - - print(f"✅ Config created: {config.k8s_cluster_name}") - - # Step 2: User applies the configuration globally - print("\n🔧 Step 2: Applying configuration globally...") - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = config - - try: - # Step 3: User imports their analysis module (realistic workflow) - print("\n📦 Step 3: User imports their analysis module...") - from user_analysis import analyze_data - - print("✅ Analysis function imported from separate module") - - # Step 4: User calls the function normally (auto-provisioning happens transparently) - print( - "\n🚀 Step 4: User calls function - auto-provisioning happens transparently..." - ) - print(" (This may take 30-60 seconds for cluster creation)") - - start_time = time.time() - - # This looks like a normal function call to the user! - result = analyze_data(10000, "medium") - - total_time = time.time() - start_time - - # Step 5: User gets results - print(f"\n🎉 Step 5: Results received in {total_time:.1f} seconds!") - print("📊 Analysis Results:") - print(f" • Dataset size: {result['dataset_size']}") - print(f" • Complexity: {result['complexity']}") - print(f" • Computation result: {result['computation_result']}") - print(f" • Computation time: {result['computation_time_seconds']}s") - print(f" • Executed on: {result['execution_info']['hostname']}") - print(f" • Platform: {result['execution_info']['platform']}") - print(f" • Environment: {result['execution_info']['environment']}") - - # Verify this actually ran on Kubernetes - hostname = result["execution_info"]["hostname"] - if any( - k8s_indicator in hostname.lower() - for k8s_indicator in ["worker", "node", "k8s", "kind"] - ): - print( - "\n✅ SUCCESS: Function executed on auto-provisioned Kubernetes cluster!" - ) - print(f"✅ Kubernetes hostname detected: {hostname}") - print("✅ Proper user workflow test PASSED!") - return True - else: - print( - f"\n⚠️ WARNING: Hostname doesn't clearly indicate Kubernetes: {hostname}" - ) - print("✅ Function executed successfully, but verifying environment...") - # Even if hostname doesn't clearly show K8s, if we got results, it worked - return True - - except Exception as e: - print(f"\n❌ ERROR: Test failed with exception: {e}") - import traceback - - traceback.print_exc() - return False - - finally: - # Restore original config - config_module._config = original_config - print(f"\n🧹 Cleanup: Restored original configuration") - - -if __name__ == "__main__": - print("Starting Proper User Workflow Test...") - success = test_proper_user_workflow() - - print("\n" + "=" * 70) - if success: - print("🎉 OVERALL TEST RESULT: SUCCESS") - print(" Real user workflow validated:") - print(" ✅ Users can create separate analysis modules") - print(" ✅ @cluster decorator works with proper module structure") - print(" ✅ Custom configurations work correctly") - print(" ✅ Auto-provisioning is transparent to users") - print(" ✅ Functions execute on auto-provisioned Kubernetes clusters") - print(" ✅ Results are returned successfully") - print(" ✅ Automatic cleanup works") - else: - print("❌ OVERALL TEST RESULT: FAILED") - print(" User workflow needs improvement") - - exit(0 if success else 1) diff --git a/tests/integration/test_real_user_experience.py b/tests/integration/test_real_user_experience.py deleted file mode 100644 index c391b57e..00000000 --- a/tests/integration/test_real_user_experience.py +++ /dev/null @@ -1,169 +0,0 @@ -#!/usr/bin/env python3 - -""" -Real user experience test for Kubernetes auto-provisioning. -This tests the exact syntax and workflow that users would follow. -""" - -import logging -import time -from clustrix import cluster -from clustrix.config import ClusterConfig - -# Set up logging to see what's happening -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger(__name__) - - -def test_user_workflow(): - """Test the actual user workflow for Kubernetes auto-provisioning.""" - - print("🧪 Testing Real User Experience: Kubernetes Auto-Provisioning") - print("=" * 60) - - # Step 1: User creates a custom configuration - print("\n📋 Step 1: Creating custom cluster configuration...") - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" # Use local Docker-based Kubernetes - config.k8s_from_scratch = True - config.k8s_node_count = 2 - config.k8s_region = "local" - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"user-test-{int(time.time())}" - - print(f"✅ Config created: {config.k8s_cluster_name}") - - # Step 2: User applies the configuration globally - print("\n🔧 Step 2: Applying configuration globally...") - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = config - - try: - # Step 3: User defines a function with @cluster decorator - print("\n⚡ Step 3: User defines function with @cluster decorator...") - - @cluster( - cores=1, - memory="512Mi", - platform="kubernetes", - auto_provision=True, - provider="local", - node_count=2, - ) - def analyze_data(dataset_size: int, complexity: str = "medium"): - """ - A realistic data analysis function that a user might write. - This demonstrates the exact user experience. - """ - import platform - import socket - import time - import math - - print( - f"🔬 Starting analysis of dataset (size: {dataset_size}, complexity: {complexity})" - ) - - # Simulate some computation - start_time = time.time() - - if complexity == "simple": - result = dataset_size * 2 - elif complexity == "medium": - result = sum(math.sqrt(i) for i in range(min(dataset_size, 1000))) - else: # complex - result = sum( - math.sin(i) * math.cos(i) for i in range(min(dataset_size, 5000)) - ) - - computation_time = time.time() - start_time - - # Return realistic analysis results - return { - "dataset_size": dataset_size, - "complexity": complexity, - "computation_result": round(result, 2), - "computation_time_seconds": round(computation_time, 3), - "execution_info": { - "platform": platform.platform(), - "hostname": socket.gethostname(), - "python_version": platform.python_version(), - "environment": "kubernetes_cluster", - }, - "success": True, - } - - print("✅ Function defined with @cluster decorator") - - # Step 4: User calls the function normally (auto-provisioning happens transparently) - print( - "\n🚀 Step 4: User calls function - auto-provisioning happens transparently..." - ) - print(" (This may take 30-60 seconds for cluster creation)") - - start_time = time.time() - - # This looks like a normal function call to the user! - result = analyze_data(10000, "medium") - - total_time = time.time() - start_time - - # Step 5: User gets results - print(f"\n🎉 Step 5: Results received in {total_time:.1f} seconds!") - print("📊 Analysis Results:") - print(f" • Dataset size: {result['dataset_size']}") - print(f" • Complexity: {result['complexity']}") - print(f" • Computation result: {result['computation_result']}") - print(f" • Computation time: {result['computation_time_seconds']}s") - print(f" • Executed on: {result['execution_info']['hostname']}") - print(f" • Platform: {result['execution_info']['platform']}") - print(f" • Environment: {result['execution_info']['environment']}") - - # Verify this actually ran on Kubernetes - if "kubernetes_cluster" in str(result.get("execution_info", {})): - print( - "\n✅ SUCCESS: Function executed on auto-provisioned Kubernetes cluster!" - ) - print("✅ User experience test PASSED!") - return True - else: - print("\n❌ ERROR: Function did not execute on Kubernetes cluster") - return False - - except Exception as e: - print(f"\n❌ ERROR: Test failed with exception: {e}") - import traceback - - traceback.print_exc() - return False - - finally: - # Restore original config - config_module._config = original_config - print(f"\n🧹 Cleanup: Restored original configuration") - - -if __name__ == "__main__": - print("Starting Real User Experience Test...") - success = test_user_workflow() - - print("\n" + "=" * 60) - if success: - print("🎉 OVERALL TEST RESULT: SUCCESS") - print(" Users can successfully:") - print(" ✅ Create custom Kubernetes configurations") - print(" ✅ Use @cluster decorator with auto-provisioning") - print(" ✅ Call functions normally (transparent provisioning)") - print(" ✅ Get results from auto-provisioned clusters") - print(" ✅ Automatic cleanup works") - else: - print("❌ OVERALL TEST RESULT: FAILED") - print(" User experience needs improvement") - - exit(0 if success else 1) diff --git a/tests/integration/test_standalone_function.py b/tests/integration/test_standalone_function.py deleted file mode 100644 index 4d829a0e..00000000 --- a/tests/integration/test_standalone_function.py +++ /dev/null @@ -1,138 +0,0 @@ -#!/usr/bin/env python3 - -""" -Test with completely standalone function that has zero external dependencies. -""" - -import logging -import time -from clustrix.config import ClusterConfig -from clustrix import cluster - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def create_standalone_function(): - """Create a completely standalone function with no external dependencies.""" - - def pure_computation(x, y): - """ - Pure computation function with zero external dependencies. - Everything needed is imported inside the function. - """ - # All imports inside function - import socket - import platform - - # Simple computation - result = x * y + 42 - - # Get execution environment info - hostname = socket.gethostname() - system = platform.system() - - # Return all info - return { - "computation": result, - "inputs": {"x": x, "y": y}, - "environment": {"hostname": hostname, "system": system}, - "verification": {"expected": 119, "correct": result == 119}, # 7*11+42 - } - - return pure_computation - - -def test_standalone_execution(): - """Test standalone execution.""" - - print("🧪 STANDALONE FUNCTION TEST") - print("=" * 50) - - # Setup config - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" - config.k8s_from_scratch = True - config.k8s_node_count = 2 - config.k8s_region = "local" - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"standalone-{int(time.time())}" - - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = config - - try: - print(f"📋 Cluster: {config.k8s_cluster_name}") - - # Create standalone function - pure_func = create_standalone_function() - - # Apply decorator - cluster_func = cluster( - cores=1, - memory="512Mi", - platform="kubernetes", - auto_provision=True, - provider="local", - )(pure_func) - - print("✅ Standalone function created and decorated") - print("🚀 Executing: pure_computation(7, 11)") - print(" Expected: 7*11+42 = 119") - - # Execute - start_time = time.time() - result = cluster_func(7, 11) - exec_time = time.time() - start_time - - # Display results - print(f"\n📊 RESULTS (time: {exec_time:.1f}s):") - print(f" 🔢 Result: {result['computation']}") - print(f" 📥 Inputs: x={result['inputs']['x']}, y={result['inputs']['y']}") - print(f" 🖥️ Host: {result['environment']['hostname']}") - print(f" 🐧 System: {result['environment']['system']}") - print(f" 🎯 Expected: {result['verification']['expected']}") - print(f" ✅ Correct: {result['verification']['correct']}") - - # Verify success - if result["verification"]["correct"]: - hostname = result["environment"]["hostname"] - print(f"\n🎉 COMPUTATION SUCCESS!") - print(f" ✅ Correct result: {result['computation']} == 119") - - if any(k in hostname.lower() for k in ["worker", "node", "k8s", "kind"]): - print(f" ✅ Kubernetes execution confirmed: {hostname}") - return True - else: - print(f" ✅ Execution succeeded: {hostname}") - return True - else: - print(f"\n❌ COMPUTATION FAILED!") - return False - - except Exception as e: - print(f"\n💥 ERROR: {e}") - import traceback - - traceback.print_exc() - return False - - finally: - config_module._config = original_config - - -if __name__ == "__main__": - success = test_standalone_execution() - - print("\n" + "=" * 50) - if success: - print("🏆 COMPLETE SUCCESS!") - print("✅ End-to-end validation confirmed!") - else: - print("❌ Still debugging needed") - - exit(0 if success else 1) diff --git a/tests/integration/test_ultimate_validation.py b/tests/integration/test_ultimate_validation.py deleted file mode 100644 index 775c45a1..00000000 --- a/tests/integration/test_ultimate_validation.py +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/env python3 - -""" -Ultimate validation test for Kubernetes auto-provisioning. -This uses the exact pattern that will work for real users. -""" - -import logging -import time -from clustrix.config import ClusterConfig -from clustrix import cluster - -# Set up logging -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger(__name__) - - -def test_ultimate_validation(): - """Ultimate validation of the Kubernetes auto-provisioning system.""" - - print("🚀 ULTIMATE VALIDATION: Kubernetes Auto-Provisioning System") - print("=" * 80) - - # Step 1: User creates configuration - print("\n📋 Step 1: User creates custom configuration...") - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" - config.k8s_from_scratch = True - config.k8s_node_count = 2 - config.k8s_region = "local" - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"ultimate-{int(time.time())}" - - print(f"✅ Configuration: {config.k8s_cluster_name}") - - # Step 2: Apply configuration - print("\n🔧 Step 2: Applying configuration...") - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = config - - try: - # Step 3: Import working function and decorate it - print("\n📦 Step 3: Importing and decorating function...") - from working_functions import analyze_dataset_simple - - # Apply @cluster decorator to imported function - analyze_on_k8s = cluster( - cores=1, - memory="512Mi", - platform="kubernetes", - auto_provision=True, - provider="local", - node_count=2, - )(analyze_dataset_simple) - - print("✅ Function decorated and ready for Kubernetes execution") - - # Step 4: Execute function - print("\n🚀 Step 4: Executing function on auto-provisioned Kubernetes...") - print(" This will automatically:") - print(" • Create a local Kubernetes cluster using Docker/kind") - print(" • Submit the function as a Kubernetes job") - print(" • Wait for completion and return results") - print(" • Clean up the cluster automatically") - print(" (Total time: ~30-60 seconds)") - - start_time = time.time() - result = analyze_on_k8s(10000, "medium") - total_time = time.time() - start_time - - # Step 5: Validate results - print(f"\n🎉 Step 5: Execution completed in {total_time:.1f} seconds!") - print(f"\n📊 EXECUTION RESULTS:") - print( - f" 📥 Input: size={result['input']['size']}, complexity={result['input']['complexity']}" - ) - print(f" ⚡ Result: {result['computation']['result']}") - print(f" ⏱️ Time: {result['computation']['time_seconds']}s") - print(f" 🖥️ Host: {result['environment']['hostname']}") - print(f" 🐧 Platform: {result['environment']['platform']}") - print(f" 🐍 Python: {result['environment']['python_version']}") - print(f" 💬 Message: {result['message']}") - - # Final validation - hostname = result["environment"]["hostname"] - success = result.get("success", False) - - if success and any( - k8s_indicator in hostname.lower() - for k8s_indicator in ["worker", "node", "kind", "k8s"] - ): - print(f"\n🏆 ULTIMATE VALIDATION PASSED!") - print(f" ✅ Function executed successfully on Kubernetes") - print(f" ✅ Kubernetes hostname confirmed: {hostname}") - print(f" ✅ Auto-provisioning worked transparently") - print(f" ✅ Results returned correctly") - return True - elif success: - print(f"\n⚠️ PARTIAL SUCCESS:") - print(f" ✅ Function executed and returned results") - print(f" ⚠️ Could not confirm Kubernetes execution from hostname") - print(f" 📝 This may still indicate success") - return True - else: - print(f"\n❌ VALIDATION FAILED:") - print(f" ❌ Function execution failed") - return False - - except Exception as e: - print(f"\n💥 EXCEPTION OCCURRED: {e}") - import traceback - - traceback.print_exc() - return False - - finally: - # Cleanup - config_module._config = original_config - print(f"\n🧹 Configuration restored") - - -if __name__ == "__main__": - print("🎯 Starting Ultimate Validation Test...") - success = test_ultimate_validation() - - print("\n" + "=" * 80) - if success: - print("🎉🎉🎉 ULTIMATE RESULT: COMPLETE SUCCESS! 🎉🎉🎉") - print() - print("🏆 KUBERNETES AUTO-PROVISIONING SYSTEM VALIDATION:") - print() - print(" ✅ LOCAL DOCKER KUBERNETES PROVISIONING") - print(" ✅ AUTOMATIC CLUSTER CREATION (~30 seconds)") - print(" ✅ FUNCTION EXECUTION IN KUBERNETES PODS") - print(" ✅ RESULT RETRIEVAL FROM REMOTE EXECUTION") - print(" ✅ AUTOMATIC CLUSTER CLEANUP") - print(" ✅ TRANSPARENT USER EXPERIENCE") - print() - print("🚀 THE SYSTEM IS FULLY OPERATIONAL!") - print(" Users can now auto-provision Kubernetes clusters") - print(" and execute functions transparently!") - else: - print("❌❌❌ ULTIMATE RESULT: SYSTEM NEEDS WORK ❌❌❌") - - exit(0 if success else 1) diff --git a/tests/integration/test_working_user_pattern.py b/tests/integration/test_working_user_pattern.py deleted file mode 100644 index 4580fb26..00000000 --- a/tests/integration/test_working_user_pattern.py +++ /dev/null @@ -1,188 +0,0 @@ -#!/usr/bin/env python3 - -""" -Working user pattern test for Kubernetes auto-provisioning. -This demonstrates a self-contained function that will actually work. -""" - -import logging -import time -from clustrix.config import ClusterConfig - -# Set up logging to see what's happening -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger(__name__) - - -def test_working_user_pattern(): - """Test the working user pattern with self-contained functions.""" - - print("🧪 Testing Working User Pattern: Kubernetes Auto-Provisioning") - print("=" * 70) - - # Step 1: User creates a custom configuration - print("\n📋 Step 1: Creating custom cluster configuration...") - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" # Use local Docker-based Kubernetes - config.k8s_from_scratch = True - config.k8s_node_count = 2 - config.k8s_region = "local" - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"working-test-{int(time.time())}" - - print(f"✅ Config created: {config.k8s_cluster_name}") - - # Step 2: User applies the configuration globally - print("\n🔧 Step 2: Applying configuration globally...") - import clustrix.config as config_module - - original_config = config_module._config - config_module._config = config - - try: - # Step 3: User imports clustrix and defines a self-contained function - print("\n⚡ Step 3: User defines a self-contained function...") - from clustrix import cluster - - # This is the working pattern - self-contained function with all imports inside - @cluster( - cores=1, - memory="512Mi", - platform="kubernetes", - auto_provision=True, - provider="local", - node_count=2, - ) - def analyze_dataset(size, complexity="medium"): - """ - Self-contained data analysis function. - All imports are inside the function - this is the working pattern! - """ - # All imports must be inside the function for remote execution - import math - import platform - import socket - import time - - print(f"🔬 Analyzing dataset (size: {size}, complexity: {complexity})") - - # Simulate computation - start_time = time.time() - - if complexity == "simple": - result = size * 2 - elif complexity == "medium": - result = sum(math.sqrt(i) for i in range(min(size, 1000))) - else: - result = sum(math.sin(i) * math.cos(i) for i in range(min(size, 5000))) - - computation_time = time.time() - start_time - - # Return comprehensive results - return { - "input": {"size": size, "complexity": complexity}, - "computation": { - "result": round(result, 2), - "time_seconds": round(computation_time, 3), - }, - "environment": { - "hostname": socket.gethostname(), - "platform": platform.platform(), - "python_version": platform.python_version(), - "execution_context": "auto_provisioned_kubernetes", - }, - "success": True, - "message": "✅ Successfully executed on auto-provisioned Kubernetes cluster!", - } - - print("✅ Self-contained function defined with @cluster decorator") - - # Step 4: User calls the function (auto-provisioning happens transparently) - print( - "\n🚀 Step 4: Calling function - auto-provisioning happens transparently..." - ) - print(" (Cluster creation may take 30-60 seconds)") - - start_time = time.time() - - # Normal function call from user perspective - result = analyze_dataset(10000, "medium") - - total_time = time.time() - start_time - - # Step 5: User receives and processes results - print(f"\n🎉 Step 5: Results received in {total_time:.1f} seconds!") - print("📊 Complete Analysis Results:") - print( - f" 📥 Input: size={result['input']['size']}, complexity={result['input']['complexity']}" - ) - print(f" ⚡ Computation result: {result['computation']['result']}") - print(f" ⏱️ Computation time: {result['computation']['time_seconds']}s") - print(f" 🖥️ Executed on: {result['environment']['hostname']}") - print(f" 🐧 Platform: {result['environment']['platform']}") - print(f" 🐍 Python: {result['environment']['python_version']}") - print(f" 🏢 Context: {result['environment']['execution_context']}") - print(f" 💬 Message: {result['message']}") - - # Verify successful Kubernetes execution - hostname = result["environment"]["hostname"] - context = result["environment"]["execution_context"] - - if "kubernetes" in context.lower() and any( - indicator in hostname.lower() - for indicator in ["worker", "node", "kind", "k8s"] - ): - print("\n✅ VERIFICATION PASSED:") - print(" 🎯 Function executed on auto-provisioned Kubernetes cluster") - print(" 🔍 Kubernetes indicators detected in hostname and context") - print(" 📦 Self-contained function pattern works correctly") - return True - else: - print(f"\n⚠️ VERIFICATION INCONCLUSIVE:") - print(f" 🔍 Hostname: {hostname}") - print(f" 🔍 Context: {context}") - print(" 📦 Function executed, but Kubernetes indicators unclear") - # Still count as success if we got valid results - return result.get("success", False) - - except Exception as e: - print(f"\n❌ ERROR: Test failed with exception: {e}") - import traceback - - traceback.print_exc() - return False - - finally: - # Restore original config - config_module._config = original_config - print(f"\n🧹 Cleanup: Original configuration restored") - - -if __name__ == "__main__": - print("Starting Working User Pattern Test...") - success = test_working_user_pattern() - - print("\n" + "=" * 70) - if success: - print("🎉 FINAL RESULT: SUCCESS ✅") - print() - print("📋 VALIDATED USER WORKFLOW:") - print(" 1️⃣ Users can create custom Kubernetes configurations") - print(" 2️⃣ Users can apply configurations globally") - print(" 3️⃣ Users can define functions with @cluster decorator") - print(" 4️⃣ Functions with all imports inside work correctly") - print(" 5️⃣ Auto-provisioning is completely transparent") - print(" 6️⃣ Functions execute on real Kubernetes clusters") - print(" 7️⃣ Results are returned successfully to users") - print(" 8️⃣ Automatic cleanup works") - print() - print("🏆 KUBERNETES AUTO-PROVISIONING SYSTEM IS WORKING!") - else: - print("❌ FINAL RESULT: FAILED ❌") - print(" System needs debugging") - - exit(0 if success else 1) diff --git a/tests/real_world/api_validation/validate_aws_batch_connectivity.py b/tests/real_world/api_validation/validate_aws_batch_connectivity.py deleted file mode 100644 index 8797d6ff..00000000 --- a/tests/real_world/api_validation/validate_aws_batch_connectivity.py +++ /dev/null @@ -1,258 +0,0 @@ -#!/usr/bin/env python3 -""" -AWS Batch Connectivity Validation Script - -This script tests AWS Batch API connectivity and basic functionality -to determine if Clustrix could integrate with AWS Batch in the future. -""" - -import sys -import os -import json -import logging -from pathlib import Path - -# Add the clustrix package to Python path -sys.path.insert(0, str(Path(__file__).parent.parent)) - -from clustrix.secure_credentials import ValidationCredentials - -# Configure logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def test_aws_batch_connectivity(): - """Test AWS Batch API connectivity.""" - print("🔍 AWS Batch Connectivity Test") - print("=" * 50) - - # Get AWS credentials - creds = ValidationCredentials() - aws_creds = creds.get_aws_credentials() - - if not aws_creds: - print("❌ No AWS credentials found") - return False - - print(f"✅ AWS credentials found") - print(f" Access Key: {aws_creds['aws_access_key_id'][:10]}...") - print(f" Region: {aws_creds['aws_region']}") - - # Set up environment variables for boto3 - os.environ["AWS_ACCESS_KEY_ID"] = aws_creds["aws_access_key_id"] - os.environ["AWS_SECRET_ACCESS_KEY"] = aws_creds["aws_secret_access_key"] - os.environ["AWS_DEFAULT_REGION"] = aws_creds["aws_region"] - - try: - import boto3 - from botocore.exceptions import ClientError, NoCredentialsError - except ImportError: - print("❌ boto3 not available. Install with: pip install boto3") - return False - - # Test AWS Batch client creation - try: - print("\n🔌 Testing AWS Batch client connection...") - batch_client = boto3.client("batch", region_name=aws_creds["aws_region"]) - - # Test basic API call - list compute environments - print("📊 Listing compute environments...") - response = batch_client.describe_compute_environments() - - compute_envs = response.get("computeEnvironments", []) - print(f"✅ AWS Batch API accessible") - print(f" Found {len(compute_envs)} compute environments") - - if compute_envs: - print(" Compute environments:") - for env in compute_envs[:5]: # Show first 5 - name = env.get("computeEnvironmentName", "Unknown") - state = env.get("state", "Unknown") - status = env.get("status", "Unknown") - print(f" - {name}: {state}/{status}") - else: - print(" ℹ️ No compute environments configured") - - # Test job queues - print("\n📋 Listing job queues...") - response = batch_client.describe_job_queues() - - job_queues = response.get("jobQueues", []) - print(f" Found {job_queues.__len__()} job queues") - - if job_queues: - print(" Job queues:") - for queue in job_queues[:5]: # Show first 5 - name = queue.get("jobQueueName", "Unknown") - state = queue.get("state", "Unknown") - status = queue.get("status", "Unknown") - priority = queue.get("priority", "Unknown") - print(f" - {name}: {state}/{status} (priority: {priority})") - else: - print(" ℹ️ No job queues configured") - - # Test job definitions - print("\n📝 Listing job definitions...") - response = batch_client.describe_job_definitions(status="ACTIVE") - - job_definitions = response.get("jobDefinitions", []) - print(f" Found {len(job_definitions)} active job definitions") - - if job_definitions: - print(" Job definitions:") - for job_def in job_definitions[:5]: # Show first 5 - name = job_def.get("jobDefinitionName", "Unknown") - revision = job_def.get("revision", "Unknown") - job_type = job_def.get("type", "Unknown") - print(f" - {name}:{revision} ({job_type})") - else: - print(" ℹ️ No active job definitions found") - - # Test basic permissions - print("\n🔐 Testing AWS Batch permissions...") - - # Try to list jobs (this will work even with no jobs) - try: - response = batch_client.list_jobs(jobQueue="*") - print("✅ Job listing permission available") - except ClientError as e: - if e.response["Error"]["Code"] == "ValidationException": - print("⚠️ No valid job queues to list jobs from") - else: - print(f"❌ Job listing permission denied: {e}") - - # Summary - total_resources = len(compute_envs) + len(job_queues) + len(job_definitions) - - if total_resources > 0: - print(f"\n✅ AWS Batch infrastructure detected!") - print(f" Total resources: {total_resources}") - print(" ✅ Clustrix could potentially integrate with AWS Batch") - return { - "api_accessible": True, - "compute_environments": len(compute_envs), - "job_queues": len(job_queues), - "job_definitions": len(job_definitions), - "ready_for_jobs": len(job_queues) > 0 and len(compute_envs) > 0, - } - else: - print(f"\n⚠️ AWS Batch API accessible but no infrastructure configured") - print(" ℹ️ Account needs AWS Batch setup before job submission") - return { - "api_accessible": True, - "compute_environments": 0, - "job_queues": 0, - "job_definitions": 0, - "ready_for_jobs": False, - "needs_setup": True, - } - - except NoCredentialsError: - print("❌ AWS credentials not properly configured") - return False - except ClientError as e: - if e.response["Error"]["Code"] == "UnauthorizedOperation": - print("❌ AWS credentials lack AWS Batch permissions") - print( - " Required permissions: batch:DescribeComputeEnvironments, batch:DescribeJobQueues, etc." - ) - elif e.response["Error"]["Code"] == "AccessDenied": - print("❌ Access denied to AWS Batch service") - print(" Check IAM permissions for AWS Batch") - else: - print(f"❌ AWS Batch API error: {e}") - return False - except Exception as e: - print(f"❌ Unexpected error: {e}") - return False - - -def test_aws_batch_job_definition_format(): - """Test creating a sample job definition format for Clustrix.""" - print("\n🧪 Testing AWS Batch Job Definition Format") - print("=" * 50) - - # Create a sample job definition that Clustrix might use - sample_job_definition = { - "jobDefinitionName": "clustrix-python-job", - "type": "container", - "containerProperties": { - "image": "python:3.11-slim", - "vcpus": 1, - "memory": 512, - "jobRoleArn": "arn:aws:iam::account:role/BatchJobRole", - "environment": [ - {"name": "CLUSTRIX_JOB_ID", "value": "test-job"}, - {"name": "PYTHONPATH", "value": "/app"}, - ], - "mountPoints": [], - "volumes": [], - "ulimits": [], - }, - "retryStrategy": {"attempts": 3}, - "timeout": {"attemptDurationSeconds": 3600}, - } - - print("📝 Sample Clustrix job definition structure:") - print(json.dumps(sample_job_definition, indent=2)) - - print("\n✅ Job definition format compatible with AWS Batch") - print(" Clustrix could create job definitions for:") - print(" - Python container execution") - print(" - Resource specification (CPU/memory)") - print(" - Environment variable injection") - print(" - Retry and timeout policies") - - return True - - -def main(): - """Main validation function.""" - print("🚀 Starting AWS Batch Connectivity Validation") - print("=" * 70) - - # Test AWS Batch connectivity - batch_result = test_aws_batch_connectivity() - - # Test job definition format - job_def_result = test_aws_batch_job_definition_format() - - # Summary - print("\n📊 AWS Batch Validation Summary") - print("=" * 70) - - if isinstance(batch_result, dict): - print("✅ AWS Batch API: ACCESSIBLE") - print(f" Compute environments: {batch_result['compute_environments']}") - print(f" Job queues: {batch_result['job_queues']}") - print(f" Job definitions: {batch_result['job_definitions']}") - - if batch_result["ready_for_jobs"]: - print("✅ Infrastructure: READY for job submission") - elif batch_result.get("needs_setup"): - print("⚠️ Infrastructure: NEEDS SETUP") - print(" Create compute environments and job queues first") - - if job_def_result: - print("✅ Job definition format: COMPATIBLE") - - print("\n🎯 Overall Assessment:") - if batch_result["ready_for_jobs"]: - print("🎉 AWS Batch integration ready for Clustrix implementation!") - else: - print("⚠️ AWS Batch API accessible, but infrastructure setup required") - return 0 - - elif batch_result: - print("✅ AWS Batch API: LIMITED ACCESS") - print("⚠️ Infrastructure status unknown") - return 0 - else: - print("❌ AWS Batch API: NOT ACCESSIBLE") - print(" Check credentials and permissions") - return 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/real_world/api_validation/validate_container_registry.py b/tests/real_world/api_validation/validate_container_registry.py deleted file mode 100644 index 1fb93d9d..00000000 --- a/tests/real_world/api_validation/validate_container_registry.py +++ /dev/null @@ -1,425 +0,0 @@ -#!/usr/bin/env python3 -""" -Container Registry Validation Script - -This script validates container registry operations (push/pull) that Clustrix -might use for Kubernetes and container-based deployments. -""" - -import json -import sys -import subprocess -import tempfile -import time -import os -import logging -from pathlib import Path -from datetime import datetime - -# Configure logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def run_command(cmd, timeout=120, check=True): - """Run a command and return the result.""" - try: - result = subprocess.run( - cmd, - shell=True, - capture_output=True, - text=True, - timeout=timeout, - check=check, - ) - return result - except subprocess.CalledProcessError as e: - logger.error(f"Command failed: {cmd}") - logger.error(f"Error: {e.stderr}") - return e - except subprocess.TimeoutExpired: - logger.error(f"Command timed out: {cmd}") - return None - - -def test_docker_availability(): - """Test if Docker is available.""" - print("🔍 Docker Availability Check") - print("=" * 40) - - result = run_command("docker --version", check=False) - if result and result.returncode == 0: - print(f"✅ Docker installed: {result.stdout.strip()}") - else: - print("❌ Docker not available") - return False - - result = run_command("docker info --format '{{.ServerVersion}}'", check=False) - if result and result.returncode == 0: - print(f"✅ Docker daemon running: v{result.stdout.strip()}") - return True - else: - print("❌ Docker daemon not running") - return False - - -def test_docker_hub_connectivity(): - """Test basic Docker Hub connectivity.""" - print("\n🌐 Docker Hub Connectivity Test") - print("=" * 40) - - # Test pull from Docker Hub (public image) - print("📥 Testing Docker Hub pull access...") - result = run_command("docker pull hello-world:latest", timeout=60) - if not result or result.returncode != 0: - print("❌ Cannot pull from Docker Hub") - return False - print("✅ Docker Hub pull access working") - - # Clean up - run_command("docker rmi hello-world:latest", check=False) - - return True - - -def test_container_image_building(): - """Test building a custom container image for Clustrix.""" - print("\n🏗️ Container Image Building Test") - print("=" * 40) - - with tempfile.TemporaryDirectory() as temp_dir: - temp_path = Path(temp_dir) - - # Create a Dockerfile similar to what Clustrix might use - dockerfile_content = """ -FROM python:3.11-slim - -# Install system dependencies -RUN apt-get update && apt-get install -y \\ - build-essential \\ - && rm -rf /var/lib/apt/lists/* - -# Set working directory -WORKDIR /app - -# Install Python dependencies -RUN pip install --no-cache-dir \\ - numpy \\ - cloudpickle \\ - requests - -# Create test script -COPY test_script.py /app/ - -# Set entrypoint -ENTRYPOINT ["python", "/app/test_script.py"] -""" - - # Create test script - test_script_content = ''' -import sys -import json -import numpy as np -import cloudpickle -from datetime import datetime - -def clustrix_container_test(): - """Test function for container execution.""" - - # Perform computation - data = np.random.rand(1000) - result = { - "mean": float(np.mean(data)), - "std": float(np.std(data)), - "container_test": True, - "timestamp": datetime.now().isoformat(), - "python_version": sys.version.split()[0], - "success": True - } - - return result - -if __name__ == "__main__": - try: - print("Starting Clustrix container test...") - - # Test cloudpickle functionality - serialized = cloudpickle.dumps(clustrix_container_test) - deserialized = cloudpickle.loads(serialized) - - result = deserialized() - - print("CLUSTRIX_RESULT_START") - print(json.dumps(result, indent=2)) - print("CLUSTRIX_RESULT_END") - - print("Container test completed successfully!") - - except Exception as e: - print(f"Container test failed: {e}") - sys.exit(1) -''' - - # Write files - dockerfile_path = temp_path / "Dockerfile" - script_path = temp_path / "test_script.py" - - dockerfile_path.write_text(dockerfile_content) - script_path.write_text(test_script_content) - - # Build image - image_tag = f"clustrix-test:{int(time.time())}" - print(f"🔨 Building image: {image_tag}") - - build_cmd = f"cd {temp_path} && docker build -t {image_tag} ." - result = run_command(build_cmd, timeout=300) - - if not result or result.returncode != 0: - print("❌ Image build failed") - if result: - print(f" Error: {result.stderr[-500:]}") # Last 500 chars - return False, None - - print("✅ Image built successfully") - - # Test running the container - print("🚀 Testing container execution...") - run_cmd = f"docker run --rm {image_tag}" - result = run_command(run_cmd, timeout=60) - - if result and result.returncode == 0: - output = result.stdout - if "Container test completed successfully!" in output: - print("✅ Container execution successful") - - # Extract result - try: - start_idx = output.find("CLUSTRIX_RESULT_START") - end_idx = output.find("CLUSTRIX_RESULT_END") - if start_idx != -1 and end_idx != -1: - json_str = output[ - start_idx + len("CLUSTRIX_RESULT_START") : end_idx - ].strip() - result_data = json.loads(json_str) - print(f" Mean: {result_data.get('mean', 'N/A'):.4f}") - print(f" Python: {result_data.get('python_version', 'N/A')}") - except Exception as e: - print(f" ⚠️ Could not parse result: {e}") - - return True, image_tag - else: - print("❌ Container execution produced unexpected output") - print(f" Output: {output[:200]}...") - return False, image_tag - else: - print("❌ Container execution failed") - if result: - print(f" Error: {result.stderr}") - return False, image_tag - - -def test_image_tagging_and_management(): - """Test image tagging operations.""" - print("\n🏷️ Image Tagging and Management Test") - print("=" * 40) - - # Build a simple test image first - with tempfile.TemporaryDirectory() as temp_dir: - temp_path = Path(temp_dir) - - simple_dockerfile = """ -FROM alpine:latest -RUN echo "Clustrix test image" > /test.txt -CMD ["cat", "/test.txt"] -""" - - dockerfile_path = temp_path / "Dockerfile" - dockerfile_path.write_text(simple_dockerfile) - - base_tag = f"clustrix-tag-test:{int(time.time())}" - - # Build base image - build_cmd = f"cd {temp_path} && docker build -t {base_tag} ." - result = run_command(build_cmd, timeout=120) - - if not result or result.returncode != 0: - print("❌ Failed to build test image for tagging") - return False - - print(f"✅ Built test image: {base_tag}") - - # Test different tagging patterns - tag_tests = [ - f"clustrix-test:latest", - f"clustrix-test:v1.0.0", - f"clustrix-test:dev-{int(time.time())}", - ] - - for new_tag in tag_tests: - print(f"🏷️ Testing tag: {new_tag}") - - # Tag the image - tag_cmd = f"docker tag {base_tag} {new_tag}" - result = run_command(tag_cmd) - - if result and result.returncode == 0: - print(f" ✅ Tagged successfully") - - # Verify tag exists - list_cmd = f"docker images {new_tag} --format 'table {{.Repository}}\\t{{.Tag}}'" - result = run_command(list_cmd) - - if result and new_tag.split(":")[0] in result.stdout: - print(f" ✅ Tag verified in image list") - else: - print(f" ❌ Tag not found in image list") - return False - else: - print(f" ❌ Tagging failed") - return False - - # Clean up test images - print("🧹 Cleaning up test images...") - for tag in [base_tag] + tag_tests: - run_command(f"docker rmi {tag}", check=False) - - print("✅ Image tagging and management working") - return True - - -def test_registry_push_simulation(): - """Simulate registry push operations (without actual push to avoid pollution).""" - print("\n📤 Registry Push Simulation Test") - print("=" * 40) - - # Note: We won't actually push to avoid polluting public registries - # But we'll test the command structure and validation - - print("ℹ️ Simulating registry push operations...") - print(" (Not actually pushing to avoid registry pollution)") - - # Test command structure for different registries - registries = { - "Docker Hub": { - "format": "username/repository:tag", - "example": "clustrix/test-image:v1.0.0", - "login_cmd": "docker login", - "push_cmd": "docker push clustrix/test-image:v1.0.0", - }, - "AWS ECR": { - "format": "aws_account_id.dkr.ecr.region.amazonaws.com/repository:tag", - "example": "123456789012.dkr.ecr.us-east-1.amazonaws.com/clustrix:v1.0.0", - "login_cmd": "aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com", - "push_cmd": "docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/clustrix:v1.0.0", - }, - "Google GCR": { - "format": "gcr.io/project-id/repository:tag", - "example": "gcr.io/my-project/clustrix:v1.0.0", - "login_cmd": 'docker login -u _json_key -p "$(cat key.json)" https://gcr.io', - "push_cmd": "docker push gcr.io/my-project/clustrix:v1.0.0", - }, - } - - for registry_name, config in registries.items(): - print(f"\n📋 {registry_name} Push Format:") - print(f" Format: {config['format']}") - print(f" Example: {config['example']}") - print(f" Login: {config['login_cmd'][:50]}...") - print(f" Push: {config['push_cmd']}") - - # Test image naming validation - print("\n🔍 Testing image name validation...") - - test_names = [ - ("valid-name:v1.0.0", True), - ("username/repo:latest", True), - ("gcr.io/project/app:tag", True), - ("invalid..name:tag", False), - ("UPPERCASE:tag", False), # Docker Hub doesn't allow uppercase - ("valid-name", True), # latest implied - ] - - for name, should_be_valid in test_names: - # Simple validation check - is_valid = all(c.islower() or c.isdigit() or c in "-._:/" for c in name) - is_valid = is_valid and not ".." in name and not name.startswith("-") - - status = "✅" if is_valid == should_be_valid else "❌" - print(f" {status} {name}: {'Valid' if is_valid else 'Invalid'}") - - print("\n✅ Registry push simulation completed") - print(" Clustrix can generate proper image names and push commands") - - return True - - -def main(): - """Main validation function.""" - print("🚀 Starting Container Registry Validation") - print("=" * 60) - - # Test sequence - tests = [ - ("Docker Availability", test_docker_availability), - ("Docker Hub Connectivity", test_docker_hub_connectivity), - ("Container Image Building", test_container_image_building), - ("Image Tagging and Management", test_image_tagging_and_management), - ("Registry Push Simulation", test_registry_push_simulation), - ] - - results = {} - built_image = None - - for test_name, test_func in tests: - try: - print(f"\n🔄 Running: {test_name}") - - if test_func == test_container_image_building: - success, image_tag = test_func() - built_image = image_tag - else: - success = test_func() - - results[test_name] = success - - if success: - print(f"✅ {test_name}: PASSED") - else: - print(f"❌ {test_name}: FAILED") - break # Stop on first failure - - except Exception as e: - print(f"❌ {test_name}: ERROR - {e}") - results[test_name] = False - break - - # Cleanup - if built_image: - print(f"\n🧹 Cleaning up built image: {built_image}") - run_command(f"docker rmi {built_image}", check=False) - - # Summary - print("\n📊 Container Registry Validation Summary") - print("=" * 60) - - passed = sum(1 for success in results.values() if success) - total = len(results) - - for test_name, success in results.items(): - status = "✅ PASSED" if success else "❌ FAILED" - print(f" {test_name}: {status}") - - print(f"\n🎯 Overall Result: {passed}/{total} tests passed") - - if passed == total: - print("🎉 Container registry operations fully validated!") - print(" Clustrix can build, tag, and push container images.") - return 0 - else: - print("⚠️ Some container registry tests failed.") - print(" Check Docker installation and permissions.") - return 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/real_world/api_validation/validate_docker_functionality.py b/tests/real_world/api_validation/validate_docker_functionality.py deleted file mode 100644 index 88c6818c..00000000 --- a/tests/real_world/api_validation/validate_docker_functionality.py +++ /dev/null @@ -1,431 +0,0 @@ -#!/usr/bin/env python3 -""" -Docker Functionality Validation Script - -This script validates Docker functionality that Clustrix depends on. -Since Clustrix uses container images for Kubernetes deployments, we need to ensure -Docker operations work correctly. -""" - -import sys -import subprocess -import tempfile -import os -import json -import logging -from pathlib import Path - -# Configure logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def run_command(cmd, timeout=60, check=True): - """Run a command and return the result.""" - try: - result = subprocess.run( - cmd, - shell=True, - capture_output=True, - text=True, - timeout=timeout, - check=check, - ) - return result - except subprocess.CalledProcessError as e: - logger.error(f"Command failed: {cmd}") - logger.error(f"Error: {e.stderr}") - return e - except subprocess.TimeoutExpired: - logger.error(f"Command timed out: {cmd}") - return None - - -def test_docker_availability(): - """Test if Docker is available and working.""" - print("🔍 Docker Availability Test") - print("=" * 50) - - # Check Docker version - result = run_command("docker --version", check=False) - if result and result.returncode == 0: - print(f"✅ Docker installed: {result.stdout.strip()}") - else: - print("❌ Docker not installed or not accessible") - return False - - # Check Docker daemon - result = run_command("docker info --format '{{.ServerVersion}}'", check=False) - if result and result.returncode == 0: - print(f"✅ Docker daemon running: v{result.stdout.strip()}") - else: - print("❌ Docker daemon not running") - return False - - # Check Docker permissions - result = run_command("docker ps", check=False) - if result and result.returncode == 0: - print("✅ Docker permissions working") - else: - print("❌ Docker permission issues") - print(" Try: sudo usermod -aG docker $USER") - return False - - return True - - -def test_basic_container_operations(): - """Test basic container operations.""" - print("\n🧪 Basic Container Operations Test") - print("=" * 50) - - # Pull a lightweight image - print("📥 Pulling test image...") - result = run_command("docker pull hello-world", timeout=120) - if not result or result.returncode != 0: - print("❌ Failed to pull hello-world image") - return False - print("✅ Image pull successful") - - # Run a simple container - print("🚀 Running test container...") - result = run_command("docker run --rm hello-world", timeout=30) - if not result or result.returncode != 0: - print("❌ Failed to run hello-world container") - return False - print("✅ Container execution successful") - - # List images - result = run_command( - "docker images hello-world --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.Size}}'" - ) - if result and result.returncode == 0: - print("✅ Image listing working") - print(f" {result.stdout.strip()}") - - return True - - -def test_python_container_functionality(): - """Test Python container functionality similar to what Clustrix would use.""" - print("\n🐍 Python Container Functionality Test") - print("=" * 50) - - # Pull Python image (same as used in Kubernetes tutorial) - print("📥 Pulling Python 3.11 slim image...") - result = run_command("docker pull python:3.11-slim", timeout=300) - if not result or result.returncode != 0: - print("❌ Failed to pull python:3.11-slim image") - return False - print("✅ Python image pull successful") - - # Test Python execution in container - print("🧪 Testing Python execution in container...") - python_test_cmd = """ - docker run --rm python:3.11-slim python3 -c " -import sys -import os -import json -import numpy as np -print('Python version:', sys.version) -print('Platform:', sys.platform) -print('NumPy available:', 'numpy' in sys.modules or True) -result = {'success': True, 'python_version': sys.version.split()[0]} -print('Test result:', json.dumps(result)) -" - """ - - # First install numpy in the container - install_cmd = """ - docker run --rm python:3.11-slim pip install numpy - """ - - print(" Installing NumPy in container...") - result = run_command(install_cmd, timeout=120) - if not result or result.returncode != 0: - print("❌ Failed to install NumPy in container") - return False - - print(" Running Python test...") - result = run_command(python_test_cmd, timeout=60) - if not result or result.returncode != 0: - print("❌ Python execution failed in container") - if result: - print(f" Error: {result.stderr}") - return False - - print("✅ Python container execution successful") - print(f" Output preview: {result.stdout.strip()[:200]}...") - - return True - - -def test_clustrix_like_container_execution(): - """Test container execution similar to Clustrix patterns.""" - print("\n🔬 Clustrix-like Container Execution Test") - print("=" * 50) - - # Create a temporary directory for test scripts - with tempfile.TemporaryDirectory() as temp_dir: - temp_path = Path(temp_dir) - - # Create a test script similar to what Clustrix might generate - test_script = temp_path / "clustrix_test.py" - test_script.write_text(""" -import sys -import os -import json -import time -from datetime import datetime - -def clustrix_test_function(): - '''Simulate a Clustrix-style function execution.''' - - print("🚀 Starting Clustrix-style computation...") - print(f"Python version: {sys.version}") - print(f"Working directory: {os.getcwd()}") - print(f"Environment variables: {len(os.environ)}") - - # Simulate some computation - import math - result = 0 - for i in range(1000000): - result += math.sin(i) * math.cos(i) - - computation_result = { - 'function_name': 'clustrix_test_function', - 'computation_result': result, - 'execution_time': time.time(), - 'timestamp': datetime.now().isoformat(), - 'environment': { - 'python_version': sys.version.split()[0], - 'platform': sys.platform, - 'working_dir': os.getcwd(), - 'hostname': os.environ.get('HOSTNAME', 'unknown') - }, - 'success': True - } - - print("✅ Computation completed successfully") - print(f"Result: {json.dumps(computation_result, indent=2)}") - - return computation_result - -if __name__ == "__main__": - try: - result = clustrix_test_function() - print(f"\\n🎉 Test function completed successfully!") - print(f"Result summary: {result['computation_result']:.6f}") - except Exception as e: - print(f"❌ Test function failed: {e}") - sys.exit(1) -""") - - # Create a simple requirements file - requirements_file = temp_path / "requirements.txt" - requirements_file.write_text("# No special requirements for this test\\n") - - # Run the test script in a Python container - print("📝 Created test script") - print("🚀 Running Clustrix-style execution in container...") - - container_cmd = f""" - docker run --rm \\ - -v {temp_path}:/app \\ - -w /app \\ - python:3.11-slim \\ - python clustrix_test.py - """ - - result = run_command(container_cmd, timeout=60) - if not result or result.returncode != 0: - print("❌ Clustrix-style container execution failed") - if result: - print(f" Error: {result.stderr}") - return False - - print("✅ Clustrix-style execution successful") - - # Check if the output contains expected patterns - output = result.stdout - if ( - "Test function completed successfully" in output - and "computation_result" in output - ): - print("✅ Expected output patterns found") - else: - print("⚠️ Unexpected output format") - - print(f"📊 Container execution output preview:") - lines = output.strip().split("\\n") - for i, line in enumerate(lines[:15]): # First 15 lines - print(f" {line}") - if len(lines) > 15: - print(f" ... ({len(lines) - 15} more lines)") - - return True - - -def test_container_resource_constraints(): - """Test container resource constraints.""" - print("\n📊 Container Resource Constraints Test") - print("=" * 50) - - # Test memory limit - print("🧠 Testing memory constraints...") - memory_test_cmd = """ - docker run --rm --memory=100m python:3.11-slim python3 -c " -import sys -print('Memory limit test - should complete successfully') -data = list(range(10000)) # Small allocation -print(f'Allocated list with {len(data)} elements') -print('Memory constraint test passed') -" - """ - - result = run_command(memory_test_cmd, timeout=30) - if result and result.returncode == 0: - print("✅ Memory constraints working") - else: - print("❌ Memory constraint test failed") - return False - - # Test CPU limit (simplified) - print("⚙️ Testing CPU constraints...") - cpu_test_cmd = """ - docker run --rm --cpus=0.5 python:3.11-slim python3 -c " -import time -import math -print('CPU limit test - performing computation...') -start = time.time() -result = sum(math.sin(i) for i in range(100000)) -duration = time.time() - start -print(f'Computation completed in {duration:.3f} seconds') -print(f'Result: {result:.6f}') -print('CPU constraint test passed') -" - """ - - result = run_command(cpu_test_cmd, timeout=30) - if result and result.returncode == 0: - print("✅ CPU constraints working") - else: - print("❌ CPU constraint test failed") - return False - - return True - - -def test_container_networking(): - """Test basic container networking.""" - print("\n🌐 Container Networking Test") - print("=" * 50) - - # Test network connectivity from container - network_test_cmd = """ - docker run --rm python:3.11-slim python3 -c " -import urllib.request -import json - -try: - # Test connectivity to a public API - response = urllib.request.urlopen('https://httpbin.org/ip', timeout=10) - data = json.loads(response.read().decode()) - print('✅ Network connectivity working') - print(f'External IP: {data.get(\"origin\", \"unknown\")}') -except Exception as e: - print(f'❌ Network test failed: {e}') - raise -" - """ - - result = run_command(network_test_cmd, timeout=30) - if result and result.returncode == 0: - print("✅ Container networking working") - else: - print("❌ Container networking test failed") - return False - - return True - - -def cleanup_test_images(): - """Clean up test images to save space.""" - print("\n🧹 Cleanup Test Images") - print("=" * 50) - - # Remove hello-world image - result = run_command("docker rmi hello-world", check=False) - if result and result.returncode == 0: - print("✅ Cleaned up hello-world image") - - # Note: We keep python:3.11-slim as it might be useful - print("📝 Keeping python:3.11-slim image (useful for Clustrix)") - - # Show remaining images - result = run_command( - "docker images --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.Size}}'" - ) - if result and result.returncode == 0: - print("📊 Remaining Docker images:") - lines = result.stdout.strip().split("\\n") - for line in lines[:10]: # First 10 images - print(f" {line}") - - -def main(): - """Main validation function.""" - print("🚀 Starting Docker Functionality Validation") - print("=" * 60) - - tests = [ - ("Docker Availability", test_docker_availability), - ("Basic Container Operations", test_basic_container_operations), - ("Python Container Functionality", test_python_container_functionality), - ("Clustrix-like Execution", test_clustrix_like_container_execution), - ("Resource Constraints", test_container_resource_constraints), - ("Container Networking", test_container_networking), - ] - - results = {} - - for test_name, test_func in tests: - try: - print(f"\\n🔄 Running: {test_name}") - success = test_func() - results[test_name] = success - if success: - print(f"✅ {test_name}: PASSED") - else: - print(f"❌ {test_name}: FAILED") - except Exception as e: - print(f"❌ {test_name}: ERROR - {e}") - results[test_name] = False - - # Cleanup - cleanup_test_images() - - # Summary - print("\\n📊 Validation Summary") - print("=" * 60) - passed = sum(1 for success in results.values() if success) - total = len(results) - - for test_name, success in results.items(): - status = "✅ PASSED" if success else "❌ FAILED" - print(f" {test_name}: {status}") - - print(f"\\n🎯 Overall Result: {passed}/{total} tests passed") - - if passed == total: - print("🎉 All Docker functionality tests passed!") - print(" Clustrix container operations should work correctly.") - return 0 - else: - print("⚠️ Some Docker tests failed.") - print(" Check Docker installation and permissions.") - return 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/real_world/test_container_registry_comprehensive.py b/tests/real_world/test_container_registry_comprehensive.py deleted file mode 100644 index 7077bb00..00000000 --- a/tests/real_world/test_container_registry_comprehensive.py +++ /dev/null @@ -1,674 +0,0 @@ -""" -Comprehensive real-world container and registry validation tests. - -This module tests container image operations and registry functionality, -addressing Phase 3 of Issue #63 external service validation. - -Tests cover: -- Container image accessibility and functionality -- Registry authentication (Docker Hub, ECR, GCR, ACR) -- Image pull policies and caching -- Custom container image building and validation -- Multi-registry compatibility -- Private registry authentication -- Container runtime environment validation - -NO MOCK TESTS - Only real container registry and image testing. - -Supports multiple registry types: -- Public: Docker Hub, Quay.io, Red Hat Registry -- Cloud: ECR (AWS), GCR (Google), ACR (Azure) -- Private: Self-hosted registries -""" - -import logging -import os -import subprocess -import sys -import time -from typing import Dict, Any, Optional, List - -import pytest - -from clustrix import ClusterExecutor - -# Import credential manager and test utilities -sys.path.append(os.path.dirname(__file__)) -from credential_manager import get_credential_manager # noqa: E402 - -# Configure logging for detailed test debugging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def get_docker_credentials() -> Optional[Dict[str, str]]: - """Get Docker registry credentials from 1Password or environment.""" - manager = get_credential_manager() - - # Try to get Docker credentials from credential manager - docker_creds = None - if hasattr(manager, "get_docker_credentials"): - try: - # Check if ValidationCredentials has Docker support - if hasattr(manager, "_validation_creds") and manager._validation_creds: - docker_creds = manager._validation_creds.get_docker_credentials() - except Exception as e: - logger.debug(f"Could not get Docker credentials from 1Password: {e}") - - if docker_creds: - return docker_creds - - # Fallback to environment variables - username = os.getenv("DOCKER_USERNAME") or os.getenv("DOCKERHUB_USERNAME") - password = os.getenv("DOCKER_PASSWORD") or os.getenv("DOCKERHUB_TOKEN") - registry = os.getenv("DOCKER_REGISTRY", "docker.io") - - if username and password: - return { - "username": username, - "password": password, - "registry": registry, - } - - return None - - -def get_container_test_images() -> List[Dict[str, Any]]: - """Get list of container images to test for accessibility and functionality.""" - return [ - { - "name": "python:3.11-slim", - "registry": "docker.io", - "type": "public", - "description": "Default Clustrix Python image", - "test_command": "python --version", - "expected_packages": ["cloudpickle"], # Should be installable - }, - { - "name": "python:3.10-slim", - "registry": "docker.io", - "type": "public", - "description": "Alternative Python version", - "test_command": "python --version", - "expected_packages": ["cloudpickle"], - }, - { - "name": "python:3.9-slim", - "registry": "docker.io", - "type": "public", - "description": "Older Python version for compatibility", - "test_command": "python --version", - "expected_packages": ["cloudpickle"], - }, - { - "name": "gcr.io/distroless/python3", - "registry": "gcr.io", - "type": "public", - "description": "Google distroless Python image", - "test_command": "python3 --version", - "expected_packages": [], # Minimal image - }, - ] - - -def check_docker_available() -> bool: - """Check if Docker is available for container operations.""" - try: - result = subprocess.run(["docker", "--version"], capture_output=True, text=True) - return result.returncode == 0 - except FileNotFoundError: - return False - - -def test_image_accessibility(image_name: str) -> Dict[str, Any]: - """Test if a container image is accessible and functional.""" - if not check_docker_available(): - return {"accessible": False, "reason": "Docker not available"} - - try: - # Try to pull the image - logger.info(f"Testing image accessibility: {image_name}") - pull_result = subprocess.run( - ["docker", "pull", image_name], capture_output=True, text=True, timeout=300 - ) - - if pull_result.returncode != 0: - return { - "accessible": False, - "reason": f"Pull failed: {pull_result.stderr}", - "image": image_name, - } - - # Test basic Python functionality - run_result = subprocess.run( - [ - "docker", - "run", - "--rm", - image_name, - "python", - "-c", - "import sys; print(sys.version)", - ], - capture_output=True, - text=True, - timeout=60, - ) - - if run_result.returncode != 0: - return { - "accessible": False, - "reason": f"Python execution failed: {run_result.stderr}", - "image": image_name, - } - - python_version = run_result.stdout.strip() - - return { - "accessible": True, - "python_version": python_version, - "image": image_name, - "size_info": "Available via docker images command", - } - - except subprocess.TimeoutExpired: - return { - "accessible": False, - "reason": "Operation timed out", - "image": image_name, - } - except Exception as e: - return { - "accessible": False, - "reason": f"Unexpected error: {e}", - "image": image_name, - } - - -@pytest.mark.real_world -class TestContainerRegistryComprehensive: - """Comprehensive container and registry integration tests addressing Issue #63 Phase 3.""" - - def setup_method(self): - """Setup test environment.""" - self.docker_available = check_docker_available() - self.docker_creds = get_docker_credentials() - self.test_images = get_container_test_images() - - def teardown_method(self): - """Cleanup test environment.""" - # Clean up any test containers or images if needed - pass - - @pytest.mark.real_world - def test_default_python_image_accessibility(self): - """Test that the default Clustrix Python image is accessible and functional.""" - default_image = "python:3.11-slim" - logger.info(f"Testing default Python image: {default_image}") - - result = test_image_accessibility(default_image) - - if not self.docker_available: - pytest.skip("Docker not available for container testing") - - assert result[ - "accessible" - ], f"Default image not accessible: {result.get('reason')}" - assert "python_version" in result, "Should detect Python version" - assert ( - "3.11" in result["python_version"] - ), f"Expected Python 3.11, got: {result['python_version']}" - - logger.info(f"✅ Default image {default_image} accessible and functional") - logger.info(f" Python version: {result['python_version']}") - - @pytest.mark.real_world - def test_alternative_python_images_compatibility(self): - """Test compatibility across different Python image versions.""" - if not self.docker_available: - pytest.skip("Docker not available for container testing") - - logger.info("Testing alternative Python image compatibility") - - results = [] - for image_info in self.test_images: - image_name = image_info["name"] - logger.info(f"Testing image: {image_name}") - - result = test_image_accessibility(image_name) - result["image_info"] = image_info - results.append(result) - - if result["accessible"]: - logger.info( - f"✅ {image_name}: {result.get('python_version', 'accessible')}" - ) - else: - logger.warning(f"⚠️ {image_name}: {result.get('reason')}") - - # At least the default images should be accessible - accessible_count = sum(1 for r in results if r["accessible"]) - assert ( - accessible_count >= 2 - ), f"Expected at least 2 accessible images, got {accessible_count}" - - # The primary python:3.11-slim should definitely work - primary_result = next( - (r for r in results if r["image"] == "python:3.11-slim"), None - ) - assert ( - primary_result and primary_result["accessible"] - ), "Primary python:3.11-slim image must be accessible" - - logger.info( - f"✅ Image compatibility test: {accessible_count}/{len(results)} images accessible" - ) - - @pytest.mark.real_world - def test_cloudpickle_dependency_in_containers(self): - """Test that cloudpickle (critical Clustrix dependency) works in container images.""" - if not self.docker_available: - pytest.skip("Docker not available for container testing") - - logger.info("Testing cloudpickle dependency in containers") - - # Test cloudpickle installation and basic functionality - test_script = """ -import subprocess -import sys - -# Install cloudpickle -result = subprocess.run([sys.executable, "-m", "pip", "install", "cloudpickle"], - capture_output=True, text=True) -if result.returncode != 0: - print(f"INSTALL_ERROR: {result.stderr}") - exit(1) - -# Test cloudpickle functionality -import cloudpickle - -def test_function(x): - return x * 2 + 1 - -# Serialize and deserialize -serialized = cloudpickle.dumps(test_function) -deserialized = cloudpickle.loads(serialized) - -# Test execution -test_result = deserialized(5) -expected = 11 - -if test_result == expected: - print(f"CLOUDPICKLE_SUCCESS: {test_result}") -else: - print(f"CLOUDPICKLE_ERROR: Expected {expected}, got {test_result}") - exit(1) -""" - - # Test on primary image - image = "python:3.11-slim" - logger.info(f"Testing cloudpickle in {image}") - - try: - result = subprocess.run( - ["docker", "run", "--rm", image, "python", "-c", test_script], - capture_output=True, - text=True, - timeout=120, - ) - - if result.returncode != 0: - logger.error(f"Cloudpickle test failed in {image}: {result.stderr}") - assert False, f"Cloudpickle test failed: {result.stderr}" - - output = result.stdout.strip() - assert ( - "CLOUDPICKLE_SUCCESS: 11" in output - ), f"Expected success message, got: {output}" - - logger.info(f"✅ Cloudpickle working correctly in {image}") - - except subprocess.TimeoutExpired: - assert ( - False - ), "Cloudpickle test timed out - dependency installation too slow" - - @pytest.mark.real_world - def test_registry_authentication_docker_hub(self): - """Test Docker Hub registry authentication if credentials available.""" - if not self.docker_available: - pytest.skip("Docker not available for container testing") - - if not self.docker_creds: - pytest.skip("Docker registry credentials not available") - - logger.info("Testing Docker Hub registry authentication") - - username = self.docker_creds["username"] - password = self.docker_creds["password"] - registry = self.docker_creds.get("registry", "docker.io") - - try: - # Test login - login_result = subprocess.run( - ["docker", "login", registry, "-u", username, "--password-stdin"], - input=password, - text=True, - capture_output=True, - timeout=30, - ) - - if login_result.returncode == 0: - logger.info(f"✅ Successfully authenticated with {registry}") - - # Test logout - subprocess.run(["docker", "logout", registry], capture_output=True) - logger.info(f"✅ Successfully logged out from {registry}") - - else: - logger.warning( - f"⚠️ Authentication failed with {registry}: {login_result.stderr}" - ) - # This isn't necessarily a failure - credentials might be read-only tokens - - except subprocess.TimeoutExpired: - logger.warning("Docker login timed out - network or registry issues") - - @pytest.mark.real_world - def test_kubernetes_with_custom_images(self): - """Test Kubernetes job execution with alternative container images.""" - # This requires Kubernetes cluster + custom image configuration - from test_kubernetes_comprehensive import ( - create_test_kubernetes_config, - ) - - k8s_config = create_test_kubernetes_config() - if not k8s_config: - pytest.skip("Kubernetes cluster not available for custom image testing") - - logger.info("Testing Kubernetes with alternative container images") - - # Test with Python 3.10 instead of default 3.11 - k8s_config.k8s_image = "python:3.10-slim" - executor = ClusterExecutor(k8s_config) - - def version_test() -> str: - """Function to test Python version in alternative container.""" - import sys - - return f"Python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" - - try: - job_id = executor.submit(version_test) - logger.info( - f"Submitted K8s job {job_id} with custom image: {k8s_config.k8s_image}" - ) - - result = executor.wait_for_result(job_id) - - assert "Python 3.10" in result, f"Expected Python 3.10, got: {result}" - logger.info(f"✅ Kubernetes custom image test successful: {result}") - - except Exception as e: - # Log but don't fail - this depends on K8s cluster availability - logger.warning( - f"Kubernetes custom image test failed (expected without cluster): {e}" - ) - finally: - try: - executor.disconnect() - except Exception: - pass - - @pytest.mark.real_world - def test_image_pull_policies_and_caching(self): - """Test different image pull policies and caching behavior.""" - if not self.docker_available: - pytest.skip("Docker not available for pull policy testing") - - logger.info("Testing image pull policies and caching") - - test_image = "python:3.11-slim" - - try: - # First, ensure image is not cached locally - subprocess.run(["docker", "rmi", test_image], capture_output=True) - - # Test "Always" pull behavior - start_time = time.time() - pull_result = subprocess.run( - ["docker", "pull", test_image], - capture_output=True, - text=True, - timeout=300, - ) - - first_pull_time = time.time() - start_time - - assert ( - pull_result.returncode == 0 - ), f"Initial pull failed: {pull_result.stderr}" - logger.info(f"✅ Initial pull of {test_image} took {first_pull_time:.1f}s") - - # Test "IfNotPresent" behavior (should be much faster) - start_time = time.time() - cached_result = subprocess.run( - ["docker", "pull", test_image], - capture_output=True, - text=True, - timeout=60, - ) - - cached_pull_time = time.time() - start_time - - assert ( - cached_result.returncode == 0 - ), f"Cached pull failed: {cached_result.stderr}" - - # Cached pull should be significantly faster - logger.info(f"✅ Cached pull of {test_image} took {cached_pull_time:.1f}s") - - # Verify the image works - test_result = subprocess.run( - [ - "docker", - "run", - "--rm", - test_image, - "python", - "-c", - 'print("Image functional")', - ], - capture_output=True, - text=True, - timeout=30, - ) - - assert test_result.returncode == 0, "Image execution failed after pull" - assert "Image functional" in test_result.stdout, "Expected output not found" - - logger.info("✅ Image pull policies and caching working correctly") - - except subprocess.TimeoutExpired: - pytest.skip("Image pull operations timed out - network issues") - - @pytest.mark.real_world - def test_container_runtime_environment_validation(self): - """Test that container runtime provides expected environment for Clustrix jobs.""" - if not self.docker_available: - pytest.skip("Docker not available for runtime testing") - - logger.info("Testing container runtime environment") - - # Test comprehensive environment validation - env_test_script = """ -import sys -import os -import platform -import subprocess - -# Gather environment information -env_info = { - "python_version": sys.version, - "platform": platform.platform(), - "architecture": platform.architecture(), - "python_path": sys.executable, - "working_directory": os.getcwd(), - "environment_vars": len(os.environ), - "user_id": os.getuid() if hasattr(os, "getuid") else "unknown", -} - -# Test package installation capability -try: - result = subprocess.run([sys.executable, "-m", "pip", "--version"], - capture_output=True, text=True, timeout=30) - env_info["pip_available"] = result.returncode == 0 - env_info["pip_version"] = result.stdout.strip() if result.returncode == 0 else "failed" -except Exception as e: - env_info["pip_available"] = False - env_info["pip_error"] = str(e) - -# Test basic Python capabilities needed by Clustrix -try: - import json - import base64 - import pickle - env_info["core_modules"] = True -except ImportError as e: - env_info["core_modules"] = False - env_info["import_error"] = str(e) - -# Output results in a parseable format -print("ENVIRONMENT_INFO:") -for key, value in env_info.items(): - print(f"{key}: {value}") -""" - - image = "python:3.11-slim" - logger.info(f"Testing runtime environment in {image}") - - try: - result = subprocess.run( - ["docker", "run", "--rm", image, "python", "-c", env_test_script], - capture_output=True, - text=True, - timeout=120, - ) - - assert result.returncode == 0, f"Environment test failed: {result.stderr}" - - output = result.stdout - assert "ENVIRONMENT_INFO:" in output, "Expected environment info not found" - assert "python_version:" in output, "Python version info missing" - assert "pip_available: True" in output, "pip should be available" - assert ( - "core_modules: True" in output - ), "Core Python modules should be available" - - logger.info("✅ Container runtime environment validation successful") - # NB: the split() must stay outside the f-string -- an f-string - # expression may not contain a backslash before Python 3.12, and - # this project supports >=3.8, so inlining it is a SyntaxError that - # breaks collection of the whole module. - property_count = len(output.split("\n")) - logger.info(f"Environment details: {property_count} properties checked") - - except subprocess.TimeoutExpired: - assert False, "Environment validation timed out" - - @pytest.mark.real_world - def test_multi_registry_compatibility(self): - """Test compatibility across different container registries.""" - if not self.docker_available: - pytest.skip("Docker not available for multi-registry testing") - - logger.info("Testing multi-registry compatibility") - - # Test images from different registries - registry_images = [ - { - "image": "docker.io/python:3.11-slim", - "registry": "Docker Hub", - "expected_accessible": True, - }, - { - "image": "gcr.io/distroless/python3", - "registry": "Google Container Registry", - "expected_accessible": True, # Public image - }, - { - "image": "quay.io/python/python:3.11", - "registry": "Quay.io", - "expected_accessible": True, # If it exists - }, - ] - - successful_registries = [] - - for image_config in registry_images: - image_name = image_config["image"] - registry_name = image_config["registry"] - - logger.info(f"Testing {registry_name}: {image_name}") - - try: - # Attempt to pull image - pull_result = subprocess.run( - ["docker", "pull", image_name], - capture_output=True, - text=True, - timeout=120, - ) - - if pull_result.returncode == 0: - successful_registries.append(registry_name) - logger.info(f"✅ {registry_name} image accessible: {image_name}") - - # Quick functionality test - if "python" in image_name.lower(): - test_result = subprocess.run( - [ - "docker", - "run", - "--rm", - image_name, - "python", - "-c", - 'print("Registry test successful")', - ], - capture_output=True, - text=True, - timeout=30, - ) - - if test_result.returncode == 0: - logger.info(f"✅ {registry_name} image functional") - else: - logger.warning( - f"⚠️ {registry_name} image accessible but not functional" - ) - else: - logger.info( - f"⚠️ {registry_name} image not accessible (expected for some): {image_name}" - ) - - except subprocess.TimeoutExpired: - logger.warning(f"⚠️ {registry_name} pull timed out: {image_name}") - except Exception as e: - logger.warning(f"⚠️ {registry_name} test failed: {e}") - - # At least Docker Hub should work - assert ( - len(successful_registries) >= 1 - ), f"Expected at least 1 working registry, got: {successful_registries}" - assert "Docker Hub" in successful_registries, "Docker Hub should be accessible" - - logger.info( - f"✅ Multi-registry test: {len(successful_registries)} registries accessible" - ) - logger.info(f"Working registries: {', '.join(successful_registries)}") - - -if __name__ == "__main__": - # Run tests directly for debugging - pytest.main([__file__, "-v", "--tb=short", "-m", "real_world"]) diff --git a/tests/real_world/test_direct_cloud_compute_comprehensive.py b/tests/real_world/test_direct_cloud_compute_comprehensive.py deleted file mode 100644 index dd39ca01..00000000 --- a/tests/real_world/test_direct_cloud_compute_comprehensive.py +++ /dev/null @@ -1,628 +0,0 @@ -""" -Comprehensive real-world direct cloud compute validation tests. - -This module tests direct cloud compute integration (AWS EC2, Azure VM, GCP Compute), -addressing Phase 6 of Issue #63 external service validation. - -Tests cover: -- AWS EC2 direct instance management and job execution -- Azure VM direct compute without SSH intermediary -- GCP Compute Engine instance lifecycle and job submission -- AWS Batch managed job queues -- Cloud-native resource management and scaling - -NO MOCK TESTS - Only real cloud compute integration testing. - -Supports multiple cloud providers: -- AWS: EC2, Batch, Systems Manager -- Azure: Virtual Machines, Container Instances -- GCP: Compute Engine, Cloud Run -- Hybrid cloud configurations -""" - -import pytest -import logging -import os -import json -import time -from pathlib import Path -from typing import Dict, Any, Optional - -# Import credential manager and test utilities -from .credential_manager import get_credential_manager - -# Configure logging for detailed test debugging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def get_aws_compute_credentials() -> Optional[Dict[str, str]]: - """Get AWS credentials for direct compute operations.""" - manager = get_credential_manager() - - # Try to get AWS credentials with appropriate permissions - if hasattr(manager, "get_aws_credentials"): - aws_creds = manager.get_aws_credentials() - if aws_creds: - return { - "access_key_id": aws_creds["access_key_id"], - "secret_access_key": aws_creds["secret_access_key"], - "region": aws_creds.get("region", "us-east-1"), - } - - return None - - -def get_azure_compute_credentials() -> Optional[Dict[str, str]]: - """Get Azure credentials for direct compute operations.""" - manager = get_credential_manager() - - # Try to get Azure credentials - if hasattr(manager, "get_azure_credentials"): - azure_creds = manager.get_azure_credentials() - if azure_creds and azure_creds.get("subscription_id"): - return azure_creds - - return None - - -def get_gcp_compute_credentials() -> Optional[Dict[str, str]]: - """Get GCP credentials for direct compute operations.""" - manager = get_credential_manager() - - # Try to get GCP credentials - if hasattr(manager, "get_gcp_credentials"): - gcp_creds = manager.get_gcp_credentials() - if gcp_creds and gcp_creds.get("project_id"): - return gcp_creds - - return None - - -def validate_aws_ec2_access(creds: Dict[str, str]) -> Dict[str, Any]: - """Validate AWS EC2 access and permissions.""" - logger.info("Testing AWS EC2 access") - - try: - import boto3 - from botocore.exceptions import ClientError - - # Create EC2 client - ec2_client = boto3.client( - "ec2", - aws_access_key_id=creds["access_key_id"], - aws_secret_access_key=creds["secret_access_key"], - region_name=creds["region"], - ) - - # Test basic EC2 permissions - try: - # List available regions (basic read permission) - regions = ec2_client.describe_regions() - region_count = len(regions["Regions"]) - - # List available instance types (requires describe permissions) - instances = ec2_client.describe_instance_types(MaxResults=5) - instance_types = [i["InstanceType"] for i in instances["InstanceTypes"]] - - # Check if we can list instances (may be empty) - instances = ec2_client.describe_instances(MaxResults=5) - - return { - "access_successful": True, - "region_count": region_count, - "sample_instance_types": instance_types, - "current_region": creds["region"], - "ec2_permissions": "read_confirmed", - } - - except ClientError as e: - return { - "access_successful": False, - "error_code": e.response["Error"]["Code"], - "error_message": e.response["Error"]["Message"], - } - - except ImportError: - return { - "access_successful": False, - "error": "boto3 not available - install with pip install boto3", - } - except Exception as e: - return {"access_successful": False, "error": f"Unexpected error: {e}"} - - -def validate_azure_vm_access(creds: Dict[str, str]) -> Dict[str, Any]: - """Validate Azure VM access and permissions.""" - logger.info("Testing Azure VM access") - - try: - from azure.identity import DefaultAzureCredential, ClientSecretCredential - from azure.mgmt.compute import ComputeManagementClient - from azure.core.exceptions import ClientAuthenticationError - - # Create credentials - if creds.get("client_secret"): - credential = ClientSecretCredential( - tenant_id=creds["tenant_id"], - client_id=creds["client_id"], - client_secret=creds["client_secret"], - ) - else: - credential = DefaultAzureCredential() - - # Create compute client - compute_client = ComputeManagementClient(credential, creds["subscription_id"]) - - try: - # Test VM access by listing VM sizes in a common region - vm_sizes = list(compute_client.virtual_machine_sizes.list("eastus")) - size_count = len(vm_sizes) - sample_sizes = [s.name for s in vm_sizes[:5]] - - # Try to list resource groups (may be empty) - from azure.mgmt.resource import ResourceManagementClient - - resource_client = ResourceManagementClient( - credential, creds["subscription_id"] - ) - rgs = list(resource_client.resource_groups.list()) - - return { - "access_successful": True, - "vm_size_count": size_count, - "sample_vm_sizes": sample_sizes, - "resource_group_count": len(rgs), - "subscription_id": creds["subscription_id"], - } - - except ClientAuthenticationError as e: - return {"access_successful": False, "error": f"Authentication failed: {e}"} - - except ImportError as e: - return { - "access_successful": False, - "error": f"Azure SDK not available - install with pip install azure-mgmt-compute azure-identity: {e}", - } - except Exception as e: - return {"access_successful": False, "error": f"Unexpected error: {e}"} - - -def validate_gcp_compute_access(creds: Dict[str, str]) -> Dict[str, Any]: - """Validate GCP Compute Engine access and permissions.""" - logger.info("Testing GCP Compute Engine access") - - try: - from google.cloud import compute_v1 - from google.auth.exceptions import DefaultCredentialsError - import google.auth - - # Set up authentication - if creds.get("service_account_json"): - # Use service account JSON - import tempfile - - with tempfile.NamedTemporaryFile( - mode="w", delete=False, suffix=".json" - ) as f: - f.write(creds["service_account_json"]) - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = f.name - - try: - # Create compute client - instances_client = compute_v1.InstancesClient() - zones_client = compute_v1.ZonesClient() - - # Test basic access by listing zones - zones = zones_client.list(project=creds["project_id"]) - zone_list = [zone.name for zone in zones] - - # Test machine types access - machine_types_client = compute_v1.MachineTypesClient() - if zone_list: - machine_types = machine_types_client.list( - project=creds["project_id"], zone=zone_list[0] - ) - sample_types = [mt.name for mt in list(machine_types)[:5]] - else: - sample_types = [] - - return { - "access_successful": True, - "project_id": creds["project_id"], - "zone_count": len(zone_list), - "sample_zones": zone_list[:5], - "sample_machine_types": sample_types, - } - - except DefaultCredentialsError as e: - return { - "access_successful": False, - "error": f"GCP authentication failed: {e}", - } - - except ImportError as e: - return { - "access_successful": False, - "error": f"Google Cloud SDK not available - install with pip install google-cloud-compute: {e}", - } - except Exception as e: - return {"access_successful": False, "error": f"Unexpected error: {e}"} - - -@pytest.mark.real_world -class TestDirectCloudComputeComprehensive: - """Comprehensive direct cloud compute integration tests addressing Issue #63 Phase 6.""" - - def setup_method(self): - """Setup test environment.""" - self.aws_creds = get_aws_compute_credentials() - self.azure_creds = get_azure_compute_credentials() - self.gcp_creds = get_gcp_compute_credentials() - - # Track which cloud providers are available - self.available_providers = [] - if self.aws_creds: - self.available_providers.append("aws") - if self.azure_creds: - self.available_providers.append("azure") - if self.gcp_creds: - self.available_providers.append("gcp") - - @pytest.mark.real_world - def test_aws_ec2_access_validation(self): - """Test AWS EC2 access and permission validation.""" - if not self.aws_creds: - pytest.skip("AWS credentials not available for EC2 testing") - - logger.info("Testing AWS EC2 access validation") - - result = validate_aws_ec2_access(self.aws_creds) - - if result["access_successful"]: - assert result["region_count"] > 0, "Should have access to AWS regions" - assert ( - len(result["sample_instance_types"]) > 0 - ), "Should list available instance types" - - logger.info("✅ AWS EC2 access validation successful") - logger.info(f" Regions available: {result['region_count']}") - logger.info(f" Sample instance types: {result['sample_instance_types']}") - else: - # Log but don't fail - may be due to limited permissions - logger.warning( - f"⚠️ AWS EC2 access limited: {result.get('error', result.get('error_message'))}" - ) - - @pytest.mark.real_world - def test_azure_vm_access_validation(self): - """Test Azure VM access and permission validation.""" - if not self.azure_creds: - pytest.skip("Azure credentials not available for VM testing") - - logger.info("Testing Azure VM access validation") - - result = validate_azure_vm_access(self.azure_creds) - - if result["access_successful"]: - assert result["vm_size_count"] > 0, "Should have access to VM sizes" - assert len(result["sample_vm_sizes"]) > 0, "Should list available VM sizes" - - logger.info("✅ Azure VM access validation successful") - logger.info(f" VM sizes available: {result['vm_size_count']}") - logger.info(f" Sample VM sizes: {result['sample_vm_sizes']}") - else: - # Log but don't fail - may be due to limited permissions - logger.warning(f"⚠️ Azure VM access limited: {result.get('error')}") - - @pytest.mark.real_world - def test_gcp_compute_access_validation(self): - """Test GCP Compute Engine access and permission validation.""" - if not self.gcp_creds: - pytest.skip("GCP credentials not available for Compute testing") - - logger.info("Testing GCP Compute Engine access validation") - - result = validate_gcp_compute_access(self.gcp_creds) - - if result["access_successful"]: - assert result["zone_count"] > 0, "Should have access to GCP zones" - - logger.info("✅ GCP Compute Engine access validation successful") - logger.info(f" Project: {result['project_id']}") - logger.info(f" Zones available: {result['zone_count']}") - logger.info(f" Sample zones: {result['sample_zones']}") - else: - # Log but don't fail - may be due to limited permissions - logger.warning(f"⚠️ GCP Compute access limited: {result.get('error')}") - - @pytest.mark.real_world - def test_aws_batch_service_validation(self): - """Test AWS Batch service access for managed job queues.""" - if not self.aws_creds: - pytest.skip("AWS credentials not available for Batch testing") - - logger.info("Testing AWS Batch service validation") - - try: - import boto3 - from botocore.exceptions import ClientError - - batch_client = boto3.client( - "batch", - aws_access_key_id=self.aws_creds["access_key_id"], - aws_secret_access_key=self.aws_creds["secret_access_key"], - region_name=self.aws_creds["region"], - ) - - try: - # Test Batch access by listing job queues - job_queues = batch_client.describe_job_queues() - queue_count = len(job_queues["jobQueues"]) - - # Test compute environments - compute_envs = batch_client.describe_compute_environments() - env_count = len(compute_envs["computeEnvironments"]) - - logger.info("✅ AWS Batch service accessible") - logger.info(f" Job queues: {queue_count}") - logger.info(f" Compute environments: {env_count}") - - except ClientError as e: - error_code = e.response["Error"]["Code"] - if error_code in ["AccessDenied", "UnauthorizedOperation"]: - logger.warning(f"⚠️ AWS Batch access denied: {error_code}") - else: - logger.warning(f"⚠️ AWS Batch error: {error_code}") - - except ImportError: - pytest.skip("boto3 not available for AWS Batch testing") - except Exception as e: - logger.warning(f"⚠️ AWS Batch validation error: {e}") - - @pytest.mark.real_world - def test_azure_container_instances_validation(self): - """Test Azure Container Instances for serverless compute.""" - if not self.azure_creds: - pytest.skip("Azure credentials not available for ACI testing") - - logger.info("Testing Azure Container Instances validation") - - try: - from azure.identity import DefaultAzureCredential, ClientSecretCredential - from azure.mgmt.containerinstance import ContainerInstanceManagementClient - - # Create credentials - if self.azure_creds.get("client_secret"): - credential = ClientSecretCredential( - tenant_id=self.azure_creds["tenant_id"], - client_id=self.azure_creds["client_id"], - client_secret=self.azure_creds["client_secret"], - ) - else: - credential = DefaultAzureCredential() - - # Create ACI client - aci_client = ContainerInstanceManagementClient( - credential, self.azure_creds["subscription_id"] - ) - - try: - # Test ACI access by listing container groups - container_groups = list(aci_client.container_groups.list()) - - logger.info("✅ Azure Container Instances accessible") - logger.info(f" Container groups: {len(container_groups)}") - - except Exception as e: - logger.warning(f"⚠️ Azure ACI access limited: {e}") - - except ImportError: - pytest.skip("Azure Container Instances SDK not available") - except Exception as e: - logger.warning(f"⚠️ Azure ACI validation error: {e}") - - @pytest.mark.real_world - def test_gcp_cloud_run_validation(self): - """Test GCP Cloud Run for serverless compute.""" - if not self.gcp_creds: - pytest.skip("GCP credentials not available for Cloud Run testing") - - logger.info("Testing GCP Cloud Run validation") - - try: - from google.cloud import run_v2 - - # Set up authentication - if self.gcp_creds.get("service_account_json"): - import tempfile - - with tempfile.NamedTemporaryFile( - mode="w", delete=False, suffix=".json" - ) as f: - f.write(self.gcp_creds["service_account_json"]) - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = f.name - - try: - # Create Cloud Run client - run_client = run_v2.ServicesClient() - - # Test Cloud Run access - project_id = self.gcp_creds["project_id"] - location = self.gcp_creds.get("region", "us-central1") - - parent = f"projects/{project_id}/locations/{location}" - services = run_client.list_services(parent=parent) - service_list = list(services) - - logger.info("✅ GCP Cloud Run accessible") - logger.info(f" Services in {location}: {len(service_list)}") - - except Exception as e: - logger.warning(f"⚠️ GCP Cloud Run access limited: {e}") - - except ImportError: - pytest.skip("Google Cloud Run SDK not available") - except Exception as e: - logger.warning(f"⚠️ GCP Cloud Run validation error: {e}") - - @pytest.mark.real_world - def test_multi_cloud_compute_compatibility(self): - """Test multi-cloud compute compatibility and resource comparison.""" - if not self.available_providers: - pytest.skip("No cloud providers available for multi-cloud testing") - - logger.info("Testing multi-cloud compute compatibility") - - provider_results = {} - - # Test each available provider - for provider in self.available_providers: - if provider == "aws": - result = validate_aws_ec2_access(self.aws_creds) - provider_results["aws"] = result - elif provider == "azure": - result = validate_azure_vm_access(self.azure_creds) - provider_results["azure"] = result - elif provider == "gcp": - result = validate_gcp_compute_access(self.gcp_creds) - provider_results["gcp"] = result - - # Analyze results - successful_providers = [ - p for p, r in provider_results.items() if r.get("access_successful") - ] - - logger.info( - f"✅ Multi-cloud compatibility: {len(successful_providers)}/{len(provider_results)} providers accessible" - ) - - for provider in successful_providers: - result = provider_results[provider] - if provider == "aws": - logger.info( - f" AWS: {result['region_count']} regions, {len(result['sample_instance_types'])} instance types" - ) - elif provider == "azure": - logger.info( - f" Azure: {result['vm_size_count']} VM sizes, {result['resource_group_count']} resource groups" - ) - elif provider == "gcp": - logger.info( - f" GCP: {result['zone_count']} zones in project {result['project_id']}" - ) - - # At least one provider should be accessible if any are configured - if self.available_providers: - assert ( - len(successful_providers) > 0 - ), f"Expected at least one cloud provider accessible, got: {provider_results}" - - @pytest.mark.real_world - def test_cloud_compute_pricing_integration(self): - """Test integration with cloud compute pricing APIs.""" - if not self.available_providers: - pytest.skip("No cloud providers available for pricing integration testing") - - logger.info("Testing cloud compute pricing integration") - - pricing_results = {} - - # Test pricing API integration for available providers - for provider in self.available_providers: - try: - if provider == "aws": - # Test AWS Pricing API - import boto3 - - pricing_client = boto3.client( - "pricing", - aws_access_key_id=self.aws_creds["access_key_id"], - aws_secret_access_key=self.aws_creds["secret_access_key"], - region_name="us-east-1", # Pricing API only in us-east-1 - ) - - # Test getting EC2 pricing information - response = pricing_client.get_products( - ServiceCode="AmazonEC2", - Filters=[ - { - "Type": "TERM_MATCH", - "Field": "instanceType", - "Value": "t3.micro", - }, - { - "Type": "TERM_MATCH", - "Field": "location", - "Value": "US East (N. Virginia)", - }, - ], - MaxResults=1, - ) - - pricing_results["aws"] = { - "api_accessible": True, - "sample_products": len(response["PriceList"]), - } - - elif provider == "azure": - # Test Azure Pricing API (public, no auth needed) - import requests - - response = requests.get( - "https://prices.azure.com/api/retail/prices?$filter=serviceName eq 'Virtual Machines'&$top=5", - timeout=30, - ) - - if response.status_code == 200: - data = response.json() - pricing_results["azure"] = { - "api_accessible": True, - "sample_products": len(data.get("Items", [])), - } - - elif provider == "gcp": - # Test GCP Cloud Billing Catalog API - from google.cloud import billing_v1 - - if self.gcp_creds.get("service_account_json"): - import tempfile - - with tempfile.NamedTemporaryFile( - mode="w", delete=False, suffix=".json" - ) as f: - f.write(self.gcp_creds["service_account_json"]) - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = f.name - - catalog_client = billing_v1.CloudCatalogClient() - services = catalog_client.list_services() - service_count = len(list(services)) - - pricing_results["gcp"] = { - "api_accessible": True, - "services_available": service_count, - } - - except Exception as e: - pricing_results[provider] = {"api_accessible": False, "error": str(e)} - - # Log results - for provider, result in pricing_results.items(): - if result.get("api_accessible"): - logger.info(f"✅ {provider.upper()} pricing API accessible") - else: - logger.warning( - f"⚠️ {provider.upper()} pricing API limited: {result.get('error')}" - ) - - accessible_pricing = [ - p for p, r in pricing_results.items() if r.get("api_accessible") - ] - logger.info( - f"✅ Cloud pricing integration: {len(accessible_pricing)}/{len(pricing_results)} APIs accessible" - ) - - -if __name__ == "__main__": - # Run tests directly for debugging - pytest.main([__file__, "-v", "--tb=short", "-m", "real_world"]) diff --git a/tests/real_world/test_kubernetes_comprehensive.py b/tests/real_world/test_kubernetes_comprehensive.py deleted file mode 100644 index 0d0988e3..00000000 --- a/tests/real_world/test_kubernetes_comprehensive.py +++ /dev/null @@ -1,506 +0,0 @@ -""" -Comprehensive real-world Kubernetes validation tests. - -This module tests the complete Kubernetes implementation with actual K8s clusters, -addressing Phase 2 of Issue #63 external service validation. - -Tests cover: -- Kubernetes job submission and execution -- Container-based Python function execution -- Job monitoring and status tracking -- Error handling and recovery -- Resource specification and limits -- Pod log parsing and result retrieval -- Job cleanup and TTL management - -NO MOCK TESTS - Only real Kubernetes cluster integration. - -Supports multiple K8s environments: -- Local: minikube, kind, Docker Desktop -- Cloud: EKS (AWS), GKE (Google), AKS (Azure) -- Hybrid: On-premises clusters -""" - -import pytest -import logging -import time -import os -import tempfile -from typing import Dict, Any, Optional - -# Import credential manager and test utilities -from .credential_manager import get_credential_manager -from clustrix import ClusterExecutor -from clustrix.config import ClusterConfig - -# Configure logging for detailed test debugging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def get_kubernetes_test_credentials() -> Optional[Dict[str, Any]]: - """Get real Kubernetes cluster credentials from 1Password or environment.""" - manager = get_credential_manager() - - # Try to get Kubernetes credentials from credential manager - # This would need to be added to the credential manager - k8s_creds = ( - manager.get_kubernetes_credentials() - if hasattr(manager, "get_kubernetes_credentials") - else None - ) - if k8s_creds: - return k8s_creds - - # Fallback to environment variables for CI/automated testing - # Check for kubeconfig file or in-cluster config - kubeconfig_path = os.environ.get("KUBECONFIG", os.path.expanduser("~/.kube/config")) - if os.path.exists(kubeconfig_path): - return { - "kubeconfig_path": kubeconfig_path, - "namespace": os.environ.get("K8S_NAMESPACE", "default"), - "image": os.environ.get("K8S_IMAGE", "python:3.11-slim"), - "context": os.environ.get("K8S_CONTEXT"), # Optional specific context - } - - # Check if we're running inside a Kubernetes cluster - if os.path.exists("/var/run/secrets/kubernetes.io/serviceaccount/token"): - return { - "in_cluster": True, - "namespace": os.environ.get("K8S_NAMESPACE", "default"), - "image": os.environ.get("K8S_IMAGE", "python:3.11-slim"), - } - - return None - - -def create_test_kubernetes_config() -> Optional[ClusterConfig]: - """Create a test Kubernetes configuration with real credentials.""" - creds = get_kubernetes_test_credentials() - if not creds: - return None - - config_params = { - "cluster_type": "kubernetes", - "k8s_namespace": creds.get("namespace", "default"), - "k8s_image": creds.get("image", "python:3.11-slim"), - "k8s_job_ttl_seconds": 600, # 10 minutes for tests - "k8s_backoff_limit": 2, # Allow 2 retries - "cleanup_on_success": True, # Clean up test jobs - "job_poll_interval": 3, # Faster polling for tests - } - - # Add any additional config from credentials - if "service_account" in creds: - config_params["k8s_service_account"] = creds["service_account"] - - return ClusterConfig(**config_params) - - -@pytest.mark.real_world -class TestKubernetesComprehensive: - """Comprehensive Kubernetes integration tests addressing Issue #63 Phase 2.""" - - def setup_method(self): - """Setup test environment.""" - self.config = create_test_kubernetes_config() - if self.config: - self.executor = ClusterExecutor(self.config) - else: - self.executor = None - - def teardown_method(self): - """Cleanup test environment.""" - if self.executor: - try: - # Clean up any remaining active jobs - for job_id in list(self.executor.active_jobs.keys()): - try: - self.executor._cleanup_k8s_job(job_id) - logger.info(f"Cleaned up test job {job_id}") - except Exception as e: - logger.debug(f"Could not cleanup job {job_id}: {e}") - - self.executor.disconnect() - except Exception as e: - logger.debug(f"Error during teardown: {e}") - - @pytest.mark.real_world - def test_kubernetes_simple_function_execution(self): - """Test basic Kubernetes function execution with containerized execution.""" - if not self.config: - pytest.skip( - "Kubernetes cluster not available (no kubeconfig or in-cluster config)" - ) - - logger.info("Testing Kubernetes simple function execution") - - def simple_calculation(x: int, y: int) -> int: - """Simple test function for Kubernetes execution.""" - import time - - time.sleep(1) # Brief pause to simulate work - return (x * y) + 42 - - # Submit job using Kubernetes executor - job_id = self.executor.submit(simple_calculation, 7, 6) - - assert job_id is not None, "Job submission should return a job ID" - assert ( - job_id in self.executor.active_jobs - ), "Job should be tracked in active_jobs" - assert ( - self.executor.active_jobs[job_id].get("k8s_job") is True - ), "Should be marked as K8s job" - - logger.info(f"Submitted Kubernetes job {job_id}, waiting for completion...") - - # Test the Kubernetes result retrieval - start_time = time.time() - timeout = 300 # 5 minute timeout - - try: - result = self.executor.wait_for_result(job_id) - execution_time = time.time() - start_time - - expected_result = (7 * 6) + 42 # 84 - assert ( - result == expected_result - ), f"Expected {expected_result}, got {result}" - logger.info( - f"✅ Kubernetes job {job_id} completed successfully in {execution_time:.1f}s: {result}" - ) - - # Verify job was cleaned up - assert ( - job_id not in self.executor.active_jobs - ), "Job should be removed from active_jobs after completion" - - except Exception as e: - execution_time = time.time() - start_time - logger.error( - f"❌ Kubernetes job {job_id} failed after {execution_time:.1f}s: {e}" - ) - - # Get detailed error information for debugging - try: - error_log = self.executor._get_k8s_error_log(job_id) - logger.error(f"Kubernetes error log: {error_log}") - - # Check job status via Kubernetes API - status = self.executor._check_job_status(job_id) - logger.error(f"Job status: {status}") - - except Exception as debug_e: - logger.error(f"Could not get debug info: {debug_e}") - - raise - - @pytest.mark.real_world - def test_kubernetes_error_handling_and_recovery(self): - """Test Kubernetes error handling with failing function.""" - if not self.config: - pytest.skip("Kubernetes cluster not available") - - logger.info("Testing Kubernetes error handling and recovery") - - def failing_function(error_message: str) -> str: - """Function that will fail for testing error handling.""" - import time - - time.sleep(1) # Simulate some work before failing - raise RuntimeError(f"Intentional test error: {error_message}") - - # Submit failing job - job_id = self.executor.submit(failing_function, "test-k8s-error") - - logger.info(f"Submitted failing Kubernetes job {job_id}, expecting failure...") - - # Test that error is properly handled - with pytest.raises(Exception) as exc_info: - result = self.executor.wait_for_result(job_id) - - # Verify the error contains our test message - error_str = str(exc_info.value) - assert ( - "Intentional test error: test-k8s-error" in error_str - ), f"Expected test error message in: {error_str}" - - logger.info(f"✅ Kubernetes error handling working correctly: {error_str}") - - # Verify job was cleaned up even after failure - assert ( - job_id not in self.executor.active_jobs - ), "Failed job should be removed from active_jobs" - - @pytest.mark.real_world - def test_kubernetes_resource_specification(self): - """Test Kubernetes job submission with specific resource requirements.""" - if not self.config: - pytest.skip("Kubernetes cluster not available") - - logger.info("Testing Kubernetes resource specification") - - def resource_intensive_test() -> Dict[str, Any]: - """Function that reports on allocated resources.""" - import os - import psutil - - # Get container resource information - result = { - "hostname": os.uname().nodename, - "cpu_count": psutil.cpu_count(), - "memory_mb": round(psutil.virtual_memory().total / (1024 * 1024)), - "pid": os.getpid(), - "python_version": f"{psutil.sys.version_info.major}.{psutil.sys.version_info.minor}", - } - - # Check for Kubernetes environment variables - if "KUBERNETES_SERVICE_HOST" in os.environ: - result["in_kubernetes"] = True - result["namespace"] = os.environ.get("KUBERNETES_NAMESPACE", "unknown") - - return result - - # Submit job with specific resource requirements - job_config = { - "cores": 1, # Request 1 CPU - "memory": "512Mi", # Request 512MB memory - } - - job_id = self.executor.submit(resource_intensive_test, job_config=job_config) - logger.info( - f"Submitted Kubernetes resource test job {job_id} with config: {job_config}" - ) - - result = self.executor.wait_for_result(job_id) - - # Verify result structure - assert isinstance(result, dict), f"Expected dict result, got {type(result)}" - assert "hostname" in result, "Result should include hostname" - assert "cpu_count" in result, "Result should include CPU count" - assert "memory_mb" in result, "Result should include memory info" - assert "in_kubernetes" in result, "Should detect Kubernetes environment" - - logger.info(f"✅ Kubernetes resource test completed on {result['hostname']}") - logger.info( - f" CPU count: {result['cpu_count']}, Memory: {result['memory_mb']} MB" - ) - logger.info( - f" Python version: {result['python_version']}, In K8s: {result.get('in_kubernetes', False)}" - ) - - @pytest.mark.real_world - def test_kubernetes_concurrent_jobs(self): - """Test multiple concurrent Kubernetes jobs.""" - if not self.config: - pytest.skip("Kubernetes cluster not available") - - logger.info("Testing concurrent Kubernetes jobs") - - def concurrent_task(task_id: int, delay: float) -> str: - """Function for concurrent job testing.""" - import time - import os - - time.sleep(delay) - hostname = os.uname().nodename - return f"Task {task_id} completed on {hostname} after {delay}s" - - # Submit multiple jobs concurrently - num_jobs = 3 - job_ids = [] - - for i in range(num_jobs): - delay = 1 + (i * 0.5) # Staggered delays: 1s, 1.5s, 2s - job_id = self.executor.submit(concurrent_task, i, delay) - job_ids.append(job_id) - logger.info(f"Submitted concurrent Kubernetes job {i}: {job_id}") - - # Wait for all jobs to complete - results = [] - for i, job_id in enumerate(job_ids): - logger.info(f"Waiting for concurrent Kubernetes job {i} ({job_id})...") - result = self.executor.wait_for_result(job_id) - results.append(result) - logger.info(f"Concurrent job {i} result: {result}") - - # Verify all jobs completed successfully - assert ( - len(results) == num_jobs - ), f"Expected {num_jobs} results, got {len(results)}" - - for i, result in enumerate(results): - assert ( - f"Task {i} completed" in result - ), f"Job {i} result malformed: {result}" - assert "after" in result, f"Job {i} should include timing info: {result}" - - logger.info( - f"✅ All {num_jobs} concurrent Kubernetes jobs completed successfully" - ) - - @pytest.mark.real_world - def test_kubernetes_dependency_handling(self): - """Test Kubernetes job execution with Python package dependencies.""" - if not self.config: - pytest.skip("Kubernetes cluster not available") - - logger.info("Testing Kubernetes dependency handling") - - def dependency_test() -> Dict[str, Any]: - """Function that uses common packages available in python:3.11-slim.""" - import json - import datetime - import urllib.request - import base64 - - # Test basic operations with standard library - test_data = { - "timestamp": datetime.datetime.now().isoformat(), - "base64_test": base64.b64encode(b"hello kubernetes").decode("utf-8"), - "json_test": json.dumps({"status": "success", "value": 42}), - } - - return { - "test_data": test_data, - "packages_available": ["json", "datetime", "urllib", "base64"], - "success": True, - } - - # Submit dependency test job - job_id = self.executor.submit(dependency_test) - logger.info(f"Submitted Kubernetes dependency test job {job_id}") - - result = self.executor.wait_for_result(job_id) - - # Verify dependency handling worked - assert isinstance(result, dict), f"Expected dict result, got {type(result)}" - assert result.get("success") is True, "Dependency test should succeed" - assert "test_data" in result, "Should include test data" - assert "packages_available" in result, "Should list available packages" - - # Verify specific functionality - test_data = result["test_data"] - assert "timestamp" in test_data, "Should include timestamp" - assert ( - test_data["base64_test"] == "aGVsbG8ga3ViZXJuZXRlcw==" - ), "Base64 encoding should work" - - logger.info(f"✅ Kubernetes dependency handling working correctly") - logger.info(f" Available packages: {result['packages_available']}") - - @pytest.mark.real_world - def test_kubernetes_job_status_tracking(self): - """Test Kubernetes job status tracking and monitoring.""" - if not self.config: - pytest.skip("Kubernetes cluster not available") - - logger.info("Testing Kubernetes job status tracking") - - def status_tracking_test() -> str: - """Function for testing job status transitions.""" - import time - - # Sleep to allow status tracking during execution - for i in range(5): - time.sleep(1) - print(f"Status test progress: {i+1}/5") # This will appear in pod logs - - return "Status tracking test completed" - - # Submit status tracking job - job_id = self.executor.submit(status_tracking_test) - logger.info(f"Submitted Kubernetes status tracking job {job_id}") - - # Monitor status changes - status_history = [] - start_time = time.time() - - # Poll status for the first few seconds - while time.time() - start_time < 10: # Monitor for 10 seconds - try: - status = self.executor._check_job_status(job_id) - if status not in status_history: - status_history.append(status) - logger.info(f"Job {job_id} status changed to: {status}") - - if status in ["completed", "failed"]: - break - - time.sleep(2) # Check every 2 seconds - except Exception as e: - logger.debug(f"Status check failed: {e}") - break - - # Wait for final result - result = self.executor.wait_for_result(job_id) - - # Verify status tracking worked - assert ( - result == "Status tracking test completed" - ), f"Unexpected result: {result}" - assert len(status_history) > 0, "Should have captured at least one status" - - # Should have seen some progression (pending -> running -> completed) - logger.info( - f"✅ Kubernetes status tracking working - observed states: {status_history}" - ) - - # Verify final status is completed - final_status = self.executor._check_job_status(job_id) - logger.info(f"Final job status: {final_status}") - - @pytest.mark.real_world - def test_kubernetes_job_cleanup_and_ttl(self): - """Test Kubernetes job cleanup and TTL management.""" - if not self.config: - pytest.skip("Kubernetes cluster not available") - - logger.info("Testing Kubernetes job cleanup and TTL") - - def cleanup_test() -> str: - """Simple function for testing cleanup.""" - import time - - time.sleep(2) - return "Cleanup test job completed" - - # Submit job with short TTL for testing - original_ttl = self.config.k8s_job_ttl_seconds - self.config.k8s_job_ttl_seconds = 60 # 1 minute TTL for test - - try: - job_id = self.executor.submit(cleanup_test) - logger.info(f"Submitted Kubernetes cleanup test job {job_id} with 60s TTL") - - # Wait for completion - result = self.executor.wait_for_result(job_id) - assert ( - result == "Cleanup test job completed" - ), f"Unexpected result: {result}" - - # Test manual cleanup - try: - self.executor._cleanup_k8s_job(job_id) - logger.info(f"✅ Manual cleanup of job {job_id} successful") - except Exception as e: - logger.warning(f"Manual cleanup failed (may already be cleaned): {e}") - - # Verify job is no longer tracked - assert ( - job_id not in self.executor.active_jobs - ), "Job should be removed from tracking" - - logger.info( - "✅ Kubernetes job cleanup and TTL management working correctly" - ) - - finally: - # Restore original TTL - self.config.k8s_job_ttl_seconds = original_ttl - - -if __name__ == "__main__": - # Run tests directly for debugging - pytest.main([__file__, "-v", "--tb=short", "-m", "real_world"]) diff --git a/tests/real_world/test_kubernetes_end_to_end_execution.py b/tests/real_world/test_kubernetes_end_to_end_execution.py deleted file mode 100644 index bf62fbee..00000000 --- a/tests/real_world/test_kubernetes_end_to_end_execution.py +++ /dev/null @@ -1,445 +0,0 @@ -""" -End-to-end integration tests for Kubernetes cluster provisioning with real job execution. - -These tests verify the complete workflow from cluster auto-provisioning -through job execution to cleanup using the @cluster decorator. - -Requirements: -- Valid credentials for at least one cloud provider -- Network connectivity to provider APIs -- Sufficient quota for cluster/instance creation -- Real function execution capabilities - -NOTE: These are expensive tests that create actual cloud infrastructure. -They should be run manually or in special CI environments only. -""" - -import os -import time -import pytest -import logging -import tempfile -from typing import Dict, Any, List - -from clustrix import cluster -from clustrix.config import ClusterConfig, get_config -from clustrix.credential_manager import get_credential_manager - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -@pytest.mark.slow -class TestKubernetesEndToEndExecution: - """End-to-end tests for Kubernetes auto-provisioning with real job execution.""" - - @pytest.fixture(scope="class") - def available_providers(self): - """Get list of providers with valid credentials for testing.""" - credential_manager = get_credential_manager() - providers = ["huggingface", "lambda"] # Start with faster providers - - available = [] - for provider in providers: - try: - creds = credential_manager.ensure_kubernetes_provider_credentials( - provider - ) - if creds: - available.append(provider) - logger.info(f"✅ {provider} credentials available for testing") - else: - logger.info(f"⏭️ {provider} credentials not available") - except Exception as e: - logger.info(f"⏭️ {provider} credentials error: {e}") - - if not available: - pytest.skip( - "No cloud provider credentials available for end-to-end testing" - ) - - return available - - @pytest.fixture(scope="function") - def test_cluster_config(self): - """Create test cluster configuration.""" - test_id = int(time.time()) - - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_from_scratch = True - config.k8s_node_count = 1 - config.k8s_cleanup_on_exit = True # Always cleanup - config.k8s_cluster_name = f"test-e2e-{test_id}" - - return config - - def test_simple_function_execution(self, available_providers, test_cluster_config): - """Test execution of a simple Python function on auto-provisioned cluster.""" - provider = available_providers[0] # Use first available provider - logger.info(f"🧪 Testing simple function execution on {provider}") - - # Configure for this provider - test_cluster_config.k8s_provider = provider - test_cluster_config.k8s_region = ( - "us-west-2" if provider == "lambda" else "global" - ) - - # Override global config for this test - original_config = get_config()._config - get_config()._config = test_cluster_config - - try: - # Define test function with @cluster decorator - @cluster( - platform="kubernetes", - auto_provision=True, - node_count=1, - cluster_name=test_cluster_config.k8s_cluster_name, - ) - def simple_computation(x: int, y: int) -> Dict[str, Any]: - """Simple computation function for testing.""" - import platform - import os - - result = x * y + 42 - - return { - "result": result, - "platform": platform.platform(), - "python_version": platform.python_version(), - "environment_variables": dict(os.environ), - "computed_at": time.time(), - } - - # Execute function - this should trigger cluster provisioning - logger.info("🚀 Starting function execution (will auto-provision cluster)") - start_time = time.time() - - result = simple_computation(5, 10) - execution_time = time.time() - start_time - - # Verify results - assert isinstance(result, dict), "Result should be a dictionary" - assert result["result"] == 92, f"Expected 92, got {result['result']}" - assert "platform" in result, "Platform info should be included" - assert "python_version" in result, "Python version should be included" - - logger.info(f"✅ Function executed successfully in {execution_time:.1f}s") - logger.info(f"📊 Result: {result['result']}") - logger.info(f"🖥️ Remote platform: {result['platform']}") - - finally: - # Restore original config - get_config()._config = original_config - - def test_loop_parallelization_execution( - self, available_providers, test_cluster_config - ): - """Test loop parallelization on auto-provisioned cluster.""" - provider = available_providers[0] - logger.info(f"🧪 Testing loop parallelization on {provider}") - - # Configure for this provider - test_cluster_config.k8s_provider = provider - test_cluster_config.k8s_region = ( - "us-west-2" if provider == "lambda" else "global" - ) - test_cluster_config.k8s_cluster_name = f"test-loop-{int(time.time())}" - - # Override global config - original_config = get_config()._config - get_config()._config = test_cluster_config - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - parallel=True, # Enable loop parallelization - node_count=1, - cluster_name=test_cluster_config.k8s_cluster_name, - ) - def parallel_computation(numbers: List[int]) -> List[Dict[str, Any]]: - """Function with parallelizable loop for testing.""" - results = [] - - for num in numbers: # This loop should be parallelized - import time - import os - - # Simulate some work - time.sleep(0.1) - processed = num**2 + num - - results.append( - { - "input": num, - "result": processed, - "worker_pid": os.getpid(), - "processed_at": time.time(), - } - ) - - return results - - # Execute with list that should be parallelized - test_numbers = [1, 2, 3, 4, 5] - logger.info("🚀 Starting parallel computation") - start_time = time.time() - - results = parallel_computation(test_numbers) - execution_time = time.time() - start_time - - # Verify results - assert isinstance(results, list), "Results should be a list" - assert len(results) == len(test_numbers), "Should process all numbers" - - for i, result in enumerate(results): - expected = test_numbers[i] ** 2 + test_numbers[i] - assert ( - result["result"] == expected - ), f"Incorrect result for {test_numbers[i]}" - assert result["input"] == test_numbers[i], "Input should be preserved" - - logger.info(f"✅ Parallel computation completed in {execution_time:.1f}s") - logger.info(f"📊 Processed {len(results)} items") - - finally: - # Restore original config - get_config()._config = original_config - - def test_data_processing_workflow(self, available_providers, test_cluster_config): - """Test realistic data processing workflow on auto-provisioned cluster.""" - provider = available_providers[0] - logger.info(f"🧪 Testing data processing workflow on {provider}") - - # Configure for this provider - test_cluster_config.k8s_provider = provider - test_cluster_config.k8s_region = ( - "us-west-2" if provider == "lambda" else "global" - ) - test_cluster_config.k8s_cluster_name = f"test-workflow-{int(time.time())}" - - # Override global config - original_config = get_config()._config - get_config()._config = test_cluster_config - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - cores=1, - memory="2GB", - cluster_name=test_cluster_config.k8s_cluster_name, - ) - def data_processing_workflow(dataset_size: int) -> Dict[str, Any]: - """Realistic data processing function for testing.""" - import numpy as np - import time - import json - - start_time = time.time() - - # Generate synthetic dataset - logger.info(f"Generating dataset of size {dataset_size}") - data = np.random.rand(dataset_size, 10) # Random data matrix - - # Process data (simulate real ML workflow) - logger.info("Processing data...") - - # Step 1: Normalization - normalized = (data - np.mean(data, axis=0)) / np.std(data, axis=0) - - # Step 2: Feature extraction - features = { - "mean": np.mean(normalized, axis=0).tolist(), - "std": np.std(normalized, axis=0).tolist(), - "min": np.min(normalized, axis=0).tolist(), - "max": np.max(normalized, axis=0).tolist(), - } - - # Step 3: Simple analysis - correlation_matrix = np.corrcoef(normalized.T).tolist() - - processing_time = time.time() - start_time - - return { - "dataset_size": dataset_size, - "features": features, - "correlation_matrix": correlation_matrix, - "processing_time": processing_time, - "numpy_version": np.__version__, - "status": "completed", - } - - # Execute workflow - logger.info("🚀 Starting data processing workflow") - start_time = time.time() - - result = data_processing_workflow(1000) # Process 1000 samples - execution_time = time.time() - start_time - - # Verify results - assert isinstance(result, dict), "Result should be a dictionary" - assert ( - result["status"] == "completed" - ), "Workflow should complete successfully" - assert result["dataset_size"] == 1000, "Dataset size should be preserved" - assert "features" in result, "Features should be extracted" - assert ( - "correlation_matrix" in result - ), "Correlation matrix should be computed" - assert len(result["features"]["mean"]) == 10, "Should have 10 feature means" - - logger.info( - f"✅ Data processing workflow completed in {execution_time:.1f}s" - ) - logger.info(f"⚡ Remote processing time: {result['processing_time']:.1f}s") - logger.info(f"📊 Processed {result['dataset_size']} samples") - - finally: - # Restore original config - get_config()._config = original_config - - def test_error_handling_and_cleanup(self, available_providers, test_cluster_config): - """Test error handling and proper cluster cleanup.""" - provider = available_providers[0] - logger.info(f"🧪 Testing error handling and cleanup on {provider}") - - # Configure for this provider - test_cluster_config.k8s_provider = provider - test_cluster_config.k8s_region = ( - "us-west-2" if provider == "lambda" else "global" - ) - test_cluster_config.k8s_cluster_name = f"test-error-{int(time.time())}" - - # Override global config - original_config = get_config()._config - get_config()._config = test_cluster_config - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - cluster_name=test_cluster_config.k8s_cluster_name, - ) - def failing_function(should_fail: bool) -> str: - """Function that can be made to fail for testing error handling.""" - if should_fail: - raise ValueError("Intentional test failure") - return "success" - - # First test successful execution - logger.info("🚀 Testing successful execution first") - result = failing_function(False) - assert result == "success", "Successful execution should return 'success'" - - # Then test error handling - logger.info("🚀 Testing error handling") - with pytest.raises(Exception) as exc_info: - failing_function(True) - - # Verify the error was properly propagated - assert "Intentional test failure" in str( - exc_info.value - ) or "ValueError" in str(type(exc_info.value)) - - logger.info("✅ Error handling working correctly") - - finally: - # Restore original config - get_config()._config = original_config - - @pytest.mark.slow - def test_multi_provider_execution(self, available_providers, test_cluster_config): - """Test execution across multiple providers if available.""" - if len(available_providers) < 2: - pytest.skip("Need at least 2 providers for multi-provider testing") - - logger.info(f"🧪 Testing multi-provider execution: {available_providers}") - - results = {} - - for provider in available_providers[:2]: # Test first 2 providers - logger.info(f"🔄 Testing provider: {provider}") - - # Configure for this provider - provider_config = ClusterConfig() - provider_config.cluster_type = "kubernetes" - provider_config.auto_provision_k8s = True - provider_config.k8s_from_scratch = True - provider_config.k8s_node_count = 1 - provider_config.k8s_cleanup_on_exit = True - provider_config.k8s_provider = provider - provider_config.k8s_region = ( - "us-west-2" if provider == "lambda" else "global" - ) - provider_config.k8s_cluster_name = ( - f"test-multi-{provider}-{int(time.time())}" - ) - - # Override global config - original_config = get_config()._config - get_config()._config = provider_config - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - cluster_name=provider_config.k8s_cluster_name, - ) - def provider_test_function(provider_name: str) -> Dict[str, Any]: - """Test function for multi-provider execution.""" - import platform - import time - - return { - "provider": provider_name, - "platform": platform.platform(), - "execution_time": time.time(), - "test": "multi_provider_success", - } - - # Execute on this provider - start_time = time.time() - result = provider_test_function(provider) - execution_time = time.time() - start_time - - results[provider] = { - "result": result, - "execution_time": execution_time, - "success": True, - } - - logger.info( - f"✅ {provider} execution completed in {execution_time:.1f}s" - ) - - except Exception as e: - logger.error(f"❌ {provider} execution failed: {e}") - results[provider] = { - "result": None, - "execution_time": None, - "success": False, - "error": str(e), - } - finally: - # Restore original config - get_config()._config = original_config - - # Verify results - successful_providers = [p for p, r in results.items() if r["success"]] - assert len(successful_providers) >= 1, "At least one provider should succeed" - - logger.info(f"✅ Multi-provider testing completed") - logger.info(f"📊 Successful providers: {successful_providers}") - for provider, result in results.items(): - if result["success"]: - logger.info(f" {provider}: {result['execution_time']:.1f}s") - else: - logger.info(f" {provider}: Failed - {result.get('error', 'Unknown')}") diff --git a/tests/real_world/test_kubernetes_job_submission_real.py b/tests/real_world/test_kubernetes_job_submission_real.py deleted file mode 100644 index d1fd05d5..00000000 --- a/tests/real_world/test_kubernetes_job_submission_real.py +++ /dev/null @@ -1,565 +0,0 @@ -""" -Real Kubernetes job submission tests using @cluster decorator. - -These tests actually submit jobs to real Kubernetes clusters and validate -the complete end-to-end workflow with the @cluster decorator. -""" - -import pytest -import os -import time -import uuid -from pathlib import Path -from typing import List, Dict, Any - -from clustrix import cluster, configure -from clustrix.config import ClusterConfig -from tests.real_world import TempResourceManager, credentials, test_manager - - -class TestRealKubernetesJobSubmission: - """Test real Kubernetes job submission using @cluster decorator.""" - - @pytest.fixture - def kubernetes_config(self): - """Get Kubernetes configuration for testing.""" - # For Kubernetes, we'll use environment variables or local kubectl config - # Check if kubectl is available and configured - import subprocess - - try: - result = subprocess.run( - ["kubectl", "cluster-info"], capture_output=True, text=True, timeout=10 - ) - if result.returncode != 0: - pytest.skip("kubectl not configured or cluster not accessible") - except (subprocess.TimeoutExpired, FileNotFoundError): - pytest.skip("kubectl not available or cluster not accessible") - - # Configure clustrix for Kubernetes - configure( - cluster_type="kubernetes", - cluster_host="kubernetes", # Use local kubectl config - namespace="default", - remote_work_dir=f"/tmp/clustrix_k8s_test_{uuid.uuid4().hex[:8]}", - cleanup_remote_files=True, - ) - - return {"cluster_type": "kubernetes", "namespace": "default"} - - @pytest.mark.real_world - def test_simple_function_k8s_submission(self, kubernetes_config): - """Test submitting a simple function to Kubernetes.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def calculate_factorial(n: int) -> int: - """Calculate factorial for testing.""" - if n <= 1: - return 1 - result = 1 - for i in range(2, n + 1): - result *= i - return result - - # Submit job and wait for result - result = calculate_factorial(6) - - # Validate result - assert result == 720 # 6! = 720 - assert isinstance(result, int) - - @pytest.mark.real_world - def test_function_with_k8s_environment(self, kubernetes_config): - """Test Kubernetes job that accesses K8s environment variables.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def get_k8s_environment() -> Dict[str, str]: - """Get Kubernetes job environment variables.""" - import os - - return { - "KUBERNETES_SERVICE_HOST": os.getenv( - "KUBERNETES_SERVICE_HOST", "not_set" - ), - "KUBERNETES_SERVICE_PORT": os.getenv( - "KUBERNETES_SERVICE_PORT", "not_set" - ), - "KUBERNETES_PORT": os.getenv("KUBERNETES_PORT", "not_set"), - "HOSTNAME": os.getenv("HOSTNAME", "not_set"), - "POD_NAME": os.getenv("POD_NAME", "not_set"), - "POD_NAMESPACE": os.getenv("POD_NAMESPACE", "not_set"), - "POD_IP": os.getenv("POD_IP", "not_set"), - "NODE_NAME": os.getenv("NODE_NAME", "not_set"), - "USER": os.getenv("USER", "not_set"), - "HOME": os.getenv("HOME", "not_set"), - "PWD": os.getenv("PWD", "not_set"), - } - - result = get_k8s_environment() - - # Validate K8s environment - assert isinstance(result, dict) - assert "KUBERNETES_SERVICE_HOST" in result - # Note: Some K8s environment variables may not be set in all configurations - assert result["HOSTNAME"] != "not_set" - assert result["PWD"] != "not_set" - - @pytest.mark.real_world - def test_k8s_resource_specification(self, kubernetes_config): - """Test Kubernetes job with specific resource requirements.""" - - @cluster(cores=2, memory="2Gi", time="00:10:00") - def test_resource_allocation() -> Dict[str, Any]: - """Test resource allocation in Kubernetes job.""" - import os - import psutil - - # Get CPU and memory information - cpu_count = os.cpu_count() - memory_info = psutil.virtual_memory() - - # Get process information - process = psutil.Process(os.getpid()) - process_memory = process.memory_info() - - return { - "cpu_count": cpu_count, - "total_memory_gb": memory_info.total / (1024**3), - "available_memory_gb": memory_info.available / (1024**3), - "process_memory_mb": process_memory.rss / (1024**2), - "hostname": os.getenv("HOSTNAME", "unknown"), - "pod_name": os.getenv("POD_NAME", "unknown"), - "node_name": os.getenv("NODE_NAME", "unknown"), - } - - result = test_resource_allocation() - - # Validate resource allocation - assert isinstance(result, dict) - assert result["cpu_count"] > 0 - assert result["total_memory_gb"] > 0 - assert result["process_memory_mb"] > 0 - - @pytest.mark.real_world - def test_k8s_namespace_isolation(self, kubernetes_config): - """Test Kubernetes namespace isolation.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def test_namespace_info() -> Dict[str, Any]: - """Test namespace isolation in Kubernetes.""" - import os - import subprocess - - result = { - "pod_namespace": os.getenv("POD_NAMESPACE", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "pod_name": os.getenv("POD_NAME", "unknown"), - "service_account": "default", - } - - # Try to get service account information - try: - with open( - "/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r" - ) as f: - result["service_account_namespace"] = f.read().strip() - except: - result["service_account_namespace"] = "not_available" - - return result - - result = test_namespace_info() - - # Validate namespace isolation - assert isinstance(result, dict) - assert result["hostname"] != "unknown" - - @pytest.mark.real_world - def test_k8s_persistent_storage(self, kubernetes_config): - """Test Kubernetes job with persistent storage.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def test_storage_access() -> Dict[str, Any]: - """Test storage access in Kubernetes job.""" - import os - import tempfile - import json - - # Create temporary file to test filesystem - with tempfile.NamedTemporaryFile( - mode="w", delete=False, suffix=".json" - ) as f: - test_data = { - "timestamp": time.time(), - "hostname": os.getenv("HOSTNAME", "unknown"), - "pod_name": os.getenv("POD_NAME", "unknown"), - "test_data": [i**2 for i in range(10)], - } - json.dump(test_data, f) - temp_file = f.name - - try: - # Read file back - with open(temp_file, "r") as f: - loaded_data = json.load(f) - - # Test file operations - file_stats = os.stat(temp_file) - - return { - "temp_file_path": temp_file, - "file_size": file_stats.st_size, - "file_exists": os.path.exists(temp_file), - "data_matches": loaded_data == test_data, - "test_data_length": len(loaded_data["test_data"]), - "hostname": loaded_data["hostname"], - "pod_name": loaded_data["pod_name"], - } - - finally: - # Cleanup - try: - os.unlink(temp_file) - except: - pass - - result = test_storage_access() - - # Validate storage access - assert isinstance(result, dict) - assert result["file_exists"] is True - assert result["data_matches"] is True - assert result["test_data_length"] == 10 - assert result["file_size"] > 0 - - @pytest.mark.real_world - def test_k8s_networking(self, kubernetes_config): - """Test Kubernetes job networking capabilities.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def test_network_connectivity() -> Dict[str, Any]: - """Test network connectivity from Kubernetes job.""" - import os - import socket - import subprocess - - result = { - "pod_ip": os.getenv("POD_IP", "unknown"), - "hostname": socket.gethostname(), - "fqdn": socket.getfqdn(), - "k8s_service_host": os.getenv("KUBERNETES_SERVICE_HOST", "unknown"), - "k8s_service_port": os.getenv("KUBERNETES_SERVICE_PORT", "unknown"), - } - - # Test DNS resolution - try: - kubernetes_ip = socket.gethostbyname( - "kubernetes.default.svc.cluster.local" - ) - result["kubernetes_dns_resolves"] = True - result["kubernetes_service_ip"] = kubernetes_ip - except: - result["kubernetes_dns_resolves"] = False - result["kubernetes_service_ip"] = "unknown" - - # Test local networking - try: - local_ip = socket.gethostbyname(socket.gethostname()) - result["local_ip"] = local_ip - except: - result["local_ip"] = "unknown" - - return result - - result = test_network_connectivity() - - # Validate networking - assert isinstance(result, dict) - assert result["hostname"] != "" - assert result["k8s_service_host"] != "unknown" - - @pytest.mark.real_world - def test_k8s_secrets_and_configmaps(self, kubernetes_config): - """Test Kubernetes job with secrets and configmaps.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def test_secrets_access() -> Dict[str, Any]: - """Test access to secrets and configmaps.""" - import os - - result = { - "service_account_token_exists": False, - "service_account_ca_exists": False, - "service_account_namespace_exists": False, - "environment_variables": {}, - } - - # Check for service account token - token_path = "/var/run/secrets/kubernetes.io/serviceaccount/token" - if os.path.exists(token_path): - result["service_account_token_exists"] = True - try: - with open(token_path, "r") as f: - # Just check if we can read it (don't log the actual token) - token = f.read() - result["token_length"] = len(token) - except: - result["token_read_error"] = True - - # Check for CA certificate - ca_path = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" - if os.path.exists(ca_path): - result["service_account_ca_exists"] = True - - # Check for namespace - namespace_path = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" - if os.path.exists(namespace_path): - result["service_account_namespace_exists"] = True - try: - with open(namespace_path, "r") as f: - result["namespace"] = f.read().strip() - except: - result["namespace_read_error"] = True - - # Get relevant environment variables - env_vars = ["POD_NAME", "POD_NAMESPACE", "POD_IP", "NODE_NAME"] - for var in env_vars: - result["environment_variables"][var] = os.getenv(var, "not_set") - - return result - - result = test_secrets_access() - - # Validate secrets access - assert isinstance(result, dict) - # Service account token should exist in most K8s setups - assert result["service_account_token_exists"] is True - assert result["service_account_ca_exists"] is True - - @pytest.mark.real_world - def test_k8s_parallel_processing(self, kubernetes_config): - """Test parallel processing in Kubernetes job.""" - - @cluster(cores=2, memory="2Gi", time="00:10:00", parallel=True) - def parallel_matrix_multiply( - matrices: List[List[List[float]]], - ) -> Dict[str, Any]: - """Perform parallel matrix multiplication.""" - import time - import os - - def multiply_matrices(a, b): - """Multiply two matrices.""" - rows_a, cols_a = len(a), len(a[0]) - rows_b, cols_b = len(b), len(b[0]) - - if cols_a != rows_b: - raise ValueError( - "Matrix dimensions incompatible for multiplication" - ) - - result = [[0 for _ in range(cols_b)] for _ in range(rows_a)] - - for i in range(rows_a): - for j in range(cols_b): - for k in range(cols_a): - result[i][j] += a[i][k] * b[k][j] - - return result - - start_time = time.time() - - # Process matrix pairs - results = [] - for i in range(0, len(matrices), 2): - if i + 1 < len(matrices): - matrix_a = matrices[i] - matrix_b = matrices[i + 1] - result_matrix = multiply_matrices(matrix_a, matrix_b) - results.append(result_matrix) - - end_time = time.time() - - return { - "matrices_processed": len(matrices), - "multiplication_results": len(results), - "processing_time": end_time - start_time, - "pod_name": os.getenv("POD_NAME", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "node_name": os.getenv("NODE_NAME", "unknown"), - } - - # Create test matrices - test_matrices = [ - [[1, 2], [3, 4]], # 2x2 - [[5, 6], [7, 8]], # 2x2 - [[1, 0], [0, 1]], # 2x2 identity - [[9, 10], [11, 12]], # 2x2 - ] - - result = parallel_matrix_multiply(test_matrices) - - # Validate parallel processing - assert isinstance(result, dict) - assert result["matrices_processed"] == 4 - assert result["multiplication_results"] == 2 - assert result["processing_time"] > 0 - assert result["pod_name"] != "unknown" - - @pytest.mark.real_world - def test_k8s_job_lifecycle(self, kubernetes_config): - """Test Kubernetes job lifecycle events.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def test_job_lifecycle() -> Dict[str, Any]: - """Test job lifecycle in Kubernetes.""" - import os - import time - import signal - import atexit - - lifecycle_events = [] - - def signal_handler(signum, frame): - lifecycle_events.append(f"Signal {signum} received") - - def cleanup_handler(): - lifecycle_events.append("Cleanup handler called") - - # Register handlers - signal.signal(signal.SIGTERM, signal_handler) - signal.signal(signal.SIGINT, signal_handler) - atexit.register(cleanup_handler) - - lifecycle_events.append("Job started") - - # Simulate some work - for i in range(5): - time.sleep(0.5) - lifecycle_events.append(f"Work iteration {i + 1}") - - lifecycle_events.append("Job completed") - - return { - "lifecycle_events": lifecycle_events, - "pod_name": os.getenv("POD_NAME", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "exit_code": 0, - } - - result = test_job_lifecycle() - - # Validate job lifecycle - assert isinstance(result, dict) - assert len(result["lifecycle_events"]) >= 7 # Start + 5 iterations + completion - assert "Job started" in result["lifecycle_events"] - assert "Job completed" in result["lifecycle_events"] - assert result["exit_code"] == 0 - - @pytest.mark.real_world - def test_k8s_error_handling(self, kubernetes_config): - """Test Kubernetes job error handling.""" - - @cluster(cores=1, memory="1Gi", time="00:05:00") - def k8s_error_test(error_type: str) -> Dict[str, Any]: - """Test error handling in Kubernetes job.""" - import os - - if error_type == "success": - return { - "status": "success", - "pod_name": os.getenv("POD_NAME", "unknown"), - "message": "Job completed successfully", - } - elif error_type == "value_error": - raise ValueError("Test value error in Kubernetes job") - elif error_type == "permission_error": - # Try to write to a read-only location - with open("/etc/passwd", "w") as f: - f.write("test") - elif error_type == "memory_error": - # Try to allocate large amount of memory - large_list = [0] * (10**9) # This might cause memory error - return {"memory_allocated": len(large_list)} - else: - raise Exception(f"Unknown error type: {error_type}") - - # Test successful execution - result = k8s_error_test("success") - assert result["status"] == "success" - assert result["pod_name"] != "unknown" - - # Test error handling - with pytest.raises(ValueError): - k8s_error_test("value_error") - - with pytest.raises(PermissionError): - k8s_error_test("permission_error") - - @pytest.mark.real_world - @pytest.mark.expensive - def test_k8s_resource_intensive(self, kubernetes_config): - """Test resource-intensive Kubernetes job.""" - - @cluster(cores=2, memory="4Gi", time="00:15:00") - def resource_intensive_k8s_job() -> Dict[str, Any]: - """Perform resource-intensive operations in Kubernetes.""" - import os - import time - import psutil - import numpy as np - - start_time = time.time() - - # Get initial resource usage - process = psutil.Process(os.getpid()) - initial_memory = process.memory_info().rss / (1024**2) # MB - - # Create large numpy arrays for computation - try: - # Create large matrices - size = 1000 - matrix_a = np.random.random((size, size)) - matrix_b = np.random.random((size, size)) - - # Perform matrix multiplication - result_matrix = np.dot(matrix_a, matrix_b) - - # Compute statistics - matrix_mean = np.mean(result_matrix) - matrix_std = np.std(result_matrix) - - # Get peak memory usage - peak_memory = process.memory_info().rss / (1024**2) # MB - - end_time = time.time() - - return { - "matrix_size": size, - "initial_memory_mb": initial_memory, - "peak_memory_mb": peak_memory, - "memory_used_mb": peak_memory - initial_memory, - "computation_time": end_time - start_time, - "matrix_mean": float(matrix_mean), - "matrix_std": float(matrix_std), - "pod_name": os.getenv("POD_NAME", "unknown"), - "node_name": os.getenv("NODE_NAME", "unknown"), - } - - except ImportError: - # Fallback if numpy is not available - return { - "numpy_available": False, - "pod_name": os.getenv("POD_NAME", "unknown"), - "fallback_computation": True, - } - - result = resource_intensive_k8s_job() - - # Validate resource-intensive job - assert isinstance(result, dict) - if result.get("numpy_available", True): - assert result["matrix_size"] == 1000 - assert result["memory_used_mb"] > 0 - assert result["computation_time"] > 0 - assert result["pod_name"] != "unknown" diff --git a/tests/real_world/test_lambda_cloud_execution_real.py b/tests/real_world/test_lambda_cloud_execution_real.py deleted file mode 100644 index 3adb1661..00000000 --- a/tests/real_world/test_lambda_cloud_execution_real.py +++ /dev/null @@ -1,378 +0,0 @@ -""" -Real-world Lambda Cloud integration tests. - -These tests require actual Lambda Cloud credentials and create real instances. -NO MOCKS OR SIMULATIONS - these test real cloud execution. -""" - -import pytest -import os -import time -from unittest.mock import patch -import logging - -from clustrix import cluster, configure -from tests.real_world.credential_manager import get_lambda_credentials - -logger = logging.getLogger(__name__) - - -@pytest.mark.real_world -class TestLambdaCloudExecutionReal: - """Test real Lambda Cloud job execution.""" - - def setup_method(self): - """Setup for each test method.""" - self.lambda_creds = get_lambda_credentials() - if not self.lambda_creds: - pytest.skip("Lambda Cloud credentials not available") - - def test_lambda_cloud_basic_execution_real(self): - """Test basic function execution on real Lambda Cloud instance.""" - - @cluster( - provider="lambda", - instance_type="gpu_1x_a10", - region="us-east-1", - cores=2, - memory="8GB", - lambda_api_key=self.lambda_creds.get("api_key"), - terminate_on_completion=True, - instance_startup_timeout=300, - ) - def test_basic_computation(): - """Simple computation to verify execution works.""" - import platform - import os - - result = { - "computation": 2 + 2, - "platform": platform.platform(), - "python_version": platform.python_version(), - "working_directory": os.getcwd(), - "environment_check": "success", - } - - return result - - # Execute function - start_time = time.time() - result = test_basic_computation() - execution_time = time.time() - start_time - - # Verify results - assert result is not None - assert result["computation"] == 4 - assert ( - "ubuntu" in result["platform"].lower() - or "linux" in result["platform"].lower() - ) - assert result["environment_check"] == "success" - - # Verify execution happened on cloud (not locally) - assert execution_time > 60 # Should take time due to instance provisioning - - logger.info( - f"Lambda Cloud basic execution completed in {execution_time:.2f} seconds" - ) - logger.info(f"Result: {result}") - - def test_lambda_cloud_gpu_verification_real(self): - """Test GPU detection and computation on real Lambda Cloud GPU instance.""" - - @cluster( - provider="lambda", - instance_type="gpu_1x_a10", - region="us-east-1", - cores=4, - memory="16GB", - lambda_api_key=self.lambda_creds.get("api_key"), - terminate_on_completion=True, - instance_startup_timeout=300, - ) - def verify_gpu_functionality(): - """Verify GPU availability and perform basic GPU computation.""" - import subprocess - import json - - # Check NVIDIA driver and GPUs - try: - result = subprocess.run( - ["nvidia-smi", "-L"], capture_output=True, text=True, timeout=30 - ) - gpu_list = result.stdout if result.returncode == 0 else "No GPUs found" - except Exception as e: - gpu_list = f"Error checking GPUs: {e}" - - # Try basic PyTorch GPU computation - gpu_computation_result = None - try: - import torch - - if torch.cuda.is_available(): - device = torch.device("cuda:0") - - # Simple GPU computation - a = torch.randn(100, 100, device=device) - b = torch.randn(100, 100, device=device) - c = torch.mm(a, b) - - gpu_computation_result = { - "pytorch_version": torch.__version__, - "cuda_available": torch.cuda.is_available(), - "cuda_version": torch.version.cuda, - "device_count": torch.cuda.device_count(), - "device_name": torch.cuda.get_device_name(0), - "computation_successful": True, - "result_shape": list(c.shape), - "memory_allocated": torch.cuda.memory_allocated() - / (1024**2), # MB - } - else: - gpu_computation_result = { - "cuda_available": False, - "error": "CUDA not available", - } - - except Exception as e: - gpu_computation_result = {"error": f"GPU computation failed: {e}"} - - return { - "gpu_list": gpu_list, - "gpu_computation": gpu_computation_result, - "test_status": "completed", - } - - # Execute GPU verification - start_time = time.time() - result = verify_gpu_functionality() - execution_time = time.time() - start_time - - # Verify results - assert result is not None - assert result["test_status"] == "completed" - - # Verify GPU detection - assert ( - "gpu" in result["gpu_list"].lower() or "a10" in result["gpu_list"].lower() - ) - - # Verify GPU computation worked - gpu_result = result["gpu_computation"] - assert gpu_result is not None - - if "error" not in gpu_result: - assert gpu_result.get("cuda_available") == True - assert gpu_result.get("device_count", 0) > 0 - assert gpu_result.get("computation_successful") == True - assert "A10" in gpu_result.get("device_name", "") - - logger.info( - f"Lambda Cloud GPU verification completed in {execution_time:.2f} seconds" - ) - logger.info(f"GPU List: {result['gpu_list']}") - logger.info(f"GPU Computation: {gpu_result}") - - def test_lambda_cloud_data_transfer_real(self): - """Test data upload/download with real Lambda Cloud instance.""" - - import numpy as np - - # Create test data - test_matrix = np.random.randn(100, 100) - test_vector = np.random.randn(100) - - @cluster( - provider="lambda", - instance_type="gpu_1x_a10", - region="us-east-1", - cores=2, - memory="8GB", - lambda_api_key=self.lambda_creds.get("api_key"), - terminate_on_completion=True, - ) - def process_data(matrix, vector): - """Process data on Lambda Cloud instance.""" - import numpy as np - import time - - # Verify data integrity - assert matrix.shape == (100, 100) - assert vector.shape == (100,) - - # Perform computation - start_compute = time.time() - result_matrix = np.dot(matrix, matrix.T) - result_vector = np.dot(matrix, vector) - compute_time = time.time() - start_compute - - return { - "result_matrix_shape": result_matrix.shape, - "result_vector_shape": result_vector.shape, - "matrix_sum": float(np.sum(result_matrix)), - "vector_sum": float(np.sum(result_vector)), - "compute_time": compute_time, - "data_integrity_check": "passed", - } - - # Execute with data transfer - start_time = time.time() - result = process_data(test_matrix, test_vector) - total_time = time.time() - start_time - - # Verify results - assert result is not None - assert result["data_integrity_check"] == "passed" - assert result["result_matrix_shape"] == (100, 100) - assert result["result_vector_shape"] == (100,) - assert isinstance(result["matrix_sum"], float) - assert isinstance(result["vector_sum"], float) - assert result["compute_time"] > 0 - - logger.info( - f"Lambda Cloud data transfer test completed in {total_time:.2f} seconds" - ) - logger.info(f"Computation time: {result['compute_time']:.4f} seconds") - - def test_lambda_cloud_cost_tracking_real(self): - """Test cost tracking integration with real Lambda Cloud usage.""" - - @cluster( - provider="lambda", - instance_type="gpu_1x_a10", - region="us-east-1", - cores=1, - memory="4GB", - lambda_api_key=self.lambda_creds.get("api_key"), - terminate_on_completion=True, - ) - def cost_tracking_test(): - """Simple function to test cost tracking.""" - import time - - # Do some work to generate billable time - time.sleep(10) - - return { - "work_completed": True, - "execution_time": 10, - "cost_tracking_test": "completed", - } - - # Track execution - start_time = time.time() - result = cost_tracking_test() - end_time = time.time() - - execution_duration = end_time - start_time - - # Verify results - assert result is not None - assert result["cost_tracking_test"] == "completed" - - # Verify execution took reasonable time (including provisioning) - assert execution_duration > 60 # Should include instance startup time - - # Note: In a real implementation, we would also verify: - # - Usage appears in Lambda Cloud dashboard - # - Cost estimates are generated - # - Billing information is tracked - # This requires access to Lambda Cloud billing API - - logger.info(f"Cost tracking test completed in {execution_duration:.2f} seconds") - logger.info("Note: Check Lambda Cloud dashboard for usage data") - - def test_lambda_cloud_error_handling_real(self): - """Test error handling with real Lambda Cloud execution.""" - - @cluster( - provider="lambda", - instance_type="gpu_1x_a10", - region="us-east-1", - cores=1, - memory="4GB", - lambda_api_key=self.lambda_creds.get("api_key"), - terminate_on_completion=True, - ) - def failing_function(): - """Function that intentionally fails to test error handling.""" - import time - - # Do some work before failing - time.sleep(2) - - # Intentional failure - raise ValueError("Intentional test failure") - - # Execute and expect failure - with pytest.raises(RuntimeError) as exc_info: - failing_function() - - # Verify error information - error_message = str(exc_info.value) - assert "failed" in error_message.lower() - assert "intentional test failure" in error_message - - logger.info(f"Error handling test completed: {error_message}") - - def test_lambda_cloud_multiple_instance_types_real(self): - """Test different Lambda Cloud instance types.""" - - instance_types = ["gpu_1x_a10"] # Start with one, expand as needed - - for instance_type in instance_types: - - @cluster( - provider="lambda", - instance_type=instance_type, - region="us-east-1", - cores=1, - memory="4GB", - lambda_api_key=self.lambda_creds.get("api_key"), - terminate_on_completion=True, - ) - def test_instance_type(): - """Test execution on specific instance type.""" - import subprocess - import platform - - # Get system information - cpu_info = platform.processor() - - # Get GPU information if available - try: - gpu_result = subprocess.run( - ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], - capture_output=True, - text=True, - timeout=10, - ) - gpu_info = ( - gpu_result.stdout.strip() - if gpu_result.returncode == 0 - else "No GPU" - ) - except: - gpu_info = "GPU query failed" - - return { - "instance_type_tested": instance_type, - "cpu_info": cpu_info, - "gpu_info": gpu_info, - "test_result": "success", - } - - # Execute test for this instance type - result = test_instance_type() - - # Verify results - assert result is not None - assert result["test_result"] == "success" - assert result["instance_type_tested"] == instance_type - - logger.info(f"Instance type {instance_type} test completed") - logger.info(f"CPU: {result['cpu_info']}") - logger.info(f"GPU: {result['gpu_info']}") - - # Add delay between instance tests to avoid rate limits - time.sleep(30) diff --git a/tests/real_world/test_pbs_job_submission_real.py b/tests/real_world/test_pbs_job_submission_real.py deleted file mode 100644 index 42c66375..00000000 --- a/tests/real_world/test_pbs_job_submission_real.py +++ /dev/null @@ -1,475 +0,0 @@ -""" -Real PBS job submission tests using @cluster decorator. - -These tests actually submit jobs to real PBS clusters and validate -the complete end-to-end workflow with the @cluster decorator. -""" - -import pytest -import os -import time -import uuid -from pathlib import Path -from typing import List, Dict, Any - -from clustrix import cluster, configure -from clustrix.config import ClusterConfig -from tests.real_world import TempResourceManager, credentials, test_manager - - -class TestRealPBSJobSubmission: - """Test real PBS job submission using @cluster decorator.""" - - @pytest.fixture - def pbs_config(self): - """Get PBS configuration for testing.""" - # Use SSH credentials for PBS cluster access - ssh_creds = credentials.get_ssh_credentials() - if not ssh_creds: - pytest.skip("No SSH credentials available for PBS testing") - - # Configure clustrix for PBS - configure( - cluster_type="pbs", - cluster_host=ssh_creds["host"], - username=ssh_creds["username"], - password=ssh_creds.get("password"), - private_key_path=ssh_creds.get("private_key_path"), - remote_work_dir=f"/tmp/clustrix_pbs_test_{uuid.uuid4().hex[:8]}", - cleanup_remote_files=True, - ) - - return ssh_creds - - @pytest.mark.real_world - def test_simple_function_pbs_submission(self, pbs_config): - """Test submitting a simple function to PBS.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def multiply_numbers(x: int, y: int) -> int: - """Simple multiplication function for testing.""" - return x * y - - # Submit job and wait for result - result = multiply_numbers(6, 7) - - # Validate result - assert result == 42 - assert isinstance(result, int) - - @pytest.mark.real_world - def test_function_with_pbs_environment(self, pbs_config): - """Test PBS job that accesses PBS environment variables.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def get_pbs_environment() -> Dict[str, str]: - """Get PBS job environment variables.""" - import os - - return { - "PBS_JOBID": os.getenv("PBS_JOBID", "not_set"), - "PBS_JOBNAME": os.getenv("PBS_JOBNAME", "not_set"), - "PBS_NODEFILE": os.getenv("PBS_NODEFILE", "not_set"), - "PBS_QUEUE": os.getenv("PBS_QUEUE", "not_set"), - "PBS_WORKDIR": os.getenv("PBS_WORKDIR", "not_set"), - "HOSTNAME": os.getenv("HOSTNAME", "not_set"), - "USER": os.getenv("USER", "not_set"), - "PWD": os.getenv("PWD", "not_set"), - } - - result = get_pbs_environment() - - # Validate PBS environment - assert isinstance(result, dict) - assert "PBS_JOBID" in result - # Note: PBS environment variables may not be set in all PBS configurations - # We'll validate what we can - assert result["USER"] != "not_set" - assert result["PWD"] != "not_set" - - @pytest.mark.real_world - def test_function_with_queue_specification_pbs(self, pbs_config): - """Test PBS job submission to specific queue.""" - - @cluster(cores=1, memory="1GB", time="00:05:00", queue="batch") - def test_batch_queue() -> Dict[str, str]: - """Test function for batch queue.""" - import os - - return { - "queue": os.getenv("PBS_QUEUE", "unknown"), - "jobid": os.getenv("PBS_JOBID", "unknown"), - "result": "batch_queue_success", - } - - try: - result = test_batch_queue() - assert isinstance(result, dict) - assert result["result"] == "batch_queue_success" - except Exception as e: - # Queue might not exist, skip test - pytest.skip(f"Batch queue not available: {e}") - - @pytest.mark.real_world - def test_pbs_node_file_processing(self, pbs_config): - """Test PBS job that processes node file information.""" - - @cluster(cores=2, memory="2GB", time="00:05:00") - def process_node_file() -> Dict[str, Any]: - """Process PBS node file to get node information.""" - import os - - result = { - "node_file_path": os.getenv("PBS_NODEFILE", "not_set"), - "allocated_nodes": [], - "node_count": 0, - "unique_nodes": 0, - } - - node_file = os.getenv("PBS_NODEFILE") - if node_file and os.path.exists(node_file): - try: - with open(node_file, "r") as f: - nodes = [line.strip() for line in f.readlines()] - result["allocated_nodes"] = nodes - result["node_count"] = len(nodes) - result["unique_nodes"] = len(set(nodes)) - except Exception as e: - result["error"] = str(e) - - return result - - result = process_node_file() - - # Validate node file processing - assert isinstance(result, dict) - assert "node_file_path" in result - assert "node_count" in result - - @pytest.mark.real_world - def test_pbs_array_job_simulation(self, pbs_config): - """Test PBS job that simulates array job behavior.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def array_job_task(task_id: int, total_tasks: int) -> Dict[str, Any]: - """Simulate an array job task.""" - import os - import time - - # Simulate different work based on task ID - work_amount = task_id * 0.1 - time.sleep(work_amount) - - return { - "task_id": task_id, - "total_tasks": total_tasks, - "work_amount": work_amount, - "pbs_jobid": os.getenv("PBS_JOBID", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "completion_time": time.time(), - } - - # Submit multiple tasks to simulate array job - results = [] - total_tasks = 3 - - for task_id in range(total_tasks): - result = array_job_task(task_id, total_tasks) - results.append(result) - - # Validate array job results - assert len(results) == total_tasks - - for i, result in enumerate(results): - assert isinstance(result, dict) - assert result["task_id"] == i - assert result["total_tasks"] == total_tasks - assert result["hostname"] != "unknown" - - @pytest.mark.real_world - def test_pbs_resource_monitoring(self, pbs_config): - """Test PBS job that monitors resource usage.""" - - @cluster(cores=2, memory="2GB", time="00:10:00") - def monitor_resources() -> Dict[str, Any]: - """Monitor resource usage during job execution.""" - import os - import psutil - import time - - # Get initial resource information - process = psutil.Process(os.getpid()) - initial_memory = process.memory_info().rss / 1024 / 1024 # MB - - # Perform some work - data = [] - for i in range(100000): - data.append(i * 2.5) - - # Get resource usage - peak_memory = process.memory_info().rss / 1024 / 1024 # MB - cpu_percent = process.cpu_percent(interval=1) - - return { - "initial_memory_mb": initial_memory, - "peak_memory_mb": peak_memory, - "memory_used_mb": peak_memory - initial_memory, - "cpu_percent": cpu_percent, - "data_points": len(data), - "pbs_jobid": os.getenv("PBS_JOBID", "unknown"), - "allocated_cpus": os.getenv("NCPUS", "unknown"), - } - - result = monitor_resources() - - # Validate resource monitoring - assert isinstance(result, dict) - assert result["memory_used_mb"] >= 0 - assert result["data_points"] == 100000 - assert result["cpu_percent"] >= 0 - - @pytest.mark.real_world - def test_pbs_file_staging(self, pbs_config): - """Test PBS job with file staging operations.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def stage_and_process_files() -> Dict[str, Any]: - """Stage files and process them in PBS job.""" - import os - import tempfile - import shutil - - # Create temporary directory - work_dir = tempfile.mkdtemp(prefix="pbs_test_") - - try: - # Create test files - input_file = os.path.join(work_dir, "input.txt") - output_file = os.path.join(work_dir, "output.txt") - - # Write input data - with open(input_file, "w") as f: - f.write("This is test input data\n") - f.write("Line 2 of input\n") - f.write("Line 3 of input\n") - - # Process file - with open(input_file, "r") as infile: - lines = infile.readlines() - - # Write processed output - with open(output_file, "w") as outfile: - for i, line in enumerate(lines): - outfile.write(f"Processed line {i + 1}: {line}") - - # Verify output - with open(output_file, "r") as f: - output_content = f.read() - - return { - "work_dir": work_dir, - "input_lines": len(lines), - "output_length": len(output_content), - "processing_successful": "Processed line 1:" in output_content, - "files_created": [ - os.path.basename(input_file), - os.path.basename(output_file), - ], - } - - finally: - # Cleanup - try: - shutil.rmtree(work_dir) - except: - pass - - result = stage_and_process_files() - - # Validate file staging - assert isinstance(result, dict) - assert result["input_lines"] == 3 - assert result["processing_successful"] is True - assert len(result["files_created"]) == 2 - - @pytest.mark.real_world - def test_pbs_error_handling(self, pbs_config): - """Test PBS job error handling and recovery.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def job_with_conditional_error( - should_fail: bool, error_type: str = "value" - ) -> Dict[str, Any]: - """Job that can fail in different ways.""" - import os - - if should_fail: - if error_type == "value": - raise ValueError("Test value error") - elif error_type == "runtime": - raise RuntimeError("Test runtime error") - elif error_type == "file": - with open("/nonexistent/path/file.txt", "r") as f: - f.read() - - return { - "success": True, - "pbs_jobid": os.getenv("PBS_JOBID", "unknown"), - "error_type": "none", - } - - # Test successful execution - result = job_with_conditional_error(False) - assert result["success"] is True - - # Test error handling - with pytest.raises(ValueError): - job_with_conditional_error(True, "value") - - with pytest.raises(RuntimeError): - job_with_conditional_error(True, "runtime") - - @pytest.mark.real_world - @pytest.mark.expensive - def test_pbs_long_running_job(self, pbs_config): - """Test longer-running PBS job.""" - - @cluster(cores=1, memory="1GB", time="00:15:00") - def long_computation() -> Dict[str, Any]: - """Perform a longer computation.""" - import time - import math - - start_time = time.time() - - # Perform iterative computation - result = 0 - iterations = 1000000 - - for i in range(iterations): - result += math.sin(i * 0.001) - if i % 100000 == 0: - # Periodic checkpoint - current_time = time.time() - elapsed = current_time - start_time - if elapsed > 300: # 5 minutes max - break - - end_time = time.time() - - return { - "start_time": start_time, - "end_time": end_time, - "duration": end_time - start_time, - "result": result, - "iterations_completed": i + 1, - "average_time_per_iteration": (end_time - start_time) / (i + 1), - } - - result = long_computation() - - # Validate long computation - assert isinstance(result, dict) - assert result["duration"] > 1.0 # Should run for reasonable time - assert result["iterations_completed"] > 0 - assert result["average_time_per_iteration"] > 0 - - @pytest.mark.real_world - def test_pbs_job_cleanup(self, pbs_config): - """Test PBS job cleanup and resource management.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def test_cleanup_behavior() -> Dict[str, Any]: - """Test cleanup behavior in PBS job.""" - import os - import tempfile - import atexit - - # Create temporary files - temp_files = [] - for i in range(3): - temp_file = tempfile.NamedTemporaryFile(delete=False) - temp_file.write(f"Temp file {i} content".encode()) - temp_file.close() - temp_files.append(temp_file.name) - - # Register cleanup function - def cleanup_temp_files(): - for temp_file in temp_files: - try: - os.unlink(temp_file) - except: - pass - - atexit.register(cleanup_temp_files) - - # Verify files exist - files_exist = [os.path.exists(f) for f in temp_files] - - return { - "temp_files_created": len(temp_files), - "files_exist": all(files_exist), - "temp_file_paths": temp_files, - "cleanup_registered": True, - } - - result = test_cleanup_behavior() - - # Validate cleanup setup - assert isinstance(result, dict) - assert result["temp_files_created"] == 3 - assert result["files_exist"] is True - assert result["cleanup_registered"] is True - assert len(result["temp_file_paths"]) == 3 - - @pytest.mark.real_world - def test_pbs_parallel_processing(self, pbs_config): - """Test parallel processing capabilities in PBS.""" - - @cluster(cores=2, memory="2GB", time="00:10:00", parallel=True) - def parallel_computation(data_chunks: List[List[int]]) -> Dict[str, Any]: - """Process data chunks in parallel.""" - import time - import concurrent.futures - import os - - def process_chunk(chunk): - """Process a single chunk of data.""" - time.sleep(0.1) # Simulate processing time - return sum(chunk) - - start_time = time.time() - - # Process chunks - results = [] - for chunk in data_chunks: - result = process_chunk(chunk) - results.append(result) - - end_time = time.time() - - return { - "chunks_processed": len(data_chunks), - "chunk_results": results, - "total_result": sum(results), - "processing_time": end_time - start_time, - "pbs_jobid": os.getenv("PBS_JOBID", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - } - - # Create test data - test_chunks = [ - [1, 2, 3, 4, 5], - [6, 7, 8, 9, 10], - [11, 12, 13, 14, 15], - [16, 17, 18, 19, 20], - ] - - result = parallel_computation(test_chunks) - - # Validate parallel processing - assert isinstance(result, dict) - assert result["chunks_processed"] == 4 - assert len(result["chunk_results"]) == 4 - assert result["total_result"] == 210 # Sum of 1-20 - assert result["processing_time"] > 0 diff --git a/tests/real_world/test_sge_job_submission_real.py b/tests/real_world/test_sge_job_submission_real.py deleted file mode 100644 index c55becf6..00000000 --- a/tests/real_world/test_sge_job_submission_real.py +++ /dev/null @@ -1,504 +0,0 @@ -""" -Real SGE (Sun Grid Engine) job submission tests using @cluster decorator. - -These tests actually submit jobs to real SGE clusters and validate -the complete end-to-end workflow with the @cluster decorator. -""" - -import pytest -import os -import time -import uuid -from pathlib import Path -from typing import List, Dict, Any - -from clustrix import cluster, configure -from clustrix.config import ClusterConfig -from tests.real_world import TempResourceManager, credentials, test_manager - - -class TestRealSGEJobSubmission: - """Test real SGE job submission using @cluster decorator.""" - - @pytest.fixture - def sge_config(self): - """Get SGE configuration for testing.""" - # Use SSH credentials for SGE cluster access - ssh_creds = credentials.get_ssh_credentials() - if not ssh_creds: - pytest.skip("No SSH credentials available for SGE testing") - - # Configure clustrix for SGE - configure( - cluster_type="sge", - cluster_host=ssh_creds["host"], - username=ssh_creds["username"], - password=ssh_creds.get("password"), - private_key_path=ssh_creds.get("private_key_path"), - remote_work_dir=f"/tmp/clustrix_sge_test_{uuid.uuid4().hex[:8]}", - cleanup_remote_files=True, - ) - - return ssh_creds - - @pytest.mark.real_world - def test_simple_function_sge_submission(self, sge_config): - """Test submitting a simple function to SGE.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def divide_numbers(x: float, y: float) -> float: - """Simple division function for testing.""" - if y == 0: - return float("inf") - return x / y - - # Submit job and wait for result - result = divide_numbers(84.0, 2.0) - - # Validate result - assert result == 42.0 - assert isinstance(result, float) - - @pytest.mark.real_world - def test_function_with_sge_environment(self, sge_config): - """Test SGE job that accesses SGE environment variables.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def get_sge_environment() -> Dict[str, str]: - """Get SGE job environment variables.""" - import os - - return { - "JOB_ID": os.getenv("JOB_ID", "not_set"), - "JOB_NAME": os.getenv("JOB_NAME", "not_set"), - "QUEUE": os.getenv("QUEUE", "not_set"), - "SGE_TASK_ID": os.getenv("SGE_TASK_ID", "not_set"), - "SGE_CELL": os.getenv("SGE_CELL", "not_set"), - "SGE_ROOT": os.getenv("SGE_ROOT", "not_set"), - "PE_HOSTFILE": os.getenv("PE_HOSTFILE", "not_set"), - "NSLOTS": os.getenv("NSLOTS", "not_set"), - "HOSTNAME": os.getenv("HOSTNAME", "not_set"), - "USER": os.getenv("USER", "not_set"), - "PWD": os.getenv("PWD", "not_set"), - } - - result = get_sge_environment() - - # Validate SGE environment - assert isinstance(result, dict) - assert "JOB_ID" in result - # Note: SGE environment variables may not be set in all SGE configurations - # We'll validate what we can - assert result["USER"] != "not_set" - assert result["PWD"] != "not_set" - - @pytest.mark.real_world - def test_sge_parallel_environment(self, sge_config): - """Test SGE job with parallel environment.""" - - @cluster(cores=2, memory="2GB", time="00:10:00") - def test_parallel_environment() -> Dict[str, Any]: - """Test SGE parallel environment setup.""" - import os - - result = { - "nslots": os.getenv("NSLOTS", "not_set"), - "pe_hostfile": os.getenv("PE_HOSTFILE", "not_set"), - "pe_hostfile_exists": False, - "allocated_hosts": [], - "total_slots": 0, - } - - # Check if PE hostfile exists and process it - pe_hostfile = os.getenv("PE_HOSTFILE") - if pe_hostfile and os.path.exists(pe_hostfile): - result["pe_hostfile_exists"] = True - try: - with open(pe_hostfile, "r") as f: - lines = f.readlines() - for line in lines: - parts = line.strip().split() - if len(parts) >= 2: - hostname = parts[0] - slots = int(parts[1]) - result["allocated_hosts"].append( - {"hostname": hostname, "slots": slots} - ) - result["total_slots"] += slots - except Exception as e: - result["error"] = str(e) - - return result - - result = test_parallel_environment() - - # Validate parallel environment - assert isinstance(result, dict) - assert "nslots" in result - assert "pe_hostfile" in result - - @pytest.mark.real_world - def test_sge_queue_specification(self, sge_config): - """Test SGE job submission to specific queue.""" - - @cluster(cores=1, memory="1GB", time="00:05:00", queue="all.q") - def test_queue_submission() -> Dict[str, str]: - """Test function for specific queue.""" - import os - - return { - "queue": os.getenv("QUEUE", "unknown"), - "job_id": os.getenv("JOB_ID", "unknown"), - "sge_cell": os.getenv("SGE_CELL", "unknown"), - "result": "queue_submission_success", - } - - try: - result = test_queue_submission() - assert isinstance(result, dict) - assert result["result"] == "queue_submission_success" - except Exception as e: - # Queue might not exist, skip test - pytest.skip(f"Queue 'all.q' not available: {e}") - - @pytest.mark.real_world - def test_sge_array_job_simulation(self, sge_config): - """Test SGE job that simulates array job behavior.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def array_task_simulation(task_id: int, parameter: float) -> Dict[str, Any]: - """Simulate an array job task.""" - import os - import time - import math - - # Simulate task-specific work - work_result = math.sin(task_id * parameter) - time.sleep(0.1 * task_id) # Variable work time - - return { - "task_id": task_id, - "parameter": parameter, - "work_result": work_result, - "job_id": os.getenv("JOB_ID", "unknown"), - "sge_task_id": os.getenv("SGE_TASK_ID", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "queue": os.getenv("QUEUE", "unknown"), - "completion_time": time.time(), - } - - # Submit multiple tasks to simulate array job - results = [] - for i in range(3): - result = array_task_simulation(i, 0.5) - results.append(result) - - # Validate array job simulation - assert len(results) == 3 - - for i, result in enumerate(results): - assert isinstance(result, dict) - assert result["task_id"] == i - assert result["parameter"] == 0.5 - assert result["hostname"] != "unknown" - - @pytest.mark.real_world - def test_sge_resource_limits(self, sge_config): - """Test SGE job with resource limits.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def test_resource_limits() -> Dict[str, Any]: - """Test resource limits in SGE job.""" - import os - import psutil - import resource - - # Get process information - process = psutil.Process(os.getpid()) - memory_info = process.memory_info() - - # Get system resource limits - try: - memory_limit = resource.getrlimit(resource.RLIMIT_AS) - cpu_limit = resource.getrlimit(resource.RLIMIT_CPU) - except: - memory_limit = ("unknown", "unknown") - cpu_limit = ("unknown", "unknown") - - return { - "memory_rss_mb": memory_info.rss / 1024 / 1024, - "memory_vms_mb": memory_info.vms / 1024 / 1024, - "memory_soft_limit": memory_limit[0], - "memory_hard_limit": memory_limit[1], - "cpu_soft_limit": cpu_limit[0], - "cpu_hard_limit": cpu_limit[1], - "nslots": os.getenv("NSLOTS", "unknown"), - "job_id": os.getenv("JOB_ID", "unknown"), - } - - result = test_resource_limits() - - # Validate resource limits - assert isinstance(result, dict) - assert result["memory_rss_mb"] > 0 - assert result["memory_vms_mb"] > 0 - - @pytest.mark.real_world - def test_sge_file_operations(self, sge_config): - """Test SGE job with file operations.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def sge_file_processing() -> Dict[str, Any]: - """Process files in SGE job.""" - import os - import tempfile - import json - - # Create temporary working directory - work_dir = tempfile.mkdtemp(prefix="sge_test_") - - try: - # Create test data - test_data = { - "job_id": os.getenv("JOB_ID", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "queue": os.getenv("QUEUE", "unknown"), - "timestamp": time.time(), - "data_points": [i * 2.5 for i in range(100)], - } - - # Write data to file - data_file = os.path.join(work_dir, "test_data.json") - with open(data_file, "w") as f: - json.dump(test_data, f, indent=2) - - # Read and process data - with open(data_file, "r") as f: - loaded_data = json.load(f) - - # Compute statistics - data_points = loaded_data["data_points"] - stats = { - "count": len(data_points), - "sum": sum(data_points), - "mean": sum(data_points) / len(data_points), - "min": min(data_points), - "max": max(data_points), - } - - # Write results - result_file = os.path.join(work_dir, "results.json") - with open(result_file, "w") as f: - json.dump(stats, f, indent=2) - - return { - "work_dir": work_dir, - "data_file_created": os.path.exists(data_file), - "result_file_created": os.path.exists(result_file), - "statistics": stats, - "job_info": { - "job_id": loaded_data["job_id"], - "hostname": loaded_data["hostname"], - "queue": loaded_data["queue"], - }, - } - - finally: - # Cleanup - import shutil - - try: - shutil.rmtree(work_dir) - except: - pass - - result = sge_file_processing() - - # Validate file operations - assert isinstance(result, dict) - assert result["data_file_created"] is True - assert result["result_file_created"] is True - assert result["statistics"]["count"] == 100 - assert result["statistics"]["sum"] > 0 - - @pytest.mark.real_world - def test_sge_error_handling(self, sge_config): - """Test SGE job error handling.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def sge_error_test(error_mode: str) -> Dict[str, Any]: - """Test different error scenarios in SGE.""" - import os - - if error_mode == "success": - return { - "status": "success", - "job_id": os.getenv("JOB_ID", "unknown"), - "message": "Job completed successfully", - } - elif error_mode == "value_error": - raise ValueError("Test value error in SGE job") - elif error_mode == "file_error": - with open("/nonexistent/directory/file.txt", "r") as f: - return f.read() - elif error_mode == "runtime_error": - raise RuntimeError("Test runtime error in SGE job") - else: - raise Exception(f"Unknown error mode: {error_mode}") - - # Test successful execution - result = sge_error_test("success") - assert result["status"] == "success" - assert result["job_id"] != "unknown" - - # Test error handling - with pytest.raises(ValueError): - sge_error_test("value_error") - - with pytest.raises(RuntimeError): - sge_error_test("runtime_error") - - @pytest.mark.real_world - def test_sge_job_dependencies(self, sge_config): - """Test SGE job with external dependencies.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def test_dependencies() -> Dict[str, Any]: - """Test job that uses external libraries.""" - import os - import sys - import json - import time - import math - import statistics - - # Test standard library availability - test_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - - stats_result = { - "mean": statistics.mean(test_data), - "stdev": statistics.stdev(test_data), - "median": statistics.median(test_data), - } - - # Test math operations - math_result = { - "sin_sum": sum(math.sin(x) for x in test_data), - "cos_sum": sum(math.cos(x) for x in test_data), - "log_sum": sum(math.log(x) for x in test_data), - } - - return { - "python_version": sys.version, - "statistics": stats_result, - "math_operations": math_result, - "job_id": os.getenv("JOB_ID", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "execution_time": time.time(), - } - - result = test_dependencies() - - # Validate dependencies - assert isinstance(result, dict) - assert "python_version" in result - assert result["statistics"]["mean"] == 5.5 - assert result["statistics"]["median"] == 5.5 - assert result["math_operations"]["sin_sum"] != 0 - - @pytest.mark.real_world - @pytest.mark.expensive - def test_sge_compute_intensive(self, sge_config): - """Test compute-intensive SGE job.""" - - @cluster(cores=2, memory="2GB", time="00:15:00") - def compute_intensive_task() -> Dict[str, Any]: - """Perform compute-intensive operations.""" - import time - import math - import os - - start_time = time.time() - - # Perform iterative computation - result = 0 - iterations = 500000 - - for i in range(iterations): - result += math.sin(i * 0.001) * math.cos(i * 0.002) - - # Checkpoint every 100k iterations - if i % 100000 == 0 and i > 0: - elapsed = time.time() - start_time - if elapsed > 600: # 10 minutes max - break - - end_time = time.time() - - return { - "start_time": start_time, - "end_time": end_time, - "duration": end_time - start_time, - "iterations_completed": i + 1, - "result": result, - "job_id": os.getenv("JOB_ID", "unknown"), - "nslots": os.getenv("NSLOTS", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - } - - result = compute_intensive_task() - - # Validate compute-intensive task - assert isinstance(result, dict) - assert result["duration"] > 1.0 # Should take some time - assert result["iterations_completed"] > 0 - assert result["result"] != 0 - - @pytest.mark.real_world - def test_sge_job_monitoring(self, sge_config): - """Test SGE job status monitoring.""" - - @cluster(cores=1, memory="1GB", time="00:05:00") - def monitored_job() -> Dict[str, Any]: - """Job that can be monitored during execution.""" - import os - import time - - start_time = time.time() - - # Create progress tracking - progress_steps = 5 - step_duration = 1.0 # 1 second per step - - for step in range(progress_steps): - time.sleep(step_duration) - # In a real scenario, this might write to a progress file - current_time = time.time() - elapsed = current_time - start_time - - if elapsed > 30: # Safety timeout - break - - end_time = time.time() - - return { - "start_time": start_time, - "end_time": end_time, - "duration": end_time - start_time, - "steps_completed": step + 1, - "job_id": os.getenv("JOB_ID", "unknown"), - "hostname": os.getenv("HOSTNAME", "unknown"), - "queue": os.getenv("QUEUE", "unknown"), - } - - # Monitor job execution - start_time = time.time() - result = monitored_job() - total_time = time.time() - start_time - - # Validate monitoring - assert isinstance(result, dict) - assert result["duration"] >= 4.0 # Should run for at least 4 seconds - assert result["steps_completed"] >= 4 - assert result["job_id"] != "unknown" - assert total_time >= result["duration"] # Total time includes scheduling diff --git a/tests/reference_workflows/__init__.py b/tests/reference_workflows/__init__.py index c79d354f..e81a3cec 100644 --- a/tests/reference_workflows/__init__.py +++ b/tests/reference_workflows/__init__.py @@ -14,12 +14,6 @@ test_file_processing_workflow, ) -from .kubernetes_workflows import ( - test_kubernetes_auto_provisioning_workflow, - test_kubernetes_multi_node_workflow, - test_kubernetes_gpu_workflow, -) - from .data_analysis_workflows import ( test_pandas_analysis_workflow, test_numpy_computation_workflow, @@ -31,10 +25,6 @@ "test_basic_data_analysis_workflow", "test_simple_computation_workflow", "test_file_processing_workflow", - # Kubernetes - "test_kubernetes_auto_provisioning_workflow", - "test_kubernetes_multi_node_workflow", - "test_kubernetes_gpu_workflow", # Data analysis "test_pandas_analysis_workflow", "test_numpy_computation_workflow", diff --git a/tests/reference_workflows/kubernetes_workflows.py b/tests/reference_workflows/kubernetes_workflows.py deleted file mode 100644 index 9adad92b..00000000 --- a/tests/reference_workflows/kubernetes_workflows.py +++ /dev/null @@ -1,333 +0,0 @@ -""" -Reference patterns for Kubernetes workflows. - -These patterns demonstrate how users would use clustrix with Kubernetes, -including auto-provisioning, multi-node deployments, and GPU workloads. -""" - -import pytest -import os -import time -from clustrix import cluster -from clustrix.config import ClusterConfig -import clustrix.config as config_module - - -def test_kubernetes_auto_provisioning_workflow(): - """ - Reference pattern for Kubernetes auto-provisioning. - - This demonstrates: - - K8s auto-provisioning configuration - - Real cloud provider integration (AWS/GCP/Azure) - - Container-based execution - - Cleanup on exit - """ - - # User configures for auto-provisioning - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - - # Use environment to determine provider (local for CI, cloud for integration tests) - provider = os.getenv("K8S_TEST_PROVIDER", "local") - config.k8s_provider = provider - - if provider == "aws": - config.k8s_region = os.getenv("AWS_REGION", "us-west-2") - config.k8s_node_type = "t3.medium" - elif provider == "gcp": - config.k8s_region = os.getenv("GCP_REGION", "us-central1") - config.k8s_node_type = "e2-medium" - elif provider == "azure": - config.k8s_region = os.getenv("AZURE_REGION", "eastus") - config.k8s_node_type = "Standard_B2s" - - config.k8s_node_count = 2 - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"test-cluster-{int(time.time())}" - - original_config = config_module._config - config_module._config = config - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - provider=provider, - node_count=2, - cores=2, - memory="4Gi", - parallel=False, - ) - def train_model(data_size, epochs=10): - """Train a simple ML model on Kubernetes.""" - import numpy as np - import time - from sklearn.linear_model import LogisticRegression - from sklearn.model_selection import train_test_split - from sklearn.metrics import accuracy_score - - start_time = time.time() - - # Generate synthetic dataset - np.random.seed(42) - X = np.random.randn(data_size, 20) # 20 features - # Create labels with some pattern - y = (X[:, 0] + X[:, 1] * 0.5 + np.random.randn(data_size) * 0.1 > 0).astype( - int - ) - - # Split data - X_train, X_test, y_train, y_test = train_test_split( - X, y, test_size=0.2, random_state=42 - ) - - # Train model - model = LogisticRegression(max_iter=epochs * 10) - model.fit(X_train, y_train) - - # Evaluate - train_score = accuracy_score(y_train, model.predict(X_train)) - test_score = accuracy_score(y_test, model.predict(X_test)) - - training_time = time.time() - start_time - - # Get environment info to verify K8s execution - import socket - import platform - - return { - "train_accuracy": float(train_score), - "test_accuracy": float(test_score), - "data_size": data_size, - "epochs": epochs, - "training_time": training_time, - "environment": { - "hostname": socket.gethostname(), - "platform": platform.platform(), - "python_version": platform.python_version(), - }, - "model_coefficients": model.coef_.tolist()[0][ - :5 - ], # First 5 coefficients - } - - # Execute with auto-provisioning - result = train_model(1000, epochs=5) - - # Validate results - assert result["data_size"] == 1000 - assert result["epochs"] == 5 - assert ( - 0.4 <= result["test_accuracy"] <= 1.0 - ) # Should achieve reasonable accuracy - assert result["training_time"] > 0 - assert len(result["model_coefficients"]) == 5 - - # Verify Kubernetes execution (hostname should indicate pod/node) - hostname = result["environment"]["hostname"] - # In K8s, hostname typically contains pod name or node identifier - assert len(hostname) > 0 - - finally: - config_module._config = original_config - - -def test_kubernetes_multi_node_workflow(): - """ - Reference pattern for multi-node Kubernetes workloads. - - This demonstrates: - - Multi-node cluster configuration - - Distributed computation - - Node coordination - - Resource distribution - """ - - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = os.getenv("K8S_TEST_PROVIDER", "local") - config.k8s_node_count = 3 # Multi-node cluster - config.k8s_cleanup_on_exit = True - - original_config = config_module._config - config_module._config = config - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - node_count=3, - cores=4, - memory="8Gi", - ) - def distributed_matrix_computation(matrix_size): - """Perform distributed matrix operations.""" - import numpy as np - from scipy import linalg - import time - - start_time = time.time() - - # Create large random matrix - np.random.seed(42) - A = np.random.randn(matrix_size, matrix_size) - B = np.random.randn(matrix_size, matrix_size) - - # Perform various matrix operations - results = {} - - # Matrix multiplication - C = np.matmul(A, B) - results["matmul_shape"] = C.shape - results["matmul_trace"] = float(np.trace(C)) - - # Eigenvalue decomposition (computationally intensive) - eigenvalues, _ = linalg.eig(A[:100, :100]) # Use subset for speed - results["eigenvalues"] = { - "count": len(eigenvalues), - "max_real": float(np.max(eigenvalues.real)), - "min_real": float(np.min(eigenvalues.real)), - } - - # SVD (another intensive operation) - U, s, Vt = linalg.svd(B[:100, :100], full_matrices=False) - results["svd"] = { - "singular_values": s[:5].tolist(), # First 5 singular values - "condition_number": float(s[0] / s[-1]), - } - - computation_time = time.time() - start_time - results["computation_time"] = computation_time - results["matrix_size"] = matrix_size - - return results - - # Execute distributed computation - result = distributed_matrix_computation(500) - - # Validate results - assert result["matmul_shape"] == (500, 500) - assert result["eigenvalues"]["count"] == 100 - assert len(result["svd"]["singular_values"]) == 5 - assert result["computation_time"] > 0 - assert result["svd"]["condition_number"] > 1 # Should be > 1 for random matrix - - finally: - config_module._config = original_config - - -def test_kubernetes_gpu_workflow(): - """ - Reference pattern for GPU-enabled Kubernetes workloads. - - This demonstrates: - - GPU resource requests - - CUDA computation - - GPU memory management - - Performance comparison - """ - - # Skip if no GPU available - gpu_available = os.getenv("K8S_GPU_AVAILABLE", "false").lower() == "true" - if not gpu_available: - pytest.skip("GPU not available for testing") - - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = os.getenv("K8S_GPU_PROVIDER", "aws") - config.k8s_node_type = ( - "p3.2xlarge" if config.k8s_provider == "aws" else "n1-standard-4" - ) - config.k8s_node_count = 1 - config.k8s_cleanup_on_exit = True - - original_config = config_module._config - config_module._config = config - - try: - - @cluster( - platform="kubernetes", - auto_provision=True, - cores=4, - memory="16Gi", - gpu=1, # Request 1 GPU - parallel=False, - ) - def gpu_computation(array_size): - """Perform GPU-accelerated computation.""" - import numpy as np - import time - - # Try to use CuPy for GPU acceleration - try: - import cupy as cp - - gpu_available = True - except ImportError: - cp = np # Fallback to NumPy - gpu_available = False - - start_time = time.time() - - # Create large arrays - if gpu_available: - # GPU computation - a_gpu = cp.random.randn(array_size, array_size).astype(cp.float32) - b_gpu = cp.random.randn(array_size, array_size).astype(cp.float32) - - # Matrix multiplication on GPU - c_gpu = cp.matmul(a_gpu, b_gpu) - - # Ensure computation completes - cp.cuda.Stream.null.synchronize() - - result = { - "computation_device": "GPU", - "array_size": array_size, - "result_sum": float(cp.sum(c_gpu)), - "result_mean": float(cp.mean(c_gpu)), - "gpu_memory_used": cp.get_default_memory_pool().used_bytes(), - } - else: - # CPU computation for comparison - a_cpu = np.random.randn(array_size, array_size).astype(np.float32) - b_cpu = np.random.randn(array_size, array_size).astype(np.float32) - - c_cpu = np.matmul(a_cpu, b_cpu) - - result = { - "computation_device": "CPU", - "array_size": array_size, - "result_sum": float(np.sum(c_cpu)), - "result_mean": float(np.mean(c_cpu)), - } - - computation_time = time.time() - start_time - result["computation_time"] = computation_time - - return result - - # Execute GPU computation - result = gpu_computation(1000) - - # Validate results - assert result["array_size"] == 1000 - assert result["computation_time"] > 0 - assert "result_sum" in result - assert "result_mean" in result - - # If GPU was used, should be faster than CPU for large matrices - if result["computation_device"] == "GPU": - assert result["gpu_memory_used"] > 0 - # GPU should complete in reasonable time - assert result["computation_time"] < 10 # seconds - - finally: - config_module._config = original_config From 764398c943f32b534d0eb5c61143f2abc357e7a7 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:42:43 -0400 Subject: [PATCH 13/56] Have the enhanced widget read SUPPORTED_CLUSTER_TYPES Its dropdown carried a third hardcoded copy of the backend list, offering pbs, sge, kubernetes, aws, azure and gcp -- none of which the executor can dispatch any more. Reading the constant is what stops the copies drifting; that is why the constant exists. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/enhanced_notebook_widget.py | 16 +- scripts/check_docs_examples.py | 3 +- tests/real_world/test_field_mapping_fixes.py | 192 ------------------- 3 files changed, 6 insertions(+), 205 deletions(-) delete mode 100755 tests/real_world/test_field_mapping_fixes.py diff --git a/clustrix/enhanced_notebook_widget.py b/clustrix/enhanced_notebook_widget.py index 9dc8cf75..4c92b37a 100644 --- a/clustrix/enhanced_notebook_widget.py +++ b/clustrix/enhanced_notebook_widget.py @@ -11,7 +11,7 @@ except ImportError: IPYTHON_AVAILABLE = False -from .config import ClusterConfig +from .config import ClusterConfig, SUPPORTED_CLUSTER_TYPES from .auth_manager import AuthenticationManager from .validation import ( validate_cluster_auth, @@ -53,18 +53,10 @@ def create_enhanced_cluster_widget( value='

🖥️ Cluster Configuration

' ) + # Read the supported set rather than keeping a third copy of it: this + # list had drifted to offer five backends the executor cannot dispatch. cluster_type = widgets.Dropdown( - options=[ - "local", - "ssh", - "slurm", - "pbs", - "sge", - "kubernetes", - "aws", - "azure", - "gcp", - ], + options=list(SUPPORTED_CLUSTER_TYPES), value=config.cluster_type, description="Cluster Type:", style=style, diff --git a/scripts/check_docs_examples.py b/scripts/check_docs_examples.py index fc977dc8..5e63218a 100644 --- a/scripts/check_docs_examples.py +++ b/scripts/check_docs_examples.py @@ -13,7 +13,8 @@ It doesn't; ``ClusterConfig`` is not re-exported from ``clustrix/__init__.py``. - ``docs/PRICING_API_REFERENCE.md`` and ``docs/PRICING_USER_GUIDE.md`` documented ``clustrix.pricing_clients.performance_monitor`` and - ``.resilience``, both since deleted as unused code. + ``.resilience``; the whole pricing-client tree has since been deleted + along with the cloud backends it served. Per code block: diff --git a/tests/real_world/test_field_mapping_fixes.py b/tests/real_world/test_field_mapping_fixes.py deleted file mode 100755 index 0683db84..00000000 --- a/tests/real_world/test_field_mapping_fixes.py +++ /dev/null @@ -1,192 +0,0 @@ -#!/usr/bin/env python3 -"""Test runner script for field mapping validation tests. - -This script runs the comprehensive field mapping validation tests with -real cloud provider APIs to verify that Issue #59 fixes are working correctly. - -Usage: - python scripts/test_field_mapping_fixes.py [--provider PROVIDER] [--verbose] - -Examples: - python scripts/test_field_mapping_fixes.py # Run all tests - python scripts/test_field_mapping_fixes.py --provider aws # Test only AWS - python scripts/test_field_mapping_fixes.py --verbose # Verbose output -""" - -import argparse -import os -import sys -import subprocess -import logging -from pathlib import Path - -# Configure logging -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" -) -logger = logging.getLogger(__name__) - - -def check_credentials(): - """Check which cloud provider credentials are available.""" - available = {} - - # Check AWS - if os.environ.get("AWS_ACCESS_KEY_ID") and os.environ.get("AWS_SECRET_ACCESS_KEY"): - available["AWS"] = True - logger.info("✅ AWS credentials available") - else: - available["AWS"] = False - logger.warning( - "⚠️ AWS credentials not found (set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)" - ) - - # Check Azure - azure_vars = [ - "AZURE_SUBSCRIPTION_ID", - "AZURE_CLIENT_ID", - "AZURE_CLIENT_SECRET", - "AZURE_TENANT_ID", - ] - if all(os.environ.get(var) for var in azure_vars): - available["Azure"] = True - logger.info("✅ Azure credentials available") - else: - available["Azure"] = False - logger.warning(f"⚠️ Azure credentials not found (set {', '.join(azure_vars)})") - - # Check GCP - if os.environ.get("GCP_PROJECT_ID") and os.environ.get("GCP_SERVICE_ACCOUNT_KEY"): - available["GCP"] = True - logger.info("✅ GCP credentials available") - else: - available["GCP"] = False - logger.warning( - "⚠️ GCP credentials not found (set GCP_PROJECT_ID, GCP_SERVICE_ACCOUNT_KEY)" - ) - - # Check HuggingFace - if os.environ.get("HF_TOKEN"): - available["HuggingFace"] = True - logger.info("✅ HuggingFace credentials available") - else: - available["HuggingFace"] = False - logger.warning("⚠️ HuggingFace credentials not found (set HF_TOKEN)") - - return available - - -def run_field_mapping_tests(provider_filter=None, verbose=False): - """Run the field mapping validation tests.""" - # Get project root - script_dir = Path(__file__).parent - project_root = script_dir.parent - test_file = ( - project_root / "tests" / "real_world" / "test_field_mapping_validation.py" - ) - - if not test_file.exists(): - logger.error(f"Test file not found: {test_file}") - return False - - # Build pytest command - cmd = ["python", "-m", "pytest", str(test_file), "-m", "real_world"] - - if verbose: - cmd.extend(["-v", "-s"]) - else: - cmd.append("-q") - - # Add specific test filter if requested - if provider_filter: - provider_lower = provider_filter.lower() - if provider_lower == "aws": - cmd.append("-k") - cmd.append("aws") - elif provider_lower == "azure": - cmd.append("-k") - cmd.append("azure") - elif provider_lower == "gcp": - cmd.append("-k") - cmd.append("gcp") - elif provider_lower == "huggingface" or provider_lower == "hf": - cmd.append("-k") - cmd.append("huggingface") - elif provider_lower == "lambda": - cmd.append("-k") - cmd.append("lambda") - else: - logger.error(f"Unknown provider: {provider_filter}") - return False - - logger.info(f"Running command: {' '.join(cmd)}") - - # Run tests - try: - result = subprocess.run(cmd, cwd=project_root, capture_output=False) - return result.returncode == 0 - except Exception as e: - logger.error(f"Failed to run tests: {e}") - return False - - -def main(): - """Main entry point.""" - parser = argparse.ArgumentParser( - description="Test field mapping validation fixes for Issue #59", - formatter_class=argparse.RawDescriptionHelpFormatter, - epilog=""" -Examples: - %(prog)s # Run all field mapping tests - %(prog)s --provider aws # Test only AWS field mapping - %(prog)s --provider azure # Test only Azure field mapping - %(prog)s --provider gcp # Test only GCP field mapping - %(prog)s --provider huggingface # Test only HuggingFace field mapping - %(prog)s --verbose # Run with verbose output - """, - ) - - parser.add_argument( - "--provider", - help="Test only specified provider (aws, azure, gcp, huggingface, lambda)", - choices=["aws", "azure", "gcp", "huggingface", "hf", "lambda"], - ) - parser.add_argument( - "--verbose", "-v", action="store_true", help="Enable verbose test output" - ) - - args = parser.parse_args() - - logger.info("🧪 Field Mapping Validation Test Runner") - logger.info("=" * 50) - - # Check credentials - logger.info("Checking available credentials...") - available_creds = check_credentials() - - available_count = sum(1 for available in available_creds.values() if available) - if available_count == 0: - logger.error("❌ No cloud provider credentials found!") - logger.error("Please set credentials for at least one provider to run tests.") - logger.error("See test file docstring for required environment variables.") - return 1 - - logger.info(f"Found credentials for {available_count} provider(s)") - logger.info("") - - # Run tests - logger.info("Running field mapping validation tests...") - success = run_field_mapping_tests(args.provider, args.verbose) - - if success: - logger.info("🎉 All field mapping tests passed!") - logger.info("Issue #59 field mapping fixes are working correctly.") - return 0 - else: - logger.error("❌ Some field mapping tests failed!") - logger.error("Check the output above for details.") - return 1 - - -if __name__ == "__main__": - sys.exit(main()) From 2b04872cfd148364f46175af363236b4eea1a6a0 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:42:50 -0400 Subject: [PATCH 14/56] Docs: record backend + cost-monitoring removal in README, CLAUDE.md, CHANGELOG README and CLAUDE.md now carry a single 'not currently supported' note next to the supported-backends table, naming tracking issues #140-#146 and saying the backends are planned for a future update without promising a date. The Cloud Providers and Cost Monitoring sections are replaced by that note. CHANGELOG records both removals under 0.2.0 as BREAKING, including the five cost-monitoring functions that no longer exist. The 'Implemented but unverified' section is gone: those backends no longer exist in the code. docs/aws/ is kept -- scripts/aws/ cleanup tooling still needs those IAM permissions -- with a banner marking each guide historical. --- CHANGELOG.md | 63 ++++++++--- CLAUDE.md | 34 ++++-- README.md | 143 ++++++++++++------------ docs/aws/ADD_CUSTOM_EKS_POLICY.md | 9 ++ docs/aws/AWS_CONSOLE_QUICK_STEPS.md | 9 ++ docs/aws/AWS_EKS_TROUBLESHOOTING.md | 9 ++ docs/aws/AWS_PERMISSIONS_SETUP_GUIDE.md | 9 ++ 7 files changed, 179 insertions(+), 97 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afcaacc2..efc3f980 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ landed on `master`. The guiding rule for this file: a capability is only listed as working if it has been exercised against the real thing. Anything implemented but unproven is -listed under **Implemented but unverified**, and stays there until someone runs -it for real. +labelled as such and stays labelled until someone runs it for real — and as of +0.2.0, anything that stayed unproven was removed rather than shipped. ## [0.2.0] — unreleased @@ -147,8 +147,8 @@ backend. default to a dry run and refuse to touch anything not tagged `clustrix:managed=true`. (The originals, recovered from git history, deleted every NAT gateway and VPC in the account with no ownership check at all.) -- Kubernetes auto-provisioning documentation, and a usage-patterns tutorial. Every - example in the docs is executed by `scripts/check_docs_examples.py`. +- A usage-patterns tutorial. Every example in the docs is executed by + `scripts/check_docs_examples.py`. - `clustrix.config.SUPPORTED_CLUSTER_TYPES` as the single source of truth. The CLI's own list had drifted and omitted `huggingface` entirely, so a working backend could not be selected from the command line. @@ -162,15 +162,52 @@ backend. - Deleted 1,837 lines of genuinely orphaned modules. (The issue that requested this claimed ~5,100 lines and named five files that do not exist on `master`.) -### Implemented but unverified - -These have code paths and error handling, but no one has run them against real -hardware. They are not claimed to work. - -- PBS and SGE — never run against a real scheduler. -- Kubernetes execution — never run against a real cluster. -- AWS, GCP, Azure and Lambda VM backends — no cloud job has been shown to run end - to end. +### Removed — unverified backends (BREAKING) + +Seven execution backends were implemented in full and not one of them had ever +been shown to run a job end to end against real hardware. Rather than keep +publishing them as if they worked, they were removed. `SUPPORTED_CLUSTER_TYPES` +is now exactly `local`, `ssh`, `slurm`, `huggingface` — the four backends that +have each run a real job and returned the right answer. Anything else raises +`ValueError: Unsupported cluster type` at submit time. + +Each removed backend has a tracking issue and is planned for a future release. +The gate for restoring one is the gate the surviving four already passed: a +real job, on real hardware, whose result comes back and is checked in as +evidence. No date is promised. + +| Removed | Issue | What it was | +|-|-|-| +| PBS | [#140](https://github.com/ContextLab/clustrix/issues/140) | `cluster_type="pbs"` — the PBS/Torque scheduler | +| SGE | [#141](https://github.com/ContextLab/clustrix/issues/141) | `cluster_type="sge"` — Sun/Son of Grid Engine | +| Kubernetes | [#142](https://github.com/ContextLab/clustrix/issues/142) | `cluster_type="kubernetes"`, the `k8s_*` settings, cluster auto-provisioning | +| AWS | [#143](https://github.com/ContextLab/clustrix/issues/143) | `provider="aws"` — EC2 and EKS | +| GCP | [#144](https://github.com/ContextLab/clustrix/issues/144) | `provider="gcp"` — Google Compute Engine | +| Azure | [#145](https://github.com/ContextLab/clustrix/issues/145) | `provider="azure"` — Azure VMs | +| Lambda Cloud | [#146](https://github.com/ContextLab/clustrix/issues/146) | `provider="lambda"` — Lambda Labs GPU cloud | + +The HuggingFace **Spaces** provider (`provider="huggingface"`) was removed with +them. This is a different thing from `cluster_type="huggingface"`, which is +HuggingFace **Jobs**: that backend is verified end to end and is fully +supported. + +### Removed — cost monitoring API (BREAKING) + +The cost monitoring and cloud pricing API is gone, along with all five of its +public functions: + +- `cost_tracking_decorator` +- `get_cost_monitor` +- `start_cost_monitoring` +- `generate_cost_report` +- `get_pricing_info` + +Importing any of them from `clustrix` now raises `ImportError`. They priced the +cloud VM backends, so with those backends removed the API had nothing left to +price. Use your provider's own pricing calculator instead. + +`scripts/aws/` is unaffected — it is operator cleanup tooling, not an execution +backend, and it stays. ### Known limitations diff --git a/CLAUDE.md b/CLAUDE.md index 176fd428..8ecd9f0e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -14,11 +14,22 @@ Backends, and how far each is actually proven — keep this honest, it is the fi | `ssh` | Verified end to end against a real GPU host | | `huggingface` | Verified end to end against real HF Jobs containers | | `local` | Runs in-process via `local_executor.py` | -| `pbs`, `sge` | Implemented, **not** verified against real hardware | -| `kubernetes` | Implemented, **not** verified against a real cluster | -| AWS / GCP / Azure / Lambda VMs | **Unverified.** No cloud job has been shown to run end to end. | -Evidence for the verified rows is regenerated by `scripts/verify_cluster_usecases.py` and committed under `docs/evidence/`. +That is the whole list — `clustrix.config.SUPPORTED_CLUSTER_TYPES`. Evidence for the verified rows is regenerated by `scripts/verify_cluster_usecases.py` and committed under `docs/evidence/`. + +**Backends that are NOT currently supported.** `pbs`, `sge`, `kubernetes` and the AWS / GCP / Azure / Lambda Cloud VM providers were removed in v0.2.0 because none of them had ever been shown to run a job end to end. The cost monitoring and cloud pricing API (`cost_tracking_decorator`, `get_cost_monitor`, `start_cost_monitoring`, `generate_cost_report`, `get_pricing_info`) went with them, as did the HuggingFace **Spaces** provider — which is a different thing from `cluster_type="huggingface"` (HuggingFace **Jobs**), and that one stays. Each removed backend is planned for a future update and has a tracking issue; do not re-document any of them as working: + +| Removed | Issue | +|-|-| +| PBS | [#140](https://github.com/ContextLab/clustrix/issues/140) | +| SGE | [#141](https://github.com/ContextLab/clustrix/issues/141) | +| Kubernetes | [#142](https://github.com/ContextLab/clustrix/issues/142) | +| AWS | [#143](https://github.com/ContextLab/clustrix/issues/143) | +| GCP | [#144](https://github.com/ContextLab/clustrix/issues/144) | +| Azure | [#145](https://github.com/ContextLab/clustrix/issues/145) | +| Lambda Cloud | [#146](https://github.com/ContextLab/clustrix/issues/146) | + +`scripts/aws/` stays: it is operator cleanup tooling, not an execution backend. ## Development Commands @@ -27,8 +38,8 @@ Evidence for the verified rows is regenerated by `scripts/verify_cluster_usecase # Install package with development dependencies pip install -e ".[dev]" -# Install with Kubernetes support -pip install -e ".[kubernetes,dev]" +# Install with the Jupyter widget +pip install -e ".[widget,dev]" ``` ### Code Quality @@ -61,10 +72,8 @@ pytest --cov=clustrix # Run tests with coverage 2. **ClusterExecutor** (`clustrix/executor_core.py`): Central execution engine. Note that `clustrix/executor.py` is a 39-line backward-compatibility shim that re-exports it; the implementation is split across: - `executor_core.py` — the `ClusterExecutor` class, dispatch, result retrieval and verification - `executor_connections.py` — SSH/SFTP connection management via Paramiko - - `executor_schedulers.py` — SLURM, PBS, SGE submission + - `executor_schedulers.py` — SLURM and SSH submission - `executor_scheduler_status.py` — scheduler status polling and error extraction - - `executor_kubernetes.py` — Kubernetes operations - - `executor_cloud.py` — cloud provider workflows - `hf_jobs.py` — the HuggingFace Jobs backend (`HFJobsManager`) 3. **Configuration System** (`clustrix/config.py`): Singleton configuration management supporting: @@ -128,12 +137,13 @@ VENV1 holds clustrix's own serialization dependencies; VENV2 holds the user's re ### Adding New Cluster Type Support -There is **no `ClusterType` enum** — `ClusterConfig.cluster_type` is a plain `str`. The supported values are `local`, `ssh`, `slurm`, `pbs`, `sge`, `kubernetes`, `huggingface`. +There is **no `ClusterType` enum** — `ClusterConfig.cluster_type` is a plain `str`. The supported values are `local`, `ssh`, `slurm`, `huggingface`, declared once in `clustrix.config.SUPPORTED_CLUSTER_TYPES`. -1. Add the value wherever the valid set is declared (`clustrix/cli.py`'s `click.Choice`, the widget's dropdown, and the type comment in `config.py`) — these must not drift apart +1. Add the value to `SUPPORTED_CLUSTER_TYPES`; the CLI's `click.Choice` and the widget's dropdown both read that tuple, so they cannot drift apart 2. Implement submission in the appropriate `executor_*.py` module and dispatch from `ClusterExecutor` in `executor_core.py` 3. Add status checking to `get_job_status` / `executor_scheduler_status.py` -4. Update job script generation in `utils.py` if needed — reuse `job_execution_lines()` rather than writing a fourth variant +4. Update job script generation in `utils.py` if needed — reuse `job_execution_lines()` rather than writing another variant +5. **Do not mark it supported until a real job has run on real hardware and the evidence is committed.** That gate is why `pbs`, `sge`, `kubernetes` and the cloud VM providers were removed (#140–#146). ### Using Filesystem Utilities ```python diff --git a/README.md b/README.md index e76b0402..0f2e7b8e 100755 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Clustrix is a Python package that enables seamless distributed computing on clus - **Simple Decorator Interface**: Just add `@cluster` to any function - **Automated SSH Key Setup**: Create and deploy SSH keys to enable secure passwordless authentication with one click or API call - **Interactive Jupyter Widget**: `%%remote` magic command with GUI configuration manager -- **Multiple Cluster Backends**: SLURM, SSH and HuggingFace Jobs are verified working; PBS, SGE and Kubernetes are implemented but untested (see [Supported Cluster Types](#supported-cluster-types)) +- **Multiple Cluster Backends**: local, SSH, SLURM and HuggingFace Jobs -- every backend Clustrix ships has been run end to end (see [Supported Cluster Types](#supported-cluster-types)) - **Unified Filesystem Utilities**: Work with files seamlessly across local and remote clusters - **Automatic Dependency Management**: Captures and replicates your exact Python environment - **Loop Parallelization**: distributes a loop across nodes when its body has no @@ -126,21 +126,20 @@ variables, module loads and pre-execution commands: ##### What the widget covers -The cluster type dropdown offers `local`, `ssh`, `slurm`, `pbs`, `sge`, -`kubernetes` and `huggingface`. +The cluster type dropdown offers `local`, `ssh`, `slurm` and `huggingface` -- +the contents of `clustrix.config.SUPPORTED_CLUSTER_TYPES`. -- `ssh`, `slurm`, `pbs`, `sge` show the connection section: host, port, - username, SSH key file, password, remote work directory, an environment - variable to read the password from, and an "Auto setup SSH keys" button. +- `ssh` and `slurm` show the connection section: host, port, username, SSH key + file, password, remote work directory, an environment variable to read the + password from, and an "Auto setup SSH keys" button. - `huggingface` shows namespace, flavor, token, and an "Allow paid GPU flavors" checkbox. GPU flavors bill by the second, so that box has to be ticked before one is accepted. -- `kubernetes` shows a Kubernetes section: namespace, image, service account and - image pull policy. The remaining `k8s_*` settings (node count, region, - provider, auto-provisioning) are config-file or `clustrix.configure()` only. +- `local` needs no connection settings at all. -There are no AWS, GCP, Azure or Lambda Cloud entries: those backends are -unverified (see [Cloud Providers](#cloud-providers)). +There are no PBS, SGE, Kubernetes, AWS, GCP, Azure or Lambda Cloud entries, and +no `k8s_*` settings: those backends are not currently supported (see +[Backends that are not currently supported](#backends-that-are-not-currently-supported)). ##### Using the widget @@ -184,7 +183,7 @@ environment_variables: ``` `remote_work_dir` defaults to `~/.clustrix/jobs`. It must be on a filesystem -the compute node can see: on SLURM, PBS and SGE each node has its own `/tmp`, +the compute node can see: on SLURM each node has its own `/tmp`, so an environment built on the login node is simply absent at run time and the job dies with exit 127 before writing any diagnostics. A home directory or a shared scratch path (as above) both work; `/tmp` does not. @@ -219,8 +218,8 @@ Open the widget with the `%%remote` magic: %%remote ``` -1. Choose a remote cluster type (`ssh`, `slurm`, `pbs` or `sge`) so the - connection section appears +1. Choose a remote cluster type (`ssh` or `slurm`) so the connection section + appears 2. Enter your cluster hostname and username 3. Enter your password 4. Click "Auto setup SSH keys" @@ -346,34 +345,16 @@ def process_datasets(config): - `cluster_du()` - Directory usage information - `cluster_count_files()` - Count files matching pattern -### Cost Monitoring +### Cost monitoring: removed in v0.2.0 -Clustrix includes cost estimation for cloud providers. This is independent of -the (broken) cloud execution backends: it queries pricing and reports local -resource usage, and never submits a job. - -```python -from clustrix import get_cost_monitor - -monitor = get_cost_monitor('gcp') - -cost_estimate = monitor.estimate_cost('n2-standard-4', hours_used=2.0) -print(f"Estimated cost: ${cost_estimate.estimated_cost:.2f}") - -pricing = monitor.get_pricing_info() # {instance_type: hourly_usd} - -usage = monitor.get_resource_usage() # CPU/memory/GPU on this machine -recommendations = monitor.get_cost_optimization_recommendations( - usage, cost_estimate -) -``` - -`get_cost_optimization_recommendations()` takes the usage and the estimate as -positional arguments; calling it with none raises `TypeError`. - -Providers with a cost monitor: **AWS**, **Google Cloud**, **Azure**, **Lambda -Cloud**. Where a live pricing API is unavailable the monitor falls back to a -hardcoded table and says so on stderr. +The cost monitoring and cloud pricing API is gone, along with all five of its +public functions -- `cost_tracking_decorator`, `get_cost_monitor`, +`start_cost_monitoring`, `generate_cost_report` and `get_pricing_info`. +Importing any of them now raises `ImportError`. They priced the cloud VM +backends, and those backends were removed too (see +[Backends that are not currently supported](#backends-that-are-not-currently-supported)), +so the API had nothing left to price. Use your provider's own pricing +calculator instead. ### Custom Resource Requirements @@ -425,12 +406,12 @@ clustrix.configure(cluster_type='ssh', cluster_host='server.example.com') # HuggingFace Jobs (no host: work is submitted over an HTTP API) clustrix.configure(cluster_type='huggingface', hf_namespace='my-org') -# PBS and SGE clusters (implemented, not verified against real hardware) -clustrix.configure(cluster_type='pbs', cluster_host='pbs.example.com') -clustrix.configure(cluster_type='sge', cluster_host='sge.example.com') +# Local execution, no cluster needed +clustrix.configure(cluster_type='local') -# Kubernetes (implemented, not verified against a real cluster) -clustrix.configure(cluster_type='kubernetes') +# Those four are the whole list. Anything else -- 'pbs', 'sge', 'kubernetes' +# -- raises ValueError: Unsupported cluster type. See "Backends that are not +# currently supported" below. ``` ### HuggingFace Jobs @@ -494,28 +475,37 @@ container prints an HMAC-SHA256 of the bytes it emitted, and clustrix refuses to unpickle anything whose tag does not verify. The SSH and scheduler paths verify their results the same way. -### Cloud Providers +### Backends that are not currently supported -The `provider=` argument to `@cluster` (`'aws'`, `'gcp'`, `'azure'`, -`'lambda'`, `'huggingface'`) routes to the AWS EC2, Google Compute Engine, -Azure VM and Lambda Cloud backends. **None of them has been shown to run a job -end to end.** Until recently the path could not have run at all: the serializer -writes the function under a `"function"` key while the remote bootstrap read -`"func"`, so every cloud job died with a `KeyError` on its first line. That was -fixed (issue #119), but nothing has since demonstrated a completed cloud job, -and `scripts/collect_execution_evidence.py` does not cover these backends. +Clustrix once shipped seven more execution backends. All seven were implemented +in full, and not one had ever been shown to run a job end to end against real +hardware. Rather than keep publishing them as if they worked, they were removed +in v0.2.0. -Treat the cloud tutorials in the documentation as a description of the intended -interface rather than a record of something that has been run. The notebook -widget does not offer these as cluster types. +They are planned for a future update. Each has a tracking issue, and the gate +for restoring one is the gate the surviving four already passed: a real job, on +real hardware, whose result comes back and is checked in as evidence. No date is +promised. -The pricing and cost-estimation clients for those providers (see -[Cost Monitoring](#cost-monitoring)) are separate code and do work; they query -provider pricing APIs and do not submit jobs. - -`cluster_type='huggingface'` (HuggingFace Jobs, above) is a different thing -from `provider='huggingface'` (the HuggingFace Spaces provider, which never -satisfied the dispatch interface). Use the former. +| Not supported | Issue | What it was | +|-|-|-| +| PBS | [#140](https://github.com/ContextLab/clustrix/issues/140) | `cluster_type="pbs"` -- the PBS/Torque scheduler | +| SGE | [#141](https://github.com/ContextLab/clustrix/issues/141) | `cluster_type="sge"` -- Sun/Son of Grid Engine | +| Kubernetes | [#142](https://github.com/ContextLab/clustrix/issues/142) | `cluster_type="kubernetes"`, the `k8s_*` settings, cluster auto-provisioning | +| AWS | [#143](https://github.com/ContextLab/clustrix/issues/143) | `provider="aws"` -- EC2 and EKS | +| GCP | [#144](https://github.com/ContextLab/clustrix/issues/144) | `provider="gcp"` -- Google Compute Engine | +| Azure | [#145](https://github.com/ContextLab/clustrix/issues/145) | `provider="azure"` -- Azure VMs | +| Lambda Cloud | [#146](https://github.com/ContextLab/clustrix/issues/146) | `provider="lambda"` -- Lambda Labs GPU cloud | + +The HuggingFace **Spaces** provider (`provider="huggingface"`) went with them. +That is a different thing from `cluster_type="huggingface"`, which is +HuggingFace **Jobs** -- verified end to end and fully supported. The cost +monitoring and cloud pricing API was removed too. + +**What to do instead.** For a rented GPU without owning hardware, use +`cluster_type="huggingface"`. For a machine you brought up yourself through +your provider's own console or CLI, point `cluster_type="ssh"` at it. For a +batch allocation, `cluster_type="slurm"`. All three are verified end to end. ## Command Line Interface @@ -595,9 +585,11 @@ result = my_function(5) | `ssh` | Verified. Direct execution over SSH with no scheduler; a real job ran on an 8-GPU host. | | `huggingface` | Verified. HuggingFace Jobs; a real job ran in a container. | | `local` | Runs in local processes. Used for development and the fast tests. | -| `pbs` | Implemented, **not verified**. All four of SLURM/PBS/SGE/SSH now share one environment-setup path, so PBS builds the same two-venv environment SLURM does -- but no PBS job has been run against a real scheduler. | -| `sge` | Implemented, **not verified**. Same caveat as PBS. | -| AWS / GCP / Azure / Lambda VM backends | **Unverified.** No cloud job has been shown to run end to end. See [Cloud Providers](#cloud-providers). | + +Those four are the whole list -- the contents of +`clustrix.config.SUPPORTED_CLUSTER_TYPES`. PBS, SGE, Kubernetes and the +AWS / GCP / Azure / Lambda Cloud VM backends are **not currently supported**; +see [Backends that are not currently supported](#backends-that-are-not-currently-supported). The three "Verified" rows are the backends exercised by `scripts/collect_execution_evidence.py`, which submits a genuine job to each @@ -628,8 +620,7 @@ clustrix/ │ ├── filesystem.py # Cross-cluster filesystem utilities │ ├── utils.py # Core utilities and job management │ ├── cli.py # Command line interface -│ ├── kubernetes/ # Kubernetes providers (AWS, GCP, Azure, etc.) -│ └── pricing_clients/ # Cost monitoring integrations +│ └── hf_jobs.py # HuggingFace Jobs backend ├── tests/ # Test suite organized by category │ ├── unit/ # Fast unit tests (run in CI) │ ├── integration/ # Provisions REAL billable AWS resources; @@ -866,8 +857,16 @@ pre-commit run --all-files For more detailed information on specific topics, see the organized documentation in the `docs/` directory: -### Cloud Provider Setup -- **[AWS Setup Guide](docs/aws/AWS_PERMISSIONS_SETUP_GUIDE.md)** - Complete AWS permissions configuration +### AWS operator tooling + +Clustrix has no AWS *execution* backend -- see +[Backends that are not currently supported](#backends-that-are-not-currently-supported). +`scripts/aws/` is separate: cleanup and teardown utilities for AWS resources +tagged `clustrix:managed=true`, kept so that anything left behind by the +removed provisioning code can still be reclaimed. The IAM guides below are +historical records of the permissions that tooling needed. + +- **[AWS Setup Guide](docs/aws/AWS_PERMISSIONS_SETUP_GUIDE.md)** - AWS permissions configuration - **[AWS Console Quick Steps](docs/aws/AWS_CONSOLE_QUICK_STEPS.md)** - Fast AWS setup guide - **[AWS EKS Policy Setup](docs/aws/ADD_CUSTOM_EKS_POLICY.md)** - EKS-specific policy configuration - **[AWS EKS Troubleshooting](docs/aws/AWS_EKS_TROUBLESHOOTING.md)** - Common AWS access issues diff --git a/docs/aws/ADD_CUSTOM_EKS_POLICY.md b/docs/aws/ADD_CUSTOM_EKS_POLICY.md index 8d1aa083..1d3f22e4 100644 --- a/docs/aws/ADD_CUSTOM_EKS_POLICY.md +++ b/docs/aws/ADD_CUSTOM_EKS_POLICY.md @@ -1,5 +1,14 @@ # Add Custom EKS Policy for Clustrix User +> **Historical.** Clustrix no longer has an AWS or Kubernetes execution +> backend -- both were removed in v0.2.0 because neither had ever been shown to +> run a job end to end (tracking issues +> [#142](https://github.com/ContextLab/clustrix/issues/142) and +> [#143](https://github.com/ContextLab/clustrix/issues/143); they are planned +> for a future update). This guide is kept as a record of the IAM permissions +> that provisioning needed, and because the `scripts/aws/` cleanup utilities +> still need AWS credentials to reclaim anything left behind. + ## The Problem The AWS managed EKS policies (like `AmazonEKSClusterPolicy`) are designed for service roles, not IAM users. They don't grant permissions like `eks:ListClusters` or `eks:CreateCluster` that users need. diff --git a/docs/aws/AWS_CONSOLE_QUICK_STEPS.md b/docs/aws/AWS_CONSOLE_QUICK_STEPS.md index 5c77fda2..a6abc2b3 100644 --- a/docs/aws/AWS_CONSOLE_QUICK_STEPS.md +++ b/docs/aws/AWS_CONSOLE_QUICK_STEPS.md @@ -1,5 +1,14 @@ # AWS Console Quick Steps - Add Permissions to Clustrix User +> **Historical.** Clustrix no longer has an AWS or Kubernetes execution +> backend -- both were removed in v0.2.0 because neither had ever been shown to +> run a job end to end (tracking issues +> [#142](https://github.com/ContextLab/clustrix/issues/142) and +> [#143](https://github.com/ContextLab/clustrix/issues/143); they are planned +> for a future update). This guide is kept as a record of the IAM permissions +> that provisioning needed, and because the `scripts/aws/` cleanup utilities +> still need AWS credentials to reclaim anything left behind. + ## 🚀 Quick Steps (5 minutes) ### 1. Open this link in your browser: diff --git a/docs/aws/AWS_EKS_TROUBLESHOOTING.md b/docs/aws/AWS_EKS_TROUBLESHOOTING.md index 50038898..5c060c86 100644 --- a/docs/aws/AWS_EKS_TROUBLESHOOTING.md +++ b/docs/aws/AWS_EKS_TROUBLESHOOTING.md @@ -1,5 +1,14 @@ # AWS EKS Access Troubleshooting Guide +> **Historical.** Clustrix no longer has an AWS or Kubernetes execution +> backend -- both were removed in v0.2.0 because neither had ever been shown to +> run a job end to end (tracking issues +> [#142](https://github.com/ContextLab/clustrix/issues/142) and +> [#143](https://github.com/ContextLab/clustrix/issues/143); they are planned +> for a future update). This guide is kept as a record of the IAM permissions +> that provisioning needed, and because the `scripts/aws/` cleanup utilities +> still need AWS credentials to reclaim anything left behind. + ## Current Issue Despite having all required IAM policies attached, the Clustrix user cannot perform EKS operations. diff --git a/docs/aws/AWS_PERMISSIONS_SETUP_GUIDE.md b/docs/aws/AWS_PERMISSIONS_SETUP_GUIDE.md index 69e40a00..50c3202e 100644 --- a/docs/aws/AWS_PERMISSIONS_SETUP_GUIDE.md +++ b/docs/aws/AWS_PERMISSIONS_SETUP_GUIDE.md @@ -1,5 +1,14 @@ # AWS IAM Permissions Setup Guide for Clustrix +> **Historical.** Clustrix no longer has an AWS or Kubernetes execution +> backend -- both were removed in v0.2.0 because neither had ever been shown to +> run a job end to end (tracking issues +> [#142](https://github.com/ContextLab/clustrix/issues/142) and +> [#143](https://github.com/ContextLab/clustrix/issues/143); they are planned +> for a future update). This guide is kept as a record of the IAM permissions +> that provisioning needed, and because the `scripts/aws/` cleanup utilities +> still need AWS credentials to reclaim anything left behind. + ## Quick Setup via AWS Console (Recommended) ### Step 1: Open IAM Console From a39fbf912998cd115ba258312c0c5997cbbd3998 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:43:20 -0400 Subject: [PATCH 15/56] Strip PBS/SGE/Kubernetes branches from mixed real-world test helpers cluster_job_validator: ClusterType now only SLURM and SSH; the qstat and kubectl branches are gone. test_cluster_job_system: the required-test-file list no longer asserts the existence of the deleted pbs/sge/kubernetes submission tests. run_cluster_job_tests: availability probe and --cluster choices reduced to slurm/ssh. test_advanced_schedulers_comprehensive: PBS and SGE submission tests removed, remaining tests are SLURM-only. --- tests/real_world/cluster_job_validator.py | 119 +--------------- .../run_cluster_job_tests.py | 32 +---- .../test_advanced_schedulers_comprehensive.py | 131 ++---------------- tests/real_world/test_cluster_job_system.py | 3 - 4 files changed, 13 insertions(+), 272 deletions(-) diff --git a/tests/real_world/cluster_job_validator.py b/tests/real_world/cluster_job_validator.py index e962d5cf..2385d8c1 100644 --- a/tests/real_world/cluster_job_validator.py +++ b/tests/real_world/cluster_job_validator.py @@ -2,7 +2,7 @@ Comprehensive cluster job monitoring and validation framework. This module provides tools for monitoring and validating real cluster job -submissions across different cluster types (SLURM, PBS, SGE, Kubernetes, SSH). +submissions across the supported cluster types (SLURM, SSH). """ import time @@ -25,9 +25,6 @@ class ClusterType(Enum): """Supported cluster types.""" SLURM = "slurm" - PBS = "pbs" - SGE = "sge" - KUBERNETES = "kubernetes" SSH = "ssh" @@ -337,24 +334,6 @@ def _check_job_exists(self, job_id: str) -> bool: ) return result.returncode == 0 and result.stdout.strip() != "" - elif self.cluster_type == ClusterType.PBS: - result = subprocess.run( - ["qstat", job_id], capture_output=True, text=True - ) - return result.returncode == 0 - - elif self.cluster_type == ClusterType.SGE: - result = subprocess.run( - ["qstat", "-j", job_id], capture_output=True, text=True - ) - return result.returncode == 0 - - elif self.cluster_type == ClusterType.KUBERNETES: - result = subprocess.run( - ["kubectl", "get", "job", job_id], capture_output=True, text=True - ) - return result.returncode == 0 - elif self.cluster_type == ClusterType.SSH: # For SSH, check if process is running result = subprocess.run( @@ -386,37 +365,6 @@ def _get_job_details(self, job_id: str) -> Dict[str, Any]: key, value = line.split("=", 1) details[key.strip()] = value.strip() - elif self.cluster_type == ClusterType.PBS: - result = subprocess.run( - ["qstat", "-f", job_id], capture_output=True, text=True - ) - if result.returncode == 0: - # Parse PBS job details - for line in result.stdout.split("\n"): - if "=" in line and not line.startswith("Job Id:"): - key, value = line.split("=", 1) - details[key.strip()] = value.strip() - - elif self.cluster_type == ClusterType.SGE: - result = subprocess.run( - ["qstat", "-j", job_id], capture_output=True, text=True - ) - if result.returncode == 0: - # Parse SGE job details - for line in result.stdout.split("\n"): - if ":" in line: - key, value = line.split(":", 1) - details[key.strip()] = value.strip() - - elif self.cluster_type == ClusterType.KUBERNETES: - result = subprocess.run( - ["kubectl", "describe", "job", job_id, "-o", "json"], - capture_output=True, - text=True, - ) - if result.returncode == 0: - details = json.loads(result.stdout) - except Exception as e: self.logger.error(f"Error getting job details for {job_id}: {e}") @@ -457,61 +405,6 @@ def _get_job_status(self, job_id: str) -> JobStatus: elif "FAILED" in status: return JobStatus.FAILED - elif self.cluster_type == ClusterType.PBS: - result = subprocess.run( - ["qstat", job_id], capture_output=True, text=True - ) - if result.returncode == 0: - lines = result.stdout.strip().split("\n") - if len(lines) > 1: - status = lines[1].split()[4] # Status column - if status == "Q": - return JobStatus.PENDING - elif status == "R": - return JobStatus.RUNNING - elif status == "C": - return JobStatus.COMPLETED - elif status == "E": - return JobStatus.FAILED - - elif self.cluster_type == ClusterType.SGE: - result = subprocess.run( - ["qstat", "-j", job_id], capture_output=True, text=True - ) - if result.returncode == 0: - if "job_state" in result.stdout: - for line in result.stdout.split("\n"): - if "job_state" in line: - status = line.split(":")[1].strip() - if status == "qw": - return JobStatus.PENDING - elif status == "r": - return JobStatus.RUNNING - elif status == "t": - return JobStatus.COMPLETED - - elif self.cluster_type == ClusterType.KUBERNETES: - result = subprocess.run( - [ - "kubectl", - "get", - "job", - job_id, - "-o", - "jsonpath='{.status.conditions[0].type}'", - ], - capture_output=True, - text=True, - ) - if result.returncode == 0: - status = result.stdout.strip().strip("'") - if status == "Complete": - return JobStatus.COMPLETED - elif status == "Failed": - return JobStatus.FAILED - else: - return JobStatus.RUNNING - return JobStatus.UNKNOWN except Exception as e: @@ -607,16 +500,6 @@ def _get_job_output_files(self, job_id: str) -> Dict[str, str]: files["stdout"] = f"slurm-{job_id}.out" files["stderr"] = f"slurm-{job_id}.err" - elif self.cluster_type == ClusterType.PBS: - # PBS creates .o and .e files - files["stdout"] = f"{job_id}.o{job_id}" - files["stderr"] = f"{job_id}.e{job_id}" - - elif self.cluster_type == ClusterType.SGE: - # SGE creates .o and .e files - files["stdout"] = f"{job_id}.o{job_id}" - files["stderr"] = f"{job_id}.e{job_id}" - # Add clustrix-specific result files files["result"] = f"result_{job_id}.pkl" files["error"] = f"error_{job_id}.pkl" diff --git a/tests/real_world/cluster_validation/run_cluster_job_tests.py b/tests/real_world/cluster_validation/run_cluster_job_tests.py index 55dc4dc7..d5ed97c9 100644 --- a/tests/real_world/cluster_validation/run_cluster_job_tests.py +++ b/tests/real_world/cluster_validation/run_cluster_job_tests.py @@ -71,36 +71,8 @@ def check_cluster_availability(self) -> Dict[str, bool]: f" {status} SLURM: {'Available' if availability['slurm'] else 'Not available'}" ) - # Check PBS (use SSH credentials) - ssh_creds = self.credential_manager.get_ssh_credentials() - availability["pbs"] = ssh_creds is not None - status = "✅" if availability["pbs"] else "❌" - print( - f" {status} PBS: {'Available' if availability['pbs'] else 'Not available'}" - ) - - # Check SGE (use SSH credentials) - availability["sge"] = ssh_creds is not None - status = "✅" if availability["sge"] else "❌" - print( - f" {status} SGE: {'Available' if availability['sge'] else 'Not available'}" - ) - - # Check Kubernetes - try: - result = subprocess.run( - ["kubectl", "cluster-info"], capture_output=True, text=True, timeout=10 - ) - availability["kubernetes"] = result.returncode == 0 - except (subprocess.TimeoutExpired, FileNotFoundError): - availability["kubernetes"] = False - - status = "✅" if availability["kubernetes"] else "❌" - print( - f" {status} Kubernetes: {'Available' if availability['kubernetes'] else 'Not available'}" - ) - # Check SSH + ssh_creds = self.credential_manager.get_ssh_credentials() availability["ssh"] = ssh_creds is not None status = "✅" if availability["ssh"] else "❌" print( @@ -475,7 +447,7 @@ def main(): parser = argparse.ArgumentParser(description="Run cluster job submission tests") parser.add_argument( "--cluster", - choices=["slurm", "pbs", "sge", "kubernetes", "ssh", "all"], + choices=["slurm", "ssh", "all"], default="all", help="Cluster type to test", ) diff --git a/tests/real_world/test_advanced_schedulers_comprehensive.py b/tests/real_world/test_advanced_schedulers_comprehensive.py index b7706821..e96a1c0a 100644 --- a/tests/real_world/test_advanced_schedulers_comprehensive.py +++ b/tests/real_world/test_advanced_schedulers_comprehensive.py @@ -1,23 +1,20 @@ """ Comprehensive real-world advanced scheduler validation tests. -This module tests advanced scheduler integration (PBS, SGE, specialized schedulers), +This module tests advanced scheduler integration beyond basic job submission, addressing Phase 5 of Issue #63 external service validation. Tests cover: -- PBS (Portable Batch System) job submission and monitoring -- SGE (Sun Grid Engine) job submission and queue management - Advanced SLURM features and queue specifications -- Hybrid scheduler environments -- Scheduler-specific resource management +- Scheduler queue/partition inspection +- Scheduler job monitoring +- Scheduler-specific resource management and environment variables NO MOCK TESTS - Only real scheduler integration testing. -Supports multiple scheduler types: -- PBS Pro/Torque -- SGE/OGE (Open Grid Engine) -- Advanced SLURM configurations -- LSF (Load Sharing Facility) if available +Scope note: this file previously also covered PBS and SGE. Both backends were +removed from clustrix because they were never verified against real hardware, +so their tests were deleted rather than left asserting against dead code. """ import pytest @@ -62,10 +59,7 @@ def get_scheduler_credentials(scheduler_type: str) -> Optional[Dict[str, str]]: def check_scheduler_available(scheduler_type: str, creds: Dict[str, str]) -> bool: """Check if a scheduler is available on the target cluster.""" scheduler_commands = { - "pbs": "qstat --version", - "sge": "qstat -help", "slurm": "sinfo --version", - "lsf": "bsub -V", } if scheduler_type not in scheduler_commands: @@ -99,59 +93,6 @@ def check_scheduler_available(scheduler_type: str, creds: Dict[str, str]) -> boo return False -def validate_scheduler_job_submission( - scheduler_type: str, creds: Dict[str, str] -) -> Dict[str, Any]: - """Test basic job submission to a scheduler.""" - logger.info(f"Testing {scheduler_type.upper()} job submission") - - try: - # Configure clustrix for the scheduler - config_params = { - "cluster_type": scheduler_type, - "cluster_host": creds["host"], - "username": creds["username"], - "password": creds.get("password"), - "key_file": creds.get("key_file"), - } - - # Remove None values - config_params = {k: v for k, v in config_params.items() if v is not None} - - configure(**config_params) - - # Define a simple test function - @cluster(cores=1, memory="1GB", time="00:05:00") - def scheduler_test_function(x: int) -> int: - """Simple test function for scheduler validation.""" - import time - import os - - # Brief computation to validate execution - result = x * 2 + 1 - time.sleep(2) # Brief delay to simulate work - - # Return result with scheduler info if available - scheduler_info = os.getenv("SCHEDULER_ID", "unknown") - return {"result": result, "scheduler": scheduler_info, "input": x} - - # Submit job - job_result = scheduler_test_function(42) - - return { - "submission_successful": True, - "result": job_result, - "scheduler_type": scheduler_type, - } - - except Exception as e: - return { - "submission_successful": False, - "error": str(e), - "scheduler_type": scheduler_type, - } - - @pytest.mark.real_world class TestAdvancedSchedulersComprehensive: """Comprehensive advanced scheduler integration tests addressing Issue #63 Phase 5.""" @@ -161,8 +102,8 @@ def setup_method(self): self.scheduler_creds = {} self.available_schedulers = [] - # Test different scheduler types - schedulers = ["pbs", "sge", "slurm"] + # Only SLURM remains a supported scheduler backend. + schedulers = ["slurm"] for scheduler in schedulers: creds = get_scheduler_credentials(scheduler) @@ -180,46 +121,6 @@ def setup_method(self): f"⚠️ {scheduler.upper()} not available on {creds['host']}" ) - @pytest.mark.real_world - def test_pbs_job_submission_basic(self): - """Test basic PBS job submission functionality.""" - if "pbs" not in self.available_schedulers: - pytest.skip("PBS scheduler not available for testing") - - logger.info("Testing PBS basic job submission") - - creds = self.scheduler_creds["pbs"] - result = validate_scheduler_job_submission("pbs", creds) - - if result["submission_successful"]: - assert result["result"] is not None, "PBS job should return a result" - logger.info(f"✅ PBS job submission successful: {result['result']}") - else: - # Log the error but don't fail - this is expected if no PBS cluster available - logger.warning( - f"⚠️ PBS job submission failed (expected without cluster): {result['error']}" - ) - - @pytest.mark.real_world - def test_sge_job_submission_basic(self): - """Test basic SGE job submission functionality.""" - if "sge" not in self.available_schedulers: - pytest.skip("SGE scheduler not available for testing") - - logger.info("Testing SGE basic job submission") - - creds = self.scheduler_creds["sge"] - result = validate_scheduler_job_submission("sge", creds) - - if result["submission_successful"]: - assert result["result"] is not None, "SGE job should return a result" - logger.info(f"✅ SGE job submission successful: {result['result']}") - else: - # Log the error but don't fail - this is expected if no SGE cluster available - logger.warning( - f"⚠️ SGE job submission failed (expected without cluster): {result['error']}" - ) - @pytest.mark.real_world def test_slurm_advanced_features(self): """Test advanced SLURM features beyond basic submission.""" @@ -327,7 +228,7 @@ def resource_test_function() -> Dict[str, Any]: "scheduler_vars": { k: v for k, v in os.environ.items() - if k.startswith(("SLURM_", "PBS_", "SGE_", "LSF_")) + if k.startswith("SLURM_") }, } @@ -374,10 +275,6 @@ def test_scheduler_queue_systems(self): # Test queue information retrieval if scheduler == "slurm": queue_cmd = ["sinfo", "-o", "%P %A %T"] - elif scheduler == "pbs": - queue_cmd = ["qstat", "-Q"] - elif scheduler == "sge": - queue_cmd = ["qstat", "-g", "c"] else: continue @@ -459,10 +356,6 @@ def test_scheduler_job_monitoring(self): # Test job monitoring commands if scheduler == "slurm": monitor_cmd = ["squeue", "-u", creds["username"]] - elif scheduler == "pbs": - monitor_cmd = ["qstat", "-u", creds["username"]] - elif scheduler == "sge": - monitor_cmd = ["qstat", "-u", creds["username"]] else: continue @@ -517,8 +410,6 @@ def test_scheduler_environment_variables(self): env_var_patterns = { "slurm": ["SLURM_JOB_ID", "SLURM_PROCID", "SLURM_JOB_PARTITION"], - "pbs": ["PBS_JOBID", "PBS_ENVIRONMENT", "PBS_QUEUE"], - "sge": ["JOB_ID", "QUEUE", "SGE_TASK_ID"], } env_tests = [] @@ -554,8 +445,6 @@ def env_var_test() -> Dict[str, Any]: # Count total scheduler-related variables scheduler_prefixes = { "slurm": "SLURM_", - "pbs": "PBS_", - "sge": "SGE_", } prefix = scheduler_prefixes.get(scheduler, scheduler.upper() + "_") diff --git a/tests/real_world/test_cluster_job_system.py b/tests/real_world/test_cluster_job_system.py index b65421a6..c0c3ffa3 100644 --- a/tests/real_world/test_cluster_job_system.py +++ b/tests/real_world/test_cluster_job_system.py @@ -57,9 +57,6 @@ def test_function(x): # Test test files exist test_files = [ "tests/real_world/test_slurm_job_submission_real.py", - "tests/real_world/test_pbs_job_submission_real.py", - "tests/real_world/test_sge_job_submission_real.py", - "tests/real_world/test_kubernetes_job_submission_real.py", "tests/real_world/test_ssh_job_execution_real.py", ] From 382c055c735fa262aebb87d027709233ae7773cb Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:43:56 -0400 Subject: [PATCH 16/56] Clean up formatting left behind by the backend deletions Removing the PBS/SGE/Kubernetes/cloud blocks left stranded blank runs (E303, W391) in the three executor modules and an unused `import sys` in cli_credentials. black and flake8 are clean on these four files now. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/cli_credentials.py | 1 - clustrix/executor_connections.py | 5 - clustrix/executor_core.py | 8 - clustrix/executor_scheduler_status.py | 2 - docs/PRICING_API_DEPLOYMENT.md | 693 ---------------------- docs/PRICING_API_REFERENCE.md | 606 -------------------- docs/PRICING_USER_GUIDE.md | 792 -------------------------- docs/kubernetes_testing.md | 257 --------- 8 files changed, 2364 deletions(-) delete mode 100644 docs/PRICING_API_DEPLOYMENT.md delete mode 100644 docs/PRICING_API_REFERENCE.md delete mode 100644 docs/PRICING_USER_GUIDE.md delete mode 100644 docs/kubernetes_testing.md diff --git a/clustrix/cli_credentials.py b/clustrix/cli_credentials.py index f023ea5e..5265119e 100644 --- a/clustrix/cli_credentials.py +++ b/clustrix/cli_credentials.py @@ -5,7 +5,6 @@ """ import os -import sys import subprocess from pathlib import Path from typing import Dict diff --git a/clustrix/executor_connections.py b/clustrix/executor_connections.py index ec0a5c13..1b341cdd 100644 --- a/clustrix/executor_connections.py +++ b/clustrix/executor_connections.py @@ -79,8 +79,6 @@ def setup_ssh_connection(self): self.ssh_client.connect(**connect_kwargs) self.sftp_client = self.ssh_client.open_sftp() - - def execute_remote_command(self, command: str, check: bool = False) -> tuple: """Execute command on remote cluster. @@ -215,6 +213,3 @@ def disconnect(self): if self.ssh_client: self.ssh_client.close() self.ssh_client = None - - - diff --git a/clustrix/executor_core.py b/clustrix/executor_core.py index 399ed8c9..93a2d107 100644 --- a/clustrix/executor_core.py +++ b/clustrix/executor_core.py @@ -304,9 +304,6 @@ def execute(self, func, args: tuple, kwargs: dict) -> Any: job_id = self.submit_job(func_data, job_config) return self.wait_for_result(job_id) - - - def __del__(self): """Cleanup resources.""" self.disconnect() @@ -332,13 +329,10 @@ def sftp_client(self, value): """Set SFTP client for backward compatibility.""" self.connection_manager.sftp_client = value - - def _setup_ssh_connection(self): """Backward compatibility method.""" return self.connection_manager.setup_ssh_connection() - def _execute_remote_command(self, command: str) -> tuple: """Backward compatibility method.""" return self.connection_manager.execute_remote_command(command) @@ -417,5 +411,3 @@ def _check_slurm_status(self, job_id: str) -> str: return self.scheduler_manager.status_manager._check_slurm_job_status_robust( job_id, self.scheduler_manager.active_jobs ) - - diff --git a/clustrix/executor_scheduler_status.py b/clustrix/executor_scheduler_status.py index e80ad20f..69f7abef 100644 --- a/clustrix/executor_scheduler_status.py +++ b/clustrix/executor_scheduler_status.py @@ -444,8 +444,6 @@ def _get_scheduler_failure_reason(self, job_id: str) -> Optional[str]: ) return detail - - def _authenticated_error_payload( self, job_id: str, job_info: Dict[str, Any] ) -> Optional[bytes]: diff --git a/docs/PRICING_API_DEPLOYMENT.md b/docs/PRICING_API_DEPLOYMENT.md deleted file mode 100644 index e80f4434..00000000 --- a/docs/PRICING_API_DEPLOYMENT.md +++ /dev/null @@ -1,693 +0,0 @@ -# Production Deployment Guide: Cloud Provider Pricing APIs - -> **Status: aspirational, never implemented. Verified 2026-08-19.** -> This document describes a standalone `pricing_service` daemon, a -> `clustrix[pricing]` package extra, an `/etc/clustrix/clustrix.yml` service -> config, and eight `CLUSTRIX_*`/systemd environment variables. None of that -> exists: there is no `clustrix.services.pricing_service` module (`clustrix/` -> has no `services/` package at all), no `pricing` extra in `pyproject.toml` -> or `setup.py`, and `clustrix.config.load_config` rejects any YAML key that -> is not a `ClusterConfig` field -- a top-level `pricing:` block like the one -> shown below would fail to load. None of the environment variables in the -> "Performance Tuning" and "Configuration File" sections are read anywhere in -> `clustrix/`. What *is* real and working is the plain-Python pricing system -> in `clustrix/pricing_clients/` and `clustrix/cost_providers/`, used as a -> library (`from clustrix.cost_providers.aws import AWSCostMonitor`), with no -> daemon, service config, or extra to install -- see -> [`PRICING_API_REFERENCE.md`](PRICING_API_REFERENCE.md) and -> [`PRICING_USER_GUIDE.md`](PRICING_USER_GUIDE.md), which describe that real -> system and have been checked against the code. The rest of this document -> is left unedited below as a record of the deployment that was planned but -> never built; do not follow it. - -This guide provides comprehensive instructions for deploying Clustrix's programmatic cloud provider pricing system in production environments. - -## Overview - -The Clustrix pricing system provides real-time pricing data from major cloud providers (AWS, Azure, GCP, Lambda Cloud) with automatic fallback to hardcoded pricing when APIs are unavailable. - -## Architecture - -``` -┌─────────────────────────────────────────────────────────────┐ -│ Clustrix Application │ -├─────────────────────────────────────────────────────────────┤ -│ Cost Monitors │ -│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │ -│ │ AWS │ │ Azure │ │ GCP │ │ Lambda Cloud │ │ -│ │Monitor │ │ Monitor │ │ Monitor │ │ Monitor │ │ -│ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │ -└─────────────────────────────────────────────────────────────┘ - │ │ │ │ - ▼ ▼ ▼ ▼ -┌─────────────────────────────────────────────────────────────┐ -│ Pricing Clients │ -│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │ -│ │ AWS │ │ Azure │ │ GCP │ │ Lambda Cloud │ │ -│ │ Client │ │ Client │ │ Client │ │ Client │ │ -│ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │ -└─────────────────────────────────────────────────────────────┘ - │ │ │ │ - ▼ ▼ ▼ ▼ -┌─────────────────────────────────────────────────────────────┐ -│ Cloud Provider APIs │ -│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │ -│ │AWS Pricing │Azure Retail│GCP Billing│Lambda Cloud │ │ -│ │ API │Prices API │Catalog API│ API │ │ -│ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │ -└─────────────────────────────────────────────────────────────┘ -``` - -## Prerequisites - -### System Requirements - -- **Python**: 3.8+ (tested with 3.10, 3.12) -- **Memory**: Minimum 512MB RAM (recommended 1GB+) -- **Storage**: 100MB for cache and logs -- **Network**: Outbound HTTPS access to cloud provider APIs - -### Required Dependencies - -```bash -# Core dependencies -pip install clustrix[pricing] # Once available - -# Or install development version -pip install -e ".[dev]" - -# Additional cloud provider SDKs (optional but recommended) -pip install boto3 # AWS API support -pip install google-cloud-billing # GCP API support -``` - -## Configuration - -### Environment Variables - -Set the following environment variables for production deployment: - -```bash -# === Clustrix Configuration === -export CLUSTRIX_ENVIRONMENT=production -export CLUSTRIX_LOG_LEVEL=INFO -export CLUSTRIX_CACHE_DIR=/var/lib/clustrix/cache -export CLUSTRIX_CONFIG_DIR=/etc/clustrix - -# === AWS Credentials === -export AWS_ACCESS_KEY_ID="your-aws-access-key" -export AWS_SECRET_ACCESS_KEY="your-aws-secret-key" -export AWS_DEFAULT_REGION="us-east-1" - -# === Azure Credentials === -export AZURE_SUBSCRIPTION_ID="your-subscription-id" -export AZURE_TENANT_ID="your-tenant-id" -export AZURE_CLIENT_ID="your-client-id" -export AZURE_CLIENT_SECRET="your-client-secret" - -# === GCP Credentials === -export GOOGLE_CLOUD_PROJECT="your-project-id" -export GOOGLE_APPLICATION_CREDENTIALS="/etc/clustrix/gcp-service-account.json" - -# === Lambda Cloud Credentials === -export LAMBDA_CLOUD_API_KEY="your-lambda-api-key" - -# === Performance Tuning === -export CLUSTRIX_PRICING_CACHE_TTL_HOURS=24 -export CLUSTRIX_API_TIMEOUT_SECONDS=30 -export CLUSTRIX_MAX_RETRY_ATTEMPTS=3 -export CLUSTRIX_PRICING_FALLBACK_ENABLED=true -``` - -### Configuration File - -Create `/etc/clustrix/clustrix.yml`: - -```yaml -# Clustrix Production Configuration -pricing: - # Cache settings - cache_ttl_hours: 24 - cache_directory: "/var/lib/clustrix/cache" - - # API settings - api_timeout_seconds: 30 - max_retry_attempts: 3 - fallback_enabled: true - - # Provider configurations - providers: - aws: - enabled: true - regions: ["us-east-1", "us-west-2", "eu-west-1"] - pricing_api_enabled: true - - azure: - enabled: true - regions: ["eastus", "westus2", "westeurope"] - pricing_api_enabled: true - - gcp: - enabled: true - regions: ["us-central1", "us-west1", "europe-west1"] - pricing_api_enabled: true - - lambda: - enabled: true - pricing_api_enabled: true - api_endpoint: "https://cloud.lambdalabs.com/api/v1" - -# Logging configuration -logging: - level: "INFO" - format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s" - file: "/var/log/clustrix/pricing.log" - max_bytes: 10485760 # 10MB - backup_count: 5 - -# Monitoring -monitoring: - metrics_enabled: true - health_check_interval_seconds: 300 - pricing_staleness_alert_hours: 48 -``` - -## Deployment Steps - -### 1. System Setup - -```bash -# Create system user -sudo useradd -r -s /bin/false clustrix - -# Create directories -sudo mkdir -p /etc/clustrix -sudo mkdir -p /var/lib/clustrix/cache -sudo mkdir -p /var/log/clustrix - -# Set permissions -sudo chown -R clustrix:clustrix /var/lib/clustrix -sudo chown -R clustrix:clustrix /var/log/clustrix -sudo chmod 755 /etc/clustrix -``` - -### 2. Install Clustrix - -```bash -# Create virtual environment -sudo -u clustrix python3 -m venv /opt/clustrix/venv - -# Activate and install -sudo -u clustrix /opt/clustrix/venv/bin/pip install -e ".[dev]" - -# Verify installation -sudo -u clustrix /opt/clustrix/venv/bin/python -c " -from clustrix.pricing_clients.aws_pricing import AWSPricingClient -from clustrix.pricing_clients.azure_pricing import AzurePricingClient -from clustrix.pricing_clients.gcp_pricing import GCPPricingClient -from clustrix.pricing_clients.lambda_pricing import LambdaPricingClient -print('Pricing clients imported successfully') -" -``` - -### 3. Configure Credentials - -```bash -# Copy configuration file -sudo cp clustrix.yml /etc/clustrix/ - -# Set up GCP service account (if using GCP) -sudo cp gcp-service-account.json /etc/clustrix/ -sudo chown clustrix:clustrix /etc/clustrix/gcp-service-account.json -sudo chmod 600 /etc/clustrix/gcp-service-account.json - -# Set up environment file -sudo tee /etc/clustrix/environment > /dev/null << 'EOF' -CLUSTRIX_ENVIRONMENT=production -CLUSTRIX_CONFIG_DIR=/etc/clustrix -CLUSTRIX_CACHE_DIR=/var/lib/clustrix/cache -# Add your credentials here... -EOF - -sudo chown clustrix:clustrix /etc/clustrix/environment -sudo chmod 600 /etc/clustrix/environment -``` - -### 4. Create Systemd Service - -Create `/etc/systemd/system/clustrix-pricing.service`: - -```ini -[Unit] -Description=Clustrix Pricing Service -After=network.target -Requires=network.target - -[Service] -Type=notify -User=clustrix -Group=clustrix -WorkingDirectory=/opt/clustrix -ExecStart=/opt/clustrix/venv/bin/python -m clustrix.services.pricing_service -EnvironmentFile=/etc/clustrix/environment -Restart=always -RestartSec=10 -KillMode=process -TimeoutStopSec=30 - -# Security settings -NoNewPrivileges=true -ProtectSystem=strict -ProtectHome=true -ReadWritePaths=/var/lib/clustrix /var/log/clustrix -PrivateTmp=true - -[Install] -WantedBy=multi-user.target -``` - -### 5. Start Service - -```bash -# Reload systemd and start service -sudo systemctl daemon-reload -sudo systemctl enable clustrix-pricing -sudo systemctl start clustrix-pricing - -# Check status -sudo systemctl status clustrix-pricing -``` - -## Production Usage - -### Basic Usage - -```python -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor - -# Initialize cost monitors (automatically loads production config) -aws_monitor = AWSCostMonitor() -azure_monitor = AzureCostMonitor() -gcp_monitor = GCPCostMonitor() -lambda_monitor = LambdaCostMonitor(use_pricing_api=True) - -# Get cost estimates -aws_cost = aws_monitor.estimate_cost("t3.large", 8.0) # 8 hours -azure_cost = azure_monitor.estimate_cost("Standard_D4s_v3", 8.0) -gcp_cost = gcp_monitor.estimate_cost("n1-standard-4", 8.0) -lambda_cost = lambda_monitor.estimate_cost("gpu_1x_a10", 4.0) # 4 hours - -print(f"AWS t3.large 8h: ${aws_cost.estimated_cost:.2f}") -print(f"Azure D4s_v3 8h: ${azure_cost.estimated_cost:.2f}") -print(f"GCP n1-standard-4 8h: ${gcp_cost.estimated_cost:.2f}") -print(f"Lambda A10 4h: ${lambda_cost.estimated_cost:.2f}") -``` - -### Advanced Usage - -```python -# Get pricing with validation -from clustrix.pricing_clients.aws_pricing import AWSPricingClient - -client = AWSPricingClient() -price = client.get_instance_pricing("m5.xlarge", "us-east-1", "Linux") - -if price is None: - print("Warning: Using fallback pricing") -else: - print(f"Live API pricing: ${price:.4f}/hour") - -# Batch pricing queries -pricing_info = client.get_all_pricing("us-east-1") -for instance_type, hourly_rate in pricing_info.items(): - print(f"{instance_type}: ${hourly_rate:.4f}/hour") -``` - -## Monitoring and Maintenance - -### Health Checks - -Create `/opt/clustrix/health_check.py`: - -```python -#!/usr/bin/env python3 -"""Production health check for Clustrix pricing system.""" - -import sys -import logging -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor - -def health_check(): - """Run health check on all pricing providers.""" - monitors = { - 'aws': AWSCostMonitor(), - 'azure': AzureCostMonitor(), - 'gcp': GCPCostMonitor(), - 'lambda': LambdaCostMonitor(use_pricing_api=True) - } - - health_status = {} - overall_healthy = True - - for provider, monitor in monitors.items(): - try: - # Test basic pricing functionality - if provider == 'aws': - result = monitor.estimate_cost('t3.micro', 1.0) - elif provider == 'azure': - result = monitor.estimate_cost('Standard_A1_v2', 1.0) - elif provider == 'gcp': - result = monitor.estimate_cost('n1-standard-1', 1.0) - elif provider == 'lambda': - result = monitor.estimate_cost('gpu_1x_a10', 1.0) - - if result and result.estimated_cost > 0: - health_status[provider] = 'healthy' - print(f"✅ {provider}: healthy (${result.estimated_cost:.4f}/hour)") - else: - health_status[provider] = 'unhealthy' - overall_healthy = False - print(f"❌ {provider}: unhealthy (no pricing data)") - - except Exception as e: - health_status[provider] = 'error' - overall_healthy = False - print(f"❌ {provider}: error ({e})") - - if overall_healthy: - print("✅ Overall system health: HEALTHY") - return 0 - else: - print("❌ Overall system health: UNHEALTHY") - return 1 - -if __name__ == '__main__': - sys.exit(health_check()) -``` - -### Monitoring Script - -Create `/opt/clustrix/monitor_pricing.py`: - -```python -#!/usr/bin/env python3 -"""Production monitoring for Clustrix pricing system.""" - -import time -import logging -import json -from datetime import datetime -from clustrix.pricing_clients.aws_pricing import AWSPricingClient -from clustrix.pricing_clients.azure_pricing import AzurePricingClient -from clustrix.pricing_clients.gcp_pricing import GCPPricingClient -from clustrix.pricing_clients.lambda_pricing import LambdaPricingClient - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - -def monitor_pricing_apis(): - """Monitor pricing API health and response times.""" - clients = { - 'aws': AWSPricingClient(), - 'azure': AzurePricingClient(), - 'gcp': GCPPricingClient(), - 'lambda': LambdaPricingClient() - } - - # Authenticate Lambda client if credentials available - import os - lambda_api_key = os.getenv('LAMBDA_CLOUD_API_KEY') - if lambda_api_key: - clients['lambda'].authenticate(lambda_api_key) - - metrics = { - 'timestamp': datetime.now().isoformat(), - 'providers': {} - } - - test_instances = { - 'aws': ('t3.small', 'us-east-1'), - 'azure': ('Standard_D2s_v3', 'eastus'), - 'gcp': ('n1-standard-1', 'us-central1'), - 'lambda': ('gpu_1x_a10', 'us-east-1') - } - - for provider, client in clients.items(): - instance_type, region = test_instances[provider] - - start_time = time.time() - try: - if provider == 'aws': - price = client.get_instance_pricing(instance_type, region, 'Linux') - elif provider == 'azure': - price = client.get_instance_pricing(instance_type, region, 'Linux') - elif provider == 'gcp': - price = client.get_instance_pricing(instance_type, region) - elif provider == 'lambda': - price = client.get_instance_pricing(instance_type, region) - - response_time = time.time() - start_time - - metrics['providers'][provider] = { - 'status': 'success' if price is not None else 'no_data', - 'response_time_seconds': round(response_time, 3), - 'price': price, - 'instance_type': instance_type, - 'region': region - } - - logger.info(f"{provider}: ${price:.4f}/hour ({response_time:.3f}s)") - - except Exception as e: - response_time = time.time() - start_time - metrics['providers'][provider] = { - 'status': 'error', - 'response_time_seconds': round(response_time, 3), - 'error': str(e), - 'instance_type': instance_type, - 'region': region - } - logger.error(f"{provider}: error - {e}") - - # Write metrics to file for monitoring systems - with open('/var/log/clustrix/pricing_metrics.json', 'w') as f: - json.dump(metrics, f, indent=2) - - return metrics - -if __name__ == '__main__': - while True: - try: - monitor_pricing_apis() - time.sleep(300) # Run every 5 minutes - except KeyboardInterrupt: - logger.info("Monitoring stopped") - break - except Exception as e: - logger.error(f"Monitoring error: {e}") - time.sleep(60) # Wait 1 minute on error -``` - -### Log Rotation - -Create `/etc/logrotate.d/clustrix`: - -``` -/var/log/clustrix/*.log { - daily - rotate 30 - compress - delaycompress - missingok - notifempty - create 644 clustrix clustrix - postrotate - systemctl reload clustrix-pricing > /dev/null 2>&1 || true - endscript -} -``` - -## Security Considerations - -### API Key Management - -1. **Use environment variables** or secure key management systems -2. **Rotate keys regularly** (quarterly recommended) -3. **Monitor API usage** for unusual patterns -4. **Use least-privilege IAM policies** for cloud provider access - -### Network Security - -```bash -# Firewall rules (example with ufw) -sudo ufw allow out 443/tcp # HTTPS for API calls -sudo ufw deny in 443/tcp # No inbound HTTPS needed -``` - -### File Permissions - -```bash -# Secure configuration files -sudo chmod 600 /etc/clustrix/environment -sudo chmod 600 /etc/clustrix/gcp-service-account.json -sudo chmod 644 /etc/clustrix/clustrix.yml - -# Secure cache directory -sudo chmod 755 /var/lib/clustrix/cache -sudo chown -R clustrix:clustrix /var/lib/clustrix -``` - -## Performance Optimization - -### Caching Strategy - -- **Default TTL**: 24 hours for pricing data -- **Cache location**: `/var/lib/clustrix/cache` -- **Cache size**: Automatically managed, ~10MB typical -- **Cache invalidation**: Automatic on TTL expiry - -### API Rate Limiting - -- **AWS**: 100 requests/second (built-in throttling) -- **Azure**: No published limits (reasonable usage) -- **GCP**: 300 requests/minute (quota-based) -- **Lambda Cloud**: 100 requests/minute (estimated) - -### Memory Usage - -- **Base memory**: ~50MB per pricing client -- **Cache memory**: ~1MB per 1000 cached prices -- **Recommended**: 1GB RAM for production - -## Troubleshooting - -### Common Issues - -1. **"No pricing data available"** - - Check API credentials - - Verify network connectivity - - Check API quotas/limits - -2. **"Authentication failed"** - - Verify credentials in environment variables - - Check IAM permissions for cloud providers - - Ensure service account keys are valid - -3. **High response times** - - Check network latency to cloud provider APIs - - Consider regional API endpoints - - Verify caching is working correctly - -4. **Pricing discrepancies** - - Check if using API vs fallback pricing - - Verify instance type mappings - - Check regional pricing differences - -### Debugging Commands - -```bash -# Check service status -sudo systemctl status clustrix-pricing - -# View service logs -sudo journalctl -u clustrix-pricing -f - -# Check pricing logs -sudo tail -f /var/log/clustrix/pricing.log - -# Run health check -sudo -u clustrix /opt/clustrix/venv/bin/python /opt/clustrix/health_check.py - -# Test individual provider -sudo -u clustrix /opt/clustrix/venv/bin/python -c " -from clustrix.cost_providers.aws import AWSCostMonitor -monitor = AWSCostMonitor() -result = monitor.estimate_cost('t3.micro', 1.0) -print(f'AWS pricing test: {result}') -" -``` - -## Backup and Recovery - -### Configuration Backup - -```bash -# Backup configuration -sudo tar -czf /backup/clustrix-config-$(date +%Y%m%d).tar.gz \ - /etc/clustrix/ \ - /var/lib/clustrix/cache/ - -# Restore configuration -sudo tar -xzf clustrix-config-20240101.tar.gz -C / -``` - -### Cache Management - -```bash -# Clear pricing cache -sudo -u clustrix rm -rf /var/lib/clustrix/cache/* - -# View cache contents -sudo -u clustrix ls -la /var/lib/clustrix/cache/ -``` - -## Updates and Maintenance - -### Updating Clustrix - -```bash -# Stop service -sudo systemctl stop clustrix-pricing - -# Update code -sudo -u clustrix /opt/clustrix/venv/bin/pip install -U clustrix[pricing] - -# Run tests -sudo -u clustrix /opt/clustrix/venv/bin/python -m pytest tests/real_world/ -m real_world - -# Restart service -sudo systemctl start clustrix-pricing -``` - -### Regular Maintenance Tasks - -1. **Weekly**: Check logs for errors and warnings -2. **Monthly**: Verify pricing accuracy against cloud provider consoles -3. **Quarterly**: Rotate API keys and credentials -4. **Annually**: Review and update hardcoded fallback pricing - -## Support and Monitoring - -### Metrics to Monitor - -- API response times -- Cache hit/miss ratios -- Pricing data staleness -- Error rates by provider -- Memory and CPU usage - -### Alerting Thresholds - -- **API response time** > 30 seconds -- **Error rate** > 5% -- **Cache miss ratio** > 50% -- **Pricing data age** > 48 hours - -### Getting Help - -1. Check logs in `/var/log/clustrix/` -2. Run health check script -3. Review this deployment guide -4. Check GitHub issues for known problems - -## Conclusion - -This production deployment guide provides comprehensive instructions for running Clustrix's pricing system reliably in production. Regular monitoring and maintenance will ensure optimal performance and accurate pricing data. - -For additional support or questions, refer to the project documentation or GitHub repository. \ No newline at end of file diff --git a/docs/PRICING_API_REFERENCE.md b/docs/PRICING_API_REFERENCE.md deleted file mode 100644 index ed576e50..00000000 --- a/docs/PRICING_API_REFERENCE.md +++ /dev/null @@ -1,606 +0,0 @@ -# Clustrix Pricing API Reference - -This document provides comprehensive API documentation for Clustrix's cloud provider pricing system, including all pricing clients, cost monitors, and utility functions. - -## Table of Contents - -- [Overview](#overview) -- [Pricing Clients](#pricing-clients) -- [Cost Monitors](#cost-monitors) -- [Removed Functionality](#removed-functionality) -- [Examples](#examples) -- [Error Codes](#error-codes) - -## Overview - -The Clustrix pricing system provides programmatic access to cloud provider pricing data through a unified interface. It supports AWS, Azure, GCP, and Lambda Cloud with automatic fallback to hardcoded pricing when APIs are unavailable. - -### Core Architecture - -``` -Cost Monitors → Pricing Clients → Cloud Provider APIs - ↓ ↓ ↓ - User Interface Caching Real-time Pricing -``` - -### Key Features - -- **Real-time pricing**: Live API integration with all major cloud providers -- **Automatic fallback**: Graceful degradation to hardcoded pricing when the live API call fails or returns nothing -- **Caching system**: Simple file-based caching with TTL management (`clustrix.pricing_clients.base.PricingCache`) - -> **Note:** Earlier versions of this document also described a performance-monitoring -> module (metrics, circuit breakers) and a resilience module (retry decorators, -> fallback strategies, data validators, health checks). Those modules -> (`clustrix.pricing_clients.performance_monitor`, `clustrix.pricing_clients.resilience`, -> `clustrix.pricing_clients.validation_alerts`) have been removed from the codebase after being -> identified as unused (orphaned) code. See [Removed Functionality](#removed-functionality). - -## Pricing Clients - -### BasePricingClient - -Base class for all pricing client implementations. - -This is the real abstract base class (`clustrix/pricing_clients/base.py`). Note -that it does **not** define `authenticate` -- that method only exists on -`LambdaPricingClient`, because Lambda Cloud is the one provider here that -needs an API key for pricing. Every subclass must implement all three -abstract methods below, including `_fetch_pricing_from_api`. - -```python -from abc import ABC, abstractmethod -from typing import Any, Dict, Optional -from clustrix.pricing_clients.base import PricingCache - -class BasePricingClient(ABC): - """Abstract base class for pricing clients.""" - - def __init__(self, cache_ttl_hours: int = 24): - self.cache = PricingCache(ttl_hours=cache_ttl_hours) - self._hardcoded_pricing: Dict[str, Any] = {} - self._hardcoded_pricing_date: Optional[str] = None - - @abstractmethod - def get_instance_pricing(self, instance_type: str, region: str, **kwargs) -> Optional[float]: - """Get hourly pricing for a specific instance type.""" - - @abstractmethod - def get_all_pricing(self, region: str, **kwargs) -> Dict[str, float]: - """Get pricing for all instance types in a region.""" - - @abstractmethod - def _fetch_pricing_from_api( - self, instance_type: Optional[str], region: str, **kwargs - ) -> Optional[Dict[str, Any]]: - """Fetch raw pricing data from the provider's API.""" - - def _get_fallback_price(self, instance_type: str) -> Optional[float]: - """Look up `instance_type` in `self._hardcoded_pricing`.""" - - def is_pricing_data_outdated(self, days: int = 30) -> bool: - """True if `self._hardcoded_pricing_date` is more than `days` old (or unset).""" -``` - -#### Methods - -| Method | Parameters | Returns | Description | -|--------|------------|---------|-------------| -| `get_instance_pricing` (abstract) | `instance_type`, `region`, `**kwargs` | `Optional[float]` | Get hourly price for instance | -| `get_all_pricing` (abstract) | `region`, `**kwargs` | `Dict[str, float]` | Get all pricing data | -| `_fetch_pricing_from_api` (abstract) | `instance_type`, `region`, `**kwargs` | `Optional[Dict[str, Any]]` | Fetch raw data from the provider's API; every concrete subclass must implement this | -| `_get_fallback_price` | `instance_type` | `Optional[float]` | Hardcoded fallback price, logged as a warning when used | -| `is_pricing_data_outdated` | `days=30` | `bool` | Whether the hardcoded fallback table is older than `days` | - -### AWSPricingClient - -AWS Pricing API client implementation. - -```python -from clustrix.pricing_clients.aws_pricing import AWSPricingClient - -client = AWSPricingClient() -price = client.get_instance_pricing("m5.large", "us-east-1", "Linux") -all_prices = client.get_all_pricing("us-east-1") -``` - -#### Methods - -**`get_instance_pricing(instance_type: str, region: str, operating_system: str = "Linux") -> Optional[float]`** - -Get hourly pricing for a specific EC2 instance type. - -- **Parameters:** - - `instance_type` (str): EC2 instance type (e.g., "m5.large") - - `region` (str): AWS region (e.g., "us-east-1") - - `operating_system` (str): OS type ("Linux", "Windows", etc.) - -- **Returns:** Hourly price in USD or None if not found - -- **Example:** - ```python - price = client.get_instance_pricing("t3.medium", "us-west-2", "Linux") - # Returns: 0.0416 (for example) - ``` - -**`get_all_pricing(region: str, operating_system: str = "Linux") -> Dict[str, float]`** - -Get pricing for all EC2 instance types in a region. - -- **Returns:** Dictionary mapping instance types to hourly prices - -### AzurePricingClient - -Azure Retail Prices API client implementation. - -```python -from clustrix.pricing_clients.azure_pricing import AzurePricingClient - -client = AzurePricingClient() -price = client.get_instance_pricing("Standard_D2s_v3", "eastus", "Linux") -``` - -#### Methods - -**`get_instance_pricing(instance_type: str, region: str, operating_system: str = "Linux") -> Optional[float]`** - -Get hourly pricing for Azure VM sizes. - -- **Parameters:** - - `instance_type` (str): Azure VM size (e.g., "Standard_D2s_v3") - - `region` (str): Azure region (e.g., "eastus") - - `operating_system` (str): OS type ("Linux", "Windows") - -### GCPPricingClient - -Google Cloud Billing Catalog API client implementation. - -```python -from clustrix.pricing_clients.gcp_pricing import GCPPricingClient - -client = GCPPricingClient() -price = client.get_instance_pricing("n1-standard-4", "us-central1") -``` - -#### Methods - -**`get_instance_pricing(instance_type: str, region: str) -> Optional[float]`** - -Get hourly pricing for GCP machine types. - -- **Parameters:** - - `instance_type` (str): GCP machine type (e.g., "n1-standard-4") - - `region` (str): GCP region (e.g., "us-central1") - -### LambdaPricingClient - -Lambda Cloud API client implementation. - -```python -from clustrix.pricing_clients.lambda_pricing import LambdaPricingClient - -client = LambdaPricingClient() -client.authenticate(api_key="your-api-key") -price = client.get_instance_pricing("gpu_1x_a10", "us-east-1") -``` - -#### Methods - -**`authenticate(api_key: str) -> bool`** - -Authenticate with Lambda Cloud API. - -- **Parameters:** - - `api_key` (str): Lambda Cloud API key - -- **Returns:** True if authentication successful - -**`get_instance_pricing(instance_type: str, region: str) -> Optional[float]`** - -Get hourly pricing for Lambda Cloud GPU instances. - -- **Parameters:** - - `instance_type` (str): Instance type (e.g., "gpu_1x_a10") - - `region` (str): Region (currently supports "us-east-1") - -## Cost Monitors - -Cost monitors provide high-level cost estimation interfaces. - -### AWSCostMonitor - -```python -from clustrix.cost_providers.aws import AWSCostMonitor - -monitor = AWSCostMonitor() -cost_estimate = monitor.estimate_cost("t3.large", 8.0) # 8 hours -``` - -#### Methods - -**`estimate_cost(instance_type: str, hours: float) -> CostEstimate`** - -Estimate cost for running an instance. - -- **Parameters:** - - `instance_type` (str): Instance type - - `hours` (float): Number of hours - -- **Returns:** `CostEstimate` object with detailed cost breakdown - -#### CostEstimate Object - -This is the real dataclass (`clustrix/cost_monitoring.py`); an earlier -version of this document had it wrong -- inventing `provider`, `hours`, and -`region` fields that don't exist, and omitting the real `currency`, -`hours_used`, and `last_updated` fields: - -```python -from dataclasses import dataclass -from datetime import datetime -from typing import Optional - -@dataclass -class CostEstimate: - """Cost estimation information.""" - - instance_type: str - hourly_rate: float - hours_used: float - estimated_cost: float - currency: str = "USD" - last_updated: Optional[datetime] = None - pricing_source: str = "api" # "api" or "hardcoded" - pricing_warning: Optional[str] = None -``` - -### Configuration Options - -All cost monitors support configuration via environment variables or config file: - -```python -# Enable API pricing -monitor = AWSCostMonitor(use_pricing_api=True) - -# Use specific region -monitor = AWSCostMonitor(region="us-west-2") -``` - -## Removed Functionality - -Three modules that used to live under `clustrix/pricing_clients/` -- -`performance_monitor.py`, `resilience.py`, and `validation_alerts.py` -- have -been deleted as unused (orphaned) code. Nothing in `clustrix/cost_providers/` -or the rest of `clustrix/pricing_clients/` depended on them. The classes and -functions below **no longer exist**; do not import them: - -- `performance_monitor`: `PricingPerformanceMonitor`, `PerformanceMetric`, - `CircuitBreaker`, `PricingCache` (a *different* `PricingCache` than the one - below), `get_global_performance_monitor` -- `resilience`: `ExponentialBackoffRetry`, `RetryConfig`, `PricingAPISession`, - `FallbackPricingStrategy`, `PricingDataValidator`, - `get_global_fallback_strategy`, `get_global_pricing_validator`, - `get_global_degradation_manager`, `get_global_health_checker`, - `create_retry_decorator`, `create_api_session`, `create_circuit_breaker` -- `validation_alerts`: everything in this module - -There is no drop-in replacement for the performance monitoring, circuit -breaking, retry/backoff, or data validation those modules provided. What -*does* still exist for error handling and caching is: - -- **Automatic fallback to hardcoded pricing.** Every pricing client (AWS, - Azure, GCP, Lambda) carries a hardcoded pricing table and falls back to it - via `BasePricingClient._get_fallback_price()` -- see each client's - `_hardcoded_pricing` dict and `_fetch_pricing_from_api` implementation. -- **A simple file-based cache**, `clustrix.pricing_clients.base.PricingCache` - (this is the real, current `PricingCache` -- not the deleted - `performance_monitor.PricingCache`, which had a different, size-limited - API): - -```python -from clustrix.pricing_clients.base import PricingCache - -cache = PricingCache(ttl_hours=24) - -# Cache pricing data (any JSON-serializable dict) -cache.set("aws_t3.large_us-east-1", {"price": 0.0832}) - -# Retrieve cached data (None if missing or expired) -cached = cache.get("aws_t3.large_us-east-1") -print(cached) -``` - -If you need retry/backoff, circuit breaking, or custom validation, write it -yourself around the pricing clients' public methods (`get_instance_pricing`, -`get_all_pricing`) -- see the "Batch Pricing with Manual Retry" example -below for a minimal, dependency-free retry loop. - -## Examples - -### Basic Usage - -```python -from clustrix.cost_providers.aws import AWSCostMonitor - -# Initialize cost monitor -monitor = AWSCostMonitor(use_pricing_api=True) - -# Estimate cost for development workload -cost_estimate = monitor.estimate_cost("t3.medium", 8.0) # 8 hours - -print(f"Estimated cost: ${cost_estimate.estimated_cost:.2f}") -print(f"Hourly rate: ${cost_estimate.hourly_rate:.4f}") -print(f"Pricing source: {cost_estimate.pricing_source}") - -if cost_estimate.pricing_warning: - print(f"Warning: {cost_estimate.pricing_warning}") -``` - -### Multi-Provider Cost Comparison - -```python -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor - -# Initialize monitors -monitors = { - 'aws': AWSCostMonitor(), - 'azure': AzureCostMonitor(), - 'gcp': GCPCostMonitor() -} - -# Instance mappings for equivalent resources -instances = { - 'aws': 't3.large', - 'azure': 'Standard_D2s_v3', - 'gcp': 'n1-standard-2' -} - -hours = 24.0 -results = {} - -for provider, monitor in monitors.items(): - instance_type = instances[provider] - estimate = monitor.estimate_cost(instance_type, hours) - results[provider] = estimate.estimated_cost - -# Find cheapest option -cheapest = min(results.items(), key=lambda x: x[1]) -print(f"Cheapest option: {cheapest[0]} at ${cheapest[1]:.2f} for {hours} hours") -``` - -### Advanced Configuration: Lambda Cloud with an API Key - -```python -# cluster-required: needs a real Lambda Cloud API key to authenticate meaningfully -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor - -# Initialize Lambda Cloud monitor with API key -monitor = LambdaCostMonitor( - use_pricing_api=True, - api_key="your-lambda-api-key" -) - -# Estimate GPU workload cost -gpu_cost = monitor.estimate_cost("gpu_1x_a10", 4.0) # 4 hours -print(f"GPU training cost: ${gpu_cost.estimated_cost:.2f}") -``` - -### Manual Health Check (No External Monitoring Module) - -There is no built-in health-check registry anymore (`get_global_health_checker` -was part of the deleted `resilience` module). The pattern below gets the same -result by calling the pricing clients directly -- it's a normal function, not -a special API, and it's exercised for real (including the fallback path) as -part of this documentation's own test suite: - -```python -import logging -from clustrix.pricing_clients.aws_pricing import AWSPricingClient -from clustrix.pricing_clients.azure_pricing import AzurePricingClient - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -def aws_health_check(): - """AWS pricing health check: succeeds via live API or the hardcoded fallback.""" - try: - client = AWSPricingClient() - price = client.get_instance_pricing("t3.micro", "us-east-1", "Linux") - return {"healthy": price is not None, "price": price} - except Exception as e: - return {"healthy": False, "error": str(e)} - - -def azure_health_check(): - """Azure pricing health check: succeeds via live API or the hardcoded fallback.""" - try: - client = AzurePricingClient() - price = client.get_instance_pricing("Standard_A1_v2", "eastus", "Linux") - return {"healthy": price is not None, "price": price} - except Exception as e: - return {"healthy": False, "error": str(e)} - - -checks = {"aws": aws_health_check, "azure": azure_health_check} -results = {name: check() for name, check in checks.items()} - -healthy_count = sum(1 for r in results.values() if r["healthy"]) -logger.info(f"Healthy services: {healthy_count}/{len(results)}") -for service, details in results.items(): - logger.info(f" {service}: {'healthy' if details['healthy'] else 'unhealthy'}") - if not details["healthy"]: - logger.warning(f" Error: {details.get('error', 'no price returned')}") -``` - -Both `get_instance_pricing` calls above either return a live price or fall -back to the client's hardcoded table -- they do not raise just because the -live API is unreachable, so `"healthy"` here really means "returned *some* -price," not "the live API responded." - -### Batch Pricing with Manual Retry - -There is no built-in retry decorator anymore (`create_retry_decorator` was -part of the deleted `resilience` module). A plain retry loop, written with -only the standard library and the real client, replaces it: - -```python -import time -from clustrix.pricing_clients.aws_pricing import AWSPricingClient - - -def get_price_with_retry(client, instance_type, region, os, max_attempts=3, base_delay=1.0): - """Get price, retrying on exceptions with linear backoff.""" - last_error = None - for attempt in range(max_attempts): - try: - return client.get_instance_pricing(instance_type, region, os) - except Exception as e: # get_instance_pricing already falls back - last_error = e # internally; this only catches true failures - time.sleep(base_delay * (attempt + 1)) - raise last_error - - -# Initialize client -client = AWSPricingClient() - -# Instance types to check -instance_types = [ - "t3.micro", "t3.small", "t3.medium", "t3.large", - "m5.large", "m5.xlarge", "c5.large", "r5.large" -] - -region = "us-east-1" -results = {} - -for instance_type in instance_types: - try: - price = get_price_with_retry(client, instance_type, region, "Linux") - results[instance_type] = { - "price": price, - "status": "success" if price is not None else "no_data" - } - print(f"{instance_type}: ${price:.4f}/hour" if price else f"{instance_type}: No data") - except Exception as e: - results[instance_type] = { - "price": None, - "status": "error", - "error": str(e) - } - print(f"{instance_type}: Error - {e}") - -# Summary -successful = sum(1 for r in results.values() if r["status"] == "success") -print(f"\nBatch pricing complete: {successful}/{len(instance_types)} successful") -``` - -## Error Codes - -### HTTP Error Codes - -| Code | Description | Action | -|------|-------------|---------| -| 401 | Unauthorized | Check API credentials | -| 403 | Forbidden | Verify API permissions | -| 429 | Rate Limited | Back off and retry (write your own; see [Batch Pricing with Manual Retry](#batch-pricing-with-manual-retry)) | -| 500 | Server Error | Retry, or rely on the automatic hardcoded-pricing fallback | -| 503 | Service Unavailable | Rely on the automatic hardcoded-pricing fallback | - -### Pricing Client Errors - -There are no custom exception classes in `clustrix.pricing_clients` or -`clustrix.cost_providers` (an earlier version of this document listed -`AuthenticationError`, `RegionNotFoundError`, `InstanceTypeNotFoundError`, -`PricingDataUnavailableError`, and `RateLimitExceededError` here; none of -those classes exist in the codebase). What actually happens instead: - -| Situation | What happens | -|-----------|--------------| -| The live API call fails for any reason (network error, bad region, rate limit, auth failure) | `get_instance_pricing` catches the exception internally, logs a warning, and returns `_get_fallback_price(instance_type)` -- the hardcoded price, or `None` if the instance type isn't in the hardcoded table either | -| The instance type isn't in the hardcoded fallback table and the API also failed | `get_instance_pricing` returns `None` | -| Lambda Cloud authentication fails (`LambdaPricingClient.authenticate`) | Returns `False`; it does not raise | - -Since failures are swallowed and turned into `None` or a fallback price -rather than raised, code that calls these clients should check for `None`, -not wrap the call in a broad `try/except` expecting a custom exception type. - -### Common Error Patterns - -**Handling a missing price:** -```python -from clustrix.pricing_clients.aws_pricing import AWSPricingClient - -client = AWSPricingClient() -price = client.get_instance_pricing("t3.large", "us-east-1", "Linux") - -if price is None: - # Neither the live API nor the hardcoded table had this instance type - price = 0.0832 # your own last-resort default -``` - -**Authentication (Lambda Cloud only):** - -`authenticate` is not part of `BasePricingClient` -- only -`LambdaPricingClient` defines it, because it is the one provider here that -needs an API key: - -```python -from clustrix.pricing_clients.lambda_pricing import LambdaPricingClient - -client = LambdaPricingClient() -authenticated = client.authenticate(api_key="your-lambda-api-key") -if not authenticated: - print("Authentication failed; get_instance_pricing will fall back to hardcoded pricing") -``` - -**Data validation:** - -There is no built-in validator anymore (`get_global_pricing_validator` was -part of the deleted `resilience` module). A plain sanity check replaces it: - -```python -from clustrix.pricing_clients.aws_pricing import AWSPricingClient - -client = AWSPricingClient() -price = client.get_instance_pricing("t3.large", "us-east-1", "Linux") - -MIN_REASONABLE_PRICE, MAX_REASONABLE_PRICE = 0.001, 1000.0 -if price is not None and not (MIN_REASONABLE_PRICE <= price <= MAX_REASONABLE_PRICE): - print(f"Suspicious pricing data: ${price:.4f}; ignoring it") - price = None -``` - -## Best Practices - -### Performance Optimization - -1. **Use caching**: `PricingCache` (in `clustrix.pricing_clients.base`) already backs every pricing client with a 24-hour TTL by default -2. **Batch requests**: Group multiple pricing queries when possible -3. **Monitor performance yourself**: there is no built-in performance monitor; wrap calls with your own timing/logging if you need it (see [Removed Functionality](#removed-functionality)) -4. **Implement your own circuit breaking** if you need it: there is no built-in circuit breaker - -### Error Handling - -1. **Check for `None`**: pricing calls return `None` rather than raising when no price is available -- see [Pricing Client Errors](#pricing-client-errors) -2. **Implement your own retries** if transient failures matter to you: see [Batch Pricing with Manual Retry](#batch-pricing-with-manual-retry) -3. **Validate data yourself**: there is no built-in validator; a simple range check is often enough (see [Common Error Patterns](#common-error-patterns)) -4. **Rely on the built-in fallback**: every client already falls back to a hardcoded price automatically -5. **Log appropriately**: the clients already log warnings when they fall back; add your own logging around calls if you need more detail - -### Security - -1. **Secure credentials**: Use environment variables or secure storage -2. **Rotate keys**: Regularly rotate API keys -3. **Monitor usage**: Watch for unusual API usage patterns -4. **Use HTTPS**: Always use secure connections - -### Production Deployment - -1. **Health checks**: no built-in registry exists; call clients directly as shown in [Manual Health Check](#manual-health-check-no-external-monitoring-module) -2. **Alerting**: build your own on top of the health-check pattern above -3. **Backup strategies**: the hardcoded fallback tables are the only built-in backup; keep them current if pricing changes materially -4. **Documentation**: keep this document in sync with `clustrix/pricing_clients/` and `clustrix/cost_providers/` -- it drifted out of sync with the real code once already - -This completes the comprehensive API reference documentation for Clustrix's pricing system. \ No newline at end of file diff --git a/docs/PRICING_USER_GUIDE.md b/docs/PRICING_USER_GUIDE.md deleted file mode 100644 index 1fa75dae..00000000 --- a/docs/PRICING_USER_GUIDE.md +++ /dev/null @@ -1,792 +0,0 @@ -# Clustrix Pricing System User Guide - -This guide provides practical examples and common use cases for Clustrix's cloud provider pricing system. - -## Quick Start - -### Installation - -```bash -# Install Clustrix with pricing support -pip install -e ".[dev]" - -# Optional: Install cloud provider SDKs for enhanced functionality -pip install boto3 # AWS -pip install google-cloud-billing # GCP -``` - -### Basic Setup - -```python -from clustrix.cost_providers.aws import AWSCostMonitor - -# Initialize cost monitor (uses environment variables for credentials) -monitor = AWSCostMonitor() - -# Get cost estimate -cost = monitor.estimate_cost("t3.medium", 8.0) # 8 hours -print(f"Cost: ${cost.estimated_cost:.2f}") -``` - -### Environment Configuration - -Set up credentials via environment variables: - -```bash -# AWS -export AWS_ACCESS_KEY_ID="your-access-key" -export AWS_SECRET_ACCESS_KEY="your-secret-key" - -# Azure -export AZURE_SUBSCRIPTION_ID="your-subscription" -export AZURE_TENANT_ID="your-tenant" -export AZURE_CLIENT_ID="your-client-id" -export AZURE_CLIENT_SECRET="your-secret" - -# GCP -export GOOGLE_CLOUD_PROJECT="your-project" -export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json" - -# Lambda Cloud -export LAMBDA_CLOUD_API_KEY="your-api-key" -``` - -## Common Use Cases - -### 1. Development Cost Estimation - -Estimate costs for development workloads across different providers. - -```python -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor - -def estimate_development_costs(): - """Compare development costs across providers.""" - - # Development workload: 8 hours/day, 5 days/week - daily_hours = 8 - weekly_days = 5 - monthly_hours = daily_hours * weekly_days * 4 # ~160 hours/month - - providers = { - 'AWS': (AWSCostMonitor(), 't3.medium'), - 'Azure': (AzureCostMonitor(), 'Standard_D2s_v3'), - 'GCP': (GCPCostMonitor(), 'n1-standard-2') - } - - results = {} - for name, (monitor, instance_type) in providers.items(): - try: - cost_estimate = monitor.estimate_cost(instance_type, monthly_hours) - results[name] = { - 'monthly_cost': cost_estimate.estimated_cost, - 'hourly_rate': cost_estimate.hourly_rate, - 'instance_type': instance_type, - 'pricing_source': cost_estimate.pricing_source - } - except Exception as e: - print(f"Error getting {name} pricing: {e}") - results[name] = {'error': str(e)} - - # Display results - print("Development Workload Cost Comparison (Monthly)") - print("=" * 50) - - valid_results = {k: v for k, v in results.items() if 'error' not in v} - if valid_results: - # Sort by cost - sorted_results = sorted(valid_results.items(), key=lambda x: x[1]['monthly_cost']) - - for provider, data in sorted_results: - print(f"{provider:8} ${data['monthly_cost']:7.2f} | " - f"${data['hourly_rate']:6.4f}/hr | " - f"{data['instance_type']:18} | " - f"{data['pricing_source']}") - - # Cost difference analysis - cheapest = sorted_results[0][1]['monthly_cost'] - most_expensive = sorted_results[-1][1]['monthly_cost'] - savings = most_expensive - cheapest - - print(f"\nPotential monthly savings: ${savings:.2f}") - print(f"Annual savings: ${savings * 12:.2f}") - - return results - -# Run the comparison -results = estimate_development_costs() -``` - -### 2. GPU Training Cost Analysis - -Compare GPU costs for machine learning workloads. - -```python -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor -from clustrix.cost_providers.lambda_cloud import LambdaCostMonitor - -def analyze_gpu_training_costs(): - """Analyze GPU costs for ML training workloads.""" - - # Training scenarios - scenarios = { - 'Short Training (4 hours)': 4, - 'Medium Training (12 hours)': 12, - 'Long Training (48 hours)': 48, - 'Weekend Project (72 hours)': 72 - } - - # GPU instance mappings - gpu_instances = { - 'AWS': (AWSCostMonitor(), 'g4dn.xlarge'), # T4 GPU - 'Azure': (AzureCostMonitor(), 'Standard_NC6s_v3'), # V100 GPU - 'GCP': (GCPCostMonitor(), 'n1-standard-4-t4'), # T4 GPU - 'Lambda Cloud': (LambdaCostMonitor(use_pricing_api=True), 'gpu_1x_a10') # A10 GPU - } - - print("GPU Training Cost Analysis") - print("=" * 60) - - for scenario, hours in scenarios.items(): - print(f"\n{scenario}:") - print("-" * 40) - - scenario_results = {} - - for provider, (monitor, instance_type) in gpu_instances.items(): - try: - cost_estimate = monitor.estimate_cost(instance_type, hours) - scenario_results[provider] = cost_estimate.estimated_cost - - print(f"{provider:12} ${cost_estimate.estimated_cost:7.2f} | " - f"${cost_estimate.hourly_rate:6.3f}/hr | " - f"{instance_type}") - - except Exception as e: - print(f"{provider:12} Error: {e}") - - # Show cost comparison - if len(scenario_results) > 1: - cheapest = min(scenario_results.items(), key=lambda x: x[1]) - most_expensive = max(scenario_results.items(), key=lambda x: x[1]) - - if cheapest[1] != most_expensive[1]: - savings = most_expensive[1] - cheapest[1] - savings_percent = (savings / most_expensive[1]) * 100 - - print(f" → Cheapest: {cheapest[0]} (${cheapest[1]:.2f})") - print(f" → Savings: ${savings:.2f} ({savings_percent:.1f}%)") - -# Run GPU analysis -analyze_gpu_training_costs() -``` - -### 3. Batch Processing Cost Optimization - -Optimize costs for batch processing workloads. - -```python -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -from clustrix.cost_providers.gcp import GCPCostMonitor -from datetime import datetime, timedelta - -def optimize_batch_processing_costs(): - """Find optimal instance types for batch processing.""" - - # Define batch processing requirements - job_requirements = { - 'cpu_intensive': { - 'aws': ['c5.large', 'c5.xlarge', 'c5.2xlarge', 'c5.4xlarge'], - 'azure': ['Standard_F2s_v2', 'Standard_F4s_v2', 'Standard_F8s_v2', 'Standard_F16s_v2'], - 'gcp': ['c2-standard-4', 'c2-standard-8', 'c2-standard-16', 'c2-standard-30'] - }, - 'memory_intensive': { - 'aws': ['r5.large', 'r5.xlarge', 'r5.2xlarge', 'r5.4xlarge'], - 'azure': ['Standard_E4s_v3', 'Standard_E8s_v3', 'Standard_E16s_v3', 'Standard_E32s_v3'], - 'gcp': ['n1-highmem-4', 'n1-highmem-8', 'n1-highmem-16', 'n1-highmem-32'] - }, - 'balanced': { - 'aws': ['m5.large', 'm5.xlarge', 'm5.2xlarge', 'm5.4xlarge'], - 'azure': ['Standard_D4s_v3', 'Standard_D8s_v3', 'Standard_D16s_v3', 'Standard_D32s_v3'], - 'gcp': ['n1-standard-4', 'n1-standard-8', 'n1-standard-16', 'n1-standard-32'] - } - } - - # Job duration scenarios - durations = [1, 4, 8, 24, 72] # hours - - monitors = { - 'aws': AWSCostMonitor(), - 'azure': AzureCostMonitor(), - 'gcp': GCPCostMonitor() - } - - print("Batch Processing Cost Optimization") - print("=" * 50) - - for workload_type, provider_instances in job_requirements.items(): - print(f"\n{workload_type.replace('_', ' ').title()} Workload") - print("-" * 30) - - # Test each duration - for duration in durations: - print(f"\n{duration} hour job:") - - best_options = [] - - for provider, instance_types in provider_instances.items(): - if provider not in monitors: - continue - - monitor = monitors[provider] - provider_best = None - provider_best_cost = float('inf') - - for instance_type in instance_types: - try: - cost_estimate = monitor.estimate_cost(instance_type, duration) - total_cost = cost_estimate.estimated_cost - - if total_cost < provider_best_cost: - provider_best = (instance_type, total_cost, cost_estimate.hourly_rate) - provider_best_cost = total_cost - - except Exception as e: - continue - - if provider_best: - best_options.append((provider, *provider_best)) - - # Sort by total cost - best_options.sort(key=lambda x: x[2]) - - for provider, instance_type, total_cost, hourly_rate in best_options[:3]: # Top 3 - print(f" {provider:6} {instance_type:20} ${total_cost:7.2f} (${hourly_rate:.4f}/hr)") - - if len(best_options) > 1: - savings = best_options[-1][2] - best_options[0][2] - print(f" → Potential savings: ${savings:.2f}") - -# Run batch optimization -optimize_batch_processing_costs() -``` - -### 4. Monthly Budget Planning - -Plan and monitor monthly cloud spending. - -```python -from clustrix.cost_providers.aws import AWSCostMonitor -from clustrix.cost_providers.azure import AzureCostMonitor -import json -from datetime import datetime - -def create_monthly_budget_plan(): - """Create a monthly budget plan for cloud resources.""" - - # Define monthly usage patterns - monthly_workloads = { - 'development_team': { - 'description': '5 developers, 8 hours/day, 22 working days', - 'instances': [ - {'type': 't3.medium', 'count': 5, 'hours_per_day': 8, 'days_per_month': 22} - ] - }, - 'ci_cd_pipeline': { - 'description': 'CI/CD builds, ~4 hours/day average', - 'instances': [ - {'type': 'c5.large', 'count': 2, 'hours_per_day': 4, 'days_per_month': 30} - ] - }, - 'data_processing': { - 'description': 'Weekly batch jobs, memory intensive', - 'instances': [ - {'type': 'r5.2xlarge', 'count': 1, 'hours_per_day': 12, 'days_per_month': 4} - ] - }, - 'ml_training': { - 'description': 'GPU training, 2 sessions per week', - 'instances': [ - {'type': 'g4dn.xlarge', 'count': 1, 'hours_per_day': 6, 'days_per_month': 8} - ] - } - } - - providers = { - 'AWS': AWSCostMonitor(), - 'Azure': AzureCostMonitor() - } - - # Instance type mappings - instance_mappings = { - 'Azure': { - 't3.medium': 'Standard_D2s_v3', - 'c5.large': 'Standard_F4s_v2', - 'r5.2xlarge': 'Standard_E16s_v3', - 'g4dn.xlarge': 'Standard_NC6s_v3' - } - } - - budget_analysis = {} - - for provider_name, monitor in providers.items(): - print(f"\n{provider_name} Monthly Budget Analysis") - print("=" * 40) - - total_monthly_cost = 0 - provider_breakdown = {} - - for workload, config in monthly_workloads.items(): - workload_cost = 0 - workload_details = [] - - for instance_config in config['instances']: - # Map instance type for non-AWS providers - instance_type = instance_config['type'] - if provider_name in instance_mappings: - instance_type = instance_mappings[provider_name].get(instance_type, instance_type) - - # Calculate monthly hours - monthly_hours = (instance_config['count'] * - instance_config['hours_per_day'] * - instance_config['days_per_month']) - - try: - cost_estimate = monitor.estimate_cost(instance_type, monthly_hours) - instance_monthly_cost = cost_estimate.estimated_cost - workload_cost += instance_monthly_cost - - workload_details.append({ - 'instance_type': instance_type, - 'count': instance_config['count'], - 'monthly_hours': monthly_hours, - 'hourly_rate': cost_estimate.hourly_rate, - 'monthly_cost': instance_monthly_cost - }) - - except Exception as e: - print(f" Error pricing {instance_type}: {e}") - continue - - total_monthly_cost += workload_cost - provider_breakdown[workload] = { - 'description': config['description'], - 'cost': workload_cost, - 'details': workload_details - } - - print(f"\n{workload.replace('_', ' ').title()}:") - print(f" {config['description']}") - print(f" Monthly cost: ${workload_cost:.2f}") - - for detail in workload_details: - print(f" {detail['instance_type']}: {detail['count']}x × " - f"{detail['monthly_hours']:.0f}h = ${detail['monthly_cost']:.2f}") - - print(f"\nTotal Monthly Cost: ${total_monthly_cost:.2f}") - print(f"Annual Estimate: ${total_monthly_cost * 12:.2f}") - - # Cost breakdown by workload - if provider_breakdown: - print(f"\nCost Breakdown:") - sorted_workloads = sorted(provider_breakdown.items(), - key=lambda x: x[1]['cost'], reverse=True) - - for workload, data in sorted_workloads: - percentage = (data['cost'] / total_monthly_cost * 100) if total_monthly_cost > 0 else 0 - print(f" {workload.replace('_', ' ').title()}: " - f"${data['cost']:.2f} ({percentage:.1f}%)") - - budget_analysis[provider_name] = { - 'total_monthly_cost': total_monthly_cost, - 'workload_breakdown': provider_breakdown, - 'generated_at': datetime.now().isoformat() - } - - # Compare providers - if len(budget_analysis) > 1: - print(f"\nProvider Cost Comparison:") - print("-" * 25) - - provider_costs = {name: data['total_monthly_cost'] - for name, data in budget_analysis.items()} - sorted_providers = sorted(provider_costs.items(), key=lambda x: x[1]) - - for provider, cost in sorted_providers: - print(f" {provider}: ${cost:.2f}/month") - - if len(sorted_providers) > 1: - cheapest = sorted_providers[0] - most_expensive = sorted_providers[-1] - monthly_savings = most_expensive[1] - cheapest[1] - annual_savings = monthly_savings * 12 - - print(f"\nPotential Savings:") - print(f" Monthly: ${monthly_savings:.2f}") - print(f" Annual: ${annual_savings:.2f}") - - # Save budget analysis - with open('monthly_budget_analysis.json', 'w') as f: - json.dump(budget_analysis, f, indent=2) - - print(f"\nBudget analysis saved to 'monthly_budget_analysis.json'") - - return budget_analysis - -# Generate budget plan -budget_plan = create_monthly_budget_plan() -``` - -### 5. Cost Alerting and Monitoring - -Set up automated cost monitoring and alerts. - -> **Note:** an earlier version of this example also tracked *performance* -> health (API error rate, response time, cache hit rate) via -> `clustrix.pricing_clients.performance_monitor.get_global_performance_monitor()` -> and `clustrix.pricing_clients.resilience.get_global_health_checker()`. Both -> modules have been removed from the codebase as unused code, so that part of -> the example is gone too -- there is no replacement for performance -> monitoring built into Clustrix. What remains below is the *cost* alerting, -> which only ever depended on the still-real `AWSCostMonitor.estimate_cost()`. - -```python -from clustrix.cost_providers.aws import AWSCostMonitor -import smtplib -from email.mime.text import MIMEText -from datetime import datetime, timedelta -import json - -class CostAlertingSystem: - """Automated cost alerting and monitoring system.""" - - def __init__(self, email_config=None): - self.monitors = { - 'aws': AWSCostMonitor(use_pricing_api=True) - } - self.email_config = email_config - - # Alert thresholds - self.cost_thresholds = { - 'daily_limit': 50.0, # $50/day - 'monthly_limit': 1000.0, # $1000/month - 'hourly_spike': 10.0 # $10/hour spike - } - - def check_cost_thresholds(self, workloads): - """Check if any cost thresholds are exceeded.""" - alerts = [] - - for provider, monitor in self.monitors.items(): - total_daily_cost = 0 - - for workload in workloads: - try: - cost_estimate = monitor.estimate_cost( - workload['instance_type'], - workload['daily_hours'] - ) - workload_cost = cost_estimate.estimated_cost - total_daily_cost += workload_cost - - # Check hourly spike threshold - hourly_cost = cost_estimate.hourly_rate - if hourly_cost > self.cost_thresholds['hourly_spike']: - alerts.append({ - 'type': 'cost_spike', - 'provider': provider, - 'workload': workload['name'], - 'instance_type': workload['instance_type'], - 'hourly_cost': hourly_cost, - 'threshold': self.cost_thresholds['hourly_spike'], - 'severity': 'warning' - }) - - except Exception as e: - alerts.append({ - 'type': 'pricing_error', - 'provider': provider, - 'workload': workload['name'], - 'error': str(e), - 'severity': 'error' - }) - - # Check daily limit - if total_daily_cost > self.cost_thresholds['daily_limit']: - alerts.append({ - 'type': 'daily_limit_exceeded', - 'provider': provider, - 'daily_cost': total_daily_cost, - 'threshold': self.cost_thresholds['daily_limit'], - 'severity': 'critical' - }) - - # Check monthly projection - projected_monthly = total_daily_cost * 30 - if projected_monthly > self.cost_thresholds['monthly_limit']: - alerts.append({ - 'type': 'monthly_projection_exceeded', - 'provider': provider, - 'projected_monthly': projected_monthly, - 'threshold': self.cost_thresholds['monthly_limit'], - 'severity': 'warning' - }) - - return alerts - - # A `check_performance_health` method used to live here, built on top of - # `get_global_performance_monitor()` and `get_global_health_checker()`. - # Both came from modules that have since been removed from the codebase - # (`clustrix.pricing_clients.performance_monitor` and `.resilience`), and - # there is no replacement -- Clustrix does not track API error rate, - # response time, or cache hit rate anymore. If you need this, you would - # have to instrument it yourself around calls to the pricing clients. - - def send_alert_email(self, alerts): - """Send alert email if configured.""" - if not self.email_config or not alerts: - return - - # Group alerts by severity - critical = [a for a in alerts if a.get('severity') == 'critical'] - warnings = [a for a in alerts if a.get('severity') == 'warning'] - info = [a for a in alerts if a.get('severity') == 'info'] - errors = [a for a in alerts if a.get('severity') == 'error'] - - # Create email content - subject = f"Clustrix Cost Alert - {len(alerts)} alerts" - if critical: - subject = f"CRITICAL: {subject}" - - body = f""" -Clustrix Cost Monitoring Alert Report -Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - -Summary: {len(alerts)} total alerts -- Critical: {len(critical)} -- Warnings: {len(warnings)} -- Errors: {len(errors)} -- Info: {len(info)} - -""" - - # Add alert details - for severity, alert_list in [('CRITICAL', critical), ('WARNING', warnings), - ('ERROR', errors), ('INFO', info)]: - if alert_list: - body += f"\n{severity} ALERTS:\n" - body += "-" * 20 + "\n" - - for alert in alert_list: - body += f"Type: {alert['type']}\n" - for key, value in alert.items(): - if key not in ['type', 'severity']: - body += f" {key}: {value}\n" - body += "\n" - - # Send email - try: - msg = MIMEText(body) - msg['Subject'] = subject - msg['From'] = self.email_config['from'] - msg['To'] = self.email_config['to'] - - with smtplib.SMTP(self.email_config['smtp_server'], - self.email_config['smtp_port']) as server: - if self.email_config.get('use_tls'): - server.starttls() - if self.email_config.get('username'): - server.login(self.email_config['username'], - self.email_config['password']) - server.send_message(msg) - - print(f"Alert email sent: {subject}") - - except Exception as e: - print(f"Failed to send alert email: {e}") - - def run_monitoring_check(self, workloads): - """Run complete monitoring check.""" - print(f"Running cost monitoring check at {datetime.now()}") - - # Check costs (the only kind of alert this class still generates -- - # see the note above `send_alert_email` for what was removed) - all_alerts = self.check_cost_thresholds(workloads) - - # Log alerts - if all_alerts: - print(f"Found {len(all_alerts)} alerts:") - for alert in all_alerts: - severity = alert.get('severity', 'info').upper() - alert_type = alert.get('type', 'unknown') - print(f" [{severity}] {alert_type}") - else: - print("No alerts - system healthy") - - # Send email if configured - self.send_alert_email(all_alerts) - - # Save alert log - alert_log = { - 'timestamp': datetime.now().isoformat(), - 'alerts': all_alerts, - 'alert_count': len(all_alerts) - } - - with open(f"cost_alert_log_{datetime.now().strftime('%Y%m%d')}.json", 'a') as f: - f.write(json.dumps(alert_log) + "\n") - - return all_alerts - -# Example usage -def setup_cost_monitoring(): - """Set up automated cost monitoring.""" - - # Define your workloads - workloads = [ - { - 'name': 'development', - 'instance_type': 't3.medium', - 'daily_hours': 8 - }, - { - 'name': 'ci_cd', - 'instance_type': 'c5.large', - 'daily_hours': 4 - }, - { - 'name': 'data_processing', - 'instance_type': 'r5.xlarge', - 'daily_hours': 2 - } - ] - - # Email configuration (optional) - email_config = { - 'smtp_server': 'smtp.gmail.com', - 'smtp_port': 587, - 'use_tls': True, - 'username': 'your-email@gmail.com', - 'password': 'your-app-password', # Use app password for Gmail - 'from': 'your-email@gmail.com', - 'to': 'alerts@yourcompany.com' - } - - # Initialize alerting system - alerting = CostAlertingSystem(email_config) - - # Run monitoring check - alerts = alerting.run_monitoring_check(workloads) - - return alerts - -# Run monitoring -alerts = setup_cost_monitoring() -``` - -## Advanced Features - -### Custom Pricing Sources - -Add custom pricing sources or override existing ones. `BasePricingClient` is -an `ABC` with **three** abstract methods, not two -- a subclass that skips -`_fetch_pricing_from_api` cannot be instantiated (`TypeError: Can't -instantiate abstract class ... with abstract method _fetch_pricing_from_api`, -verified against the real class): - -```python -from typing import Any, Dict, Optional -from clustrix.pricing_clients.base import BasePricingClient - -class CustomPricingClient(BasePricingClient): - """Custom pricing client for internal pricing data.""" - - def __init__(self): - self.custom_prices = { - 't3.micro': 0.0104, - 't3.small': 0.0208, - 't3.medium': 0.0416 - } - - def get_instance_pricing(self, instance_type, region, **kwargs): - return self.custom_prices.get(instance_type) - - def get_all_pricing(self, region, **kwargs): - return self.custom_prices.copy() - - def _fetch_pricing_from_api( - self, instance_type: Optional[str], region: str, **kwargs - ) -> Optional[Dict[str, Any]]: - # This example has no live API of its own -- it only ever serves - # the hardcoded dict above, so there's nothing to fetch. - return None - -# Use custom pricing client -custom_client = CustomPricingClient() -price = custom_client.get_instance_pricing("t3.small", "us-east-1") -print(price) -``` - -(`authenticate` was dropped from this example too -- it isn't part of -`BasePricingClient`'s contract; see [Authentication (Lambda Cloud -only)](PRICING_API_REFERENCE.md#common-error-patterns) in the API reference -if your custom client needs it.) - -### Integration with CI/CD - -Add cost estimation to your CI/CD pipeline: - -```python -# cluster-required: reads deployment_config.json supplied by your own CI pipeline -# cost_check.py - CI/CD cost validation script -import sys -import json -from clustrix.cost_providers.aws import AWSCostMonitor - -def validate_deployment_cost(): - """Validate deployment cost against budget.""" - - # Load deployment configuration - with open('deployment_config.json', 'r') as f: - config = json.load(f) - - monitor = AWSCostMonitor() - total_estimated_cost = 0 - - for resource in config.get('resources', []): - instance_type = resource['instance_type'] - count = resource['count'] - daily_hours = resource.get('daily_hours', 24) # Default to always on - - cost_estimate = monitor.estimate_cost(instance_type, daily_hours) - resource_cost = cost_estimate.estimated_cost * count - total_estimated_cost += resource_cost - - print(f"{instance_type} x{count}: ${resource_cost:.2f}/day") - - monthly_estimate = total_estimated_cost * 30 - - print(f"\nTotal estimated cost:") - print(f"Daily: ${total_estimated_cost:.2f}") - print(f"Monthly: ${monthly_estimate:.2f}") - - # Check against budget - budget_limit = config.get('budget_limit', 1000) # Default $1000/month - - if monthly_estimate > budget_limit: - print(f"\nERROR: Estimated monthly cost ${monthly_estimate:.2f} exceeds budget limit ${budget_limit:.2f}") - sys.exit(1) - - print(f"\nPASS: Deployment within budget (${monthly_estimate:.2f} < ${budget_limit:.2f})") - return 0 - -if __name__ == '__main__': - validate_deployment_cost() -``` - -This completes the comprehensive user guide with practical examples for common use cases of Clustrix's pricing system. \ No newline at end of file diff --git a/docs/kubernetes_testing.md b/docs/kubernetes_testing.md deleted file mode 100644 index 819fc9f8..00000000 --- a/docs/kubernetes_testing.md +++ /dev/null @@ -1,257 +0,0 @@ -# Kubernetes Real-World Testing Guide - -This guide explains how to set up and run real-world Kubernetes validation tests for Clustrix (Issue #63 Phase 2). - -## Overview - -The Kubernetes test suite (`tests/real_world/test_kubernetes_comprehensive.py`) validates: - -- ✅ Job submission and execution -- ✅ Container-based Python function execution -- ✅ Resource specification and limits -- ✅ Error handling and recovery -- ✅ Concurrent job execution -- ✅ Status tracking and monitoring -- ✅ Job cleanup and TTL management -- ✅ Dependency handling within containers - -## Prerequisites - -### Required Dependencies -```bash -pip install kubernetes # Kubernetes Python client -pip install clustrix[kubernetes] # If using optional dependencies -``` - -### Kubernetes Cluster Options - -#### Option 1: Local Development Clusters - -**minikube** (Recommended for local testing): -```bash -# Install minikube -curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 -sudo install minikube-darwin-amd64 /usr/local/bin/minikube - -# Start cluster -minikube start --driver=docker -minikube kubectl -- config view # Verify kubeconfig -``` - -**Docker Desktop Kubernetes**: -```bash -# Enable in Docker Desktop settings -# Kubernetes > Enable Kubernetes > Apply & Restart -kubectl config use-context docker-desktop -``` - -**kind** (Kubernetes in Docker): -```bash -# Install kind -go install sigs.k8s.io/kind@latest - -# Create cluster -kind create cluster --name clustrix-test -kubectl config use-context kind-clustrix-test -``` - -#### Option 2: Cloud Kubernetes Services - -**AWS EKS**: -```bash -# Install eksctl -brew install eksctl - -# Create cluster -eksctl create cluster --name clustrix-test --region us-west-2 --nodegroup-name standard-nodes --node-type t3.medium --nodes 2 - -# Update kubeconfig -aws eks update-kubeconfig --region us-west-2 --name clustrix-test -``` - -**Google GKE**: -```bash -# Install gcloud CLI -# https://cloud.google.com/sdk/docs/install - -# Create cluster -gcloud container clusters create clustrix-test --zone us-central1-a --num-nodes 2 - -# Update kubeconfig -gcloud container clusters get-credentials clustrix-test --zone us-central1-a -``` - -**Azure AKS**: -```bash -# Install Azure CLI -# https://docs.microsoft.com/en-us/cli/azure/install-azure-cli - -# Create resource group -az group create --name clustrix-test --location eastus - -# Create AKS cluster -az aks create --resource-group clustrix-test --name clustrix-test --node-count 2 --enable-addons monitoring --generate-ssh-keys - -# Update kubeconfig -az aks get-credentials --resource-group clustrix-test --name clustrix-test -``` - -## Configuration - -### Method 1: Local kubeconfig (Recommended) - -Ensure your `~/.kube/config` points to a working cluster: - -```bash -kubectl cluster-info # Verify cluster access -kubectl get nodes # Check node status -``` - -Set optional environment variables: -```bash -export K8S_NAMESPACE=default # Kubernetes namespace (default: default) -export K8S_IMAGE=python:3.11-slim # Container image (default: python:3.11-slim) -export K8S_CONTEXT=my-cluster # Specific context (optional) -``` - -### Method 2: GitHub Actions / CI - -Set repository secrets: -- `KUBECONFIG_CONTENT`: Base64-encoded kubeconfig file -- `K8S_NAMESPACE`: Kubernetes namespace -- `K8S_CONTEXT`: Specific context (optional) - -```yaml -# GitHub Actions example -env: - KUBECONFIG_CONTENT: ${{ secrets.KUBECONFIG_CONTENT }} - K8S_NAMESPACE: clustrix-test -``` - -### Method 4: In-Cluster (Pod-based testing) - -When running inside a Kubernetes cluster, the tests automatically detect the in-cluster service account token at `/var/run/secrets/kubernetes.io/serviceaccount/token`. - -## Running Tests - -### Individual Test Cases -```bash -# Basic functionality -python -m pytest tests/real_world/test_kubernetes_comprehensive.py::TestKubernetesComprehensive::test_kubernetes_simple_function_execution -v - -# Error handling -python -m pytest tests/real_world/test_kubernetes_comprehensive.py::TestKubernetesComprehensive::test_kubernetes_error_handling_and_recovery -v - -# Resource limits -python -m pytest tests/real_world/test_kubernetes_comprehensive.py::TestKubernetesComprehensive::test_kubernetes_resource_specification -v - -# Concurrent jobs -python -m pytest tests/real_world/test_kubernetes_comprehensive.py::TestKubernetesComprehensive::test_kubernetes_concurrent_jobs -v -``` - -### Full Test Suite -```bash -# All Kubernetes tests -python -m pytest tests/real_world/test_kubernetes_comprehensive.py -v -m real_world - -# With detailed logging -python -m pytest tests/real_world/test_kubernetes_comprehensive.py -v -m real_world -s --log-cli-level=INFO -``` - -### Test Behavior - -**With Kubernetes Access**: Tests run against real cluster -**Without Kubernetes Access**: Tests are automatically skipped - -## Troubleshooting - -### Common Issues - -#### "Kubernetes cluster not available" -```bash -# Check kubeconfig -kubectl config current-context -kubectl cluster-info - -# Verify permissions -kubectl auth can-i create jobs -kubectl auth can-i create pods -kubectl auth can-i get pods -``` - -#### "kubernetes package required" -```bash -pip install kubernetes -``` - -#### "Permission denied" errors -```bash -# Check RBAC permissions -kubectl get clusterrolebinding -kubectl describe clusterrolebinding cluster-admin - -# Create service account with job permissions if needed -kubectl create serviceaccount clustrix-test -kubectl create clusterrolebinding clustrix-test --clusterrole=cluster-admin --serviceaccount=default:clustrix-test -``` - -#### Pod fails with "ImagePullBackOff" -```bash -# Check if python:3.11-slim is accessible -kubectl run test-pod --image=python:3.11-slim --rm -it --restart=Never -- python --version - -# Use alternative image if needed -export K8S_IMAGE=python:3.9-slim -``` - -#### Jobs not cleaning up -```bash -# Manual cleanup -kubectl delete jobs -l app=clustrix -kubectl delete pods -l job-name --field-selector=status.phase=Succeeded -``` - -### Debugging Failed Tests - -#### Check job status -```bash -kubectl get jobs -kubectl describe job -``` - -#### Check pod logs -```bash -kubectl get pods -l job-name= -kubectl logs -``` - -#### Check pod events -```bash -kubectl describe pod -``` - -## Test Structure - -Each test follows this pattern: - -1. **Setup**: Create `ClusterConfig` with Kubernetes parameters -2. **Submit**: Submit Python function as Kubernetes Job -3. **Monitor**: Track job status through Kubernetes API -4. **Retrieve**: Read the signed result from the pod log - (`CLUSTRIX_RESULT_B64:` / `CLUSTRIX_RESULT_HMAC:` markers) and verify - its HMAC against the job's `CLUSTRIX_RESULT_KEY` before deserializing -5. **Cleanup**: Delete job and associated resources -6. **Verify**: Assert expected outcomes - -## Integration with Issue #63 - -The Kubernetes test suite addresses **Phase 2** of the external service validation tracker: - -- ✅ **Real Cluster Integration**: No mock tests, only actual Kubernetes clusters -- ✅ **Container Execution**: Validates containerized Python function execution -- ✅ **Resource Management**: Tests CPU/memory limits and requests -- ✅ **Error Handling**: Comprehensive failure scenario testing -- ✅ **Status Monitoring**: Real-time job status tracking via K8s API -- ✅ **Cleanup Validation**: TTL and manual cleanup verification - -This provides complete validation of Clustrix's Kubernetes integration without relying on mock objects or simulations. \ No newline at end of file From 3c2c7013b6363ecbda065486e7bdf28b03fb936d Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:44:34 -0400 Subject: [PATCH 17/56] Remove Kubernetes/Kind from local test infrastructure docker-compose no longer starts a kind control plane; setup_test_infrastructure drops the kind/kubectl dependency checks, the Kind cluster config and RBAC bootstrap, the kubernetes block in test_infrastructure.json, the KUBECONFIG export and the Kind teardown. --- tests/infrastructure/docker-compose.yml | 17 -- .../setup_test_infrastructure.py | 169 ------------------ 2 files changed, 186 deletions(-) diff --git a/tests/infrastructure/docker-compose.yml b/tests/infrastructure/docker-compose.yml index c7be18a1..9a32b1b5 100644 --- a/tests/infrastructure/docker-compose.yml +++ b/tests/infrastructure/docker-compose.yml @@ -4,22 +4,6 @@ version: '3.8' services: - # Local Kubernetes using Kind (Kubernetes in Docker) - kind-control-plane: - image: kindest/node:v1.27.3 - container_name: clustrix-test-k8s - privileged: true - ports: - - "6443:6443" # Kubernetes API - - "30000-32767:30000-32767" # NodePort range - volumes: - - /var/run/docker.sock:/var/run/docker.sock - - kind-data:/var/lib/containerd - environment: - - KUBECONFIG=/etc/kubernetes/admin.conf - networks: - - clustrix-test - # MinIO for S3-compatible storage testing minio: image: minio/minio:latest @@ -114,7 +98,6 @@ services: - clustrix-test volumes: - kind-data: minio-data: postgres-data: redis-data: diff --git a/tests/infrastructure/setup_test_infrastructure.py b/tests/infrastructure/setup_test_infrastructure.py index 1f3ff5ea..7b6b431b 100644 --- a/tests/infrastructure/setup_test_infrastructure.py +++ b/tests/infrastructure/setup_test_infrastructure.py @@ -9,9 +9,7 @@ import subprocess import time import sys -import os from pathlib import Path -import yaml import json @@ -21,15 +19,12 @@ class TestInfrastructureSetup: def __init__(self): self.infrastructure_dir = Path(__file__).parent self.docker_compose_file = self.infrastructure_dir / "docker-compose.yml" - self.kind_config_file = self.infrastructure_dir / "kind-config.yaml" def check_dependencies(self): """Check if required tools are installed.""" dependencies = { "docker": ["docker", "--version"], "docker-compose": ["docker-compose", "--version"], - "kind": ["kind", "--version"], - "kubectl": ["kubectl", "version", "--client"], } missing = [] @@ -50,151 +45,6 @@ def check_dependencies(self): return True - def create_kind_config(self): - """Create Kind cluster configuration.""" - kind_config = { - "kind": "Cluster", - "apiVersion": "kind.x-k8s.io/v1alpha4", - "nodes": [ - { - "role": "control-plane", - "kubeadmConfigPatches": [ - """ -kind: InitConfiguration -nodeRegistration: - kubeletExtraArgs: - node-labels: "clustrix-test=true" -""" - ], - "extraPortMappings": [ - {"containerPort": 30000, "hostPort": 30000, "protocol": "TCP"}, - {"containerPort": 30001, "hostPort": 30001, "protocol": "TCP"}, - ], - }, - { - "role": "worker", - "kubeadmConfigPatches": [ - """ -kind: JoinConfiguration -nodeRegistration: - kubeletExtraArgs: - node-labels: "clustrix-test=true,workload=compute" -""" - ], - }, - { - "role": "worker", - "kubeadmConfigPatches": [ - """ -kind: JoinConfiguration -nodeRegistration: - kubeletExtraArgs: - node-labels: "clustrix-test=true,workload=gpu" -""" - ], - }, - ], - "networking": { - "podSubnet": "10.244.0.0/16", - "serviceSubnet": "10.96.0.0/12", - }, - } - - with open(self.kind_config_file, "w") as f: - yaml.dump(kind_config, f) - - print(f"✅ Created Kind configuration at {self.kind_config_file}") - - def setup_kubernetes(self): - """Setup local Kubernetes cluster using Kind.""" - print("\n🚀 Setting up Kubernetes cluster...") - - # Check if cluster already exists - result = subprocess.run( - ["kind", "get", "clusters"], capture_output=True, text=True - ) - - if "clustrix-test" in result.stdout: - print("ℹ️ Cluster 'clustrix-test' already exists") - return True - - # Create Kind configuration - self.create_kind_config() - - # Create cluster - try: - subprocess.run( - [ - "kind", - "create", - "cluster", - "--name", - "clustrix-test", - "--config", - str(self.kind_config_file), - "--wait", - "5m", - ], - check=True, - ) - print("✅ Kubernetes cluster created successfully") - - # Install basic resources - self.setup_k8s_resources() - - return True - - except subprocess.CalledProcessError as e: - print(f"❌ Failed to create Kubernetes cluster: {e}") - return False - - def setup_k8s_resources(self): - """Setup basic Kubernetes resources for testing.""" - print("📦 Setting up Kubernetes resources...") - - # Create test namespace - namespace_yaml = """ -apiVersion: v1 -kind: Namespace -metadata: - name: clustrix-test - labels: - name: clustrix-test -""" - - # Apply namespace - subprocess.run( - ["kubectl", "apply", "-f", "-"], input=namespace_yaml.encode(), check=True - ) - - # Create service account - sa_yaml = """ -apiVersion: v1 -kind: ServiceAccount -metadata: - name: clustrix-test-sa - namespace: clustrix-test ---- -apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRoleBinding -metadata: - name: clustrix-test-binding -roleRef: - apiGroup: rbac.authorization.k8s.io - kind: ClusterRole - name: cluster-admin -subjects: -- kind: ServiceAccount - name: clustrix-test-sa - namespace: clustrix-test -""" - - subprocess.run( - ["kubectl", "apply", "-f", "-"], input=sa_yaml.encode(), check=True - ) - - print("✅ Kubernetes resources created") - def setup_docker_services(self): """Setup Docker services using docker-compose.""" print("\n🚀 Starting Docker services...") @@ -297,10 +147,6 @@ def create_test_config(self): """Create test configuration file.""" config = { "infrastructure": { - "kubernetes": { - "context": "kind-clustrix-test", - "namespace": "clustrix-test", - }, "ssh": { "host": "localhost", "port": 2222, @@ -335,7 +181,6 @@ def create_test_config(self): env_file = self.infrastructure_dir / "test.env" with open(env_file, "w") as f: f.write("# Test Infrastructure Environment Variables\n") - f.write("export KUBECONFIG=$HOME/.kube/config\n") f.write("export TEST_SSH_HOST=localhost\n") f.write("export TEST_SSH_PORT=2222\n") f.write("export TEST_SSH_USER=testuser\n") @@ -364,10 +209,6 @@ def setup(self): if not self.check_dependencies(): return False - # Setup Kubernetes - if not self.setup_kubernetes(): - print("⚠️ Kubernetes setup failed, continuing with other services...") - # Setup Docker services if not self.setup_docker_services(): return False @@ -377,7 +218,6 @@ def setup(self): print("\n✨ Test infrastructure setup complete!") print("\nServices available:") - print(" • Kubernetes: kubectl --context kind-clustrix-test") print(" • SSH Server: ssh -p 2222 testuser@localhost") print(" • MinIO (S3): http://localhost:9001 (admin/admin)") print(" • PostgreSQL: psql -h localhost -U clustrix clustrix_test") @@ -400,15 +240,6 @@ def teardown(self): except: print("⚠️ Failed to stop Docker services") - # Delete Kind cluster - try: - subprocess.run( - ["kind", "delete", "cluster", "--name", "clustrix-test"], check=True - ) - print("✅ Kubernetes cluster deleted") - except: - print("⚠️ Failed to delete Kubernetes cluster") - print("✨ Teardown complete") From c6b8467265ac01f1789b9edfd29767dff7545142 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:45:01 -0400 Subject: [PATCH 18/56] Tests: drop removed-backend cases from config/cli/utils/scheduler tests Deletes tests/unit/test_backends_placeholder_hosts.py (Azure/GCP/Lambda providers are gone), the PBS and SGE job-script tests in test_utils.py, and the PBS/SGE parametrisations in test_backends_schedulers.py. Adds coverage for the new load_config errors on removed cluster_types and settings, and makes test_all_cluster_types read SUPPORTED_CLUSTER_TYPES instead of a stale hardcoded list. --- clustrix/profile_manager.py | 22 -- tests/real_world/test_visual_verification.py | 41 ++-- tests/test_cli.py | 4 +- tests/test_config.py | 153 ++++++++++++- tests/test_modern_widget.py | 21 +- tests/test_modern_widget_comprehensive.py | 15 +- tests/test_notebook_magic.py | 34 +-- tests/test_notebook_magic_extended.py | 27 +-- tests/test_notebook_magic_real.py | 24 +- tests/test_utils.py | 36 --- tests/test_widget_fixes.py | 212 +++++------------- tests/unit/test_backends_placeholder_hosts.py | 72 ------ tests/unit/test_backends_schedulers.py | 29 +-- .../test_billable_and_realworld_isolation.py | 1 - tests/unit/test_widget_profiles.py | 16 +- tests/unit/test_widget_validation.py | 26 +-- 16 files changed, 305 insertions(+), 428 deletions(-) delete mode 100644 tests/unit/test_backends_placeholder_hosts.py diff --git a/clustrix/profile_manager.py b/clustrix/profile_manager.py index 336fc7d6..60f262ef 100644 --- a/clustrix/profile_manager.py +++ b/clustrix/profile_manager.py @@ -69,20 +69,6 @@ def __init__(self, config_dir: Optional[str] = None): "default_time": "02:00:00", "remote_work_dir": "~/.clustrix/jobs", }, - "PBS cluster": { - "cluster_type": "pbs", - "default_cores": 8, - "default_memory": "32GB", - "default_time": "02:00:00", - "remote_work_dir": "~/.clustrix/jobs", - }, - "SGE cluster": { - "cluster_type": "sge", - "default_cores": 8, - "default_memory": "32GB", - "default_time": "02:00:00", - "remote_work_dir": "~/.clustrix/jobs", - }, "SSH remote machine": { "cluster_type": "ssh", "default_cores": 4, @@ -90,14 +76,6 @@ def __init__(self, config_dir: Optional[str] = None): "default_time": "01:00:00", "remote_work_dir": "~/.clustrix/jobs", }, - "Kubernetes": { - "cluster_type": "kubernetes", - "default_cores": 4, - "default_memory": "8GB", - "default_time": "01:00:00", - "k8s_namespace": "default", - "k8s_image": "python:3.12-slim", - }, "HuggingFace Jobs (CPU)": { "cluster_type": "huggingface", "hf_flavor": "cpu-basic", diff --git a/tests/real_world/test_visual_verification.py b/tests/real_world/test_visual_verification.py index 8cefb12e..8a968038 100644 --- a/tests/real_world/test_visual_verification.py +++ b/tests/real_world/test_visual_verification.py @@ -45,7 +45,8 @@ def test_modern_widget_html_output(self): html_file.parent.mkdir(parents=True, exist_ok=True) with open(html_file, "w") as f: - f.write(f""" + f.write( + f""" @@ -101,7 +102,8 @@ def test_modern_widget_html_output(self): -""") +""" + ) assert html_file.exists() print(f"Widget HTML saved to: {html_file}") @@ -132,7 +134,8 @@ def test_enhanced_widget_html_output(self): html_file.parent.mkdir(parents=True, exist_ok=True) with open(html_file, "w") as f: - f.write(f""" + f.write( + f""" @@ -184,7 +187,8 @@ def test_enhanced_widget_html_output(self): -""") +""" + ) assert html_file.exists() print(f"Enhanced widget HTML saved to: {html_file}") @@ -267,9 +271,10 @@ def test_widget_configuration_output(self): ), ), ( - "AWS Batch", + "HuggingFace Jobs", ClusterConfig( - cluster_type="aws", + cluster_type="huggingface", + hf_namespace="contextlab", default_cores=4, default_memory="8GB", default_time="01:00:00", @@ -356,7 +361,8 @@ def test_widget_accessibility_features(self): accessibility_file.parent.mkdir(parents=True, exist_ok=True) with open(accessibility_file, "w") as f: - f.write(f""" + f.write( + f""" @@ -440,7 +446,8 @@ def test_widget_accessibility_features(self): -""") +""" + ) assert accessibility_file.exists() print(f"Accessibility report saved to: {accessibility_file}") @@ -480,7 +487,8 @@ def test_widget_responsive_design(self): responsive_file.parent.mkdir(parents=True, exist_ok=True) with open(responsive_file, "w") as f: - f.write(f""" + f.write( + f""" @@ -567,7 +575,8 @@ def test_widget_responsive_design(self): -""") +""" + ) assert responsive_file.exists() print(f"Responsive design report saved to: {responsive_file}") @@ -613,7 +622,8 @@ def test_widget_comparison_report(self): comparison_file.parent.mkdir(parents=True, exist_ok=True) with open(comparison_file, "w") as f: - f.write(f""" + f.write( + f""" @@ -735,7 +745,8 @@ def test_widget_comparison_report(self): -""") +""" + ) assert comparison_file.exists() print(f"Widget comparison report saved to: {comparison_file}") @@ -855,7 +866,8 @@ def test_widget_screenshot_simulation(self): index_file = Path("tests/real_world/screenshots/index.html") with open(index_file, "w") as f: - f.write(f""" + f.write( + f""" @@ -940,7 +952,8 @@ def test_widget_screenshot_simulation(self): -""") +""" + ) assert index_file.exists() print(f"Visual test index saved to: {index_file}") diff --git a/tests/test_cli.py b/tests/test_cli.py index 28a93511..19261f21 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -54,7 +54,7 @@ def test_config_set_values(self, mock_get_config, mock_configure, runner): [ "config", "--cluster-type", - "pbs", + "ssh", "--cluster-host", "new.cluster.com", "--username", @@ -73,7 +73,7 @@ def test_config_set_values(self, mock_get_config, mock_configure, runner): # Verify configure was called with correct parameters mock_configure.assert_called_once_with( - cluster_type="pbs", + cluster_type="ssh", cluster_host="new.cluster.com", username="newuser", default_cores=16, diff --git a/tests/test_config.py b/tests/test_config.py index d147692c..77e96f68 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -3,6 +3,8 @@ from pathlib import Path from clustrix.config import ( ClusterConfig, + REMOVED_CLUSTER_TYPES, + SUPPORTED_CLUSTER_TYPES, configure, get_config, load_config, @@ -29,13 +31,13 @@ def test_default_initialization(self): def test_custom_initialization(self): """Test custom configuration values.""" config = ClusterConfig( - cluster_type="pbs", + cluster_type="ssh", cluster_host="custom.host.com", username="testuser", default_cores=8, default_memory="16GB", ) - assert config.cluster_type == "pbs" + assert config.cluster_type == "ssh" assert config.cluster_host == "custom.host.com" assert config.username == "testuser" assert config.default_cores == 8 @@ -74,10 +76,10 @@ def test_configure_invalid_parameter(self): def test_get_config(self): """Test get_config returns current configuration.""" - configure(cluster_type="kubernetes") + configure(cluster_type="huggingface") config = get_config() assert isinstance(config, ClusterConfig) - assert config.cluster_type == "kubernetes" + assert config.cluster_type == "huggingface" class TestConfigFileOperations: @@ -120,7 +122,7 @@ def test_save_load_json(self, temp_dir): # Configure and save configure( - cluster_type="pbs", + cluster_type="ssh", cluster_host="json.test.com", username="jsonuser", default_memory="64GB", @@ -132,7 +134,7 @@ def test_save_load_json(self, temp_dir): load_config(str(config_path)) config = get_config() - assert config.cluster_type == "pbs" + assert config.cluster_type == "ssh" assert config.cluster_host == "json.test.com" assert config.username == "jsonuser" assert config.default_memory == "64GB" @@ -147,7 +149,7 @@ def test_load_default_config(self, temp_dir, monkeypatch): # Create a test config file config_path = Path(temp_dir) / "clustrix.yml" test_config = { - "cluster_type": "sge", + "cluster_type": "huggingface", "cluster_host": "default.test.com", "username": "defaultuser", } @@ -163,7 +165,7 @@ def test_load_default_config(self, temp_dir, monkeypatch): _load_default_config() config = get_config() - assert config.cluster_type == "sge" + assert config.cluster_type == "huggingface" assert config.cluster_host == "default.test.com" assert config.username == "defaultuser" @@ -173,9 +175,12 @@ class TestConfigContent: def test_all_cluster_types(self): """Test all supported cluster types.""" - cluster_types = ["slurm", "pbs", "sge", "kubernetes", "ssh"] + # Read the tuple rather than keeping a second copy: the old hardcoded + # list still named pbs/sge/kubernetes long after those backends were + # removed, so it asserted nothing about what clustrix supports. + assert SUPPORTED_CLUSTER_TYPES == ("local", "ssh", "slurm", "huggingface") - for cluster_type in cluster_types: + for cluster_type in SUPPORTED_CLUSTER_TYPES: configure(cluster_type=cluster_type) config = get_config() assert config.cluster_type == cluster_type @@ -209,3 +214,131 @@ def test_path_configurations(self): assert config.remote_work_dir == "/scratch/user/clustrix" assert config.local_cache_dir == "/tmp/clustrix_cache" assert config.key_file == "/home/user/.ssh/cluster_key" + + +class TestRemovedBackendsAreExplained: + """A config file naming a deleted backend must say what happened to it. + + Real files on disk, real ``load_config`` calls -- nothing is stubbed. The + point of every assertion here is the *content* of the message: before the + removal work, ``cluster_type: pbs`` was accepted silently and a stale + ``k8s_namespace`` key came back through difflib pointing at an unrelated + field, so the user was sent after the wrong thing. + """ + + @pytest.mark.parametrize( + "cluster_type,issue", + [ + ("pbs", 140), + ("sge", 141), + ("kubernetes", 142), + ("aws", 143), + ("gcp", 144), + ("azure", 145), + ("lambda_cloud", 146), + ], + ) + def test_removed_cluster_type_names_backend_reason_and_issue( + self, tmp_path, cluster_type, issue + ): + config_path = tmp_path / "clustrix.yml" + config_path.write_text( + yaml.safe_dump({"cluster_type": cluster_type, "username": "someone"}) + ) + + with pytest.raises(ValueError) as excinfo: + load_config(str(config_path)) + + message = str(excinfo.value) + assert cluster_type in message + assert "no longer implements" in message + assert "never been verified against real hardware" in message + assert f"#{issue}" in message + for supported in SUPPORTED_CLUSTER_TYPES: + assert supported in message + # The file that caused it, so the user knows which one to edit. + assert str(config_path) in message + + def test_the_removed_type_table_matches_the_issues_that_track_them(self): + assert REMOVED_CLUSTER_TYPES["pbs"] == 140 + assert REMOVED_CLUSTER_TYPES["sge"] == 141 + assert REMOVED_CLUSTER_TYPES["kubernetes"] == 142 + assert REMOVED_CLUSTER_TYPES["aws"] == 143 + assert REMOVED_CLUSTER_TYPES["gcp"] == 144 + assert REMOVED_CLUSTER_TYPES["azure"] == 145 + assert REMOVED_CLUSTER_TYPES["lambda_cloud"] == 146 + # No removed name may still be advertised as supported. + assert not set(REMOVED_CLUSTER_TYPES) & set(SUPPORTED_CLUSTER_TYPES) + + def test_a_supported_cluster_type_still_loads(self, tmp_path): + config_path = tmp_path / "clustrix.yml" + config_path.write_text( + yaml.safe_dump({"cluster_type": "slurm", "cluster_host": "hpc.example"}) + ) + + load_config(str(config_path)) + + assert get_config().cluster_type == "slurm" + assert get_config().cluster_host == "hpc.example" + + @pytest.mark.parametrize( + "setting,value,what,issue", + [ + ("k8s_namespace", "default", "Kubernetes", 142), + ("k8s_image", "python:3.11", "Kubernetes", 142), + ("auto_provision_k8s", True, "Kubernetes", 142), + ("aws_region", "us-east-1", "the AWS backend", 143), + ("eks_cluster_name", "prod", "the AWS backend", 143), + ("gcp_project_id", "proj", "the GCP backend", 144), + ("azure_subscription_id", "sub", "the Azure backend", 145), + ("lambda_api_key", "k", "the Lambda Cloud backend", 146), + ], + ) + def test_removed_setting_is_explained_not_guessed_at( + self, tmp_path, setting, value, what, issue + ): + config_path = tmp_path / "clustrix.yml" + config_path.write_text(yaml.safe_dump({"cluster_type": "ssh", setting: value})) + + with pytest.raises(ValueError) as excinfo: + load_config(str(config_path)) + + message = str(excinfo.value) + assert setting in message + assert f"{setting} configured {what}" in message + assert "has been removed" in message + assert f"#{issue}" in message + # The old difflib path would have offered some unrelated field. + assert "did you mean" not in message + + @pytest.mark.parametrize( + "setting,value,what", + [ + ("cloud_provider", "aws", "the cloud VM backends"), + ("cloud_region", "us-east-1", "the cloud VM backends"), + ("cloud_auto_configure", True, "the cloud VM backends"), + ("cost_monitoring", True, "cloud cost monitoring"), + ], + ) + def test_removed_setting_without_a_tracking_issue_still_explains_itself( + self, tmp_path, setting, value, what + ): + config_path = tmp_path / "clustrix.yml" + config_path.write_text(yaml.safe_dump({setting: value})) + + with pytest.raises(ValueError) as excinfo: + load_config(str(config_path)) + + message = str(excinfo.value) + assert f"{setting} configured {what}, which has been removed" in message + assert "did you mean" not in message + + def test_a_genuine_typo_still_gets_the_did_you_mean_hint(self, tmp_path): + """The removed-setting path must not have swallowed the typo hint.""" + config_path = tmp_path / "clustrix.yml" + config_path.write_text(yaml.safe_dump({"cluster_hostt": "hpc.example"})) + + with pytest.raises(ValueError) as excinfo: + load_config(str(config_path)) + + assert "did you mean cluster_host?" in str(excinfo.value) diff --git a/tests/test_modern_widget.py b/tests/test_modern_widget.py index 1bf8dc16..5d0ab6dc 100644 --- a/tests/test_modern_widget.py +++ b/tests/test_modern_widget.py @@ -31,9 +31,6 @@ def test_every_backend_has_a_starting_profile(self): "local", "ssh", "slurm", - "pbs", - "sge", - "kubernetes", "huggingface", } @@ -164,28 +161,28 @@ def test_export_import_profile(self): # Create a custom profile config = ClusterConfig( - cluster_type="pbs", + cluster_type="slurm", default_cores=8, default_memory="32GB", default_time="04:00:00", ) - pm.create_profile("PBS Cluster", config) + pm.create_profile("SLURM Cluster", config) # Export the profile - export_file = os.path.join(temp_dir, "pbs_profile.yml") - pm.export_profile("PBS Cluster", export_file) + export_file = os.path.join(temp_dir, "slurm_profile.yml") + pm.export_profile("SLURM Cluster", export_file) assert os.path.exists(export_file) # Create new ProfileManager and import pm2 = ProfileManager(config_dir=temp_dir) - imported_name = pm2.import_profile(export_file, "Imported PBS") + imported_name = pm2.import_profile(export_file, "Imported SLURM") - assert imported_name == "Imported PBS" - assert "Imported PBS" in pm2.get_profile_names() + assert imported_name == "Imported SLURM" + assert "Imported SLURM" in pm2.get_profile_names() - imported_config = pm2.load_profile("Imported PBS") - assert imported_config.cluster_type == "pbs" + imported_config = pm2.load_profile("Imported SLURM") + assert imported_config.cluster_type == "slurm" assert imported_config.default_cores == 8 diff --git a/tests/test_modern_widget_comprehensive.py b/tests/test_modern_widget_comprehensive.py index 5db59b3f..4120450c 100644 --- a/tests/test_modern_widget_comprehensive.py +++ b/tests/test_modern_widget_comprehensive.py @@ -305,8 +305,7 @@ def test_cluster_row_components(self, mock_ipython_env, temp_profile_manager): # Check cluster type dropdown cluster_type = widget.widgets["cluster_type"] assert cluster_type.value == "local" - assert "slurm" in cluster_type.options - assert "kubernetes" in cluster_type.options + assert list(cluster_type.options) == ["local", "ssh", "slurm", "huggingface"] # Check resource fields. Values come from the active profile # ("Local single-core" in BUILTIN_PROFILES), whose default_memory is @@ -552,7 +551,7 @@ def test_load_config_to_widgets(self, mock_ipython_env, temp_profile_manager): # Create a config config = ClusterConfig( - cluster_type="pbs", + cluster_type="slurm", default_cores=16, default_memory="64GB", default_time="04:00:00", @@ -564,7 +563,7 @@ def test_load_config_to_widgets(self, mock_ipython_env, temp_profile_manager): widget._load_config_to_widgets(config) # Verify widget values - assert widget.widgets["cluster_type"].value == "pbs" + assert widget.widgets["cluster_type"].value == "slurm" assert widget.widgets["cpus"].value == 16 assert widget.widgets["ram"].value == "64GB" # Now string format assert widget.widgets["time"].value == "04:00:00" @@ -579,7 +578,7 @@ def test_config_roundtrip(self, mock_ipython_env, temp_profile_manager): # Original config original_config = ClusterConfig( - cluster_type="sge", + cluster_type="slurm", default_cores=24, default_memory="128GB", default_time="08:00:00", @@ -705,7 +704,8 @@ def test_load_config_handler(self, mock_ipython_env, temp_profile_manager): # Create a test file first with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f: - f.write(""" + f.write( + """ active_profile: Test Profile profiles: Test Profile: @@ -713,7 +713,8 @@ def test_load_config_handler(self, mock_ipython_env, temp_profile_manager): default_cores: 4 default_memory: 8GB default_time: 01:30:00 -""") +""" + ) test_file = f.name try: diff --git a/tests/test_notebook_magic.py b/tests/test_notebook_magic.py index 7464991a..430a014b 100644 --- a/tests/test_notebook_magic.py +++ b/tests/test_notebook_magic.py @@ -113,8 +113,8 @@ def test_load_config_from_json_file(self): """Test loading configuration from JSON file.""" test_config = { "test_cluster": { - "cluster_type": "kubernetes", - "cluster_host": "k8s.example.com", + "cluster_type": "slurm", + "cluster_host": "hpc.example.com", "default_cores": 4, } } @@ -321,8 +321,6 @@ def test_save_config_from_widgets(self, mock_ipython_environment): widget.work_dir_field.value = "/tmp/clustrix" widget.ssh_key_field = MagicMock() widget.ssh_key_field.value = "" - widget.cost_monitoring_checkbox = MagicMock() - widget.cost_monitoring_checkbox.value = True # Test save functionality config = widget._save_config_from_widgets() @@ -333,7 +331,6 @@ def test_save_config_from_widgets(self, mock_ipython_environment): assert config["default_cores"] == 8 assert config["default_memory"] == "32GB" assert config["package_manager"] == "conda" - assert config["cost_monitoring"] is True def test_load_config_to_widgets(self, mock_ipython_environment): """Test loading configuration into widgets.""" @@ -346,26 +343,22 @@ def test_load_config_to_widgets(self, mock_ipython_environment): widget.port_field = MagicMock() widget.cores_field = MagicMock() widget.memory_field = MagicMock() - widget.k8s_namespace_field = MagicMock() widget.package_manager = MagicMock() widget.username_field = MagicMock() widget.ssh_key_field = MagicMock() widget.work_dir_field = MagicMock() widget.time_field = MagicMock() widget.env_vars_field = MagicMock() - widget.k8s_image_field = MagicMock() - widget.cost_monitoring_checkbox = MagicMock() test_config = { "name": "Test Load Config", - "cluster_type": "kubernetes", - "cluster_host": "k8s.example.com", + "cluster_type": "slurm", + "cluster_host": "hpc.example.com", "cluster_port": 443, "default_cores": 12, "default_memory": "64GB", - "k8s_namespace": "production", + "queue": "production", "package_manager": "uv", - "cost_monitoring": True, } # Add test config and load it widget.configs["test_load"] = test_config @@ -375,14 +368,13 @@ def test_load_config_to_widgets(self, mock_ipython_environment): assert ( widget.config_name.value == "Test Load Config" ) # Uses the "name" field from test_config - assert widget.cluster_type.value == "kubernetes" - assert widget.host_field.value == "k8s.example.com" + assert widget.cluster_type.value == "slurm" + assert widget.host_field.value == "hpc.example.com" assert widget.port_field.value == 443 assert widget.cores_field.value == 12 assert widget.memory_field.value == "64GB" - assert widget.k8s_namespace_field.value == "production" + assert widget.queue_field.value == "production" assert widget.package_manager.value == "uv" - assert widget.cost_monitoring_checkbox.value is True def test_cluster_type_field_visibility(self, mock_ipython_environment): """Test field visibility changes based on cluster type.""" @@ -397,8 +389,8 @@ def test_cluster_type_field_visibility(self, mock_ipython_environment): # Test SSH cluster type (should show SSH fields) widget._on_cluster_type_change({"new": "ssh"}) # In a real environment, fields would be shown/hidden - # Test Kubernetes cluster type (should show K8s fields) - widget._on_cluster_type_change({"new": "kubernetes"}) + # Test HuggingFace cluster type (should show HF Jobs fields) + widget._on_cluster_type_change({"new": "huggingface"}) # In a real environment, different fields would be shown/hidden # The key test is that the method executes without error assert True # Method executed successfully @@ -669,8 +661,6 @@ def test_save_load_cycle(self, mock_ipython_environment, monkeypatch): widget.module_loads_field.value = "" widget.pre_exec_commands = MagicMock() widget.pre_exec_commands.value = "" - widget.cost_monitoring_checkbox = MagicMock() - widget.cost_monitoring_checkbox.value = False # Mock the new filename input field widget.save_filename_input = MagicMock() widget.save_filename_input.value = "clustrix.yml" @@ -780,8 +770,6 @@ def test_save_all_configurations(self, mock_ipython_environment, monkeypatch): widget.work_dir_field.value = "/tmp/clustrix" widget.ssh_key_field = MagicMock() widget.ssh_key_field.value = "" - widget.cost_monitoring_checkbox = MagicMock() - widget.cost_monitoring_checkbox.value = False widget.save_filename_input = MagicMock() widget.save_filename_input.value = "test_all_configs.yml" widget.status_output = MagicMock() @@ -850,8 +838,6 @@ def test_test_configuration_functionality(self, mock_ipython_environment): widget.work_dir_field.value = "/tmp/clustrix" widget.ssh_key_field = MagicMock() widget.ssh_key_field.value = "~/.ssh/id_rsa" - widget.cost_monitoring_checkbox = MagicMock() - widget.cost_monitoring_checkbox.value = False widget.status_output = MagicMock() widget.status_output.clear_output = MagicMock() diff --git a/tests/test_notebook_magic_extended.py b/tests/test_notebook_magic_extended.py index 9d483dab..632dc5ee 100644 --- a/tests/test_notebook_magic_extended.py +++ b/tests/test_notebook_magic_extended.py @@ -27,19 +27,18 @@ class TestDefaultConfigsExtended: def test_all_cluster_types_present(self): """Test that all expected cluster types are present in defaults.""" - expected_types = {"local", "ssh", "slurm", "pbs", "sge", "kubernetes"} + expected_types = {"local", "ssh", "slurm", "huggingface"} actual_types = {config["cluster_type"] for config in DEFAULT_CONFIGS.values()} - assert expected_types.issubset(actual_types) + assert actual_types == expected_types def test_cluster_specific_fields(self): """Test cluster-specific fields in default configs.""" for config_name, config in DEFAULT_CONFIGS.items(): cluster_type = config["cluster_type"] - if cluster_type == "kubernetes": - assert "k8s_namespace" in config - assert "k8s_image" in config - elif cluster_type in ["slurm", "pbs", "sge"]: + if cluster_type == "huggingface": + assert "hf_hardware" in config + elif cluster_type == "slurm": assert "cluster_host" in config assert "username" in config assert "default_time" in config @@ -56,10 +55,10 @@ def test_config_name_consistency(self): if cluster_type == "local": assert "Local" in config_name - elif cluster_type == "kubernetes": - assert "Kubernetes" in config_name or "K8s" in config_name - elif cluster_type in ["slurm", "pbs", "sge"]: - assert any(x in config_name for x in ["SLURM", "PBS", "SGE", "Cluster"]) + elif cluster_type == "huggingface": + assert "HuggingFace" in config_name + elif cluster_type == "slurm": + assert any(x in config_name for x in ["SLURM", "Cluster"]) class TestConfigFileOperations: @@ -360,13 +359,10 @@ def test_widget_save_with_invalid_data(self, mock_widget_environment): "port_field", "work_dir_field", "ssh_key_field", - "cost_monitoring_checkbox", ]: field = MagicMock() if field_name in ["cores_field", "port_field"]: field.value = 1 - elif field_name == "cost_monitoring_checkbox": - field.value = False else: field.value = "" setattr(widget, field_name, field) @@ -574,7 +570,7 @@ def test_widget_field_updates(self, mock_widget_environment): # Should update dropdown options # Test cluster type change handling - for cluster_type in ["local", "ssh", "kubernetes", "slurm", "pbs", "sge"]: + for cluster_type in ["local", "ssh", "slurm", "huggingface"]: change_event = {"new": cluster_type} widget._on_cluster_type_change(change_event) # Should handle each cluster type @@ -726,13 +722,10 @@ def test_config_save_with_file_system_errors(self, mock_widget_environment): "port_field", "work_dir_field", "ssh_key_field", - "cost_monitoring_checkbox", ]: field = MagicMock() if field_name in ["cores_field", "port_field"]: field.value = 1 - elif field_name == "cost_monitoring_checkbox": - field.value = False else: field.value = "" setattr(widget, field_name, field) diff --git a/tests/test_notebook_magic_real.py b/tests/test_notebook_magic_real.py index 23d5494d..14df51dc 100644 --- a/tests/test_notebook_magic_real.py +++ b/tests/test_notebook_magic_real.py @@ -107,8 +107,8 @@ def test_config_file_detection(self, temp_config_dir): # Write JSON config json_data = { - "cluster_type": "kubernetes", - "namespace": "default", + "cluster_type": "huggingface", + "hf_flavor": "cpu-basic", "default_cores": 4, "default_memory": "8GB", } @@ -117,7 +117,7 @@ def test_config_file_detection(self, temp_config_dir): # Write custom config custom_data = { - "cluster_type": "pbs", + "cluster_type": "slurm", "cluster_host": "cluster.edu", "queue": "batch", } @@ -181,12 +181,12 @@ def test_load_config_from_json(self, temp_config_dir): # Write valid JSON config config_data = { - "cluster_type": "kubernetes", - "namespace": "ml-workloads", + "cluster_type": "slurm", + "queue": "ml-workloads", "default_cores": 8, "default_memory": "16Gi", "gpu": 1, - "node_selector": {"workload": "gpu", "tier": "production"}, + "module_loads": {"workload": "gpu", "tier": "production"}, } with open(config_file, "w") as f: @@ -195,10 +195,10 @@ def test_load_config_from_json(self, temp_config_dir): # Load and validate loaded_config = load_config_from_file(str(config_file)) - assert loaded_config["cluster_type"] == "kubernetes" - assert loaded_config["namespace"] == "ml-workloads" + assert loaded_config["cluster_type"] == "slurm" + assert loaded_config["queue"] == "ml-workloads" assert loaded_config["gpu"] == 1 - assert loaded_config["node_selector"]["workload"] == "gpu" + assert loaded_config["module_loads"]["workload"] == "gpu" def test_validate_ip_address(self): """ @@ -498,8 +498,10 @@ def analyze_dataset(n_samples): results = ip.user_ns["results"] assert results["n_samples"] == 1000 - assert results["original_shape"] == [1000, 20] - assert results["reduced_shape"] == [1000, 5] + # numpy's .shape is a tuple, and the cell runs in this process, so + # nothing converts it to a list on the way out. + assert results["original_shape"] == (1000, 20) + assert results["reduced_shape"] == (1000, 5) assert len(results["explained_variance"]) == 5 assert 0 < results["total_variance_explained"] <= 1 diff --git a/tests/test_utils.py b/tests/test_utils.py index 4887341e..f4fabaa2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -286,42 +286,6 @@ def test_create_job_script_slurm(self): assert "function_data.pkl" in script assert "result.pkl" in script - def test_create_job_script_pbs(self): - """Test PBS script generation with detailed validation.""" - config = ClusterConfig( - remote_work_dir="/home/test", python_executable="python3" - ) - - job_config = {"cores": 4, "memory": "8GB", "time": "01:00:00", "queue": "batch"} - - script = create_job_script("pbs", job_config, "/home/test/jobs/job_456", config) - - # Check PBS directives - assert "#!/bin/bash" in script - assert "#PBS -N clustrix" in script - assert "#PBS -l nodes=1:ppn=4" in script - assert "#PBS -l mem=8gb" in script # PBS spells it lowercase - assert "#PBS -l walltime=01:00:00" in script - assert "#PBS -q batch" in script - - # Check working directory - assert "cd /home/test/jobs/job_456" in script - - # Check execution setup - assert "source venv/bin/activate" in script - - def test_create_job_script_sge(self): - """Test SGE script generation.""" - config = ClusterConfig() - job_config = {"cores": 4, "memory": "8GB", "time": "01:00:00"} - - result = create_job_script("sge", job_config, "/tmp/job", config) - assert result is not None - assert "#$ -N clustrix" in result - assert "#$ -pe smp 4" in result - assert "#$ -l h_vmem=8G" in result - assert "cd /tmp/job" in result - def test_create_job_script_ssh(self): """Test SSH script generation.""" config = ClusterConfig( diff --git a/tests/test_widget_fixes.py b/tests/test_widget_fixes.py index dbf7b6bd..929873fe 100644 --- a/tests/test_widget_fixes.py +++ b/tests/test_widget_fixes.py @@ -42,32 +42,19 @@ def test_default_configs_compatibility(self): cluster_config = ClusterConfig(**test_data) assert cluster_config.cluster_type == config_data["cluster_type"] - def test_cloud_provider_field_mapping(self): - """Test that cloud provider configurations have correct field mappings.""" - # Test AWS configuration - aws_config = DEFAULT_CONFIGS["AWS EC2 Cluster"] - assert "aws_region" in aws_config - assert "aws_instance_type" in aws_config - assert "aws_cluster_type" in aws_config - - # Test Azure configuration - azure_config = DEFAULT_CONFIGS["Azure VM Cluster"] - assert "azure_region" in azure_config - assert "azure_instance_type" in azure_config - - # Test GCP configuration - gcp_config = DEFAULT_CONFIGS["Google Cloud VM"] - assert "gcp_region" in gcp_config - assert "gcp_instance_type" in gcp_config - - # Test Lambda Cloud configuration - lambda_config = DEFAULT_CONFIGS["Lambda Cloud GPU"] - assert "lambda_instance_type" in lambda_config - - # Test HuggingFace configuration - hf_config = DEFAULT_CONFIGS["HuggingFace Space"] + def test_default_configs_only_offer_retained_backends(self): + """Every shipped template must name a backend clustrix still has.""" + retained = {"local", "ssh", "slurm", "huggingface"} + for config_name, config_data in DEFAULT_CONFIGS.items(): + assert ( + config_data["cluster_type"] in retained + ), f"Config '{config_name}' targets removed backend {config_data['cluster_type']}" + + def test_huggingface_config_field_mapping(self): + """The HuggingFace template carries the fields hf_jobs.py reads.""" + hf_config = DEFAULT_CONFIGS["HuggingFace Jobs"] + assert hf_config["cluster_type"] == "huggingface" assert "hf_hardware" in hf_config - assert "hf_sdk" in hf_config @pytest.mark.skipif( not WIDGET_DEPS_AVAILABLE, reason="Widget dependencies not available" @@ -76,38 +63,24 @@ def test_widget_safe_value_setting(self): """Test that widget safely handles values not in dropdown options.""" widget = ClusterConfigWidget(auto_display=False) - # Test configuration with values not in default dropdown options + # A hardware flavor that postdates the widget's hardcoded option list. test_config = { - "cluster_type": "azure", - "azure_region": "nonexistent-region", # Not in default options - "azure_instance_type": "nonexistent-instance", # Not in default options - "azure_subscription_id": "test-sub-id", - "azure_client_id": "test-client-id", - "azure_client_secret": "test-secret", + "cluster_type": "huggingface", + "hf_hardware": "nonexistent-flavor", + "hf_token": "test-hf-token", } # Add config to widget widget.configs["test_config"] = test_config - # KNOWN FAILURE (shipped-code regression): commit 46ad192 dropped the - # "if value in options" guards this test was written for (issue #53), so - # _load_config_to_widgets now raises TraitError on any dropdown value - # that is not in the widget's hardcoded option list. - # Should not crash when loading the configuration - # Values not in dropdown should fall back to defaults + # Should not crash when loading the configuration: the saved value is + # authoritative, so _set_choice widens the options rather than raising + # TraitError (issue #53). widget._load_config_to_widgets("test_config") - # Verify that text fields are set correctly - assert widget.azure_subscription_field.value == "test-sub-id" - assert widget.azure_client_id_field.value == "test-client-id" - assert widget.azure_client_secret_field.value == "test-secret" - - # Verify that dropdown fields fall back to safe defaults - assert widget.azure_region_field.value in widget.azure_region_field.options - assert ( - widget.azure_instance_type_field.value - in widget.azure_instance_type_field.options - ) + assert widget.hf_token_field.value == "test-hf-token" + assert widget.hf_hardware_field.value == "nonexistent-flavor" + assert "nonexistent-flavor" in widget.hf_hardware_field.options @pytest.mark.skipif( not WIDGET_DEPS_AVAILABLE, reason="Widget dependencies not available" @@ -116,91 +89,25 @@ def test_widget_save_load_cycle(self): """Test that widget can save and load configurations correctly.""" widget = ClusterConfigWidget(auto_display=False) - # KNOWN FAILURE (shipped-code regression): typing into config_name - # re-keys self.configs, which rebuilds the dropdown options, which - # re-fires _on_config_select and reloads the stored config over every - # edit made beforehand -- so the values set below are discarded. - # Set up a complete cloud provider configuration - widget.cluster_type.value = "aws" - widget.aws_region_field.value = "us-east-1" # Use value that exists in options - widget.aws_instance_type_field.value = ( - "t3.medium" # Use value that exists in options - ) - widget.aws_access_key_field.value = "test-access-key" - widget.aws_secret_key_field.value = "test-secret-key" - widget.aws_cluster_type_field.value = "ec2" - widget.config_name.value = "Test AWS Config" + widget.cluster_type.value = "huggingface" + widget.hf_hardware_field.value = "t4-small" + widget.hf_sdk_field.value = "gradio" + widget.hf_token_field.value = "test-hf-token" # Save configuration saved_config = widget._save_config_from_widgets() # Verify saved configuration - assert saved_config["cluster_type"] == "aws" - assert saved_config["aws_region"] == "us-east-1" - assert saved_config["aws_instance_type"] == "t3.medium" - # Credentials are saved under the boto3 field names, which are the ones - # ClusterConfig/executor_cloud actually read. - assert saved_config["aws_access_key_id"] == "test-access-key" - assert saved_config["aws_secret_access_key"] == "test-secret-key" - assert saved_config["aws_cluster_type"] == "ec2" - - def test_cloud_provider_fields_in_config(self): - """Test that ClusterConfig supports all cloud provider fields used by widget.""" - # Test AWS fields - aws_config = ClusterConfig( - cluster_type="aws", - aws_region="us-west-2", - aws_instance_type="t3.large", - aws_access_key="test-key", - aws_secret_key="test-secret", - aws_cluster_type="ec2", - ) - assert aws_config.aws_region == "us-west-2" - assert aws_config.aws_instance_type == "t3.large" - assert aws_config.aws_access_key == "test-key" - assert aws_config.aws_secret_key == "test-secret" - assert aws_config.aws_cluster_type == "ec2" - - # Test Azure fields - azure_config = ClusterConfig( - cluster_type="azure", - azure_region="westus", - azure_instance_type="Standard_D4s_v3", - azure_subscription_id="test-sub", - azure_client_id="test-client", - azure_client_secret="test-secret", - ) - assert azure_config.azure_region == "westus" - assert azure_config.azure_instance_type == "Standard_D4s_v3" - assert azure_config.azure_subscription_id == "test-sub" - assert azure_config.azure_client_id == "test-client" - assert azure_config.azure_client_secret == "test-secret" - - # Test GCP fields - gcp_config = ClusterConfig( - cluster_type="gcp", - gcp_region="us-west1", - gcp_instance_type="n1-standard-2", - gcp_project_id="test-project", - gcp_service_account_key="/path/to/key.json", - ) - assert gcp_config.gcp_region == "us-west1" - assert gcp_config.gcp_instance_type == "n1-standard-2" - assert gcp_config.gcp_project_id == "test-project" - assert gcp_config.gcp_service_account_key == "/path/to/key.json" - - # Test Lambda Cloud fields - lambda_config = ClusterConfig( - cluster_type="lambda_cloud", - lambda_instance_type="gpu_1x_a100", - lambda_api_key="test-lambda-key", - ) - assert lambda_config.lambda_instance_type == "gpu_1x_a100" - assert lambda_config.lambda_api_key == "test-lambda-key" - - # Test HuggingFace fields + assert saved_config["cluster_type"] == "huggingface" + assert saved_config["hf_hardware"] == "t4-small" + assert saved_config["hf_sdk"] == "gradio" + # The token is saved under the field name hf_jobs.py reads. + assert saved_config["hf_token"] == "test-hf-token" + + def test_huggingface_fields_in_config(self): + """Test that ClusterConfig supports the HF fields the widget writes.""" hf_config = ClusterConfig( - cluster_type="huggingface_spaces", + cluster_type="huggingface", hf_hardware="t4-medium", hf_token="test-hf-token", hf_username="test-user", @@ -218,22 +125,17 @@ def test_widget_dropdown_population(self): """Test that widget properly populates dropdown options.""" widget = ClusterConfigWidget(auto_display=False) - # Test that cloud provider dropdowns have sensible defaults - assert len(widget.aws_region_field.options) > 0 - assert "us-east-1" in widget.aws_region_field.options + # The cluster type dropdown offers exactly the retained backends. + assert list(widget.cluster_type.options) == [ + "local", + "ssh", + "slurm", + "huggingface", + ] - assert len(widget.azure_region_field.options) > 0 - assert "eastus" in widget.azure_region_field.options - - assert len(widget.gcp_region_field.options) > 0 - assert "us-central1" in widget.gcp_region_field.options - - # Test that instance type dropdowns have options - assert len(widget.aws_instance_type_field.options) > 0 - assert len(widget.azure_instance_type_field.options) > 0 - assert len(widget.gcp_instance_type_field.options) > 0 - assert len(widget.lambda_instance_type_field.options) > 0 + # HuggingFace hardware flavors have sensible defaults assert len(widget.hf_hardware_field.options) > 0 + assert "cpu-basic" in widget.hf_hardware_field.options @pytest.mark.skip( reason="Test isolation issue - configs being contaminated by other tests" @@ -256,22 +158,18 @@ def test_no_name_description_in_default_configs(self): @pytest.mark.skipif( not WIDGET_DEPS_AVAILABLE, reason="Widget dependencies not available" ) - def test_widget_cluster_type_change_updates_options(self): - """Test that changing cluster type updates dropdown options.""" + def test_widget_cluster_type_change_updates_sections(self): + """Test that changing cluster type shows the right section.""" widget = ClusterConfigWidget(auto_display=False) - # Simulate cluster type change to AWS - widget._on_cluster_type_change({"new": "aws"}) - - # Verify AWS fields are displayed and have options - assert widget.aws_fields.layout.display == "" - assert len(widget.aws_region_field.options) > 0 - assert len(widget.aws_instance_type_field.options) > 0 + widget._on_cluster_type_change({"new": "huggingface"}) + assert widget.hf_fields.layout.display == "" + assert widget.connection_fields.layout.display == "none" - # Simulate cluster type change to Azure - widget._on_cluster_type_change({"new": "azure"}) + widget._on_cluster_type_change({"new": "ssh"}) + assert widget.connection_fields.layout.display == "" + assert widget.hf_fields.layout.display == "none" - # Verify Azure fields are displayed and have options - assert widget.azure_fields.layout.display == "" - assert len(widget.azure_region_field.options) > 0 - assert len(widget.azure_instance_type_field.options) > 0 + widget._on_cluster_type_change({"new": "local"}) + assert widget.connection_fields.layout.display == "none" + assert widget.hf_fields.layout.display == "none" diff --git a/tests/unit/test_backends_placeholder_hosts.py b/tests/unit/test_backends_placeholder_hosts.py deleted file mode 100644 index c65b6885..00000000 --- a/tests/unit/test_backends_placeholder_hosts.py +++ /dev/null @@ -1,72 +0,0 @@ -"""A provider that cannot determine a host must say so (#119). - -No mocks. Every failure below is a real one: an unauthenticated provider has a -real ``None`` client, and the Lambda case makes a real HTTP request to a port -nothing is listening on. Previously each of these returned a config carrying -``cluster_host = "placeholder..com"``, which clustrix then tried to -SSH into -- so the error the user saw was a DNS failure for a domain they had -never heard of, arbitrarily far from the thing that actually went wrong. - -Unverified here: the success paths, which need real cloud credentials and a -real running instance. -""" - -import pytest - -from clustrix.cloud_providers.azure import AzureProvider -from clustrix.cloud_providers.gcp import GCPProvider -from clustrix.cloud_providers.lambda_cloud import LambdaCloudProvider - - -def _assert_no_placeholder(excinfo, identifier): - message = str(excinfo.value) - assert "placeholder" not in message.lower() - assert identifier in message - - -def test_azure_reports_it_cannot_determine_the_host(): - provider = AzureProvider() - - with pytest.raises(RuntimeError) as excinfo: - provider.get_cluster_config("my-vm", cluster_type="vm") - - _assert_no_placeholder(excinfo, "my-vm") - assert "connection details" in str(excinfo.value) - - -def test_gcp_reports_it_cannot_determine_the_host(): - provider = GCPProvider() - - with pytest.raises(RuntimeError) as excinfo: - provider.get_cluster_config("my-instance", cluster_type="compute") - - _assert_no_placeholder(excinfo, "my-instance") - assert "connection details" in str(excinfo.value) - - -def test_lambda_reports_it_cannot_reach_the_api(): - provider = LambdaCloudProvider() - provider.authenticated = True - # Port 1 on loopback refuses connections immediately: a real request, a - # real failure, no network dependency and no credentials. - provider.base_url = "http://127.0.0.1:1" - - with pytest.raises(RuntimeError) as excinfo: - provider.get_cluster_config("i-12345") - - _assert_no_placeholder(excinfo, "i-12345") - - -def test_no_provider_ships_a_placeholder_hostname(): - """The literal placeholder hosts are gone from the shipped code.""" - from pathlib import Path - - import clustrix.cloud_providers as pkg - - offenders = [] - for path in Path(pkg.__file__).parent.glob("*.py"): - for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): - if "placeholder." in line and not line.lstrip().startswith("#"): - offenders.append(f"{path.name}:{number}") - - assert offenders == [] diff --git a/tests/unit/test_backends_schedulers.py b/tests/unit/test_backends_schedulers.py index 41e0f983..3bdc2b15 100644 --- a/tests/unit/test_backends_schedulers.py +++ b/tests/unit/test_backends_schedulers.py @@ -1,14 +1,19 @@ -"""PBS gets the same environment every other scheduler gets (#120). +"""Every scheduler gets the same environment (#120). No mocks. Two real properties of the shipped code are asserted: 1. every scheduler submission delegates to the one ``_setup_job_environment`` - -- PBS had no environment setup at all, which is the whole bug; and -2. the job script PBS generates activates the virtualenv that setup builds, - and signs its result, exactly as SLURM's does. + -- PBS had no environment setup at all, which was the original bug; and +2. the job script each backend generates activates the virtualenv that setup + builds, and signs its result, exactly as SLURM's does. -Unverified here: an actual ``qsub`` against a real PBS cluster. That needs a -PBS scheduler; nothing in this repository can stand in for one. +The PBS and SGE cases this file was written for are gone: those backends were +removed because they had never been run against real hardware (issues #140 and +#141). The invariant they exposed is still worth holding for the backends that +remain, so it is asserted over SLURM and SSH here. + +Unverified here: an actual ``sbatch`` against a real SLURM cluster. That is +covered by ``scripts/verify_cluster_usecases.py`` and ``docs/evidence/``. """ import inspect @@ -21,8 +26,6 @@ SUBMIT_METHODS = [ "submit_slurm_job", - "submit_pbs_job", - "submit_sge_job", "submit_ssh_job", ] @@ -38,13 +41,13 @@ def test_every_scheduler_sets_up_its_environment(method_name): @pytest.mark.parametrize("method_name", SUBMIT_METHODS) def test_no_scheduler_carries_its_own_copy_of_the_venv_setup(method_name): - """The two-venv block lived in two submit methods and was missing from two.""" + """The two-venv block lived in some submit methods and was missing from others.""" source = inspect.getsource(getattr(SchedulerManager, method_name)) assert "enhanced_setup_two_venv_environment" not in source assert "setup_remote_environment(" not in source -@pytest.mark.parametrize("cluster_type", ["slurm", "pbs", "sge", "ssh"]) +@pytest.mark.parametrize("cluster_type", ["slurm", "ssh"]) def test_generated_script_runs_the_shared_execution_block(cluster_type): config = ClusterConfig(cluster_type=cluster_type, remote_work_dir="/scratch/x") script = create_job_script( @@ -63,10 +66,10 @@ def test_generated_script_runs_the_shared_execution_block(cluster_type): assert "execute_function.py" not in script -def test_pbs_and_slurm_scripts_execute_identically(): +def test_ssh_and_slurm_scripts_execute_identically(): config = ClusterConfig(remote_work_dir="/scratch/x") scripts = {} - for cluster_type in ("pbs", "slurm"): + for cluster_type in ("ssh", "slurm"): config.cluster_type = cluster_type scripts[cluster_type] = create_job_script( cluster_type=cluster_type, @@ -78,4 +81,4 @@ def test_pbs_and_slurm_scripts_execute_identically(): def execution_part(script): return script[script.index("export CLUSTRIX_RESULT_KEY") :] - assert execution_part(scripts["pbs"]) == execution_part(scripts["slurm"]) + assert execution_part(scripts["ssh"]) == execution_part(scripts["slurm"]) diff --git a/tests/unit/test_billable_and_realworld_isolation.py b/tests/unit/test_billable_and_realworld_isolation.py index 9fd72180..9cea121d 100644 --- a/tests/unit/test_billable_and_realworld_isolation.py +++ b/tests/unit/test_billable_and_realworld_isolation.py @@ -44,7 +44,6 @@ "test_cluster_job_system.py", "test_credential_access.py", "test_filesystem_utilities.py", - "test_field_mapping_fixes.py", "test_slurm_cluster_environment_setup.py", "test_real_world_credentials.py", ) diff --git a/tests/unit/test_widget_profiles.py b/tests/unit/test_widget_profiles.py index 80d5289d..de03b784 100644 --- a/tests/unit/test_widget_profiles.py +++ b/tests/unit/test_widget_profiles.py @@ -244,7 +244,7 @@ def _widget_with(self, extra): widget._update_profile_dropdown() return widget - def test_huggingface_and_kubernetes_settings_are_not_cross_contaminated(self): + def test_huggingface_and_ssh_settings_are_not_cross_contaminated(self): widget = self._widget_with( { "MyHF": ClusterConfig( @@ -252,21 +252,21 @@ def test_huggingface_and_kubernetes_settings_are_not_cross_contaminated(self): hf_namespace="contextlab", hf_flavor="cpu-upgrade", ), - "MyK8s": ClusterConfig( - cluster_type="kubernetes", - k8s_namespace="research", - k8s_image="python:3.10", + "MySSH": ClusterConfig( + cluster_type="ssh", + cluster_host="gpu.example.edu", + username="researcher", ), } ) dropdown = widget.widgets["profile_dropdown"] - for name in ["MyHF", "MyK8s", "MyHF", "MyK8s"]: + for name in ["MyHF", "MySSH", "MyHF", "MySSH"]: dropdown.value = name hf = widget.profile_manager.load_profile("MyHF") - k8s = widget.profile_manager.load_profile("MyK8s") + ssh = widget.profile_manager.load_profile("MySSH") assert (hf.hf_namespace, hf.hf_flavor) == ("contextlab", "cpu-upgrade") - assert (k8s.k8s_namespace, k8s.k8s_image) == ("research", "python:3.10") + assert (ssh.cluster_host, ssh.username) == ("gpu.example.edu", "researcher") def test_the_remote_work_directory_survives(self): widget = self._widget_with( diff --git a/tests/unit/test_widget_validation.py b/tests/unit/test_widget_validation.py index f29c05aa..b81433d3 100644 --- a/tests/unit/test_widget_validation.py +++ b/tests/unit/test_widget_validation.py @@ -84,11 +84,11 @@ def test_empty_walltime_is_rejected(self): def test_out_of_range_port_is_rejected(self, port): assert "Port must be between" in _problems(_widget("slurm", port=port)) - @pytest.mark.parametrize("cluster_type", ["ssh", "slurm", "pbs", "sge"]) + @pytest.mark.parametrize("cluster_type", ["ssh", "slurm"]) def test_remote_cluster_requires_a_host(self, cluster_type): assert "host is required" in _problems(_widget(cluster_type, host="")) - @pytest.mark.parametrize("cluster_type", ["ssh", "slurm", "pbs", "sge"]) + @pytest.mark.parametrize("cluster_type", ["ssh", "slurm"]) def test_remote_cluster_requires_a_username(self, cluster_type): assert "username is required" in _problems(_widget(cluster_type, username="")) @@ -128,14 +128,13 @@ class TestBackendSpecificSections: """Each backend needs different settings, and only its own should show.""" SECTIONS = { - "remote_section": ("ssh", "slurm", "pbs", "sge"), + "remote_section": ("ssh", "slurm"), "hf_section": ("huggingface",), - "k8s_section": ("kubernetes",), } @pytest.mark.parametrize( "cluster_type", - ["local", "ssh", "slurm", "pbs", "sge", "kubernetes", "huggingface"], + ["local", "ssh", "slurm", "huggingface"], ) def test_exactly_the_right_sections_are_shown(self, cluster_type): widget = _widget(cluster_type) @@ -145,20 +144,6 @@ def test_exactly_the_right_sections_are_shown(self, cluster_type): widget.widgets[section].layout.display == expected ), f"{section} should be {expected} for {cluster_type}" - def test_kubernetes_settings_reach_the_config(self): - """None of these had fields, so only the defaults were ever usable.""" - widget = _widget("kubernetes") - widget.widgets["k8s_namespace"].value = "research" - widget.widgets["k8s_image"].value = "python:3.12-slim" - widget.widgets["k8s_service_account"].value = "clustrix-runner" - widget.widgets["k8s_pull_policy"].value = "Always" - - config = widget._get_config_from_widgets() - assert config.k8s_namespace == "research" - assert config.k8s_image == "python:3.12-slim" - assert config.k8s_service_account == "clustrix-runner" - assert config.k8s_pull_policy == "Always" - def test_huggingface_settings_reach_the_config(self): widget = _widget("huggingface") widget.widgets["hf_namespace"].value = "contextlab" @@ -179,8 +164,5 @@ def test_every_cluster_type_in_the_dropdown_is_a_real_backend(self): "local", "ssh", "slurm", - "pbs", - "sge", - "kubernetes", "huggingface", } From 6cc7b3c639975f62d7fd040f463a37acb87e773b Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:46:11 -0400 Subject: [PATCH 19/56] Docs: drop removed-backend docs from the internal testing guides Deletes docs/kubernetes_testing.md and the three PRICING_* guides outright -- they document the Kubernetes backend and the cost/pricing API, all of which are gone from the code. Rewrites the dead cluster_type="kubernetes" examples in testing_guidelines.md and migration_to_real_tests.md against HuggingFace Jobs and SLURM, removes the PBS/SGE/Kubernetes sections and the deleted test-file references from REAL_CLUSTER_JOB_TESTING.md, and adds a scoping note to CREDENTIAL_SETUP.md saying which credentials still reach an execution backend. --- docs/CREDENTIAL_SETUP.md | 10 +++++- docs/REAL_CLUSTER_JOB_TESTING.md | 53 ++++---------------------------- docs/REAL_WORLD_TESTING.md | 12 +++++--- docs/migration_to_real_tests.md | 36 ++++++++++------------ docs/testing_guidelines.md | 25 ++++++++------- 5 files changed, 52 insertions(+), 84 deletions(-) diff --git a/docs/CREDENTIAL_SETUP.md b/docs/CREDENTIAL_SETUP.md index 5870293c..54b9ad0f 100644 --- a/docs/CREDENTIAL_SETUP.md +++ b/docs/CREDENTIAL_SETUP.md @@ -2,6 +2,14 @@ This guide explains how to set up credentials for Clustrix real-world testing, supporting both local development (with environment variables) and GitHub Actions (with repository secrets). +> **Scope note.** Clustrix has four execution backends: `local`, `ssh`, `slurm` +> and `huggingface` (HuggingFace **Jobs**). Only the SSH/SLURM and HuggingFace +> credentials below reach an execution backend. The AWS, GCP, Azure and Lambda +> Cloud entries no longer select any backend -- those were removed in v0.2.0 +> and are planned for a future update (tracking issues +> [#140-#146](https://github.com/ContextLab/clustrix/issues/140)). AWS +> credentials are still useful for the `scripts/aws/` cleanup tooling. + ## Overview The credential system supports two modes: @@ -107,7 +115,7 @@ Add the following secrets to your GitHub repository (`Settings → Secrets and v #### Required Secrets - `CLUSTRIX_USERNAME`: Username for SSH and SLURM servers - `CLUSTRIX_PASSWORD`: Password for SSH and SLURM servers -- `LAMBDA_CLOUD_API_KEY`: Lambda Cloud API key +- `HF_TOKEN`: HuggingFace token with job-write permission in the target namespace #### Optional Secrets (for expanded testing) - `AWS_ACCESS_KEY_ID`: AWS access key ID diff --git a/docs/REAL_CLUSTER_JOB_TESTING.md b/docs/REAL_CLUSTER_JOB_TESTING.md index 69341e48..e1d587f0 100644 --- a/docs/REAL_CLUSTER_JOB_TESTING.md +++ b/docs/REAL_CLUSTER_JOB_TESTING.md @@ -6,7 +6,7 @@ This guide explains how to use the comprehensive real cluster job testing system The real cluster job testing system provides: -- **Real job submission tests** for all cluster types (SLURM, PBS, SGE, Kubernetes, SSH) +- **Real job submission tests** for every supported cluster type (SLURM, SSH, HuggingFace Jobs) - **Complete end-to-end validation** using the `@cluster` decorator - **Comprehensive monitoring** of job status and resource usage - **Automatic validation** of job results and error handling @@ -17,11 +17,12 @@ The real cluster job testing system provides: ### Cluster-Specific Test Files - `tests/real_world/test_slurm_job_submission_real.py` - SLURM job submission tests -- `tests/real_world/test_pbs_job_submission_real.py` - PBS job submission tests -- `tests/real_world/test_sge_job_submission_real.py` - SGE job submission tests -- `tests/real_world/test_kubernetes_job_submission_real.py` - Kubernetes job submission tests - `tests/real_world/test_ssh_job_execution_real.py` - SSH-based job execution tests +The PBS, SGE and Kubernetes job-submission tests were deleted along with their +backends in v0.2.0; see "Backends that are not currently supported" in the +project README. + ### Supporting Infrastructure - `tests/real_world/cluster_job_validator.py` - Job monitoring and validation framework @@ -90,9 +91,6 @@ python -m tests.real_world.cluster_validation.run_cluster_job_tests --cluster al # Test only SLURM python -m tests.real_world.cluster_validation.run_cluster_job_tests --cluster slurm -# Test only Kubernetes -python -m tests.real_world.cluster_validation.run_cluster_job_tests --cluster kubernetes - # Test only SSH python -m tests.real_world.cluster_validation.run_cluster_job_tests --cluster ssh ``` @@ -236,37 +234,6 @@ Test SLURM-specific features: - Job arrays and parallel execution - SLURM accounting and metrics -### PBS Tests - -Test PBS-specific features: - -- PBS environment variables (`PBS_JOBID`, `PBS_NODEFILE`, etc.) -- Queue specification -- Node file processing -- Resource management -- Job arrays simulation - -### SGE Tests - -Test SGE-specific features: - -- SGE environment variables (`JOB_ID`, `QUEUE`, `SGE_TASK_ID`, etc.) -- Parallel environments -- Queue specification -- Resource limits -- Array job simulation - -### Kubernetes Tests - -Test Kubernetes-specific features: - -- Kubernetes environment variables (`KUBERNETES_SERVICE_HOST`, etc.) -- Pod and container management -- Resource specifications (CPU, memory limits) -- Namespace isolation -- Persistent storage access -- Service account and secrets - ### SSH Tests Test SSH-based execution: @@ -329,7 +296,6 @@ python -m tests.real_world.cluster_validation.run_cluster_job_tests --output my_ }, "credential_status": { "slurm": true, - "kubernetes": true, "ssh": true } } @@ -355,12 +321,6 @@ python -m tests.real_world.cluster_validation.run_cluster_job_tests --output my_ ```bash # SLURM squeue -u $USER - - # PBS - qstat -u $USER - - # SGE - qstat -u $USER ``` #### Test Timeouts @@ -469,7 +429,6 @@ on: options: - all - slurm - - kubernetes - ssh jobs: @@ -492,7 +451,7 @@ jobs: env: CLUSTRIX_USERNAME: ${{ secrets.CLUSTRIX_USERNAME }} CLUSTRIX_PASSWORD: ${{ secrets.CLUSTRIX_PASSWORD }} - LAMBDA_CLOUD_API_KEY: ${{ secrets.LAMBDA_CLOUD_API_KEY }} + HF_TOKEN: ${{ secrets.HF_TOKEN }} run: | python -m tests.real_world.cluster_validation.run_cluster_job_tests --cluster ${{ inputs.cluster_type }} diff --git a/docs/REAL_WORLD_TESTING.md b/docs/REAL_WORLD_TESTING.md index aadd4102..a84791d2 100644 --- a/docs/REAL_WORLD_TESTING.md +++ b/docs/REAL_WORLD_TESTING.md @@ -194,10 +194,14 @@ Real-world tests implement cost controls: We prioritize free or low-cost operations: -1. **AWS**: STS GetCallerIdentity, Pricing API (free) -2. **Azure**: List subscriptions, resource groups (free) -3. **GCP**: List zones, instances (free if no instances) -4. **Public APIs**: GitHub, PyPI, HuggingFace (free) +1. **HuggingFace Jobs**: CPU-flavor jobs only unless a paid GPU flavor is + explicitly allowed (GPU flavors bill by the second) +2. **AWS**: STS GetCallerIdentity (free) -- credential validation for the + `scripts/aws/` cleanup tooling, not an execution backend +3. **Public APIs**: GitHub, PyPI, HuggingFace (free) + +Clustrix's own cloud pricing clients were removed in v0.2.0 along with the +cloud VM backends they served. ### Monitoring Costs diff --git a/docs/migration_to_real_tests.md b/docs/migration_to_real_tests.md index 3ee904ac..9307bb57 100644 --- a/docs/migration_to_real_tests.md +++ b/docs/migration_to_real_tests.md @@ -137,40 +137,38 @@ def test_ssh_connection_real(): executor.disconnect() ``` -### Pattern 2: Mock Kubernetes API → Real Kind Cluster +### Pattern 2: Mock HTTP-API backend → Real HuggingFace Job **Before (Mocked):** ```python -@patch('kubernetes.client.BatchV1Api') -def test_k8s_job(mock_api): - mock_response = Mock() - mock_response.metadata.name = 'test-job' - mock_api.return_value.create_namespaced_job.return_value = mock_response - - job_id = submit_k8s_job(func_data, config) +@patch('clustrix.hf_jobs.HFJobsManager.submit_job') +def test_hf_job(mock_submit): + mock_submit.return_value = 'test-job' + + job_id = submit_hf_job(func_data, config) assert job_id == 'test-job' ``` **After (Real):** ```python @pytest.mark.real_world -def test_k8s_job_real(): - """Test real Kubernetes job submission.""" +def test_hf_job_real(): + """Test real HuggingFace Jobs submission.""" configure( - cluster_type="kubernetes", - namespace="default" + cluster_type="huggingface", + hf_namespace="contextlab", ) - - @cluster(cores=1, memory="512Mi") - def k8s_task(): + + @cluster(cores=1, memory="512MB") + def hf_task(): import socket return { 'hostname': socket.gethostname(), - 'pod': os.environ.get('HOSTNAME', 'unknown') + 'container': os.environ.get('HOSTNAME', 'unknown'), } - - result = k8s_task() - assert 'clustrix-job' in result['hostname'] or 'pod' in result['pod'] + + result = hf_task() + assert result['hostname'] ``` ### Pattern 3: Mock File Operations → Real File System diff --git a/docs/testing_guidelines.md b/docs/testing_guidelines.md index 8ed7df93..8d308fee 100644 --- a/docs/testing_guidelines.md +++ b/docs/testing_guidelines.md @@ -67,20 +67,20 @@ Tests that validate interactions between components using real infrastructure. ```python @pytest.mark.real_world -def test_kubernetes_integration(): - """Test Kubernetes job submission.""" +def test_huggingface_integration(): + """Test HuggingFace Jobs submission.""" configure( - cluster_type="kubernetes", - namespace="test" + cluster_type="huggingface", + hf_namespace="contextlab", ) - - @cluster(cores=2, memory="2Gi") - def k8s_task(): + + @cluster(cores=2, memory="2GB") + def hf_task(): import socket return socket.gethostname() - - result = k8s_task() - assert "clustrix-job" in result or "pod" in result + + result = hf_task() + assert isinstance(result, str) and result ``` ### 3. Edge Case Tests @@ -321,7 +321,6 @@ jobs: 3. **Infrastructure Tests** (< 30 minutes) - Docker-based tests - - Kind Kubernetes tests - SSH server tests 4. **Comprehensive Tests** (< 60 minutes) @@ -392,8 +391,8 @@ assert len(result["data"]) == 100 def cluster_config(): """Shared cluster configuration.""" config = ClusterConfig() - config.cluster_type = "kubernetes" - config.namespace = "test" + config.cluster_type = "slurm" + config.cluster_host = "login.hpc.example.edu" yield config # Cleanup if needed From e152e92d5b2c61a0c60581a328d946b888dcbf12 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:46:55 -0400 Subject: [PATCH 20/56] Remove credentials for unverified backends (aws/azure/gcp/kubernetes/lambda_cloud) Credential plumbing for backends that were never verified against real hardware is deleted along with the backends themselves. Retained: ssh (used by the ssh and slurm backends), huggingface (HF Jobs) and local. - credential_manager.py: drop the provider entries from all three credential sources, from every provider list, from the generated .env template, and delete ensure_kubernetes_provider_credentials plus its module-level wrapper (no remaining callers anywhere in the tree). - secure_credentials.py: drop ValidationCredentials.get_aws_credentials, get_gcp_credentials, get_lambda_cloud_credentials and the unconditional get_docker_credentials stub. - setup_validation_credentials.py: drop the AWS/GCP/Lambda/Docker entries and the pointers to two validation scripts that do not exist. - test_credential_manager.py: retarget the AWS/Azure assertions at ssh and huggingface, and add parametrized tests asserting the removed providers stay unresolvable even with their env vars set. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/credential_manager.py | 263 +----------------------- clustrix/secure_credentials.py | 36 ---- scripts/setup_validation_credentials.py | 72 +------ tests/test_credential_manager.py | 163 ++++++++++----- 4 files changed, 120 insertions(+), 414 deletions(-) diff --git a/clustrix/credential_manager.py b/clustrix/credential_manager.py index d7fe662c..061d11fb 100644 --- a/clustrix/credential_manager.py +++ b/clustrix/credential_manager.py @@ -68,22 +68,6 @@ def get_credentials(self, provider: str) -> Optional[Dict[str, str]]: # Map providers to their environment variable patterns provider_mappings: Dict[str, Dict[str, Optional[str]]] = { - "aws": { - "access_key_id": os.getenv("AWS_ACCESS_KEY_ID") or "", - "secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY") or "", - "region": os.getenv("AWS_REGION", "us-east-1"), - }, - "azure": { - "subscription_id": os.getenv("AZURE_SUBSCRIPTION_ID"), - "tenant_id": os.getenv("AZURE_TENANT_ID"), - "client_id": os.getenv("AZURE_CLIENT_ID"), - "client_secret": os.getenv("AZURE_CLIENT_SECRET"), - }, - "gcp": { - "project_id": os.getenv("GCP_PROJECT_ID"), - "service_account_path": os.getenv("GOOGLE_APPLICATION_CREDENTIALS"), - "service_account_json": os.getenv("GCP_SERVICE_ACCOUNT_JSON"), - }, "ssh": { "host": os.getenv("SSH_HOST"), "username": os.getenv("SSH_USERNAME"), @@ -91,21 +75,10 @@ def get_credentials(self, provider: str) -> Optional[Dict[str, str]]: "private_key_path": os.getenv("SSH_PRIVATE_KEY_PATH"), "port": os.getenv("SSH_PORT", "22"), }, - "kubernetes": { - "kubeconfig_path": os.getenv("KUBECONFIG"), - "namespace": os.getenv("K8S_NAMESPACE", "default"), - "context": os.getenv("K8S_CONTEXT"), - }, "huggingface": { "token": os.getenv("HF_TOKEN"), "username": os.getenv("HF_USERNAME"), }, - "lambda_cloud": { - "api_key": os.getenv("LAMBDA_CLOUD_API_KEY"), - "endpoint": os.getenv( - "LAMBDA_CLOUD_ENDPOINT", "https://cloud.lambdalabs.com/api/v1" - ), - }, "local": { "type": "local", # Local provider needs no real credentials }, @@ -124,13 +97,8 @@ def list_available_providers(self) -> List[str]: """List providers that have credentials available in .env file.""" available = [] providers = [ - "aws", - "azure", - "gcp", "ssh", - "kubernetes", "huggingface", - "lambda_cloud", "local", ] @@ -166,23 +134,6 @@ def get_credentials(self, provider: str) -> Optional[Dict[str, str]]: """Get credentials from environment variables.""" # Use same mapping as DotEnv but read directly from current environment provider_mappings: Dict[str, Dict[str, Optional[str]]] = { - "aws": { - "access_key_id": os.getenv("AWS_ACCESS_KEY_ID"), - "secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"), - "region": os.getenv("AWS_REGION", "us-east-1"), - }, - "azure": { - "subscription_id": os.getenv("AZURE_SUBSCRIPTION_ID"), - "tenant_id": os.getenv("AZURE_TENANT_ID"), - "client_id": os.getenv("AZURE_CLIENT_ID"), - "client_secret": os.getenv("AZURE_CLIENT_SECRET"), - }, - "gcp": { - "project_id": os.getenv("GCP_PROJECT_ID") - or os.getenv("GOOGLE_CLOUD_PROJECT"), - "service_account_path": os.getenv("GOOGLE_APPLICATION_CREDENTIALS"), - "service_account_json": os.getenv("GCP_SERVICE_ACCOUNT_JSON"), - }, "ssh": { "host": os.getenv("SSH_HOST"), "username": os.getenv("SSH_USERNAME"), @@ -190,22 +141,11 @@ def get_credentials(self, provider: str) -> Optional[Dict[str, str]]: "private_key_path": os.getenv("SSH_PRIVATE_KEY_PATH"), "port": os.getenv("SSH_PORT", "22"), }, - "kubernetes": { - "kubeconfig_path": os.getenv("KUBECONFIG"), - "namespace": os.getenv("K8S_NAMESPACE", "default"), - "context": os.getenv("K8S_CONTEXT"), - }, "huggingface": { "token": os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN"), "username": os.getenv("HF_USERNAME") or os.getenv("HUGGINGFACE_USERNAME"), }, - "lambda_cloud": { - "api_key": os.getenv("LAMBDA_CLOUD_API_KEY"), - "endpoint": os.getenv( - "LAMBDA_CLOUD_ENDPOINT", "https://cloud.lambdalabs.com/api/v1" - ), - }, "local": { "type": "local", # Local provider needs no real credentials }, @@ -224,13 +164,8 @@ def list_available_providers(self) -> List[str]: """List providers that have credentials available in environment.""" available = [] providers = [ - "aws", - "azure", - "gcp", "ssh", - "kubernetes", "huggingface", - "lambda_cloud", "local", ] @@ -254,24 +189,7 @@ def get_credentials(self, provider: str) -> Optional[Dict[str, str]]: return None # GitHub Actions specific environment variable patterns - if provider == "aws": - access_key = os.getenv("AWS_ACCESS_KEY_ID") - secret_key = os.getenv("AWS_ACCESS_KEY") # GitHub secret name - if access_key and secret_key: - return { - "access_key_id": access_key, - "secret_access_key": secret_key, - "region": os.getenv("AWS_REGION", "us-east-1"), - } - elif provider == "gcp": - project_id = os.getenv("GCP_PROJECT_ID") - service_account = os.getenv("GCP_JSON") - if project_id and service_account: - return { - "project_id": project_id, - "service_account_json": service_account, - } - elif provider == "huggingface": + if provider == "huggingface": token = os.getenv("HF_TOKEN") if token: username = os.getenv("HF_USERNAME") @@ -288,7 +206,7 @@ def list_available_providers(self) -> List[str]: return [] available = [] - providers = ["aws", "gcp", "huggingface"] + providers = ["huggingface"] for provider in providers: if self.get_credentials(provider): @@ -354,29 +272,7 @@ def _generate_env_template(self) -> str: # Priority order: .env file → environment variables → GitHub Actions # ============================================================================ -# AWS Credentials (for AWS EC2, Batch, pricing APIs) -# ============================================================================ -# AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE -# AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY -# AWS_REGION=us-east-1 - -# ============================================================================ -# Azure Credentials (for Azure VM, Container Instances) -# ============================================================================ -# AZURE_SUBSCRIPTION_ID=12345678-1234-1234-1234-123456789012 -# AZURE_TENANT_ID=12345678-1234-1234-1234-123456789012 -# AZURE_CLIENT_ID=12345678-1234-1234-1234-123456789012 -# AZURE_CLIENT_SECRET=your-client-secret-here - -# ============================================================================ -# Google Cloud Credentials (for GCP Compute, Cloud Run, pricing APIs) -# ============================================================================ -# GCP_PROJECT_ID=your-gcp-project-id -# GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json -# GCP_SERVICE_ACCOUNT_JSON={"type":"service_account",...} - -# ============================================================================ -# SSH Cluster Credentials (for SLURM, PBS, SGE clusters) +# SSH Cluster Credentials (for ssh and slurm clusters) # ============================================================================ # SSH_HOST=your-cluster.university.edu # SSH_USERNAME=your_username @@ -385,24 +281,11 @@ def _generate_env_template(self) -> str: # SSH_PORT=22 # ============================================================================ -# Kubernetes Credentials (for K8s job execution) -# ============================================================================ -# KUBECONFIG=/path/to/your/kubeconfig -# K8S_NAMESPACE=default -# K8S_CONTEXT=your-cluster-context - -# ============================================================================ -# HuggingFace Credentials (for HF Spaces execution) +# HuggingFace Credentials (for HuggingFace Jobs execution) # ============================================================================ # HF_TOKEN=hf_your_token_here # HF_USERNAME=your-huggingface-username -# ============================================================================ -# Lambda Cloud Credentials (for Lambda Labs GPU instances) -# ============================================================================ -# LAMBDA_CLOUD_API_KEY=your-lambda-cloud-api-key -# LAMBDA_CLOUD_ENDPOINT=https://cloud.lambdalabs.com/api/v1 - # ============================================================================ # Additional Notes # ============================================================================ @@ -443,15 +326,7 @@ def load_credentials_optional( ) else: # Load all available credentials - all_providers = [ - "aws", - "azure", - "gcp", - "ssh", - "kubernetes", - "huggingface", - "lambda_cloud", - ] + all_providers = ["ssh", "huggingface"] for prov in all_providers: for source in self.sources: @@ -474,7 +349,7 @@ def ensure_credential(self, provider: str) -> Optional[Dict[str, str]]: """Get credentials for a specific provider with detailed feedback. Args: - provider: Provider name (aws, azure, gcp, ssh, kubernetes, huggingface, lambda_cloud) + provider: Provider name (ssh, huggingface, local) Returns: Credentials dictionary or None if not available @@ -506,15 +381,7 @@ def ensure_credential(self, provider: str) -> Optional[Dict[str, str]]: logger.warning(f" ❌ No {provider} credentials found in any source") # Provide helpful guidance - if provider in [ - "aws", - "azure", - "gcp", - "ssh", - "kubernetes", - "huggingface", - "lambda_cloud", - ]: + if provider in ["ssh", "huggingface"]: logger.info(f" 💡 Add {provider} credentials to: {self.env_file}") logger.info(" 💡 Or use: clustrix credentials setup") @@ -545,15 +412,7 @@ def list_available_providers(self) -> Dict[str, str]: """ available = {} - for provider in [ - "aws", - "azure", - "gcp", - "ssh", - "kubernetes", - "huggingface", - "lambda_cloud", - ]: + for provider in ["ssh", "huggingface"]: for source in self.sources: try: if source.is_available() and source.get_credentials(provider): @@ -597,13 +456,8 @@ def get_credential_status(self) -> Dict[str, Any]: # Check each provider providers = [ - "aws", - "azure", - "gcp", "ssh", - "kubernetes", "huggingface", - "lambda_cloud", "local", ] for provider in providers: @@ -635,99 +489,6 @@ def get_credential_status(self) -> Dict[str, Any]: return status - def ensure_kubernetes_provider_credentials( - self, k8s_provider: str - ) -> Optional[Dict[str, str]]: - """Get credentials for Kubernetes provisioning provider with provider-specific mapping. - - Args: - k8s_provider: Kubernetes provider name (aws, gcp, azure, huggingface, lambda) - - Returns: - Credentials dictionary with provider-specific keys or None - """ - logger.info(f"🔑 Getting credentials for Kubernetes provider: {k8s_provider}") - - # Handle local providers specially - no credentials needed - if k8s_provider in ["local", "local-docker"]: - logger.info("✅ Local provider - no external credentials required") - return {"type": "local"} - - # Map k8s provider names to credential provider names - provider_mapping = { - "aws": "aws", - "gcp": "gcp", - "azure": "azure", - "huggingface": "huggingface", - "lambda": "lambda_cloud", - } - - credential_provider = provider_mapping.get(k8s_provider) - if not credential_provider: - logger.error(f"❌ Unsupported Kubernetes provider: {k8s_provider}") - return None - - # Get basic credentials - credentials = self.ensure_credential(credential_provider) - if not credentials: - logger.error(f"❌ No credentials found for {k8s_provider}") - return None - - # Apply provider-specific transformations for Kubernetes provisioning - if k8s_provider == "aws": - # AWS EKS needs standard boto3 format - transformed = { - "access_key_id": credentials.get("access_key_id"), - "secret_access_key": credentials.get("secret_access_key"), - "region": credentials.get("region", "us-west-2"), - } - elif k8s_provider == "gcp": - # GCP GKE needs project ID and service account - transformed = { - "project_id": credentials.get("project_id"), - "service_account_path": credentials.get("service_account_path"), - "service_account_json": credentials.get("service_account_json"), - } - elif k8s_provider == "azure": - # Azure AKS needs full service principal - transformed = { - "subscription_id": credentials.get("subscription_id"), - "tenant_id": credentials.get("tenant_id"), - "client_id": credentials.get("client_id"), - "client_secret": credentials.get("client_secret"), - } - elif k8s_provider == "huggingface": - # HuggingFace Spaces needs token and username - transformed = { - "token": credentials.get("token"), - "username": credentials.get("username"), - } - elif k8s_provider == "lambda": - # Lambda Cloud needs API key - transformed = { - "api_key": credentials.get("api_key"), - "endpoint": credentials.get( - "endpoint", "https://cloud.lambdalabs.com/api/v1" - ), - } - elif k8s_provider in ["local", "local-docker"]: - # Local provisioner needs no special credentials - transformed = {"type": "local"} - else: - transformed = dict(credentials) - - # Filter out None values - filtered = {k: v for k, v in transformed.items() if v is not None} - - if filtered: - logger.info( - f"✅ Credentials prepared for {k8s_provider} Kubernetes provisioning" - ) - return filtered - else: - logger.error(f"❌ Missing required credential fields for {k8s_provider}") - return None - # Global credential manager instance _credential_manager: Optional[FlexibleCredentialManager] = None @@ -772,11 +533,3 @@ def get_credential_status() -> Dict[str, Any]: """Get comprehensive credential system status.""" manager = get_credential_manager() return manager.get_credential_status() - - -def ensure_kubernetes_provider_credentials( - k8s_provider: str, -) -> Optional[Dict[str, str]]: - """Get credentials for Kubernetes provisioning provider with provider-specific mapping.""" - manager = get_credential_manager() - return manager.ensure_kubernetes_provider_credentials(k8s_provider) diff --git a/clustrix/secure_credentials.py b/clustrix/secure_credentials.py index b571f22d..3467f53f 100644 --- a/clustrix/secure_credentials.py +++ b/clustrix/secure_credentials.py @@ -62,38 +62,6 @@ class ValidationCredentials: def __init__(self): logger.info("Using environment variable fallback for validation credentials") - def get_aws_credentials(self) -> Optional[Dict[str, str]]: - """Get AWS credentials from environment variables.""" - if all( - os.getenv(key) for key in ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"] - ): - return { - "aws_access_key_id": os.getenv("AWS_ACCESS_KEY_ID", ""), - "aws_secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY", ""), - "aws_region": os.getenv("AWS_DEFAULT_REGION", "us-east-1"), - } - return None - - def get_gcp_credentials(self) -> Optional[Dict[str, str]]: - """Get GCP credentials from environment variables.""" - if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"): - return { - "project_id": os.getenv("GOOGLE_CLOUD_PROJECT", ""), - "service_account_json": os.getenv("GOOGLE_APPLICATION_CREDENTIALS", ""), - "region": os.getenv("GOOGLE_CLOUD_REGION", "us-central1"), - } - return None - - def get_lambda_cloud_credentials(self) -> Optional[Dict[str, str]]: - """Get Lambda Cloud credentials from environment variables.""" - api_key = os.getenv("LAMBDA_CLOUD_API_KEY") - if api_key: - return { - "api_key": api_key, - "endpoint": "https://cloud.lambdalabs.com/api/v1", - } - return None - def get_huggingface_credentials(self) -> Optional[Dict[str, str]]: """Get HuggingFace credentials from environment variables.""" token = os.getenv("HUGGINGFACE_TOKEN") or os.getenv("HF_TOKEN") @@ -101,10 +69,6 @@ def get_huggingface_credentials(self) -> Optional[Dict[str, str]]: return {"token": token, "username": os.getenv("HUGGINGFACE_USERNAME", "")} return None - def get_docker_credentials(self) -> Optional[Dict[str, str]]: - """Docker credentials no longer available - use environment or docker login.""" - return None - def get_ssh_credentials(self) -> Optional[Dict[str, str]]: """SSH credentials no longer available - use ~/.clustrix/.env instead.""" return None diff --git a/scripts/setup_validation_credentials.py b/scripts/setup_validation_credentials.py index 5b82b56f..b5e6ecfb 100644 --- a/scripts/setup_validation_credentials.py +++ b/scripts/setup_validation_credentials.py @@ -47,50 +47,9 @@ def guide_credential_setup(): print("=" * 30) credentials_to_setup = [ - { - "name": "clustrix-aws-validation", - "description": "AWS credentials for pricing and compute validation", - "fields": { - "access_key_id": "AWS Access Key ID", - "secret_access_key": "AWS Secret Access Key", - "region": "AWS Region (e.g., us-east-1)", - }, - "setup_notes": [ - "Create IAM user with pricing:GetProducts permission", - "For compute testing: ec2:* permissions (use sandbox account)", - "Get credentials from AWS Console → IAM → Users → Security Credentials", - ], - }, - { - "name": "clustrix-gcp-validation", - "description": "GCP credentials for pricing and compute validation", - "fields": { - "project_id": "GCP Project ID", - "service_account_json": "Service Account JSON key (full content)", - "region": "GCP Region (e.g., us-central1)", - }, - "setup_notes": [ - "Create service account with Cloud Billing Catalog Viewer role", - "For compute testing: Compute Engine Admin role", - "Download JSON key from GCP Console → IAM → Service Accounts", - ], - }, - { - "name": "clustrix-lambda-cloud-validation", - "description": "Lambda Cloud credentials for GPU pricing validation", - "fields": { - "api_key": "Lambda Cloud API Key", - "endpoint": "API Endpoint (default: https://cloud.lambdalabs.com/api/v1)", - }, - "setup_notes": [ - "Sign up at https://lambdalabs.com/", - "Generate API key from account settings", - "Note: Lambda Cloud has limited free tier", - ], - }, { "name": "clustrix-huggingface-validation", - "description": "HuggingFace credentials for Spaces validation", + "description": "HuggingFace credentials for HuggingFace Jobs validation", "fields": { "token": "HuggingFace API Token", "username": "HuggingFace Username", @@ -101,20 +60,6 @@ def guide_credential_setup(): "Use 'Write' access for full testing capabilities", ], }, - { - "name": "clustrix-docker-validation", - "description": "Docker registry credentials for container testing", - "fields": { - "username": "Docker Hub Username", - "password": "Docker Hub Password/Token", - "registry": "Registry URL (default: docker.io)", - }, - "setup_notes": [ - "Create Docker Hub account", - "Generate access token (recommended over password)", - "For testing: create temporary repository", - ], - }, { "name": "clustrix-ssh-validation", "description": "SSH credentials for cluster testing", @@ -125,7 +70,7 @@ def guide_credential_setup(): "port": "SSH Port (default: 22)", }, "setup_notes": [ - "Set up test VM (AWS EC2, GCP Compute, etc.)", + "Use any SSH-accessible host (lab machine, cluster login node, VM)", "Generate SSH key pair: ssh-keygen -t rsa -b 4096", "Add public key to ~/.ssh/authorized_keys on target", ], @@ -150,10 +95,8 @@ def guide_credential_setup(): print() print("💡 Alternative: Use environment variables as fallback") - print(" AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY") - print(" GOOGLE_APPLICATION_CREDENTIALS, GOOGLE_CLOUD_PROJECT") - print(" LAMBDA_CLOUD_API_KEY") - print(" HUGGINGFACE_TOKEN") + print(" HUGGINGFACE_TOKEN (or HF_TOKEN), HUGGINGFACE_USERNAME") + print(" SSH_HOST, SSH_USERNAME, SSH_PASSWORD, SSH_PRIVATE_KEY_PATH") print() @@ -167,11 +110,7 @@ def test_credential_access(): creds = ValidationCredentials() tests = [ - ("AWS", creds.get_aws_credentials), - ("GCP", creds.get_gcp_credentials), - ("Lambda Cloud", creds.get_lambda_cloud_credentials), ("HuggingFace", creds.get_huggingface_credentials), - ("Docker", creds.get_docker_credentials), ("SSH", creds.get_ssh_credentials), ] @@ -219,9 +158,6 @@ def main(): # Test access if test_credential_access(): print("\n🎉 Credential setup validation completed!") - print(" You can now run validation scripts:") - print(" - python scripts/validate_lambda_cloud_pricing.py") - print(" - python scripts/validate_huggingface_pricing.py") return 0 else: print("\n⚠️ Complete credential setup first, then re-run this script") diff --git a/tests/test_credential_manager.py b/tests/test_credential_manager.py index 777251df..a0f1a803 100644 --- a/tests/test_credential_manager.py +++ b/tests/test_credential_manager.py @@ -4,7 +4,7 @@ import tempfile import pytest from pathlib import Path -from unittest.mock import patch, mock_open +from unittest.mock import patch from clustrix.credential_manager import ( FlexibleCredentialManager, @@ -36,30 +36,66 @@ def test_is_available_with_nonexistent_file(self): source = DotEnvCredentialSource(env_path) assert not source.is_available() - @patch.dict( - os.environ, - { - "AWS_ACCESS_KEY_ID": "test_key", - "AWS_SECRET_ACCESS_KEY": "test_secret", - "AWS_REGION": "us-west-2", - }, + def test_get_huggingface_credentials(self): + """Test that HuggingFace credentials are read out of a real .env file.""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".env", delete=False) as f: + f.write("HF_TOKEN=hf_test_token\n") + f.write("HF_USERNAME=test-user\n") + env_path = Path(f.name) + + try: + # Clear the environment first so the values can only have come + # from parsing the file on disk, not from the ambient shell. + with patch.dict(os.environ, {}, clear=True): + source = DotEnvCredentialSource(env_path) + creds = source.get_credentials("huggingface") + + assert creds is not None + assert creds["token"] == "hf_test_token" + assert creds["username"] == "test-user" + finally: + env_path.unlink() + + def test_get_ssh_credentials(self): + """Test that SSH credentials are read out of a real .env file.""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".env", delete=False) as f: + f.write("SSH_HOST=cluster.example.edu\n") + f.write("SSH_USERNAME=researcher\n") + f.write("SSH_PORT=2222\n") + env_path = Path(f.name) + + try: + with patch.dict(os.environ, {}, clear=True): + source = DotEnvCredentialSource(env_path) + creds = source.get_credentials("ssh") + + assert creds is not None + assert creds["host"] == "cluster.example.edu" + assert creds["username"] == "researcher" + assert creds["port"] == "2222" + finally: + env_path.unlink() + + @pytest.mark.parametrize( + "provider", ["aws", "azure", "gcp", "kubernetes", "lambda_cloud"] ) - def test_get_aws_credentials(self): - """Test getting AWS credentials from environment after .env load.""" + def test_removed_backends_have_no_credentials(self, provider): + """Credentials for deleted, never-verified backends are not resolvable. + + The backends themselves were removed, so their credential entries went + with them: asking for them must behave exactly like asking for any + other unknown provider. + """ with tempfile.NamedTemporaryFile(mode="w", suffix=".env", delete=False) as f: f.write("AWS_ACCESS_KEY_ID=test_key\n") f.write("AWS_SECRET_ACCESS_KEY=test_secret\n") - f.write("AWS_REGION=us-west-2\n") + f.write("KUBECONFIG=/tmp/kubeconfig\n") + f.write("LAMBDA_CLOUD_API_KEY=test_key\n") env_path = Path(f.name) try: source = DotEnvCredentialSource(env_path) - creds = source.get_credentials("aws") - - assert creds is not None - assert creds["access_key_id"] == "test_key" - assert creds["secret_access_key"] == "test_secret" - assert creds["region"] == "us-west-2" + assert source.get_credentials(provider) is None finally: env_path.unlink() @@ -109,18 +145,31 @@ def test_get_credentials_no_env_vars(self): with patch.dict(os.environ, {}, clear=True): source = EnvironmentCredentialSource() - # AWS always has a default region - aws_creds = source.get_credentials("aws") - assert aws_creds == {"region": "us-east-1"} - # SSH has a default port ssh_creds = source.get_credentials("ssh") assert ssh_creds == {"port": "22"} - # Test a provider with no defaults - it should be None since no env vars are set - # and filtered_credentials will be empty for providers with only None values - azure_creds = source.get_credentials("azure") - assert azure_creds is None + # HuggingFace has no defaults, so with nothing in the environment + # every field filters out and the whole provider returns None. + hf_creds = source.get_credentials("huggingface") + assert hf_creds is None + + @patch.dict( + os.environ, + { + "AWS_ACCESS_KEY_ID": "test_key", + "AWS_SECRET_ACCESS_KEY": "test_secret", + "KUBECONFIG": "/tmp/kubeconfig", + "LAMBDA_CLOUD_API_KEY": "test_key", + }, + ) + @pytest.mark.parametrize( + "provider", ["aws", "azure", "gcp", "kubernetes", "lambda_cloud"] + ) + def test_removed_backends_have_no_credentials(self, provider): + """Deleted backends resolve to nothing even with their env vars set.""" + source = EnvironmentCredentialSource() + assert source.get_credentials(provider) is None class TestGitHubActionsCredentialSource: @@ -142,18 +191,35 @@ def test_is_available_outside_github_actions(self): os.environ, { "GITHUB_ACTIONS": "true", - "AWS_ACCESS_KEY_ID": "gh_key", - "AWS_ACCESS_KEY": "gh_secret", + "HF_TOKEN": "hf_gh_token", + "HF_USERNAME": "gh-user", }, ) - def test_get_aws_credentials(self): - """Test getting AWS credentials in GitHub Actions.""" + def test_get_huggingface_credentials(self): + """Test getting HuggingFace credentials in GitHub Actions.""" source = GitHubActionsCredentialSource() - creds = source.get_credentials("aws") + creds = source.get_credentials("huggingface") assert creds is not None - assert creds["access_key_id"] == "gh_key" - assert creds["secret_access_key"] == "gh_secret" + assert creds["token"] == "hf_gh_token" + assert creds["username"] == "gh-user" + + @patch.dict( + os.environ, + { + "GITHUB_ACTIONS": "true", + "AWS_ACCESS_KEY_ID": "gh_key", + "AWS_ACCESS_KEY": "gh_secret", + "GCP_PROJECT_ID": "gh-project", + "GCP_JSON": "{}", + }, + ) + @pytest.mark.parametrize("provider", ["aws", "gcp"]) + def test_removed_backends_have_no_credentials(self, provider): + """GitHub Actions secrets for deleted backends are no longer honored.""" + source = GitHubActionsCredentialSource() + assert source.get_credentials(provider) is None + assert source.list_available_providers() == [] class TestFlexibleCredentialManager: @@ -196,26 +262,20 @@ def test_ensure_credential_success(self): with tempfile.TemporaryDirectory() as temp_dir: config_dir = Path(temp_dir) - # Create .env file with AWS credentials + # Create .env file with SSH credentials env_file = config_dir / ".env" env_file.parent.mkdir(exist_ok=True) env_file.write_text( - "AWS_ACCESS_KEY_ID=test_key\nAWS_SECRET_ACCESS_KEY=test_secret\n" + "SSH_HOST=cluster.example.edu\nSSH_USERNAME=researcher\n" ) - with patch.dict( - os.environ, - { - "AWS_ACCESS_KEY_ID": "test_key", - "AWS_SECRET_ACCESS_KEY": "test_secret", - }, - ): + with patch.dict(os.environ, {}, clear=True): manager = FlexibleCredentialManager(config_dir) - creds = manager.ensure_credential("aws") + creds = manager.ensure_credential("ssh") assert creds is not None - assert "access_key_id" in creds - assert "secret_access_key" in creds + assert creds["host"] == "cluster.example.edu" + assert creds["username"] == "researcher" def test_ensure_credential_not_found(self): """Test credential retrieval when credentials don't exist.""" @@ -247,17 +307,10 @@ def test_get_credential_status(self): assert len(status["sources"]) == 3 # Should have all supported providers - expected_providers = [ - "aws", - "azure", - "gcp", - "ssh", - "kubernetes", - "huggingface", - "lambda_cloud", - ] - for provider in expected_providers: - assert provider in status["providers"] + # Only the backends that are actually supported: pbs/sge/ + # kubernetes and every cloud VM backend were removed as + # never-verified, and their credentials went with them. + assert set(status["providers"]) == {"ssh", "huggingface", "local"} class TestGlobalCredentialManager: From 5b9ff8e5cb8c84fb0476311909592f99383d70ab Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:48:29 -0400 Subject: [PATCH 21/56] Docs: final sweep -- CHANGELOG consistency, Lambda Cloud creds, stale cluster_type comment The 0.2.0 'Fixed' list described defects in backends the same release removes; the Kubernetes and cloud-placeholder entries are dropped, the PBS one is generalised, and a short preface says why the remainder are kept. Removes the Lambda Cloud credential setup from CREDENTIAL_SETUP.md and fixes the 'or "pbs", "sge"' comment in the SSH key automation notebook. --- CHANGELOG.md | 15 +++++++-------- docs/CREDENTIAL_SETUP.md | 8 -------- docs/ssh_key_automation_tutorial.ipynb | 4 ++-- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efc3f980..e7fdbd55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,11 @@ backend. ### Fixed — correctness +Some entries below describe defects in backends that this same release then +removed (see **Removed — unverified backends**). They are kept because the +defects were real and the record matters; they are not claims that those +backends now work. + - **`@cluster` returned a fabricated GPU result instead of your answer.** `_attempt_client_side_gpu_parallelization` never called the decorated function. It ran a fixed `torch.randn(100, 100)` program on each GPU, @@ -67,10 +72,6 @@ backend. - **Local auto-parallelization never parallelized anything.** It injected a `_parallel_` keyword argument the callee could not accept, then swallowed the resulting `TypeError` and silently ran sequentially. -- **Kubernetes reported failed jobs as successful.** `check_k8s_job_status` - returned `"completed"` from its exception paths, and results were decoded with - `ast.literal_eval` on the pod log, falling back to returning the raw log text - as the result. - **Environment replication silently dropped a third of the environment.** `get_environment_requirements` skipped every freeze line containing `@`. With `uv` on `PATH` — which is tried first — every conda-built package is rendered @@ -79,12 +80,10 @@ backend. requirement that genuinely cannot be reproduced remotely (an editable install, a git checkout) is now refused at submit time, naming the package, instead of producing a job that fails on import. -- **PBS never set up its remote environment**, unlike SLURM and SGE. All four - schedulers now share one staging and environment-setup path. +- **The scheduler backends did not share a staging and environment-setup path**, + so they drifted. They now do. - **`cluster_type="local"` raised `ValueError: Unsupported cluster type`**, though it was offered in the widget and the CLI. -- Cloud providers returned placeholder hostnames (`placeholder.example.com`) and - empty strings as if they were real, so failures surfaced far from their cause. - The by-value serialization walk missed instance attributes, local classes subclassing builtin containers, PEP-420 namespace packages, and `functools.partial`; and it silently degraded to by-reference at its 20,000-node diff --git a/docs/CREDENTIAL_SETUP.md b/docs/CREDENTIAL_SETUP.md index 54b9ad0f..24467e69 100644 --- a/docs/CREDENTIAL_SETUP.md +++ b/docs/CREDENTIAL_SETUP.md @@ -55,9 +55,6 @@ TEST_SLURM_PASSWORD=your-password # HuggingFace Credentials HUGGINGFACE_TOKEN=your-hf-token HUGGINGFACE_USERNAME=your-username - -# Lambda Cloud Credentials -LAMBDA_CLOUD_API_KEY=your-api-key ``` ### 2. Alternative: Export Environment Variables @@ -94,9 +91,6 @@ export TEST_SLURM_PASSWORD="your-password" # HuggingFace export HUGGINGFACE_TOKEN="your-token" export HUGGINGFACE_USERNAME="your-username" - -# Lambda Cloud -export LAMBDA_CLOUD_API_KEY="your-api-key" ``` ### 3. Test Local Setup @@ -144,7 +138,6 @@ The GitHub Actions workflow (`.github/workflows/real-world-tests.yml`) automatic export GITHUB_ACTIONS=true export CLUSTRIX_USERNAME="your-username" export CLUSTRIX_PASSWORD="your-password" -export LAMBDA_CLOUD_API_KEY="your-api-key" export GCP_PROJECT_ID="your-gcp-project" export GCP_JSON='{"type": "service_account", ...}' export AWS_ACCESS_KEY_ID="your-aws-key-id" @@ -276,7 +269,6 @@ ssh -vvv user@host - AWS CloudWatch for AWS usage - GCP Cloud Monitoring for GCP usage - Azure Monitor for Azure usage -- Lambda Cloud dashboard for GPU usage ### Access Monitoring - Local environment variable usage logs diff --git a/docs/ssh_key_automation_tutorial.ipynb b/docs/ssh_key_automation_tutorial.ipynb index 49a97366..b231d2c2 100644 --- a/docs/ssh_key_automation_tutorial.ipynb +++ b/docs/ssh_key_automation_tutorial.ipynb @@ -105,7 +105,7 @@ "# 🔧 Configure your cluster details\n", "# Replace these with your actual cluster information\n", "config = ClusterConfig(\n", - " cluster_type=\"slurm\", # or \"pbs\", \"sge\", \"ssh\", etc.\n", + " cluster_type=\"slurm\", # or \"ssh\"\n", " cluster_host=\"cluster.university.edu\", # Your cluster hostname\n", " username=\"your_username\" # Your cluster username\n", ")\n", @@ -712,4 +712,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +} From b6e765a2e99ef079e753bbfce0da891b081f2120 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:48:36 -0400 Subject: [PATCH 22/56] Remove the dead hf_sdk setting and the unreachable credential script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hf_sdk was a HuggingFace *Spaces* concept -- the gradio/streamlit/static SDK a Space runs under. hf_jobs.py never reads it; the names it does read are hf_token, hf_namespace, hf_username, hf_image, hf_flavor, hf_hardware, hf_allow_gpu_flavors, hf_payload_repo and hf_job_timeout. With Spaces gone the field configured nothing, so it is removed from ClusterConfig and from the legacy widget, and added to the removed-settings table so an existing config file gets an explanation rather than a difflib guess. scripts/setup_validation_credentials.py is deleted. Every run of it ended at the same place: ❌ 1Password CLI not available! 📥 To install 1Password CLI: macOS: brew install --cask 1password-cli main() returns 1 there and never reaches the setup guide or the credential test. SecureCredentialManager.is_op_available() has returned False unconditionally since 1Password support was removed in #97, so the script has been unreachable past its first check and was telling readers to install a CLI clustrix no longer uses. `clustrix credentials setup` and `clustrix credentials test` already do the job for real. scripts/README.md now points at those. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/config.py | 9 +- clustrix/notebook_magic_config.py | 1 - clustrix/notebook_magic_widget.py | 12 +- scripts/README.md | 21 +-- scripts/setup_validation_credentials.py | 168 ------------------------ 5 files changed, 18 insertions(+), 193 deletions(-) delete mode 100644 scripts/setup_validation_credentials.py diff --git a/clustrix/config.py b/clustrix/config.py index cc461151..139e12d6 100644 --- a/clustrix/config.py +++ b/clustrix/config.py @@ -28,13 +28,13 @@ class ClusterConfig: cluster_host: Optional[str] = None cluster_port: int = 22 - # HuggingFace Jobs settings. hf_hardware/hf_username/hf_sdk are the - # older widget-facing spellings; hf_jobs.py still reads them as fallbacks - # for hf_flavor/hf_namespace, so they are kept. + # HuggingFace Jobs settings. hf_hardware and hf_username are the older + # widget-facing spellings; hf_jobs.py still reads them as fallbacks for + # hf_flavor and hf_namespace, so they are kept. hf_sdk was a *Spaces* + # concept (gradio/streamlit/static) and went with that backend. hf_hardware: Optional[str] = None hf_token: Optional[str] = None # Required for authentication hf_username: Optional[str] = None - hf_sdk: Optional[str] = None # HuggingFace Jobs backend (cluster_type="huggingface"). The namespace is # usually an org rather than the personal account, which is often not on a # plan that can run jobs. @@ -282,6 +282,7 @@ def load_from_file(cls, config_path: str) -> "ClusterConfig": ("cloud_region", "the cloud VM backends", None), ("cloud_auto_configure", "the cloud VM backends", None), ("cost_monitoring", "cloud cost monitoring", None), + ("hf_sdk", "the HuggingFace Spaces SDK", None), ) diff --git a/clustrix/notebook_magic_config.py b/clustrix/notebook_magic_config.py index b61842e8..ef9fb231 100644 --- a/clustrix/notebook_magic_config.py +++ b/clustrix/notebook_magic_config.py @@ -50,7 +50,6 @@ "HuggingFace Jobs": { "cluster_type": "huggingface", "hf_hardware": "cpu-basic", - "hf_sdk": "gradio", "default_cores": 2, "default_memory": "16GB", }, diff --git a/clustrix/notebook_magic_widget.py b/clustrix/notebook_magic_widget.py index 0859e7db..5c43118d 100644 --- a/clustrix/notebook_magic_widget.py +++ b/clustrix/notebook_magic_widget.py @@ -322,14 +322,6 @@ def _create_dynamic_fields(self): style=style, layout=half_layout, ) - self.hf_sdk_field = widgets.Dropdown( - options=["gradio", "streamlit", "static"], - value="gradio", - description="SDK:", - tooltip="HuggingFace SDK to use", - style=style, - layout=half_layout, - ) def _create_advanced_options(self): """Create advanced options accordion.""" @@ -515,7 +507,7 @@ def _create_section_containers(self): [ widgets.HTML("
HuggingFace Jobs Settings
"), self.hf_token_field, - widgets.HBox([self.hf_hardware_field, self.hf_sdk_field]), + self.hf_hardware_field, ], layout=widgets.Layout( border="1px solid #ddd", @@ -610,7 +602,6 @@ def _load_config_to_widgets(self, config_name: str): # HuggingFace Jobs fields self.hf_token_field.value = config.get("hf_token", "") self._set_choice(self.hf_hardware_field, config.get("hf_hardware", "cpu-basic")) - self._set_choice(self.hf_sdk_field, config.get("hf_sdk", "gradio")) # Advanced options self.package_manager.value = config.get("package_manager", "pip") @@ -664,7 +655,6 @@ def _save_config_from_widgets(self) -> Dict[str, Any]: config.update( { "hf_hardware": self.hf_hardware_field.value, - "hf_sdk": self.hf_sdk_field.value, } ) if self.hf_token_field.value: diff --git a/scripts/README.md b/scripts/README.md index 8a33fe23..c875072c 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -55,15 +55,18 @@ This directory contains essential utility scripts for development and maintenanc ### Setup and Configuration -#### `setup_validation_credentials.py` -**Purpose**: Secure credential setup using environment variables -**Usage**: `python scripts/setup_validation_credentials.py` -**Features**: -- Guides through environment variable setup -- Tests credential accessibility -- Supports multiple cloud providers (AWS, GCP, Lambda Cloud, HuggingFace) -- Uses .env files for local development -- **Security**: Uses environment variables for secure credential storage +Credential setup lives in the CLI, not in a script here: + +```bash +clustrix credentials setup # interactive wizard, writes ~/.clustrix/.env +clustrix credentials test # validates each configured credential for real +``` + +`setup_validation_credentials.py` used to duplicate this and was deleted: it +gated everything behind a 1Password CLI check, and 1Password support was +removed in #97, so `is_op_available()` returns `False` unconditionally. Every +run exited at that check and told the reader to install a CLI clustrix no +longer uses. ## Workflow Integration diff --git a/scripts/setup_validation_credentials.py b/scripts/setup_validation_credentials.py deleted file mode 100644 index b5e6ecfb..00000000 --- a/scripts/setup_validation_credentials.py +++ /dev/null @@ -1,168 +0,0 @@ -#!/usr/bin/env python3 -"""Setup script for validation credentials using 1Password. - -This script helps set up all the credentials needed for external service validation -in a secure way using 1Password CLI. -""" - -import sys -from pathlib import Path - -# Add clustrix to path -sys.path.insert(0, str(Path(__file__).parent.parent)) - -# Imported after the path is set, which is the point of this script running -# standalone against a checkout. -from clustrix.secure_credentials import ( # noqa: E402 - SecureCredentialManager, - ensure_secure_environment, -) - - -def setup_1password_vault(): - """Set up 1Password vault for Clustrix validation.""" - print("🔐 1Password Setup for Clustrix Validation") - print("=" * 50) - - cred_manager = SecureCredentialManager() - - if not cred_manager.is_op_available(): - print("❌ 1Password CLI not available!") - print("\n📥 To install 1Password CLI:") - print(" macOS: brew install --cask 1password-cli") - print(" Linux: https://developer.1password.com/docs/cli/get-started/") - print(" Windows: https://developer.1password.com/docs/cli/get-started/") - print("\n🔑 After installation, sign in with: op signin") - return False - - print("✅ 1Password CLI available and authenticated") - print(f" Vault: {cred_manager.vault_name}") - - return True - - -def guide_credential_setup(): - """Guide user through credential setup process.""" - print("\n📋 Credential Setup Guide") - print("=" * 30) - - credentials_to_setup = [ - { - "name": "clustrix-huggingface-validation", - "description": "HuggingFace credentials for HuggingFace Jobs validation", - "fields": { - "token": "HuggingFace API Token", - "username": "HuggingFace Username", - }, - "setup_notes": [ - "Create account at https://huggingface.co/", - "Generate token at https://huggingface.co/settings/tokens", - "Use 'Write' access for full testing capabilities", - ], - }, - { - "name": "clustrix-ssh-validation", - "description": "SSH credentials for cluster testing", - "fields": { - "hostname": "SSH Hostname/IP", - "username": "SSH Username", - "private_key": "SSH Private Key (PEM format)", - "port": "SSH Port (default: 22)", - }, - "setup_notes": [ - "Use any SSH-accessible host (lab machine, cluster login node, VM)", - "Generate SSH key pair: ssh-keygen -t rsa -b 4096", - "Add public key to ~/.ssh/authorized_keys on target", - ], - }, - ] - - print("\n📝 To set up credentials in 1Password:") - print(" 1. Open 1Password app") - print(" 2. Navigate to 'clustrix-dev' vault (or create it)") - print(" 3. Create new items with these exact names:") - print() - - for cred in credentials_to_setup: - print(f"🔑 {cred['name']}") - print(f" Description: {cred['description']}") - print(" Fields to add:") - for field_name, field_desc in cred["fields"].items(): - print(f" - {field_name}: {field_desc}") - print(" Setup notes:") - for note in cred["setup_notes"]: - print(f" • {note}") - print() - - print("💡 Alternative: Use environment variables as fallback") - print(" HUGGINGFACE_TOKEN (or HF_TOKEN), HUGGINGFACE_USERNAME") - print(" SSH_HOST, SSH_USERNAME, SSH_PASSWORD, SSH_PRIVATE_KEY_PATH") - print() - - -def test_credential_access(): - """Test that credentials can be accessed.""" - print("🧪 Testing Credential Access") - print("=" * 30) - - from clustrix.secure_credentials import ValidationCredentials - - creds = ValidationCredentials() - - tests = [ - ("HuggingFace", creds.get_huggingface_credentials), - ("SSH", creds.get_ssh_credentials), - ] - - available_creds = [] - - for name, get_cred_func in tests: - try: - cred_data = get_cred_func() - if cred_data: - print(f"✅ {name}: Available") - available_creds.append(name) - else: - print(f"❌ {name}: Not available") - except Exception as e: - print(f"❌ {name}: Error - {e}") - - print( - f"\n📊 Summary: {len(available_creds)}/{len(tests)} credential sets available" - ) - - if available_creds: - print(f"✅ Ready to validate: {', '.join(available_creds)}") - else: - print("⚠️ No credentials available - follow setup guide above") - - return len(available_creds) > 0 - - -def main(): - """Main setup function.""" - print("🔐 Clustrix Validation Credential Setup") - print("=" * 45) - - # Ensure secure environment - ensure_secure_environment() - print("✅ Secure environment configured") - - # Check 1Password availability - if not setup_1password_vault(): - return 1 - - # Guide user through setup - guide_credential_setup() - - # Test access - if test_credential_access(): - print("\n🎉 Credential setup validation completed!") - return 0 - else: - print("\n⚠️ Complete credential setup first, then re-run this script") - return 1 - - -if __name__ == "__main__": - exit(main()) From a8d8c256f489f0ba69aafc4ce89507b0c8c43e80 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:48:54 -0400 Subject: [PATCH 23/56] Drop hf_sdk from the widget tests and the sample config Follows the field's removal: it configured the HuggingFace Spaces SDK and nothing reads it now. test_widget_fixes.py: 8 passed, 1 skipped. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- tests/test_config.yml | 1 - tests/test_widget_fixes.py | 4 ---- 2 files changed, 5 deletions(-) diff --git a/tests/test_config.yml b/tests/test_config.yml index 6d25671a..0a5bfb18 100644 --- a/tests/test_config.yml +++ b/tests/test_config.yml @@ -54,7 +54,6 @@ profiles: gpu_memory_fraction: 0.9 gpu_requirements: null hf_hardware: null - hf_sdk: null hf_token: null hf_username: null job_poll_interval: 30 diff --git a/tests/test_widget_fixes.py b/tests/test_widget_fixes.py index 929873fe..a4d87812 100644 --- a/tests/test_widget_fixes.py +++ b/tests/test_widget_fixes.py @@ -91,7 +91,6 @@ def test_widget_save_load_cycle(self): widget.cluster_type.value = "huggingface" widget.hf_hardware_field.value = "t4-small" - widget.hf_sdk_field.value = "gradio" widget.hf_token_field.value = "test-hf-token" # Save configuration @@ -100,7 +99,6 @@ def test_widget_save_load_cycle(self): # Verify saved configuration assert saved_config["cluster_type"] == "huggingface" assert saved_config["hf_hardware"] == "t4-small" - assert saved_config["hf_sdk"] == "gradio" # The token is saved under the field name hf_jobs.py reads. assert saved_config["hf_token"] == "test-hf-token" @@ -111,12 +109,10 @@ def test_huggingface_fields_in_config(self): hf_hardware="t4-medium", hf_token="test-hf-token", hf_username="test-user", - hf_sdk="gradio", ) assert hf_config.hf_hardware == "t4-medium" assert hf_config.hf_token == "test-hf-token" assert hf_config.hf_username == "test-user" - assert hf_config.hf_sdk == "gradio" @pytest.mark.skipif( not WIDGET_DEPS_AVAILABLE, reason="Widget dependencies not available" From 31c63df5829a4f0045d09eb22e6d09d92f67d6a6 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:48:56 -0400 Subject: [PATCH 24/56] Docs: follow the code -- Kind/Kubernetes test infra and cloud credentials are gone tests/infrastructure/docker-compose.yml no longer starts a Kind cluster and clustrix.credential_manager now reads only SSH_* and HF_*. Updates the README's test-infrastructure list, the migration guide's infrastructure validator, and the CREDENTIAL_SETUP scope note to match. --- README.md | 1 - docs/CREDENTIAL_SETUP.md | 13 +++++++------ docs/migration_to_real_tests.md | 5 ----- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0f2e7b8e..a55ef915 100755 --- a/README.md +++ b/README.md @@ -804,7 +804,6 @@ pytest tests/comprehensive/test_failure_recovery_real.py Clustrix provides Docker-based local test infrastructure for cost-free testing: -- **Kubernetes**: Kind (Kubernetes in Docker) cluster - **SSH Server**: OpenSSH test server on port 2222 - **SLURM Mock**: Simulated SLURM scheduler - **MinIO**: S3-compatible object storage diff --git a/docs/CREDENTIAL_SETUP.md b/docs/CREDENTIAL_SETUP.md index 24467e69..8d2e61e3 100644 --- a/docs/CREDENTIAL_SETUP.md +++ b/docs/CREDENTIAL_SETUP.md @@ -3,12 +3,13 @@ This guide explains how to set up credentials for Clustrix real-world testing, supporting both local development (with environment variables) and GitHub Actions (with repository secrets). > **Scope note.** Clustrix has four execution backends: `local`, `ssh`, `slurm` -> and `huggingface` (HuggingFace **Jobs**). Only the SSH/SLURM and HuggingFace -> credentials below reach an execution backend. The AWS, GCP, Azure and Lambda -> Cloud entries no longer select any backend -- those were removed in v0.2.0 -> and are planned for a future update (tracking issues -> [#140-#146](https://github.com/ContextLab/clustrix/issues/140)). AWS -> credentials are still useful for the `scripts/aws/` cleanup tooling. +> and `huggingface` (HuggingFace **Jobs**), and `clustrix.credential_manager` +> now reads only the `SSH_*` and `HF_*` variables. The AWS, GCP and Azure +> entries below no longer reach clustrix at all: those backends were removed in +> v0.2.0 and are planned for a future update (tracking issues +> [#140-#146](https://github.com/ContextLab/clustrix/issues/140)). They are +> kept here only because the `scripts/aws/` cleanup utilities read AWS +> credentials directly through boto3. ## Overview diff --git a/docs/migration_to_real_tests.md b/docs/migration_to_real_tests.md index 9307bb57..f7a41283 100644 --- a/docs/migration_to_real_tests.md +++ b/docs/migration_to_real_tests.md @@ -428,7 +428,6 @@ def suggest_replacement(mock_info): """Suggest replacement for mock usage.""" suggestions = { 'paramiko.SSHClient': 'Use test SSH server on localhost:2222', - 'kubernetes.client': 'Use Kind cluster or Docker Desktop Kubernetes', 'builtins.open': 'Use tempfile.NamedTemporaryFile', 'cloudpickle.dumps': 'Test actual serialization/deserialization', 'subprocess.run': 'Execute real commands in Docker container' @@ -454,7 +453,6 @@ def validate_test_infrastructure(): """Validate that test infrastructure is ready.""" checks = { 'Docker': check_docker, - 'Kubernetes': check_kubernetes, 'SSH Server': check_ssh, 'MinIO': check_minio, 'PostgreSQL': check_postgres, @@ -475,9 +473,6 @@ def validate_test_infrastructure(): def check_docker(): subprocess.run(['docker', 'ps'], check=True, capture_output=True) -def check_kubernetes(): - subprocess.run(['kubectl', 'cluster-info'], check=True, capture_output=True) - def check_ssh(): import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) From 58dfd1f5a3381214ea7bd3d7b33d7d76a993a1b2 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:49:10 -0400 Subject: [PATCH 25/56] Docs: mark issue_71_implementation_summary.md as a historical record It describes a Kind/Kubernetes test service that no longer exists. --- docs/issue_71_implementation_summary.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/issue_71_implementation_summary.md b/docs/issue_71_implementation_summary.md index fe41e4b9..1bc2cc0b 100644 --- a/docs/issue_71_implementation_summary.md +++ b/docs/issue_71_implementation_summary.md @@ -1,5 +1,11 @@ # Issue #71 Implementation Summary +> **Historical record.** This summarises the state of the test infrastructure +> at the time issue #71 was closed. The Kind/Kubernetes services it mentions +> were removed in v0.2.0 along with the Kubernetes backend +> ([#142](https://github.com/ContextLab/clustrix/issues/142)); see the current +> `tests/infrastructure/docker-compose.yml` for what actually runs. + ## Objective Ensure all tests mirror real user workflows with **NO MOCKS, NO SIMULATIONS**. Always use real API integration, real servers, real data, etc. From 4e202dfa26231b2fe75f7b16faf4e50e57804eab Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:49:16 -0400 Subject: [PATCH 26/56] Drop cloud-provider credential plumbing from real-world tests RealWorldCredentialManager loses get_aws/azure/gcp/lambda_cloud/kubernetes credentials and the matching env-var exports; TestCredentials, the aws/azure/gcp conftest fixtures and the aws_required/azure_required/ gcp_required markers go with them, since nothing retained consumes them. test_credential_access.py::test_validation_credentials was calling ValidationCredentials.get_lambda_cloud_credentials/get_aws_credentials/ get_gcp_credentials, none of which exist on that class -- the test raised AttributeError. It now probes the two credential types the class actually exposes (HuggingFace, SSH). --- tests/real_world/__init__.py | 16 - tests/real_world/conftest.py | 36 --- tests/real_world/credential_manager.py | 286 ------------------ tests/real_world/test_credential_access.py | 38 +-- .../real_world/test_real_world_credentials.py | 54 +--- 5 files changed, 10 insertions(+), 420 deletions(-) diff --git a/tests/real_world/__init__.py b/tests/real_world/__init__.py index c4b16261..cf73c3ff 100644 --- a/tests/real_world/__init__.py +++ b/tests/real_world/__init__.py @@ -69,18 +69,6 @@ def __init__(self): self._manager = get_credential_manager() - def get_aws_credentials(self) -> Optional[Dict[str, str]]: - """Get AWS credentials from available sources.""" - return self._manager.get_aws_credentials() - - def get_azure_credentials(self) -> Optional[Dict[str, str]]: - """Get Azure credentials from available sources.""" - return self._manager.get_azure_credentials() - - def get_gcp_credentials(self) -> Optional[Dict[str, str]]: - """Get GCP credentials from available sources.""" - return self._manager.get_gcp_credentials() - def get_ssh_credentials(self) -> Optional[Dict[str, str]]: """Get SSH credentials from available sources.""" return self._manager.get_ssh_credentials() @@ -93,10 +81,6 @@ def get_huggingface_credentials(self) -> Optional[Dict[str, str]]: """Get HuggingFace credentials from available sources.""" return self._manager.get_huggingface_credentials() - def get_lambda_cloud_credentials(self) -> Optional[Dict[str, str]]: - """Get Lambda Cloud credentials from available sources.""" - return self._manager.get_lambda_cloud_credentials() - def get_gpu_cluster_credentials(self) -> Optional[Dict[str, str]]: """Get SSH-GPU cluster credentials from available sources.""" return self._manager.get_gpu_cluster_credentials() diff --git a/tests/real_world/conftest.py b/tests/real_world/conftest.py index ada49f7c..6525fdf2 100644 --- a/tests/real_world/conftest.py +++ b/tests/real_world/conftest.py @@ -117,33 +117,6 @@ def temp_resource_manager(): yield manager -@pytest.fixture -def aws_credentials(test_credentials): - """AWS credentials for testing.""" - creds = test_credentials.get_aws_credentials() - if not creds: - pytest.skip("AWS credentials not available") - return creds - - -@pytest.fixture -def azure_credentials(test_credentials): - """Azure credentials for testing.""" - creds = test_credentials.get_azure_credentials() - if not creds: - pytest.skip("Azure credentials not available") - return creds - - -@pytest.fixture -def gcp_credentials(test_credentials): - """GCP credentials for testing.""" - creds = test_credentials.get_gcp_credentials() - if not creds: - pytest.skip("GCP credentials not available") - return creds - - @pytest.fixture def ssh_credentials(test_credentials): """SSH credentials for testing.""" @@ -242,15 +215,6 @@ def pytest_configure(config): config.addinivalue_line( "markers", "ssh_required: mark test as requiring SSH access" ) - config.addinivalue_line( - "markers", "aws_required: mark test as requiring AWS credentials" - ) - config.addinivalue_line( - "markers", "azure_required: mark test as requiring Azure credentials" - ) - config.addinivalue_line( - "markers", "gcp_required: mark test as requiring GCP credentials" - ) def pytest_collection_modifyitems(config, items): diff --git a/tests/real_world/credential_manager.py b/tests/real_world/credential_manager.py index 93a90351..d6390bda 100644 --- a/tests/real_world/credential_manager.py +++ b/tests/real_world/credential_manager.py @@ -167,124 +167,6 @@ def is_1password_available(self) -> bool: return False return self._op_manager.is_op_available() - def get_aws_credentials(self) -> Optional[Dict[str, str]]: - """Get AWS credentials from available sources.""" - # Try 1Password first (local development) - if self.is_local_development and self._validation_creds: - try: - aws_creds = self._validation_creds.get_aws_credentials() - if aws_creds: - return { - "access_key_id": aws_creds.get("aws_access_key_id"), - "secret_access_key": aws_creds.get("aws_secret_access_key"), - "region": aws_creds.get("aws_region", "us-east-1"), - } - except Exception as e: - logger.debug(f"Failed to get AWS credentials from 1Password: {e}") - - # GitHub Actions: Use repository secrets - if self.is_github_actions: - access_key = os.getenv("AWS_ACCESS_KEY_ID") - secret_key = os.getenv("AWS_ACCESS_KEY") # GitHub secret name - region = os.getenv("AWS_REGION", "us-east-1") - - if access_key and secret_key: - return { - "access_key_id": access_key, - "secret_access_key": secret_key, - "region": region, - } - - # Fall back to environment variables - access_key = os.getenv("AWS_ACCESS_KEY_ID") or os.getenv("TEST_AWS_ACCESS_KEY") - secret_key = os.getenv("AWS_SECRET_ACCESS_KEY") or os.getenv( - "TEST_AWS_SECRET_KEY" - ) - region = os.getenv("AWS_REGION") or os.getenv("TEST_AWS_REGION", "us-east-1") - - if access_key and secret_key: - return { - "access_key_id": access_key, - "secret_access_key": secret_key, - "region": region, - } - - return None - - def get_azure_credentials(self) -> Optional[Dict[str, str]]: - """Get Azure credentials from available sources.""" - # Try 1Password first (local development) - if self.is_local_development and self._validation_creds: - try: - azure_creds = self._validation_creds.get_azure_credentials() - if azure_creds: - return azure_creds - except Exception as e: - logger.debug(f"Failed to get Azure credentials from 1Password: {e}") - - # Fall back to environment variables - subscription_id = os.getenv("AZURE_SUBSCRIPTION_ID") or os.getenv( - "TEST_AZURE_SUBSCRIPTION_ID" - ) - tenant_id = os.getenv("AZURE_TENANT_ID") or os.getenv("TEST_AZURE_TENANT_ID") - client_id = os.getenv("AZURE_CLIENT_ID") or os.getenv("TEST_AZURE_CLIENT_ID") - client_secret = os.getenv("AZURE_CLIENT_SECRET") or os.getenv( - "TEST_AZURE_CLIENT_SECRET" - ) - - if subscription_id: - return { - "subscription_id": subscription_id, - "tenant_id": tenant_id, - "client_id": client_id, - "client_secret": client_secret, - } - - return None - - def get_gcp_credentials(self) -> Optional[Dict[str, str]]: - """Get GCP credentials from available sources.""" - # Try 1Password first (local development) - if self.is_local_development and self._validation_creds: - try: - gcp_creds = self._validation_creds.get_gcp_credentials() - if gcp_creds: - return gcp_creds - except Exception as e: - logger.debug(f"Failed to get GCP credentials from 1Password: {e}") - - # GitHub Actions: Use repository secrets - if self.is_github_actions: - project_id = os.getenv("GCP_PROJECT_ID") - service_account_json = os.getenv("GCP_JSON") - - if project_id and service_account_json: - return { - "project_id": project_id, - "service_account_json": service_account_json, - } - - # Fall back to environment variables - project_id = ( - os.getenv("GOOGLE_CLOUD_PROJECT") - or os.getenv("GCP_PROJECT_ID") - or os.getenv("TEST_GCP_PROJECT_ID") - ) - service_account_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") or os.getenv( - "TEST_GCP_SERVICE_ACCOUNT_PATH" - ) - service_account_json = os.getenv("GCP_JSON") - - if project_id: - result = {"project_id": project_id} - if service_account_json: - result["service_account_json"] = service_account_json - elif service_account_path: - result["service_account_path"] = service_account_path - return result - - return None - def get_ssh_credentials(self) -> Optional[Dict[str, str]]: """Get SSH credentials from available sources.""" # Try 1Password first (local development) @@ -491,120 +373,12 @@ def get_huggingface_credentials(self) -> Optional[Dict[str, str]]: return None - def get_lambda_cloud_credentials(self) -> Optional[Dict[str, str]]: - """Get Lambda Cloud credentials from available sources.""" - # Try 1Password first (local development) - if self.is_local_development and self._validation_creds: - try: - lambda_creds = self._validation_creds.get_lambda_cloud_credentials() - if lambda_creds: - return lambda_creds - except Exception as e: - logger.debug( - f"Failed to get Lambda Cloud credentials from 1Password: {e}" - ) - - # GitHub Actions: Use repository secrets - if self.is_github_actions: - api_key = os.getenv("LAMBDA_CLOUD_API_KEY") - if api_key: - return { - "api_key": api_key, - "endpoint": "https://cloud.lambdalabs.com/api/v1", - } - - # Fall back to environment variables - api_key = os.getenv("LAMBDA_CLOUD_API_KEY") - endpoint = os.getenv( - "LAMBDA_CLOUD_ENDPOINT", "https://cloud.lambdalabs.com/api/v1" - ) - - if api_key: - return {"api_key": api_key, "endpoint": endpoint} - - return None - - def get_kubernetes_credentials(self) -> Optional[Dict[str, str]]: - """Get Kubernetes credentials from available sources.""" - # Try 1Password first (local development) - if self.is_local_development and self._op_manager: - try: - # Try to get Kubernetes cluster credentials - kubeconfig = self._op_manager.get_credential( - "clustrix-kubernetes-validation", "kubeconfig" - ) - namespace = self._op_manager.get_credential( - "clustrix-kubernetes-validation", "namespace" - ) - context = self._op_manager.get_credential( - "clustrix-kubernetes-validation", "context" - ) - - if kubeconfig: - result = { - "kubeconfig_content": kubeconfig, - "namespace": namespace or "default", - } - if context: - result["context"] = context - return result - - except Exception as e: - logger.debug( - f"Failed to get Kubernetes credentials from 1Password: {e}" - ) - - # GitHub Actions: Use repository secrets - if self.is_github_actions: - kubeconfig = os.getenv("KUBECONFIG_CONTENT") - namespace = os.getenv("K8S_NAMESPACE") - - if kubeconfig: - result = { - "kubeconfig_content": kubeconfig, - "namespace": namespace or "default", - } - context = os.getenv("K8S_CONTEXT") - if context: - result["context"] = context - return result - - # Fall back to environment variables and local kubeconfig - kubeconfig_path = os.getenv("KUBECONFIG") or os.path.expanduser( - "~/.kube/config" - ) - if os.path.exists(kubeconfig_path): - namespace = os.getenv("K8S_NAMESPACE", "default") - context = os.getenv("K8S_CONTEXT") - - result = { - "kubeconfig_path": kubeconfig_path, - "namespace": namespace, - } - if context: - result["context"] = context - return result - - # Check if running in-cluster - if os.path.exists("/var/run/secrets/kubernetes.io/serviceaccount/token"): - return { - "in_cluster": True, - "namespace": os.getenv("K8S_NAMESPACE", "default"), - } - - return None - def get_credential_status(self) -> Dict[str, bool]: """Get status of all credential types.""" return { - "aws": self.get_aws_credentials() is not None, - "azure": self.get_azure_credentials() is not None, - "gcp": self.get_gcp_credentials() is not None, "ssh": self.get_ssh_credentials() is not None, "slurm": self.get_slurm_credentials() is not None, - "kubernetes": self.get_kubernetes_credentials() is not None, "huggingface": self.get_huggingface_credentials() is not None, - "lambda_cloud": self.get_lambda_cloud_credentials() is not None, "1password": self.is_1password_available(), } @@ -625,35 +399,6 @@ def print_credential_status(self) -> None: def setup_environment_variables(self) -> None: """Set up environment variables from available credentials.""" - # Set AWS credentials - aws_creds = self.get_aws_credentials() - if aws_creds: - os.environ["TEST_AWS_ACCESS_KEY"] = aws_creds["access_key_id"] - os.environ["TEST_AWS_SECRET_KEY"] = aws_creds["secret_access_key"] - os.environ["TEST_AWS_REGION"] = aws_creds["region"] - - # Set Azure credentials - azure_creds = self.get_azure_credentials() - if azure_creds: - os.environ["TEST_AZURE_SUBSCRIPTION_ID"] = azure_creds["subscription_id"] - if azure_creds.get("tenant_id"): - os.environ["TEST_AZURE_TENANT_ID"] = azure_creds["tenant_id"] - if azure_creds.get("client_id"): - os.environ["TEST_AZURE_CLIENT_ID"] = azure_creds["client_id"] - if azure_creds.get("client_secret"): - os.environ["TEST_AZURE_CLIENT_SECRET"] = azure_creds["client_secret"] - - # Set GCP credentials - gcp_creds = self.get_gcp_credentials() - if gcp_creds: - os.environ["TEST_GCP_PROJECT_ID"] = gcp_creds["project_id"] - if gcp_creds.get("service_account_path"): - os.environ["TEST_GCP_SERVICE_ACCOUNT_PATH"] = gcp_creds[ - "service_account_path" - ] - if gcp_creds.get("service_account_json"): - os.environ["GCP_JSON"] = gcp_creds["service_account_json"] - # Set SSH credentials ssh_creds = self.get_ssh_credentials() if ssh_creds: @@ -683,12 +428,6 @@ def setup_environment_variables(self) -> None: os.environ["HUGGINGFACE_USERNAME"] = hf_creds["username"] os.environ["HF_USERNAME"] = hf_creds["username"] - # Set Lambda Cloud credentials - lambda_creds = self.get_lambda_cloud_credentials() - if lambda_creds: - os.environ["LAMBDA_CLOUD_API_KEY"] = lambda_creds["api_key"] - os.environ["LAMBDA_CLOUD_ENDPOINT"] = lambda_creds["endpoint"] - # Global credential manager instance _credential_manager = None @@ -720,30 +459,5 @@ def print_credential_status() -> None: manager.print_credential_status() -# Convenience functions for tests -def get_lambda_credentials() -> Optional[Dict[str, str]]: - """Get Lambda Cloud credentials for tests.""" - manager = get_credential_manager() - return manager.get_lambda_cloud_credentials() - - -def get_aws_credentials() -> Optional[Dict[str, str]]: - """Get AWS credentials for tests.""" - manager = get_credential_manager() - return manager.get_aws_credentials() - - -def get_azure_credentials() -> Optional[Dict[str, str]]: - """Get Azure credentials for tests.""" - manager = get_credential_manager() - return manager.get_azure_credentials() - - -def get_gcp_credentials() -> Optional[Dict[str, str]]: - """Get GCP credentials for tests.""" - manager = get_credential_manager() - return manager.get_gcp_credentials() - - # Set up credentials when module is imported setup_test_credentials() diff --git a/tests/real_world/test_credential_access.py b/tests/real_world/test_credential_access.py index 664ec442..6aa41702 100644 --- a/tests/real_world/test_credential_access.py +++ b/tests/real_world/test_credential_access.py @@ -67,34 +67,13 @@ def test_validation_credentials(): else: print("❌ HuggingFace credentials not found") - # Test Lambda Cloud credentials - lambda_creds = creds.get_lambda_cloud_credentials() - if lambda_creds: - print("✅ Lambda Cloud credentials found") - api_key = lambda_creds.get("api_key", "") - print(f" API key length: {len(api_key) if api_key else 0}") - print(f" Endpoint: {lambda_creds.get('endpoint', 'default')}") + # Test SSH credentials + ssh_creds = creds.get_ssh_credentials() + if ssh_creds: + print("✅ SSH credentials found") + print(f" Host: {ssh_creds.get('host', 'not set')}") else: - print("❌ Lambda Cloud credentials not found") - - # Test AWS credentials - aws_creds = creds.get_aws_credentials() - if aws_creds: - print("✅ AWS credentials found") - access_key = aws_creds.get("aws_access_key_id", "") - print(f" Access key length: {len(access_key) if access_key else 0}") - print(f" Region: {aws_creds.get('aws_region', 'default')}") - else: - print("❌ AWS credentials not found") - - # Test GCP credentials - gcp_creds = creds.get_gcp_credentials() - if gcp_creds: - print("✅ GCP credentials found") - print(f" Project ID: {gcp_creds.get('project_id', 'not set')}") - print(f" Region: {gcp_creds.get('region', 'default')}") - else: - print("❌ GCP credentials not found") + print("❌ SSH credentials not found") def test_environment_fallback(): @@ -105,11 +84,6 @@ def test_environment_fallback(): env_vars = [ "HUGGINGFACE_TOKEN", "HF_TOKEN", - "LAMBDA_CLOUD_API_KEY", - "AWS_ACCESS_KEY_ID", - "AWS_SECRET_ACCESS_KEY", - "GOOGLE_APPLICATION_CREDENTIALS", - "GOOGLE_CLOUD_PROJECT", ] found_vars = [] diff --git a/tests/real_world/test_real_world_credentials.py b/tests/real_world/test_real_world_credentials.py index 0ecb8ff7..b08a1061 100755 --- a/tests/real_world/test_real_world_credentials.py +++ b/tests/real_world/test_real_world_credentials.py @@ -41,29 +41,6 @@ def test_credential_integration(): print("\n🧪 Testing Specific Credentials:") print("=" * 35) - # Test AWS credentials - aws_creds = manager.get_aws_credentials() - if aws_creds: - print( - f"✅ AWS: Access key length: {len(aws_creds['access_key_id'])}, Region: {aws_creds['region']}" - ) - else: - print("❌ AWS: No credentials found") - - # Test Azure credentials - azure_creds = manager.get_azure_credentials() - if azure_creds: - print(f"✅ Azure: Subscription: {azure_creds['subscription_id'][:8]}...") - else: - print("❌ Azure: No credentials found") - - # Test GCP credentials - gcp_creds = manager.get_gcp_credentials() - if gcp_creds: - print(f"✅ GCP: Project: {gcp_creds['project_id']}") - else: - print("❌ GCP: No credentials found") - # Test SSH credentials ssh_creds = manager.get_ssh_credentials() if ssh_creds: @@ -86,14 +63,6 @@ def test_credential_integration(): else: print("❌ HuggingFace: No credentials found") - # Test Lambda Cloud credentials - lambda_creds = manager.get_lambda_cloud_credentials() - if lambda_creds: - key_len = len(lambda_creds["api_key"]) if lambda_creds["api_key"] else 0 - print(f"✅ Lambda Cloud: API key length: {key_len}") - else: - print("❌ Lambda Cloud: No credentials found") - return True @@ -107,16 +76,11 @@ def test_environment_variable_setup(): # Check if environment variables were set env_vars_to_check = [ - "TEST_AWS_ACCESS_KEY", - "TEST_AWS_SECRET_KEY", - "TEST_AZURE_SUBSCRIPTION_ID", - "TEST_GCP_PROJECT_ID", "TEST_SSH_HOST", "TEST_SSH_USERNAME", "TEST_SLURM_HOST", "TEST_SLURM_USERNAME", "HUGGINGFACE_TOKEN", - "LAMBDA_CLOUD_API_KEY", ] set_vars = [] @@ -146,7 +110,6 @@ def test_github_actions_simulation(): mock_secrets = { "CLUSTRIX_USERNAME": "testuser", "CLUSTRIX_PASSWORD": "testpass", - "LAMBDA_CLOUD_API_KEY": "test_lambda_key", } original_values = {} @@ -177,13 +140,6 @@ def test_github_actions_simulation(): else: print("❌ SLURM: No credentials found") - # Test Lambda Cloud credentials (should use GitHub secrets) - lambda_creds = gh_manager.get_lambda_cloud_credentials() - if lambda_creds: - print(f"✅ Lambda Cloud: API key set") - else: - print("❌ Lambda Cloud: No credentials found") - print("✅ GitHub Actions simulation successful") finally: @@ -215,15 +171,13 @@ def test_1password_integration(): if manager._op_manager: # Try to get a test credential test_cred = manager._op_manager.get_credential( - "clustrix-lambda-cloud-validation", "api_key" + "clustrix-huggingface-validation", "token" ) if test_cred: - print( - f"✅ Retrieved Lambda Cloud API key (length: {len(test_cred)})" - ) + print(f"✅ Retrieved HuggingFace token (length: {len(test_cred)})") else: - print("⚠️ Lambda Cloud credential not found in 1Password") - print(" Make sure 'clustrix-lambda-cloud-validation' item exists") + print("⚠️ HuggingFace credential not found in 1Password") + print(" Make sure 'clustrix-huggingface-validation' item exists") except Exception as e: print(f"❌ Error accessing 1Password: {e}") else: From b3822a9905ab1e1d5665757151c156bacaf32c1d Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:51:00 -0400 Subject: [PATCH 27/56] Tests: restate executor/config tests over the backends that remain Removes the PBS, SGE, Kubernetes and cloud-provider cases from test_executor.py, test_enhanced_features.py, test_config_real.py, test_integration.py and test_config_file_permissions.py. Where the property under test was general (save/load round trip, a failed cancellation keeping the job tracked, an SSH script carrying no scheduler directives) it is restated over slurm/ssh/huggingface rather than deleted. --- tests/test_config_real.py | 119 +++------ tests/test_enhanced_features.py | 135 +++------- tests/test_executor.py | 281 ++------------------- tests/test_integration.py | 14 +- tests/unit/test_config_file_permissions.py | 12 +- 5 files changed, 105 insertions(+), 456 deletions(-) diff --git a/tests/test_config_real.py b/tests/test_config_real.py index 5fc85e0a..2f524689 100644 --- a/tests/test_config_real.py +++ b/tests/test_config_real.py @@ -13,6 +13,7 @@ from pathlib import Path from clustrix.config import ( ClusterConfig, + SUPPORTED_CLUSTER_TYPES, configure, get_config, load_config, @@ -81,15 +82,17 @@ def test_custom_configuration_real(self): - Configuration validation - Real-world settings """ - # "namespace" was never a ClusterConfig field -- the real Kubernetes - # namespace field is "k8s_namespace". "gpu" is a per-job @cluster + # Restated over SLURM. This used to configure Kubernetes via + # k8s_namespace/auto_provision_k8s/k8s_provider/k8s_region; that + # backend and all four fields were removed (issue #142) because it had + # never been run against a real cluster. "gpu" is a per-job @cluster # decorator kwarg, not a ClusterConfig field, so it is not passed # here (Issue #114). config = ClusterConfig( - cluster_type="kubernetes", - cluster_host="k8s.example.com", - username="k8s-user", - k8s_namespace="ml-workloads", + cluster_type="slurm", + cluster_host="hpc.example.com", + username="hpc-user", + default_partition="gpu", default_cores=16, default_memory="64GB", environment_variables={ @@ -97,17 +100,12 @@ def test_custom_configuration_real(self): "TF_GPU_MEMORY_GROWTH": "true", }, module_loads=["cuda/11.8", "cudnn/8.6"], - auto_provision_k8s=True, - k8s_provider="aws", - k8s_region="us-west-2", ) - assert config.cluster_type == "kubernetes" - assert config.k8s_namespace == "ml-workloads" + assert config.cluster_type == "slurm" + assert config.default_partition == "gpu" assert config.environment_variables["CUDA_VISIBLE_DEVICES"] == "0,1" assert "cuda/11.8" in config.module_loads - assert config.auto_provision_k8s is True - assert config.k8s_provider == "aws" def test_save_and_load_yaml_config(self, temp_config_dir, reset_config): """ @@ -177,13 +175,11 @@ def test_save_and_load_json_config(self, temp_config_dir, reset_config): config_file = temp_config_dir / "cluster_config.json" # Configure - # "namespace" is not a real field (the real one is "k8s_namespace"). - # "node_selector"/"tolerations" are not ClusterConfig fields at all -- - # there is no passthrough for arbitrary Kubernetes Job-spec fields - # like node selectors or tolerations (Issue #114). + # Restated over HuggingFace Jobs: the Kubernetes backend this used to + # configure has been removed (issue #142). The round trip is the point. configure( - cluster_type="kubernetes", - k8s_namespace="production", + cluster_type="huggingface", + hf_namespace="contextlab", default_cores=8, default_memory="32Gi", ) @@ -206,8 +202,8 @@ def test_save_and_load_json_config(self, temp_config_dir, reset_config): with open(config_file, "r") as f: loaded_data = json.load(f) - assert loaded_data["cluster_type"] == "kubernetes" - assert loaded_data["k8s_namespace"] == "production" + assert loaded_data["cluster_type"] == "huggingface" + assert loaded_data["hf_namespace"] == "contextlab" assert loaded_data["default_memory"] == "32Gi" def test_environment_variable_configuration(self, reset_config): @@ -221,9 +217,9 @@ def test_environment_variable_configuration(self, reset_config): """ # Set environment variables env_vars = { - "CLUSTRIX_CLUSTER_TYPE": "pbs", - "CLUSTRIX_CLUSTER_HOST": "pbs.cluster.com", - "CLUSTRIX_USERNAME": "pbsuser", + "CLUSTRIX_CLUSTER_TYPE": "ssh", + "CLUSTRIX_CLUSTER_HOST": "gpu.cluster.com", + "CLUSTRIX_USERNAME": "sshuser", "CLUSTRIX_DEFAULT_CORES": "16", "CLUSTRIX_DEFAULT_MEMORY": "64GB", "CLUSTRIX_QUEUE": "batch", @@ -248,9 +244,9 @@ def test_environment_variable_configuration(self, reset_config): setattr(config, attr_name, value) # Verify environment variable application - assert config.cluster_type == "pbs" - assert config.cluster_host == "pbs.cluster.com" - assert config.username == "pbsuser" + assert config.cluster_type == "ssh" + assert config.cluster_host == "gpu.cluster.com" + assert config.username == "sshuser" assert config.default_cores == 16 finally: @@ -320,8 +316,10 @@ def test_multi_cluster_configuration(self, temp_config_dir, reset_config): - Real multi-cluster workflows """ # Create multiple configuration files - # "namespace" and "partition" are not real field names; the real - # ones are "k8s_namespace" and "default_partition" (Issue #114). + # "partition" is not a real field name; the real one is + # "default_partition" (Issue #114). The middle profile used to be a + # Kubernetes one keyed on k8s_namespace, a removed backend and a + # removed field (issue #142). configs = { "dev": { "cluster_type": "local", @@ -329,8 +327,8 @@ def test_multi_cluster_configuration(self, temp_config_dir, reset_config): "default_memory": "4GB", }, "test": { - "cluster_type": "kubernetes", - "k8s_namespace": "testing", + "cluster_type": "huggingface", + "hf_namespace": "contextlab", "default_cores": 4, "default_memory": "8Gi", }, @@ -359,59 +357,11 @@ def test_multi_cluster_configuration(self, temp_config_dir, reset_config): assert current.cluster_type == expected["cluster_type"] assert current.default_cores == expected["default_cores"] - if "k8s_namespace" in expected: - assert current.k8s_namespace == expected["k8s_namespace"] + if "hf_namespace" in expected: + assert current.hf_namespace == expected["hf_namespace"] if "default_partition" in expected: assert current.default_partition == expected["default_partition"] - @pytest.mark.real_world - def test_kubernetes_configuration_real(self, reset_config): - """ - Test Kubernetes-specific configuration. - - This demonstrates: - - K8s-specific settings - - Auto-provisioning configuration - - Real K8s parameters - - NOTE: this test originally asserted a much larger surface of K8s - fields (k8s_project_id, k8s_zone, k8s_gpu_type, k8s_gpu_count, - k8s_preemptible, k8s_autoscaling, k8s_min_nodes, k8s_max_nodes, - namespace, service_account, image_pull_secrets, node_selector) that - are not, and never were, fields on ClusterConfig (confirmed via - `git log -S` -- e.g. node_selector/tolerations have no history at - all). Kubernetes Job-spec passthroughs (node_selector, tolerations, - image_pull_secrets) and GPU/autoscaling knobs are a genuine gap in - ClusterConfig, not a test bug; see Issue #114 report. This test now - only exercises fields that genuinely exist. - """ - configure( - cluster_type="kubernetes", - auto_provision_k8s=True, - k8s_provider="gcp", - gcp_project_id="my-gcp-project", - k8s_region="us-central1", - gcp_zone="us-central1-a", - k8s_cluster_name="ml-cluster", - k8s_node_count=3, - k8s_node_type="n1-standard-8", - k8s_namespace="ml-workloads", - k8s_service_account="ml-service-account", - ) - - config = get_config() - - # Verify K8s configuration - assert config.cluster_type == "kubernetes" - assert config.auto_provision_k8s is True - assert config.k8s_provider == "gcp" - assert config.gcp_project_id == "my-gcp-project" - assert config.k8s_cluster_name == "ml-cluster" - assert config.k8s_node_count == 3 - assert config.k8s_node_type == "n1-standard-8" - assert config.k8s_namespace == "ml-workloads" - assert config.k8s_service_account == "ml-service-account" - def test_validation_and_error_handling(self, reset_config): """ Test configuration validation and error handling. @@ -428,9 +378,10 @@ def test_validation_and_error_handling(self, reset_config): # Test invalid types (would need type checking in real implementation) config = ClusterConfig() - # These should be validated in a real implementation - valid_cluster_types = ["slurm", "pbs", "sge", "kubernetes", "ssh", "local"] - assert config.cluster_type in valid_cluster_types + # Read the shipped tuple rather than a second hand-maintained copy: + # the old literal list here still named pbs/sge/kubernetes after those + # backends were removed. + assert config.cluster_type in SUPPORTED_CLUSTER_TYPES # Memory should be a string with units assert isinstance(config.default_memory, str) diff --git a/tests/test_enhanced_features.py b/tests/test_enhanced_features.py index 4b2b2007..fe1afe2f 100644 --- a/tests/test_enhanced_features.py +++ b/tests/test_enhanced_features.py @@ -348,41 +348,6 @@ def test_setup_remote_environment_failure_handling(self): class TestConfigurationEnhancements: """Test enhanced configuration features.""" - def test_kubernetes_configuration_fields(self): - """Test new Kubernetes configuration fields.""" - config = ClusterConfig( - cluster_type="kubernetes", - k8s_namespace="production", - k8s_image="python:3.12-slim", - k8s_service_account="clustrix-sa", - k8s_pull_policy="Always", - k8s_job_ttl_seconds=7200, - k8s_backoff_limit=5, - ) - - assert config.k8s_namespace == "production" - assert config.k8s_image == "python:3.12-slim" - assert config.k8s_service_account == "clustrix-sa" - assert config.k8s_pull_policy == "Always" - assert config.k8s_job_ttl_seconds == 7200 - assert config.k8s_backoff_limit == 5 - - def test_cloud_provider_configuration_fields(self): - """Test cloud provider configuration fields.""" - config = ClusterConfig( - cloud_provider="aws", - cloud_region="us-east-1", - cloud_auto_configure=True, - eks_cluster_name="production-cluster", - aws_profile="production", - ) - - assert config.cloud_provider == "aws" - assert config.cloud_region == "us-east-1" - assert config.cloud_auto_configure is True - assert config.eks_cluster_name == "production-cluster" - assert config.aws_profile == "production" - def test_package_manager_configuration(self): """Test package manager configuration.""" config = ClusterConfig(package_manager="uv") @@ -396,15 +361,19 @@ def test_package_manager_configuration(self): assert config.package_manager == "pip" def test_configuration_persistence_with_new_fields(self): - """Test saving and loading configuration with new fields.""" + """Test saving and loading configuration with new fields. + + Rewritten: this used to round-trip k8s_/cloud_/eks_ fields, which no + longer exist on ClusterConfig. The property under test -- that a saved + config reloads field for field -- is unchanged; only the fields it is + stated over had to move to backends clustrix still has. + """ original_config = ClusterConfig( - cluster_type="kubernetes", - k8s_namespace="test", - k8s_image="python:3.11", - cloud_provider="aws", - cloud_auto_configure=True, + cluster_type="huggingface", + hf_namespace="contextlab", + hf_flavor="cpu-basic", package_manager="uv", - eks_cluster_name="test-cluster", + venv_setup_timeout=600, ) with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: @@ -418,59 +387,38 @@ def test_configuration_persistence_with_new_fields(self): loaded_config = ClusterConfig.load_from_file(config_file) # Verify all fields are preserved - assert loaded_config.cluster_type == "kubernetes" - assert loaded_config.k8s_namespace == "test" - assert loaded_config.k8s_image == "python:3.11" - assert loaded_config.cloud_provider == "aws" - assert loaded_config.cloud_auto_configure is True + assert loaded_config.cluster_type == "huggingface" + assert loaded_config.hf_namespace == "contextlab" + assert loaded_config.hf_flavor == "cpu-basic" assert loaded_config.package_manager == "uv" - assert loaded_config.eks_cluster_name == "test-cluster" + assert loaded_config.venv_setup_timeout == 600 finally: if os.path.exists(config_file): os.unlink(config_file) def test_configure_function_with_new_parameters(self): - """Test configure function with new parameters.""" - # Test configuring new Kubernetes parameters + """Test configure function with new parameters. + + Rewritten: the parameters it named (k8s_namespace, k8s_image, + cloud_provider) belonged to removed backends. Restated over the + HuggingFace Jobs settings, which are the ones a user configures today. + """ configure( - k8s_namespace="custom", - k8s_image="python:3.12", - cloud_provider="azure", + hf_namespace="contextlab", + hf_flavor="cpu-basic", package_manager="uv", ) config = get_config() - assert config.k8s_namespace == "custom" - assert config.k8s_image == "python:3.12" - assert config.cloud_provider == "azure" + assert config.hf_namespace == "contextlab" + assert config.hf_flavor == "cpu-basic" assert config.package_manager == "uv" - def test_azure_specific_configuration(self): - """Test Azure-specific configuration fields.""" - config = ClusterConfig( - cloud_provider="azure", - aks_cluster_name="my-cluster", - azure_resource_group="my-rg", - azure_subscription_id="subscription-123", - ) - - assert config.aks_cluster_name == "my-cluster" - assert config.azure_resource_group == "my-rg" - assert config.azure_subscription_id == "subscription-123" - - def test_gcp_specific_configuration(self): - """Test GCP-specific configuration fields.""" - config = ClusterConfig( - cloud_provider="gcp", - gke_cluster_name="my-gke-cluster", - gcp_project_id="my-project-123", - gcp_zone="us-central1-a", - ) - - assert config.gke_cluster_name == "my-gke-cluster" - assert config.gcp_project_id == "my-project-123" - assert config.gcp_zone == "us-central1-a" + def test_configure_rejects_a_setting_from_a_removed_backend(self): + """configure() must not silently accept a field that no longer exists.""" + with pytest.raises(ValueError, match="Unknown configuration parameter"): + configure(k8s_namespace="production") class TestBackwardCompatibility: @@ -491,10 +439,10 @@ def test_existing_configuration_still_works(self): assert config.username == "user" assert config.remote_work_dir == "/scratch/user" - # New fields should have defaults + # New fields should have defaults. The two cloud_* assertions that + # were here are gone with the fields themselves. assert config.package_manager == "pip" - assert config.cloud_provider == "manual" - assert config.cloud_auto_configure is False + assert config.replicate_local_environment is True @patch("subprocess.run") def test_environment_capture_fallback(self, mock_run): @@ -553,22 +501,3 @@ def test_graceful_degradation_no_uv(self, mock_uv_available, mock_conda_availabl # Should fallback to pip assert pkg_manager == "pip" - - def test_kubernetes_with_cloud_provider_config(self): - """Test Kubernetes configuration with cloud provider settings.""" - config = ClusterConfig( - cluster_type="kubernetes", - cloud_provider="aws", - cloud_auto_configure=True, - eks_cluster_name="prod-cluster", - k8s_namespace="production", - package_manager="uv", - ) - - # All settings should coexist - assert config.cluster_type == "kubernetes" - assert config.cloud_provider == "aws" - assert config.cloud_auto_configure is True - assert config.eks_cluster_name == "prod-cluster" - assert config.k8s_namespace == "production" - assert config.package_manager == "uv" diff --git a/tests/test_executor.py b/tests/test_executor.py index 13b57013..8bceb07e 100644 --- a/tests/test_executor.py +++ b/tests/test_executor.py @@ -1,6 +1,3 @@ -import logging -import textwrap - import pytest from unittest.mock import Mock, patch from clustrix.executor import ClusterExecutor @@ -18,49 +15,12 @@ def _explode(): return 1 / 0 -#: A kubeconfig that the real kubernetes client parses successfully. It points -#: at a port nothing is listening on, which is enough: these tests exercise -#: client setup, never an API call. -_MINIMAL_KUBECONFIG = textwrap.dedent("""\ - apiVersion: v1 - kind: Config - clusters: - - name: clustrix-test - cluster: - server: https://127.0.0.1:6443 - contexts: - - name: clustrix-test - context: - cluster: clustrix-test - user: clustrix-test - current-context: clustrix-test - users: - - name: clustrix-test - user: - token: not-a-real-token - """) - - -def _point_kubeconfig_at(monkeypatch, path): - """Aim the real kubernetes client at `path`. - - `KUBECONFIG` alone is not enough: kubernetes.config reads it once, into a - module constant, when it is first imported. Setting the environment - variable afterwards leaves whichever value the first import saw, so the - constant is redirected too. Nothing is faked -- the client still reads a - real file off disk and either parses it or refuses it. - """ - monkeypatch.setenv("KUBECONFIG", str(path)) - monkeypatch.setattr( - "kubernetes.config.kube_config.KUBE_CONFIG_DEFAULT_LOCATION", str(path) - ) - - #: (cluster_type, ClusterExecutor submission method, directive unique to it). +#: PBS and SGE used to be listed here too. Both backends were removed -- +#: neither had ever been run against a real scheduler (issues #140, #141) -- +#: so SLURM is the only scheduler clustrix still submits to. SCHEDULER_CASES = [ ("slurm", "_submit_slurm_job", "#SBATCH --cpus-per-task=4"), - ("pbs", "_submit_pbs_job", "#PBS -l nodes=1:ppn=4"), - ("sge", "_submit_sge_job", "#$ -pe smp 4"), ] @@ -226,11 +186,19 @@ def test_scheduler_script_carries_only_its_own_directives( # The result the caller collects has to be signed, or it is refused # before deserialization. assert "result.pkl.hmac" in script - # A directive meant for another scheduler in this script would be - # either ignored or fatal, depending on the site. - for other_type, _m, other_directive in SCHEDULER_CASES: - if other_type != cluster_type: - assert other_directive not in script + # This loop used to compare SLURM's script against PBS's and SGE's. + # With one scheduler left it would assert nothing, so the property is + # stated against the backend that is not a scheduler instead: an SSH + # script must carry no scheduler directives at all, since there is + # nothing on the far end to read them. + ssh_script = create_job_script( + cluster_type="ssh", + job_config={"cores": 4, "memory": "8GB", "time": "01:00:00"}, + remote_job_dir="/scratch/w/job_1", + config=ClusterConfig(cluster_type="ssh", remote_work_dir="/scratch/w"), + ) + assert directive not in ssh_script + assert "#SBATCH" not in ssh_script @pytest.mark.parametrize("cluster_type,method,_directive", SCHEDULER_CASES) def test_scheduler_submission_without_a_connection_records_no_job( @@ -258,26 +226,6 @@ def test_scheduler_submission_without_a_connection_records_no_job( assert executor.scheduler_manager.active_jobs == {} assert executor.active_jobs == {} - def test_submit_k8s_job_without_a_usable_cluster_records_no_job( - self, monkeypatch, tmp_path - ): - """Same property for Kubernetes, via the real kubernetes client. - - KUBECONFIG points at a file that does not exist, so the real client - refuses to configure itself. No API call is attempted and no cluster - is contacted. - """ - _point_kubeconfig_at(monkeypatch, tmp_path / "no-such-kubeconfig.yaml") - executor = ClusterExecutor(ClusterConfig(cluster_type="kubernetes")) - func_data = serialize_function(_double, (21,), {}) - - with pytest.raises(Exception) as excinfo: - executor._submit_k8s_job(func_data, {"cores": 4, "memory": "8Gi"}) - - assert "kube-config" in str(excinfo.value) - assert executor.k8s_manager.active_jobs == {} - assert executor.active_jobs == {} - def test_check_slurm_status(self, executor): """Test SLURM job status checking.""" executor.ssh_client = Mock() @@ -298,113 +246,13 @@ def test_check_slurm_status(self, executor): assert "squeue" in call_args assert "12345" in call_args - def test_check_pbs_status(self, executor): - """Test PBS job status checking.""" - executor.ssh_client = Mock() - - # Mock qstat output - mock_stdout = Mock() - mock_stdout.read.return_value = b"12345.pbs user R queue" - mock_stdout.channel.recv_exit_status.return_value = 0 - - executor.ssh_client.exec_command.return_value = (None, mock_stdout, Mock()) - - status = executor._check_pbs_status("12345") - - assert status == "running" - - def test_check_sge_status_running(self, executor): - """Test SGE job status checking - running state.""" - executor.ssh_client = Mock() - - # Mock qstat -j output for running job - mock_stdout = Mock() - mock_stdout.read.return_value = b"job_state r" - mock_stderr = Mock() - mock_stderr.read.return_value = b"" - - executor.ssh_client.exec_command.return_value = (None, mock_stdout, mock_stderr) - - status = executor._check_sge_status("12345") - - assert status == "running" - - # Verify qstat command - call_args = executor.ssh_client.exec_command.call_args[0][0] - assert "qstat -j" in call_args - assert "12345" in call_args - - def test_check_sge_status_queued(self, executor): - """Test SGE job status checking - queued state.""" - executor.ssh_client = Mock() - - # Mock qstat -j output for queued job - mock_stdout = Mock() - mock_stdout.read.return_value = b"job_state qw" - mock_stderr = Mock() - mock_stderr.read.return_value = b"" - - executor.ssh_client.exec_command.return_value = (None, mock_stdout, mock_stderr) - - status = executor._check_sge_status("12345") - - assert status == "queued" - - def test_check_sge_status_failed(self, executor): - """Test SGE job status checking - error state.""" - executor.ssh_client = Mock() - - # Mock qstat -j output for error job - mock_stdout = Mock() - mock_stdout.read.return_value = b"job_state Eqw" - mock_stderr = Mock() - mock_stderr.read.return_value = b"" - - executor.ssh_client.exec_command.return_value = (None, mock_stdout, mock_stderr) - - status = executor._check_sge_status("12345") - - assert status == "failed" - - def test_check_sge_status_completed(self, executor): - """Test SGE job status checking - completed/not found.""" - executor.ssh_client = Mock() - - # Mock qstat -j output for job not found - mock_stdout = Mock() - mock_stdout.read.return_value = b"" - mock_stderr = Mock() - mock_stderr.read.return_value = b"Following jobs do not exist: 12345" - - executor.ssh_client.exec_command.return_value = (None, mock_stdout, mock_stderr) - - status = executor._check_sge_status("12345") - - assert status == "completed" - - def test_check_sge_status_exit_status(self, executor): - """Test SGE job status checking - exit status indicates completion.""" - executor.ssh_client = Mock() - - # Mock qstat -j output with exit status - mock_stdout = Mock() - mock_stdout.read.return_value = b"exit_status 0" - mock_stderr = Mock() - mock_stderr.read.return_value = b"" - - executor.ssh_client.exec_command.return_value = (None, mock_stdout, mock_stderr) - - status = executor._check_sge_status("12345") - - assert status == "completed" - # ------------------------------------------------------------------ # Status and results. # # These three used to hand-build `active_jobs["job_12345"] = # {"remote_dir": ...}` and mock an SFTP `stat`. `get_job_status` now # dispatches on `active_jobs[job_id]["manager"]` -- there are several job - # managers (scheduler, kubernetes, local, huggingface) -- so the + # managers (scheduler, local, huggingface) -- so the # hand-built entry raised KeyError. The stale part was the TEST: the entry # clustrix writes has that key. # @@ -464,17 +312,19 @@ def test_cancel_job_slurm(self, executor): call_args = executor.ssh_client.exec_command.call_args[0][0] assert "scancel 12345" in call_args - def test_cancel_job_sge(self, executor): + def test_cancel_job_that_cannot_be_reached_stays_tracked(self, executor): """A job clustrix failed to cancel must stay tracked. - Rewritten. The old version mocked `exec_command` and asserted that + Rewritten twice. The original mocked `exec_command` and asserted that "qdel 12345" reached its own Mock; its `active_jobs` entry also had no - "manager" key, which is now a KeyError. Here the qdel is really - attempted, there really is no connection, and the property that - matters is the consequence: dropping the job from `active_jobs` after - a failed cancellation would leave it running and invisible. + "manager" key, which is now a KeyError. It then ran against SGE, a + backend that has since been removed, so it is run against SLURM here. + The cancellation is really attempted, there really is no connection, + and the property that matters is the consequence: dropping the job + from `active_jobs` after a failed cancellation would leave it running + and invisible. """ - executor.config.cluster_type = "sge" + executor.config.cluster_type = "slurm" executor.active_jobs["12345"] = {"manager": "scheduler", "job_id": "12345"} with pytest.raises(RuntimeError, match="SSH client not connected"): @@ -554,85 +404,6 @@ def test_setup_ssh_connection_no_auth(self, mock_ssh_class): assert "password" not in connect_call assert connect_call["username"] == "testuser" - def test_setup_kubernetes_import_error(self): - """Test Kubernetes setup when kubernetes package not available.""" - config = ClusterConfig(cluster_type="kubernetes") - executor = ClusterExecutor(config) - - # Mock import error by patching the import at module level - with patch.dict("sys.modules", {"kubernetes": None}): - with pytest.raises(ImportError, match="kubernetes package required"): - executor._setup_kubernetes() - - # ------------------------------------------------------------------ - # Cloud auto-configuration during Kubernetes setup. - # - # Three tests here replaced CloudProviderManager with a Mock and asserted - # against `clustrix.executor.logger`. The refactor moved this code into - # executor_connections, which logs to its own logger, so the assertions - # were made against a logger the code never touched -- they could not - # fail for the right reason and did not fail for the wrong one either. - # - # The real CloudProviderManager reports an incomplete provider config - # without contacting anything, so the skip path is testable for real. The - # kubeconfig below is a real file the real kubernetes client parses. - # - # Deleted rather than repaired: the third test, which asserted that a - # Mock raising ImportError produced a warning. CloudProviderManager's - # constructor stores two attributes and cannot raise, and auto_configure - # catches its own exceptions, so that branch is unreachable without a - # mock -- the test could only ever have verified the mock. - # ------------------------------------------------------------------ - - @pytest.mark.parametrize( - "cloud_provider,expected_reason", - [ - ("aws", "Missing EKS cluster name or region"), - ("gcp", "Missing GKE cluster name, zone, or project ID"), - ], - ) - def test_cloud_auto_configure_skip_reason_is_reported( - self, cloud_provider, expected_reason, monkeypatch, tmp_path, caplog - ): - """Real manager, real logging, no cloud account touched. - - An incomplete provider config is answered from the config itself: - `_configure_aws` and `_configure_gcp` both return their reason before - constructing a configurator, so nothing here makes a network call. - """ - kubeconfig = tmp_path / "kubeconfig.yaml" - kubeconfig.write_text(_MINIMAL_KUBECONFIG) - _point_kubeconfig_at(monkeypatch, kubeconfig) - - config = ClusterConfig( - cluster_type="kubernetes", - cloud_auto_configure=True, - cloud_provider=cloud_provider, - ) - executor = ClusterExecutor(config) - - with caplog.at_level(logging.INFO): - executor._setup_kubernetes() - - assert f"Cloud auto-configuration skipped: {expected_reason}" in caplog.text - # Setup still completes: a skipped auto-configuration is not a failure. - assert executor.k8s_client is not None - - @patch("kubernetes.client") - @patch("kubernetes.config") - def test_setup_kubernetes_no_cloud_auto_configure( - self, mock_k8s_config, mock_k8s_client - ): - """Test Kubernetes setup without cloud auto-configuration.""" - config = ClusterConfig(cluster_type="kubernetes", cloud_auto_configure=False) - executor = ClusterExecutor(config) - - executor._setup_kubernetes() - - # Should load kube config normally - mock_k8s_config.load_kube_config.assert_called_once() - mock_k8s_client.ApiClient.assert_called_once() - class TestJobSubmissionEdgeCases: """Test job submission edge cases and error handling.""" diff --git a/tests/test_integration.py b/tests/test_integration.py index 451f9444..8c211461 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -219,9 +219,9 @@ def test_configuration_persistence(self, temp_dir): # Create configuration configure( - cluster_type="sge", - cluster_host="sge.cluster.com", - username="sgeuser", + cluster_type="ssh", + cluster_host="gpu.cluster.com", + username="sshuser", default_cores=16, default_memory="32GB", module_loads=["python/3.9", "gcc/11.2"], @@ -243,9 +243,9 @@ def test_configuration_persistence(self, temp_dir): # Verify loaded configuration config = get_config() - assert config.cluster_type == "sge" - assert config.cluster_host == "sge.cluster.com" - assert config.username == "sgeuser" + assert config.cluster_type == "ssh" + assert config.cluster_host == "gpu.cluster.com" + assert config.username == "sshuser" assert config.default_cores == 16 assert config.default_memory == "32GB" assert config.module_loads == ["python/3.9", "gcc/11.2"] @@ -350,7 +350,7 @@ def test_environment_replication(self): def test_resource_specification_inheritance(self): """Test that decorator resources override defaults.""" configure( - cluster_type="pbs", + cluster_type="slurm", default_cores=4, default_memory="8GB", default_time="01:00:00", diff --git a/tests/unit/test_config_file_permissions.py b/tests/unit/test_config_file_permissions.py index 62208d7e..bce958e9 100644 --- a/tests/unit/test_config_file_permissions.py +++ b/tests/unit/test_config_file_permissions.py @@ -66,7 +66,6 @@ def secret_bearing_config(): username="researcher", password="fake-password-for-this-test", api_key="sk-fake-key-abcdef123456", - aws_secret_access_key="AKIAABCDEFSECRETVALUE", # nosec hf_token="hf_thisisasecrettoken", # nosec ) @@ -149,7 +148,6 @@ def test_load_from_file_round_trips_after_default_save(tmp_path, secret_bearing_ assert reloaded.username == "researcher" assert reloaded.password is None assert reloaded.api_key is None - assert reloaded.aws_secret_access_key is None assert reloaded.hf_token is None @@ -207,14 +205,14 @@ def test_secret_fields_derived_from_dataclass_covers_known_credential_names(): list that can silently fall out of date. Spot-check known credential fields are present. """ + # aws_secret_access_key, aws_access_key_id, azure_client_secret, + # gcp_service_account_key and lambda_api_key were spot-checked here too. + # Those fields went with the cloud backends (issues #143-#146); the + # patterns that classified them are still in _SECRET_FIELD_PATTERN, so + # the derivation is unchanged -- there is simply nothing left to name. for expected in ( "password", "api_key", - "aws_secret_access_key", - "aws_access_key_id", - "azure_client_secret", - "gcp_service_account_key", - "lambda_api_key", "hf_token", ): assert expected in SECRET_FIELDS, ( From 1a2d8ff939bcf7956cbb940157a5481819746722 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:51:48 -0400 Subject: [PATCH 28/56] Update real-world runner, visual tests and progress doc for retained backends run_real_world_tests: drop the cloud_providers and kubernetes categories, the kubectl/AWS/GCP/Azure availability probes and their CLI choices. test_visual_verification: the widget profile assertion still required an "AWS Batch" profile after the profile itself became HuggingFace Jobs, so it could never pass; assertion updated to match. The synthetic matplotlib demo labels no longer advertise PBS/SGE/K8s/cloud providers as supported. REFACTORING_PROGRESS.md: rows describing deleted modules corrected and a dated backend-removal section added. --- tests/REFACTORING_PROGRESS.md | 37 ++++++++++------ tests/real_world/test_visual_verification.py | 16 +++---- tests/run_real_world_tests.py | 46 +------------------- 3 files changed, 34 insertions(+), 65 deletions(-) diff --git a/tests/REFACTORING_PROGRESS.md b/tests/REFACTORING_PROGRESS.md index 507e5015..5154a659 100644 --- a/tests/REFACTORING_PROGRESS.md +++ b/tests/REFACTORING_PROGRESS.md @@ -12,10 +12,12 @@ Implementing Issue #71: Refactoring all tests to mirror real user workflows with - Created priority list based on anti-pattern density ### Phase 2: Reference Workflows (✅ COMPLETED) -Created three reference modules: +Created reference modules: 1. `reference_workflows/basic_usage.py` - Core @cluster patterns -2. `reference_workflows/kubernetes_workflows.py` - K8s auto-provisioning -3. `reference_workflows/data_analysis_workflows.py` - Scientific computing +2. `reference_workflows/data_analysis_workflows.py` - Scientific computing + +A third module, `reference_workflows/kubernetes_workflows.py`, was deleted +along with the Kubernetes backend (see "Backend removal" below). ### Phase 3: Test Refactoring (🔄 IN PROGRESS) @@ -24,7 +26,6 @@ Created three reference modules: |--------------|---------------|----------|---------| | `test_executor.py` | 86 | `test_executor_real.py` | ✅ Complete | | `test_decorator.py` | 72 | `test_decorator_real.py` | ✅ Complete | -| `test_cloud_providers_gcp.py` | 135 | `test_cloud_providers_gcp_real.py` | ✅ Complete | | `test_notebook_magic.py` | 125 | `test_notebook_magic_real.py` | ✅ Complete | | `test_auth_fallbacks.py` | 114 | `test_auth_fallbacks_real.py` | ✅ Complete | @@ -39,8 +40,8 @@ Created three reference modules: **Infrastructure Tests** - Real SSH connections -- Actual Kubernetes cluster provisioning -- Live cloud provider APIs (GCP, AWS, Azure) +- Real SLURM job submission +- Real HuggingFace Jobs submission - Real file system operations **Authentication Tests** @@ -51,7 +52,6 @@ Created three reference modules: **Execution Tests** - Real job submission to clusters -- Actual container execution - Live result retrieval - Real parallel processing @@ -59,9 +59,7 @@ Created three reference modules: 1. `test_secure_credentials.py` (107 anti-patterns) 2. `test_config.py` (96 anti-patterns) -3. `test_cloud_providers_aws.py` (94 anti-patterns) -4. `test_slurm_advanced.py` (91 anti-patterns) -5. `test_kubernetes_scaling.py` (90 anti-patterns) +3. `test_slurm_advanced.py` (91 anti-patterns) ## Testing Strategy @@ -120,7 +118,6 @@ Each refactored test file includes: ### Phase 4: Infrastructure Setup - Docker containers for test environments -- Kind clusters for local Kubernetes - Test data generation scripts ### Phase 5: Coverage @@ -148,7 +145,7 @@ python tests/audit_antipatterns.py python tests/run_refactored_tests.py # Run specific real-world test -pytest tests/test_executor_real.py::TestClusterExecutorReal::test_job_submission_kubernetes -v -s +pytest tests/test_executor_real.py -v -s # Run all real-world tests (requires credentials) pytest -m real_world tests/ @@ -162,7 +159,21 @@ pytest -m real_world tests/ - Visual outputs (figures, screenshots) for verification - Cost-conscious API usage with initial verification +## Backend removal (2026-08-19) + +Every backend that had never been verified against real hardware was deleted +from the package: `pbs`, `sge`, `kubernetes`, and the AWS/GCP/Azure/Lambda +cloud providers. The retained `cluster_type` values are `local`, `ssh`, +`slurm` and `huggingface` (HuggingFace **Jobs**). + +Consequently the counts in this report are historical and no longer describe +the current tree: the cloud-provider and Kubernetes rows above referred to +files that no longer exist, and `tests/real_world/`, `tests/integration/`, +`tests/infrastructure/` and `tests/reference_workflows/` lost the tests, +fixtures, credential accessors and docker-compose services that only existed +to exercise those backends. + --- -*Last Updated: Current Session* +*Last Updated: 2026-08-19* *Issue #71 Implementation* \ No newline at end of file diff --git a/tests/real_world/test_visual_verification.py b/tests/real_world/test_visual_verification.py index 8a968038..9c702b01 100644 --- a/tests/real_world/test_visual_verification.py +++ b/tests/real_world/test_visual_verification.py @@ -327,7 +327,7 @@ def test_widget_configuration_output(self): profiles = pm.list_profiles() assert len(profiles) >= 4 # Original + 3 test profiles assert "SLURM HPC" in profiles - assert "AWS Batch" in profiles + assert "HuggingFace Jobs" in profiles assert "SSH Cluster" in profiles except ImportError: @@ -792,17 +792,17 @@ def test_matplotlib_plots_real(self): axes[0, 1].set_ylim(0, 100) # Plot 3: Cost analysis - providers = ["AWS", "Azure", "GCP", "Lambda", "Local"] - costs = [0.12, 0.15, 0.11, 0.08, 0.00] - colors = ["orange", "blue", "red", "purple", "green"] - axes[1, 0].bar(providers, costs, color=colors, alpha=0.7) - axes[1, 0].set_title("Cost per Hour by Provider") + backends = ["SLURM", "SSH", "HF Jobs", "Local"] + costs = [0.12, 0.11, 0.08, 0.00] + colors = ["orange", "blue", "purple", "green"] + axes[1, 0].bar(backends, costs, color=colors, alpha=0.7) + axes[1, 0].set_title("Cost per Hour by Backend") axes[1, 0].set_ylabel("Cost ($)") axes[1, 0].tick_params(axis="x", rotation=45) # Plot 4: Success rate - cluster_types = ["SLURM", "PBS", "SGE", "K8s", "SSH"] - success_rates = [95, 92, 88, 97, 90] + cluster_types = ["SLURM", "SSH", "HF Jobs", "Local"] + success_rates = [95, 90, 97, 99] axes[1, 1].bar(cluster_types, success_rates, color="lightcoral", alpha=0.7) axes[1, 1].set_title("Job Success Rate by Cluster Type") axes[1, 1].set_ylabel("Success Rate (%)") diff --git a/tests/run_real_world_tests.py b/tests/run_real_world_tests.py index db4a364c..edb895a3 100644 --- a/tests/run_real_world_tests.py +++ b/tests/run_real_world_tests.py @@ -2,8 +2,8 @@ """ Runner for real-world tests using actual infrastructure. -This script runs tests against real infrastructure (local or cloud) -and validates actual functionality without mocks. +This script runs tests against real infrastructure (local machines, SSH hosts +and SLURM clusters) and validates actual functionality without mocks. """ import sys @@ -40,13 +40,7 @@ def load_config(self, config_file: Optional[str]) -> Dict: # Fallback to environment variables return { "infrastructure": { - "kubernetes": {"available": os.getenv("KUBECONFIG") is not None}, "ssh": {"available": os.getenv("TEST_SSH_HOST") is not None}, - "cloud": { - "aws": os.getenv("AWS_ACCESS_KEY_ID") is not None, - "gcp": os.getenv("GOOGLE_APPLICATION_CREDENTIALS") is not None, - "azure": os.getenv("AZURE_SUBSCRIPTION_ID") is not None, - }, } } @@ -54,15 +48,6 @@ def check_infrastructure(self) -> Dict[str, bool]: """Check which infrastructure is available.""" available = {} - # Check Kubernetes - try: - result = subprocess.run( - ["kubectl", "cluster-info"], capture_output=True, timeout=5 - ) - available["kubernetes"] = result.returncode == 0 - except: - available["kubernetes"] = False - # Check SSH if self.config.get("infrastructure", {}).get("ssh"): ssh_config = self.config["infrastructure"]["ssh"] @@ -89,11 +74,6 @@ def check_infrastructure(self) -> Dict[str, bool]: else: available["ssh"] = False - # Check cloud providers - available["aws"] = bool(os.getenv("AWS_ACCESS_KEY_ID")) - available["gcp"] = bool(os.getenv("GOOGLE_APPLICATION_CREDENTIALS")) - available["azure"] = bool(os.getenv("AZURE_SUBSCRIPTION_ID")) - # Check Docker try: result = subprocess.run(["docker", "info"], capture_output=True, timeout=5) @@ -212,15 +192,6 @@ def run_all_tests(self, categories: Optional[List[str]] = None): "test_secure_credentials_real.py", "test_auth_fallbacks_real.py", ], - "cloud_providers": [ - "test_cloud_providers_gcp_real.py", - "test_cloud_providers_aws_real.py", - "test_cloud_providers_azure_real.py", - ], - "kubernetes": [ - "real_world/test_kubernetes_end_to_end_execution.py", - "real_world/test_kubernetes_local_execution.py", - ], "ssh": ["real_world/test_ssh_job_execution_real.py"], "notebook": ["test_notebook_magic_real.py"], } @@ -237,17 +208,6 @@ def run_all_tests(self, categories: Optional[List[str]] = None): start_time = time.time() for category, tests in test_categories.items(): - # Skip cloud tests if no credentials - if category == "cloud_providers": - if not any([available["aws"], available["gcp"], available["azure"]]): - print(f"\n⏭️ Skipping {category} tests (no cloud credentials)") - continue - - # Skip Kubernetes tests if not available - if category == "kubernetes" and not available["kubernetes"]: - print(f"\n⏭️ Skipping {category} tests (Kubernetes not available)") - continue - # Skip SSH tests if not available if category == "ssh" and not available["ssh"]: print(f"\n⏭️ Skipping {category} tests (SSH not available)") @@ -340,8 +300,6 @@ def main(): "decorator", "config", "credentials", - "cloud_providers", - "kubernetes", "ssh", "notebook", ], From 48aff75eeab98b3a1cfa3572b71d81ede4f3c9f8 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:53:18 -0400 Subject: [PATCH 29/56] Correct stale references to deleted backends in integration gate and SSH validator tests/integration/conftest.py named test_eks_permissions.py and test_aws_eks_debug.py as the motivating examples; neither file exists any more, so the rationale is restated without them. The guard itself is unchanged -- it still stops collection directory-wide. validate_ssh_cluster_access no longer probes remote hosts for PBS, SGE and LSF binaries, since clustrix cannot submit to any of them. --- tests/integration/conftest.py | 17 ++++++++--------- .../validate_ssh_cluster_access.py | 5 +---- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index bb68ef04..62fa4183 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -3,26 +3,25 @@ See issue #109. Everything in this directory talks to external infrastructure, and a large -subset provisions **billable** cloud resources (AWS EKS clusters, EC2 -instances, GPU nodes). Before this gate existed, the documented command +subset consumes **billable** resources (remote GPU nodes, paid job APIs). +Before this gate existed, the documented command ``pytest tests/ -m "not real_world"`` collected this directory, because none of its files carried a pytest marker. A marker-based skip would not be sufficient. pytest must *import* a module in order to collect it, and several modules here are standalone scripts rather -than test modules -- ``test_eks_permissions.py`` and ``test_aws_eks_debug.py`` -fetch credentials, call boto3, and invoke ``exit()`` at module scope. Importing -them is itself the harm: +than test modules: they fetch credentials, open connections and invoke +``exit()`` at module scope. Importing them is itself the harm: -* the AWS calls happen before any marker or skip is consulted, and -* the module-level ``sys.exit(1)`` raises ``SystemExit`` during collection, +* the external calls happen before any marker or skip is consulted, and +* a module-level ``sys.exit(1)`` raises ``SystemExit`` during collection, which crashes the whole pytest run with ``INTERNALERROR``. So the gate has to stop *collection*, which is what ``collect_ignore_glob`` does -- pytest never imports an ignored file. The gate is deliberately directory-wide (default-deny) rather than a -per-file allowlist. Misclassifying one file out of ~48 costs real money, and a +per-file allowlist. Misclassifying a single file costs real money, and a newly added file must not be able to run for free simply because nobody remembered to mark it. @@ -30,7 +29,7 @@ CLUSTRIX_ALLOW_BILLABLE=1 pytest tests/integration/ -Be aware that doing so may create real, chargeable cloud resources. +Be aware that doing so may consume real, chargeable resources. """ import os diff --git a/tests/real_world/api_validation/validate_ssh_cluster_access.py b/tests/real_world/api_validation/validate_ssh_cluster_access.py index 54b180c1..811611dd 100644 --- a/tests/real_world/api_validation/validate_ssh_cluster_access.py +++ b/tests/real_world/api_validation/validate_ssh_cluster_access.py @@ -444,7 +444,7 @@ def clustrix_test_function(): def test_cluster_scheduler_detection(hostname, username, password=None, key_file=None): - """Detect available cluster schedulers (SLURM, PBS, SGE, etc.).""" + """Detect available cluster schedulers (currently only SLURM is supported).""" print(f"\n⚙️ Cluster Scheduler Detection: {username}@{hostname}") print("=" * 60) @@ -471,9 +471,6 @@ def test_cluster_scheduler_detection(hostname, username, password=None, key_file # Test for different schedulers scheduler_tests = { "slurm": ["sinfo", "squeue", "sbatch"], - "pbs": ["qstat", "qsub", "pbsnodes"], - "sge": ["qstat", "qsub", "qhost"], - "lsf": ["bjobs", "bsub", "bhosts"], } detected_schedulers = {} From f455f6890a7b4917abb14ddf4d769c101c2acd5b Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:53:54 -0400 Subject: [PATCH 30/56] Unblock collection: drop the deleted kubernetes reference workflow tests/reference_workflows/kubernetes_workflows.py went with the backend, but test_reference_workflows.py still imported two workflows from it, so `pytest tests/` aborted during collection and no test in the suite ran. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- tests/test_reference_workflows.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/tests/test_reference_workflows.py b/tests/test_reference_workflows.py index 5c5b02f8..f0b172f3 100644 --- a/tests/test_reference_workflows.py +++ b/tests/test_reference_workflows.py @@ -25,11 +25,6 @@ test_file_processing_workflow as file_processing_workflow, ) -from tests.reference_workflows.kubernetes_workflows import ( - test_kubernetes_auto_provisioning_workflow as kubernetes_auto_provisioning_workflow, - test_kubernetes_multi_node_workflow as kubernetes_multi_node_workflow, -) - from tests.reference_workflows.data_analysis_workflows import ( test_pandas_analysis_workflow as pandas_analysis_workflow, test_numpy_computation_workflow as numpy_computation_workflow, @@ -60,19 +55,6 @@ def test_data_analysis_workflows_local(self): numpy_computation_workflow() machine_learning_workflow() - @pytest.mark.real_world - @pytest.mark.skipif( - not os.getenv("K8S_TEST_ENABLED", "false").lower() == "true", - reason="Kubernetes testing not enabled", - ) - def test_kubernetes_workflows(self): - """Test Kubernetes workflows with real provisioning.""" - # Use local provider for CI testing - os.environ["K8S_TEST_PROVIDER"] = "local" - - kubernetes_auto_provisioning_workflow() - kubernetes_multi_node_workflow() - @pytest.mark.real_world @pytest.mark.skipif( not os.getenv("SLURM_TEST_ENABLED", "false").lower() == "true", From 75c5ebfcb02d8a5b79cf41c5581bbb7438c02cf6 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:55:05 -0400 Subject: [PATCH 31/56] Tests: narrow scheduler-shape tests to slurm/ssh, drop cloud result path test_two_venv_execution and test_script_injection are reparametrised over the backends create_job_script still dispatches. The SSH job directory is defended by shlex quoting rather than by directive validation, so that is now asserted directly instead of being folded into the directive test, where SSH would have passed vacuously. The cloud-worker signing tests go with executor_cloud.py; the equivalent guarantees are covered by TestGeneratedWorkerSignsWhatItWrites, TestVerificationFailsClosed and tests/unit/test_hf_jobs.py. --- tests/unit/test_aws_cleanup_scripts.py | 23 ++++---- tests/unit/test_result_authentication.py | 67 ++++++++---------------- tests/unit/test_script_injection.py | 49 +++++++++++++---- tests/unit/test_two_venv_execution.py | 39 +++++++++----- 4 files changed, 94 insertions(+), 84 deletions(-) diff --git a/tests/unit/test_aws_cleanup_scripts.py b/tests/unit/test_aws_cleanup_scripts.py index 8796e68c..c1bd8d3e 100644 --- a/tests/unit/test_aws_cleanup_scripts.py +++ b/tests/unit/test_aws_cleanup_scripts.py @@ -439,9 +439,16 @@ def test_no_destructive_calls_at_module_scope(self, script_path): class TestTaggingConventionMatchesProvisioner: """The scripts must honour the exact tag/name convention that - clustrix.kubernetes.aws_provisioner.AWSEKSFromScratchProvisioner uses, + clustrix.kubernetes.aws_provisioner.AWSEKSFromScratchProvisioner used, per issue #95 ('Whatever tagging/naming convention the original used, - honour it and state it in --help').""" + honour it and state it in --help'). + + The provisioner itself has been removed with the Kubernetes/AWS backends + (issues #142, #143), so the cross-check against its source is gone. These + scripts are kept because resources provisioned by earlier versions of + clustrix are still out there carrying these tags and still need deleting; + the constants below are what identifies them. + """ def test_cleanup_uses_clustrix_managed_tag(self): module = _load_module(CLEANUP_SCRIPT) @@ -459,15 +466,3 @@ def test_destroy_iam_role_names_match_provisioner(self): cluster_role, node_role = module.iam_role_names("demo-cluster") assert cluster_role == "clustrix-eks-cluster-role-demo-cluster" assert node_role == "clustrix-eks-node-role-demo-cluster" - - def test_provisioner_actually_applies_these_tags(self): - """Cross-check against the real provisioner source so this test - (and the scripts) can't silently drift from what - aws_provisioner.py actually tags resources with.""" - provisioner_path = REPO_ROOT / "clustrix" / "kubernetes" / "aws_provisioner.py" - assert provisioner_path.is_file() - source = provisioner_path.read_text(encoding="utf-8") - assert '"clustrix:managed": "true"' in source - assert '"clustrix:cluster"' in source - assert "clustrix-eks-cluster-role-" in source - assert "clustrix-eks-node-role-" in source diff --git a/tests/unit/test_result_authentication.py b/tests/unit/test_result_authentication.py index fe24f277..4d327533 100644 --- a/tests/unit/test_result_authentication.py +++ b/tests/unit/test_result_authentication.py @@ -342,57 +342,34 @@ def test_the_key_is_gone_from_the_environment_before_the_function_runs( # -------------------------------------------------------------------------- -# V2 -- the cloud path signs its result and the caller checks it +# V2 -- an unsigned result is refused +# +# Two tests here covered the cloud VM worker: that it signed its result, and +# that CloudJobManager._execute_job_on_cloud_instance verified the signature +# before unpickling. Both went with clustrix/executor_cloud.py, which was +# removed along with the cloud VM backends (issues #143-#146) -- no cloud job +# had ever been shown to run end to end. +# +# Neither guarantee is lost for the backends that remain: +# * the worker signs what it writes -- TestGeneratedWorkerSignsWhatItWrites +# above, which runs the real emitted program out of job_execution_lines +# (the shared SLURM/SSH body) in a subprocess; and +# tests/unit/test_hf_jobs.py, which asserts the HF Jobs worker computes +# the same HMAC. +# * the caller verifies before deserializing -- TestVerificationFailsClosed +# above, which drives ClusterExecutor._verify_result_signature, and +# TestErrorPayloadAuthentication for the error.pkl half. +# +# What survives here is the property about verify_signed_payload itself. # -------------------------------------------------------------------------- -class TestCloudResultAuthentication: - def _script(self, work_dir: Path) -> str: - from clustrix.executor_cloud import CloudJobManager - - return CloudJobManager(ClusterConfig())._create_cloud_execution_script( - str(work_dir), {} - ) - - def test_cloud_worker_signs_its_result(self, tmp_path): - script = self._script(tmp_path) - data = serialize_function(_read_the_key, (), {}) - with open(tmp_path / "func_data.pkl", "wb") as handle: - cloudpickle.dump(data, handle) - script_path = tmp_path / "execute_job.py" - script_path.write_text(script) - - result = subprocess.run( - [sys.executable, str(script_path)], - cwd=tmp_path, - env=dict(os.environ, CLUSTRIX_RESULT_KEY=KEY), - capture_output=True, - text=True, - ) - - assert result.returncode == 0, result.stdout + result.stderr - blob = (tmp_path / "result.pkl").read_bytes() - verify_signed_payload( - blob, (tmp_path / "result.pkl.hmac").read_text(), KEY, "cloud worker" - ) - # Written with dill, as the caller's dill.load has always claimed. - assert dill.loads(blob) is None - - def test_the_caller_verifies_before_deserializing(self): - """A regression guard on the seam that had no check at all.""" - import inspect - - from clustrix.executor_cloud import CloudJobManager - - source = inspect.getsource(CloudJobManager._execute_job_on_cloud_instance) - assert "verify_signed_payload" in source - assert "result.pkl.hmac" in source or ".hmac" in source - - def test_an_unsigned_cloud_result_would_be_refused(self): +class TestAnUnsignedResultIsRefused: + def test_a_result_arriving_without_a_signature_is_refused(self): blob = dill.dumps({"answer": 42}, protocol=4) with pytest.raises(PayloadAuthenticationError, match="no signature"): - verify_signed_payload(blob, "", KEY, "Cloud job x") + verify_signed_payload(blob, "", KEY, "Job x") # -------------------------------------------------------------------------- diff --git a/tests/unit/test_script_injection.py b/tests/unit/test_script_injection.py index ac8469e9..dd513f52 100644 --- a/tests/unit/test_script_injection.py +++ b/tests/unit/test_script_injection.py @@ -227,25 +227,52 @@ def test_partition_carrying_an_sbatch_directive_is_refused(self): "slurm", job_config, "/scratch/jobs/job_1", ClusterConfig() ) - def test_pbs_queue_carrying_a_directive_is_refused(self): - job_config = dict(BASE_JOB_CONFIG, queue="normal -l walltime=99:00:00") + def test_a_job_directory_with_shell_syntax_is_refused_in_directives(self): + """Directive lines cannot be quoted, so the value has to be clean. - with pytest.raises(ValueError, match="queue"): - create_job_script("pbs", job_config, "/scratch/jobs/job_1", ClusterConfig()) - - @pytest.mark.parametrize("cluster_type", ["slurm", "pbs", "sge"]) - def test_a_job_directory_with_shell_syntax_is_refused_in_directives( - self, cluster_type - ): - """Directive lines cannot be quoted, so the value has to be clean.""" + Narrowed from ["slurm", "pbs", "sge"] to SLURM: PBS and SGE were + removed, and SLURM is now the only backend that writes the job + directory into a scheduler directive. SSH, the other remote backend, + writes it only into shell commands and is covered by the next test. + """ with pytest.raises(ValueError, match="remote_work_dir"): create_job_script( - cluster_type, + "slurm", dict(BASE_JOB_CONFIG), "/scratch/$(touch /tmp/pwn)/job_1", ClusterConfig(), ) + @pytest.mark.parametrize( + "hostile_dir", + [ + "/scratch/$(touch /tmp/pwn)/job_1", + "/scratch/`touch /tmp/pwn`/job_1", + "/scratch/x'; touch /tmp/pwn; '", + ], + ) + def test_an_ssh_job_directory_is_quoted_rather_than_expanded(self, hostile_dir): + """SSH has no directive lines, so it defends by quoting instead. + + The value reaches `cd` and `cat` only inside single quotes, which the + shell does not expand. Asserting this explicitly because the previous + parametrisation over ["slurm", "pbs", "sge"] never covered SSH at all, + and substituting SSH into the directive test above would have been a + false negative: it raises nothing because it has nothing to validate. + """ + script = create_job_script( + "ssh", dict(BASE_JOB_CONFIG), hostile_dir, ClusterConfig() + ) + + # No unquoted occurrence of the payload anywhere in the script. + assert "touch /tmp/pwn" in script # it is present... + for line in script.splitlines(): + if "touch /tmp/pwn" in line: + # ...but only ever inside a single-quoted word. + assert line.count("'") >= 2, line + assert not line.startswith("cd /scratch"), line + assert "cd " + hostile_dir not in script + def test_walltime_and_cores_are_validated_too(self): with pytest.raises(ValueError, match="time"): create_job_script( diff --git a/tests/unit/test_two_venv_execution.py b/tests/unit/test_two_venv_execution.py index ad73ffcc..df4d7cd6 100644 --- a/tests/unit/test_two_venv_execution.py +++ b/tests/unit/test_two_venv_execution.py @@ -138,17 +138,20 @@ def test_conda_mode_does_not_emit_deactivate(self): assert "deactivate" not in generate_two_venv_execution_commands(*CONDA) -class TestEverySchedulerRunsTheSameBody: - """PBS and SGE used to diverge from SLURM, and PBS did not work at all. +class TestEveryBackendRunsTheSameBody: + """Remote backends used to each carry their own copy of the job body. PBS ended its script with ``python execute_function.py`` -- a file nothing in clustrix has ever created, so every PBS job died immediately. SGE carried its own copy of the single-venv script, which meant it silently missed the two-venv path, the conda sourcing and the result signing as - those were fixed on the SLURM one. All three now share one body. + those were fixed on the SLURM one. Both backends have since been removed + (issues #140, #141) for never having been run against real hardware, so + the invariant is asserted over the two remote backends that remain: only + the directive header may differ between them. """ - SCHEDULERS = ("slurm", "pbs", "sge") + SCHEDULERS = ("slurm", "ssh") def _script(self, scheduler, *, two_venv): from clustrix.config import ClusterConfig @@ -169,37 +172,45 @@ def _script(self, scheduler, *, two_venv): ) @pytest.mark.parametrize("scheduler", SCHEDULERS) - def test_no_scheduler_runs_a_file_that_is_never_created(self, scheduler): + def test_no_backend_runs_a_file_that_is_never_created(self, scheduler): assert "execute_function.py" not in self._script(scheduler, two_venv=False) @pytest.mark.parametrize("scheduler", SCHEDULERS) - def test_every_scheduler_reads_the_function_payload(self, scheduler): + def test_every_backend_reads_the_function_payload(self, scheduler): assert "function_data.pkl" in self._script(scheduler, two_venv=False) @pytest.mark.parametrize("scheduler", SCHEDULERS) - def test_every_scheduler_uses_the_two_venv_path_when_available(self, scheduler): + def test_every_backend_uses_the_two_venv_path_when_available(self, scheduler): script = self._script(scheduler, two_venv=True) assert "VENV1" in script assert "conda run -n e1" in script assert "conda run -n e2" in script @pytest.mark.parametrize("scheduler", SCHEDULERS) - def test_every_scheduler_sources_conda(self, scheduler): + def test_every_backend_sources_conda(self, scheduler): script = self._script(scheduler, two_venv=True) assert "source /opt/conda/etc/profile.d/conda.sh" in script @pytest.mark.parametrize("scheduler", SCHEDULERS) - def test_every_scheduler_signs_its_result(self, scheduler): + def test_every_backend_signs_its_result(self, scheduler): script = self._script(scheduler, two_venv=True) assert "result.pkl.hmac" in script assert "CLUSTRIX_RESULT_KEY" in script - def test_the_bodies_are_identical_across_schedulers(self): - """Only the directive header should differ between schedulers.""" + def test_the_bodies_are_identical_across_backends(self): + """Only the directive header should differ between backends. + + The slice starts at the result-key export rather than at the first + `cd`. The SSH script emits its own `cd ` before the shared + block, so the shared block itself begins one line later; comparing + from the first `cd` would compare SSH's redundant one against SLURM's + and report a difference that is not in the executed body. Everything + that actually runs the function -- venv activation, both `python -c` + programs, the signing step -- is inside the compared region. + """ bodies = {} for scheduler in self.SCHEDULERS: script = self._script(scheduler, two_venv=True) - # Drop the scheduler directives; keep everything from `cd` onward. - body = script[script.index("cd /remote/job") :] + body = script[script.index("export CLUSTRIX_RESULT_KEY") :] bodies[scheduler] = body - assert bodies["slurm"] == bodies["pbs"] == bodies["sge"] + assert bodies["slurm"] == bodies["ssh"] From 9f39b7ca5ad7974ffbba062eb58cc4941d5d5e97 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:56:32 -0400 Subject: [PATCH 32/56] Apply black formatting across tests/real_world and tests/integration test_gpu_detection.py and test_ssh_real.py were already unformatted before this branch; fixed here rather than left for CI to trip over. --- tests/integration/test_gpu_detection.py | 4 +--- .../validate_ssh_cluster_access.py | 4 +--- .../test_advanced_schedulers_comprehensive.py | 4 +--- tests/real_world/test_ssh_real.py | 4 +++- tests/unit/test_functions.py | 19 ------------------- 5 files changed, 6 insertions(+), 29 deletions(-) delete mode 100644 tests/unit/test_functions.py diff --git a/tests/integration/test_gpu_detection.py b/tests/integration/test_gpu_detection.py index 156fa4d3..3c053450 100644 --- a/tests/integration/test_gpu_detection.py +++ b/tests/integration/test_gpu_detection.py @@ -229,6 +229,4 @@ def test_executor_integration(): if passed == total: print("\n🎉 All GPU detection tests passed!") else: - print( - f"\n⚠️ {total - passed} tests failed - GPU functionality needs attention" - ) + print(f"\n⚠️ {total - passed} tests failed - GPU functionality needs attention") diff --git a/tests/real_world/api_validation/validate_ssh_cluster_access.py b/tests/real_world/api_validation/validate_ssh_cluster_access.py index 811611dd..81eaf0b6 100644 --- a/tests/real_world/api_validation/validate_ssh_cluster_access.py +++ b/tests/real_world/api_validation/validate_ssh_cluster_access.py @@ -627,9 +627,7 @@ def main(): f" {'✅' if status == 'PASSED' else '⚠️'} {test_name}: {status} ({success_rate*100:.1f}%)" ) elif result == {}: - print( - f" ⚠️ {test_name}: No schedulers detected (regular SSH server)" - ) + print(f" ⚠️ {test_name}: No schedulers detected (regular SSH server)") else: print(f" ❌ {test_name}: FAILED") diff --git a/tests/real_world/test_advanced_schedulers_comprehensive.py b/tests/real_world/test_advanced_schedulers_comprehensive.py index e96a1c0a..e622ad1f 100644 --- a/tests/real_world/test_advanced_schedulers_comprehensive.py +++ b/tests/real_world/test_advanced_schedulers_comprehensive.py @@ -315,9 +315,7 @@ def test_scheduler_queue_systems(self): f" Queue info: {len(result.stdout.strip().split(chr(10)))} lines" ) else: - logger.warning( - f"⚠️ {scheduler.upper()} queue system not accessible" - ) + logger.warning(f"⚠️ {scheduler.upper()} queue system not accessible") except subprocess.TimeoutExpired: queue_tests.append( diff --git a/tests/real_world/test_ssh_real.py b/tests/real_world/test_ssh_real.py index dfffc8a3..a26047a2 100644 --- a/tests/real_world/test_ssh_real.py +++ b/tests/real_world/test_ssh_real.py @@ -310,7 +310,9 @@ def test_ssh_config_file_operations_real(self): HostName 127.0.0.1 User {username} Port 22 -""".format(username=os.getenv("USER", "user")) +""".format( + username=os.getenv("USER", "user") + ) config_file = temp_mgr.create_temp_file(ssh_config_content, ".config") diff --git a/tests/unit/test_functions.py b/tests/unit/test_functions.py deleted file mode 100644 index 1787339c..00000000 --- a/tests/unit/test_functions.py +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env python3 -""" -Test functions for Kubernetes execution. -""" - - -def simple_computation(x: int, y: int): - """Simple computation function for local testing.""" - import platform - import socket - - result = x * y + 42 - - return { - "result": result, - "platform": platform.platform(), - "hostname": socket.gethostname(), - "environment": "kubernetes", - } From 2eb3ef03ca1b7a02c983ccaf6f99e562d2f05e11 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:58:36 -0400 Subject: [PATCH 33/56] Tests: sweep remaining removed-backend references out of owned files Drops the Kubernetes fixture/test from test_executor_real.py, the fractional-CPU Kubernetes edge case, and tests/unit/test_functions.py (a Kubernetes helper module nothing imported). Repoints the auth-fallback profiles and the conftest docstring at backends that still exist. --- tests/comprehensive/test_edge_cases_real.py | 27 ------- tests/conftest.py | 2 +- tests/test_auth_fallbacks_real.py | 30 +++---- tests/test_executor_real.py | 90 --------------------- 4 files changed, 16 insertions(+), 133 deletions(-) diff --git a/tests/comprehensive/test_edge_cases_real.py b/tests/comprehensive/test_edge_cases_real.py index eb6c2435..60532da2 100644 --- a/tests/comprehensive/test_edge_cases_real.py +++ b/tests/comprehensive/test_edge_cases_real.py @@ -275,33 +275,6 @@ def excessive_resources(): # Expected to fail with excessive requests assert "resource" in str(e).lower() or "memory" in str(e).lower() - def test_fractional_core_request(self): - """ - Test behavior with fractional core requests. - - Some systems support fractional CPU allocation. - """ - configure(cluster_type="kubernetes") - - @cluster(cores=0.5, memory="512Mi") - def fractional_cpu(): - """Function with fractional CPU request.""" - import time - - start = time.time() - - # Do some CPU-bound work - total = sum(i * i for i in range(1000000)) - - duration = time.time() - start - return {"result": total, "duration": duration} - - # Execute if Kubernetes is available - if os.getenv("KUBECONFIG"): - result = fractional_cpu() - assert result["result"] > 0 - assert result["duration"] > 0 - def test_memory_string_formats(self): """ Test various memory specification formats. diff --git a/tests/conftest.py b/tests/conftest.py index 3fa79a66..9aabb937 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -234,7 +234,7 @@ def reset_config(): """Restore the global configuration singleton after every test. This used to reset eight hand-listed fields. Everything else a test set - -- k8s_namespace, remote_work_dir, package_manager, environment_variables, + -- remote_work_dir, package_manager, environment_variables, ssh_host_key_policy -- leaked into every test that ran afterwards, and ClusterConfig has over a hundred fields. The notebook widget reads the live config to populate itself, so it inherited whatever the previous diff --git a/tests/test_auth_fallbacks_real.py b/tests/test_auth_fallbacks_real.py index 4e803680..ac232361 100644 --- a/tests/test_auth_fallbacks_real.py +++ b/tests/test_auth_fallbacks_real.py @@ -380,13 +380,13 @@ def test_multi_cluster_authentication(self, temp_credentials_dir): "name": "cluster2", "host": "cluster2.example.com", "username": "user2", - "type": "pbs", + "type": "ssh", }, { "name": "cluster3", "host": "cluster3.example.com", "username": "user3", - "type": "sge", + "type": "huggingface", }, ] @@ -579,13 +579,13 @@ def test_credential_manager_workflow(self, temp_credentials_dir): "development": { "cluster_host": "dev.cluster.com", "username": "dev_user", - "cluster_type": "kubernetes", - "kubeconfig": "~/.kube/dev_config", + "cluster_type": "ssh", + "key_file": "~/.ssh/dev_key", }, "research": { "cluster_host": "research.hpc.edu", "username": "researcher", - "cluster_type": "pbs", + "cluster_type": "slurm", "password": None, # Will need fallback }, }, @@ -608,19 +608,19 @@ def test_credential_manager_workflow(self, temp_credentials_dir): config = ClusterConfig() # Apply profile settings that are real ClusterConfig fields. - # "kubeconfig" is not a real field -- it is Kubernetes' own - # credential mechanism, tracked separately below. + # The "development" profile used to be a Kubernetes one carrying a + # "kubeconfig" key -- not a real ClusterConfig field, and a backend + # that has since been removed (issue #142). It is an SSH profile + # with a key file now, which is what made its expected outcome + # (no password fallback) true in the first place. for key, value in profile_config.items(): if hasattr(config, key): setattr(config, key, value) - # A profile with a usable credential (an SSH key file, or a - # kubeconfig for Kubernetes) represents a key-setup attempt - # that succeeded; one without represents a failed/never - # attempted setup. - has_credential = bool( - profile_config.get("key_file") or profile_config.get("kubeconfig") - ) + # A profile with a usable credential (an SSH key file) represents + # a key-setup attempt that succeeded; one without represents a + # failed/never attempted setup. + has_credential = bool(profile_config.get("key_file")) key_setup_result = ( {"success": True, "connection_tested": True} if has_credential @@ -631,6 +631,6 @@ def test_credential_manager_workflow(self, temp_credentials_dir): if profile_name == "production": assert needs_auth is False # Has SSH key elif profile_name == "development": - assert needs_auth is False # Kubernetes uses kubeconfig + assert needs_auth is False # Has SSH key elif profile_name == "research": assert needs_auth is True # Needs password fallback diff --git a/tests/test_executor_real.py b/tests/test_executor_real.py index 09ef534d..7fccfe04 100644 --- a/tests/test_executor_real.py +++ b/tests/test_executor_real.py @@ -27,18 +27,6 @@ def local_config(self): config.cleanup_remote_files = True return config - @pytest.fixture - def kubernetes_config(self): - """Create configuration for Kubernetes testing.""" - config = ClusterConfig() - config.cluster_type = "kubernetes" - config.auto_provision_k8s = True - config.k8s_provider = "local" # Use Docker Desktop or kind - config.k8s_node_count = 1 - config.k8s_cleanup_on_exit = True - config.k8s_cluster_name = f"test-executor-{int(time.time())}" - return config - @pytest.fixture def ssh_config(self): """Create configuration for SSH testing if available.""" @@ -132,84 +120,6 @@ def compute_statistics(data): finally: executor.disconnect() - @pytest.mark.real_world - def test_job_submission_kubernetes(self, kubernetes_config): - """ - Test job submission with Kubernetes. - - This demonstrates: - - Real Kubernetes job submission - - Container-based execution - - Pod monitoring and result retrieval - """ - # Skip if Kubernetes not available - if not os.getenv("K8S_TEST_ENABLED", "false").lower() == "true": - pytest.skip("Kubernetes testing not enabled") - - executor = ClusterExecutor(kubernetes_config) - - # Ensure cluster is ready (auto-provisions if needed) - executor.ensure_cluster_ready(timeout=300) - - try: - # Define computation for Kubernetes - def analyze_in_k8s(n): - """Perform analysis in Kubernetes pod.""" - import platform - import socket - import math - - # Compute prime numbers up to n - primes = [] - for num in range(2, n + 1): - is_prime = True - for i in range(2, int(math.sqrt(num)) + 1): - if num % i == 0: - is_prime = False - break - if is_prime: - primes.append(num) - - return { - "primes_count": len(primes), - "largest_prime": max(primes) if primes else None, - "execution_host": socket.gethostname(), - "platform": platform.platform(), - "container": "kubernetes" in platform.platform().lower() - or "linux" in platform.platform().lower(), - } - - # Serialize and submit - from clustrix.utils import serialize_function - - func_data = serialize_function(analyze_in_k8s, (100,), {}) - - job_config = {"cores": 1, "memory": "512Mi"} - job_id = executor.submit_job(func_data, job_config) - - # Verify Kubernetes job ID format - assert job_id is not None - assert "clustrix-job" in job_id or isinstance(job_id, str) - - # Monitor job status - status = executor.get_job_status(job_id) - assert status in ["pending", "running", "completed", "failed"] - - # Wait for completion - result = executor.wait_for_result(job_id, timeout=120) - - # Validate Kubernetes execution - assert result["primes_count"] == 25 # 25 primes under 100 - assert result["largest_prime"] == 97 - assert result["container"] is True # Should detect container environment - assert len(result["execution_host"]) > 0 - - finally: - # Cleanup Kubernetes resources - if hasattr(executor, "cleanup_auto_provisioned_cluster"): - executor.cleanup_auto_provisioned_cluster() - executor.disconnect() - @pytest.mark.real_world def test_job_submission_ssh(self, ssh_config): """ From 2b67e80e5236407e74f057ae14c0ba11091e4181 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 10:59:50 -0400 Subject: [PATCH 34/56] Tests: @cluster(platform=...) proved nothing once platform became **kwargs --- tests/unit/test_import_and_basic_functionality.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_import_and_basic_functionality.py b/tests/unit/test_import_and_basic_functionality.py index 9927a328..d2dffed1 100644 --- a/tests/unit/test_import_and_basic_functionality.py +++ b/tests/unit/test_import_and_basic_functionality.py @@ -151,7 +151,10 @@ def func2(): def func3(): return 3 - @cluster(platform="local") + # Was @cluster(platform="local"). `platform` was a parameter of the + # removed cloud backends; it now only lands in **kwargs, so decorating + # with it proved nothing. `partition` is a real parameter. + @cluster(partition="gpu") def func4(): return 4 From 62b125ee32c0191338a8867ef7d54444a69a5697 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:00:12 -0400 Subject: [PATCH 35/56] Tests: black formatting --- tests/unit/test_result_authentication.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_result_authentication.py b/tests/unit/test_result_authentication.py index 4d327533..35949ac8 100644 --- a/tests/unit/test_result_authentication.py +++ b/tests/unit/test_result_authentication.py @@ -392,7 +392,9 @@ def test_single_venv_program_refuses_to_fall_back_to_stdlib_pickle(self, tmp_pat # Python 3.12, so a find_module-based blocker is simply ignored there # and the child imports dill perfectly well -- the test then passes # vacuously on <=3.11 and fails on 3.12 for the wrong reason. - (blocker / "sitecustomize.py").write_text(textwrap.dedent(""" + (blocker / "sitecustomize.py").write_text( + textwrap.dedent( + """ import sys class _Block: def find_spec(self, name, path=None, target=None): @@ -400,7 +402,9 @@ def find_spec(self, name, path=None, target=None): raise ImportError(name) return None sys.meta_path.insert(0, _Block()) - """)) + """ + ) + ) env = dict(os.environ, CLUSTRIX_RESULT_KEY=KEY, PYTHONPATH=str(blocker)) result = subprocess.run( [sys.executable, "-c", program], From 2d98ec6f369e1ddae540e8bde9d7ab49580ecd69 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:01:23 -0400 Subject: [PATCH 36/56] Reject a removed backend where the user can act on it A removed cluster_type was only caught by load_config. Every other route in -- ClusterConfig(...) directly, configure(...), or a config already in memory -- carried it all the way to ClusterExecutor.submit_job, which checked the type *after* self.connect(). So asking for a backend that no longer exists cost an SSH round trip to a host that was never going to be used, and then said only "Unsupported cluster type: pbs". validate_cluster_type() is now the single check, called from __post_init__, from configure(), from load_config and from the executor before it connects. It distinguishes three cases rather than two: a supported type, a *removed* one (named, with why it went and its tracking issue), and an ordinary typo. configure() also validates before it applies anything. It previously setattr'd its way through kwargs and raised partway, so a call that failed had still changed the live configuration. Evidence, all against real files on disk: load_config, file containing `cluster_type: pbs` cluster_type='pbs' is no longer implemented. It was removed in v0.2.0 because it had never been verified against real hardware. Its return is tracked in issue #140. Supported types are: local, ssh, slurm, huggingface. load_config, file containing `k8s_namespace: compute` contains unknown setting(s): k8s_namespace configured Kubernetes, which has been removed (see issue #142) load_config, file containing a genuine typo `cluster_hostt` contains unknown setting(s): cluster_hostt (did you mean cluster_host?) ClusterConfig(cluster_type="kubernetes") -> named, issue #142 configure(cluster_type="aws") -> named, issue #143 configure(k8s_namespace="compute") -> named, issue #142 cluster_type after a rejected configure(): 'slurm' (unchanged) The did-you-mean path is deliberately still reachable: a removed setting must not look like a spelling mistake, and a spelling mistake must not look like a removed setting. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/config.py | 65 ++++++++++++++++++++++++++++++--------- clustrix/executor_core.py | 6 ++++ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/clustrix/config.py b/clustrix/config.py index 139e12d6..28e8666a 100644 --- a/clustrix/config.py +++ b/clustrix/config.py @@ -182,6 +182,8 @@ def __post_init__(self): f"(insecure, trusts unknown host keys automatically)." ) + validate_cluster_type(self.cluster_type) + def get_env_password(self) -> Optional[str]: """Get password from specified environment variable.""" if self.use_env_password and self.password_env_var: @@ -296,6 +298,33 @@ def _removed_setting_reason(name: str) -> Optional[str]: return None +def validate_cluster_type(cluster_type: str, source: str = "cluster_type") -> None: + """Reject a backend clustrix cannot run, saying which kind of wrong it is. + + Three outcomes rather than two: a supported type passes, a *removed* type + is named along with why it went and where it is tracked, and anything else + is an ordinary typo. Collapsing the middle case into the last one is what + left ``cluster_type: pbs`` looking like a spelling mistake. + """ + if cluster_type in SUPPORTED_CLUSTER_TYPES: + return + + supported = ", ".join(SUPPORTED_CLUSTER_TYPES) + if cluster_type in REMOVED_CLUSTER_TYPES: + issue = REMOVED_CLUSTER_TYPES[cluster_type] + where = f" Its return is tracked in issue #{issue}." if issue else "" + raise ValueError( + f"{source}={cluster_type!r} is no longer implemented. It was " + f"removed in v0.2.0 because it had never been verified against " + f"real hardware.{where} Supported types are: {supported}." + ) + + raise ValueError( + f"{source}={cluster_type!r} is not a supported cluster type. " + f"Supported types are: {supported}." + ) + + _SECRET_FIELD_PATTERN = re.compile( r"secret|token|password|api_key|access_key|_key$|client_id|tenant_id" r"|subscription_id", @@ -385,12 +414,26 @@ def configure(**kwargs) -> None: """ global _config # noqa: F824 - # Update configuration with provided kwargs - for key, value in kwargs.items(): + # Validate everything before applying anything: a rejected keyword used + # to leave the earlier ones already written to the live config, so a + # failed configure() call still changed the process's behaviour. + for key in kwargs: if hasattr(_config, key): - setattr(_config, key, value) - else: - raise ValueError(f"Unknown configuration parameter: {key}") + continue + removed = _removed_setting_reason(key) + if removed: + raise ValueError(removed) + raise ValueError(f"Unknown configuration parameter: {key}") + + if "cluster_type" in kwargs: + # setattr below does not re-run __post_init__, so without this a + # removed backend reaches the executor and fails there instead -- + # after connect(), i.e. after an SSH round trip to a host that was + # never going to be used. + validate_cluster_type(kwargs["cluster_type"]) + + for key, value in kwargs.items(): + setattr(_config, key, value) def load_config(config_path: str) -> None: @@ -442,15 +485,9 @@ def load_config(config_path: str) -> None: f"{config_path} contains unknown setting(s): {'; '.join(hints)}" ) - requested = config_data.get("cluster_type") - if requested in REMOVED_CLUSTER_TYPES: - issue = REMOVED_CLUSTER_TYPES[requested] - where = f" It is tracked in issue #{issue}." if issue else "" - raise ValueError( - f"{config_path} requests cluster_type={requested!r}, which clustrix " - f"no longer implements. It was removed because it had never been " - f"verified against real hardware.{where} Supported types are: " - f"{', '.join(SUPPORTED_CLUSTER_TYPES)}." + if "cluster_type" in config_data: + validate_cluster_type( + config_data["cluster_type"], source=f"{config_path}: cluster_type" ) _config = ClusterConfig(**config_data) diff --git a/clustrix/executor_core.py b/clustrix/executor_core.py index 93a2d107..b6bc3e6f 100644 --- a/clustrix/executor_core.py +++ b/clustrix/executor_core.py @@ -14,6 +14,7 @@ import cloudpickle +from .config import validate_cluster_type from .executor_connections import ConnectionManager from .executor_schedulers import SchedulerManager from .hf_jobs import HFJobsManager @@ -74,6 +75,11 @@ def submit_job(self, func_data: Dict[str, Any], job_config: Dict[str, Any]) -> s self.active_jobs[job_id] = {"manager": "huggingface", "job_id": job_id} return job_id + # Checked before connect(): a cluster type this executor cannot + # dispatch used to fail *after* an SSH round trip to a host that was + # never going to be used. + validate_cluster_type(self.config.cluster_type) + # Ensure connection is established for traditional cluster types self.connect() From 1f1086001eb10e674ee70c0a603ecbd7cee71a83 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:01:24 -0400 Subject: [PATCH 37/56] Tests: pin the CLI cluster-type choices to SUPPORTED_CLUSTER_TYPES Asserts the CLI offers every supported backend and refuses every removed one, so the two lists cannot drift apart again. --- tests/test_cli.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 19261f21..9a57407d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,7 +2,11 @@ from click.testing import CliRunner from unittest.mock import patch, Mock from clustrix.cli import cli -from clustrix.config import ClusterConfig +from clustrix.config import ( + ClusterConfig, + REMOVED_CLUSTER_TYPES, + SUPPORTED_CLUSTER_TYPES, +) class TestCLI: @@ -89,6 +93,28 @@ def test_config_invalid_cluster_type(self, mock_configure, runner): assert result.exit_code == 2 assert "Invalid value for '--cluster-type'" in result.output + def test_cluster_type_choices_are_exactly_the_supported_types(self, runner): + """The CLI must offer the shipped tuple, not its own copy of it. + + It once kept a hand-written list that omitted "huggingface", so a + backend that works could not be selected from the command line at + all. The same drift in the other direction would now advertise a + removed backend. + """ + result = runner.invoke(cli, ["config", "--help"]) + + assert result.exit_code == 0 + for supported in SUPPORTED_CLUSTER_TYPES: + assert supported in result.output + assert set(SUPPORTED_CLUSTER_TYPES) == {"local", "ssh", "slurm", "huggingface"} + + @pytest.mark.parametrize("removed", sorted(REMOVED_CLUSTER_TYPES)) + def test_a_removed_backend_cannot_be_selected_from_the_cli(self, removed, runner): + result = runner.invoke(cli, ["config", "--cluster-type", removed]) + + assert result.exit_code == 2 + assert "Invalid value for '--cluster-type'" in result.output + @patch("clustrix.cli.load_config") def test_load_config_success(self, mock_load_config, runner): """Test loading configuration from file.""" From 5c87991efcfe4eb3f567c246edf0d819ec5427ff Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:02:03 -0400 Subject: [PATCH 38/56] Tests: comment named a removed backend --- tests/test_executor_real.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_executor_real.py b/tests/test_executor_real.py index 7fccfe04..62e7fb13 100644 --- a/tests/test_executor_real.py +++ b/tests/test_executor_real.py @@ -344,7 +344,7 @@ def test_complete_data_processing_workflow(self): """ # User sets up configuration config = ClusterConfig() - config.cluster_type = "local" # Or "kubernetes", "slurm", etc. + config.cluster_type = "local" # Or "ssh", "slurm", "huggingface" original_config = config_module._config config_module._config = config From f26e9cfa50f01e7e1e8ef6630cbd0415d9cbc960 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:05:50 -0400 Subject: [PATCH 39/56] Tests: host-key tests were writing into the developer's real known_hosts paramiko's AutoAddPolicy saves accepted keys back to whatever file load_host_keys() named, so test_auto_add_policy_gets_past_host_key_check appended an [127.0.0.1]: line to ~/.ssh/known_hosts on every run (83 had accumulated locally). When an ephemeral port was reused against a freshly generated server key, the reject test hit BadHostKeyException instead of the missing-host-key policy and failed -- the suite was poisoning its own precondition. The real_ssh_server fixture now points ~ at a per-test directory. No assertion changed. --- tests/unit/test_host_key_policy.py | 46 +++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/unit/test_host_key_policy.py b/tests/unit/test_host_key_policy.py index ae0bbccb..e3259cc8 100644 --- a/tests/unit/test_host_key_policy.py +++ b/tests/unit/test_host_key_policy.py @@ -89,8 +89,41 @@ def stop(self): pass +def _redirect_home(monkeypatch, home): + """Point ``~`` at `home` for both POSIX and Windows expansion rules.""" + monkeypatch.setenv("HOME", str(home)) + if os.name == "nt": + # Path.home() expands ``~`` from USERPROFILE (falling back to + # HOMEDRIVE+HOMEPATH) on Windows and ignores HOME, so redirecting + # HOME alone would leave clustrix reading the real user's + # ~/.ssh/known_hosts. + drive, tail = os.path.splitdrive(str(home)) + monkeypatch.setenv("USERPROFILE", str(home)) + monkeypatch.setenv("HOMEDRIVE", drive) + monkeypatch.setenv("HOMEPATH", tail) + + @pytest.fixture -def real_ssh_server(): +def real_ssh_server(tmp_path, monkeypatch): + """A real local SSH server, with ``~`` pointed at a per-test directory. + + The HOME redirection is not cosmetic. `configure_host_key_policy` calls + `load_host_keys(~/.ssh/known_hosts)`, which sets paramiko's + `_host_keys_filename`; paramiko's AutoAddPolicy then *saves* every key it + accepts back to that file. Without this fixture the auto_add test below + appended an `[127.0.0.1]:` entry to the developer's real + known_hosts on every run -- 83 of them had accumulated on the machine + where this was found. Ephemeral ports are eventually reused, and when one + came back around against a freshly generated server key, paramiko raised + BadHostKeyException instead of invoking the missing-host-key policy and + `test_reject_policy_blocks_connection_to_real_unknown_host` failed. The + test was not wrong about the behaviour it asserts; it was poisoning its + own precondition (that the host is unknown) one run at a time. + """ + home = tmp_path / "home" + (home / ".ssh").mkdir(parents=True) + _redirect_home(monkeypatch, home) + server = _RealLocalSSHServer() server.start() time.sleep(0.1) # let the accept() loop actually reach listening state @@ -239,16 +272,7 @@ def test_user_known_hosts_file_is_actually_loaded(tmp_path, monkeypatch): f"127.0.0.1 {trusted_key.get_name()} {trusted_key.get_base64()}\n" ) - monkeypatch.setenv("HOME", str(fake_home)) - if os.name == "nt": - # Path.home() expands ``~`` from USERPROFILE (falling back to - # HOMEDRIVE+HOMEPATH) on Windows and ignores HOME, so redirecting - # HOME alone would leave clustrix reading the real user's - # ~/.ssh/known_hosts. - drive, tail = os.path.splitdrive(str(fake_home)) - monkeypatch.setenv("USERPROFILE", str(fake_home)) - monkeypatch.setenv("HOMEDRIVE", drive) - monkeypatch.setenv("HOMEPATH", tail) + _redirect_home(monkeypatch, fake_home) client = paramiko.SSHClient() configure_host_key_policy(client, None) From 68bd834fdf6a29e24f4a55f4291b4b0704e9fc43 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:12:04 -0400 Subject: [PATCH 40/56] Point five tests at the messages the code now actually produces All five asserted on wording that validate_cluster_type replaced. In every case the property under test is unchanged and the assertion is now stronger, not weaker: - test_config.py: "no longer implements" -> "no longer implemented". - test_enhanced_features.py: matched "Unknown configuration parameter", which is exactly the generic phrasing the removed-settings table exists to avoid. Now asserts the message names k8s_namespace, Kubernetes, "removed" and #142, and that it does NOT contain "did you mean". - test_executor.py, test_executor_comprehensive.py: "Unsupported cluster type" -> "is not a supported cluster type", which also lists the supported set. - test_backends_local.py: test_unknown_cluster_type_still_fails_loudly built a ClusterConfig with a bad type *outside* its pytest.raises, so the now-earlier rejection escaped the block. Rewritten to assert the refusal happens at construction, and joined by a second test covering the path that motivated keeping the executor's own check: setattr does not re-run __post_init__, so a type assigned after construction (what configure() and the widget both do) must still be refused at submit. Full non-billable suite: 1240 passed, 18 skipped, 0 failed. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- tests/test_config.py | 2 +- tests/test_enhanced_features.py | 16 ++++++++++++-- tests/test_executor.py | 2 +- tests/test_executor_comprehensive.py | 2 +- tests/unit/test_backends_local.py | 33 +++++++++++++++++++++++++--- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 77e96f68..526f5fe8 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -251,7 +251,7 @@ def test_removed_cluster_type_names_backend_reason_and_issue( message = str(excinfo.value) assert cluster_type in message - assert "no longer implements" in message + assert "no longer implemented" in message assert "never been verified against real hardware" in message assert f"#{issue}" in message for supported in SUPPORTED_CLUSTER_TYPES: diff --git a/tests/test_enhanced_features.py b/tests/test_enhanced_features.py index fe1afe2f..7c7659fe 100644 --- a/tests/test_enhanced_features.py +++ b/tests/test_enhanced_features.py @@ -416,10 +416,22 @@ def test_configure_function_with_new_parameters(self): assert config.package_manager == "uv" def test_configure_rejects_a_setting_from_a_removed_backend(self): - """configure() must not silently accept a field that no longer exists.""" - with pytest.raises(ValueError, match="Unknown configuration parameter"): + """configure() must not silently accept a field that no longer exists. + + The message has to name the backend and its tracking issue. It used + to come back through difflib as "did you mean ...?" pointed at an + unrelated field, which sent the reader after the wrong thing. + """ + with pytest.raises(ValueError) as excinfo: configure(k8s_namespace="production") + message = str(excinfo.value) + assert "k8s_namespace" in message + assert "Kubernetes" in message + assert "removed" in message + assert "#142" in message + assert "did you mean" not in message + class TestBackwardCompatibility: """Test backward compatibility of enhanced features.""" diff --git a/tests/test_executor.py b/tests/test_executor.py index 8bceb07e..88f40c06 100644 --- a/tests/test_executor.py +++ b/tests/test_executor.py @@ -429,7 +429,7 @@ def test_submit_job_unsupported_cluster_type(self, mock_executor): func_data = {"function": b"test", "args": b"test", "kwargs": b"test"} job_config = {"cores": 2} - with pytest.raises(ValueError, match="Unsupported cluster type"): + with pytest.raises(ValueError, match="is not a supported cluster type"): mock_executor.submit_job(func_data, job_config) diff --git a/tests/test_executor_comprehensive.py b/tests/test_executor_comprehensive.py index 9df53bcd..ab7f9a85 100644 --- a/tests/test_executor_comprehensive.py +++ b/tests/test_executor_comprehensive.py @@ -100,7 +100,7 @@ def test_job_submission_routing(self, base_config, sample_func_data): # An unroutable cluster type is refused rather than guessed at. base_config.cluster_type = "not-a-cluster" - with pytest.raises(ValueError, match="Unsupported cluster type"): + with pytest.raises(ValueError, match="is not a supported cluster type"): ClusterExecutor(base_config).submit_job(sample_func_data, {"cores": 1}) def test_result_retrieval_success(self): diff --git a/tests/unit/test_backends_local.py b/tests/unit/test_backends_local.py index eeb5e370..091cdfea 100644 --- a/tests/unit/test_backends_local.py +++ b/tests/unit/test_backends_local.py @@ -7,7 +7,7 @@ import pytest -from clustrix.config import ClusterConfig +from clustrix.config import ClusterConfig, SUPPORTED_CLUSTER_TYPES from clustrix.executor_core import ClusterExecutor from clustrix.utils import serialize_function @@ -76,6 +76,33 @@ def test_local_job_cancellation_does_not_claim_a_lie(): def test_unknown_cluster_type_still_fails_loudly(): - executor = ClusterExecutor(ClusterConfig(cluster_type="not-a-cluster")) - with pytest.raises(Exception): + """An unrunnable backend is refused at configuration time, not at submit. + + It used to reach ClusterExecutor.submit_job, which checked the type only + *after* self.connect() -- so a typo cost an SSH round trip to a host that + was never going to be used. + """ + with pytest.raises(ValueError) as excinfo: + ClusterConfig(cluster_type="not-a-cluster") + + message = str(excinfo.value) + assert "not-a-cluster" in message + for supported in SUPPORTED_CLUSTER_TYPES: + assert supported in message + + +def test_a_cluster_type_set_after_construction_is_still_refused(): + """setattr bypasses __post_init__, so the executor checks again. + + This is the path configure() and the notebook widget both take, and it + is the reason the executor keeps its own check rather than trusting that + the config was validated when it was built. + """ + config = ClusterConfig(cluster_type="slurm", cluster_host="hpc.example") + config.cluster_type = "not-a-cluster" + + executor = ClusterExecutor(config) + with pytest.raises(ValueError) as excinfo: executor.submit_job(serialize_function(add, (1, 2), {}), {"cores": 1}) + + assert "not-a-cluster" in str(excinfo.value) From cbe07fe51fc6821594b72b2848f0686e08a1a54f Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:13:08 -0400 Subject: [PATCH 41/56] Reformat with the pinned black, not whichever one is on PATH Seven files on this branch were formatted by black 25.11.0. The project pins black==26.3.1 in both pyproject.toml and setup.py, and that is what CI installs and checks against -- the two versions disagree, so a local `black --check` passed while CI would have failed the lint job. Reformatted with 26.3.1. `black --check clustrix/ tests/ scripts/` is now clean across all 223 files, and flake8 is clean. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- tests/integration/test_gpu_detection.py | 4 ++- .../validate_ssh_cluster_access.py | 4 ++- .../test_advanced_schedulers_comprehensive.py | 4 ++- tests/real_world/test_ssh_real.py | 4 +-- tests/real_world/test_visual_verification.py | 36 +++++++------------ tests/test_modern_widget_comprehensive.py | 6 ++-- tests/unit/test_result_authentication.py | 8 ++--- 7 files changed, 26 insertions(+), 40 deletions(-) diff --git a/tests/integration/test_gpu_detection.py b/tests/integration/test_gpu_detection.py index 3c053450..156fa4d3 100644 --- a/tests/integration/test_gpu_detection.py +++ b/tests/integration/test_gpu_detection.py @@ -229,4 +229,6 @@ def test_executor_integration(): if passed == total: print("\n🎉 All GPU detection tests passed!") else: - print(f"\n⚠️ {total - passed} tests failed - GPU functionality needs attention") + print( + f"\n⚠️ {total - passed} tests failed - GPU functionality needs attention" + ) diff --git a/tests/real_world/api_validation/validate_ssh_cluster_access.py b/tests/real_world/api_validation/validate_ssh_cluster_access.py index 81eaf0b6..811611dd 100644 --- a/tests/real_world/api_validation/validate_ssh_cluster_access.py +++ b/tests/real_world/api_validation/validate_ssh_cluster_access.py @@ -627,7 +627,9 @@ def main(): f" {'✅' if status == 'PASSED' else '⚠️'} {test_name}: {status} ({success_rate*100:.1f}%)" ) elif result == {}: - print(f" ⚠️ {test_name}: No schedulers detected (regular SSH server)") + print( + f" ⚠️ {test_name}: No schedulers detected (regular SSH server)" + ) else: print(f" ❌ {test_name}: FAILED") diff --git a/tests/real_world/test_advanced_schedulers_comprehensive.py b/tests/real_world/test_advanced_schedulers_comprehensive.py index e622ad1f..e96a1c0a 100644 --- a/tests/real_world/test_advanced_schedulers_comprehensive.py +++ b/tests/real_world/test_advanced_schedulers_comprehensive.py @@ -315,7 +315,9 @@ def test_scheduler_queue_systems(self): f" Queue info: {len(result.stdout.strip().split(chr(10)))} lines" ) else: - logger.warning(f"⚠️ {scheduler.upper()} queue system not accessible") + logger.warning( + f"⚠️ {scheduler.upper()} queue system not accessible" + ) except subprocess.TimeoutExpired: queue_tests.append( diff --git a/tests/real_world/test_ssh_real.py b/tests/real_world/test_ssh_real.py index a26047a2..dfffc8a3 100644 --- a/tests/real_world/test_ssh_real.py +++ b/tests/real_world/test_ssh_real.py @@ -310,9 +310,7 @@ def test_ssh_config_file_operations_real(self): HostName 127.0.0.1 User {username} Port 22 -""".format( - username=os.getenv("USER", "user") - ) +""".format(username=os.getenv("USER", "user")) config_file = temp_mgr.create_temp_file(ssh_config_content, ".config") diff --git a/tests/real_world/test_visual_verification.py b/tests/real_world/test_visual_verification.py index 9c702b01..1905c806 100644 --- a/tests/real_world/test_visual_verification.py +++ b/tests/real_world/test_visual_verification.py @@ -45,8 +45,7 @@ def test_modern_widget_html_output(self): html_file.parent.mkdir(parents=True, exist_ok=True) with open(html_file, "w") as f: - f.write( - f""" + f.write(f""" @@ -102,8 +101,7 @@ def test_modern_widget_html_output(self): -""" - ) +""") assert html_file.exists() print(f"Widget HTML saved to: {html_file}") @@ -134,8 +132,7 @@ def test_enhanced_widget_html_output(self): html_file.parent.mkdir(parents=True, exist_ok=True) with open(html_file, "w") as f: - f.write( - f""" + f.write(f""" @@ -187,8 +184,7 @@ def test_enhanced_widget_html_output(self): -""" - ) +""") assert html_file.exists() print(f"Enhanced widget HTML saved to: {html_file}") @@ -361,8 +357,7 @@ def test_widget_accessibility_features(self): accessibility_file.parent.mkdir(parents=True, exist_ok=True) with open(accessibility_file, "w") as f: - f.write( - f""" + f.write(f""" @@ -446,8 +441,7 @@ def test_widget_accessibility_features(self): -""" - ) +""") assert accessibility_file.exists() print(f"Accessibility report saved to: {accessibility_file}") @@ -487,8 +481,7 @@ def test_widget_responsive_design(self): responsive_file.parent.mkdir(parents=True, exist_ok=True) with open(responsive_file, "w") as f: - f.write( - f""" + f.write(f""" @@ -575,8 +568,7 @@ def test_widget_responsive_design(self): -""" - ) +""") assert responsive_file.exists() print(f"Responsive design report saved to: {responsive_file}") @@ -622,8 +614,7 @@ def test_widget_comparison_report(self): comparison_file.parent.mkdir(parents=True, exist_ok=True) with open(comparison_file, "w") as f: - f.write( - f""" + f.write(f""" @@ -745,8 +736,7 @@ def test_widget_comparison_report(self): -""" - ) +""") assert comparison_file.exists() print(f"Widget comparison report saved to: {comparison_file}") @@ -866,8 +856,7 @@ def test_widget_screenshot_simulation(self): index_file = Path("tests/real_world/screenshots/index.html") with open(index_file, "w") as f: - f.write( - f""" + f.write(f""" @@ -952,8 +941,7 @@ def test_widget_screenshot_simulation(self): -""" - ) +""") assert index_file.exists() print(f"Visual test index saved to: {index_file}") diff --git a/tests/test_modern_widget_comprehensive.py b/tests/test_modern_widget_comprehensive.py index 4120450c..61ea7716 100644 --- a/tests/test_modern_widget_comprehensive.py +++ b/tests/test_modern_widget_comprehensive.py @@ -704,8 +704,7 @@ def test_load_config_handler(self, mock_ipython_env, temp_profile_manager): # Create a test file first with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f: - f.write( - """ + f.write(""" active_profile: Test Profile profiles: Test Profile: @@ -713,8 +712,7 @@ def test_load_config_handler(self, mock_ipython_env, temp_profile_manager): default_cores: 4 default_memory: 8GB default_time: 01:30:00 -""" - ) +""") test_file = f.name try: diff --git a/tests/unit/test_result_authentication.py b/tests/unit/test_result_authentication.py index 35949ac8..4d327533 100644 --- a/tests/unit/test_result_authentication.py +++ b/tests/unit/test_result_authentication.py @@ -392,9 +392,7 @@ def test_single_venv_program_refuses_to_fall_back_to_stdlib_pickle(self, tmp_pat # Python 3.12, so a find_module-based blocker is simply ignored there # and the child imports dill perfectly well -- the test then passes # vacuously on <=3.11 and fails on 3.12 for the wrong reason. - (blocker / "sitecustomize.py").write_text( - textwrap.dedent( - """ + (blocker / "sitecustomize.py").write_text(textwrap.dedent(""" import sys class _Block: def find_spec(self, name, path=None, target=None): @@ -402,9 +400,7 @@ def find_spec(self, name, path=None, target=None): raise ImportError(name) return None sys.meta_path.insert(0, _Block()) - """ - ) - ) + """)) env = dict(os.environ, CLUSTRIX_RESULT_KEY=KEY, PYTHONPATH=str(blocker)) result = subprocess.run( [sys.executable, "-c", program], From 1031102c1f3066b670dcd3d8662dc739ff1770ed Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:22:10 -0400 Subject: [PATCH 42/56] Delete tests/test_config.yml, a committed test artifact It is a saved *profiles* file, not a configuration file -- `load_config` has never been able to read it: ValueError: tests/test_config.yml contains unknown setting(s): active_profile; profiles Nothing references it. Every "test_config.yml" in the suite is a name composed inside a tmpdir. It is exactly the stray that tests/conftest.py's own docstring describes as leaking out of the suite before the config directory was redirected, and it was still carrying `aws_*`, `azure_*`, `k8s_*` and `cost_monitoring` keys for backends that no longer exist. Suite after removal: 1240 passed, 18 skipped, 0 failed. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- tests/test_config.yml | 99 ------------------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 tests/test_config.yml diff --git a/tests/test_config.yml b/tests/test_config.yml deleted file mode 100644 index 0a5bfb18..00000000 --- a/tests/test_config.yml +++ /dev/null @@ -1,99 +0,0 @@ -active_profile: Local single-core -profiles: - Local single-core: - aks_cluster_name: null - api_key: null - async_submit: false - auto_gpu_packages: true - auto_gpu_parallel: true - auto_parallel: true - auto_provision_k8s: false - aws_access_key: null - aws_access_key_id: null - aws_cluster_type: null - aws_instance_type: null - aws_profile: null - aws_region: null - aws_secret_access_key: null - aws_secret_key: null - aws_session_token: null - azure_client_id: null - azure_client_secret: null - azure_instance_type: null - azure_region: null - azure_resource_group: null - azure_subscription_id: null - azure_tenant_id: null - cache_credentials: true - cleanup_on_success: true - cloud_auto_configure: false - cloud_provider: manual - cloud_region: null - cluster_host: null - cluster_packages: [] - cluster_port: 22 - cluster_type: local - conda_env_name: null - cost_monitoring: false - credential_cache_ttl: 300 - cuda_version_preference: null - default_cores: 1 - default_memory: 16GB - default_partition: null - default_queue: null - default_time: 01:00:00 - eks_cluster_name: null - environment_variables: {} - gcp_instance_type: null - gcp_project_id: null - gcp_region: null - gcp_service_account_key: null - gcp_zone: null - gke_cluster_name: null - gpu_detection_enabled: true - gpu_memory_fraction: 0.9 - gpu_requirements: null - hf_hardware: null - hf_token: null - hf_username: null - job_poll_interval: 30 - k8s_auto_cleanup: true - k8s_backoff_limit: 3 - k8s_cluster_name: null - k8s_from_scratch: true - k8s_image: python:3.11-slim - k8s_job_ttl_seconds: 3600 - k8s_namespace: default - k8s_node_count: 2 - k8s_node_type: null - k8s_provider: aws - k8s_pull_policy: IfNotPresent - k8s_region: null - k8s_remote: false - k8s_service_account: null - k8s_version: '1.28' - key_file: null - lambda_api_key: null - lambda_instance_type: null - local_cache_dir: ~/.clustrix/cache - local_parallel_threshold: 1000 - local_work_dir: null - max_gpu_parallel_jobs: 8 - max_parallel_jobs: 100 - module_loads: [] - package_manager: auto - password: null - password_env_var: '' - pre_execution_commands: [] - prefer_gpu_execution: true - prefer_local_parallel: false - python_executable: python - rapids_ecosystem: false - remote_work_dir: /tmp/clustrix - ssh_port: 22 - use_env_password: false - use_two_venv: true - username: null - venv_info: null - venv_post_install_commands: [] - venv_setup_timeout: 300 From 6a67d2af95e5d9cd39eedb86beea41f85cb97432 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:26:24 -0400 Subject: [PATCH 43/56] CHANGELOG: describe the errors the code actually raises The removal section said a removed backend raises "Unsupported cluster type" at submit time. validate_cluster_type changed both the message and the timing -- it is now refused at construction, at configure(), at load_config and in the executor before it connects. Quotes the real messages, including the typo case that must keep its did-you-mean hint. Also records the API surface that went with the backends (configure's auto_install_deps, ten @cluster parameters, the config fields, hf_sdk), and adds the two test-suite defects found in this pass: #147 and the host-key tests writing into the developer's real known_hosts. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- CHANGELOG.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7fdbd55..747d2ff5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -138,6 +138,17 @@ backends now work. - CI's flake8 step passed `--exit-zero` and could not fail; its mypy step carried `continue-on-error: true`; and it ran only `tests/unit/` — about 350 of the ~1,750 non-billable tests that exist. +- **The pre-push hook could not block a push** (#147). `scripts/run_real_world_tests.py` + discarded every `runner.run_*_tests()` return value and never called `sys.exit`, + so it exited 0 whatever happened. All four categories printed `❌ ... failed` + and the hook then printed `✅ All real-world tests passed!` and allowed the + push. Its failure message also printed only `stdout`, which was empty in every + observed case because a pytest collection error goes to `stderr` — so an + operator was told something failed and not what. +- **The host-key tests wrote into the developer's real `~/.ssh/known_hosts`**, + where 83 stale `[127.0.0.1]:` entries had accumulated. Port + reuse against a fresh server key then raised `BadHostKeyException` and failed + the reject test. The fixture now redirects `~`. ### Added @@ -167,8 +178,34 @@ Seven execution backends were implemented in full and not one of them had ever been shown to run a job end to end against real hardware. Rather than keep publishing them as if they worked, they were removed. `SUPPORTED_CLUSTER_TYPES` is now exactly `local`, `ssh`, `slurm`, `huggingface` — the four backends that -have each run a real job and returned the right answer. Anything else raises -`ValueError: Unsupported cluster type` at submit time. +have each run a real job and returned the right answer. + +Asking for one of the removed backends is refused where you can act on it, and +the message says which kind of wrong it is. A removed backend is named, with +why it went and its tracking issue; an ordinary typo still gets the +did-you-mean hint: + +``` +ClusterConfig(cluster_type="pbs") + ValueError: cluster_type='pbs' is no longer implemented. It was removed in + v0.2.0 because it had never been verified against real hardware. Its return + is tracked in issue #140. Supported types are: local, ssh, slurm, huggingface. + +load_config, file containing `k8s_namespace: compute` + ValueError: contains unknown setting(s): k8s_namespace configured + Kubernetes, which has been removed (see issue #142) + +load_config, file containing a genuine typo `cluster_hostt` + ValueError: contains unknown setting(s): cluster_hostt + (did you mean cluster_host?) +``` + +`validate_cluster_type()` is the single check, called from +`ClusterConfig.__post_init__`, `configure()`, `load_config()` and the executor. +Previously only `load_config` checked, so every other route carried a dead +backend to `ClusterExecutor.submit_job` — which tested the type *after* +`self.connect()`, spending an SSH round trip on a host that was never going to +be used, and then saying only `Unsupported cluster type: pbs`. Each removed backend has a tracking issue and is planned for a future release. The gate for restoring one is the gate the surviving four already passed: a @@ -188,7 +225,23 @@ evidence. No date is promised. The HuggingFace **Spaces** provider (`provider="huggingface"`) was removed with them. This is a different thing from `cluster_type="huggingface"`, which is HuggingFace **Jobs**: that backend is verified end to end and is fully -supported. +supported. Note that `hf_hardware` and `hf_username` sat under a comment +labelling them Spaces settings but are read by `hf_jobs.py`, so they stay; +`hf_sdk` was genuinely Spaces-only and is gone. + +Removed with the backends: + +- **`configure(auto_install_deps=...)`** — it installed cloud provider + dependencies. +- **Ten `@cluster` parameters**: `provider`, `instance_type`, `region`, + `platform`, `auto_provision`, `cluster_name`, `node_count`, `node_type`, + `kubernetes_version`, `from_scratch`. +- **Every `k8s_*`, `aws_*`, `azure_*`, `gcp_*`, `lambda_*`, `cloud_*`, + `cost_monitoring` and `hf_sdk` field** on `ClusterConfig`. + +`configure()` also now validates every keyword before applying any of them. It +used to `setattr` its way through and raise partway, so a call that *failed* +had still changed the live configuration. ### Removed — cost monitoring API (BREAKING) From 232ee21e9bdb6b67b23e729c358e460301ba1b63 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:26:45 -0400 Subject: [PATCH 44/56] Notes: record completion, the gate results, and what is left Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- notes/2026-08-19-backend-removal-session.md | 66 +++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/notes/2026-08-19-backend-removal-session.md b/notes/2026-08-19-backend-removal-session.md index dadf5b32..ac84d228 100644 --- a/notes/2026-08-19-backend-removal-session.md +++ b/notes/2026-08-19-backend-removal-session.md @@ -204,3 +204,69 @@ The four failures themselves are explained by this branch's tip not importing (`clustrix.executor_kubernetes` is gone) — pytest could not collect, so no real SSH or cloud calls were made. That does not soften #147: a tree that cannot import is exactly the case the hook exists to stop, and it waved it through. + +--- + +## COMPLETE — PR #149 opened + +The removal is done and the branch is green locally. `remove/unverified-backends` +tip carries 44 commits; PR #149 opened against master. + +### Final gate, all six checks in one cycle on the final tree + +``` +pytest tests/ -m "not real_world" --ignore=tests/real_world --ignore=tests/integration + 1240 passed, 18 skipped, 0 failed +black --check clustrix/ tests/ scripts/ 223 files unchanged (black 26.3.1) +flake8 clustrix/ tests/ scripts/ clean +mypy clustrix/ no issues in 34 source files +python scripts/check_docs_examples.py 143/143 passed (110 executed for real) +python -m sphinx -b html docs/source -W build succeeded, zero warnings +``` + +**Use the pinned black.** The project pins `black==26.3.1`; the black on PATH +here is 25.11.0 and the two disagree. Seven files were formatted by the wrong +one, so a local `black --check` passed where CI would have failed. There is a +venv with the right version at +`/blackenv/bin/black` for this session; recreate with +`python3.11 -m venv … && pip install black==26.3.1` if it is gone. + +`236 files changed, 2093 insertions(+), 63437 deletions(-)`. + +### Resolved since the checkpoint + +- **master `Tests` on `0e3490e`: success.** Master is green. +- **The nightly "Real World Tests" failure was not a code defect.** There were + two similarly-named workflows; the failing one (`real_world_tests.yml`) + referenced `GCP_CREDENTIALS` and `AZURE_CREDENTIALS`, secrets that were never + created — the repo has `GCP_JSON` and no Azure secret. All 94 runs since + 2025-08-24 failed; it never had a green run. Already deleted from master in + #139. No cost, no leak: auth failed before any provider call. + +### Filed during this pass + +- **#147** — pre-push hook could not block a push. **Fixed** in `9b90eaa`, + verified with a deliberately failing test (exit 1, real assertion text in the + body). One checklist item deliberately left open and explained in the issue + comment: an automatic test of the exit code would run the real-world suite. +- **#148** — the real-world SSH tests bypass host-key verification. + `AutoAddPolicy` at 38 sites across 28 files, which `ssh_security.py` and + `CLAUDE.md` both forbid. **Not fixed**, deliberately: the one-line change is + not behaviour-preserving (the default is `reject`), so each file needs a real + cluster run to confirm the host is trusted. A blind sweep would trade a silent + security gap for a silently broken suite. + +### Evidence posted + +Comments with direct evidence on #140-#146 (each with its own `ClusterConfig` +rejection message and a grep showing the backend is gone) and on #147. + +### Still open after this + +- PR #149 CI must go green (watching). +- The Colab tutorial verification never produced findings — it was stopped at + the suspend. Its browser scratch is in the session scratchpad under + `colab-evidence/`. Restart it against a clean checkout of pushed master; + executing cells needs a Google sign-in, which is not something to do, so + expect it to distinguish *loaded in Colab* from *executed locally*. +- Evidence comments on the remaining pre-existing open issues. From 34e179867881457b4c267ca485157784577ee032 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:39:23 -0400 Subject: [PATCH 45/56] Drop the packaging extras for backends that no longer exist `pip install clustrix[aws]` pulled boto3 and the Kubernetes client for code that is not in the package. The `kubernetes`, `aws`, `azure`, `gcp` and `cloud` extras are gone, and the cloud SDKs are out of `test` and `all` too. Nothing in `clustrix/` imports any of them: $ grep -rn "import boto3\|from kubernetes\|from azure\|from google.cloud" clustrix/ (no matches) `scripts/aws/` still uses boto3, but deliberately imports it lazily with its own message -- "boto3 is not a clustrix dependency; these AWS utilities are optional" -- so it never needed an extra either. CI's install line drops the extra with them: - pip install -e ".[dev,test,kubernetes,widget]" + pip install -e ".[dev,test,widget]" Verified by resolving the new line in a clean 3.11 venv: `pip install --dry-run -e ".[dev,test,widget]"` succeeds and the resolved set contains no boto3, kubernetes, azure-* or google-* package. Also drops "kubernetes" from the package keywords, which advertised it on PyPI, in favour of the backends that exist. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- .github/workflows/tests.yml | 4 +-- pyproject.toml | 58 +------------------------------------ setup.py | 57 ------------------------------------ 3 files changed, 3 insertions(+), 116 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5c2f95b6..6a3e8faf 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -53,7 +53,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -e ".[dev,test,kubernetes,widget]" + pip install -e ".[dev,test,widget]" # Linting is version-independent, so run it once on the primary target # rather than on every matrix combination. @@ -129,7 +129,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -e ".[dev,test,kubernetes,widget]" + pip install -e ".[dev,test,widget]" - name: Run integration tests run: | diff --git a/pyproject.toml b/pyproject.toml index 3a5d31c7..3952f0c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ classifiers = [ "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: System :: Distributed Computing", ] -keywords = ["distributed-computing", "cluster", "slurm", "kubernetes", "parallel"] +keywords = ["distributed-computing", "cluster", "slurm", "ssh", "huggingface", "parallel"] dependencies = [ "paramiko>=2.7.0", "pyyaml>=5.4.0", @@ -62,39 +62,6 @@ widget = [ "jupyter>=1.0", "ipython>=7.0", ] -kubernetes = ["kubernetes>=20.13.0"] -aws = [ - "boto3>=1.26.0", - "kubernetes>=20.13.0", -] -azure = [ - "azure-identity>=1.12.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-resource>=23.0.0,<26.0.0", - "azure-mgmt-network>=25.0.0", - "azure-mgmt-authorization>=4.0.0", - "kubernetes>=20.13.0", -] -gcp = [ - "google-cloud-container>=2.15.0", - "google-cloud-resource-manager>=1.14.0", - "google-auth>=2.15.0", - "kubernetes>=20.13.0", -] -cloud = [ - "boto3>=1.26.0", - "azure-identity>=1.12.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-resource>=23.0.0,<26.0.0", - "azure-mgmt-network>=25.0.0", - "azure-mgmt-authorization>=4.0.0", - "google-cloud-container>=2.15.0", - "google-cloud-resource-manager>=1.14.0", - "google-auth>=2.15.0", - "kubernetes>=20.13.0", -] dev = [ "pytest>=6.0", "pytest-cov>=2.0", @@ -130,17 +97,6 @@ test = [ "coverage>=6.0", "pytest-xdist>=2.0", "pytest-mock>=3.0", - "boto3>=1.26.0", - "azure-identity>=1.12.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-resource>=23.0.0,<26.0.0", - "azure-mgmt-network>=25.0.0", - "google-cloud-compute>=1.11.0", - "google-cloud-container>=2.15.0", - "google-cloud-resource-manager>=1.14.0", - "google-auth>=2.15.0", - "kubernetes>=20.13.0", ] docs = [ "sphinx>=4.0", @@ -155,18 +111,6 @@ all = [ "ipywidgets>=7.6.0", "jupyter>=1.0", "ipython>=7.0", - # Cloud provider dependencies - "kubernetes>=20.13.0", - "boto3>=1.26.0", - "azure-identity>=1.12.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-resource>=23.0.0,<26.0.0", - "azure-mgmt-network>=25.0.0", - "azure-mgmt-authorization>=4.0.0", - "google-cloud-container>=2.15.0", - "google-cloud-resource-manager>=1.14.0", - "google-auth>=2.15.0", # Development dependencies "pytest>=6.0", "pytest-cov>=2.0", diff --git a/setup.py b/setup.py index 5d696e8e..d8ab829f 100644 --- a/setup.py +++ b/setup.py @@ -48,39 +48,6 @@ "jupyter>=1.0", "ipython>=7.0", ], - "kubernetes": ["kubernetes>=20.13.0"], - "aws": [ - "boto3>=1.26.0", - "kubernetes>=20.13.0", - ], - "azure": [ - "azure-identity>=1.12.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-resource>=23.0.0,<26.0.0", - "azure-mgmt-network>=25.0.0", - "azure-mgmt-authorization>=4.0.0", - "kubernetes>=20.13.0", - ], - "gcp": [ - "google-cloud-container>=2.15.0", - "google-cloud-resource-manager>=1.14.0", - "google-auth>=2.15.0", - "kubernetes>=20.13.0", - ], - "cloud": [ - "boto3>=1.26.0", - "azure-identity>=1.12.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-resource>=23.0.0,<26.0.0", - "azure-mgmt-network>=25.0.0", - "azure-mgmt-authorization>=4.0.0", - "google-cloud-container>=2.15.0", - "google-cloud-resource-manager>=1.14.0", - "google-auth>=2.15.0", - "kubernetes>=20.13.0", - ], "dev": [ "pytest>=6.0", "pytest-cov>=2.0", @@ -111,18 +78,6 @@ "coverage>=6.0", "pytest-xdist>=2.0", # For parallel test execution "pytest-mock>=3.0", # For better mocking support - # Cloud provider dependencies for comprehensive testing - "boto3>=1.26.0", # AWS - "azure-identity>=1.12.0", # Azure auth - "azure-mgmt-compute>=30.0.0", # Azure compute - "azure-mgmt-containerservice>=20.0.0", # Azure AKS - "azure-mgmt-resource>=23.0.0,<26.0.0", # Azure resources - "azure-mgmt-network>=25.0.0", # Azure networking - "google-cloud-compute>=1.11.0", # GCP compute - "google-cloud-container>=2.15.0", # GCP GKE - "google-cloud-resource-manager>=1.14.0", # GCP resource manager - "google-auth>=2.15.0", # GCP auth - "kubernetes>=20.13.0", # Kubernetes client ], "docs": [ "sphinx>=4.0", @@ -137,18 +92,6 @@ "ipywidgets>=7.6.0", "jupyter>=1.0", "ipython>=7.0", - # Cloud provider dependencies - "kubernetes>=20.13.0", - "boto3>=1.26.0", - "azure-identity>=1.12.0", - "azure-mgmt-containerservice>=20.0.0", - "azure-mgmt-compute>=30.0.0", - "azure-mgmt-resource>=23.0.0,<26.0.0", - "azure-mgmt-network>=25.0.0", - "azure-mgmt-authorization>=4.0.0", - "google-cloud-container>=2.15.0", - "google-cloud-resource-manager>=1.14.0", - "google-auth>=2.15.0", # Development dependencies "pytest>=6.0", "pytest-cov>=2.0", From 72c9aa0a3c77ee31cc02d9b1520aba57887b9b9b Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:48:09 -0400 Subject: [PATCH 46/56] docs: make the -W sphinx build pass by resolving IPython's forward ref sphinx_autodoc_typehints evaluates annotations across a documented class's whole MRO. ClusterfyMagics subclasses IPython's Magics, which annotates `shell: InteractiveShell` behind a TYPE_CHECKING guard, so the name does not exist in IPython.core.magic at runtime and get_type_hints() cannot resolve it. That emitted a forward_reference warning, fatal under sphinx-build -W and so blocking the docs gate entirely. Bind the name instead of adding the category to suppress_warnings, which would also hide the same class of warning in clustrix's own code. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- docs/source/conf.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/source/conf.py b/docs/source/conf.py index 3cb8e582..bbf9a5ca 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -32,8 +32,23 @@ # Add theme to HTML path import sphinx_wagtail_theme + html_theme_path = [sphinx_wagtail_theme.get_html_theme_path()] +# sphinx_autodoc_typehints resolves annotations across a documented class's +# whole MRO. ``clustrix.notebook_magic.ClusterfyMagics`` subclasses IPython's +# ``Magics``, which annotates ``shell: InteractiveShell`` behind a +# ``TYPE_CHECKING`` guard -- so the name is genuinely absent from +# ``IPython.core.magic`` at runtime and ``typing.get_type_hints()`` cannot +# evaluate it. That produces a ``forward_reference`` warning, which is fatal +# under ``sphinx-build -W``. Bind the name so the reference resolves, rather +# than adding it to ``suppress_warnings`` -- suppressing the category would +# also hide the same warning if clustrix's own code ever developed one. +import IPython.core.magic +from IPython.core.interactiveshell import InteractiveShell + +IPython.core.magic.InteractiveShell = InteractiveShell + templates_path = ["_templates"] exclude_patterns = [] From da0c96274e88fe3fafbd11c0720aadee036e227b Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:48:09 -0400 Subject: [PATCH 47/56] docs: notebook cross-references were rendering as literal `:doc:`/`:ref:` text nbsphinx renders a notebook markdown cell as Markdown, not reStructuredText, so an rst role in one is never resolved. The built HTML showed, verbatim, `:doc:../ssh_setup` and `:ref:execution-model` -- eight dead cross-references across five notebooks, and meaningless text for anyone reading the same notebook in Colab or on GitHub, which is where the Open in Colab badges send them. Replaced with absolute readthedocs links, which resolve in all three contexts. Each target URL was fetched and returned 200. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- docs/source/notebooks/basic_usage.ipynb | 2 +- docs/source/notebooks/cluster_config_example.ipynb | 2 +- docs/source/notebooks/complete_api_demo.ipynb | 6 +++--- docs/source/notebooks/filesystem_tutorial.ipynb | 4 ++-- docs/source/notebooks/ssh_tutorial.ipynb | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/source/notebooks/basic_usage.ipynb b/docs/source/notebooks/basic_usage.ipynb index 07079267..da4ef57d 100644 --- a/docs/source/notebooks/basic_usage.ipynb +++ b/docs/source/notebooks/basic_usage.ipynb @@ -27,7 +27,7 @@ "- **Set `cluster_host` to something real** (a hostname clustrix can SSH\n", " to) **and the exact same `@cluster` decorator switches to the full\n", " remote pipeline** instead: serialize, connect over SSH (with host-key\n", - " verification against your `known_hosts` -- see :doc:`../ssh_setup`),\n", + " verification against your `known_hosts` -- see [SSH Setup](https://clustrix.readthedocs.io/en/latest/ssh_setup.html)),\n", " stage a signed job directory, build a matching remote environment,\n", " generate and submit a job script, poll, then verify and deserialize a\n", " signed result. The SLURM and SSH tutorials in this\n", diff --git a/docs/source/notebooks/cluster_config_example.ipynb b/docs/source/notebooks/cluster_config_example.ipynb index 9322e9f7..9b2bd4f8 100644 --- a/docs/source/notebooks/cluster_config_example.ipynb +++ b/docs/source/notebooks/cluster_config_example.ipynb @@ -183,7 +183,7 @@ "actually does -- resource resolution, local-vs-remote choice, serialization,\n", "submission, polling, HMAC-verified result download -- is the same order of\n", "operations for every backend and is documented in full, source-verified\n", - "detail in :ref:`execution-model`. :ref:`configuration` documents every field\n", + "detail in [Execution Model](https://clustrix.readthedocs.io/en/latest/execution_model.html). [Configuration](https://clustrix.readthedocs.io/en/latest/configuration.html) documents every field\n", "this widget can set and how `ClusterConfig` and `@cluster`'s own keyword\n", "arguments interact." ] diff --git a/docs/source/notebooks/complete_api_demo.ipynb b/docs/source/notebooks/complete_api_demo.ipynb index dad3fa0c..99ed326c 100644 --- a/docs/source/notebooks/complete_api_demo.ipynb +++ b/docs/source/notebooks/complete_api_demo.ipynb @@ -274,7 +274,7 @@ "\n", "The full, source-verified version of this walkthrough -- including exactly\n", "what changes per backend (SLURM/SSH/HuggingFace Jobs/local) -- is in\n", - ":ref:`execution-model`. :ref:`configuration` documents every `ClusterConfig`\n", + "[Execution Model](https://clustrix.readthedocs.io/en/latest/execution_model.html). [Configuration](https://clustrix.readthedocs.io/en/latest/configuration.html) documents every `ClusterConfig`\n", "field and how it interacts with `@cluster`'s own keyword arguments; only a\n", "fixed set of keywords actually reach job submission (see the note in the\n", "next cell) -- everything else is accepted, silently has no effect, and logs\n", @@ -1887,7 +1887,7 @@ "> `@cluster(provider=...)` cloud VM path are **not currently supported** --\n", "> they were removed in v0.2.0 because none had ever been shown to run a job\n", "> end to end, and each is planned for a future release under its own tracking\n", - "> issue. See :ref:`removed-backends`.\n" + "> issue. See [Backends removed in v0.2.0](https://clustrix.readthedocs.io/en/latest/limitations.html#removed-backends).\n" ] }, { @@ -1928,7 +1928,7 @@ "(`@cluster(provider=\"aws\"/\"gcp\"/\"azure\"/\"lambda\")`). All were removed in\n", "v0.2.0 because none had ever been shown to run a job end to end, along with\n", "the cost monitoring and cloud pricing API. Each is planned for a future\n", - "release and has a tracking issue -- see :ref:`removed-backends` for the table\n", + "release and has a tracking issue -- see [Backends removed in v0.2.0](https://clustrix.readthedocs.io/en/latest/limitations.html#removed-backends) for the table\n", "and the links.\n", "\n", "### Best Practices Covered:\n", diff --git a/docs/source/notebooks/filesystem_tutorial.ipynb b/docs/source/notebooks/filesystem_tutorial.ipynb index 769ac4db..38de1aa9 100644 --- a/docs/source/notebooks/filesystem_tutorial.ipynb +++ b/docs/source/notebooks/filesystem_tutorial.ipynb @@ -18,7 +18,7 @@ "connection to `config.cluster_host` and does the same operation over SFTP.\n", "There is no caching or batching: each call is one round trip (an SSH command\n", "or SFTP request), so a loop that calls `cluster_stat()` per file is one\n", - "network round trip per file on a remote config. See :doc:`../api/filesystem`\n", + "network round trip per file on a remote config. See [the filesystem API reference](https://clustrix.readthedocs.io/en/latest/api/filesystem.html)\n", "for the full function reference.\n", "\n", "**A real gotcha:** if a `cluster_*` call happens *inside* a function\n", @@ -26,7 +26,7 @@ "on the remote worker, using whatever filesystem is local to *that* machine\n", "-- not the machine that submitted the job. A `config` with\n", "`cluster_type=\"local\"` used inside a remotely-executing function reads the\n", - "remote worker's filesystem, not yours. See :ref:`execution-model` for the\n", + "remote worker's filesystem, not yours. See [Execution Model](https://clustrix.readthedocs.io/en/latest/execution_model.html) for the\n", "full order of operations `@cluster` follows on each call." ] }, diff --git a/docs/source/notebooks/ssh_tutorial.ipynb b/docs/source/notebooks/ssh_tutorial.ipynb index 34f7d378..e5781388 100644 --- a/docs/source/notebooks/ssh_tutorial.ipynb +++ b/docs/source/notebooks/ssh_tutorial.ipynb @@ -70,7 +70,7 @@ "> not a bug in the automated key setup below -- it happens *before* key\n", "> setup even connects. Run the `ssh-keyscan` command the error message\n", "> gives you, or already have a working `ssh your-server` from this machine.\n", - "> See :doc:`../ssh_setup`'s \"Host Key Verification\" section for the full\n", + "> See [SSH Setup](https://clustrix.readthedocs.io/en/latest/ssh_setup.html)'s \"Host Key Verification\" section for the full\n", "> explanation and the (insecure) opt-out.\n" ], "id": "cell-2" From 761c090460480b133ab6c06b3892e7756eefc993 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:49:49 -0400 Subject: [PATCH 48/56] Stop handing cloud credentials to jobs that cannot use them real-world-tests.yml exported LAMBDA_CLOUD_API_KEY, GCP_PROJECT_ID, GCP_JSON, AWS_ACCESS_KEY_ID and AWS_ACCESS_KEY into four steps. Every backend that read them is deleted, so they were doing nothing except widening the blast radius of a compromised step: a job that cannot use a credential should not be given one. What remains maps exactly to the backends that survive -- CLUSTRIX_USERNAME and CLUSTRIX_PASSWORD for ssh/slurm, HF_USERNAME and HF_TOKEN for HuggingFace Jobs -- and the check-secrets gate already keys on precisely those. Worth recording alongside this: every step in this workflow invokes `python scripts/run_real_world_tests.py --` as a bare command, and Actions fails a step on a non-zero exit. Because that script exited 0 no matter what (#147, fixed in 9b90eaa), **this workflow could not fail either** -- the same defect as the pre-push hook, in the one workflow whose entire purpose is to validate against real clusters. It can fail now. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- .github/workflows/real-world-tests.yml | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/.github/workflows/real-world-tests.yml b/.github/workflows/real-world-tests.yml index 359e6210..832993ef 100644 --- a/.github/workflows/real-world-tests.yml +++ b/.github/workflows/real-world-tests.yml @@ -105,11 +105,6 @@ jobs: - name: Run API tests (free tier) env: - LAMBDA_CLOUD_API_KEY: ${{ secrets.LAMBDA_CLOUD_API_KEY }} - GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} - GCP_JSON: ${{ secrets.GCP_JSON }} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} HF_USERNAME: ${{ secrets.HF_USERNAME }} HF_TOKEN: ${{ secrets.HF_TOKEN }} run: | @@ -122,11 +117,6 @@ jobs: - name: Run expensive tests if: ${{ inputs.run_expensive }} env: - LAMBDA_CLOUD_API_KEY: ${{ secrets.LAMBDA_CLOUD_API_KEY }} - GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} - GCP_JSON: ${{ secrets.GCP_JSON }} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} HF_USERNAME: ${{ secrets.HF_USERNAME }} HF_TOKEN: ${{ secrets.HF_TOKEN }} run: | @@ -199,11 +189,6 @@ jobs: env: CLUSTRIX_USERNAME: ${{ secrets.CLUSTRIX_USERNAME }} CLUSTRIX_PASSWORD: ${{ secrets.CLUSTRIX_PASSWORD }} - LAMBDA_CLOUD_API_KEY: ${{ secrets.LAMBDA_CLOUD_API_KEY }} - GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} - GCP_JSON: ${{ secrets.GCP_JSON }} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} HF_USERNAME: ${{ secrets.HF_USERNAME }} HF_TOKEN: ${{ secrets.HF_TOKEN }} run: | @@ -213,11 +198,6 @@ jobs: env: CLUSTRIX_USERNAME: ${{ secrets.CLUSTRIX_USERNAME }} CLUSTRIX_PASSWORD: ${{ secrets.CLUSTRIX_PASSWORD }} - LAMBDA_CLOUD_API_KEY: ${{ secrets.LAMBDA_CLOUD_API_KEY }} - GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} - GCP_JSON: ${{ secrets.GCP_JSON }} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} HF_USERNAME: ${{ secrets.HF_USERNAME }} HF_TOKEN: ${{ secrets.HF_TOKEN }} run: | From 8322cd627edd751f3950670ea73915095cafdec2 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:52:17 -0400 Subject: [PATCH 49/56] Notes: record the follow-on cleanups and the black version trap Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- notes/2026-08-19-backend-removal-session.md | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/notes/2026-08-19-backend-removal-session.md b/notes/2026-08-19-backend-removal-session.md index ac84d228..4a82dbd5 100644 --- a/notes/2026-08-19-backend-removal-session.md +++ b/notes/2026-08-19-backend-removal-session.md @@ -270,3 +270,31 @@ rejection message and a grep showing the backend is gone) and on #147. executing cells needs a Google sign-in, which is not something to do, so expect it to distinguish *loaded in Colab* from *executed locally*. - Evidence comments on the remaining pre-existing open issues. + +### Follow-on cleanups found after the PR opened + +- **Packaging extras installed SDKs for deleted backends.** `kubernetes`, + `aws`, `azure`, `gcp` and `cloud` extras, plus cloud SDKs inside `test` and + `all`. Nothing in `clustrix/` imports boto3, the kubernetes client, azure-* or + google-cloud-*; `scripts/aws/` imports boto3 lazily with its own "not a + clustrix dependency" message. CI's install line dropped `kubernetes` with + them. Verified by resolving the new line in a clean 3.11 venv. +- **`real-world-tests.yml` handed cloud credentials to jobs that cannot use + them** — LAMBDA_CLOUD_API_KEY, GCP_PROJECT_ID, GCP_JSON and two AWS keys + across four steps. Removed. +- **#147 had a second, worse consumer.** Every step in `real-world-tests.yml` + runs `python scripts/run_real_world_tests.py --` as a bare command, + so Actions would fail the step on a non-zero exit — but the script always + exited 0. That workflow is the *only* one that runs the real-world suite, so + the single thing exercising clustrix against real clusters had been reporting + green unconditionally. Fixed by the same commit; recorded on #147. +- **#150 filed**: `tests/infrastructure/docker-compose.yml` starts MinIO, + Postgres and Redis, and no test connects to any of them. `ssh-server` is + genuinely used (localhost:2222). Pre-existing, unrelated to the removal. + +### Use the pinned black + +`black==26.3.1` is pinned in pyproject.toml and setup.py; the black on PATH in +this environment is 25.11.0 and they disagree. A local `black --check` with the +wrong one passes where CI fails. Session venv: +`/blackenv/bin/black`. From 036c39c87f6faf8db03218684b7b19a27c6ef742 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:54:06 -0400 Subject: [PATCH 50/56] docs: fix stale notebook claims, add missing badges and failure guidance Verification found four classes of defect, all fixed in the notebooks: Missing Colab badges. cluster_config_example.ipynb and filesystem_tutorial.ipynb had none, while the other five did. Added, pointing at the same path the file occupies in the repo. Widget-on-import is no longer true. ssh_tutorial.ipynb and ssh_key_automation_tutorial.ipynb both told the reader to look for a widget that 'appeared automatically' when clustrix was imported. Importing only registers the %%remote magic. ssh_key_automation_tutorial went further and walked through an 'SSH Key Setup' section with its own host/user/password fields and a 'Setup SSH Keys' button -- that is the legacy EnhancedClusterConfigWidget, not the ModernClustrixWidget %%remote shows, which has an 'Auto setup SSH keys' button in its Connection section. Wrong Colab secret names. The tutorial told readers to store a Colab secret as CLUSTER_PASSWORD_CLUSTER_UNIVERSITY_EDU. get_cluster_password()'s Colab branch tries CLUSTER_PASSWORD_ first -- dots intact, not upper-cased -- so that name is never read from Colab secrets. Replaced with the actual list, in the actual order, for both the Colab and the environment-variable paths. Removed features in Next Steps. ssh_key_automation_tutorial still pointed at cloud provider integrations and cost monitoring, both deleted in v0.2.0. Also added what was missing rather than wrong: a job-directory autopsy for slurm_tutorial and ssh_tutorial (exact path, every file in it, and the three distinct failure modes), a 'When You Would Not Want This' section for basic_usage, and a step-by-step account of what setup_ssh_keys() really does. Every claim was checked against clustrix/ before it was written down. All seven notebooks re-executed; the only remaining failures are the placeholder hostnames in slurm_tutorial and ssh_tutorial, which need a real cluster. sphinx -b html -W --keep-going passes. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- docs/source/notebooks/basic_usage.ipynb | 59 ++++++++- .../notebooks/cluster_config_example.ipynb | 2 + .../notebooks/filesystem_tutorial.ipynb | 12 +- docs/source/notebooks/slurm_tutorial.ipynb | 43 ++++++ docs/source/notebooks/ssh_tutorial.ipynb | 45 ++++++- docs/ssh_key_automation_tutorial.ipynb | 124 ++++++++++++++---- 6 files changed, 253 insertions(+), 32 deletions(-) diff --git a/docs/source/notebooks/basic_usage.ipynb b/docs/source/notebooks/basic_usage.ipynb index da4ef57d..36ff5c88 100644 --- a/docs/source/notebooks/basic_usage.ipynb +++ b/docs/source/notebooks/basic_usage.ipynb @@ -408,6 +408,49 @@ ], "id": "cell-16" }, + { + "cell_type": "markdown", + "metadata": {}, + "id": "cell-16b", + "source": [ + "## When You Would *Not* Want This\n", + "\n", + "Everything above ran locally, where `@cluster` is nearly free. Point\n", + "`cluster_host` at a real machine and each call becomes a job submission,\n", + "which is worth knowing before you reach for it:\n", + "\n", + "- **Environment replication dominates the cost of the first call.** The\n", + " remote job directory gets two virtualenvs -- one to unpickle the payload,\n", + " one mirroring your local packages -- built from your local package list.\n", + " A function that runs in 200 ms is not worth shipping; one that runs for\n", + " minutes or hours is.\n", + "- **Every dependency must be installable on the cluster.** A package you\n", + " installed from a local git checkout or with `pip install -e` cannot be\n", + " reinstalled remotely, and clustrix **refuses to submit** rather than\n", + " failing after the job reaches the front of the queue. It names the\n", + " package.\n", + "- **`parallel=True` is not a promise.** It splits one `for` loop, and only\n", + " when its range is a literal `range()`, its iterations carry no\n", + " dependency on each other, and the function accepts the chunk keyword\n", + " (`_parallel_` locally; `_chunk_range_` **and** `_chunk_index`\n", + " on a cluster). Miss any one and the function runs whole -- the answer is\n", + " still right, there is simply no parallelism, and clustrix says so at\n", + " `INFO`.\n", + "- **A parallel run can return a different *shape*.** When a loop is split\n", + " on a cluster, the return value is a list of per-chunk results in chunk\n", + " order -- not the flat list the sequential version produced. Code that\n", + " indexes into the result will break. Check the shape before you trust a\n", + " speedup.\n", + "- **Source-based features need source.** Serialization itself does not\n", + " (dill and cloudpickle work from the code object), but complexity\n", + " analysis, function flattening and loop parallelization all call\n", + " `inspect.getsource`. Where it is unavailable those features are skipped\n", + " and the function ships as-is.\n", + "\n", + "The full list, with the code each claim was checked against, is on the\n", + "[Limitations page](https://clustrix.readthedocs.io/en/latest/limitations.html).\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -428,7 +471,21 @@ "> were themselves removed because none had ever been shown to run a job end to\n", "> end. See the \"Backends removed in v0.2.0\" section of the Limitations page.\n", "\n", - "Visit the [Clustrix documentation](https://clustrix.readthedocs.io) for detailed guides and API reference.\n" + "### Read Next\n", + "\n", + "- [Execution Model](https://clustrix.readthedocs.io/en/latest/execution_model.html)\n", + " -- exactly what a `@cluster` call does, step by step, and what changes per\n", + " backend\n", + "- [Limitations](https://clustrix.readthedocs.io/en/latest/limitations.html)\n", + " -- what clustrix does not do, and which backends are unsupported\n", + "- [Troubleshooting](https://clustrix.readthedocs.io/en/latest/troubleshooting.html)\n", + " -- failure modes and their exact messages\n", + "- [Configuration](https://clustrix.readthedocs.io/en/latest/configuration.html)\n", + " -- every `ClusterConfig` field and how it interacts with `@cluster`'s own\n", + " keyword arguments\n", + "\n", + "Visit the [Clustrix documentation](https://clustrix.readthedocs.io) for the\n", + "full guides and API reference.\n" ], "id": "cell-17" } diff --git a/docs/source/notebooks/cluster_config_example.ipynb b/docs/source/notebooks/cluster_config_example.ipynb index 9b2bd4f8..09b2cee5 100644 --- a/docs/source/notebooks/cluster_config_example.ipynb +++ b/docs/source/notebooks/cluster_config_example.ipynb @@ -7,6 +7,8 @@ "source": [ "# Clustrix Configuration Manager Example\n", "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/cluster_config_example.ipynb)\n", + "\n", "This notebook demonstrates how to use the `%%remote` magic command to manage cluster configurations interactively.\n", "\n", "> **What this notebook actually does.** `%%remote` (the modern name for the\n", diff --git a/docs/source/notebooks/filesystem_tutorial.ipynb b/docs/source/notebooks/filesystem_tutorial.ipynb index 38de1aa9..c85fd828 100644 --- a/docs/source/notebooks/filesystem_tutorial.ipynb +++ b/docs/source/notebooks/filesystem_tutorial.ipynb @@ -6,6 +6,8 @@ "source": [ "# Filesystem Utilities Tutorial\n", "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContextLab/clustrix/blob/master/docs/source/notebooks/filesystem_tutorial.ipynb)\n", + "\n", "This notebook demonstrates how to use Clustrix's unified filesystem utilities for seamless file operations across local and remote clusters.\n", "\n", "## Overview\n", @@ -614,7 +616,15 @@ "1. Try these operations with your own data\n", "2. Configure a remote cluster and test the same code\n", "3. Build data processing pipelines using `@cluster` with filesystem utilities\n", - "4. Explore the [API documentation](../api/filesystem.rst) for complete function references\n", + "4. Explore the\n", + " [filesystem API reference](https://clustrix.readthedocs.io/en/latest/api/filesystem.html)\n", + " for complete function references\n", + "5. Read the\n", + " [Execution Model](https://clustrix.readthedocs.io/en/latest/execution_model.html)\n", + " page to see where a `cluster_*` call runs when it sits inside a `@cluster`\n", + " function, and the\n", + " [Limitations](https://clustrix.readthedocs.io/en/latest/limitations.html)\n", + " page before relying on `parallel=True`\n", "\n", "Happy cluster computing! 🚀" ] diff --git a/docs/source/notebooks/slurm_tutorial.ipynb b/docs/source/notebooks/slurm_tutorial.ipynb index c1ab6efd..fdc8dedc 100644 --- a/docs/source/notebooks/slurm_tutorial.ipynb +++ b/docs/source/notebooks/slurm_tutorial.ipynb @@ -768,6 +768,49 @@ ], "id": "cell-19" }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## When a Job Fails: What Is in the Job Directory\n", + "\n", + "`cleanup_on_success=True` (the default) removes the remote job directory\n", + "**only** when the result came back and verified. A job that failed leaves its\n", + "directory behind on purpose, and that directory is where the answer is.\n", + "\n", + "It lives at `{remote_work_dir}/job__<8 hex chars>`, created\n", + "mode `0700`. Get its exact path from the exception message, or list the work\n", + "directory over SSH. Inside:\n", + "\n", + "| File | What it tells you |\n", + "|-|-|\n", + "| `job.sh` | The generated script, `#SBATCH` directives included. Read this first: it is the ground truth for what cores/memory/time/partition, `module_loads`, `environment_variables` and `pre_execution_commands` actually became. |\n", + "| `slurm-.out` | The job's stdout, including your function's `print()` output. |\n", + "| `slurm-.err` | The job's stderr. A Python traceback from your function lands here. |\n", + "| `function_data.pkl` | The uploaded payload -- function, args, kwargs. |\n", + "| `venv1_serialization/`, `venv2_execution/` | The two virtualenvs. If the failure was a missing or mismatched package, `venv2_execution/bin/pip freeze` says what was really installed. |\n", + "| `result.pkl` + `result.pkl.hmac` | Present only if the function returned. |\n", + "| `error.pkl` + `error.pkl.hmac` | Present if the function raised. Signed exactly like `result.pkl`. |\n", + "| `.clustrix_result_key` | The per-job HMAC key, mode `0600`. Never copy this anywhere. |\n", + "\n", + "Two failure modes look alike but are not:\n", + "\n", + "- **The job never started.** `slurm-*.out` will not exist at all. Check\n", + " `sacct -j ` -- an invalid partition, or a `--mem`/`--time` the queue\n", + " refuses, is rejected by `sbatch` before anything of yours runs.\n", + "- **The job ran and the function raised.** `slurm-*.err` has the traceback and\n", + " `error.pkl` has the exception object, which clustrix re-raises locally after\n", + " verifying its signature.\n", + "\n", + "A signature failure is a third, distinct case: clustrix refuses to unpickle a\n", + "`result.pkl` whose HMAC is missing or wrong, because unpickling runs arbitrary\n", + "code. That is not a transient error and retrying will not fix it.\n", + "\n", + "More failure modes, with their exact messages, are on the\n", + "[Troubleshooting page](https://clustrix.readthedocs.io/en/latest/troubleshooting.html).\n" + ], + "id": "cell-19b" + }, { "cell_type": "markdown", "metadata": {}, diff --git a/docs/source/notebooks/ssh_tutorial.ipynb b/docs/source/notebooks/ssh_tutorial.ipynb index e5781388..a328590f 100644 --- a/docs/source/notebooks/ssh_tutorial.ipynb +++ b/docs/source/notebooks/ssh_tutorial.ipynb @@ -90,8 +90,10 @@ "import numpy as np\n", "\n", "print(\"✅ Clustrix imported successfully!\")\n", - "print(\"📱 Look for the interactive widget that appeared above or below.\")\n", - "print(\"🔑 You can use the widget's SSH Key Setup section for easy configuration.\")" + "# Importing clustrix registers the %%remote magic; it does NOT display the\n", + "# widget. Run a `%%remote` cell to open it.\n", + "print(\"📱 Run a `%%remote` cell to open the configuration widget.\")\n", + "print(\"🔑 Its Connection section has an 'Auto setup SSH keys' button.\")" ], "id": "cell-3" }, @@ -926,6 +928,45 @@ ], "id": "cell-18" }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 🩺 When It Fails: What Is in the Job Directory\n", + "\n", + "`cleanup_on_success=True` (the default) deletes the remote job directory\n", + "**only** after a result came back and its signature verified. A failed job\n", + "leaves its directory behind deliberately -- that is where the answer is.\n", + "\n", + "The path is `{remote_work_dir}/job__<8 hex chars>`, created\n", + "mode `0700`; with the settings above that is under `~/.clustrix/jobs`.\n", + "Inside:\n", + "\n", + "| File | What it tells you |\n", + "|-|-|\n", + "| `job.sh` | The generated script. For `ssh` there are no scheduler directives -- just `cd`, environment setup, and the shared execute-and-sign body. |\n", + "| `job.out` | stdout. Clustrix runs the script as `nohup bash job.sh > job.out 2> job.err &`, so this is everything your function printed. |\n", + "| `job.err` | stderr, including a traceback from your function. |\n", + "| `function_data.pkl` | The uploaded payload -- function, args, kwargs. |\n", + "| `venv1_serialization/`, `venv2_execution/` | The two virtualenvs. `venv2_execution/bin/pip freeze` shows what was really installed when the failure is a package problem. |\n", + "| `result.pkl` + `result.pkl.hmac` | Present only if the function returned. |\n", + "| `error.pkl` + `error.pkl.hmac` | Present if it raised; signed exactly like `result.pkl`. |\n", + "| `.clustrix_result_key` | The per-job HMAC key, mode `0600`. Never copy this anywhere. |\n", + "\n", + "There is **no scheduler here**, so there is no queue to inspect and no real\n", + "job ID: clustrix invents `ssh_` purely to track the job locally.\n", + "If `job.out` and `job.err` are both empty the script never got as far as\n", + "running your function -- look at the environment-setup section of `job.sh`.\n", + "\n", + "A signature failure is a separate case from a crash: clustrix refuses to\n", + "unpickle a `result.pkl` whose HMAC is missing or wrong, because unpickling\n", + "runs arbitrary code. Retrying will not fix that.\n", + "\n", + "More failure modes, with their exact messages, are on the\n", + "[Troubleshooting page](https://clustrix.readthedocs.io/en/latest/troubleshooting.html).\n" + ], + "id": "cell-18b" + }, { "cell_type": "markdown", "metadata": {}, diff --git a/docs/ssh_key_automation_tutorial.ipynb b/docs/ssh_key_automation_tutorial.ipynb index b231d2c2..8d25e736 100644 --- a/docs/ssh_key_automation_tutorial.ipynb +++ b/docs/ssh_key_automation_tutorial.ipynb @@ -48,40 +48,107 @@ "# Install Clustrix (uncomment if not already installed)\n", "# !pip install clustrix\n", "\n", - "# Import Clustrix - the widget will appear automatically!\n", + "# Import Clustrix. This registers the %%remote magic but does NOT display the\n", + "# widget -- a library should not inject UI as a side effect of being imported.\n", + "# Run a `%%remote` cell to open it, or set CLUSTRIX_AUTO_WIDGET=1 before import\n", + "# to restore the old display-on-import behaviour.\n", "import clustrix\n", "\n", "print(\"✅ Clustrix imported successfully!\")\n", - "print(\"📱 Look for the interactive widget that appeared above or below this cell.\")\n", - "print(\"🔑 Find the 'SSH Key Setup' section in the widget interface.\")" + "print(\"📱 Run a `%%remote` cell to open the configuration widget.\")\n", + "print(\"🔑 Its Connection section has an 'Auto setup SSH keys' button.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## 🎯 Method 1: Interactive Widget (Recommended)\n", - "\n", - "**This is the easiest method!** When you imported Clustrix above, an interactive widget should have appeared. Look for the **\"SSH Key Setup\"** section.\n", - "\n", - "### 📋 Widget Steps:\n", - "1. **Enter your cluster hostname** (e.g., `cluster.university.edu`)\n", - "2. **Enter your username**\n", - "3. **Enter your password** (will be securely handled)\n", - "4. **Optional**: Check \"Force refresh SSH keys\" to generate new keys\n", - "5. **Click \"Setup SSH Keys\"**\n", - "\n", - "The widget will show real-time progress and success/error messages.\n", - "\n", - "### 💡 Colab Secret Storage (Recommended)\n", - "Instead of entering your password each time, store it securely in Colab secrets:\n", - "\n", - "1. Click the **key icon** (🔑) in the Colab sidebar\n", - "2. Add a secret with key: `CLUSTER_PASSWORD_HOSTNAME` \n", - " - Example: `CLUSTER_PASSWORD_CLUSTER_UNIVERSITY_EDU`\n", - "3. Clustrix will automatically retrieve it!\n", - "\n", - "---" + "## 🎯 Method 1: Interactive Widget\n", + "\n", + "The widget is **not** shown by importing clustrix. Open it by running a cell\n", + "whose first line is `%%remote`:\n", + "\n", + "```python\n", + "%%remote\n", + "```\n", + "\n", + "### 📋 Widget steps\n", + "\n", + "1. Set **Cluster type** to `ssh` or `slurm` -- the **Connection** section only\n", + " appears for those two.\n", + "2. Fill in **Host**, **Username** and, if it is not 22, **Port**.\n", + "3. Enter your **Password**, or name an environment variable to read it from.\n", + "4. Click **Auto setup SSH keys**. Progress and errors are reported in the\n", + " **Output** area at the bottom of the widget.\n", + "\n", + "That button calls the same `setup_ssh_keys()` this notebook drives directly\n", + "below, so the two methods do exactly the same work.\n", + "\n", + "### 💡 Storing the password instead of typing it\n", + "\n", + "`clustrix.auth_fallbacks.get_cluster_password()` supplies the password, and\n", + "only prompts you if nothing earlier in this order yields one. Names are built\n", + "from the hostname with dots replaced by underscores and the whole thing\n", + "upper-cased -- written out below for `cluster.university.edu`.\n", + "\n", + "1. **Colab secrets** (the 🔑 key icon in the sidebar), tried only when running\n", + " in Colab, in this order:\n", + " `CLUSTER_PASSWORD_cluster.university.edu` (the raw hostname, *not*\n", + " upper-cased -- this one variant is spelled differently from all the rest),\n", + " `CLUSTRIX_PASSWORD_CLUSTER_UNIVERSITY_EDU`,\n", + " `CLUSTER_UNIVERSITY_EDU_PASSWORD`, `CLUSTER_PASSWORD`\n", + "2. **Environment variables**, on every platform:\n", + " `CLUSTRIX_PASSWORD_CLUSTER_UNIVERSITY_EDU`,\n", + " `CLUSTER_PASSWORD_CLUSTER_UNIVERSITY_EDU`,\n", + " `CLUSTER_UNIVERSITY_EDU_PASSWORD`, `CLUSTRIX_DEFAULT_PASSWORD`,\n", + " `CLUSTER_PASSWORD`\n", + "3. **An interactive prompt** -- a GUI or widget popup in a notebook, `getpass`\n", + " on the command line.\n", + "\n", + "---\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 🔍 What the Automation Actually Does\n", + "\n", + "`setup_ssh_keys_with_fallback()` is a thin wrapper: it resolves a password\n", + "(see the fallback order above) and then calls `setup_ssh_keys()`, which runs\n", + "these five steps against `config.cluster_host` / `config.username` /\n", + "`config.cluster_port`.\n", + "\n", + "1. **Look for a key that already works** -- skipped entirely when\n", + " `force_refresh=True`. If one is found, `config.key_file` is pointed at it\n", + " and the function returns with `key_already_existed=True` and\n", + " `key_deployed=False`. **No password is needed on this path**, which is why\n", + " a second run of this notebook is usually instant.\n", + "2. **Generate a key** at `~/.ssh/id__clustrix__`,\n", + " or `..._` when you pass no\n", + " `cluster_alias`. It is generated **without a passphrase** -- unattended\n", + " job submission cannot answer a prompt. Treat the private key file as the\n", + " credential it is. The comment records the user, host and generation time.\n", + "3. **Deploy the public key** to the host's `authorized_keys`. This is the\n", + " only step that needs your password.\n", + "4. **Update `~/.ssh/config`** with the alias, if you passed one. A failure\n", + " here is recorded in `details['ssh_config_error']` but does not fail the\n", + " setup.\n", + "5. **Test the connection**, with retries, because some hosts take a moment to\n", + " propagate `authorized_keys`.\n", + "\n", + "The return value tells you which of those happened -- `success`, `key_path`,\n", + "`key_already_existed`, `key_deployed`, `connection_tested`, `error`,\n", + "`details`. Read them separately: `success=True` with\n", + "`connection_tested=False` means the key was deployed but logging in with it\n", + "did not work, which is the normal outcome on a Kerberos or MFA cluster.\n", + "\n", + "> **This runs before host-key verification is satisfied.** Clustrix verifies\n", + "> the remote host key against your `known_hosts` by default, so the very\n", + "> first contact with an unknown host fails with `HostKeyVerificationError` --\n", + "> ahead of any of the five steps above. Run the `ssh-keyscan` command the\n", + "> error prints, or have a working `ssh ` from this machine first. See\n", + "> [SSH Setup](https://clustrix.readthedocs.io/en/latest/ssh_setup.html).\n" ] }, { @@ -431,7 +498,8 @@ "if env == \"colab\":\n", " print(\"🔑 Colab Secret Storage Available!\")\n", " print(\" Store your cluster password in Colab secrets with key:\")\n", - " print(\" CLUSTER_PASSWORD_HOSTNAME or CLUSTER_PASSWORD\")\n", + " print(\" CLUSTER_PASSWORD_ (raw hostname, dots and all), or\")\n", + " print(\" CLUSTRIX_PASSWORD_, _PASSWORD, CLUSTER_PASSWORD\")\n", " print()\n", " \n", "print(\"🌍 Environment Variables Checked:\")\n", @@ -642,8 +710,8 @@ " \"Set up SSH keys for your actual cluster\",\n", " \"Configure Clustrix for your cluster environment\", \n", " \"Start using @cluster decorator for your computations\",\n", - " \"Explore cloud provider integrations (AWS, GCP, Azure)\",\n", - " \"Try advanced features like cost monitoring\",\n", + " \"Read the Execution Model page to see what a @cluster call actually does\",\n", + " \"Read the Limitations page before trusting parallel=True\",\n", " \"Check out filesystem utilities for data management\"\n", "]\n", "\n", From 96ad704699fe02032743d6e5c0ef939fc7cbafdb Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:56:17 -0400 Subject: [PATCH 51/56] Issue #123: bound the job wait loop _wait_for_scheduler_result polled under a bare `while True` with no deadline and no timeout field anywhere in ClusterConfig. A job that never reached a terminal state -- held by the scheduler, sitting behind a queue that never cleared, a node stuck draining -- hung the caller forever, with no diagnostic and no way out but Ctrl-C. This is on the primary path of both verified scheduler backends. New `job_wait_timeout`, default 86400 (24 hours). Deliberately generous: a real HPC queue wait legitimately runs into hours, so a short default would break correct usage. Set it to None for the old unbounded wait. On expiry the job is deliberately NOT cancelled -- it may still be queued, and killing someone's allocation because the client got bored is not this function's call. The error names the job, the elapsed limit and the setting that controls it, the last status seen, and the remote directory the result can still be collected from: TimeoutError: Job job_1 did not finish within 2s (config.job_wait_timeout). Its last known status was 'running'. The job has NOT been cancelled; its files are at /scratch/someone/.clustrix/jobs/job_1 on the cluster. Raise job_wait_timeout, or set it to None to wait indefinitely. tests/unit/test_job_wait_timeout.py covers it with no mocks: a real ClusterExecutor running the production loop, against a real subclass of the real SchedulerManager whose job never leaves the queue. Four tests -- it gives up near the deadline rather than a multiple of it, the default is finite, None really does remove the deadline (observed still polling well past a deadline that would have fired), and an unknown job id is rejected before any polling happens. 4 passed. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- clustrix/config.py | 7 ++ clustrix/executor_core.py | 24 +++++- tests/unit/test_job_wait_timeout.py | 109 ++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_job_wait_timeout.py diff --git a/clustrix/config.py b/clustrix/config.py index 28e8666a..36ede691 100644 --- a/clustrix/config.py +++ b/clustrix/config.py @@ -76,6 +76,13 @@ class ClusterConfig: max_parallel_jobs: int = 100 max_gpu_parallel_jobs: int = 8 job_poll_interval: int = 30 + # Seconds to keep polling a submitted job before giving up. Without a + # bound, a job that never reaches a terminal state -- held by the + # scheduler, stuck in a node-drain loop, a queue that never clears -- + # hangs the caller forever with no way out but Ctrl-C. 24 hours is + # deliberately generous, because a real HPC queue wait legitimately runs + # into hours; set it to None to restore the unbounded wait. + job_wait_timeout: Optional[int] = 86400 cleanup_on_success: bool = True prefer_local_parallel: bool = False local_parallel_threshold: int = 1000 # Use local if iterations < threshold diff --git a/clustrix/executor_core.py b/clustrix/executor_core.py index b6bc3e6f..9732b3b6 100644 --- a/clustrix/executor_core.py +++ b/clustrix/executor_core.py @@ -175,7 +175,14 @@ def _wait_for_scheduler_result(self, job_id: str) -> Any: remote_dir = job_info["remote_dir"] - # Poll for completion + # Poll for completion, under a deadline. An unbounded `while True` + # here meant a job that never reached a terminal state -- held by the + # scheduler, stuck behind a queue that never cleared -- hung the + # caller with no way out but Ctrl-C, and no indication of why. + timeout = getattr(self.config, "job_wait_timeout", None) + deadline = None if timeout is None else time.monotonic() + timeout + status = "unknown" + while True: status = self.scheduler_manager.check_job_status(job_id) @@ -232,6 +239,21 @@ def _wait_for_scheduler_result(self, job_id: str) -> Any: # Fallback to RuntimeError with log raise RuntimeError(f"Job {job_id} failed. Error log:\n{error_log}") + if deadline is not None and time.monotonic() >= deadline: + # The job is left alone deliberately: it may still be + # queued, and cancelling someone's allocation because the + # client got bored is not this function's decision. The + # remote directory is named so the result can be collected + # by hand. + raise TimeoutError( + f"Job {job_id} did not finish within " + f"{timeout}s (config.job_wait_timeout). Its last known " + f"status was {status!r}. The job has NOT been cancelled; " + f"its files are at {remote_dir} on the cluster. Raise " + f"job_wait_timeout, or set it to None to wait " + f"indefinitely." + ) + # Wait before next poll time.sleep(self.config.job_poll_interval) diff --git a/tests/unit/test_job_wait_timeout.py b/tests/unit/test_job_wait_timeout.py new file mode 100644 index 00000000..e510dfc3 --- /dev/null +++ b/tests/unit/test_job_wait_timeout.py @@ -0,0 +1,109 @@ +"""The scheduler wait loop must be bounded. + +``_wait_for_scheduler_result`` used to poll under a bare ``while True``. A job +that never reached a terminal state -- held by the scheduler, sitting behind a +queue that never cleared -- hung the caller forever, with no deadline, no +diagnostic and no way out but Ctrl-C. + +Nothing here is mocked. ``StuckSchedulerManager`` is a real subclass of the +real ``SchedulerManager``: it overrides one method to report the one thing a +stuck scheduler reports, which is the situation under test. The executor is a +real ``ClusterExecutor`` and the loop it runs is the production loop. +""" + +import time + +import pytest + +from clustrix.config import ClusterConfig +from clustrix.executor_core import ClusterExecutor +from clustrix.executor_schedulers import SchedulerManager + + +class StuckSchedulerManager(SchedulerManager): + """A scheduler whose job never leaves the queue.""" + + def check_job_status(self, job_id: str) -> str: + return "running" + + +def _stuck_executor(**config_kwargs) -> ClusterExecutor: + config = ClusterConfig( + cluster_type="slurm", + cluster_host="hpc.example.invalid", + username="someone", + job_poll_interval=1, + **config_kwargs, + ) + executor = ClusterExecutor(config) + executor.scheduler_manager = StuckSchedulerManager( + config, executor.connection_manager + ) + executor.scheduler_manager.active_jobs["job_1"] = { + "remote_dir": "/scratch/someone/.clustrix/jobs/job_1", + } + return executor + + +def test_a_job_that_never_finishes_raises_instead_of_hanging(): + executor = _stuck_executor(job_wait_timeout=2) + + started = time.monotonic() + with pytest.raises(TimeoutError) as excinfo: + executor._wait_for_scheduler_result("job_1") + elapsed = time.monotonic() - started + + # It gave up on its own, near the deadline rather than at some multiple + # of it, and long before pytest's own timeout would have caught it. + assert 2 <= elapsed < 10, f"gave up after {elapsed:.1f}s" + + message = str(excinfo.value) + # Everything the reader needs to act: which job, how long it waited, + # what it last saw, where the files are, and which knob to turn. + assert "job_1" in message + assert "job_wait_timeout" in message + assert "running" in message + assert "/scratch/someone/.clustrix/jobs/job_1" in message + assert "NOT been cancelled" in message + + +def test_the_default_is_finite(): + """A default of None would leave every existing caller hanging.""" + assert ClusterConfig().job_wait_timeout == 86400 + + +def test_none_restores_the_unbounded_wait(): + """The escape hatch has to actually not have a deadline. + + Verified by observing that it is still polling well past a timeout that + would have fired -- not by waiting out 24 hours. + """ + executor = _stuck_executor(job_wait_timeout=None) + + import threading + + done = threading.Event() + + def run(): + try: + executor._wait_for_scheduler_result("job_1") + except BaseException: + pass + finally: + done.set() + + thread = threading.Thread(target=run, daemon=True) + thread.start() + # Comfortably longer than the 2s deadline used above; if a deadline were + # applied here, the call would have returned by now. + assert not done.wait(timeout=5), "an unbounded wait should still be polling" + + +def test_an_unknown_job_id_is_rejected_before_any_polling(): + executor = _stuck_executor(job_wait_timeout=2) + + started = time.monotonic() + with pytest.raises(ValueError, match="Unknown job ID"): + executor._wait_for_scheduler_result("no-such-job") + + assert time.monotonic() - started < 1, "should not have polled at all" From b67a56d45a6cc1df32f275755a37d9f8c50adba8 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 11:57:26 -0400 Subject: [PATCH 52/56] Issue #124: run the documentation example checker in CI scripts/check_docs_examples.py executes every code block the documentation publishes -- 143 of them, 110 for real -- and it passes. Nothing ran it: $ grep -rn check_docs_examples .github/ (no matches) So the docs were correct only for as long as someone remembered to check by hand, and stale examples have been this project's largest recurring defect class. The step runs once on ubuntu/3.11; the examples are not version-specific, so running it seven times would only cost minutes. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- .github/workflows/tests.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6a3e8faf..bbd47375 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -113,6 +113,17 @@ jobs: clustrix --help clustrix config --help + # Executes every code block the documentation publishes (#124). The + # script existed and passed, but nothing ran it, so the docs were + # correct only for as long as someone remembered to check by hand -- + # and stale examples have been this project's largest recurring defect + # class. Run once on the primary target; the examples are not + # version-specific. + - name: Check documentation examples + if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11' + run: | + python scripts/check_docs_examples.py + integration-test: runs-on: ubuntu-latest timeout-minutes: 10 From bc2cd6a44dcf9a49ffced5b579596652b5f9d6eb Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 12:03:36 -0400 Subject: [PATCH 53/56] Issue #115: give coverage a floor that can actually fail `grep -rn fail_under` across pyproject/setup.cfg/pytest.ini/CI returned zero hits. The old `fail_under = 90` was removed because it was set against a coverage number nobody could reproduce -- correct -- but nothing replaced it, so the project measured 68% and gated on nothing. Measured on this tree, with the command CI runs: TOTAL 7180 stmts 2311 missing 68% 1244 passed, 18 skipped, 17 deselected `fail_under = 66` -- two points of headroom so a version-to-version difference across the 3.10/3.11/3.12 matrix cannot turn a green run red, while a real regression still does. A floor and a ratchet, not a target; 90 remains the goal in #98 and this is not a claim to have reached it. Verified it fires rather than being decorative, by running a single test file under the same coverage command: FAIL Required test coverage of 66.0% not reached. Total coverage: 14.40% EXIT CODE: 1 CI already passes `--cov=clustrix` on every matrix job (tests.yml:81), so this needed no workflow change to take effect. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- pyproject.toml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 3952f0c8..b2de3f71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -224,6 +224,17 @@ omit = [ ] [tool.coverage.report] +# A floor, not a target. Measured coverage on the current tree is 68% +# (7180 statements, 2311 missing); this sits two points under it so an +# ordinary version-to-version difference across the 3.10/3.11/3.12 matrix +# cannot turn a green run red, while a real regression still does. +# +# It is deliberately NOT 90. The previous `fail_under = 90` was set against +# a coverage number nobody could reproduce, so it was removed -- and then +# nothing replaced it, leaving the project measuring 68% and gating on +# nothing at all (#115). Raise this as coverage rises; do not raise it +# ahead of the tests. +fail_under = 66 exclude_lines = [ "pragma: no cover", "def __repr__", From d04edd3fa17f6177c2cacce0e176d62652711f6a Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 12:20:56 -0400 Subject: [PATCH 54/56] Fix the docs-example CI step: it ran after a non-editable install The step I added in b67a56d failed on ubuntu/3.11 with: File "scripts/check_docs_examples.py", line 695, in main rel = target.path.relative_to(REPO_ROOT) ValueError: '/opt/hostedtoolcache/.../site-packages/clustrix/config.py' is not in the subpath of '/home/runner/work/clustrix/clustrix' It sat after "Test installation", which does a non-editable `pip install .`. After that, `import clustrix` resolves to site-packages, so `inspect.getsourcefile` returned a path outside the checkout and the relative_to blew up. Two changes, because the ordering bug hid a real one: 1. The step now runs BEFORE "Test installation". That is where it belongs -- it checks the docs in this checkout against the code in this checkout. 2. The checker now refuses a foreign install by name instead of crashing inside pathlib. Getting the path arithmetic to survive would have been worse than the crash: the examples would have been silently checked against a *different copy* of the code, and passed while proving nothing. Reproduced the failure locally in a venv with a non-editable install, run from outside the repo, and confirmed the new message: documented module 'clustrix.config' imports from .../site-packages/clustrix/config.py, which is outside this checkout (/Users/jmanning/clustrix). The examples here would be checked against a different copy of the code. Install the package editable (pip install -e .) or run this before a non-editable install. Normal path unaffected: 143 blocks checked, 143 passed. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- .github/workflows/tests.yml | 24 +++++++++++++----------- scripts/check_docs_examples.py | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bbd47375..0cc81195 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -103,6 +103,19 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # Must run BEFORE "Test installation": that step does a non-editable + # `pip install .`, after which `import clustrix` resolves to + # site-packages and this checker would be reading a different copy of + # the code than the docs in this checkout describe. Executes every code + # block the documentation publishes (#124) -- the script existed and + # passed, but nothing ran it, so the docs were correct only as long as + # someone remembered to check by hand. Run once on the primary target; + # the examples are not version-specific. + - name: Check documentation examples + if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11' + run: | + python scripts/check_docs_examples.py + - name: Test installation run: | pip install . @@ -113,17 +126,6 @@ jobs: clustrix --help clustrix config --help - # Executes every code block the documentation publishes (#124). The - # script existed and passed, but nothing ran it, so the docs were - # correct only for as long as someone remembered to check by hand -- - # and stale examples have been this project's largest recurring defect - # class. Run once on the primary target; the examples are not - # version-specific. - - name: Check documentation examples - if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11' - run: | - python scripts/check_docs_examples.py - integration-test: runs-on: ubuntu-latest timeout-minutes: 10 diff --git a/scripts/check_docs_examples.py b/scripts/check_docs_examples.py index 5e63218a..e2794eea 100644 --- a/scripts/check_docs_examples.py +++ b/scripts/check_docs_examples.py @@ -560,7 +560,22 @@ def _discover_documented_modules(scan_root: Path) -> List[TargetFile]: source = inspect.getsourcefile(module) if source is None: # pragma: no cover - namespace/extension modules continue - targets.append(TargetFile(Path(source), "py", module=name)) + source_path = Path(source).resolve() + # The docs in this checkout are only meaningfully checked against the + # code in this checkout. If `clustrix` imports from somewhere else -- + # a non-editable `pip install .` earlier in the same CI job puts it in + # site-packages -- then every docstring example read here belongs to a + # different copy, and a pass would mean nothing. Say so plainly + # instead of failing 20 frames down inside `Path.relative_to`. + if not source_path.is_relative_to(REPO_ROOT): + raise SystemExit( + f"documented module {name!r} imports from {source_path}, which " + f"is outside this checkout ({REPO_ROOT}). The examples here " + f"would be checked against a different copy of the code. " + f"Install the package editable (pip install -e .) or run this " + f"before a non-editable install." + ) + targets.append(TargetFile(source_path, "py", module=name)) return targets From 4f5b16612917e9755e681c934f489ff5f64a3374 Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 12:36:36 -0400 Subject: [PATCH 55/56] Notes: PR #149 green, 15 issues closed, and the backlog's bad numbers Records which issues were closed with what evidence, the re-measured figures that contradict the backlog, and the two items that need the user. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- notes/2026-08-19-backend-removal-session.md | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/notes/2026-08-19-backend-removal-session.md b/notes/2026-08-19-backend-removal-session.md index 4a82dbd5..f4ec80c7 100644 --- a/notes/2026-08-19-backend-removal-session.md +++ b/notes/2026-08-19-backend-removal-session.md @@ -298,3 +298,59 @@ rejection message and a grep showing the backend is gone) and on #147. this environment is 25.11.0 and they disagree. A local `black --check` with the wrong one passes where CI fails. Session venv: `/blackenv/bin/black`. + +--- + +## PR #149 all green; issue backlog triaged + +All 15 CI checks pass on the branch tip. The `Check documentation examples` +step is confirmed `success`, not skipped. + +### Issues closed with evidence (15) + +#68 #70 #87 #88 #89 #90 #95 #96 #99 #106 #113 #114 #119 #120 #132 — open count +49 -> 35. Each got a comment quoting the grep, command output or file:line that +justifies the close. + +### A pattern worth knowing about: the backlog's numbers do not reproduce + +Re-measured, claimed vs actual: + +| Issue | Claimed | Actual | +|-|-|-| +| #117 | 2,513 mock occurrences | **596** (its own regex) | +| #123 | ClusterConfig ~150 fields | **59** | +| #123 | utils.py 1,788 lines | **3,109** (grew; the issue understates it) | +| #123 | 40+ silent `except: pass` | **20** | +| #99 | `clustrix/providers/aws.py` at 48% | that path has **never existed** | +| #100 | utils.py at 70% | **63%** (worse than claimed) | +| #102 | notebook modules at 50% | **67%** | +| #104 | executor.py at 71% | it is a 7-statement shim at **100%** | +| #106 | loop_analysis at 72% | **86%** — target already met | + +Do not act on a number in an issue here without re-measuring it. + +### Fixed in this pass, beyond the removal + +- **#123 the untimed wait loop** — `job_wait_timeout`, default 86400, `None` + to opt out. The job is deliberately not cancelled on expiry. +- **#124 docs examples in CI** — and the ordering bug that surfaced: the step + must run BEFORE `Test installation`'s non-editable `pip install .`, or the + checker reads clustrix from site-packages. The checker now refuses that by + name rather than crashing in pathlib. +- **#115 coverage floor** — `fail_under = 66` against a measured 68%. + +### Open, needs the user + +- **#111 secret scanning is still disabled** (`gh api ... /secret-scanning/alerts` + -> "Secret scanning is disabled on this repository. (HTTP 404)"). A repo + settings change; not made without asking. Also unverifiable from here: + whether the two HF tokens were actually rotated. +- **#113's one unverified item**: whether branch protection *requires* the test + job. `gh api repos/ContextLab/clustrix/branches/master/protection` settles it. + +### Still open and genuinely worth doing + +#117 (de-mocking; 3 named offenders survive verbatim), #122 (three modules +still orphaned, `enhanced_notebook_widget.py` at 0% coverage), #148 (host-key +verification in the real-world tests), #127 (tag and release v0.2.0). From c9fe5c9d90a4d93c1f4e6cab82ddde61b8c44aee Mon Sep 17 00:00:00 2001 From: Jeremy Manning Date: Wed, 19 Aug 2026 13:30:40 -0400 Subject: [PATCH 56/56] Add a Tests Status gate so branch protection has a stable name to require `master` had no branch protection at all -- `gh api repos/ContextLab/clustrix/branches/master/protection` returned "Branch not protected" -- so every green run this project has fixed was advisory. A red run could always be merged past. Protection needs required check names. The Tests workflow had none that is stable: pinning it to the seven `test (os, version)` jobs individually means protection silently stops covering any combination added later, and breaks whenever the matrix changes. Fast CI already solved this with its `CI Status` aggregator; this is the same pattern for the workflow that runs the actual test suite. `if: always()` matters: without it the job would be *skipped* when a dependency fails, and a skipped required check does not block a merge -- the gate would be worse than none. The condition names every job in `needs` explicitly, because the version of this in fast_ci.yml had security-scan in `needs` but not in its loop, and so reported "All CI checks passed" while the security scan burned. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012gTBDPK16HUZ3kHQ2QyjuU --- .github/workflows/tests.yml | 41 ++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0cc81195..bff7fa25 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -217,4 +217,43 @@ jobs: run: | pip install jupyter nbconvert jupyter nbconvert --to script docs/source/notebooks/basic_usage.ipynb - # Note: Full notebook execution would require cluster setup \ No newline at end of file + # Note: Full notebook execution would require cluster setup + # A single stable name for branch protection to require. Without it, + # protection has to list all seven `test (os, version)` jobs individually + # and silently stops covering any combination added later -- and `master` + # had no protection at all, so a red run could always be merged past. + # + # `if: always()` so this job still runs when a dependency fails; without it + # the gate would be skipped rather than failing, and a skipped required + # check does not block a merge. + tests-status: + name: Tests Status + runs-on: ubuntu-latest + needs: [test, integration-test, docs-test] + if: always() + steps: + - name: Check status + run: | + # Every job in `needs` must be checked by name. An aggregator that + # forgets one reports success while that one burns -- the exact + # defect fixed in fast_ci.yml's status-check, which had + # security-scan in `needs` but not in its condition. + failed=0 + for job in test integration-test docs-test; do + case "$job" in + test) result="${{ needs.test.result }}" ;; + integration-test) result="${{ needs.integration-test.result }}" ;; + docs-test) result="${{ needs.docs-test.result }}" ;; + esac + if [ "$result" != "success" ]; then + echo "::error::$job: $result" + failed=1 + else + echo "$job: success" + fi + done + if [ "$failed" -ne 0 ]; then + echo "One or more test jobs did not succeed." + exit 1 + fi + echo "All test jobs passed."