{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "### Importy", "id": "3f003166664d0d93" }, { "metadata": { "collapsed": true }, "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": null }, { "metadata": {}, "cell_type": "markdown", "source": "### Zmienne", "id": "f6e400467ff0b4fa" }, { "metadata": {}, "cell_type": "code", "source": [ "SERVER_TYPE = \"cx22\"\n", "IMAGE_UBUNTU = \"ubuntu-24.04\"\n", "IMAGE_DOCKER = \"docker-ce\"\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\"" ], "id": "7c6135c6a124c6fd", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": "### Interfejs do chmury", "id": "21a6ddf4c3a72ac2" }, { "metadata": {}, "cell_type": "code", "source": [ "client = Client(\n", " token=API_KEY\n", ")" ], "id": "2dc34a0ff6d4b329", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": "### Funkcje pomocnicze", "id": "7ef8e2846ce4ceac" }, { "metadata": {}, "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": null }, { "metadata": {}, "cell_type": "markdown", "source": "### Klucz SSH", "id": "d31df3271a0375e1" }, { "metadata": {}, "cell_type": "code", "source": [ "ssh_key = client.ssh_keys.get_by_name(SSH_KEY_NAME)\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": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": "### Sieć", "id": "e8b40788d6ccf603" }, { "metadata": {}, "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": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": "### Wolumen", "id": "837c0a9fbce15b4a" }, { "metadata": {}, "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", " automount=False\n", " )\n", "print(f\"Wolumen {VOLUME_NAME} został utworzony\")" ], "id": "1ab418d3246a60ea", "outputs": [], "execution_count": null }, { "metadata": {}, "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}\"" ], "id": "47e671674c8fc432", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": "### Baza danych", "id": "230997b301366c" }, { "metadata": {}, "cell_type": "code", "source": [ "cloud_init_db = r'''#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: gitea\n", " MYSQL_DATABASE: gitea\n", " MYSQL_USER: gitea\n", " MYSQL_PASSWORD: gitea\n", " volumes:\n", " - db_data:/var/lib/mysql\n", " \n", " phpmyadmin:\n", " image: phpmyadmin\n", " restart: always\n", " ports:\n", " - \"8080:80\"\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": null }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-20T17:20:46.112931Z", "start_time": "2024-11-20T17:20:40.905064Z" } }, "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": 131 }, { "metadata": {}, "cell_type": "markdown", "source": "### Gitea", "id": "10b7d087cc42e560" }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-20T17:23:14.455275Z", "start_time": "2024-11-20T17:23:14.444015Z" } }, "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: gitea\n", " GITEA__database__USER: gitea\n", " GITEA__database__PASSWD: gitea\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": 132 }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-20T17:23:21.444861Z", "start_time": "2024-11-20T17:23:15.533075Z" } }, "cell_type": "code", "source": [ "gitea_server = client.servers.create(\n", " name=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": 133 }, { "metadata": {}, "cell_type": "code", "source": "delete_all_servers()", "id": "ec5f64ced2185519", "outputs": [], "execution_count": null }, { "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 }