commit b7d1d6d038caa3085c6dd1731e0d55a7f88ad20f Author: AWieczarek Date: Wed Nov 20 18:09:07 2024 +0100 Added 3.2 solution diff --git a/README.md b/README.md new file mode 100644 index 0000000..4af8467 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# s464979-hetzner + +W repozytorium znajdują się rozwiązania do zadań 3.2 (+ 3.1) oraz 3.5 z przedmiotu Praktyczne Zastosowania Chmury Obliczeniowej. + +## Instrukcja uruchomienia zadania 3.2 + +1. Należy sklonować repozytorium. + +```bash +git clone +``` + +2. Przejdź do katalogu z zadaniem 3.2. + +```bash +cd s464979-hetzner/3.2 +``` + +3. Nadaj uprawnienia do uruchomienia skryptu `deploy.sh`. + +```bash +chmod +x deploy.sh +``` + +4. Uruchom skrypt `deploy.sh`. + +```bash +./deploy.sh +``` + +Program wypisze na ekranie adres IP serwera w postaci: + +``` +Serwer: 464979-gitea + publiczne IP: {adres_ip} + prywatne IP: {prywatny_adres_ip} +``` + +Po pomyślnym zakończeniu skryptu, aplikacja będzie dostępna pod adresem `http://{adres_ip}:3000` (uruchomienie i automatyczna konfiguracja serwerów zajmuje około minuty, należy odczekać wskazany czas przed przejściem na wskazany adres). \ No newline at end of file diff --git a/db_cloud_init.yml b/db_cloud_init.yml new file mode 100644 index 0000000..15ede77 --- /dev/null +++ b/db_cloud_init.yml @@ -0,0 +1,45 @@ +#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 \ No newline at end of file diff --git a/delte_resources_with_prefix.py b/delte_resources_with_prefix.py new file mode 100644 index 0000000..6e5ec3a --- /dev/null +++ b/delte_resources_with_prefix.py @@ -0,0 +1,34 @@ +from hcloud import Client + +client = Client( + token="oiA3vdWArVn67e4kHdyIKjDdnGNQGxEJJUI0ln8GrTsZjs10cmyAkqwjC3ufdZC8" +) +PREFIX = "464979" + +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.sh b/deploy.sh new file mode 100644 index 0000000..9618836 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +chmod +x gitea_config.py + +pip install hcloud + +python3 gitea_config.py \ No newline at end of file diff --git a/gitea_cloud_init.yml b/gitea_cloud_init.yml new file mode 100644 index 0000000..5ffbd83 --- /dev/null +++ b/gitea_cloud_init.yml @@ -0,0 +1,50 @@ +#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: + server: + image: gitea/gitea:latest + + container_name: gitea + + 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 + + restart: always + + volumes: + - ./gitea:/data + - /etc/timezone:/etc/timezone:ro + - /etc/localtime:/etc/localtime:ro + + ports: + - "3000:3000" + - "222:22" + +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 \ No newline at end of file diff --git a/gitea_config.py b/gitea_config.py new file mode 100644 index 0000000..32ed60f --- /dev/null +++ b/gitea_config.py @@ -0,0 +1,65 @@ +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="oiA3vdWArVn67e4kHdyIKjDdnGNQGxEJJUI0ln8GrTsZjs10cmyAkqwjC3ufdZC8" +) +PREFIX = "464979" + +ssh_key = client.ssh_keys.get_by_name(name="464979-pzc-ssh-key") +print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}") + +vnet = client.networks.create( + name=f"{PREFIX}-gitea-net", + 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})") + + +with open("db_cloud_init.yml", 'r', encoding='utf-8') as file: + cloud_init_db = file.read() + +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\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}") + + + + +with open("gitea_cloud_init.yml", 'r', encoding='utf-8') as file: + cloud_init_wp1 = file.read() + +wp1_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_wp1 +) + +wp1_server.action.wait_until_finished() +print(f"Tworzenie serwera Gitea: {wp1_server.action.complete}") + +wp1_server = client.servers.get_by_name(f"{PREFIX}-gitea") +print( + f"Serwer: {wp1_server.data_model.name}\n\tpubliczne IP: {wp1_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {wp1_server.data_model.private_net[0].ip}")