diff --git a/hlna.py b/hlna.py index b919ca1..ad4e78d 100755 --- a/hlna.py +++ b/hlna.py @@ -1,21 +1,22 @@ #!/usr/bin/env python3 import os -import time -import threading -from pathlib import Path -from pprint import pprint +import threading import yaml import click import colorama -import click_completion +from pathlib import Path from rcon.source import Client home_dir = Path.home() dir_server = f"{home_dir}/ARK_Servers/" -dir_config = f"{home_dir}/.config/hlna/maps/" +server_dir = dir_server + "ShooterGame/Binaries/Linux/" +dir_unit = f"{home_dir}/.config/systemd/user/" +dir_config = f"{home_dir}/.config/hlna/" +dir_maps = f"{dir_config}maps/" + mods_id = "" listen_server = True @@ -37,7 +38,7 @@ def find_file(path): return arr -list_config = find_file(dir_config) +list_config = find_file(dir_maps) delist_config = find_file(dir_config+"deactivated") @@ -52,12 +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/"): - os.chdir(f"{home_dir}/.config/") - os.mkdir("hlna") - if not os.path.exists(f"{home_dir}/.config/hlna/maps"): - os.chdir(f"{home_dir}/.config/hlna/") - os.mkdir("maps") def check_int(number=""): @@ -74,8 +69,9 @@ def check_int(number=""): create_dir(dir_server) +create_dir(dir_unit) create_dir(dir_config) - +create_dir(dir_maps) @hlna.command(help='Для конфигурирования параметров запускаемого сервера или кластера серверов') def config(list_config=list_config): @@ -83,10 +79,10 @@ def config(list_config=list_config): port_s = [] rcon_p = [] query_p = [] - + cluster_id = "" cluster_dir_override = "" - + count_cluster = check_int("""Укажите требуется ли кластер? 1. Да 2. Нет @@ -105,16 +101,16 @@ def config(list_config=list_config): for i in list_config: data = read_yaml(i) print(f"{i} : {data['map']}") - # id_srv[data['SessionName']] = data['id_server'] + # id_srv[data['SessionName']] = data['id_server'] count_maps = check_int("Укажите количество карт: \n") if count_maps == 0: # 0 возвращает check_int когда, ничего не было введено count_maps = 1 - + for i in range(count_maps): while True: "Проверка на выбор карты из списка" - + # os.system("clear") amount_map = check_int("""Выберите карту из списка указав номер 1. The Island @@ -182,10 +178,10 @@ def config(list_config=list_config): print("Порт уже занят") else: return port - + if list_config: data = read_yaml(list_config[-1]) - + while True: name_server = input("Укажите название Сервера: \n") if name_server == "": @@ -195,7 +191,7 @@ def config(list_config=list_config): while new_name in list_config: new_name = f"{map_s}{str(count)}" count += 1 - + list_config.append(new_name) print(list_config) break @@ -212,12 +208,12 @@ def config(list_config=list_config): port_server = ports(port_s) print("Укажите query порт сервера:\n") query_port = ports(query_p) - + if port_server == 0: port_server = 7777 if query_port == 0: query_port = 27015 - + print("Порт Сервера=", port_server) print("Query Port=", query_port) @@ -234,54 +230,60 @@ def config(list_config=list_config): max_players = 70 systemd_unit_create(cluster_server, map_s, list_config[-1], port_server, query_port, rcon_enabled, rcon_port, adminpassword_server, password_server, max_players, cluster_id, cluster_dir_override) - # yaml_create(cluster_server, map_s, list_config[-1], port_server, query_port, rcon_enabled, rcon_port, adminpassword_server, password_server, max_players, cluster_id, cluster_dir_override) + yaml_create(cluster_server, map_s, list_config[-1], port_server, query_port, rcon_enabled, rcon_port, adminpassword_server, password_server, max_players, cluster_id, cluster_dir_override) -def systemd_unit_create(cluster_server, map_s, name_server, port_server, query_port, rcon_enabled, rcon_port, adminpassword_server, password_server, max_players, cluster_id, cluster_dir_override) -unit = f'''[Unit] -Description=ARK: Survival Evolved dedicated server +def yaml_create(cluster_server, map_s, name_server, port_server, query_port, rcon_enabled, rcon_port, adminpassword_server, password_server, max_players, cluster_id, cluster_dir_override): + settings_hlna = [ + { + 'map' : map_s, + 'Cluster' : cluster_server, + 'SessionName' : name_server, + 'Port' : port_server, + 'QueryPort' : query_port, + 'RCONEnabled' : rcon_enabled, + 'RCONPort' : rcon_port, + 'ServerAdminPassword': adminpassword_server, + 'ServerPassword' : password_server, + 'MaxPlayers' : max_players, + 'ModsId' : mods_id, + 'Listen' : listen_server, + 'ServerPath' : dir_server, + 'clusterid' : cluster_id, + 'clusterdir' : cluster_dir_override + } + ] + with open(dir_maps + f"{name_server}", 'w') as yamlfile: + yaml.dump(settings_hlna, yamlfile) + print(colorama.Fore.GREEN + "Конфиг создан" + colorama.Style.RESET_ALL) + + +def systemd_unit_create(cluster_server, map_s, name_server, port_server, query_port, rcon_enabled, rcon_port, adminpassword_server, password_server, max_players, cluster_id, cluster_dir_override): + if not cluster_server: + ntff = "" + else: + ntff = "-NoTransferFromFiltering" + unit_file = f'{dir_unit}ARK_{name_server}.service' + unit_text = f'''[Unit] +Description=ARK: Survival Evolved dedicated server - {map_s} Wants=network-online.target After=syslog.target network.target nss-lookup.target network-online.target [Service] -ExecStartPre=/home/steam/steamcmd +login anonymous +force_install_dir /home/steam/servers/ark +app_update 376030 +quit -ExecStart=/home/steam/servers/ark/ShooterGame/Binaries/Linux/ShooterGameServer TheIsland?listen?SessionName= -server -log -WorkingDirectory=/home/steam/servers/ark/ShooterGame/Binaries/Linux +ExecStartPre=/usr/bin/steamcmd +force_install_dir {dir_server} +login anonymous +app_update 376030 +quit +TimeoutStartSec=60 +ExecStart={server_dir}ShooterGameServer {map_s}?{listen_server}?SessionName={name_server}?Port={port_server}?QueryPort={query_port}?RCONEnabled={rcon_enabled}?RCONPort={rcon_port}?ServerAdminPassword={adminpassword_server}?MaxPlayers={max_players} -clusterid={cluster_id}-ClusterDirOverride={cluster_dir_override} {ntff} +WorkingDirectory={server_dir} LimitNOFILE=100000 ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s INT $MAINPID -User=steam -Group=steam [Install] -WantedBy=multi-user.target\ +WantedBy=multi-user.target ''' -with open(unit, 'w') as systemd_unit: - systemd_unit.write(unit) -os.system('systemctl daemon-reload') -# def yaml_create(cluster_server, map_s, name_server, port_server, query_port, rcon_enabled, rcon_port, adminpassword_server, password_server, max_players, cluster_id, cluster_dir_override): -# settings_hlna = [ -# { -# 'map' : map_s, -# 'Cluster' : cluster_server, -# 'SessionName' : name_server, -# 'Port' : port_server, -# 'QueryPort' : query_port, -# 'RCONEnabled' : rcon_enabled, -# 'RCONPort' : rcon_port, -# 'ServerAdminPassword': adminpassword_server, -# 'ServerPassword' : password_server, -# 'MaxPlayers' : max_players, -# 'ModsId' : mods_id, -# 'Listen' : listen_server, -# 'ServerPath' : dir_server, -# 'clusterid' : cluster_id, -# 'clusterdir' : cluster_dir_override -# } -# ] -# with open(dir_config + f"{name_server}", 'w') as yamlfile: -# yaml.dump(settings_hlna, yamlfile) -# print(colorama.Fore.GREEN + "Конфиг создан" + colorama.Style.RESET_ALL) + with open(unit_file, 'w') as systemd_unit: + systemd_unit.write(unit_text) + os.system('systemctl daemon-reload') def test_mod_install(): @@ -420,7 +422,6 @@ def start(m, b, name_server=list_config): else: names_serverstart = choose_map(names_serverstart) - server_dir = dir_server + "ShooterGame/Binaries/Linux/" print_line("Валидация файлов сервера") x = os.system(f"steamcmd +force_install_dir {dir_server} +login anonymous +app_update 376030 +quit") os.chdir(server_dir) @@ -448,12 +449,12 @@ def start(m, b, name_server=list_config): else: print("Ни одной карты не установлено") - + def read_yaml(name_server, flag=True): # Читаем конфиги активных или неактивных карт в зависимости от флага if flag: - dirs = f"{home_dir}/.config/hlna/maps/{name_server}" + dirs = f"{dir_maps}{name_server}" else: dirs = f"{home_dir}/.config/hlna/deactivated/{name_server}" with open(dirs, "r") as yamlfile: @@ -512,7 +513,5 @@ def zero(x=""): return "" -# if __name__ == 'hlna': -# input = zero if __name__ == '__main__': hlna()