s464979-hetzner/gitea_config.py

79 lines
2.3 KiB
Python
Raw Normal View History

2024-11-20 18:09:07 +01:00
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
client = Client(
token="oiA3vdWArVn67e4kHdyIKjDdnGNQGxEJJUI0ln8GrTsZjs10cmyAkqwjC3ufdZC8"
)
PREFIX = "464979"
ssh_key = client.ssh_keys.get_by_name(name="464979-pzc-ssh-key")
print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}")
vnet = client.networks.create(
name=f"{PREFIX}-gitea-net",
ip_range="10.10.10.0/24",
subnets=[
NetworkSubnet(ip_range="10.10.10.0/24", network_zone="eu-central", type="cloud")
]
)
print(f"Utworzono sieć wirtualną {vnet.data_model.name} ({vnet.data_model.ip_range})")
with open("db_cloud_init.yml", 'r', encoding='utf-8') as file:
cloud_init_db = file.read()
db_server = client.servers.create(
name=f"{PREFIX}-db",
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_db
)
db_server.action.wait_until_finished()
print(f"Tworzenie serwera db: {db_server.action.complete}")
db_server = client.servers.get_by_name(f"{PREFIX}-db")
print(f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}")
2024-11-20 19:11:39 +01:00
volume_name = f"{PREFIX}-gitea-vol"
volume = client.volumes.create(
name=volume_name,
size=10,
format='ext4',
location=Location("hel1")
)
print(f"Created data volume: {volume.volume.name}")
2024-11-20 18:09:07 +01:00
with open("gitea_cloud_init.yml", 'r', encoding='utf-8') as file:
cloud_init_wp1 = file.read()
2024-11-20 19:11:39 +01:00
cloud_init_wp1.replace("VOLUME_NAME", volume_name)
2024-11-20 18:09:07 +01:00
wp1_server = client.servers.create(
name=f"{PREFIX}-gitea",
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key],
networks=[vnet],
2024-11-20 19:11:39 +01:00
volumes=[volume.volume],
2024-11-20 18:09:07 +01:00
location=Location("hel1"),
user_data=cloud_init_wp1
)
wp1_server.action.wait_until_finished()
print(f"Tworzenie serwera Gitea: {wp1_server.action.complete}")
wp1_server = client.servers.get_by_name(f"{PREFIX}-gitea")
print(
f"Serwer: {wp1_server.data_model.name}\n\tpubliczne IP: {wp1_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {wp1_server.data_model.private_net[0].ip}")