2023-01-22 15:03:37 +01:00
|
|
|
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
|
2023-01-22 18:59:39 +01:00
|
|
|
from hcloud.load_balancers.domain import LoadBalancerTarget, LoadBalancerService, LoadBalancerServiceHttp, LoadBalancerHealthCheck, LoadBalancerHealtCheckHttp
|
2023-01-22 15:03:37 +01:00
|
|
|
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})")
|
|
|
|
|
2023-01-22 18:59:39 +01:00
|
|
|
webservice_init=r'''#cloud-config
|
|
|
|
packages:
|
|
|
|
- apt-transport-https
|
|
|
|
- ca-certificates
|
|
|
|
- curl
|
|
|
|
- gnupg-agent
|
|
|
|
- software-properties-common
|
|
|
|
|
|
|
|
write_files:
|
|
|
|
- path: /root/Dockerfile
|
|
|
|
content: |
|
|
|
|
FROM ubuntu
|
|
|
|
COPY ./webservice /
|
|
|
|
RUN chmod +x ./webservice
|
|
|
|
EXPOSE 80:8080/tcp
|
|
|
|
CMD ./webservice
|
|
|
|
|
2023-01-22 15:03:37 +01:00
|
|
|
runcmd:
|
2023-01-22 18:59:39 +01:00
|
|
|
- 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
|
|
|
|
- curl -fsSL https://git.wmi.amu.edu.pl/s444498/cloud/raw/branch/master/Public_cloud/webservice > /root/webservice
|
|
|
|
- systemctl start docker
|
|
|
|
- systemctl enable docker
|
|
|
|
- cd /root/
|
|
|
|
- docker build -t webservice .
|
|
|
|
- docker run -d -p 80:8080 -t webservice
|
2023-01-22 15:03:37 +01:00
|
|
|
'''
|
2023-01-22 18:59:39 +01:00
|
|
|
|
2023-01-22 15:03:37 +01:00
|
|
|
for i in range(NUM_OF_SERVERS):
|
|
|
|
webservice_server = client.servers.create(
|
|
|
|
name=f"{PREFIX}-webservice-{i+1}",
|
|
|
|
server_type=ServerType("cx11"),
|
2023-01-22 16:51:01 +01:00
|
|
|
image=Image(name="ubuntu-22.04"),
|
2023-01-22 15:03:37 +01:00
|
|
|
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}")
|
2023-01-22 18:59:39 +01:00
|
|
|
loadbalancer_targets.append(LoadBalancerTarget(type="server", server=webservice_server.server, use_private_ip=False))
|
2023-01-22 15:03:37 +01:00
|
|
|
|
2023-01-22 18:59:39 +01:00
|
|
|
loadbalancer = client.load_balancers.create(
|
|
|
|
name= f"{PREFIX}-loadbalancer",
|
|
|
|
load_balancer_type=LoadBalancerType(name="lb11"),
|
|
|
|
location=Location("hel1"),
|
|
|
|
targets=loadbalancer_targets,
|
|
|
|
public_interface=True,
|
|
|
|
services=[
|
|
|
|
LoadBalancerService(
|
|
|
|
protocol="http",
|
|
|
|
listen_port=80,
|
|
|
|
destination_port=80,
|
|
|
|
proxyprotocol=False,
|
|
|
|
health_check=LoadBalancerHealthCheck(
|
2023-01-22 15:03:37 +01:00
|
|
|
protocol="http",
|
2023-01-22 18:59:39 +01:00
|
|
|
port=80,
|
|
|
|
interval=15,
|
|
|
|
timeout=10,
|
|
|
|
retries=3,
|
|
|
|
http=LoadBalancerHealtCheckHttp(
|
|
|
|
path="/factors/32",
|
|
|
|
status_codes=["2??", "3??"],
|
|
|
|
tls=False
|
|
|
|
)
|
|
|
|
),
|
|
|
|
http=LoadBalancerServiceHttp(
|
|
|
|
cookie_name='HCLBSTICKY',
|
|
|
|
cookie_lifetime=300,
|
|
|
|
sticky_sessions=True,
|
|
|
|
certificates=[],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2023-01-22 15:03:37 +01:00
|
|
|
|
|
|
|
loadbalancer.action.wait_until_finished()
|
|
|
|
print(f"Loadbalancer creating...: {loadbalancer.action.complete}")
|
|
|
|
|
|
|
|
load_balancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer")
|
2023-01-22 18:59:39 +01:00
|
|
|
print(f"Webserwis: http://{load_balancer.data_model.public_net.ipv4.ip}:80/factors/")
|