dpzc/lab4/4.1-hetzner/start-hetzner.py
2023-01-10 23:25:25 +01:00

115 lines
3.6 KiB
Python

import os
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 LoadBalancerHealthCheck, LoadBalancerService, LoadBalancerHealtCheckHttp, LoadBalancerTarget
from dotenv import load_dotenv
load_dotenv(dotenv_path=".env")
api_token = os.getenv("hetzner_api_token")
client = Client(token=api_token)
PREFIX = "s444417"
YOUR_LOCAL_SSH_PUBKEY = os.getenv("hetzner_ssh_public_key")
vnet_name = f"{PREFIX}-pzc-test-vnet"
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}-pzc-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: {ssh_key.data_model.public_key}")
cloud_init_wp1=r'''#cloud-config
runcmd:
- cd /home
- git clone https://git.wmi.amu.edu.pl/s444417/dpzc
- chmod +x /home/dpzc/lab4/4.1-hetzner/webservice
- /home/dpzc/lab4/4.1-hetzner/webservice
'''
servers = []
for i in range(2):
name=f"{PREFIX}-service-{i+1}"
server = client.servers.create(
name=name,
server_type=ServerType("cx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_wp1
)
server.action.wait_until_finished()
print(f"Tworzenie serwera: {server.action.complete}")
server = client.servers.get_by_name(name)
print(f"Serwer: {server.data_model.name}\n\tpubliczne IP: {server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {server.data_model.private_net[0].ip}")
servers.append(server)
name = f"{PREFIX}-load-balancer"
targets = []
for s in servers:
targets.append(LoadBalancerTarget(
type="server",
server=s,
use_private_ip=True
))
load_balancer = client.load_balancers.create(
name=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=vnet
)
load_balancer.action.wait_until_finished()
print(f"Tworzenie load balancera {name}: {load_balancer.action.complete}")
load_balancer = client.load_balancers.get_by_name(name)
print(f"Load balancer: {load_balancer.data_model.name}\n\tpubliczne IP: {load_balancer.data_model.public_net.ipv4.ip}")