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
16 changes: 16 additions & 0 deletions src/dstack/_internal/core/backends/nebius/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,22 @@ def update_provisioning_data(
self, provisioning_data, project_ssh_public_key, project_ssh_private_key
):
instance = resources.get_instance(self._sdk, provisioning_data.instance_id)
state = instance.status.state
# A new instance is `STOPPED` before it starts, with `reconciling` set. `STOPPED`
# is final once Nebius has no operation left to run on the instance.
is_stopped = (
state == InstanceStatus.InstanceState.STOPPED and not instance.status.reconciling
)
if is_stopped or state in [
InstanceStatus.InstanceState.DELETING,
InstanceStatus.InstanceState.ERROR,
]:
raise ProvisioningError(f"Instance failed to start. Instance state: {state.name}")
# Nebius assigns addresses to an instance that is still starting, and this method
# is no longer called once the hostname is set. So the hostname is set only when
# the instance is running. Otherwise, a failed start would go unnoticed.
if state != InstanceStatus.InstanceState.RUNNING:
return
if not instance.status.network_interfaces:
return
interface = instance.status.network_interfaces[0]
Expand Down
76 changes: 75 additions & 1 deletion src/tests/_internal/core/backends/nebius/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
Instance,
InstanceSpec,
InstanceStatus,
IPAddressStatus,
NetworkInterfaceStatus,
PublicIPAddressStatus,
ResourcesSpec,
)

Expand All @@ -33,7 +36,7 @@
NebiusOfferBackendData,
NebiusServiceAccountCreds,
)
from dstack._internal.core.errors import NoCapacityError, NotYetTerminated
from dstack._internal.core.errors import NoCapacityError, NotYetTerminated, ProvisioningError
from dstack._internal.core.models.backends.base import BackendType
from dstack._internal.core.models.backends.orphans import ListedResourceGroup, OwnerType
from dstack._internal.core.models.instances import (
Expand All @@ -49,6 +52,7 @@
PlacementGroupProvisioningData,
PlacementStrategy,
)
from dstack._internal.core.models.runs import JobProvisioningData

CapacityAvailability = ResourceAdviceStatus.Availability
CAPACITY_LEVELS = CapacityAvailability.AvailabilityLevel
Expand Down Expand Up @@ -735,6 +739,76 @@ def make_resource_group(
)


def make_provisioning_data() -> JobProvisioningData:
return JobProvisioningData(
backend=BackendType.NEBIUS,
instance_type=make_offer(1, spot=False, is_preemptible_flat_rate=False).instance,
instance_id="instance-1",
hostname=None,
region="eu-north1",
price=1,
username="ubuntu",
ssh_port=22,
dockerized=True,
)


class TestUpdateProvisioningData:
@pytest.fixture(autouse=True)
def _mock_sdk(self, mocker):
mocker.patch.object(NebiusCompute, "_sdk", Mock())

@pytest.fixture
def mock_instance(self, mocker):
def mock(state: InstanceStatus.InstanceState, reconciling: bool = False) -> None:
instance = make_instance(state=state)
instance.status.reconciling = reconciling
instance.status.network_interfaces = [
NetworkInterfaceStatus(
ip_address=IPAddressStatus(address="10.0.0.5/32"),
public_ip_address=PublicIPAddressStatus(address="203.0.113.5/32"),
)
]
mocker.patch.object(compute_module.resources, "get_instance", return_value=instance)

return mock

@pytest.mark.parametrize(
("state", "reconciling", "expected_hostname"),
[
# A new instance is stopped before it starts
(InstanceStatus.InstanceState.STOPPED, True, None),
(InstanceStatus.InstanceState.STARTING, True, None),
(InstanceStatus.InstanceState.RUNNING, False, "203.0.113.5"),
],
)
def test_sets_hostname_only_when_running(
self, mock_instance, state, reconciling, expected_hostname
):
mock_instance(state, reconciling)
provisioning_data = make_provisioning_data()

make_compute().update_provisioning_data(provisioning_data, "public-key", "private-key")

assert provisioning_data.hostname == expected_hostname

@pytest.mark.parametrize(
"state",
[
InstanceStatus.InstanceState.STOPPED,
InstanceStatus.InstanceState.DELETING,
InstanceStatus.InstanceState.ERROR,
],
)
def test_raises_if_instance_will_not_run(self, mock_instance, state):
mock_instance(state)

with pytest.raises(ProvisioningError, match=state.name):
make_compute().update_provisioning_data(
make_provisioning_data(), "public-key", "private-key"
)


class TestListResourceGroups:
@pytest.fixture(autouse=True)
def _mock_sdk(self, mocker):
Expand Down
Loading