chmury_gitea/main.py
2022-12-05 14:59:49 +01:00

199 lines
5.5 KiB
Python

from hcloud import Client, APIException
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
from pathlib import Path
with open('.credentials/token', 'r') as file:
client = Client(token=str(file.readline()).strip())
with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file:
ssh_raw = file.readline()
ssh_name = "ssh-s444501"
network_name = "s444501-gitea"
volume_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")
]
)
try:
v = client.volumes.get_by_name(volume_name)
try:
action = client.volumes.detach(v)
action.wait_until_finished()
except APIException:
pass
client.volumes.delete(v)
except AttributeError:
pass
volume = client.volumes.create(
name=volume_name,
size=10,
format='ext4',
location=Location("hel1")
)
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],
volumes=[volume.volume],
automount=True,
location=Location("hel1"),
user_data=cloud_init_gitea
)
gitea_server.action.wait_until_finished()
print(f"Tworzenie serwera gitea: {gitea_server.action.complete}")
# automount dirty fix
# v = client.volumes.get_by_name(volume_name)
# client.volumes.attach(v, gitea_server.server, automount=True)
# HC_Volume_id
# print(f"Usuwanie volumenu")
# v = client.volumes.get_by_name(volume_name)
# try:
# action = client.volumes.detach(v)
# action.wait_until_finished()
# except APIException:
# pass
#
# client.volumes.delete(v)
#
# 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}")