commit 2653334d998663ff5eb2b148abfba5ee06fc2882 Author: Kosinsk Date: Mon Dec 9 20:21:07 2024 +0100 Add LAB2 diff --git a/cloud.ipynb b/cloud.ipynb new file mode 100644 index 0000000..c7b9f42 --- /dev/null +++ b/cloud.ipynb @@ -0,0 +1,416 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## LAB2 - s444820 - Krystian Osiński\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. define constants and client:" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [], + "source": [ + "#Config\n", + "TOKEN = \"ZPN0YNl3TpYGe1xfxvFzV4zPOBVIEZTunC8Cg26JI1MEA9cso6O5Ydrin2wQnGxh\"\n", + "PREFIX = \"s444820\"\n", + "SSH_PUBKEY = \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDUSprPwtOEBCrPdf3pDhtwpWN2SNxemlrXvglqqL+kKGjnfP3RO7CIcjw7etQDC+2FIWaiqToWPSAKrxi4w235gPlT8GPvoKb1Wwv4BRMWiIr20bTiASorojzCQOlgb5ZShZH/opsWQ5geP9M26mkpNF5Ev/OMIKzfS6d6RHs1ZyX+81wfDhK2oxe4kAHTU8oQB/C7dGpeT4GohnG/SIj7ChoT10+Vkr0h+/yL9gVJUdnELacA29wBR+qhfzAP7YrSPwEtaFZxQ1rwOpJ39niljKfqPIxqA0ZOsNZnkCuTUMwMnQRFyO21x8nDzJe/WG95ptXiN4huPZVo0WQK7bfz5od8IY50Rm0XtCyjG0cVu0HSdJJzMgpluu4YQRvvs3xnwvknuT1G8prydJCDzNJ1nN8gnIEd4EymUPCoLRsc0s1fBvkX+t11cqnVvvTlCsBFzKYFzsKpSWRfFrwAfog6nOPb/3kAW8nj5QJinEByDkj9BtR2bCmLwp9DjlRokvDbijVVdEd8Ia5yyri9R9RRufmzmI5epe4PHCfzG6n3Wl4kj9+YHWDrg10x2IZo1zdzDKbH2cNqzS5c8HD2yjrVy7UkOKZTwlL/CHwIGqBQ9pMS0qgNpBJG/C6Y7MVQJEgP63IgtxSWuhGXwKbTuQnz0ROxlbrjdeMPVxH+6O0iDQ== kryosi@st.amu.edu.pl\"" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [], + "source": [ + "from hcloud import Client\n", + "client = Client(\n", + " token=TOKEN\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "\n", + "## 2. Add ssh key" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Klucz s444820-ssh-key został dodany: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDUSprPwtOEBCrPdf3pDhtwpWN2SNxemlrXvglqqL+kKGjnfP3RO7CIcjw7etQDC+2FIWaiqToWPSAKrxi4w235gPlT8GPvoKb1Wwv4BRMWiIr20bTiASorojzCQOlgb5ZShZH/opsWQ5geP9M26mkpNF5Ev/OMIKzfS6d6RHs1ZyX+81wfDhK2oxe4kAHTU8oQB/C7dGpeT4GohnG/SIj7ChoT10+Vkr0h+/yL9gVJUdnELacA29wBR+qhfzAP7YrSPwEtaFZxQ1rwOpJ39niljKfqPIxqA0ZOsNZnkCuTUMwMnQRFyO21x8nDzJe/WG95ptXiN4huPZVo0WQK7bfz5od8IY50Rm0XtCyjG0cVu0HSdJJzMgpluu4YQRvvs3xnwvknuT1G8prydJCDzNJ1nN8gnIEd4EymUPCoLRsc0s1fBvkX+t11cqnVvvTlCsBFzKYFzsKpSWRfFrwAfog6nOPb/3kAW8nj5QJinEByDkj9BtR2bCmLwp9DjlRokvDbijVVdEd8Ia5yyri9R9RRufmzmI5epe4PHCfzG6n3Wl4kj9+YHWDrg10x2IZo1zdzDKbH2cNqzS5c8HD2yjrVy7UkOKZTwlL/CHwIGqBQ9pMS0qgNpBJG/C6Y7MVQJEgP63IgtxSWuhGXwKbTuQnz0ROxlbrjdeMPVxH+6O0iDQ== kryosi@st.amu.edu.pl\n" + ] + } + ], + "source": [ + "ssh_key = client.ssh_keys.create(name=f\"{PREFIX}-ssh-key\", public_key=SSH_PUBKEY)\n", + "print(f\"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "\n", + "### 3. Create vNet " + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Utworzono sieć wirtualną s444820-vnet (10.10.10.0/24)\n" + ] + } + ], + "source": [ + "from hcloud.networks.domain import NetworkSubnet\n", + "\n", + "vnet = client.networks.create(\n", + " name=f\"{PREFIX}-vnet\", \n", + " ip_range=\"10.10.10.0/24\", \n", + " subnets=[\n", + " NetworkSubnet(ip_range=\"10.10.10.0/24\", network_zone=\"eu-central\", type=\"cloud\")\n", + " ]\n", + ")\n", + "\n", + "\n", + "print(f\"Utworzono sieć wirtualną {vnet.data_model.name} ({vnet.data_model.ip_range})\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "\n", + "### 4. Create DB VPS (PostgreSQL)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "cloud_init_db=f'''#cloud-config\n", + "\n", + "write_files:\n", + " - path: /root/docker-compose.yml\n", + " content: |\n", + " version: '3.9'\n", + "\n", + " services:\n", + " db:\n", + " image: postgres:14\n", + " restart: always\n", + " ports:\n", + " - \"10.10.10.2:5432:5432\"\n", + " environment:\n", + " - POSTGRES_USER=gitea\n", + " - POSTGRES_PASSWORD=gitea\n", + " - POSTGRES_DB=gitea\n", + " volumes:\n", + " - ./DB:/var/lib/postgresql/data\n", + "\n", + "runcmd:\n", + " - apt update\n", + " - apt install -y docker.io docker-compose\n", + " - docker-compose -f /root/docker-compose.yml up -d\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Utworzono serwer: s444820-DB (37.27.197.45) 10.10.10.2\n" + ] + } + ], + "source": [ + "from hcloud.images.domain import Image\n", + "from hcloud.server_types.domain import ServerType\n", + "from hcloud.locations import Location\n", + "\n", + "db_server = client.servers.create(\n", + " name=f\"{PREFIX}-DB\", server_type=ServerType(\"cx22\"), image=Image(name=\"ubuntu-24.04\"),location=Location(\"hel1\"),ssh_keys=[ssh_key],networks=[vnet],user_data=cloud_init_db \n", + ")\n", + "\n", + "db_server.action.wait_until_finished()\n", + "print(f\"Utworzono serwer: {db_server.server.data_model.name} ({db_server.server.data_model.public_net.ipv4.ip}) {db_server.server.data_model.private_net[0].ip}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "\n", + "### 5. Create volume:" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Utworzono wolumen: s444820-volume (10 GB)\n" + ] + } + ], + "source": [ + "from hcloud.volumes.domain import Volume\n", + "\n", + "volume = client.volumes.create(\n", + " name=f\"{PREFIX}-volume\",size=10,location=Location(\"hel1\")\n", + ")\n", + "\n", + "print(f\"Utworzono wolumen: {volume.volume.data_model.name} ({volume.volume.data_model.size} GB)\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6. Create Gitea VPS" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [], + "source": [ + "cloud_init_gitea=f'''#cloud-config\n", + "\n", + "write_files:\n", + " - path: /root/docker-compose.yml\n", + " content: |\n", + " version: \"3\"\n", + " services:\n", + " server:\n", + " image: gitea/gitea:1.14.5-rootless\n", + " environment:\n", + " - GITEA__database__DB_TYPE=postgres\n", + " - GITEA__database__HOST={db_server.server.data_model.private_net[0].ip}\n", + " - GITEA__database__NAME=gitea\n", + " - GITEA__database__USER=gitea\n", + " - GITEA__database__PASSWD=gitea\n", + " restart: always\n", + " volumes:\n", + " - /mnt/HC_Volume_101746670/data:/var/lib/gitea\n", + " - /mnt/HC_Volume_101746670/config:/etc/gitea\n", + " ports:\n", + " - \"3000:3000\"\n", + " - \"2222:2222\"\n", + "\n", + "runcmd:\n", + " - apt update\n", + " - apt install -y docker.io docker-compose\n", + " - mkdir -p /mnt/HC_Volume_101746670/data\n", + " - mkdir -p /mnt/HC_Volume_101746670/config\n", + " - sudo chmod -R 777 /mnt/HC_Volume_101746670/data\n", + " - sudo chmod -R 777 /mnt/HC_Volume_101746670/config\n", + " - docker-compose -f /root/docker-compose.yml up -d\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tworzenie serwera db: True\n" + ] + } + ], + "source": [ + "from hcloud.locations.domain import Location\n", + "\n", + "gitea_server = client.servers.create(\n", + " name=f\"{PREFIX}-gitea-server\", \n", + " server_type=ServerType(\"cx22\"), \n", + " image=Image(name=\"ubuntu-24.04\"), \n", + " ssh_keys=[ssh_key], \n", + " networks=[vnet], \n", + " location=Location(\"hel1\"), \n", + " volumes=[volume.volume],\n", + " user_data=cloud_init_gitea\n", + ")\n", + "\n", + "gitea_server.action.wait_until_finished()\n", + "print(f\"Tworzenie serwera db: {gitea_server.action.complete}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "\n", + "### 7. Task verification: (Wait a moment)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Otwórz stronę w przeglądarce: http://65.21.53.29:3000\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import webbrowser\n", + "\n", + "gitea_url = f\"http://{gitea_server.server.data_model.public_net.ipv4.ip}:3000\"\n", + "print(f\"Otwórz stronę w przeglądarce: {gitea_url}\")\n", + "webbrowser.open(gitea_url)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "\n", + "## 8. Resource release:" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Usuwanie 2 serwerów\n", + "\tUsuwanie serwera s444820-DB (37.27.197.45): running\n", + "\tUsuwanie serwera s444820-gitea-server (65.21.53.29): running\n", + "Usuwanie 1 kluczy SSH\n", + "\tUsuwanie klucza s444820-ssh-key: True\n", + "Usuwanie 1 sieci wirtualnych\n", + "\tUsuwanie sieci wirtualnej s444820-vnet: True\n", + "Usuwanie 1 wolumenów\n", + "\tUsuwanie wolumenu s444820-volume: True\n" + ] + } + ], + "source": [ + "servers = client.servers.get_all()\n", + "print(f\"Usuwanie {len(servers)} serwerów\")\n", + "for s in servers:\n", + " if s.data_model.name.startswith(PREFIX):\n", + " action = client.servers.delete(s)\n", + " print(f\"\\tUsuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}\")\n", + " action.wait_until_finished()\n", + "\n", + "\n", + "ssh_keys = client.ssh_keys.get_all()\n", + "print(f\"Usuwanie {len(ssh_keys)} kluczy SSH\")\n", + "for s in ssh_keys:\n", + " if s.data_model.name.startswith(PREFIX):\n", + " action = client.ssh_keys.delete(s)\n", + " print(f\"\\tUsuwanie klucza {s.name}: {action}\")\n", + "\n", + "vnets = client.networks.get_all()\n", + "print(f\"Usuwanie {len(vnets)} sieci wirtualnych\")\n", + "for s in vnets:\n", + " if s.data_model.name.startswith(PREFIX):\n", + " action = client.networks.delete(s)\n", + " print(f\"\\tUsuwanie sieci wirtualnej {s.name}: {action}\")\n", + "\n", + "volumes = client.volumes.get_all()\n", + "print(f\"Usuwanie {len(volumes)} wolumenów\")\n", + "for v in volumes:\n", + " if s.data_model.name.startswith(PREFIX):\n", + " action = client.volumes.delete(v)\n", + " print(f\"\\tUsuwanie wolumenu {v.name}: {action}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/costs.pdf b/costs.pdf new file mode 100644 index 0000000..305a388 Binary files /dev/null and b/costs.pdf differ