164 lines
5.1 KiB
Python
164 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
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
|
|
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"
|
|
db_name = "s444501-gitea-db"
|
|
gitea_name = "s444501-gitea-server"
|
|
|
|
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_raw)
|
|
|
|
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")
|
|
]
|
|
)
|
|
|
|
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=db_name,
|
|
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"
|
|
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:
|
|
- /mnt/s444501-gitea/data:/var/lib/gitea
|
|
- /mnt/s444501-gitea/config:/etc/gitea
|
|
- /etc/timezone:/etc/timezone:ro
|
|
- /etc/localtime:/etc/localtime:ro
|
|
ports:
|
|
- "3000:3000"
|
|
- "2222:2222"
|
|
'''
|
|
runcmd = f'''
|
|
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
|
|
- sudo mkfs.ext4 -F /dev/disk/by-id/scsi-0HC_Volume_{volume.volume.id}
|
|
- mkdir /mnt/{volume_name}
|
|
- mount -o discard,defaults /dev/disk/by-id/scsi-0HC_Volume_{volume.volume.id} /mnt/{volume_name}
|
|
- echo "/dev/disk/by-id/scsi-0HC_Volume_{volume.volume.id} /mnt/{volume_name} ext4 discard,nofail,defaults 0 0" >> /etc/fstab
|
|
- mkdir /mnt/{volume_name}/data
|
|
- mkdir /mnt/{volume_name}/config
|
|
- sudo chown 1000:1000 /mnt/{volume_name}/config/ /mnt/{volume_name}/data
|
|
- docker-compose up -d
|
|
'''
|
|
|
|
cloud_init_gitea = cloud_init_gitea + runcmd
|
|
|
|
gitea_server = client.servers.create(
|
|
name=gitea_name,
|
|
server_type=ServerType("cpx11"),
|
|
image=Image(name="ubuntu-22.04"),
|
|
ssh_keys=[ssh_key],
|
|
networks=[vnet],
|
|
volumes=[volume.volume],
|
|
automount=True, # automount nie działa z poziomu skryptu a działa z przeglądarki
|
|
location=Location("hel1"),
|
|
user_data=cloud_init_gitea
|
|
)
|
|
|
|
gitea_server.action.wait_until_finished()
|
|
print(f"Tworzenie serwera gitea: {gitea_server.action.complete}")
|
|
|
|
print(f"Serwer dostępny pod adresem http://{gitea_server.server.data_model.public_net.ipv4.ip}:3000")
|
|
print("Uruchumienie GITEA może zająć 2-3 minuty") |