diff --git a/3_2/token.txt b/3_2/token.txt new file mode 100644 index 0000000..38ab087 --- /dev/null +++ b/3_2/token.txt @@ -0,0 +1 @@ +KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23 \ No newline at end of file diff --git a/3_4/del.py b/3_4/del.py new file mode 100644 index 0000000..8871be5 --- /dev/null +++ b/3_4/del.py @@ -0,0 +1,16 @@ +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") diff --git a/3_4/main_vsc.py b/3_4/main_vsc.py new file mode 100644 index 0000000..46f89a0 --- /dev/null +++ b/3_4/main_vsc.py @@ -0,0 +1,71 @@ +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 + + +INDEKS = "s444455" + +hetzner_token = "" +ssh_name = f"{INDEKS}-key" +code_name = f"{INDEKS}-code" +vnet_name = f"{INDEKS}-pzc-vnet" + + +with open("token.txt", "r") as file: + hetzner_token = file.read().strip() + +client = Client(token=hetzner_token) + + +vnet = client.networks.get_by_name(vnet_name) or None +if not vnet: + 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") + ] + ) +print("Vnet: done") + + +ssh_key = client.ssh_keys.get_by_name(ssh_name) +print("Key: done") + +cloud_init_db=r''' +#cloud-config +write_files: + - content: | + curl -fsSL https://code-server.dev/install.sh > /root/install.sh + path: /root/install.sh + owner: root:root + - content: | + bind-addr: 0.0.0.0:8080 + auth: password + password: vscpassword + cert: false + path: /root/config.yml + owner: root:root + +runcmd: + - cd /root/ + - chmod u+x install.sh + - ./install.sh + - code-server --config config.yml +''' + +db_server = client.servers.create( + name=code_name, + server_type=ServerType("cx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key], + networks=[vnet], + location=Location("hel1"), + user_data=cloud_init_db +) + +db_server.action.wait_until_finished() +db_server = client.servers.get_by_name(code_name) +print("Serwer code: done") \ No newline at end of file diff --git a/3_4/token.txt b/3_4/token.txt new file mode 100644 index 0000000..38ab087 --- /dev/null +++ b/3_4/token.txt @@ -0,0 +1 @@ +KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23 \ No newline at end of file diff --git a/4/Dockerfile b/4/Dockerfile new file mode 100644 index 0000000..6147f6f --- /dev/null +++ b/4/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/4/del.py b/4/del.py new file mode 100644 index 0000000..8871be5 --- /dev/null +++ b/4/del.py @@ -0,0 +1,16 @@ +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") diff --git a/4/main.py b/4/main.py new file mode 100644 index 0000000..d2cf241 --- /dev/null +++ b/4/main.py @@ -0,0 +1,147 @@ +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 = "s444455" + +hetzner_token = "" +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") + +with open("ssh.txt", "r") as file: + ssh_key_text = file.read().strip() + +#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/s444455/DPZC_3.git + - cd DPZC_3/4 + - 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("Serwer1: 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=80, + destination_port=80, + health_check=LoadBalancerHealthCheck( + protocol='http', + port=80, + 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=[], + ) + ), + ] +) diff --git a/4/run.sh b/4/run.sh new file mode 100755 index 0000000..8004056 --- /dev/null +++ b/4/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/4/ssh.txt b/4/ssh.txt new file mode 100644 index 0000000..dc178d3 --- /dev/null +++ b/4/ssh.txt @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKZuvJc3h9m/SzYB3a/t7VEPpssXow6IF0F0QP6pMHnj mikolajpaterka@Mikoajs-MacBook-Air.local diff --git a/4/test_web.py b/4/test_web.py new file mode 100644 index 0000000..3475842 --- /dev/null +++ b/4/test_web.py @@ -0,0 +1,128 @@ +import requests +import random +import math +import time +import threading +import logging +logging.getLogger().setLevel(logging.INFO) + + +API_URL="http://localhost:8080" + + +UNIT = 5.0 # secs + +# Pre generated primes +first_primes_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, + 31, 37, 41, 43, 47, 53, 59, 61, 67, + 71, 73, 79, 83, 89, 97, 101, 103, + 107, 109, 113, 127, 131, 137, 139, + 149, 151, 157, 163, 167, 173, 179, + 181, 191, 193, 197, 199, 211, 223, + 227, 229, 233, 239, 241, 251, 257, + 263, 269, 271, 277, 281, 283, 293, + 307, 311, 313, 317, 331, 337, 347, 349] + + +def nBitRandom(n): + return random.randrange(2**(n-1)+1, 2**n - 1) + +def getLowLevelPrime(n): + '''Generate a prime candidate divisible + by first primes''' + while True: + # Obtain a random number + pc = nBitRandom(n) + + # Test divisibility by pre-generated + # primes + for divisor in first_primes_list: + if pc % divisor == 0 and divisor**2 <= pc: + break + else: return pc + +def isMillerRabinPassed(mrc): + '''Run 20 iterations of Rabin Miller Primality test''' + maxDivisionsByTwo = 0 + ec = mrc-1 + while ec % 2 == 0: + ec >>= 1 + maxDivisionsByTwo += 1 + assert(2**maxDivisionsByTwo * ec == mrc-1) + + def trialComposite(round_tester): + if pow(round_tester, ec, mrc) == 1: + return False + for i in range(maxDivisionsByTwo): + if pow(round_tester, 2**i * ec, mrc) == mrc-1: + return False + return True + + # Set number of trials here + numberOfRabinTrials = 20 + for i in range(numberOfRabinTrials): + round_tester = random.randrange(2, mrc) + if trialComposite(round_tester): + return False + return True + +def random_large_prime(bits): + while True: + prime_candidate = getLowLevelPrime(bits) + if not isMillerRabinPassed(prime_candidate): + continue + else: + return prime_candidate + +def thread_function(i, fast, timeout): + start = time.time() + + c = 5 # bits: 20: 200ms; 21: 350ms; 22: 700ms 23: 1.5s; 25: 6s; 26: 10s; 27: 24s + bits = 19 if fast else 23 + last_report = time.time() + processing_time = 0.0 + reqs = 0 + while True: + iter_start = time.time() + if iter_start - start > timeout: + logging.info("Thread: %d\treqs: %d\tmean time: %.3fs\t%s"%(i, reqs, processing_time/reqs if reqs>0 else 0.0, "fast\t" if fast else "")) + results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0 + return + if iter_start - last_report > UNIT/2: + if len(results[i])%2 == 0: + logging.info("Thread: %d\treqs: %d\tmean time: %.3fs\t%s"%(i, reqs, processing_time/reqs if reqs>0 else 0.0, "fast\t" if fast else "")) + results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0 + processing_time = 0.0 + reqs = 0 + last_report=iter_start + + factors = [random_large_prime(bits) for i in range(c)] + factors.sort() + n=math.prod(factors) + + r = requests.get(API_URL+'/factors/%d'%(n)) + if r.status_code != 200: + logging.error("wrong status code from webservice") + else: + result = r.json() + if result != factors: + logging.error("Wrong factors") + + processing_time+=time.time() - iter_start + reqs+=1 + time.sleep(0.5) + +START = time.time() +slow_threads = 4 + +results = [ {} for i in range(slow_threads+1)] + +t0 = threading.Thread(target=thread_function, args=(0, True, (5 + slow_threads*3) * UNIT)) +t0.start() +time.sleep(2 * UNIT) +for i in range(slow_threads): + t = threading.Thread(target=thread_function, args=(i+1, False, (slow_threads-i) * 3 * UNIT)) + t.start() + time.sleep(2 * UNIT) + +t0.join() \ No newline at end of file diff --git a/4/token.txt b/4/token.txt new file mode 100644 index 0000000..38ab087 --- /dev/null +++ b/4/token.txt @@ -0,0 +1 @@ +KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23 \ No newline at end of file diff --git a/4/webservice b/4/webservice new file mode 100644 index 0000000..afd2204 Binary files /dev/null and b/4/webservice differ