4
This commit is contained in:
commit
9f82dcc821
1
API_key.txt
Normal file
1
API_key.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
v7PmTeBj1atesVQLLVZcUSunsCTibWKcx0J33A2b1hesuWNavMrRSRuREJOPejDT
|
7
cloud_init.yaml
Normal file
7
cloud_init.yaml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#cloud-config
|
||||||
|
runcmd:
|
||||||
|
- cd /home
|
||||||
|
- git clone https://git.wmi.amu.edu.pl/s434742/DPZC-4
|
||||||
|
- chmod +x /home/webservice
|
||||||
|
- cp /home/webservice.service /etc/systemd/system/webservice.service
|
||||||
|
- systemctl start webservice
|
176
deploy_hetzner.py
Normal file
176
deploy_hetzner.py
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
from hcloud import Client
|
||||||
|
from hcloud.networks.domain import NetworkSubnet
|
||||||
|
from hcloud.locations.domain import Location
|
||||||
|
from hcloud.server_types.domain import ServerType
|
||||||
|
from hcloud.images.domain import Image
|
||||||
|
from hcloud.volumes.domain import Volume
|
||||||
|
from hcloud.load_balancer_types.domain import LoadBalancerType
|
||||||
|
from hcloud.load_balancers.domain import LoadBalancerAlgorithm, LoadBalancerServiceHttp, LoadBalancerHealthCheck, LoadBalancerService, LoadBalancerHealtCheckHttp, LoadBalancerTarget
|
||||||
|
|
||||||
|
class HetzerDeployment:
|
||||||
|
|
||||||
|
KEY_NAME = "patrycjalazna@patrycjalazna.local"
|
||||||
|
client = None
|
||||||
|
ssh_key = None
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, name, server_type = "cx11", image = "ubuntu-20.04", location = "hel1"):
|
||||||
|
self.name = name
|
||||||
|
self.server_type = server_type
|
||||||
|
self.image = image
|
||||||
|
self.location = location
|
||||||
|
|
||||||
|
self.client = self.create_client()
|
||||||
|
self.ssh_key = self.get_ssh_key()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_ssh_key(self):
|
||||||
|
with open("ssh_key.txt", "r") as f:
|
||||||
|
ssh_pub_key = f.read()
|
||||||
|
|
||||||
|
if self.client.ssh_keys.get_by_name(self.KEY_NAME):
|
||||||
|
ssh_key = self.client.ssh_keys.get_by_name(self.KEY_NAME)
|
||||||
|
|
||||||
|
else:
|
||||||
|
ssh_key = self.client.ssh_keys.create(name=self.KEY_NAME, public_key=ssh_pub_key)
|
||||||
|
|
||||||
|
return ssh_key
|
||||||
|
|
||||||
|
|
||||||
|
def get_auth_token(self):
|
||||||
|
with open("API_key.txt", "r") as f:
|
||||||
|
API_key = f.read()
|
||||||
|
|
||||||
|
return API_key
|
||||||
|
|
||||||
|
def create_client(self):
|
||||||
|
API_key = self.get_auth_token()
|
||||||
|
|
||||||
|
client = Client(token=API_key)
|
||||||
|
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def create_network(self):
|
||||||
|
|
||||||
|
vnet = self.client.networks.create(
|
||||||
|
name= self.name + "-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})")
|
||||||
|
|
||||||
|
return vnet
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def create_server(self, vnet, server_name, filename, num, volume=None):
|
||||||
|
|
||||||
|
with open(filename, "r") as f:
|
||||||
|
cloud_init = f.read()
|
||||||
|
|
||||||
|
name = self.name + server_name + str(num)
|
||||||
|
|
||||||
|
if volume is not None:
|
||||||
|
server = self.client.servers.create(
|
||||||
|
name=name,
|
||||||
|
server_type=ServerType(self.server_type),
|
||||||
|
image=Image(name=self.image),
|
||||||
|
ssh_keys=[self.ssh_key],
|
||||||
|
networks=[vnet],
|
||||||
|
location=Location(self.location),
|
||||||
|
user_data=cloud_init,
|
||||||
|
volumes=[volume.volume]
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
server = self.client.servers.create(
|
||||||
|
name=name,
|
||||||
|
server_type=ServerType(self.server_type),
|
||||||
|
image=Image(name=self.image),
|
||||||
|
ssh_keys=[self.ssh_key],
|
||||||
|
networks=[vnet],
|
||||||
|
location=Location(self.location),
|
||||||
|
user_data=cloud_init
|
||||||
|
)
|
||||||
|
|
||||||
|
server.action.wait_until_finished()
|
||||||
|
|
||||||
|
print(f"Tworzenie serwera {name}: {server.action.complete}")
|
||||||
|
|
||||||
|
server_ip = self.client.servers.get_by_name(name)
|
||||||
|
print(f"Serwer: {server_ip.data_model.name}\n\tpubliczne IP: {server_ip.data_model.public_net.ipv4.ip}")
|
||||||
|
|
||||||
|
return server.server
|
||||||
|
|
||||||
|
|
||||||
|
def create_load_balancer(self, servers, vnet, lb_type):
|
||||||
|
name = self.name + "-load-balancer"
|
||||||
|
|
||||||
|
targets = []
|
||||||
|
for s in servers:
|
||||||
|
targets.append(LoadBalancerTarget(
|
||||||
|
type="server",
|
||||||
|
server=s,
|
||||||
|
use_private_ip=True
|
||||||
|
))
|
||||||
|
|
||||||
|
load_balancer = self.client.load_balancers.create(
|
||||||
|
name=name,
|
||||||
|
load_balancer_type=LoadBalancerType(name=lb_type),
|
||||||
|
location=Location(self.location),
|
||||||
|
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=targets,
|
||||||
|
public_interface=True,
|
||||||
|
network=vnet
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
load_balancer.action.wait_until_finished()
|
||||||
|
print(f"Tworzenie load balancera {name}: {load_balancer.action.complete}")
|
||||||
|
|
||||||
|
load_balancer = self.client.load_balancers.get_by_name(name)
|
||||||
|
print(f"Load balancer: {load_balancer.data_model.name}\n\tpubliczne IP: {load_balancer.data_model.public_net.ipv4.ip}")
|
||||||
|
|
||||||
|
return load_balancer
|
||||||
|
|
||||||
|
|
||||||
|
def deploy(self):
|
||||||
|
vnet = self.create_network()
|
||||||
|
servers = []
|
||||||
|
for i in range(2):
|
||||||
|
servers.append(self.create_server(vnet, "prime", "cloud_init.yaml", i))
|
||||||
|
|
||||||
|
load_balancer = self.create_load_balancer(servers, vnet, "lb11")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
h = HetzerDeployment('patlaz---')
|
||||||
|
h.deploy()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
hcloud==1.16.0
|
128
script.py
Normal file
128
script.py
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
import requests
|
||||||
|
import random
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import logging
|
||||||
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
|
API_URL="http://95.217.174.114: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()
|
38
ssh_key.txt
Normal file
38
ssh_key.txt
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||||
|
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
|
||||||
|
NhAAAAAwEAAQAAAYEA1OImW8Y3tqM0k/+KN4/0oSX4Fs1o0jzGk2+LBumz4Cu5rBevDrGL
|
||||||
|
vZL9NPBD6VWM7s+i1VKonhN2VdQloXsS2a2zIEZ96i0krJ4O2qLugTEAdzDND7laMNuxs0
|
||||||
|
7+mfFDHm4jdKL+cjcKj4dQG6I4t/UKPo/3rzsb1p8QeJDhoBMbNF0D7R/b1s/NdOP3y+M9
|
||||||
|
XCdtgGxQLOpsVArRFDiZkrKbHDriFeqEr4MrK8IaQX45u+/wZpDGmtIy3QsdMqu7MDjLSz
|
||||||
|
dVMk07eDufv7ajCeJbwatpjdyvR3DsPR3VkLYwdksmUejdbe0hJFZfbuxLC6NNM4ObGecT
|
||||||
|
12jFEajyyTRU8RnJ8UV904KP9t8r5C/5IE/z0iBCKy8KHwxEUr3hbZdJBslxokHi4YS7T/
|
||||||
|
ZaYyGDQfXGdKw2zl4T0EfOFbb419uRJl7EC6LU/ZHYPfTvpIerCAUKWAnU8HNZCbJhczVa
|
||||||
|
dKzYKqAmqIdBEgjJrzMY1vRc1790KANkCwRmV9AJAAAFmBgcalIYHGpSAAAAB3NzaC1yc2
|
||||||
|
EAAAGBANTiJlvGN7ajNJP/ijeP9KEl+BbNaNI8xpNviwbps+AruawXrw6xi72S/TTwQ+lV
|
||||||
|
jO7PotVSqJ4TdlXUJaF7EtmtsyBGfeot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8IFN5I8p0NWETfJvYoqiSWKDDDxtbwKkkHNxtL3FB3HVSs1ItTsAhKA8euSHF/8gMBJ
|
||||||
|
TYLcY2sH5gUHKfSfsJ7/kuYlh136BbkFAKTxgL726MvFwg3tnFUvh+l1uj9VO30aUIvBHw
|
||||||
|
ebwgY5DABZIY8AprAMa33mU35asHicRE/tU7ek2280kzm3+2uFfr3tnKJmtTcQOUMjT3ZQ
|
||||||
|
eINIs0Qu21Q1DBPlwdwjVEjjVohzAG6GWaTAHV5wA5KUHoh8sAAADBAOoGh57vDA1GIves
|
||||||
|
X5Mj5LwFbF4jLT1MEYbuv6LByZzwlx2qqqWqJKhY78/pG0Hn2MGMbfFRoU04BVBcUYlkic
|
||||||
|
dyBkREslfGdKgUnkKA7UFgnFdmoYTQ8ca8yVSj0mJShRoVHQibW1Id4Nwt6nTgsxrEJVlZ
|
||||||
|
rW0+vuKwjEr42hHepwJY+84Dlog9mJeOg20rc15akM4Wb22+Yacz1zzOUUztsHebl07yUF
|
||||||
|
gDakHxxfvZad77clzD0izJFlTRD5tonQAAAMEA6N9owZCFiUfF4Ck5RWw2Xl1es0co+Fqw
|
||||||
|
662sLxMWfXEzSzs5kp3u/UiuHM5IFumw+4/lXMCDHiD+vO2i+nt0UCR5MPzRf3NJ9TfI5K
|
||||||
|
DjwlOaMmp8bSvcYGIf65McpXLGuOP5K28RAh+daH3KiXeDWS25yl24Uxj1Px5snVda1kdD
|
||||||
|
A9RO86Z0AqO7ny+oXmta/4PfYFJoZrB9pEFIgNkj1GtKn8WMH9qa9Ycd07PGBY5buGkV0X
|
||||||
|
2tZ4OEu3Axj1tdAAAAIXBhdHJ5Y2phbGF6bmFAcGF0cnljamFsYXpuYS5sb2NhbAE=
|
||||||
|
-----END OPENSSH PRIVATE KEY-----
|
BIN
webservice
Normal file
BIN
webservice
Normal file
Binary file not shown.
9
webservice.service
Normal file
9
webservice.service
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=web service
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/home/hetzner_webservice/webservice
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
Loading…
Reference in New Issue
Block a user