Added 3.2 solution

This commit is contained in:
AWieczarek 2024-11-20 18:09:07 +01:00
commit b7d1d6d038
6 changed files with 240 additions and 0 deletions

39
README.md Normal file
View File

@ -0,0 +1,39 @@
# s464979-hetzner
W repozytorium znajdują się rozwiązania do zadań 3.2 (+ 3.1) oraz 3.5 z przedmiotu Praktyczne Zastosowania Chmury Obliczeniowej.
## Instrukcja uruchomienia zadania 3.2
1. Należy sklonować repozytorium.
```bash
git clone
```
2. Przejdź do katalogu z zadaniem 3.2.
```bash
cd s464979-hetzner/3.2
```
3. Nadaj uprawnienia do uruchomienia skryptu `deploy.sh`.
```bash
chmod +x deploy.sh
```
4. Uruchom skrypt `deploy.sh`.
```bash
./deploy.sh
```
Program wypisze na ekranie adres IP serwera w postaci:
```
Serwer: 464979-gitea
publiczne IP: {adres_ip}
prywatne IP: {prywatny_adres_ip}
```
Po pomyślnym zakończeniu skryptu, aplikacja będzie dostępna pod adresem `http://{adres_ip}:3000` (uruchomienie i automatyczna konfiguracja serwerów zajmuje około minuty, należy odczekać wskazany czas przed przejściem na wskazany adres).

45
db_cloud_init.yml Normal file
View File

@ -0,0 +1,45 @@
#cloud-config
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
write_files:
- path: /root/docker-compose.yml
content: |
version: '3.9'
services:
db:
image: mysql:5.7
restart: always
ports:
- "10.10.10.2:3306:3306"
environment:
MYSQL_ROOT_PASSWORD: notSecureChangeMe
MYSQL_DATABASE: gitea
MYSQL_USER: gitea
MYSQL_PASSWORD: gitea
volumes:
- db_data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin
restart: always
volumes:
db_data: {}
runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- apt-get update -y
- apt-get install -y docker-ce docker-ce-cli containerd.io
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- systemctl start docker
- systemctl enable docker
- cd /root/ && docker-compose up -d

View File

@ -0,0 +1,34 @@
from hcloud import Client
client = Client(
token="oiA3vdWArVn67e4kHdyIKjDdnGNQGxEJJUI0ln8GrTsZjs10cmyAkqwjC3ufdZC8"
)
PREFIX = "464979"
lbs = client.load_balancers.get_all()
print(f"Usuwanie {len(lbs)} mechanizmów LB")
for s in lbs:
if s.data_model.name.startswith(PREFIX):
action = client.load_balancers.delete(s)
print(f"\tUsuwanie LB {s.data_model.name}: {action}")
servers = client.servers.get_all()
print(f"Usuwanie {len(servers)} serwerów")
for s in servers:
if s.data_model.name.startswith(PREFIX):
action = client.servers.delete(s)
print(f"\tUsuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
vnets = client.networks.get_all()
print(f"Usuwanie {len(vnets)} sieci wirtualnych")
for s in vnets:
if s.data_model.name.startswith(PREFIX):
action = client.networks.delete(s)
print(f"\tUsuwanie sieci wirtualnej {s.name}: {action}")
volumes = client.volumes.get_all()
print(f"Usuwanie {len(volumes)} wolumenów")
for v in volumes:
if s.data_model.name.startswith(PREFIX):
action = client.volumes.delete(v)
print(f"\tUsuwanie wolumenu {v.name}: {action}")

7
deploy.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
chmod +x gitea_config.py
pip install hcloud
python3 gitea_config.py

50
gitea_cloud_init.yml Normal file
View File

@ -0,0 +1,50 @@
#cloud-config
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
write_files:
- path: /root/docker-compose.yml
content: |
version: "3.9"
services:
server:
image: gitea/gitea:latest
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__database__DB_TYPE=mysql
- GITEA__database__HOST="10.10.10.2:3306"
- GITEA__database__NAME=gitea
- GITEA__database__USER=gitea
- GITEA__database__PASSWD=gitea
restart: always
volumes:
- ./gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"
runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- apt-get update -y
- apt-get install -y docker-ce docker-ce-cli containerd.io
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- systemctl start docker
- systemctl enable docker
- cd /root/ && docker-compose up -d

65
gitea_config.py Normal file
View File

@ -0,0 +1,65 @@
from hcloud import Client
from hcloud.networks.domain import NetworkSubnet
from hcloud.locations.domain import Location
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
client = Client(
token="oiA3vdWArVn67e4kHdyIKjDdnGNQGxEJJUI0ln8GrTsZjs10cmyAkqwjC3ufdZC8"
)
PREFIX = "464979"
ssh_key = client.ssh_keys.get_by_name(name="464979-pzc-ssh-key")
print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}")
vnet = client.networks.create(
name=f"{PREFIX}-gitea-net",
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})")
with open("db_cloud_init.yml", 'r', encoding='utf-8') as file:
cloud_init_db = file.read()
db_server = client.servers.create(
name=f"{PREFIX}-db",
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_db
)
db_server.action.wait_until_finished()
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}")
with open("gitea_cloud_init.yml", 'r', encoding='utf-8') as file:
cloud_init_wp1 = file.read()
wp1_server = client.servers.create(
name=f"{PREFIX}-gitea",
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_wp1
)
wp1_server.action.wait_until_finished()
print(f"Tworzenie serwera Gitea: {wp1_server.action.complete}")
wp1_server = client.servers.get_by_name(f"{PREFIX}-gitea")
print(
f"Serwer: {wp1_server.data_model.name}\n\tpubliczne IP: {wp1_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {wp1_server.data_model.private_net[0].ip}")