In this example, we'll set up an automated script to monitor the spot marketplace and automatically rent servers when prices meet predefined thresholds. This script will help users capture GPU rental opportunities at optimal rates, ensuring cost efficiency without manual intervention.
Objective
Automatically rent available servers from the spot marketplace when their prices are below specified thresholds. This use case is beneficial for users who want to take advantage of variable pricing but do not want to monitor the marketplace manually.
Prerequisites
Authorization token for Clore API access.
Predefined price thresholds for each server type.
Implementation
import requests
import time
# Authorization token
AUTH_TOKEN = 'your_auth_token_here'
# Set price thresholds per GPU model
price_thresholds = {
'GeForce GTX 1080 Ti': 0.00005, # BTC per day
'GeForce RTX 3070': 0.00008,
'Tesla V100': 0.0002
}
# Clore API endpoints
MARKETPLACE_URL = 'https://api.clore.ai/v1/spot_marketplace'
RENTAL_URL = 'https://api.clore.ai/v1/create_order'
def get_available_servers():
headers = {'auth': AUTH_TOKEN}
response = requests.get(MARKETPLACE_URL, headers=headers)
if response.status_code == 200:
data = response.json()
if data.get("code") == 0:
return data['market']['offers']
else:
print("Error fetching marketplace data:", response.status_code, response.text)
return []
def rent_server(offer_id, server_id, price):
headers = {
'auth': AUTH_TOKEN,
'Content-type': 'application/json'
}
rental_data = {
'renting_server': server_id,
'type': 'spot',
'spotprice': price
}
response = requests.post(RENTAL_URL, headers=headers, json=rental_data)
if response.status_code == 200:
print(f"Server with ID {server_id} rented successfully.")
else:
print("Error renting server:", response.status_code, response.text)
def main():
while True:
servers = get_available_servers()
for server in servers:
model = server['specs']['gpu']
price = server['spot']['bitcoin']
server_id = server['id']
offer_id = server['offer_id']
# Check if the price meets threshold
if model in price_thresholds and price <= price_thresholds[model]:
print(f"Renting server {server_id} with {model} at {price} BTC (Threshold: {price_thresholds[model]} BTC)")
rent_server(offer_id, server_id, price)
# Sleep before re-checking marketplace
time.sleep(300) # Adjust based on desired check frequency
if __name__ == "__main__":
main()
Explanation
get_available_servers: Fetches the current available servers from the spot marketplace.
rent_server: Initiates a rental request for a server if it meets the predefined price conditions.
main loop: Repeatedly checks for servers at optimal prices and rents them when conditions are met.
Summary
This script helps you dynamically rent servers at optimal prices by continuously monitoring and acting on the spot marketplace without manual intervention.