From 44854f8f617d350f3fd07091312f7326847938c6 Mon Sep 17 00:00:00 2001 From: pbiskup Date: Tue, 30 Nov 2021 23:40:06 +0100 Subject: [PATCH] zad 3.2 --- .idea/.gitignore | 8 + .idea/gitea_hetzner.iml | 10 ++ .idea/inspectionProfiles/Project_Default.xml | 15 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + api_key.txt | 0 deploy.py | 157 ++++++++++++++++++ requirements.txt | 1 + ssh.txt | 0 11 files changed, 215 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/gitea_hetzner.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 api_key.txt create mode 100644 deploy.py create mode 100644 requirements.txt create mode 100644 ssh.txt diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/gitea_hetzner.iml b/.idea/gitea_hetzner.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/gitea_hetzner.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..579ee6c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cefde5e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..85abe91 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api_key.txt b/api_key.txt new file mode 100644 index 0000000..e69de29 diff --git a/deploy.py b/deploy.py new file mode 100644 index 0000000..8417093 --- /dev/null +++ b/deploy.py @@ -0,0 +1,157 @@ +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 + +with open("api_key.txt", "r") as file: + api_key = file.read() + +with open("ssh.txt", "r") as file: + ssh_key = file.read() + +client = Client( + token=api_key +) + +ssh_key_name = "piobis@st.amu.edu.pl" +if client.ssh_keys.get_by_name(ssh_key_name): + ssh_key_h = client.ssh_keys.get_by_name(ssh_key_name) +else: + ssh_key_h = client.ssh_keys.create(name=ssh_key_name, public_key=ssh_key) + +network_name = "pb_network" +if client.networks.get_by_name(network_name): + vnet = client.networks.get_by_name(network_name) +else: + 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"Utworzono sieć wirtualną {vnet.data_model.name} ({vnet.data_model.ip_range})") + +volume_name = "pb-volume" +if client.volumes.get_by_name(volume_name): + volume = client.volumes.get_by_name(volume_name) +else: + volume = client.volumes.create(size=10, name=volume_name, location=Location('hel1')) + +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 = "s470609-pb-db" +if client.servers.get_by_name(db_server_name): + db_server = client.servers.get_by_name(db_server_name) +else: + db_server = client.servers.create( + name=db_server_name, + server_type=ServerType("cpx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key_h], + networks=[vnet], + location=Location("hel1"), + user_data=cloud_init_db + ) + +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/* +''' + +gitea_server_name = "s470609-pb-gitea" +if client.servers.get_by_name(gitea_server_name): + gitea_server = client.servers.get_by_name(gitea_server_name) +else: + gitea_server = client.servers.create( + name=gitea_server_name, + server_type=ServerType("cpx11"), + image=Image(name="ubuntu-20.04"), + ssh_keys=[ssh_key_h], + networks=[vnet], + location=Location("hel1"), + user_data=gitea_cloud_init, + volumes=[volume.volume] + ) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c026b1f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +hcloud==1.16.0 diff --git a/ssh.txt b/ssh.txt new file mode 100644 index 0000000..e69de29