|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +import re |
| 5 | +from typing import TYPE_CHECKING, Dict, List, Optional, Union |
| 6 | + |
| 7 | +from ....core.base import ResourceList |
| 8 | +from ....core.handler import _APIOperationExecutor |
| 9 | +from ....http_client import APIHttpClient |
| 10 | +from .operations import ( |
| 11 | + _DEPLOY_OP, |
| 12 | + _DEPLOY_WITH_PROFILE_OP, |
| 13 | + _SCALE_OP, |
| 14 | + _TEARDOWN_OP, |
| 15 | +) |
| 16 | +from .schemas import Profile, ProfileConfig |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from ..schemas import CommandOutput |
| 20 | + |
| 21 | +log = logging.getLogger(__name__) |
| 22 | + |
| 23 | +# Regex pattern to match ci.<profile>.yml files |
| 24 | +_PROFILE_FILE_PATTERN = re.compile(r"^ci\.([A-Za-z0-9_-]+)\.yml$") |
| 25 | +# Pattern for valid profile names |
| 26 | +_VALID_PROFILE_NAME = re.compile(r"^[A-Za-z0-9_-]+$") |
| 27 | + |
| 28 | + |
| 29 | +def _validate_profile_name(name: str) -> None: |
| 30 | + if not _VALID_PROFILE_NAME.match(name): |
| 31 | + raise ValueError( |
| 32 | + f"Invalid profile name '{name}'. Must match pattern ^[A-Za-z0-9_-]+$" |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def _profile_filename(name: str) -> str: |
| 37 | + _validate_profile_name(name) |
| 38 | + return f"ci.{name}.yml" |
| 39 | + |
| 40 | + |
| 41 | +class WorkspaceLandscapeManager(_APIOperationExecutor): |
| 42 | + def __init__(self, http_client: APIHttpClient, workspace_id: int): |
| 43 | + self._http_client = http_client |
| 44 | + self._workspace_id = workspace_id |
| 45 | + self.id = workspace_id |
| 46 | + |
| 47 | + async def _run_command(self, command: str) -> "CommandOutput": |
| 48 | + from ..operations import _EXECUTE_COMMAND_OP |
| 49 | + from ..schemas import CommandInput |
| 50 | + |
| 51 | + return await self._execute_operation( |
| 52 | + _EXECUTE_COMMAND_OP, data=CommandInput(command=command) |
| 53 | + ) |
| 54 | + |
| 55 | + async def list_profiles(self) -> ResourceList[Profile]: |
| 56 | + result = await self._run_command("ls -1 *.yml 2>/dev/null || true") |
| 57 | + |
| 58 | + profiles: List[Profile] = [] |
| 59 | + if result.output: |
| 60 | + for line in result.output.strip().split("\n"): |
| 61 | + if match := _PROFILE_FILE_PATTERN.match(line.strip()): |
| 62 | + profiles.append(Profile(name=match.group(1))) |
| 63 | + |
| 64 | + return ResourceList[Profile](root=profiles) |
| 65 | + |
| 66 | + async def save_profile(self, name: str, config: Union[ProfileConfig, str]) -> None: |
| 67 | + filename = _profile_filename(name) |
| 68 | + |
| 69 | + if isinstance(config, ProfileConfig): |
| 70 | + yaml_content = config.to_yaml() |
| 71 | + else: |
| 72 | + yaml_content = config |
| 73 | + |
| 74 | + body = yaml_content if yaml_content.endswith("\n") else yaml_content + "\n" |
| 75 | + await self._run_command( |
| 76 | + f"cat > {filename} << 'PROFILE_EOF'\n{body}PROFILE_EOF\n" |
| 77 | + ) |
| 78 | + |
| 79 | + async def get_profile(self, name: str) -> str: |
| 80 | + result = await self._run_command(f"cat {_profile_filename(name)}") |
| 81 | + return result.output |
| 82 | + |
| 83 | + async def delete_profile(self, name: str) -> None: |
| 84 | + await self._run_command(f"rm -f {_profile_filename(name)}") |
| 85 | + |
| 86 | + async def deploy(self, profile: Optional[str] = None) -> None: |
| 87 | + if profile is not None: |
| 88 | + _validate_profile_name(profile) |
| 89 | + await self._execute_operation(_DEPLOY_WITH_PROFILE_OP, profile=profile) |
| 90 | + else: |
| 91 | + await self._execute_operation(_DEPLOY_OP) |
| 92 | + |
| 93 | + async def teardown(self) -> None: |
| 94 | + await self._execute_operation(_TEARDOWN_OP) |
| 95 | + |
| 96 | + async def scale(self, services: Dict[str, int]) -> None: |
| 97 | + await self._execute_operation(_SCALE_OP, data=services) |
0 commit comments