zad 4 hetzner

This commit is contained in:
korne 2023-01-15 21:14:09 +01:00
commit 09acf08bb5
6 changed files with 199 additions and 0 deletions

19
hetzner/README.md Normal file
View File

@ -0,0 +1,19 @@
# PZC - zadanie 4
**Clone repository:**
`git@git.wmi.amu.edu.pl:s478815/PZC.git`
`cd PZC/zadania4/hetzner`\
**Run the script:**
`
chmod +x run.sh
`
`
./run.sh
`
**To delete:**
`
chmod +x run_delete.sh
`
`
./run_delete.sh
`

146
hetzner/create.py Normal file
View File

@ -0,0 +1,146 @@
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"
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/s478815/PZC.git
- cd PZC/zadania4
- docker build -t webservice .
- docker run -d -p 80:8080 -t webservice
'''
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")
web_service1 = client.servers.create(
name=f"{PREFIX}-webservice1",
server_type=ServerType("cx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init
)
web_service1.action.wait_until_finished()
print(f"Tworzenie serwera web service 1: {web_service1.action.complete}")
web_service1 = client.servers.get_by_name(f"{PREFIX}-webservice1")
print(f"Serwer: {web_service1.data_model.name}\n\tpubliczne IP: {web_service1.data_model.public_net.ipv4.ip}\n\tprywatne IP: {web_service1.data_model.private_net[0].ip}")
web_service2 = client.servers.create(
name=f"{PREFIX}-webservice2",
server_type=ServerType("cx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init
)
web_service2.action.wait_until_finished()
print(f"Tworzenie serwera web service 2: {web_service2.action.complete}")
web_service2 = client.servers.get_by_name(f"{PREFIX}-webservice2")
print(f"Serwer: {web_service2.data_model.name}\n\tpubliczne IP: {web_service2.data_model.public_net.ipv4.ip}\n\tprywatne IP: {web_service2.data_model.private_net[0].ip}")
load_balancer = client.load_balancers.create(
name=f'{PREFIX}-loadbalancer',
load_balancer_type=LoadBalancerType(name='lb11'),
location=Location('hel1'),
network=vnet,
targets=[
LoadBalancerTarget(
type='server',
server=web_service1,
use_private_ip=True,
),
LoadBalancerTarget(
type='server',
server=web_service2,
use_private_ip=True,
)
],
services=[
LoadBalancerService(
protocol='http',
listen_port=80,
destination_port=80,
health_check=LoadBalancerHealthCheck(
protocol='http',
port=80,
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()
print(f"Tworzenie load balancera: {load_balancer.action.complete}")

26
hetzner/delete.py Normal file
View File

@ -0,0 +1,26 @@
from hcloud import Client
from hcloud.load_balancers.domain import (
LoadBalancerService,
LoadBalancerServiceHttp,
LoadBalancerHealthCheck,
LoadBalancerHealtCheckHttp,
LoadBalancerTarget
)
from hcloud.load_balancer_types.domain import LoadBalancerType
client = Client(
token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23"
)
PREFIX = "478815"
servers = client.servers.get_all()
for s in servers:
if s.data_model.name.startswith(PREFIX):
action = client.servers.delete(s)
print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
loadbalancers = client.load_balancers.get_all()
for l in loadbalancers:
if l.data_model.name.startswith(PREFIX):
action = client.load_balancers.delete(l)
print(f"Usuwanie load balancera")

4
hetzner/run.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
pip install hcloud
python create.py

4
hetzner/run_delete.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
pip install hcloud
python delete.py

BIN
webservice Normal file

Binary file not shown.