webservice

This commit is contained in:
s444501 2023-01-12 17:27:41 +01:00
parent 04eaceed1f
commit c56b6ac4e9
5 changed files with 100 additions and 5 deletions

View File

@ -8,4 +8,6 @@ Upewnij się, że w katalogu domowym ``~/.ssh/`` posiadasz klucz ``id_ed25519.pu
Upewnij się, że zainstalowany jest ``Python3.9`` lub nowszy oraz ``pip``
## Uruchamianie
,,,,,,,,
Webserwis uruchamiany jest poprzez wywołanie pliku ``deploy.sh``
Webserwis usuwany jest przez wywołanie pliku ``clean.sh``

52
clean.py Normal file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env python3
import sys
from hcloud import Client
if len(sys.argv) > 1:
VERBOSE = True
else:
VERBOSE = False
with open('.credentials/token', 'r') as file:
client = Client(token=str(file.readline()).strip())
index = 's444501'
ssh_name = index + '-ssh'
network_name = index + '-network'
server_base_name = index + '-server'
load_balancer_name = index + '-lb'
SERVER_COUNT = 5
print("Clearing SSH key... ", end='') if VERBOSE else None
try:
action = client.ssh_keys.delete(client.ssh_keys.get_by_name(ssh_name))
action.wait_until_finished()
except AttributeError:
pass
print('\t\tdone.') if VERBOSE else None
print("Clearing network... ", end='') if VERBOSE else None
try:
action = client.networks.delete(client.networks.get_by_name(network_name))
action.wait_until_finished()
except AttributeError:
pass
print('\t\tdone.') if VERBOSE else None
print("Clearing servers... ", end='') if VERBOSE else None
for i in range(SERVER_COUNT):
try:
action = client.servers.delete(client.servers.get_by_name(server_base_name + '-' + str(i + 1)))
action.wait_until_finished()
except AttributeError:
pass
print('\tdone.') if VERBOSE else None
print("Clearing load balancer... ", end='') if VERBOSE else None
try:
action = client.servers.delete(client.load_balancers.get_by_name(load_balancer_name))
action.wait_until_finished()
except AttributeError:
pass
print('\tdone.') if VERBOSE else None

6
clean.sh Normal file
View File

@ -0,0 +1,6 @@
chmod +x clean.py
echo "Creating virtual environment..."
python3 -m venv venv
source venv/bin/activate
pip install -q hcloud
./clean.py 1

7
deploy.sh Normal file
View File

@ -0,0 +1,7 @@
chmod +x main.py clean.py
echo "Creating virtual environment..."
python3 -m venv venv
source venv/bin/activate
pip install -q hcloud
./clean.py
./main.py

36
main.py
View File

@ -3,6 +3,8 @@ from pathlib import Path
from hcloud import Client
from hcloud.images.domain import Image
from hcloud.load_balancer_types.domain import LoadBalancerType
from hcloud.load_balancers.domain import LoadBalancerTarget, LoadBalancerService, LoadBalancerHealthCheck
from hcloud.locations.domain import Location
from hcloud.networks.domain import NetworkSubnet
from hcloud.server_types.domain import ServerType
@ -17,6 +19,7 @@ index = 's444501'
ssh_name = index + '-ssh'
network_name = index + '-network'
server_base_name = index + '-server'
load_balancer_name = index + '-lb'
SERVER_COUNT = 5
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_raw)
@ -31,9 +34,12 @@ vnet = client.networks.create(
cloud_init = r'''#cloud-config
runcmd:
-
- git clone https://git.wmi.amu.edu.pl/s444501/chmury-loadbalancer.git
- cd chmury-loadbalancer
- chmod +x webservice
- ./webservice
'''
servers = []
lb_targets = []
for i in range(SERVER_COUNT):
server = client.servers.create(
name=server_base_name + '-' + str(i+1),
@ -46,6 +52,28 @@ for i in range(SERVER_COUNT):
)
server.action.wait_until_finished()
print(f"Tworzenie serwera: {server.action.complete}")
servers.append(server)
lb_targets.append(LoadBalancerTarget(type="server", server=server, use_private_ip=True))
print(f"Webserwis pod adresem http://{server.server.data_model.public_net.ipv4.ip}:8080/factors/")
lb = client.load_balancers.create(
name=load_balancer_name,
load_balancer_type=LoadBalancerType(name="lb11"),
location=Location("hel1"),
services=[
LoadBalancerService(
protocol="http",
listen_port=8080,
destination_port=8080,
proxyprotocol=False
)
],
targets=lb_targets,
public_interface=True,
network=vnet
)
lb.action.wait_until_finished()
print(f"Mechanizm równoważenia obciążenia został utworzony: {lb.action.complete}")
print(f"Webserwis pod adresem http://{lb.data_model.public_net.ipv4.ip}:8080/factors/")