Merge branch 'issue_7' into 'dev'

Issue 7

See merge request crafty-controller/crafty-commander!14
This commit is contained in:
Phillip Tarrant 2020-12-13 17:22:43 +00:00
commit b66f22b12a
5 changed files with 122 additions and 1 deletions

View File

@ -3,6 +3,8 @@ import time
import logging import logging
import sys import sys
import yaml import yaml
import asyncio
import zipfile
from distutils import dir_util from distutils import dir_util
from app.classes.shared.helpers import helper from app.classes.shared.helpers import helper
@ -228,6 +230,13 @@ class Controller:
return False return False
return True return True
@staticmethod
def verify_zip_server(zip_path: str):
zip_check = helper.check_file_exists(zip_path)
if not zip_check:
return False
return True
def import_jar_server(self, server_name: str, server_path: str, server_jar: str, min_mem: int, max_mem: int, port: int): def import_jar_server(self, server_name: str, server_path: str, server_jar: str, min_mem: int, max_mem: int, port: int):
server_id = helper.create_uuid() server_id = helper.create_uuid()
new_server_dir = os.path.join(helper.servers_dir, server_id) new_server_dir = os.path.join(helper.servers_dir, server_id)
@ -244,6 +253,25 @@ class Controller:
server_log_file, server_stop, port) server_log_file, server_stop, port)
return new_id return new_id
def import_zip_server(self, server_name: str, zip_path: str, server_jar: str, min_mem: int, max_mem: int, port: int):
server_id = helper.create_uuid()
new_server_dir = os.path.join(helper.servers_dir, server_id)
if helper.check_file_perms(zip_path):
helper.ensure_dir_exists(new_server_dir)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(new_server_dir)
else:
return "false"
full_jar_path = os.path.join(new_server_dir, server_jar)
server_command = 'java -Xms{}G -Xmx{}G -jar {} nogui'.format(min_mem, max_mem, full_jar_path)
server_log_file = "{}/logs/latest.log".format(new_server_dir)
server_stop = "stop"
new_id = self.register_server(server_name, server_id, new_server_dir, server_command, server_jar,
server_log_file, server_stop, port)
return new_id
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, server_port=25565): 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, server_port=25565):
# put data in the db # put data in the db
new_id = Servers.insert({ new_id = Servers.insert({

View File

@ -45,6 +45,14 @@ class Helpers:
self.passhasher = PasswordHasher() self.passhasher = PasswordHasher()
self.exiting = False self.exiting = False
def check_file_perms(self, path):
try:
fp = open(path, "r").close()
logger.info("{} is readable".format(path))
return True
except PermissionError:
return False
def is_file_older_than_x_days(self, file, days=1): def is_file_older_than_x_days(self, file, days=1):
if self.check_file_exists(file): if self.check_file_exists(file):
file_time = os.path.getmtime(file) file_time = os.path.getmtime(file)

View File

@ -42,7 +42,7 @@ class PanelHandler(BaseHandler):
} }
# if no servers defined, let's go to the build server area # if no servers defined, let's go to the build server area
if page_data['server_stats']['total'] == 0: if page_data['server_stats']['total'] == 0 and page != "error":
self.set_status(301) self.set_status(301)
self.redirect("/server/step1") self.redirect("/server/step1")
return False return False

View File

@ -97,7 +97,16 @@ class ServerHandler(BaseHandler):
return False return False
new_server_id = controller.import_jar_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port) new_server_id = controller.import_jar_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port)
elif import_type == 'import_zip':
good_path = controller.verify_zip_server(import_server_path)
if not good_path:
self.redirect("/panel/error?error=Zip file not found!")
return False
new_server_id = controller.import_zip_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port)
if new_server_id == "false":
self.redirect("/panel/error?error=ZIP file not accessible! You can fix this permissions issue with sudo chown -R crafty:crafty {} And sudo chmod 2775 -R {}".format(import_server_path, import_server_path))
return False
else: else:
# todo: add server type check here and call the correct server add functions if not a jar # 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) new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port)

View File

@ -149,6 +149,82 @@
</div> </div>
</div> </div>
</div> </div>
<div class="col-sm-13 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<h4>Import From Zip File</h4>
<br />
<p class="card-description">
<form method="post">
{% raw xsrf_form_html() %}
<input type="hidden" value="import_zip" name="create_type">
<div class="row">
<div class="col-sm-9">
<h4 class="card-title">Server Details</h4>
<hr>
<div class="col-sm-12">
<div class="form-group">
<label for="server_name">Server Name</label>
<input type="text" class="form-control" id="server_name" name="server_name" value="" placeholder="My Imported Server">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="server">Zip Path <small>Absolute path to your zip file</small></label>
<input type="text" class="form-control" id="server_path" name="server_path" placeholder="/var/opt/server.zip">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="server_jar">Server Jar Name</label>
<input type="text" class="form-control" id="server_jar" name="server_jar" value="" placeholder="paper.jar">
</div>
</div>
</div>
<div class="col-sm-3">
<h4 class="card-title">Quick Settings <small> - Don't worry, you can change these later</small></h4>
<hr>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="min_memory">Min Memory <small> - Size in GB</small></label>
<input type="number" class="form-control" id="min_memory" name="min_memory" value="1" step="0.5" min="0.5">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="max_memory">Max Memory <small> - Size in GB</small></label>
<input type="number" class="form-control" id="max_memory" name="max_memory" value="2" step="0.5" min="0.5">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="port">Server Port <small> - 25565 default</small></label>
<input type="number" class="form-control" id="port" name="port" value="25565" step="1" min="1">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary mr-2" onclick="wait_msg()">Import My Server!</button>
<button type="reset" class="btn btn-danger mr-2">Reset Form</button>
</div>
</div>
</form>
</p>
</div>
</div>
</div>
</div> </div>
{% end %} {% end %}