This commit is contained in:
Maciej Sobkowiak 2021-11-28 20:57:13 +01:00
parent eeeaaa2680
commit 0e5dea34d5
2 changed files with 100 additions and 10 deletions

View File

@ -1,4 +1,5 @@
import json
import time
from hcloud import Client
from hcloud.networks.domain import NetworkSubnet
from hcloud.locations.domain import Location
@ -8,15 +9,16 @@ 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()
self.server_db = None
self.server_gitea = None
servers = self.client.servers.get_all()
print(servers[0])
def load_config(self):
with open('config.json') as json_file:
@ -55,21 +57,66 @@ class Client_MS:
size=10, name=self.config["volume"], location=Location(id=self.config["location"]))
def create_db_server(self):
with open("db.yml", "r") as f:
db_yml = f.read()
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"])
self.server_db = self.client.servers.get_by_name(
self.config["db-server-name"])
return self.server_db
else:
print("DB: Creating server.")
return self.client.servers.create(
self.server_db = 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)
user_data=db_yml)
return self.server_db
def create_gitea_server(self):
with open("gitea.yml", "r") as f:
gitea_yml = f.read()
if(self.client.servers.get_by_name(self.config["gitea-server-name"])):
print("GITEA: Server exists.")
self.server_gitea = self.client.servers.get_by_name(
self.config["gitea-server-name"])
return self.server_gitea
else:
self.server_gitea = self.client.servers.create(
name=self.config["gitea-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=gitea_yml,
volumes=[self.volume]
)
return self.server_gitea
def delete_all(self):
self.network.delete()
print("NETWORK: Deleted.")
self.volume.detach()
self.volume.delete()
print("VOLUME: Deleted.")
self.server_db.delete()
print("DB: Deleted.")
self.server_gitea.delete()
print("GITEA: Deleted.")
def main():
c = Client_MS()
print(c.client)
print(c.ssh_key)
c.create_db_server()
c.create_gitea_server()
# c.delete_all()
if __name__ == '__main__':
main()

43
gitea.yml Normal file
View File

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