2022-01-04 21:54:09 +01:00
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import requests
|
|
|
|
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.vscode = self.create_vscode_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_vscode_server(self):
|
2022-01-04 22:22:32 +01:00
|
|
|
with open("webservice.yml", "r") as f:
|
2022-01-04 21:54:09 +01:00
|
|
|
vs = f.read()
|
|
|
|
|
2022-01-04 22:22:32 +01:00
|
|
|
if(self.client.servers.get_by_name(self.config["server-name"])):
|
2022-01-04 21:54:09 +01:00
|
|
|
print("Server exists.")
|
|
|
|
self.vscode = self.client.servers.get_by_name(
|
2022-01-04 22:22:32 +01:00
|
|
|
self.config["server-name"])
|
2022-01-04 21:54:09 +01:00
|
|
|
return self.vscode
|
|
|
|
else:
|
|
|
|
print("Creating server.")
|
|
|
|
vscode = self.client.servers.create(
|
2022-01-04 22:22:32 +01:00
|
|
|
name=self.config["server-name"],
|
2022-01-04 21:54:09 +01:00
|
|
|
server_type=ServerType(self.config["server-type"]),
|
|
|
|
image=Image(name=self.config["server-image"]),
|
|
|
|
ssh_keys=[self.ssh_key],
|
|
|
|
location=Location(self.config["location"]),
|
|
|
|
user_data=vs)
|
|
|
|
vscode.action.wait_until_finished()
|
|
|
|
self.vscode = vscode.server
|
|
|
|
return vscode.server
|
|
|
|
|
|
|
|
def print_info(self):
|
|
|
|
print("----- Server info -----")
|
|
|
|
print(
|
|
|
|
f"\t{self.vscode.data_model.name}\n\t\tIP: {self.vscode.data_model.public_net.ipv4.ip}")
|
|
|
|
print(
|
|
|
|
f"\nTo access vscode go to: http:\\\\{self.vscode.data_model.public_net.ipv4.ip}:8080")
|
|
|
|
|
|
|
|
def delete_all(self):
|
|
|
|
self.vscode.delete()
|
|
|
|
print("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()
|
2022-01-04 22:22:32 +01:00
|
|
|
# c.get_password()
|
2022-01-04 21:54:09 +01:00
|
|
|
try:
|
|
|
|
for remaining in range(60, 0, -1):
|
|
|
|
sys.stdout.write("\r")
|
|
|
|
sys.stdout.write(
|
|
|
|
"Deleting server starts in: {:2d}. Press Ctrl + C to stop.".format(remaining))
|
|
|
|
sys.stdout.flush()
|
|
|
|
time.sleep(1)
|
|
|
|
print("")
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
c.delete_all()
|