164 lines
5.9 KiB
Python
164 lines
5.9 KiB
Python
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_balancers.domain import (
|
|
LoadBalancerService,
|
|
LoadBalancerServiceHttp,
|
|
LoadBalancerHealthCheck,
|
|
LoadBalancerHealtCheckHttp,
|
|
LoadBalancerTarget
|
|
)
|
|
from hcloud.load_balancer_types.domain import LoadBalancerType
|
|
|
|
YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdsckgNysKURgUyf5lQ5k+o48dXF1wQcG2BdQNvXox1ri+dGdEG4peVqgRNqlfiVeoE7Qx413v1ZvOA+MPEy6/HveHQVgth0jQpWwuwxA3FhIsMAyJZjXb1oT+/fChR4gk1xkHvulRj5k5uYyJ0heXOGJc30KN3HqB7+iMKs5wf7tjCVOHfiGdQdSVNKs6OKr44MuwLil48kc+fJ+Mj5Zlo10qMUEGTxre8grYTMEUMAEfqM1s8sj+Ru+LNC3OJg2ernDj+bCCFalGj3P3F4pPqXPGNkwFitby3szcYiGPHEWhxypmBhwfsh6FHBfN2nTiwvHa13hb6PanWVbRTmORHbF5AkXaxWL+DbdXFp+3w8xn9nagdICB66eTwIa9y+4H9R/4O1xytxrDe96vEjaTl3jRG/z+OGvyRq6p8fax1ZOc1q0KjRFnr6RrL1p9IcNmsGF5iSYTNn1dmckVOXQa9eFkTj8gbZQhx4aDGF74vAKm1pwfbTrZVmd3XwiPPGUpKq4hrqDLdK5erzjEf6c1tVd7tA2uo7YRfBMuu3aPWqJQiJkjssPdGIPR4A9H6/lS7H5x1KnYC4zYHaiEhst9N5l7Q7RCnHfaqB1oK1NRcYnYD/j1v5hsRdcxzl58KK5WGfFsawArQRuHt5QsAwJTrMbgMUt0yF+GRYTPeGM3mQ== olczig@MacBook-Air-Aleksandra.local"
|
|
|
|
client = Client(
|
|
token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23")
|
|
|
|
ID = "s444376"
|
|
|
|
servers = client.servers.get_all()
|
|
for s in servers:
|
|
if s.data_model.name.startswith(ID):
|
|
action = client.servers.delete(s)
|
|
print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
|
|
|
|
load_balancers = client.load_balancers.get_all()
|
|
|
|
for lb in load_balancers:
|
|
if lb.data_model.name.startswith(ID):
|
|
action = client.load_balancers.delete(lb)
|
|
print(f'Cleaning load balanced: done')
|
|
|
|
|
|
vnet_name = f"{ID}-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"Sieć: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
|
|
|
ssh_name = f"{ID}-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=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/s444376/DPZC_Ola.git
|
|
- cd DPZC_Ola/Zadanie_4_3
|
|
- docker build -t webservice .
|
|
- docker run -d -p 80:8080 -t webservice
|
|
'''
|
|
|
|
|
|
|
|
server1 = client.servers.create(
|
|
name=f"{ID}-serv1",
|
|
server_type=ServerType("cx11"),
|
|
image=Image(name="ubuntu-20.04"),
|
|
ssh_keys=[ssh_key],
|
|
networks=[vnet],
|
|
location=Location("hel1"),
|
|
user_data=cloud_init
|
|
)
|
|
|
|
server1.action.wait_until_finished()
|
|
print(f"Serwer1: {server1.action.complete}")
|
|
|
|
server1 = client.servers.get_by_name(f"{ID}-serv1")
|
|
print(f"Serwer1: {server1.data_model.name}\n\tpubliczne IP: {server1.data_model.public_net.ipv4.ip}\n\tprywatne IP: {server1.data_model.private_net[0].ip}")
|
|
|
|
server2 = client.servers.create(
|
|
name=f"{ID}-serv2",
|
|
server_type=ServerType("cx11"),
|
|
image=Image(name="ubuntu-20.04"),
|
|
ssh_keys=[ssh_key],
|
|
networks=[vnet],
|
|
location=Location("hel1"),
|
|
user_data=cloud_init
|
|
)
|
|
|
|
server2.action.wait_until_finished()
|
|
print(f"Serwer2: {server2.action.complete}")
|
|
|
|
server2 = client.servers.get_by_name(f"{ID}-serv2")
|
|
print(f"Serwer2: {server2.data_model.name}\n\tpubliczne IP: {server2.data_model.public_net.ipv4.ip}\n\tprywatne IP: {server2.data_model.private_net[0].ip}")
|
|
|
|
load_balancer = client.load_balancers.create(
|
|
name=f"{ID}-loadbalancer",
|
|
load_balancer_type=LoadBalancerType(name='lb11'),
|
|
location=Location('hel1'),
|
|
network=vnet,
|
|
targets=[
|
|
LoadBalancerTarget(
|
|
type='server',
|
|
server=server1,
|
|
use_private_ip=True,
|
|
),
|
|
LoadBalancerTarget(
|
|
type='server',
|
|
server=server2,
|
|
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(f"{ID}-loadbalancer")
|
|
print(f"Load balancer: http://{load_balancer.data_model.public_net.ipv4.ip}:8080") |