hetzner-gitea/hetzner.ipynb

529 lines
14 KiB
Plaintext
Raw Permalink Normal View History

2024-11-20 18:26:45 +01:00
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "### Importy",
"id": "3f003166664d0d93"
},
{
"metadata": {
2024-11-20 20:16:29 +01:00
"collapsed": true,
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T16:58:01.734742Z",
"start_time": "2024-11-21T16:58:01.556991Z"
2024-11-20 20:16:29 +01:00
}
2024-11-20 18:26:45 +01:00
},
"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": [],
2024-11-21 19:04:57 +01:00
"execution_count": 1
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Zmienne",
"id": "f6e400467ff0b4fa"
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T16:58:01.765421Z",
"start_time": "2024-11-21T16:58:01.751242Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"cell_type": "code",
"source": [
2024-11-20 20:16:29 +01:00
"# Hetzner Cloud\n",
2024-11-20 18:26:45 +01:00
"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",
2024-11-20 20:16:29 +01:00
"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\")"
2024-11-20 18:26:45 +01:00
],
"id": "7c6135c6a124c6fd",
"outputs": [],
2024-11-21 19:04:57 +01:00
"execution_count": 2
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Interfejs do chmury",
"id": "21a6ddf4c3a72ac2"
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T16:58:10.096674Z",
"start_time": "2024-11-21T16:58:10.087560Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"cell_type": "code",
"source": [
"client = Client(\n",
" token=API_KEY\n",
")"
],
"id": "2dc34a0ff6d4b329",
"outputs": [],
2024-11-20 20:16:29 +01:00
"execution_count": 3
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Funkcje pomocnicze",
"id": "7ef8e2846ce4ceac"
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T16:58:10.985045Z",
"start_time": "2024-11-21T16:58:10.975665Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"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": [],
2024-11-20 20:16:29 +01:00
"execution_count": 4
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Klucz SSH",
"id": "d31df3271a0375e1"
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T16:58:13.237162Z",
"start_time": "2024-11-21T16:58:12.914359Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"cell_type": "code",
"source": [
2024-11-21 19:04:57 +01:00
"ssh_key = client.ssh_keys.get_by_name(\"broke\")\n",
2024-11-20 18:26:45 +01:00
"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",
2024-11-20 20:16:29 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-11-21 19:04:57 +01:00
"Pomyślnie wczytano klucz: broke\n"
2024-11-20 20:16:29 +01:00
]
}
],
"execution_count": 5
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Sieć",
"id": "e8b40788d6ccf603"
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T16:58:14.500143Z",
"start_time": "2024-11-21T16:58:14.281194Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"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",
2024-11-20 20:16:29 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Znaleziono sieć o zadanej nazwie: s464863-network\n"
]
}
],
"execution_count": 6
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Wolumen",
"id": "837c0a9fbce15b4a"
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T17:32:03.361909Z",
"start_time": "2024-11-21T17:32:03.082110Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"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",
2024-11-21 19:04:57 +01:00
" format=\"ext4\"\n",
2024-11-20 18:26:45 +01:00
" )\n",
2024-11-21 19:04:57 +01:00
" print(f\"Wolumen {VOLUME_NAME} został utworzony\")\n",
"else:\n",
" print(f\"Znaleziono wolumen o zadanej nazwie: {volume.data_model.name}\")"
2024-11-20 18:26:45 +01:00
],
"id": "1ab418d3246a60ea",
2024-11-20 20:16:29 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-11-21 19:04:57 +01:00
"Znaleziono wolumen o zadanej nazwie: s464863-volume\n"
2024-11-20 20:16:29 +01:00
]
}
],
2024-11-21 19:04:57 +01:00
"execution_count": 19
2024-11-20 18:26:45 +01:00
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T17:32:05.686771Z",
"start_time": "2024-11-21T17:32:04.662473Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"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",
2024-11-21 19:04:57 +01:00
"VOLUME_PATH = f\"/mnt/HC_Volume_{VOLUME_ID}\"\n",
2024-11-20 20:16:29 +01:00
"print(f\"ID wolumenu: {VOLUME_ID}\")"
2024-11-20 18:26:45 +01:00
],
"id": "47e671674c8fc432",
2024-11-20 20:16:29 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-11-21 19:04:57 +01:00
"ID wolumenu: 101655125\n"
2024-11-20 20:16:29 +01:00
]
}
],
2024-11-21 19:04:57 +01:00
"execution_count": 20
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Baza danych",
"id": "230997b301366c"
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T17:12:54.873272Z",
"start_time": "2024-11-21T17:12:54.861518Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"cell_type": "code",
"source": [
2024-11-20 20:16:29 +01:00
"cloud_init_db = f'''#cloud-config\n",
2024-11-20 18:26:45 +01:00
"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",
2024-11-20 20:16:29 +01:00
" MYSQL_ROOT_PASSWORD: {MYSQL_ROOT_PASSWORD}\n",
" MYSQL_DATABASE: {MYSQL_DATABASE}\n",
" MYSQL_USER: {MYSQL_USER}\n",
" MYSQL_PASSWORD: {MYSQL_PASSWORD}\n",
2024-11-20 18:26:45 +01:00
" volumes:\n",
" - db_data:/var/lib/mysql\n",
" volumes:\n",
2024-11-20 20:16:29 +01:00
" db_data: {{}}\n",
2024-11-20 18:26:45 +01:00
"\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": [],
2024-11-21 19:04:57 +01:00
"execution_count": 14
2024-11-20 18:26:45 +01:00
},
{
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T17:13:00.753631Z",
"start_time": "2024-11-21T17:12:55.591434Z"
2024-11-20 18:26:45 +01:00
}
},
"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"
]
}
],
2024-11-21 19:04:57 +01:00
"execution_count": 15
2024-11-20 18:26:45 +01:00
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Gitea",
"id": "10b7d087cc42e560"
},
{
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T17:32:12.611234Z",
"start_time": "2024-11-21T17:32:12.601080Z"
2024-11-20 18:26:45 +01:00
}
},
"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",
2024-11-20 20:16:29 +01:00
" GITEA__database__NAME: {MYSQL_DATABASE}\n",
" GITEA__database__USER: {MYSQL_USER}\n",
" GITEA__database__PASSWD: {MYSQL_PASSWORD}\n",
2024-11-20 18:26:45 +01:00
"\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": [],
2024-11-21 19:04:57 +01:00
"execution_count": 21
2024-11-20 18:26:45 +01:00
},
{
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-21T17:32:24.838419Z",
"start_time": "2024-11-21T17:32:18.056718Z"
2024-11-20 18:26:45 +01:00
}
},
"cell_type": "code",
"source": [
"gitea_server = client.servers.create(\n",
2024-11-21 19:04:57 +01:00
" name=f\"{GITEA_SERVER_NAME}\",\n",
2024-11-20 18:26:45 +01:00
" 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"
]
}
],
2024-11-21 19:04:57 +01:00
"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
2024-11-20 18:26:45 +01:00
},
{
2024-11-20 20:16:29 +01:00
"metadata": {
"ExecuteTime": {
2024-11-21 19:04:57 +01:00
"end_time": "2024-11-20T20:01:24.112181Z",
"start_time": "2024-11-20T20:01:22.339871Z"
2024-11-20 20:16:29 +01:00
}
},
2024-11-20 18:26:45 +01:00
"cell_type": "code",
"source": "delete_all_servers()",
"id": "ec5f64ced2185519",
2024-11-20 20:16:29 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-11-21 19:04:57 +01:00
"Usuwanie serwera s464863-db (65.21.149.208): running\n",
"Usuwanie serwera s464863-gitea (95.216.200.81): running\n"
2024-11-20 20:16:29 +01:00
]
}
],
2024-11-21 19:04:57 +01:00
"execution_count": 39
2024-11-20 18:26:45 +01:00
},
{
"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
}