s407409-pzc-zad-3-2/pyscript.py
Grzegorz Gapiński e875455d60 Dodanie skryptów
Dodanie skryptów bash i python oraz plików biblioteki hcloud
2023-12-31 19:28:18 +01:00

166 lines
5.9 KiB
Python

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="qOpoQdAg5VxZVpNONWbLbJO3xgGqSvkjm4kQV9H1RlnDhL2rI2P8CbII3hl2nyYb"
token = input("Podaj token autoryzacyjny do chmury: ")
)
PREFIX = "s407409"
#YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDWz9H/ju3qc3aplOzQFa+xMkAniCweD/VLxgQ77fE3fOMHoNuze++dxrNfNnUgrHZawqLP1Ftej7X1rKsJOGLrFxefxWLcY3NMCy9W5/tIpMG7ltzhyfjwPW5MPYucWbOCVUbyCRCpClXASWMlaSJoZkQR4Gx4ix1Z89AdbEdfS8umAxwuPIWq2a6S2PuhiwJAN5XHMdoUEp3oNne4jsDclcdYB34jQ2cgldkD8VnBIGEnK5rtVXTzpTxSL/66Hdq1UgWinDbMu+fSG91qO6THMVD6dp2gf94iSRJblTUx/ZqlrsPy9f6bpXLylr+gDuGk9CfIyAWNOHJt4eb48tcur2p67RLD1bJrYCRQniVqMAPeiurestBYXrCm+fznIir3s49iIXyrknIws7rmdade5LQvOW1SpR3XQ1Tv2ou5PHpn7IDHWNE4s0zPyAvCs6N+1JVexJYpQyniwsTGwEUXGwjjpktL9T8EBJW0/b5GV2qSy/AlbXQtpMa1dvT5xqE= s407409@pc19a"
YOUR_LOCAL_SSH_PUBKEY = input("Podaj swój lokalny klucz publiczny ssh: ")
ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key", public_key=YOUR_LOCAL_SSH_PUBKEY)
print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}")
vnet = client.networks.create(
name=f"{PREFIX}-gitea",
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_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: notSecureChangeMe
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: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"
# 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
'''
wp1_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
)
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}")