150 lines
4.3 KiB
Python
150 lines
4.3 KiB
Python
from hcloud import Client
|
|
from hcloud.networks.domain import NetworkSubnet
|
|
from hcloud.locations.domain import Location
|
|
from hcloud.images.domain import Image
|
|
from hcloud.server_types.domain import ServerType
|
|
from hcloud.load_balancers.domain import (
|
|
LoadBalancerService,
|
|
LoadBalancerServiceHttp,
|
|
LoadBalancerHealthCheck,
|
|
LoadBalancerHealtCheckHttp,
|
|
LoadBalancerTarget
|
|
)
|
|
from hcloud.load_balancer_types.domain import LoadBalancerType
|
|
|
|
|
|
INDEKS = "s444455"
|
|
|
|
hetzner_token = ""
|
|
ssh_name = f"{INDEKS}-key"
|
|
vnet_name = f"{INDEKS}-pzc-vnet"
|
|
serwer_1_name = f"{INDEKS}-serwer-1"
|
|
serwer_2_name = f"{INDEKS}-serwer-2"
|
|
load_balancer_name = f"{INDEKS}-load-balancer"
|
|
|
|
|
|
with open("token.txt", "r") as file:
|
|
hetzner_token = file.read().strip()
|
|
|
|
client = Client(token=hetzner_token)
|
|
"""
|
|
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')
|
|
]
|
|
)
|
|
"""
|
|
vnet = client.networks.get_by_name(vnet_name)
|
|
print("Vnet: done")
|
|
|
|
#ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_key_text)
|
|
ssh_key = client.ssh_keys.get_by_name(ssh_name)
|
|
print("Key: done")
|
|
|
|
server = r'''
|
|
#cloud-config
|
|
packages:
|
|
- apt-transport-https
|
|
- ca-certificates
|
|
- curl
|
|
- gnupg-agent
|
|
- software-properties-common
|
|
|
|
runcmd:
|
|
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
|
|
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
|
|
- apt-get update -y
|
|
- apt-get install -y docker-ce docker-ce-cli containerd.io
|
|
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
|
- chmod +x /usr/local/bin/docker-compose
|
|
- systemctl start docker
|
|
- systemctl enable docker
|
|
- git clone https://git.wmi.amu.edu.pl/s444455/DPZC_3.git
|
|
- cd DPZC_3/4
|
|
- docker build -t webservice .
|
|
- docker run -d -p 80:8080 -t webservice
|
|
'''
|
|
|
|
|
|
serwer1 = client.servers.create(
|
|
name=serwer_1_name,
|
|
server_type=ServerType("cx11"),
|
|
image=Image(name="ubuntu-20.04"),
|
|
ssh_keys=[ssh_key],
|
|
networks=[vnet],
|
|
location=Location("hel1"),
|
|
user_data=server
|
|
)
|
|
|
|
serwer1.action.wait_until_finished()
|
|
serwer1 = client.servers.get_by_name(serwer_1_name)
|
|
print("Serwer1: done")
|
|
|
|
|
|
serwer2 = client.servers.create(
|
|
name=serwer_2_name,
|
|
server_type=ServerType("cx11"),
|
|
image=Image(name="ubuntu-20.04"),
|
|
ssh_keys=[ssh_key],
|
|
networks=[vnet],
|
|
location=Location("hel1"),
|
|
user_data=server
|
|
)
|
|
|
|
serwer2.action.wait_until_finished()
|
|
serwer2 = client.servers.get_by_name(serwer_2_name)
|
|
print("Serwer2: done")
|
|
|
|
load_balancer = client.load_balancers.create(
|
|
name=load_balancer_name,
|
|
load_balancer_type=LoadBalancerType(name='lb11'),
|
|
location=Location('hel1'),
|
|
network=vnet,
|
|
targets=[
|
|
LoadBalancerTarget(
|
|
type='server',
|
|
server=serwer1,
|
|
use_private_ip=True,
|
|
),
|
|
LoadBalancerTarget(
|
|
type='server',
|
|
server=serwer2,
|
|
use_private_ip=True,
|
|
)
|
|
],
|
|
services=[
|
|
LoadBalancerService(
|
|
protocol='http',
|
|
listen_port=8080,
|
|
destination_port=8080,
|
|
health_check=LoadBalancerHealthCheck(
|
|
protocol='http',
|
|
port=8080,
|
|
interval=15,
|
|
timeout=10,
|
|
retries=3,
|
|
http=LoadBalancerHealtCheckHttp(
|
|
path='/factors/10',
|
|
status_codes=['2??', '3??'],
|
|
tls=False,
|
|
)
|
|
),
|
|
http=LoadBalancerServiceHttp(
|
|
cookie_name='HCLBSTICKY',
|
|
cookie_lifetime=300,
|
|
sticky_sessions=True,
|
|
certificates=[],
|
|
),
|
|
)
|
|
]
|
|
)
|
|
|
|
|
|
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: http://{load_balancer.data_model.public_net.ipv4.ip}:8080") |