Добавление начального функционала для systemd

This commit is contained in:
Евгений Храмов 2023-05-06 16:46:07 +03:00
parent 822b20dd87
commit 041532f2ce

107
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):
@ -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=<session_name> -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)
@ -453,7 +454,7 @@ def start(m, b, name_server=list_config):
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()