-
Notifications
You must be signed in to change notification settings - Fork 222
Add CloudRift backend #2771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+560
−0
Merged
Add CloudRift backend #2771
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bd1adc8
WIP
Slonegg ccf8df5
added instance renting logic
6erun d5dbaac
pass custom commands
6erun 6a47535
Merge branch 'master' into dtrifonov/integrate-cloudrift
6erun f1507f0
doc
6erun c91a183
updated version
6erun 49dcd78
Cloudrift backend
6erun 0a17cf9
Merge branch 'dstackai:master' into master
6erun 45f9f6e
tests
6erun 33eabc2
test fix
6erun d6559dc
Merge branch 'dstackai:master' into master
6erun 7701bf1
Merge pull request #2 from cloudrift-ai/dtrifonov/integrate-cloudrift
6erun b4eeb7e
PR feedback
6erun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
208 changes: 208 additions & 0 deletions
208
src/dstack/_internal/core/backends/cloudrift/api_client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| import os | ||
| import re | ||
| from typing import Any, Dict, List, Mapping, Optional, Union | ||
|
|
||
| import requests | ||
| from packaging import version | ||
| from requests import Response | ||
|
|
||
| from dstack._internal.core.errors import BackendError, BackendInvalidCredentialsError | ||
| from dstack._internal.utils.logging import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| CLOUDRIFT_SERVER_ADDRESS = "https://api.cloudrift.ai" | ||
| CLOUDRIFT_API_VERSION = "2025-05-29" | ||
|
|
||
|
|
||
| class RiftClient: | ||
| def __init__(self, api_key: Optional[str] = None): | ||
| self.public_api_root = os.path.join(CLOUDRIFT_SERVER_ADDRESS, "api/v1") | ||
| self.api_key = api_key | ||
|
|
||
| def validate_api_key(self) -> bool: | ||
| """ | ||
| Validates the API key by making a request to the server. | ||
| Returns True if the API key is valid, False otherwise. | ||
| """ | ||
| try: | ||
| response = self._make_request("auth/me") | ||
| if isinstance(response, dict): | ||
| return "email" in response | ||
| return False | ||
| except BackendInvalidCredentialsError: | ||
| return False | ||
| except Exception as e: | ||
| logger.error(f"Error validating API key: {e}") | ||
| return False | ||
|
|
||
| def get_instance_types(self) -> List[Dict]: | ||
| request_data = {"selector": {"ByServiceAndLocation": {"services": ["vm"]}}} | ||
| response_data = self._make_request("instance-types/list", request_data) | ||
| if isinstance(response_data, dict): | ||
| return response_data.get("instance_types", []) | ||
| return [] | ||
|
|
||
| def list_recipes(self) -> List[Dict]: | ||
| request_data = {} | ||
| response_data = self._make_request("recipes/list", request_data) | ||
| if isinstance(response_data, dict): | ||
| return response_data.get("groups", []) | ||
| return [] | ||
|
|
||
| def get_vm_recipies(self) -> List[Dict]: | ||
| """ | ||
| Retrieves a list of VM recipes from the CloudRift API. | ||
| Returns a list of dictionaries containing recipe information. | ||
| """ | ||
| recipe_group = self.list_recipes() | ||
| vm_recipes = [] | ||
| for group in recipe_group: | ||
| tags = group.get("tags", []) | ||
| has_vm = "vm" in map(str.lower, tags) | ||
| if group.get("name", "").lower() != "linux" or not has_vm: | ||
| continue | ||
|
|
||
| recipes = group.get("recipes", []) | ||
| for recipe in recipes: | ||
| details = recipe.get("details", {}) | ||
| if details.get("VirtualMachine", False): | ||
| vm_recipes.append(recipe) | ||
|
|
||
| return vm_recipes | ||
|
|
||
| def get_vm_image_url(self) -> Optional[str]: | ||
| recipes = self.get_vm_recipies() | ||
| ubuntu_images = [] | ||
| for recipe in recipes: | ||
| has_nvidia_driver = "nvidia-driver" in recipe.get("tags", []) | ||
| if not has_nvidia_driver: | ||
| continue | ||
|
|
||
| recipe_name = recipe.get("name", "") | ||
| if "Ubuntu" not in recipe_name: | ||
| continue | ||
|
|
||
| url = recipe["details"].get("VirtualMachine", {}).get("image_url", None) | ||
| version_match = re.search(r".* (\d+\.\d+)", recipe_name) | ||
| if url and version_match and version_match.group(1): | ||
| ubuntu_version = version.parse(version_match.group(1)) | ||
| ubuntu_images.append((ubuntu_version, url)) | ||
|
|
||
| ubuntu_images.sort(key=lambda x: x[0]) # Sort by version | ||
| if ubuntu_images: | ||
| return ubuntu_images[-1][1] | ||
|
|
||
| return None | ||
|
|
||
| def deploy_instance( | ||
| self, instance_type: str, region: str, ssh_keys: List[str], cmd: str | ||
| ) -> List[str]: | ||
| image_url = self.get_vm_image_url() | ||
| if not image_url: | ||
| raise BackendError("No suitable VM image found.") | ||
|
|
||
| request_data = { | ||
| "config": { | ||
| "VirtualMachine": { | ||
| "cloudinit_commands": cmd, | ||
| "image_url": image_url, | ||
| "ssh_key": {"PublicKeys": ssh_keys}, | ||
| } | ||
| }, | ||
| "selector": { | ||
| "ByInstanceTypeAndLocation": { | ||
| "datacenters": [region], | ||
| "instance_type": instance_type, | ||
| } | ||
| }, | ||
| "with_public_ip": True, | ||
| } | ||
| logger.debug("Deploying instance with request data: %s", request_data) | ||
|
|
||
| response_data = self._make_request("instances/rent", request_data) | ||
| if isinstance(response_data, dict): | ||
| return response_data.get("instance_ids", []) | ||
| return [] | ||
|
|
||
| def list_instances(self, instance_ids: Optional[List[str]] = None) -> List[Dict]: | ||
| request_data = { | ||
| "selector": { | ||
| "ByStatus": ["Initializing", "Active", "Deactivating"], | ||
| } | ||
| } | ||
| logger.debug("Listing instances with request data: %s", request_data) | ||
| response_data = self._make_request("instances/list", request_data) | ||
| if isinstance(response_data, dict): | ||
| return response_data.get("instances", []) | ||
|
|
||
| return [] | ||
|
|
||
| def get_instance_by_id(self, instance_id: str) -> Optional[Dict]: | ||
| request_data = {"selector": {"ById": [instance_id]}} | ||
| logger.debug("Getting instance with request data: %s", request_data) | ||
| response_data = self._make_request("instances/list", request_data) | ||
| if isinstance(response_data, dict): | ||
| instances = response_data.get("instances", []) | ||
| if isinstance(instances, list) and len(instances) > 0: | ||
| return instances[0] | ||
|
|
||
| return None | ||
|
|
||
| def terminate_instance(self, instance_id: str) -> bool: | ||
| request_data = {"selector": {"ById": [instance_id]}} | ||
| logger.debug("Terminating instance with request data: %s", request_data) | ||
| response_data = self._make_request("instances/terminate", request_data) | ||
| if isinstance(response_data, dict): | ||
| info = response_data.get("terminated", []) | ||
| return len(info) > 0 | ||
|
|
||
| return False | ||
|
|
||
| def _make_request( | ||
| self, | ||
| endpoint: str, | ||
| data: Optional[Mapping[str, Any]] = None, | ||
| method: str = "POST", | ||
| **kwargs, | ||
| ) -> Union[Mapping[str, Any], str, Response]: | ||
| headers = {} | ||
| if self.api_key is not None: | ||
| headers["X-API-Key"] = self.api_key | ||
|
|
||
| version = CLOUDRIFT_API_VERSION | ||
| full_url = f"{self.public_api_root}/{endpoint}" | ||
|
|
||
| try: | ||
| response = requests.request( | ||
| method, | ||
| full_url, | ||
| headers=headers, | ||
| json={"version": version, "data": data}, | ||
| timeout=15, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| if not response.ok: | ||
| response.raise_for_status() | ||
| try: | ||
| response_json = response.json() | ||
| if isinstance(response_json, str): | ||
| return response_json | ||
| if version is not None and version < response_json["version"]: | ||
| logger.warning( | ||
| "The API version %s is lower than the server version %s. ", | ||
| version, | ||
| response_json["version"], | ||
| ) | ||
| return response_json["data"] | ||
| except requests.exceptions.JSONDecodeError: | ||
| return response | ||
| except requests.HTTPError as e: | ||
| if e.response is not None and e.response.status_code in ( | ||
| requests.codes.forbidden, | ||
| requests.codes.unauthorized, | ||
| ): | ||
| raise BackendInvalidCredentialsError(e.response.text) | ||
| raise | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from dstack._internal.core.backends.base.backend import Backend | ||
| from dstack._internal.core.backends.cloudrift.compute import CloudRiftCompute | ||
| from dstack._internal.core.backends.cloudrift.models import CloudRiftConfig | ||
| from dstack._internal.core.models.backends.base import BackendType | ||
|
|
||
|
|
||
| class CloudRiftBackend(Backend): | ||
| TYPE = BackendType.CLOUDRIFT | ||
| COMPUTE_CLASS = CloudRiftCompute | ||
|
|
||
| def __init__(self, config: CloudRiftConfig): | ||
| self.config = config | ||
| self._compute = CloudRiftCompute(self.config) | ||
|
|
||
| def compute(self) -> CloudRiftCompute: | ||
| return self._compute |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) Unused