add cloud_init, modify gitea_build

This commit is contained in:
s478839 2022-12-31 11:52:43 +01:00
parent 110092fb69
commit f7c7ce7dd3
4 changed files with 91 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
token.txt

69
cloud-init.py Normal file
View File

@ -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")

View File

@ -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")

View File

@ -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@<publiczne_ip>
cat ~/.config/code-server/config.yaml
```
## Usuwanie serwera
```bash
python3 clean.py
```