-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathadvanced_create_instance.py
More file actions
71 lines (58 loc) · 3.03 KB
/
advanced_create_instance.py
File metadata and controls
71 lines (58 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from datacrunch import DataCrunchClient
from datacrunch.exceptions import APIException
"""
In this hypothetical example, we check if we have enough balance
to deploy an 8V100.48V instance for a week.
If there's not enough balance, we deploy a 4V100.20V instance.
This example uses the balance service to check the current balance,
the instace_types service to check instance type details (price per hour)
We also perform other basic tasks such as creating the client and adding a new SSH key.
"""
# The instance types we want to deploy
INSTANCE_TYPE_8V = '8V100.48V'
INSTANCE_TYPE_4V = '4V100.20V'
# Arbitrary duration for the example
DURATION = 24 * 7 # one week
# Get client secret and id from environment variables
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
try:
# Create datcrunch client
datacrunch = DataCrunchClient(
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
# Create new SSH key
public_key = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI0qq2Qjt5GPi7DKdcnBHOkvk8xNsG9dA607tnWagOkHC test_key'
ssh_key = datacrunch.ssh_keys.create('my test key', public_key)
# Get all SSH keys
ssh_keys = datacrunch.ssh_keys.get()
ssh_keys_ids = list(map(lambda ssh_key: ssh_key.id, ssh_keys))
# Get our current balance
balance = datacrunch.balance.get()
print(balance.amount)
# Get instance types
instance_types = datacrunch.instance_types.get()
# Deploy 8V instance if enough balance for a week, otherwise deploy a 4V
for instance_details in instance_types:
if instance_details.instance_type == INSTANCE_TYPE_8V:
price_per_hour = instance_details.price_per_hour
if price_per_hour * DURATION < balance.amount:
# Deploy a new 8V instance
instance = datacrunch.instances.create(instance_type=INSTANCE_TYPE_8V,
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='large instance',
os_volume={
"name": "Large OS volume",
"size": 95
})
else:
# Deploy a new 4V instance
instance = datacrunch.instances.create(instance_type=INSTANCE_TYPE_4V,
image='ubuntu-24.04-cuda-12.8-open-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='medium instance')
except APIException as exception:
print(exception)