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 import time from pathlib import Path with open("../token.txt", "r") as file: api_token = file.read().strip() with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file: ssh_key = file.readline() client = Client( token=api_token ) PREFIX = "s444498" try: ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key-2", public_key=ssh_key) print(f"Key {ssh_key.data_model.name} created: {ssh_key.data_model.public_key}") except: ssh_key = client.ssh_keys.get_by_name(f"{PREFIX}-pzc-ssh-key-2") print(f"Key {ssh_key.data_model.name} already in use: {ssh_key.data_model.public_key}") try: vnet = client.networks.create( name=f"{PREFIX}-pzc-test-vnet", ip_range="10.10.10.0/24", subnets=[ NetworkSubnet(ip_range="10.10.10.0/24", network_zone="eu-central", type="cloud") ] ) print(f"Created network: {vnet.data_model.name} ({vnet.data_model.ip_range})") except: vnet = client.networks.get_by_name( f"{PREFIX}-pzc-test-vnet", ) print(f"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})") try: shared_volume = client.volumes.create( size=15, name="gitea-volume-s444498", location=Location("hel1")) shared_volume.action.wait_until_finished() print(f"Created volume: {shared_volume.volume}") except: shared_volume = client.volumes.get_by_name("gitea-volume-s444498") shared_volume.action.wait_until_finished() print(f"Volume in use: {shared_volume.volume}") volume_device = shared_volume.volume.linux_device cloud_init_mysql=r'''#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 environment: MYSQL_DATABASE: gitea MYSQL_USER: gitea MYSQL_PASSWORD: gitea MYSQL_ROOT_PASSWORD: gitea ports: - "10.10.10.2:3306:3306" volumes: - my-db:/var/lib/mysql phpmyadmin: image: phpmyadmin restart: always ports: - "8080:80" volumes: my-db: {} 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 ''' mysql_server = client.servers.create( name=f"{PREFIX}-mysql1", server_type=ServerType("cx11"), image=Image(name="ubuntu-20.04"), ssh_keys=[ssh_key], networks=[vnet], location=Location("hel1"), user_data=cloud_init_mysql ) mysql_server.action.wait_until_finished() print(f"Creating mysql server: {mysql_server.action.complete}") time.sleep(20) cloud_init_gitea=r'''#cloud-config packages: - apt-transport-https - ca-certificates - curl - gnupg-agent - software-properties-common write_files: - path: /root/docker-compose.yml content: | version: "3" networks: gitea: external: false services: server: image: gitea/gitea:1.17.4 container_name: gitea environment: - USER_UID=1000 - USER_GID=1000 - GITEA__server__DOMAIN=${DOMAIN} - GITEA__database__DB_TYPE=mysql - GITEA__database__HOST=10.10.10.2:3306 - GITEA__database__NAME=gitea - GITEA__database__USER=gitea - GITEA__database__PASSWD=gitea restart: always networks: - gitea volumes: - ./data:/root/gitea - ./config:/root/gitea/config - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro - /mnt/data_gitea:/data ports: - "3000:3000" - "222:22" ''' runcmd = f''' 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 /mnt/ - mkdir data_gitea - cd /root/ - IP=$(hostname -I | cut -d ' ' -f 1) - echo "DOMAIN=$IP" >> .env - sudo mkfs.xfs -f {volume_device} - mount -o discard,defaults {volume_device} /mnt/data_gitea - docker-compose up -d ''' cloud_init_gitea += runcmd gitea_server = client.servers.create( name=f"{PREFIX}-gitea1", server_type=ServerType("cx11"), image=Image(name="ubuntu-20.04"), ssh_keys=[ssh_key], networks=[vnet], location=Location("hel1"), user_data=cloud_init_gitea, volumes=[shared_volume.volume], automount=True ) gitea_server.action.wait_until_finished() print(f"Creating gitea server: {gitea_server.action.complete}")