From c56b6ac4e96c0bac239ebc663ce33f14407b8afa Mon Sep 17 00:00:00 2001 From: s444501 Date: Thu, 12 Jan 2023 17:27:41 +0100 Subject: [PATCH] webservice --- README.md | 4 +++- clean.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ clean.sh | 6 ++++++ deploy.sh | 7 +++++++ main.py | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 clean.py create mode 100644 clean.sh create mode 100644 deploy.sh diff --git a/README.md b/README.md index 577b710..9b2ff69 100644 --- a/README.md +++ b/README.md @@ -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 -,,,,,,,, \ No newline at end of file +Webserwis uruchamiany jest poprzez wywołanie pliku ``deploy.sh`` + +Webserwis usuwany jest przez wywołanie pliku ``clean.sh`` \ No newline at end of file diff --git a/clean.py b/clean.py new file mode 100644 index 0000000..e4fade8 --- /dev/null +++ b/clean.py @@ -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 diff --git a/clean.sh b/clean.sh new file mode 100644 index 0000000..377dba2 --- /dev/null +++ b/clean.sh @@ -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 diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..fe79ebc --- /dev/null +++ b/deploy.sh @@ -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 diff --git a/main.py b/main.py index db16161..717f9dc 100644 --- a/main.py +++ b/main.py @@ -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/")