i
This commit is contained in:
commit
bf5c2e4731
2
1 hetzner/.gitignore
vendored
Normal file
2
1 hetzner/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
venv/
|
||||
config.json
|
8
1 hetzner/config_base.json
Normal file
8
1 hetzner/config_base.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"ssh_name": "s434784",
|
||||
"server-type": "cx11",
|
||||
"server-image": "ubuntu-20.04",
|
||||
"location": "hel1",
|
||||
"ssh_private": "<FILL>",
|
||||
"ssh_public": "<FILL>",
|
||||
}
|
110
1 hetzner/deploy.py
Normal file
110
1 hetzner/deploy.py
Normal file
@ -0,0 +1,110 @@
|
||||
import json
|
||||
import time
|
||||
import sys
|
||||
import paramiko
|
||||
import requests
|
||||
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.volumes.domain import Volume
|
||||
|
||||
|
||||
class Client_MS:
|
||||
def __init__(self, json_config) -> None:
|
||||
self.config = self.load_config(json_config)
|
||||
|
||||
self.client = self.get_client()
|
||||
self.ssh_key = self.get_ssh()
|
||||
|
||||
self.vscode = self.create_vscode_server()
|
||||
|
||||
def load_config(self, json_config):
|
||||
with open(json_config) as json_file:
|
||||
return json.load(json_file)
|
||||
|
||||
def get_client(self):
|
||||
try:
|
||||
return Client(token=self.config["token"])
|
||||
except:
|
||||
return None
|
||||
|
||||
def get_ssh(self):
|
||||
if(self.client.ssh_keys.get_by_name(self.config["ssh_name"])):
|
||||
print("SSH_KEY: Key exists.")
|
||||
return self.client.ssh_keys.get_by_name(self.config["ssh_name"])
|
||||
else:
|
||||
print("SSH_KEY: Creating ssh_key.")
|
||||
return self.client.ssh_keys.create(name=self.config["ssh_name"], public_key=self.config["ssh_public"])
|
||||
|
||||
def create_vscode_server(self):
|
||||
with open("vscode.yml", "r") as f:
|
||||
vs = f.read()
|
||||
|
||||
if(self.client.servers.get_by_name(self.config["vs-code-name"])):
|
||||
print("Server exists.")
|
||||
self.vscode = self.client.servers.get_by_name(
|
||||
self.config["vs-code-name"])
|
||||
return self.vscode
|
||||
else:
|
||||
print("Creating server.")
|
||||
vscode = self.client.servers.create(
|
||||
name=self.config["vs-code-name"],
|
||||
server_type=ServerType(self.config["server-type"]),
|
||||
image=Image(name=self.config["server-image"]),
|
||||
ssh_keys=[self.ssh_key],
|
||||
location=Location(self.config["location"]),
|
||||
user_data=vs)
|
||||
vscode.action.wait_until_finished()
|
||||
self.vscode = vscode.server
|
||||
return vscode.server
|
||||
|
||||
def get_password(self):
|
||||
while(True):
|
||||
try:
|
||||
print("Status: Setting up...", end='\r')
|
||||
requests.get(f'http://{self.vscode.data_model.public_net.ipv4.ip}:8080')
|
||||
break
|
||||
except:
|
||||
time.sleep(1)
|
||||
print("Status: Running. ", end='\n')
|
||||
ssh = paramiko.SSHClient()
|
||||
k = paramiko.RSAKey.from_private_key_file(self.config["ssh_private"])
|
||||
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect( hostname=self.vscode.data_model.public_net.ipv4.ip, username="root", pkey=k, timeout=1000)
|
||||
stdin, stdout, stderr = ssh.exec_command('cat < ~/.config/code-server/config.yaml')
|
||||
print(f'VS Code password: {stdout.readlines()[2].split()[1]}\n')
|
||||
|
||||
def print_info(self):
|
||||
print("----- Server info -----")
|
||||
print(
|
||||
f"\t{self.vscode.data_model.name}\n\t\tIP: {self.vscode.data_model.public_net.ipv4.ip}")
|
||||
print(
|
||||
f"\nTo access vscode go to: http:\\\\{self.vscode.data_model.public_net.ipv4.ip}:8080")
|
||||
|
||||
def delete_all(self):
|
||||
self.vscode.delete()
|
||||
print("Deleted.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if(len(sys.argv) != 2):
|
||||
print("Podaj ścieżkę do pliku konfiguracyjnego.")
|
||||
else:
|
||||
c = Client_MS(sys.argv[1])
|
||||
c.print_info()
|
||||
c.get_password()
|
||||
try:
|
||||
for remaining in range(60, 0, -1):
|
||||
sys.stdout.write("\r")
|
||||
sys.stdout.write(
|
||||
"Deleting server starts in: {:2d}. Press Ctrl + C to stop.".format(remaining))
|
||||
sys.stdout.flush()
|
||||
time.sleep(1)
|
||||
print("")
|
||||
except KeyboardInterrupt:
|
||||
sys.exit()
|
||||
|
||||
c.delete_all()
|
BIN
1 hetzner/requirements.txt
Normal file
BIN
1 hetzner/requirements.txt
Normal file
Binary file not shown.
BIN
1 hetzner/webservice
Normal file
BIN
1 hetzner/webservice
Normal file
Binary file not shown.
11
1 hetzner/webservice.yml
Normal file
11
1 hetzner/webservice.yml
Normal file
@ -0,0 +1,11 @@
|
||||
#cloud-config
|
||||
write_files:
|
||||
- content: |
|
||||
curl -fsSL https://code-server.dev/install.sh > /root/install.sh
|
||||
permissions: "755"
|
||||
owner: root:root
|
||||
path: /root/install.sh
|
||||
|
||||
runcmd:
|
||||
- bash /root/install.sh
|
||||
- code-server --bind-addr 0.0.0.0:8080
|
138
Sprawdzarka1/main.py
Normal file
138
Sprawdzarka1/main.py
Normal file
@ -0,0 +1,138 @@
|
||||
import requests
|
||||
import random
|
||||
import math
|
||||
import time
|
||||
import threading
|
||||
import logging
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
|
||||
|
||||
API_URL = "http://95.216.192.77: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()
|
Loading…
Reference in New Issue
Block a user