2023-01-15 21:14:09 +01:00
|
|
|
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_balancers.domain import (
|
|
|
|
LoadBalancerService,
|
|
|
|
LoadBalancerServiceHttp,
|
|
|
|
LoadBalancerHealthCheck,
|
|
|
|
LoadBalancerHealtCheckHttp,
|
|
|
|
LoadBalancerTarget
|
|
|
|
)
|
|
|
|
|
|
|
|
from hcloud.load_balancer_types.domain import LoadBalancerType
|
|
|
|
from hcloud import Client
|
|
|
|
|
|
|
|
client = Client(
|
|
|
|
token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23"
|
|
|
|
)
|
|
|
|
|
|
|
|
PREFIX = "478815"
|
|
|
|
YOUR_LOCAL_SSH_PUBKEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICZpOz3Rrdlg0daWESWGqSvMNSCrlROYU/Yy5/f+cPbh comment"
|
|
|
|
|
2023-01-25 16:25:06 +01:00
|
|
|
webservice_init=r'''#cloud-config
|
2023-01-15 21:14:09 +01:00
|
|
|
packages:
|
|
|
|
- apt-transport-https
|
|
|
|
- ca-certificates
|
|
|
|
- curl
|
|
|
|
- gnupg-agent
|
|
|
|
- software-properties-common
|
|
|
|
runcmd:
|
2023-01-25 16:25:06 +01:00
|
|
|
- curl -fsSL https://git.wmi.amu.edu.pl/s478815/pzc-4/raw/branch/master/webservice > /root/webservice
|
|
|
|
- cd /root/
|
|
|
|
- chmod +x webservice
|
|
|
|
- ./webservice
|
2023-01-15 21:14:09 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
vnet_name = f"{PREFIX}-network"
|
|
|
|
vnet = client.networks.get_by_name(vnet_name) or None
|
|
|
|
if not vnet:
|
|
|
|
vnet = client.networks.create(
|
|
|
|
name=vnet_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"Utworzono sieć wirtualną {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
|
|
|
|
|
|
|
ssh_name = f"{PREFIX}-ssh-key"
|
|
|
|
ssh_key = client.ssh_keys.get_by_name(ssh_name) or None
|
|
|
|
if not ssh_key:
|
|
|
|
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=YOUR_LOCAL_SSH_PUBKEY)
|
|
|
|
|
|
|
|
print(f"Klucz {ssh_key.data_model.name} został dodany")
|
|
|
|
|
2023-01-25 16:25:06 +01:00
|
|
|
loadbalancer_targets = []
|
2023-01-31 21:44:27 +01:00
|
|
|
servers_num = 5
|
|
|
|
|
|
|
|
for i in range(servers_num):
|
|
|
|
webservice_server = client.servers.create(
|
|
|
|
name=f"{PREFIX}-webservice-{i+1}",
|
|
|
|
server_type=ServerType("cx11"),
|
|
|
|
image=Image(name="ubuntu-20.04"),
|
|
|
|
ssh_keys=[ssh_key],
|
|
|
|
networks=[vnet],
|
|
|
|
location=Location("hel1"),
|
|
|
|
user_data=webservice_init
|
|
|
|
)
|
2023-01-25 16:25:06 +01:00
|
|
|
|
2023-01-31 21:44:27 +01:00
|
|
|
webservice_server.action.wait_until_finished()
|
|
|
|
print(f"Tworzenie serwera webservice {i+1}: {webservice_server.action.complete}")
|
|
|
|
loadbalancer_targets.append(LoadBalancerTarget(type="server", server=webservice_server.server, use_private_ip=True))
|
2023-01-15 21:14:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
load_balancer = client.load_balancers.create(
|
|
|
|
name=f'{PREFIX}-loadbalancer',
|
|
|
|
load_balancer_type=LoadBalancerType(name='lb11'),
|
|
|
|
location=Location('hel1'),
|
|
|
|
network=vnet,
|
|
|
|
services=[
|
2023-01-25 16:25:06 +01:00
|
|
|
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
|
2023-01-15 21:14:09 +01:00
|
|
|
)
|
|
|
|
),
|
2023-01-25 16:25:06 +01:00
|
|
|
)
|
|
|
|
],
|
|
|
|
targets=loadbalancer_targets,
|
|
|
|
public_interface=True
|
2023-01-15 21:14:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
load_balancer.action.wait_until_finished()
|
|
|
|
print(f"Tworzenie load balancera: {load_balancer.action.complete}")
|