diff --git a/lab4/clean.py b/lab4/clean.py new file mode 100644 index 0000000..312132c --- /dev/null +++ b/lab4/clean.py @@ -0,0 +1,22 @@ +from hcloud import Client + +with open("token.txt", "r") as file: + api_token = file.read().strip() + +client = Client(token=api_token) + +PREFIX = "s478839" + +servers = client.servers.get_all() +print(f"Usuwanie {len(servers)} serwerów") +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}") + + +loadbalancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer") or None +if loadbalancer: + print("Usuwanie loadbalancera...") + action = client.load_balancers.delete(loadbalancer) + print("Usunięto loadbalancer") \ No newline at end of file diff --git a/lab4/webservice b/lab4/webservice new file mode 100644 index 0000000..afd2204 Binary files /dev/null and b/lab4/webservice differ diff --git a/lab4/webservice-init.py b/lab4/webservice-init.py new file mode 100644 index 0000000..530def6 --- /dev/null +++ b/lab4/webservice-init.py @@ -0,0 +1,102 @@ +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_balancer_types.domain import LoadBalancerType +from hcloud.load_balancers.domain import LoadBalancerTarget, LoadBalancerService, LoadBalancerHealthCheck, LoadBalancerHealtCheckHttp +from pathlib import Path + +with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file: + ssh_pub = file.readline() + +with open("token.txt", "r") as file: + api_token = file.read().strip() + +client = Client(token=api_token) + +PREFIX = "s478839" +NUM_OF_SERVERS = 5 + +loadbalancer_targets = [] + +ssh_name = f"{PREFIX}-pzc-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=f"{PREFIX}-pzc-ssh-key", public_key=ssh_pub) +print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}") + +vnet_name = f"{PREFIX}-pzc-test-vnet" +vnet = client.networks.get_by_name(vnet_name) or None +if not vnet: + vnet = client.networks.create( + name=f"{PREFIX}-pzc-test-vnet", + 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})") + +webservice_init=r''' +#cloud-config +runcmd: + - git clone https://git.wmi.amu.edu.pl/s478839/dpzc-hetzner.git + - cd lab4 + - chmod +x webservice + - ./webservice +''' +for i in range(NUM_OF_SERVERS): + webservice_server = client.servers.create( + name=f"{PREFIX}-webservice-{i+1}", + server_type=ServerType("cx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key], + networks=[vnet], + location=Location("hel1"), + user_data=webservice_init + ) + + webservice_server.action.wait_until_finished() + print(f"Tworzenie serwera webservice {i+1}: {webservice_server.action.complete}") + loadbalancer_targets.append(LoadBalancerTarget(type="server", server=webservice_server.server, use_private_ip=True)) + +loadbalancer = client.load_balancers.create( + name= f"{PREFIX}-loadbalancer", + load_balancer_type=LoadBalancerType(name="lb11"), + location=Location("hel1"), + services=[ + LoadBalancerService( + protocol="http", + listen_port=8080, + destination_port=8080, + proxyprotocol=False, + health_check=LoadBalancerHealthCheck( + protocol="http", + port="8080", + interval=15, + timeout=10, + retries=3, + http=LoadBalancerHealtCheckHttp( + path="/factors/10", + status_codes=["2??", "3??"], + tls=False + ) + ), + ) + ], + targets=loadbalancer_targets, + public_interface=True, + network=vnet +) + +loadbalancer.action.wait_until_finished() +print(f"Tworzenie loadbalancera: {loadbalancer.action.complete}") + +load_balancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer") +print(f"Adres webserwisu: http://{load_balancer.data_model.public_net.ipv4.ip}:8080/factors/") + + + + + diff --git a/readme.md b/readme.md index 51eadde..8e2471e 100644 --- a/readme.md +++ b/readme.md @@ -1,31 +1,48 @@ -# Zadanie 2 +# Lab 3 -## Wymagania +## Zadanie 2 + +### Wymagania Po sklonowaniu repozytorium do folderu dpzc-hetzner należy wstawić plik ```token.txt``` zawierający token dostępowy do Hetznera. Należy się również upewnić, że w folderze ```~/.ssh/ ``` znajduje się klucz publiczny ```id_ed25519.pub ``` -## Uruchomienie +### Uruchomienie ```bash git clone git@git.wmi.amu.edu.pl:s478839/dpzc-hetzner.git cd dpzc-hetzner sh deploy.sh ``` -# Zadanie 4 +## Zadanie 4 -## Uruchomienie +### Uruchomienie ```bash git clone git@git.wmi.amu.edu.pl:s478839/dpzc-hetzner.git cd dpzc-hetzner python3 cloud-init.py ``` -## Logowanie do VScode +### Logowanie do VScode W polu password należy umieścić hasło uzyskane przez: ```bash ssh root@ cat ~/.config/code-server/config.yaml ``` -## Usuwanie serwera +### Usuwanie serwera +```bash +python3 clean.py +``` +# Lab 4 + +## Zadanie 1 i 3 (4.1 i 4.3) + +### Uruchomienie +```bash +git clone git@git.wmi.amu.edu.pl:s478839/dpzc-hetzner.git +cd dpzc-hetzner/lab4 +python3 webservice-init.py +``` + +### Usuwanie zasobów ```bash python3 clean.py ``` \ No newline at end of file