hetzner-gitea/hetzner.ipynb
dzikafoczka 8b1c611e37 Hetzner
2024-11-21 19:04:57 +01:00

14 KiB

Importy

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

Zmienne

# 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")

Interfejs do chmury

client = Client(
    token=API_KEY
)

Funkcje pomocnicze

def delete_all_servers():
    servers = client.servers.get_all()
    for server in servers:
        if server.name.startswith(PREFIX):
            action = client.servers.delete(server)
            print(
                f"Usuwanie serwera {server.data_model.name} ({server.data_model.public_net.ipv4.ip}): {action.data_model.status}")

Klucz SSH

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}")
Pomyślnie wczytano klucz: broke

Sieć

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}")
Znaleziono sieć o zadanej nazwie: s464863-network

Wolumen

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}")
Znaleziono wolumen o zadanej nazwie: s464863-volume
# 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}")
ID wolumenu: 101655125

Baza danych

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")
Serwer s464863-db został utworzony

Gitea

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")
Serwer s464863-gitea został utworzony
gitea_server.server.data_model.public_net.ipv4.ip
'37.27.83.246'
delete_all_servers()
Usuwanie serwera s464863-db (65.21.149.208): running
Usuwanie serwera s464863-gitea (95.216.200.81): running