import server code done

This commit is contained in:
Phillip Tarrant 2020-09-14 16:01:38 -04:00
parent f96649613f
commit 90ca5c151f
4 changed files with 121 additions and 10 deletions

View File

@ -3,6 +3,7 @@ import time
import logging import logging
import sys import sys
import yaml import yaml
from distutils import dir_util
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
@ -215,11 +216,31 @@ class Controller:
new_id = self.register_server(name, server_id, server_dir, server_command, server_file, server_log_file, server_stop) new_id = self.register_server(name, server_id, server_dir, server_command, server_file, server_log_file, server_stop)
return new_id return new_id
# todo - Do import server @staticmethod
def import_server(self): def verify_jar_server( server_path: str, server_jar: str):
print("todo") path_check = helper.check_path_exits(server_path)
jar_check = helper.check_file_exists(os.path.join(server_path, server_jar))
if not path_check or not jar_check:
return False
return True
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): 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()
new_server_dir = os.path.join(helper.servers_dir, server_id)
helper.ensure_dir_exists(new_server_dir)
dir_util.copy_tree(server_path, new_server_dir)
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):
# put data in the db # put data in the db
new_id = Servers.insert({ new_id = Servers.insert({
Servers.server_name: name, Servers.server_name: name,
@ -231,6 +252,7 @@ class Controller:
Servers.auto_start_delay: 10, Servers.auto_start_delay: 10,
Servers.crash_detection: False, Servers.crash_detection: False,
Servers.log_path: server_log_file, Servers.log_path: server_log_file,
Servers.server_port: server_port,
Servers.stop_command: server_stop Servers.stop_command: server_stop
}).execute() }).execute()

View File

@ -84,19 +84,35 @@ class ServerHandler(BaseHandler):
min_mem = bleach.clean(self.get_argument('min_memory', '')) min_mem = bleach.clean(self.get_argument('min_memory', ''))
max_mem = bleach.clean(self.get_argument('max_memory', '')) max_mem = bleach.clean(self.get_argument('max_memory', ''))
port = bleach.clean(self.get_argument('port', '')) port = bleach.clean(self.get_argument('port', ''))
import_type = bleach.clean(self.get_argument('create_type', ''))
import_server_path = bleach.clean(self.get_argument('server_path', ''))
import_server_jar = bleach.clean(self.get_argument('server_jar', ''))
server_parts = server.split("|") server_parts = server.split("|")
# todo: add server type check here and call the correct server add functions if not a jar if import_type == 'import_jar':
new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) good_path = controller.verify_jar_server(import_server_path, import_server_jar)
if not good_path:
self.redirect("/panel/error?error=Server path or Server Jar not found!")
return False
new_server_id = controller.import_jar_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port)
else:
# 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'],
"Created server {} named {}".format(server, server_name), "Created server {} named {}".format(server, server_name),
new_server_id, new_server_id,
self.get_remote_ip()) self.get_remote_ip())
stats.record_stats() else:
self.redirect("/panel/dashboard") logger.error("Unable to create server")
console.error("Unable to create server")
stats.record_stats()
self.redirect("/panel/dashboard")
self.render( self.render(
template, template,

View File

@ -68,7 +68,7 @@
<div class="col-md-12"> <div class="col-md-12">
<div class="input-group"> <div class="input-group">
<div id="virt_console" class="" style="font-size: .8em; padding: 5px 10px; border: 1px solid #383e5d; background-color:#2a2c44;height:500px; overflow: scroll;"></div> <div id="virt_console" class="" style="width: 100%; font-size: .8em; padding: 5px 10px; border: 1px solid #383e5d; background-color:#2a2c44;height:500px; overflow: scroll;"></div>
</div> </div>
<br /> <br />

View File

@ -75,6 +75,79 @@
</div> </div>
</div> </div>
</div> </div>
<div class="col-sm-6 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<h4>Import an Existing Server</h4>
<br />
<p class="card-description">
<form method="post">
{% raw xsrf_form_html() %}
<input type="hidden" value="import_jar" name="create_type">
<div class="row">
<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">Server Path <small>Absolute path to your server</small></label>
<input type="text" class="form-control" id="server_path" name="server_path" placeholder="/var/opt/server">
</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>
<br />
<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-3">
<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-3 offset-1">
<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-3 offset-1">
<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>
</form>
</p>
</div>
</div>
</div>
</div> </div>
</div> </div>