From 376e9fe88fe5a60ae62ff6658ea094a35557938e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Raczy=C5=84ski?= Date: Sat, 23 Nov 2024 19:53:34 +0100 Subject: [PATCH] first commit --- README.md | 0 delete.py | 35 ++++++++++++ deploy.py | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ deploy.sh | 5 ++ 4 files changed, 196 insertions(+) create mode 100644 README.md create mode 100644 delete.py create mode 100644 deploy.py create mode 100644 deploy.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/delete.py b/delete.py new file mode 100644 index 0000000..86a6f97 --- /dev/null +++ b/delete.py @@ -0,0 +1,35 @@ +from hcloud import Client +client = Client( + token="V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK" +) +PREFIX = "464962" + +lbs = client.load_balancers.get_all() +print(f"Usuwanie {len(lbs)} mechanizmów LB") +for s in lbs: + if s.data_model.name.startswith(PREFIX): + action = client.load_balancers.delete(s) + print(f"\tUsuwanie LB {s.data_model.name}: {action}") + +servers = client.servers.get_all() +print(f"Usuwanie {len(servers)} serwerów") +for s in servers: + if s.data_model.name.startswith(PREFIX): + action = client.servers.delete(s) + print(f"\tUsuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}") + + +vnets = client.networks.get_all() +print(f"Usuwanie {len(vnets)} sieci wirtualnych") +for s in vnets: + if s.data_model.name.startswith(PREFIX): + action = client.networks.delete(s) + print(f"\tUsuwanie sieci wirtualnej {s.name}: {action}") + +volumes = client.volumes.get_all() +print(f"Usuwanie {len(volumes)} wolumenów") +for v in volumes: + if s.data_model.name.startswith(PREFIX): + action = client.volumes.delete(v) + print(f"\tUsuwanie wolumenu {v.name}: {action}") + diff --git a/deploy.py b/deploy.py new file mode 100644 index 0000000..4562227 --- /dev/null +++ b/deploy.py @@ -0,0 +1,156 @@ +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="V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK" +) +PREFIX = "464962" + +ssh_key = client.ssh_keys.get_by_name(name="kr-pzc-ssh-key") +# print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}") + +vnet_name = f"{PREFIX}-pzc-test-vnet" + +vnet = client.networks.get_by_name(vnet_name) or None +if not vnet: + vnet = client.networks.create( + name=vnet_name, + 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})") + +cloud_init_db = 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 + ports: + - "10.10.10.2:3306:3306" + environment: + MYSQL_ROOT_PASSWORD: notSecureChangeMe + MYSQL_DATABASE: gitea + MYSQL_USER: gitea + MYSQL_PASSWORD: gitea + volumes: + - db_data:/var/lib/mysql + + phpmyadmin: + image: phpmyadmin + restart: always + + 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=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 +) + +volume_name = f"{PREFIX}-gitea-vol" + +volume = client.volumes.get_by_name(volume_name) +if not volume: + volume = client.volumes.create( + size=10, + name=volume_name, + location=Location("hel1"), + format="ext4" + ) + # print(f"Wolumen {volume_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: + - /mnt/{volume_name}/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: gitea + GITEA__database__USER: gitea + GITEA__database__PASSWD: gitea + +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"{PREFIX}-gitea", + server_type=ServerType("cpx11"), + image=Image(name="ubuntu-22.04"), + ssh_keys=[ssh_key], + networks=[vnet], + location=Location("hel1"), + user_data=cloud_init_gitea +) + +gitea_server.action.wait_until_finished() +# print(f"Tworzenie serwera wordpress-1: {gitea_server.action.complete}") + +gitea_server = client.servers.get_by_name(f"{PREFIX}-gitea") +# print(f"Serwer: {gitea_server.data_model.name}\n\tpubliczne IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {gitea_server.data_model.private_net[0].ip}") +print(f"{gitea_server.data_model.public_net.ipv4.ip}") diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..b8d6d5e --- /dev/null +++ b/deploy.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +chmod +x deploy.py +pip install hcloud +python deploy.py \ No newline at end of file