|
| 1 | +""" |
| 2 | +Base deployment configuration classes. |
| 3 | +
|
| 4 | +Provides common configuration structures and validation for all deployment providers. |
| 5 | +""" |
| 6 | + |
| 7 | +from dataclasses import dataclass, field |
| 8 | +from typing import Dict, Any, Optional, List |
| 9 | +from enum import Enum |
| 10 | + |
| 11 | + |
| 12 | +class DeploymentProvider(str, Enum): |
| 13 | + """Supported deployment providers.""" |
| 14 | + |
| 15 | + GCP = "gcp" |
| 16 | + AWS = "aws" |
| 17 | + AZURE = "azure" |
| 18 | + DIGITALOCEAN = "digitalocean" |
| 19 | + LOCAL = "local" |
| 20 | + |
| 21 | + |
| 22 | +class DeploymentConfigError(Exception): |
| 23 | + """Raised when deployment configuration is invalid.""" |
| 24 | + |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class ServiceConfig: |
| 30 | + """Common service configuration shared across all providers.""" |
| 31 | + |
| 32 | + name: str |
| 33 | + image: str |
| 34 | + port: int = 8080 |
| 35 | + memory: str = "4Gi" # Updated: Required for NLP matching operations |
| 36 | + cpu: int = 2 |
| 37 | + min_instances: int = 1 |
| 38 | + max_instances: int = 100 |
| 39 | + timeout: int = 300 # 5 minutes for long-running matching operations |
| 40 | + environment_vars: Dict[str, str] = field(default_factory=dict) |
| 41 | + secrets: Dict[str, str] = field(default_factory=dict) |
| 42 | + labels: Dict[str, str] = field(default_factory=dict) |
| 43 | + |
| 44 | + def validate(self) -> None: |
| 45 | + """Validate service configuration.""" |
| 46 | + if not self.name: |
| 47 | + raise DeploymentConfigError("Service name is required") |
| 48 | + if not self.image: |
| 49 | + raise DeploymentConfigError("Service image is required") |
| 50 | + if self.port < 1 or self.port > 65535: |
| 51 | + raise DeploymentConfigError(f"Invalid port: {self.port}") |
| 52 | + if self.cpu < 1: |
| 53 | + raise DeploymentConfigError(f"CPU must be at least 1, got {self.cpu}") |
| 54 | + if self.min_instances < 0: |
| 55 | + raise DeploymentConfigError( |
| 56 | + f"min_instances must be >= 0, got {self.min_instances}" |
| 57 | + ) |
| 58 | + if self.max_instances < self.min_instances: |
| 59 | + raise DeploymentConfigError( |
| 60 | + f"max_instances ({self.max_instances}) must be >= min_instances ({self.min_instances})" |
| 61 | + ) |
| 62 | + if self.timeout < 1: |
| 63 | + raise DeploymentConfigError(f"Timeout must be at least 1 second, got {self.timeout}") |
| 64 | + |
| 65 | + |
| 66 | +@dataclass |
| 67 | +class BaseDeploymentConfig: |
| 68 | + """Base deployment configuration for all providers.""" |
| 69 | + |
| 70 | + provider: DeploymentProvider |
| 71 | + environment: str = "production" |
| 72 | + region: Optional[str] = None # Provider-specific format, no default |
| 73 | + service: ServiceConfig = field(default_factory=lambda: ServiceConfig( |
| 74 | + name="supply-graph-ai", |
| 75 | + image="ghcr.io/helpfulengineering/supply-graph-ai:latest" |
| 76 | + )) |
| 77 | + provider_config: Dict[str, Any] = field(default_factory=dict) |
| 78 | + |
| 79 | + def validate(self) -> None: |
| 80 | + """Validate deployment configuration.""" |
| 81 | + if not self.provider: |
| 82 | + raise DeploymentConfigError("Provider is required") |
| 83 | + # Region validation is provider-specific, so we don't validate format here |
| 84 | + # Provider-specific deployers should validate region format |
| 85 | + if not self.environment: |
| 86 | + raise DeploymentConfigError("Environment is required") |
| 87 | + self.service.validate() |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def from_dict(cls, data: Dict[str, Any]) -> "BaseDeploymentConfig": |
| 91 | + """Create configuration from dictionary.""" |
| 92 | + provider_str = data.get("provider", "gcp") |
| 93 | + try: |
| 94 | + provider = DeploymentProvider(provider_str.lower()) |
| 95 | + except ValueError: |
| 96 | + raise DeploymentConfigError(f"Unsupported provider: {provider_str}") |
| 97 | + |
| 98 | + # Parse service config |
| 99 | + service_data = data.get("service", {}) |
| 100 | + service = ServiceConfig( |
| 101 | + name=service_data.get("name", "supply-graph-ai"), |
| 102 | + image=service_data.get("image", "ghcr.io/helpfulengineering/supply-graph-ai:latest"), |
| 103 | + port=service_data.get("port", 8080), |
| 104 | + memory=service_data.get("memory", "4Gi"), |
| 105 | + cpu=service_data.get("cpu", 2), |
| 106 | + min_instances=service_data.get("min_instances", 1), |
| 107 | + max_instances=service_data.get("max_instances", 100), |
| 108 | + timeout=service_data.get("timeout", 300), |
| 109 | + environment_vars=service_data.get("environment_vars", {}), |
| 110 | + secrets=service_data.get("secrets", {}), |
| 111 | + labels=service_data.get("labels", {}), |
| 112 | + ) |
| 113 | + |
| 114 | + # Region should be provided in config or set by provider-specific config |
| 115 | + # No default region to avoid provider-specific assumptions |
| 116 | + config = cls( |
| 117 | + provider=provider, |
| 118 | + environment=data.get("environment", "production"), |
| 119 | + region=data.get("region"), # No default - must be specified |
| 120 | + service=service, |
| 121 | + provider_config=data.get("providers", {}).get(provider_str, {}), |
| 122 | + ) |
| 123 | + |
| 124 | + config.validate() |
| 125 | + return config |
| 126 | + |
| 127 | + def to_dict(self) -> Dict[str, Any]: |
| 128 | + """Convert configuration to dictionary.""" |
| 129 | + return { |
| 130 | + "provider": self.provider.value, |
| 131 | + "environment": self.environment, |
| 132 | + "region": self.region, |
| 133 | + "service": { |
| 134 | + "name": self.service.name, |
| 135 | + "image": self.service.image, |
| 136 | + "port": self.service.port, |
| 137 | + "memory": self.service.memory, |
| 138 | + "cpu": self.service.cpu, |
| 139 | + "min_instances": self.service.min_instances, |
| 140 | + "max_instances": self.service.max_instances, |
| 141 | + "timeout": self.service.timeout, |
| 142 | + "environment_vars": self.service.environment_vars, |
| 143 | + "secrets": self.service.secrets, |
| 144 | + "labels": self.service.labels, |
| 145 | + }, |
| 146 | + "providers": { |
| 147 | + self.provider.value: self.provider_config, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
0 commit comments