DPZC-03_IaaS/deploy.py
Maciej Sobkowiak 6e5a0fbe7b Final fixes
2021-11-28 22:04:50 +01:00

154 lines
5.6 KiB
Python

import json
import time
import sys
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
from hcloud.volumes.domain import Volume
class Client_MS:
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 = self.create_db_server()
self.server_gitea = self.create_gitea_server()
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.")
return self.client.ssh_keys.get_by_name(self.config["ssh_name"])
else:
print("SSH_KEY: Creating ssh_key.")
return self.client.ssh_keys.create(name=self.config["ssh_name"], public_key=self.config["ssh_public"])
def create_network(self):
if(self.client.networks.get_by_name(self.config["network"])):
print("NETWORK: Network exists.")
return self.client.networks.get_by_name(self.config["network"])
else:
print("NETWORK: Creating network.")
return self.client.networks.create(
name=self.config["network"],
ip_range=self.config["network-ip"],
subnets=[
NetworkSubnet(ip_range=self.config["network-ip"],
network_zone="eu-central", type="cloud")
]
)
def create_volume(self):
if(self.client.volumes.get_by_name(self.config["volume"])):
print("VOLUME: Volume exists.")
volume = self.client.volumes.get_by_name(self.config["volume"])
return volume
else:
print("VOLUME: Creating volume.")
volume = self.client.volumes.create(
size=10, name=self.config["volume"], location=Location(id=self.config["location"]))
volume.action.wait_until_finished()
return volume.volume
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.")
self.server_db = self.client.servers.get_by_name(
self.config["db-server-name"])
return self.server_db
else:
print("DB: Creating server.")
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=db_yml)
return self.server_db.server
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:
print("GITEA: Creating server.")
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.server
def print_info(self):
print("----- Server info -----")
print(
f"\t{self.server_db.data_model.name}\n\t\tIP: {self.server_db.data_model.public_net.ipv4.ip}")
print(
f"\t{self.server_gitea.data_model.name}\n\t\tIP: {self.server_gitea.data_model.public_net.ipv4.ip}")
print(
f"\nTo access gitea go to: {self.server_gitea.data_model.public_net.ipv4.ip}:3000\n")
def delete_all(self):
self.network.delete()
print("NETWORK: Deleted.")
w = self.volume.detach()
w.wait_until_finished()
self.volume.delete()
print("VOLUME: Deleted.")
self.server_db.delete()
print("DB: Deleted.")
self.server_gitea.delete()
print("GITEA: Deleted.")
if __name__ == '__main__':
if(len(sys.argv) != 2):
print("Podaj ścieżkę do pliku konfiguracyjnego.")
else:
c = Client_MS(sys.argv[1])
c.print_info()
try:
for remaining in range(60, 0, -1):
sys.stdout.write("\r")
sys.stdout.write(
"Deleting servers starts in: {:2d}. Press Ctrl + C to stop.".format(remaining))
sys.stdout.flush()
time.sleep(1)
print("")
except KeyboardInterrupt:
sys.exit()
c.delete_all()