Merge branch 'dev' into feature/pretzel-users-roles-manager

This commit is contained in:
Zedifus 2022-08-27 17:25:49 +01:00
commit 1b4777fbab
15 changed files with 292 additions and 135 deletions

View File

@ -1,11 +1,11 @@
# Changelog
## --- [4.0.11] - 2022/TBD
### New features
TBD
- Add server import status indicators ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/433))
### Bug fixes
TBD
### Tweaks
TBD
- Make imports threaded ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/433))
### Lang
TBD
<br><br>

View File

@ -110,19 +110,19 @@ class ServersController(metaclass=Singleton):
return ret
@staticmethod
def set_download(server_id):
def set_import(server_id):
srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.set_download()
return srv.stats_helper.set_import()
@staticmethod
def finish_download(server_id):
def finish_import(server_id):
srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.finish_download()
return srv.stats_helper.finish_import()
@staticmethod
def get_download_status(server_id):
def get_import_status(server_id):
server = ServersController().get_server_instance_by_id(server_id)
return server.stats_helper.get_download_status()
return server.stats_helper.get_import_status()
def remove_server(self, server_id):
roles_list = PermissionsServers.get_roles_from_server(server_id)

View File

@ -175,7 +175,7 @@ class ServerJars:
# we submit a db update for it's stats.
while True:
try:
ServersController.set_download(server_id)
ServersController.set_import(server_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(
user, "send_start_reload", {}
@ -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

@ -53,7 +53,7 @@ class ServerStats(Model):
waiting_start = BooleanField(default=False)
first_run = BooleanField(default=True)
crashed = BooleanField(default=False)
downloading = BooleanField(default=False)
importing = BooleanField(default=False)
class Meta:
table_name = "server_stats"
@ -207,26 +207,26 @@ class HelperServerStats:
ServerStats.server_id == self.server_id
).execute(self.database)
def set_download(self):
def set_import(self):
# self.select_database(self.server_id)
ServerStats.update(downloading=True).where(
ServerStats.update(importing=True).where(
ServerStats.server_id == self.server_id
).execute(self.database)
def finish_download(self):
def finish_import(self):
# self.select_database(self.server_id)
ServerStats.update(downloading=False).where(
ServerStats.update(importing=False).where(
ServerStats.server_id == self.server_id
).execute(self.database)
def get_download_status(self):
def get_import_status(self):
# self.select_database(self.server_id)
download_status = (
import_status = (
ServerStats.select()
.where(ServerStats.server_id == self.server_id)
.get(self.database)
)
return download_status.downloading
return import_status.importing
def server_crash_reset(self):
if self.server_id is None:
@ -249,7 +249,6 @@ class HelperServerStats:
def set_update(self, value):
if self.server_id is None:
return
# self.select_database(self.server_id)
try:
# Checks if server even exists

View File

@ -27,10 +27,15 @@ class FileHelpers:
FileHelpers.del_dirs(sub)
else:
# Delete file if it is a file:
sub.unlink()
# This removes the top-level folder:
path.rmdir()
try:
sub.unlink()
except:
logger.error(f"Unable to delete file {sub}")
try:
# This removes the top-level folder:
path.rmdir()
except:
logger.error("Unable to remove top level")
return True
@staticmethod

View File

@ -0,0 +1,216 @@
import os
import time
import shutil
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", {})
def import_java_zip_server(self, temp_dir, new_server_dir, port, new_id):
import_thread = threading.Thread(
target=self.import_threaded_java_zip_server,
daemon=True,
args=(temp_dir, new_server_dir, port, new_id),
name=f"{new_id}_java_zip_import",
)
import_thread.start()
def import_threaded_java_zip_server(self, temp_dir, new_server_dir, port, new_id):
has_properties = False
# extracts archive to temp directory
for item in os.listdir(temp_dir):
if str(item) == "server.properties":
has_properties = True
try:
if not os.path.isdir(os.path.join(temp_dir, item)):
FileHelpers.move_file(
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
)
else:
if item != "db_stats":
FileHelpers.move_dir(
os.path.join(temp_dir, item),
os.path.join(new_server_dir, item),
)
except Exception as ex:
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
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()
server_users = PermissionsServers.get_server_user_list(new_id)
ServersController.finish_import(new_id)
for user in server_users:
self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {})
# deletes temp dir
FileHelpers.del_dirs(temp_dir)
def import_bedrock_server(
self, server_path, new_server_dir, port, full_jar_path, new_id
):
import_thread = threading.Thread(
target=self.import_threaded_bedrock_server,
daemon=True,
args=(server_path, new_server_dir, port, full_jar_path, new_id),
name=f"{new_id}_bedrock_import",
)
import_thread.start()
def import_threaded_bedrock_server(
self, server_path, new_server_dir, port, full_jar_path, 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()
if os.name != "nt":
if Helpers.check_file_exists(full_jar_path):
os.chmod(full_jar_path, 0o2760)
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", {})
def import_bedrock_zip_server(
self, temp_dir, new_server_dir, full_jar_path, port, new_id
):
import_thread = threading.Thread(
target=self.import_threaded_bedrock_zip_server,
daemon=True,
args=(temp_dir, new_server_dir, full_jar_path, port, new_id),
name=f"{new_id}_bedrock_import",
)
import_thread.start()
def import_threaded_bedrock_zip_server(
self, temp_dir, new_server_dir, full_jar_path, port, new_id
):
has_properties = False
# extracts archive to temp directory
for item in os.listdir(temp_dir):
if str(item) == "server.properties":
has_properties = True
try:
if not os.path.isdir(os.path.join(temp_dir, item)):
FileHelpers.move_file(
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
)
else:
if item != "db_stats":
FileHelpers.move_dir(
os.path.join(temp_dir, item),
os.path.join(new_server_dir, item),
)
except Exception as ex:
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
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()
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", {})
if os.name != "nt":
if Helpers.check_file_exists(full_jar_path):
os.chmod(full_jar_path, 0o2760)
# deletes temp dir
FileHelpers.del_dirs(temp_dir)

View File

@ -28,15 +28,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)
@ -543,25 +545,6 @@ class Controller:
Helpers.ensure_dir_exists(new_server_dir)
Helpers.ensure_dir_exists(backup_path)
server_path = Helpers.get_os_understandable_path(server_path)
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()
full_jar_path = os.path.join(new_server_dir, server_jar)
@ -593,6 +576,8 @@ class Controller:
user_id,
server_type="minecraft-java",
)
ServersController.set_import(new_id)
self.import_helper.import_jar_server(server_path, new_server_dir, port, new_id)
return new_id
def import_zip_server(
@ -617,32 +602,6 @@ class Controller:
temp_dir = Helpers.get_os_understandable_path(zip_path)
Helpers.ensure_dir_exists(new_server_dir)
Helpers.ensure_dir_exists(backup_path)
has_properties = False
# extracts archive to temp directory
for item in os.listdir(temp_dir):
if str(item) == "server.properties":
has_properties = True
try:
if not os.path.isdir(os.path.join(temp_dir, item)):
FileHelpers.move_file(
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
)
else:
FileHelpers.move_dir(
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
)
except Exception as ex:
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
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()
full_jar_path = os.path.join(new_server_dir, server_jar)
@ -675,6 +634,10 @@ class Controller:
user_id,
server_type="minecraft-java",
)
ServersController.set_import(new_id)
self.import_helper.import_java_zip_server(
temp_dir, new_server_dir, port, new_id
)
return new_id
# **********************************************************************************
@ -701,25 +664,6 @@ class Controller:
Helpers.ensure_dir_exists(new_server_dir)
Helpers.ensure_dir_exists(backup_path)
server_path = Helpers.get_os_understandable_path(server_path)
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()
full_jar_path = os.path.join(new_server_dir, server_exe)
@ -744,9 +688,10 @@ class Controller:
user_id,
server_type="minecraft-bedrock",
)
if os.name != "nt":
if Helpers.check_file_exists(full_jar_path):
os.chmod(full_jar_path, 0o2760)
ServersController.set_import(new_id)
self.import_helper.import_bedrock_server(
server_path, new_server_dir, port, full_jar_path, new_id
)
return new_id
def import_bedrock_zip_server(
@ -769,32 +714,6 @@ class Controller:
temp_dir = Helpers.get_os_understandable_path(zip_path)
Helpers.ensure_dir_exists(new_server_dir)
Helpers.ensure_dir_exists(backup_path)
has_properties = False
# extracts archive to temp directory
for item in os.listdir(temp_dir):
if str(item) == "server.properties":
has_properties = True
try:
if not os.path.isdir(os.path.join(temp_dir, item)):
FileHelpers.move_file(
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
)
else:
FileHelpers.move_dir(
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
)
except Exception as ex:
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
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()
full_jar_path = os.path.join(new_server_dir, server_exe)
@ -819,6 +738,9 @@ class Controller:
user_id,
server_type="minecraft-bedrock",
)
self.import_helper.import_bedrock_zip_server(
temp_dir, new_server_dir, full_jar_path, port, new_id
)
if os.name != "nt":
if Helpers.check_file_exists(full_jar_path):
os.chmod(full_jar_path, 0o2760)

View File

@ -293,7 +293,7 @@ class ServerInstance:
else:
user_lang = HelperUsers.get_user_lang_by_id(user_id)
if self.stats_helper.get_download_status():
if self.stats_helper.get_import_status():
if user_id:
self.helper.websocket_helper.broadcast_user(
user_id,

View File

@ -453,8 +453,8 @@ class PanelHandler(BaseHandler):
for server in un_used_servers[:]:
if flag == 0:
server["stats"][
"downloading"
] = self.controller.servers.get_download_status(
"importing"
] = self.controller.servers.get_import_status(
str(server["stats"]["server_id"]["server_id"])
)
server["stats"]["crashed"] = self.controller.servers.is_crashed(
@ -571,11 +571,11 @@ class PanelHandler(BaseHandler):
"started": "False",
}
if not self.failed_server:
page_data["downloading"] = self.controller.servers.get_download_status(
page_data["importing"] = self.controller.servers.get_import_status(
server_id
)
else:
page_data["downloading"] = False
page_data["importing"] = False
page_data["server_id"] = server_id
try:
page_data["waiting_start"] = self.controller.servers.get_waiting_start(

View File

@ -411,8 +411,6 @@ class ServerHandler(BaseHandler):
new_server_id,
self.get_remote_ip(),
)
# deletes temp dir
FileHelpers.del_dirs(zip_path)
else:
if len(server_parts) != 3:
self.redirect("/panel/error?error=Invalid server data")
@ -549,8 +547,6 @@ class ServerHandler(BaseHandler):
new_server_id,
self.get_remote_ip(),
)
# deletes temp dir
FileHelpers.del_dirs(zip_path)
else:
if len(server_parts) != 2:
self.redirect("/panel/error?error=Invalid server data")

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"
@ -399,12 +399,12 @@
data['lang']) }}</i></a>
</div>
</div>
{% elif server['stats']['downloading']%}
{% elif server['stats']['importing']%}
<div class="row">
<div class="col-12 px-0">
<a data-id="{{server['server_data']['server_id']}}" class="btn btn-link"><i
class="fa fa-spinner fa-spin"></i>
{{ translate('serverTerm', 'downloading', data['lang']) }}</a>
{{ translate('serverTerm', 'importing', data['lang']) }}</a>
</div>
</div>
{% else %}

View File

@ -65,9 +65,9 @@
<button onclick="" id="restart-btn" style="max-width: 7rem;" class="btn btn-outline-primary m-1 flex-grow-1 disabled">{% raw translate('serverTerm', 'restart', data['lang']) %}</button>
<button onclick="" id="stop-btn" style="max-width: 7rem;" class="btn btn-danger m-1 flex-grow-1 disabled">{{ translate('serverTerm', 'stop', data['lang']) }}</button>
</div>
{% elif data['downloading'] %}
{% elif data['importing'] %}
<div id="control_buttons" class="mt-4 flex-wrap d-flex justify-content-between justify-content-md-center align-items-center px-5 px-md-0" style="visibility: visible">
<button onclick="" id="start-btn" style="max-width: 12rem; white-space: nowrap;" class="btn btn-secondary m-1 flex-grow-1 disabled"><i class="fa fa-spinner fa-spin"></i> {{ translate('serverTerm', 'downloading',
<button onclick="" id="start-btn" style="max-width: 12rem; white-space: nowrap;" class="btn btn-secondary m-1 flex-grow-1 disabled"><i class="fa fa-spinner fa-spin"></i> {{ translate('serverTerm', 'importing',
data['lang']) }}</button>
<button onclick="" id="restart-btn" style="max-width: 7rem;" class="btn btn-outline-primary m-1 flex-grow-1 disabled">{% raw translate('serverTerm', 'restart', data['lang']) %}</button>
<button onclick="" id="stop-btn" style="max-width: 7rem;" class="btn btn-danger m-1 flex-grow-1 disabled">{{ translate('serverTerm', 'stop', data['lang']) }}</button>

View File

@ -0,0 +1,17 @@
# Generated by database migrator
import peewee
def migrate(migrator, database, **kwargs):
migrator.rename_column("server_stats", "downloading", "importing")
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
migrator.rename_column("server_stats", "importing", "downloading")
"""
Write your rollback migrations here.
"""

View File

@ -464,7 +464,7 @@
"serverTerm": {
"commandInput": "Enter your command",
"delay-explained": "The service/agent has recently started and is delaying the start of the minecraft server instance",
"downloading": "Downloading...",
"importing": "Importing...",
"restart": "Restart",
"sendCommand": "Send command",
"start": "Start",

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()