55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
import json
|
|
from hcloud import Client
|
|
from hcloud.networks.domain import NetworkSubnet
|
|
from hcloud.locations.domain import Location
|
|
|
|
|
|
class Client_MS:
|
|
def __init__(self) -> None:
|
|
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()
|
|
|
|
def load_config(self):
|
|
with open('config.json') as json_file:
|
|
return json.load(json_file)
|
|
|
|
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.")
|
|
return self.client.volumes.get_by_name(self.config["volume"])
|
|
else:
|
|
print("VOLUME: Creating volume.")
|
|
return self.client.volumes.create(
|
|
size=10, name=self.config["volume"], location=Location(id="hel1"))
|
|
|
|
|
|
c = Client_MS()
|
|
print(c.client)
|
|
print(c.ssh_key)
|