2023-01-22 15:03:37 +01:00
|
|
|
from hcloud import Client
|
|
|
|
from hcloud.images.domain import Image
|
|
|
|
from hcloud.server_types.domain import ServerType
|
|
|
|
from hcloud.networks.domain import NetworkSubnet
|
|
|
|
from hcloud.locations.domain import Location
|
|
|
|
from hcloud.load_balancer_types.domain import LoadBalancerType
|
|
|
|
from hcloud.load_balancers.domain import LoadBalancerTarget, LoadBalancerService, LoadBalancerHealthCheck, LoadBalancerHealtCheckHttp
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
with open("../token.txt", "r") as file:
|
|
|
|
api_token = file.read().strip()
|
|
|
|
|
|
|
|
with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file:
|
|
|
|
ssh_key = file.readline()
|
|
|
|
|
|
|
|
client = Client(token=api_token)
|
|
|
|
|
|
|
|
PREFIX = "s444498"
|
|
|
|
|
|
|
|
try:
|
|
|
|
ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key-2", public_key=ssh_key)
|
|
|
|
print(f"Key {ssh_key.data_model.name} created: {ssh_key.data_model.public_key}")
|
|
|
|
except:
|
|
|
|
ssh_key = client.ssh_keys.get_by_name(f"{PREFIX}-pzc-ssh-key-2")
|
|
|
|
print(f"Key {ssh_key.data_model.name} already in use: {ssh_key.data_model.public_key}")
|
|
|
|
|
|
|
|
NUM_OF_SERVERS = 5
|
|
|
|
loadbalancer_targets = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
vnet = client.networks.create(
|
|
|
|
name=f"{PREFIX}-pzc-test-vnet",
|
|
|
|
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: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
|
|
|
except:
|
|
|
|
vnet = client.networks.get_by_name(
|
|
|
|
f"{PREFIX}-pzc-test-vnet",
|
|
|
|
)
|
|
|
|
print(f"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
|
|
|
|
|
|
|
webservice_init=r'''
|
|
|
|
#cloud-config
|
|
|
|
runcmd:
|
|
|
|
- git clone https://git.wmi.amu.edu.pl/s444498/cloud
|
|
|
|
- cd Public_cloud
|
|
|
|
- chmod +x webservice
|
|
|
|
- ./webservice
|
|
|
|
'''
|
|
|
|
for i in range(NUM_OF_SERVERS):
|
|
|
|
webservice_server = client.servers.create(
|
|
|
|
name=f"{PREFIX}-webservice-{i+1}",
|
|
|
|
server_type=ServerType("cx11"),
|
2023-01-22 16:51:01 +01:00
|
|
|
image=Image(name="ubuntu-22.04"),
|
2023-01-22 15:03:37 +01:00
|
|
|
ssh_keys=[ssh_key],
|
|
|
|
networks=[vnet],
|
|
|
|
location=Location("hel1"),
|
|
|
|
user_data=webservice_init
|
|
|
|
)
|
|
|
|
|
|
|
|
webservice_server.action.wait_until_finished()
|
|
|
|
print(f"Creating webservice {i+1}: {webservice_server.action.complete}")
|
|
|
|
loadbalancer_targets.append(LoadBalancerTarget(type="server", server=webservice_server.server, use_private_ip=True))
|
|
|
|
|
|
|
|
try:
|
|
|
|
loadbalancer = client.load_balancers.create(
|
|
|
|
name= f"{PREFIX}-loadbalancer",
|
|
|
|
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(
|
2023-01-22 16:51:01 +01:00
|
|
|
path="/factors/32",
|
2023-01-22 15:03:37 +01:00
|
|
|
status_codes=["2??", "3??"],
|
|
|
|
tls=False
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
targets=loadbalancer_targets,
|
|
|
|
public_interface=True,
|
|
|
|
network=vnet
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
loadbalancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer")
|
|
|
|
|
|
|
|
loadbalancer.action.wait_until_finished()
|
|
|
|
print(f"Loadbalancer creating...: {loadbalancer.action.complete}")
|
|
|
|
|
|
|
|
load_balancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer")
|
|
|
|
print(f"Webserwis: http://{load_balancer.data_model.public_net.ipv4.ip}:8080/factors/")
|