From 448d2d5209854002ea535b50a64b5c44f328efc8 Mon Sep 17 00:00:00 2001 From: Maciej Sobkowiak Date: Sun, 28 Nov 2021 21:42:25 +0100 Subject: [PATCH] Readme update --- README.md | 4 +++- config_base.json | 13 ++++++++++++ deploy.py | 51 +++++++++++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 config_base.json diff --git a/README.md b/README.md index 0df0bb6..eb07220 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -Init \ No newline at end of file +1. Instalacja paczek komendą "pip3 install -r requirements.txt" +2. Uzupełnienie pliku config_base.json wartościami token i ssh_public +3. Uruchomienie skryptu komendą "python3 deploy.py config_base.json" \ No newline at end of file diff --git a/config_base.json b/config_base.json new file mode 100644 index 0000000..2879eda --- /dev/null +++ b/config_base.json @@ -0,0 +1,13 @@ +{ + "ssh_name": "s434784", + "network": "ms-network-1", + "network-ip": "10.10.10.0/24", + "volume": "ms-volume", + "db-server-name": "ms-gitea-db", + "gitea-server-name": "ms-gitea", + "server-type": "cx11", + "server-image": "ubuntu-20.04", + "location": "hel1", + "token": "", + "ssh_public": "" +} \ No newline at end of file diff --git a/deploy.py b/deploy.py index 90264da..7addb8f 100644 --- a/deploy.py +++ b/deploy.py @@ -1,5 +1,6 @@ import json import time +import sys from hcloud import Client from hcloud.networks.domain import NetworkSubnet from hcloud.locations.domain import Location @@ -8,22 +9,28 @@ from hcloud.server_types.domain import ServerType class Client_MS: - def __init__(self) -> None: - self.config = self.load_config() - self.client = Client(token=self.config["token"]) + def __init__(self, json_config) -> None: + self.config = self.load_config(json_config) + + self.client = self.get_client() + self.ssh_key = self.get_ssh() self.network = self.create_network() self.volume = self.create_volume() - self.server_db = None - self.server_gitea = None - servers = self.client.servers.get_all() - print(servers[0]) + self.server_db = self.create_db_server() + self.server_gitea = self.create_gitea_server() - def load_config(self): - with open('config.json') as json_file: + def load_config(self, json_config): + with open(json_config) as json_file: return json.load(json_file) + def get_client(self): + try: + return Client(token=self.config["token"]) + except: + return None + def get_ssh(self): if(self.client.ssh_keys.get_by_name(self.config["ssh_name"])): print("SSH_KEY: Key exists.") @@ -50,11 +57,15 @@ class Client_MS: def create_volume(self): if(self.client.volumes.get_by_name(self.config["volume"])): print("VOLUME: Volume exists.") - return self.client.volumes.get_by_name(self.config["volume"]) + volume = self.client.volumes.get_by_name(self.config["volume"]) + return volume else: print("VOLUME: Creating volume.") - return self.client.volumes.create( + volume = self.client.volumes.create( size=10, name=self.config["volume"], location=Location(id=self.config["location"])) + volume.action.wait_until_finished() + time.sleep(5) + return volume def create_db_server(self): with open("db.yml", "r") as f: @@ -87,6 +98,7 @@ class Client_MS: self.config["gitea-server-name"]) return self.server_gitea else: + print("GITEA: Creating server.") self.server_gitea = self.client.servers.create( name=self.config["gitea-server-name"], server_type=ServerType(self.config["server-type"]), @@ -102,7 +114,8 @@ class Client_MS: def delete_all(self): self.network.delete() print("NETWORK: Deleted.") - self.volume.detach() + w = self.volume.detach() + w.wait_until_finished() self.volume.delete() print("VOLUME: Deleted.") self.server_db.delete() @@ -111,12 +124,10 @@ class Client_MS: print("GITEA: Deleted.") -def main(): - c = Client_MS() - c.create_db_server() - c.create_gitea_server() - # c.delete_all() - - if __name__ == '__main__': - main() + if(len(sys.argv) != 2): + print("Podaj ścieżkę do pliku konfiguracyjnego.") + else: + c = Client_MS(sys.argv[1]) + # time.sleep(10) + # c.delete_all()