From 60b341cf140acc6e7da26a874006ba069f25a5d1 Mon Sep 17 00:00:00 2001 From: nlitkowski Date: Sat, 27 Nov 2021 13:44:57 +0100 Subject: [PATCH] Add scripts creating db and gitea --- .gitignore | 2 + README.md | 1 + cloud_init_db | 50 ++++++++++++++++++++++ cloud_init_gitea | 42 +++++++++++++++++++ constants.py | 17 ++++++++ deploy.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++ deploy.sh | 2 + 7 files changed, 219 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 cloud_init_db create mode 100644 cloud_init_gitea create mode 100644 constants.py create mode 100644 deploy.py create mode 100644 deploy.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..743942b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +api_key +ssh_key.pub diff --git a/README.md b/README.md new file mode 100644 index 0000000..46f7728 --- /dev/null +++ b/README.md @@ -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') \ No newline at end of file diff --git a/cloud_init_db b/cloud_init_db new file mode 100644 index 0000000..04ade5d --- /dev/null +++ b/cloud_init_db @@ -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 diff --git a/cloud_init_gitea b/cloud_init_gitea new file mode 100644 index 0000000..847e694 --- /dev/null +++ b/cloud_init_gitea @@ -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/* diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..620d78d --- /dev/null +++ b/constants.py @@ -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" diff --git a/deploy.py b/deploy.py new file mode 100644 index 0000000..46b7123 --- /dev/null +++ b/deploy.py @@ -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() diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..f7f3906 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,2 @@ +pip3 install hcloud +python3 deploy.py \ No newline at end of file