This commit is contained in:
olczig 2023-01-03 22:26:56 +01:00
commit 9126981797
3 changed files with 171 additions and 0 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
1) chmod +x run.sh
2) ./run.sh

165
chmurki.py Normal file
View File

@ -0,0 +1,165 @@
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
YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDdsckgNysKURgUyf5lQ5k+o48dXF1wQcG2BdQNvXox1ri+dGdEG4peVqgRNqlfiVeoE7Qx413v1ZvOA+MPEy6/HveHQVgth0jQpWwuwxA3FhIsMAyJZjXb1oT+/fChR4gk1xkHvulRj5k5uYyJ0heXOGJc30KN3HqB7+iMKs5wf7tjCVOHfiGdQdSVNKs6OKr44MuwLil48kc+fJ+Mj5Zlo10qMUEGTxre8grYTMEUMAEfqM1s8sj+Ru+LNC3OJg2ernDj+bCCFalGj3P3F4pPqXPGNkwFitby3szcYiGPHEWhxypmBhwfsh6FHBfN2nTiwvHa13hb6PanWVbRTmORHbF5AkXaxWL+DbdXFp+3w8xn9nagdICB66eTwIa9y+4H9R/4O1xytxrDe96vEjaTl3jRG/z+OGvyRq6p8fax1ZOc1q0KjRFnr6RrL1p9IcNmsGF5iSYTNn1dmckVOXQa9eFkTj8gbZQhx4aDGF74vAKm1pwfbTrZVmd3XwiPPGUpKq4hrqDLdK5erzjEf6c1tVd7tA2uo7YRfBMuu3aPWqJQiJkjssPdGIPR4A9H6/lS7H5x1KnYC4zYHaiEhst9N5l7Q7RCnHfaqB1oK1NRcYnYD/j1v5hsRdcxzl58KK5WGfFsawArQRuHt5QsAwJTrMbgMUt0yF+GRYTPeGM3mQ== olczig@MacBook-Air-Aleksandra.local"
client = Client(
token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23")
ID = "s444376"
servers = client.servers.get_all()
for s in servers:
if s.data_model.name.startswith(ID):
action = client.servers.delete(s)
print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
vnet_name = f"{ID}-network"
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"Sieć: {vnet.data_model.name} ({vnet.data_model.ip_range})")
ssh_name = f"{ID}-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
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: gitea
MYSQL_DATABASE: gitea
MYSQL_USER: gitea
MYSQL_PASSWORD: gitea
volumes:
- db_data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- "8080:80"
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
'''
db_server = client.servers.create(
name=f"{ID}-database",
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"Serwer bazodanowy: {db_server.action.complete}")
db_server = client.servers.get_by_name(f"{ID}-database")
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_gitea=r'''
#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: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: gitea
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"
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
'''
final_server = client.servers.create(
name=f"{ID}-final",
server_type=ServerType("cx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_gitea
)
final_server.action.wait_until_finished()
print(f"Serwer gitea: {final_server.action.complete}")
final_server = client.servers.get_by_name(f"{ID}-final")
print(f"Serwer: {final_server.data_model.name}\n\tpubliczne IP: {final_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {final_server.data_model.private_net[0].ip}")

4
run.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
pip3 install hcloud
python3 chmurki.py