Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ jobs:
run: uv sync --all-extras
- name: Run pyright
uses: jakebailey/pyright-action@v3
env:
# The default Node heap on the macOS runners (~2 GB) is not enough for pyright
NODE_OPTIONS: --max-old-space-size=4096
with:
pylance-version: latest-release
- name: Download frontend build
Expand Down
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ include = [
"src/dstack/plugins",
"src/dstack/_internal/server",
"src/dstack/_internal/core/services",
"src/dstack/_internal/core/backends/aws",
"src/dstack/_internal/core/backends/daytona",
"src/dstack/_internal/core/backends/kubernetes",
"src/dstack/_internal/core/backends/lium",
"src/dstack/_internal/core/backends/runpod",
"src/dstack/_internal/core/backends/slurm",
"src/dstack/_internal/core/backends",
"src/dstack/_internal/cli/services/configurators",
"src/dstack/_internal/cli/commands",
"src/tests/_internal/server/background/pipeline_tasks",
]
exclude = [
# OCI has pyright errors. Excluded rather than ignored because checking it makes pyright analyze
# the large OCI SDK, which is slow and takes a lot of memory. Editors still show its errors
"src/dstack/_internal/core/backends/oci",
]
ignore = [
"src/dstack/_internal/server/migrations/versions",
]
Expand Down
20 changes: 10 additions & 10 deletions src/dstack/_internal/core/backends/azure/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Union
from typing import Union

from azure.core.exceptions import ClientAuthenticationError
from azure.identity import ClientSecretCredential, DefaultAzureCredential
Expand All @@ -9,26 +9,26 @@
AzureClientCreds,
)
from dstack._internal.core.errors import BackendAuthError
from dstack._internal.utils.common import get_or_error

AzureCredential = Union[ClientSecretCredential, DefaultAzureCredential]


def authenticate(creds: AnyAzureCreds) -> Tuple[AzureCredential, str]:
credential, tenant_id = get_credential(creds)
def authenticate(creds: AnyAzureCreds) -> AzureCredential:
credential = get_credential(creds)
check_credential(credential)
return credential, tenant_id
return credential


def get_credential(creds: AnyAzureCreds) -> Tuple[AzureCredential, str]:
def get_credential(creds: AnyAzureCreds) -> AzureCredential:
if isinstance(creds, AzureClientCreds):
credential = ClientSecretCredential(
tenant_id=creds.tenant_id,
return ClientSecretCredential(
# The configurator populates `tenant_id` from the backend config if it's missing
tenant_id=get_or_error(creds.tenant_id),
client_id=creds.client_id,
client_secret=creds.client_secret,
)
return credential, creds.tenant_id

return DefaultAzureCredential(), None
return DefaultAzureCredential()


def check_credential(credential: AzureCredential):
Expand Down
2 changes: 1 addition & 1 deletion src/dstack/_internal/core/backends/azure/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AzureBackend(Backend):

def __init__(self, config: AzureConfig):
self.config = config
self.credential, _ = auth.authenticate(self.config.creds)
self.credential = auth.authenticate(self.config.creds)
self._compute = AzureCompute(
config=self.config,
credential=self.credential,
Expand Down
54 changes: 30 additions & 24 deletions src/dstack/_internal/core/backends/azure/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ def create_instance(
allocate_public_ip = self.config.allocate_public_ips
network_resource_group, network, subnet = get_resource_group_network_subnet_or_error(
network_client=self._network_client,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
vpc_ids=self.config.vpc_ids,
subnet_ids=self.config.subnet_ids,
location=location,
allocate_public_ip=allocate_public_ip,
)
network_security_group = azure_utils.get_default_network_security_group_name(
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
location=location,
)

Expand Down Expand Up @@ -190,7 +190,7 @@ def create_instance(
compute_client=self._compute_client,
subscription_id=self.config.subscription_id,
location=location,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
network_security_group=network_security_group,
network=network,
subnet=subnet,
Expand All @@ -214,7 +214,7 @@ def create_instance(
logger.info("Request succeeded")
public_ip, private_ip = _get_vm_public_private_ips(
network_client=self._network_client,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
vm=vm,
)
hostname = public_ip
Expand All @@ -223,7 +223,7 @@ def create_instance(
return JobProvisioningData(
backend=instance_offer.backend,
instance_type=instance_offer.instance,
instance_id=vm.name,
instance_id=get_or_error(vm.name),
hostname=hostname,
internal_ip=private_ip,
region=location,
Expand All @@ -240,7 +240,7 @@ def terminate_instance(
):
_terminate_instance(
compute_client=self._compute_client,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
instance_name=instance_id,
)

Expand All @@ -256,7 +256,7 @@ def list_resource_groups(self) -> ResourceListing:
try:
vms = list(
self._compute_client.virtual_machines.list(
resource_group_name=self.config.resource_group
resource_group_name=self.config.get_resource_group()
)
)
except AzureError as e:
Expand Down Expand Up @@ -290,7 +290,7 @@ def delete_resource_group(self, group: ListedResourceGroup) -> None:
try:
_terminate_instance(
compute_client=self._compute_client,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
instance_name=get_or_error(group.primary_id),
)
except ResourceNotFoundError:
Expand Down Expand Up @@ -320,14 +320,14 @@ def create_gateway_replica(
)
network_resource_group, network, subnet = get_resource_group_network_subnet_or_error(
network_client=self._network_client,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
vpc_ids=self.config.vpc_ids,
subnet_ids=self.config.subnet_ids,
location=configuration.region,
allocate_public_ip=True,
)
network_security_group = azure_utils.get_default_network_security_group_name(
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
location=configuration.region,
)

Expand All @@ -349,7 +349,7 @@ def create_gateway_replica(
compute_client=self._compute_client,
subscription_id=self.config.subscription_id,
location=configuration.region,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
network_security_group=network_security_group,
network=network,
subnet=subnet,
Expand All @@ -369,12 +369,12 @@ def create_gateway_replica(
logger.info("Request succeeded")
public_ip, _ = _get_vm_public_private_ips(
network_client=self._network_client,
resource_group=self.config.resource_group,
resource_group=self.config.get_resource_group(),
vm=vm,
)
return GatewayReplicaProvisioningData(
instance_id=vm.name,
ip_address=public_ip,
instance_id=get_or_error(vm.name),
ip_address=get_or_error(public_ip),
region=configuration.region,
)

Expand Down Expand Up @@ -549,10 +549,11 @@ def _supported_instances(offer: InstanceOffer) -> bool:

def _get_offers_with_availability(
compute_client: compute_mgmt.ComputeManagementClient,
config_locations: List[str],
config_locations: Optional[List[str]],
offers: List[InstanceOffer],
) -> List[InstanceOfferWithAvailability]:
offers = [offer for offer in offers if offer.region in config_locations]
if config_locations is not None:
offers = [offer for offer in offers if offer.region in config_locations]
locations = set(offer.region for offer in offers)

def get_location_quotas(location: str) -> List[str]:
Expand Down Expand Up @@ -583,11 +584,12 @@ def get_location_quotas(location: str) -> List[str]:


def _vm_type_available(vm_resource: ResourceSku) -> bool:
if len(vm_resource.restrictions) == 0:
restrictions = vm_resource.restrictions
if not restrictions:
return True
# If a VM type is restricted in "Zone", it is still available in other zone.
# Otherwise the restriction type is "Location"
if vm_resource.restrictions[0].type == "Zone":
if restrictions[0].type == "Zone":
return True
return False

Expand Down Expand Up @@ -671,10 +673,13 @@ def _begin_create_instance(
},
)
try:
# The typed overloads of the TypeSpec-generated models (azure-mgmt-compute 38+) only
# accept nested `properties`. The flattened kwargs used here are supported at runtime
# and are the only form accepted by older SDK versions.
poller = compute_client.virtual_machines.begin_create_or_update(
resource_group,
instance_name,
VirtualMachine(
VirtualMachine( # pyright: ignore[reportCallIssue]
location=location,
hardware_profile=HardwareProfile(vm_size=vm_size),
storage_profile=StorageProfile(
Expand Down Expand Up @@ -708,7 +713,7 @@ def _begin_create_instance(
# Azure deletes the NIC and public IP created from these configurations
# together with the VM.
network_interface_configurations=[
VirtualMachineNetworkInterfaceConfiguration(
VirtualMachineNetworkInterfaceConfiguration( # pyright: ignore[reportCallIssue]
name="nic_config",
network_security_group=SubResource(
id=azure_utils.get_network_security_group_id(
Expand All @@ -718,7 +723,7 @@ def _begin_create_instance(
)
),
ip_configurations=[
VirtualMachineNetworkInterfaceIPConfiguration(
VirtualMachineNetworkInterfaceIPConfiguration( # pyright: ignore[reportCallIssue]
name="ip_config",
subnet=SubResource(
id=azure_utils.get_subnet_id(
Expand Down Expand Up @@ -855,11 +860,12 @@ def _get_vm_public_private_ips(
network_interface_name=nic_name,
)

private_ip = nic.ip_configurations[0].private_ip_address
if nic.ip_configurations[0].public_ip_address is None:
ip_configuration = get_or_error(nic.ip_configurations)[0]
private_ip = get_or_error(ip_configuration.private_ip_address)
if ip_configuration.public_ip_address is None:
return None, private_ip

public_ip_id = nic.ip_configurations[0].public_ip_address.id
public_ip_id = get_or_error(ip_configuration.public_ip_address.id)
public_ip_name = azure_utils.get_resource_name_from_resource_id(public_ip_id)
public_ip = network_client.public_ip_addresses.get(resource_group, public_ip_name)
return public_ip.ip_address, private_ip
Expand Down
9 changes: 5 additions & 4 deletions src/dstack/_internal/core/backends/azure/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
BackendType,
)
from dstack._internal.core.models.common import validate_extra_ignore, validate_json_extra_ignore
from dstack._internal.utils.common import get_or_error

LOCATIONS = [
("(US) Central US", "centralus"),
Expand Down Expand Up @@ -86,7 +87,7 @@ def validate_config(self, config: AzureBackendConfigWithCreds, default_creds_ena
if isinstance(config.creds, AzureClientCreds):
self._set_client_creds_tenant_id(config.creds, config.tenant_id)
try:
credential, _ = auth.authenticate(config.creds)
credential = auth.authenticate(config.creds)
except BackendAuthError:
if isinstance(config.creds, AzureClientCreds):
raise_invalid_credentials_error(
Expand All @@ -113,7 +114,7 @@ def create_backend(
config.regions = DEFAULT_LOCATIONS
if isinstance(config.creds, AzureClientCreds):
self._set_client_creds_tenant_id(config.creds, config.tenant_id)
credential, _ = auth.authenticate(config.creds)
credential = auth.authenticate(config.creds)
if config.resource_group is None:
config.resource_group = self._create_resource_group(
credential=credential,
Expand Down Expand Up @@ -393,7 +394,7 @@ def create_resource_group(
location=location,
),
)
return resource_group.name
return get_or_error(resource_group.name)

def resource_group_exists(
self,
Expand Down Expand Up @@ -435,7 +436,7 @@ def create_virtual_network(
],
),
).result()
return network.name, subnet_name
return get_or_error(network.name), subnet_name

def create_network_security_group(
self,
Expand Down
14 changes: 12 additions & 2 deletions src/dstack/_internal/core/backends/azure/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Annotated, Dict, List, Literal, Optional, Union

from pydantic import Field, RootModel
from pydantic import Field, RootModel, model_validator
from typing_extensions import Self

from dstack._internal.core.models.common import CoreModel
from dstack._internal.utils.common import get_or_error


class AzureClientCreds(CoreModel):
Expand Down Expand Up @@ -95,7 +97,11 @@ class AzureBackendConfigWithCreds(AzureBackendConfig):


class AzureStoredConfig(AzureBackendConfig):
resource_group: str = ""
@model_validator(mode="after")
def validate_resource_group(self) -> Self:
if self.resource_group is None:
raise ValueError("resource_group must be set in the stored config")
return self


class AzureConfig(AzureStoredConfig):
Expand All @@ -106,3 +112,7 @@ def allocate_public_ips(self) -> bool:
if self.public_ips is not None:
return self.public_ips
return True

def get_resource_group(self) -> str:
# Guaranteed by `AzureStoredConfig.validate_resource_group()`
return get_or_error(self.resource_group)
13 changes: 10 additions & 3 deletions src/dstack/_internal/core/backends/base/offers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def get_catalog_offers(
) -> List[InstanceOffer]:
"""
Args:
configurable_disk_size: disk sizes the backend can provision. Used for catalog items
without a fixed disk size: the offer's disk is set to the smallest size within both
this range and the required range (`DEFAULT_DISK` if `requirements` don't specify disk).
`min` must be set, since it is the lower bound when the required range has none.
catalog_item_filter: applied to raw catalog items before the conversion to
`InstanceOffer` models. Use it for filtering that can be done on raw catalog fields
to avoid expensive model construction for items that will be discarded.
Expand Down Expand Up @@ -93,6 +97,8 @@ def catalog_item_to_offer(
# The rest use construct() to skip redundant validation — data comes from already validated CatalogItem.
gpus = []
if item.gpu_count > 0:
# gpuhunt requires GPU name and memory for items with GPUs
assert item.gpu_name is not None and item.gpu_memory is not None
gpu = Gpu(
vendor=item.gpu_vendor, name=item.gpu_name, memory_mib=round(item.gpu_memory * 1024)
)
Expand Down Expand Up @@ -181,8 +187,9 @@ def requirements_to_query_filter(
q.min_memory = res.memory.min
q.max_memory = res.memory.max
if res.disk and not ignore_disk:
q.min_disk_size = res.disk.size.min
q.max_disk_size = res.disk.size.max
# `QueryFilter` disk size bounds are typed as `int` until the next gpuhunt release
q.min_disk_size = res.disk.size.min # pyright: ignore[reportAttributeAccessIssue]
q.max_disk_size = res.disk.size.max # pyright: ignore[reportAttributeAccessIssue]

if res.gpu:
q.gpu_vendor = res.gpu.vendor
Expand Down Expand Up @@ -229,7 +236,7 @@ def choose_disk_size_mib(
disk_size_range = disk_size_range.intersect(configurable_disk_size)
if disk_size_range is None:
return None
disk_size_gib = disk_size_range.min
disk_size_gib = get_or_error(disk_size_range.min)

return round(disk_size_gib * 1024)

Expand Down
Loading
Loading