52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
|
#!/usr/bin/env python3
|
||
|
from pathlib import Path
|
||
|
|
||
|
from hcloud import Client
|
||
|
from hcloud.images.domain import Image
|
||
|
from hcloud.locations.domain import Location
|
||
|
from hcloud.networks.domain import NetworkSubnet
|
||
|
from hcloud.server_types.domain import ServerType
|
||
|
|
||
|
with open('.credentials/token', 'r') as file:
|
||
|
client = Client(token=str(file.readline()).strip())
|
||
|
|
||
|
with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file:
|
||
|
ssh_raw = file.readline()
|
||
|
|
||
|
index = 's444501'
|
||
|
ssh_name = index + '-ssh'
|
||
|
network_name = index + '-network'
|
||
|
server_base_name = index + '-server'
|
||
|
SERVER_COUNT = 5
|
||
|
|
||
|
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_raw)
|
||
|
|
||
|
vnet = client.networks.create(
|
||
|
name=network_name,
|
||
|
ip_range="10.10.10.0/24",
|
||
|
subnets=[
|
||
|
NetworkSubnet(ip_range="10.10.10.0/24", network_zone="eu-central", type="cloud")
|
||
|
]
|
||
|
)
|
||
|
|
||
|
cloud_init = r'''#cloud-config
|
||
|
runcmd:
|
||
|
-
|
||
|
'''
|
||
|
servers = []
|
||
|
for i in range(SERVER_COUNT):
|
||
|
server = client.servers.create(
|
||
|
name=server_base_name + '-' + str(i+1),
|
||
|
server_type=ServerType("cx11"),
|
||
|
image=Image(name="ubuntu-22.04"),
|
||
|
ssh_keys=[ssh_key],
|
||
|
networks=[vnet],
|
||
|
location=Location("hel1"),
|
||
|
user_data=cloud_init
|
||
|
)
|
||
|
server.action.wait_until_finished()
|
||
|
print(f"Tworzenie serwera: {server.action.complete}")
|
||
|
servers.append(server)
|
||
|
|
||
|
print(f"Webserwis pod adresem http://{server.server.data_model.public_net.ipv4.ip}:8080/factors/")
|