from hcloud import Client from hcloud.images import Image from hcloud.networks import Network, NetworkSubnet from hcloud.volumes import Volume from hcloud.server_types import ServerType from hcloud._exceptions import APIException from hcloud.servers.domain import ServerCreatePublicNetwork YOUR_API_TOKEN = "" NETWORK_PRIV = "zpzc-s434686-priv-net" NETWORK_TYPE = "server" NETWORK_ZONE = "eu-central" IP_RANGE = "10.0.0.0/8" IP_RANGE_SUBNET = "10.0.0.0/24" DB_SERVER_NAME = "zpzc-s434686-db-script" GITEA_SERVER_NAME = "zpzc-s434686-gitea-script" LOCATION = "hel1" VOLUME_NAME = "s434686-volume" PROC = "cx11" SERVER_SYS = "ubuntu-22.04" print("Bound connection") client = Client(token=f"{YOUR_API_TOKEN}") print("Get location") loc = client.locations.get_by_name(LOCATION) print("Creating DB volume") try: volume = client.volumes.create(size=10, name=VOLUME_NAME, location=loc) volume = volume.volume print(f"Volume created: {volume.name}") except APIException: volume = client.volumes.get_by_name(name=VOLUME_NAME) print(f"Volume already exists: {volume.name}") print("Creating Network") try: network = client.networks.create(name=NETWORK_PRIV, ip_range=IP_RANGE) print(f"network created: {network.name}") except APIException: network = client.networks.get_by_name(name=NETWORK_PRIV) print(f"Network already exists: {network.name}") print("Creating Subnet") try: subnet = client.networks.add_subnet( network=network, subnet=NetworkSubnet(ip_range=IP_RANGE_SUBNET, network_zone=NETWORK_ZONE, type=NETWORK_TYPE) ) except APIException: pass print("Creating DB server") try: response = client.servers.create( name=DB_SERVER_NAME, server_type=ServerType(name=PROC), image=Image(name=SERVER_SYS), location=loc, volumes=[Volume(id=volume.id)], networks=[Network(id=network.id)], public_net=ServerCreatePublicNetwork(enable_ipv4=False, enable_ipv6=False), user_data=""" #cloud-config package_update: true package_upgrade: true groups: - docker system_info: default_user: groups: [ docker ] packages: - apt-transport-https - ca-certificates - curl - gnupg - lsb-release - unattended-upgrades runcmd: - mkdir -p /etc/apt/keyrings - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg - echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null - apt-get update - apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin - systemctl enable docker - systemctl start docker - git clone https://git.wmi.amu.edu.pl/s434686/zpzc-3.git /zpzc-3 - docker compose -f /zpzc-3/db.yml up -d - echo 'Done' bootcmd: - docker compose -f /zpzc-3/db.yml up -d final_message: "The system is finally up, after $UPTIME seconds" """ ) db_server = response.server print(f"{db_server.id=} {db_server.name=} {db_server.status=}") except APIException: print("Server already exists:") db_server = client.servers.get_by_name(name=DB_SERVER_NAME) print("Creating GITEA server") try: gitea_server = client.servers.create( name=GITEA_SERVER_NAME, server_type=ServerType(name=PROC), image=Image(name=SERVER_SYS), location=loc, networks=[Network(id=network.id)], user_data=""" #cloud-config package_update: true package_upgrade: true groups: - docker system_info: default_user: groups: [ docker ] packages: - apt-transport-https - ca-certificates - curl - gnupg - lsb-release - unattended-upgrades runcmd: - mkdir -p /etc/apt/keyrings - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg - echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null - apt-get update - apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin - systemctl enable docker - systemctl start docker - git clone https://git.wmi.amu.edu.pl/s434686/zpzc-3.git /zpzc-3 - docker compose -f /zpzc-3/gitea.yml up -d - echo 'Done' bootcmd: - docker compose -f /zpzc-3/gitea.yml up -d final_message: "The system is finally up, after $UPTIME seconds" """ ) _gitea_server = gitea_server.server print(f"{_gitea_server.id=} {_gitea_server.name=} {_gitea_server.status=}, {gitea_server.root_password} ") except APIException: gitea_server = client.servers.get_by_name(name=DB_SERVER_NAME) print(f"Server already exists: {gitea_server.name}") print("done")