forked from verda-cloud/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_create_instance.py
More file actions
74 lines (60 loc) · 2.5 KB
/
advanced_create_instance.py
File metadata and controls
74 lines (60 loc) · 2.5 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
72
73
74
import os
from verda import VerdaClient
from verda.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 instance_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
CLIENT_ID = os.environ.get('VERDA_CLIENT_ID')
CLIENT_SECRET = os.environ.get('VERDA_CLIENT_SECRET')
try:
# Create client
verda = VerdaClient(CLIENT_ID, CLIENT_SECRET)
# Create new SSH key
public_key = (
'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI0qq2Qjt5GPi7DKdcnBHOkvk8xNsG9dA607tnWagOkHC test_key'
)
ssh_key = verda.ssh_keys.create('my test key', public_key)
# Get all SSH keys
ssh_keys = verda.ssh_keys.get()
ssh_keys_ids = [ssh_key.id for ssh_key in ssh_keys]
# Get our current balance
balance = verda.balance.get()
print(balance.amount)
# Get instance types
instance_types = verda.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 = verda.instances.create(
instance_type=INSTANCE_TYPE_8V,
image='ubuntu-22.04-cuda-12.0-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 = verda.instances.create(
instance_type=INSTANCE_TYPE_4V,
image='ubuntu-22.04-cuda-12.0-docker',
ssh_key_ids=ssh_keys_ids,
hostname='example',
description='medium instance',
)
except APIException as exception:
print(exception)