Refactored the server add outside of serverjars.py (since it's going away). Also made it more flexible since we are doing more than just jars now

This commit is contained in:
Phillip Tarrant 2020-09-10 11:01:57 -04:00
parent e5df031f56
commit 66d7c6f10a
5 changed files with 82 additions and 66 deletions

View File

@ -9,7 +9,7 @@ from datetime import datetime
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
from app.classes.shared.console import console from app.classes.shared.console import console
from app.classes.shared.models import Servers from app.classes.shared.models import Servers
from app.classes.shared.controller import controller # from app.classes.shared.controller import controller
from app.classes.minecraft.server_props import ServerProps from app.classes.minecraft.server_props import ServerProps
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -156,62 +156,5 @@ class ServerJars:
return False return False
# todo: import server
def build_server(self, server: str, version: str, name: str, min_mem: int, max_mem: int, port: int):
server_id = helper.create_uuid()
server_dir = os.path.join(helper.servers_dir, server_id)
jar_file = "{server}-{version}.jar".format(server=server, version=version)
full_jar_path = os.path.join(server_dir, jar_file)
# make the dir - perhaps a UUID?
helper.ensure_dir_exists(server_dir)
# download the jar
self.download_jar(server, version, full_jar_path)
time.sleep(3)
# todo: verify the MD5
# put data in the db
new_id = Servers.insert({
Servers.server_name: name,
Servers.server_uuid: server_id,
Servers.path: server_dir,
Servers.executable: jar_file,
Servers.execution_command: 'java -Xms{}G -Xmx{}G -jar {} nogui'.format(min_mem, max_mem, full_jar_path),
Servers.auto_start: False,
Servers.auto_start_delay: 10,
Servers.crash_detection: False,
Servers.log_path: "{}/logs/latest.log".format(server_dir),
Servers.stop_command: 'stop'
}).execute()
try:
# place a file in the dir saying it's owned by crafty
with open(os.path.join(server_dir, "crafty_managed.txt"), 'w') as f:
f.write("The server in this directory is managed by Crafty Controller.\n Leave this directory/files alone please")
f.close()
# do a eula.txt
with open(os.path.join(server_dir, "eula.txt"), 'w') as f:
f.write("eula=true")
f.close()
# setup server.properties with the port
with open(os.path.join(server_dir, "server.properties"), "w") as f:
f.write("server-port={}".format(port))
f.close()
except Exception as e:
logger.error("Unable to create required server files due to :{}".format(e))
return False
# let's re-init all servers
controller.init_all_servers()
return new_id
server_jar_obj = ServerJars() server_jar_obj = ServerJars()

View File

@ -7,10 +7,11 @@ import yaml
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
from app.classes.shared.console import console from app.classes.shared.console import console
from app.classes.shared.models import db_helper from app.classes.shared.models import db_helper, Servers
from app.classes.shared.server import Server from app.classes.shared.server import Server
from app.classes.minecraft.server_props import ServerProps from app.classes.minecraft.server_props import ServerProps
from app.classes.minecraft.serverjars import server_jar_obj
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -150,5 +151,75 @@ class Controller:
logger.info("All Servers Stopped") logger.info("All Servers Stopped")
console.info("All Servers Stopped") console.info("All Servers Stopped")
def create_jar_server(self, server: str, version: str, name: str, min_mem: int, max_mem: int, port: int):
server_id = helper.create_uuid()
server_dir = os.path.join(helper.servers_dir, server_id)
server_file = "{server}-{version}.jar".format(server=server, version=version)
full_jar_path = os.path.join(server_dir, server_file)
# make the dir - perhaps a UUID?
helper.ensure_dir_exists(server_dir)
try:
# do a eula.txt
with open(os.path.join(server_dir, "eula.txt"), 'w') as f:
f.write("eula=true")
f.close()
# setup server.properties with the port
with open(os.path.join(server_dir, "server.properties"), "w") as f:
f.write("server-port={}".format(port))
f.close()
except Exception as e:
logger.error("Unable to create required server files due to :{}".format(e))
return False
server_command = 'java -Xms{}G -Xmx{}G -jar {} nogui'.format(min_mem, max_mem, full_jar_path)
server_log_file = "{}/logs/latest.log".format(server_dir)
server_stop = "stop"
# download the jar
server_jar_obj.download_jar(server, version, full_jar_path, server_command, server_file)
self.register_server(name, server_id, server_dir, server_command, server_file, server_log_file, server_stop)
# todo - Do import server
def import_server(self):
print("todo")
def register_server(self, name: str, server_id: str, server_dir: str, server_command: str, server_file: str,
server_log_file: str, server_stop: str):
# put data in the db
new_id = Servers.insert({
Servers.server_name: name,
Servers.server_uuid: server_id,
Servers.path: server_dir,
Servers.executable: server_file,
Servers.execution_command: server_command,
Servers.auto_start: False,
Servers.auto_start_delay: 10,
Servers.crash_detection: False,
Servers.log_path: server_log_file,
Servers.stop_command: server_stop
}).execute()
try:
# place a file in the dir saying it's owned by crafty
with open(os.path.join(server_dir, "crafty_managed.txt"), 'w') as f:
f.write(
"The server is managed by Crafty Controller.\n Leave this directory/files alone please")
f.close()
except Exception as e:
logger.error("Unable to create required server files due to :{}".format(e))
return False
# let's re-init all servers
self.init_all_servers()
return new_id
controller = Controller() controller = Controller()

View File

@ -87,7 +87,8 @@ class ServerHandler(BaseHandler):
server_parts = server.split("|") server_parts = server.split("|")
new_server_id = server_jar_obj.build_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) # todo: add server type check here and call the correct server add functions if not a jar
new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port)
if new_server_id: if new_server_id:
db_helper.add_to_audit_log(user_data['user_id'], db_helper.add_to_audit_log(user_data['user_id'],

View File

@ -1,5 +1,5 @@
{ {
"last_refreshed": "09/03/2020, 22:13:41", "last_refreshed": "09/08/2020, 07:56:16",
"servers": { "servers": {
"nukkitx": [ "nukkitx": [
"1.14" "1.14"
@ -171,20 +171,21 @@
"1.1620w13b", "1.1620w13b",
"1.1620w13a", "1.1620w13a",
"1.1620w12a", "1.1620w12a",
"1.16rc1",
"1.1620w11a", "1.1620w11a",
"1.1620w10a", "1.1620w10a",
"1.1620w09a", "1.1620w09a",
"1.1620w08a", "1.1620w08a",
"1.1620w07a", "1.1620w07a",
"1.16.2rc2",
"1.16.2rc1",
"1.1620w06a", "1.1620w06a",
"1.16.2rc2",
"1.16.3rc1",
"1.16.2rc1",
"1.16.2pre3", "1.16.2pre3",
"1.16.2pre2", "1.16.2pre2",
"1.16.2pre1", "1.16.2pre1",
"1.16.2", "1.16.2",
"1.161.16.1", "1.161.16.1",
"1.16rc1",
"1.16.1", "1.16.1",
"1.16", "1.16",
"1.15pre7", "1.15pre7",

View File

@ -157,10 +157,10 @@
</div> </div>
<div class="text-center"> <div class="text-center">
{% if data['server_stats'][0]['running'] %} {% if data['server_stats'][0]['running'] %}
<a href="/panel/command" class="btn btn-sm btn-danger disabled">Delete Server</a><br /> <a class="btn btn-sm btn-danger disabled">Delete Server</a><br />
<small>Please stop the server before deleting it</small> <small>Please stop the server before deleting it</small>
{% else %} {% else %}
<a class="btn btn-sm btn-danger">Delete Server</a> <a href="/panel/command" class="btn btn-sm btn-danger">Delete Server</a>
{% end %} {% end %}
</div> </div>