This commit is contained in:
Krystian Wasilewski 2022-12-05 11:10:18 +01:00
commit de1891f9b1

162
main.py Normal file
View File

@ -0,0 +1,162 @@
from hcloud import Client
from hcloud.images.domain import Image
from hcloud.networks.domain import NetworkSubnet
from hcloud.locations.domain import Location
from hcloud.server_types.domain import ServerType
client = Client(token="")
ssh_raw = ""
ssh_name = ssh_raw.split()[2]
network_name = "s444501-gitea"
try:
client.ssh_keys.delete(client.ssh_keys.get_by_name(ssh_name))
except AttributeError:
pass
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_raw)
try:
client.networks.delete(client.networks.get_by_name(network_name))
except AttributeError:
pass
vnet = client.networks.create(
name=network_name,
ip_range="10.10.10.0/24",
subnets=[
NetworkSubnet(ip_range="10.10.10.0/24", network_zone="eu-central", type="cloud")
]
)
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: "2"
services:
db:
image: postgres:14
restart: always
ports:
- "10.10.10.2:5432:5432"
environment:
- POSTGRES_USER=gitea
- POSTGRES_PASSWORD=gitea
- POSTGRES_DB=gitea
volumes:
- ./postgres:/var/lib/postgresql/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="s444501-gitea-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}")
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: "2"
volumes:
gitea-data:
driver: local
gitea-config:
driver: local
services:
server:
image: gitea/gitea:1.17.3-rootless
environment:
- GITEA__server__DOMAIN=${DOMAIN}
- GITEA__database__DB_TYPE=postgres
- GITEA__database__HOST="10.10.10.2:5432"
- GITEA__database__NAME=gitea
- GITEA__database__USER=gitea
- GITEA__database__PASSWD=gitea
restart: always
volumes:
- gitea-data:/var/lib/gitea
- gitea-config:/etc/gitea
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "2222:2222"
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/
- IP=$(hostname -I | cut -d ' ' -f 1)
- echo "DOMAIN=$IP" >> .env
- docker-compose up -d
'''
gitea_server = client.servers.create(
name="s444501-gitea-server",
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_gitea
)
gitea_server.action.wait_until_finished()
print(f"Tworzenie serwera gitea: {gitea_server.action.complete}")
# servers = client.servers.get_all()
# print(f"Usuwanie {len(servers)} serwerów")
# for s in servers:
# action = client.servers.delete(s)
# print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")