From f7c7ce7dd3e09a530acebd3971e3392f3d7b8583 Mon Sep 17 00:00:00 2001 From: s478839 Date: Sat, 31 Dec 2022 11:52:43 +0100 Subject: [PATCH] add cloud_init, modify gitea_build --- .gitignore | 1 + cloud-init.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ gitea_build.py | 3 ++- readme.md | 19 ++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 cloud-init.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77b3ac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +token.txt \ No newline at end of file diff --git a/cloud-init.py b/cloud-init.py new file mode 100644 index 0000000..23d2652 --- /dev/null +++ b/cloud-init.py @@ -0,0 +1,69 @@ +from hcloud import Client +from hcloud.images.domain import Image +from hcloud.server_types.domain import ServerType +from hcloud.networks.domain import NetworkSubnet +from hcloud.locations.domain import Location +from pathlib import Path + +with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file: + ssh_pub = file.readline() + +with open("token.txt", "r") as file: + api_token = file.read().strip() + +client = Client(token=api_token) + +PREFIX = "s478839" + + +ssh_name = f"{PREFIX}-pzc-ssh-key" +ssh_key = client.ssh_keys.get_by_name(ssh_name) or None +if not ssh_key: + ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key", public_key=ssh_pub) +print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}") + +vnet_name = f"{PREFIX}-pzc-test-vnet" +vnet = client.networks.get_by_name(vnet_name) or None +if not vnet: + vnet = client.networks.create( + name=f"{PREFIX}-pzc-test-vnet", + 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})") + +cloud_init_vscode=r'''#cloud-config +write_files: + - content: | + curl -fsSL https://code-server.dev/install.sh > /root/install.sh + path: /root/install.sh + owner: root:root + permissions: '755' + +runcmd: + - bash /root/install.sh + - code-server --bind-addr 0.0.0.0:8080 +''' + +vscode_server = client.servers.create( + name=f"{PREFIX}-vscode", + server_type=ServerType("cx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key], + networks=[vnet], + location=Location("hel1"), + user_data=cloud_init_vscode) + +vscode_server.action.wait_until_finished() +print(f"Tworzenie serwera vscode: {vscode_server.action.complete}") + +vscode_server = client.servers.get_by_name(f"{PREFIX}-vscode") +print(f"Serwer: {vscode_server.data_model.name}\n\tpubliczne IP: {vscode_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {vscode_server.data_model.private_net[0].ip}") + +print(f"http://{vscode_server.data_model.public_net.ipv4.ip}:8080") + + + + diff --git a/gitea_build.py b/gitea_build.py index 0418280..2e861da 100644 --- a/gitea_build.py +++ b/gitea_build.py @@ -98,6 +98,7 @@ 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}") +print(f"http://{db_server.data_model.public_net.ipv4.ip}:8080") cloud_init_gitea=r'''#cloud-config # lista podstawowych pakietów, które należy zainstalować @@ -159,6 +160,6 @@ print(f"Tworzenie serwera gitea: {gitea_server.action.complete}") gitea_server = client.servers.get_by_name(f"{PREFIX}-gitea") print(f"Serwer: {gitea_server.data_model.name}\n\tpubliczne IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {gitea_server.data_model.private_net[0].ip}") - +print(f"http://{gitea_server.data_model.public_net.ipv4.ip}:3000") diff --git a/readme.md b/readme.md index 3b2cf36..3806e52 100644 --- a/readme.md +++ b/readme.md @@ -8,4 +8,23 @@ Po sklonowaniu repozytorium do folderu dpzc-hetzner należy wstawić plik token. git clone git@git.wmi.amu.edu.pl:s478839/dpzc-hetzner.git cd dpzc-hetzner sh deploy.sh +``` + +# Zadanie 4 + +## Uruchomienie +```bash +git clone git@git.wmi.amu.edu.pl:s478839/dpzc-hetzner.git +cd dpzc-hetzner +python3 cloud-init.py +``` +## Logowanie do VScode +W polu password należy umieścić hasło uzyskane przez: +```bash +ssh root@ +cat ~/.config/code-server/config.yaml +``` +## Usuwanie serwera +```bash +python3 clean.py ``` \ No newline at end of file