Refactor and add import_helpers.

Working jar imports
This commit is contained in:
amcmanu3 2022-08-17 17:22:03 -04:00
parent 65c8ca680b
commit eea5e329af
6 changed files with 79 additions and 39 deletions

View File

@ -190,7 +190,7 @@ class ServerJars:
try:
with open(path, "wb") as output:
shutil.copyfileobj(r.raw, output)
ServersController.finish_download(server_id)
ServersController.finish_import(server_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(
@ -203,7 +203,7 @@ class ServerJars:
return True
except Exception as e:
logger.error(f"Unable to save jar to {path} due to error:{e}")
ServersController.finish_download(server_id)
ServersController.finish_import(server_id)
server_users = PermissionsServers.get_server_user_list(server_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(

View File

@ -0,0 +1,67 @@
from genericpath import isdir
import shutil
import os
import time
import logging
import threading
from app.classes.controllers.server_perms_controller import PermissionsServers
from app.classes.controllers.servers_controller import ServersController
from app.classes.shared.helpers import Helpers
from app.classes.shared.file_helpers import FileHelpers
logger = logging.getLogger(__name__)
class ImportHelpers:
allowed_quotes = ['"', "'", "`"]
def __init__(self, helper, file_helper):
self.file_helper: FileHelpers = file_helper
self.helper: Helpers = helper
def import_jar_server(self, server_path, new_server_dir, port, new_id):
import_thread = threading.Thread(
target=self.import_threaded_jar_server,
daemon=True,
args=(server_path, new_server_dir, port, new_id),
name=f"{new_id}_import",
)
import_thread.start()
def import_threaded_jar_server(self, server_path, new_server_dir, port, new_id):
for item in os.listdir(server_path):
if not item == "db_stats":
try:
if os.path.isdir(os.path.join(server_path, item)):
FileHelpers.copy_dir(
os.path.join(server_path, item),
os.path.join(new_server_dir, item),
)
else:
FileHelpers.copy_file(
os.path.join(server_path, item),
os.path.join(new_server_dir, item),
)
except shutil.Error as ex:
logger.error(f"Server import failed with error: {ex}")
has_properties = False
for item in os.listdir(new_server_dir):
if str(item) == "server.properties":
has_properties = True
if not has_properties:
logger.info(
f"No server.properties found on zip file import. "
f"Creating one with port selection of {str(port)}"
)
with open(
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
) as file:
file.write(f"server-port={port}")
file.close()
time.sleep(5)
ServersController.finish_import(new_id)
server_users = PermissionsServers.get_server_user_list(new_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})

View File

@ -30,15 +30,17 @@ from app.classes.shared.authentication import Authentication
from app.classes.shared.console import Console
from app.classes.shared.helpers import Helpers
from app.classes.shared.file_helpers import FileHelpers
from app.classes.shared.import_helper import ImportHelpers
from app.classes.minecraft.serverjars import ServerJars
logger = logging.getLogger(__name__)
class Controller:
def __init__(self, database, helper, file_helper):
def __init__(self, database, helper, file_helper, import_helper):
self.helper: Helpers = helper
self.file_helper: FileHelpers = file_helper
self.import_helper: ImportHelpers = import_helper
self.server_jars: ServerJars = ServerJars(helper)
self.users_helper: HelperUsers = HelperUsers(database, self.helper)
self.roles_helper: HelperRoles = HelperRoles(database)
@ -569,41 +571,10 @@ class Controller:
port,
server_type="minecraft-java",
)
import_thread = threading.Thread(
name=f"server_import-{server_id}",
target=self.import_threaded_jar_server,
daemon=True,
args=(server_path, new_server_dir, port, new_id),
)
self.servers.set_import(new_id)
import_thread.start()
ServersController.set_import(new_id)
self.import_helper.import_jar_server(server_path, new_server_dir, port, new_id)
return new_id
def import_threaded_jar_server(self, server_path, new_server_dir, port, new_id):
try:
FileHelpers.copy_dir(server_path, new_server_dir, True)
except shutil.Error as ex:
logger.error(f"Server import failed with error: {ex}")
has_properties = False
for item in os.listdir(new_server_dir):
if str(item) == "server.properties":
has_properties = True
if not has_properties:
logger.info(
f"No server.properties found on zip file import. "
f"Creating one with port selection of {str(port)}"
)
with open(
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
) as file:
file.write(f"server-port={port}")
file.close()
self.servers.finish_import(new_id)
server_users = PermissionsServers.get_server_user_list(new_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})
def import_zip_server(
self,
server_name: str,

View File

@ -193,9 +193,9 @@
<a data-id="{{server['server_data']['server_id']}}" class="" title="{{
translate('dashboard', 'delay-explained' , data['lang'])}}">{{ translate('dashboard', 'starting',
data['lang']) }}</i></a>
{% elif server['stats']['downloading']%}
{% elif server['stats']['importing']%}
<a data-id="{{server['server_data']['server_id']}}" class=""><i class="fa fa-spinner fa-spin"></i>
{{ translate('serverTerm', 'downloading',
{{ translate('serverTerm', 'importing',
data['lang']) }}</a>
{% else %}
<a data-id="{{server['server_data']['server_id']}}" class="play_button" data-toggle="tooltip"

View File

@ -14,6 +14,7 @@ from app.classes.shared.import3 import Import3
from app.classes.shared.console import Console
from app.classes.shared.helpers import Helpers
from app.classes.models.users import HelperUsers
from app.classes.shared.import_helper import ImportHelpers
console = Console()
helper = Helpers()
@ -135,8 +136,9 @@ if __name__ == "__main__":
else:
Console.debug("Existing install detected")
file_helper = FileHelpers(helper)
import_helper = ImportHelpers(helper, file_helper)
# now the tables are created, we can load the tasks_manager and server controller
controller = Controller(database, helper, file_helper)
controller = Controller(database, helper, file_helper, import_helper)
import3 = Import3(helper, controller)
tasks_manager = TasksManager(helper, controller)
tasks_manager.start_webserver()