should be working

This commit is contained in:
Aaron Riedel 2022-08-22 11:59:13 +02:00
parent 1f5e7876de
commit 2712b803ce
Signed by: aaron
GPG key ID: 643004654D40D577
2 changed files with 68 additions and 25 deletions

View file

@ -1,2 +1,3 @@
tabulate==0.8.10 tabulate==0.8.10
hcloud==1.18.0 hcloud==1.18.0
rich==12.5.1

View file

@ -1,7 +1,15 @@
#!/bin/python3 #!/bin/python3
import sys import sys
from rich.console import Console
from rich.table import Table
from rich.prompt import Prompt
from rich.progress import Progress
from rich.pretty import pprint
from rich.prompt import Confirm
from os import environ, system, path from os import environ, system, path
from tabulate import tabulate import subprocess
from time import sleep
#from tabulate import tabulate
from hcloud import Client from hcloud import Client
from hcloud.images.domain import Image from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType from hcloud.server_types.domain import ServerType
@ -27,6 +35,7 @@ token_file = open("%s/.token"%path.dirname(__file__), "r")
token = token_file.read().replace("\n","") token = token_file.read().replace("\n","")
token_file.close() token_file.close()
client = Client(token=token) client = Client(token=token)
console = Console() # for rich module
# get volume # get volume
volume = Volume(volume_id) volume = Volume(volume_id)
@ -36,17 +45,16 @@ models = client.server_types.get_all()
# functions # functions
def delete_server(s): def delete_server(s):
print("stop game") with Progress() as progress:
system("ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null shcloud.eu 'su -c \"cd /gameserver/%s && /gameserver/%s/%s stop\" gameserver'"%(server_game, server_game, server_game)) task_stop_game = progress.add_task("[red]Stop Game", total=None)
print("shut down server") subprocess.check_output("ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null shcloud.eu 'su -c \"cd /gameserver/%s && /gameserver/%s/%s stop\" gameserver'"%(server_game, server_game, server_game), shell=True,stderr=subprocess.STDOUT)
progress.update(task_stop_game, completed=1, total=1)
response = s.shutdown() response = s.shutdown()
response.wait_until_finished() track_progress(response.id, response.command)
print("detach volume")
response = client.volumes.detach(volume) response = client.volumes.detach(volume)
response.wait_until_finished() track_progress(response.id, response.command)
print("deleting server...")
response = s.delete() response = s.delete()
response.wait_until_finished() track_progress(response.id, response.command)
def check_servertype(server_type): def check_servertype(server_type):
# return id or None # return id or None
@ -57,11 +65,37 @@ def check_servertype(server_type):
def select_servertype(): def select_servertype():
# return id or None # return id or None
models_list = [] table = Table(title="Server Types Hetzner Cloud")
table.add_column("ID", justify="left", style="grey46", no_wrap=True)
table.add_column("Name", justify="left", style="cyan", no_wrap=True)
table.add_column("Description", justify="left", style="orange1", no_wrap=True)
table.add_column("Cores", justify="left", style="red", no_wrap=True)
table.add_column("Memory", justify="left", style="red", no_wrap=True)
table.add_column("Disk", justify="left", style="blue", no_wrap=True)
table.add_column("Storage Type", justify="center", style="blue", no_wrap=True)
table.add_column("CPU Type", justify="center", style="cyan2", no_wrap=True)
table.add_column("Deprecated", justify="center", style="grey46", no_wrap=True)
for m in models: for m in models:
models_list.append([str(m.id), str(m.name), str(m.description), str(m.cores), str(m.memory), str(m.disk), str(m.storage_type), str(m.cpu_type), str(m.deprecated)]) table.add_row(str(m.id), str(m.name), str(m.description), str(m.cores), str(int(m.memory)) + " GB", str(m.disk) + " GB", str(m.storage_type), str(m.cpu_type), str(m.deprecated))
print(tabulate(models_list, headers=["ID","Name","Description", "Cores", "Memory", "Disk", "Storage Type", "CPU Type", "Deprecated"], tablefmt="rounded_grid")) console.print(table)
return(input("Selection: ")) return(Prompt.ask("Selection: ", default="cpx31"))
def track_progress(a_id, description):
with Progress() as progress:
task = progress.add_task("[cyan]%s..."%description, total=None)
while not progress.finished:
a = client.actions.get_by_id(a_id)
if a.progress != 0:
total = 100
else:
total = None
progress.update(task, completed=int(a.progress), total=total)
if a.finished != None:
progress.stop_task(task)
sleep(0.1)
return
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("shcloud python script") print("shcloud python script")
@ -72,7 +106,16 @@ if len(sys.argv) < 2:
exit(0) exit(0)
if sys.argv[1] == "delete": if sys.argv[1] == "delete":
delete_server(client.servers.get_by_name(server_name)) with Progress() as progress:
task_search = progress.add_task("[green]Search for server", total=None)
s = client.servers.get_by_name(server_name)
progress.update(task_search, completed=1, total=1)
if s == None:
console.print("[red]Server not found")
exit(0)
else:
console.print("[green]Server found")
delete_server(s)
exit(0) exit(0)
if sys.argv[1] == "create": if sys.argv[1] == "create":
@ -82,27 +125,25 @@ if sys.argv[1] == "create":
server_type_id = check_servertype(sys.argv[2]) server_type_id = check_servertype(sys.argv[2])
# if not found force the user to select one # if not found force the user to select one
while server_type_id == None: while server_type_id == None:
print("Server type not found. Please select one:") console.print("[red]Server type not found.")
server_type_id = check_servertype(select_servertype()) server_type_id = check_servertype(select_servertype())
else: else:
while server_type_id == None: while server_type_id == None:
print("Server type not found. Please select one:") console.print("[red]Server type not found.")
server_type_id = check_servertype(select_servertype()) server_type_id = check_servertype(select_servertype())
# check if server already exists # check if server already exists
servers = client.servers.get_all() servers = client.servers.get_all()
for s in servers: for s in servers:
if s.name == server_name: if s.name == server_name:
print("server already there") console.print("[red]server already there")
answer = input("delete? y/N: ") if Confirm.ask("Delete?"):
if answer.lower() == "y":
delete_server(s) delete_server(s)
exit(0) # for now, until I have a way to wait exit(0) # for now, until I have a way to wait
else: else:
print("Server should not be deleted. Aboring...") console.print("[red]Server should not be deleted. Aboring...")
exit(0) exit(0)
# create new server # create new server
print("create new server")
response = client.servers.create( response = client.servers.create(
name=server_name, name=server_name,
server_type=ServerType(id=server_type_id), server_type=ServerType(id=server_type_id),
@ -114,5 +155,6 @@ if sys.argv[1] == "create":
user_data="#!/bin/bash\ncurl -sL ar21.de/shinit.php?GAME=%s\\&VOLUME=%s | bash"%(server_game, volume_id), user_data="#!/bin/bash\ncurl -sL ar21.de/shinit.php?GAME=%s\\&VOLUME=%s | bash"%(server_game, volume_id),
public_net=ServerCreatePublicNetwork(ipv4=PrimaryIP(id=server_ipv4),ipv6=PrimaryIP(id=server_ipv6),enable_ipv4 = True, enable_ipv6 = True) public_net=ServerCreatePublicNetwork(ipv4=PrimaryIP(id=server_ipv4),ipv6=PrimaryIP(id=server_ipv6),enable_ipv4 = True, enable_ipv6 = True)
) )
response.action.wait_until_finished() track_progress(response.action.id, response.action.command)
print("done") for a in response.next_actions:
track_progress(a.id, a.command)