2023-01-16 14:44:19 +01:00
|
|
|
from hcloud import Client
|
|
|
|
from hcloud.images.domain import Image
|
|
|
|
from hcloud.networks.domain import NetworkSubnet
|
|
|
|
from hcloud.server_types.domain import ServerType
|
|
|
|
from hcloud.locations.domain import Location
|
|
|
|
from hcloud.load_balancers.domain import (
|
|
|
|
LoadBalancerHealthCheck,
|
|
|
|
LoadBalancerService,
|
|
|
|
LoadBalancerHealtCheckHttp,
|
|
|
|
LoadBalancerTarget,
|
|
|
|
)
|
|
|
|
from hcloud.load_balancer_types.domain import LoadBalancerType
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
prefix = "444380"
|
|
|
|
ssh_key_name = prefix+"-webservice-ssh-key"
|
|
|
|
subnet_name = prefix+"-webservice-subnet"
|
|
|
|
server_name = prefix+"-webservice-server"
|
2023-01-18 19:54:23 +01:00
|
|
|
load_balancer_name = prefix+"-webservice-loadbalancer"
|
2023-01-16 14:44:19 +01:00
|
|
|
|
|
|
|
public_key = sys.argv[2]
|
|
|
|
token = sys.argv[1]
|
|
|
|
|
2023-01-18 19:54:23 +01:00
|
|
|
server_count = 5
|
2023-01-16 14:44:19 +01:00
|
|
|
|
|
|
|
CLOUD_INIT = """#cloud-config
|
|
|
|
runcmd:
|
|
|
|
- git clone https://git.wmi.amu.edu.pl/s444380/hetzner-webservice
|
|
|
|
- cd hetzner-webservice
|
|
|
|
- chmod +x webservice
|
|
|
|
- ./webservice
|
|
|
|
"""
|
|
|
|
|
|
|
|
client = Client(
|
|
|
|
token=token
|
|
|
|
)
|
|
|
|
|
|
|
|
print("Adding ssh-key...")
|
|
|
|
try:
|
|
|
|
ssh_key = client.ssh_keys.create(name=ssh_key_name, public_key=public_key)
|
|
|
|
except:
|
|
|
|
ssh_key = client.ssh_keys.get_by_name(name=ssh_key_name)
|
|
|
|
print(f"Added ssh-key {ssh_key.data_model.name} ||| {ssh_key.data_model.public_key} ")
|
|
|
|
|
|
|
|
print("Creating subnet...")
|
|
|
|
subnet = client.networks.create(
|
|
|
|
name=subnet_name,
|
|
|
|
ip_range="10.10.10.0/24",
|
|
|
|
subnets=[
|
|
|
|
NetworkSubnet(ip_range="10.10.10.0/24", network_zone="eu-central", type="cloud")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
print(f"Created network: {subnet.data_model.name} ({subnet.data_model.ip_range})")
|
|
|
|
|
|
|
|
targets = []
|
|
|
|
|
|
|
|
print("Creating servers...")
|
|
|
|
for i in range(server_count):
|
|
|
|
server = client.servers.create(
|
|
|
|
name=f"{server_name}-{i+1}",
|
|
|
|
server_type=ServerType("cx11"),
|
|
|
|
image=Image(name="ubuntu-20.04"),
|
|
|
|
ssh_keys=[ssh_key],
|
|
|
|
networks=[subnet],
|
|
|
|
location=Location("hel1"),
|
|
|
|
user_data=CLOUD_INIT
|
|
|
|
)
|
|
|
|
server.action.wait_until_finished()
|
|
|
|
if server.action.complete:
|
|
|
|
print(f"Created server: {server.server.name}")
|
|
|
|
targets.append(LoadBalancerTarget(type="server", server=server.server, use_private_ip=True))
|
|
|
|
|
|
|
|
load_balancer = client.load_balancers.create(
|
|
|
|
name=load_balancer_name,
|
|
|
|
load_balancer_type=LoadBalancerType(name="lb11"),
|
|
|
|
location=Location("hel1"),
|
|
|
|
services=[
|
|
|
|
LoadBalancerService(
|
|
|
|
protocol="http",
|
|
|
|
listen_port=8080,
|
|
|
|
destination_port=8080,
|
|
|
|
proxyprotocol=False,
|
|
|
|
health_check=LoadBalancerHealthCheck(
|
|
|
|
protocol="http",
|
|
|
|
port="8080",
|
|
|
|
interval=15,
|
|
|
|
timeout=10,
|
|
|
|
retries=3,
|
|
|
|
http=LoadBalancerHealtCheckHttp(
|
|
|
|
path="/factors/10",
|
|
|
|
status_codes=["2??", "3??"],
|
|
|
|
tls=False
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
targets=targets,
|
|
|
|
public_interface=True,
|
|
|
|
network=subnet
|
|
|
|
)
|
|
|
|
|
|
|
|
load_balancer.action.wait_until_finished()
|
|
|
|
if load_balancer.action.complete:
|
|
|
|
load_balancer = client.load_balancers.get_by_name(load_balancer_name)
|
|
|
|
print(f"Load balancer is up. http://{load_balancer.data_model.public_net.ipv4.ip}:8080")
|