commit fe475e7a8565feb9df795927fb4f601d97d42a1b Author: PawelDopierala Date: Mon Nov 25 22:43:31 2024 +0100 first commit diff --git a/3_2.py b/3_2.py new file mode 100644 index 0000000..35df481 --- /dev/null +++ b/3_2.py @@ -0,0 +1,172 @@ +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 +import sys + +# TOKEN = "V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK" +TOKEN = sys.argv[1] + +client = Client( + token=TOKEN +) +PREFIX = "PD" + +# YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzIOEX3AABgr3ZKLFcvZYkKr10JJjD0+AXlXoJ5CfpNw56SVj17Gh/Qbag4QeB8eJYaJxHV+TPhIOtHNxDJFlcVXUKPCBDErlIFYIJ8t0uhqvssd9r6XCZxvDjTC20FNayG+Zr1adB3BYe501I3JB9KiGZxg9juh/lY/1jZyqjjPzt8vLE1M2j6C7rbLAlIOub37B8OC7SGsy/nz4Ub5kS3Jxh9Trmr6vtuXa5WnASIaqX+3jl9hBhsg55j5WKUrGM1BOEKmwYxFJEH7bY0NjmcNAeaw1mm5bxERSW6EFXX3hEk+kx5zuFbO1LXDBh3HP5FyjwOXDKY70aLfVVpdGj" +YOUR_LOCAL_SSH_PUBKEY = sys.argv[2] + +ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key", public_key=YOUR_LOCAL_SSH_PUBKEY) +print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}") + +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 + 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("cpx11"), + image=Image(name="ubuntu-22.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\tprywatne IP: {db_server.data_model.private_net[0].ip}") + +volume = client.volumes.create( + name=f"{PREFIX}-gitea-volume", + size=10, # GB + location=Location("hel1"), + format="ext4" +) + +volume_id = volume.volume.id + +cloud_init_gitea = f'''#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:latest + environment: + USER_UID: 1000 + USER_GID: 1000 + 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 + volumes: + - /mnt/{PREFIX}-gitea-volume:/data + - /etc/timezone:/etc/timezone:ro + - /etc/localtime:/etc/localtime:ro + ports: + - "3000:3000" + - "222:22" + restart: always + +# 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 + - mkfs.ext4 -F /dev/disk/by-id/scsi-0HC_Volume_{volume_id} + - mkdir /mnt/{PREFIX}-gitea-volume + - mount -o discard,defaults /dev/disk/by-id/scsi-0HC_Volume_{volume_id} /mnt/{PREFIX}-gitea-volume + - echo "/dev/disk/by-id/scsi-0HC_Volume_{volume_id} /mnt/{PREFIX}-gitea-volume ext4 discard,nofail,defaults 0 0" >> /etc/fstab + - 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, + volumes=[volume.volume] +) + +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}") diff --git a/3_2_delete.py b/3_2_delete.py new file mode 100644 index 0000000..bf9d7d5 --- /dev/null +++ b/3_2_delete.py @@ -0,0 +1,29 @@ +from hcloud import Client + +client = Client( + token="V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK" +) +PREFIX = "PD" + +YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzIOEX3AABgr3ZKLFcvZYkKr10JJjD0+AXlXoJ5CfpNw56SVj17Gh/Qbag4QeB8eJYaJxHV+TPhIOtHNxDJFlcVXUKPCBDErlIFYIJ8t0uhqvssd9r6XCZxvDjTC20FNayG+Zr1adB3BYe501I3JB9KiGZxg9juh/lY/1jZyqjjPzt8vLE1M2j6C7rbLAlIOub37B8OC7SGsy/nz4Ub5kS3Jxh9Trmr6vtuXa5WnASIaqX+3jl9hBhsg55j5WKUrGM1BOEKmwYxFJEH7bY0NjmcNAeaw1mm5bxERSW6EFXX3hEk+kx5zuFbO1LXDBh3HP5FyjwOXDKY70aLfVVpdGj" + +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}: {action.data_model.status}") + +ssh_keys = client.ssh_keys.get_all() +print(f"Usuwanie {len(ssh_keys)} kluczy SSH") +for s in ssh_keys: + if s.data_model.name.startswith(PREFIX): + action = client.ssh_keys.delete(s) + print(f"\tUsuwanie klucza {s.name}: {action}") + +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}") diff --git a/README.md b/README.md new file mode 100644 index 0000000..3abb8ca --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +W celu skonfigorowania środowiska, możesz ustawić następujące zmienne w pliku `deploy.sh`: +- HETZNER_TOKEN +- SH_PUBLIC_KEY \ No newline at end of file diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..2b7f1ab --- /dev/null +++ b/deploy.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +HETZNER_TOKEN="V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK" +SSH_PUBLIC_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzIOEX3AABgr3ZKLFcvZYkKr10JJjD0+AXlXoJ5CfpNw56SVj17Gh/Qbag4QeB8eJYaJxHV+TPhIOtHNxDJFlcVXUKPCBDErlIFYIJ8t0uhqvssd9r6XCZxvDjTC20FNayG+Zr1adB3BYe501I3JB9KiGZxg9juh/lY/1jZyqjjPzt8vLE1M2j6C7rbLAlIOub37B8OC7SGsy/nz4Ub5kS3Jxh9Trmr6vtuXa5WnASIaqX+3jl9hBhsg55j5WKUrGM1BOEKmwYxFJEH7bY0NjmcNAeaw1mm5bxERSW6EFXX3hEk+kx5zuFbO1LXDBh3HP5FyjwOXDKY70aLfVVpdGj" + +pip install hcloud --quiet + +python3 3_2.py $HETZNER_TOKEN $SSH_PUBLIC_KEY \ No newline at end of file