commit c135d8256bb527913fbcbde8745ced50f2c33837 Author: Aleksandra Jonas Date: Sun Jan 29 21:31:48 2023 +0100 tym razem na hetznerze diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6147f6f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM ubuntu +COPY ./webservice / +RUN chmod +x ./webservice +EXPOSE 80:8080/tcp +CMD ./webservice \ No newline at end of file diff --git a/del.py b/del.py new file mode 100644 index 0000000..f014ba9 --- /dev/null +++ b/del.py @@ -0,0 +1,23 @@ +from hcloud import Client + +api_token = "" +with open("token.txt", "r") as file: + api_token = file.read().strip() + +client = Client( + token=api_token +) +INDEKS = "s444455" + +servers = client.servers.get_all() +for i in servers: + if i.data_model.name.startswith(INDEKS): + action = client.servers.delete(i) +print("Cleaning servers: done") + +load_balancers = client.load_balancers.get_all() + +for lb in load_balancers: + if lb.data_model.name.startswith(INDEKS): + action = client.load_balancers.delete(lb) +print(f'Cleaning load balanced: done') diff --git a/main.py b/main.py new file mode 100644 index 0000000..6778113 --- /dev/null +++ b/main.py @@ -0,0 +1,150 @@ +from hcloud import Client +from hcloud.networks.domain import NetworkSubnet +from hcloud.locations.domain import Location +from hcloud.images.domain import Image +from hcloud.server_types.domain import ServerType +from hcloud.load_balancers.domain import ( + LoadBalancerService, + LoadBalancerServiceHttp, + LoadBalancerHealthCheck, + LoadBalancerHealtCheckHttp, + LoadBalancerTarget +) +from hcloud.load_balancer_types.domain import LoadBalancerType + + +INDEKS = "s444391" + +hetzner_token = "KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23" +ssh_name = f"{INDEKS}-key" +vnet_name = f"{INDEKS}-pzc-vnet" +serwer_1_name = f"{INDEKS}-serwer-1" +serwer_2_name = f"{INDEKS}-serwer-2" +load_balancer_name = f"{INDEKS}-load-balancer" + + +with open("token.txt", "r") as file: + hetzner_token = file.read().strip() + +client = Client(token=hetzner_token) +""" +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') + ] + ) +""" +vnet = client.networks.get_by_name(vnet_name) +print("Vnet: done") + +#ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_key_text) +ssh_key = client.ssh_keys.get_by_name(ssh_name) +print("Key: done") + +server = 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/s444391/hetzner-chmurki.git + - cd hetzner-chmurki + - docker build -t webservice . + - docker run -d -p 80:8080 -t webservice +''' + + +serwer1 = client.servers.create( + name=serwer_1_name, + server_type=ServerType("cx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key], + networks=[vnet], + location=Location("hel1"), + user_data=server +) + +serwer1.action.wait_until_finished() +serwer1 = client.servers.get_by_name(serwer_1_name) +print("Serwer1: done") + + +serwer2 = client.servers.create( + name=serwer_2_name, + server_type=ServerType("cx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key], + networks=[vnet], + location=Location("hel1"), + user_data=server +) + +serwer2.action.wait_until_finished() +serwer2 = client.servers.get_by_name(serwer_2_name) +print("Serwer2: done") + +load_balancer = client.load_balancers.create( + name=load_balancer_name, + load_balancer_type=LoadBalancerType(name='lb11'), + location=Location('hel1'), + network=vnet, + targets=[ + LoadBalancerTarget( + type='server', + server=serwer1, + use_private_ip=True, + ), + LoadBalancerTarget( + type='server', + server=serwer2, + use_private_ip=True, + ) + ], + services=[ + LoadBalancerService( + protocol='http', + listen_port=8080, + destination_port=8080, + health_check=LoadBalancerHealthCheck( + protocol='http', + port=8080, + 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() +if load_balancer.action.complete: + load_balancer = client.load_balancers.get_by_name(load_balancer_name) + print(f"Load balancer: http://{load_balancer.data_model.public_net.ipv4.ip}:8080") \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..929a50d --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash +pip3 install hcloud + +python3 del.py +python3 main.py \ No newline at end of file diff --git a/webservice b/webservice new file mode 100644 index 0000000..afd2204 Binary files /dev/null and b/webservice differ