Skip to content

Commit 5382d27

Browse files
committed
removed custom instance str func, wait for provisioning status on create
1 parent 7f4843b commit 5382d27

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

datacrunch/instances/instances.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import time
12
from typing import List, Union, Optional, Dict, Literal
23
from dataclasses import dataclass
34
from dataclasses_json import dataclass_json
4-
from datacrunch.helpers import stringify_class_object_properties
5-
from datacrunch.constants import Locations
5+
from datacrunch.constants import Locations, InstanceStatus
66

77
INSTANCES_ENDPOINT = '/instances'
88

@@ -21,16 +21,16 @@ class Instance:
2121
price_per_hour: Cost per hour of running the instance.
2222
hostname: Network hostname of the instance.
2323
description: Human-readable description of the instance.
24-
ip: IP address assigned to the instance.
2524
status: Current operational status of the instance.
2625
created_at: Timestamp of instance creation.
2726
ssh_key_ids: List of SSH key IDs associated with the instance.
2827
cpu: CPU configuration details.
2928
gpu: GPU configuration details.
3029
memory: Memory configuration details.
3130
storage: Storage configuration details.
32-
os_volume_id: ID of the operating system volume.
3331
gpu_memory: GPU memory configuration details.
32+
ip: IP address assigned to the instance.
33+
os_volume_id: ID of the operating system volume.
3434
location: Datacenter location code (default: Locations.FIN_01).
3535
image: Image ID or type used for the instance.
3636
startup_script_id: ID of the startup script to run.
@@ -44,31 +44,25 @@ class Instance:
4444
price_per_hour: float
4545
hostname: str
4646
description: str
47-
ip: str
4847
status: str
4948
created_at: str
5049
ssh_key_ids: List[str]
5150
cpu: dict
5251
gpu: dict
5352
memory: dict
5453
storage: dict
55-
os_volume_id: str
5654
gpu_memory: dict
55+
# Can be None if instance is still not provisioned
56+
ip: Optional[str] = None
57+
# Can be None if instance is still not provisioned
58+
os_volume_id: Optional[str] = None
5759
location: str = Locations.FIN_01
5860
image: Optional[str] = None
5961
startup_script_id: Optional[str] = None
6062
is_spot: bool = False
6163
contract: Optional[Contract] = None
6264
pricing: Optional[Pricing] = None
6365

64-
def __str__(self) -> str:
65-
"""Returns a JSON string representation of the instance.
66-
67-
Returns:
68-
JSON string containing all instance properties.
69-
"""
70-
return stringify_class_object_properties(self)
71-
7266

7367
class InstancesService:
7468
"""Service for managing cloud instances through the API.
@@ -173,7 +167,22 @@ def create(self,
173167
if pricing:
174168
payload['pricing'] = pricing
175169
id = self._http_client.post(INSTANCES_ENDPOINT, json=payload).text
176-
return self.get_by_id(id)
170+
171+
# Wait for instance to enter provisioning state with timeout
172+
MAX_WAIT_TIME = 60 # Maximum wait time in seconds
173+
POLL_INTERVAL = 0.5 # Time between status checks
174+
175+
start_time = time.time()
176+
while True:
177+
instance = self.get_by_id(id)
178+
if instance.status == InstanceStatus.PROVISIONING:
179+
return instance
180+
181+
if time.time() - start_time > MAX_WAIT_TIME:
182+
raise TimeoutError(
183+
f"Instance {id} did not enter provisioning state within {MAX_WAIT_TIME} seconds")
184+
185+
time.sleep(POLL_INTERVAL)
177186

178187
def action(self, id_list: Union[List[str], str], action: str, volume_ids: Optional[List[str]] = None) -> None:
179188
"""Performs an action on one or more instances.

examples/simple_create_instance.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import time
12
import os
23
from datacrunch import DataCrunchClient
4+
from datacrunch.constants import Locations, InstanceStatus
35

46
# Get client secret and id from environment variables
57
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
@@ -14,11 +16,19 @@
1416

1517
# Create a new instance
1618
instance = datacrunch.instances.create(instance_type='1V100.6V',
17-
image='ubuntu-24.04-cuda-12.8-open-docker',
19+
image='ubuntu-22.04-cuda-12.0-docker',
20+
location=Locations.FIN_01,
1821
ssh_key_ids=ssh_keys_ids,
1922
hostname='example',
2023
description='example instance')
2124

25+
# Wait for instance to enter running state
26+
while instance.status != InstanceStatus.RUNNING:
27+
time.sleep(0.5)
28+
instance = datacrunch.instances.get_by_id(instance.id)
29+
30+
print(instance)
31+
2232
# Delete instance
2333
datacrunch.instances.action(
2434
instance.id, datacrunch.constants.instance_actions.DELETE)

0 commit comments

Comments
 (0)