-
Notifications
You must be signed in to change notification settings - Fork 222
add hotaisle backend #2935
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
Merged
add hotaisle backend #2935
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f5f2f2e
add hotaisle backend
b9ca0be
Daemonize launch_command to solve dstack restart issue
9558c29
Update backends.md and config.yml.md
1bf72e0
Resolve Review Comments
16e77e3
Bump gpuhunt to 0.1.7
d2846ae
Resolve Remaining Review Comments
c19065b
Add hotaisle to TestListBackendTypes
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
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
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 @@ | ||
| # Hotaisle backend for dstack |
109 changes: 109 additions & 0 deletions
109
src/dstack/_internal/core/backends/hotaisle/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,109 @@ | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| import requests | ||
|
|
||
| from dstack._internal.core.backends.base.configurator import raise_invalid_credentials_error | ||
| from dstack._internal.utils.logging import get_logger | ||
|
|
||
| API_URL = "https://admin.hotaisle.app/api" | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class HotAisleAPIClient: | ||
| def __init__(self, api_key: str, team_handle: str): | ||
| self.api_key = api_key | ||
| self.team_handle = team_handle | ||
|
|
||
| def validate_api_key(self) -> bool: | ||
| try: | ||
| self._validate_user_and_team() | ||
| return True | ||
| except requests.HTTPError as e: | ||
| if e.response.status_code == 401: | ||
| raise_invalid_credentials_error( | ||
| fields=[["creds", "api_key"]], details="Invalid API key" | ||
| ) | ||
| elif e.response.status_code == 403: | ||
| raise_invalid_credentials_error( | ||
| fields=[["creds", "api_key"]], | ||
| details="Authenticated user does note have required permissions", | ||
| ) | ||
| raise e | ||
| except ValueError as e: | ||
| error_message = str(e) | ||
| if "No Hot Aisle teams found" in error_message: | ||
| raise_invalid_credentials_error( | ||
| fields=[["creds", "api_key"]], | ||
| details="Valid API key but no teams found for this user", | ||
| ) | ||
| elif "not found" in error_message: | ||
| raise_invalid_credentials_error( | ||
| fields=[["team_handle"]], details=f"Team handle '{self.team_handle}' not found" | ||
| ) | ||
|
Comment on lines
+33
to
+43
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) Looking for patterns in our own error messages and then raising with another error message looks quite redundant. It's also error-prone, because we can change the error message in Some alternatives I can suggest:
|
||
| raise e | ||
|
|
||
| def _validate_user_and_team(self) -> None: | ||
| url = f"{API_URL}/user/" | ||
| response = self._make_request("GET", url) | ||
| response.raise_for_status() | ||
| user_data = response.json() | ||
|
|
||
| teams = user_data.get("teams", []) | ||
| if not teams: | ||
| raise ValueError("No Hot Aisle teams found for this user") | ||
|
|
||
| available_teams = [team["handle"] for team in teams] | ||
| if self.team_handle not in available_teams: | ||
| raise ValueError(f"Hot Aisle team '{self.team_handle}' not found.") | ||
|
|
||
| def upload_ssh_key(self, public_key: str) -> bool: | ||
| url = f"{API_URL}/user/ssh_keys/" | ||
| payload = {"authorized_key": public_key} | ||
|
|
||
| response = self._make_request("POST", url, json=payload) | ||
|
|
||
| if response.status_code == 409: | ||
| return True # Key already exists - success | ||
| response.raise_for_status() | ||
| return True | ||
|
|
||
| def create_virtual_machine(self, vm_payload: Dict[str, Any]) -> Dict[str, Any]: | ||
| url = f"{API_URL}/teams/{self.team_handle}/virtual_machines/" | ||
| response = self._make_request("POST", url, json=vm_payload) | ||
| response.raise_for_status() | ||
| vm_data = response.json() | ||
| return vm_data | ||
|
|
||
| def get_vm_state(self, vm_name: str) -> str: | ||
| url = f"{API_URL}/teams/{self.team_handle}/virtual_machines/{vm_name}/state/" | ||
| response = self._make_request("GET", url) | ||
| response.raise_for_status() | ||
| state_data = response.json() | ||
| return state_data["state"] | ||
|
|
||
| def terminate_virtual_machine(self, vm_name: str) -> None: | ||
| url = f"{API_URL}/teams/{self.team_handle}/virtual_machines/{vm_name}/" | ||
| response = self._make_request("DELETE", url) | ||
| if response.status_code == 404: | ||
| logger.debug("Hot Aisle virtual machine %s not found", vm_name) | ||
| return | ||
| response.raise_for_status() | ||
|
|
||
| def _make_request( | ||
| self, method: str, url: str, json: Optional[Dict[str, Any]] = None, timeout: int = 30 | ||
| ) -> requests.Response: | ||
| headers = { | ||
| "accept": "application/json", | ||
| "Authorization": f"Token {self.api_key}", | ||
| } | ||
| if json is not None: | ||
| headers["Content-Type"] = "application/json" | ||
|
|
||
| return requests.request( | ||
| method=method, | ||
| url=url, | ||
| headers=headers, | ||
| json=json, | ||
| timeout=timeout, | ||
| ) | ||
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.hotaisle.compute import HotAisleCompute | ||
| from dstack._internal.core.backends.hotaisle.models import HotAisleConfig | ||
| from dstack._internal.core.models.backends.base import BackendType | ||
|
|
||
|
|
||
| class HotAisleBackend(Backend): | ||
| TYPE = BackendType.HOTAISLE | ||
| COMPUTE_CLASS = HotAisleCompute | ||
|
|
||
| def __init__(self, config: HotAisleConfig): | ||
| self.config = config | ||
| self._compute = HotAisleCompute(self.config) | ||
|
|
||
| def compute(self) -> HotAisleCompute: | ||
| return self._compute |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.