Add Task 3.2 solution

This commit is contained in:
michalzareba 2021-11-30 20:01:33 +01:00
parent e8fff73ef9
commit 7ea83b7ac2
3 changed files with 149 additions and 1 deletions

View File

@ -1,3 +1,23 @@
# DPZC-Zad-3-2
Rozwiązanie zadania 3.2 z DPZC
Aby uruchomić zadanie:
1. Zainstaluj zależności:
````bash
pip install -r requirements.txt
````
2. Dodaj zmienne środowiskowe:
```bash
export HETZNER_ACCESS_TOKEN={hetzner token}
export SSH_KEY_NAME={SSH_KEY_NAME}
export DB_SERVER_NAME={databse server name}
export GITEA_SERVER_NAME={gitea server name}
```
3. Uruchom skrypt:
```bash
python main.py
```

127
main.py Normal file
View File

@ -0,0 +1,127 @@
import os
from hcloud import Client
from hcloud.locations.domain import Location
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
hetzner_token = os.environ['HETZNER_ACCESS_TOKEN']
ssh_key_name = os.environ["SSH_KEY_NAME"]
db_server_name = os.environ["DB_SERVER_NAME"]
gitea_server_name = os.environ["GITEA_SERVER_NAME"]
client = Client(
token=hetzner_token
)
ssh_key = client.ssh_keys.get_by_name(ssh_key_name)
vnet = client.networks.get_by_name('S430705-network2')
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.3: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
'''
gitea_cloud_init = 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.3: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
- chmod a+w /mnt/*
'''
db_server = client.servers.create(
name=db_server_name,
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_db,
)
gitea_server = client.servers.create(
name=gitea_server_name,
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=gitea_cloud_init,
)

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
hcloud==1.16.0