From c7a75b518a3fe9e64860248800983c20d78ac0da Mon Sep 17 00:00:00 2001 From: xpamych Date: Sun, 19 Mar 2023 17:32:21 +0300 Subject: [PATCH] 1 --- hlna.py | 61 +++++++++++++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/hlna.py b/hlna.py index 085e8d9..b6a257b 100755 --- a/hlna.py +++ b/hlna.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import os +import psutil import threading from pathlib import Path @@ -14,7 +15,8 @@ from rcon.source import Client home_dir = Path.home() dir_server = f"{home_dir}/ARK_Servers/" -dir_config = f"{home_dir}/.config/hlna/maps/" +dir_config = f"{home_dir}/.config/hlna/" +dir_config_maps = f"{dir_config}maps/" mods_id = "" listen_server = True @@ -36,8 +38,8 @@ def find_file(path): return arr -list_config = find_file(dir_config) -delist_config = find_file(dir_config+"deactivated") +list_config = find_file(dir_config_maps) +delist_config = find_file(dir_config_maps+"deactivated") def print_line(text): @@ -51,10 +53,6 @@ def create_dir(directory): """Проверка и создание директории""" if not os.path.exists(directory): os.mkdir(directory) - if not os.path.exists(f"{home_dir}/.config/hlna/maps"): - os.chdir(f"{home_dir}/.config/hlna/") - os.chdir(f"{home_dir}/.config/hlna/") - os.mkdir("maps") def check_int(number=""): @@ -72,6 +70,7 @@ def check_int(number=""): create_dir(dir_server) create_dir(dir_config) +create_dir(dir_config_maps) @hlna.command(help='Для конфигурирования параметров запускаемого сервера или кластера серверов') @@ -254,16 +253,16 @@ def yaml_create(cluster_server, map_s, name_server, port_server, query_port, rco 'clusterdir' : cluster_dir_override } ] - with open(dir_config + f"{name_server}", 'w') as yamlfile: + with open(dir_config_maps + f"{name_server}", 'w') as yamlfile: yaml.dump(settings_hlna, yamlfile) print(colorama.Fore.GREEN + "Конфиг создан" + colorama.Style.RESET_ALL) -def test_mod_install(): - pathTest = f"{dir_server}ShooterGame/Saved/Config/LinuxServer/" - os.chdir(pathTest) - # "Добавить файл в Game.ini, если модов несколько добавляем еще строку ModIDS= [ModInstaller] ModIDS=" c этим не разобрался - os.system("echo ActiveMods=2943454417 >> GameUserSettings.ini") -test_mod_install() +# def test_mod_install(): +# pathTest = f"{dir_server}ShooterGame/Saved/Config/LinuxServer/" +# os.chdir(pathTest) +# # "Добавить файл в Game.ini, если модов несколько добавляем еще строку ModIDS= [ModInstaller] ModIDS=" c этим не разобрался +# os.system("echo ActiveMods=2943454417 >> GameUserSettings.ini") +# test_mod_install() @hlna.command() @click.option("-m", required=True, help="Название Сервера") @@ -272,7 +271,7 @@ def enablemap(m,e): """Тут переписать надо""" m = m.split(",") # m = check_name_map(m, False) - create_dir(dir_config + "deactivated") + create_dir(dir_config_maps + "deactivated") if e == True: port_s = [] query_p = [] @@ -290,7 +289,7 @@ def enablemap(m,e): print("Предлагаем заменить") if data['QueryPort'] in query_p: print("Заменить query port?") - x = os.system(f"mv {dir_config}deactivated/{i} {dir_config + i} 2>> {dir_config}logs") #Добавить текущее время + x = os.system(f"mv {dir_config_maps}deactivated/{i} {dir_config + i} 2>> {dir_config}logs") #Добавить текущее время if x == 0: print(f"Карта активирована - {i}") else: @@ -305,7 +304,7 @@ def enablemap(m,e): if i in delist_config: print(f"Карта {i} уже есть в деактивированных") continue - x = os.system(f"mv {dir_config + i} {dir_config}deactivated/{i} 2>> {dir_config}logs") #Добавить текущее время + x = os.system(f"mv {dir_config_maps + i} {dir_config_maps}deactivated/{i} 2>> {dir_config}logs") #Добавить текущее время if x == 0: print(f"Карта деактивирована - {i}") else: @@ -400,24 +399,18 @@ def start(m,b, name_server=list_config): ntff = "-NoTransferFromFiltering" def starting(i): - pid = os.getpid() - settings = [{data['SessionName']:pid}] - create_dir(f"{home_dir}/.config/hlna/ServerSettings/conf.txt") - with open(f"{home_dir}/.config/hlna/ServerSettings/conf.txt", 'a') as yamlfile: - yaml.dump(settings, yamlfile) - os.system(f"{server_dir}ShooterGameServer {i}?SessionName={data['SessionName']}?Port={data['Port']}?QueryPort={data['QueryPort']}?RCONEnabled={data['RCONEnabled']}?RCONPort={data['RCONPort']}?ServerAdminPassword={data['ServerAdminPassword']}?MaxPlayers={data['MaxPlayers']}?GameModIds={data['ModsId']}?listen={data['Listen']} -clusterid={data['clusterid']} -ClusterDirOverride={data['clusterdir']} {ntff}") - if x == 0: print_line("Запускаем карту" + i) threads = threading.Thread(target=starting, args=(starting_map,)) threads.start() + else: print(f"Карта не запущена, сервер не установлен") else: print("Ни одной карты не установлено") - + """ @hlna.command(help='Для удаления серверов') @click.option('-m', required=True, help="Название карты для удаления") @@ -425,7 +418,7 @@ def delete(m, list_config=list_config): for i in list_config: data = read_yaml(i) if data['map_s'] == m: - + n = input(f"Вы действительно хотите удалить сервер? Это действие отменить невозможно - {i}, y\\N") if n == "y": os.system(f"rm {home_dir}/.config/hlna/{i}") @@ -435,13 +428,12 @@ def delete(m, list_config=list_config): """ @hlna.command() @click.option('-m', default='all', help="Название карты для завершения или all для завершения всех карт") -def shootdown(m): - pids = pid() - print(pids) +def restart(m): shootdown_servers = [] if list_config != []: if m == "all": - shootdown_servers = pids + for i in list_config: + os.system(f"$HOME/Yandex.Disk/git/hln-a/hlna.py rcon DoRestartLevel -m {i}") else: arr = m.split(',') dict_mapname = {} @@ -458,10 +450,7 @@ def shootdown(m): os.system(f"kill {i}") else: print("Ни одной карты не установлено") -def pid(): - with open(f"{home_dir}/.config/hlna/ServerSettings/conf.txt", "r") as yamlfile: - data = yaml.load(yamlfile, Loader=yaml.FullLoader) - return data[0] # возвращаем словарь со всеми значениями + def read_yaml(name_server, flag=True): # Читаем конфиги активных или неактивных карт в зависимости от флага @@ -499,8 +488,8 @@ def choose_map(arr): @click.argument('c', nargs=1) @click.option('-m', required=True, help="Название карты для применения rcon команды") def rcon(m,c): - print_line("Команда: ", c) - print_line("Карты: ", m) + # print_line(f"Команда: , {c}") + # print_line(f"Карты: , {m}") dict_mapname = {} dict_adminpwd = {} for i in list_config: