434708-PZC-4.3/deploy.py

156 lines
4.7 KiB
Python
Raw Permalink Normal View History

2022-01-05 00:38:14 +01:00
import sys
from constants import *
from hcloud import Client
from hcloud.locations.domain import Location
from hcloud.networks.domain import NetworkSubnet
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
from hcloud.load_balancer_types.domain import LoadBalancerType
from hcloud.load_balancers.domain import LoadBalancerAlgorithm
from hcloud.load_balancers.domain import LoadBalancerService
from hcloud.load_balancers.domain import LoadBalancerServiceHttp
from hcloud.load_balancers.domain import LoadBalancerHealthCheck
from hcloud.load_balancers.domain import LoadBalancerHealtCheckHttp
from hcloud.load_balancers.domain import LoadBalancerTarget
with open(SSH_KEY_FNAME, "r") as file:
ssh_key_text = file.read()
with open(CLOUD_INIT_FNAME, "r") as file:
cloud_init = file.read()
if len(sys.argv) < 2:
raise ValueError("API key must be provided as an argument!")
client = Client(
token=str(sys.argv[1])
)
# ssh key
if client.ssh_keys.get_by_name(SSH_KEY_NAME):
ssh_key = client.ssh_keys.get_by_name(SSH_KEY_NAME)
else:
ssh_key = client.ssh_keys.create(name=SSH_KEY_NAME, public_key=ssh_key_text)
print(f"Klucz {ssh_key.data_model.name} został utworzony.")
# network
if client.networks.get_by_name(VNET_NAME):
vnet = client.networks.get_by_name(VNET_NAME)
else:
vnet = client.networks.create(
name=VNET_NAME,
ip_range=IP_RANGE,
subnets=[
NetworkSubnet(ip_range=IP_RANGE, network_zone=NETWORK_ZONE, type=NETWORK_TYPE)
]
)
print(f"Utworzono sieć wirtualną: {vnet.data_model.name} ({vnet.data_model.ip_range})")
# webservice_1
if(client.servers.get_by_name(SERVER_NAME_1)):
webservice_1 = client.servers.get_by_name(SERVER_NAME_1)
else:
webservice_1_response = client.servers.create(
name=SERVER_NAME_1,
server_type=ServerType(MACHINE_TYPE),
image=Image(name=MACHINE_OS),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location(LOCATION),
user_data=cloud_init
)
webservice_1_response.action.wait_until_finished()
webservice_1 = webservice_1_response.server
print(f"Utworzono webservice 1: {webservice_1.data_model.name} ({webservice_1.data_model.public_net.ipv4.ip})")
# webservice_2
if(client.servers.get_by_name(SERVER_NAME_2)):
webservice_2 = client.servers.get_by_name(SERVER_NAME_2)
else:
webservice_2_response = client.servers.create(
name=SERVER_NAME_2,
server_type=ServerType(MACHINE_TYPE),
image=Image(name=MACHINE_OS),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location(LOCATION),
user_data=cloud_init
)
webservice_2_response.action.wait_until_finished()
webservice_2 = webservice_2_response.server
print(f"Utworzono webservice 2: {webservice_2.data_model.name} ({webservice_2.data_model.public_net.ipv4.ip})")
# load balance service health check http
lb_health_check_http = LoadBalancerHealtCheckHttp(
path="/factors/10",
status_codes=["2??", "3??"],
tls=False,
)
# load balance service health check
lb_health_check = LoadBalancerHealthCheck(
protocol="http",
port="8080",
interval=10,
timeout=6,
retries=3,
http=lb_health_check_http
)
# load balance http
lb_service_http = LoadBalancerServiceHttp(
cookie_name="HCLBSTICKY",
cookie_lifetime="300",
certificates=[],
redirect_http=False,
sticky_sessions=False,
)
# load balancer service
lb_service = LoadBalancerService(
protocol="http",
listen_port="8080",
destination_port="8080",
proxyprotocol=False,
health_check=lb_health_check,
http=lb_service_http
)
# webservice_1 target
webservice_1_target = LoadBalancerTarget(
type="server",
server=webservice_1,
use_private_ip=True
)
# webservice_2 target
webservice_2_target = LoadBalancerTarget(
type="server",
server=webservice_2,
use_private_ip=True
)
# load balancer
if(client.load_balancers.get_by_name(LOAD_BALANCER_NAME)):
load_balancer = client.load_balancers.get_by_name(LOAD_BALANCER_NAME)
else:
load_balancer_response = client.load_balancers.create(
name=LOAD_BALANCER_NAME,
load_balancer_type=LoadBalancerType(name=LB_TYPE),
location=Location(LOCATION),
algorithm=LoadBalancerAlgorithm(LOAD_BALANCER_ALG),
services=[lb_service],
targets=[webservice_1_target, webservice_2_target],
public_interface=True,
network=vnet
)
load_balancer_response.action.wait_until_finished()
load_balancer = client.load_balancers.get_by_name(LOAD_BALANCER_NAME)
print(f"Utworzono load balancer: {load_balancer.data_model.name} ({load_balancer.data_model.public_net.ipv4.ip}:8080)")