Hetzner
This commit is contained in:
parent
9b1c3dae9a
commit
478035c6e3
151
balancer/main.py
Normal file
151
balancer/main.py
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
from hcloud import Client
|
||||||
|
from hcloud.networks.domain import NetworkSubnet
|
||||||
|
from hcloud.locations.domain import Location
|
||||||
|
from hcloud.server_types.domain import ServerType
|
||||||
|
from hcloud.images.domain import Image
|
||||||
|
from hcloud.load_balancers.domain import (
|
||||||
|
LoadBalancerService,
|
||||||
|
LoadBalancerServiceHttp,
|
||||||
|
LoadBalancerHealthCheck,
|
||||||
|
LoadBalancerHealtCheckHttp,
|
||||||
|
LoadBalancerTarget
|
||||||
|
)
|
||||||
|
from hcloud.load_balancer_types.domain import LoadBalancerType
|
||||||
|
|
||||||
|
|
||||||
|
TOKEN = 'KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23'
|
||||||
|
SSH_PUBKEY = ''
|
||||||
|
|
||||||
|
cloud_init = """#cloud-config
|
||||||
|
packages:
|
||||||
|
- apt-transport-https
|
||||||
|
- ca-certificates
|
||||||
|
- curl
|
||||||
|
- gnupg-agent
|
||||||
|
- software-properties-common
|
||||||
|
|
||||||
|
runcmd:
|
||||||
|
- 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
|
||||||
|
- systemctl start docker
|
||||||
|
- systemctl enable docker
|
||||||
|
- git clone https://git.wmi.amu.edu.pl/s444465/pzc-hetzner.git
|
||||||
|
- cd pzc-hetzner/balancer
|
||||||
|
- docker build -t webservice .
|
||||||
|
- docker run -d -p 80:8080 -t webservice"""
|
||||||
|
|
||||||
|
|
||||||
|
PREFIX = '444465'
|
||||||
|
key_name = f'{PREFIX}-key'
|
||||||
|
net_name = f'{PREFIX}-vnet'
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
client = Client(token=TOKEN)
|
||||||
|
ssh_key = client.ssh_keys.get_by_name(key_name) or None
|
||||||
|
if not ssh_key:
|
||||||
|
ssh_key = client.ssh_keys.create(name=key_name, public_key=SSH_PUBKEY)
|
||||||
|
|
||||||
|
vnet = client.networks.get_by_name(net_name) or None
|
||||||
|
if not vnet:
|
||||||
|
vnet = client.networks.create(
|
||||||
|
name=net_name,
|
||||||
|
ip_range='10.10.10.0/24',
|
||||||
|
subnets=[
|
||||||
|
NetworkSubnet(ip_range='10.10.10.0/24',
|
||||||
|
network_zone='eu-central',
|
||||||
|
type='cloud')
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
server1 = client.servers.create(
|
||||||
|
name=f'{PREFIX}-webservice-1',
|
||||||
|
server_type=ServerType('cx11'),
|
||||||
|
image=Image(name='ubuntu-20.04'),
|
||||||
|
ssh_keys=[ssh_key],
|
||||||
|
networks=[vnet],
|
||||||
|
location=Location('hel1'),
|
||||||
|
user_data=cloud_init
|
||||||
|
)
|
||||||
|
server1.action.wait_until_finished()
|
||||||
|
print(f'Server 1 created: {server1.action.complete}')
|
||||||
|
server1 = client.servers.get_by_name(f'{PREFIX}-webservice-1')
|
||||||
|
|
||||||
|
server2 = client.servers.create(
|
||||||
|
name=f'{PREFIX}-webservice-2',
|
||||||
|
server_type=ServerType('cx11'),
|
||||||
|
image=Image(name='ubuntu-20.04'),
|
||||||
|
ssh_keys=[ssh_key],
|
||||||
|
networks=[vnet],
|
||||||
|
location=Location('hel1'),
|
||||||
|
user_data=cloud_init
|
||||||
|
)
|
||||||
|
server2.action.wait_until_finished()
|
||||||
|
print(f'Server 2 created: {server2.action.complete}')
|
||||||
|
server2 = client.servers.get_by_name(f'{PREFIX}-webservice-2')
|
||||||
|
|
||||||
|
|
||||||
|
server3 = client.servers.create(
|
||||||
|
name=f'{PREFIX}-webservice-3',
|
||||||
|
server_type=ServerType('cx11'),
|
||||||
|
image=Image(name='ubuntu-20.04'),
|
||||||
|
ssh_keys=[ssh_key],
|
||||||
|
networks=[vnet],
|
||||||
|
location=Location('hel1'),
|
||||||
|
user_data=cloud_init
|
||||||
|
)
|
||||||
|
server3.action.wait_until_finished()
|
||||||
|
print(f'Server 3 created: {server3.action.complete}')
|
||||||
|
server3 = client.servers.get_by_name(f'{PREFIX}-webservice-3')
|
||||||
|
|
||||||
|
load_balancer = client.load_balancers.create(
|
||||||
|
name=f'{PREFIX}-load-balancer',
|
||||||
|
load_balancer_type=LoadBalancerType(name='lb11'),
|
||||||
|
location=Location('hel1'),
|
||||||
|
network=vnet,
|
||||||
|
targets=[
|
||||||
|
LoadBalancerTarget(
|
||||||
|
type='server',
|
||||||
|
server=server1,
|
||||||
|
use_private_ip=True,
|
||||||
|
),
|
||||||
|
LoadBalancerTarget(
|
||||||
|
type='server',
|
||||||
|
server=server2,
|
||||||
|
use_private_ip=True,
|
||||||
|
),
|
||||||
|
LoadBalancerTarget(
|
||||||
|
type='server',
|
||||||
|
server=server3,
|
||||||
|
use_private_ip=True,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
services=[
|
||||||
|
LoadBalancerService(
|
||||||
|
protocol='http',
|
||||||
|
listen_port=80,
|
||||||
|
destination_port=80,
|
||||||
|
health_check=LoadBalancerHealthCheck(
|
||||||
|
protocol='http',
|
||||||
|
port=80,
|
||||||
|
interval=15,
|
||||||
|
timeout=10,
|
||||||
|
retries=3,
|
||||||
|
http=LoadBalancerHealtCheckHttp(
|
||||||
|
path='/factors/10',
|
||||||
|
status_codes=['2??', '3??'],
|
||||||
|
tls=False,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
http=LoadBalancerServiceHttp(
|
||||||
|
cookie_name='HCLBSTICKY',
|
||||||
|
cookie_lifetime=300,
|
||||||
|
sticky_sessions=True,
|
||||||
|
certificates=[],
|
||||||
|
)
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
@ -16,7 +16,7 @@ client = Client(token=api_token)
|
|||||||
|
|
||||||
PREFIX = "444465"
|
PREFIX = "444465"
|
||||||
|
|
||||||
YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC7JxVZ/BY1lfvAyUPysBBhq5SDs+48HEySg+aZ8dvjnnMc1kV8ENOgYpBmih1FI/Gmvkhw/8tHUH9AgGQrKQ3s0EKi7uqycDQnvstQH1y9YhuvyveXK0VR2v4MvVZXj6vD1ayqAZ2ojytmwWc4Cz+BBF+evTisGtjhy3iNmumi67le6u6tDJz1j4/KVxZPusG970pCU9UKR/rFOq3GE95L8vZeMPZ0Y5bTL16QiWWLX4gyaYnMQCij/kOxIeSfv9wnJFZ/JYUIssKOEHOaqVb3mNd50+eSGN+zJzLIy6WZNiois6KYuoBlkyJriZYYHiEKkVJX5dtraGJfF8a/M5U0zJ02kS+mwHmcTdXI2LQGLOvRFvkNsckRHwJaFNK/8L/xaYQnM8xPzVgC8QIntRJAIUmpEbInuqy0jgZ1b6K1sN2IHD6s0g8LkmgKxnr07LxyyXnp4u6sRuuex9ylMP3P3aAo5Iwg69QK+oz4pCXD+pLi4MhR01ri63Bz2QYCJp8= andrzej@andrzej-VivoBook-ASUSLaptop-X515JA-F515JA"
|
YOUR_LOCAL_SSH_PUBKEY = ""
|
||||||
|
|
||||||
vnet_name = f"{PREFIX}-pzc-test-vnet"
|
vnet_name = f"{PREFIX}-pzc-test-vnet"
|
||||||
vnet = client.networks.get_by_name(vnet_name) or None
|
vnet = client.networks.get_by_name(vnet_name) or None
|
||||||
|
65
zadanie_4.py
Normal file
65
zadanie_4.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
from hcloud import Client
|
||||||
|
from hcloud.locations.domain import Location
|
||||||
|
from hcloud.images.domain import Image
|
||||||
|
from hcloud.server_types.domain import ServerType
|
||||||
|
from hcloud.networks.domain import NetworkSubnet
|
||||||
|
|
||||||
|
api_token = ""
|
||||||
|
|
||||||
|
with open("token.txt", "r") as file:
|
||||||
|
api_token = file.read().strip()
|
||||||
|
|
||||||
|
client = Client(token=api_token)
|
||||||
|
|
||||||
|
PREFIX = "444465"
|
||||||
|
YOUR_LOCAL_SSH_PUBKEY = ""
|
||||||
|
|
||||||
|
vnet_name = f"{PREFIX}-pzc-test-vnet"
|
||||||
|
vnet = client.networks.get_by_name(vnet_name) or None
|
||||||
|
if not vnet:
|
||||||
|
vnet = client.networks.create(
|
||||||
|
name=vnet_name,
|
||||||
|
ip_range="10.0.0.0/16",
|
||||||
|
subnets=[
|
||||||
|
NetworkSubnet(ip_range="10.0.0.0/16", network_zone="eu-central", type="cloud")
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
ssh_name = f"{PREFIX}-ssh-key"
|
||||||
|
ssh_key = client.ssh_keys.get_by_name(ssh_name) or None
|
||||||
|
if not ssh_key:
|
||||||
|
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=YOUR_LOCAL_SSH_PUBKEY)
|
||||||
|
|
||||||
|
|
||||||
|
cloud_init=r'''#cloud-config
|
||||||
|
packages:
|
||||||
|
- apt-transport-https
|
||||||
|
- ca-certificates
|
||||||
|
- curl
|
||||||
|
- gnupg-agent
|
||||||
|
- software-properties-common
|
||||||
|
|
||||||
|
write_files:
|
||||||
|
- content: |
|
||||||
|
curl -fsSL https://code-server.dev/install.sh > /root/install.sh
|
||||||
|
path: /root/install.sh
|
||||||
|
owner: root:root
|
||||||
|
permissions: '755'
|
||||||
|
|
||||||
|
runcmd:
|
||||||
|
- bash /root/install.sh
|
||||||
|
- code-server --bind-addr 0.0.0.0:8080
|
||||||
|
'''
|
||||||
|
|
||||||
|
vscode_server = client.servers.create(
|
||||||
|
name=f"{PREFIX}-code-server",
|
||||||
|
server_type=ServerType("cx11"),
|
||||||
|
image=Image(name="ubuntu-20.04"),
|
||||||
|
ssh_keys=[ssh_key],
|
||||||
|
networks=[vnet],
|
||||||
|
location=Location("hel1"),
|
||||||
|
user_data=cloud_init
|
||||||
|
)
|
||||||
|
|
||||||
|
vscode_server.action.wait_until_finished()
|
||||||
|
print(f"VSCode server created: {vscode_server.action.complete}")
|
Loading…
Reference in New Issue
Block a user