4-3
This commit is contained in:
parent
e248075771
commit
6ca50cdeda
2
4-3/.gitignore
vendored
Normal file
2
4-3/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
api_key
|
||||
ssh_key.pub
|
6
4-3/README.md
Normal file
6
4-3/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
1. Wygenerować nowy klucz ssh
|
||||
2. Utworzyć plik z kluczem publicznym ssh pod nazwą 'ssh_key.pub'
|
||||
2. Utworzyć plik z kluczem api pod nazwą 'api_key'
|
||||
3. chmod +x deploy.sh
|
||||
4. Uruchomić skrypt 'deploy.sh'
|
||||
5. (Opcjonalnie) jeżeli chce się usunąć utworzone zasoby - uruchomić skrypt 'remove.py'
|
9
4-3/cloud_init_ws
Normal file
9
4-3/cloud_init_ws
Normal file
@ -0,0 +1,9 @@
|
||||
#cloud-config
|
||||
packages:
|
||||
- git
|
||||
runcmd:
|
||||
- cd /home
|
||||
- git clone https://git.wmi.amu.edu.pl/s440054/dpzc-4
|
||||
- chmod +x /home/dpzc-4/04_Public_cloud/zadania/webservice
|
||||
- cp /home/dpzc-4/04_Public_cloud/zadania/webservice.service /etc/systemd/system/webservice.service
|
||||
- systemctl start webservice
|
20
4-3/constants.py
Normal file
20
4-3/constants.py
Normal file
@ -0,0 +1,20 @@
|
||||
API_KEY_FNAME = "api_key"
|
||||
SSH_KEY_FNAME = "ssh_key.pub"
|
||||
CLOUD_INIT_WS_FNAME = "cloud_init_ws"
|
||||
|
||||
SSH_KEY_NAME = "s440054"
|
||||
IP_RANGE = "10.10.10.0/24"
|
||||
NETWORK_ZONE = "eu-central"
|
||||
NETWORK_TYPE = "cloud"
|
||||
VNET_NAME = "s440054-4-3-vnet"
|
||||
VOL_NAME = "s440054-4-3-vol"
|
||||
WS_SERVER_NAME = "s440054-4-3-ws"
|
||||
LB_NAME = "s440054-4-3-lb"
|
||||
|
||||
MACHINE_TYPE = "cx11"
|
||||
MACHINE_LOCATION = "hel1"
|
||||
MACHINE_OS = "ubuntu-20.04"
|
||||
|
||||
LB_TYPE = "lb11"
|
||||
|
||||
MACHINE_COUNT = 2
|
141
4-3/deploy.py
Normal file
141
4-3/deploy.py
Normal file
@ -0,0 +1,141 @@
|
||||
from typing import Iterable
|
||||
from hcloud import Client
|
||||
from hcloud.load_balancers.client import BoundLoadBalancer
|
||||
from hcloud.networks.client import BoundNetwork
|
||||
from hcloud.servers.client import BoundServer
|
||||
from hcloud.ssh_keys.client import BoundSSHKey
|
||||
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 constants import *
|
||||
from hcloud.load_balancer_types.domain import LoadBalancerType
|
||||
from hcloud.load_balancers.domain import LoadBalancerHealthCheck, LoadBalancerService, LoadBalancerHealtCheckHttp, LoadBalancerTarget
|
||||
|
||||
|
||||
def main():
|
||||
with open(API_KEY_FNAME) as f:
|
||||
api_key = f.read()
|
||||
|
||||
client = Client(api_key)
|
||||
|
||||
ssh_key = get_ssh_key(client)
|
||||
vnet = get_vnet(client)
|
||||
servers = []
|
||||
for i in range(1, MACHINE_COUNT + 1):
|
||||
servers.append(create_ws_server(client, vnet, ssh_key, i))
|
||||
_ = create_load_balancer(client, vnet, ssh_key, servers)
|
||||
|
||||
|
||||
def get_ssh_key(client: Client) -> BoundSSHKey:
|
||||
ssh_key = client.ssh_keys.get_by_name(SSH_KEY_NAME)
|
||||
if ssh_key is None:
|
||||
with open(SSH_KEY_FNAME) as f:
|
||||
ssh_key_pub = f.read()
|
||||
ssh_key = client.ssh_keys.create(
|
||||
name=SSH_KEY_NAME, public_key=ssh_key_pub)
|
||||
print(
|
||||
f"Klucz SSH: {ssh_key.data_model.name}, {ssh_key.data_model.public_key}")
|
||||
return ssh_key
|
||||
|
||||
|
||||
def get_vnet(client: Client) -> BoundNetwork:
|
||||
vnet = client.networks.get_by_name(VNET_NAME)
|
||||
if vnet is None:
|
||||
vnet = client.networks.create(
|
||||
name=VNET_NAME,
|
||||
ip_range=IP_RANGE,
|
||||
subnets=[
|
||||
NetworkSubnet(ip_range=IP_RANGE,
|
||||
network_zone=NETWORK_ZONE, type=NETWORK_TYPE)
|
||||
]
|
||||
)
|
||||
print(
|
||||
f"Sieć wirtualna: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
||||
return vnet
|
||||
|
||||
|
||||
def create_ws_server(client: Client, vnet: BoundNetwork, ssh_key: BoundSSHKey, i: int) -> BoundServer:
|
||||
name = f"{WS_SERVER_NAME}_{str(i)}"
|
||||
ws_server = client.servers.get_by_name(name)
|
||||
if ws_server is not None:
|
||||
print(
|
||||
f"Serwer: {ws_server.data_model.name}\n\tpubliczne IP: {ws_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {ws_server.data_model.private_net[0].ip}")
|
||||
return ws_server
|
||||
with open(CLOUD_INIT_WS_FNAME) as f:
|
||||
cloud_init_db = f.read()
|
||||
ws_server_resp = client.servers.create(
|
||||
name=name,
|
||||
server_type=ServerType(MACHINE_TYPE),
|
||||
image=Image(name=MACHINE_OS),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location(MACHINE_LOCATION),
|
||||
user_data=cloud_init_db
|
||||
)
|
||||
|
||||
ws_server_resp.action.wait_until_finished()
|
||||
print(f"Tworzenie serwera ws: {ws_server_resp.action.complete}")
|
||||
|
||||
ws_server = client.servers.get_by_name(name)
|
||||
print(
|
||||
f"Serwer: {ws_server.data_model.name}\n\tpubliczne IP: {ws_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {ws_server.data_model.private_net[0].ip}")
|
||||
|
||||
return ws_server
|
||||
|
||||
def create_load_balancer(client: Client, vnet: BoundNetwork, ssh_key: BoundSSHKey, servers: Iterable[BoundServer]) -> BoundLoadBalancer:
|
||||
if(client.load_balancers.get_by_name(LB_NAME)):
|
||||
load_balancer = client.load_balancers.get_by_name(LB_NAME)
|
||||
print(
|
||||
f"Load balancer: {load_balancer.data_model.name}\n\tpubliczne IP: {load_balancer.data_model.public_net.ipv4.ip}")
|
||||
return load_balancer
|
||||
else:
|
||||
targets = []
|
||||
for server in servers:
|
||||
targets.append(LoadBalancerTarget(
|
||||
type="server",
|
||||
server=server,
|
||||
use_private_ip=True
|
||||
))
|
||||
|
||||
load_balancer = client.load_balancers.create(
|
||||
name=LB_NAME,
|
||||
|
||||
load_balancer_type=LoadBalancerType(LB_TYPE),
|
||||
|
||||
location=Location(MACHINE_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: {load_balancer.action.complete}")
|
||||
load_balancer = client.load_balancers.get_by_name(LB_NAME)
|
||||
print(
|
||||
f"Load balancer: {load_balancer.data_model.name}\n\tpubliczne IP: {load_balancer.data_model.public_net.ipv4.ip}\n\tprywatne IP: {load_balancer.data_model.private_net[0].ip}")
|
||||
|
||||
return load_balancer
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
2
4-3/deploy.sh
Normal file
2
4-3/deploy.sh
Normal file
@ -0,0 +1,2 @@
|
||||
pip3 install hcloud
|
||||
python3 deploy.py
|
23
4-3/remove.py
Normal file
23
4-3/remove.py
Normal file
@ -0,0 +1,23 @@
|
||||
from constants import *
|
||||
from hcloud import Client
|
||||
|
||||
|
||||
def main():
|
||||
with open(API_KEY_FNAME) as f:
|
||||
api_key = f.read()
|
||||
|
||||
client = Client(api_key)
|
||||
|
||||
ssh_key = client.ssh_keys.get_by_name(SSH_KEY_NAME)
|
||||
vnet = client.networks.get_by_name(VNET_NAME)
|
||||
db_server = client.servers.get_by_name(DB_SERVER_NAME)
|
||||
gitea_server = client.servers.get_by_name(GITEA_SERVER_NAME)
|
||||
|
||||
ssh_key.delete()
|
||||
vnet.delete()
|
||||
db_server.delete()
|
||||
gitea_server.delete()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user