{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "### Importy", "id": "3f003166664d0d93" }, { "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2024-11-21T16:58:01.734742Z", "start_time": "2024-11-21T16:58:01.556991Z" } }, "cell_type": "code", "source": [ "import os\n", "from dotenv import load_dotenv\n", "\n", "load_dotenv()\n", "\n", "from hcloud import Client\n", "from hcloud.images.domain import Image\n", "from hcloud.server_types.domain import ServerType\n", "from hcloud.networks.domain import NetworkSubnet\n", "from hcloud.locations.domain import Location" ], "id": "initial_id", "outputs": [], "execution_count": 1 }, { "metadata": {}, "cell_type": "markdown", "source": "### Zmienne", "id": "f6e400467ff0b4fa" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T16:58:01.765421Z", "start_time": "2024-11-21T16:58:01.751242Z" } }, "cell_type": "code", "source": [ "# Hetzner Cloud\n", "SERVER_TYPE = \"cx22\"\n", "IMAGE_UBUNTU = \"ubuntu-24.04\"\n", "API_KEY = os.getenv(\"API_KEY\")\n", "SSH_PUBKEY = os.getenv(\"SSH_PUBKEY\")\n", "PREFIX = \"s464863\"\n", "SSH_KEY_NAME = PREFIX\n", "IP_RANGE = \"10.10.10.0/24\"\n", "LOCATION = \"hel1\"\n", "NETWORK_NAME = f\"{PREFIX}-network\"\n", "DB_SERVER_NAME = f\"{PREFIX}-db\"\n", "VOLUME_NAME = f\"{PREFIX}-volume\"\n", "GITEA_SERVER_NAME = f\"{PREFIX}-gitea\"\n", "\n", "# Credentials\n", "MYSQL_ROOT_PASSWORD = os.getenv(\"MYSQL_ROOT_PASSWORD\")\n", "MYSQL_DATABASE = os.getenv(\"MYSQL_DATABASE\")\n", "MYSQL_USER = os.getenv(\"MYSQL_USER\")\n", "MYSQL_PASSWORD = os.getenv(\"MYSQL_PASSWORD\")" ], "id": "7c6135c6a124c6fd", "outputs": [], "execution_count": 2 }, { "metadata": {}, "cell_type": "markdown", "source": "### Interfejs do chmury", "id": "21a6ddf4c3a72ac2" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T16:58:10.096674Z", "start_time": "2024-11-21T16:58:10.087560Z" } }, "cell_type": "code", "source": [ "client = Client(\n", " token=API_KEY\n", ")" ], "id": "2dc34a0ff6d4b329", "outputs": [], "execution_count": 3 }, { "metadata": {}, "cell_type": "markdown", "source": "### Funkcje pomocnicze", "id": "7ef8e2846ce4ceac" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T16:58:10.985045Z", "start_time": "2024-11-21T16:58:10.975665Z" } }, "cell_type": "code", "source": [ "def delete_all_servers():\n", " servers = client.servers.get_all()\n", " for server in servers:\n", " if server.name.startswith(PREFIX):\n", " action = client.servers.delete(server)\n", " print(\n", " f\"Usuwanie serwera {server.data_model.name} ({server.data_model.public_net.ipv4.ip}): {action.data_model.status}\")" ], "id": "6eb2d5b262a900fd", "outputs": [], "execution_count": 4 }, { "metadata": {}, "cell_type": "markdown", "source": "### Klucz SSH", "id": "d31df3271a0375e1" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T16:58:13.237162Z", "start_time": "2024-11-21T16:58:12.914359Z" } }, "cell_type": "code", "source": [ "ssh_key = client.ssh_keys.get_by_name(\"broke\")\n", "if not ssh_key:\n", " ssh_key = client.ssh_keys.create(name=SSH_KEY_NAME, public_key=SSH_PUBKEY)\n", " print(f\"Klucz {ssh_key.data_model.name} został utworzony\")\n", "else:\n", " print(f\"Pomyślnie wczytano klucz: {ssh_key.data_model.name}\")" ], "id": "7812184e5ff7dd3a", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pomyślnie wczytano klucz: broke\n" ] } ], "execution_count": 5 }, { "metadata": {}, "cell_type": "markdown", "source": "### Sieć", "id": "e8b40788d6ccf603" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T16:58:14.500143Z", "start_time": "2024-11-21T16:58:14.281194Z" } }, "cell_type": "code", "source": [ "network = client.networks.get_by_name(NETWORK_NAME)\n", "if not network:\n", " network = client.networks.create(\n", " name=NETWORK_NAME,\n", " ip_range=IP_RANGE,\n", " subnets=[\n", " NetworkSubnet(ip_range=IP_RANGE, network_zone=\"eu-central\", type=\"cloud\")\n", " ]\n", " )\n", " print(f\"Sieć {network.data_model.name} została utworzona\")\n", "else:\n", " print(f\"Znaleziono sieć o zadanej nazwie: {network.data_model.name}\")" ], "id": "45b9674ed7e9d15c", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Znaleziono sieć o zadanej nazwie: s464863-network\n" ] } ], "execution_count": 6 }, { "metadata": {}, "cell_type": "markdown", "source": "### Wolumen", "id": "837c0a9fbce15b4a" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T17:32:03.361909Z", "start_time": "2024-11-21T17:32:03.082110Z" } }, "cell_type": "code", "source": [ "volume = client.volumes.get_by_name(VOLUME_NAME)\n", "if not volume:\n", " volume = client.volumes.create(\n", " size=10,\n", " name=VOLUME_NAME,\n", " location=Location(LOCATION),\n", " format=\"ext4\"\n", " )\n", " print(f\"Wolumen {VOLUME_NAME} został utworzony\")\n", "else:\n", " print(f\"Znaleziono wolumen o zadanej nazwie: {volume.data_model.name}\")" ], "id": "1ab418d3246a60ea", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Znaleziono wolumen o zadanej nazwie: s464863-volume\n" ] } ], "execution_count": 19 }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T17:32:05.686771Z", "start_time": "2024-11-21T17:32:04.662473Z" } }, "cell_type": "code", "source": [ "# Pobranie ID wolumenu - potrzebne do mapowania wolumenu w docker-compose.yml\n", "volume = client.volumes.get_by_name(VOLUME_NAME)\n", "VOLUME_ID = volume.data_model.id\n", "VOLUME_PATH = f\"/mnt/HC_Volume_{VOLUME_ID}\"\n", "print(f\"ID wolumenu: {VOLUME_ID}\")" ], "id": "47e671674c8fc432", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ID wolumenu: 101655125\n" ] } ], "execution_count": 20 }, { "metadata": {}, "cell_type": "markdown", "source": "### Baza danych", "id": "230997b301366c" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T17:12:54.873272Z", "start_time": "2024-11-21T17:12:54.861518Z" } }, "cell_type": "code", "source": [ "cloud_init_db = f'''#cloud-config\n", "packages:\n", " - apt-transport-https\n", " - ca-certificates\n", " - curl\n", " - gnupg-agent\n", " - software-properties-common\n", "\n", "write_files:\n", " - path: /root/docker-compose.yml\n", " content: |\n", " version: '3.9'\n", " services:\n", " db:\n", " image: mysql:5.7\n", " restart: always\n", " ports:\n", " - \"10.10.10.2:3306:3306\"\n", " environment:\n", " MYSQL_ROOT_PASSWORD: {MYSQL_ROOT_PASSWORD}\n", " MYSQL_DATABASE: {MYSQL_DATABASE}\n", " MYSQL_USER: {MYSQL_USER}\n", " MYSQL_PASSWORD: {MYSQL_PASSWORD}\n", " volumes:\n", " - db_data:/var/lib/mysql\n", " volumes:\n", " db_data: {{}}\n", "\n", "runcmd:\n", " - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -\n", " - add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\"\n", " - apt-get update -y\n", " - apt-get install -y docker-ce docker-ce-cli containerd.io\n", " - curl -L \"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose\n", " - chmod +x /usr/local/bin/docker-compose\n", " - systemctl start docker\n", " - systemctl enable docker\n", " - cd /root/ && docker-compose up -d\n", "'''" ], "id": "60f3510e656643a1", "outputs": [], "execution_count": 14 }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T17:13:00.753631Z", "start_time": "2024-11-21T17:12:55.591434Z" } }, "cell_type": "code", "source": [ "db_server = client.servers.create(\n", " name=DB_SERVER_NAME,\n", " server_type=ServerType(SERVER_TYPE),\n", " image=Image(name=IMAGE_UBUNTU),\n", " ssh_keys=[ssh_key],\n", " networks=[network],\n", " location=Location(LOCATION),\n", " user_data=cloud_init_db\n", ")\n", "\n", "db_server.action.wait_until_finished()\n", "print(f\"Serwer {DB_SERVER_NAME} został utworzony\")" ], "id": "5c0990eff071a623", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Serwer s464863-db został utworzony\n" ] } ], "execution_count": 15 }, { "metadata": {}, "cell_type": "markdown", "source": "### Gitea", "id": "10b7d087cc42e560" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T17:32:12.611234Z", "start_time": "2024-11-21T17:32:12.601080Z" } }, "cell_type": "code", "source": [ "cloud_init_gitea = f'''#cloud-config\n", "packages:\n", " - apt-transport-https\n", " - ca-certificates\n", " - curl\n", " - gnupg-agent\n", " - software-properties-common\n", "\n", "write_files:\n", " - path: /root/docker-compose.yml\n", " content: |\n", " version: '3.9'\n", " services:\n", " gitea:\n", " image: gitea/gitea:1.22.3\n", " volumes:\n", " - {VOLUME_PATH}:/data\n", " - /etc/timezone:/etc/timezone:ro\n", " - /etc/localtime:/etc/localtime:ro\n", " ports:\n", " - \"3000:3000\"\n", " - \"222:22\"\n", " restart: always\n", " environment:\n", " GITEA__database__DB_TYPE: mysql\n", " GITEA__database__HOST: \"10.10.10.2:3306\"\n", " GITEA__database__NAME: {MYSQL_DATABASE}\n", " GITEA__database__USER: {MYSQL_USER}\n", " GITEA__database__PASSWD: {MYSQL_PASSWORD}\n", "\n", "runcmd:\n", " - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -\n", " - add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\"\n", " - apt-get update -y\n", " - apt-get install -y docker-ce docker-ce-cli containerd.io\n", " - curl -L \"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose\n", " - chmod +x /usr/local/bin/docker-compose\n", " - systemctl start docker\n", " - systemctl enable docker\n", " - cd /root/ && docker-compose up -d\n", "'''" ], "id": "212f2d6a64597bdc", "outputs": [], "execution_count": 21 }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T17:32:24.838419Z", "start_time": "2024-11-21T17:32:18.056718Z" } }, "cell_type": "code", "source": [ "gitea_server = client.servers.create(\n", " name=f\"{GITEA_SERVER_NAME}\",\n", " server_type=ServerType(SERVER_TYPE),\n", " image=Image(name=IMAGE_UBUNTU),\n", " ssh_keys=[ssh_key],\n", " networks=[network],\n", " volumes=[volume],\n", " location=Location(LOCATION),\n", " user_data=cloud_init_gitea\n", ")\n", "\n", "gitea_server.action.wait_until_finished()\n", "print(f\"Serwer {GITEA_SERVER_NAME} został utworzony\")" ], "id": "42159c53789b87e", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Serwer s464863-gitea został utworzony\n" ] } ], "execution_count": 22 }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-21T17:54:09.122949Z", "start_time": "2024-11-21T17:54:09.112897Z" } }, "cell_type": "code", "source": "gitea_server.server.data_model.public_net.ipv4.ip", "id": "768b3d22dd3b3232", "outputs": [ { "data": { "text/plain": [ "'37.27.83.246'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 23 }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-20T20:01:24.112181Z", "start_time": "2024-11-20T20:01:22.339871Z" } }, "cell_type": "code", "source": "delete_all_servers()", "id": "ec5f64ced2185519", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Usuwanie serwera s464863-db (65.21.149.208): running\n", "Usuwanie serwera s464863-gitea (95.216.200.81): running\n" ] } ], "execution_count": 39 }, { "metadata": {}, "cell_type": "code", "source": "", "id": "7c6c20918f7001e7", "outputs": [], "execution_count": null } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }