-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_creator.py
More file actions
91 lines (75 loc) · 3.14 KB
/
server_creator.py
File metadata and controls
91 lines (75 loc) · 3.14 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import requests
from SECRETS import HETZNER_API_KEY, HETZNER_SSH_KEY_NAME
def server_creator(server_name):
# Define the API endpoint for retrieving locations and server types
url_locations = "https://api.hetzner.cloud/v1/locations"
url_server_types = "https://api.hetzner.cloud/v1/server_types?type=basic"
# Define the API token to authenticate the request
headers = {
"Authorization": f"Bearer {HETZNER_API_KEY}",
"Content-Type": "application/json",
}
# Retrieve the available locations from the API
response = requests.get(url_locations, headers=headers)
# Check if the request was successful
if response.status_code == 200:
locations = response.json()["locations"]
else:
print("Failed to retrieve locations:")
print(response.json())
exit()
# Ask the user to choose a location
print("Available locations:")
for i, location in enumerate(locations):
print(f"{i + 1}: {location['name']} ({location['description']})")
location_choice = input("Choose a location (1-{}): ".format(len(locations)))
# Retrieve the available server types from the API for the chosen location
response = requests.get(url_server_types + "&location=" + locations[int(location_choice) - 1]["name"],
headers=headers)
# Check if the request was successful
if response.status_code == 200:
server_types = response.json()["server_types"]
else:
print("Failed to retrieve server types:")
print(response.json())
exit()
# Select the cheapest server type
print(server_types)
cheapest_server_type = min(server_types, key=lambda x: x["prices"][0]["price_hourly"]["net"])
server_type_choice = cheapest_server_type["name"]
# Define the API endpoint for creating a server
url_servers = "https://api.hetzner.cloud/v1/servers"
# Define the payload for the server creation request
data = {
"name": f"{server_name}",
"server_type": server_type_choice,
"location": locations[int(location_choice) - 1]["name"],
"image": "ubuntu-20.04",
"start_after_create": True,
"ssh_keys": [HETZNER_SSH_KEY_NAME],
}
# Send the server creation request
response = requests.post(url_servers, headers=headers, json=data)
# Check if the request was successful
if response.status_code != 201:
print(f"Error creating server: {response.text}")
exit(1)
print("Server created successfully:")
server = response.json()['server']
print(server)
# Replace "YOUR_SERVER_ID" with the actual ID of your server
server_id = server["id"]
server_status = None
while server_status != "running":
response = requests.get(
f"https://api.hetzner.cloud/v1/servers/{server_id}", headers=headers
)
server_status = response.json()["server"]["status"]
login_info = {
"server_id": server["id"],
"server_name": server["name"],
"server_ip": server["public_net"]["ipv4"]["ip"],
}
print("Server Created. Login information:")
print(login_info)
return login_info