Compare commits

...

5 Commits

Author SHA1 Message Date
Piotr Wrzodak
c1f8167f06 add README.md with 'Getting started' section
add .env.example

add requierements.txt
2024-02-14 23:00:09 +01:00
Piotr Wrzodak
9b04cde31b add main.py 2024-02-14 22:58:48 +01:00
Piotr Wrzodak
bd12c5ddf1 add gitea.yml docker compose file 2024-02-14 22:57:10 +01:00
Piotr Wrzodak
29ad23cf5a add db.yml docker compose file 2024-02-14 22:56:56 +01:00
Piotr Wrzodak
ee028ad4ed delete vscode-cloud-init.yaml 2024-02-14 22:31:38 +01:00
7 changed files with 228 additions and 14 deletions

1
.env.example Normal file
View File

@ -0,0 +1 @@
HETZNER_API_TOKEN=

11
README.md Normal file
View File

@ -0,0 +1,11 @@
## Getting started
0. Install Python.
1. Create virtual environment: `python3 -m venv .venv`.
2. To activate the virtual environment, enter: `source .venv/bin/activate`.
3. Install a list of requirements specified in [requirements.txt](./requirements.txt):
`python -m pip install -r requirements.txt`.
4. Create a .env file `cp .env.example .env` and provide values of envirnoment variables.
5. Run the script: `python main.py`.
> When you're finished with your virtual environment, enter the following command to deactivate it: `deactivate`.

20
db.yml Normal file
View File

@ -0,0 +1,20 @@
version: '3.6'
networks:
gitea:
external: false
services:
db:
image: postgres:9.6
restart: always
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
volumes:
- /mnt/HC_Volume_100342428/Post:/var/lib/postgresql/data
networks:
- gitea
ports:
- '5432:5432'

28
gitea.yml Normal file
View File

@ -0,0 +1,28 @@
version: '3.6'
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:1.13.2
container_name: gitea
environment:
GITEA__database__DB_TYPE: postgres
GITEA__database__HOST: 10.0.0.2:5432
GITEA__database__NAME: db
GITEA__database__USER: user
GITEA__database__PASSWD: password
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- '3000:3000'
- '2222:2222'
- '443:443'
- '80:3000'

166
main.py Normal file
View File

@ -0,0 +1,166 @@
from hcloud import Client
from hcloud.images import Image
from hcloud.networks import Network, NetworkSubnet
from hcloud.volumes import Volume
from hcloud.server_types import ServerType
from hcloud._exceptions import APIException
from hcloud.servers.domain import ServerCreatePublicNetwork
from os import getenv
from dotenv import load_dotenv
load_dotenv()
HETZNER_API_TOKEN = getenv('HETZNER_API_TOKEN')
NETWORK_NAME = "s444510-priv-net"
NETWORK_TYPE = "server"
NETWORK_ZONE = "eu-central"
IP_RANGE = "10.0.0.0/8"
IP_RANGE_SUBNET = "10.0.0.0/24"
LOCATION = "hel1"
VOLUME_NAME = "s444510-volume"
SERVER_TYPE = "cx11"
SERVER_IMAGE = "ubuntu-22.04"
DB_SERVER_NAME = "s444510-db"
GITEA_SERVER_NAME = "s444510-gitea"
print("Create Hetzner client")
client = Client(token=f"{HETZNER_API_TOKEN}")
print("Get location")
location = client.locations.get_by_name(LOCATION)
print("Creating DB volume")
try:
volume = client.volumes.create(size=10, name=VOLUME_NAME, location=location)
volume = volume.volume
print(f"Volume created: {volume.name}")
except APIException:
volume = client.volumes.get_by_name(name=VOLUME_NAME)
print(f"Volume already exists: {volume.name}")
print("Creating Network")
try:
network = client.networks.create(name=NETWORK_NAME, ip_range=IP_RANGE)
print(f"network created: {network.name}")
except APIException:
network = client.networks.get_by_name(name=NETWORK_NAME)
print(f"Network already exists: {network.name}")
print("Creating Subnet")
try:
subnet = client.networks.add_subnet(
network=network, subnet=NetworkSubnet(ip_range=IP_RANGE_SUBNET, network_zone=NETWORK_ZONE, type=NETWORK_TYPE)
)
except APIException:
pass
print("Creating DB server")
try:
response = client.servers.create(
name=DB_SERVER_NAME,
server_type=ServerType(name=SERVER_TYPE),
image=Image(name=SERVER_IMAGE),
location=location,
volumes=[Volume(id=volume.id)],
networks=[Network(id=network.id)],
public_net=ServerCreatePublicNetwork(enable_ipv4=False, enable_ipv6=False),
user_data="""
#cloud-config
package_update: true
package_upgrade: true
groups:
- docker
system_info:
default_user:
groups: [ docker ]
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- unattended-upgrades
runcmd:
- mkdir -p /etc/apt/keyrings
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
- apt-get update
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
- systemctl enable docker
- systemctl start docker
- git clone https://git.wmi.amu.edu.pl/s444510/zpzc-2023.git /zpzc-2023
- docker compose -f /zpzc-2023/db.yml up -d
bootcmd:
- docker compose -f /zpzc-2023/db.yml up -d
final_message: "The system is finally up, after $UPTIME seconds"
"""
)
db_server = response.server
print(f"{db_server.id=} {db_server.name=} {db_server.status=}")
except APIException:
print("Server already exists:")
db_server = client.servers.get_by_name(name=DB_SERVER_NAME)
print("Creating Gitea server")
try:
gitea_server = client.servers.create(
name=GITEA_SERVER_NAME,
server_type=ServerType(name=SERVER_TYPE),
image=Image(name=SERVER_IMAGE),
location=location,
networks=[Network(id=network.id)],
user_data="""
#cloud-config
package_update: true
package_upgrade: true
groups:
- docker
system_info:
default_user:
groups: [ docker ]
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- unattended-upgrades
runcmd:
- mkdir -p /etc/apt/keyrings
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
- apt-get update
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
- systemctl enable docker
- systemctl start docker
- git clone https://git.wmi.amu.edu.pl/s444510/zpzc-2023.git /zpzc-2023
- docker compose -f /zpzc-2023/gitea.yml up -d
bootcmd:
- docker compose -f /zpzc-2023/gitea.yml up -d
final_message: "The system is finally up, after $UPTIME seconds"
"""
)
_gitea_server = gitea_server.server
print(f"{_gitea_server.id=} {_gitea_server.name=} {_gitea_server.status=}, {gitea_server.root_password} ")
except APIException:
gitea_server = client.servers.get_by_name(name=DB_SERVER_NAME)
print(f"Server already exists: {gitea_server.name}")

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
python-dotenv
hcloud

View File

@ -1,14 +0,0 @@
#cloud-config
users:
- name: s444510
groups: users, admin
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- <public_ssh_key>
package_update: true
package_upgrade: true
runcmd:
- curl -fsSL https://code-server.dev/install.sh | sh
- systemctl --user enable --now code-server