import os from dotenv import load_dotenv load_dotenv() 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 # Hetzner Cloud SERVER_TYPE = "cx22" IMAGE_UBUNTU = "ubuntu-24.04" API_KEY = os.getenv("API_KEY") SSH_PUBKEY = os.getenv("SSH_PUBKEY") PREFIX = "s464863" SSH_KEY_NAME = PREFIX IP_RANGE = "10.10.10.0/24" LOCATION = "hel1" NETWORK_NAME = f"{PREFIX}-network" DB_SERVER_NAME = f"{PREFIX}-db" VOLUME_NAME = f"{PREFIX}-volume" GITEA_SERVER_NAME = f"{PREFIX}-gitea" # Credentials MYSQL_ROOT_PASSWORD = os.getenv("MYSQL_ROOT_PASSWORD") MYSQL_DATABASE = os.getenv("MYSQL_DATABASE") MYSQL_USER = os.getenv("MYSQL_USER") MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD") client = Client( token=API_KEY ) ssh_key = client.ssh_keys.get_by_name("broke") if not ssh_key: ssh_key = client.ssh_keys.create(name=SSH_KEY_NAME, public_key=SSH_PUBKEY) print(f"Klucz {ssh_key.data_model.name} został utworzony") else: print(f"Pomyślnie wczytano klucz: {ssh_key.data_model.name}") network = client.networks.get_by_name(NETWORK_NAME) if not network: network = client.networks.create( name=NETWORK_NAME, ip_range=IP_RANGE, subnets=[ NetworkSubnet(ip_range=IP_RANGE, network_zone="eu-central", type="cloud") ] ) print(f"Sieć {network.data_model.name} została utworzona") else: print(f"Znaleziono sieć o zadanej nazwie: {network.data_model.name}") volume = client.volumes.get_by_name(VOLUME_NAME) if not volume: volume = client.volumes.create( size=10, name=VOLUME_NAME, location=Location(LOCATION), format="ext4" ) print(f"Wolumen {VOLUME_NAME} został utworzony") else: print(f"Znaleziono wolumen o zadanej nazwie: {volume.data_model.name}") # Pobranie ID wolumenu - potrzebne do mapowania wolumenu w docker-compose.yml volume = client.volumes.get_by_name(VOLUME_NAME) VOLUME_ID = volume.data_model.id VOLUME_PATH = f"/mnt/HC_Volume_{VOLUME_ID}" print(f"ID wolumenu: {VOLUME_ID}") cloud_init_db = f'''#cloud-config packages: - apt-transport-https - ca-certificates - curl - gnupg-agent - software-properties-common write_files: - path: /root/docker-compose.yml content: | version: '3.9' services: db: image: mysql:5.7 restart: always ports: - "10.10.10.2:3306:3306" environment: MYSQL_ROOT_PASSWORD: {MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: {MYSQL_DATABASE} MYSQL_USER: {MYSQL_USER} MYSQL_PASSWORD: {MYSQL_PASSWORD} volumes: - db_data:/var/lib/mysql volumes: db_data: {{}} 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 - cd /root/ && docker-compose up -d ''' db_server = client.servers.create( name=DB_SERVER_NAME, server_type=ServerType(SERVER_TYPE), image=Image(name=IMAGE_UBUNTU), ssh_keys=[ssh_key], networks=[network], location=Location(LOCATION), user_data=cloud_init_db ) db_server.action.wait_until_finished() print(f"Serwer {DB_SERVER_NAME} został utworzony") cloud_init_gitea = f'''#cloud-config packages: - apt-transport-https - ca-certificates - curl - gnupg-agent - software-properties-common write_files: - path: /root/docker-compose.yml content: | version: '3.9' services: gitea: image: gitea/gitea:1.22.3 volumes: - {VOLUME_PATH}:/data - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro ports: - "3000:3000" - "222:22" restart: always environment: GITEA__database__DB_TYPE: mysql GITEA__database__HOST: "10.10.10.2:3306" GITEA__database__NAME: {MYSQL_DATABASE} GITEA__database__USER: {MYSQL_USER} GITEA__database__PASSWD: {MYSQL_PASSWORD} 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 - cd /root/ && docker-compose up -d ''' gitea_server = client.servers.create( name=f"{GITEA_SERVER_NAME}", server_type=ServerType(SERVER_TYPE), image=Image(name=IMAGE_UBUNTU), ssh_keys=[ssh_key], networks=[network], volumes=[volume], location=Location(LOCATION), user_data=cloud_init_gitea ) gitea_server.action.wait_until_finished() print(f"Serwer {GITEA_SERVER_NAME} został utworzony") print(f"Adres serwera Gitea: {gitea_server.server.data_model.public_net.ipv4.ip}:3000")