This commit is contained in:
s444417 2023-01-10 17:38:17 +01:00
commit 08251b41e5
6 changed files with 209 additions and 0 deletions

1
.gitignore vendored Normal file
View File

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

5
README.md Normal file
View File

@ -0,0 +1,5 @@
## 3.1, 3.2:
należy uruchomić skrypt run.sh, uruchomienie pliku del_gitea.py usuwa stworzone serwery
## 3.4
rozwiązanie w pliki cloud-init.yml

11
cloud-init.yml Normal file
View File

@ -0,0 +1,11 @@
#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

14
del_gitea.py Normal file
View File

@ -0,0 +1,14 @@
from hcloud import Client
client = Client(
token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23"
)
PREFIX = "s444417"
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"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")

173
gitea_auto.py Normal file
View File

@ -0,0 +1,173 @@
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
api_token = ""
with open("token.txt", "r") as file:
api_token = file.read().strip()
client = Client(token=api_token)
PREFIX = "s444417"
YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQChkJi01w/rrQ7HICEJYDN/4ldPxorU4umbc8Sw1MRIU2Mz/rds/pd2f+B/PYTjvvtOdZml4SyC4Y8kFoBo4AmRUTxn2yk3g/d1eiDtRzhY1c22+mua6V1dee72EhWAWHaAAa9caChMWV3oP2TljgUEF/Hd7ctMkMxo0Hlh6EVHec1HR6lMIWspYRWjIKPLzknW7WePNgAB5MBDz6+WzokwnULbglUnJ5uy+WrFgWl9tfN7MoNyG1aZkLiWuXvF1HUddAS+gmhs2MEEWeGi3kNewBKQfviAxdCgEKirPra+4ffgLUej7exDzcacxU8AjcK5DLlk3LJr34YTpeV+LMN6M3FB/XVu4vAv9IAyHCOJRer8Vb7gM+ehukgr6HCPrFyCgO3GBh9TvQaBeUajtqjJQ2YHw7D2SFWEfZ6XSx2YSW0NQpfturEHlFLLzN7ohpzSzupe/8yHXlhASSntducXrsXNm1Sz2PdqjDSkoMRZjCzNIxhUS67SZDncOxqsmjE= s444417@MIK"
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=vnet_name,
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})")
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=ssh_name, public_key=YOUR_LOCAL_SSH_PUBKEY)
print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}")
cloud_init_db=r'''
#cloud-config
# lista podstawowych pakietów, które należy zainstalować
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
# tworzymy plik docker-compose.yml
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: 6XigvpPYf0N0MjO7WW5LmwwGgaDFJ
MYSQL_DATABASE: gitea
MYSQL_USER: gitea
MYSQL_PASSWORD: gitea
volumes:
- db_data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin
restart: always
# przekierowanie portów zostawione tylko dla przykładu, nie należy tak robić na produkcji
ports:
- "8080:80"
volumes:
db_data: {}
# instalujemy docker i docker-compose, a następnie uruchamiamy naszą bazę danych
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
'''
db_server = client.servers.create(
name=f"{PREFIX}-db",
server_type=ServerType("cx11"),
image=Image(name="ubuntu-20.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}")
cloud_init_wp1=r'''#cloud-config
# lista podstawowych pakietów, które należy zainstalować
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
# tworzymy plik docker-compose.yml
write_files:
- path: /root/docker-compose.yml
content: |
version: '3.9'
services:
server:
image: gitea/gitea:1.15.6-rootless
environment:
GITEA_database_DB_TYPE: mysql
GITEA_database_HOST: 10.10.10.2:3306
GITEA_database_NAME: gitea
GITEA_database_USER: gitea
GITEA_database_PASSWD: 6XigvpPYf0N0MjO7WW5LmwwGgaDFJ
restart: always
volumes:
- ./data:/root/gitea
- ./config:/root/gitea/config
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
- /mnt/volume:/data
ports:
- "3000:3000"
- "222:22"
# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych
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
'''
db_server = client.servers.create(
name=f"{PREFIX}-gitea",
server_type=ServerType("cx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_wp1
)
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}")

5
run.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
pip install hcloud
python del_gitea.py
python gitea_auto.py