refactor files structure and init loadbalancer
This commit is contained in:
parent
c804134449
commit
6e032ea31b
104
Public_cloud/loadbalancer-init.py
Normal file
104
Public_cloud/loadbalancer-init.py
Normal file
@ -0,0 +1,104 @@
|
||||
from hcloud import Client
|
||||
from hcloud.images.domain import Image
|
||||
from hcloud.server_types.domain import ServerType
|
||||
from hcloud.networks.domain import NetworkSubnet
|
||||
from hcloud.locations.domain import Location
|
||||
from hcloud.load_balancer_types.domain import LoadBalancerType
|
||||
from hcloud.load_balancers.domain import LoadBalancerTarget, LoadBalancerService, LoadBalancerHealthCheck, LoadBalancerHealtCheckHttp
|
||||
from pathlib import Path
|
||||
|
||||
with open("../token.txt", "r") as file:
|
||||
api_token = file.read().strip()
|
||||
|
||||
with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file:
|
||||
ssh_key = file.readline()
|
||||
|
||||
client = Client(token=api_token)
|
||||
|
||||
PREFIX = "s444498"
|
||||
|
||||
try:
|
||||
ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key-2", public_key=ssh_key)
|
||||
print(f"Key {ssh_key.data_model.name} created: {ssh_key.data_model.public_key}")
|
||||
except:
|
||||
ssh_key = client.ssh_keys.get_by_name(f"{PREFIX}-pzc-ssh-key-2")
|
||||
print(f"Key {ssh_key.data_model.name} already in use: {ssh_key.data_model.public_key}")
|
||||
|
||||
NUM_OF_SERVERS = 5
|
||||
loadbalancer_targets = []
|
||||
|
||||
try:
|
||||
vnet = client.networks.create(
|
||||
name=f"{PREFIX}-pzc-test-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"Created network: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
||||
except:
|
||||
vnet = client.networks.get_by_name(
|
||||
f"{PREFIX}-pzc-test-vnet",
|
||||
)
|
||||
print(f"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
||||
|
||||
webservice_init=r'''
|
||||
#cloud-config
|
||||
runcmd:
|
||||
- git clone https://git.wmi.amu.edu.pl/s444498/cloud
|
||||
- cd Public_cloud
|
||||
- chmod +x webservice
|
||||
- ./webservice
|
||||
'''
|
||||
for i in range(NUM_OF_SERVERS):
|
||||
webservice_server = client.servers.create(
|
||||
name=f"{PREFIX}-webservice-{i+1}",
|
||||
server_type=ServerType("cx11"),
|
||||
image=Image(name="ubuntu-20.04"),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location("hel1"),
|
||||
user_data=webservice_init
|
||||
)
|
||||
|
||||
webservice_server.action.wait_until_finished()
|
||||
print(f"Creating webservice {i+1}: {webservice_server.action.complete}")
|
||||
loadbalancer_targets.append(LoadBalancerTarget(type="server", server=webservice_server.server, use_private_ip=True))
|
||||
|
||||
try:
|
||||
loadbalancer = client.load_balancers.create(
|
||||
name= f"{PREFIX}-loadbalancer",
|
||||
load_balancer_type=LoadBalancerType(name="lb11"),
|
||||
location=Location("hel1"),
|
||||
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=loadbalancer_targets,
|
||||
public_interface=True,
|
||||
network=vnet
|
||||
)
|
||||
except:
|
||||
loadbalancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer")
|
||||
|
||||
loadbalancer.action.wait_until_finished()
|
||||
print(f"Loadbalancer creating...: {loadbalancer.action.complete}")
|
||||
|
||||
load_balancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer")
|
||||
print(f"Webserwis: http://{load_balancer.data_model.public_net.ipv4.ip}:8080/factors/")
|
23
Public_cloud/reset.py
Normal file
23
Public_cloud/reset.py
Normal file
@ -0,0 +1,23 @@
|
||||
from hcloud import Client
|
||||
|
||||
with open("../token.txt", "r") as file:
|
||||
api_token = file.read().strip()
|
||||
|
||||
client = Client(token=api_token)
|
||||
|
||||
PREFIX = "s444498"
|
||||
|
||||
servers = client.servers.get_all()
|
||||
print(f"Deleting {len(servers)} servers")
|
||||
for s in servers:
|
||||
if s.data_model.name.startswith(PREFIX):
|
||||
action = client.servers.delete(s)
|
||||
print(f"Delete server {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
|
||||
|
||||
volume = client.volumes.get_by_name(f"{PREFIX}-pzc-test-volume") or None
|
||||
if volume:
|
||||
print("Remove volume...")
|
||||
action = client.volumes.detach(volume)
|
||||
action.wait_until_finished()
|
||||
action = client.volumes.delete(volume)
|
||||
print("Volume removed")
|
BIN
Public_cloud/webservice
Normal file
BIN
Public_cloud/webservice
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user