diff --git a/db.yml b/db.yml new file mode 100644 index 0000000..509f0b2 --- /dev/null +++ b/db.yml @@ -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: + 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 + # przekierowanie portów zostawione tylko dla przykładu, nie należy tak robić na produkcji + 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 \ No newline at end of file diff --git a/deploy.py b/deploy.py index 3c48fa7..c5da715 100644 --- a/deploy.py +++ b/deploy.py @@ -2,15 +2,21 @@ import json 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 class Client_MS: def __init__(self) -> None: + with open("db.yml", "r") as f: + self.db_yml = f.read() + self.config = self.load_config() self.client = Client(token=self.config["token"]) self.ssh_key = self.get_ssh() self.network = self.create_network() self.volume = self.create_volume() + self.server_db = self.create_db_server() def load_config(self): with open('config.json') as json_file: @@ -46,7 +52,22 @@ class Client_MS: else: print("VOLUME: Creating volume.") return self.client.volumes.create( - size=10, name=self.config["volume"], location=Location(id="hel1")) + size=10, name=self.config["volume"], location=Location(id=self.config["location"])) + + def create_db_server(self): + if(self.client.servers.get_by_name(self.config["db-server-name"])): + print("DB: Server exists.") + return self.client.servers.get_by_name(self.config["db-server-name"]) + else: + print("DB: Creating server.") + return self.client.servers.create( + name=self.config["db-server-name"], + server_type=ServerType(self.config["server-type"]), + image=Image(name=self.config["server-image"]), + ssh_keys=[self.ssh_key], + networks=[self.network], + location=Location(self.config["location"]), + user_data=self.db_yml) c = Client_MS()