added resolved task 3.2
This commit is contained in:
commit
e942d036ed
10
README.rm
Normal file
10
README.rm
Normal file
@ -0,0 +1,10 @@
|
||||
## Rozwiązanie do zadania 3.2
|
||||
|
||||
### Uruchomienie rozwiązania:
|
||||
|
||||
#### 1. Dodaj plik `.env` z poszczególnymi sekretami
|
||||
#### 2. uruchom skrypt
|
||||
|
||||
`./deploy.sh`
|
||||
|
||||
#### który zainstaluje zależności i uruchomi domyślny plik pythona tworzący odpowiednie maszyny.
|
182
deploy.py
Normal file
182
deploy.py
Normal file
@ -0,0 +1,182 @@
|
||||
import environ
|
||||
|
||||
from hcloud import Client
|
||||
from hcloud.networks.domain import NetworkSubnet
|
||||
from hcloud.locations.domain import Location
|
||||
from hcloud.images.domain import Image
|
||||
from hcloud.server_types.domain import ServerType
|
||||
|
||||
# Load secrets
|
||||
env = environ.Env(
|
||||
DEBUG=(bool, False)
|
||||
)
|
||||
|
||||
environ.Env.read_env()
|
||||
|
||||
API_KEY = env.str("API_KEY", "api_key")
|
||||
SSH_KEY = env.str("SSH_KEY", "ssh_key")
|
||||
|
||||
# Connect with cloud
|
||||
|
||||
client = Client(
|
||||
token=API_KEY
|
||||
)
|
||||
ssh_key = client.ssh_keys.get_by_name("s434804@home") or None
|
||||
|
||||
print("1. Connected to Hetzner")
|
||||
|
||||
# Create network
|
||||
network_name = "network-2-dawid-w"
|
||||
|
||||
vnet = client.networks.get_by_name(network_name) or None
|
||||
if not vnet:
|
||||
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")
|
||||
]
|
||||
)
|
||||
print(f"2. Network created")
|
||||
|
||||
volume_name = "s434804-volume"
|
||||
|
||||
volume = client.volumes.get_by_name(volume_name) or None
|
||||
if not volume:
|
||||
volume = client.volumes.create(size=10, name=volume_name, location=Location('hel1'))
|
||||
|
||||
print("3. Volume Created")
|
||||
|
||||
# Create database
|
||||
|
||||
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.2: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
|
||||
'''
|
||||
|
||||
|
||||
db_server_name = "s434804-database"
|
||||
db_server = client.servers.get_by_name(db_server_name) or None
|
||||
if not db_server:
|
||||
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
|
||||
)
|
||||
|
||||
print("4. Database Created")
|
||||
|
||||
|
||||
# Create gitea
|
||||
|
||||
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.2: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_name = "s434804-gitea"
|
||||
gitea_server = client.servers.get_by_name(db_server_name) or None
|
||||
if not gitea_server:
|
||||
gitea_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=gitea_cloud_init,
|
||||
)
|
||||
|
||||
print("5. Created gitea")
|
||||
|
||||
|
||||
|
3
deploy.sh
Normal file
3
deploy.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
pip install -r requirements.txt
|
||||
python deploy.py
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
hcloud==1.16.0
|
||||
python-environ==0.4.54
|
Loading…
Reference in New Issue
Block a user