From f88d3828c363f16eb0b2cb3e5a25e5a5d1a891ac Mon Sep 17 00:00:00 2001 From: Aaron Riedel Date: Tue, 15 Apr 2025 18:05:36 +0200 Subject: [PATCH] update to allow multiple games --- .woodpecker.yaml | 2 +- requirements.txt | 2 +- shcloud.py | 125 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 38 deletions(-) diff --git a/.woodpecker.yaml b/.woodpecker.yaml index d20c5d0..666b28e 100644 --- a/.woodpecker.yaml +++ b/.woodpecker.yaml @@ -6,7 +6,7 @@ steps: TOKEN: from_secret: AWX_TOKEN commands: - - "curl -k -X POST -H \"Authorization: Bearer $TOKEN\" https://awx-aaron.apps.yolokube.de/api/v2/job_templates/13/launch/" + - "curl -k -X POST -H \"Authorization: Bearer $TOKEN\" https://awx-aaron.apps.yolokube.de/api/v2/job_templates/16/launch/" when: - event: push branch: main diff --git a/requirements.txt b/requirements.txt index 5d0bf57..adcc8dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ tabulate==0.9.0 hcloud==2.4.0 -rich==13.9.4 \ No newline at end of file +rich==13.9.4 diff --git a/shcloud.py b/shcloud.py index e039ef3..49c186f 100755 --- a/shcloud.py +++ b/shcloud.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/env python3 import sys from rich.console import Console from rich.table import Table @@ -9,7 +9,8 @@ from rich.prompt import Confirm from os import environ, system, path import subprocess from time import sleep -#from tabulate import tabulate +import argparse + from hcloud import Client from hcloud.images.domain import Image from hcloud.server_types.domain import ServerType @@ -21,21 +22,26 @@ from hcloud.servers.domain import ServerCreatePublicNetwork from hcloud.locations.domain import Location server_name = "lgsm-1" -server_game = "sfserver" -#server_type = "cx11" +server_game = "fctrserver" +server_type = "ccx13" server_type_id = None server_key = 6513932 server_image = 114690387 server_ipv4 = 11737045 server_ipv6 = 11737053 -volume_id = 11742041 +volume_id = 102426010 +game_choices = ["fctrserver", "sfserver", "cs2server"] +backup_paths = { + "sfserver": "/gameserver/home/.config/Epic/FactoryGame/Saved/SaveGames/server/*", + "fctrserver": "/gameserver/fctrserver/serverfiles/save1.zip", +} # 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 = open("%s/.token" % path.dirname(__file__), "r") +token = token_file.read().replace("\n", "") token_file.close() client = Client(token=token) -console = Console() # for rich module +console = Console() # for rich module≈ # get volume volume = Volume(volume_id) @@ -43,16 +49,51 @@ volume = Volume(volume_id) # get server types models = client.server_types.get_all() +# parse arguments +parser = argparse.ArgumentParser() +parser.add_argument("action", type=str, help="create or delete") +parser.add_argument("-t", "--type", help="server type") +parser.add_argument( + "-g", "--game", help="game", choices=game_choices, default=server_game +) +args = parser.parse_args() + + # functions def delete_server(s): with Progress() as progress: + check_connection = progress.add_task( + "[green]Check connection to server", total=None + ) + subprocess.check_output( + f"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null shcloud.eu whoami", + shell=True, + stderr=subprocess.STDOUT, + ) + progress.update(check_connection, completed=1, total=1) 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) + 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) + # backup game files - task_backup = progress.add_task("[red]Backup files", total=None) - subprocess.check_output("scp shcloud.eu:/gameserver/home/gameserver/.config/Epic/FactoryGame/Saved/SaveGames/server/* /root/backup/sfserver/", shell=True,stderr=subprocess.STDOUT) - progress.update(task_backup, completed=1, total=1) + if server_game in backup_paths: + task_backup = progress.add_task("[red]Backup files", total=None) + subprocess.check_output( + f"mkdir -p /root/backup/{server_game}", + shell=True, + stderr=subprocess.STDOUT, + ) + subprocess.check_output( + f"scp shcloud.eu:{backup_paths[server_game]} /root/backup/{server_game}/", + shell=True, + stderr=subprocess.STDOUT, + ) + progress.update(task_backup, completed=1, total=1) response = s.shutdown() track_progress(response.id, response.command) response = client.volumes.detach(volume) @@ -60,12 +101,14 @@ def delete_server(s): response = s.delete() track_progress(response.id, response.command) + 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) + return m.id + return None + def select_servertype(): # return id or None @@ -77,21 +120,32 @@ def select_servertype(): 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("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("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) for m in models: - #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", str(m.cpu_type) , format(float(m.prices[0]["price_hourly"]["gross"]), '.2f') + " €", format(float(m.prices[0]["price_monthly"]["gross"]), '.2f') + " €") + # 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", + str(m.cpu_type), + format(float(m.prices[0]["price_hourly"]["gross"]), ".2f") + " €", + format(float(m.prices[0]["price_monthly"]["gross"]), ".2f") + " €", + ) console.print(table) - return(Prompt.ask("Selection: ", default="ccx23")) + return Prompt.ask("Selection: ", default=server_type) + def track_progress(a_id, description): with Progress() as progress: - task = progress.add_task("[cyan]%s..."%description, total=None) + 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: @@ -104,15 +158,8 @@ def track_progress(a_id, description): sleep(0.1) return -if len(sys.argv) < 2: - print("shcloud python script") - print("") - print("usage:") - print("shcloud.py create ") - print("shcloud.py delete") - exit(0) -if sys.argv[1] == "delete": +if args.action == "delete": with Progress() as progress: task_search = progress.add_task("[green]Search for server", total=None) s = client.servers.get_by_name(server_name) @@ -125,11 +172,11 @@ if sys.argv[1] == "delete": delete_server(s) exit(0) -if sys.argv[1] == "create": +if args.action == "create": # check if server model was selected - if len(sys.argv) > 2: + if args.type: # get server models - server_type_id = check_servertype(sys.argv[2]) + server_type_id = check_servertype(args.type) # if not found force the user to select one while server_type_id == None: console.print("[red]Server type not found.") @@ -145,7 +192,7 @@ if sys.argv[1] == "create": console.print("[red]server already there") if Confirm.ask("Delete?"): 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: console.print("[red]Server should not be deleted. Aboring...") exit(0) @@ -158,10 +205,16 @@ if sys.argv[1] == "create": ssh_keys=[SSHKey(id=server_key)], location=Location(name="fsn1"), volumes=[Volume(id=volume_id)], - labels={"game" : server_game}, - 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) + labels={"game": server_game}, + 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, + ), ) track_progress(response.action.id, response.action.command) for a in response.next_actions: - track_progress(a.id, a.command) \ No newline at end of file + track_progress(a.id, a.command)