|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Generic, TypeVar |
| 4 | + |
| 5 | +import requests |
| 6 | +from pydantic import BaseModel, ValidationError |
| 7 | + |
| 8 | +from dstack._internal.core.errors import ServerClientError |
| 9 | +from dstack._internal.core.models.fleets import FleetSpec |
| 10 | +from dstack._internal.core.models.gateways import GatewaySpec |
| 11 | +from dstack._internal.core.models.volumes import VolumeSpec |
| 12 | +from dstack.plugins import ApplyPolicy, Plugin, RunSpec, get_plugin_logger |
| 13 | +from dstack.plugins._models import ApplySpec |
| 14 | + |
| 15 | +logger = get_plugin_logger(__name__) |
| 16 | + |
| 17 | +PLUGIN_SERVICE_URI_ENV_VAR_NAME = "DSTACK_PLUGIN_SERVICE_URI" |
| 18 | +PLUGIN_REQUEST_TIMEOUT = 8 # in seconds |
| 19 | + |
| 20 | +SpecType = TypeVar("SpecType", RunSpec, FleetSpec, VolumeSpec, GatewaySpec) |
| 21 | + |
| 22 | + |
| 23 | +class SpecRequest(BaseModel, Generic[SpecType]): |
| 24 | + user: str |
| 25 | + project: str |
| 26 | + spec: SpecType |
| 27 | + |
| 28 | + |
| 29 | +RunSpecRequest = SpecRequest[RunSpec] |
| 30 | +FleetSpecRequest = SpecRequest[FleetSpec] |
| 31 | +VolumeSpecRequest = SpecRequest[VolumeSpec] |
| 32 | +GatewaySpecRequest = SpecRequest[GatewaySpec] |
| 33 | + |
| 34 | + |
| 35 | +class CustomApplyPolicy(ApplyPolicy): |
| 36 | + def __init__(self): |
| 37 | + self._plugin_service_uri = os.getenv(PLUGIN_SERVICE_URI_ENV_VAR_NAME) |
| 38 | + logger.info(f"Found plugin service at {self._plugin_service_uri}") |
| 39 | + if not self._plugin_service_uri: |
| 40 | + logger.error( |
| 41 | + f"Cannot create policy because {PLUGIN_SERVICE_URI_ENV_VAR_NAME} is not set" |
| 42 | + ) |
| 43 | + raise ServerClientError(f"{PLUGIN_SERVICE_URI_ENV_VAR_NAME} is not set") |
| 44 | + |
| 45 | + def _call_plugin_service(self, spec_request: SpecRequest, endpoint: str) -> ApplySpec: |
| 46 | + response = None |
| 47 | + try: |
| 48 | + response = requests.post( |
| 49 | + f"{self._plugin_service_uri}{endpoint}", |
| 50 | + json=spec_request.dict(), |
| 51 | + headers={"accept": "application/json", "Content-Type": "application/json"}, |
| 52 | + timeout=PLUGIN_REQUEST_TIMEOUT, |
| 53 | + ) |
| 54 | + response.raise_for_status() |
| 55 | + spec_json = json.loads(response.text) |
| 56 | + return spec_json |
| 57 | + except requests.exceptions.ConnectionError as e: |
| 58 | + logger.error( |
| 59 | + f"Could not connect to plugin service at {self._plugin_service_uri}: %s", e |
| 60 | + ) |
| 61 | + raise e |
| 62 | + except requests.RequestException as e: |
| 63 | + logger.error("Request to the plugin service failed: %s", e) |
| 64 | + if response: |
| 65 | + logger.error(f"Error response from plugin service:\n{response.text}") |
| 66 | + raise e |
| 67 | + except ValidationError as e: |
| 68 | + # Received 200 code but response body is invalid |
| 69 | + logger.exception( |
| 70 | + f"Plugin service returned invalid response:\n{response.text if response else None}" |
| 71 | + ) |
| 72 | + raise e |
| 73 | + |
| 74 | + def on_run_apply(self, user: str, project: str, spec: RunSpec) -> RunSpec: |
| 75 | + spec_request = RunSpecRequest(user=user, project=project, spec=spec) |
| 76 | + spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_run_apply") |
| 77 | + return RunSpec(**spec_json) |
| 78 | + |
| 79 | + def on_fleet_apply(self, user: str, project: str, spec: FleetSpec) -> FleetSpec: |
| 80 | + spec_request = FleetSpecRequest(user=user, project=project, spec=spec) |
| 81 | + spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_fleet_apply") |
| 82 | + return FleetSpec(**spec_json) |
| 83 | + |
| 84 | + def on_volume_apply(self, user: str, project: str, spec: VolumeSpec) -> VolumeSpec: |
| 85 | + spec_request = VolumeSpecRequest(user=user, project=project, spec=spec) |
| 86 | + spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_volume_apply") |
| 87 | + return VolumeSpec(**spec_json) |
| 88 | + |
| 89 | + def on_gateway_apply(self, user: str, project: str, spec: GatewaySpec) -> GatewaySpec: |
| 90 | + spec_request = GatewaySpecRequest(user=user, project=project, spec=spec) |
| 91 | + spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_gateway_apply") |
| 92 | + return GatewaySpec(**spec_json) |
| 93 | + |
| 94 | + |
| 95 | +class RESTPlugin(Plugin): |
| 96 | + def get_apply_policies(self) -> list[ApplyPolicy]: |
| 97 | + return [CustomApplyPolicy()] |
0 commit comments