This commit is contained in:
Maciej Sobkowiak 2022-01-05 02:27:16 +01:00
parent fdd68299db
commit 0e14842c11
9 changed files with 234 additions and 102 deletions

View File

@ -1,8 +0,0 @@
{
"ssh_name": "s434784",
"server-type": "cx11",
"server-image": "ubuntu-20.04",
"location": "hel1",
"ssh_private": "<FILL>",
"ssh_public": "<FILL>",
}

View File

@ -1,92 +0,0 @@
import json
import time
import sys
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("webservice.yml", "r") as f:
vs = f.read()
if(self.client.servers.get_by_name(self.config["server-name"])):
print("Server exists.")
self.vscode = self.client.servers.get_by_name(
self.config["server-name"])
return self.vscode
else:
print("Creating server.")
vscode = self.client.servers.create(
name=self.config["server-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 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()

View File

@ -4,10 +4,14 @@ import math
import time import time
import threading import threading
import logging import logging
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
logging.getLogger().setLevel(logging.INFO) logging.getLogger().setLevel(logging.INFO)
API_URL = "http://95.216.192.77:8080/" API_URL = " http://34.234.169.21:80/"
UNIT = 5.0 # secs UNIT = 5.0 # secs
@ -109,7 +113,8 @@ def thread_function(i, fast, timeout):
r = requests.get(API_URL+'/factors/%d' % (n)) r = requests.get(API_URL+'/factors/%d' % (n))
if r.status_code != 200: if r.status_code != 200:
logging.error("wrong status code from webservice") logging.error("wrong status code from webservice" +
str(r.status_code))
else: else:
result = r.json() result = r.json()
if result != factors: if result != factors:
@ -136,3 +141,13 @@ for i in range(slow_threads):
time.sleep(2 * UNIT) time.sleep(2 * UNIT)
t0.join() t0.join()
mu = 0
std = 1
for i, result in enumerate(results):
x = [(x - START)/UNIT for x in result.keys()]
y = result.values()
plt.plot(x, y, label="t%d"%(i,))
plt.legend()
plt.show()

14
Hetzner/config_base.json Normal file
View File

@ -0,0 +1,14 @@
{
"server-number": "3",
"server-name": "ms-server",
"server-type": "cx11",
"server-image": "ubuntu-20.04",
"location": "hel1",
"network": "ms-network",
"network-ip": "10.10.10.0/24",
"load-balancer-name": "ms-load-balancer",
"load-balancer-type": "lb11",
"ssh-name": "s434784",
"ssh-public": "<FILL>",
"token-hetzner": "<FILL>"
}

181
Hetzner/deploy.py Normal file
View File

@ -0,0 +1,181 @@
import json
import time
import sys
import requests
from hcloud import Client
from hcloud.load_balancer_types.domain import LoadBalancerType
from hcloud.load_balancers.domain import LoadBalancerAlgorithm, LoadBalancerServiceHttp, LoadBalancerHealthCheck, LoadBalancerService, LoadBalancerHealtCheckHttp, LoadBalancerTarget
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.network = self.create_network()
self.servers = []
for i in range(int(self.config["server-number"])):
self.servers.append(self.create_virual(i))
self.loadBalancer = self.create_load_balancer()
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-hetzner"])
except:
return None
def get_ssh(self):
if(self.client.ssh_keys.get_by_name(self.config["ssh-name"])):
print("SSHKEY: Key exists.")
return self.client.ssh_keys.get_by_name(self.config["ssh-name"])
else:
print("SSHKEY: Creating ssh_key.")
return self.client.ssh_keys.create(name=self.config["ssh-name"], public_key=self.config["ssh-public"])
def create_virual(self, i):
with open("webservice.yml", "r") as f:
vs = f.read()
if(self.client.servers.get_by_name(str(self.config["server-name"] + "-" + str(i)))):
print("SERVER %s: Server exists." % (str(i)))
server = self.client.servers.get_by_name(
str(self.config["server-name"] + "-" + str(i)))
print(
f"\tName: {server.data_model.name}\n\tIP: {server.data_model.public_net.ipv4.ip}")
return server
else:
print("SERVER %s: Creating server." % (str(i)))
server = self.client.servers.create(
name=str(self.config["server-name"] + "-" + str(i)),
server_type=ServerType(self.config["server-type"]),
image=Image(name=self.config["server-image"]),
ssh_keys=[self.ssh_key],
location=Location(self.config["location"]),
networks=[self.network],
user_data=vs)
server.action.wait_until_finished()
print(
f"\tName: {server.server.data_model.name}\n\tIP: {server.server.data_model.public_net.ipv4.ip}")
return server.server
def create_network(self):
if(self.client.networks.get_by_name(self.config["network"])):
print("NETWORK: Network exists.")
return self.client.networks.get_by_name(self.config["network"])
else:
print("NETWORK: Creating network.")
return self.client.networks.create(
name=self.config["network"],
ip_range=self.config["network-ip"],
subnets=[
NetworkSubnet(ip_range=self.config["network-ip"],
network_zone="eu-central", type="cloud")
]
)
def create_load_balancer(self):
if(self.client.load_balancers.get_by_name(self.config["load-balancer-name"])):
print("LOADBALANCER: Load balancer exists.")
loadBalancer = self.client.load_balancers.get_by_name(
self.config["load-balancer-name"])
print(
f"\tName: {loadBalancer.data_model.name}\n\tIP: {loadBalancer.data_model.public_net.ipv4.ip}")
return loadBalancer
else:
print("LOADBALANCER: Creating load balancer.")
targets = []
for server in self.servers:
targets.append(LoadBalancerTarget(
type="server",
server=server,
use_private_ip=True
))
loadBalancer = self.client.load_balancers.create(
name=self.config["load-balancer-name"],
load_balancer_type=LoadBalancerType(
name=self.config["load-balancer-type"]),
location=Location(self.config["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=self.network
)
loadBalancer.action.wait_until_finished()
ip = self.client.load_balancers.get_by_name(
self.config["load-balancer-name"]).data_model.public_net.ipv4.ip
print(
f"\tName: {loadBalancer.load_balancer.data_model.name}\n\tIP: {ip}")
return loadBalancer.load_balancer
def print_info(self):
print("\n----- Server info -----")
ip = self.client.load_balancers.get_by_name(
self.config["load-balancer-name"]).data_model.public_net.ipv4.ip
print(
f"Webservice dostępny pod adresem: http://{ip}:8080\n")
def delete_all(self):
for i, server in enumerate(self.servers):
print("SERVER%s: Deleted." % (str(i)))
server.delete()
self.loadBalancer.delete()
print("LOADBALANCER: 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()
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()

22
README.md Normal file
View File

@ -0,0 +1,22 @@
1. Skrypt do tworzenia serwerów i load balancera dostępny w folderze Hetzner.
2. Instalacja paczek komendą "pip3 install -r requirements.txt"
2. Uzupełnienie pliku config_base.json wartościami token-hetzner i ssh-public
```
{
"server-number": "3",
"server-name": "ms-server",
"server-type": "cx11",
"server-image": "ubuntu-20.04",
"location": "hel1",
"network": "ms-network",
"network-ip": "10.10.10.0/24",
"load-balancer-name": "ms-load-balancer",
"load-balancer-type": "lb11",
"ssh-name": "s434784",
"ssh-public": "<FILL>",
"token-hetzner": "<FILL>"
}
```
3. Uruchomienie skryptu komendą "python3 deploy.py config_base.json"
4. Po 60 sekundach jeżeli skrypt nie zostanie zatzymany wszystkie zasoby zostaną usunięte.