Add scripts creating db and gitea

This commit is contained in:
nlitkowski 2021-11-27 13:44:57 +01:00
commit 60b341cf14
7 changed files with 219 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
api_key
ssh_key.pub

1
README.md Normal file
View File

@ -0,0 +1 @@
Aby zadziałało, należy utworzyć plik z kluczem publicznym ssh (pod nazwą 'ssh_key.pub') oraz z kluczem api (pod nazwą 'api_key')

50
cloud_init_db Normal file
View File

@ -0,0 +1,50 @@
#cloud-config
# lista podstawowych pakietów, które należy zainstalować
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
# tworzymy plik docker-compose.yml
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: notSecureChangeMe
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
- db_data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin
restart: always
# przekierowanie portów zostawione tylko dla przykładu, nie należy tak robić na produkcji
ports:
- "8080:80"
volumes:
db_data: {}
# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych
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

42
cloud_init_gitea Normal file
View File

@ -0,0 +1,42 @@
#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/*

17
constants.py Normal file
View File

@ -0,0 +1,17 @@
API_KEY_FNAME = "api_key"
SSH_KEY_FNAME = "ssh_key.pub"
CLOUD_INIT_DB_FNAME = "cloud_init_db"
CLOUD_INIT_GITEA_FNAME = "cloud_init_gitea"
SSH_KEY_NAME = "s440054_3_2_ssh_key"
IP_RANGE = "10.10.10.0/24"
NETWORK_ZONE = "eu-central"
NETWORK_TYPE = "cloud"
VNET_NAME = "s440054_3_2_vnet"
DB_SERVER_NAME = "s440054_3_2_db"
VOL_NAME = "s440054_3_2_vol"
GITEA_SERVER_NAME = "s440054_3_2_gitea"
MACHINE_TYPE = "cx11"
MACHINE_LOCATION = "hel1"
MACHINE_OS = "ubuntu-20.04"

105
deploy.py Normal file
View File

@ -0,0 +1,105 @@
from hcloud import Client
from hcloud.networks.client import BoundNetwork
from hcloud.servers.client import BoundServer
from hcloud.ssh_keys.client import BoundSSHKey
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
from hcloud.volumes.domain import Volume
from constants import *
def main():
with open(API_KEY_FNAME) as f:
api_key = f.read()
client = Client(api_key)
ssh_key = get_ssh_key(client)
vnet = get_vnet(client)
_ = create_db_server(client, vnet, ssh_key)
_ = create_gitea_server(client, vnet, ssh_key)
def get_ssh_key(client: Client) -> BoundSSHKey:
ssh_key = client.ssh_keys.get_by_name(SSH_KEY_NAME)
if ssh_key is None:
with open(SSH_KEY_FNAME) as f:
ssh_key_pub = f.read()
ssh_key = client.ssh_keys.create(
name=SSH_KEY_NAME, public_key=ssh_key_pub)
print(
f"Klucz SSH: {ssh_key.data_model.name}, {ssh_key.data_model.public_key}")
def get_vnet(client: Client) -> BoundNetwork:
vnet = client.networks.get_by_name(VNET_NAME)
if vnet is None:
vnet = client.networks.create(
name=VNET_NAME,
ip_range=IP_RANGE,
subnets=[
NetworkSubnet(ip_range=IP_RANGE,
network_zone=NETWORK_ZONE, type=NETWORK_TYPE)
]
)
print(
f"Sieć wirtualna: {vnet.data_model.name} ({vnet.data_model.ip_range})")
def create_db_server(client: Client, vnet: BoundNetwork, ssh_key: BoundSSHKey) -> BoundServer:
db_server = client.servers.get_by_name(DB_SERVER_NAME)
if db_server is not None:
print(
f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}")
return db_server
with open(CLOUD_INIT_DB_FNAME) as f:
cloud_init_db = f.read()
db_server_resp = client.servers.create(
name=DB_SERVER_NAME,
server_type=ServerType(MACHINE_TYPE),
image=Image(name=MACHINE_OS),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location(MACHINE_LOCATION),
user_data=cloud_init_db
)
db_server_resp.action.wait_until_finished()
print(f"Tworzenie serwera db: {db_server_resp.action.complete}")
db_server = client.servers.get_by_name(DB_SERVER_NAME)
print(
f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}")
return db_server
def create_gitea_server(client: Client, vnet: BoundNetwork, ssh_key: BoundSSHKey) -> BoundServer:
gitea_server = client.servers.get_by_name(GITEA_SERVER_NAME)
if gitea_server is not None:
print(
f"Serwer: {gitea_server.data_model.name}\n\tpubliczne IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {gitea_server.data_model.private_net[0].ip}")
return gitea_server
with open(CLOUD_INIT_GITEA_FNAME) as f:
cloud_init_gitea = f.read()
gitea_server_resp = client.servers.create(
name=GITEA_SERVER_NAME,
server_type=ServerType(MACHINE_TYPE),
image=Image(name=MACHINE_OS),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location(MACHINE_LOCATION),
user_data=cloud_init_gitea
)
gitea_server_resp.action.wait_until_finished()
print(f"Tworzenie serwera gitea: {gitea_server_resp.action.complete}")
gitea_server = client.servers.get_by_name(GITEA_SERVER_NAME)
print(
f"Serwer: {gitea_server.data_model.name}\n\tpubliczne IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {gitea_server.data_model.private_net[0].ip}")
return gitea_server
if __name__ == '__main__':
main()

2
deploy.sh Normal file
View File

@ -0,0 +1,2 @@
pip3 install hcloud
python3 deploy.py