2022-08-21 13:45:45 +02:00
|
|
|
#!/bin/python3
|
|
|
|
import sys
|
2022-08-22 11:59:13 +02:00
|
|
|
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
|
2022-08-21 13:45:45 +02:00
|
|
|
from os import environ, system, path
|
2022-08-22 11:59:13 +02:00
|
|
|
import subprocess
|
|
|
|
from time import sleep
|
|
|
|
#from tabulate import tabulate
|
2022-08-21 13:45:45 +02:00
|
|
|
from hcloud import Client
|
|
|
|
from hcloud.images.domain import Image
|
|
|
|
from hcloud.server_types.domain import ServerType
|
|
|
|
from hcloud.ssh_keys.domain import SSHKey
|
|
|
|
from hcloud.datacenters.domain import Datacenter
|
|
|
|
from hcloud.primary_ips.domain import PrimaryIP
|
|
|
|
from hcloud.volumes.domain import Volume
|
|
|
|
from hcloud.servers.domain import ServerCreatePublicNetwork
|
|
|
|
from hcloud.locations.domain import Location
|
|
|
|
|
|
|
|
server_name = "lgsm-1"
|
2022-08-22 06:21:37 +02:00
|
|
|
server_game = "sfserver"
|
2022-08-21 13:45:45 +02:00
|
|
|
#server_type = "cx11"
|
|
|
|
server_type_id = None
|
|
|
|
server_key = 6513932
|
|
|
|
server_image = 45557056
|
|
|
|
server_ipv4 = 11737045
|
|
|
|
server_ipv6 = 11737053
|
2022-08-22 06:21:37 +02:00
|
|
|
volume_id = 11742041
|
2022-08-21 13:45:45 +02:00
|
|
|
|
|
|
|
# please put the token in a file named .token in the same dir as the python script
|
|
|
|
token_file = open("%s/.token"%path.dirname(__file__), "r")
|
|
|
|
token = token_file.read().replace("\n","")
|
|
|
|
token_file.close()
|
|
|
|
client = Client(token=token)
|
2022-08-22 11:59:13 +02:00
|
|
|
console = Console() # for rich module
|
2022-08-21 13:45:45 +02:00
|
|
|
|
|
|
|
# get volume
|
|
|
|
volume = Volume(volume_id)
|
|
|
|
|
|
|
|
# get server types
|
|
|
|
models = client.server_types.get_all()
|
|
|
|
|
|
|
|
# functions
|
|
|
|
def delete_server(s):
|
2022-08-22 11:59:13 +02:00
|
|
|
with Progress() as progress:
|
|
|
|
task_stop_game = progress.add_task("[red]Stop Game", total=None)
|
|
|
|
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)
|
2022-08-22 06:21:37 +02:00
|
|
|
response = s.shutdown()
|
2022-08-22 11:59:13 +02:00
|
|
|
track_progress(response.id, response.command)
|
2022-08-21 13:45:45 +02:00
|
|
|
response = client.volumes.detach(volume)
|
2022-08-22 11:59:13 +02:00
|
|
|
track_progress(response.id, response.command)
|
2022-08-21 13:45:45 +02:00
|
|
|
response = s.delete()
|
2022-08-22 11:59:13 +02:00
|
|
|
track_progress(response.id, response.command)
|
2022-08-21 13:45:45 +02:00
|
|
|
|
|
|
|
def check_servertype(server_type):
|
|
|
|
# return id or None
|
|
|
|
for m in models:
|
|
|
|
if m.name == server_type and m.deprecated == False:
|
|
|
|
return(m.id)
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
def select_servertype():
|
|
|
|
# return id or None
|
2022-08-22 11:59:13 +02:00
|
|
|
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)
|
2022-08-22 16:33:22 +02:00
|
|
|
#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)
|
|
|
|
table.add_column("Hourly", justify="left", style="cyan2", no_wrap=True)
|
|
|
|
table.add_column("Monthly", justify="left", style="cyan2", no_wrap=True)
|
2022-08-22 11:59:13 +02:00
|
|
|
|
2022-08-21 13:45:45 +02:00
|
|
|
for m in models:
|
2022-08-22 16:33:22 +02:00
|
|
|
if str(m.cpu_type) == "shared":
|
|
|
|
table.add_row(str(m.id), str(m.name), str(m.description), str(m.cores), str(int(m.memory)) + " GB", str(m.disk) + " GB", format(float(m.prices[0]["price_hourly"]["gross"]), '.2f') + " €", format(float(m.prices[0]["price_monthly"]["gross"]), '.2f') + " €")
|
2022-08-22 11:59:13 +02:00
|
|
|
console.print(table)
|
|
|
|
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
|
2022-08-21 13:45:45 +02:00
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("shcloud python script")
|
|
|
|
print("")
|
|
|
|
print("usage:")
|
|
|
|
print("shcloud.py create <server_model>")
|
|
|
|
print("shcloud.py delete")
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
if sys.argv[1] == "delete":
|
2022-08-22 11:59:13 +02:00
|
|
|
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:
|
2022-08-22 16:33:22 +02:00
|
|
|
console.print("[red]:heavy_multiplication_x: Server not found")
|
2022-08-22 11:59:13 +02:00
|
|
|
exit(0)
|
|
|
|
else:
|
2022-08-22 16:33:22 +02:00
|
|
|
console.print("[green]:heavy_check_mark: Server found")
|
2022-08-22 11:59:13 +02:00
|
|
|
delete_server(s)
|
|
|
|
exit(0)
|
2022-08-21 13:45:45 +02:00
|
|
|
|
|
|
|
if sys.argv[1] == "create":
|
|
|
|
# check if server model was selected
|
|
|
|
if len(sys.argv) > 2:
|
|
|
|
# get server models
|
|
|
|
server_type_id = check_servertype(sys.argv[2])
|
|
|
|
# if not found force the user to select one
|
|
|
|
while server_type_id == None:
|
2022-08-22 11:59:13 +02:00
|
|
|
console.print("[red]Server type not found.")
|
2022-08-21 13:45:45 +02:00
|
|
|
server_type_id = check_servertype(select_servertype())
|
|
|
|
else:
|
|
|
|
while server_type_id == None:
|
2022-08-22 11:59:13 +02:00
|
|
|
console.print("[red]Server type not found.")
|
2022-08-21 13:45:45 +02:00
|
|
|
server_type_id = check_servertype(select_servertype())
|
|
|
|
# check if server already exists
|
|
|
|
servers = client.servers.get_all()
|
|
|
|
for s in servers:
|
|
|
|
if s.name == server_name:
|
2022-08-22 11:59:13 +02:00
|
|
|
console.print("[red]server already there")
|
|
|
|
if Confirm.ask("Delete?"):
|
2022-08-21 13:45:45 +02:00
|
|
|
delete_server(s)
|
|
|
|
exit(0) # for now, until I have a way to wait
|
|
|
|
else:
|
2022-08-22 11:59:13 +02:00
|
|
|
console.print("[red]Server should not be deleted. Aboring...")
|
2022-08-21 13:45:45 +02:00
|
|
|
exit(0)
|
|
|
|
|
|
|
|
# create new server
|
|
|
|
response = client.servers.create(
|
|
|
|
name=server_name,
|
|
|
|
server_type=ServerType(id=server_type_id),
|
|
|
|
image=Image(id=server_image),
|
|
|
|
ssh_keys=[SSHKey(id=server_key)],
|
|
|
|
location=Location(name="fsn1"),
|
|
|
|
volumes=[Volume(id=volume_id)],
|
2022-08-22 06:21:37 +02:00
|
|
|
labels={"game" : server_game},
|
|
|
|
user_data="#!/bin/bash\ncurl -sL ar21.de/shinit.php?GAME=%s\\&VOLUME=%s | bash"%(server_game, volume_id),
|
2022-08-21 13:45:45 +02:00
|
|
|
public_net=ServerCreatePublicNetwork(ipv4=PrimaryIP(id=server_ipv4),ipv6=PrimaryIP(id=server_ipv6),enable_ipv4 = True, enable_ipv6 = True)
|
|
|
|
)
|
2022-08-22 11:59:13 +02:00
|
|
|
track_progress(response.action.id, response.action.command)
|
|
|
|
for a in response.next_actions:
|
|
|
|
track_progress(a.id, a.command)
|