diff --git a/zad4/clean.py b/zad4/clean.py index e69de29..a5c4f89 100644 --- a/zad4/clean.py +++ b/zad4/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 = "s478873" + +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/zad4/deploy-webservice b/zad4/deploy-webservice deleted file mode 100644 index e69de29..0000000 diff --git a/zad4/deploy-webservice.py b/zad4/deploy-webservice.py new file mode 100644 index 0000000..10482c4 --- /dev/null +++ b/zad4/deploy-webservice.py @@ -0,0 +1,105 @@ +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 = "s478873" +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 +packages: + - apt-transport-https + - ca-certificates + - curl + - gnupg-agent + - software-properties-common +runcmd: + - curl -fsSL https://git.wmi.amu.edu.pl/s478873/DPZC-Hetzner/src/branch/master/zad4/webservice > /root/webservice + - cd /root/ + - 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/") + +