diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77b3ac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +token.txt \ No newline at end of file diff --git a/clean.py b/clean.py new file mode 100644 index 0000000..074345d --- /dev/null +++ b/clean.py @@ -0,0 +1,15 @@ +from hcloud import Client + +with open("token.txt", "r") as file: + api_token = file.read().strip() + +client = Client(token=api_token) + +PREFIX = "s478873" + +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"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}") \ No newline at end of file diff --git a/create_gitea.py b/create_gitea.py new file mode 100644 index 0000000..47e40d0 --- /dev/null +++ b/create_gitea.py @@ -0,0 +1,160 @@ +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 +from pathlib import Path + +with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file: + ssh_pub = file.readline() + +with open("token.txt", "r") as file: + api_token = file.read().strip() + +client = Client(token=api_token) + +PREFIX = "s478873" + +ssh_name = f"{PREFIX}-pzc-ssh-key" +ssh_key = client.ssh_keys.get_by_name(ssh_name) or None +if not ssh_key: + ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key", public_key=ssh_pub) +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=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"Utworzono sieć wirtualną {vnet.data_model.name} ({vnet.data_model.ip_range})") + +cloud_init_db=r''' +#cloud-config +# lista podstawowych pakietów, które należy zainstalować +packages: + - apt-transport-https + - ca-certificates + - curl + - gnupg-agent + - software-properties-common +# tworzymy plik docker-compose.yml +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 + # przekierowanie portów zostawione tylko dla przykładu, nie należy tak robić na produkcji + ports: + - "8080:80" + volumes: + db_data: {} +# instalujemy docker i docker-compose, a następnie uruchamiamy naszą bazę danych +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("cx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key], + networks=[vnet], + location=Location("hel1"), + user_data=cloud_init_db +) + +db_server.action.wait_until_finished() +print(f"Tworzenie serwera db: {db_server.action.complete}") + +db_server = client.servers.get_by_name(f"{PREFIX}-db") +print(f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}") + +cloud_init_gitea=r'''#cloud-config +# lista podstawowych pakietów, które należy zainstalować +packages: + - apt-transport-https + - ca-certificates + - curl + - gnupg-agent + - software-properties-common +# tworzymy plik docker-compose.yml +write_files: + - path: /root/docker-compose.yml + content: | + version: '3.9' + services: + server: + image: gitea/gitea:1.15.6-rootless + 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: notSecureChangeMe + restart: always + volumes: + - ./data:/root/gitea + - ./config:/root/gitea/config + - /etc/timezone:/etc/timezone:ro + - /etc/localtime:/etc/localtime:ro + - /mnt/volume:/data + ports: + - "3000:3000" + - "222:22" +# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych +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("cx11"), + image=Image(name="ubuntu-20.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 gitea: {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}") \ No newline at end of file diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..7c798b8 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,5 @@ +#!/bin/bash +pip install hcloud + +python3 clean.py +python3 create_gitea.py \ No newline at end of file